KVM: selftests: Add vcpu_arch_put_guest() to do writes from guest code
authorSean Christopherson <seanjc@google.com>
Thu, 14 Mar 2024 18:54:57 +0000 (11:54 -0700)
committerSean Christopherson <seanjc@google.com>
Mon, 29 Apr 2024 19:50:43 +0000 (12:50 -0700)
Introduce a macro, vcpu_arch_put_guest(), for "putting" values to memory
from guest code in "interesting" situations, e.g. when writing memory that
is being dirty logged.  Structure the macro so that arch code can provide
a custom implementation, e.g. x86 will use the macro to force emulation of
the access.

Use the helper in dirty_log_test, which is of particular interest (see
above), and in xen_shinfo_test, which isn't all that interesting, but
provides a second usage of the macro with a different size operand
(uint8_t versus uint64_t), i.e. to help verify that the macro works for
more than just 64-bit values.

Use "put" as the verb to align with the kernel's {get,put}_user()
terminology.

Link: https://lore.kernel.org/r/20240314185459.2439072-5-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/dirty_log_test.c
tools/testing/selftests/kvm/include/kvm_util_base.h
tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c

index aaf6d61c08db1153c4a4fbf9f3726d1a24364699..a83cbc7b7f0d9292e2597d49bdb2aa9e749b32fa 100644 (file)
@@ -105,7 +105,7 @@ static void guest_code(void)
         */
        for (i = 0; i < guest_num_pages; i++) {
                addr = guest_test_virt_mem + i * guest_page_size;
-               *(uint64_t *)addr = READ_ONCE(iteration);
+               vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration));
        }
 
        while (true) {
@@ -114,7 +114,8 @@ static void guest_code(void)
                        addr += (guest_random_u64(&guest_rng) % guest_num_pages)
                                * guest_page_size;
                        addr = align_down(addr, host_page_size);
-                       *(uint64_t *)addr = READ_ONCE(iteration);
+
+                       vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration));
                }
 
                GUEST_SYNC(1);
index af02308e264e9266c2ebff55137e5b0665a2ede8..e850269a321937c84e65f2818418a5b705d0bf87 100644 (file)
@@ -609,6 +609,9 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);
 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva);
 void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa);
 
+#ifndef vcpu_arch_put_guest
+#define vcpu_arch_put_guest(mem, val) do { (mem) = (val); } while (0)
+#endif
 
 static inline vm_paddr_t vm_untag_gpa(struct kvm_vm *vm, vm_paddr_t gpa)
 {
index d2ea0435f4f76332fb702d3e84d19b536a8ea4ee..1ba06551526b1faef9fedbc05440b442f6d7b548 100644 (file)
@@ -171,8 +171,9 @@ static volatile bool guest_saw_irq;
 static void evtchn_handler(struct ex_regs *regs)
 {
        struct vcpu_info *vi = (void *)VCPU_INFO_VADDR;
-       vi->evtchn_upcall_pending = 0;
-       vi->evtchn_pending_sel = 0;
+
+       vcpu_arch_put_guest(vi->evtchn_upcall_pending, 0);
+       vcpu_arch_put_guest(vi->evtchn_pending_sel, 0);
        guest_saw_irq = true;
 
        GUEST_SYNC(TEST_GUEST_SAW_IRQ);