From patchwork Tue Dec 12 20:46:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177554 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993093vqy; Tue, 12 Dec 2023 12:47:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IGAn36mnF84/kh2kH/RquB6VeklalVHfXUw3oXPYvorNTUiZlOnYM9UdB2MujRA6ZvdrrfK X-Received: by 2002:a05:6358:5e0f:b0:170:ad0e:c222 with SMTP id q15-20020a0563585e0f00b00170ad0ec222mr8459564rwn.9.1702414027033; Tue, 12 Dec 2023 12:47:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414026; cv=none; d=google.com; s=arc-20160816; b=VZO5nyt3wD/Dhdxf7EdtcPG+vo5S/1yqLdutAGoS2dFwXALuv7Hik16UFGif0GAeLa Cz4ydQua+FHHLaRKirXM/ploNiiwm0MeDRNkRX8Yl9ezkVF9BhLzVolCA2QLWxM0tbwp Q2M0I+ABvCjwXvHGuO/CwZoP7YwgfHYSMSgUFL1DZ0FfsHb99Ue955h1X6R9TRMIRtKv 34+FmfLVRXLZYIo6v1VowWnFqHZMUzgFuPBUjn2zdmVqNMBLtU8h0UeC2Nuuy6cy5oSc HB7sodkVW5ItzxQN2c+px6KB6fJaZ2LV+c4DRfsQ1DutVpHcS7FuMul1noIZJreu3vZs BLAQ== 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=UNdApJhYNj7CgDTo7NNKspnxbte38oVK+sy/EZvIraM=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=UY3IlzxfWfvlbr60crontyPG8f+ZrAu6Ly0uc/hsSeIHFK6lOVjexSeXn9mi879A5s hYVF7MBTNqxuHNakBmsxOL3T+ua/lv9vmVadaRRpLzevFyKdyDyYdR0ygPLFjHNJETH5 pdDjIsEA4PzfRwlSBbSLMqEFqOTFIjsR2+9XHdH8zbVePvkWY4TDWb3kD7HZXTQ5Dlo6 dEufy1zP2fFR1ZtME3nq12bAkFPJeVR/47hIdG55E5lDjRapNzeIEchcCjopfaBBSndP DBKF3PxzTT0i/W09qua88dJMF7T8A7LWrqcMa5KYYxL5QF+PHqOXtFczikut5gZ2E9XG LeDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=N94F31B3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id u17-20020a656711000000b005bd2b888545si8054316pgf.119.2023.12.12.12.47.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=N94F31B3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id BA05880617DC; Tue, 12 Dec 2023 12:47:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377177AbjLLUqy (ORCPT + 99 others); Tue, 12 Dec 2023 15:46:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229800AbjLLUqx (ORCPT ); Tue, 12 Dec 2023 15:46:53 -0500 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 33F83BC for ; Tue, 12 Dec 2023 12:47:00 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d3376c1c83so5305305ad.0 for ; Tue, 12 Dec 2023 12:47:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414019; x=1703018819; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=UNdApJhYNj7CgDTo7NNKspnxbte38oVK+sy/EZvIraM=; b=N94F31B3bIZLtNWdfrDSo5htih+Hoe3JRgnTjzbaAwRIDTfLpJAFgNE/T9Jer0/hkF ju7eCQ4LABrzkPtc+eNyhaa/lBI15nag9szWsemTSnXtr5rluizdTTT1g8iowN8DQ3yS xtx74XlfkGJU5sijJeGGCUmLW9e+2/kksPfLU8HP/mS7gm1rwEKo8oRGhYWxydqM5SYO qjDGWEJxgZsC4JPDqqVBvoQbibi0xXNccMkV1L0FpQTQOUE1rtMiDpBiRaafHOhdqpPH vhquKXN9fQbdcQnnkQJp3TfbhvBgDAG5NcvqjweoUuUGUK4nHY/Sk0d6eXDnyMTZF4Hf jyhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414019; x=1703018819; 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=UNdApJhYNj7CgDTo7NNKspnxbte38oVK+sy/EZvIraM=; b=Z/HCdOrRKbhmGWhOpknNrADXa6vSodlkwFcWzYlSHvZ20gz5uHWMJh2nHM/0Hzc0kS GQUe0tSJ/oPwvcXpVuP2o5XaJmkeWiKNbadN47KQYoxh0gGpA0Y4q3Y5R37GHEbK31DC 92gJ9eBgsdu0VZbCFpJ51IJ3hBGHr71iU4hNggMiyWd5Dw9F+7cGqr+5GfCSG3xkwoUx ys0MWyTbw4sG/Cqo93U1DVBgvFhkvqq+/0an1QLgUhlImZY08Aa0qnunZ+T/QSDrsb7x /sTz+d4SqzNNEFw/RuLPu4uNTtAGWpsLO9Go+kUj7XB1MNz3DmHCpIQQGwt+ICJXT/yz 6vvQ== X-Gm-Message-State: AOJu0YyePvGnC97Y7qUABBE7UWWQ1IChWey8S6BA9MybZzaH+oy4RGxu 4/mm193JXEwnJpA5gWTiVTzqOHpTqA== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:db09:b0:1d0:8fff:cd75 with SMTP id m9-20020a170902db0900b001d08fffcd75mr51402plx.10.1702414019454; Tue, 12 Dec 2023 12:46:59 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:16 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-2-sagis@google.com> Subject: [RFC PATCH v5 01/29] KVM: selftests: Add function to allow one-to-one GVA to GPA mappings From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-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_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:06 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110490853590016 X-GMAIL-MSGID: 1785110490853590016 From: Ackerley Tng 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 Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- .../selftests/kvm/include/kvm_util_base.h | 2 + tools/testing/selftests/kvm/lib/kvm_util.c | 63 ++++++++++++++++--- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 1426e88ebdc7..c2e5c5f25dfc 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -564,6 +564,8 @@ vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, enum kvm_mem_region_type type); 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, enum kvm_mem_region_type type); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index febc63d7a46b..4f1ae0f1eef0 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1388,17 +1388,37 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, return pgidx_start * vm->page_size; } +/* + * VM Virtual Address Allocate Shared/Encrypted + * + * Input Args: + * 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 + * + * Return: + * Starting guest virtual address + * + * Allocates at least sz bytes within the virtual address space of the vm + * given by vm. The allocated bytes are mapped to a virtual address >= + * the address given by vaddr_min. Note that each allocation uses a + * a unique set of pages, with the minimum real allocation being at least + * a page. + */ static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, - vm_vaddr_t vaddr_min, - enum kvm_mem_region_type type, - bool encrypt) + 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, - vm->memslots[type], encrypt); + vm_paddr_t paddr = _vm_phy_pages_alloc(vm, pages, paddr_min, + data_memslot, encrypt); /* * Find an unused range of virtual page addresses of at least @@ -1408,8 +1428,7 @@ static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, /* Map the virtual pages. */ for (vm_vaddr_t vaddr = vaddr_start; pages > 0; - pages--, vaddr += vm->page_size, paddr += vm->page_size) { - + pages--, vaddr += vm->page_size, paddr += vm->page_size) { virt_pg_map(vm, vaddr, paddr); sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift); @@ -1421,12 +1440,16 @@ static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, enum kvm_mem_region_type type) { - return ____vm_vaddr_alloc(vm, sz, vaddr_min, type, vm->protected); + return ____vm_vaddr_alloc(vm, sz, vaddr_min, + KVM_UTIL_MIN_PFN * vm->page_size, + vm->memslots[type], 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, MEM_REGION_TEST_DATA, false); + return ____vm_vaddr_alloc(vm, sz, vaddr_min, + KVM_UTIL_MIN_PFN * vm->page_size, + vm->memslots[MEM_REGION_TEST_DATA], false); } /* @@ -1453,6 +1476,26 @@ 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, MEM_REGION_TEST_DATA); } +/** + * 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); + TEST_ASSERT_EQ(gva, addr_gva2gpa(vm, gva)); + + return gva; +} + /* * VM Virtual Address Allocate Pages * From patchwork Tue Dec 12 20:46:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177556 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993175vqy; Tue, 12 Dec 2023 12:47:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGpBEARSSgxeWRSCWvYI6qc1Er4vIXjkiXtXbeqGTc6rnplwXPJRsdwQTFXplqR1M0RlQF5 X-Received: by 2002:a17:90a:d706:b0:286:e703:738f with SMTP id y6-20020a17090ad70600b00286e703738fmr3249399pju.95.1702414035502; Tue, 12 Dec 2023 12:47:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414035; cv=none; d=google.com; s=arc-20160816; b=LgEn6ZydFwDiEfnJTcdrYYVehZM0n90zY9AdNoces5y7SIrce4ko/8Dl6qa52rhhRU uL6NjzF9fu/m3mE5OrL+WhKMlOuuLXAUrnm4Bw7fbbM+S5l+oqGszLVVxgoOt1N2XW0O aJY7ZodsEK56derBY4glZAVUKrBQomBVO7TT8NdNwrbpuU7dhWsocikbDN2kI8xE949f qDALuBAjo6XRFQyXi3jaXIAFHf6Cq4suvFotIThF1NHvmPzlRlgraTRpDil2kS3nNANb iF8eA5DK5Gsbvl9YhktHlyo+O9ki1G6gxpqYdV4Z48Eo4seR4Ta0R2PXY3LQF8+4WBXV hM3A== 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=mIyNCwZuG05oGqfsGJdjuxWZ9bJhVxMnAF0jWTMuK/Y=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=BgPAr2vz3aRP9pwogUDTWjkMQVILKKqyQebCXAWvHo5AhZpshBJh1GTxxoaZED7YXP mU6fD1MXa7SclUjZh63T1I9NgvtfVPhHZ8hX550AqPjb4xAa7lmXkjCVDsBZ74REHa5N aXk53S4ld3oaQ4H9eFOhwsM8VZsDIpDT27Hb6KrzYQmOPeSGGXRcX9YkP00p9/SIJWiP haaLlv0YNpP9fNe5ezS6tFaRmfstHoqYkOooOMf2x/DgWgGTxqzCGVP9Zm9SqZG4QtHV ierspTLfbn3PuiGMv1RLPGWEKbh3BccRho3XzU/s3xspODN6eGQ6nSI8T/5yQxEP4DU4 IHjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=s6iIYI84; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id ng10-20020a17090b1a8a00b00280479459f7si9902570pjb.50.2023.12.12.12.47.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=s6iIYI84; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 487D780ACCE6; Tue, 12 Dec 2023 12:47:11 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377228AbjLLUq4 (ORCPT + 99 others); Tue, 12 Dec 2023 15:46:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377219AbjLLUqz (ORCPT ); Tue, 12 Dec 2023 15:46:55 -0500 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 26389BE for ; Tue, 12 Dec 2023 12:47:02 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5e1e74d9d0eso8075877b3.1 for ; Tue, 12 Dec 2023 12:47:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414021; x=1703018821; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mIyNCwZuG05oGqfsGJdjuxWZ9bJhVxMnAF0jWTMuK/Y=; b=s6iIYI84SnTkiUokmIAErHmVvzwNUtLYpwfRJAQow7T+J6rtYc1RURHMRWzx9wIR/p soCLqbIVqHyhen4U98VvDl7XMpmy5UdCN1gJE1yYS7uCSXLZOiuW99WB4w0PJ2Rkm0gz 8cA+Eudl30rUumS/5oRkTVTOz7aidNvVoAfOeRWy877C4L0HdCq5vmFF1R2O4FtLX6rO /zPxbr1oU85fTeSQyWesaN+TYGRUaVLe2rnDqfx3mFW7FrEq5s8hmFL4DTVy5VzoebJF JNsZV21lOXsD8FQgq6CDfQtM9xgaX9yYSeu2Ytfq6k0WFVrJBJsDbcjwMP/aeHTUUk4g NK2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414021; x=1703018821; 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=mIyNCwZuG05oGqfsGJdjuxWZ9bJhVxMnAF0jWTMuK/Y=; b=szSLJh6gsFsKEF21EpXqH+lCA55beKAmnLx+pRZ+f93hTrVm5P9wqGlOPNDYSNjw44 +Ym4EQyfjYNVzUU2pZ0FTol+LQzxIBk2RBKMDC87pvNO0KRDUcPeOj9FWQHhAp6Qa1x3 /hSGb0pgn7Nwhq7SOxw2tMUr+AfYP96phHTw80F8FHOOPMdK4hJ5jXN8DxDJLy++RhPo oRqiSDdreJkgSwYl7r5SHLu7bOIbGCdSaAZMxMz4HMooQvcV1OURp8YuH/VPc50yhf2J ViUDY6fvZzEhDWmFxuUovvGFh0WoP6oPw4siyeOBdYhwybAMOizx1Iinnj3dgXlST742 XOBw== X-Gm-Message-State: AOJu0Ywyvmg4bqykw21JViOZTq88DFH3/XsiwufHxTf4KTHlbsoUMI+T TB3TrafmlayktQEn/LKYaz1eDFoiWg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:690c:2e8a:b0:5d8:ef49:748 with SMTP id eu10-20020a05690c2e8a00b005d8ef490748mr107655ywb.5.1702414021164; Tue, 12 Dec 2023 12:47:01 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:17 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-3-sagis@google.com> Subject: [RFC PATCH v5 02/29] KVM: selftests: Expose function that sets up sregs based on VM's mode From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:11 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110499453084780 X-GMAIL-MSGID: 1785110499453084780 From: Ackerley Tng This allows initializing sregs without setting vCPU registers in KVM. No functional change intended. Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar Reviewed-by: Binbin Wu --- .../selftests/kvm/include/x86_64/processor.h | 2 + .../selftests/kvm/lib/x86_64/processor.c | 39 ++++++++++--------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 35fcf4d78dfa..0b8855d68744 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -958,6 +958,8 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries) void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid); void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu); +void vcpu_setup_mode_sregs(struct kvm_vm *vm, struct kvm_sregs *sregs); + static inline struct kvm_cpuid_entry2 *__vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu, uint32_t function, uint32_t index) diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index aef1c021c4bb..f130f78a4974 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -543,36 +543,39 @@ static void kvm_setup_tss_64bit(struct kvm_vm *vm, struct kvm_segment *segp, kvm_seg_fill_gdt_64bit(vm, segp); } -static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu) +void vcpu_setup_mode_sregs(struct kvm_vm *vm, struct kvm_sregs *sregs) { - struct kvm_sregs sregs; - - /* Set mode specific system register values. */ - vcpu_sregs_get(vcpu, &sregs); - - sregs.idt.limit = 0; + sregs->idt.limit = 0; - kvm_setup_gdt(vm, &sregs.gdt); + kvm_setup_gdt(vm, &sregs->gdt); switch (vm->mode) { case VM_MODE_PXXV48_4K_SEV: case VM_MODE_PXXV48_4K: - sregs.cr0 = X86_CR0_PE | X86_CR0_NE | X86_CR0_PG; - sregs.cr4 |= X86_CR4_PAE | X86_CR4_OSFXSR; - sregs.efer |= (EFER_LME | EFER_LMA | EFER_NX); - - kvm_seg_set_unusable(&sregs.ldt); - kvm_seg_set_kernel_code_64bit(vm, DEFAULT_CODE_SELECTOR, &sregs.cs); - kvm_seg_set_kernel_data_64bit(vm, DEFAULT_DATA_SELECTOR, &sregs.ds); - kvm_seg_set_kernel_data_64bit(vm, DEFAULT_DATA_SELECTOR, &sregs.es); - kvm_setup_tss_64bit(vm, &sregs.tr, 0x18); + sregs->cr0 = X86_CR0_PE | X86_CR0_NE | X86_CR0_PG; + sregs->cr4 |= X86_CR4_PAE | X86_CR4_OSFXSR; + sregs->efer |= (EFER_LME | EFER_LMA | EFER_NX); + + kvm_seg_set_unusable(&sregs->ldt); + kvm_seg_set_kernel_code_64bit(vm, DEFAULT_CODE_SELECTOR, &sregs->cs); + kvm_seg_set_kernel_data_64bit(vm, DEFAULT_DATA_SELECTOR, &sregs->ds); + kvm_seg_set_kernel_data_64bit(vm, DEFAULT_DATA_SELECTOR, &sregs->es); + kvm_setup_tss_64bit(vm, &sregs->tr, 0x18); break; default: TEST_FAIL("Unknown guest mode, mode: 0x%x", vm->mode); } - sregs.cr3 = vm->pgd; + sregs->cr3 = vm->pgd; +} + +static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu) +{ + struct kvm_sregs sregs; + + vcpu_sregs_get(vcpu, &sregs); + vcpu_setup_mode_sregs(vm, &sregs); vcpu_sregs_set(vcpu, &sregs); } From patchwork Tue Dec 12 20:46:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177555 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993150vqy; Tue, 12 Dec 2023 12:47:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IGjUkSjmOs7STpgE1BRCrInzwt4z57rXdDYza14KCERwmR9DLtmHFs8WvX23yR89Bt+l2Th X-Received: by 2002:a05:6358:cc1e:b0:170:87e:78d with SMTP id gx30-20020a056358cc1e00b00170087e078dmr7446051rwb.19.1702414033733; Tue, 12 Dec 2023 12:47:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414033; cv=none; d=google.com; s=arc-20160816; b=taPumn0TqSTgFhn9fxGEhKyHmn3PnL2do9PbS+PZZuSVcShJRQJk6QCK6Aba6gqqvD VgKdrU/78kOV62pELQOZCvct3lNypskYDI+RzAZuGcL8+equ43vNtirzMH5RzAPfrHZI yocJaQqJDd4QNXIM9kLDD8WahTX3zYj9ZJBwGf8wkBg9k0O+YcsY2uUAhJYtfNK2ofGv D8hFD+CL5J+zlbuwwUvR7VXughgfL1/157tu30bFS0YyiwfKU/pbrOzm6rT3GnR0tsGh X+KEBjX9PVV2ANW9zKragM55i3NUx1oVTCzC+4Rct+hrCQgQuaWrLUmgEGma0Ced1+qW OT5Q== 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=ir7Qh8CW4lvS6Nb/8/FOXimO+77Lwly2G5VfmqeIAwU=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=OA3U86s/lh9Su2fBQWCQ5sY/P2DzoNeT3bIRhva0DRlUTXEXCwIbY3iAmd4ObixkrD /dKqgHS/8AZyPpFLdefvfhzGS92FoWhaIueOQn7JCSTSKjeDgv9ypIA/NiuRMqyp1N4T WMvFt5znqpPFeS3o6Sd8dImo61OGwJYVvq1TZfR4+pqiuzjz3xLFaIajsJxUkNR/YGMo T1zPEkDVXpJZqX93tMotVw+7Ss3fuZ8h5cdsH/9qpp1vTg6s0SIajhUZ7jO9ns17ScK5 WYHv28GCEgEuCmv75XFtuIL4+hmU4X3kPXSjaZg26kRxYDe8WSaVd8/JovioucAro64E SJGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mKnVKtaL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id s1-20020a6550c1000000b005c688ef7549si8197068pgp.98.2023.12.12.12.47.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mKnVKtaL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 7977980747B9; Tue, 12 Dec 2023 12:47:12 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377255AbjLLUq7 (ORCPT + 99 others); Tue, 12 Dec 2023 15:46:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377235AbjLLUq5 (ORCPT ); Tue, 12 Dec 2023 15:46:57 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D84E8BD for ; Tue, 12 Dec 2023 12:47:03 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5df9f98994bso22942177b3.0 for ; Tue, 12 Dec 2023 12:47:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414023; x=1703018823; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ir7Qh8CW4lvS6Nb/8/FOXimO+77Lwly2G5VfmqeIAwU=; b=mKnVKtaLzz92jCYRp6EPap2lEuR2Ltznu7AUYXOTqHFp9r+93Xb4LgOrqRZ2hs9cvE fu1w5OKc4nSSrms2nsLNDKTSccJD1EG0YS0Gz7hvMVpoyT9EqEK3pfplUcBqrey37+7I F6gpHgP1kJ6dcFyyJkGReiIGh7OM9BkQTtuNVRW9JbUiZgcFGsyluFwRzgDJ/zWL4ku0 x6unFUGGqXruJzLPPQHZUwa5gfgNAL4YFO6scmoPySYy9BIiqmrxx3A3XeookSYFiK4a yXhB7w2+8vdydEh7ocG7Tkk7Qpz2nBlp653rUwZYRNBJbKTxmZBObdRBKm8hVv+UJXaB hdYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414023; x=1703018823; 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=ir7Qh8CW4lvS6Nb/8/FOXimO+77Lwly2G5VfmqeIAwU=; b=tLN88VdINNiXn4DxqEvJsAYnbcZD+2NyW1DbaCvy0Zb7P749ac+GyvRdySZ5cSjJfB MvlmylJyynQmcEaA8Jd4qhcf5s6yHbeKa3t6fCDGXa97zYcALV6BJxncO+ev/RDOZl/g KGZ7i3zJkvRaLi1pDc3fO+NAG1OXTvGZyKF5vEziv6mbVWrSwXHjZxqip/tWgRVejWBx J/dRpeH1IoSW5uQkkkICgnffKrr8NrP+1BNTl9fSKWO14j9McJGT5sMpbHfoodvW+J4r Zpy6pcEueyN0iVInHUXaBHuMtUINaMPYUBAzztRpys0/+wDIPIbK9JCAB2gqdcNnu2bw LxWw== X-Gm-Message-State: AOJu0YzXTG4SN3sUNJjd/GEkx01Oaq02F/2yXE6NeuLrKmzKl2pMGy7b dzUr+3f8gk4zqG44QFUx6jGEHV2XHw== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a25:cb55:0:b0:dbc:b2d4:4793 with SMTP id b82-20020a25cb55000000b00dbcb2d44793mr34903ybg.6.1702414023009; Tue, 12 Dec 2023 12:47:03 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:18 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-4-sagis@google.com> Subject: [RFC PATCH v5 03/29] KVM: selftests: Store initial stack address in struct kvm_vcpu From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-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_BLOCKED,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:12 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110497776131802 X-GMAIL-MSGID: 1785110497776131802 From: Ackerley Tng TDX guests' registers cannot be initialized directly using vcpu_regs_set(), hence the stack pointer needs to be initialized by the guest itself, running boot code beginning at the reset vector. We store the stack address as part of struct kvm_vcpu so that it can be accessible later to be passed to the boot code for rsp initialization. Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar Reviewed-by: Binbin Wu --- tools/testing/selftests/kvm/include/kvm_util_base.h | 1 + tools/testing/selftests/kvm/lib/x86_64/processor.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index c2e5c5f25dfc..b353617fcdd1 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -68,6 +68,7 @@ struct kvm_vcpu { int fd; struct kvm_vm *vm; struct kvm_run *run; + vm_vaddr_t initial_stack_addr; #ifdef __x86_64__ struct kvm_cpuid2 *cpuid; #endif diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index f130f78a4974..b6b9438e0a33 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -621,10 +621,12 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); vcpu_setup(vm, vcpu); + vcpu->initial_stack_addr = stack_vaddr; + /* Setup guest general purpose registers */ vcpu_regs_get(vcpu, ®s); regs.rflags = regs.rflags | 0x2; - regs.rsp = stack_vaddr; + regs.rsp = vcpu->initial_stack_addr; regs.rip = (unsigned long) guest_code; vcpu_regs_set(vcpu, ®s); From patchwork Tue Dec 12 20:46:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177557 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993215vqy; Tue, 12 Dec 2023 12:47:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IELLOq4/WTSm5BKeT31+CxPeS4tibOhLkKhZKHsCM5R8QhL7oiw6U1SvgDtD5+ovfYww/TG X-Received: by 2002:a17:902:e84c:b0:1d0:4cde:6e2b with SMTP id t12-20020a170902e84c00b001d04cde6e2bmr4702401plg.57.1702414041402; Tue, 12 Dec 2023 12:47:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414041; cv=none; d=google.com; s=arc-20160816; b=Gg//mUWpkRJJKuqTQzgm0bNWVsWq5Xs+KGbdnggRD+bxMafWzCFIn4hthSE5KFK/Im AEr3ihQWTQeGwoVC68AO0+4cIES4W0adwjJO8e3VAyofcv10ErIjxaXa2IHozifuXKti ewmYVqwygOKw6bzic7kwlQujVJn/DnylDDJRZdeVwdHk6TUmQwlFodsxPH5ixvGAcMHm y590v10JUy+FpD6e2Bb0tK9UCP5eqdrxi1+qZr2Etr5Zeo5A+pmfQE/6OZtKPdtvAqAS LjWv2um4q5tZd0WImVPGyD4cxwa/hPgoHfUgCaV2qIl/Tw8eP5IvXwnRQtVJLprUsDU1 ytMg== 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=x1lodvb6OtWfxjhf5SoaUUtA7ex1XZFNPc9w8OKKvUg=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=AMsercsa/I88W55iSrv1u4heK4dt/ATYBoL0BovW9t14hxQwVLtxFBdmAlehzRRFiK walBfI+qZRYED5LUQzYKmV/bIQ2A4CclsKzIsk0A6BhXYVeCZZPfu0qYtBAuvlW7yXTM HHZFLf7Pl2HIes5MMjKX8PLFNkOs7tP2qc9a3cBZmZ1KV0Uagb4lK4XMBAV1YzqHu8D7 55hkGuBOCax+JHEveBW6263Ly7BeuTRzOtXiGIn/rNcvoElkZ0SQzW5s0oALZ5sw5Ucj X+nBQh2T5SyqdjqOG+/IYq3FwPDLiNaPkYS3UargmpNz7FdA4VAPRP1wqhdd0WlVf+c2 Q2lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=V05TFJWO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id j4-20020a170902690400b001d3156ab971si4267849plk.389.2023.12.12.12.47.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=V05TFJWO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id CBCAB80F924F; Tue, 12 Dec 2023 12:47:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377301AbjLLUrC (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377254AbjLLUq7 (ORCPT ); Tue, 12 Dec 2023 15:46:59 -0500 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 86676D2 for ; Tue, 12 Dec 2023 12:47:05 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d6b751dabcso53483207b3.1 for ; Tue, 12 Dec 2023 12:47:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414025; x=1703018825; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=x1lodvb6OtWfxjhf5SoaUUtA7ex1XZFNPc9w8OKKvUg=; b=V05TFJWOpwlhLd3k3oEhlQ0LTZ8oXfSmFksigdfx8HAUfSFJdOmSQHvqPGxZt5PrbS ZJrzdXRUlDa19nPAB9Im4bwuejasfYoRwtTbYtibufvUVZzePX35jBD7Q8hsicivC8ZO 2UV+Yt65WPJ1LDBH1HParEl96fCwNr5/TbeHKw4j8Qr43mnhb0e4Z+YeTRSwd3TdKALe i/0Ez1XLfcJ2DF1hg3xGLq92PEhd+b0gVsS5IS7wPzgBAVdCklX7LtKQNxdeyJqRGIez f3QNnVtCDji5mHOO7O3hOegyQXQIlrNTd/YX1j9j018MR9/v9J9pcdOO90iJYvSdS4TW QyeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414025; x=1703018825; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x1lodvb6OtWfxjhf5SoaUUtA7ex1XZFNPc9w8OKKvUg=; b=wBFAWMw3CzBzwj26lNOd9kKgNUKwppWWOslt/Kw+HdMRRpLnbo9GHLAAEj4HDofgm9 r+MG3OE+tULyju67JSh+fVMolmff7BdDo+YKZY8mL3140L4Z5w06PBC3qDq7nbAEShjU bduw5zsuTCWFTsYXSV/plDOGLLmMZnqbcoRb6q1+g0WRal/4J+4+HIjrktQLq+Ps4Bng ASEYudyV3gJtikGuf2lR8fgaGk5J4vlaw1tRpf1mGVeg2MFNnsGKCmlobbJlE/jhPCRQ DMXQi5EVnqk5BfbdPJKqeumBEwgXHGcufWy4JVrmZ8wQIGjjcwTUS22ZKJXoIaugEvf8 2C5A== X-Gm-Message-State: AOJu0Yz0XW9KSrL0Th2+k6stXGILiR902uGcqmDNfaKbSgmTx0wWOUy5 c4RIJ0uisfEfFhd6umwqI1yD+x5rrw== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a25:ab26:0:b0:da0:3117:f35 with SMTP id u35-20020a25ab26000000b00da031170f35mr56027ybi.3.1702414024705; Tue, 12 Dec 2023 12:47:04 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:19 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-5-sagis@google.com> Subject: [RFC PATCH v5 04/29] KVM: selftests: Refactor steps in vCPU descriptor table initialization From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:18 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110505623524115 X-GMAIL-MSGID: 1785110505623524115 From: Ackerley Tng Split the vCPU descriptor table initialization process into a few steps and expose them: + Setting up the IDT + Syncing exception handlers into the guest In kvm_setup_idt(), we conditionally allocate guest memory for vm->idt to avoid double allocation when kvm_setup_idt() is used after vm_init_descriptor_tables(). Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- .../selftests/kvm/include/x86_64/processor.h | 2 ++ .../selftests/kvm/lib/x86_64/processor.c | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 0b8855d68744..5c4e9a27d9e2 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -1089,6 +1089,8 @@ struct idt_entry { uint32_t offset2; uint32_t reserved; }; +void kvm_setup_idt(struct kvm_vm *vm, struct kvm_dtable *dt); +void sync_exception_handlers_to_guest(struct kvm_vm *vm); void vm_init_descriptor_tables(struct kvm_vm *vm); void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu); void vm_install_exception_handler(struct kvm_vm *vm, int vector, diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index b6b9438e0a33..566d82829da4 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -1155,19 +1155,32 @@ void vm_init_descriptor_tables(struct kvm_vm *vm) DEFAULT_CODE_SELECTOR); } +void kvm_setup_idt(struct kvm_vm *vm, struct kvm_dtable *dt) +{ + if (!vm->idt) + vm->idt = vm_vaddr_alloc_page(vm); + + dt->base = vm->idt; + dt->limit = NUM_INTERRUPTS * sizeof(struct idt_entry) - 1; +} + +void sync_exception_handlers_to_guest(struct kvm_vm *vm) +{ + *(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers; +} + void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu) { struct kvm_vm *vm = vcpu->vm; struct kvm_sregs sregs; vcpu_sregs_get(vcpu, &sregs); - sregs.idt.base = vm->idt; - sregs.idt.limit = NUM_INTERRUPTS * sizeof(struct idt_entry) - 1; + kvm_setup_idt(vcpu->vm, &sregs.idt); sregs.gdt.base = vm->gdt; sregs.gdt.limit = getpagesize() - 1; kvm_seg_set_kernel_data_64bit(NULL, DEFAULT_DATA_SELECTOR, &sregs.gs); vcpu_sregs_set(vcpu, &sregs); - *(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers; + sync_exception_handlers_to_guest(vm); } void vm_install_exception_handler(struct kvm_vm *vm, int vector, From patchwork Tue Dec 12 20:46:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177558 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993236vqy; Tue, 12 Dec 2023 12:47:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6qsBPzVU0WHonXeCBFzQPSjFv+jJR4avhSsqTYk8A8/XRqs3HO9GLJJoUDxVNuQ+odOda X-Received: by 2002:a05:6a20:7f9b:b0:18f:97c:9272 with SMTP id d27-20020a056a207f9b00b0018f097c9272mr4373463pzj.87.1702414044243; Tue, 12 Dec 2023 12:47:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414044; cv=none; d=google.com; s=arc-20160816; b=ek8IWsy2Xy27n/Tq/qmG5YjWP/gyLh6ZWv1CxRVHReJfq9pYpFbXVniFoTCIZvlncF jJGhda5fQt0GBBYZyI+SmV1HJ1f5BFomsLfC4kyWPxfhfpwN7v4g144vbpp92Z1+gCeH 0NdlTwD3pV+bltgzhsj2kw7Udz2djud9tbRcBo++kHaA6Y3/wKTtBMp8/Oph6nNPokcs 3syxbLHV9LejSIQEyYEa8wiLxZcVmjHj1R3o1iU0bKWYeEkAbxcZnpIFOOe3tJ1fMXu4 6cKzb77rzLf0HTvPnYVsxUCqmH7UZxqQLFtqZw1/QG+vuZ4OYdbiugQdUOnybThsIgQl 64cQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=x7ZvOLT9J06Zy8digh7xf1W70IowkS7g1NC7PBZhRFQ=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=HRqRyulUkH6dSBgaDSwxoAw8H1HuHtwgZwD/ZPWPpmnzJr6bLmGdPXzSaBZQ1tKnpv U8heOAlzX/V7ldbP7g5VMtwR5jraxVjsuDsUkKLN0zmvMuG5hr6wIatpbWQRxDgyQhmJ GRw3iG25yaGTfXCLbwQX0dJCgnvYTNw0QtChKlXsyEA1WwFfVXJScbL8Wfc/OUjz0POQ I7JbVL74Hl694WKqAAkW7i1nFrIhCf3zO3JDNBiv2SmZsM6xIAWx/B85W0NJZsMnVo3q QzIA/eZe/fbQK/xMdxl9HTUkNHJlJr6Us26j+qm3Pe+/PsF1VygVV9DvhV/oIuMmqW74 ++bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Gn0JyL/S"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id z4-20020a656644000000b005c1faa82c06si8103122pgv.470.2023.12.12.12.47.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Gn0JyL/S"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 77E2980F9BF8; Tue, 12 Dec 2023 12:47:19 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377302AbjLLUrG (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377306AbjLLUrD (ORCPT ); Tue, 12 Dec 2023 15:47:03 -0500 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 213C6E8 for ; Tue, 12 Dec 2023 12:47:07 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1d04ce0214dso54509985ad.1 for ; Tue, 12 Dec 2023 12:47:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414026; x=1703018826; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=x7ZvOLT9J06Zy8digh7xf1W70IowkS7g1NC7PBZhRFQ=; b=Gn0JyL/SRcwO7lBtV/b2og+P8ZbPAYU+AibXXVshNoXOG0Il2XuLt9f3RpyV8ZQ/r0 zwCZJMxluwYJj24WGFmU2MdtPHIqtF8QAz9n6xZyHzGUUVXikhXd6wzaFDxsTjCeRLSR yQYEHNZ80TeWIWKRzJ2cFPwJz8TFq4ZYqMoSalbHsS/RO4Clop0QqS3JCmoyH0JTxC+a Uzmw6ADLy5I+Ig0mvJuThPceoXwM2y2eg6ookSOqw9QZJz/FusnwmRNAqqt955wODnHZ ZgtV7KGspCcU3DpKi7YSsPJYpETkzxVVHnVA1Pcm13f3zas/Ta+M4JUJfr/ykctQWE7p RYmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414026; x=1703018826; h=content-transfer-encoding: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=x7ZvOLT9J06Zy8digh7xf1W70IowkS7g1NC7PBZhRFQ=; b=iWmzDH3NZB2iT7osOuqVwnFEOZIoeqWxZAPOA2/N4bWrFoHj8cWAbZ5NzSyJjfqaXl Je0vdcZYa3V9zo1YUQkHiEGQ87WjY+nhCO5Ull7qkXfiPmv2GphV5JT6tPPFK5EbpHOM fhPxIaF9bkqfODTlAXmrVzguj9CAKoYBLo66DhZsu1Gh3bvVEregmX/hxnInyhKh1EHw 6VanL4r31WjhUnaBRfkpgS+1A88pgEl13g4VKeadXef3m9M0mm1ScDvFzBmfks2+77/A nJvu/ltFpIsaRlSEgHXLkWvNiU71Mz6xK9iDeurz5i9UnlPqFZ6TiKCxGHs+p2KXHLpz DBlA== X-Gm-Message-State: AOJu0Yxg/1DrzolFxCjqufnTrIA29O5Q9AvVAk1THOCcAT5GuRapedUS I3EkvNrFkdYAfsiz4VdLdDf0AmsX2A== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:f683:b0:1d0:56d3:8cee with SMTP id l3-20020a170902f68300b001d056d38ceemr50065plg.1.1702414026537; Tue, 12 Dec 2023 12:47:06 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:20 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-6-sagis@google.com> Subject: [RFC PATCH v5 05/29] KVM: selftests: Add helper functions to create TDX VMs From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:19 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110508816957970 X-GMAIL-MSGID: 1785110508816957970 From: Erdem Aktas TDX requires additional IOCTLs to initialize VM and vCPUs to add private memory and to finalize the VM memory. Also additional utility functions are provided to manipulate a TD, similar to those that manipulate a VM in the current selftest framework. A TD's initial register state cannot be manipulated directly by setting the VM's memory, hence boot code is provided at the TD's reset vector. This boot code takes boot parameters loaded in the TD's memory and sets up the TD for the selftest. Signed-off-by: Erdem Aktas Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng --- tools/testing/selftests/kvm/Makefile | 2 + .../kvm/include/x86_64/tdx/td_boot.h | 82 ++++ .../kvm/include/x86_64/tdx/td_boot_asm.h | 16 + .../kvm/include/x86_64/tdx/tdx_util.h | 16 + .../selftests/kvm/lib/x86_64/tdx/td_boot.S | 101 ++++ .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 434 ++++++++++++++++++ 6 files changed, 651 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/td_boot.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/td_boot_asm.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/td_boot.S create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index b11ac221aba4..a35150ab855f 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -50,6 +50,8 @@ 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_x86_64 += lib/x86_64/sev.c +LIBKVM_x86_64 += lib/x86_64/tdx/tdx_util.c +LIBKVM_x86_64 += lib/x86_64/tdx/td_boot.S LIBKVM_aarch64 += lib/aarch64/gic.c LIBKVM_aarch64 += lib/aarch64/gic_v3.c diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/td_boot.h b/tools/testing/selftests/kvm/include/x86_64/tdx/td_boot.h new file mode 100644 index 000000000000..148057e569d6 --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/td_boot.h @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_TDX_TD_BOOT_H +#define SELFTEST_TDX_TD_BOOT_H + +#include +#include "tdx/td_boot_asm.h" + +/* + * Layout for boot section (not to scale) + * + * GPA + * ┌─────────────────────────────┬──0x1_0000_0000 (4GB) + * │ Boot code trampoline │ + * ├─────────────────────────────┼──0x0_ffff_fff0: Reset vector (16B below 4GB) + * │ Boot code │ + * ├─────────────────────────────┼──td_boot will be copied here, so that the + * │ │ jmp to td_boot is exactly at the reset vector + * │ Empty space │ + * │ │ + * ├─────────────────────────────┤ + * │ │ + * │ │ + * │ Boot parameters │ + * │ │ + * │ │ + * └─────────────────────────────┴──0x0_ffff_0000: TD_BOOT_PARAMETERS_GPA + */ +#define FOUR_GIGABYTES_GPA (4ULL << 30) + +/** + * The exact memory layout for LGDT or LIDT instructions. + */ +struct __packed td_boot_parameters_dtr { + uint16_t limit; + uint32_t base; +}; + +/** + * The exact layout in memory required for a ljmp, including the selector for + * changing code segment. + */ +struct __packed td_boot_parameters_ljmp_target { + uint32_t eip_gva; + uint16_t code64_sel; +}; + +/** + * Allows each vCPU to be initialized with different eip and esp. + */ +struct __packed td_per_vcpu_parameters { + uint32_t esp_gva; + struct td_boot_parameters_ljmp_target ljmp_target; +}; + +/** + * Boot parameters for the TD. + * + * Unlike a regular VM, we can't ask KVM to set registers such as esp, eip, etc + * before boot, so to run selftests, these registers' values have to be + * initialized by the TD. + * + * This struct is loaded in TD private memory at TD_BOOT_PARAMETERS_GPA. + * + * The TD boot code will read off parameters from this struct and set up the + * vcpu for executing selftests. + */ +struct __packed td_boot_parameters { + uint32_t cr0; + uint32_t cr3; + uint32_t cr4; + struct td_boot_parameters_dtr gdtr; + struct td_boot_parameters_dtr idtr; + struct td_per_vcpu_parameters per_vcpu[]; +}; + +extern void td_boot(void); +extern void reset_vector(void); +extern void td_boot_code_end(void); + +#define TD_BOOT_CODE_SIZE (td_boot_code_end - td_boot) + +#endif /* SELFTEST_TDX_TD_BOOT_H */ diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/td_boot_asm.h b/tools/testing/selftests/kvm/include/x86_64/tdx/td_boot_asm.h new file mode 100644 index 000000000000..0a07104f7deb --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/td_boot_asm.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_TDX_TD_BOOT_ASM_H +#define SELFTEST_TDX_TD_BOOT_ASM_H + +/* + * GPA where TD boot parameters wil lbe loaded. + * + * TD_BOOT_PARAMETERS_GPA is arbitrarily chosen to + * + * + be within the 4GB address space + * + provide enough contiguous memory for the struct td_boot_parameters such + * that there is one struct td_per_vcpu_parameters for KVM_MAX_VCPUS + */ +#define TD_BOOT_PARAMETERS_GPA 0xffff0000 + +#endif // SELFTEST_TDX_TD_BOOT_ASM_H diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h new file mode 100644 index 000000000000..274b245f200b --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_TDX_KVM_UTIL_H +#define SELFTESTS_TDX_KVM_UTIL_H + +#include + +#include "kvm_util_base.h" + +struct kvm_vcpu *td_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, void *guest_code); + +struct kvm_vm *td_create(void); +void td_initialize(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, + uint64_t attributes); +void td_finalize(struct kvm_vm *vm); + +#endif // SELFTESTS_TDX_KVM_UTIL_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/td_boot.S b/tools/testing/selftests/kvm/lib/x86_64/tdx/td_boot.S new file mode 100644 index 000000000000..800e09264d4e --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/td_boot.S @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include "tdx/td_boot_asm.h" + +/* Offsets for reading struct td_boot_parameters */ +#define TD_BOOT_PARAMETERS_CR0 0 +#define TD_BOOT_PARAMETERS_CR3 4 +#define TD_BOOT_PARAMETERS_CR4 8 +#define TD_BOOT_PARAMETERS_GDT 12 +#define TD_BOOT_PARAMETERS_IDT 18 +#define TD_BOOT_PARAMETERS_PER_VCPU 24 + +/* Offsets for reading struct td_per_vcpu_parameters */ +#define TD_PER_VCPU_PARAMETERS_ESP_GVA 0 +#define TD_PER_VCPU_PARAMETERS_LJMP_TARGET 4 + +#define SIZEOF_TD_PER_VCPU_PARAMETERS 10 + +.code32 + +.globl td_boot +td_boot: + /* In this procedure, edi is used as a temporary register */ + cli + + /* Paging is off */ + + movl $TD_BOOT_PARAMETERS_GPA, %ebx + + /* + * Find the address of struct td_per_vcpu_parameters for this + * vCPU based on esi (TDX spec: initialized with vcpu id). Put + * struct address into register for indirect addressing + */ + movl $SIZEOF_TD_PER_VCPU_PARAMETERS, %eax + mul %esi + leal TD_BOOT_PARAMETERS_PER_VCPU(%ebx), %edi + addl %edi, %eax + + /* Setup stack */ + movl TD_PER_VCPU_PARAMETERS_ESP_GVA(%eax), %esp + + /* Setup GDT */ + leal TD_BOOT_PARAMETERS_GDT(%ebx), %edi + lgdt (%edi) + + /* Setup IDT */ + leal TD_BOOT_PARAMETERS_IDT(%ebx), %edi + lidt (%edi) + + /* + * Set up control registers (There are no instructions to + * mov from memory to control registers, hence we need to use ebx + * as a scratch register) + */ + movl TD_BOOT_PARAMETERS_CR4(%ebx), %edi + movl %edi, %cr4 + movl TD_BOOT_PARAMETERS_CR3(%ebx), %edi + movl %edi, %cr3 + movl TD_BOOT_PARAMETERS_CR0(%ebx), %edi + movl %edi, %cr0 + + /* Paging is on after setting the most significant bit on cr0 */ + + /* + * Jump to selftest guest code. Far jumps read from . This location has + * already been set up in boot parameters, and we can read boot + * parameters because boot code and boot parameters are loaded so + * that GVA and GPA are mapped 1:1. + */ + ljmp *TD_PER_VCPU_PARAMETERS_LJMP_TARGET(%eax) + +.globl reset_vector +reset_vector: + jmp td_boot + /* + * Pad reset_vector to its full size of 16 bytes so that this + * can be loaded with the end of reset_vector aligned to GPA=4G + */ + int3 + int3 + int3 + int3 + int3 + int3 + int3 + int3 + int3 + int3 + int3 + int3 + int3 + int3 + +/* Leave marker so size of td_boot code can be computed */ +.globl td_boot_code_end +td_boot_code_end: + +/* Disable executable stack */ +.section .note.GNU-stack,"",%progbits diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c new file mode 100644 index 000000000000..9b69c733ce01 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c @@ -0,0 +1,434 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#include "kvm_util.h" +#include "test_util.h" +#include "tdx/td_boot.h" +#include "kvm_util_base.h" +#include "processor.h" + +/* + * TDX ioctls + */ + +static char *tdx_cmd_str[] = { + "KVM_TDX_CAPABILITIES", + "KVM_TDX_INIT_VM", + "KVM_TDX_INIT_VCPU", + "KVM_TDX_INIT_MEM_REGION", + "KVM_TDX_FINALIZE_VM" +}; +#define TDX_MAX_CMD_STR (ARRAY_SIZE(tdx_cmd_str)) + +static void tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) +{ + struct kvm_tdx_cmd tdx_cmd; + int r; + + TEST_ASSERT(ioctl_no < TDX_MAX_CMD_STR, "Unknown TDX CMD : %d\n", + ioctl_no); + + memset(&tdx_cmd, 0x0, sizeof(tdx_cmd)); + tdx_cmd.id = ioctl_no; + tdx_cmd.flags = flags; + tdx_cmd.data = (uint64_t)data; + + r = ioctl(fd, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); + TEST_ASSERT(r == 0, "%s failed: %d %d", tdx_cmd_str[ioctl_no], r, + errno); +} + +#define XFEATURE_MASK_CET (XFEATURE_MASK_CET_USER | XFEATURE_MASK_CET_KERNEL) + +static void tdx_apply_cpuid_restrictions(struct kvm_cpuid2 *cpuid_data) +{ + for (int i = 0; i < cpuid_data->nent; i++) { + struct kvm_cpuid_entry2 *e = &cpuid_data->entries[i]; + + if (e->function == 0xd && e->index == 0) { + /* + * TDX module requires both XTILE_{CFG, DATA} to be set. + * Both bits are required for AMX to be functional. + */ + if ((e->eax & XFEATURE_MASK_XTILE) != + XFEATURE_MASK_XTILE) { + e->eax &= ~XFEATURE_MASK_XTILE; + } + } + if (e->function == 0xd && e->index == 1) { + /* + * TDX doesn't support LBR yet. + * Disable bits from the XCR0 register. + */ + e->ecx &= ~XFEATURE_MASK_LBR; + /* + * TDX modules requires both CET_{U, S} to be set even + * if only one is supported. + */ + if (e->ecx & XFEATURE_MASK_CET) + e->ecx |= XFEATURE_MASK_CET; + } + } +} + +static void tdx_td_init(struct kvm_vm *vm, uint64_t attributes) +{ + const struct kvm_cpuid2 *cpuid; + struct kvm_tdx_init_vm *init_vm; + + cpuid = kvm_get_supported_cpuid(); + + init_vm = malloc(sizeof(*init_vm) + + sizeof(init_vm->cpuid.entries[0]) * cpuid->nent); + + memset(init_vm, 0, sizeof(*init_vm)); + memcpy(&init_vm->cpuid, cpuid, kvm_cpuid2_size(cpuid->nent)); + + init_vm->attributes = attributes; + + tdx_apply_cpuid_restrictions(&init_vm->cpuid); + + tdx_ioctl(vm->fd, KVM_TDX_INIT_VM, 0, init_vm); +} + +static void tdx_td_vcpu_init(struct kvm_vcpu *vcpu) +{ + const struct kvm_cpuid2 *cpuid = kvm_get_supported_cpuid(); + + vcpu_init_cpuid(vcpu, cpuid); + tdx_ioctl(vcpu->fd, KVM_TDX_INIT_VCPU, 0, NULL); +} + +static void tdx_init_mem_region(struct kvm_vm *vm, void *source_pages, + uint64_t gpa, uint64_t size) +{ + struct kvm_tdx_init_mem_region mem_region = { + .source_addr = (uint64_t)source_pages, + .gpa = gpa, + .nr_pages = size / PAGE_SIZE, + }; + uint32_t metadata = KVM_TDX_MEASURE_MEMORY_REGION; + + TEST_ASSERT((mem_region.nr_pages > 0) && + ((mem_region.nr_pages * PAGE_SIZE) == size), + "Cannot add partial pages to the guest memory.\n"); + TEST_ASSERT(((uint64_t)source_pages & (PAGE_SIZE - 1)) == 0, + "Source memory buffer is not page aligned\n"); + tdx_ioctl(vm->fd, KVM_TDX_INIT_MEM_REGION, metadata, &mem_region); +} + +static void tdx_td_finalizemr(struct kvm_vm *vm) +{ + tdx_ioctl(vm->fd, KVM_TDX_FINALIZE_VM, 0, NULL); +} + +/* + * TD creation/setup/finalization + */ + +static void tdx_enable_capabilities(struct kvm_vm *vm) +{ + int rc; + + rc = kvm_check_cap(KVM_CAP_X2APIC_API); + TEST_ASSERT(rc, "TDX: KVM_CAP_X2APIC_API is not supported!"); + rc = kvm_check_cap(KVM_CAP_SPLIT_IRQCHIP); + TEST_ASSERT(rc, "TDX: KVM_CAP_SPLIT_IRQCHIP is not supported!"); + + vm_enable_cap(vm, KVM_CAP_X2APIC_API, + KVM_X2APIC_API_USE_32BIT_IDS | + KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK); + vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24); +} + +static void tdx_configure_memory_encryption(struct kvm_vm *vm) +{ + /* Configure shared/enCrypted bit for this VM according to TDX spec */ + vm->arch.s_bit = 1ULL << (vm->pa_bits - 1); + vm->arch.c_bit = 0; + /* Set gpa_protected_mask so that tagging/untagging of GPAs works */ + vm->gpa_protected_mask = vm->arch.s_bit; + /* This VM is protected (has memory encryption) */ + vm->protected = true; +} + +static void tdx_apply_cr4_restrictions(struct kvm_sregs *sregs) +{ + /* TDX spec 11.6.2: CR4 bit MCE is fixed to 1 */ + sregs->cr4 |= X86_CR4_MCE; + + /* Set this because UEFI also sets this up, to handle XMM exceptions */ + sregs->cr4 |= X86_CR4_OSXMMEXCPT; + + /* TDX spec 11.6.2: CR4 bit VMXE and SMXE are fixed to 0 */ + sregs->cr4 &= ~(X86_CR4_VMXE | X86_CR4_SMXE); +} + +static void load_td_boot_code(struct kvm_vm *vm) +{ + void *boot_code_hva = addr_gpa2hva(vm, FOUR_GIGABYTES_GPA - TD_BOOT_CODE_SIZE); + + TEST_ASSERT(td_boot_code_end - reset_vector == 16, + "The reset vector must be 16 bytes in size."); + memcpy(boot_code_hva, td_boot, TD_BOOT_CODE_SIZE); +} + +static void load_td_per_vcpu_parameters(struct td_boot_parameters *params, + struct kvm_sregs *sregs, + struct kvm_vcpu *vcpu, + void *guest_code) +{ + /* Store vcpu_index to match what the TDX module would store internally */ + static uint32_t vcpu_index; + + struct td_per_vcpu_parameters *vcpu_params = ¶ms->per_vcpu[vcpu_index]; + + TEST_ASSERT(vcpu->initial_stack_addr != 0, + "initial stack address should not be 0"); + TEST_ASSERT(vcpu->initial_stack_addr <= 0xffffffff, + "initial stack address must fit in 32 bits"); + TEST_ASSERT((uint64_t)guest_code <= 0xffffffff, + "guest_code must fit in 32 bits"); + TEST_ASSERT(sregs->cs.selector != 0, "cs.selector should not be 0"); + + vcpu_params->esp_gva = (uint32_t)(uint64_t)vcpu->initial_stack_addr; + vcpu_params->ljmp_target.eip_gva = (uint32_t)(uint64_t)guest_code; + vcpu_params->ljmp_target.code64_sel = sregs->cs.selector; + + vcpu_index++; +} + +static void load_td_common_parameters(struct td_boot_parameters *params, + struct kvm_sregs *sregs) +{ + /* Set parameters! */ + params->cr0 = sregs->cr0; + params->cr3 = sregs->cr3; + params->cr4 = sregs->cr4; + params->gdtr.limit = sregs->gdt.limit; + params->gdtr.base = sregs->gdt.base; + params->idtr.limit = sregs->idt.limit; + params->idtr.base = sregs->idt.base; + + TEST_ASSERT(params->cr0 != 0, "cr0 should not be 0"); + TEST_ASSERT(params->cr3 != 0, "cr3 should not be 0"); + TEST_ASSERT(params->cr4 != 0, "cr4 should not be 0"); + TEST_ASSERT(params->gdtr.base != 0, "gdt base address should not be 0"); +} + +static void load_td_boot_parameters(struct td_boot_parameters *params, + struct kvm_vcpu *vcpu, void *guest_code) +{ + struct kvm_sregs sregs; + + /* Assemble parameters in sregs */ + memset(&sregs, 0, sizeof(struct kvm_sregs)); + vcpu_setup_mode_sregs(vcpu->vm, &sregs); + tdx_apply_cr4_restrictions(&sregs); + kvm_setup_idt(vcpu->vm, &sregs.idt); + + if (!params->cr0) + load_td_common_parameters(params, &sregs); + + load_td_per_vcpu_parameters(params, &sregs, vcpu, guest_code); +} + +/** + * Adds a vCPU to a TD (Trusted Domain) with minimum defaults. It will not set + * up any general purpose registers as they will be initialized by the TDX. In + * TDX, vCPUs RIP is set to 0xFFFFFFF0. See Intel TDX EAS Section "Initial State + * of Guest GPRs" for more information on vCPUs initial register values when + * entering the TD first time. + * + * Input Args: + * vm - Virtual Machine + * vcpuid - The id of the VCPU to add to the VM. + */ +struct kvm_vcpu *td_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, void *guest_code) +{ + struct kvm_vcpu *vcpu; + + /* + * TD setup will not use the value of rip set in vm_vcpu_add anyway, so + * NULL can be used for guest_code. + */ + vcpu = vm_vcpu_add(vm, vcpu_id, NULL); + + tdx_td_vcpu_init(vcpu); + + load_td_boot_parameters(addr_gpa2hva(vm, TD_BOOT_PARAMETERS_GPA), + vcpu, guest_code); + + return vcpu; +} + +/** + * Iterate over set ranges within sparsebit @s. In each iteration, + * @range_begin and @range_end will take the beginning and end of the set range, + * which are of type sparsebit_idx_t. + * + * For example, if the range [3, 7] (inclusive) is set, within the iteration, + * @range_begin will take the value 3 and @range_end will take the value 7. + * + * Ensure that there is at least one bit set before using this macro with + * sparsebit_any_set(), because sparsebit_first_set() will abort if none are + * set. + */ +#define sparsebit_for_each_set_range(s, range_begin, range_end) \ + for (range_begin = sparsebit_first_set(s), \ + range_end = sparsebit_next_clear(s, range_begin) - 1; \ + range_begin && range_end; \ + range_begin = sparsebit_next_set(s, range_end), \ + range_end = sparsebit_next_clear(s, range_begin) - 1) +/* + * sparsebit_next_clear() can return 0 if [x, 2**64-1] are all set, and the -1 + * would then cause an underflow back to 2**64 - 1. This is expected and + * correct. + * + * If the last range in the sparsebit is [x, y] and we try to iterate, + * sparsebit_next_set() will return 0, and sparsebit_next_clear() will try and + * find the first range, but that's correct because the condition expression + * would cause us to quit the loop. + */ + +static void load_td_memory_region(struct kvm_vm *vm, + struct userspace_mem_region *region) +{ + const struct sparsebit *pages = region->protected_phy_pages; + const uint64_t hva_base = region->region.userspace_addr; + const vm_paddr_t gpa_base = region->region.guest_phys_addr; + const sparsebit_idx_t lowest_page_in_region = gpa_base >> + vm->page_shift; + + sparsebit_idx_t i; + sparsebit_idx_t j; + + if (!sparsebit_any_set(pages)) + return; + + sparsebit_for_each_set_range(pages, i, j) { + const uint64_t size_to_load = (j - i + 1) * vm->page_size; + const uint64_t offset = + (i - lowest_page_in_region) * vm->page_size; + const uint64_t hva = hva_base + offset; + const uint64_t gpa = gpa_base + offset; + void *source_addr; + + /* + * KVM_TDX_INIT_MEM_REGION ioctl cannot encrypt memory in place, + * hence we have to make a copy if there's only one backing + * memory source + */ + source_addr = mmap(NULL, size_to_load, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + TEST_ASSERT( + source_addr, + "Could not allocate memory for loading memory region"); + + memcpy(source_addr, (void *)hva, size_to_load); + + tdx_init_mem_region(vm, source_addr, gpa, size_to_load); + + munmap(source_addr, size_to_load); + } +} + +static void load_td_private_memory(struct kvm_vm *vm) +{ + int ctr; + struct userspace_mem_region *region; + + hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) { + load_td_memory_region(vm, region); + } +} + +struct kvm_vm *td_create(void) +{ + struct vm_shape shape; + + shape.mode = VM_MODE_DEFAULT; + shape.type = KVM_X86_TDX_VM; + return ____vm_create(shape); +} + +static void td_setup_boot_code(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type) +{ + vm_vaddr_t addr; + size_t boot_code_allocation = round_up(TD_BOOT_CODE_SIZE, PAGE_SIZE); + vm_paddr_t boot_code_base_gpa = FOUR_GIGABYTES_GPA - boot_code_allocation; + size_t npages = DIV_ROUND_UP(boot_code_allocation, PAGE_SIZE); + + vm_userspace_mem_region_add(vm, src_type, boot_code_base_gpa, 1, npages, + KVM_MEM_PRIVATE); + addr = vm_vaddr_alloc_1to1(vm, boot_code_allocation, boot_code_base_gpa, 1); + TEST_ASSERT_EQ(addr, boot_code_base_gpa); + + load_td_boot_code(vm); +} + +static size_t td_boot_parameters_size(void) +{ + int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS); + size_t total_per_vcpu_parameters_size = + max_vcpus * sizeof(struct td_per_vcpu_parameters); + + return sizeof(struct td_boot_parameters) + total_per_vcpu_parameters_size; +} + +static void td_setup_boot_parameters(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type) +{ + vm_vaddr_t addr; + size_t boot_params_size = td_boot_parameters_size(); + int npages = DIV_ROUND_UP(boot_params_size, PAGE_SIZE); + size_t total_size = npages * PAGE_SIZE; + + vm_userspace_mem_region_add(vm, src_type, TD_BOOT_PARAMETERS_GPA, 2, + npages, KVM_MEM_PRIVATE); + addr = vm_vaddr_alloc_1to1(vm, total_size, TD_BOOT_PARAMETERS_GPA, 2); + TEST_ASSERT_EQ(addr, TD_BOOT_PARAMETERS_GPA); +} + +void td_initialize(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, + uint64_t attributes) +{ + uint64_t nr_pages_required; + + tdx_enable_capabilities(vm); + + tdx_configure_memory_encryption(vm); + + tdx_td_init(vm, attributes); + + nr_pages_required = vm_nr_pages_required(VM_MODE_DEFAULT, 1, 0); + + /* + * Add memory (add 0th memslot) for TD. This will be used to setup the + * CPU (provide stack space for the CPU) and to load the elf file. + */ + vm_userspace_mem_region_add(vm, src_type, 0, 0, nr_pages_required, + KVM_MEM_PRIVATE); + + kvm_vm_elf_load(vm, program_invocation_name); + + vm_init_descriptor_tables(vm); + + td_setup_boot_code(vm, src_type); + td_setup_boot_parameters(vm, src_type); +} + +void td_finalize(struct kvm_vm *vm) +{ + sync_exception_handlers_to_guest(vm); + + load_td_private_memory(vm); + + tdx_td_finalizemr(vm); +} From patchwork Tue Dec 12 20:46:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177559 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993276vqy; Tue, 12 Dec 2023 12:47:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IEq/Fm5vmvLeOmHbbeS56Lu6mOTlenkQl3sYV0Ibgad+Np+hYA7+GdqDkHQDODrpNmO4MxE X-Received: by 2002:a05:6a20:3d81:b0:190:c314:35f8 with SMTP id s1-20020a056a203d8100b00190c31435f8mr3123256pzi.46.1702414049922; Tue, 12 Dec 2023 12:47:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414049; cv=none; d=google.com; s=arc-20160816; b=K/p7Yb71EPb18U0aPx9x31mE9xF5zX2ayQhBiPfd/Zw9yefp4DV6jk3uIOPEj2xY5y 70EG3KqgcVrVooZB6THYpVICtIak8OP3v5lWS4Z9RpLokxNYo7PCNpDzSekYaxjdXvCv d3Gg4KOAqruQOhXbj1CbRRf+hYm/ftIkPbdwzydnZX6RI4LyAsnA2X4uqYjApqqbS0Gi CjbHXRz3ktaQYxzyROuWG3tVo+roSg21orRkSusawbgCCfuyqHtB5beH3kSjRdfFVGpH qyZRNV2n77TGaeAQ/yZD5rmjRTNuAwTrC67l+fkPXYEAkBESP2QImLDl5Vy+YPBvd3A3 /MkQ== 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=63FW+3XX5/e8zea7VCMPF1eNiMssQ00z9r2TQiyi8pA=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=Ho4XCUkYd2vvll1qXKBq9QvlKwt12NifUCa5UozSq1VAo9NmMjRIsn2GjQ5H6kI0vg cnN2qNlLKqE72EKAImxbWEvdQYgvhlZDnE5CqY+2quHAAkj/xcrpNsCSev3e3gE4ngwg 0yqdKPaYBeKUjpWFZqJhth9ajsTAyBfgnoRi82ZnYWhhCkFpFCh6NquM7Mrpm5NA2Ad3 b+5I2el6jZpzM2tFzmsx8hTdeZKb+YdTWhcrlzPuAz+TUJKkGLvPOAUih55FPUkD/RUR xCZgxJlW4UKyR+07AxFx++aFfsu2YUOhso5DnUAQ/F2a3eTlyB2IPBLAuBbJpv8/8W/S zLtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mTsUbYaw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id kk10-20020a17090b4a0a00b00286f189a050si8543727pjb.22.2023.12.12.12.47.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mTsUbYaw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id D2F4E8047061; Tue, 12 Dec 2023 12:47:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377241AbjLLUrP (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377254AbjLLUrE (ORCPT ); Tue, 12 Dec 2023 15:47:04 -0500 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 179FBF2 for ; Tue, 12 Dec 2023 12:47:09 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d09a64eaebso54431485ad.3 for ; Tue, 12 Dec 2023 12:47:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414028; x=1703018828; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=63FW+3XX5/e8zea7VCMPF1eNiMssQ00z9r2TQiyi8pA=; b=mTsUbYawaFu4uUJg03yc2JUrbrvQKJW6LxsvxthD45xTSefLhQWOZ1UxgTIpGAXlG3 GEMQ1AM38NOiiWZm6HIsTjovI16+xDY/WznpeoE7nZHifj/luV+t8mryxjNBdox4u/ad r8Yq3PK87taTRxvaWQxVpaCouM3H+lW9ZzGsuON6GoR9CZnilQkstLkgA4fCUULQJWyY GUwOiKBvEb1wU3OeHIJdqGkd+ITSOptTcASBf3mHbXyDgSnLxs28N7eemEzJRkoreLFB DuMP+hTEaJbQONDLv/Bo+T7rQwPOWM2sjnxnxq1JWsRiUTilpRPujNSMnv+Ki9aN4nmU FxNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414028; x=1703018828; 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=63FW+3XX5/e8zea7VCMPF1eNiMssQ00z9r2TQiyi8pA=; b=b+wAZT21bIfwKOR2rL1d+aZt36U9VnlbaFMN988xnSHGJi+l2uIcbFsHOsO/NcYB3i 2gfunpL1ixetm+Z2c+t4qGdVQYmEr4YOM/+NOaYwb305x1fkD326WroGsEdiOCVV8Ugj /5roSRcUj+33/93Z+amQnFjSOdDurIeDFg5aELHJezfy0wOyRORaTZBo98NybiqyGF5R 8ZvQJJFa/gVU1PTF1Fptu5YEzl4RGCWbX7w0WrbgzjVw7kmPCrPvxTBgiVuO8mgRE16P iekAp7GMatxmTJrTNgdEugZrsz5oL5ezR5aliu6YrzHCEGz9FFbB+Y/sQ5JdRD7qNQTM gHFg== X-Gm-Message-State: AOJu0YzUvWpPwJofMJcLATYdIrBa6yhBSeMg8i1bGY1h2IUeeiMMhxpv JYxY9OUhDTpvpLjOu9kle2hKys8aag== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:e74e:b0:1d0:820a:cf0d with SMTP id p14-20020a170902e74e00b001d0820acf0dmr51718plf.6.1702414028261; Tue, 12 Dec 2023 12:47:08 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:21 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-7-sagis@google.com> Subject: [RFC PATCH v5 06/29] KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs' attribute configuration From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-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_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:29 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110515081733113 X-GMAIL-MSGID: 1785110515081733113 From: Ackerley Tng This also exercises the KVM_TDX_CAPABILITIES ioctl. Suggested-by: Isaku Yamahata Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar Reviewed-by: Binbin Wu --- .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 69 ++++++++++++++++++- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c index 9b69c733ce01..6b995c3f6153 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c @@ -27,10 +27,9 @@ static char *tdx_cmd_str[] = { }; #define TDX_MAX_CMD_STR (ARRAY_SIZE(tdx_cmd_str)) -static void tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) +static int _tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) { struct kvm_tdx_cmd tdx_cmd; - int r; TEST_ASSERT(ioctl_no < TDX_MAX_CMD_STR, "Unknown TDX CMD : %d\n", ioctl_no); @@ -40,11 +39,58 @@ static void tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) tdx_cmd.flags = flags; tdx_cmd.data = (uint64_t)data; - r = ioctl(fd, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); + return ioctl(fd, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); +} + +static void tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) +{ + int r; + + r = _tdx_ioctl(fd, ioctl_no, flags, data); TEST_ASSERT(r == 0, "%s failed: %d %d", tdx_cmd_str[ioctl_no], r, errno); } +static struct kvm_tdx_capabilities *tdx_read_capabilities(struct kvm_vm *vm) +{ + int i; + int rc = -1; + int nr_cpuid_configs = 4; + struct kvm_tdx_capabilities *tdx_cap = NULL; + + do { + nr_cpuid_configs *= 2; + + tdx_cap = realloc( + tdx_cap, sizeof(*tdx_cap) + + nr_cpuid_configs * sizeof(*tdx_cap->cpuid_configs)); + TEST_ASSERT(tdx_cap != NULL, + "Could not allocate memory for tdx capability nr_cpuid_configs %d\n", + nr_cpuid_configs); + + tdx_cap->nr_cpuid_configs = nr_cpuid_configs; + rc = _tdx_ioctl(vm->fd, KVM_TDX_CAPABILITIES, 0, tdx_cap); + } while (rc < 0 && errno == E2BIG); + + TEST_ASSERT(rc == 0, "KVM_TDX_CAPABILITIES failed: %d %d", + rc, errno); + + pr_debug("tdx_cap: attrs: fixed0 0x%016llx fixed1 0x%016llx\n" + "tdx_cap: xfam fixed0 0x%016llx fixed1 0x%016llx\n", + tdx_cap->attrs_fixed0, tdx_cap->attrs_fixed1, + tdx_cap->xfam_fixed0, tdx_cap->xfam_fixed1); + + for (i = 0; i < tdx_cap->nr_cpuid_configs; i++) { + const struct kvm_tdx_cpuid_config *config = + &tdx_cap->cpuid_configs[i]; + pr_debug("cpuid config[%d]: leaf 0x%x sub_leaf 0x%x eax 0x%08x ebx 0x%08x ecx 0x%08x edx 0x%08x\n", + i, config->leaf, config->sub_leaf, + config->eax, config->ebx, config->ecx, config->edx); + } + + return tdx_cap; +} + #define XFEATURE_MASK_CET (XFEATURE_MASK_CET_USER | XFEATURE_MASK_CET_KERNEL) static void tdx_apply_cpuid_restrictions(struct kvm_cpuid2 *cpuid_data) @@ -78,6 +124,21 @@ static void tdx_apply_cpuid_restrictions(struct kvm_cpuid2 *cpuid_data) } } +static void tdx_check_attributes(struct kvm_vm *vm, uint64_t attributes) +{ + struct kvm_tdx_capabilities *tdx_cap; + + tdx_cap = tdx_read_capabilities(vm); + + /* TDX spec: any bits 0 in attrs_fixed0 must be 0 in attributes */ + TEST_ASSERT_EQ(attributes & ~tdx_cap->attrs_fixed0, 0); + + /* TDX spec: any bits 1 in attrs_fixed1 must be 1 in attributes */ + TEST_ASSERT_EQ(attributes & tdx_cap->attrs_fixed1, tdx_cap->attrs_fixed1); + + free(tdx_cap); +} + static void tdx_td_init(struct kvm_vm *vm, uint64_t attributes) { const struct kvm_cpuid2 *cpuid; @@ -91,6 +152,8 @@ static void tdx_td_init(struct kvm_vm *vm, uint64_t attributes) memset(init_vm, 0, sizeof(*init_vm)); memcpy(&init_vm->cpuid, cpuid, kvm_cpuid2_size(cpuid->nent)); + tdx_check_attributes(vm, attributes); + init_vm->attributes = attributes; tdx_apply_cpuid_restrictions(&init_vm->cpuid); From patchwork Tue Dec 12 20:46:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177560 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993349vqy; Tue, 12 Dec 2023 12:47:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IEdTWLVMfzJdZ96IRPqNgGX6zrddpotYeWzj1iO7VCkC6Zkuf9vZt9Ekck73d1AH61QmD6C X-Received: by 2002:a17:90b:3708:b0:28a:cb6d:9291 with SMTP id mg8-20020a17090b370800b0028acb6d9291mr846203pjb.49.1702414063445; Tue, 12 Dec 2023 12:47:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414063; cv=none; d=google.com; s=arc-20160816; b=UPOjFIRpwOy1lBJQGxssPwhAj1CX9GB9eXG631E1A1gWW/A6PlxSxsCiV7jNY6fxkF 0kFCT+7LUgvrz2pNvDo7SST1UBGssdyVdd8XgqWsDBLGRbbCgyMoe7JzkukvFmlxr6Cg yx5FNG99CfLClbbVdbJt9KMFWgTspi30T7Rz6PdDLjyKarMy+uQALz9lKbjKASGC6p+d nywo5qKtrcWj2+Gqywh6iMp3nmVGfEfIW8ooHscAfH65PzAK9IyOqr2WgLVdtalS5R2U 7aHG3qLlb74+vDg3qWyc+jdiJyfk6eANfRTzOBnzZNBobGOpgl5UlgB+sZD74M7sRr9T 7z0w== 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=vZ+55M1H0qZo6X91hhLwa23PvdZhvqeKwQZ30mHQwEs=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=YtMIfcFz9mufi9vqPMbQv7YuBxKAxYLtYPmUvDE4baoIQTrxpp8q/YK9rb92qXqcxS 7PaMs1S5csVI8l2qGWLThAQjy7kTHWN0ddX/omNGwOn33PfnnMN6oMoOii+OWHgaAHCX pT7aoo/a2D3MIP4f0sRpq5khAiMokFw/XFjgPgAG76YqIWyFBBIzao3RiHBX84kEujiP TN7zYTMIoIeP9Z4qs3sw5m3BBBZ+2GOmIRHO6bTtX5T8tCvt5vu07dqwy4eExaXU8/EV WSFkKSqR82LNDuflG4Tlhp+EpihopAzFaGK4It5heKCu3C4WP2Q4gXmJSo2kAdCizAnz SSYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sgwQFDBe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id gx4-20020a17090b124400b00280664dd1f4si8278379pjb.9.2023.12.12.12.47.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sgwQFDBe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 9FAE980A80D2; Tue, 12 Dec 2023 12:47:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377254AbjLLUrT (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233106AbjLLUrN (ORCPT ); Tue, 12 Dec 2023 15:47:13 -0500 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 BA2F7110 for ; Tue, 12 Dec 2023 12:47:10 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d053953954so30441405ad.2 for ; Tue, 12 Dec 2023 12:47:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414030; x=1703018830; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vZ+55M1H0qZo6X91hhLwa23PvdZhvqeKwQZ30mHQwEs=; b=sgwQFDBeO7D7AOiCS9F/WPcG2kU+yP66lxhitL1QjGb4ri/z+W1c8FvGKsgJkBynq1 FBKU+vs04Gzf58dwetyTpmzBQWjqsO3MJP7Y9DKvYg5YZQPI4iQtbcUHqM5rIlvP2DVq JqPjBgmfH3D8OrPDfYrQ4gikRWEk8i1iXqWQa87M0Y7UvbbLHfqYyPMsJEBX9PVq6fgz VJzNLR8X522WaMZZ6JyaByDmehRh1vjH1Mz+j99ljatqIpy3NAZux2L374ov1jqyFfhj P43e5kD6u3SOYTZspRW3jHEG8OQwE92TlSSmlryy21XrEP2of0WP28TDdFR5iOHDSdjv bjjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414030; x=1703018830; 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=vZ+55M1H0qZo6X91hhLwa23PvdZhvqeKwQZ30mHQwEs=; b=dfA/maT8wHxkiBVXR3Qi4QFFvC7hKg9pM9MmgTUFVTl9DCe20O0HOt3NkVeJZnvF5X AXOhInNGf5wfp6CdcIdcTLOamh/P87EJuBL+dtqTjfI9aOSdG/R5SXe7NkAq2i/nvifL DyZg7fFK6j5OZzn04VI2tea8yE5lXpTsHlpQzDv8E8iSNvjtT/oAtn08FYJq3S1mC9vq 40d+W5W5KAktp9j3br484aw/cXG6Zer55TyoLI86CSeJOO+6b0rh94VRu+/4EEAjwh+D gF9DSvIMCww34lj4LbDZFwMB+7t69KVM2pZ2lzMCgtkmS2w39GFKfpMzM7QiPAskl3ej EplA== X-Gm-Message-State: AOJu0Yx+bxN0lp5jK4CJWO0XEGR/DHQfCmp89x7uf6W1BcenSWvH9HtR 7baBAuMsS2eOEJ+kaQ3iJvGpAjKv/g== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:e84a:b0:1d0:c738:73ad with SMTP id t10-20020a170902e84a00b001d0c73873admr49342plg.7.1702414029871; Tue, 12 Dec 2023 12:47:09 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:22 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-8-sagis@google.com> Subject: [RFC PATCH v5 07/29] KVM: selftests: TDX: Update load_td_memory_region for VM memory backed by guest memfd From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:35 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110528895638980 X-GMAIL-MSGID: 1785110528895638980 From: Ackerley Tng If guest memory is backed by restricted memfd + UPM is being used, hence encrypted memory region has to be registered + Can avoid making a copy of guest memory before getting TDX to initialize the memory region Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c index 6b995c3f6153..063ff486fb86 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c @@ -192,6 +192,21 @@ static void tdx_td_finalizemr(struct kvm_vm *vm) tdx_ioctl(vm->fd, KVM_TDX_FINALIZE_VM, 0, NULL); } +/* + * Other ioctls + */ + +/** + * Register a memory region that may contain encrypted data in KVM. + */ +static void register_encrypted_memory_region( + struct kvm_vm *vm, struct userspace_mem_region *region) +{ + vm_set_memory_attributes(vm, region->region.guest_phys_addr, + region->region.memory_size, + KVM_MEMORY_ATTRIBUTE_PRIVATE); +} + /* * TD creation/setup/finalization */ @@ -376,30 +391,38 @@ static void load_td_memory_region(struct kvm_vm *vm, if (!sparsebit_any_set(pages)) return; + + if (region->region.guest_memfd != -1) + register_encrypted_memory_region(vm, region); + sparsebit_for_each_set_range(pages, i, j) { const uint64_t size_to_load = (j - i + 1) * vm->page_size; const uint64_t offset = (i - lowest_page_in_region) * vm->page_size; const uint64_t hva = hva_base + offset; const uint64_t gpa = gpa_base + offset; - void *source_addr; + void *source_addr = (void *)hva; /* * KVM_TDX_INIT_MEM_REGION ioctl cannot encrypt memory in place, * hence we have to make a copy if there's only one backing * memory source */ - source_addr = mmap(NULL, size_to_load, PROT_READ | PROT_WRITE, - MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); - TEST_ASSERT( - source_addr, - "Could not allocate memory for loading memory region"); - - memcpy(source_addr, (void *)hva, size_to_load); + if (region->region.guest_memfd == -1) { + source_addr = mmap(NULL, size_to_load, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + TEST_ASSERT( + source_addr, + "Could not allocate memory for loading memory region"); + + memcpy(source_addr, (void *)hva, size_to_load); + memset((void *)hva, 0, size_to_load); + } tdx_init_mem_region(vm, source_addr, gpa, size_to_load); - munmap(source_addr, size_to_load); + if (region->region.guest_memfd == -1) + munmap(source_addr, size_to_load); } } From patchwork Tue Dec 12 20:46:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177563 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993421vqy; Tue, 12 Dec 2023 12:47:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IESqpTtwZvbXr+t80dWKndXSRfRGU6aaaMjpQvjATgGwkazlJJhL1HYmNkXGT6a4OvDGS5C X-Received: by 2002:a05:6a00:2451:b0:6ce:4010:d206 with SMTP id d17-20020a056a00245100b006ce4010d206mr3499865pfj.20.1702414076172; Tue, 12 Dec 2023 12:47:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414076; cv=none; d=google.com; s=arc-20160816; b=syeblRhXoO1pEfRuW0NR3d5SdJonzypmJ+T0KMjhalyLVDubSRmaIgRhmflqAar4ww 0OF0zTUyLmp3LvwZOIf74B+vmrMYoq7j/iVIwU6yIKh6hRKPZQJfzVM4UVWdtOYr72ST b+H1LIiMf1d/Xp+N3puFqGKk66yAxVwBX7Ee64RRcDlqyxW8pCGk5Pr4a8UmKzBqzkIT Zi4WloDwkmubFC1OaOhuc5HJpZEWY7XC6PZCyYeOd0HXRYr835F3/kRi7ZJMQISdJtN2 YPeJ2J8Dc7SBjMdPAOypYzBgsEfSmBlcVhZge1IOSxkw4w8Rb8nWzMov5H5FMdZVjSRl sf8A== 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=Lj4D0PhZEWa+J2oCsJqxn2vkhxEFsIHZYAmom0ZtdVA=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=KlFVZcDdK47CMpsV5bHunO9oZnFTDJObBrFAWOikWO/9NAfLBfiwaRfRhBtG6y1XC0 PnSQVlAm1bOd55wnjGZIZDNvlFL5YrDMimJtMl95zgVwd53K+zhUuaK2b8IT+CQf+IFo zDzpuim+KmWbg3O5phLyyn8PLnV9KodMxLZL3RbnfpUT+TbbUTsYpSM4Bt9P/uBNAQ5C cCTnQK9bNyPcIZYv21l6sEYEk34IYvQueaOtSfj+j27o4VzQerWXcyNgELwBYSgdshYi CiozO+rgDaC3bDkMntkRV+GMuv6sP9C3TD3XtvnEpi04VHOf4NfkvfOx1F5SlIcaDh8A 3VUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Xdy1wauA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id r9-20020a63ec49000000b005c658c16acdsi3516847pgj.196.2023.12.12.12.47.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Xdy1wauA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 7C80480B1802; Tue, 12 Dec 2023 12:47:47 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233306AbjLLUr3 (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235162AbjLLUrO (ORCPT ); Tue, 12 Dec 2023 15:47:14 -0500 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 7D224E8 for ; Tue, 12 Dec 2023 12:47:12 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d08383e566so54487245ad.2 for ; Tue, 12 Dec 2023 12:47:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414032; x=1703018832; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Lj4D0PhZEWa+J2oCsJqxn2vkhxEFsIHZYAmom0ZtdVA=; b=Xdy1wauAco14NOPJcwSIFWO8/PyfUzM4WYqVqKtXTgXWG3vYUzkDnLD66TEr2OeVq9 B7wgqnugikyAPngChOi7LvBkYA1GCOAYomQtKHmRLp4q0l7c+z1HRRfPDgbcETTrhYU6 /Cxyn5t5krDIfKIR21NYEa4r5JhyHkIQ7bApVqbz/ydfeWR9idwFD1pFhf/43wX/eWpD nfZmlSY7UeTSIJOrQAd37yA2t2R2qlEuyb7GI5P+//xKf2aHLDIkHZvRyC5qjhalsOL7 JU8403mOascA15s6iEl33algbSFiiVVvXl3SkCtdsdzJU86xnuUH39Nhov0f1cD3nz3x g25A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414032; x=1703018832; 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=Lj4D0PhZEWa+J2oCsJqxn2vkhxEFsIHZYAmom0ZtdVA=; b=oH6XkZ/dFbNG0oJ2z2wUTbIhumOoEq0po6EtSA3w2mru17prR4IEx6NekOhSmrtnzG opQYrWyhPq3hM9WOtITjceS1H24rvwZXnArPg/swKzLteaMPWiYD6Tn0jlPUkH86tXHa ESkLmUELokJanDRRSFzmeOvbXzlC3lmwuEZn5o2jKGXY2S4A0ztaeM+gleTu7TPiqoMO 1rSdxAQvpP6s8elCNm8VeM301TOQdiWFnPYEdwyF0PS2k3j4W50uT6Cn2Qs8cLKIWdQx 7H9U5ui61aXp9lcOcDfr909qNoVj2tEP/PCh8B6lB2IZvGCL3Ar42TxWO5XYuYZfZdJZ lhSQ== X-Gm-Message-State: AOJu0YzMTjR9lUgbDcoKAbI/8W9fvmq9Ww5HqDFpAxocpB3FlRMPPYDP cUIDagAIVE9qRhyZfoly7JHMNdUvTg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:da8b:b0:1d0:cd87:64db with SMTP id j11-20020a170902da8b00b001d0cd8764dbmr49191plx.12.1702414031654; Tue, 12 Dec 2023 12:47:11 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:23 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-9-sagis@google.com> Subject: [RFC PATCH v5 08/29] KVM: selftests: TDX: Add TDX lifecycle test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:47 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110542260550873 X-GMAIL-MSGID: 1785110542260550873 From: Erdem Aktas Adding a test to verify TDX lifecycle by creating a TD and running a dummy TDG.VP.VMCALL inside it. Signed-off-by: Erdem Aktas Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng --- tools/testing/selftests/kvm/Makefile | 4 + .../selftests/kvm/include/x86_64/tdx/tdcall.h | 35 ++++++++ .../selftests/kvm/include/x86_64/tdx/tdx.h | 12 +++ .../kvm/include/x86_64/tdx/test_util.h | 52 +++++++++++ .../selftests/kvm/lib/x86_64/tdx/tdcall.S | 90 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 27 ++++++ .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 1 + .../selftests/kvm/lib/x86_64/tdx/test_util.c | 34 +++++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 45 ++++++++++ 9 files changed, 300 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c create mode 100644 tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index a35150ab855f..80d4a50eeb9f 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -52,6 +52,9 @@ LIBKVM_x86_64 += lib/x86_64/vmx.c LIBKVM_x86_64 += lib/x86_64/sev.c LIBKVM_x86_64 += lib/x86_64/tdx/tdx_util.c LIBKVM_x86_64 += lib/x86_64/tdx/td_boot.S +LIBKVM_x86_64 += lib/x86_64/tdx/tdcall.S +LIBKVM_x86_64 += lib/x86_64/tdx/tdx.c +LIBKVM_x86_64 += lib/x86_64/tdx/test_util.c LIBKVM_aarch64 += lib/aarch64/gic.c LIBKVM_aarch64 += lib/aarch64/gic_v3.c @@ -152,6 +155,7 @@ TEST_GEN_PROGS_x86_64 += set_memory_region_test TEST_GEN_PROGS_x86_64 += steal_time TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test TEST_GEN_PROGS_x86_64 += system_counter_offset_test +TEST_GEN_PROGS_x86_64 += x86_64/tdx_vm_tests # Compiled outputs used by test targets TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h new file mode 100644 index 000000000000..78001bfec9c8 --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Adapted from arch/x86/include/asm/shared/tdx.h */ + +#ifndef SELFTESTS_TDX_TDCALL_H +#define SELFTESTS_TDX_TDCALL_H + +#include +#include + +#define TDG_VP_VMCALL_INSTRUCTION_IO_READ 0 +#define TDG_VP_VMCALL_INSTRUCTION_IO_WRITE 1 + +#define TDX_HCALL_HAS_OUTPUT BIT(0) + +#define TDX_HYPERCALL_STANDARD 0 + +/* + * Used in __tdx_hypercall() to pass down and get back registers' values of + * the TDCALL instruction when requesting services from the VMM. + * + * This is a software only structure and not part of the TDX module/VMM ABI. + */ +struct tdx_hypercall_args { + u64 r10; + u64 r11; + u64 r12; + u64 r13; + u64 r14; + u64 r15; +}; + +/* Used to request services from the VMM */ +u64 __tdx_hypercall(struct tdx_hypercall_args *args, unsigned long flags); + +#endif // SELFTESTS_TDX_TDCALL_H diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h new file mode 100644 index 000000000000..a7161efe4ee2 --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_TDX_TDX_H +#define SELFTEST_TDX_TDX_H + +#include + +#define TDG_VP_VMCALL_INSTRUCTION_IO 30 + +uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size, + uint64_t write, uint64_t *data); + +#endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h new file mode 100644 index 000000000000..b570b6d978ff --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_TDX_TEST_UTIL_H +#define SELFTEST_TDX_TEST_UTIL_H + +#include + +#include "tdcall.h" + +#define TDX_TEST_SUCCESS_PORT 0x30 +#define TDX_TEST_SUCCESS_SIZE 4 + +/** + * Assert that tdx_test_success() was called in the guest. + */ +#define TDX_TEST_ASSERT_SUCCESS(VCPU) \ + (TEST_ASSERT( \ + ((VCPU)->run->exit_reason == KVM_EXIT_IO) && \ + ((VCPU)->run->io.port == TDX_TEST_SUCCESS_PORT) && \ + ((VCPU)->run->io.size == TDX_TEST_SUCCESS_SIZE) && \ + ((VCPU)->run->io.direction == \ + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE), \ + "Unexpected exit values while waiting for test completion: %u (%s) %d %d %d\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason), \ + (VCPU)->run->io.port, (VCPU)->run->io.size, \ + (VCPU)->run->io.direction)) + +/** + * Run a test in a new process. + * + * There might be multiple tests we are running and if one test fails, it will + * prevent the subsequent tests to run due to how tests are failing with + * TEST_ASSERT function. The run_in_new_process function will run a test in a + * new process context and wait for it to finish or fail to prevent TEST_ASSERT + * to kill the main testing process. + */ +void run_in_new_process(void (*func)(void)); + +/** + * Verify that the TDX is supported by KVM. + */ +bool is_tdx_enabled(void); + +/** + * Report test success to userspace. + * + * Use TDX_TEST_ASSERT_SUCCESS() to assert that this function was called in the + * guest. + */ +void tdx_test_success(void); + +#endif // SELFTEST_TDX_TEST_UTIL_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S new file mode 100644 index 000000000000..df9c1ed4bb2d --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Adapted from arch/x86/coco/tdx/tdcall.S */ + +#define TDX_HYPERCALL_r10 0 /* offsetof(struct tdx_hypercall_args, r10) */ +#define TDX_HYPERCALL_r11 8 /* offsetof(struct tdx_hypercall_args, r11) */ +#define TDX_HYPERCALL_r12 16 /* offsetof(struct tdx_hypercall_args, r12) */ +#define TDX_HYPERCALL_r13 24 /* offsetof(struct tdx_hypercall_args, r13) */ +#define TDX_HYPERCALL_r14 32 /* offsetof(struct tdx_hypercall_args, r14) */ +#define TDX_HYPERCALL_r15 40 /* offsetof(struct tdx_hypercall_args, r15) */ + +/* + * Bitmasks of exposed registers (with VMM). + */ +#define TDX_R10 0x400 +#define TDX_R11 0x800 +#define TDX_R12 0x1000 +#define TDX_R13 0x2000 +#define TDX_R14 0x4000 +#define TDX_R15 0x8000 + +#define TDX_HCALL_HAS_OUTPUT 0x1 + +/* + * These registers are clobbered to hold arguments for each + * TDVMCALL. They are safe to expose to the VMM. + * Each bit in this mask represents a register ID. Bit field + * details can be found in TDX GHCI specification, section + * titled "TDCALL [TDG.VP.VMCALL] leaf". + */ +#define TDVMCALL_EXPOSE_REGS_MASK ( TDX_R10 | TDX_R11 | \ + TDX_R12 | TDX_R13 | \ + TDX_R14 | TDX_R15 ) + +.code64 +.section .text + +.globl __tdx_hypercall +.type __tdx_hypercall, @function +__tdx_hypercall: + /* Set up stack frame */ + push %rbp + movq %rsp, %rbp + + /* Save callee-saved GPRs as mandated by the x86_64 ABI */ + push %r15 + push %r14 + push %r13 + push %r12 + + /* Mangle function call ABI into TDCALL ABI: */ + /* Set TDCALL leaf ID (TDVMCALL (0)) in RAX */ + xor %eax, %eax + + /* Copy hypercall registers from arg struct: */ + movq TDX_HYPERCALL_r10(%rdi), %r10 + movq TDX_HYPERCALL_r11(%rdi), %r11 + movq TDX_HYPERCALL_r12(%rdi), %r12 + movq TDX_HYPERCALL_r13(%rdi), %r13 + movq TDX_HYPERCALL_r14(%rdi), %r14 + movq TDX_HYPERCALL_r15(%rdi), %r15 + + movl $TDVMCALL_EXPOSE_REGS_MASK, %ecx + + tdcall + + /* TDVMCALL leaf return code is in R10 */ + movq %r10, %rax + + /* Copy hypercall result registers to arg struct if needed */ + testq $TDX_HCALL_HAS_OUTPUT, %rsi + jz .Lout + + movq %r10, TDX_HYPERCALL_r10(%rdi) + movq %r11, TDX_HYPERCALL_r11(%rdi) + movq %r12, TDX_HYPERCALL_r12(%rdi) + movq %r13, TDX_HYPERCALL_r13(%rdi) + movq %r14, TDX_HYPERCALL_r14(%rdi) + movq %r15, TDX_HYPERCALL_r15(%rdi) +.Lout: + /* Restore callee-saved GPRs as mandated by the x86_64 ABI */ + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + pop %rbp + ret + +/* Disable executable stack */ +.section .note.GNU-stack,"",%progbits diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c new file mode 100644 index 000000000000..c2414523487a --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "tdx/tdcall.h" +#include "tdx/tdx.h" + +uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size, + uint64_t write, uint64_t *data) +{ + uint64_t ret; + struct tdx_hypercall_args args = { + .r10 = TDX_HYPERCALL_STANDARD, + .r11 = TDG_VP_VMCALL_INSTRUCTION_IO, + .r12 = size, + .r13 = write, + .r14 = port, + }; + + if (write) + args.r15 = *data; + + ret = __tdx_hypercall(&args, write ? 0 : TDX_HCALL_HAS_OUTPUT); + + if (!write) + *data = args.r11; + + return ret; +} diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c index 063ff486fb86..b302060049d5 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c @@ -224,6 +224,7 @@ static void tdx_enable_capabilities(struct kvm_vm *vm) KVM_X2APIC_API_USE_32BIT_IDS | KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK); vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24); + vm_enable_cap(vm, KVM_CAP_MAX_VCPUS, 512); } static void tdx_configure_memory_encryption(struct kvm_vm *vm) diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c new file mode 100644 index 000000000000..6905d0ca3877 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include +#include + +#include "kvm_util_base.h" +#include "tdx/tdx.h" +#include "tdx/test_util.h" + +void run_in_new_process(void (*func)(void)) +{ + if (fork() == 0) { + func(); + exit(0); + } + wait(NULL); +} + +bool is_tdx_enabled(void) +{ + return !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_TDX_VM)); +} + +void tdx_test_success(void) +{ + uint64_t code = 0; + + tdg_vp_vmcall_instruction_io(TDX_TEST_SUCCESS_PORT, + TDX_TEST_SUCCESS_SIZE, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, &code); +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c new file mode 100644 index 000000000000..a18d1c9d6026 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include "kvm_util_base.h" +#include "tdx/tdx_util.h" +#include "tdx/test_util.h" +#include "test_util.h" + +void guest_code_lifecycle(void) +{ + tdx_test_success(); +} + +void verify_td_lifecycle(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_code_lifecycle); + td_finalize(vm); + + printf("Verifying TD lifecycle:\n"); + + vcpu_run(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + +int main(int argc, char **argv) +{ + setbuf(stdout, NULL); + + if (!is_tdx_enabled()) { + print_skip("TDX is not supported by the KVM"); + exit(KSFT_SKIP); + } + + run_in_new_process(&verify_td_lifecycle); + + return 0; +} From patchwork Tue Dec 12 20:46:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177561 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993371vqy; Tue, 12 Dec 2023 12:47:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IEOR5vKPej/4zDHciYjPsHJnEa+hByw88wFmB43nEUcRLoRnSjXBAVCxKEsPaigeUk6uLu/ X-Received: by 2002:a05:6a20:9390:b0:188:2b6:316b with SMTP id x16-20020a056a20939000b0018802b6316bmr8961229pzh.38.1702414067045; Tue, 12 Dec 2023 12:47:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414067; cv=none; d=google.com; s=arc-20160816; b=IEKFY/C5OJcJLwiw2MYu910YBYUzgQvOOxPCGFkc3brD3E4bQC3uBmm22+iN1bzQE3 Ogr17FLyFSB3NEB+rgHfxLoz7sDKkKxp9Hl2U37qxdZxwiaGiUvlGsh5YCdqDXWbfzne A7/pxbZCPqDSsTHJJ4uXSPw9BHnokdRi5JWIECPOf0QFOWk+RP/sU8G5k1QpwbYAdjzA L6dj/Z3yfsCeyxPrndTgwqHAB2/NMnb3K8r7cukGqPNiDM/6hb2LVFvSpgrrzj9UKoI4 A5qxDMulT9+AHQYCdfXww722WuBIRC/sb2lGKni9+15telNEKYXHR9qiKHlriq8hy5+7 CyCQ== 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=+oFTGXJSfXzvmAiUKHB1pxE3R2ft6C/AFFmYxuAN1SU=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=zXqYiUZnfoefC1WP7BlEb+BUNQNazbUxspP3mNFNY87F3uFe61Vtp3/D03cVdAtTFY grbtnoz9tH7Yr4O6keiVRQz5QL2sTokvfyVFWq8tASC6OSoQ7VKAkT9XjaUOROAAw1Dn h4UfaBPz9/hwFwYqSCIixdMpnYJ7Mv0hxQFS5YhS3dbylJ0G9QOTt/+GcCkRVUZXXUPq tpzDbyP+uk00QeLaexgL5PFcpoV7s/e4suJhXc2Pc9be/EALowicg7w9V0B4LwGdvGCW AkvFzo/mUhSJWsPS7V0HU8XvSKildOBUIMMaRWLbNDUtTMv5mB3M0RbZvTyMvx8x7qRo Tikg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="sjCiQ/SX"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id n128-20020a632786000000b005b11ce9c97esi8164307pgn.353.2023.12.12.12.47.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="sjCiQ/SX"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id A17DC80B2316; Tue, 12 Dec 2023 12:47:45 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235207AbjLLUre (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235166AbjLLUrO (ORCPT ); Tue, 12 Dec 2023 15:47:14 -0500 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 53FB612B for ; Tue, 12 Dec 2023 12:47:14 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5e183c40a91so23072117b3.2 for ; Tue, 12 Dec 2023 12:47:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414033; x=1703018833; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+oFTGXJSfXzvmAiUKHB1pxE3R2ft6C/AFFmYxuAN1SU=; b=sjCiQ/SXSLEOPJLvBBHQ0WQfhsVJNRAcypme2seSziGLvwIlQUWcD0JE9sZgpYzQBb klmP8nCNlI7TQOdMhM11iHwOaM8XlnMeVBvZ3JovhyRc9BgOgdAqpTk4BxIAbVcpJkXC aARRijMc1ZblZtrVL1IO+JxqG1Qt/2nWAeodCg8eJ4mG8BPZw5LbzTc2EPh8EzzPZqzq vXXxx/M/FxhEyQGmBVQ2gRHvzNYbvgCRDTGJzwh1Cofsl6DHFaNlifAaZ1c/30IMNJW4 w6SKCu/pI5o6j1+urA2479uOUsX1pti3HoDyOsuZC9Dyz+SQPDvX7GYhvhiW9+PeFN4p Sykg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414033; x=1703018833; 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=+oFTGXJSfXzvmAiUKHB1pxE3R2ft6C/AFFmYxuAN1SU=; b=wB//tHk1sp/rj1HP/ek5r85kdR+lCH0pirg2Utyv4/A3TY0Vy/Egjg9j4sQVroLG4/ kiCDSn9dDQq+QrGOQFLQpCxzdvzH/0OgSfyIK0dzJ4rPhT9CMfgND2oUGDvW6ZhGqiwk Yx4dsEldxEFJpLKTw56eFLfg9gW8Tm9GVMng/Jwu6X0jL1nkubl1lwlSj4g6ep58JlhG C19gedEOAR6ZYPFL9F20M31/Qn3/HobxK1WUGjqibbHP786DbLeXiJFgeFYOvvcx/SLP FpFiK6KXtxT+u7TxJQXI6RQhIaxsA8iaSo0q2Oje32bu3eY3qY8WC37K/ewAJFATGT4o MFNg== X-Gm-Message-State: AOJu0YzFuqQlGBYWAMWAuW7FiiumNsh88wvgZmaZ2jv17pxX1fjGABCt xvc1FzrXC1Oc43bOr8dvUepHhVx7LQ== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:6902:534:b0:db4:6936:48b7 with SMTP id y20-20020a056902053400b00db4693648b7mr58935ybs.2.1702414033563; Tue, 12 Dec 2023 12:47:13 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:24 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-10-sagis@google.com> Subject: [RFC PATCH v5 09/29] KVM: selftests: TDX: Add report_fatal_error test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-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_BLOCKED,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:45 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110532668562880 X-GMAIL-MSGID: 1785110532668562880 The test checks report_fatal_error functionality. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/x86_64/tdx/tdx.h | 6 ++- .../kvm/include/x86_64/tdx/tdx_util.h | 1 + .../kvm/include/x86_64/tdx/test_util.h | 19 ++++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 39 ++++++++++++++++ .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 12 +++++ .../selftests/kvm/lib/x86_64/tdx/test_util.c | 10 +++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 45 +++++++++++++++++++ 7 files changed, 131 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index a7161efe4ee2..1340c1070002 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -3,10 +3,14 @@ #define SELFTEST_TDX_TDX_H #include +#include "kvm_util_base.h" -#define TDG_VP_VMCALL_INSTRUCTION_IO 30 +#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 +#define TDG_VP_VMCALL_INSTRUCTION_IO 30 +void handle_userspace_tdg_vp_vmcall_exit(struct kvm_vcpu *vcpu); uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size, uint64_t write, uint64_t *data); +void tdg_vp_vmcall_report_fatal_error(uint64_t error_code, uint64_t data_gpa); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h index 274b245f200b..32dd6b8fda46 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h @@ -12,5 +12,6 @@ struct kvm_vm *td_create(void); void td_initialize(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t attributes); void td_finalize(struct kvm_vm *vm); +void td_vcpu_run(struct kvm_vcpu *vcpu); #endif // SELFTESTS_TDX_KVM_UTIL_H diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h index b570b6d978ff..6d69921136bd 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h @@ -49,4 +49,23 @@ bool is_tdx_enabled(void); */ void tdx_test_success(void); +/** + * Report an error with @error_code to userspace. + * + * Return value from tdg_vp_vmcall_report_fatal_error is ignored since execution + * is not expected to continue beyond this point. + */ +void tdx_test_fatal(uint64_t error_code); + +/** + * Report an error with @error_code to userspace. + * + * @data_gpa may point to an optional shared guest memory holding the error + * string. + * + * Return value from tdg_vp_vmcall_report_fatal_error is ignored since execution + * is not expected to continue beyond this point. + */ +void tdx_test_fatal_with_data(uint64_t error_code, uint64_t data_gpa); + #endif // SELFTEST_TDX_TEST_UTIL_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index c2414523487a..b854c3aa34ff 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -1,8 +1,31 @@ // SPDX-License-Identifier: GPL-2.0-only +#include + #include "tdx/tdcall.h" #include "tdx/tdx.h" +void handle_userspace_tdg_vp_vmcall_exit(struct kvm_vcpu *vcpu) +{ + struct kvm_tdx_vmcall *vmcall_info = &vcpu->run->tdx.u.vmcall; + uint64_t vmcall_subfunction = vmcall_info->subfunction; + + switch (vmcall_subfunction) { + case TDG_VP_VMCALL_REPORT_FATAL_ERROR: + vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; + vcpu->run->system_event.ndata = 3; + vcpu->run->system_event.data[0] = + TDG_VP_VMCALL_REPORT_FATAL_ERROR; + vcpu->run->system_event.data[1] = vmcall_info->in_r12; + vcpu->run->system_event.data[2] = vmcall_info->in_r13; + vmcall_info->status_code = 0; + break; + default: + TEST_FAIL("TD VMCALL subfunction %lu is unsupported.\n", + vmcall_subfunction); + } +} + uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size, uint64_t write, uint64_t *data) { @@ -25,3 +48,19 @@ uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size, return ret; } + +void tdg_vp_vmcall_report_fatal_error(uint64_t error_code, uint64_t data_gpa) +{ + struct tdx_hypercall_args args; + + memset(&args, 0, sizeof(struct tdx_hypercall_args)); + + if (data_gpa) + error_code |= 0x8000000000000000; + + args.r11 = TDG_VP_VMCALL_REPORT_FATAL_ERROR; + args.r12 = error_code; + args.r13 = data_gpa; + + __tdx_hypercall(&args, 0); +} diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c index b302060049d5..d745bb6287c1 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c @@ -10,6 +10,7 @@ #include "kvm_util.h" #include "test_util.h" +#include "tdx/tdx.h" #include "tdx/td_boot.h" #include "kvm_util_base.h" #include "processor.h" @@ -519,3 +520,14 @@ void td_finalize(struct kvm_vm *vm) tdx_td_finalizemr(vm); } + +void td_vcpu_run(struct kvm_vcpu *vcpu) +{ + vcpu_run(vcpu); + + /* Handle TD VMCALLs that require userspace handling. */ + if (vcpu->run->exit_reason == KVM_EXIT_TDX && + vcpu->run->tdx.type == KVM_EXIT_TDX_VMCALL) { + handle_userspace_tdg_vp_vmcall_exit(vcpu); + } +} diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c index 6905d0ca3877..7f3cd8089cea 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c @@ -32,3 +32,13 @@ void tdx_test_success(void) TDX_TEST_SUCCESS_SIZE, TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, &code); } + +void tdx_test_fatal_with_data(uint64_t error_code, uint64_t data_gpa) +{ + tdg_vp_vmcall_report_fatal_error(error_code, data_gpa); +} + +void tdx_test_fatal(uint64_t error_code) +{ + tdx_test_fatal_with_data(error_code, 0); +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index a18d1c9d6026..8638c7bbedaa 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -2,6 +2,7 @@ #include #include "kvm_util_base.h" +#include "tdx/tdx.h" #include "tdx/tdx_util.h" #include "tdx/test_util.h" #include "test_util.h" @@ -30,6 +31,49 @@ void verify_td_lifecycle(void) printf("\t ... PASSED\n"); } +void guest_code_report_fatal_error(void) +{ + uint64_t err; + + /* + * Note: err should follow the GHCI spec definition: + * bits 31:0 should be set to 0. + * bits 62:32 are used for TD-specific extended error code. + * bit 63 is used to mark additional information in shared memory. + */ + err = 0x0BAAAAAD00000000; + if (err) + tdx_test_fatal(err); + + tdx_test_success(); +} +void verify_report_fatal_error(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_code_report_fatal_error); + td_finalize(vm); + + printf("Verifying report_fatal_error:\n"); + + td_vcpu_run(vcpu); + + TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + TEST_ASSERT_EQ(vcpu->run->system_event.ndata, 3); + TEST_ASSERT_EQ(vcpu->run->system_event.data[0], TDG_VP_VMCALL_REPORT_FATAL_ERROR); + TEST_ASSERT_EQ(vcpu->run->system_event.data[1], 0x0BAAAAAD00000000); + TEST_ASSERT_EQ(vcpu->run->system_event.data[2], 0); + + vcpu_run(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -40,6 +84,7 @@ int main(int argc, char **argv) } run_in_new_process(&verify_td_lifecycle); + run_in_new_process(&verify_report_fatal_error); return 0; } From patchwork Tue Dec 12 20:46:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177562 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993405vqy; Tue, 12 Dec 2023 12:47:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IH4nB1PeRj/9lzOTYgrNALiSeCaGOK1269wcp8sl6I15bVHjGtU/B3x2E35wor1vnc/4XI4 X-Received: by 2002:a17:902:8682:b0:1d0:cd9e:424b with SMTP id g2-20020a170902868200b001d0cd9e424bmr6402002plo.43.1702414073204; Tue, 12 Dec 2023 12:47:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414073; cv=none; d=google.com; s=arc-20160816; b=T8ECTCrBivHc15Z50qbTQvFQnt09t0FjeAFQy+5TUv5qKnuEr9kRh7TMl48XOlO4it nL3oN7TArIlGSKo7rMUc442a6Ofed2xK9cErR0t3MiY+H65P1COPnkz2zLQYd2CFT9+V g42hzopA14970qtcbuKgfZNOpVGaPQtlaXKiKv8ikOg7vrqoQVt21Xas3yKKYqt4qR7D fwaNiW7ZKpI/oAJMNsWzAjiVFxFgkc1fG7gMfoiNECv9O7fd1Xy09v8NHyXFPKckW2Hj qtnvfjxjYgwEL/BvpdIm0W0d/itmiF1fJqVLxsvHIZlGll365GaENzXNOdehBFyiNYf4 OuAg== 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=XMXRsZ7PnaagoSZjHTHBbcv9aekrkY1R/86PgELFK84=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=lCSv1OgN7oeLbB5Xl5CWbEZrTJSq5wPn5p4jRXbWZC7IQt3F4SLXIb7bpdQ8LtZr9Q vuhwj06TIESBRqOt0RyeJsXFt6UQEg+cYVVWrFf3rHnRWlianloytsBxnk1JzbgJMGYl +/Qoxq9p4fKAhgzrF2mLhu4e2lpcEDtSE2jwAqw2/9se+FdfoEZ3abbXPsMqHjweo8hK cdqP3LItLfnl8SvzldHjit32rEZuehPG/MxTZ+DcL3fnYKwIo394itrIY1rMvplPUX4V uijApldIh6UlKwZoAg1+8prXhHCfqALlXlpENhk1rtKRPzrv+lm616h1A4OmSLlWuDpF pOWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fh0buOwF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id em9-20020a17090b014900b0028ad088026asi764679pjb.76.2023.12.12.12.47.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fh0buOwF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id EC08D80B2322; Tue, 12 Dec 2023 12:47:51 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377302AbjLLUrk (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377253AbjLLUrP (ORCPT ); Tue, 12 Dec 2023 15:47:15 -0500 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 D0083CD for ; Tue, 12 Dec 2023 12:47:15 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d33751a322so5236095ad.3 for ; Tue, 12 Dec 2023 12:47:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414035; x=1703018835; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=XMXRsZ7PnaagoSZjHTHBbcv9aekrkY1R/86PgELFK84=; b=fh0buOwFBJxvTVVrcGQW2OIX3FM/IByL12fG8HL57mw4E5H+dSmwJFgZvjzKg+2OgP iQ2tVsGP5j2M5ElR9E74cbb3o0wC1CYxat6fAAMwFXXqXxv4ZQq3LbBba54O1t7TQIHA EvBAWRR/vQn1ShO0NIzcAAwLIWcdQ05SI4J7jBya1B5CFQFI3Ri0t9iX5GWXsQSSEdzc 3S9eVQN17rIh+yfLVxM2JxAGW+YNi5Io6ppy/ErhOFpPu7G8u7bzuEOLFVNeuvzfE52+ P1OsY/Rb47MaIFPySrfyyVYaoHOs3N5NXedH0m8qkXBKPrSg0m9cr9ncNjsmyLDO7OfA G2DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414035; x=1703018835; 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=XMXRsZ7PnaagoSZjHTHBbcv9aekrkY1R/86PgELFK84=; b=RJqp8Ox8JRRApZp9wPhzE0K54hDaRNvEiqFzWl8L7pqtOU4tywVNR14YV965uGQZ7I 48GukclKCvcw/1554jI1GIuOPKHH8fjmnkcOVl+rz3nTRfgkLM9jb+zWKQAUBys/Fmxf GgrUNXu/kjdXWsvlojEnu0Tevt1YSfHcmMedhgOzKNBWvTC4VLzfrwDl5RGLj4QQYOOX a2mMOKX3Qx4dcstGckACQohpSa9YH1U/hFRm9kKwZiFtseu3dvvpCNMpzldVyQp4sr16 VCDXc52OVUco06yznPkRY0EVJ3Om7mTrpYRqIPPNLZQyClh9oZ4W9xJ9o3bw4L5Axbn1 F52w== X-Gm-Message-State: AOJu0YwKb1TFwDB65IQKRmkoLmEQwJknrIU4htcgVGmLw191cEBE/7yQ 77kbisWA0Ov18/QQ5wvDpg/pcEpT4A== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:b712:b0:1d3:46f4:7f1e with SMTP id d18-20020a170902b71200b001d346f47f1emr4985pls.1.1702414035248; Tue, 12 Dec 2023 12:47:15 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:25 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-11-sagis@google.com> Subject: [RFC PATCH v5 10/29] KVM: selftests: TDX: Adding test case for TDX port IO From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-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_BLOCKED,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:52 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110538915602380 X-GMAIL-MSGID: 1785110538915602380 From: Erdem Aktas Verifies TDVMCALL READ and WRITE operations. Signed-off-by: Erdem Aktas Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Reviewed-by: Binbin Wu --- .../kvm/include/x86_64/tdx/test_util.h | 34 ++++++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 82 +++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h index 6d69921136bd..95a5d5be7f0b 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h @@ -9,6 +9,40 @@ #define TDX_TEST_SUCCESS_PORT 0x30 #define TDX_TEST_SUCCESS_SIZE 4 +/** + * Assert that some IO operation involving tdg_vp_vmcall_instruction_io() was + * called in the guest. + */ +#define TDX_TEST_ASSERT_IO(VCPU, PORT, SIZE, DIR) \ + do { \ + TEST_ASSERT((VCPU)->run->exit_reason == KVM_EXIT_IO, \ + "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason)); \ + \ + TEST_ASSERT(((VCPU)->run->exit_reason == KVM_EXIT_IO) && \ + ((VCPU)->run->io.port == (PORT)) && \ + ((VCPU)->run->io.size == (SIZE)) && \ + ((VCPU)->run->io.direction == (DIR)), \ + "Got unexpected IO exit values: %u (%s) %d %d %d\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason), \ + (VCPU)->run->io.port, (VCPU)->run->io.size, \ + (VCPU)->run->io.direction); \ + } while (0) + +/** + * Check and report if there was some failure in the guest, either an exception + * like a triple fault, or if a tdx_test_fatal() was hit. + */ +#define TDX_TEST_CHECK_GUEST_FAILURE(VCPU) \ + do { \ + if ((VCPU)->run->exit_reason == KVM_EXIT_SYSTEM_EVENT) \ + TEST_FAIL("Guest reported error. error code: %lld (0x%llx)\n", \ + (VCPU)->run->system_event.data[1], \ + (VCPU)->run->system_event.data[1]); \ + } while (0) + /** * Assert that tdx_test_success() was called in the guest. */ diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 8638c7bbedaa..75467c407ca7 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -2,6 +2,7 @@ #include #include "kvm_util_base.h" +#include "tdx/tdcall.h" #include "tdx/tdx.h" #include "tdx/tdx_util.h" #include "tdx/test_util.h" @@ -74,6 +75,86 @@ void verify_report_fatal_error(void) printf("\t ... PASSED\n"); } +#define TDX_IOEXIT_TEST_PORT 0x50 + +/* + * Verifies IO functionality by writing a |value| to a predefined port. + * Verifies that the read value is |value| + 1 from the same port. + * If all the tests are passed then write a value to port TDX_TEST_PORT + */ +void guest_ioexit(void) +{ + uint64_t data_out, data_in, delta; + uint64_t ret; + + data_out = 0xAB; + ret = tdg_vp_vmcall_instruction_io(TDX_IOEXIT_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &data_out); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_instruction_io(TDX_IOEXIT_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_READ, + &data_in); + if (ret) + tdx_test_fatal(ret); + + delta = data_in - data_out; + if (delta != 1) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +void verify_td_ioexit(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + uint32_t port_data; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_ioexit); + td_finalize(vm); + + printf("Verifying TD IO Exit:\n"); + + /* Wait for guest to do a IO write */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IOEXIT_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + port_data = *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + printf("\t ... IO WRITE: OK\n"); + + /* + * Wait for the guest to do a IO read. Provide the previous written data + * + 1 back to the guest + */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IOEXIT_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_READ); + *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = port_data + 1; + + printf("\t ... IO READ: OK\n"); + + /* + * Wait for the guest to complete execution successfully. The read + * value is checked within the guest. + */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + printf("\t ... IO verify read/write values: OK\n"); + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -85,6 +166,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_td_lifecycle); run_in_new_process(&verify_report_fatal_error); + run_in_new_process(&verify_td_ioexit); return 0; } From patchwork Tue Dec 12 20:46:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177565 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993543vqy; Tue, 12 Dec 2023 12:48:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGy8qB/cv8AZyusFBownm0EmoozJXGICYPwBP2Q6XAekjsTf4fRyB6ga4+QWedyFfQzCRQp X-Received: by 2002:a17:902:b195:b0:1d0:75d8:6ce2 with SMTP id s21-20020a170902b19500b001d075d86ce2mr2773902plr.82.1702414094759; Tue, 12 Dec 2023 12:48:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414094; cv=none; d=google.com; s=arc-20160816; b=hU7cXzTeqghIuFHjs+L/LLduS3nWUrJ6uG+lxfPitV9mdMhlsBGH1H0R96dV4IE+A9 tZjrEAI7amJq986nXlJ8beQT1obZXn27iCxL4KmoCpDUxnbR1Ac3CK+uucvTTgB3cMPy jocaeZ6Nx8Rd2bCzXIs/BZsW6XImdQAaX+lbtONcF7l4eYiBumaCOPNOM2zWvFxQcYUe 95eadjn6MmoKcjnXa5ileIngpfn7kMNzocQAB+jfAT1YMrsAvf5bBFxOI0uFDSRvawPR xB9tTRrsm2fMgdYI7x7REHB1VEc0U2ljQ58KVCyY/dftFvmrXF2NpmY5cwx7K6fkIqfs ozqQ== 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=yeLWUyiP/wO8xtMoo+vlGGvYyGcJ6kthyZbia5KZ2g4=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=mbt8H7CxXgcK1Us2poxNPHXcCRmA1CN4FLqlCqY9qeGrK8EqpRMOdO0GA3ju+qubm+ OabzFro1HxKpk2iGl5Ps0x3TbHP5aM14foVjekuH3S/y6TmyXEYKNok3MyEkYMuYNBp+ 5gWd7hRt4IGjRprdaPrf9ELhSkTXpXoG7Hh16JWCUqkb1PsfKacS5yifco5tL7xzcizu AUm8bnrG6CsgccOontVz8sulKy6oQ6kxzKTbS9JABZ9iFR95x5Oc9GEWs4acH2/ZVJK0 W0RS5gOQb7W6apfOfMuiclXgnZFJiRBrW1E5ejamTQeN6j/lbdovJPJms4YFk2iJKkSJ eKxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JbTR0JbF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id s2-20020a170902ea0200b001d0a0ee28e0si8557335plg.288.2023.12.12.12.48.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JbTR0JbF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 886DA804C23C; Tue, 12 Dec 2023 12:48:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377367AbjLLUrn (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377371AbjLLUr0 (ORCPT ); Tue, 12 Dec 2023 15:47:26 -0500 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 E5EC0191 for ; Tue, 12 Dec 2023 12:47:19 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5e20c9c4080so11326987b3.3 for ; Tue, 12 Dec 2023 12:47:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414039; x=1703018839; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yeLWUyiP/wO8xtMoo+vlGGvYyGcJ6kthyZbia5KZ2g4=; b=JbTR0JbFkl/zb1WihjXaeUgwhgKH+GZvmaaCf7LpiKfZZnRgqPc/fz2PPXGExlBbZl YRQ6TknZziqm8L78LntvCKSduNrIdp1FIYHzqnjRW7gzgfYzYRoIIsaUouGjrFCgI+AC vue9fQw6XVEHPnztfQXniHrCrsF8MYzL9fbB0ZMMS3Wn7Sf5K9VGP6644CnAGQJU1JGw hKqwa6kF9HJ5HVBt2e53xZkHyMZEBcWXNx6Kh6iy2c3ADEE+xMtWndxVDntmNDUwWGCJ nYWtRlK4+oigdl/bPTBpt+9rrZjtXa+DOPwbZE+rUBezgjJrmoz+BmOq/HP/FJgsdP54 3KZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414039; x=1703018839; 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=yeLWUyiP/wO8xtMoo+vlGGvYyGcJ6kthyZbia5KZ2g4=; b=Ql53VCUXhZam8ye8EKquZVAA/yIdkML1YmIM7MZAN/HMjk/GBF0nn+5M02m9EIKzak LuMeIDzWZayP8Muu+v3fY9QxwSdBIWAoO62hnqr+3dSz4eZ/2XroeXmsz3xN9yobxrQG Tslxx8wLJccKCS3RPfxXdw8XCuyVxeOLXxVHQP2pM80LhL2/Y/INl2SSxIWx2COV+Vdw nM6qZtTy5FdwqiCLI8sZrgn2sz9CLFFLdiM3fweZuA0vs9pTNOB7n28KO4jDaCHkdGlJ shTE8dAzEh9amM6dh+sK2gFmRU5m+4Cwo2mhbmEhWMWZzN2Ti0X1c2c3nhmIsb6YTvYJ qp1A== X-Gm-Message-State: AOJu0YxOAzngZf+0EFoMCWfY3ENOGnMVoCUPBFMSOhVYZbEcwppEyws7 tiF1GaLOzZxDSmM9NgkZoPTUf37YiA== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:690c:c09:b0:5d3:3aa4:6f3a with SMTP id cl9-20020a05690c0c0900b005d33aa46f3amr68898ywb.3.1702414037003; Tue, 12 Dec 2023 12:47:17 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:26 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-12-sagis@google.com> Subject: [RFC PATCH v5 11/29] KVM: selftests: TDX: Add basic TDX CPUID test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:05 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110561490340237 X-GMAIL-MSGID: 1785110561490340237 The test reads CPUID values from inside a TD VM and compare them to expected values. The test targets CPUID values which are virtualized as "As Configured", "As Configured (if Native)", "Calculated", "Fixed" and "Native" according to the TDX spec. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../kvm/include/x86_64/tdx/test_util.h | 9 ++ .../selftests/kvm/lib/x86_64/tdx/test_util.c | 11 ++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 106 ++++++++++++++++++ 3 files changed, 126 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h index 95a5d5be7f0b..af0ddbfe8d71 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h @@ -9,6 +9,9 @@ #define TDX_TEST_SUCCESS_PORT 0x30 #define TDX_TEST_SUCCESS_SIZE 4 +#define TDX_TEST_REPORT_PORT 0x31 +#define TDX_TEST_REPORT_SIZE 4 + /** * Assert that some IO operation involving tdg_vp_vmcall_instruction_io() was * called in the guest. @@ -102,4 +105,10 @@ void tdx_test_fatal(uint64_t error_code); */ void tdx_test_fatal_with_data(uint64_t error_code, uint64_t data_gpa); +/** + * Report a 32 bit value from the guest to user space using TDG.VP.VMCALL + * call. Data is reported on port TDX_TEST_REPORT_PORT. + */ +uint64_t tdx_test_report_to_user_space(uint32_t data); + #endif // SELFTEST_TDX_TEST_UTIL_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c index 7f3cd8089cea..55c5a1e634df 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c @@ -42,3 +42,14 @@ void tdx_test_fatal(uint64_t error_code) { tdx_test_fatal_with_data(error_code, 0); } + +uint64_t tdx_test_report_to_user_space(uint32_t data) +{ + /* Upcast data to match tdg_vp_vmcall_instruction_io signature */ + uint64_t data_64 = data; + + return tdg_vp_vmcall_instruction_io(TDX_TEST_REPORT_PORT, + TDX_TEST_REPORT_SIZE, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &data_64); +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 75467c407ca7..1b30e6f5a569 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -2,6 +2,7 @@ #include #include "kvm_util_base.h" +#include "processor.h" #include "tdx/tdcall.h" #include "tdx/tdx.h" #include "tdx/tdx_util.h" @@ -155,6 +156,110 @@ void verify_td_ioexit(void) printf("\t ... PASSED\n"); } +/* + * Verifies CPUID functionality by reading CPUID values in guest. The guest + * will then send the values to userspace using an IO write to be checked + * against the expected values. + */ +void guest_code_cpuid(void) +{ + uint64_t err; + uint32_t ebx, ecx; + + /* Read CPUID leaf 0x1 */ + asm volatile ( + "cpuid" + : "=b" (ebx), "=c" (ecx) + : "a" (0x1) + : "edx"); + + err = tdx_test_report_to_user_space(ebx); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_to_user_space(ecx); + if (err) + tdx_test_fatal(err); + + tdx_test_success(); +} + +void verify_td_cpuid(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + uint32_t ebx, ecx; + const struct kvm_cpuid_entry2 *cpuid_entry; + uint32_t guest_clflush_line_size; + uint32_t guest_max_addressable_ids, host_max_addressable_ids; + uint32_t guest_sse3_enabled; + uint32_t guest_fma_enabled; + uint32_t guest_initial_apic_id; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_code_cpuid); + td_finalize(vm); + + printf("Verifying TD CPUID:\n"); + + /* Wait for guest to report ebx value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + ebx = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + /* Wait for guest to report either ecx value or error */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + ecx = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + /* Wait for guest to complete execution */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + /* Verify the CPUID values we got from the guest. */ + printf("\t ... Verifying CPUID values from guest\n"); + + /* Get KVM CPUIDs for reference */ + cpuid_entry = get_cpuid_entry(kvm_get_supported_cpuid(), 1, 0); + TEST_ASSERT(cpuid_entry, "CPUID entry missing\n"); + + host_max_addressable_ids = (cpuid_entry->ebx >> 16) & 0xFF; + + guest_sse3_enabled = ecx & 0x1; // Native + guest_clflush_line_size = (ebx >> 8) & 0xFF; // Fixed + guest_max_addressable_ids = (ebx >> 16) & 0xFF; // As Configured + guest_fma_enabled = (ecx >> 12) & 0x1; // As Configured (if Native) + guest_initial_apic_id = (ebx >> 24) & 0xFF; // Calculated + + TEST_ASSERT_EQ(guest_sse3_enabled, 1); + TEST_ASSERT_EQ(guest_clflush_line_size, 8); + TEST_ASSERT_EQ(guest_max_addressable_ids, host_max_addressable_ids); + + /* TODO: This only tests the native value. To properly test + * "As Configured (if Native)" we need to override this value + * in the TD params + */ + TEST_ASSERT_EQ(guest_fma_enabled, 1); + + /* TODO: guest_initial_apic_id is calculated based on the number of + * VCPUs in the TD. From the spec: "Virtual CPU index, starting from 0 + * and allocated sequentially on each successful TDH.VP.INIT" + * To test non-trivial values we either need a TD with multiple VCPUs + * or to pick a different calculated value. + */ + TEST_ASSERT_EQ(guest_initial_apic_id, 0); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -167,6 +272,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_td_lifecycle); run_in_new_process(&verify_report_fatal_error); run_in_new_process(&verify_td_ioexit); + run_in_new_process(&verify_td_cpuid); return 0; } From patchwork Tue Dec 12 20:46:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177564 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993522vqy; Tue, 12 Dec 2023 12:48:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHAdJkOZwuQIKE4fEcHkMOmprPQOZUjwzwVM1LErmzZFcra1tDyGWs5ZgFBsDqNFgD7u0UX X-Received: by 2002:a17:90a:7443:b0:28a:c420:ca0f with SMTP id o3-20020a17090a744300b0028ac420ca0fmr896469pjk.77.1702414091252; Tue, 12 Dec 2023 12:48:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414091; cv=none; d=google.com; s=arc-20160816; b=olTX9mHLi0wk9yFQ4F6UXHWLduTa62W3y/jy0qx6EV6XSl7jzqiLHV1WoYcJmCbqMI zptSAw8c+fcwabpFplW5uC3iFggY27BLPUy/YdL8m6OzyWF8no2+yORmyYqZTzGsTxcJ JKX2ejXiOni+m3aJR5aj7UOh79WTs8aY650SGlzjrYrc0zhq4SkpWkhLbVLgL85Et1u0 inoaATGCioZmNTPlDeXnSFX7pK4QKEBLjUsU5ANgzhKR7b1269F64Ple+GAnUYxyfQsp 4k4hNVVkZc4AT2osFef5bnL3niSNw1KNO9UEbxF/rgkeoDNBxaNiu40M//Yu9truqamW QK0g== 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=QmMEuN8SsdugGRuBIQYyKlPPcc9Vaoj0O+wuwT+Ut3w=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=YRyGX9iilOXiP/wuPWIERUEd3ZNCLpIkKlUY98ZJqe9vvvfSAriVHHOjUDJVYddSMl sibv4NvYgtxhLfuZdKemIb5IaFC3pg68txU+r0l8DAlz1w13yS4cRrx0HeNfLsY5lxpQ sN0eHl6jWJNR3kWG0NuXZ0w/GFYLQ/447UJiwMG+/8lNyC/z95S9P5jEjFas2jAJ33Ax GEUs6hrWbogOFEn9s29Nl7OKdtmK/ViBnsqNtAxM8Cvzy3ZigH7KXZZP1AzClW6lO+h6 uVDzHtTfqGbvRzvhwX9spxJaHcp/j0yn4XE6VS3U1yk63Shqhayhr9xrkhrRCDCxFE4I NfgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bY9ZQrXE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id a1-20020a17090a8c0100b002792c288cc3si8393654pjo.169.2023.12.12.12.48.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bY9ZQrXE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id DA0628045955; Tue, 12 Dec 2023 12:48:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235243AbjLLUrz (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377390AbjLLUr1 (ORCPT ); Tue, 12 Dec 2023 15:47:27 -0500 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 BEF46199 for ; Tue, 12 Dec 2023 12:47:21 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d053953954so30442525ad.2 for ; Tue, 12 Dec 2023 12:47:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414041; x=1703018841; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QmMEuN8SsdugGRuBIQYyKlPPcc9Vaoj0O+wuwT+Ut3w=; b=bY9ZQrXEk9uUqUJX/cR5T0IlFdmHntxqRwEIJsuWPaqXeZINY+2c+mybbTyhCpl7aI fP5bdpADnFi/oZsKLR87MfkkS3hffU2rkj3dk8KRIUQ2Ek0vnvhUBTK0/BV0G0lkpxNu zspbebX2VkZdQqSY3UP+dJnEH9kBPyx983nuDz7YEe4rg8wPpT/GnLBzcmWQHODqW4ZC yb2l/0OnmkGNldfNn7wmyz0Q2VmgSV3+iz39bhexFvr0986y8/CRKBYTnPpAtc6FPyVN w5Y1RkBjaajP68j6cUSCkLW2boVOoFAnTu4z/QJBxZyOgYtzkFhQhWyZ2s/1/qimJKof dZ4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414041; x=1703018841; 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=QmMEuN8SsdugGRuBIQYyKlPPcc9Vaoj0O+wuwT+Ut3w=; b=XtJEgn6GboP1VATNkdarJpQcZ0UXco/f+WTLi5RRD/TU0b/LqiRguiU5VJnGGlN5Qd E3sJRbaGI4BFExEmcuzlWzYxI237Gnn/7h/aTHwZfU8qLnMuY10Ujuk3EpoJDuIqgqe5 Z0twOXbaCsA6+yECSyjlnDOieu4vFWFbWMofOVdWU/utV+PEQVLpLbvvC2ca3zEJ8voq lHm5hyU888Caiuf1om46w7FwWYlAafkkjmdxn75O2C7KW3mswDVExUt5B+TmS9WWtgzM g8S4CnTmPuK6ozUsm+lDRe3C6+6g651H5cIbfzoXCqIIxH8zhQArtw0ZNW2p8sfo6XQb ui8g== X-Gm-Message-State: AOJu0YxD7d/dbXQh4I+ZIMOgQHPQBgU64oB2NpvSqcwSJJIa/x47IMDc cZI7Jqb5B2WBmMJpMvUewXOMvY4qeQ== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:903:41cd:b0:1d2:ebcd:2341 with SMTP id u13-20020a17090341cd00b001d2ebcd2341mr54766ple.9.1702414040823; Tue, 12 Dec 2023 12:47:20 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:27 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-13-sagis@google.com> Subject: [RFC PATCH v5 12/29] KVM: selftests: TDX: Add basic get_td_vmcall_info test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-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_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:10 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110558104188137 X-GMAIL-MSGID: 1785110558104188137 The test calls get_td_vmcall_info from the guest and verifies the expected returned values. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Reviewed-by: Binbin Wu --- .../selftests/kvm/include/x86_64/tdx/tdx.h | 3 + .../kvm/include/x86_64/tdx/test_util.h | 27 +++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 23 ++++++ .../selftests/kvm/lib/x86_64/tdx/test_util.c | 46 +++++++++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 80 +++++++++++++++++++ 5 files changed, 179 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index 1340c1070002..63788012bf94 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -5,6 +5,7 @@ #include #include "kvm_util_base.h" +#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 #define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 #define TDG_VP_VMCALL_INSTRUCTION_IO 30 @@ -12,5 +13,7 @@ void handle_userspace_tdg_vp_vmcall_exit(struct kvm_vcpu *vcpu); uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size, uint64_t write, uint64_t *data); void tdg_vp_vmcall_report_fatal_error(uint64_t error_code, uint64_t data_gpa); +uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12, + uint64_t *r13, uint64_t *r14); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h index af0ddbfe8d71..8a9b6a1bec3e 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h @@ -4,6 +4,7 @@ #include +#include "kvm_util_base.h" #include "tdcall.h" #define TDX_TEST_SUCCESS_PORT 0x30 @@ -111,4 +112,30 @@ void tdx_test_fatal_with_data(uint64_t error_code, uint64_t data_gpa); */ uint64_t tdx_test_report_to_user_space(uint32_t data); +/** + * Report a 64 bit value from the guest to user space using TDG.VP.VMCALL + * call. + * + * Data is sent to host in 2 calls. LSB is sent (and needs to be read) first. + */ +uint64_t tdx_test_send_64bit(uint64_t port, uint64_t data); + +/** + * Report a 64 bit value from the guest to user space using TDG.VP.VMCALL + * call. Data is reported on port TDX_TEST_REPORT_PORT. + */ +uint64_t tdx_test_report_64bit_to_user_space(uint64_t data); + +/** + * Read a 64 bit value from the guest in user space, sent using + * tdx_test_send_64bit(). + */ +uint64_t tdx_test_read_64bit(struct kvm_vcpu *vcpu, uint64_t port); + +/** + * Read a 64 bit value from the guest in user space, sent using + * tdx_test_report_64bit_to_user_space. + */ +uint64_t tdx_test_read_64bit_report_from_guest(struct kvm_vcpu *vcpu); + #endif // SELFTEST_TDX_TEST_UTIL_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index b854c3aa34ff..e5a9e13c62e2 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -64,3 +64,26 @@ void tdg_vp_vmcall_report_fatal_error(uint64_t error_code, uint64_t data_gpa) __tdx_hypercall(&args, 0); } + +uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12, + uint64_t *r13, uint64_t *r14) +{ + uint64_t ret; + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_GET_TD_VM_CALL_INFO, + .r12 = 0, + }; + + ret = __tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT); + + if (r11) + *r11 = args.r11; + if (r12) + *r12 = args.r12; + if (r13) + *r13 = args.r13; + if (r14) + *r14 = args.r14; + + return ret; +} diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c index 55c5a1e634df..3ae651cd5fac 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c @@ -7,6 +7,7 @@ #include #include "kvm_util_base.h" +#include "tdx/tdcall.h" #include "tdx/tdx.h" #include "tdx/test_util.h" @@ -53,3 +54,48 @@ uint64_t tdx_test_report_to_user_space(uint32_t data) TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, &data_64); } + +uint64_t tdx_test_send_64bit(uint64_t port, uint64_t data) +{ + uint64_t err; + uint64_t data_lo = data & 0xFFFFFFFF; + uint64_t data_hi = (data >> 32) & 0xFFFFFFFF; + + err = tdg_vp_vmcall_instruction_io(port, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &data_lo); + if (err) + return err; + + return tdg_vp_vmcall_instruction_io(port, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &data_hi); +} + +uint64_t tdx_test_report_64bit_to_user_space(uint64_t data) +{ + return tdx_test_send_64bit(TDX_TEST_REPORT_PORT, data); +} + +uint64_t tdx_test_read_64bit(struct kvm_vcpu *vcpu, uint64_t port) +{ + uint32_t lo, hi; + uint64_t res; + + TDX_TEST_ASSERT_IO(vcpu, port, 4, TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + lo = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + vcpu_run(vcpu); + + TDX_TEST_ASSERT_IO(vcpu, port, 4, TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + hi = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + res = hi; + res = (res << 32) | lo; + return res; +} + +uint64_t tdx_test_read_64bit_report_from_guest(struct kvm_vcpu *vcpu) +{ + return tdx_test_read_64bit(vcpu, TDX_TEST_REPORT_PORT); +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 1b30e6f5a569..569c8fb0a59f 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -260,6 +260,85 @@ void verify_td_cpuid(void) printf("\t ... PASSED\n"); } +/* + * Verifies get_td_vmcall_info functionality. + */ +void guest_code_get_td_vmcall_info(void) +{ + uint64_t err; + uint64_t r11, r12, r13, r14; + + err = tdg_vp_vmcall_get_td_vmcall_info(&r11, &r12, &r13, &r14); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_64bit_to_user_space(r11); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_64bit_to_user_space(r12); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_64bit_to_user_space(r13); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_64bit_to_user_space(r14); + if (err) + tdx_test_fatal(err); + + tdx_test_success(); +} + +void verify_get_td_vmcall_info(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + uint64_t r11, r12, r13, r14; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_code_get_td_vmcall_info); + td_finalize(vm); + + printf("Verifying TD get vmcall info:\n"); + + /* Wait for guest to report r11 value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + r11 = tdx_test_read_64bit_report_from_guest(vcpu); + + /* Wait for guest to report r12 value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + r12 = tdx_test_read_64bit_report_from_guest(vcpu); + + /* Wait for guest to report r13 value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + r13 = tdx_test_read_64bit_report_from_guest(vcpu); + + /* Wait for guest to report r14 value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + r14 = tdx_test_read_64bit_report_from_guest(vcpu); + + TEST_ASSERT_EQ(r11, 0); + TEST_ASSERT_EQ(r12, 0); + TEST_ASSERT_EQ(r13, 0); + TEST_ASSERT_EQ(r14, 0); + + /* Wait for guest to complete execution */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -273,6 +352,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_report_fatal_error); run_in_new_process(&verify_td_ioexit); run_in_new_process(&verify_td_cpuid); + run_in_new_process(&verify_get_td_vmcall_info); return 0; } From patchwork Tue Dec 12 20:46:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177566 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993597vqy; Tue, 12 Dec 2023 12:48:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IFvzEjdvzgIZmBNur1Ireqe2JfOikBEilad6GB5FNJGGYMbvSua/ds9BBQX9I1Fb96Y9YhN X-Received: by 2002:a05:6a21:78a8:b0:190:1258:3da3 with SMTP id bf40-20020a056a2178a800b0019012583da3mr10156452pzc.4.1702414103823; Tue, 12 Dec 2023 12:48:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414103; cv=none; d=google.com; s=arc-20160816; b=SqggnlHxCyjwSjUMMp4/53DgZeajzIibqgOGG9WbJ0+pR+54GzM0oPKr7oeykvkqie mE1J7JiB+FCNhCimj+dlyJtGZ3Y8/i1/aa/inqdmjzeW8is95J/KAKEYwxVSw5r+m7tB DYcZFDHSWvVREeVCt+FbBuNyHlaykM53+OjS7OooTN8ZZGXyaAgSW3v9TI/zwtWjTAPI TgekLWJ2EBeQxI0vMFKuTbXb+n4WO3bE9Eo9d1f2NDNW+lgGOqJHT7thyu5bDl9EMQ+2 k77spy+oWNAPj9wfYNVBA8/UqehO8RUauOo5oHo27i1x/0/updM6NqZIQvOPX/8InNbw yAOA== 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=IJkjWxWHyZzDnEqdZ8iaPH05VJWonQ7XdgN0T6HdUW8=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=ZxklAJRH3jt2uY7LX0kvLGuOmzFZyUcCirR5AvlEuCvxzCo9tImeQBLQSFHmci/vAO bTKRB9DFs8f8NjG7627oVduEbawYmRVTgV/zbdrXo27APU9zAfQoRsbA94kSilt2zY68 w/nYnAlJIj/VLKLXoLDocwEqyXgq3sG9JrCEhCSyFgeWDwcy2rRz1lzmu9RuXDjQRa1c 4FiV7fn7np8aSScMxhwLdR6NLl1SmpucQ2m9G0ztQyLWYR7RjEZCV74v8XsNwceQ3kP8 AG04dByLI4gLBqBrZVBs4hKdTuhh6dFLdrsgFbRkOmXNs6uBWNUj+DXSSmW4EmQPXHKC GOFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="psHW/NdD"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id h21-20020a63e155000000b005c69765acddsi8277591pgk.87.2023.12.12.12.48.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="psHW/NdD"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id CD62380FE965; Tue, 12 Dec 2023 12:48:16 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233319AbjLLUr7 (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377402AbjLLUr2 (ORCPT ); Tue, 12 Dec 2023 15:47:28 -0500 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 7DF521AE for ; Tue, 12 Dec 2023 12:47:23 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1d053953954so30442665ad.2 for ; Tue, 12 Dec 2023 12:47:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414043; x=1703018843; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=IJkjWxWHyZzDnEqdZ8iaPH05VJWonQ7XdgN0T6HdUW8=; b=psHW/NdDK8z8wrvk/9vGBuBjFq3hY6pzJj1w3OLzJ8Eqw3CBWyEI8/Qawy6LaPTuuZ T6PkJ6YLR2n7YrjFxjtVNJenrLXcGCOVkvwCc2mDnEGE/G3N5osbGZLYiLI/DNNHcXXm RgxSMkkBuq1v+1ZhEAG1V0qLwh6DOKIc11WAdnhDSyTefnXPRJncuvQq6k3Gqxj+kjiq tVcAJHcmONzksaCnQclZLiemJq5tYyLMWTMGoyBaAy30KTFrhVe23isQA96HwbwPet5a RkKBjmXkFJjlngiT3RnncGPR0qWfJZdNhb9YF1BbirZOsSKO9AWHfIxP6lhdRNCy8Gqi ntMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414043; x=1703018843; 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=IJkjWxWHyZzDnEqdZ8iaPH05VJWonQ7XdgN0T6HdUW8=; b=Eg8UOzIoZyHoX3VlRlr4b6fEp2KrF9I2C9clpZegXutBFpy1FhBHlCesEdowPpq4A7 tD42EqbKwqNaZvlfvqsdBqT5SC/CPxOcD8u0Uyaq9BYuL65jvT+UfmFreyyq7YWGHDPE qdz8RyA0KYFOFxvsp2e1TyKqXZYmYW4j9ZoioyoNusNMhTrBqWqM7/ZYB71wQZIHWXEW 1xy9wRfMdTdQmC0MLq4meHQb5q4xXbxOUEjekFYJOc8ra/RhPgkW2TbnCbF/REVnYpfo MQQnQzpIayXVnUvflVekbaGzK6FqnA3T2ITjkB1zgjTz1TNH6pPuYxZy6//FrSCwu+N3 dZPg== X-Gm-Message-State: AOJu0YxkdOrQinSE5gC+XdaV5uQQWtEg3NoZRwtwyrrhNqH9nYKQCahC 6SONG1UOpayBRcFYw8PyX2ZrCdnKSw== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:ec85:b0:1d0:537e:852d with SMTP id x5-20020a170902ec8500b001d0537e852dmr58319plg.13.1702414042901; Tue, 12 Dec 2023 12:47:22 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:28 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-14-sagis@google.com> Subject: [RFC PATCH v5 13/29] KVM: selftests: TDX: Add TDX IO writes test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:16 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110571360077650 X-GMAIL-MSGID: 1785110571360077650 The test verifies IO writes of various sizes from the guest to the host. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Reviewed-by: Binbin Wu --- .../selftests/kvm/include/x86_64/tdx/tdcall.h | 3 + .../selftests/kvm/x86_64/tdx_vm_tests.c | 91 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h index 78001bfec9c8..b5e94b7c48fa 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h @@ -10,6 +10,9 @@ #define TDG_VP_VMCALL_INSTRUCTION_IO_READ 0 #define TDG_VP_VMCALL_INSTRUCTION_IO_WRITE 1 +#define TDG_VP_VMCALL_SUCCESS 0x0000000000000000 +#define TDG_VP_VMCALL_INVALID_OPERAND 0x8000000000000000 + #define TDX_HCALL_HAS_OUTPUT BIT(0) #define TDX_HYPERCALL_STANDARD 0 diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 569c8fb0a59f..a2b3e1aef151 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -339,6 +339,96 @@ void verify_get_td_vmcall_info(void) printf("\t ... PASSED\n"); } +#define TDX_IO_WRITES_TEST_PORT 0x51 + +/* + * Verifies IO functionality by writing values of different sizes + * to the host. + */ +void guest_io_writes(void) +{ + uint64_t byte_1 = 0xAB; + uint64_t byte_2 = 0xABCD; + uint64_t byte_4 = 0xFFABCDEF; + uint64_t ret; + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_WRITES_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &byte_1); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_WRITES_TEST_PORT, 2, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &byte_2); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_WRITES_TEST_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &byte_4); + if (ret) + tdx_test_fatal(ret); + + // Write an invalid number of bytes. + ret = tdg_vp_vmcall_instruction_io(TDX_IO_WRITES_TEST_PORT, 5, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &byte_4); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +void verify_guest_writes(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + uint8_t byte_1; + uint16_t byte_2; + uint32_t byte_4; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_io_writes); + td_finalize(vm); + + printf("Verifying guest writes:\n"); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_WRITES_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + byte_1 = *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_WRITES_TEST_PORT, 2, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + byte_2 = *(uint16_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_WRITES_TEST_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + byte_4 = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + TEST_ASSERT_EQ(byte_1, 0xAB); + TEST_ASSERT_EQ(byte_2, 0xABCD); + TEST_ASSERT_EQ(byte_4, 0xFFABCDEF); + + td_vcpu_run(vcpu); + TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + TEST_ASSERT_EQ(vcpu->run->system_event.data[1], TDG_VP_VMCALL_INVALID_OPERAND); + + td_vcpu_run(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -353,6 +443,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_td_ioexit); run_in_new_process(&verify_td_cpuid); run_in_new_process(&verify_get_td_vmcall_info); + run_in_new_process(&verify_guest_writes); return 0; } From patchwork Tue Dec 12 20:46:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177567 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993610vqy; Tue, 12 Dec 2023 12:48:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IGEdETAWytJrcPbCaQVxnjZeR/i/e/scZ+7wfyoFcGX6+KmIdr3JCg+2PUUmJBIZ61+P0K7 X-Received: by 2002:a05:6a00:2356:b0:6ce:2732:283 with SMTP id j22-20020a056a00235600b006ce27320283mr4527232pfj.50.1702414105541; Tue, 12 Dec 2023 12:48:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414105; cv=none; d=google.com; s=arc-20160816; b=s+K/0K1QE7vlnE13s4zxxxYjAYKNBh4VWRgRCzrvSXpCkFudKmFVKv2r6gA4fr7DH6 skQEqXZfd7G0//ULcwaIFhCRjM36phm0QRwkhV02uzHBzxFbumlzkkB5zf5jNtcMf//C K0kGDDy1ubedu0/0sphyJG/0NMyb5GV29n2oYTm2AubUnHpW6quxvLXZbHQhGn5silCM wfEsXBdcU6esMJE/IvvNGZAvuLsIfRzPA6rI+YmT4tvIn7vvcX+THg7DQxwOXGCy6Gfb DzUBdZ1J1feHLA4k6ANQ4gg6c9jgSSDwwPWyZqGMi2qi2mIPPhYy1Js4BvTL4Ni+H09q jgyw== 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=pOWQKACugG9g7cz1/AmBUQwWqXmyF/X6aVaYz3Y0fys=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=VxjAqLbk8hTEEIZNn41SXPXIHDlNl251YDfwkggWTs98GhYV6suXiWzXEUKg6eBZtc 4qWLsxfb17e6pfp89NjECTnP45W64tZdKCXM0zmTMDG9ZiW/Dexhf+S8uS+qzBmr4Z/g q646dqJ2IStHOTDPrLhdp3+AkXmzd1q4g0myL6/z7DNKwHgAo8Zg5LAHKk7aiaJynBRv Rk9zNy33wk4Apb9LYIkza0+dRI+sjQzz+4QvNV4FDzDTvg88jRH3TbgfTaOHbIdxP5C8 aEM4KbGhMAtzChxVyn6NOnaS+8HecjXnoVs3rlJ4oAWOxq4uY/+QUj+vZn4mrVK1w8xO l9HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xEsg+Ocr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id kr13-20020a056a004b4d00b006d09731981asi3276165pfb.45.2023.12.12.12.48.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xEsg+Ocr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 02A12804C23F; Tue, 12 Dec 2023 12:48:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377350AbjLLUsC (ORCPT + 99 others); Tue, 12 Dec 2023 15:48:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235132AbjLLUr3 (ORCPT ); Tue, 12 Dec 2023 15:47:29 -0500 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 084EC10F for ; Tue, 12 Dec 2023 12:47:24 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2869cdac540so3534401a91.0 for ; Tue, 12 Dec 2023 12:47:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414044; x=1703018844; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pOWQKACugG9g7cz1/AmBUQwWqXmyF/X6aVaYz3Y0fys=; b=xEsg+OcraIn1wXFiLbdcLlsvoQ7XrcnNSRujN/AX1yG2NSZwu0+sr2Ac3X6shM3EAg bl+rVA1HYCsXgLW2NxMMZ55Zo44UY/WcF7kMR9cGIL7NDf7/4dwfR1+ZH/m7WrVPyUaS voknZB+KcKzc21caD9XiY6nTY/iZFNWp/mySUHkSPZpvYOP4tPCPTyA4fY4XELLwjj4g /tC6TdM05yTeNd/Jujgq0g/v9lmbpd+/l8yHmyJxLcUY9TZ4JZo0OzbU7EmQM0iky9jh uSzsyYQxbQVazo21s5rstc1zQQcijjLX9HlP2kz5FRpkZa2iCzqT8N84TmjYCNJn5SGV qGCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414044; x=1703018844; 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=pOWQKACugG9g7cz1/AmBUQwWqXmyF/X6aVaYz3Y0fys=; b=v3iU8RrYz1Ktk5EoazB3hbdpcKKXSlzQAmhNajGWNuAq96wM7GAHcVzlKesWKXDJqV /fLS+jaUvsggnAESa8/skzaiTHt+l4QZNDsHhuHO9EXmkKgIGtQV2BcuNbIcvw1vd+3O iMkCIWHV6rL/Lq/bacPXDPR7tzivQo8Rz9DZ27C0mwNgSKAxvVlNK1WNaOxL4FC9no/F Uxhb7tn2Y82kfYZ1L8KPDLEfJM5CgSZlSR3WrBRr/BnJrDBWBFHJmgsIMlj979xvk8zS nDBLHfpeQXcaOsQASXvFenRETGZUD6zfnQIBvJTS6mlqVCTtswaRvprW92m36BySjnbW iN7A== X-Gm-Message-State: AOJu0Yw28WvVJ2TH7yKHOywpi2f9hkVrcYzIBl2zT8OaQvkJjyPYRkT3 CDYsnTkU62B+PPEzmzaX45fmKrlb8Q== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:903:32cb:b0:1d0:c3e7:d4c with SMTP id i11-20020a17090332cb00b001d0c3e70d4cmr53041plr.2.1702414044401; Tue, 12 Dec 2023 12:47:24 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:29 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-15-sagis@google.com> Subject: [RFC PATCH v5 14/29] KVM: selftests: TDX: Add TDX IO reads test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:15 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110572828495536 X-GMAIL-MSGID: 1785110572828495536 The test verifies IO reads of various sizes from the host to the guest. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Reviewed-by: Binbin Wu --- .../selftests/kvm/x86_64/tdx_vm_tests.c | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index a2b3e1aef151..699cba36e9ce 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -429,6 +429,92 @@ void verify_guest_writes(void) printf("\t ... PASSED\n"); } +#define TDX_IO_READS_TEST_PORT 0x52 + +/* + * Verifies IO functionality by reading values of different sizes + * from the host. + */ +void guest_io_reads(void) +{ + uint64_t data; + uint64_t ret; + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_READS_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_READ, + &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0xAB) + tdx_test_fatal(1); + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_READS_TEST_PORT, 2, + TDG_VP_VMCALL_INSTRUCTION_IO_READ, + &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0xABCD) + tdx_test_fatal(2); + + ret = tdg_vp_vmcall_instruction_io(TDX_IO_READS_TEST_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_READ, + &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0xFFABCDEF) + tdx_test_fatal(4); + + // Read an invalid number of bytes. + ret = tdg_vp_vmcall_instruction_io(TDX_IO_READS_TEST_PORT, 5, + TDG_VP_VMCALL_INSTRUCTION_IO_READ, + &data); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +void verify_guest_reads(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_io_reads); + td_finalize(vm); + + printf("Verifying guest reads:\n"); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_READS_TEST_PORT, 1, + TDG_VP_VMCALL_INSTRUCTION_IO_READ); + *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = 0xAB; + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_READS_TEST_PORT, 2, + TDG_VP_VMCALL_INSTRUCTION_IO_READ); + *(uint16_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = 0xABCD; + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_IO_READS_TEST_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_READ); + *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = 0xFFABCDEF; + + td_vcpu_run(vcpu); + TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + TEST_ASSERT_EQ(vcpu->run->system_event.data[1], TDG_VP_VMCALL_INVALID_OPERAND); + + td_vcpu_run(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -444,6 +530,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_td_cpuid); run_in_new_process(&verify_get_td_vmcall_info); run_in_new_process(&verify_guest_writes); + run_in_new_process(&verify_guest_reads); return 0; } From patchwork Tue Dec 12 20:46:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177568 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993659vqy; Tue, 12 Dec 2023 12:48:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IFeRyTKMFLflgDPlEBoSksmrRAFSPl2IpWeVojLERafadCyorTXUj3Cy/sP23ywvyqLM+We X-Received: by 2002:a17:90a:f0ce:b0:286:9cc1:a10 with SMTP id fa14-20020a17090af0ce00b002869cc10a10mr5747885pjb.8.1702414113136; Tue, 12 Dec 2023 12:48:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414113; cv=none; d=google.com; s=arc-20160816; b=wLoz+vPlsJD+gW7CM44NrQNtgr51Mm5iwETamM4RMZGIxrAlaheE2NUksuRPB4cqRo 7/wZ8pipOMmj5sLhwNR87dPikP96kf1uLJz6gkSc/LKUqIJW6mN492iRxG30dN7vyMtf I2UFYRKwRqm9JvXqDbEFnoA3NCZoCRepyp2dglvIb5NdrFFFTp1WQd/Ei9jhR7phNyr9 iCcRd61lxZk4GEhwrJYAy3OeEKNa/IULIntyXvaHMhwSMwZFYv1OUXEEUdEuszRyBp+J HkI9Kktkghmba9QmJJz4orY/WFwtRw3VeKPUhN6EqESQSlGvPe+gaH+uhhCLSzDKc5WV /B7g== 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=tGOeri4zVsC3MNu9Ed0LWydKBDXJJokVJF2VNK8BRVk=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=QZ8W2Z+DUHWqqEZ+8RrfN3AOAwfPMSLF83Y0VuvXDyJfPX3cGUNIjHyWMrHFuCbM+9 rSvKNnpFYF33NlKaMwlA5XW5UnIT4Og4W6W+8RqUfSrfW5GYLBvsCU2WHbJJfR7d4AsW FgL9wWTNS2iGwBNbWpGLOOAq00MCt+mojtdfJnXHuUc/GuW2M5qhfu67EhTzjYJoSLPC 2OpbrJ9dJZByrx7m+dQURZ6LaZndQMXgT0b5pCLQ4A+Ak8aOpb4v6RMzbL/ovwjrZZMX D5uRsQa396iaZh8NL9VKBKqSP6jFHzoE7G9lKzuMJHCGxNYhqOjl5RkwhV4Areba5lk3 FvrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=X5V+vqdu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id a14-20020a17090ad80e00b00286ce8ab125si286802pjv.0.2023.12.12.12.48.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=X5V+vqdu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id CB23880B1809; Tue, 12 Dec 2023 12:48:29 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235186AbjLLUsM (ORCPT + 99 others); Tue, 12 Dec 2023 15:48:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235200AbjLLUrd (ORCPT ); Tue, 12 Dec 2023 15:47:33 -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 D2284D5E for ; Tue, 12 Dec 2023 12:47:27 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-db7dd9a8bd6so6414593276.1 for ; Tue, 12 Dec 2023 12:47:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414046; x=1703018846; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tGOeri4zVsC3MNu9Ed0LWydKBDXJJokVJF2VNK8BRVk=; b=X5V+vqdu4rRx6/QuFvsY47zBm84bIujdYZxTPW2AD0WHYgnfaRoVevpo6sWXEto0b6 oZwsZWzYb9gDrhgBzxu+THTwBLZ8fPyHHVvTE9ZlXJfg1tZQKOcqXdNtV+bpndAZwfC3 /MV7706/K+aUhBh/voz+HEVdgThbQHoEQb48hZZb4sOdhcuEDftYcKa2bZrSQv/ZqMuG X19/Yay/FHYKkR9RtdOgT+EbwF8+59Qbk23G5vF7gl56AwrxS5onaKhmv8JTV047UKsU yeljYe1hPWGWUiaf7o+EJ9K73crwYrNJKDGKPs37/03l4Ha15MEloY2ZSrX+FqrAQZuD zYfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414046; x=1703018846; 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=tGOeri4zVsC3MNu9Ed0LWydKBDXJJokVJF2VNK8BRVk=; b=jmBdfJAObkKduPwtlzsYvS+wSw5Pa1QipW6S0E1tI6voHCbOTmWNzt8KzCHPZEUszG aPXopV8CyDWEYa1QXRv3VRgLl56qXbWG3gFv4qRi9O6bb46A6hrIa/iD5jYqBLKMKRar WchelxLRqsKlmAZevZvoHmJvzmAS98ZUPDuyMVnItN2ctHGkUh6dxU/2I3RUWhX3N2eZ T+oExyeAt4RWym6WC7sPVOpkGMJcynOkALxLuhCFd8Ca1FouVunAJkcBm6310ZED2Dou 3yv4rGUSmJHogolEML/4Iuiqc+BY82WRFMuZ+G0XWy5YdqscTFr6u6DxUNfoBKbqqGsA ZN0w== X-Gm-Message-State: AOJu0YwTj73B5uWi/JVm3h055S/QW3fHF6rTderf8QwjgH0DT1GNxW3r /+LocW7/TPHCSYoqV2O0PRiwrtACjw== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a25:8a09:0:b0:db5:4d59:3b30 with SMTP id g9-20020a258a09000000b00db54d593b30mr44477ybl.8.1702414046156; Tue, 12 Dec 2023 12:47:26 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:30 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-16-sagis@google.com> Subject: [RFC PATCH v5 15/29] KVM: selftests: TDX: Add TDX MSR read/write tests From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:30 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110580934172928 X-GMAIL-MSGID: 1785110580934172928 The test verifies reads and writes for MSR registers with different access level. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/x86_64/tdx/tdx.h | 5 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 27 +++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 209 ++++++++++++++++++ 3 files changed, 241 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index 63788012bf94..85ba6aab79a7 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -9,11 +9,16 @@ #define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 #define TDG_VP_VMCALL_INSTRUCTION_IO 30 +#define TDG_VP_VMCALL_INSTRUCTION_RDMSR 31 +#define TDG_VP_VMCALL_INSTRUCTION_WRMSR 32 + void handle_userspace_tdg_vp_vmcall_exit(struct kvm_vcpu *vcpu); uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size, uint64_t write, uint64_t *data); void tdg_vp_vmcall_report_fatal_error(uint64_t error_code, uint64_t data_gpa); uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12, uint64_t *r13, uint64_t *r14); +uint64_t tdg_vp_vmcall_instruction_rdmsr(uint64_t index, uint64_t *ret_value); +uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index e5a9e13c62e2..88ea6f2a6469 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -87,3 +87,30 @@ uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12, return ret; } + +uint64_t tdg_vp_vmcall_instruction_rdmsr(uint64_t index, uint64_t *ret_value) +{ + uint64_t ret; + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_INSTRUCTION_RDMSR, + .r12 = index, + }; + + ret = __tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT); + + if (ret_value) + *ret_value = args.r11; + + return ret; +} + +uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value) +{ + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_INSTRUCTION_WRMSR, + .r12 = index, + .r13 = value, + }; + + return __tdx_hypercall(&args, 0); +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 699cba36e9ce..5db3701cc6d9 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -515,6 +515,213 @@ void verify_guest_reads(void) printf("\t ... PASSED\n"); } +/* + * Define a filter which denies all MSR access except the following: + * MSR_X2APIC_APIC_ICR: Allow read/write access (allowed by default) + * MSR_IA32_MISC_ENABLE: Allow read access + * MSR_IA32_POWER_CTL: Allow write access + */ +#define MSR_X2APIC_APIC_ICR 0x830 +static u64 tdx_msr_test_allow_bits = 0xFFFFFFFFFFFFFFFF; +struct kvm_msr_filter tdx_msr_test_filter = { + .flags = KVM_MSR_FILTER_DEFAULT_DENY, + .ranges = { + { + .flags = KVM_MSR_FILTER_READ, + .nmsrs = 1, + .base = MSR_IA32_MISC_ENABLE, + .bitmap = (uint8_t *)&tdx_msr_test_allow_bits, + }, { + .flags = KVM_MSR_FILTER_WRITE, + .nmsrs = 1, + .base = MSR_IA32_POWER_CTL, + .bitmap = (uint8_t *)&tdx_msr_test_allow_bits, + }, + }, +}; + +/* + * Verifies MSR read functionality. + */ +void guest_msr_read(void) +{ + uint64_t data; + uint64_t ret; + + ret = tdg_vp_vmcall_instruction_rdmsr(MSR_X2APIC_APIC_ICR, &data); + if (ret) + tdx_test_fatal(ret); + + ret = tdx_test_report_64bit_to_user_space(data); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_instruction_rdmsr(MSR_IA32_MISC_ENABLE, &data); + if (ret) + tdx_test_fatal(ret); + + ret = tdx_test_report_64bit_to_user_space(data); + if (ret) + tdx_test_fatal(ret); + + /* We expect this call to fail since MSR_IA32_POWER_CTL is write only */ + ret = tdg_vp_vmcall_instruction_rdmsr(MSR_IA32_POWER_CTL, &data); + if (ret) { + ret = tdx_test_report_64bit_to_user_space(ret); + if (ret) + tdx_test_fatal(ret); + } else { + tdx_test_fatal(-99); + } + + tdx_test_success(); +} + +void verify_guest_msr_reads(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + uint64_t data; + int ret; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + + /* + * Set explicit MSR filter map to control access to the MSR registers + * used in the test. + */ + printf("\t ... Setting test MSR filter\n"); + ret = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR); + TEST_ASSERT(ret, "KVM_CAP_X86_USER_SPACE_MSR is unavailable"); + vm_enable_cap(vm, KVM_CAP_X86_USER_SPACE_MSR, KVM_MSR_EXIT_REASON_FILTER); + + ret = kvm_check_cap(KVM_CAP_X86_MSR_FILTER); + TEST_ASSERT(ret, "KVM_CAP_X86_MSR_FILTER is unavailable"); + + ret = ioctl(vm->fd, KVM_X86_SET_MSR_FILTER, &tdx_msr_test_filter); + TEST_ASSERT(ret == 0, + "KVM_X86_SET_MSR_FILTER failed, ret: %i errno: %i (%s)", + ret, errno, strerror(errno)); + + vcpu = td_vcpu_add(vm, 0, guest_msr_read); + td_finalize(vm); + + printf("Verifying guest msr reads:\n"); + + printf("\t ... Setting test MSR values\n"); + /* Write arbitrary to the MSRs. */ + vcpu_set_msr(vcpu, MSR_X2APIC_APIC_ICR, 4); + vcpu_set_msr(vcpu, MSR_IA32_MISC_ENABLE, 5); + vcpu_set_msr(vcpu, MSR_IA32_POWER_CTL, 6); + + printf("\t ... Running guest\n"); + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + data = tdx_test_read_64bit_report_from_guest(vcpu); + TEST_ASSERT_EQ(data, 4); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + data = tdx_test_read_64bit_report_from_guest(vcpu); + TEST_ASSERT_EQ(data, 5); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + data = tdx_test_read_64bit_report_from_guest(vcpu); + TEST_ASSERT_EQ(data, TDG_VP_VMCALL_INVALID_OPERAND); + + td_vcpu_run(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + +/* + * Verifies MSR write functionality. + */ +void guest_msr_write(void) +{ + uint64_t ret; + + ret = tdg_vp_vmcall_instruction_wrmsr(MSR_X2APIC_APIC_ICR, 4); + if (ret) + tdx_test_fatal(ret); + + /* We expect this call to fail since MSR_IA32_MISC_ENABLE is read only */ + ret = tdg_vp_vmcall_instruction_wrmsr(MSR_IA32_MISC_ENABLE, 5); + if (ret) { + ret = tdx_test_report_64bit_to_user_space(ret); + if (ret) + tdx_test_fatal(ret); + } else { + tdx_test_fatal(-99); + } + + + ret = tdg_vp_vmcall_instruction_wrmsr(MSR_IA32_POWER_CTL, 6); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +void verify_guest_msr_writes(void) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + uint64_t data; + int ret; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + + /* + * Set explicit MSR filter map to control access to the MSR registers + * used in the test. + */ + printf("\t ... Setting test MSR filter\n"); + ret = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR); + TEST_ASSERT(ret, "KVM_CAP_X86_USER_SPACE_MSR is unavailable"); + vm_enable_cap(vm, KVM_CAP_X86_USER_SPACE_MSR, KVM_MSR_EXIT_REASON_FILTER); + + ret = kvm_check_cap(KVM_CAP_X86_MSR_FILTER); + TEST_ASSERT(ret, "KVM_CAP_X86_MSR_FILTER is unavailable"); + + ret = ioctl(vm->fd, KVM_X86_SET_MSR_FILTER, &tdx_msr_test_filter); + TEST_ASSERT(ret == 0, + "KVM_X86_SET_MSR_FILTER failed, ret: %i errno: %i (%s)", + ret, errno, strerror(errno)); + + vcpu = td_vcpu_add(vm, 0, guest_msr_write); + td_finalize(vm); + + printf("Verifying guest msr writes:\n"); + + printf("\t ... Running guest\n"); + /* Only the write to MSR_IA32_MISC_ENABLE should trigger an exit */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + data = tdx_test_read_64bit_report_from_guest(vcpu); + TEST_ASSERT_EQ(data, TDG_VP_VMCALL_INVALID_OPERAND); + + td_vcpu_run(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + printf("\t ... Verifying MSR values writen by guest\n"); + + TEST_ASSERT_EQ(vcpu_get_msr(vcpu, MSR_X2APIC_APIC_ICR), 4); + TEST_ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_MISC_ENABLE), 0x1800); + TEST_ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_POWER_CTL), 6); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -531,6 +738,8 @@ int main(int argc, char **argv) run_in_new_process(&verify_get_td_vmcall_info); run_in_new_process(&verify_guest_writes); run_in_new_process(&verify_guest_reads); + run_in_new_process(&verify_guest_msr_writes); + run_in_new_process(&verify_guest_msr_reads); return 0; } From patchwork Tue Dec 12 20:46:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177569 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993820vqy; Tue, 12 Dec 2023 12:48:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IEaMkAob5lLXn/3MNNIpHSL8dTsvukD1HZJ/MH25+5HMLescrY4hQFe3w3ejHXVO04/UxN/ X-Received: by 2002:a05:6a20:3948:b0:18f:97c:4f4a with SMTP id r8-20020a056a20394800b0018f097c4f4amr4218367pzg.86.1702414131456; Tue, 12 Dec 2023 12:48:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414131; cv=none; d=google.com; s=arc-20160816; b=tKV7B66tGojPqSWOakRKaOOyO5vZdv/Cu1SGew1Zu3QEYfztpICi2fy5ala9L7J3ek OD9b9mYzzNQauP+tekYACfQm60Eat63JOQdJXE3LCYWtiL0i3hJExDN5dPSWiJTZqFRN MiDRrdcl9E96HBXCylWwZO6lR28hTPcUtlIvygZPFJynyFFJa86NlWqOzfZyYh3KsGdm VdbV996E2a71Iu7VkDDrMd/3k24eIchW0Nf9WqngGQxWcPpr3uJka1VtjOPW/F3kOK5h nMvjRNmizr8xcLvLbmkyTeOvswCZt5fmiNaHnabjOcj7GmqwjDuraywGA98KHG+2SagG gIXQ== 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=5MyTn48cv4nrVxvqRuS9eNhhEY7jSVIhffkw0y0uC7o=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=yrZZTlDXeY2Jy6A4A8aLWF7uCkoQj+5cL+0bWdZv5EImMusjxmgzzAK+nidB77rD98 WnHfi4aHr3rP7XYHc36TCxv295x5Wff6tlWnI9t/rt/pKQALmWm8pU/+zDEEVcp4KMuR Ew4y6TN9xKUwlSCVVfqWO7xIRQxObNdAl2snHZco5ZM0a6xmTD4cUuE4bYtd7TwcLf7q 0+hn4lSHvYQXkxhTnQqlyuXPq2OaNiM2JOZZuOiR3vm491OhnenmYoY9YFQQrbscFZ7g 2MWcX0gjdndKfh5F2IDwcYVsejm+nTou6llaOSW0gvs/CcRXBfC5FCFCFEfGbREOlRZZ jOdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Hijd/8DP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id a3-20020a654183000000b005b9a1494637si8133426pgq.317.2023.12.12.12.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Hijd/8DP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id CC0F380FFD80; Tue, 12 Dec 2023 12:48:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377373AbjLLUsV (ORCPT + 99 others); Tue, 12 Dec 2023 15:48:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235224AbjLLUry (ORCPT ); Tue, 12 Dec 2023 15:47:54 -0500 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 E509410F0 for ; Tue, 12 Dec 2023 12:47:30 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d33751a322so5237085ad.3 for ; Tue, 12 Dec 2023 12:47:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414048; x=1703018848; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5MyTn48cv4nrVxvqRuS9eNhhEY7jSVIhffkw0y0uC7o=; b=Hijd/8DPJdYHAfJvIEjgagibhTXaYX0KplIL+/L08ijNytvu42LzGUFb7L6vSD79I0 vWLRRx7Fhj5ndw13FvEyXloHA0lbtIjWHDBS9h0Stnx8KXuqXy0UPE3ov0gJbdvQiL5c bTXtcdLdQWMxzhKn/7789p5DETf79I2+wUzGu6XJ8rbzhaXoi7yrHairjFB7goPP96h1 C5lLPjVUaqXtkaRpefylDo3sjnEYDBTNB5KSVLxkopTNyP85me7q+lRfJg03eg4iOmTC zXGrx04GIgmKNyBffZV6/q0IM66sllCJmHNRvcCQzdXnkDiOyTh8UK4sDQAIG4XpsfJr Q60g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414048; x=1703018848; 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=5MyTn48cv4nrVxvqRuS9eNhhEY7jSVIhffkw0y0uC7o=; b=sK4Lfr/gotmdscPo0JUOTq1faDvIHzLWDobPsX5brlHwj4aI4Hl3t24AaeML9puNYP Il3Q/uS8nm4e7okDO56i6+Hp7I9fnyiEAu5t9QcWXDQvVhdz24XAg0aFNj7ldEArcgDX AFwFTMjXTxtMc9Dr7WBrO9bPIG16k8UFV+2BtdAzy3RZknhYkIKWgUNujmZVCDu9YgF4 zw0lGUAsUaP0miWbDy/X27sYv2mjCpYa00BLxrKzIFBiQNKQQEvUhVYWUkAfqWcS/rz6 UwKkRdUh1wT+zV+UHvf3uvuGRBLoiWUFlxzzvYepO9Wqa3f71w0MJROrlKq+Ao3sjC2p pA/w== X-Gm-Message-State: AOJu0Yz20RzAt9kdrIKalAWS9urw3EU/YUbegJ0+7ib2wF+LPYk7PS9a 6bWHaSA5ZKUstGy7Neg7Jas8aDiECg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:b712:b0:1d3:46f4:7f1e with SMTP id d18-20020a170902b71200b001d346f47f1emr4989pls.1.1702414047966; Tue, 12 Dec 2023 12:47:27 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:31 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-17-sagis@google.com> Subject: [RFC PATCH v5 16/29] KVM: selftests: TDX: Add TDX HLT exit test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:49 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110600139200130 X-GMAIL-MSGID: 1785110600139200130 The test verifies that the guest runs TDVMCALL and the guest vCPU enters to the halted state. Signed-off-by: Erdem Aktas Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/x86_64/tdx/tdx.h | 2 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 10 +++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 78 +++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index 85ba6aab79a7..b18e39d20498 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -8,6 +8,7 @@ #define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 #define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 +#define TDG_VP_VMCALL_INSTRUCTION_HLT 12 #define TDG_VP_VMCALL_INSTRUCTION_IO 30 #define TDG_VP_VMCALL_INSTRUCTION_RDMSR 31 #define TDG_VP_VMCALL_INSTRUCTION_WRMSR 32 @@ -20,5 +21,6 @@ uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12, uint64_t *r13, uint64_t *r14); uint64_t tdg_vp_vmcall_instruction_rdmsr(uint64_t index, uint64_t *ret_value); uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value); +uint64_t tdg_vp_vmcall_instruction_hlt(uint64_t interrupt_blocked_flag); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index 88ea6f2a6469..9485bafedc38 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -114,3 +114,13 @@ uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value) return __tdx_hypercall(&args, 0); } + +uint64_t tdg_vp_vmcall_instruction_hlt(uint64_t interrupt_blocked_flag) +{ + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_INSTRUCTION_HLT, + .r12 = interrupt_blocked_flag, + }; + + return __tdx_hypercall(&args, 0); +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 5db3701cc6d9..5fae4c6e5f95 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -721,6 +721,83 @@ void verify_guest_msr_writes(void) printf("\t ... PASSED\n"); } +/* + * Verifies HLT functionality. + */ +void guest_hlt(void) +{ + uint64_t ret; + uint64_t interrupt_blocked_flag; + + interrupt_blocked_flag = 0; + ret = tdg_vp_vmcall_instruction_hlt(interrupt_blocked_flag); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +void _verify_guest_hlt(int signum); + +void wake_me(int interval) +{ + struct sigaction action; + + action.sa_handler = _verify_guest_hlt; + sigemptyset(&action.sa_mask); + action.sa_flags = 0; + + TEST_ASSERT(sigaction(SIGALRM, &action, NULL) == 0, + "Could not set the alarm handler!"); + + alarm(interval); +} + +void _verify_guest_hlt(int signum) +{ + struct kvm_vm *vm; + static struct kvm_vcpu *vcpu; + + /* + * This function will also be called by SIGALRM handler to check the + * vCPU MP State. If vm has been initialized, then we are in the signal + * handler. Check the MP state and let the guest run again. + */ + if (vcpu != NULL) { + struct kvm_mp_state mp_state; + + vcpu_mp_state_get(vcpu, &mp_state); + TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_HALTED); + + /* Let the guest to run and finish the test.*/ + mp_state.mp_state = KVM_MP_STATE_RUNNABLE; + vcpu_mp_state_set(vcpu, &mp_state); + return; + } + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_hlt); + td_finalize(vm); + + printf("Verifying HLT:\n"); + + printf("\t ... Running guest\n"); + + /* Wait 1 second for guest to execute HLT */ + wake_me(1); + td_vcpu_run(vcpu); + + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + +void verify_guest_hlt(void) +{ + _verify_guest_hlt(0); +} int main(int argc, char **argv) { @@ -740,6 +817,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_guest_reads); run_in_new_process(&verify_guest_msr_writes); run_in_new_process(&verify_guest_msr_reads); + run_in_new_process(&verify_guest_hlt); return 0; } From patchwork Tue Dec 12 20:46:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177573 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994004vqy; Tue, 12 Dec 2023 12:49:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IE0jhWxgloUPR4ToAKZ9INzuSZrVxglhmxG4esq7tRk+gigLXnMk665MtxJ7Si++hJKVG4i X-Received: by 2002:a17:90a:6b47:b0:28a:c561:f5af with SMTP id x7-20020a17090a6b4700b0028ac561f5afmr820382pjl.16.1702414155604; Tue, 12 Dec 2023 12:49:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414155; cv=none; d=google.com; s=arc-20160816; b=pUaEFTOi8Ol224UZpWwV7yZ5tHXbMNfo52zwWQrR7K+2bbHOaZqB36LZp+IlHQ32Ds lx/nx9KH/e4v/ZuHoQiAEWS7gH+kwp6VTy4Qm7C5J4z3UNibG8kF2AZ+3I4AFnQPkN5e 2vdf8AUzd2EDCbbQ4MZxZw6IU3PV7soXi75wHxVsRTHkrnPo9uh02svRnRYzgPA+x69+ YnyjiELvgzo+WP4huFyNsdJ2BPjt/NLobpl1/95eNZdP+Gdc+ad4vJWtYx0E92mMRbK0 1JYdnLH+5+s+9yTkXG8FlV5Bseuq2D+M+KN/PXVlAKQh2QnXzvfT832+QVpxrQ/Q2m+j 9PPQ== 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=4egsLMzPG6RqT1HiF2arhOZ1FLT3WKNE+n1ktHUZWdA=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=qhZdedS5Sqcnwtb/cj3Ir/9GXLC9wG7zhwwg51+jWiYFVCh1s6+drZOq/sKEY0v9YY p/QxvtEAQ6YAMHMbYFGGCIMDEnAILmQRYowYE0oPZZz3fENyeBg+qlIUGddgEn6Cd76f a0fx3W2/v2hytoU79MZaOFu1bw81awRuuqJ3YQhBNZwAJJ7SbrLIkPLw25DZGfTsWQU7 h9EHINBabslI81yHJ4Ddfci8F23Z3Lb8StN/j//ggRt3HwfbXcEFyDr8+WXA2shZzDM1 2D5VAwGGSmhlgBqtBD/rBvMA8+eOGEX9lIjuq+0M2vmmx9nCKLhIzXQ3tTJLVZLm8Bnd sksQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Vbl2IYqN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id w9-20020a17090aad4900b00285f918f395si8225956pjv.190.2023.12.12.12.49.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Vbl2IYqN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 88E0880B123A; Tue, 12 Dec 2023 12:49:12 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377289AbjLLUsr (ORCPT + 99 others); Tue, 12 Dec 2023 15:48:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377345AbjLLUsU (ORCPT ); Tue, 12 Dec 2023 15:48:20 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 259F4123 for ; Tue, 12 Dec 2023 12:47:34 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dbc1f00dc35so6913414276.3 for ; Tue, 12 Dec 2023 12:47:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414050; x=1703018850; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4egsLMzPG6RqT1HiF2arhOZ1FLT3WKNE+n1ktHUZWdA=; b=Vbl2IYqN1MkG2HBPLQMW/9AKGtyl7M0Zio6vfnqMpTnPrTLjzSDuAWNN9b2wDjIlGL 24XeJnN4a5Ye6tWFeWHgpA3bqeojhVMNYf/8aPB1BSXX3lRDMQVYSboNys0PvEQNFH2s 61ugIktVhEPu8ES1uLHGrWz1OtvQuy2+Abqls22Yackv+IQe0v4fJy4ZELH2VhrNBSaS c6wmoizWvc2LSpVbRHB7pbg4ozS7mtCLPBfLK84+BoBfJr5XydcDeb2+7rdPa4un0YEf ScnZwxTE4HLJb/AKY9+TkEFQUhFhzkwv2krEOevjrFa+XozyWDUqhouW/cDuBKnTmgER DU0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414050; x=1703018850; 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=4egsLMzPG6RqT1HiF2arhOZ1FLT3WKNE+n1ktHUZWdA=; b=b7G443r8vdZCxvkuoRLGwYD2pP8a0TcvxWbfVwBmh7oW8LDdcTSguq80MezqdybNlo wVuR2FJo8m0/zTlnSCHtZq2NXuJuqG8NAuY1unbpKYyxNSaNX4TwfS0Ide9M0ZEe2dOP zqulnUmGHAo1w87tprpTiGhVo495zuXisQ8OACfWe6B1RtN5vtd1xJncCSVQ/IpEZq3K 0PgU4Y67c0cgTtZcnQosTM7xt/xAy7x58tBV7JTndBcxKmTLg9FfF2JHKLv63gZz0/3o L/cRoYycz4krtocEL3Dc+btNB5ldqGBDtDxn7wJiZ6eU/XXjOLJuUtvUz/JyP5qYPH+q 44yQ== X-Gm-Message-State: AOJu0YyH6YpJ8kUeFvLenCxp83K214F/bGlpS2cfxO9f9CmokcMD7Uem 9NIypxwR3Eg1uTf2QIhHdAkN1iPflg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:6902:185:b0:dbc:c7f0:7c2e with SMTP id t5-20020a056902018500b00dbcc7f07c2emr6240ybh.2.1702414049815; Tue, 12 Dec 2023 12:47:29 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:32 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-18-sagis@google.com> Subject: [RFC PATCH v5 17/29] KVM: selftests: TDX: Add TDX MMIO reads test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:49:12 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110625709073618 X-GMAIL-MSGID: 1785110625709073618 The test verifies MMIO reads of various sizes from the host to the guest. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/x86_64/tdx/tdcall.h | 2 + .../selftests/kvm/include/x86_64/tdx/tdx.h | 3 + .../kvm/include/x86_64/tdx/test_util.h | 23 +++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 19 ++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 87 +++++++++++++++++++ 5 files changed, 134 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h index b5e94b7c48fa..95fcdbd8404e 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h @@ -9,6 +9,8 @@ #define TDG_VP_VMCALL_INSTRUCTION_IO_READ 0 #define TDG_VP_VMCALL_INSTRUCTION_IO_WRITE 1 +#define TDG_VP_VMCALL_VE_REQUEST_MMIO_READ 0 +#define TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE 1 #define TDG_VP_VMCALL_SUCCESS 0x0000000000000000 #define TDG_VP_VMCALL_INVALID_OPERAND 0x8000000000000000 diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index b18e39d20498..13ce60df5684 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -12,6 +12,7 @@ #define TDG_VP_VMCALL_INSTRUCTION_IO 30 #define TDG_VP_VMCALL_INSTRUCTION_RDMSR 31 #define TDG_VP_VMCALL_INSTRUCTION_WRMSR 32 +#define TDG_VP_VMCALL_VE_REQUEST_MMIO 48 void handle_userspace_tdg_vp_vmcall_exit(struct kvm_vcpu *vcpu); uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size, @@ -22,5 +23,7 @@ uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12, uint64_t tdg_vp_vmcall_instruction_rdmsr(uint64_t index, uint64_t *ret_value); uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value); uint64_t tdg_vp_vmcall_instruction_hlt(uint64_t interrupt_blocked_flag); +uint64_t tdg_vp_vmcall_ve_request_mmio_read(uint64_t address, uint64_t size, + uint64_t *data_out); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h index 8a9b6a1bec3e..af412b764604 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h @@ -35,6 +35,29 @@ (VCPU)->run->io.direction); \ } while (0) + +/** + * Assert that some MMIO operation involving TDG.VP.VMCALL <#VERequestMMIO> was + * called in the guest. + */ +#define TDX_TEST_ASSERT_MMIO(VCPU, ADDR, SIZE, DIR) \ + do { \ + TEST_ASSERT((VCPU)->run->exit_reason == KVM_EXIT_MMIO, \ + "Got exit_reason other than KVM_EXIT_MMIO: %u (%s)\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason)); \ + \ + TEST_ASSERT(((VCPU)->run->exit_reason == KVM_EXIT_MMIO) && \ + ((VCPU)->run->mmio.phys_addr == (ADDR)) && \ + ((VCPU)->run->mmio.len == (SIZE)) && \ + ((VCPU)->run->mmio.is_write == (DIR)), \ + "Got an unexpected MMIO exit values: %u (%s) %llu %d %d\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason), \ + (VCPU)->run->mmio.phys_addr, (VCPU)->run->mmio.len, \ + (VCPU)->run->mmio.is_write); \ + } while (0) + /** * Check and report if there was some failure in the guest, either an exception * like a triple fault, or if a tdx_test_fatal() was hit. diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index 9485bafedc38..b19f07ebc0e7 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -124,3 +124,22 @@ uint64_t tdg_vp_vmcall_instruction_hlt(uint64_t interrupt_blocked_flag) return __tdx_hypercall(&args, 0); } + +uint64_t tdg_vp_vmcall_ve_request_mmio_read(uint64_t address, uint64_t size, + uint64_t *data_out) +{ + uint64_t ret; + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_VE_REQUEST_MMIO, + .r12 = size, + .r13 = TDG_VP_VMCALL_VE_REQUEST_MMIO_READ, + .r14 = address, + }; + + ret = __tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT); + + if (data_out) + *data_out = args.r11; + + return ret; +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 5fae4c6e5f95..48902b69d13e 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -799,6 +799,92 @@ void verify_guest_hlt(void) _verify_guest_hlt(0); } +/* Pick any address that was not mapped into the guest to test MMIO */ +#define TDX_MMIO_TEST_ADDR 0x200000000 + +void guest_mmio_reads(void) +{ + uint64_t data; + uint64_t ret; + + ret = tdg_vp_vmcall_ve_request_mmio_read(TDX_MMIO_TEST_ADDR, 1, &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0x12) + tdx_test_fatal(1); + + ret = tdg_vp_vmcall_ve_request_mmio_read(TDX_MMIO_TEST_ADDR, 2, &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0x1234) + tdx_test_fatal(2); + + ret = tdg_vp_vmcall_ve_request_mmio_read(TDX_MMIO_TEST_ADDR, 4, &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0x12345678) + tdx_test_fatal(4); + + ret = tdg_vp_vmcall_ve_request_mmio_read(TDX_MMIO_TEST_ADDR, 8, &data); + if (ret) + tdx_test_fatal(ret); + if (data != 0x1234567890ABCDEF) + tdx_test_fatal(8); + + // Read an invalid number of bytes. + ret = tdg_vp_vmcall_ve_request_mmio_read(TDX_MMIO_TEST_ADDR, 10, &data); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +/* + * Varifies guest MMIO reads. + */ +void verify_mmio_reads(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_mmio_reads); + td_finalize(vm); + + printf("Verifying TD MMIO reads:\n"); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 1, TDG_VP_VMCALL_VE_REQUEST_MMIO_READ); + *(uint8_t *)vcpu->run->mmio.data = 0x12; + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 2, TDG_VP_VMCALL_VE_REQUEST_MMIO_READ); + *(uint16_t *)vcpu->run->mmio.data = 0x1234; + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 4, TDG_VP_VMCALL_VE_REQUEST_MMIO_READ); + *(uint32_t *)vcpu->run->mmio.data = 0x12345678; + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 8, TDG_VP_VMCALL_VE_REQUEST_MMIO_READ); + *(uint64_t *)vcpu->run->mmio.data = 0x1234567890ABCDEF; + + td_vcpu_run(vcpu); + TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + TEST_ASSERT_EQ(vcpu->run->system_event.data[1], TDG_VP_VMCALL_INVALID_OPERAND); + + td_vcpu_run(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -818,6 +904,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_guest_msr_writes); run_in_new_process(&verify_guest_msr_reads); run_in_new_process(&verify_guest_hlt); + run_in_new_process(&verify_mmio_reads); return 0; } From patchwork Tue Dec 12 20:46:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177570 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993833vqy; Tue, 12 Dec 2023 12:48:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IEsJhbf2InsxUTOwbGLO43RcNYMSr2NDz+dKjTyGSWKtwZ1BUlsrHSBM3Fcg0MW4DeVd9BA X-Received: by 2002:a05:6e02:15c5:b0:35d:59a2:bd2 with SMTP id q5-20020a056e0215c500b0035d59a20bd2mr6033065ilu.104.1702414133118; Tue, 12 Dec 2023 12:48:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414133; cv=none; d=google.com; s=arc-20160816; b=EHJgvzZHfuZ1lW25MpCSqJpUI5/IhWowWMWHLWpxN+X/b1Vtwp4UDXF4tRMDEmLj7f BTyahdChA/+WY6DXdEEDS8ogHLp971jjGJPvE+Dj4Qyu3Z53hDLgt2D0BxKR+l9KJ3Jx 29Be+jApiD5Pjtg8XcJKy+8/zPZE375/6OvOB8LypFkebjPEMFnNo2NB1jGXmZ/2yJXI 9pgXQRoQZPu22Ul39T5BnHLWC4t4ZXjEwSUTIMy8bGOSp81sjD+JE6rm5rvAYHp1gLRC SCOaK30iuNC/+CGs206+hvw/FH+oFVR9sNBoHlz1idksGxOSs3XsolC0fAJDyLYlEFHf HCjw== 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=mgr9hweDYRXBGfxKvpySsIK8PkryOfdG9AXrBZT3Fps=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=V9GlIbSHsFG8GzkS9Gg31ssiqWHbwrhuES6Kv6EQEvUMJiR5ZS5lnY2MRYmkMWkdKi esu6lXrK/REaZEqRP6+uq+2MpWpWnpc1q0//mAvpfBgzXyaF/T2XWK9jSQoMVOBRK7dx +RIZRwJbyR0tqCrkw7M8ORuANGmhvb6iH9E6KKgqTFQwk0d4ErQ96wKZGlRRoOIotiUQ gm//lHqanERWL6hhGsgD2MtKrrcYNqfejrt4/ffUfilc9erBTrBfr7bj1I+eZsA834/p gtSQVCICTiaNs7L/hkBLxL790n4oMrc+2RL+5FwPtMm3WqChcWt63/9Jl9Y/QiLaC1+m F0Zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JY5LJD4K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id o2-20020a655202000000b005898d997f52si8410342pgp.66.2023.12.12.12.48.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:48:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JY5LJD4K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id BD2B28023778; Tue, 12 Dec 2023 12:48:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377422AbjLLUsj (ORCPT + 99 others); Tue, 12 Dec 2023 15:48:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235180AbjLLUsM (ORCPT ); Tue, 12 Dec 2023 15:48:12 -0500 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 4F296173C for ; Tue, 12 Dec 2023 12:47:34 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5e175c8b326so21388587b3.2 for ; Tue, 12 Dec 2023 12:47:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414051; x=1703018851; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mgr9hweDYRXBGfxKvpySsIK8PkryOfdG9AXrBZT3Fps=; b=JY5LJD4K90PQbgBzv1Xi3PdxBwRktSYWxAyB2m/rKQENg6cmOMn5286I2Aa8qD26Df Z6CTSwRuS6uuBZ2I4GH3wnIvyBoFdlIAFVCntwrIQ3HLaUSAuy1ot2kACYBQtAqf0XgV kFXABvX0nU9ytQLPphvQOANuBNukaDL07BNJ5BpEVwaVem6glx87kXqI/7Qorbo19ZQi bJPysTemt6ziby4h5aE0BC7hIydxCDjoUE9XPFEwru8GwPB6N8FOW8AvcmffAoTwMaQm i2apSmHCs9ekOA+vJ86RA8ce+AlV6pzaksgNDRUqf416mpeVi2+jd5rnFOrQ4RGbU6FU Yg1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414051; x=1703018851; 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=mgr9hweDYRXBGfxKvpySsIK8PkryOfdG9AXrBZT3Fps=; b=UUQYeaHlSfDxCUo+mzFWd28ihOSH1fpfy+lToS1B3Xd1QSQi5zatUeYSvPU47X11NW 8ii33kW/iWAknjnwPqt1rL9oFDm73cBGmmicjvrzw+5wDzI7r4TNRnSI7ryNgVYAqFrT 6XoXkaHzZ7m4zeRJB6U4doJDpSwX9hJ9yyzgAd1YvOaQmdH3OA2Wcj3nQ0yWnoKyRoLD g9QUYDNHie3qhqpB4bSN2+Q0rENiSF5ZIeANDv8rxZiM+lqCrd/7BjB3bx3N+XIvi2E6 AquzvbLfKmxSYpSXeUg3XcKPSi1bzWA6ekc2hb0/iwnFUfITJ2JvOEtGn81uBYgUgfOa jAYg== X-Gm-Message-State: AOJu0Yzhtc2gixuocIBTU3j23VAwwioQ1nKYeyrtL6bueqOOS9UkoC5v LM1zriFx5MIgv5PBXGyfR3HMMFTK+g== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:690c:3506:b0:5d9:447a:43e8 with SMTP id fq6-20020a05690c350600b005d9447a43e8mr67267ywb.5.1702414051607; Tue, 12 Dec 2023 12:47:31 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:33 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-19-sagis@google.com> Subject: [RFC PATCH v5 18/29] KVM: selftests: TDX: Add TDX MMIO writes test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:48 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110601723046990 X-GMAIL-MSGID: 1785110601723046990 The test verifies MMIO writes of various sizes from the guest to the host. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/x86_64/tdx/tdx.h | 2 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 14 +++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 85 +++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index 13ce60df5684..502b670ea699 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -25,5 +25,7 @@ uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value); uint64_t tdg_vp_vmcall_instruction_hlt(uint64_t interrupt_blocked_flag); uint64_t tdg_vp_vmcall_ve_request_mmio_read(uint64_t address, uint64_t size, uint64_t *data_out); +uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size, + uint64_t data_in); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index b19f07ebc0e7..f4afa09f7e3d 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -143,3 +143,17 @@ uint64_t tdg_vp_vmcall_ve_request_mmio_read(uint64_t address, uint64_t size, return ret; } + +uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size, + uint64_t data_in) +{ + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_VE_REQUEST_MMIO, + .r12 = size, + .r13 = TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE, + .r14 = address, + .r15 = data_in, + }; + + return __tdx_hypercall(&args, 0); +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 48902b69d13e..5e28ba828a92 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -885,6 +885,90 @@ void verify_mmio_reads(void) printf("\t ... PASSED\n"); } +void guest_mmio_writes(void) +{ + uint64_t ret; + + ret = tdg_vp_vmcall_ve_request_mmio_write(TDX_MMIO_TEST_ADDR, 1, 0x12); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_ve_request_mmio_write(TDX_MMIO_TEST_ADDR, 2, 0x1234); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_ve_request_mmio_write(TDX_MMIO_TEST_ADDR, 4, 0x12345678); + if (ret) + tdx_test_fatal(ret); + + ret = tdg_vp_vmcall_ve_request_mmio_write(TDX_MMIO_TEST_ADDR, 8, 0x1234567890ABCDEF); + if (ret) + tdx_test_fatal(ret); + + // Write across page boundary. + ret = tdg_vp_vmcall_ve_request_mmio_write(PAGE_SIZE - 1, 8, 0); + if (ret) + tdx_test_fatal(ret); + + tdx_test_success(); +} + +/* + * Varifies guest MMIO writes. + */ +void verify_mmio_writes(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + uint8_t byte_1; + uint16_t byte_2; + uint32_t byte_4; + uint64_t byte_8; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_mmio_writes); + td_finalize(vm); + + printf("Verifying TD MMIO writes:\n"); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 1, TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE); + byte_1 = *(uint8_t *)(vcpu->run->mmio.data); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 2, TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE); + byte_2 = *(uint16_t *)(vcpu->run->mmio.data); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 4, TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE); + byte_4 = *(uint32_t *)(vcpu->run->mmio.data); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_MMIO(vcpu, TDX_MMIO_TEST_ADDR, 8, TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE); + byte_8 = *(uint64_t *)(vcpu->run->mmio.data); + + TEST_ASSERT_EQ(byte_1, 0x12); + TEST_ASSERT_EQ(byte_2, 0x1234); + TEST_ASSERT_EQ(byte_4, 0x12345678); + TEST_ASSERT_EQ(byte_8, 0x1234567890ABCDEF); + + td_vcpu_run(vcpu); + TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT); + TEST_ASSERT_EQ(vcpu->run->system_event.data[1], TDG_VP_VMCALL_INVALID_OPERAND); + + td_vcpu_run(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -905,6 +989,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_guest_msr_reads); run_in_new_process(&verify_guest_hlt); run_in_new_process(&verify_mmio_reads); + run_in_new_process(&verify_mmio_writes); return 0; } From patchwork Tue Dec 12 20:46:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177571 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993939vqy; Tue, 12 Dec 2023 12:49:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IEn92tVsovL/Hg2aqEvwS71Dj9Jb2RJgtb16R8KKxnd/TNmlD8jL2O1r3x4PceAP4389eEj X-Received: by 2002:a17:903:22cb:b0:1d0:6ffe:1e6c with SMTP id y11-20020a17090322cb00b001d06ffe1e6cmr3880628plg.79.1702414145360; Tue, 12 Dec 2023 12:49:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414145; cv=none; d=google.com; s=arc-20160816; b=moaWbxZL/T5WhCcUnLHCyrvtzc8gMSaK/fA8AoophXNUimItMLSd6xCxyBdLsdV3T3 CYhu0X9L1zidnz6VDq7gGU3qhUu13yDHz0TOGFW1U1NAH1UZRp8P43/TSuHq8nCEyUrq XvZ0sswGoP7vPO5LAZ4a6bbz2TmPLUpyIbE483Z+gLMMSVAPtkVqO8iqM0nOczjnsBjd URJuxN+LQaOZ5tTseEohbxSLtGX1D/vzlYZAXg4Ns4lrxHUd2cn39+ZCgDdWk9iIc7ZC mSX91iQ8uxwBGRQbKObTL69OU4w8rXAOPWLhyT1Z2eUBH2WiIpLqK3gWhr9ZXUe6Uyw9 2ehQ== 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=zFFcB69i+m00s0TO+Fs1znX5Nq1xcr5cHosByTfFHVM=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=V0iOfkkCTK0blVRoOvpCC+DtZnq9ijnPV0qJnM2/r5lM8QxpQcuJhKgqAUsfPHArap yJMrXaC30buhTHHW91jhVvSptLxYllx/3wUA9QCDJHmjkJ9fEBT9Zi+HNEyBor9wA/Wc n468LSIU95sKxd1AqCuy3IzMLMfhD7qHIWYViQPY3/M0+CVMlbGTP00fu83mrD0/KmQU Ul5nwQ4NsZevHdnejQ/UMrxpS15h0BBAkzcmqCFHYl5VdL+Qhrwkq6f1dJYcB6T0CrfG NmVqTQLkUF2M++8m0Cd3LHAwWi5JGR5z9t2ZG0YnWqEDms/OjkFOK6lrLNqHKXLTC6Sn BptQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xGO+KhOZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id c12-20020a170903234c00b001cfcf1faaa6si8587432plh.206.2023.12.12.12.49.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xGO+KhOZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 9A3C58020481; Tue, 12 Dec 2023 12:48:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235180AbjLLUso (ORCPT + 99 others); Tue, 12 Dec 2023 15:48:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235287AbjLLUsR (ORCPT ); Tue, 12 Dec 2023 15:48:17 -0500 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 1D5B61985 for ; Tue, 12 Dec 2023 12:47:35 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1d053953954so30443845ad.2 for ; Tue, 12 Dec 2023 12:47:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414053; x=1703018853; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zFFcB69i+m00s0TO+Fs1znX5Nq1xcr5cHosByTfFHVM=; b=xGO+KhOZgDjPvgckKjEG04OvN69sQHxRm0lE3Mq02+s5JE+81jnrbvwKWSsjOgsHg7 QpXEo8Ob9bkSP3TgBBcMGrSVks9VaxXxD6swkEhnhCtSGhjVbdyH0t9Cl4ofk55WemXv kv8R9Ho8CNeAxQTeWIANhDl2xcrVNZB1MfRiO6kJD+rHoMPESC+N4sR7n74Nh564Ge6C iYQbtqPzPLHlJvLW9kUPzwiQEN0iUEojjmjkEQ2bSz03TTR3IXZ9q/2sgJgrQguueF17 em/gwPZnpMO9AOXPF4iH7Pq3DJ99xrdiF8ppuYjxzGyM5rpBpZaFUPR43FOq7txuJl/+ AaVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414053; x=1703018853; 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=zFFcB69i+m00s0TO+Fs1znX5Nq1xcr5cHosByTfFHVM=; b=n1vZR5zp44Nu/k2s1C3szNeeqAI+2OJOR4j1e+/3x/R7TyfyJHBpD9E1A8x/7Rc4ZL X4sk8RnC1mop7Hqac+yrkQzRMBuu2dppFsudF4xkNzshJ88XwyaKYitbdKWfViltE5Vd dyK8XGr1FMd/nk+DFwVfV9iYD0TwlJosxGBMt9RmsCnvutqvVczN6wGQ2WhnG3bU2HQY ESBjKCKpVpUpR7dW984RFM/Bo19IN3OJBpVRPbxMqk/8UqR4pAodMpXB+HO0eVAjRWsd Z2T5V2BTTpovfBkKlaLf1xrYDd/f9rcSOnuTpOFI+6Jx5apeFyVnRJsWXoXbKM/IlZ6p nZTw== X-Gm-Message-State: AOJu0YxXTmB8KBB17lvuk9RjgnEakq+ahdbraCUEpSvLvlydLhQSVIQz u/f9gwj9PwMgNsMiIv4rICQQ2xSszg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:f54a:b0:1d0:5d31:4672 with SMTP id h10-20020a170902f54a00b001d05d314672mr54883plf.5.1702414053405; Tue, 12 Dec 2023 12:47:33 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:34 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-20-sagis@google.com> Subject: [RFC PATCH v5 19/29] KVM: selftests: TDX: Add TDX CPUID TDVMCALL test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:48:56 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110614582779460 X-GMAIL-MSGID: 1785110614582779460 This test issues a CPUID TDVMCALL from inside the guest to get the CPUID values as seen by KVM. Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/x86_64/tdx/tdx.h | 4 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 26 +++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 94 +++++++++++++++++++ 3 files changed, 124 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index 502b670ea699..b13a533234fd 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -8,6 +8,7 @@ #define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 #define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 +#define TDG_VP_VMCALL_INSTRUCTION_CPUID 10 #define TDG_VP_VMCALL_INSTRUCTION_HLT 12 #define TDG_VP_VMCALL_INSTRUCTION_IO 30 #define TDG_VP_VMCALL_INSTRUCTION_RDMSR 31 @@ -27,5 +28,8 @@ uint64_t tdg_vp_vmcall_ve_request_mmio_read(uint64_t address, uint64_t size, uint64_t *data_out); uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size, uint64_t data_in); +uint64_t tdg_vp_vmcall_instruction_cpuid(uint32_t eax, uint32_t ecx, + uint32_t *ret_eax, uint32_t *ret_ebx, + uint32_t *ret_ecx, uint32_t *ret_edx); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index f4afa09f7e3d..a45e2ceb6eda 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -157,3 +157,29 @@ uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size, return __tdx_hypercall(&args, 0); } + +uint64_t tdg_vp_vmcall_instruction_cpuid(uint32_t eax, uint32_t ecx, + uint32_t *ret_eax, uint32_t *ret_ebx, + uint32_t *ret_ecx, uint32_t *ret_edx) +{ + uint64_t ret; + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_INSTRUCTION_CPUID, + .r12 = eax, + .r13 = ecx, + }; + + + ret = __tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT); + + if (ret_eax) + *ret_eax = args.r12; + if (ret_ebx) + *ret_ebx = args.r13; + if (ret_ecx) + *ret_ecx = args.r14; + if (ret_edx) + *ret_edx = args.r15; + + return ret; +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 5e28ba828a92..6935604d768b 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -969,6 +969,99 @@ void verify_mmio_writes(void) printf("\t ... PASSED\n"); } +/* + * Verifies CPUID TDVMCALL functionality. + * The guest will then send the values to userspace using an IO write to be + * checked against the expected values. + */ +void guest_code_cpuid_tdcall(void) +{ + uint64_t err; + uint32_t eax, ebx, ecx, edx; + + // Read CPUID leaf 0x1 from host. + err = tdg_vp_vmcall_instruction_cpuid(/*eax=*/1, /*ecx=*/0, + &eax, &ebx, &ecx, &edx); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_to_user_space(eax); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_to_user_space(ebx); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_to_user_space(ecx); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_to_user_space(edx); + if (err) + tdx_test_fatal(err); + + tdx_test_success(); +} + +void verify_td_cpuid_tdcall(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + uint32_t eax, ebx, ecx, edx; + const struct kvm_cpuid_entry2 *cpuid_entry; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_code_cpuid_tdcall); + td_finalize(vm); + + printf("Verifying TD CPUID TDVMCALL:\n"); + + /* Wait for guest to report CPUID values */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + eax = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + ebx = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + ecx = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + edx = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + /* Get KVM CPUIDs for reference */ + cpuid_entry = get_cpuid_entry(kvm_get_supported_cpuid(), 1, 0); + TEST_ASSERT(cpuid_entry, "CPUID entry missing\n"); + + TEST_ASSERT_EQ(cpuid_entry->eax, eax); + // Mask lapic ID when comparing ebx. + TEST_ASSERT_EQ(cpuid_entry->ebx & ~0xFF000000, ebx & ~0xFF000000); + TEST_ASSERT_EQ(cpuid_entry->ecx, ecx); + TEST_ASSERT_EQ(cpuid_entry->edx, edx); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -990,6 +1083,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_guest_hlt); run_in_new_process(&verify_mmio_reads); run_in_new_process(&verify_mmio_writes); + run_in_new_process(&verify_td_cpuid_tdcall); return 0; } From patchwork Tue Dec 12 20:46:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177574 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994085vqy; Tue, 12 Dec 2023 12:49:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IEzziuA6jGOznoxrpzI6ExUVVkw8eEIcogOjo6PqjCIgvLeTLzVU4oe0q1nq7/GtN1zawsI X-Received: by 2002:a17:90a:8d86:b0:286:b91c:7774 with SMTP id d6-20020a17090a8d8600b00286b91c7774mr3571329pjo.47.1702414168917; Tue, 12 Dec 2023 12:49:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414168; cv=none; d=google.com; s=arc-20160816; b=LVKUPP+mzoGjzUq5fjSWTn/5PVsPxmA5INpcUXqTF8ktS4TiXK2D7KhoRVa4M1KE6w qR9ZPfRNsNJk+FcxhjhvBRp3JsXdgkPBXbWlRD6Rf8DPCMRF10G/FYEdKCKgNTD7Cbt4 kMwZbWkRW+sozhWJKmXjWxlBVbWmg6YsG9I/UEOx1T/b4HpqB/qzwQlUz6Be8rzoqaFz Ag3iKUpFuxyY13Oam/wLsFlqa+HgA5VyQl/YSZ+RL3oxByyRG30ZkeLsN7DdibK7D5AO 75ufwWZLhvAIPYBDdn7e0BdTQlqe7cVVZ2pO55oy8QwkQZ6pL5Q1JihtyoVYOf3U8Vk3 de+g== 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=OBiOJZf7KtnyH3BNNYJPAadRM1tNvWRCeEycOvMN+pU=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=oyW5b1zXK1kKBxfkh4YT2NS+UOdCQj1Hs6qRs3R1LbO0lb8q04pJuAgWjH1H/R2hDr 4lSmtuNqxBMfCDtlzaOoD9fJINpDWH1+iSZe5dhU+mMKAxXPq5jwJqBuI8Jo/Ay8jLxE cEKGLWRW+E3l0tmwueoxsV81rnTfgrRpkLqmui6glFaDRU8skKWUzbmheSByahWGkN5E 003q1aQa6+p3V/tTR32vBR2wry2uuh1uL0Thf8CRI3IH58kZWLqNEfNRm/JxsoXagNhV X3/3wOz7/FnYesurGweIlRsQI0AOMrHoAJjY0COQlVs+LH3h/Z/Hwph9oxk/H3aT+tfD mhPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NnfZGkQ2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id lb15-20020a17090b4a4f00b00288657cdf5fsi9729175pjb.101.2023.12.12.12.49.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NnfZGkQ2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 87C2B80FFDBE; Tue, 12 Dec 2023 12:49:26 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377486AbjLLUtG (ORCPT + 99 others); Tue, 12 Dec 2023 15:49:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235218AbjLLUsd (ORCPT ); Tue, 12 Dec 2023 15:48:33 -0500 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 EAA9BD7F for ; Tue, 12 Dec 2023 12:47:37 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1d34b1ea914so696315ad.1 for ; Tue, 12 Dec 2023 12:47:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414055; x=1703018855; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OBiOJZf7KtnyH3BNNYJPAadRM1tNvWRCeEycOvMN+pU=; b=NnfZGkQ2apcvX/2Rd3wgDpv90mk+ufZQ8ErQE1WRuNh7O7FOHKDouuaO8Y0HEb13nC 8J7M1U2ShFpL3tkcbujZmF66Ydy/6+t2NxmnLxtc/GQE2AaewNFpqTQWZK2pBZLSK8Ex hcTeeyQ/XbJSUYO1UHpe4phyzaFKjz3vrCx/OJYjxSZ2/Rbkma+CQazO4a6Sf9WDxUHj 4trDNFvTQhAjwPZ4he6tN5JhCT/9DuvijaPfNmOIqe/wRs9tGdaaTgDZSg5gAFQ0SGpf UyOJwUbyNgaBlzc+ztHy/2O1RqYXHd6ij9cCKR5eYI3KlkiKGR79SG6uXEPR9OIz2E6n 85aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414055; x=1703018855; 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=OBiOJZf7KtnyH3BNNYJPAadRM1tNvWRCeEycOvMN+pU=; b=sd0SaFFMkPViUgM4/26gtRoYt2ydgbJpNWIrBxgWp/fona7/A1vgzjX4bpiaZHk+tX 40QLuFGJTFxOuNbGe3L2ip/nVgq2kaD1RCpU+OrSyFxzPrjqc/YAP7Lc+i0A9g4lvevD 8ZHZgSrC69ivb8NL21Yvt4Dfdaj2KW9bERaklk8+LMFrux87CZV6c/DsvSFn0x5zs5SF NKg6te/OxJIcsjtGNQuuV6AdK9RdbRvubM+DRXRDSv5r2jS18bMmQktlheSx9j4KFxB4 TEwP1umlgQlPUeVxb7oPNbyKPYvma9qAkEZzgJIsSgIckJVviy2yWkz1+VFKZDC3Kga+ yhAQ== X-Gm-Message-State: AOJu0Yw8BoVxW9bDXzL2rBtB7ggTi4T567zs4Mt9RDwTtc+BFZS9Pt2U xSKD/M3NyRX91SQaiCEH+aEvzb9i1w== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:cec5:b0:1d2:f388:6def with SMTP id d5-20020a170902cec500b001d2f3886defmr52709plg.10.1702414055001; Tue, 12 Dec 2023 12:47:35 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:35 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-21-sagis@google.com> Subject: [RFC PATCH v5 20/29] KVM: selftests: TDX: Verify the behavior when host consumes a TD private memory From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:49:26 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110639479005345 X-GMAIL-MSGID: 1785110639479005345 From: Ryan Afranji The test checks that host can only read fixed values when trying to access the guest's private memory. Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng --- .../selftests/kvm/x86_64/tdx_vm_tests.c | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index 6935604d768b..c977223ff871 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -1062,6 +1062,90 @@ void verify_td_cpuid_tdcall(void) printf("\t ... PASSED\n"); } +/* + * Shared variables between guest and host for host reading private mem test + */ +static uint64_t tdx_test_host_read_private_mem_addr; +#define TDX_HOST_READ_PRIVATE_MEM_PORT_TEST 0x53 + +void guest_host_read_priv_mem(void) +{ + uint64_t ret; + uint64_t placeholder = 0; + + /* Set value */ + *((uint32_t *) tdx_test_host_read_private_mem_addr) = 0xABCD; + + /* Exit so host can read value */ + ret = tdg_vp_vmcall_instruction_io( + TDX_HOST_READ_PRIVATE_MEM_PORT_TEST, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, &placeholder); + if (ret) + tdx_test_fatal(ret); + + /* Update guest_var's value and have host reread it. */ + *((uint32_t *) tdx_test_host_read_private_mem_addr) = 0xFEDC; + + tdx_test_success(); +} + +void verify_host_reading_private_mem(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm_vaddr_t test_page; + uint64_t *host_virt; + uint64_t first_host_read; + uint64_t second_host_read; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_host_read_priv_mem); + + test_page = vm_vaddr_alloc_page(vm); + TEST_ASSERT(test_page < BIT_ULL(32), + "Test address should fit in 32 bits so it can be sent to the guest"); + + host_virt = addr_gva2hva(vm, test_page); + TEST_ASSERT(host_virt != NULL, + "Guest address not found in guest memory regions\n"); + + tdx_test_host_read_private_mem_addr = test_page; + sync_global_to_guest(vm, tdx_test_host_read_private_mem_addr); + + td_finalize(vm); + + printf("Verifying host's behavior when reading TD private memory:\n"); + + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_HOST_READ_PRIVATE_MEM_PORT_TEST, + 4, TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + printf("\t ... Guest's variable contains 0xABCD\n"); + + /* Host reads guest's variable. */ + first_host_read = *host_virt; + printf("\t ... Host's read attempt value: %lu\n", first_host_read); + + /* Guest updates variable and host rereads it. */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + printf("\t ... Guest's variable updated to 0xFEDC\n"); + + second_host_read = *host_virt; + printf("\t ... Host's second read attempt value: %lu\n", + second_host_read); + + TEST_ASSERT(first_host_read == second_host_read, + "Host did not read a fixed pattern\n"); + + printf("\t ... Fixed pattern was returned to the host\n"); + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -1084,6 +1168,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_mmio_reads); run_in_new_process(&verify_mmio_writes); run_in_new_process(&verify_td_cpuid_tdcall); + run_in_new_process(&verify_host_reading_private_mem); return 0; } From patchwork Tue Dec 12 20:46:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177575 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994104vqy; Tue, 12 Dec 2023 12:49:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IFNO+MbLPSrwLJiy4FRxHwp8pDlV0bXkNxBhZL8gSnKqNsoqpVsF2awI9zE442qVbjlJSEG X-Received: by 2002:a05:6a00:2401:b0:6ce:425a:5ece with SMTP id z1-20020a056a00240100b006ce425a5ecemr3905993pfh.27.1702414173249; Tue, 12 Dec 2023 12:49:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414173; cv=none; d=google.com; s=arc-20160816; b=FlGP6I+qMsmzjQOtbkDFj1N4cgWWj2352t0kfpTYwvuZwKICFhiQQMQpIvLjziEFru Zr623DDomNwbprZZcc/KZWbGAtS5dXcQufXVYse++96Jjacs4FVVcMLOGLjEgj+oj/ET TdSxUUcRsLMSe2G+H3Ey5enNkx5p+yJvUcoKJlJBqtYmEKkh4/wvMJoXWu3hRWj4FKnx JL77h9x9nHpycPzxC2EI3mhNFqfmx3a0MLNgJmleKnCJy5nedvky8J9lVRjtfPS/LZRA sbh/FmTGipahG/unt3ruFL9AFQDyTJSPNW6RAKbewf79OHxeNjMahc0XMTYHrEXMtYt8 xJFw== 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=RpSPCIRhHRpzymkSz5bDDdD/wHDWR62G9Siv+tSDD8s=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=sRrXTZ3ePsK2W90lZPd7iPJzUKCBKPSnSZT5Dnj8FnGcqdAAMxm3W5uDAu1jtxvxJT Jy3pxaz0nZcZHOQoDEwEoM1F9iLUixfCZNMo+iU7afTUHsk5WZ6Md/enK838lvTn4Jf9 qGTdPe9ymJ8sWc30IOaO4nyDIIjLKb7R/XqpShfLuuJoUsHfx4A0yG01xqlYg46rhBs0 vNbcPtwOIlgCDpKaDnG/bI5k0klRfO8Aq/07RJuGVjJwICynd0XS7ToPoqq0uniTiDOJ oasW2aWj0TQinsYbjcUgUYBze/Rp+XrTF4TsnnV6NubPhN8DboNFYrPSfQitxdtljkMh g8RQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NhbJW4is; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id u11-20020a62d44b000000b006ce2efa4422si8067344pfl.405.2023.12.12.12.49.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NhbJW4is; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 06FFF80FFDBE; Tue, 12 Dec 2023 12:49:30 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377473AbjLLUtA (ORCPT + 99 others); Tue, 12 Dec 2023 15:49:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235186AbjLLUsd (ORCPT ); Tue, 12 Dec 2023 15:48:33 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6715719AA for ; Tue, 12 Dec 2023 12:47:39 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-db410931c23so6400833276.2 for ; Tue, 12 Dec 2023 12:47:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414057; x=1703018857; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RpSPCIRhHRpzymkSz5bDDdD/wHDWR62G9Siv+tSDD8s=; b=NhbJW4isfeFU7k3U6zdYtLtI2vtF/XBcpsU4bnX4wk1VBN1EUpjZ1A0Hl705wPhHsy 5sOmNRCuXJ69evzb+mOw00IopgNV2H+7f6RhavDVZRGA0huTclBaj2BcTxbHybKH8Rf8 4XPQY/4Hdrhi3LAXWH7bvXasygMOjT+gW1JHfdx9QTHW4bvLoEH9FyUwf+5Cpi7T1xRu kAw52oCczgOnlALqIastyrVXZrrJs2B4WzV3+7boLbNCpBmNWHK8I76NErQeGroYfkT8 ZFjRYBy14WcpKNrjBT2n4x/dTRDxL06CgSsXTfjpxWwfHAqiUWEMTO1wUbpa08nAWJQt EQMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414057; x=1703018857; 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=RpSPCIRhHRpzymkSz5bDDdD/wHDWR62G9Siv+tSDD8s=; b=Hb65uECu7P36nlRxiSAgBVYWZns2T+jxE7IAQ5e52YzUOrVKfsagWY7HiahvDwrzTs /QVWAOXASeRl3GKktqNmINoxfGj3Ybcjgzi031rZd0QTceO6RBuhnSlZqidYz5RJ0gHy AYPFRHaSO03sdUHwpG6HmrKPtuMINNtQL0hoIXUq01jYLuXggYvXGfBLEwhpn6AlEbSa 6odp5k6EKUGn71UokdvyNJn/3JmijwPGc4QJXu1QgBkKx8fSw1N47XpK/thwzHUKpQOL X6Vk45+VizLpbjskVztu9ULZeIKMRa4t/CBnODlnvzzp+4t0JL4tJb/MyQY59JEk98J6 KvHQ== X-Gm-Message-State: AOJu0YxRGMQ3GLumcQoqZQR+VvanfsYJC/K7fRr1H3yZWlhOnsL0d7nX BhKeausIB5wMcRhho/eFrRbCfKh+hg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:6902:534:b0:db5:41e9:aa1c with SMTP id y20-20020a056902053400b00db541e9aa1cmr43956ybs.11.1702414056918; Tue, 12 Dec 2023 12:47:36 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:36 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-22-sagis@google.com> Subject: [RFC PATCH v5 21/29] KVM: selftests: TDX: Add TDG.VP.INFO test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:49:30 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110644143666061 X-GMAIL-MSGID: 1785110644143666061 From: Roger Wang Adds a test for TDG.VP.INFO Signed-off-by: Roger Wang Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji --- .../selftests/kvm/include/x86_64/tdx/tdcall.h | 19 +++ .../selftests/kvm/include/x86_64/tdx/tdx.h | 5 + .../selftests/kvm/lib/x86_64/tdx/tdcall.S | 68 ++++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 27 ++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 145 ++++++++++++++++++ 5 files changed, 264 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h index 95fcdbd8404e..a65ce8f3c109 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h @@ -37,4 +37,23 @@ struct tdx_hypercall_args { /* Used to request services from the VMM */ u64 __tdx_hypercall(struct tdx_hypercall_args *args, unsigned long flags); +/* + * Used to gather the output registers values of the TDCALL and SEAMCALL + * instructions when requesting services from the TDX module. + * + * This is a software only structure and not part of the TDX module/VMM ABI. + */ +struct tdx_module_output { + u64 rcx; + u64 rdx; + u64 r8; + u64 r9; + u64 r10; + u64 r11; +}; + +/* Used to communicate with the TDX module */ +u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, + struct tdx_module_output *out); + #endif // SELFTESTS_TDX_TDCALL_H diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index b13a533234fd..6b176de1e795 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -5,6 +5,8 @@ #include #include "kvm_util_base.h" +#define TDG_VP_INFO 1 + #define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 #define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 @@ -31,5 +33,8 @@ uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size, uint64_t tdg_vp_vmcall_instruction_cpuid(uint32_t eax, uint32_t ecx, uint32_t *ret_eax, uint32_t *ret_ebx, uint32_t *ret_ecx, uint32_t *ret_edx); +uint64_t tdg_vp_info(uint64_t *rcx, uint64_t *rdx, + uint64_t *r8, uint64_t *r9, + uint64_t *r10, uint64_t *r11); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S index df9c1ed4bb2d..601d71531443 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S @@ -86,5 +86,73 @@ __tdx_hypercall: pop %rbp ret +#define TDX_MODULE_rcx 0 /* offsetof(struct tdx_module_output, rcx) */ +#define TDX_MODULE_rdx 8 /* offsetof(struct tdx_module_output, rdx) */ +#define TDX_MODULE_r8 16 /* offsetof(struct tdx_module_output, r8) */ +#define TDX_MODULE_r9 24 /* offsetof(struct tdx_module_output, r9) */ +#define TDX_MODULE_r10 32 /* offsetof(struct tdx_module_output, r10) */ +#define TDX_MODULE_r11 40 /* offsetof(struct tdx_module_output, r11) */ + +.globl __tdx_module_call +.type __tdx_module_call, @function +__tdx_module_call: + /* Set up stack frame */ + push %rbp + movq %rsp, %rbp + + /* Callee-saved, so preserve it */ + push %r12 + + /* + * Push output pointer to stack. + * After the operation, it will be fetched into R12 register. + */ + push %r9 + + /* Mangle function call ABI into TDCALL/SEAMCALL ABI: */ + /* Move Leaf ID to RAX */ + mov %rdi, %rax + /* Move input 4 to R9 */ + mov %r8, %r9 + /* Move input 3 to R8 */ + mov %rcx, %r8 + /* Move input 1 to RCX */ + mov %rsi, %rcx + /* Leave input param 2 in RDX */ + + tdcall + + /* + * Fetch output pointer from stack to R12 (It is used + * as temporary storage) + */ + pop %r12 + + /* + * Since this macro can be invoked with NULL as an output pointer, + * check if caller provided an output struct before storing output + * registers. + * + * Update output registers, even if the call failed (RAX != 0). + * Other registers may contain details of the failure. + */ + test %r12, %r12 + jz .Lno_output_struct + + /* Copy result registers to output struct: */ + movq %rcx, TDX_MODULE_rcx(%r12) + movq %rdx, TDX_MODULE_rdx(%r12) + movq %r8, TDX_MODULE_r8(%r12) + movq %r9, TDX_MODULE_r9(%r12) + movq %r10, TDX_MODULE_r10(%r12) + movq %r11, TDX_MODULE_r11(%r12) + +.Lno_output_struct: + /* Restore the state of R12 register */ + pop %r12 + + pop %rbp + ret + /* Disable executable stack */ .section .note.GNU-stack,"",%progbits diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index a45e2ceb6eda..bcd9cceb3372 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -183,3 +183,30 @@ uint64_t tdg_vp_vmcall_instruction_cpuid(uint32_t eax, uint32_t ecx, return ret; } + +uint64_t tdg_vp_info(uint64_t *rcx, uint64_t *rdx, + uint64_t *r8, uint64_t *r9, + uint64_t *r10, uint64_t *r11) +{ + uint64_t ret; + struct tdx_module_output out; + + memset(&out, 0, sizeof(struct tdx_module_output)); + + ret = __tdx_module_call(TDG_VP_INFO, 0, 0, 0, 0, &out); + + if (rcx) + *rcx = out.rcx; + if (rdx) + *rdx = out.rdx; + if (r8) + *r8 = out.r8; + if (r9) + *r9 = out.r9; + if (r10) + *r10 = out.r10; + if (r11) + *r11 = out.r11; + + return ret; +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c index c977223ff871..60b4504d1245 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c @@ -1146,6 +1146,150 @@ void verify_host_reading_private_mem(void) printf("\t ... PASSED\n"); } +/* + * Do a TDG.VP.INFO call from the guest + */ +void guest_tdcall_vp_info(void) +{ + uint64_t err; + uint64_t rcx, rdx, r8, r9, r10, r11; + + err = tdg_vp_info(&rcx, &rdx, &r8, &r9, &r10, &r11); + if (err) + tdx_test_fatal(err); + + /* return values to user space host */ + err = tdx_test_report_64bit_to_user_space(rcx); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_64bit_to_user_space(rdx); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_64bit_to_user_space(r8); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_64bit_to_user_space(r9); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_64bit_to_user_space(r10); + if (err) + tdx_test_fatal(err); + + err = tdx_test_report_64bit_to_user_space(r11); + if (err) + tdx_test_fatal(err); + + tdx_test_success(); +} + +/* + * TDG.VP.INFO call from the guest. Verify the right values are returned + */ +void verify_tdcall_vp_info(void) +{ + const int num_vcpus = 2; + struct kvm_vcpu *vcpus[num_vcpus]; + struct kvm_vm *vm; + + uint64_t rcx, rdx, r8, r9, r10, r11; + uint32_t ret_num_vcpus, ret_max_vcpus; + uint64_t attributes; + uint32_t i; + const struct kvm_cpuid_entry2 *cpuid_entry; + int max_pa = -1; + + vm = td_create(); + +#define TDX_TDPARAM_ATTR_SEPT_VE_DISABLE_BIT (1UL << 28) +#define TDX_TDPARAM_ATTR_PKS_BIT (1UL << 30) + /* Setting attributes parameter used by TDH.MNG.INIT to 0x50000000 */ + attributes = TDX_TDPARAM_ATTR_SEPT_VE_DISABLE_BIT | + TDX_TDPARAM_ATTR_PKS_BIT; + + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, attributes); + + for (i = 0; i < num_vcpus; i++) + vcpus[i] = td_vcpu_add(vm, i, guest_tdcall_vp_info); + + td_finalize(vm); + + printf("Verifying TDG.VP.INFO call:\n"); + + /* Get KVM CPUIDs for reference */ + cpuid_entry = get_cpuid_entry(kvm_get_supported_cpuid(), 0x80000008, 0); + TEST_ASSERT(cpuid_entry, "CPUID entry missing\n"); + max_pa = cpuid_entry->eax & 0xff; + + for (i = 0; i < num_vcpus; i++) { + struct kvm_vcpu *vcpu = vcpus[i]; + + /* Wait for guest to report rcx value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + rcx = tdx_test_read_64bit_report_from_guest(vcpu); + + /* Wait for guest to report rdx value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + rdx = tdx_test_read_64bit_report_from_guest(vcpu); + + /* Wait for guest to report r8 value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + r8 = tdx_test_read_64bit_report_from_guest(vcpu); + + /* Wait for guest to report r9 value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + r9 = tdx_test_read_64bit_report_from_guest(vcpu); + + /* Wait for guest to report r10 value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + r10 = tdx_test_read_64bit_report_from_guest(vcpu); + + /* Wait for guest to report r11 value */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + r11 = tdx_test_read_64bit_report_from_guest(vcpu); + + ret_num_vcpus = r8 & 0xFFFFFFFF; + ret_max_vcpus = (r8 >> 32) & 0xFFFFFFFF; + + /* first bits 5:0 of rcx represent the GPAW */ + TEST_ASSERT_EQ(rcx & 0x3F, max_pa); + /* next 63:6 bits of rcx is reserved and must be 0 */ + TEST_ASSERT_EQ(rcx >> 6, 0); + TEST_ASSERT_EQ(rdx, attributes); + TEST_ASSERT_EQ(ret_num_vcpus, num_vcpus); + TEST_ASSERT_EQ(ret_max_vcpus, 512); + /* VCPU_INDEX = i */ + TEST_ASSERT_EQ(r9, i); + /* + * verify reserved bits are 0 + * r10 bit 0 (SYS_RD) indicates that the TDG.SYS.RD/RDM/RDALL + * functions are available and can be either 0 or 1. + */ + TEST_ASSERT_EQ(r10 & ~1, 0); + TEST_ASSERT_EQ(r11, 0); + + /* Wait for guest to complete execution */ + td_vcpu_run(vcpu); + + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + printf("\t ... Guest completed run on VCPU=%u\n", i); + } + + kvm_vm_free(vm); + printf("\t ... PASSED\n"); +} + int main(int argc, char **argv) { setbuf(stdout, NULL); @@ -1169,6 +1313,7 @@ int main(int argc, char **argv) run_in_new_process(&verify_mmio_writes); run_in_new_process(&verify_td_cpuid_tdcall); run_in_new_process(&verify_host_reading_private_mem); + run_in_new_process(&verify_tdcall_vp_info); return 0; } From patchwork Tue Dec 12 20:46:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177572 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994000vqy; Tue, 12 Dec 2023 12:49:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IFqFo1Z9oy+XkjX2eYclLc2nuTzdAgUlnq9PnJOX+mV51svjKECBbgAQZEuGMRM8IsEQWOl X-Received: by 2002:a05:6a00:4601:b0:6ce:2731:47b4 with SMTP id ko1-20020a056a00460100b006ce273147b4mr8227798pfb.20.1702414154800; Tue, 12 Dec 2023 12:49:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414154; cv=none; d=google.com; s=arc-20160816; b=Q4cxDybGrjlzWMh7mim+ZhD47zJFcuNnrB9N8Giv9h1bxTGQA+LFIwtzv8A0IK+HxP GTmHJMVZesGKgFOSp83Doa0wtlDAYbVHhiIl2tTEFziECiUNgAPZjCZ1DDdiXMlvFHA2 bD96PUSwHEa0yIJum3Mq/QqsY8kpFCgu689BoiMyQ+qG33lp8eIc32xgZAYvHtvjBAs3 PVCW77TO1FhDQN/u0nHO9UGchcDNDhaOobFRCx3tQVZgaixiCSe1XY+QKap5zIJ/AOi6 /T6o9raahpjEUVWkcYD+Bf134q4YWUVVurUc121MSnSaBDpT6B717OT5vjuGJUyVty0w 6gUA== 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=H/Dndi1239gwCc2M1UpDzeOelUNamAjzhLbQUW3a080=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=i5lPlHJWpijwFBGA/ogk3KgmZ26bQn+EkrUWhJFJnugNGddRhWTPFEYkfpjzQfcPFu Co9XsmdrWRcnSWq4d2sCRJXNwIU1oONoqd0NjukKIoGIJxc5Tu7mkOvrcFXoDoTJ93YR nS49kWb09vDsMEc3qhb5F/ky06oFJB0nfUPehqtI6F+DZL8xfDpvjafE1IZkiIeJ2r31 oaD3Yus11Vmci1SzaOE8Oy+DZ0qstnZMT0eq7Lx6XOF9ZIogjSfzbR0eVHGmlYUUqr49 +Fe2DuIfMcV8JLVjhzR0d9/d9Cr+WmOvLxLTWGhCbn+xO6oHTfH3QGWDQ3Z9hTRpWW2B YlMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="o/fM5xQl"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id v29-20020a63481d000000b005bde6fae09asi8069661pga.135.2023.12.12.12.49.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="o/fM5xQl"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 530048026C19; Tue, 12 Dec 2023 12:49:11 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377449AbjLLUs4 (ORCPT + 99 others); Tue, 12 Dec 2023 15:48:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377412AbjLLUsb (ORCPT ); Tue, 12 Dec 2023 15:48:31 -0500 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 C643C19B0 for ; Tue, 12 Dec 2023 12:47:39 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d34b1ea914so696845ad.1 for ; Tue, 12 Dec 2023 12:47:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414058; x=1703018858; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=H/Dndi1239gwCc2M1UpDzeOelUNamAjzhLbQUW3a080=; b=o/fM5xQldEVi6jYSmRB3HNLhkO20sFzxDYwBB6jRRV9ks06+vn1Mj6hI/bRaATejSx u3Yz3U2QBDALYrdRq4SYsNXfvgad3JXfQ7EJ+W0QEPJmmxJFWunv9QQc9QgflHWs9yVI r1BPABaarVd7HbBt3GMhF26Ov4ykK3Yd9UCH0bf3o6gCNAyudShLIR0SlXtUFwW2Kzbx EeoxoqYlb4b3e11AshzO2ss94Xg41M5a3gsFZbauVz7zV7NvyonRu+7aVXqI/84jHf98 HyJdH3xHxK0pMaj2nwdD5g0foLm1DdVrhs9+tn//7n1FvFKtBuR9fRJfjLe2Wigv0RwQ 20uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414058; x=1703018858; 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=H/Dndi1239gwCc2M1UpDzeOelUNamAjzhLbQUW3a080=; b=nevMBuTrM9J+xgZAgN6JF2U/RTukuccC/b9oL9ssFoOqKaJCuPptQ4QXoNQD7EFeYi vIwORQo5PzrhOqUw1/l1Sw2uH2fqAWHPpKaCLxHox6wStPioCJFMLaDicLt3bqmJquHl jpNINh9HYZq3wyrhIRdSo1U5r5RZCVyJ2532v1q6NhLCLwH43LrqiPo0212VlJoB8S4a XUH+qxPCCPt+Y6g5JWuC1dMlRGzx+zv2J0lg7qfnPcMbkEPx+x9WGu4QgKgs403v6LEM OONjQq9lOrrH06fw/0CU7qAcYRo1blnRcBE1v9/X85mNmggy/mCiMPyQkqNdsbno35sF db5w== X-Gm-Message-State: AOJu0YweFsYJwVxvspkLNSJu7WTpChiR/ClIFRS63llbvarF1gZ4B2YZ j44bdSnJPxoCNT9VfS1o3kJZxl/eEg== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:903:2292:b0:1d0:bf4c:163d with SMTP id b18-20020a170903229200b001d0bf4c163dmr56827plh.2.1702414058496; Tue, 12 Dec 2023 12:47:38 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:37 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-23-sagis@google.com> Subject: [RFC PATCH v5 22/29] KVM: selftests: Add functions to allow mapping as shared From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:49:11 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110625169849830 X-GMAIL-MSGID: 1785110625169849830 From: Ackerley Tng Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- .../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 b353617fcdd1..efd7ae8abb20 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -574,6 +574,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); @@ -1034,6 +1036,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 4f1ae0f1eef0..28780fa1f0f2 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1573,6 +1573,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 566d82829da4..aa2a57ddb8d3 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -190,7 +190,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; @@ -235,17 +236,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) { From patchwork Tue Dec 12 20:46:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177577 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994201vqy; Tue, 12 Dec 2023 12:49:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJWsMPOuASRi+0pS+tILssnnwrN73DGmi4Xdo6K1XHIXh566IXxQhbJ0wBPoarW275C2U3 X-Received: by 2002:a05:6a20:7344:b0:18f:97c:4f46 with SMTP id v4-20020a056a20734400b0018f097c4f46mr4270653pzc.82.1702414191700; Tue, 12 Dec 2023 12:49:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414191; cv=none; d=google.com; s=arc-20160816; b=0EAxIL0HbQmX+WX+2k+iX8rF/cdsEMMzd7fqOgweGcmBIcjoRImtT98ICzNMRG8r63 UpttIdqSE/NC0N27KLumrb1ZQsQ5xEKgTjwquqKYgtM5doblogTN5xtLv4wgn/iPSF/0 /EhCPlAdq58hMe8MFg4glW6IxbIOZA0C7bPmMFWnch0U7gYNzutNQH7L+Kxwvo9nN+TW SxTEFc43BoyQkFmCdaRMacz7dhl24VQ/Vg3tnHqoDsx4xi8ChwH5m+ba4x2Am3FZzPH+ GkaiNg4lt0smDi4vu3N+ZUsExXsmHFMsB09N4VTjwjuwm8PpCq0/Su6GQVuCLCR6Xb1G rSGA== 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=VQXzt/ExaOzeKgYGP3AjD06mEvm7Zdsq1Ks/qnyB1N0=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=XQibtVX+moBF+d1pXWh58WdrJ07VMV4FvNnSPpbwnU/+aUgEBNNju4vK16aDSNl+8X xXiQOLMIS05IPJ+omoUyrWX6R3NrlCF4zAnktVWdGC6Gn+cvyJPRP8ETvwcoyciW4dBv 6vIGbH99MxceFwgx7ALZ0HIwdqrbt1T5bz1Ft1G4QSMbeDoDga4lNLR5zQIJHSdqJqmK EZ3vflMCp61EYkhRITMfcnHgrrB3yra7/IJwPFmAMVrx1ASqmpPbfPSnRSVfmH2IDzk/ YY7lsm4fCwbenJdPvBbYoTEbncoC9sb4KIz1RwY/usnq1fznb7RZul+W5s3hamWSGlFW kCBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="HQ/eNcid"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id p11-20020a17090ad30b00b002887c0800desi8406954pju.68.2023.12.12.12.49.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="HQ/eNcid"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5C3F68107557; Tue, 12 Dec 2023 12:49:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377408AbjLLUtd (ORCPT + 99 others); Tue, 12 Dec 2023 15:49:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377409AbjLLUsz (ORCPT ); Tue, 12 Dec 2023 15:48:55 -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 BB0A91BCA for ; Tue, 12 Dec 2023 12:47:41 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-db547d41413so4876941276.0 for ; Tue, 12 Dec 2023 12:47:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414060; x=1703018860; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=VQXzt/ExaOzeKgYGP3AjD06mEvm7Zdsq1Ks/qnyB1N0=; b=HQ/eNcidkwys+1AmUQxoM1Ve7lFlt5a5v5KsqCnB6zqtPglGMHsnjmom+ar6fwQxYf 5ePQMMhY92u2UiXXsYbpZvWNvQIBUsZZwBXKfmJPQ3qwt3kaR0MTHOdcD0+nGc05zfEa 7j/A1Qis60CViZ93G1SOIIi1bnAYp74Lp05AonFs8QvJRnXOmGDcPOgHZVvokWZ8DU+3 NEo1qiwaNWTNr6s5ZCj70CgPLuKFgbrjkSVFOWEczlq8Gpnur4A3TdRokvoHqzrjEr0n nHQVQjoMqyrluUTSQLX+iQdaLtdT4SQXSQXCXQ22h8nNIgghcvGen8w6sBFmEK+SsHzD bwfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414060; x=1703018860; 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=VQXzt/ExaOzeKgYGP3AjD06mEvm7Zdsq1Ks/qnyB1N0=; b=ZFL1vQ+YnBYd3EOsHOzxwcw0qPtAHYydUVb1Np2x779ZFtEeBP18qtWj73dC9MKATm pE1QdUtYlyfEn3QMWYdyrtL+tokvVWTAORobIRl046O69Urv+dvnPQQIZxYVBfJ1pq6O 5lprW30im4imxQnE1ImYK5we5wiix4ekc2+AN5PFrsqtrpLEbutq+3fd3x/G/u4u5IJn ELHT0ydz6LxXUaPA+omh53cnvQJNqDkdU3YNPY3dJ0FBY6aIbjfXJSInZc022GqlsVOz a4xlY98kRolOTn4xrgp68Qg8sAUnVYOJSuUCm9XvnqAwzapAOgYYySCIcB0IAmLjr5D2 qIWw== X-Gm-Message-State: AOJu0Yz3cqzJ30eqWOjirlZ44JRvDc8HqtUIXAGWHxTav6u35YE9JjkZ i6joWkawoG/c/k7BHF/jgxn7aSYkdA== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a25:8150:0:b0:dbc:66ea:455 with SMTP id j16-20020a258150000000b00dbc66ea0455mr88876ybm.6.1702414060312; Tue, 12 Dec 2023 12:47:40 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:38 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-24-sagis@google.com> Subject: [RFC PATCH v5 23/29] KVM: selftests: TDX: Add shared memory test From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:49:48 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110663623607363 X-GMAIL-MSGID: 1785110663623607363 From: Ryan Afranji Adds a test that sets up shared memory between the host and guest. Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar Signed-off-by: Ackerley Tng --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/tdx/tdx.h | 2 + .../kvm/include/x86_64/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 26 ++++ .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 25 ++++ .../kvm/x86_64/tdx_shared_mem_test.c | 135 ++++++++++++++++++ 6 files changed, 191 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/tdx_shared_mem_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 80d4a50eeb9f..8c0a6b395ee5 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -156,6 +156,7 @@ TEST_GEN_PROGS_x86_64 += steal_time TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test TEST_GEN_PROGS_x86_64 += system_counter_offset_test TEST_GEN_PROGS_x86_64 += x86_64/tdx_vm_tests +TEST_GEN_PROGS_x86_64 += x86_64/tdx_shared_mem_test # Compiled outputs used by test targets TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index 6b176de1e795..db4cc62abb5d 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -8,6 +8,7 @@ #define TDG_VP_INFO 1 #define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 +#define TDG_VP_VMCALL_MAP_GPA 0x10001 #define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 #define TDG_VP_VMCALL_INSTRUCTION_CPUID 10 @@ -36,5 +37,6 @@ uint64_t tdg_vp_vmcall_instruction_cpuid(uint32_t eax, uint32_t ecx, uint64_t tdg_vp_info(uint64_t *rcx, uint64_t *rdx, uint64_t *r8, uint64_t *r9, uint64_t *r10, uint64_t *r11); +uint64_t tdg_vp_vmcall_map_gpa(uint64_t address, uint64_t size, uint64_t *data_out); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h index 32dd6b8fda46..3e850ecb85a6 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h @@ -13,5 +13,7 @@ void td_initialize(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t attributes); void td_finalize(struct kvm_vm *vm); void td_vcpu_run(struct kvm_vcpu *vcpu); +void handle_memory_conversion(struct kvm_vm *vm, uint64_t gpa, uint64_t size, + bool shared_to_private); #endif // SELFTESTS_TDX_KVM_UTIL_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index bcd9cceb3372..061a5c0bef34 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -4,9 +4,11 @@ #include "tdx/tdcall.h" #include "tdx/tdx.h" +#include "tdx/tdx_util.h" void handle_userspace_tdg_vp_vmcall_exit(struct kvm_vcpu *vcpu) { + struct kvm_vm *vm = vcpu->vm; struct kvm_tdx_vmcall *vmcall_info = &vcpu->run->tdx.u.vmcall; uint64_t vmcall_subfunction = vmcall_info->subfunction; @@ -20,6 +22,14 @@ void handle_userspace_tdg_vp_vmcall_exit(struct kvm_vcpu *vcpu) vcpu->run->system_event.data[2] = vmcall_info->in_r13; vmcall_info->status_code = 0; break; + case TDG_VP_VMCALL_MAP_GPA: + uint64_t gpa = vmcall_info->in_r12 & ~vm->arch.s_bit; + bool shared_to_private = !(vm->arch.s_bit & + vmcall_info->in_r12); + handle_memory_conversion(vm, gpa, vmcall_info->in_r13, + shared_to_private); + vmcall_info->status_code = 0; + break; default: TEST_FAIL("TD VMCALL subfunction %lu is unsupported.\n", vmcall_subfunction); @@ -210,3 +220,19 @@ uint64_t tdg_vp_info(uint64_t *rcx, uint64_t *rdx, return ret; } + +uint64_t tdg_vp_vmcall_map_gpa(uint64_t address, uint64_t size, uint64_t *data_out) +{ + uint64_t ret; + struct tdx_hypercall_args args = { + .r11 = TDG_VP_VMCALL_MAP_GPA, + .r12 = address, + .r13 = size + }; + + ret = __tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT); + + if (data_out) + *data_out = args.r11; + return ret; +} diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c index d745bb6287c1..92fa6bd13229 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c @@ -531,3 +531,28 @@ void td_vcpu_run(struct kvm_vcpu *vcpu) handle_userspace_tdg_vp_vmcall_exit(vcpu); } } + +/** + * Handle conversion of memory with @size beginning @gpa for @vm. Set + * @shared_to_private to true for shared to private conversions and false + * otherwise. + * + * Since this is just for selftests, we will just keep both pieces of backing + * memory allocated and not deallocate/allocate memory; we'll just do the + * minimum of calling KVM_MEMORY_ENCRYPT_REG_REGION and + * KVM_MEMORY_ENCRYPT_UNREG_REGION. + */ +void handle_memory_conversion(struct kvm_vm *vm, uint64_t gpa, uint64_t size, + bool shared_to_private) +{ + struct kvm_memory_attributes range; + + range.address = gpa; + range.size = size; + range.attributes = shared_to_private ? KVM_MEMORY_ATTRIBUTE_PRIVATE : 0; + range.flags = 0; + + printf("\t ... calling KVM_SET_MEMORY_ATTRIBUTES ioctl with gpa=%#lx, size=%#lx, attributes=%#llx\n", gpa, size, range.attributes); + + vm_ioctl(vm, KVM_SET_MEMORY_ATTRIBUTES, &range); +} diff --git a/tools/testing/selftests/kvm/x86_64/tdx_shared_mem_test.c b/tools/testing/selftests/kvm/x86_64/tdx_shared_mem_test.c new file mode 100644 index 000000000000..ba6bdc470270 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/tdx_shared_mem_test.c @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include + +#include "kvm_util_base.h" +#include "processor.h" +#include "tdx/tdcall.h" +#include "tdx/tdx.h" +#include "tdx/tdx_util.h" +#include "tdx/test_util.h" +#include "test_util.h" + +#define TDX_SHARED_MEM_TEST_PRIVATE_GVA (0x80000000) +#define TDX_SHARED_MEM_TEST_VADDR_SHARED_MASK BIT_ULL(30) +#define TDX_SHARED_MEM_TEST_SHARED_GVA \ + (TDX_SHARED_MEM_TEST_PRIVATE_GVA | \ + TDX_SHARED_MEM_TEST_VADDR_SHARED_MASK) + +#define TDX_SHARED_MEM_TEST_GUEST_WRITE_VALUE (0xcafecafe) +#define TDX_SHARED_MEM_TEST_HOST_WRITE_VALUE (0xabcdabcd) + +#define TDX_SHARED_MEM_TEST_INFO_PORT 0x87 + +/* + * Shared variables between guest and host + */ +static uint64_t test_mem_private_gpa; +static uint64_t test_mem_shared_gpa; + +void guest_shared_mem(void) +{ + uint32_t *test_mem_shared_gva = + (uint32_t *)TDX_SHARED_MEM_TEST_SHARED_GVA; + + uint64_t placeholder; + uint64_t ret; + + /* Map gpa as shared */ + ret = tdg_vp_vmcall_map_gpa(test_mem_shared_gpa, PAGE_SIZE, + &placeholder); + if (ret) + tdx_test_fatal_with_data(ret, __LINE__); + + *test_mem_shared_gva = TDX_SHARED_MEM_TEST_GUEST_WRITE_VALUE; + + /* Exit so host can read shared value */ + ret = tdg_vp_vmcall_instruction_io(TDX_SHARED_MEM_TEST_INFO_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + &placeholder); + if (ret) + tdx_test_fatal_with_data(ret, __LINE__); + + /* Read value written by host and send it back out for verification */ + ret = tdg_vp_vmcall_instruction_io(TDX_SHARED_MEM_TEST_INFO_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, + (uint64_t *)test_mem_shared_gva); + if (ret) + tdx_test_fatal_with_data(ret, __LINE__); +} + +int verify_shared_mem(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm_vaddr_t test_mem_private_gva; + uint32_t *test_mem_hva; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_shared_mem); + + /* + * Set up shared memory page for testing by first allocating as private + * and then mapping the same GPA again as shared. This way, the TD does + * not have to remap its page tables at runtime. + */ + test_mem_private_gva = vm_vaddr_alloc(vm, vm->page_size, + TDX_SHARED_MEM_TEST_PRIVATE_GVA); + TEST_ASSERT_EQ(test_mem_private_gva, TDX_SHARED_MEM_TEST_PRIVATE_GVA); + + test_mem_hva = addr_gva2hva(vm, test_mem_private_gva); + TEST_ASSERT(test_mem_hva != NULL, + "Guest address not found in guest memory regions\n"); + + test_mem_private_gpa = addr_gva2gpa(vm, test_mem_private_gva); + virt_pg_map_shared(vm, TDX_SHARED_MEM_TEST_SHARED_GVA, + test_mem_private_gpa); + + test_mem_shared_gpa = test_mem_private_gpa | BIT_ULL(vm->pa_bits - 1); + sync_global_to_guest(vm, test_mem_private_gpa); + sync_global_to_guest(vm, test_mem_shared_gpa); + + td_finalize(vm); + + printf("Verifying shared memory accesses for TDX\n"); + + /* Begin guest execution; guest writes to shared memory. */ + printf("\t ... Starting guest execution\n"); + + /* Handle map gpa as shared */ + td_vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + + td_vcpu_run(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_SHARED_MEM_TEST_INFO_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + TEST_ASSERT_EQ(*test_mem_hva, TDX_SHARED_MEM_TEST_GUEST_WRITE_VALUE); + + *test_mem_hva = TDX_SHARED_MEM_TEST_HOST_WRITE_VALUE; + td_vcpu_run(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_SHARED_MEM_TEST_INFO_PORT, 4, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + TEST_ASSERT_EQ( + *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset), + TDX_SHARED_MEM_TEST_HOST_WRITE_VALUE); + + printf("\t ... PASSED\n"); + + kvm_vm_free(vm); + + return 0; +} + +int main(int argc, char **argv) +{ + if (!is_tdx_enabled()) { + printf("TDX is not supported by the KVM\n" + "Skipping the TDX tests.\n"); + return 0; + } + + return verify_shared_mem(); +} From patchwork Tue Dec 12 20:46:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177579 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994215vqy; Tue, 12 Dec 2023 12:49:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IEBIU7wUkZVARmtT+RN3FoEgkUaiTWBYo30GFWJNkE2Tkx+hLNoBFEdUxqRphD28FH/LuNT X-Received: by 2002:a05:6358:a086:b0:170:e924:1f1e with SMTP id u6-20020a056358a08600b00170e9241f1emr2362653rwn.35.1702414194262; Tue, 12 Dec 2023 12:49:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414194; cv=none; d=google.com; s=arc-20160816; b=N8SvlsJWdO673uaT/yM/Apm4oFtBTF6nenEUkDb0jQXJRDO+xws5H76x7SRxpbmyWE Euwv9gG6VBAOhmb/m4EIA4W1vTdvbjlkRxthITiXMUnn6XzPMk+Zt3xBNbWXBfYoFJmg Cjli8tz7pOJBirUckPBkZlA4j8uxDXaLzyNsrHDGa65KQcJQcCNxVAUN3k2kGIwQKz6M zJE8fHadSzNzR39j7mBVtNbR1qse/GWrsFc/N5mG0f7jOXbXKOiBc1TXUiSLB4DAyq1n T9vpBMxUBIP85Q4uWAzwHbBvcEYqRj9MeF9LlC1x6f/zTw/zVt/YMNIAaL6gY2R0VfGh VQvw== 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=QZdQqVk39iJlhBpyUnh6sf+6Hw3ATYVfrr1vi3AMJYE=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=ezNWk6YCfd9tp3YTGN3dkxzUQRe6LE6ZgbF+6pIS66fjVuxT+oYt1KdItwliTm2y8L FI9uQUOtFVfDyCvF0lfDgPeVcQA05jEQbhrFzravzx4JO1LXwNoCuokxoKH8CwDvKpkR SAwa2glKGbQ4KvCy1m7lZigyMibdaJops0OH9K7HjWiJM5AIPLUUHGtJQn66I4QYPERk GZisawOFbdpgIvMQMrSNDZBGpN/cP/UaajyIjns7Vrld9O0pWSRsP7aRKrl1c2lhJNfS D+i0pxDh0Xtn4o5F/GjfZsCqyCDOlDUCdNmhlbtLvOfnyNtCV6rU0JtKH6Glo46cE0uV aYmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mtpFjSLq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id fk3-20020a056a003a8300b006cbee55fe7csi8233482pfb.379.2023.12.12.12.49.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mtpFjSLq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id C5C4E8107559; Tue, 12 Dec 2023 12:49:51 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377329AbjLLUtY (ORCPT + 99 others); Tue, 12 Dec 2023 15:49:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235189AbjLLUso (ORCPT ); Tue, 12 Dec 2023 15:48:44 -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 5515C1BDD for ; Tue, 12 Dec 2023 12:47:43 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-daee86e2d70so6839872276.0 for ; Tue, 12 Dec 2023 12:47:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414062; x=1703018862; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QZdQqVk39iJlhBpyUnh6sf+6Hw3ATYVfrr1vi3AMJYE=; b=mtpFjSLqKP9FQ+2PgecqAf3DHWIAPOQF2s/DwxfYT7RTWLBqTk2vuzBQ7RV6Uwi0nm JzwTkNEFOCIZvJoS8qYa7VUyAhy88hyg12THsWeP/p52LvmZz7znMXUqM7ORnKfnZ62m UxPdO3//KctHbVTIBOmMvor22gRA2wOuCsJHc0DJ0uc37RgJ0/wiyxfGow8HT4CSjCxA DkTmTcu5s4Ts+MbCKFPoOFZZfW9+U62jTkdktM0RQuPdn8wdtdl55LSlhotMvs/lQ07e CAY7K96Du6ZcOkqdlcW/+oLDosm9PKfmaOBPzTjm2+HIoUHqB1HaXmLruGDzDg5x2SVM grFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414062; x=1703018862; 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=QZdQqVk39iJlhBpyUnh6sf+6Hw3ATYVfrr1vi3AMJYE=; b=Op51A+S4EH+MXAxYUoXMR4nGA6uh3uhzcIJDlIXz7fTPLSZtblfrCQsTLfmn4TiFK/ GCDMxxQBJg4ZEYP8aD5oT9ig0oqHy5hS54R+oiOiPiCYSRpj73ZEgcjx5pJWlpEv8Ggu UQm+feO5mPhDBSM4A4usZ2d9sXMjwG/v1+7zBz1/ysIrsK1j+pSrGA/gTnx1+i37P5Ww yPZi8BhxHmhYFmbrYVrVwKy92ELrAnNCXsxG2OvGxI1APONHpnZ3+r6XZ0aYJazt+yfv miBmb4q2dfmJGEYv8hgyVV9ucd3MhEmFHQbQ5BAuxSwvQ4Nr0N4JGL95+LdJspgOGfYB ibZA== X-Gm-Message-State: AOJu0YxD9TKqcrKip1ATpyY8FSqoEqSTNlo8bE2XXJpm/NkV2/cLX9sk GvTeEdqOb1ycT+Vh+/D3yhPtmuNd2w== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a25:9e90:0:b0:db3:fa34:50b0 with SMTP id p16-20020a259e90000000b00db3fa3450b0mr51580ybq.4.1702414061971; Tue, 12 Dec 2023 12:47:41 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:39 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-25-sagis@google.com> Subject: [RFC PATCH v5 24/29] KVM: selftests: Expose _vm_vaddr_alloc From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:49:51 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110666450446788 X-GMAIL-MSGID: 1785110666450446788 From: Ackerley Tng vm_vaddr_alloc always allocates memory in memslot 0. This allows users of this function to choose which memslot to allocate virtual memory in. Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- tools/testing/selftests/kvm/include/kvm_util_base.h | 3 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index efd7ae8abb20..5dbebf5cfd07 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -561,6 +561,9 @@ 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); void vm_populate_vaddr_bitmap(struct kvm_vm *vm); vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); +vm_vaddr_t ____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); vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, enum kvm_mem_region_type type); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 28780fa1f0f2..d024abc5379c 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1410,9 +1410,9 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, * a unique set of pages, with the minimum real allocation being at least * a page. */ -static vm_vaddr_t ____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) +vm_vaddr_t ____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); From patchwork Tue Dec 12 20:46:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177578 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994208vqy; Tue, 12 Dec 2023 12:49:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IFx9HNqIdLVinTmqMda6oRFzzF6jqeZ+odaDZhYKSAqIpEgwZo47GSgydjQe1fCLzOg+Bz6 X-Received: by 2002:a17:90b:954:b0:286:6cc1:8667 with SMTP id dw20-20020a17090b095400b002866cc18667mr3270335pjb.76.1702414192674; Tue, 12 Dec 2023 12:49:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414192; cv=none; d=google.com; s=arc-20160816; b=ScARxoClxkDoCS/9kLQwUxSY10d0SfzL1pW8wcVFEg3qsku8Y1/ItZYOsXHtyVv5gb sH+QsDSVD43uCQjC1fQ7p1ZHhaeBcrwPqQDtGqS4hpQatYslG/NKbCmxJ56zFEf/kSkB /b5Kcprza9RQjsCC8ahPFZX2XfpfqJnTF5c8vrHLZ2h2Y1le0p+ziKUSaEDvfr5YWqBG rJZx2yTZFa5XGu3Mi5aQjfh1Px2Dmd1YMviUa2hu+poGDBoJWFjd0Uen2z/GaKud6jud qd6pLRKuMgpAitESxlFIS2DA4l5aKw0C68PiWBDfOnvIbph75kt9eBHBuy4HClTdaG3D Yl8Q== 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=spishPpmYPggI3jrHmifejwbO19KNsurBr8OcADllgg=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=uAmQ7zyH123aE5yeK9iYEEE9VylnA5FfXw/oErvVkAk/IGR35z9QMbnU+2fUT6FzXm OUU1HNn8rPzObsUKvlVsK/LLDHmwZOFsAOZRvqFAcf6BOJkzEOF8gI8VKHnqFCe9Qh0s pyYAUGjK4qtJO4MWtOhSIA1EIWW1BBlbmTuzrDRB5Nc11NZw5tsgsiSN0hHdZ3UK4fsE hzuvBCoxdiwlTehhiP+t+lQDq2iuVuwmDGEN0e4QRSFGl3gJRxyzBe9nKsLRcgdBn0wM l+eboFuvrcjsbuiBTI1M5LzaAYS+qGjgUwPpkf2agZJCXJ92hDTjdd+dehqAxLVwaTVH 5Mfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AEo3KYgZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id b14-20020a17090a7ace00b002861c962e15si8375696pjl.15.2023.12.12.12.49.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AEo3KYgZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 3812D8107556; Tue, 12 Dec 2023 12:49:50 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377398AbjLLUt1 (ORCPT + 99 others); Tue, 12 Dec 2023 15:49:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377391AbjLLUsw (ORCPT ); Tue, 12 Dec 2023 15:48:52 -0500 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 C1C271BEF for ; Tue, 12 Dec 2023 12:47:44 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1d08383e566so54491525ad.2 for ; Tue, 12 Dec 2023 12:47:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414063; x=1703018863; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=spishPpmYPggI3jrHmifejwbO19KNsurBr8OcADllgg=; b=AEo3KYgZ5i4SDTChjzjNMVNy33d2JI1HsAM93C7g2gSTWDWPhwfbWiB8PxD5l9Kw0W aUryWzAf7iYF+PLR5rzIWHfzcC/XYF6woNz6CJTUWVcpwa7HSwujY5Zxr50q/Xm82vUb Ub60azWqR4PIeT08pUtRasltsapmhsVQnMkZb1BpktQodsAbtDlCS/CkW4QlkeMKzuij UClgkq1wZfp0hsgR2MiNVBofEGKLAIyNV7u7xSku5HIzACXkNTIII2DVTaUmZEh0zOuh 7J+491qVYITd/vnf23XhOAz1S9bNyczCyinHmdrSsL4dbS2/NB++8/9nzM02lrWk71xR OOeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414063; x=1703018863; 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=spishPpmYPggI3jrHmifejwbO19KNsurBr8OcADllgg=; b=jq3IRQmBlBlRMG083VzOkqq3fpqkLUmdL/EdJEFSZEdGqGGECfGYoLFg2rzgcrQDy/ HH1+CCr3kSZ9i5ozg5QjSjYTGEHXgGMPKtIj1Egn6sioEdFSW99evn3b+pRBDdZW/VCM ensPE1meO0UmTMvM0p+RnAivI15t1RYB2pVNjP/H+E/G604o22KCUJvIfBjjvWDwBvSe BFlKMyiKyRkfN80gMYqkWZGlbp7Az8bS1GXOyKb500LmQkCkPhaOZETFQVvaAEIGJSUK X9jBqFD4stlsl6T0WxerJBj3wnXFCg+01LLYF01jq3une9aq8Org52jTraGXxuzGOsA3 RPbA== X-Gm-Message-State: AOJu0YxM+CNi2SvXrElAHKOtCx5jtvSC+G3CHaFf6JsiZaRKAFRTjWHc HN0b+SW4DYRAEW3wpdbh08fP5Yy27g== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:903:234a:b0:1d0:6638:b9da with SMTP id c10-20020a170903234a00b001d06638b9damr52616plh.8.1702414063541; Tue, 12 Dec 2023 12:47:43 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:40 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-26-sagis@google.com> Subject: [RFC PATCH v5 25/29] KVM: selftests: TDX: Add support for TDG.MEM.PAGE.ACCEPT From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:49:50 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110664742469155 X-GMAIL-MSGID: 1785110664742469155 From: Ackerley Tng Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h | 2 ++ tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index db4cc62abb5d..b71bcea40b5c 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -6,6 +6,7 @@ #include "kvm_util_base.h" #define TDG_VP_INFO 1 +#define TDG_MEM_PAGE_ACCEPT 6 #define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 #define TDG_VP_VMCALL_MAP_GPA 0x10001 @@ -38,5 +39,6 @@ uint64_t tdg_vp_info(uint64_t *rcx, uint64_t *rdx, uint64_t *r8, uint64_t *r9, uint64_t *r10, uint64_t *r11); uint64_t tdg_vp_vmcall_map_gpa(uint64_t address, uint64_t size, uint64_t *data_out); +uint64_t tdg_mem_page_accept(uint64_t gpa, uint8_t level); #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index 061a5c0bef34..d8c4ab635c06 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -236,3 +236,8 @@ uint64_t tdg_vp_vmcall_map_gpa(uint64_t address, uint64_t size, uint64_t *data_o *data_out = args.r11; return ret; } + +uint64_t tdg_mem_page_accept(uint64_t gpa, uint8_t level) +{ + return __tdx_module_call(TDG_MEM_PAGE_ACCEPT, gpa | level, 0, 0, 0, NULL); +} From patchwork Tue Dec 12 20:46:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177581 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7995052vqy; Tue, 12 Dec 2023 12:51:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IHXTOqlmAiukfor4r913LpST1PzEo0W/2aiYl+IDgeSOtZDABYXWkxHAh/QPWumo94l4Vml X-Received: by 2002:a05:6359:1b02:b0:170:17ea:f4e0 with SMTP id up2-20020a0563591b0200b0017017eaf4e0mr5851049rwb.45.1702414313051; Tue, 12 Dec 2023 12:51:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414313; cv=none; d=google.com; s=arc-20160816; b=oDUy8iogfqM1P0QuvKc+5pr2E1kY36glmHe9QeZjQwMqAlbqghoMmtzQ705Gm0xVwt 3jy/pk3Brj5LiusIFhythaOZyR5usroS1hTs6rdlZgGTyfhx7LLOWWx9v3BtC/OWXY52 +gqQAZXRd49luWON1eCQAXZYC81AUNlMg/98MZDi2DuhWOBTdbdD0eQg8DM4Phkfqqk3 y3wd02Emlf2Zavttk+Nfax71+zwjSdivaPWF6pQwxkdWV5IcKZ7MQBElybWvUwV/Xj2a 5subDFllipBo4kmN3hnpRveOGf8uErOGf8LK4bfCBDLcB5AdwoqAkk0uyI+Sm9yzEUHH Aofg== 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=+IXZ/Kz3W3ByK8YMBqzR++sHVAa+56dFMX9xl5Pgrpw=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=1I/gA2lMG4+snPsBkImfFTV1cBhyQAS7AajvSgJzUHgwNcNO2QABaL5Ey+3g604cXR RsA1zL+lxSXdnzCUXIbFO6EQVJqSwlnB9CRg76leWvCrzlS3UthjFe9ygspP9OWN7Mkf 49i9nJ2Qs95fS2dDpKumCfdEfxJeluzLNyMukNFHDNfBaR61nXqrx2kSrlto8CrKZmFt 2uBaxxg2LYlpDAXnW+z76rmKjklikV6wmEpzzaaMb9QeEyUma3mk5uZ+3Z3Ma/doBV1m a12nPEjXbAfKlqlvvVArGR098ARPFWXoVfJxmW8dlcTtu+Ox367ZdELoe5VfzBM4rg7e 5Bsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=llnZxDvI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id a21-20020a056a001d1500b006d095146a77si3386517pfx.380.2023.12.12.12.51.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:51:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=llnZxDvI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 42C07809BE4F; Tue, 12 Dec 2023 12:51:49 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377407AbjLLUvk (ORCPT + 99 others); Tue, 12 Dec 2023 15:51:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235126AbjLLUv0 (ORCPT ); Tue, 12 Dec 2023 15:51:26 -0500 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 99C551BFC for ; Tue, 12 Dec 2023 12:47:46 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1d341b6945dso4253375ad.0 for ; Tue, 12 Dec 2023 12:47:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414065; x=1703018865; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+IXZ/Kz3W3ByK8YMBqzR++sHVAa+56dFMX9xl5Pgrpw=; b=llnZxDvI0hV3LkavGBpjWkB0VrsCb3nUAC3YQ8rNVzwUKOf2sWma5YGq2MEVO9yy02 zVbmoJwAwxem0JmXI/P80+1ZRdZ2Pneqotc2gt+95uAhdcxZyi3sukRPhQBIM53poJzn Exr0haV2PKVo/bQ+ydJTeZgnl96ileMcm7Hvg8M1aXpmSpFlUcsTQaNrVr47iAQ1ZwUH oroofogf8PmwTUV2acRuij0QVd0oHoomKRfxCbXxKpBUFXqGWInDUiSCKT9c7rncjbAd /9EUcmyeUQDPvQnTka1gpcU/i14m6WJHutOHf4tczp73vD/3qOci1qyRKCpaInX9SbtH 5k5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414065; x=1703018865; 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=+IXZ/Kz3W3ByK8YMBqzR++sHVAa+56dFMX9xl5Pgrpw=; b=e/H+h7m7jV0EezDHw4cnQkWhfH9X8gchKOzjCkR7AaU7carioZC6jYxdrnjV7NU/1Y 2pMSC3Kju6DAJfYkTlBfs8NxuA93pYxjUwdfHNZ3qy/T7Hgx+ciXZFELq22neenB/POJ weLQnercr1F9Q0OH9ehVYo9TA+4uDE88Bh8OghDCwUTcYE6baK+1A9oZ2cgSt8V+lXWI G9hBALmekMU8Yz9da+R6qZvdlG3fA11E78nTsP4txntZNvlFW/AlLi5dVNmDK39I7Dn2 2qze5Og6kh6XRLOmxB/EI/IQreeUovtkQ7i86+rX9uo6woDivI4kLzXyvJjmz/4L6nTW cWzA== X-Gm-Message-State: AOJu0YzCmJcHAlgejEG21L4Xk1AAS6RKchyhl/h707kUWHGHtZ0oqJIa jg4xQVUOtyt6Mu7id2ZfRpzdYujD6w== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:903:22cd:b0:1d0:727b:8355 with SMTP id y13-20020a17090322cd00b001d0727b8355mr51732plg.1.1702414065371; Tue, 12 Dec 2023 12:47:45 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:41 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-27-sagis@google.com> Subject: [RFC PATCH v5 26/29] KVM: selftests: TDX: Add support for TDG.VP.VEINFO.GET From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:51:49 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110791042792999 X-GMAIL-MSGID: 1785110791042792999 From: Ackerley Tng Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- .../selftests/kvm/include/x86_64/tdx/tdx.h | 21 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 19 +++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h index b71bcea40b5c..12863a8beaae 100644 --- a/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +++ b/tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h @@ -6,6 +6,7 @@ #include "kvm_util_base.h" #define TDG_VP_INFO 1 +#define TDG_VP_VEINFO_GET 3 #define TDG_MEM_PAGE_ACCEPT 6 #define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 @@ -41,4 +42,24 @@ uint64_t tdg_vp_info(uint64_t *rcx, uint64_t *rdx, uint64_t tdg_vp_vmcall_map_gpa(uint64_t address, uint64_t size, uint64_t *data_out); uint64_t tdg_mem_page_accept(uint64_t gpa, uint8_t level); +/* + * Used by the #VE exception handler to gather the #VE exception + * info from the TDX module. This is a software only structure + * and not part of the TDX module/VMM ABI. + * + * Adapted from arch/x86/include/asm/tdx.h + */ +struct ve_info { + uint64_t exit_reason; + uint64_t exit_qual; + /* Guest Linear (virtual) Address */ + uint64_t gla; + /* Guest Physical Address */ + uint64_t gpa; + uint32_t instr_len; + uint32_t instr_info; +}; + +uint64_t tdg_vp_veinfo_get(struct ve_info *ve); + #endif // SELFTEST_TDX_TDX_H diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c index d8c4ab635c06..71d9f55007f7 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c @@ -241,3 +241,22 @@ uint64_t tdg_mem_page_accept(uint64_t gpa, uint8_t level) { return __tdx_module_call(TDG_MEM_PAGE_ACCEPT, gpa | level, 0, 0, 0, NULL); } + +uint64_t tdg_vp_veinfo_get(struct ve_info *ve) +{ + uint64_t ret; + struct tdx_module_output out; + + memset(&out, 0, sizeof(struct tdx_module_output)); + + ret = __tdx_module_call(TDG_VP_VEINFO_GET, 0, 0, 0, 0, &out); + + ve->exit_reason = out.rcx; + ve->exit_qual = out.rdx; + ve->gla = out.r8; + ve->gpa = out.r9; + ve->instr_len = out.r10 & 0xffffffff; + ve->instr_info = out.r10 >> 32; + + return ret; +} From patchwork Tue Dec 12 20:46:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177576 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994156vqy; Tue, 12 Dec 2023 12:49:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IGi17+nVTBvjQQWoIE92bDri11S0+32FeCVL7c0z5xnH7Yb/FlpoQQwg629gTrctETV8P+s X-Received: by 2002:a05:6e02:1d0c:b0:35d:768b:6ebe with SMTP id i12-20020a056e021d0c00b0035d768b6ebemr8005988ila.26.1702414183289; Tue, 12 Dec 2023 12:49:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414183; cv=none; d=google.com; s=arc-20160816; b=Gw5ZZQZJ8w8gB7LYP1pqYp4CQPrSUOqADwRJFdOQSGTKsTg+sBsZtL8jWUZ4PVUum1 H9Tvexstxx/Ne2BvWmnExa0fLvm3hdZEMgKfNTZh2wa/b+Q/xV2deb8G8Ga/w9Y5qdq3 tJWD8Z7L1rYWpnS6+zjy0fyRFeAzBSN6R3AlWHuUiaK2O5Mg/DPc4s28yOKOV8ErrkfE lI6odKz+YVcame1T5VCZJQ4rNwnjJOOQqH6G+iCeEYLPHmEHhmHYnNvc0yGenGCPcRLa diuPujK+ETG50y4pFMNZoN20bRMVdNQjJ+DDnrWP1AOze2UvEOuDN4aYoBuVpR9GB2Z7 QDVA== 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=TFmE+BG4/EJDZrVEh/xZNdnTy9FeH/PqMcDgQWjrrOU=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=DQl6aRrJo5mKkkN1IYXjJDuqgpRVbnGI6PbCc9nuwn+JoSEbCQ7hhhegmbIb+gXVhC JACHK/1Qm9qigwoT2FZWmLXNe65V9KlxdHPzZ/m0NVx+3XfCkGFKJQLOtIoANfCvE/CS 89WLS5J6riQjEiZY8fmYbHeIEKas08El/mBCkKKnu8HUXyEPtxBoLNfKZOgcOBP0BfFE aYJ/q0gvJB4XIiQyk9HeKoUTRNQxZ6HE7/KM8LscoblIK5v9jrJLUt7Jq05B1S4C5Iz7 gikxW5pvKzRCnxjZKqey2rYa4jmO0VyJM0NwEf9NZS9ZJ3+Gd3qAswkpGGzm5j3UujBW VkyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=KO1fhvEq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id a23-20020a634d17000000b005ab2613a571si8302818pgb.495.2023.12.12.12.49.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:49:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=KO1fhvEq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id AF6508023719; Tue, 12 Dec 2023 12:49:37 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235222AbjLLUtV (ORCPT + 99 others); Tue, 12 Dec 2023 15:49:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235177AbjLLUsn (ORCPT ); Tue, 12 Dec 2023 15:48:43 -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 A27691FDA for ; Tue, 12 Dec 2023 12:47:48 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-db512266d27so6409844276.3 for ; Tue, 12 Dec 2023 12:47:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414067; x=1703018867; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TFmE+BG4/EJDZrVEh/xZNdnTy9FeH/PqMcDgQWjrrOU=; b=KO1fhvEqSnWjbbd24F9xNUUoNic6nJw5QlWdLiaY2/t6mmt2DuSGLbGWmtuJr2Et1t GqhUuBBAwUbG2cj2Xl8vHL2WpJK1O90k9XkTJxyqVAfyVSav/QVBguBih8lZ0qzAoy6U Zf0tzH8GCWmg2B3qlzb0ey9zvI446FRhDMCIDgqXpNr9lQfESw6uOeP3wvVfiBFIJbUT WUf0PY8RNQ9FZIcCA4emQ9BdMnJOxV0lHMw78fz3SfLxhXDndpnwf+dRgE0dMyFyvbh/ 5CY+5wTN9TQBbv3z2YWFqbECdObzcedrIToGy/BgdPfHvbcqcIy9fv1SudSqcWsmEEox RKNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414067; x=1703018867; 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=TFmE+BG4/EJDZrVEh/xZNdnTy9FeH/PqMcDgQWjrrOU=; b=SrHjvZJYsRHz0gdUHkhnrIAoA4xsW6XcElKls+2++IcHkBnTfxk2WYBruJBQndaxwk pwzvlZQ4y+x8gG5JlCVSMLNarhkRhMFvbM4x4dmxHO+yXzSzHjMqrPOMt1iEYjy1Wy/1 iOyO6iqEzGDqAvMNZe+ZVfRVi6OllFTDfvRizKm8pf5LLLWWIrLRQOC+8sdOA2N51d21 UCsY1xx3mbEUB+jw9YS/1mX2EzSp0zT8eCWkyQP0OVndJOLnwqnmN5UortqrU/xfn422 Xkms54cHmArz3X67VXSjBQlk6Jbk2LOz9xw2xOSKtq00KwDRTk5ePemKuPUw0fN4ul91 SzFQ== X-Gm-Message-State: AOJu0Yx8rAVxcBuKpcVB5zeTtrzkBYbKXDnCcLt92awIf8BwNcKxen8b D3dGNf+i4CXjHIGuM+aLzSlvg8Ojsw== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a25:d204:0:b0:dbc:cbd9:3cd0 with SMTP id j4-20020a25d204000000b00dbccbd93cd0mr566ybg.8.1702414067193; Tue, 12 Dec 2023 12:47:47 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:42 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-28-sagis@google.com> Subject: [RFC PATCH v5 27/29] KVM: selftests: Propagate KVM_EXIT_MEMORY_FAULT to userspace From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:49:38 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110654579479345 X-GMAIL-MSGID: 1785110654579479345 Allow userspace to handle KVM_EXIT_MEMORY_FAULT instead of triggering TEST_ASSERT. From the KVM_EXIT_MEMORY_FAULT documentation: Note! KVM_EXIT_MEMORY_FAULT is unique among all KVM exit reasons in that it accompanies a return code of '-1', not '0'! errno will always be set to EFAULT or EHWPOISON when KVM exits with KVM_EXIT_MEMORY_FAULT, userspace should assume kvm_run.exit_reason is stale/undefined for all other error numbers. Signed-off-by: Sagi Shahar --- tools/testing/selftests/kvm/lib/kvm_util.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index d024abc5379c..8fb041e51484 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1742,6 +1742,10 @@ void vcpu_run(struct kvm_vcpu *vcpu) { int ret = _vcpu_run(vcpu); + // Allow this scenario to be handled by the caller. + if (ret == -1 && errno == EFAULT) + return; + TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_RUN, ret)); } From patchwork Tue Dec 12 20:46:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177582 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7995195vqy; Tue, 12 Dec 2023 12:52:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IEcnl7ozbEsPpeDtz++NIqwXy8vE0fPEJGqNXAx/ooDidw30KbzUka25N4vyrRqAnAAiETE X-Received: by 2002:a05:6a20:7fa0:b0:190:28d1:8e0b with SMTP id d32-20020a056a207fa000b0019028d18e0bmr10261336pzj.35.1702414337530; Tue, 12 Dec 2023 12:52:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414337; cv=none; d=google.com; s=arc-20160816; b=VgOJzHcPOuCijs57hA6E3YlRugDuTmMLO/wzsu7kPqjBHEjV4ZGhXaDUePLcytmh8U ll0g0rffM8MiJ9EswNKnPqCtLCsmRcWnGqh79wRFljups1W6pGXOGnXwrFZMvJb7QcxR ficUU0AbZB3rq5kOvQoJD3aHdEhs/TaQKNTdQuRrK+VUl0OhybH3VFphm0O5SPBLtZ7c r0iSC8LecjECjA8z3IOSMfG/w7KKLJ27b7jD0KCS9GR0pxqjMWsIpyEnF0uoytYoWJsD 14tP8gjx9pk6UrvvMML6LQ1J+pV2ww3iJYld2ZkjgbcFdIm4YQi7s/dS9dhdk5TDYncF a7RA== 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=8feJMu0CtGp4ESzuqj7WG5RYqMN4mFy6cD8e9ssBGqo=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=HLrhNaDCsM1WuZyytm7MJACtWaNDeXNwCMb4u8JmMIOSUtpXjta8JA70X8hmzM/8ng igB7JLxjMnN1wVJere1b/2mjk8JGgBVGniI/o0LzsPCBRVBXkuASey2GCYZ5sVcV5kx+ jvOt1u5q7X/R5Sdy1PRDgM6NRctNMw9dv9Xkfd5M5vbFRlkqRbVNV2okpsvNL40y7DC1 8OE2j234iIX6cEqP366oU0GX3jdCVtJmYLkyXVHyMaKIZq3RaJ//ylLRTONL2BfnIe61 XUnnSQnRtd2CGqDDD+X/RA/3iXw0T87TQMYd3/wbeHGXqhf1SewJF/WGWSEWoT209Epn K6mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=HG7qWmwO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id j11-20020a056a00174b00b006cb88a16f29si8324743pfc.161.2023.12.12.12.52.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:52:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=HG7qWmwO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 04E55807E45D; Tue, 12 Dec 2023 12:52:14 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377439AbjLLUvn (ORCPT + 99 others); Tue, 12 Dec 2023 15:51:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235173AbjLLUv3 (ORCPT ); Tue, 12 Dec 2023 15:51:29 -0500 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 088F01FF3 for ; Tue, 12 Dec 2023 12:47:50 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1d34350cbe8so5837095ad.0 for ; Tue, 12 Dec 2023 12:47:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414069; x=1703018869; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8feJMu0CtGp4ESzuqj7WG5RYqMN4mFy6cD8e9ssBGqo=; b=HG7qWmwOXlgGGrGmadgOGxTDdB6wfYfUPjxfngvGkMMtJdwcreFxAbjlfOdytkAt15 BYiMCw+AaC3Z4w0Dadd6/3K0R0paVsCLysXD9uYgyZjDIa5zDIBON1xFuX1x76bTXXi6 Sof4Q6CrD746kmf/6zy0IJexGJBCZ8YhubxX65V5gTkN3MLAN7A+Ltt9DFuODazli9IQ 0xOcl7jxiswx1raoQgsg8dyKunpkGZufi1g3ojYtE7CAABmq1SlkJa24rqVmpALh1/RO Jist/xPz8TT/50R6+bFowY8hts1s0iDbo6HfZruKUdkFnmb6lga6D8eT/BKC+DKItapb fusA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414069; x=1703018869; 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=8feJMu0CtGp4ESzuqj7WG5RYqMN4mFy6cD8e9ssBGqo=; b=uz8xEMV32nD4RAb2/5L4LwHny5y7RVpiCDQfODMsLfnZNa4lDMmZUhzjHXVWxZJ7+O F2isuct4rsPDsEg3iLd7kr5WztPURtms1OnEL+XXmbZGnXLPyxblg0FYomW67RAGHWdT 2qJIVWr44+4GwgQdRqUa1vrLDCI6+P10oemGdxNLHzIbqD7Hsg5mxUgGKI6Hdd6yu+IJ GLBY3nuzWGFUiaqB5aLu8+bKdvkh6UyS09SWw5jBYoBh5SQ6k7rA7HNU0bsKAG6Y1aYf Yxv6flgrHDAD13AbW8eK9os0WWcDtb6ah7rkuWtW8BosVAcT6KYDuXn7voFRz1MnkYBy evGg== X-Gm-Message-State: AOJu0YxksaUuIxB6Dt6rDQven0cw3UlBgbHPEb9GaGKI4WUF8/8W9ULE k4tob7JQMRUTZSI3RkFeU9VFI2z+eQ== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:903:41cf:b0:1d0:80cd:4c44 with SMTP id u15-20020a17090341cf00b001d080cd4c44mr51797ple.10.1702414069034; Tue, 12 Dec 2023 12:47:49 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:43 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-29-sagis@google.com> Subject: [RFC PATCH v5 28/29] KVM: selftests: TDX: Add TDX UPM selftest From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:52:14 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110816081999384 X-GMAIL-MSGID: 1785110816081999384 From: Ackerley Tng This tests the use of guest memory with explicit MapGPA calls. Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/tdx_upm_test.c | 401 ++++++++++++++++++ 2 files changed, 402 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/tdx_upm_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 8c0a6b395ee5..2f2669af15d6 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -157,6 +157,7 @@ TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test TEST_GEN_PROGS_x86_64 += system_counter_offset_test TEST_GEN_PROGS_x86_64 += x86_64/tdx_vm_tests TEST_GEN_PROGS_x86_64 += x86_64/tdx_shared_mem_test +TEST_GEN_PROGS_x86_64 += x86_64/tdx_upm_test # Compiled outputs used by test targets TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test diff --git a/tools/testing/selftests/kvm/x86_64/tdx_upm_test.c b/tools/testing/selftests/kvm/x86_64/tdx_upm_test.c new file mode 100644 index 000000000000..44671874a4f1 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/tdx_upm_test.c @@ -0,0 +1,401 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include +#include + +#include "kvm_util_base.h" +#include "processor.h" +#include "tdx/tdcall.h" +#include "tdx/tdx.h" +#include "tdx/tdx_util.h" +#include "tdx/test_util.h" +#include "test_util.h" + +/* TDX UPM test patterns */ +#define PATTERN_CONFIDENCE_CHECK (0x11) +#define PATTERN_HOST_FOCUS (0x22) +#define PATTERN_GUEST_GENERAL (0x33) +#define PATTERN_GUEST_FOCUS (0x44) + +/* + * 0x80000000 is arbitrarily selected. The selected address need not be the same + * as TDX_UPM_TEST_AREA_GVA_PRIVATE, but it should not overlap with selftest + * code or boot page. + */ +#define TDX_UPM_TEST_AREA_GPA (0x80000000) +/* Test area GPA is arbitrarily selected */ +#define TDX_UPM_TEST_AREA_GVA_PRIVATE (0x90000000) +/* Select any bit that can be used as a flag */ +#define TDX_UPM_TEST_AREA_GVA_SHARED_BIT (32) +/* + * TDX_UPM_TEST_AREA_GVA_SHARED is used to map the same GPA twice into the + * guest, once as shared and once as private + */ +#define TDX_UPM_TEST_AREA_GVA_SHARED \ + (TDX_UPM_TEST_AREA_GVA_PRIVATE | \ + BIT_ULL(TDX_UPM_TEST_AREA_GVA_SHARED_BIT)) + +/* The test area is 2MB in size */ +#define TDX_UPM_TEST_AREA_SIZE (2 << 20) +/* 0th general area is 1MB in size */ +#define TDX_UPM_GENERAL_AREA_0_SIZE (1 << 20) +/* Focus area is 40KB in size */ +#define TDX_UPM_FOCUS_AREA_SIZE (40 << 10) +/* 1st general area is the rest of the space in the test area */ +#define TDX_UPM_GENERAL_AREA_1_SIZE \ + (TDX_UPM_TEST_AREA_SIZE - TDX_UPM_GENERAL_AREA_0_SIZE - \ + TDX_UPM_FOCUS_AREA_SIZE) + +/* + * The test memory area is set up as two general areas, sandwiching a focus + * area. The general areas act as control areas. After they are filled, they + * are not expected to change throughout the tests. The focus area is memory + * permissions change from private to shared and vice-versa. + * + * The focus area is intentionally small, and sandwiched to test that when the + * focus area's permissions change, the other areas' permissions are not + * affected. + */ +struct __packed tdx_upm_test_area { + uint8_t general_area_0[TDX_UPM_GENERAL_AREA_0_SIZE]; + uint8_t focus_area[TDX_UPM_FOCUS_AREA_SIZE]; + uint8_t general_area_1[TDX_UPM_GENERAL_AREA_1_SIZE]; +}; + +static void fill_test_area(struct tdx_upm_test_area *test_area_base, + uint8_t pattern) +{ + memset(test_area_base, pattern, sizeof(*test_area_base)); +} + +static void fill_focus_area(struct tdx_upm_test_area *test_area_base, + uint8_t pattern) +{ + memset(test_area_base->focus_area, pattern, + sizeof(test_area_base->focus_area)); +} + +static bool check_area(uint8_t *base, uint64_t size, uint8_t expected_pattern) +{ + size_t i; + + for (i = 0; i < size; i++) { + if (base[i] != expected_pattern) + return false; + } + + return true; +} + +static bool check_general_areas(struct tdx_upm_test_area *test_area_base, + uint8_t expected_pattern) +{ + return (check_area(test_area_base->general_area_0, + sizeof(test_area_base->general_area_0), + expected_pattern) && + check_area(test_area_base->general_area_1, + sizeof(test_area_base->general_area_1), + expected_pattern)); +} + +static bool check_focus_area(struct tdx_upm_test_area *test_area_base, + uint8_t expected_pattern) +{ + return check_area(test_area_base->focus_area, + sizeof(test_area_base->focus_area), expected_pattern); +} + +static bool check_test_area(struct tdx_upm_test_area *test_area_base, + uint8_t expected_pattern) +{ + return (check_general_areas(test_area_base, expected_pattern) && + check_focus_area(test_area_base, expected_pattern)); +} + +static bool fill_and_check(struct tdx_upm_test_area *test_area_base, uint8_t pattern) +{ + fill_test_area(test_area_base, pattern); + + return check_test_area(test_area_base, pattern); +} + +#define TDX_UPM_TEST_ASSERT(x) \ + do { \ + if (!(x)) \ + tdx_test_fatal(__LINE__); \ + } while (0) + +/* + * Shared variables between guest and host + */ +static struct tdx_upm_test_area *test_area_gpa_private; +static struct tdx_upm_test_area *test_area_gpa_shared; + +/* + * Test stages for syncing with host + */ +enum { + SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST = 1, + SYNC_CHECK_READ_SHARED_MEMORY_FROM_HOST, + SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST_AGAIN, +}; + +#define TDX_UPM_TEST_ACCEPT_PRINT_PORT 0x87 + +/** + * Does vcpu_run, and also manages memory conversions if requested by the TD. + */ +void vcpu_run_and_manage_memory_conversions(struct kvm_vm *vm, + struct kvm_vcpu *vcpu) +{ + for (;;) { + vcpu_run(vcpu); + if (vcpu->run->exit_reason == KVM_EXIT_TDX && + vcpu->run->tdx.type == KVM_EXIT_TDX_VMCALL && + vcpu->run->tdx.u.vmcall.subfunction == TDG_VP_VMCALL_MAP_GPA) { + struct kvm_tdx_vmcall *vmcall_info = &vcpu->run->tdx.u.vmcall; + uint64_t gpa = vmcall_info->in_r12 & ~vm->arch.s_bit; + + handle_memory_conversion(vm, gpa, vmcall_info->in_r13, + !(vm->arch.s_bit & vmcall_info->in_r12)); + vmcall_info->status_code = 0; + continue; + } else if ( + vcpu->run->exit_reason == KVM_EXIT_IO && + vcpu->run->io.port == TDX_UPM_TEST_ACCEPT_PRINT_PORT) { + uint64_t gpa = tdx_test_read_64bit( + vcpu, TDX_UPM_TEST_ACCEPT_PRINT_PORT); + printf("\t ... guest accepting 1 page at GPA: 0x%lx\n", gpa); + continue; + } + + break; + } +} + +static void guest_upm_explicit(void) +{ + uint64_t ret = 0; + uint64_t failed_gpa; + + struct tdx_upm_test_area *test_area_gva_private = + (struct tdx_upm_test_area *)TDX_UPM_TEST_AREA_GVA_PRIVATE; + struct tdx_upm_test_area *test_area_gva_shared = + (struct tdx_upm_test_area *)TDX_UPM_TEST_AREA_GVA_SHARED; + + /* Check: host reading private memory does not modify guest's view */ + fill_test_area(test_area_gva_private, PATTERN_GUEST_GENERAL); + + tdx_test_report_to_user_space(SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST); + + TDX_UPM_TEST_ASSERT( + check_test_area(test_area_gva_private, PATTERN_GUEST_GENERAL)); + + /* Remap focus area as shared */ + ret = tdg_vp_vmcall_map_gpa((uint64_t)test_area_gpa_shared->focus_area, + sizeof(test_area_gpa_shared->focus_area), + &failed_gpa); + TDX_UPM_TEST_ASSERT(!ret); + + /* General areas should be unaffected by remapping */ + TDX_UPM_TEST_ASSERT( + check_general_areas(test_area_gva_private, PATTERN_GUEST_GENERAL)); + + /* + * Use memory contents to confirm that the memory allocated using mmap + * is used as backing memory for shared memory - PATTERN_CONFIDENCE_CHECK + * was written by the VMM at the beginning of this test. + */ + TDX_UPM_TEST_ASSERT( + check_focus_area(test_area_gva_shared, PATTERN_CONFIDENCE_CHECK)); + + /* Guest can use focus area after remapping as shared */ + fill_focus_area(test_area_gva_shared, PATTERN_GUEST_FOCUS); + + tdx_test_report_to_user_space(SYNC_CHECK_READ_SHARED_MEMORY_FROM_HOST); + + /* Check that guest has the same view of shared memory */ + TDX_UPM_TEST_ASSERT( + check_focus_area(test_area_gva_shared, PATTERN_HOST_FOCUS)); + + /* Remap focus area back to private */ + ret = tdg_vp_vmcall_map_gpa((uint64_t)test_area_gpa_private->focus_area, + sizeof(test_area_gpa_private->focus_area), + &failed_gpa); + TDX_UPM_TEST_ASSERT(!ret); + + /* General areas should be unaffected by remapping */ + TDX_UPM_TEST_ASSERT( + check_general_areas(test_area_gva_private, PATTERN_GUEST_GENERAL)); + + /* Focus area should be zeroed after remapping */ + TDX_UPM_TEST_ASSERT(check_focus_area(test_area_gva_private, 0)); + + tdx_test_report_to_user_space(SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST_AGAIN); + + /* Check that guest can use private memory after focus area is remapped as private */ + TDX_UPM_TEST_ASSERT( + fill_and_check(test_area_gva_private, PATTERN_GUEST_GENERAL)); + + tdx_test_success(); +} + +static void run_selftest(struct kvm_vm *vm, struct kvm_vcpu *vcpu, + struct tdx_upm_test_area *test_area_base_hva) +{ + vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, TDX_TEST_REPORT_SIZE, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + TEST_ASSERT_EQ(*(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset), + SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST); + + /* + * Check that host should read PATTERN_CONFIDENCE_CHECK from guest's + * private memory. This confirms that regular memory (userspace_addr in + * struct kvm_userspace_memory_region) is used to back the host's view + * of private memory, since PATTERN_CONFIDENCE_CHECK was written to that + * memory before starting the guest. + */ + TEST_ASSERT(check_test_area(test_area_base_hva, PATTERN_CONFIDENCE_CHECK), + "Host should read PATTERN_CONFIDENCE_CHECK from guest's private memory."); + + vcpu_run_and_manage_memory_conversions(vm, vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, TDX_TEST_REPORT_SIZE, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + TEST_ASSERT_EQ(*(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset), + SYNC_CHECK_READ_SHARED_MEMORY_FROM_HOST); + + TEST_ASSERT(check_focus_area(test_area_base_hva, PATTERN_GUEST_FOCUS), + "Host should have the same view of shared memory as guest."); + TEST_ASSERT(check_general_areas(test_area_base_hva, PATTERN_CONFIDENCE_CHECK), + "Host's view of private memory should still be backed by regular memory."); + + /* Check that host can use shared memory */ + fill_focus_area(test_area_base_hva, PATTERN_HOST_FOCUS); + TEST_ASSERT(check_focus_area(test_area_base_hva, PATTERN_HOST_FOCUS), + "Host should be able to use shared memory."); + + vcpu_run_and_manage_memory_conversions(vm, vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, TDX_TEST_REPORT_SIZE, + TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); + TEST_ASSERT_EQ(*(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset), + SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST_AGAIN); + + TEST_ASSERT(check_general_areas(test_area_base_hva, PATTERN_CONFIDENCE_CHECK), + "Host's view of private memory should be backed by regular memory."); + TEST_ASSERT(check_focus_area(test_area_base_hva, PATTERN_HOST_FOCUS), + "Host's view of private memory should be backed by regular memory."); + + vcpu_run(vcpu); + TDX_TEST_CHECK_GUEST_FAILURE(vcpu); + TDX_TEST_ASSERT_SUCCESS(vcpu); + + printf("\t ... PASSED\n"); +} + +static bool address_between(uint64_t addr, void *lo, void *hi) +{ + return (uint64_t)lo <= addr && addr < (uint64_t)hi; +} + +static void guest_ve_handler(struct ex_regs *regs) +{ + uint64_t ret; + struct ve_info ve; + + ret = tdg_vp_veinfo_get(&ve); + TDX_UPM_TEST_ASSERT(!ret); + + /* For this test, we will only handle EXIT_REASON_EPT_VIOLATION */ + TDX_UPM_TEST_ASSERT(ve.exit_reason == EXIT_REASON_EPT_VIOLATION); + + /* Validate GPA in fault */ + TDX_UPM_TEST_ASSERT( + address_between(ve.gpa, + test_area_gpa_private->focus_area, + test_area_gpa_private->general_area_1)); + + tdx_test_send_64bit(TDX_UPM_TEST_ACCEPT_PRINT_PORT, ve.gpa); + +#define MEM_PAGE_ACCEPT_LEVEL_4K 0 +#define MEM_PAGE_ACCEPT_LEVEL_2M 1 + ret = tdg_mem_page_accept(ve.gpa, MEM_PAGE_ACCEPT_LEVEL_4K); + TDX_UPM_TEST_ASSERT(!ret); +} + +static void verify_upm_test(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + + vm_vaddr_t test_area_gva_private; + struct tdx_upm_test_area *test_area_base_hva; + uint64_t test_area_npages; + + vm = td_create(); + td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); + vcpu = td_vcpu_add(vm, 0, guest_upm_explicit); + + vm_install_exception_handler(vm, VE_VECTOR, guest_ve_handler); + + /* + * Set up shared memory page for testing by first allocating as private + * and then mapping the same GPA again as shared. This way, the TD does + * not have to remap its page tables at runtime. + */ + test_area_npages = TDX_UPM_TEST_AREA_SIZE / vm->page_size; + vm_userspace_mem_region_add(vm, + VM_MEM_SRC_ANONYMOUS, TDX_UPM_TEST_AREA_GPA, + 3, test_area_npages, KVM_MEM_PRIVATE); + + test_area_gva_private = ____vm_vaddr_alloc( + vm, TDX_UPM_TEST_AREA_SIZE, TDX_UPM_TEST_AREA_GVA_PRIVATE, + TDX_UPM_TEST_AREA_GPA, 3, true); + TEST_ASSERT_EQ(test_area_gva_private, TDX_UPM_TEST_AREA_GVA_PRIVATE); + + test_area_gpa_private = (struct tdx_upm_test_area *) + addr_gva2gpa(vm, test_area_gva_private); + virt_map_shared(vm, TDX_UPM_TEST_AREA_GVA_SHARED, + (uint64_t)test_area_gpa_private, + test_area_npages); + TEST_ASSERT_EQ(addr_gva2gpa(vm, TDX_UPM_TEST_AREA_GVA_SHARED), + (vm_paddr_t)test_area_gpa_private); + + test_area_base_hva = addr_gva2hva(vm, TDX_UPM_TEST_AREA_GVA_PRIVATE); + + TEST_ASSERT(fill_and_check(test_area_base_hva, PATTERN_CONFIDENCE_CHECK), + "Failed to mark memory intended as backing memory for TD shared memory"); + + sync_global_to_guest(vm, test_area_gpa_private); + test_area_gpa_shared = (struct tdx_upm_test_area *) + ((uint64_t)test_area_gpa_private | BIT_ULL(vm->pa_bits - 1)); + sync_global_to_guest(vm, test_area_gpa_shared); + + td_finalize(vm); + + printf("Verifying UPM functionality: explicit MapGPA\n"); + + run_selftest(vm, vcpu, test_area_base_hva); + + kvm_vm_free(vm); +} + +int main(int argc, char **argv) +{ + /* Disable stdout buffering */ + setbuf(stdout, NULL); + + if (!is_tdx_enabled()) { + printf("TDX is not supported by the KVM\n" + "Skipping the TDX tests.\n"); + return 0; + } + + run_in_new_process(&verify_upm_test); +} From patchwork Tue Dec 12 20:46:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177580 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7994585vqy; Tue, 12 Dec 2023 12:50:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFjSiM0Xsz4bUapq1zrGSG0HSTsqH/V0XR2AE4wJjs8m00aiE1zkiCqnHHbYE9G4RUgyxES X-Received: by 2002:a05:6a20:639e:b0:190:65d3:779b with SMTP id m30-20020a056a20639e00b0019065d3779bmr5865650pzg.92.1702414249119; Tue, 12 Dec 2023 12:50:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414249; cv=none; d=google.com; s=arc-20160816; b=iFFuBk2fHscWLQF8VEN1NkB/3zUwcTObt1gCzgGC+zksP2RL8Dsj4WoQ6Dop5oNEuA kej8/tqt10sKoVqVZzzr/b+Ty4g+v2SXG/Q4MVD6xX0qeqBVy6iuW5SXDr5dlodFqMr5 KAqOotQbOf0H6JpgWAxYvtKC6VvVuE49Nv9OFBlXd6afjXCIFb9sOUWxprHlPbztwHDG tXX9MY507lKctAIaxT6iW0SyETezUjatXmPLIx9BvW9mukNAfOa71Ex2p67vvuVvTWoh I0AqGM9XCaO7wmB0fxWh5n9Vryyz8wMpFS6wwa+LDdlnB3gkURlvfpapr4NPaM+2vIpI DSPg== 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=9b8Rem1AmPjfYzBNcLPCGf0Ya+tJ/F2me7CGYJ1U7yY=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=gYWHxkLfVZH0vz21n3ZTqMyJogUBOIseq/DZh6lWENS28Hflfze4NYGuuWCidfCJUU M0whUkxLZH+sR2GCrW942Y80nU+5qkNNx0BODrobndl6phyQEzJeT25k2DOg6oo0fSiz PJPXeaxXFfNJUQoZGrUUB5zGmPOIePicue2P4U4AhjM0w06V7UmmXe5wMbBCMYieVyvS Y/RCEAigGLTHidPO6cS1Im3Wuc3eOoFd4wBh/mDOHBUfjQx7rJnvYMbopK97PDAISk7R pSu35PYrbaARQELQR1Ca4icSDJIs6ttcPeOH+p8/DgDk4+mHEehwky/AKvNFv41MPyNM meUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yUA+G3z7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id 20-20020a630f54000000b005ca4684b4ddsi139975pgp.498.2023.12.12.12.50.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:50:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yUA+G3z7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 740F0807E45D; Tue, 12 Dec 2023 12:50:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377433AbjLLUuI (ORCPT + 99 others); Tue, 12 Dec 2023 15:50:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377428AbjLLUto (ORCPT ); Tue, 12 Dec 2023 15:49:44 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 751BD2120 for ; Tue, 12 Dec 2023 12:47:52 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5e19414f714so21999057b3.0 for ; Tue, 12 Dec 2023 12:47:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414071; x=1703018871; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9b8Rem1AmPjfYzBNcLPCGf0Ya+tJ/F2me7CGYJ1U7yY=; b=yUA+G3z7D9ewA7kk8Dj883hsjAJfjykoePzI01xOmdxe4tCxT4x+wG4cCLW2kAsBP6 mNEKqhK1Ue5tt0sd2wrRQKvJGkJ+3XtIKdjNYmZERe9abuyCyWR2K7cpPRYP1EwdbXYI YZYczvg49rClwUzXxVXCWSHBqPzngwYLDaneTyuaVgAGKCZdaqzPbQKAD2XKTCpoZv4l VzODXpZUGHDDPmgurdSJUrbOJuzvPDVl2yUaDGhm9+T8lFnK7+RwUQLdfQi5motCyXBQ KFk1WB/o1hsOHQBaCRYeYosPA8cImlfQCVUtykOzQxYEvrUNlBJbOx2EHdgABigCU9et cdcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414071; x=1703018871; 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=9b8Rem1AmPjfYzBNcLPCGf0Ya+tJ/F2me7CGYJ1U7yY=; b=rK7na27k4mZ6X2l5L4HwYDfrORkzMSEKjEFjKCLJ4o+2+FMUqu2NSE/p8RLzWuTGQp CkjJcAOncAx07VSemI6SxtA8oTXtDBfRQvB8KatbYvoBn2QcvJ78/732vhot1wKCF0g8 sCUzQBLY7zFtdXyS2rMFrvWfd9rMVhmuBHrBrcHBmJ9Yz0wAd2RhSiUmW6IQP/tVDnEU 0/CTaFmIxszXOiNU7MeTKggamDGzYyprgCqrxovTzAUZp7xHU3yKtAePj5jhnM7yuyzG 8BCoq+1CbICR+e0esH5/DyP2hy/4AEpmm28SUe/9Kmts/M9FMcATbO+Sbkkx11vqGzN8 Z9mQ== X-Gm-Message-State: AOJu0YwBjgJeOY1M4mDx9nyRNtPxH3wh8rK9pwU57mnMPWaBuZtaITca 7HEwMjoHCyZjKjt5Ct8Tyk5TuaZUvQ== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a05:690c:891:b0:5d8:eec5:f57c with SMTP id cd17-20020a05690c089100b005d8eec5f57cmr64746ywb.4.1702414070805; Tue, 12 Dec 2023 12:47:50 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:44 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-30-sagis@google.com> Subject: [RFC PATCH v5 29/29] KVM: selftests: TDX: Add TDX UPM selftests for implicit conversion From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:50:20 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110723926016332 X-GMAIL-MSGID: 1785110723926016332 From: Ackerley Tng This tests the use of guest memory without explicit MapGPA calls. Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- .../selftests/kvm/x86_64/tdx_upm_test.c | 86 +++++++++++++++++-- 1 file changed, 77 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/tdx_upm_test.c b/tools/testing/selftests/kvm/x86_64/tdx_upm_test.c index 44671874a4f1..bfa921f125a0 100644 --- a/tools/testing/selftests/kvm/x86_64/tdx_upm_test.c +++ b/tools/testing/selftests/kvm/x86_64/tdx_upm_test.c @@ -149,7 +149,7 @@ enum { * Does vcpu_run, and also manages memory conversions if requested by the TD. */ void vcpu_run_and_manage_memory_conversions(struct kvm_vm *vm, - struct kvm_vcpu *vcpu) + struct kvm_vcpu *vcpu, bool handle_conversions) { for (;;) { vcpu_run(vcpu); @@ -163,6 +163,13 @@ void vcpu_run_and_manage_memory_conversions(struct kvm_vm *vm, !(vm->arch.s_bit & vmcall_info->in_r12)); vmcall_info->status_code = 0; continue; + } else if (handle_conversions && + vcpu->run->exit_reason == KVM_EXIT_MEMORY_FAULT) { + handle_memory_conversion( + vm, vcpu->run->memory_fault.gpa, + vcpu->run->memory_fault.size, + vcpu->run->memory_fault.flags == KVM_MEMORY_EXIT_FLAG_PRIVATE); + continue; } else if ( vcpu->run->exit_reason == KVM_EXIT_IO && vcpu->run->io.port == TDX_UPM_TEST_ACCEPT_PRINT_PORT) { @@ -243,8 +250,53 @@ static void guest_upm_explicit(void) tdx_test_success(); } +static void guest_upm_implicit(void) +{ + struct tdx_upm_test_area *test_area_gva_private = + (struct tdx_upm_test_area *)TDX_UPM_TEST_AREA_GVA_PRIVATE; + struct tdx_upm_test_area *test_area_gva_shared = + (struct tdx_upm_test_area *)TDX_UPM_TEST_AREA_GVA_SHARED; + + /* Check: host reading private memory does not modify guest's view */ + fill_test_area(test_area_gva_private, PATTERN_GUEST_GENERAL); + + tdx_test_report_to_user_space(SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST); + + TDX_UPM_TEST_ASSERT( + check_test_area(test_area_gva_private, PATTERN_GUEST_GENERAL)); + + /* Use focus area as shared */ + fill_focus_area(test_area_gva_shared, PATTERN_GUEST_FOCUS); + + /* General areas should not be affected */ + TDX_UPM_TEST_ASSERT( + check_general_areas(test_area_gva_private, PATTERN_GUEST_GENERAL)); + + tdx_test_report_to_user_space(SYNC_CHECK_READ_SHARED_MEMORY_FROM_HOST); + + /* Check that guest has the same view of shared memory */ + TDX_UPM_TEST_ASSERT( + check_focus_area(test_area_gva_shared, PATTERN_HOST_FOCUS)); + + /* Use focus area as private */ + fill_focus_area(test_area_gva_private, PATTERN_GUEST_FOCUS); + + /* General areas should be unaffected by remapping */ + TDX_UPM_TEST_ASSERT( + check_general_areas(test_area_gva_private, PATTERN_GUEST_GENERAL)); + + tdx_test_report_to_user_space(SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST_AGAIN); + + /* Check that guest can use private memory after focus area is remapped as private */ + TDX_UPM_TEST_ASSERT( + fill_and_check(test_area_gva_private, PATTERN_GUEST_GENERAL)); + + tdx_test_success(); +} + static void run_selftest(struct kvm_vm *vm, struct kvm_vcpu *vcpu, - struct tdx_upm_test_area *test_area_base_hva) + struct tdx_upm_test_area *test_area_base_hva, + bool implicit) { vcpu_run(vcpu); TDX_TEST_CHECK_GUEST_FAILURE(vcpu); @@ -263,7 +315,7 @@ static void run_selftest(struct kvm_vm *vm, struct kvm_vcpu *vcpu, TEST_ASSERT(check_test_area(test_area_base_hva, PATTERN_CONFIDENCE_CHECK), "Host should read PATTERN_CONFIDENCE_CHECK from guest's private memory."); - vcpu_run_and_manage_memory_conversions(vm, vcpu); + vcpu_run_and_manage_memory_conversions(vm, vcpu, implicit); TDX_TEST_CHECK_GUEST_FAILURE(vcpu); TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, TDX_TEST_REPORT_SIZE, TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); @@ -280,7 +332,7 @@ static void run_selftest(struct kvm_vm *vm, struct kvm_vcpu *vcpu, TEST_ASSERT(check_focus_area(test_area_base_hva, PATTERN_HOST_FOCUS), "Host should be able to use shared memory."); - vcpu_run_and_manage_memory_conversions(vm, vcpu); + vcpu_run_and_manage_memory_conversions(vm, vcpu, implicit); TDX_TEST_CHECK_GUEST_FAILURE(vcpu); TDX_TEST_ASSERT_IO(vcpu, TDX_TEST_REPORT_PORT, TDX_TEST_REPORT_SIZE, TDG_VP_VMCALL_INSTRUCTION_IO_WRITE); @@ -329,18 +381,20 @@ static void guest_ve_handler(struct ex_regs *regs) TDX_UPM_TEST_ASSERT(!ret); } -static void verify_upm_test(void) +static void verify_upm_test(bool implicit) { struct kvm_vm *vm; struct kvm_vcpu *vcpu; + void *guest_code; vm_vaddr_t test_area_gva_private; struct tdx_upm_test_area *test_area_base_hva; uint64_t test_area_npages; vm = td_create(); td_initialize(vm, VM_MEM_SRC_ANONYMOUS, 0); - vcpu = td_vcpu_add(vm, 0, guest_upm_explicit); + guest_code = implicit ? guest_upm_implicit : guest_upm_explicit; + vcpu = td_vcpu_add(vm, 0, guest_code); vm_install_exception_handler(vm, VE_VECTOR, guest_ve_handler); @@ -379,13 +433,26 @@ static void verify_upm_test(void) td_finalize(vm); - printf("Verifying UPM functionality: explicit MapGPA\n"); + if (implicit) + printf("Verifying UPM functionality: implicit conversion\n"); + else + printf("Verifying UPM functionality: explicit MapGPA\n"); - run_selftest(vm, vcpu, test_area_base_hva); + run_selftest(vm, vcpu, test_area_base_hva, implicit); kvm_vm_free(vm); } +void verify_upm_test_explicit(void) +{ + verify_upm_test(false); +} + +void verify_upm_test_implicit(void) +{ + verify_upm_test(true); +} + int main(int argc, char **argv) { /* Disable stdout buffering */ @@ -397,5 +464,6 @@ int main(int argc, char **argv) return 0; } - run_in_new_process(&verify_upm_test); + run_in_new_process(&verify_upm_test_explicit); + run_in_new_process(&verify_upm_test_implicit); }