Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp236838rwb; Tue, 25 Jul 2023 15:06:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlFFtxu1RBSqwG0Bvby+lhIg9zAXTK/pYXf/RBvqCCdLiCYPG9Z4pnAe7zrdWjFHsGVjdBmP X-Received: by 2002:a17:90a:f2d1:b0:268:2024:4648 with SMTP id gt17-20020a17090af2d100b0026820244648mr465789pjb.23.1690322808150; Tue, 25 Jul 2023 15:06:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690322808; cv=none; d=google.com; s=arc-20160816; b=augtHnHAkq4pXHs/HK50eYW0ej0hK3IiJa1f3HXm1yxbCh/1buUS/uOH78jnXd4PuU fu2o8SqFORBf744dU1oREjgaAqiJd3grkOw4svYzWuAvG1z00fq+DTlIkxd6x97xWeDL VHr/liTRAKV5eDuGhdJC/lxisd8A6oPFp3JqbBfQ5mbOjcJLAehTqkNvBjqxGgS/zyXD 1mam9IGdsqtE5Xp223dhxfdH7R6XDAQNqV671+x8q1KiNSXXCq/sm3wTUvzR/gM0Vu8+ C6gSzW+mD4kLZSXeufl6vqvJ+sEX8dHrP4nFntSAVOS8AjpwXCy0aUVHwMowviMqpZfw YUWA== 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=2Vk6fb359Stuhm1VcuGIgWqQRYkL2YWLdsbYVpUx5xo=; fh=1/sgOwi5AuU8IZymeFKs1Rjnf4QELnrK+C7GntqU2hA=; b=RiIl1pmuwUDK8/I2GYzpHKE2AhUpdfaki5E9aC+NkTGJIGgbpdgSaacHE7dbGRmDpu ZH7Is9g0wTxHGDa4M0EPWuDwa0iPv52mtKv60Qgc45A8Rhbpov6Q8/1CbYbaxYaqzi43 IbJpcL3YvbUFMdL7ea6GImcpFR5PSwjD0+5hPmCmFz9gj1ag1hJ4UegFy7i0WG/X1G1Y vmuyxg/5QVqiAMQL6xSL5KU/R0ismM1jPudmHUi5eu2LRNuHw2I4+ufjIRI25oL9FUBi DyFguuxJidkGSc/wAnp5s6icktEPfmRJp7mpRpG7OwNA9b6SLrK+xPeX7fkRULygsLrY hGVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=3isJhOUv; 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 t10-20020a17090aba8a00b00262d9b8a137si92014pjr.98.2023.07.25.15.06.35; Tue, 25 Jul 2023 15:06:48 -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=3isJhOUv; 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 S231897AbjGYWE1 (ORCPT + 99 others); Tue, 25 Jul 2023 18:04:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232037AbjGYWDI (ORCPT ); Tue, 25 Jul 2023 18:03:08 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B750235AE for ; Tue, 25 Jul 2023 15:02:42 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-57a3620f8c0so58165257b3.3 for ; Tue, 25 Jul 2023 15:02:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690322551; x=1690927351; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2Vk6fb359Stuhm1VcuGIgWqQRYkL2YWLdsbYVpUx5xo=; b=3isJhOUvSruS/8cztZ/vPjCuapGQPFiQqyfoyCyDr1i19srvz/gFuhAYszripqjF++ zj5y/djO90jVg+NWQ++1x81HXrIqATvkCywjPSpixoaXW7Qtz+Qkczfc0KvgaMIjzm7q +mqct2o/rQq7e4PoIDn3W63uqMiWWH+lLYX4R4uOUB5TcjIvjoywlQog9Awd/IEdito1 0y541Lrx3Cml256bSbVNGjE211P67v2SNF42rceDqtBQcqKEUjSULNfJclR1Tzk5n979 EwjRq4LvFY+6YZOsq6U/iM3KFGWFkiyQV1yqh2YfQg5NebW/lGI41/iBRlguY/O8TgFR T7sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690322551; x=1690927351; 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=2Vk6fb359Stuhm1VcuGIgWqQRYkL2YWLdsbYVpUx5xo=; b=UGA2KP6cQ8YaUWL3On2Ltle09PK4Xo/QKgEs6JCiS1v7a0sMeyIfRQCF7//WJ2wuS7 RRzwVeey6/wLyrycVI0xwFnSqRs0VVQBWbmdXFOSn1Y5P29W4oNAuYkSbZXCVaffec9k uoJcu9/yOtU08h3defPxTDR6msQG7UnrMe1vkKiu/L7owPenRtQGlfY0tXS1O/vRUYYM XTltk5atcIYBmmtm0+AgvwwcxEGS9/hombkdumMfCzlEUbXnN6hqFYsQw/QDVTGTRq/y J1z8aHlyxTDKQO716/mA7Utpcadptf84xzARvUZutxrOkiopTV++EtMGgLm3VDLFvX6J cojQ== X-Gm-Message-State: ABy/qLa832tS9i9svthLuhrvfn7DP/UHii9xAl4fl6oSQZOkebS0VIn4 qQjMHr6s0eX7SbNLXOmmTsJjI1PFRby4 X-Received: from afranji.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:47f1]) (user=afranji job=sendgmr) by 2002:a81:ae49:0:b0:580:e6dc:e2df with SMTP id g9-20020a81ae49000000b00580e6dce2dfmr3713ywk.9.1690322550887; Tue, 25 Jul 2023 15:02:30 -0700 (PDT) Date: Tue, 25 Jul 2023 22:01:15 +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-23-afranji@google.com> Subject: [PATCH v4 22/28] KVM: selftests: Add functions to allow mapping as shared 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 Signed-off-by: Ackerley Tng Change-Id: I27676cf732276c544c9abd7e9b8589ca8ffd6ba5 Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/kvm_util_base.h | 24 ++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 32 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 15 +++++++-- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 6ed57162b49d..d793355599e1 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -524,6 +524,8 @@ vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm); void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, unsigned int npages); +void virt_map_shared(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, + unsigned int npages); void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa); void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva); @@ -983,6 +985,28 @@ static inline void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr virt_arch_pg_map(vm, vaddr, paddr); } +/* + * VM Virtual Page Map as Shared + * + * Input Args: + * vm - Virtual Machine + * vaddr - VM Virtual Address + * paddr - VM Physical Address + * memslot - Memory region slot for new virtual translation tables + * + * Output Args: None + * + * Return: None + * + * Within @vm, creates a virtual translation for the page starting + * at @vaddr to the page starting at @paddr. + */ +void virt_arch_pg_map_shared(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr); + +static inline void virt_pg_map_shared(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr) +{ + virt_arch_pg_map_shared(vm, vaddr, paddr); +} /* * Address Guest Virtual to Guest Physical diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index e2a0d979b54f..7db9437505d7 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1551,6 +1551,38 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, } } +/* + * Map a range of VM virtual address to the VM's physical address as shared + * + * Input Args: + * vm - Virtual Machine + * vaddr - Virtuall address to map + * paddr - VM Physical Address + * npages - The number of pages to map + * + * Output Args: None + * + * Return: None + * + * Within the VM given by @vm, creates a virtual translation for + * @npages starting at @vaddr to the page range starting at @paddr. + */ +void virt_map_shared(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, + unsigned int npages) +{ + size_t page_size = vm->page_size; + size_t size = npages * page_size; + + TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow"); + TEST_ASSERT(paddr + size > paddr, "Paddr overflow"); + + while (npages--) { + virt_pg_map_shared(vm, vaddr, paddr); + vaddr += page_size; + paddr += page_size; + } +} + /* * Address VM Physical to Host Virtual * diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 4893a778fff5..9283c1e4961e 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -183,7 +183,8 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm, return pte; } -void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) +static void ___virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, + int level, bool protected) { const uint64_t pg_size = PG_LEVEL_SIZE(level); uint64_t *pml4e, *pdpe, *pde; @@ -229,17 +230,27 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) "PTE already present for 4k page at vaddr: 0x%lx\n", vaddr); *pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK | (paddr & PHYSICAL_PAGE_MASK); - if (vm_is_gpa_protected(vm, paddr)) + if (protected) *pte |= vm->arch.c_bit; else *pte |= vm->arch.s_bit; } +void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) +{ + ___virt_pg_map(vm, vaddr, paddr, level, vm_is_gpa_protected(vm, paddr)); +} + void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr) { __virt_pg_map(vm, vaddr, paddr, PG_LEVEL_4K); } +void virt_arch_pg_map_shared(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr) +{ + ___virt_pg_map(vm, vaddr, paddr, PG_LEVEL_4K, false); +} + void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, uint64_t nr_bytes, int level) { -- 2.41.0.487.g6d72f3e995-goog