rdmsr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* rdmsr.c - Read CPU model-specific registers. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2019 Free Software Foundation, Inc.
  5. * Based on gcc/gcc/config/i386/driver-i386.c
  6. *
  7. * GRUB is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GRUB is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/env.h>
  24. #include <grub/command.h>
  25. #include <grub/extcmd.h>
  26. #include <grub/i18n.h>
  27. #include <grub/i386/cpuid.h>
  28. #include <grub/i386/rdmsr.h>
  29. GRUB_MOD_LICENSE("GPLv3+");
  30. static grub_extcmd_t cmd_read;
  31. static const struct grub_arg_option options[] =
  32. {
  33. {0, 'v', 0, N_("Save read value into variable VARNAME."),
  34. N_("VARNAME"), ARG_TYPE_STRING},
  35. {0, 0, 0, 0, 0, 0}
  36. };
  37. static grub_err_t
  38. grub_cmd_msr_read (grub_extcmd_context_t ctxt, int argc, char **argv)
  39. {
  40. grub_uint32_t manufacturer[3], max_cpuid, a, b, c, features, addr;
  41. grub_uint64_t value;
  42. const char *ptr;
  43. char buf[sizeof("1122334455667788")];
  44. /*
  45. * The CPUID instruction should be used to determine whether MSRs
  46. * are supported. (CPUID.01H:EDX[5] = 1)
  47. */
  48. if (! grub_cpu_is_cpuid_supported ())
  49. return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
  50. grub_cpuid (0, max_cpuid, manufacturer[0], manufacturer[2], manufacturer[1]);
  51. if (max_cpuid < 1)
  52. return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
  53. grub_cpuid (1, a, b, c, features);
  54. if (!(features & (1 << 5)))
  55. return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
  56. if (argc != 1)
  57. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  58. grub_errno = GRUB_ERR_NONE;
  59. ptr = argv[0];
  60. addr = grub_strtoul (ptr, &ptr, 0);
  61. if (grub_errno != GRUB_ERR_NONE)
  62. return grub_errno;
  63. if (*ptr != '\0')
  64. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
  65. value = grub_msr_read (addr);
  66. if (ctxt->state[0].set)
  67. {
  68. grub_snprintf (buf, sizeof(buf), "%llx", (unsigned long long) value);
  69. grub_env_set (ctxt->state[0].arg, buf);
  70. }
  71. else
  72. grub_printf ("0x%llx\n", (unsigned long long) value);
  73. return GRUB_ERR_NONE;
  74. }
  75. GRUB_MOD_INIT(rdmsr)
  76. {
  77. cmd_read = grub_register_extcmd ("rdmsr", grub_cmd_msr_read, 0, N_("ADDR"),
  78. N_("Read a CPU model specific register."),
  79. options);
  80. }
  81. GRUB_MOD_FINI(rdmsr)
  82. {
  83. grub_unregister_extcmd (cmd_read);
  84. }