Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp234400rwb; Tue, 25 Jul 2023 15:04:39 -0700 (PDT) X-Google-Smtp-Source: APBJJlFBeIlzE8Ud5kPyX854/+tUZA+VoriD/oB5PUk5cZpp0umprqFXrrOc5pl7X76Qenzf722i X-Received: by 2002:a05:6a21:78a3:b0:12e:ae87:45d2 with SMTP id bf35-20020a056a2178a300b0012eae8745d2mr302769pzc.51.1690322679301; Tue, 25 Jul 2023 15:04:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690322679; cv=none; d=google.com; s=arc-20160816; b=DsiUrD1WtMsNuXfuY2/MBvEgQXG8+G6s2hSEUm4JO1t5vuGUAwVxrkDacwS6H8XD/q FCXLv8ipUSiGbB7cfJd4abaIP0ohy7k/oEolY2sks06ao2FrpfNSoqxoHJYda14mSVA4 t3T12KB8k7WCynYTvTAKngu8TmF7e9ITjbiVjNfB/+4u7xnxzYTxhNa828T96bnX1KGZ Fbp4WEdkBd5bYSOBWxqrzthT3Z0OFK6bmOnaNSiSh8+z5w39X7VUaFdzySkCrlMbLfIY mZtnngCy+ysXqExorjfwWDzGmt10FR7TzQaxZEch0m5D8J+j+alJj6mtbaPJDc2v9ibO D5Jg== 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=l8eGH3ygkEuOZ47kPtRCFJ0Al+rLHkYn4Wu/qaGwYZM=; fh=1/sgOwi5AuU8IZymeFKs1Rjnf4QELnrK+C7GntqU2hA=; b=dL0B6ermRZpW0kJMVLRMaIo2Fk5SLYWUongdtVZVX2wHeAYhBk3ja9eZ0Ps/Dgcuea E+fai5ASlVVfcUX7iK5uuBgKPwRwklxCtiAmylVugD9oBS2ja1CNlhyARNLpge2cvcnc RepeuEH66IOoMskmCwzQkw9J4wqtYLuzw9R2Uzrx7FX1t4wEXXOzWRYFzVogDGKQ0qUc JYKstbEKr6pm2w2OI9ygZm3LNkwiJAixSuCO9Y/Se7JmdQZl4ObAo23kyw8BThnt9d64 TdestVIIAfwjfvDdf/Sf59J/kir2eVxPHT6QYiA+PSviZI4Zks/ts8JBiZbyyqD9x5Wf CsJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=oWQVsf2h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g2-20020a056a000b8200b006862e207c34si12412414pfj.66.2023.07.25.15.04.26; Tue, 25 Jul 2023 15:04:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=oWQVsf2h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231859AbjGYWCz (ORCPT + 99 others); Tue, 25 Jul 2023 18:02:55 -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-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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; }; +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 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); } +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, -- 2.41.0.487.g6d72f3e995-goog