1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef SELFTEST_KVM_UTIL_ARCH_H 3 #define SELFTEST_KVM_UTIL_ARCH_H 4 5 #include <stdbool.h> 6 #include <stdint.h> 7 8 #include "kvm_util_types.h" 9 #include "test_util.h" 10 11 extern bool is_forced_emulation_enabled; 12 13 struct kvm_vm_arch { 14 vm_vaddr_t gdt; 15 vm_vaddr_t tss; 16 vm_vaddr_t idt; 17 18 uint64_t c_bit; 19 uint64_t s_bit; 20 int sev_fd; 21 bool is_pt_protected; 22 }; 23 __vm_arch_has_protected_memory(struct kvm_vm_arch * arch)24static inline bool __vm_arch_has_protected_memory(struct kvm_vm_arch *arch) 25 { 26 return arch->c_bit || arch->s_bit; 27 } 28 29 #define vm_arch_has_protected_memory(vm) \ 30 __vm_arch_has_protected_memory(&(vm)->arch) 31 32 #define vcpu_arch_put_guest(mem, __val) \ 33 do { \ 34 const typeof(mem) val = (__val); \ 35 \ 36 if (!is_forced_emulation_enabled || guest_random_bool(&guest_rng)) { \ 37 (mem) = val; \ 38 } else if (guest_random_bool(&guest_rng)) { \ 39 __asm__ __volatile__(KVM_FEP "mov %1, %0" \ 40 : "+m" (mem) \ 41 : "r" (val) : "memory"); \ 42 } else { \ 43 uint64_t __old = READ_ONCE(mem); \ 44 \ 45 __asm__ __volatile__(KVM_FEP LOCK_PREFIX "cmpxchg %[new], %[ptr]" \ 46 : [ptr] "+m" (mem), [old] "+a" (__old) \ 47 : [new]"r" (val) : "memory", "cc"); \ 48 } \ 49 } while (0) 50 51 #endif // SELFTEST_KVM_UTIL_ARCH_H 52