Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Vishal Annapurve <vannapurve@google.com>
To: x86@kernel.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Cc: pbonzini@redhat.com, vkuznets@redhat.com, wanpengli@tencent.com,
	jmattson@google.com, joro@8bytes.org, tglx@linutronix.de,
	mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
	hpa@zytor.com, shuah@kernel.org, yang.zhong@intel.com,
	drjones@redhat.com, ricarkol@google.com, aaronlewis@google.com,
	wei.w.wang@intel.com, kirill.shutemov@linux.intel.com,
	corbet@lwn.net, hughd@google.com, jlayton@kernel.org,
	bfields@fieldses.org, akpm@linux-foundation.org,
	chao.p.peng@linux.intel.com, yu.c.zhang@linux.intel.com,
	jun.nakajima@intel.com, dave.hansen@intel.com,
	michael.roth@amd.com, qperret@google.com, steven.price@arm.com,
	ak@linux.intel.com, david@redhat.com, luto@kernel.org,
	vbabka@suse.cz, marcorr@google.com, erdemaktas@google.com,
	pgonda@google.com, nikunj@amd.com, seanjc@google.com,
	diviness@google.com, maz@kernel.org, dmatlack@google.com,
	axelrasmussen@google.com, maciej.szmigiero@oracle.com,
	mizhang@google.com, bgardon@google.com,
	Vishal Annapurve <vannapurve@google.com>
Subject: [RFC V2 PATCH 5/8] selftests: kvm: Update usage of private mem lib for SEV VMs
Date: Tue, 30 Aug 2022 22:42:56 +0000	[thread overview]
Message-ID: <20220830224259.412342-6-vannapurve@google.com> (raw)
In-Reply-To: <20220830224259.412342-1-vannapurve@google.com>

Add/update APIs to allow reusing private mem lib for SEV VMs.
Memory conversion for SEV VMs includes updating guest pagetables
based on virtual addresses to toggle C-bit.

Signed-off-by: Vishal Annapurve <vannapurve@google.com>
---
 .../kvm/include/x86_64/private_mem.h          |   9 +-
 .../selftests/kvm/lib/x86_64/private_mem.c    | 103 +++++++++++++-----
 2 files changed, 83 insertions(+), 29 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86_64/private_mem.h b/tools/testing/selftests/kvm/include/x86_64/private_mem.h
index 645bf3f61d1e..183b53b8c486 100644
--- a/tools/testing/selftests/kvm/include/x86_64/private_mem.h
+++ b/tools/testing/selftests/kvm/include/x86_64/private_mem.h
@@ -14,10 +14,10 @@ enum mem_conversion_type {
 	TO_SHARED
 };
 
-void guest_update_mem_access(enum mem_conversion_type type, uint64_t gpa,
-	uint64_t size);
-void guest_update_mem_map(enum mem_conversion_type type, uint64_t gpa,
-	uint64_t size);
+void guest_update_mem_access(enum mem_conversion_type type, uint64_t gva,
+	uint64_t gpa, uint64_t size);
+void guest_update_mem_map(enum mem_conversion_type type, uint64_t gva,
+	uint64_t gpa, uint64_t size);
 
 void guest_map_ucall_page_shared(void);
 
@@ -45,6 +45,7 @@ struct vm_setup_info {
 	struct test_setup_info test_info;
 	guest_code_fn guest_fn;
 	io_exit_handler ioexit_cb;
+	uint32_t policy; /* Used for Sev VMs */
 };
 
 void execute_vm_with_private_mem(struct vm_setup_info *info);
diff --git a/tools/testing/selftests/kvm/lib/x86_64/private_mem.c b/tools/testing/selftests/kvm/lib/x86_64/private_mem.c
index f6dcfa4d353f..28d93754e1f2 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/private_mem.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/private_mem.c
@@ -22,12 +22,45 @@
 #include <kvm_util.h>
 #include <private_mem.h>
 #include <processor.h>
+#include <sev.h>
+
+#define GUEST_PGT_MIN_VADDR	0x10000
+
+/* Variables populated by userspace logic and consumed by guest code */
+static bool is_sev_vm;
+static struct guest_pgt_info *sev_gpgt_info;
+static uint8_t sev_enc_bit;
+
+static void sev_guest_set_clr_pte_bit(uint64_t vaddr_start, uint64_t mem_size,
+	bool set)
+{
+	uint64_t vaddr = vaddr_start;
+	uint32_t guest_page_size = sev_gpgt_info->page_size;
+	uint32_t num_pages;
+
+	GUEST_ASSERT(!(mem_size % guest_page_size) && !(vaddr_start %
+		guest_page_size));
+
+	num_pages = mem_size / guest_page_size;
+	for (uint32_t i = 0; i < num_pages; i++) {
+		uint64_t *pte = guest_code_get_pte(sev_gpgt_info, vaddr);
+
+		GUEST_ASSERT(pte);
+		if (set)
+			*pte |= (1ULL << sev_enc_bit);
+		else
+			*pte &= ~(1ULL << sev_enc_bit);
+		asm volatile("invlpg (%0)" :: "r"(vaddr) : "memory");
+		vaddr += guest_page_size;
+	}
+}
 
 /*
  * Execute KVM hypercall to change memory access type for a given gpa range.
  *
  * Input Args:
  *   type - memory conversion type TO_SHARED/TO_PRIVATE
+ *   gva - starting gva address
  *   gpa - starting gpa address
  *   size - size of the range starting from gpa for which memory access needs
  *     to be changed
@@ -40,9 +73,12 @@
  * for a given gpa range. This API is useful in exercising implicit conversion
  * path.
  */
-void guest_update_mem_access(enum mem_conversion_type type, uint64_t gpa,
-	uint64_t size)
+void guest_update_mem_access(enum mem_conversion_type type, uint64_t gva,
+	uint64_t gpa, uint64_t size)
 {
+	if (is_sev_vm)
+		sev_guest_set_clr_pte_bit(gva, size, type == TO_PRIVATE ? true : false);
+
 	int ret = kvm_hypercall(KVM_HC_MAP_GPA_RANGE, gpa, size >> MIN_PAGE_SHIFT,
 		type == TO_PRIVATE ? KVM_MARK_GPA_RANGE_ENC_ACCESS :
 			KVM_CLR_GPA_RANGE_ENC_ACCESS, 0);
@@ -54,6 +90,7 @@ void guest_update_mem_access(enum mem_conversion_type type, uint64_t gpa,
  *
  * Input Args:
  *   type - memory conversion type TO_SHARED/TO_PRIVATE
+ *   gva - starting gva address
  *   gpa - starting gpa address
  *   size - size of the range starting from gpa for which memory type needs
  *     to be changed
@@ -65,9 +102,12 @@ void guest_update_mem_access(enum mem_conversion_type type, uint64_t gpa,
  * Function called by guest logic in selftests to update the memory type for a
  * given gpa range. This API is useful in exercising explicit conversion path.
  */
-void guest_update_mem_map(enum mem_conversion_type type, uint64_t gpa,
-	uint64_t size)
+void guest_update_mem_map(enum mem_conversion_type type, uint64_t gva,
+	uint64_t gpa, uint64_t size)
 {
+	if (is_sev_vm)
+		sev_guest_set_clr_pte_bit(gva, size, type == TO_PRIVATE ? true : false);
+
 	int ret = kvm_hypercall(KVM_HC_MAP_GPA_RANGE, gpa, size >> MIN_PAGE_SHIFT,
 		type == TO_PRIVATE ? KVM_MAP_GPA_RANGE_ENCRYPTED :
 			KVM_MAP_GPA_RANGE_DECRYPTED, 0);
@@ -90,30 +130,15 @@ void guest_update_mem_map(enum mem_conversion_type type, uint64_t gpa,
 void guest_map_ucall_page_shared(void)
 {
 	vm_paddr_t ucall_paddr = get_ucall_pool_paddr();
+	GUEST_ASSERT(ucall_paddr);
 
-	guest_update_mem_access(TO_SHARED, ucall_paddr, 1 << MIN_PAGE_SHIFT);
+	int ret = kvm_hypercall(KVM_HC_MAP_GPA_RANGE, ucall_paddr, 1,
+		KVM_MAP_GPA_RANGE_DECRYPTED, 0);
+	GUEST_ASSERT_1(!ret, ret);
 }
 
-/*
- * Execute KVM ioctl to back/unback private memory for given gpa range.
- *
- * Input Args:
- *   vm - kvm_vm handle
- *   gpa - starting gpa address
- *   size - size of the gpa range
- *   op - mem_op indicating whether private memory needs to be allocated or
- *     unbacked
- *
- * Output Args: None
- *
- * Return: None
- *
- * Function called by host userspace logic in selftests to back/unback private
- * memory for gpa ranges. This function is useful to setup initial boot private
- * memory and then convert memory during runtime.
- */
-void vm_update_private_mem(struct kvm_vm *vm, uint64_t gpa, uint64_t size,
-	enum mem_op op)
+static void vm_update_private_mem_internal(struct kvm_vm *vm, uint64_t gpa,
+	uint64_t size, enum mem_op op, bool encrypt)
 {
 	int priv_memfd;
 	uint64_t priv_offset, guest_phys_base, fd_offset;
@@ -142,6 +167,10 @@ void vm_update_private_mem(struct kvm_vm *vm, uint64_t gpa, uint64_t size,
 	TEST_ASSERT(ret == 0, "fallocate failed\n");
 	enc_region.addr = gpa;
 	enc_region.size = size;
+
+	if (!encrypt)
+		return;
+
 	if (op == ALLOCATE_MEM) {
 		printf("doing encryption for gpa 0x%lx size 0x%lx\n", gpa, size);
 		vm_ioctl(vm, KVM_MEMORY_ENCRYPT_REG_REGION, &enc_region);
@@ -151,6 +180,30 @@ void vm_update_private_mem(struct kvm_vm *vm, uint64_t gpa, uint64_t size,
 	}
 }
 
+/*
+ * Execute KVM ioctl to back/unback private memory for given gpa range.
+ *
+ * Input Args:
+ *   vm - kvm_vm handle
+ *   gpa - starting gpa address
+ *   size - size of the gpa range
+ *   op - mem_op indicating whether private memory needs to be allocated or
+ *     unbacked
+ *
+ * Output Args: None
+ *
+ * Return: None
+ *
+ * Function called by host userspace logic in selftests to back/unback private
+ * memory for gpa ranges. This function is useful to setup initial boot private
+ * memory and then convert memory during runtime.
+ */
+void vm_update_private_mem(struct kvm_vm *vm, uint64_t gpa, uint64_t size,
+	enum mem_op op)
+{
+	vm_update_private_mem_internal(vm, gpa, size, op, true /* encrypt */);
+}
+
 static void handle_vm_exit_map_gpa_hypercall(struct kvm_vm *vm,
 				volatile struct kvm_run *run)
 {
-- 
2.37.2.672.g94769d06f0-goog


  parent reply	other threads:[~2022-08-30 22:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-30 22:42 [RFC V2 PATCH 0/8] selftests: KVM: SEV: selftests for fd-based private memory Vishal Annapurve
2022-08-30 22:42 ` [RFC V2 PATCH 1/8] selftests: kvm: x86_64: Add support for pagetable tracking Vishal Annapurve
2022-08-30 22:42 ` [RFC V2 PATCH 2/8] kvm: Add HVA range operator Vishal Annapurve
2022-08-30 22:42 ` [RFC V2 PATCH 3/8] arch: x86: sev: Populate private memory fd during LAUNCH_UPDATE_DATA Vishal Annapurve
2022-08-30 22:42 ` [RFC V2 PATCH 4/8] selftests: kvm: sev: Support memslots with private memory Vishal Annapurve
2022-08-30 22:42 ` Vishal Annapurve [this message]
2022-08-30 22:42 ` [RFC V2 PATCH 6/8] selftests: kvm: Support executing SEV VMs " Vishal Annapurve
2022-08-30 22:42 ` [RFC V2 PATCH 7/8] selftests: kvm: Refactor testing logic for " Vishal Annapurve
2022-08-30 22:42 ` [RFC V2 PATCH 8/8] selftests: kvm: Add private memory test for SEV VMs Vishal Annapurve

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220830224259.412342-6-vannapurve@google.com \
    --to=vannapurve@google.com \
    --cc=aaronlewis@google.com \
    --cc=ak@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=axelrasmussen@google.com \
    --cc=bfields@fieldses.org \
    --cc=bgardon@google.com \
    --cc=bp@alien8.de \
    --cc=chao.p.peng@linux.intel.com \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@redhat.com \
    --cc=diviness@google.com \
    --cc=dmatlack@google.com \
    --cc=drjones@redhat.com \
    --cc=erdemaktas@google.com \
    --cc=hpa@zytor.com \
    --cc=hughd@google.com \
    --cc=jlayton@kernel.org \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=jun.nakajima@intel.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=maciej.szmigiero@oracle.com \
    --cc=marcorr@google.com \
    --cc=maz@kernel.org \
    --cc=michael.roth@amd.com \
    --cc=mingo@redhat.com \
    --cc=mizhang@google.com \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=pgonda@google.com \
    --cc=qperret@google.com \
    --cc=ricarkol@google.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=steven.price@arm.com \
    --cc=tglx@linutronix.de \
    --cc=vbabka@suse.cz \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.com \
    --cc=wei.w.wang@intel.com \
    --cc=x86@kernel.org \
    --cc=yang.zhong@intel.com \
    --cc=yu.c.zhang@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox