Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Add vcpu_arch_put_guest() to do writes from guest code

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>

+9 -4
+3 -2
tools/testing/selftests/kvm/dirty_log_test.c
··· 105 105 */ 106 106 for (i = 0; i < guest_num_pages; i++) { 107 107 addr = guest_test_virt_mem + i * guest_page_size; 108 - *(uint64_t *)addr = READ_ONCE(iteration); 108 + vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration)); 109 109 } 110 110 111 111 while (true) { ··· 114 114 addr += (guest_random_u64(&guest_rng) % guest_num_pages) 115 115 * guest_page_size; 116 116 addr = align_down(addr, host_page_size); 117 - *(uint64_t *)addr = READ_ONCE(iteration); 117 + 118 + vcpu_arch_put_guest(*(uint64_t *)addr, READ_ONCE(iteration)); 118 119 } 119 120 120 121 GUEST_SYNC(1);
+3
tools/testing/selftests/kvm/include/kvm_util_base.h
··· 609 609 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva); 610 610 void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa); 611 611 612 + #ifndef vcpu_arch_put_guest 613 + #define vcpu_arch_put_guest(mem, val) do { (mem) = (val); } while (0) 614 + #endif 612 615 613 616 static inline vm_paddr_t vm_untag_gpa(struct kvm_vm *vm, vm_paddr_t gpa) 614 617 {
+3 -2
tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c
··· 171 171 static void evtchn_handler(struct ex_regs *regs) 172 172 { 173 173 struct vcpu_info *vi = (void *)VCPU_INFO_VADDR; 174 - vi->evtchn_upcall_pending = 0; 175 - vi->evtchn_pending_sel = 0; 174 + 175 + vcpu_arch_put_guest(vi->evtchn_upcall_pending, 0); 176 + vcpu_arch_put_guest(vi->evtchn_pending_sel, 0); 176 177 guest_saw_irq = true; 177 178 178 179 GUEST_SYNC(TEST_GUEST_SAW_IRQ);