1818#include <linux/types.h>
1919
2020#include <asm/atomic.h>
21+ #include <asm/kvm.h>
2122
2223#include <sys/ioctl.h>
2324
25+ #include "kvm_util_arch.h"
2426#include "sparsebit.h"
2527
2628/*
@@ -46,6 +48,7 @@ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */
4648struct userspace_mem_region {
4749 struct kvm_userspace_memory_region2 region ;
4850 struct sparsebit * unused_phy_pages ;
51+ struct sparsebit * protected_phy_pages ;
4952 int fd ;
5053 off_t offset ;
5154 enum vm_mem_backing_src_type backing_src_type ;
@@ -90,6 +93,7 @@ enum kvm_mem_region_type {
9093struct kvm_vm {
9194 int mode ;
9295 unsigned long type ;
96+ uint8_t subtype ;
9397 int kvm_fd ;
9498 int fd ;
9599 unsigned int pgtable_levels ;
@@ -111,6 +115,9 @@ struct kvm_vm {
111115 vm_vaddr_t idt ;
112116 vm_vaddr_t handlers ;
113117 uint32_t dirty_ring_size ;
118+ uint64_t gpa_tag_mask ;
119+
120+ struct kvm_vm_arch arch ;
114121
115122 /* Cache of information for binary stats interface */
116123 int stats_fd ;
@@ -191,10 +198,14 @@ enum vm_guest_mode {
191198};
192199
193200struct vm_shape {
194- enum vm_guest_mode mode ;
195- unsigned int type ;
201+ uint32_t type ;
202+ uint8_t mode ;
203+ uint8_t subtype ;
204+ uint16_t padding ;
196205};
197206
207+ kvm_static_assert (sizeof (struct vm_shape ) == sizeof (uint64_t ));
208+
198209#define VM_TYPE_DEFAULT 0
199210
200211#define VM_SHAPE (__mode ) \
@@ -564,6 +575,13 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
564575 uint64_t guest_paddr , uint32_t slot , uint64_t npages ,
565576 uint32_t flags , int guest_memfd_fd , uint64_t guest_memfd_offset );
566577
578+ #ifndef vm_arch_has_protected_memory
579+ static inline bool vm_arch_has_protected_memory (struct kvm_vm * vm )
580+ {
581+ return false;
582+ }
583+ #endif
584+
567585void vm_mem_region_set_flags (struct kvm_vm * vm , uint32_t slot , uint32_t flags );
568586void vm_mem_region_move (struct kvm_vm * vm , uint32_t slot , uint64_t new_gpa );
569587void vm_mem_region_delete (struct kvm_vm * vm , uint32_t slot );
@@ -573,6 +591,9 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_mi
573591vm_vaddr_t vm_vaddr_alloc (struct kvm_vm * vm , size_t sz , vm_vaddr_t vaddr_min );
574592vm_vaddr_t __vm_vaddr_alloc (struct kvm_vm * vm , size_t sz , vm_vaddr_t vaddr_min ,
575593 enum kvm_mem_region_type type );
594+ vm_vaddr_t vm_vaddr_alloc_shared (struct kvm_vm * vm , size_t sz ,
595+ vm_vaddr_t vaddr_min ,
596+ enum kvm_mem_region_type type );
576597vm_vaddr_t vm_vaddr_alloc_pages (struct kvm_vm * vm , int nr_pages );
577598vm_vaddr_t __vm_vaddr_alloc_page (struct kvm_vm * vm ,
578599 enum kvm_mem_region_type type );
@@ -585,6 +606,12 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);
585606vm_paddr_t addr_hva2gpa (struct kvm_vm * vm , void * hva );
586607void * addr_gpa2alias (struct kvm_vm * vm , vm_paddr_t gpa );
587608
609+
610+ static inline vm_paddr_t vm_untag_gpa (struct kvm_vm * vm , vm_paddr_t gpa )
611+ {
612+ return gpa & ~vm -> gpa_tag_mask ;
613+ }
614+
588615void vcpu_run (struct kvm_vcpu * vcpu );
589616int _vcpu_run (struct kvm_vcpu * vcpu );
590617
@@ -827,10 +854,23 @@ const char *exit_reason_str(unsigned int exit_reason);
827854
828855vm_paddr_t vm_phy_page_alloc (struct kvm_vm * vm , vm_paddr_t paddr_min ,
829856 uint32_t memslot );
830- vm_paddr_t vm_phy_pages_alloc (struct kvm_vm * vm , size_t num ,
831- vm_paddr_t paddr_min , uint32_t memslot );
857+ vm_paddr_t __vm_phy_pages_alloc (struct kvm_vm * vm , size_t num ,
858+ vm_paddr_t paddr_min , uint32_t memslot ,
859+ bool protected );
832860vm_paddr_t vm_alloc_page_table (struct kvm_vm * vm );
833861
862+ static inline vm_paddr_t vm_phy_pages_alloc (struct kvm_vm * vm , size_t num ,
863+ vm_paddr_t paddr_min , uint32_t memslot )
864+ {
865+ /*
866+ * By default, allocate memory as protected for VMs that support
867+ * protected memory, as the majority of memory for such VMs is
868+ * protected, i.e. using shared memory is effectively opt-in.
869+ */
870+ return __vm_phy_pages_alloc (vm , num , paddr_min , memslot ,
871+ vm_arch_has_protected_memory (vm ));
872+ }
873+
834874/*
835875 * ____vm_create() does KVM_CREATE_VM and little else. __vm_create() also
836876 * loads the test binary into guest memory and creates an IRQ chip (x86 only).
@@ -969,15 +1009,18 @@ static inline void vcpu_dump(FILE *stream, struct kvm_vcpu *vcpu,
9691009 * Input Args:
9701010 * vm - Virtual Machine
9711011 * vcpu_id - The id of the VCPU to add to the VM.
972- * guest_code - The vCPU's entry point
9731012 */
974- struct kvm_vcpu * vm_arch_vcpu_add (struct kvm_vm * vm , uint32_t vcpu_id ,
975- void * guest_code );
1013+ struct kvm_vcpu * vm_arch_vcpu_add (struct kvm_vm * vm , uint32_t vcpu_id );
1014+ void vcpu_arch_set_entry_point ( struct kvm_vcpu * vcpu , void * guest_code );
9761015
9771016static inline struct kvm_vcpu * vm_vcpu_add (struct kvm_vm * vm , uint32_t vcpu_id ,
9781017 void * guest_code )
9791018{
980- return vm_arch_vcpu_add (vm , vcpu_id , guest_code );
1019+ struct kvm_vcpu * vcpu = vm_arch_vcpu_add (vm , vcpu_id );
1020+
1021+ vcpu_arch_set_entry_point (vcpu , guest_code );
1022+
1023+ return vcpu ;
9811024}
9821025
9831026/* Re-create a vCPU after restarting a VM, e.g. for state save/restore tests. */
@@ -1081,6 +1124,8 @@ void kvm_selftest_arch_init(void);
10811124
10821125void kvm_arch_vm_post_create (struct kvm_vm * vm );
10831126
1127+ bool vm_is_gpa_protected (struct kvm_vm * vm , vm_paddr_t paddr );
1128+
10841129uint32_t guest_get_vcpuid (void );
10851130
10861131#endif /* SELFTEST_KVM_UTIL_BASE_H */
0 commit comments