ucna_injection_test.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ucna_injection_test
  4. *
  5. * Copyright (C) 2022, Google LLC.
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2.
  8. *
  9. * Test that user space can inject UnCorrectable No Action required (UCNA)
  10. * memory errors to the guest.
  11. *
  12. * The test starts one vCPU with the MCG_CMCI_P enabled. It verifies that
  13. * proper UCNA errors can be injected to a vCPU with MCG_CMCI_P and
  14. * corresponding per-bank control register (MCI_CTL2) bit enabled.
  15. * The test also checks that the UCNA errors get recorded in the
  16. * Machine Check bank registers no matter the error signal interrupts get
  17. * delivered into the guest or not.
  18. *
  19. */
  20. #include <pthread.h>
  21. #include <inttypes.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #include "kvm_util.h"
  25. #include "mce.h"
  26. #include "processor.h"
  27. #include "test_util.h"
  28. #include "apic.h"
  29. #define SYNC_FIRST_UCNA 9
  30. #define SYNC_SECOND_UCNA 10
  31. #define SYNC_GP 11
  32. #define FIRST_UCNA_ADDR 0xdeadbeef
  33. #define SECOND_UCNA_ADDR 0xcafeb0ba
  34. /*
  35. * Vector for the CMCI interrupt.
  36. * Value is arbitrary. Any value in 0x20-0xFF should work:
  37. * https://wiki.osdev.org/Interrupt_Vector_Table
  38. */
  39. #define CMCI_VECTOR 0xa9
  40. #define UCNA_BANK 0x7 // IMC0 bank
  41. #define MCI_CTL2_RESERVED_BIT BIT_ULL(29)
  42. static uint64_t supported_mcg_caps;
  43. /*
  44. * Record states about the injected UCNA.
  45. * The variables started with the 'i_' prefixes are recorded in interrupt
  46. * handler. Variables without the 'i_' prefixes are recorded in guest main
  47. * execution thread.
  48. */
  49. static volatile uint64_t i_ucna_rcvd;
  50. static volatile uint64_t i_ucna_addr;
  51. static volatile uint64_t ucna_addr;
  52. static volatile uint64_t ucna_addr2;
  53. struct thread_params {
  54. struct kvm_vcpu *vcpu;
  55. uint64_t *p_i_ucna_rcvd;
  56. uint64_t *p_i_ucna_addr;
  57. uint64_t *p_ucna_addr;
  58. uint64_t *p_ucna_addr2;
  59. };
  60. static void verify_apic_base_addr(void)
  61. {
  62. uint64_t msr = rdmsr(MSR_IA32_APICBASE);
  63. uint64_t base = GET_APIC_BASE(msr);
  64. GUEST_ASSERT(base == APIC_DEFAULT_GPA);
  65. }
  66. static void ucna_injection_guest_code(void)
  67. {
  68. uint64_t ctl2;
  69. verify_apic_base_addr();
  70. xapic_enable();
  71. /* Sets up the interrupt vector and enables per-bank CMCI sigaling. */
  72. xapic_write_reg(APIC_LVTCMCI, CMCI_VECTOR | APIC_DM_FIXED);
  73. ctl2 = rdmsr(MSR_IA32_MCx_CTL2(UCNA_BANK));
  74. wrmsr(MSR_IA32_MCx_CTL2(UCNA_BANK), ctl2 | MCI_CTL2_CMCI_EN);
  75. /* Enables interrupt in guest. */
  76. sti();
  77. /* Let user space inject the first UCNA */
  78. GUEST_SYNC(SYNC_FIRST_UCNA);
  79. ucna_addr = rdmsr(MSR_IA32_MCx_ADDR(UCNA_BANK));
  80. /* Disables the per-bank CMCI signaling. */
  81. ctl2 = rdmsr(MSR_IA32_MCx_CTL2(UCNA_BANK));
  82. wrmsr(MSR_IA32_MCx_CTL2(UCNA_BANK), ctl2 & ~MCI_CTL2_CMCI_EN);
  83. /* Let the user space inject the second UCNA */
  84. GUEST_SYNC(SYNC_SECOND_UCNA);
  85. ucna_addr2 = rdmsr(MSR_IA32_MCx_ADDR(UCNA_BANK));
  86. GUEST_DONE();
  87. }
  88. static void cmci_disabled_guest_code(void)
  89. {
  90. uint64_t ctl2 = rdmsr(MSR_IA32_MCx_CTL2(UCNA_BANK));
  91. wrmsr(MSR_IA32_MCx_CTL2(UCNA_BANK), ctl2 | MCI_CTL2_CMCI_EN);
  92. GUEST_DONE();
  93. }
  94. static void cmci_enabled_guest_code(void)
  95. {
  96. uint64_t ctl2 = rdmsr(MSR_IA32_MCx_CTL2(UCNA_BANK));
  97. wrmsr(MSR_IA32_MCx_CTL2(UCNA_BANK), ctl2 | MCI_CTL2_RESERVED_BIT);
  98. GUEST_DONE();
  99. }
  100. static void guest_cmci_handler(struct ex_regs *regs)
  101. {
  102. i_ucna_rcvd++;
  103. i_ucna_addr = rdmsr(MSR_IA32_MCx_ADDR(UCNA_BANK));
  104. xapic_write_reg(APIC_EOI, 0);
  105. }
  106. static void guest_gp_handler(struct ex_regs *regs)
  107. {
  108. GUEST_SYNC(SYNC_GP);
  109. }
  110. static void run_vcpu_expect_gp(struct kvm_vcpu *vcpu)
  111. {
  112. struct ucall uc;
  113. vcpu_run(vcpu);
  114. TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
  115. TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_SYNC,
  116. "Expect UCALL_SYNC");
  117. TEST_ASSERT(uc.args[1] == SYNC_GP, "#GP is expected.");
  118. printf("vCPU received GP in guest.\n");
  119. }
  120. static void inject_ucna(struct kvm_vcpu *vcpu, uint64_t addr) {
  121. /*
  122. * A UCNA error is indicated with VAL=1, UC=1, PCC=0, S=0 and AR=0 in
  123. * the IA32_MCi_STATUS register.
  124. * MSCOD=1 (BIT[16] - MscodDataRdErr).
  125. * MCACOD=0x0090 (Memory controller error format, channel 0)
  126. */
  127. uint64_t status = MCI_STATUS_VAL | MCI_STATUS_UC | MCI_STATUS_EN |
  128. MCI_STATUS_MISCV | MCI_STATUS_ADDRV | 0x10090;
  129. struct kvm_x86_mce mce = {};
  130. mce.status = status;
  131. mce.mcg_status = 0;
  132. /*
  133. * MCM_ADDR_PHYS indicates the reported address is a physical address.
  134. * Lowest 6 bits is the recoverable address LSB, i.e., the injected MCE
  135. * is at 4KB granularity.
  136. */
  137. mce.misc = (MCM_ADDR_PHYS << 6) | 0xc;
  138. mce.addr = addr;
  139. mce.bank = UCNA_BANK;
  140. vcpu_ioctl(vcpu, KVM_X86_SET_MCE, &mce);
  141. }
  142. static void *run_ucna_injection(void *arg)
  143. {
  144. struct thread_params *params = (struct thread_params *)arg;
  145. struct ucall uc;
  146. int old;
  147. int r;
  148. r = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
  149. TEST_ASSERT(r == 0,
  150. "pthread_setcanceltype failed with errno=%d",
  151. r);
  152. vcpu_run(params->vcpu);
  153. TEST_ASSERT_KVM_EXIT_REASON(params->vcpu, KVM_EXIT_IO);
  154. TEST_ASSERT(get_ucall(params->vcpu, &uc) == UCALL_SYNC,
  155. "Expect UCALL_SYNC");
  156. TEST_ASSERT(uc.args[1] == SYNC_FIRST_UCNA, "Injecting first UCNA.");
  157. printf("Injecting first UCNA at %#x.\n", FIRST_UCNA_ADDR);
  158. inject_ucna(params->vcpu, FIRST_UCNA_ADDR);
  159. vcpu_run(params->vcpu);
  160. TEST_ASSERT_KVM_EXIT_REASON(params->vcpu, KVM_EXIT_IO);
  161. TEST_ASSERT(get_ucall(params->vcpu, &uc) == UCALL_SYNC,
  162. "Expect UCALL_SYNC");
  163. TEST_ASSERT(uc.args[1] == SYNC_SECOND_UCNA, "Injecting second UCNA.");
  164. printf("Injecting second UCNA at %#x.\n", SECOND_UCNA_ADDR);
  165. inject_ucna(params->vcpu, SECOND_UCNA_ADDR);
  166. vcpu_run(params->vcpu);
  167. TEST_ASSERT_KVM_EXIT_REASON(params->vcpu, KVM_EXIT_IO);
  168. if (get_ucall(params->vcpu, &uc) == UCALL_ABORT) {
  169. TEST_ASSERT(false, "vCPU assertion failure: %s.",
  170. (const char *)uc.args[0]);
  171. }
  172. return NULL;
  173. }
  174. static void test_ucna_injection(struct kvm_vcpu *vcpu, struct thread_params *params)
  175. {
  176. struct kvm_vm *vm = vcpu->vm;
  177. params->vcpu = vcpu;
  178. params->p_i_ucna_rcvd = (uint64_t *)addr_gva2hva(vm, (uint64_t)&i_ucna_rcvd);
  179. params->p_i_ucna_addr = (uint64_t *)addr_gva2hva(vm, (uint64_t)&i_ucna_addr);
  180. params->p_ucna_addr = (uint64_t *)addr_gva2hva(vm, (uint64_t)&ucna_addr);
  181. params->p_ucna_addr2 = (uint64_t *)addr_gva2hva(vm, (uint64_t)&ucna_addr2);
  182. run_ucna_injection(params);
  183. TEST_ASSERT(*params->p_i_ucna_rcvd == 1, "Only first UCNA get signaled.");
  184. TEST_ASSERT(*params->p_i_ucna_addr == FIRST_UCNA_ADDR,
  185. "Only first UCNA reported addr get recorded via interrupt.");
  186. TEST_ASSERT(*params->p_ucna_addr == FIRST_UCNA_ADDR,
  187. "First injected UCNAs should get exposed via registers.");
  188. TEST_ASSERT(*params->p_ucna_addr2 == SECOND_UCNA_ADDR,
  189. "Second injected UCNAs should get exposed via registers.");
  190. printf("Test successful.\n"
  191. "UCNA CMCI interrupts received: %ld\n"
  192. "Last UCNA address received via CMCI: %lx\n"
  193. "First UCNA address in vCPU thread: %lx\n"
  194. "Second UCNA address in vCPU thread: %lx\n",
  195. *params->p_i_ucna_rcvd, *params->p_i_ucna_addr,
  196. *params->p_ucna_addr, *params->p_ucna_addr2);
  197. }
  198. static void setup_mce_cap(struct kvm_vcpu *vcpu, bool enable_cmci_p)
  199. {
  200. uint64_t mcg_caps = MCG_CTL_P | MCG_SER_P | MCG_LMCE_P | KVM_MAX_MCE_BANKS;
  201. if (enable_cmci_p)
  202. mcg_caps |= MCG_CMCI_P;
  203. mcg_caps &= supported_mcg_caps | MCG_CAP_BANKS_MASK;
  204. vcpu_ioctl(vcpu, KVM_X86_SETUP_MCE, &mcg_caps);
  205. }
  206. static struct kvm_vcpu *create_vcpu_with_mce_cap(struct kvm_vm *vm, uint32_t vcpuid,
  207. bool enable_cmci_p, void *guest_code)
  208. {
  209. struct kvm_vcpu *vcpu = vm_vcpu_add(vm, vcpuid, guest_code);
  210. setup_mce_cap(vcpu, enable_cmci_p);
  211. return vcpu;
  212. }
  213. int main(int argc, char *argv[])
  214. {
  215. struct thread_params params;
  216. struct kvm_vm *vm;
  217. struct kvm_vcpu *ucna_vcpu;
  218. struct kvm_vcpu *cmcidis_vcpu;
  219. struct kvm_vcpu *cmci_vcpu;
  220. kvm_check_cap(KVM_CAP_MCE);
  221. vm = __vm_create(VM_SHAPE_DEFAULT, 3, 0);
  222. kvm_ioctl(vm->kvm_fd, KVM_X86_GET_MCE_CAP_SUPPORTED,
  223. &supported_mcg_caps);
  224. if (!(supported_mcg_caps & MCG_CMCI_P)) {
  225. print_skip("MCG_CMCI_P is not supported");
  226. exit(KSFT_SKIP);
  227. }
  228. ucna_vcpu = create_vcpu_with_mce_cap(vm, 0, true, ucna_injection_guest_code);
  229. cmcidis_vcpu = create_vcpu_with_mce_cap(vm, 1, false, cmci_disabled_guest_code);
  230. cmci_vcpu = create_vcpu_with_mce_cap(vm, 2, true, cmci_enabled_guest_code);
  231. vm_install_exception_handler(vm, CMCI_VECTOR, guest_cmci_handler);
  232. vm_install_exception_handler(vm, GP_VECTOR, guest_gp_handler);
  233. virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);
  234. test_ucna_injection(ucna_vcpu, &params);
  235. run_vcpu_expect_gp(cmcidis_vcpu);
  236. run_vcpu_expect_gp(cmci_vcpu);
  237. kvm_vm_free(vm);
  238. }