[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230121001542.2472357-2-ackerleytng@google.com>
Date: Sat, 21 Jan 2023 00:15:12 +0000
From: Ackerley Tng <ackerleytng@...gle.com>
To: linux-kselftest@...r.kernel.org
Cc: pbonzini@...hat.com, seanjc@...gle.com, isaku.yamahata@...el.com,
sagis@...gle.com, erdemaktas@...gle.com, afranji@...gle.com,
runanwang@...gle.com, shuah@...nel.org, drjones@...hat.com,
maz@...nel.org, bgardon@...gle.com, jmattson@...gle.com,
dmatlack@...gle.com, peterx@...hat.com, oupton@...gle.com,
ricarkol@...gle.com, yang.zhong@...el.com, wei.w.wang@...el.com,
xiaoyao.li@...el.com, pgonda@...gle.com, marcorr@...gle.com,
eesposit@...hat.com, borntraeger@...ibm.com, eric.auger@...hat.com,
wangyanan55@...wei.com, aaronlewis@...gle.com, vkuznets@...hat.com,
pshier@...gle.com, axelrasmussen@...gle.com,
zhenzhong.duan@...el.com, maciej.szmigiero@...cle.com,
like.xu@...ux.intel.com, linux-kernel@...r.kernel.org,
kvm@...r.kernel.org, Ackerley Tng <ackerleytng@...gle.com>
Subject: [RFC PATCH v3 01/31] KVM: selftests: Add function to allow one-to-one
GVA to GPA mappings
One-to-one GVA to GPA mappings can be used in the guest to set up boot
sequences during which paging is enabled, hence requiring a transition
from using physical to virtual addresses in consecutive instructions.
Signed-off-by: Ackerley Tng <ackerleytng@...gle.com>
---
.../selftests/kvm/include/kvm_util_base.h | 2 ++
tools/testing/selftests/kvm/lib/kvm_util.c | 35 ++++++++++++++++---
2 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index 8dac0f49d20b9..0db5cd4b8383a 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -402,6 +402,8 @@ void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id);
vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
+vm_vaddr_t vm_vaddr_alloc_1to1(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
+ uint32_t data_memslot);
vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages);
vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm);
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index fa8aea97cdb62..5257bce6f546d 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1237,6 +1237,8 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
* vm - Virtual Machine
* sz - Size in bytes
* vaddr_min - Minimum starting virtual address
+ * paddr_min - Minimum starting physical address
+ * data_memslot - memslot number to allocate in
* encrypt - Whether the region should be handled as encrypted
*
* Output Args: None
@@ -1251,14 +1253,15 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
* a page.
*/
static vm_vaddr_t
-_vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, bool encrypt)
+_vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
+ vm_paddr_t paddr_min, uint32_t data_memslot, bool encrypt)
{
uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
virt_pgd_alloc(vm);
vm_paddr_t paddr = _vm_phy_pages_alloc(vm, pages,
- KVM_UTIL_MIN_PFN * vm->page_size,
- 0, encrypt);
+ paddr_min,
+ data_memslot, encrypt);
/*
* Find an unused range of virtual page addresses of at least
@@ -1281,12 +1284,34 @@ _vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, bool encrypt
vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min)
{
- return _vm_vaddr_alloc(vm, sz, vaddr_min, vm->protected);
+ return _vm_vaddr_alloc(vm, sz, vaddr_min,
+ KVM_UTIL_MIN_PFN * vm->page_size, 0,
+ vm->protected);
}
vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min)
{
- return _vm_vaddr_alloc(vm, sz, vaddr_min, false);
+ return _vm_vaddr_alloc(vm, sz, vaddr_min,
+ KVM_UTIL_MIN_PFN * vm->page_size, 0, false);
+}
+
+/**
+ * Allocate memory in @vm of size @sz in memslot with id @data_memslot,
+ * beginning with the desired address of @vaddr_min.
+ *
+ * If there isn't enough memory at @vaddr_min, find the next possible address
+ * that can meet the requested size in the given memslot.
+ *
+ * Return the address where the memory is allocated.
+ */
+vm_vaddr_t vm_vaddr_alloc_1to1(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
+ uint32_t data_memslot)
+{
+ vm_vaddr_t gva = _vm_vaddr_alloc(vm, sz, vaddr_min, (vm_paddr_t) vaddr_min,
+ data_memslot, vm->protected);
+ ASSERT_EQ(gva, addr_gva2gpa(vm, gva));
+
+ return gva;
}
/*
--
2.39.0.246.g2a6d74b583-goog
Powered by blists - more mailing lists