kvm: selftest: pass in extra memory when create vm
authorPeter Xu <peterx@redhat.com>
Wed, 22 Aug 2018 07:19:59 +0000 (15:19 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 22 Aug 2018 14:48:38 +0000 (16:48 +0200)
This information can be used to decide the size of the default memory
slot, which will need to cover the extra pages with page tables.

Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/cr4_cpuid_sync_test.c
tools/testing/selftests/kvm/include/kvm_util.h
tools/testing/selftests/kvm/lib/x86.c
tools/testing/selftests/kvm/set_sregs_test.c
tools/testing/selftests/kvm/state_test.c
tools/testing/selftests/kvm/sync_regs_test.c
tools/testing/selftests/kvm/vmx_tsc_adjust_test.c

index d46b422..11ec358 100644 (file)
@@ -79,7 +79,7 @@ int main(int argc, char *argv[])
        setbuf(stdout, NULL);
 
        /* Create VM */
-       vm = vm_create_default(VCPU_ID, guest_code);
+       vm = vm_create_default(VCPU_ID, 0, guest_code);
        vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
        run = vcpu_state(vm, VCPU_ID);
 
index d8ca486..bd06d63 100644 (file)
@@ -127,7 +127,8 @@ kvm_get_supported_cpuid_entry(uint32_t function)
        return kvm_get_supported_cpuid_index(function, 0);
 }
 
-struct kvm_vm *vm_create_default(uint32_t vcpuid, void *guest_code);
+struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_size,
+                                void *guest_code);
 void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code);
 
 typedef void (*vmx_guest_code_t)(vm_vaddr_t vmxon_vaddr,
index e383452..a3122f1 100644 (file)
@@ -702,6 +702,9 @@ void vcpu_set_cpuid(struct kvm_vm *vm,
  *
  * Input Args:
  *   vcpuid - The id of the single VCPU to add to the VM.
+ *   extra_mem_pages - The size of extra memories to add (this will
+ *                     decide how much extra space we will need to
+ *                     setup the page tables using mem slot 0)
  *   guest_code - The vCPU's entry point
  *
  * Output Args: None
@@ -709,12 +712,23 @@ void vcpu_set_cpuid(struct kvm_vm *vm,
  * Return:
  *   Pointer to opaque structure that describes the created VM.
  */
-struct kvm_vm *vm_create_default(uint32_t vcpuid, void *guest_code)
+struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages,
+                                void *guest_code)
 {
        struct kvm_vm *vm;
+       /*
+        * For x86 the maximum page table size for a memory region
+        * will be when only 4K pages are used.  In that case the
+        * total extra size for page tables (for extra N pages) will
+        * be: N/512+N/512^2+N/512^3+... which is definitely smaller
+        * than N/512*2.
+        */
+       uint64_t extra_pg_pages = extra_mem_pages / 512 * 2;
 
        /* Create VM */
-       vm = vm_create(VM_MODE_FLAT48PG, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
+       vm = vm_create(VM_MODE_FLAT48PG,
+                      DEFAULT_GUEST_PHY_PAGES + extra_pg_pages,
+                      O_RDWR);
 
        /* Setup guest code */
        kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
index 090fd3f..881419d 100644 (file)
@@ -36,7 +36,7 @@ int main(int argc, char *argv[])
        setbuf(stdout, NULL);
 
        /* Create VM */
-       vm = vm_create_default(VCPU_ID, NULL);
+       vm = vm_create_default(VCPU_ID, 0, NULL);
 
        vcpu_sregs_get(vm, VCPU_ID, &sregs);
        sregs.apic_base = 1 << 10;
index 438d7b8..900e3e9 100644 (file)
@@ -132,7 +132,7 @@ int main(int argc, char *argv[])
        struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
 
        /* Create VM */
-       vm = vm_create_default(VCPU_ID, guest_code);
+       vm = vm_create_default(VCPU_ID, 0, guest_code);
        vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
        run = vcpu_state(vm, VCPU_ID);
 
index 2dbf2e1..213343e 100644 (file)
@@ -94,7 +94,7 @@ int main(int argc, char *argv[])
        }
 
        /* Create VM */
-       vm = vm_create_default(VCPU_ID, guest_code);
+       vm = vm_create_default(VCPU_ID, 0, guest_code);
 
        run = vcpu_state(vm, VCPU_ID);
 
index 4ddae12..49bcc68 100644 (file)
@@ -137,7 +137,7 @@ int main(int argc, char *argv[])
                exit(KSFT_SKIP);
        }
 
-       vm = vm_create_default(VCPU_ID, (void *) l1_guest_code);
+       vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code);
        vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
 
        /* Allocate VMX pages and shared descriptors (vmx_pages). */