From nobody Sun Feb 8 12:36:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15E11EB64DD for ; Tue, 25 Jul 2023 22:03:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229562AbjGYWC4 (ORCPT ); Tue, 25 Jul 2023 18:02:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231807AbjGYWCa (ORCPT ); Tue, 25 Jul 2023 18:02:30 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D58B2681 for ; Tue, 25 Jul 2023 15:02:02 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d1851c52f3dso1314923276.1 for ; Tue, 25 Jul 2023 15:02:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690322514; x=1690927314; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=l8eGH3ygkEuOZ47kPtRCFJ0Al+rLHkYn4Wu/qaGwYZM=; b=oWQVsf2hHp/RwHjoKxnpR4SZrxj+rOnThYu9FS/jYxi5XEbeFnzYG7NMqLjxRL2lrA 4C2tpylAk/xvBxDKrmWOS6gs/zdhwq5kgSWEW41GCSbc9ROJsuB0Z2gwJOIgTpoZw8/K Atk1vTkBiUXHM5xMzHWHIk9Y7oAPdBPKz1AgKbuXpEAlEUqihSia2Tlb+SztD3o1fUWX g7iho/BTaUlxiTmJKzge0op/unGLVjv2X67ONnz9qyHPCuN257mhvDOT/2dkduB0vee4 wztnViOxci0b3Jz7exmZy1CcFZApchSk6eyJmeHU2LykxhVfj279vWbV1E3VBjFyU5Bv Vu8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690322514; x=1690927314; 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=l8eGH3ygkEuOZ47kPtRCFJ0Al+rLHkYn4Wu/qaGwYZM=; b=mDwtmneJVE9f0FM93qhBQHpWM6w8KbNDZbiQVc93JhB+7vd4qXAmXaZdUIV/aRt0Cc vMlDYdrDXCFyasfJD7Du1W9a2KZXXRrWeityD0ikgP6Of4Wl7ic3e+6qQfz1GVymkZYH BPB1lkpqRd2beYBFO+ub4xFO1qEKsrX07KiJEhY0jZGUBIrqOFamwKxUgVqcUQT5WUZ+ WXglcjUkskbjWRPZxsnDF2qR4sF9BRmWsrwyrgPsodZcyWYMya6gI9ZrnV8uk2mQJhvV 8AE4i7BoOsGTFeiXGdI7uvaU+cMugBx/0284Wv68tUY9QEna2fCt4SvR5xkjODjUDdU2 +Ffw== X-Gm-Message-State: ABy/qLbij5a5qjIdlSJk/Ern6EbjTXHXUZuym00W9ceJw7lMLd4qVLEq 58aqOJZ/rIa5TBce3ygEcm4oVn1b578X X-Google-Smtp-Source: APBJJlFegGohqWqOdSnA6IS8sXWJ8VHwoCGraN7Fw++Y6c88kB55gpqKOQlvPxRwGPoMkWW8QyxDbkskqqQz X-Received: from afranji.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:47f1]) (user=afranji job=sendgmr) by 2002:a25:abd2:0:b0:d05:e080:63c6 with SMTP id v76-20020a25abd2000000b00d05e08063c6mr2231ybi.9.1690322514251; Tue, 25 Jul 2023 15:01:54 -0700 (PDT) Date: Tue, 25 Jul 2023 22:00:57 +0000 In-Reply-To: <20230725220132.2310657-1-afranji@google.com> Mime-Version: 1.0 References: <20230725220132.2310657-1-afranji@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230725220132.2310657-5-afranji@google.com> Subject: [PATCH v4 04/28] KVM: selftests: Refactor steps in vCPU descriptor table initialization From: Ryan Afranji To: linux-kselftest@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, isaku.yamahata@intel.com, sagis@google.com, erdemaktas@google.com, afranji@google.com, runanwang@google.com, shuah@kernel.org, drjones@redhat.com, maz@kernel.org, bgardon@google.com, jmattson@google.com, dmatlack@google.com, peterx@redhat.com, oupton@google.com, ricarkol@google.com, yang.zhong@intel.com, wei.w.wang@intel.com, xiaoyao.li@intel.com, pgonda@google.com, eesposit@redhat.com, borntraeger@de.ibm.com, eric.auger@redhat.com, wangyanan55@huawei.com, aaronlewis@google.com, vkuznets@redhat.com, pshier@google.com, axelrasmussen@google.com, zhenzhong.duan@intel.com, maciej.szmigiero@oracle.com, like.xu@linux.intel.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, ackerleytng@google.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ackerley Tng 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 Change-Id: I4a1229173a4e09d6a3770d4b67edc8cdfe83f868 Signed-off-by: Ryan Afranji --- .../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 a4923f92460d..2a83a23801b0 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -1060,6 +1060,8 @@ struct idt_entry { uint32_t offset2; uint32_t reserved; }; =20 +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/tes= ting/selftests/kvm/lib/x86_64/processor.c index 78dd918b9a92..4893a778fff5 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -1124,19 +1124,32 @@ void vm_init_descriptor_tables(struct kvm_vm *vm) DEFAULT_CODE_SELECTOR); } =20 +void kvm_setup_idt(struct kvm_vm *vm, struct kvm_dtable *dt) +{ + if (!vm->idt) + vm->idt =3D vm_vaddr_alloc_page(vm); + + dt->base =3D vm->idt; + dt->limit =3D 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)) =3D vm= ->handlers; +} + void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu) { struct kvm_vm *vm =3D vcpu->vm; struct kvm_sregs sregs; =20 vcpu_sregs_get(vcpu, &sregs); - sregs.idt.base =3D vm->idt; - sregs.idt.limit =3D NUM_INTERRUPTS * sizeof(struct idt_entry) - 1; + kvm_setup_idt(vcpu->vm, &sregs.idt); sregs.gdt.base =3D vm->gdt; sregs.gdt.limit =3D 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)) =3D vm= ->handlers; + sync_exception_handlers_to_guest(vm); } =20 void vm_install_exception_handler(struct kvm_vm *vm, int vector, --=20 2.41.0.487.g6d72f3e995-goog