1
0

msr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* msr tests */
  2. #include "libcflat.h"
  3. #include "processor.h"
  4. #include "msr.h"
  5. struct msr_info {
  6. int index;
  7. const char *name;
  8. struct tc {
  9. int valid;
  10. unsigned long long value;
  11. unsigned long long expected;
  12. } val_pairs[20];
  13. };
  14. #define addr_64 0x0000123456789abcULL
  15. struct msr_info msr_info[] =
  16. {
  17. { .index = 0x00000174, .name = "IA32_SYSENTER_CS",
  18. .val_pairs = {{ .valid = 1, .value = 0x1234, .expected = 0x1234}}
  19. },
  20. { .index = 0x00000175, .name = "MSR_IA32_SYSENTER_ESP",
  21. .val_pairs = {{ .valid = 1, .value = addr_64, .expected = addr_64}}
  22. },
  23. { .index = 0x00000176, .name = "IA32_SYSENTER_EIP",
  24. .val_pairs = {{ .valid = 1, .value = addr_64, .expected = addr_64}}
  25. },
  26. { .index = 0x000001a0, .name = "MSR_IA32_MISC_ENABLE",
  27. // reserved: 1:2, 4:6, 8:10, 13:15, 17, 19:21, 24:33, 35:63
  28. .val_pairs = {{ .valid = 1, .value = 0x400c51889, .expected = 0x400c51889}}
  29. },
  30. { .index = 0x00000277, .name = "MSR_IA32_CR_PAT",
  31. .val_pairs = {{ .valid = 1, .value = 0x07070707, .expected = 0x07070707}}
  32. },
  33. { .index = 0xc0000100, .name = "MSR_FS_BASE",
  34. .val_pairs = {{ .valid = 1, .value = addr_64, .expected = addr_64}}
  35. },
  36. { .index = 0xc0000101, .name = "MSR_GS_BASE",
  37. .val_pairs = {{ .valid = 1, .value = addr_64, .expected = addr_64}}
  38. },
  39. { .index = 0xc0000102, .name = "MSR_KERNEL_GS_BASE",
  40. .val_pairs = {{ .valid = 1, .value = addr_64, .expected = addr_64}}
  41. },
  42. #ifdef __x86_64__
  43. { .index = 0xc0000080, .name = "MSR_EFER",
  44. .val_pairs = {{ .valid = 1, .value = 0xD00, .expected = 0xD00}}
  45. },
  46. #endif
  47. { .index = 0xc0000082, .name = "MSR_LSTAR",
  48. .val_pairs = {{ .valid = 1, .value = addr_64, .expected = addr_64}}
  49. },
  50. { .index = 0xc0000083, .name = "MSR_CSTAR",
  51. .val_pairs = {{ .valid = 1, .value = addr_64, .expected = addr_64}}
  52. },
  53. { .index = 0xc0000084, .name = "MSR_SYSCALL_MASK",
  54. .val_pairs = {{ .valid = 1, .value = 0xffffffff, .expected = 0xffffffff}}
  55. },
  56. // MSR_IA32_DEBUGCTLMSR needs svm feature LBRV
  57. // MSR_VM_HSAVE_PA only AMD host
  58. };
  59. static int find_msr_info(int msr_index)
  60. {
  61. int i;
  62. for (i = 0; i < sizeof(msr_info)/sizeof(msr_info[0]) ; i++) {
  63. if (msr_info[i].index == msr_index) {
  64. return i;
  65. }
  66. }
  67. return -1;
  68. }
  69. static void test_msr_rw(int msr_index, unsigned long long input, unsigned long long expected)
  70. {
  71. unsigned long long r = 0;
  72. int index;
  73. const char *sptr;
  74. if ((index = find_msr_info(msr_index)) != -1) {
  75. sptr = msr_info[index].name;
  76. } else {
  77. printf("couldn't find name for msr # %#x, skipping\n", msr_index);
  78. return;
  79. }
  80. wrmsr(msr_index, input);
  81. r = rdmsr(msr_index);
  82. if (expected != r) {
  83. printf("testing %s: output = %#x:%#x expected = %#x:%#x\n", sptr,
  84. (u32)(r >> 32), (u32)r, (u32)(expected >> 32), (u32)expected);
  85. }
  86. report("%s", expected == r, sptr);
  87. }
  88. int main(int ac, char **av)
  89. {
  90. int i, j;
  91. for (i = 0 ; i < sizeof(msr_info) / sizeof(msr_info[0]); i++) {
  92. for (j = 0; j < sizeof(msr_info[i].val_pairs) / sizeof(msr_info[i].val_pairs[0]); j++) {
  93. if (msr_info[i].val_pairs[j].valid) {
  94. test_msr_rw(msr_info[i].index, msr_info[i].val_pairs[j].value, msr_info[i].val_pairs[j].expected);
  95. } else {
  96. break;
  97. }
  98. }
  99. }
  100. return report_summary();
  101. }