diff mbox series

[RFC,v5,04/29] KVM: selftests: Refactor steps in vCPU descriptor table initialization

Message ID 20231212204647.2170650-5-sagis@google.com
State New
Headers show
Series TDX KVM selftests | expand

Commit Message

Sagi Shahar Dec. 12, 2023, 8:46 p.m. UTC
From: Ackerley Tng <ackerleytng@google.com>

Split the vCPU descriptor table initialization process into a few
steps and expose them:

+ Setting up the IDT
+ Syncing exception handlers into the guest

In kvm_setup_idt(), we conditionally allocate guest memory for vm->idt
to avoid double allocation when kvm_setup_idt() is used after
vm_init_descriptor_tables().

Signed-off-by: Ackerley Tng <ackerleytng@google.com>
Signed-off-by: Ryan Afranji <afranji@google.com>
Signed-off-by: Sagi Shahar <sagis@google.com>
---
 .../selftests/kvm/include/x86_64/processor.h  |  2 ++
 .../selftests/kvm/lib/x86_64/processor.c      | 19 ++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)

Comments

Binbin Wu Feb. 21, 2024, 5:43 a.m. UTC | #1
On 12/13/2023 4:46 AM, Sagi Shahar wrote:
> From: Ackerley Tng <ackerleytng@google.com>
>
> Split the vCPU descriptor table initialization process into a few
> steps and expose them:
>
> + Setting up the IDT
> + Syncing exception handlers into the guest
>
> In kvm_setup_idt(), we conditionally allocate guest memory for vm->idt
> to avoid double allocation when kvm_setup_idt() is used after
> vm_init_descriptor_tables().
>
> Signed-off-by: Ackerley Tng <ackerleytng@google.com>
> Signed-off-by: Ryan Afranji <afranji@google.com>
> Signed-off-by: Sagi Shahar <sagis@google.com>
> ---
>   .../selftests/kvm/include/x86_64/processor.h  |  2 ++
>   .../selftests/kvm/lib/x86_64/processor.c      | 19 ++++++++++++++++---
>   2 files changed, 18 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
> index 0b8855d68744..5c4e9a27d9e2 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/processor.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
> @@ -1089,6 +1089,8 @@ struct idt_entry {
>   	uint32_t offset2; uint32_t reserved;
>   };
>   
> +void kvm_setup_idt(struct kvm_vm *vm, struct kvm_dtable *dt);
> +void sync_exception_handlers_to_guest(struct kvm_vm *vm);
>   void vm_init_descriptor_tables(struct kvm_vm *vm);
>   void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu);
>   void vm_install_exception_handler(struct kvm_vm *vm, int vector,
> diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> index b6b9438e0a33..566d82829da4 100644
> --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> @@ -1155,19 +1155,32 @@ void vm_init_descriptor_tables(struct kvm_vm *vm)
>   			DEFAULT_CODE_SELECTOR);
>   }
>   
> +void kvm_setup_idt(struct kvm_vm *vm, struct kvm_dtable *dt)
> +{
> +	if (!vm->idt)
> +		vm->idt = vm_vaddr_alloc_page(vm);

IDT is allocated in DATA memslot in current code, but here, when using
vm_vaddr_alloc_page(), it will be allocated in TEST_DATA memslot.

Do we need to follow the current code to use
__vm_vaddr_alloc_page(vm, MEM_REGION_DATA) instead?

> +
> +	dt->base = vm->idt;
> +	dt->limit = NUM_INTERRUPTS * sizeof(struct idt_entry) - 1;
> +}
> +
> +void sync_exception_handlers_to_guest(struct kvm_vm *vm)
> +{
> +	*(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers;
> +}
> +
>   void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu)
>   {
>   	struct kvm_vm *vm = vcpu->vm;
>   	struct kvm_sregs sregs;
>   
>   	vcpu_sregs_get(vcpu, &sregs);
> -	sregs.idt.base = vm->idt;
> -	sregs.idt.limit = NUM_INTERRUPTS * sizeof(struct idt_entry) - 1;
> +	kvm_setup_idt(vcpu->vm, &sregs.idt);
>   	sregs.gdt.base = vm->gdt;
>   	sregs.gdt.limit = getpagesize() - 1;
>   	kvm_seg_set_kernel_data_64bit(NULL, DEFAULT_DATA_SELECTOR, &sregs.gs);
>   	vcpu_sregs_set(vcpu, &sregs);
> -	*(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers;
> +	sync_exception_handlers_to_guest(vm);
>   }
>   
>   void vm_install_exception_handler(struct kvm_vm *vm, int vector,
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
index 0b8855d68744..5c4e9a27d9e2 100644
--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
@@ -1089,6 +1089,8 @@  struct idt_entry {
 	uint32_t offset2; uint32_t reserved;
 };
 
+void kvm_setup_idt(struct kvm_vm *vm, struct kvm_dtable *dt);
+void sync_exception_handlers_to_guest(struct kvm_vm *vm);
 void vm_init_descriptor_tables(struct kvm_vm *vm);
 void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu);
 void vm_install_exception_handler(struct kvm_vm *vm, int vector,
diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index b6b9438e0a33..566d82829da4 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -1155,19 +1155,32 @@  void vm_init_descriptor_tables(struct kvm_vm *vm)
 			DEFAULT_CODE_SELECTOR);
 }
 
+void kvm_setup_idt(struct kvm_vm *vm, struct kvm_dtable *dt)
+{
+	if (!vm->idt)
+		vm->idt = vm_vaddr_alloc_page(vm);
+
+	dt->base = vm->idt;
+	dt->limit = NUM_INTERRUPTS * sizeof(struct idt_entry) - 1;
+}
+
+void sync_exception_handlers_to_guest(struct kvm_vm *vm)
+{
+	*(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers;
+}
+
 void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu)
 {
 	struct kvm_vm *vm = vcpu->vm;
 	struct kvm_sregs sregs;
 
 	vcpu_sregs_get(vcpu, &sregs);
-	sregs.idt.base = vm->idt;
-	sregs.idt.limit = NUM_INTERRUPTS * sizeof(struct idt_entry) - 1;
+	kvm_setup_idt(vcpu->vm, &sregs.idt);
 	sregs.gdt.base = vm->gdt;
 	sregs.gdt.limit = getpagesize() - 1;
 	kvm_seg_set_kernel_data_64bit(NULL, DEFAULT_DATA_SELECTOR, &sregs.gs);
 	vcpu_sregs_set(vcpu, &sregs);
-	*(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers;
+	sync_exception_handlers_to_guest(vm);
 }
 
 void vm_install_exception_handler(struct kvm_vm *vm, int vector,