From patchwork Fri May 20 23:32:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857554 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 C90FDC433F5 for ; Fri, 20 May 2022 23:33:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354127AbiETXdD (ORCPT ); Fri, 20 May 2022 19:33:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354142AbiETXdB (ORCPT ); Fri, 20 May 2022 19:33:01 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C379C1AB7AD for ; Fri, 20 May 2022 16:32:55 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id oa7-20020a17090b1bc700b001df2567933eso7686938pjb.4 for ; Fri, 20 May 2022 16:32:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=W8jgKI50ePyABDO/nK5JyQIAmfeyEG33bBQNVNWWMLo=; b=CALYiyk62hFjuWncJSXe274CFB+XqrNlB/FTOa8h3G/K7SxwE2IDqfJiXm6KYSqLkE sUtO92G6OidMqeU7ob1/NmxOAxRA3clJ8Qkvf9iiX/qYXg94rz2z1Aoi/l453wN5+zGV 3LV5u0Podgo+TKy77VEUc2syPY+C6EQ3fg0IP6dH/yISLNs9CKX7WQZvPHRsaYvLkNqR Z2agefWMkRmGzbrtmtDcxCjiM9Ko8H2kI1kh2hozGJFl1BUZ6ZT+YgK5ZbW1JvWXw2k3 m/yo1G2+b1CnOTULYOVGNWLZ7O2thkt5C96m2lhBb9IsdesX+667xJ7q+9ELl6zukIjv n+/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=W8jgKI50ePyABDO/nK5JyQIAmfeyEG33bBQNVNWWMLo=; b=pWH2w9T8cuF7uyD64Yqcp2bI+CahlX2UYUot06Esb/lIWCZWkPSZGO1Xn5LEUbRdEH Dn37DswqkkgVohIx4+3gZevLdmM4bvK3hKCmi2CKgOmBuKDR6G32yPnLsFQwlW0EepTQ CD744yiI4qA+He/sqB5RTPbrUsv/e/Wl1PVe99RDzYVF97jOiNK0ZndlUF5rrznmiPp9 PjMehkP+X10Nto+KybflZmOZRCEdE7QZFzrhZ4wJhNkxlyp4CKz6sTD1GwfaukpP7xJv tcj7VUPkktUNT1H5qio0ONGwhhoYy6Jfj/6m+sMFiIcGa8xeIEU1u9AhCKPYNJJHklAu NRzw== X-Gm-Message-State: AOAM531t0UVeRbVlnN1rt7ft1lSqr4yoUK3xfwICiR876nAHEvoWimJd yHrKpqLZCTUKpWLga6I8h7/fetySJKpLfg== X-Google-Smtp-Source: ABdhPJx5Nt5SgXktvTUHC0boR7QQERVLbLQJhVnl5M4X2Lk4ai5G/aRczaun5e88iLc2WK3VFh9Z8a+NOh9AlQ== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:902:850b:b0:15f:2a59:1c02 with SMTP id bj11-20020a170902850b00b0015f2a591c02mr11797156plb.27.1653089575148; Fri, 20 May 2022 16:32:55 -0700 (PDT) Date: Fri, 20 May 2022 23:32:39 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-2-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 01/11] KVM: selftests: Replace x86_page_size with PG_LEVEL_XX From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org x86_page_size is an enum used to communicate the desired page size with which to map a range of memory. Under the hood they just encode the desired level at which to map the page. This ends up being clunky in a few ways: - The name suggests it encodes the size of the page rather than the level. - In other places in x86_64/processor.c we just use a raw int to encode the level. Simplify this by adopting the kernel style of PG_LEVEL_XX enums and pass around raw ints when referring to the level. This makes the code easier to understand since these macros are very common in KVM MMU code. Signed-off-by: David Matlack --- .../selftests/kvm/include/x86_64/processor.h | 18 ++++++---- .../selftests/kvm/lib/x86_64/processor.c | 33 ++++++++++--------- .../selftests/kvm/max_guest_memory_test.c | 2 +- .../selftests/kvm/x86_64/mmu_role_test.c | 2 +- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 37db341d4cc5..434a4f60f4d9 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -465,13 +465,19 @@ void vcpu_set_hv_cpuid(struct kvm_vm *vm, uint32_t vcpuid); struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vm *vm, uint32_t vcpuid); void vm_xsave_req_perm(int bit); -enum x86_page_size { - X86_PAGE_SIZE_4K = 0, - X86_PAGE_SIZE_2M, - X86_PAGE_SIZE_1G, +enum pg_level { + PG_LEVEL_NONE, + PG_LEVEL_4K, + PG_LEVEL_2M, + PG_LEVEL_1G, + PG_LEVEL_512G, + PG_LEVEL_NUM }; -void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, - enum x86_page_size page_size); + +#define PG_LEVEL_SHIFT(_level) ((_level - 1) * 9 + 12) +#define PG_LEVEL_SIZE(_level) (1ull << PG_LEVEL_SHIFT(_level)) + +void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level); /* * Basic CPU control in CR0 diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 9f000dfb5594..f733c5b02da5 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -190,7 +190,7 @@ static void *virt_get_pte(struct kvm_vm *vm, uint64_t pt_pfn, uint64_t vaddr, int level) { uint64_t *page_table = addr_gpa2hva(vm, pt_pfn << vm->page_shift); - int index = vaddr >> (vm->page_shift + level * 9) & 0x1ffu; + int index = (vaddr >> PG_LEVEL_SHIFT(level)) & 0x1ffu; return &page_table[index]; } @@ -199,15 +199,15 @@ static struct pageUpperEntry *virt_create_upper_pte(struct kvm_vm *vm, uint64_t pt_pfn, uint64_t vaddr, uint64_t paddr, - int level, - enum x86_page_size page_size) + int current_level, + int target_level) { - struct pageUpperEntry *pte = virt_get_pte(vm, pt_pfn, vaddr, level); + struct pageUpperEntry *pte = virt_get_pte(vm, pt_pfn, vaddr, current_level); if (!pte->present) { pte->writable = true; pte->present = true; - pte->page_size = (level == page_size); + pte->page_size = (current_level == target_level); if (pte->page_size) pte->pfn = paddr >> vm->page_shift; else @@ -218,20 +218,19 @@ static struct pageUpperEntry *virt_create_upper_pte(struct kvm_vm *vm, * a hugepage at this level, and that there isn't a hugepage at * this level. */ - TEST_ASSERT(level != page_size, + TEST_ASSERT(current_level != target_level, "Cannot create hugepage at level: %u, vaddr: 0x%lx\n", - page_size, vaddr); + current_level, vaddr); TEST_ASSERT(!pte->page_size, "Cannot create page table at level: %u, vaddr: 0x%lx\n", - level, vaddr); + current_level, vaddr); } return pte; } -void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, - enum x86_page_size page_size) +void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) { - const uint64_t pg_size = 1ull << ((page_size * 9) + 12); + const uint64_t pg_size = PG_LEVEL_SIZE(level); struct pageUpperEntry *pml4e, *pdpe, *pde; struct pageTableEntry *pte; @@ -256,20 +255,22 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, * early if a hugepage was created. */ pml4e = virt_create_upper_pte(vm, vm->pgd >> vm->page_shift, - vaddr, paddr, 3, page_size); + vaddr, paddr, PG_LEVEL_512G, level); if (pml4e->page_size) return; - pdpe = virt_create_upper_pte(vm, pml4e->pfn, vaddr, paddr, 2, page_size); + pdpe = virt_create_upper_pte(vm, pml4e->pfn, vaddr, paddr, PG_LEVEL_1G, + level); if (pdpe->page_size) return; - pde = virt_create_upper_pte(vm, pdpe->pfn, vaddr, paddr, 1, page_size); + pde = virt_create_upper_pte(vm, pdpe->pfn, vaddr, paddr, PG_LEVEL_2M, + level); if (pde->page_size) return; /* Fill in page table entry. */ - pte = virt_get_pte(vm, pde->pfn, vaddr, 0); + pte = virt_get_pte(vm, pde->pfn, vaddr, PG_LEVEL_4K); TEST_ASSERT(!pte->present, "PTE already present for 4k page at vaddr: 0x%lx\n", vaddr); pte->pfn = paddr >> vm->page_shift; @@ -279,7 +280,7 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr) { - __virt_pg_map(vm, vaddr, paddr, X86_PAGE_SIZE_4K); + __virt_pg_map(vm, vaddr, paddr, PG_LEVEL_4K); } static struct pageTableEntry *_vm_get_page_table_entry(struct kvm_vm *vm, int vcpuid, diff --git a/tools/testing/selftests/kvm/max_guest_memory_test.c b/tools/testing/selftests/kvm/max_guest_memory_test.c index 3875c4b23a04..15f046e19cb2 100644 --- a/tools/testing/selftests/kvm/max_guest_memory_test.c +++ b/tools/testing/selftests/kvm/max_guest_memory_test.c @@ -244,7 +244,7 @@ int main(int argc, char *argv[]) #ifdef __x86_64__ /* Identity map memory in the guest using 1gb pages. */ for (i = 0; i < slot_size; i += size_1gb) - __virt_pg_map(vm, gpa + i, gpa + i, X86_PAGE_SIZE_1G); + __virt_pg_map(vm, gpa + i, gpa + i, PG_LEVEL_1G); #else for (i = 0; i < slot_size; i += vm_get_page_size(vm)) virt_pg_map(vm, gpa + i, gpa + i); diff --git a/tools/testing/selftests/kvm/x86_64/mmu_role_test.c b/tools/testing/selftests/kvm/x86_64/mmu_role_test.c index da2325fcad87..bdecd532f935 100644 --- a/tools/testing/selftests/kvm/x86_64/mmu_role_test.c +++ b/tools/testing/selftests/kvm/x86_64/mmu_role_test.c @@ -35,7 +35,7 @@ static void mmu_role_test(u32 *cpuid_reg, u32 evil_cpuid_val) run = vcpu_state(vm, VCPU_ID); /* Map 1gb page without a backing memlot. */ - __virt_pg_map(vm, MMIO_GPA, MMIO_GPA, X86_PAGE_SIZE_1G); + __virt_pg_map(vm, MMIO_GPA, MMIO_GPA, PG_LEVEL_1G); r = _vcpu_run(vm, VCPU_ID); From patchwork Fri May 20 23:32:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857557 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 CF586C433FE for ; Fri, 20 May 2022 23:33:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240142AbiETXdL (ORCPT ); Fri, 20 May 2022 19:33:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354149AbiETXdB (ORCPT ); Fri, 20 May 2022 19:33:01 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 610AB1AB7B5 for ; Fri, 20 May 2022 16:32:57 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id s2-20020a17090302c200b00158ea215fa2so4723032plk.3 for ; Fri, 20 May 2022 16:32:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=3YnW/PaLyJgDTu61KobGsZ8UH0DNCazauPQKjEmJn0Q=; b=C97kavYd4fpevG4pJ4wjQloaKNU7HUuYB43iSNRjLiTy9rUtPh1fTOi3dxsiRr/XvZ KOnUIm2wrVVX9KYpelNOmr74727LmQ1oLmeanxG8lb5plfSZTmuQT9kF5LfO5Xw1TgKb o5hulL5BdzWI8g620RSMTk3vPoDtxAXGuIixFuZ4usknB9fQYLnK7lHofqtScxieKRte B7tl8YQSvya9/Y6CxYvw41abI3vWTP16MsKVPOc/8lmGiXaoRkZ6BszI68bzqrRAkkNX Eu+gpInnqduMxfYDHbnVSYGtvG/MBgI//9ReLVn8LYjqS7tXSYP6K11Rbscvx4kgQy1s TgGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=3YnW/PaLyJgDTu61KobGsZ8UH0DNCazauPQKjEmJn0Q=; b=eMLbWGr3ZmVktC2OVyBJ3bhsHBzMP6mwBR15HUpWp4CU7ityW6HKsuwT8bDXCXGKvI M7LgKdsk1EOtiw/Ljvkamh+NpiVONm+F8RPE2hTYQrDbYXHetheBoPhv74JjLwxW90Gz pZXo94lAhEYzrztYVatXG2P8VAnz72OSmId6htuR2ei0W36b8pzovOF/9DFTnyIE/PXt J8cRyIa3zT7+DV8k4BX6ZHqmKsLrsk8Xde2T5Sa2wM20JLSwVmcOiZPGDg6LnAPMFYYx b+Pmzn1vZ0rUO/SjrSbFSCNMx+p9j/KwGF2pvoJJWyPrhFe+sC4XZm2uCLoz49bZ+P/M awXg== X-Gm-Message-State: AOAM530NIRBvAaQrXOCMxeWeRWhHlZSrKL70nUXNZ3K1SVDlLxuPm5pI CAXTONdoUycEhvtc94/WTn+UgisjwZPM0w== X-Google-Smtp-Source: ABdhPJwmsbCwcd9CjIjBaaF0r9bvCjgfWubEcG5HwOuwGFK4kgtamME9+0F5jTQtQnKoe2yyWbu9MsA+TMbrCg== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:902:8d83:b0:161:f4b3:6457 with SMTP id v3-20020a1709028d8300b00161f4b36457mr4822213plo.122.1653089576872; Fri, 20 May 2022 16:32:56 -0700 (PDT) Date: Fri, 20 May 2022 23:32:40 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-3-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 02/11] KVM: selftests: Add option to create 2M and 1G EPT mappings From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The current EPT mapping code in the selftests only supports mapping 4K pages. This commit extends that support with an option to map at 2M or 1G. This will be used in a future commit to create large page mappings to test eager page splitting. No functional change intended. Signed-off-by: David Matlack --- tools/testing/selftests/kvm/lib/x86_64/vmx.c | 110 ++++++++++--------- 1 file changed, 60 insertions(+), 50 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c index d089d8b850b5..fdc1e6deb922 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c @@ -392,80 +392,90 @@ void nested_vmx_check_supported(void) } } -void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm, - uint64_t nested_paddr, uint64_t paddr) +static void nested_create_pte(struct kvm_vm *vm, + struct eptPageTableEntry *pte, + uint64_t nested_paddr, + uint64_t paddr, + int current_level, + int target_level) +{ + if (!pte->readable) { + pte->writable = true; + pte->readable = true; + pte->executable = true; + pte->page_size = (current_level == target_level); + if (pte->page_size) + pte->address = paddr >> vm->page_shift; + else + pte->address = vm_alloc_page_table(vm) >> vm->page_shift; + } else { + /* + * Entry already present. Assert that the caller doesn't want + * a hugepage at this level, and that there isn't a hugepage at + * this level. + */ + TEST_ASSERT(current_level != target_level, + "Cannot create hugepage at level: %u, nested_paddr: 0x%lx\n", + current_level, nested_paddr); + TEST_ASSERT(!pte->page_size, + "Cannot create page table at level: %u, nested_paddr: 0x%lx\n", + current_level, nested_paddr); + } +} + + +void __nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm, + uint64_t nested_paddr, uint64_t paddr, int target_level) { - uint16_t index[4]; - struct eptPageTableEntry *pml4e; + const uint64_t page_size = PG_LEVEL_SIZE(target_level); + struct eptPageTableEntry *pt = vmx->eptp_hva, *pte; + uint16_t index; TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use " "unknown or unsupported guest mode, mode: 0x%x", vm->mode); - TEST_ASSERT((nested_paddr % vm->page_size) == 0, + TEST_ASSERT((nested_paddr % page_size) == 0, "Nested physical address not on page boundary,\n" - " nested_paddr: 0x%lx vm->page_size: 0x%x", - nested_paddr, vm->page_size); + " nested_paddr: 0x%lx page_size: 0x%lx", + nested_paddr, page_size); TEST_ASSERT((nested_paddr >> vm->page_shift) <= vm->max_gfn, "Physical address beyond beyond maximum supported,\n" " nested_paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x", paddr, vm->max_gfn, vm->page_size); - TEST_ASSERT((paddr % vm->page_size) == 0, + TEST_ASSERT((paddr % page_size) == 0, "Physical address not on page boundary,\n" - " paddr: 0x%lx vm->page_size: 0x%x", - paddr, vm->page_size); + " paddr: 0x%lx page_size: 0x%lx", + paddr, page_size); TEST_ASSERT((paddr >> vm->page_shift) <= vm->max_gfn, "Physical address beyond beyond maximum supported,\n" " paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x", paddr, vm->max_gfn, vm->page_size); - index[0] = (nested_paddr >> 12) & 0x1ffu; - index[1] = (nested_paddr >> 21) & 0x1ffu; - index[2] = (nested_paddr >> 30) & 0x1ffu; - index[3] = (nested_paddr >> 39) & 0x1ffu; - - /* Allocate page directory pointer table if not present. */ - pml4e = vmx->eptp_hva; - if (!pml4e[index[3]].readable) { - pml4e[index[3]].address = vm_alloc_page_table(vm) >> vm->page_shift; - pml4e[index[3]].writable = true; - pml4e[index[3]].readable = true; - pml4e[index[3]].executable = true; - } + for (int level = PG_LEVEL_512G; level >= PG_LEVEL_4K; level--) { + index = (nested_paddr >> PG_LEVEL_SHIFT(level)) & 0x1ffu; + pte = &pt[index]; - /* Allocate page directory table if not present. */ - struct eptPageTableEntry *pdpe; - pdpe = addr_gpa2hva(vm, pml4e[index[3]].address * vm->page_size); - if (!pdpe[index[2]].readable) { - pdpe[index[2]].address = vm_alloc_page_table(vm) >> vm->page_shift; - pdpe[index[2]].writable = true; - pdpe[index[2]].readable = true; - pdpe[index[2]].executable = true; - } + nested_create_pte(vm, pte, nested_paddr, paddr, level, target_level); - /* Allocate page table if not present. */ - struct eptPageTableEntry *pde; - pde = addr_gpa2hva(vm, pdpe[index[2]].address * vm->page_size); - if (!pde[index[1]].readable) { - pde[index[1]].address = vm_alloc_page_table(vm) >> vm->page_shift; - pde[index[1]].writable = true; - pde[index[1]].readable = true; - pde[index[1]].executable = true; - } + if (pte->page_size) + break; - /* Fill in page table entry. */ - struct eptPageTableEntry *pte; - pte = addr_gpa2hva(vm, pde[index[1]].address * vm->page_size); - pte[index[0]].address = paddr >> vm->page_shift; - pte[index[0]].writable = true; - pte[index[0]].readable = true; - pte[index[0]].executable = true; + pt = addr_gpa2hva(vm, pte->address * vm->page_size); + } /* * For now mark these as accessed and dirty because the only * testcase we have needs that. Can be reconsidered later. */ - pte[index[0]].accessed = true; - pte[index[0]].dirty = true; + pte->accessed = true; + pte->dirty = true; + +} + +void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm, + uint64_t nested_paddr, uint64_t paddr) +{ + __nested_pg_map(vmx, vm, nested_paddr, paddr, PG_LEVEL_4K); } /* From patchwork Fri May 20 23:32:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857555 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 4CFB5C433F5 for ; Fri, 20 May 2022 23:33:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354124AbiETXdH (ORCPT ); Fri, 20 May 2022 19:33:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354151AbiETXdB (ORCPT ); Fri, 20 May 2022 19:33:01 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B02B19FF4E for ; Fri, 20 May 2022 16:32:59 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id p16-20020a170902e75000b00161d96620c4so3414959plf.14 for ; Fri, 20 May 2022 16:32:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=xju6GI4UHmGb+5whaNbhMW72VwBo+kXPtVjs0cWh6sY=; b=K9W9t8d0J6nFr8X58dID4UIOp0pxQlk60VQsFcW/MHyVMMA2LzSTbh3EA9/wvNmHkQ LDXLvASr69ejIHOny9r8jidrE/HFUoxWVq+1vNx0Y5/4RN1I2F7kAeeo/k71XEEA8Y4Z 0d5BJ5qB5ZlC+3jrvDE95sHKFt7T9+mS32KPfoXQnSm5/9SyUpW8MN0Kk6S3ko5pHRVf fnRj5V5MCEqDhpJBfIhb9iAvLFZkhleRxcGmc7KellgsWz0S9ZMBFFy7w2kSlDDYDhor g4HNG3JuyGvyKNZuF0x0BTS1xcnQ0z9lvwQzyBRNTmUcC9sgMbO/yO6NRPFE8/tTmyUh bsRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=xju6GI4UHmGb+5whaNbhMW72VwBo+kXPtVjs0cWh6sY=; b=BwuJYwwuQTtbovWTVNcZJHd3zdCH9ACyySPqzlN42lWdS+5cW7EM6fifDeE9ANDxDY yKBbg8jBgwG+I+DrH/Uqfp2JzZrodRS8bUbnPZ4KABEFUWtDoFPCjNY/2dozxvW2paQM 7TM71Ei2+6aljSR0PfEta+5TtMBAQfTk0gv3x7pf8GSwl3NjOiOmU9bS8hTpTOkw+mmv eJLMdBcZLWQ010pHx5exKt8d5TzpYwMh4/xRPHUQJp4krJAgKJ7dGN4iFvW+gWaNTeSi G0sKUTn35Q8T80Y4ipBxGCwleWFifpXY8/TQ66FdshIpv7XiGoaBjTQbaInhOzwdCdFl ++6g== X-Gm-Message-State: AOAM530RhmbJFfpP7PmFEXVmP5wRREQ75OV+mzXE1jZuWQVLdt7oRENH C1Cb+uBEP9I6wbpVaxqpKMA6v+d7j0WiSA== X-Google-Smtp-Source: ABdhPJw0h0nPIGsstcGnRc98E2t70Gv2DGQJFcGqALQcaaO7BOIIbv0DbCKP4IM6ytF1hFr+P4rNBlR1L0VB1A== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a05:6a00:cc:b0:518:1348:8dc2 with SMTP id e12-20020a056a0000cc00b0051813488dc2mr12635809pfj.52.1653089578864; Fri, 20 May 2022 16:32:58 -0700 (PDT) Date: Fri, 20 May 2022 23:32:41 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-4-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 03/11] KVM: selftests: Drop stale function parameter comment for nested_map() From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org nested_map() does not take a parameter named eptp_memslot. Drop the comment referring to it. Reviewed-by: Peter Xu Signed-off-by: David Matlack --- tools/testing/selftests/kvm/lib/x86_64/vmx.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c index fdc1e6deb922..baeaa35de113 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c @@ -486,7 +486,6 @@ void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm, * nested_paddr - Nested guest physical address to map * paddr - VM Physical Address * size - The size of the range to map - * eptp_memslot - Memory region slot for new virtual translation tables * * Output Args: None * From patchwork Fri May 20 23:32:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857556 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 22284C433EF for ; Fri, 20 May 2022 23:33:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354128AbiETXdK (ORCPT ); Fri, 20 May 2022 19:33:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354155AbiETXdC (ORCPT ); Fri, 20 May 2022 19:33:02 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11CC3199B36 for ; Fri, 20 May 2022 16:33:01 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id v10-20020a17090a0c8a00b001c7a548e4f7so7700612pja.2 for ; Fri, 20 May 2022 16:33:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=/3btooTUxYy4rjHmRouQUpf5jOli5cRswOzHLZ9mznY=; b=RnEK4JnPyOne6dW0krgCNXbOT79gho9DtZKLFxdePYyxhoRzWz7LLvZrAB3sLEvUCH AEb4rEeWnTOj34Lt+T556HbMT1pa3Pk8yIeF/MaRXK+fX72qPj5h5SR81MBz+CC0unZl dgBrqclN8xw99T/cJYeFTP59Py5OgNQl/nL87a+/f/vZnVMXou1or2UU7xUFUVgkDgVO 3tD2/tJGvx+VSyXbanScf70xAW66DT6utgIG4/+fVhgihTJck5LXVNDUEqPpRvL35Gba hx8gVOKV2N2ck85AQMZBV/ws3XBVPeS6q2Qw2SBdV99txOmvR3Ab0VTa2Zq/mJSqvheY PyaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=/3btooTUxYy4rjHmRouQUpf5jOli5cRswOzHLZ9mznY=; b=TwwZSP29wjn24cEwwac96hsZFRCH7QrptJ4XWC0slR4BH1tkTgcb0hvz7bgSsmy8gO 9dTqWad+9rmmJbCX2An2UiOWi76NoodwIROToDdpPJiDOOczcbW8Xv+/5v2sUKpf7cqg FAi1uKIDRyaOV0y1uLoUiweIQhZA+Q9UwqKZLseOgfMmmYOC88DJmoSC72YfOhYnYjyT DiSB2YHNCwYLGw1/E/kBk3lcFUfQ8qUl1IkNg+4c0s9YjiSus1NJxb4xhIghx5ln7AqV v1dwE39qddpv0SPI0Dopd+cxFYd22KyiKWNMnwPF1wzq6bGNz/uc1FwFXkTybAXCgJCm Ltbg== X-Gm-Message-State: AOAM533TTqraItQL9/wyDk+x9lFAxha8p9wziNwqj1G5/vOw4WRX6oLv +4J2gS5I3XtnXA70E/E1AAV9T0SXnAKZGw== X-Google-Smtp-Source: ABdhPJxyK4cFos6d9rdUzehmscsjEdajy2MKLgAXlhRp3lKrgdQaCN2ori2cYXWO9cHOonenQP06f0Uk7ZZJsg== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:90a:4cc2:b0:1dd:1010:d10d with SMTP id k60-20020a17090a4cc200b001dd1010d10dmr13848304pjh.205.1653089580529; Fri, 20 May 2022 16:33:00 -0700 (PDT) Date: Fri, 20 May 2022 23:32:42 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-5-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 04/11] KVM: selftests: Refactor nested_map() to specify target level From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Refactor nested_map() to specify that it explicityl wants 4K mappings (the existing behavior) and push the implementation down into __nested_map(), which can be used in subsequent commits to create huge page mappings. No function change intended. Reviewed-by: Peter Xu Signed-off-by: David Matlack --- tools/testing/selftests/kvm/lib/x86_64/vmx.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c index baeaa35de113..b8cfe4914a3a 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c @@ -486,6 +486,7 @@ void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm, * nested_paddr - Nested guest physical address to map * paddr - VM Physical Address * size - The size of the range to map + * level - The level at which to map the range * * Output Args: None * @@ -494,22 +495,29 @@ void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm, * Within the VM given by vm, creates a nested guest translation for the * page range starting at nested_paddr to the page range starting at paddr. */ -void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm, - uint64_t nested_paddr, uint64_t paddr, uint64_t size) +void __nested_map(struct vmx_pages *vmx, struct kvm_vm *vm, + uint64_t nested_paddr, uint64_t paddr, uint64_t size, + int level) { - size_t page_size = vm->page_size; + size_t page_size = PG_LEVEL_SIZE(level); size_t npages = size / page_size; TEST_ASSERT(nested_paddr + size > nested_paddr, "Vaddr overflow"); TEST_ASSERT(paddr + size > paddr, "Paddr overflow"); while (npages--) { - nested_pg_map(vmx, vm, nested_paddr, paddr); + __nested_pg_map(vmx, vm, nested_paddr, paddr, level); nested_paddr += page_size; paddr += page_size; } } +void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm, + uint64_t nested_paddr, uint64_t paddr, uint64_t size) +{ + __nested_map(vmx, vm, nested_paddr, paddr, size, PG_LEVEL_4K); +} + /* Prepare an identity extended page table that maps all the * physical pages in VM. */ From patchwork Fri May 20 23:32:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857558 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 230C9C433EF for ; Fri, 20 May 2022 23:33:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238746AbiETXdL (ORCPT ); Fri, 20 May 2022 19:33:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354126AbiETXdD (ORCPT ); Fri, 20 May 2022 19:33:03 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C100E199B36 for ; Fri, 20 May 2022 16:33:02 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id t14-20020a1709028c8e00b0015cf7e541feso4722922plo.1 for ; Fri, 20 May 2022 16:33:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Z6u374eRRq3ZxgbCbMl6Db4qdzVQrIIZZTwWKsQlAh0=; b=EpLBXxSVK5SsYvVke0dDLPW/5dG97b1hFVNLmsBv60R/qGZ8i7uy5qwB6sSZIxwnEX tyRYZ4hVB/WmU95Dp5DZHrUw0PZBXsD+VfqbKXal/QOV4v0K/Vg7hrXhGksHfr/SLl/z N0q4ZWZs6Lpje0gggD8hRkxWxeRPf2hXmt03cOwnFeWPNjual/34bcdAvCQ3SfmHO9mP ayRsEsMCVWx28k0e799cOkMODeO7mBub09RpA+S4RFsqzezgRdqTQQGwwp7yuhWxED8L FBTgu50Yanob+v1PCcn3HCFhUDQgMLZkhVNgNEW3sXYitKLwX1L9Z/i+9hqhPzp4Tlx0 PExQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Z6u374eRRq3ZxgbCbMl6Db4qdzVQrIIZZTwWKsQlAh0=; b=OJcgrh08vHVKnPURadOSPSxCcnzNIePjMmQxsdqDJK1McRDsRsi61BEV2Pg++C9lDa HiHopRfz+hyaPuzZhM7CeAeI214DjIp5AqNWngO4Xz8zFNqhU/e4OWQkZe+rvTcNpTd8 o9d4P+qCHKIEf2eAl3LeOs93QL0bsCo1+RHJf/ThVqaLl0xbomzGqfy0Xj/GUCLHiPZQ nHtT7/xm87pCgOWtav9QzeU0hktj3Su/DcJ47oI4B2sPdQJoJMxrrjJD9pXrlmEnOW+i UxpfwlMEi+/xtV/Yni420ynnYnPH5AwGDEYq98inZRqYhuUV6pqpUNirWaVpZgD/n5ru rUhw== X-Gm-Message-State: AOAM530NxYF+3xHqjDz6p5UpOD/L6qS5z5KrVgc6E2R39DoXg1U9oxyr n6p44gJHTj8kyw9JnFGeD8RF6sLLZPjF2g== X-Google-Smtp-Source: ABdhPJzPChwAYoAZiyethJ+IW4Gzdj9GbVV2AS0eZ6sNmy9/Ngq+vEleLF9jMBF4pgGMIxhYLfzYtrjfain+5w== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:902:dad1:b0:161:9abb:fb75 with SMTP id q17-20020a170902dad100b001619abbfb75mr11902065plx.135.1653089582234; Fri, 20 May 2022 16:33:02 -0700 (PDT) Date: Fri, 20 May 2022 23:32:43 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-6-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 05/11] KVM: selftests: Move VMX_EPT_VPID_CAP_AD_BITS to vmx.h From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This is a VMX-related macro so move it to vmx.h. While here, open code the mask like the rest of the VMX bitmask macros. No functional change intended. Reviewed-by: Peter Xu Signed-off-by: David Matlack --- tools/testing/selftests/kvm/include/x86_64/processor.h | 3 --- tools/testing/selftests/kvm/include/x86_64/vmx.h | 2 ++ 2 files changed, 2 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 434a4f60f4d9..04f1d540bcb2 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -494,9 +494,6 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) #define X86_CR0_CD (1UL<<30) /* Cache Disable */ #define X86_CR0_PG (1UL<<31) /* Paging */ -/* VMX_EPT_VPID_CAP bits */ -#define VMX_EPT_VPID_CAP_AD_BITS (1ULL << 21) - #define XSTATE_XTILE_CFG_BIT 17 #define XSTATE_XTILE_DATA_BIT 18 diff --git a/tools/testing/selftests/kvm/include/x86_64/vmx.h b/tools/testing/selftests/kvm/include/x86_64/vmx.h index 583ceb0d1457..3b1794baa97c 100644 --- a/tools/testing/selftests/kvm/include/x86_64/vmx.h +++ b/tools/testing/selftests/kvm/include/x86_64/vmx.h @@ -96,6 +96,8 @@ #define VMX_MISC_PREEMPTION_TIMER_RATE_MASK 0x0000001f #define VMX_MISC_SAVE_EFER_LMA 0x00000020 +#define VMX_EPT_VPID_CAP_AD_BITS 0x00200000 + #define EXIT_REASON_FAILED_VMENTRY 0x80000000 #define EXIT_REASON_EXCEPTION_NMI 0 #define EXIT_REASON_EXTERNAL_INTERRUPT 1 From patchwork Fri May 20 23:32:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857560 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 E2565C433EF for ; Fri, 20 May 2022 23:33:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233389AbiETXdO (ORCPT ); Fri, 20 May 2022 19:33:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349869AbiETXdF (ORCPT ); Fri, 20 May 2022 19:33:05 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69EB3199B36 for ; Fri, 20 May 2022 16:33:04 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id jg18-20020a17090326d200b0016178ae1c69so4709966plb.6 for ; Fri, 20 May 2022 16:33:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=zIzRUypz+W9E6mtg9tuZCtp0bD/d3NxgK2XH6wHm+BQ=; b=WS5RNNhwOkq3lNDwZkeg0fLu8x7vpQFXqXm1/i9hTw2jICUkyuKzXB/oVSunEAPy+i /qbNm6ZiKBO4dHVQKx63HP/Zs0ceiCNplNgu4iyradr7ZvUB1aLOAMsaPERLTTM70lCv Lp5+FOZfPJa6mzLa4+l+kHFIGo2iTkwHYoKYNavDMYcL5qEuLPyoCKdsaL/SIFhN+TnY kw08F2A8lwG9iDhBAA0CJ3OFsU6sc7sAguZNsRJfan+PPFnbU1rGxZw+WJhax/ispZGg NgYfggHK/SJY+u08EJckAhEpZ5u/R3p1gxVHix7AOV7MxwRBKRDaiNi6x7vsw2ecaEWD UnEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=zIzRUypz+W9E6mtg9tuZCtp0bD/d3NxgK2XH6wHm+BQ=; b=LD7Vmnt5OOwUZ/nvtcGZ9ZjTH2SAdoBhdzSX8UhCrHGt5pOfOFdPTGRmSXmy31Bplx pqeoF92JiWGNQM5rH5+qdHdL/PHuGreb+qAr5vBUF9pUg9lIYfj2u714ziHSmB4fJVxH aAOqHkViqaG6T0I+pNGCLh9KsKZft9jEeJrVTpImnBxa4wKI9DrBmD29bmYWpC37SBn9 IgrhNS0bLT4shJ1YnERU09/+X0wanzMblw649XtmwibWN9gb++f763cdlyG9t40sZVX+ VS0lGBm44zPeIznzVkPGjDUxy2/1TW4mj9z8h+9cVb0zloNLkIUqiAJM7xI1vuaJXXtc sjqA== X-Gm-Message-State: AOAM531wkY8SXDS4TY2EZddY7iaxQcFwyMF+R62CDhO2FjJqLom2pVby /+W1zJLuxqFBxf7wpizaZdpFf3JHp9/lHw== X-Google-Smtp-Source: ABdhPJwWcUFm9CbIAAOYRnE1jllqgChDLFkNIV3/mmPZngl6Lom4GcmZgwOznJK19Fts0mdIWkoAplTtwN+ODQ== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:90b:4d90:b0:1de:b54f:c5d with SMTP id oj16-20020a17090b4d9000b001deb54f0c5dmr13503524pjb.27.1653089583887; Fri, 20 May 2022 16:33:03 -0700 (PDT) Date: Fri, 20 May 2022 23:32:44 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-7-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 06/11] KVM: selftests: Add a helper to check EPT/VPID capabilities From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Create a small helper function to check if a given EPT/VPID capability is supported. This will be re-used in a follow-up commit to check for 1G page support. No functional change intended. Reviewed-by: Peter Xu Signed-off-by: David Matlack --- tools/testing/selftests/kvm/lib/x86_64/vmx.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c index b8cfe4914a3a..5bf169179455 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c @@ -198,6 +198,11 @@ bool load_vmcs(struct vmx_pages *vmx) return true; } +static bool ept_vpid_cap_supported(uint64_t mask) +{ + return rdmsr(MSR_IA32_VMX_EPT_VPID_CAP) & mask; +} + /* * Initialize the control fields to the most basic settings possible. */ @@ -215,7 +220,7 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx) struct eptPageTablePointer eptp = { .memory_type = VMX_BASIC_MEM_TYPE_WB, .page_walk_length = 3, /* + 1 */ - .ad_enabled = !!(rdmsr(MSR_IA32_VMX_EPT_VPID_CAP) & VMX_EPT_VPID_CAP_AD_BITS), + .ad_enabled = ept_vpid_cap_supported(VMX_EPT_VPID_CAP_AD_BITS), .address = vmx->eptp_gpa >> PAGE_SHIFT_4K, }; From patchwork Fri May 20 23:32:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857561 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 6AA53C433FE for ; Fri, 20 May 2022 23:33:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351257AbiETXdQ (ORCPT ); Fri, 20 May 2022 19:33:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354122AbiETXdH (ORCPT ); Fri, 20 May 2022 19:33:07 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 803E71A7D1B for ; Fri, 20 May 2022 16:33:06 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id w12-20020a170902e88c00b00161f70f090eso1250388plg.11 for ; Fri, 20 May 2022 16:33:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=+O7sOCqFij4Cg9LTfpyCAY9gXqKy94OnTf8FSIBC0lo=; b=KRxARGEGLtZcSh9RlvhfAxNRi/v2I1ypEBjKXthu7qovhojcytukPPz6d/CIEr5+UE 6ArivDKm6N41zBjZEhkJrvU1BKyw/0wkWwdn8Cf7GvFxMxNqpr8b3dd5jRVzsd7r/Lpf bo+04tinYDngLertfMgdZJu8mD/TVY8dtt/6ngs9GjLDhdvzxPnpEE5QiOtNMqj8HVuJ /IQ08fseLBdbh/ixfrW+vO6fQfFcbYtQ9XLmbL4xfa6b2Y9oFFsiaDt7wBE6PgRgjXfk BKmSxWOJLJqnPR8aefXHTphN0ZrWA2A1PPi4BOjlFnB53ZMhLyuoBtCa5MbDADpEAaNx iOWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=+O7sOCqFij4Cg9LTfpyCAY9gXqKy94OnTf8FSIBC0lo=; b=1UzL6CMj1LgiodE1OLXyhYZL2WfG8NYfdiFUyhT1OEo3BsEk6HmRhAV0tjTvi6K+Z6 Ps/yKIBAVvXZpwDRSDoDKu3wG+kuj8l365nLlGE2wK17a41+s9x3Ft6C8F/YI0+jioYS ZioAVhqmHc7Ah72OScl4sxdp/OYWa4fggWO4bhzC7toPLL4p0eBKSaf1YANGuXrm+3ae twldomuNjjFSjdb1nrs5e7lsN3MUYo9v8mx8jBN7LRZX0FgfXoFe15ApTAgch+L+kZyb FGtPtLet1OofTiMenO4VEXzGU+VUtd/QUufTfPfPIDLDvkJ+UujpbWQopx5nnp+VpCk3 XqiQ== X-Gm-Message-State: AOAM533zBev8cMnddNxHPu7T290F/SH7yLGGxRxlyYAxDPcTDaioYLyV Gunupldc7WXI0Am7YMNd9Rzw1HX18oUHDQ== X-Google-Smtp-Source: ABdhPJyiFbp4MV+j6mWFwm4EEwIoYtKq737jRlE26jFb62bAEldjFQXxgt2taNuoLX80ihRfvfPqtycugrDHaQ== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:90a:8d83:b0:1dd:258c:7c55 with SMTP id d3-20020a17090a8d8300b001dd258c7c55mr490709pjo.1.1653089585563; Fri, 20 May 2022 16:33:05 -0700 (PDT) Date: Fri, 20 May 2022 23:32:45 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-8-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 07/11] KVM: selftests: Drop unnecessary rule for STATIC_LIBS From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop the "all: $(STATIC_LIBS)" rule. The KVM selftests already depend on $(STATIC_LIBS), so there is no reason to have an extra "all" rule. Suggested-by: Peter Xu Signed-off-by: David Matlack --- tools/testing/selftests/kvm/Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 8c3db2f75315..ae49abe682a7 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -192,7 +192,6 @@ $(OUTPUT)/libkvm.a: $(LIBKVM_OBJS) $(AR) crs $@ $^ x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS)))) -all: $(STATIC_LIBS) $(TEST_GEN_PROGS): $(STATIC_LIBS) cscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib .. From patchwork Fri May 20 23:32:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857562 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 72916C433EF for ; Fri, 20 May 2022 23:33:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354123AbiETXdR (ORCPT ); Fri, 20 May 2022 19:33:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348253AbiETXdK (ORCPT ); Fri, 20 May 2022 19:33:10 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21D5219FF7F for ; Fri, 20 May 2022 16:33:08 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id r14-20020a17090a1bce00b001df665a2f8bso5358256pjr.4 for ; Fri, 20 May 2022 16:33:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=uIcCRTk30Rd3Z1grC6z/5SkL7Y9z8d0k84iCVKmVWzQ=; b=YjSiYjx2ENUy1/0HB+kQNJnmnRJmYT/VE/dNmCxKFY+yzvldUgLpr5SgkevfN6+AEt 5nI3g62miRCQUDmYQUOZ+HHroYnhdkK4qjwlEwwbpjAiamAjjCKEFQmpUTL9Wo9g/ALT D9lGpcnX+JhlV1Hss986OK29PUBwBGKDZis3ub6kjX7Rqwj0aQ5zl4R8MtPIr5BdOl27 AlGvXMZRzIh9Hqhc2V/WwmkpnHccj3Qs8XWG2F6tBJYafiO9ZFGZpv4EntuGBjJp4i5S PqkefuTnJ0ZC5DO0TCpP6tnmwGz0pZf8JAKs5Je3aVaDoTc5ZRttGH64jCLmfjp4dcbV 22gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=uIcCRTk30Rd3Z1grC6z/5SkL7Y9z8d0k84iCVKmVWzQ=; b=PrYGOTs/aYdrqB5sUKJawx/YoFpFQK7QdAQb5VfK59TGdXkiSsGSBwPiG2CnEaJ8c5 C321kKJlOS8yqrbzLFmcVRIRFAFN2HNVFtkAhUlC6di9EW6J4da1gyVo60kp1hUYlKcX ZkMfqg0A+PUPVRjqBW/AGKcZaqDmUYHEJkSvkm9mvYt4QOn8bdUYmypvQTgnNkBfNexP ta+lDvXj2+IagVACXt6WJ71uwWGqe5UPNDnZmIQ3VWqAw7dcOUzf1lSzLngvz7VqItAa Xb6+ZO4GfMIC2Aj9KtksQbj9RLHPuEkjWLU0MhjplM5+dB4a8OBzBiQ/Hv/SChYzhaZE 4ikw== X-Gm-Message-State: AOAM533Lts7bgOUcuenZVmiXj0+3pLXaAbkyEn70I9bSVpy/DBkWCzif MmbRT+VM0EtOGoPezZPhB6p/PHn1B4RFiA== X-Google-Smtp-Source: ABdhPJwUBntIs9c/BhgpeWdPf7WA+InB6rt05MG8XmHTJNz2LVRczjGwep7zsdSn39M6e3CqkqQc1yTI55PdLQ== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:90a:4f05:b0:1df:afae:180b with SMTP id p5-20020a17090a4f0500b001dfafae180bmr13668728pjh.80.1653089587556; Fri, 20 May 2022 16:33:07 -0700 (PDT) Date: Fri, 20 May 2022 23:32:46 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-9-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 08/11] KVM: selftests: Link selftests directly with lib object files From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The linker does obey strong/weak symbols when linking static libraries, it simply resolves an undefined symbol to the first-encountered symbol. This means that defining __weak arch-generic functions and then defining arch-specific strong functions to override them in libkvm will not always work. More specifically, if we have: lib/generic.c: void __weak foo(void) { pr_info("weak\n"); } void bar(void) { foo(); } lib/x86_64/arch.c: void foo(void) { pr_info("strong\n"); } And a selftest that calls bar(), it will print "weak". Now if you make generic.o explicitly depend on arch.o (e.g. add function to arch.c that is called directly from generic.c) it will print "strong". In other words, it seems that the linker is free to throw out arch.o when linking because generic.o does not explicitly depend on it, which causes the linker to lose the strong symbol. One solution is to link libkvm.a with --whole-archive so that the linker doesn't throw away object files it thinks are unnecessary. However that is a bit difficult to plumb since we are using the common selftests makefile rules. An easier solution is to drop libkvm.a just link selftests with all the .o files that were originally in libkvm.a. Reviewed-by: Peter Xu Signed-off-by: David Matlack --- tools/testing/selftests/kvm/Makefile | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index ae49abe682a7..0889fc17baa5 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -173,12 +173,13 @@ LDFLAGS += -pthread $(no-pie-option) $(pgste-option) # $(TEST_GEN_PROGS) starts with $(OUTPUT)/ include ../lib.mk -STATIC_LIBS := $(OUTPUT)/libkvm.a LIBKVM_C := $(filter %.c,$(LIBKVM)) LIBKVM_S := $(filter %.S,$(LIBKVM)) LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C)) LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S)) -EXTRA_CLEAN += $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(STATIC_LIBS) cscope.* +LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) + +EXTRA_CLEAN += $(LIBKVM_OBJS) cscope.* x := $(shell mkdir -p $(sort $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ)))) $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c @@ -187,12 +188,8 @@ $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ -LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) -$(OUTPUT)/libkvm.a: $(LIBKVM_OBJS) - $(AR) crs $@ $^ - x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS)))) -$(TEST_GEN_PROGS): $(STATIC_LIBS) +$(TEST_GEN_PROGS): $(LIBKVM_OBJS) cscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib .. cscope: From patchwork Fri May 20 23:32:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857563 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 429EAC433EF for ; Fri, 20 May 2022 23:33:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348253AbiETXdT (ORCPT ); Fri, 20 May 2022 19:33:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354107AbiETXdK (ORCPT ); Fri, 20 May 2022 19:33:10 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2016199B36 for ; Fri, 20 May 2022 16:33:09 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id oj9-20020a17090b4d8900b001df6cd6813cso7675614pjb.9 for ; Fri, 20 May 2022 16:33:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=iQaFMbmFoi2VGc8icwKYFgNyKRkuTYMPuUBun/zPSQs=; b=lbmqY0lTpfPO6GThJE9LiVwX/TU2OszrIGc/LWBpEHSNzQUQTs6AcY2Ek0DzTC9rB0 pP/I2kNfhUB3kCo5TX+6eJAvh458sFxynaypdHj2uSp0mpz/22F4b5S2bZK71bdH/Tzd FHqeGrytW8/d0Dr4B2F0QTiDzbt9cgvMyD59rVuYIK0k6SoI4krHfSCyi0ne9gLOkb2H baNyTKE8nLFvuGdhWPYl0uR/COZeRj46t272ZjjU+uY4jEY/B/isQmyvftlgNrzAkGMa wIRdoeCXpYrPpO9YAUUUZOsByBcGKytplAXqw3kI82LW+/kwrHZFsmSKsown5+1sR1mj hxCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=iQaFMbmFoi2VGc8icwKYFgNyKRkuTYMPuUBun/zPSQs=; b=dsBy0dCSAArj/1dyPUJp4cbhtr3gpIoM3tY3Pl0K4RJEQ0h6z7nEzmCwbz8sLXR3gs sGqAsdw4c2TyUKBKhrnUCWT6o3R9Q45SoEg1j2H2FGp7TUM35Nnj4WbUBkskOmnQkDur BpUGdgnDzOrN5R5/r+Q5UvyyagREUL2XfIaggj7YgQGAmTW5CYWZCys0g+WZXYN//mRz l3UfZbf8CiFjW5bGeWSbpdF+b6LdKNoAK9+7mb+HFynNU16KvhChArRrE3A58NGX1m1t Gb7MIM3oF0JRIuo1ID0X1z+S5hJutVKxPpGxkn9OvIvFQ2j67LRldImbnfj6LOS7UCGj 8SzA== X-Gm-Message-State: AOAM53229EzKl2BqO03QX8+Yt5KoASXPpqsKewYPljnavyG60pnFJsOw VYuBZkAWO4LsS8tiG6wrXtNe5sace130GQ== X-Google-Smtp-Source: ABdhPJx/EhlpBumoO2tCpEy2FzVyp65osVtC/KCdk1bGp5WRtZEybYyRK8YNXeN14NTYB5Rg4oi3WlCaLNnSeg== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:902:f7cd:b0:161:7287:11b6 with SMTP id h13-20020a170902f7cd00b00161728711b6mr11783004plw.70.1653089589191; Fri, 20 May 2022 16:33:09 -0700 (PDT) Date: Fri, 20 May 2022 23:32:47 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-10-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 09/11] KVM: selftests: Clean up LIBKVM files in Makefile From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Break up the long lines for LIBKVM and alphabetize each architecture. This makes reading the Makefile easier, and will make reading diffs to LIBKVM easier. No functional change intended. Reviewed-by: Peter Xu Signed-off-by: David Matlack --- tools/testing/selftests/kvm/Makefile | 36 ++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 0889fc17baa5..83b9ffa456ea 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -37,11 +37,37 @@ ifeq ($(ARCH),riscv) UNAME_M := riscv endif -LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/rbtree.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c -LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S -LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c lib/aarch64/vgic.c -LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c -LIBKVM_riscv = lib/riscv/processor.c lib/riscv/ucall.c +LIBKVM += lib/assert.c +LIBKVM += lib/elf.c +LIBKVM += lib/guest_modes.c +LIBKVM += lib/io.c +LIBKVM += lib/kvm_util.c +LIBKVM += lib/perf_test_util.c +LIBKVM += lib/rbtree.c +LIBKVM += lib/sparsebit.c +LIBKVM += lib/test_util.c + +LIBKVM_x86_64 += lib/x86_64/apic.c +LIBKVM_x86_64 += lib/x86_64/handlers.S +LIBKVM_x86_64 += lib/x86_64/processor.c +LIBKVM_x86_64 += lib/x86_64/svm.c +LIBKVM_x86_64 += lib/x86_64/ucall.c +LIBKVM_x86_64 += lib/x86_64/vmx.c + +LIBKVM_aarch64 += lib/aarch64/gic.c +LIBKVM_aarch64 += lib/aarch64/gic_v3.c +LIBKVM_aarch64 += lib/aarch64/handlers.S +LIBKVM_aarch64 += lib/aarch64/processor.c +LIBKVM_aarch64 += lib/aarch64/spinlock.c +LIBKVM_aarch64 += lib/aarch64/ucall.c +LIBKVM_aarch64 += lib/aarch64/vgic.c + +LIBKVM_s390x += lib/s390x/diag318_test_handler.c +LIBKVM_s390x += lib/s390x/processor.c +LIBKVM_s390x += lib/s390x/ucall.c + +LIBKVM_riscv += lib/riscv/processor.c +LIBKVM_riscv += lib/riscv/ucall.c TEST_GEN_PROGS_x86_64 = x86_64/cpuid_test TEST_GEN_PROGS_x86_64 += x86_64/cr4_cpuid_sync_test From patchwork Fri May 20 23:32:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857564 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 CDFFFC433EF for ; Fri, 20 May 2022 23:33:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354143AbiETXdV (ORCPT ); Fri, 20 May 2022 19:33:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354134AbiETXdN (ORCPT ); Fri, 20 May 2022 19:33:13 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE2A319FF4E for ; Fri, 20 May 2022 16:33:11 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id d10-20020a17090a7bca00b001df1d7dd8cfso4916531pjl.8 for ; Fri, 20 May 2022 16:33:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=MemxomWyn3xFxsnJwUeubmFMBZbsEx+Q3Mj2FuHfdHM=; b=i/93yJ80eBOooJOB0I8qsv1MgkNa4IgHe5LpU1h/N9cNUeGqiPzQu/8mcOYKti/o31 0TTloyK1pbGwJmbWzi+oZjPHzgiScKbF5vssoEpSWyMvT1dSHaMk73W/8YmD89OVjmLd 5XzHLWy8xrVHiObz9zb0GBiQgM2TEiJsHPLVvA/LCHrNgCiQqA3cTurBFZpkjbk2GrUG cek9kx0qnwjpL3TI5gesjRD0/kMjp6GyBsCAiBvPxmPAdqOytSEyNhGNx7Usa7dS00m2 0GpAYdXGKkaXA9Ci8XydObCgq0LMVcK+Itz6oPD+/eF6P0Cv2egzni8j9rkUe8UuyuNB bdJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=MemxomWyn3xFxsnJwUeubmFMBZbsEx+Q3Mj2FuHfdHM=; b=qSEruGVVbi2fiA+tgvgrZjlUTkAKQUN+7lTCatrCdP6eXjQEmY1QB8QnrYwflaLx3U IlJfzmKMAWSOeyMtEgQdqNuOyiFqrlJhbhQQNGQFqyI+nHSdkMpXaFlaray7PvPwoKWP Eat0CDJ/kdbrD748w7j5o8pRRTSwpAuAU+JlWawOuhGgGauEiw36BkuYSbaMjHKD4FCt DmV8ZssDu8rBMmwt4qPLwFV6HHFhyTTvMX/z6/cGV8SW/bojeGq/3anrM0j28deaQBn7 ecWm2uONUpgyournLO5+TIPS+ZH8wXltggZKo1aEaHVlsyQl8IFJgx4J7s0iRH/n61DP 8ndA== X-Gm-Message-State: AOAM5309BoNRfSkuXl7l8LR8UwkzL5jcQepjl8HnYxZhjvd7NRzmt0Os nHgBejD4pfAk2VfKBt77hQr0F0yZ6dXQRQ== X-Google-Smtp-Source: ABdhPJzZsgzxjbJ10PsYQdoPxB1yJ/95ezm1QVZWzawOSVId6FDEpPYtvuN2kOAxGWivTrseepqlu7fQBgqB4w== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:902:6b0b:b0:158:d1e7:3d0f with SMTP id o11-20020a1709026b0b00b00158d1e73d0fmr11507496plk.71.1653089591052; Fri, 20 May 2022 16:33:11 -0700 (PDT) Date: Fri, 20 May 2022 23:32:48 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-11-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 10/11] KVM: selftests: Add option to run dirty_log_perf_test vCPUs in L2 From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add an option to dirty_log_perf_test that configures the vCPUs to run in L2 instead of L1. This makes it possible to benchmark the dirty logging performance of nested virtualization, which is particularly interesting because KVM must shadow L1's EPT/NPT tables. For now this support only works on x86_64 CPUs with VMX. Otherwise passing -n results in the test being skipped. Signed-off-by: David Matlack --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/dirty_log_perf_test.c | 10 +- .../selftests/kvm/include/perf_test_util.h | 9 ++ .../selftests/kvm/include/x86_64/processor.h | 4 + .../selftests/kvm/include/x86_64/vmx.h | 4 + .../selftests/kvm/lib/perf_test_util.c | 35 +++++- .../selftests/kvm/lib/x86_64/perf_test_util.c | 112 ++++++++++++++++++ tools/testing/selftests/kvm/lib/x86_64/vmx.c | 15 +++ 8 files changed, 182 insertions(+), 8 deletions(-) create mode 100644 tools/testing/selftests/kvm/lib/x86_64/perf_test_util.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 83b9ffa456ea..42cb904f6e54 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -49,6 +49,7 @@ LIBKVM += lib/test_util.c LIBKVM_x86_64 += lib/x86_64/apic.c LIBKVM_x86_64 += lib/x86_64/handlers.S +LIBKVM_x86_64 += lib/x86_64/perf_test_util.c LIBKVM_x86_64 += lib/x86_64/processor.c LIBKVM_x86_64 += lib/x86_64/svm.c LIBKVM_x86_64 += lib/x86_64/ucall.c diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index 7b47ae4f952e..d60a34cdfaee 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -336,8 +336,8 @@ static void run_test(enum vm_guest_mode mode, void *arg) static void help(char *name) { puts(""); - printf("usage: %s [-h] [-i iterations] [-p offset] [-g]" - "[-m mode] [-b vcpu bytes] [-v vcpus] [-o] [-s mem type]" + printf("usage: %s [-h] [-i iterations] [-p offset] [-g] " + "[-m mode] [-n] [-b vcpu bytes] [-v vcpus] [-o] [-s mem type]" "[-x memslots]\n", name); puts(""); printf(" -i: specify iteration counts (default: %"PRIu64")\n", @@ -351,6 +351,7 @@ static void help(char *name) printf(" -p: specify guest physical test memory offset\n" " Warning: a low offset can conflict with the loaded test code.\n"); guest_modes_help(); + printf(" -n: Run the vCPUs in nested mode (L2)\n"); printf(" -b: specify the size of the memory region which should be\n" " dirtied by each vCPU. e.g. 10M or 3G.\n" " (default: 1G)\n"); @@ -387,7 +388,7 @@ int main(int argc, char *argv[]) guest_modes_append_default(); - while ((opt = getopt(argc, argv, "ghi:p:m:b:f:v:os:x:")) != -1) { + while ((opt = getopt(argc, argv, "ghi:p:m:nb:f:v:os:x:")) != -1) { switch (opt) { case 'g': dirty_log_manual_caps = 0; @@ -401,6 +402,9 @@ int main(int argc, char *argv[]) case 'm': guest_modes_cmdline(optarg); break; + case 'n': + perf_test_args.nested = true; + break; case 'b': guest_percpu_mem_size = parse_size(optarg); break; diff --git a/tools/testing/selftests/kvm/include/perf_test_util.h b/tools/testing/selftests/kvm/include/perf_test_util.h index a86f953d8d36..d822cb670f1c 100644 --- a/tools/testing/selftests/kvm/include/perf_test_util.h +++ b/tools/testing/selftests/kvm/include/perf_test_util.h @@ -30,10 +30,15 @@ struct perf_test_vcpu_args { struct perf_test_args { struct kvm_vm *vm; + /* The starting address and size of the guest test region. */ uint64_t gpa; + uint64_t size; uint64_t guest_page_size; int wr_fract; + /* Run vCPUs in L2 instead of L1, if the architecture supports it. */ + bool nested; + struct perf_test_vcpu_args vcpu_args[KVM_MAX_VCPUS]; }; @@ -49,5 +54,9 @@ void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract); void perf_test_start_vcpu_threads(int vcpus, void (*vcpu_fn)(struct perf_test_vcpu_args *)); void perf_test_join_vcpu_threads(int vcpus); +void perf_test_guest_code(uint32_t vcpu_id); + +uint64_t perf_test_nested_pages(int nr_vcpus); +void perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus); #endif /* SELFTEST_KVM_PERF_TEST_UTIL_H */ diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 04f1d540bcb2..2aaea757432a 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -477,6 +477,10 @@ enum pg_level { #define PG_LEVEL_SHIFT(_level) ((_level - 1) * 9 + 12) #define PG_LEVEL_SIZE(_level) (1ull << PG_LEVEL_SHIFT(_level)) +#define PG_SIZE_4K PG_LEVEL_SIZE(PG_LEVEL_4K) +#define PG_SIZE_2M PG_LEVEL_SIZE(PG_LEVEL_2M) +#define PG_SIZE_1G PG_LEVEL_SIZE(PG_LEVEL_1G) + void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level); /* diff --git a/tools/testing/selftests/kvm/include/x86_64/vmx.h b/tools/testing/selftests/kvm/include/x86_64/vmx.h index 3b1794baa97c..cc3604f8f1d3 100644 --- a/tools/testing/selftests/kvm/include/x86_64/vmx.h +++ b/tools/testing/selftests/kvm/include/x86_64/vmx.h @@ -96,6 +96,7 @@ #define VMX_MISC_PREEMPTION_TIMER_RATE_MASK 0x0000001f #define VMX_MISC_SAVE_EFER_LMA 0x00000020 +#define VMX_EPT_VPID_CAP_1G_PAGES 0x00020000 #define VMX_EPT_VPID_CAP_AD_BITS 0x00200000 #define EXIT_REASON_FAILED_VMENTRY 0x80000000 @@ -608,6 +609,7 @@ bool load_vmcs(struct vmx_pages *vmx); bool nested_vmx_supported(void); void nested_vmx_check_supported(void); +bool ept_1g_pages_supported(void); void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm, uint64_t nested_paddr, uint64_t paddr); @@ -615,6 +617,8 @@ void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm, uint64_t nested_paddr, uint64_t paddr, uint64_t size); void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm, uint32_t memslot); +void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm, + uint64_t addr, uint64_t size); void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm, uint32_t eptp_memslot); void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm); diff --git a/tools/testing/selftests/kvm/lib/perf_test_util.c b/tools/testing/selftests/kvm/lib/perf_test_util.c index 722df3a28791..b2ff2cee2e51 100644 --- a/tools/testing/selftests/kvm/lib/perf_test_util.c +++ b/tools/testing/selftests/kvm/lib/perf_test_util.c @@ -40,7 +40,7 @@ static bool all_vcpu_threads_running; * Continuously write to the first 8 bytes of each page in the * specified region. */ -static void guest_code(uint32_t vcpu_id) +void perf_test_guest_code(uint32_t vcpu_id) { struct perf_test_args *pta = &perf_test_args; struct perf_test_vcpu_args *vcpu_args = &pta->vcpu_args[vcpu_id]; @@ -108,7 +108,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus, { struct perf_test_args *pta = &perf_test_args; struct kvm_vm *vm; - uint64_t guest_num_pages; + uint64_t guest_num_pages, slot0_pages = DEFAULT_GUEST_PHY_PAGES; uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src); int i; @@ -134,13 +134,20 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus, "Guest memory cannot be evenly divided into %d slots.", slots); + /* + * If using nested, allocate extra pages for the nested page tables and + * in-memory data structures. + */ + if (pta->nested) + slot0_pages += perf_test_nested_pages(vcpus); + /* * Pass guest_num_pages to populate the page tables for test memory. * The memory is also added to memslot 0, but that's a benign side * effect as KVM allows aliasing HVAs in meslots. */ - vm = vm_create_with_vcpus(mode, vcpus, DEFAULT_GUEST_PHY_PAGES, - guest_num_pages, 0, guest_code, NULL); + vm = vm_create_with_vcpus(mode, vcpus, slot0_pages, guest_num_pages, 0, + perf_test_guest_code, NULL); pta->vm = vm; @@ -161,7 +168,9 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus, /* Align to 1M (segment size) */ pta->gpa = align_down(pta->gpa, 1 << 20); #endif - pr_info("guest physical test memory offset: 0x%lx\n", pta->gpa); + pta->size = guest_num_pages * pta->guest_page_size; + pr_info("guest physical test memory: [0x%lx, 0x%lx)\n", + pta->gpa, pta->gpa + pta->size); /* Add extra memory slots for testing */ for (i = 0; i < slots; i++) { @@ -178,6 +187,11 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus, perf_test_setup_vcpus(vm, vcpus, vcpu_memory_bytes, partition_vcpu_memory_access); + if (pta->nested) { + pr_info("Configuring vCPUs to run in L2 (nested).\n"); + perf_test_setup_nested(vm, vcpus); + } + ucall_init(vm, NULL); /* Export the shared variables to the guest. */ @@ -198,6 +212,17 @@ void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract) sync_global_to_guest(vm, perf_test_args); } +uint64_t __weak perf_test_nested_pages(int nr_vcpus) +{ + return 0; +} + +void __weak perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus) +{ + pr_info("%s() not support on this architecture, skipping.\n", __func__); + exit(KSFT_SKIP); +} + static void *vcpu_thread_main(void *data) { struct vcpu_thread *vcpu = data; diff --git a/tools/testing/selftests/kvm/lib/x86_64/perf_test_util.c b/tools/testing/selftests/kvm/lib/x86_64/perf_test_util.c new file mode 100644 index 000000000000..e258524435a0 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/perf_test_util.c @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * x86_64-specific extensions to perf_test_util.c. + * + * Copyright (C) 2022, Google, Inc. + */ +#include +#include +#include +#include + +#include "test_util.h" +#include "kvm_util.h" +#include "perf_test_util.h" +#include "../kvm_util_internal.h" +#include "processor.h" +#include "vmx.h" + +void perf_test_l2_guest_code(uint64_t vcpu_id) +{ + perf_test_guest_code(vcpu_id); + vmcall(); +} + +extern char perf_test_l2_guest_entry[]; +__asm__( +"perf_test_l2_guest_entry:" +" mov (%rsp), %rdi;" +" call perf_test_l2_guest_code;" +" ud2;" +); + +static void perf_test_l1_guest_code(struct vmx_pages *vmx, uint64_t vcpu_id) +{ +#define L2_GUEST_STACK_SIZE 64 + unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; + unsigned long *rsp; + + GUEST_ASSERT(vmx->vmcs_gpa); + GUEST_ASSERT(prepare_for_vmx_operation(vmx)); + GUEST_ASSERT(load_vmcs(vmx)); + GUEST_ASSERT(ept_1g_pages_supported()); + + rsp = &l2_guest_stack[L2_GUEST_STACK_SIZE - 1]; + *rsp = vcpu_id; + prepare_vmcs(vmx, perf_test_l2_guest_entry, rsp); + + GUEST_ASSERT(!vmlaunch()); + GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL); + GUEST_DONE(); +} + +uint64_t perf_test_nested_pages(int nr_vcpus) +{ + /* + * 513 page tables is enough to identity-map 256 TiB of L2 with 1G + * pages and 4-level paging, plus a few pages per-vCPU for data + * structures such as the VMCS. + */ + return 513 + 10 * nr_vcpus; +} + +void perf_test_setup_ept(struct vmx_pages *vmx, struct kvm_vm *vm) +{ + uint64_t start, end; + + prepare_eptp(vmx, vm, 0); + + /* + * Identity map the first 4G and the test region with 1G pages so that + * KVM can shadow the EPT12 with the maximum huge page size supported + * by the backing source. + */ + nested_identity_map_1g(vmx, vm, 0, 0x100000000ULL); + + start = align_down(perf_test_args.gpa, PG_SIZE_1G); + end = align_up(perf_test_args.gpa + perf_test_args.size, PG_SIZE_1G); + nested_identity_map_1g(vmx, vm, start, end - start); +} + +void perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus) +{ + struct vmx_pages *vmx, *vmx0 = NULL; + struct kvm_regs regs; + vm_vaddr_t vmx_gva; + int vcpu_id; + + nested_vmx_check_supported(); + + for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) { + vmx = vcpu_alloc_vmx(vm, &vmx_gva); + + if (vcpu_id == 0) { + perf_test_setup_ept(vmx, vm); + vmx0 = vmx; + } else { + /* Share the same EPT table across all vCPUs. */ + vmx->eptp = vmx0->eptp; + vmx->eptp_hva = vmx0->eptp_hva; + vmx->eptp_gpa = vmx0->eptp_gpa; + } + + /* + * Override the vCPU to run perf_test_l1_guest_code() which will + * bounce it into L2 before calling perf_test_guest_code(). + */ + vcpu_regs_get(vm, vcpu_id, ®s); + regs.rip = (unsigned long) perf_test_l1_guest_code; + vcpu_regs_set(vm, vcpu_id, ®s); + vcpu_args_set(vm, vcpu_id, 2, vmx_gva, vcpu_id); + } +} diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c index 5bf169179455..b77a01d0a271 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c @@ -203,6 +203,11 @@ static bool ept_vpid_cap_supported(uint64_t mask) return rdmsr(MSR_IA32_VMX_EPT_VPID_CAP) & mask; } +bool ept_1g_pages_supported(void) +{ + return ept_vpid_cap_supported(VMX_EPT_VPID_CAP_1G_PAGES); +} + /* * Initialize the control fields to the most basic settings possible. */ @@ -439,6 +444,9 @@ void __nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm, TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use " "unknown or unsupported guest mode, mode: 0x%x", vm->mode); + TEST_ASSERT((nested_paddr >> 48) == 0, + "Nested physical address 0x%lx requires 5-level paging", + nested_paddr); TEST_ASSERT((nested_paddr % page_size) == 0, "Nested physical address not on page boundary,\n" " nested_paddr: 0x%lx page_size: 0x%lx", @@ -547,6 +555,13 @@ void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm, } } +/* Identity map a region with 1GiB Pages. */ +void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm, + uint64_t addr, uint64_t size) +{ + __nested_map(vmx, vm, addr, addr, size, PG_LEVEL_1G); +} + void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm, uint32_t eptp_memslot) { From patchwork Fri May 20 23:32:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12857565 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 C8270C433FE for ; Fri, 20 May 2022 23:33:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354159AbiETXdW (ORCPT ); Fri, 20 May 2022 19:33:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243528AbiETXdO (ORCPT ); Fri, 20 May 2022 19:33:14 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7896B199B36 for ; Fri, 20 May 2022 16:33:13 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id z186-20020a6233c3000000b00510a6bc2864so4742762pfz.10 for ; Fri, 20 May 2022 16:33:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=vHJUCGMEgJqpClQFFW0EWRWWAeT42MMPHlt4o+8BnGc=; b=p9kSyvLQx2Zcxh3pNRyiwAII5bzE9hhRdnONEsbj4YPpdix0dmYBcRPnQocosjh5rm 78Grm3y27uBlrAKebW/VWrBu2sD2F6ZgrcjFaZDFJh3eSpGfb/NYkHUJNT3TSSfvPDSk 9350tPhHBwhFKHjgRTHcikraAy1tD2LW8XNO94Kz+9zhwv/LozAuuOCWXhXGPUtjMtYO tD+YOLEOyrDsbijG6YYheLC4cgIDIl0jVh0oP2KdVg/gtLwYrXMFt7sFNX0lk8csCq4/ 6KkQnzIbZCt7nnTI9xZpIXwWp4gkFGXL5+iWM6+EDRU2L65DSpiTUheI7D9i6x9iXXBh wQ+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=vHJUCGMEgJqpClQFFW0EWRWWAeT42MMPHlt4o+8BnGc=; b=a79t6NZavIovrbOKyM4AQNNGkZuo4ZAZpNSATzBBuMDjDQRXC3Z6ocCnyAqweTrZmn 9+Td6HjJIosSGl+YFwiZuAZjVuSWSslWUYcl3tIlp+M/EHw9Hb8Uy7sF4mQo6YlcKnvZ fCQ8jlkW3DuWJZf6TedFqYMbyL+NKJBenv+6+YAxqKbiN6fyGh3+AMBCTUSA3fF2Rsl+ 03k+ZxFx3AR6f/jE+2Q+G2QkQqdmF7sYuXvcRXiqfhhDDekOQ/DT+9pRsnTCOPig2fpS quc7+eifE2xQwxemBxJlzxMgaXAqpFG+v9gXOkEWMpEw6KLoVjEfJQQyd/xOs3gNpnII cwNQ== X-Gm-Message-State: AOAM531CViCHql7rIBXbxzV0JrU18Czsx9TeMOm9rvML8hNxyzeMdQdD RiXYY1rR9yzwr1ZxWyqKsJ3h1I0rWqF7ew== X-Google-Smtp-Source: ABdhPJxWEA52/Ew9jC+/Mr4wrmF+W4/T+jyMdqd+SSZKG+rwsjDU/8KXerbAE6YQRyofVA7xkw42+bC785bMsQ== X-Received: from dmatlack-heavy.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:19cd]) (user=dmatlack job=sendgmr) by 2002:a17:90b:164a:b0:1dc:981d:f197 with SMTP id il10-20020a17090b164a00b001dc981df197mr14135472pjb.228.1653089593011; Fri, 20 May 2022 16:33:13 -0700 (PDT) Date: Fri, 20 May 2022 23:32:49 +0000 In-Reply-To: <20220520233249.3776001-1-dmatlack@google.com> Message-Id: <20220520233249.3776001-12-dmatlack@google.com> Mime-Version: 1.0 References: <20220520233249.3776001-1-dmatlack@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v4 11/11] KVM: selftests: Restrict test region to 48-bit physical addresses when using nested From: David Matlack To: Paolo Bonzini Cc: Ben Gardon , Sean Christopherson , Oliver Upton , Peter Xu , Vitaly Kuznetsov , Andrew Jones , "open list:KERNEL VIRTUAL MACHINE (KVM)" , David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The selftests nested code only supports 4-level paging at the moment. This means it cannot map nested guest physical addresses with more than 48 bits. Allow perf_test_util nested mode to work on hosts with more than 48 physical addresses by restricting the guest test region to 48-bits. While here, opportunistically fix an off-by-one error when dealing with vm_get_max_gfn(). perf_test_util.c was treating this as the maximum number of GFNs, rather than the maximum allowed GFN. This didn't result in any correctness issues, but it did end up shifting the test region down slightly when using huge pages. Suggested-by: Sean Christopherson Signed-off-by: David Matlack --- .../testing/selftests/kvm/lib/perf_test_util.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/perf_test_util.c b/tools/testing/selftests/kvm/lib/perf_test_util.c index b2ff2cee2e51..f989ff91f022 100644 --- a/tools/testing/selftests/kvm/lib/perf_test_util.c +++ b/tools/testing/selftests/kvm/lib/perf_test_util.c @@ -110,6 +110,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus, struct kvm_vm *vm; uint64_t guest_num_pages, slot0_pages = DEFAULT_GUEST_PHY_PAGES; uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src); + uint64_t region_end_gfn; int i; pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode)); @@ -151,18 +152,29 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus, pta->vm = vm; + /* Put the test region at the top guest physical memory. */ + region_end_gfn = vm_get_max_gfn(vm) + 1; + +#ifdef __x86_64__ + /* + * When running vCPUs in L2, restrict the test region to 48 bits to + * avoid needing 5-level page tables to identity map L2. + */ + if (pta->nested) + region_end_gfn = min(region_end_gfn, (1UL << 48) / pta->guest_page_size); +#endif /* * If there should be more memory in the guest test region than there * can be pages in the guest, it will definitely cause problems. */ - TEST_ASSERT(guest_num_pages < vm_get_max_gfn(vm), + TEST_ASSERT(guest_num_pages < region_end_gfn, "Requested more guest memory than address space allows.\n" " guest pages: %" PRIx64 " max gfn: %" PRIx64 " vcpus: %d wss: %" PRIx64 "]\n", - guest_num_pages, vm_get_max_gfn(vm), vcpus, + guest_num_pages, region_end_gfn - 1, vcpus, vcpu_memory_bytes); - pta->gpa = (vm_get_max_gfn(vm) - guest_num_pages) * pta->guest_page_size; + pta->gpa = (region_end_gfn - guest_num_pages) * pta->guest_page_size; pta->gpa = align_down(pta->gpa, backing_src_pagesz); #ifdef __s390x__ /* Align to 1M (segment size) */