[RFC,v5,04/29] KVM: selftests: Refactor steps in vCPU descriptor table initialization
Message ID | 20231212204647.2170650-5-sagis@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993215vqy; Tue, 12 Dec 2023 12:47:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IELLOq4/WTSm5BKeT31+CxPeS4tibOhLkKhZKHsCM5R8QhL7oiw6U1SvgDtD5+ovfYww/TG X-Received: by 2002:a17:902:e84c:b0:1d0:4cde:6e2b with SMTP id t12-20020a170902e84c00b001d04cde6e2bmr4702401plg.57.1702414041402; Tue, 12 Dec 2023 12:47:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414041; cv=none; d=google.com; s=arc-20160816; b=Gg//mUWpkRJJKuqTQzgm0bNWVsWq5Xs+KGbdnggRD+bxMafWzCFIn4hthSE5KFK/Im AEr3ihQWTQeGwoVC68AO0+4cIES4W0adwjJO8e3VAyofcv10ErIjxaXa2IHozifuXKti ewmYVqwygOKw6bzic7kwlQujVJn/DnylDDJRZdeVwdHk6TUmQwlFodsxPH5ixvGAcMHm y590v10JUy+FpD6e2Bb0tK9UCP5eqdrxi1+qZr2Etr5Zeo5A+pmfQE/6OZtKPdtvAqAS LjWv2um4q5tZd0WImVPGyD4cxwa/hPgoHfUgCaV2qIl/Tw8eP5IvXwnRQtVJLprUsDU1 ytMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=x1lodvb6OtWfxjhf5SoaUUtA7ex1XZFNPc9w8OKKvUg=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=AMsercsa/I88W55iSrv1u4heK4dt/ATYBoL0BovW9t14hxQwVLtxFBdmAlehzRRFiK walBfI+qZRYED5LUQzYKmV/bIQ2A4CclsKzIsk0A6BhXYVeCZZPfu0qYtBAuvlW7yXTM HHZFLf7Pl2HIes5MMjKX8PLFNkOs7tP2qc9a3cBZmZ1KV0Uagb4lK4XMBAV1YzqHu8D7 55hkGuBOCax+JHEveBW6263Ly7BeuTRzOtXiGIn/rNcvoElkZ0SQzW5s0oALZ5sw5Ucj X+nBQh2T5SyqdjqOG+/IYq3FwPDLiNaPkYS3UargmpNz7FdA4VAPRP1wqhdd0WlVf+c2 Q2lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=V05TFJWO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id j4-20020a170902690400b001d3156ab971si4267849plk.389.2023.12.12.12.47.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=V05TFJWO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id CBCAB80F924F; Tue, 12 Dec 2023 12:47:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377301AbjLLUrC (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Tue, 12 Dec 2023 15:47:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377254AbjLLUq7 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 12 Dec 2023 15:46:59 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86676D2 for <linux-kernel@vger.kernel.org>; Tue, 12 Dec 2023 12:47:05 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d6b751dabcso53483207b3.1 for <linux-kernel@vger.kernel.org>; Tue, 12 Dec 2023 12:47:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414025; x=1703018825; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=x1lodvb6OtWfxjhf5SoaUUtA7ex1XZFNPc9w8OKKvUg=; b=V05TFJWOpwlhLd3k3oEhlQ0LTZ8oXfSmFksigdfx8HAUfSFJdOmSQHvqPGxZt5PrbS ZJrzdXRUlDa19nPAB9Im4bwuejasfYoRwtTbYtibufvUVZzePX35jBD7Q8hsicivC8ZO 2UV+Yt65WPJ1LDBH1HParEl96fCwNr5/TbeHKw4j8Qr43mnhb0e4Z+YeTRSwd3TdKALe i/0Ez1XLfcJ2DF1hg3xGLq92PEhd+b0gVsS5IS7wPzgBAVdCklX7LtKQNxdeyJqRGIez f3QNnVtCDji5mHOO7O3hOegyQXQIlrNTd/YX1j9j018MR9/v9J9pcdOO90iJYvSdS4TW QyeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414025; x=1703018825; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x1lodvb6OtWfxjhf5SoaUUtA7ex1XZFNPc9w8OKKvUg=; b=wBFAWMw3CzBzwj26lNOd9kKgNUKwppWWOslt/Kw+HdMRRpLnbo9GHLAAEj4HDofgm9 r+MG3OE+tULyju67JSh+fVMolmff7BdDo+YKZY8mL3140L4Z5w06PBC3qDq7nbAEShjU bduw5zsuTCWFTsYXSV/plDOGLLmMZnqbcoRb6q1+g0WRal/4J+4+HIjrktQLq+Ps4Bng ASEYudyV3gJtikGuf2lR8fgaGk5J4vlaw1tRpf1mGVeg2MFNnsGKCmlobbJlE/jhPCRQ DMXQi5EVnqk5BfbdPJKqeumBEwgXHGcufWy4JVrmZ8wQIGjjcwTUS22ZKJXoIaugEvf8 2C5A== X-Gm-Message-State: AOJu0Yz0XW9KSrL0Th2+k6stXGILiR902uGcqmDNfaKbSgmTx0wWOUy5 c4RIJ0uisfEfFhd6umwqI1yD+x5rrw== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a25:ab26:0:b0:da0:3117:f35 with SMTP id u35-20020a25ab26000000b00da031170f35mr56027ybi.3.1702414024705; Tue, 12 Dec 2023 12:47:04 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:19 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-5-sagis@google.com> Subject: [RFC PATCH v5 04/29] KVM: selftests: Refactor steps in vCPU descriptor table initialization From: Sagi Shahar <sagis@google.com> To: linux-kselftest@vger.kernel.org, Ackerley Tng <ackerleytng@google.com>, Ryan Afranji <afranji@google.com>, Erdem Aktas <erdemaktas@google.com>, Sagi Shahar <sagis@google.com>, Isaku Yamahata <isaku.yamahata@intel.com> Cc: Sean Christopherson <seanjc@google.com>, Paolo Bonzini <pbonzini@redhat.com>, Shuah Khan <shuah@kernel.org>, Peter Gonda <pgonda@google.com>, Haibo Xu <haibo1.xu@intel.com>, Chao Peng <chao.p.peng@linux.intel.com>, Vishal Annapurve <vannapurve@google.com>, Roger Wang <runanwang@google.com>, Vipin Sharma <vipinsh@google.com>, jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:18 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110505623524115 X-GMAIL-MSGID: 1785110505623524115 |
Series |
TDX KVM selftests
|
|
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
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 --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,