From patchwork Sat Jan 21 00:15:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 46706 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp496097wrn; Fri, 20 Jan 2023 16:17:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXvBUkBLYyhuFImxOBl6EFuK9gorEvnlnpovGfCeD2v6+7QS2bnyfULvbNMV368TIW9kGuvm X-Received: by 2002:a62:fb12:0:b0:576:65f5:c60a with SMTP id x18-20020a62fb12000000b0057665f5c60amr16570797pfm.27.1674260251944; Fri, 20 Jan 2023 16:17:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674260251; cv=none; d=google.com; s=arc-20160816; b=Ndra7/5VYN28mBAKJ1g8cA8GKs7rYmXaVovXFfoS5at3usA+7hLvD8U/xqhcmBNtoE eSgbYcLOMuypAEHRplNNznlUEQavQgameIJSQH8tsph2iqHebTCBZm8NtabIbnudUTOk NkfwMsgcyoFWrMhHM+XJJ8yJ129Y80mSc9xaUf8kqKbKbSF4qPqBMIL6l50+ZB2A6P1o eeF5choChVsksauSLeTUT9FlDTrNIhoelBjjAZ21LwgmApbVBCK0ZnOlSQb/ZP3rNQBm ePfRTfC2pcOU/nmPWE42MhiZ/9QAJRvgAzrhO+GKuDxi9+xw6du/gdPxRD0YxlQ/O8Id flPQ== 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=ubxGTk6gAS1cIf2CC99mo1vpnAlsFSvC6HXfL0FfYMo=; b=GhayMj16iCRHDBW9tcxFNOccDPGs/ZaM8M5+yuRbYW5Meubyia23lhe9YifEQOEpuH NjzYHg4fhSKt4LjPiY7Og18S2eWNR2Yh0BDrEMjzwFMPdbn7/FzzzRkxhUcvW6hy/A2x Wk80iuYnOG/QwT8cYnEWsnYQ1D/NvwEnfQQgUJU4ANUAEel0QjTvGhYdhIM5CkfKwweR WwFhQP5I2J3UTDwGqKuWgNO06VJ/i9Jwpm+CPf8Oa1EFKB5jZgIshUpkZ9YE7i4Vhh+P Z76LADCUVfODs3xfTXdS2ERZwjynHoU9gwruBIwlFNoqoY4TANIP9kdJCD+62PruI79b ojSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=IB1WGTuv; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h11-20020a056a001a4b00b005749d29f159si8287105pfv.194.2023.01.20.16.17.20; Fri, 20 Jan 2023 16:17:31 -0800 (PST) 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=20210112 header.b=IB1WGTuv; 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 S229750AbjAUAQT (ORCPT + 99 others); Fri, 20 Jan 2023 19:16:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229609AbjAUAQS (ORCPT ); Fri, 20 Jan 2023 19:16:18 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3F0B94CBF for ; Fri, 20 Jan 2023 16:16:15 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id i10-20020a25f20a000000b006ea4f43c0ddso7514889ybe.21 for ; Fri, 20 Jan 2023 16:16:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ubxGTk6gAS1cIf2CC99mo1vpnAlsFSvC6HXfL0FfYMo=; b=IB1WGTuvu8nhLqPJU6KIdsFohzOBKSI4NnuM0fwFx8tGKjnp0lx6I8O1+Ev+HPMvNt KtBBAMvONZubRoRoIahe13qF3I7FnV79jYBJVhT21MPmGWCWSormgW0W4/8sY2Bfrivh VGH+WQwesc8Ed/S2AIivkxK84nf6Ldyb0/jWJrqi36WR6ZIMyLRy/E8VD4zeDtqM3N8J BNaDwctQ7qJWu6xRrSOW0/oLrLgIvf+R8pMO5/YAZoPuy3ZKXsRGnA85QLxFtVPGqNNB wYucvMLaUOTUCPUT3GEv5oJAzhoKFt/zNUBRjX+oPVmggUmLN91tMe1jGx2/afChYK1F +0ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=ubxGTk6gAS1cIf2CC99mo1vpnAlsFSvC6HXfL0FfYMo=; b=k0c0bRZ09Y+S4RQV6JhQfHkJJdssKUjsYqpzLUUu5cRAFcplWvccsDbiCI9eaZ+U8q 5JvQL8ECMQlVVFILARMIbtp6f7WNP3Nch/uXZceaKXDDf60lWhnEhUr3cmrgztikCHMe +sTwQvBcqE4hs+azz4q0d25aZjy+eZBTgOHr0Lk/QlmGYZD+Tz+8vcUx2XskW+cJcLje a+wny4H+m4oI6hdR2N2Ljwb/Iv0v4NXAAtbfCWT9nrkP9YkWZT9otTcF8IjO69z9OJH0 gVSddInv3HOKJmquJaJeWIrQP79feD4BezNTal0QTAh7/E30XUOODSI82Y5Hb0pEWII+ oxZw== X-Gm-Message-State: AFqh2kozVBsnLKu3ykFCBSO2KD2CtsTZ4v1fV7HldpTzY7NZP1An0ujJ SxJrgfK8pQ1eTSJFQMlNDOjJcmIHLLgHatv3/w== X-Received: from ackerleytng-cloudtop-sg.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:b30]) (user=ackerleytng job=sendgmr) by 2002:a25:d10a:0:b0:7ff:48e9:4d09 with SMTP id i10-20020a25d10a000000b007ff48e94d09mr506496ybg.440.1674260174863; Fri, 20 Jan 2023 16:16:14 -0800 (PST) Date: Sat, 21 Jan 2023 00:15:12 +0000 In-Reply-To: <20230121001542.2472357-1-ackerleytng@google.com> Mime-Version: 1.0 References: <20230121001542.2472357-1-ackerleytng@google.com> X-Mailer: git-send-email 2.39.0.246.g2a6d74b583-goog Message-ID: <20230121001542.2472357-2-ackerleytng@google.com> Subject: [RFC PATCH v3 01/31] KVM: selftests: Add function to allow one-to-one GVA to GPA mappings From: Ackerley Tng 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, marcorr@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, Ackerley Tng 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,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755589117801197944?= X-GMAIL-MSGID: =?utf-8?q?1755589117801197944?= One-to-one GVA to GPA mappings can be used in the guest to set up boot sequences during which paging is enabled, hence requiring a transition from using physical to virtual addresses in consecutive instructions. Signed-off-by: Ackerley Tng --- .../selftests/kvm/include/kvm_util_base.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 35 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 8dac0f49d20b9..0db5cd4b8383a 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -402,6 +402,8 @@ void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot); struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id); vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); +vm_vaddr_t vm_vaddr_alloc_1to1(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, + uint32_t data_memslot); vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages); vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index fa8aea97cdb62..5257bce6f546d 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1237,6 +1237,8 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, * vm - Virtual Machine * sz - Size in bytes * vaddr_min - Minimum starting virtual address + * paddr_min - Minimum starting physical address + * data_memslot - memslot number to allocate in * encrypt - Whether the region should be handled as encrypted * * Output Args: None @@ -1251,14 +1253,15 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, * a page. */ static vm_vaddr_t -_vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, bool encrypt) +_vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, + vm_paddr_t paddr_min, uint32_t data_memslot, bool encrypt) { uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0); virt_pgd_alloc(vm); vm_paddr_t paddr = _vm_phy_pages_alloc(vm, pages, - KVM_UTIL_MIN_PFN * vm->page_size, - 0, encrypt); + paddr_min, + data_memslot, encrypt); /* * Find an unused range of virtual page addresses of at least @@ -1281,12 +1284,34 @@ _vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, bool encrypt vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min) { - return _vm_vaddr_alloc(vm, sz, vaddr_min, vm->protected); + return _vm_vaddr_alloc(vm, sz, vaddr_min, + KVM_UTIL_MIN_PFN * vm->page_size, 0, + vm->protected); } vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min) { - return _vm_vaddr_alloc(vm, sz, vaddr_min, false); + return _vm_vaddr_alloc(vm, sz, vaddr_min, + KVM_UTIL_MIN_PFN * vm->page_size, 0, false); +} + +/** + * Allocate memory in @vm of size @sz in memslot with id @data_memslot, + * beginning with the desired address of @vaddr_min. + * + * If there isn't enough memory at @vaddr_min, find the next possible address + * that can meet the requested size in the given memslot. + * + * Return the address where the memory is allocated. + */ +vm_vaddr_t vm_vaddr_alloc_1to1(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, + uint32_t data_memslot) +{ + vm_vaddr_t gva = _vm_vaddr_alloc(vm, sz, vaddr_min, (vm_paddr_t) vaddr_min, + data_memslot, vm->protected); + ASSERT_EQ(gva, addr_gva2gpa(vm, gva)); + + return gva; } /*