From patchwork Tue Dec 12 20:46:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 13489863 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A6DAC4332F for ; Tue, 12 Dec 2023 20:47:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D5D6C6B036A; Tue, 12 Dec 2023 15:47:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D0D506B036C; Tue, 12 Dec 2023 15:47:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B61B96B036D; Tue, 12 Dec 2023 15:47:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A130E6B036A for ; Tue, 12 Dec 2023 15:47:07 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 681F01C13F1 for ; Tue, 12 Dec 2023 20:47:07 +0000 (UTC) X-FDA: 81559350894.08.5902B7D Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf09.hostedemail.com (Postfix) with ESMTP id 9C82B14002B for ; Tue, 12 Dec 2023 20:47:05 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=um10VLle; spf=pass (imf09.hostedemail.com: domain of 3yMZ4ZQUKCGsbJPRbPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--sagis.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3yMZ4ZQUKCGsbJPRbPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--sagis.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702414025; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=x1lodvb6OtWfxjhf5SoaUUtA7ex1XZFNPc9w8OKKvUg=; b=21vGQxZyUYUzI+oIePRygSsueZTDWS+CAz3G90sjHxRdaxBfBwG79RDYqD7OruBY0zou5s 0AJ39nVWgzmIadw5nnz8lqKA9MiTakAAoheCR1i5TN/JzurBZIL7m8ebzfLSDRga4GkxLQ RmC/4HWLEYCVP/WWaoJu/B8KaULvBXM= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=um10VLle; spf=pass (imf09.hostedemail.com: domain of 3yMZ4ZQUKCGsbJPRbPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--sagis.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3yMZ4ZQUKCGsbJPRbPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--sagis.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702414025; a=rsa-sha256; cv=none; b=6yRk1BuAOtFIQ2rApIQzdTSckKTb+Pht+WCZrN8+JLmQ9zONJTV+E/+HzKzIImBVVHzcPU AWaMzhQBuqBrJ+qczHoxPrstwrjd/YTKNXbV1OtPbSui3C+IUMvRE8DGghvuZhW3+whsSK fifjrH4+H5BKPqLfIJz5yEzTpLG3myI= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dbcb864a3dbso611994276.2 for ; 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=kvack.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=um10VLlewAIoDOcC0sxp61qpDAFSx21hjxqufJ3aDnNJ7d5Yf8r1b1elJraVwxoYmE HyhSmR0a6+WZAiPOW6huSqm7f7YfmmmFWL3yPXxRUvFmEka6Rd/6LO1mUsRgCBPFGDiu uf3iNb+lU0R/MhyRpw4lN+0L+cLOzwlu5ZqqyH209q54aVMbm7xVFvbcVqqRW0zKVk5a KogWIbexYvYK08hRlP0mkuUpx0q6zvBzNSLC+L6Ias7vsiCilQ6dMcsmEzJ2AkGiDMAD rHdwlaZD2x530t1pBIk+U8qPYCUEVuABQtYCD4AswviEryAY5fOhRhqRX/rh6ttdMc8C F+FA== 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=JPuaAx9wpHMn8XkpA0fEGN2OKGJ0bcyb4sGNLcnthYYlWWkjEqQw0gZEAGWwoBee3+ EjVr5cIad1lvMeQ5VGk99UICbiunPm88FEZ854Kcwv2fp/DYp6MDBsVJDPo6E2YRFIWT MGXC+Wb9bx+3lWaLuVHwHnQBir0hPrx72degsV2hy3rO3/Sp9+9CZI3uSfRgyjRlHHsI e6DOHokqkBvzNF7YdGpKdaJCCTi6EpXRYysnZesPygpz9C3r/7Ccm2+PN9d5GRp7+bb7 A30lR51pGddlF/OP01BmTy2cvovV4Ar7Hk23qoTst6GfT6ji+LeLAGJGxzoIAoeTSq7r 8UPw== X-Gm-Message-State: AOJu0YwxbHT1aFDNXkEzuo3M1h1nrNcsYW4ql7pgpRxAwTP+rKFSlifJ GLSrhJxvtxebeaL6W8Yn77uk/iirVw== X-Google-Smtp-Source: AGHT+IFA0fKoM26J8ai3StFQnJI6WfpZDk7m02MxQ8rkZS/AQ4lQ2xMjuz7+KEcD4kLjVBP5Xztzh7FZew== 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 To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Rspamd-Queue-Id: 9C82B14002B X-Rspam-User: X-Stat-Signature: m3s76eb1mn9a5k5wqzbj346irsewty88 X-Rspamd-Server: rspam01 X-HE-Tag: 1702414025-269823 X-HE-Meta: U2FsdGVkX1+Jhm7/rWM+xMlKnEqZcL+S+KioJ+Ya9DFhXfG5y0SJB5PmrXcLih3Jl4LCN1MJJReZqswvMc8Hx+VX66MMDVDDZRRnC66xeTvIV50fc0gSYP3VOBBRoVTCNWDX1d7s9PHo4rqYBEMT9COi0+JPL31DWd0K2qveVWcheMXy/NEzSRNSemTmKP6VWj/JLUHap8NSPN1aukHJLrPfNx/g7PBJTEAEGNF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TOpjpiGqRWhYoFP28NZf215Oj39EEEMvLgnq7QZ1D+m0nWkm3/02ipItwiWNYWAryRWHFd9A1OAVKlSx/nj+Gq/8rIjWljXANKZ0NH6cL5MtqWEwiId8ycaciAxxYT1lJFhCPcU6UyWUQc0tXAvF8IdS5vIzL0kTK7PFsOv167tIr/rZlUcQ/pt6kpTvIvxiuG3EB7VovJ7HUk07cJK2VEg6ZLrAk8fKjguzSkcMOPejRYiGaY1PJUmpJPD0AU6VepTKc0BrN6jCQQAk0lQetNJ06Z7TeBPbeEoxVrGH5suf5YDWoaggdlRgbk1tiiFPNAd6uJ41u73bk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- .../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); + + 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,