From patchwork Fri Aug 6 00:16:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 493023 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24D50C432BE for ; Fri, 6 Aug 2021 00:17:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 043ED61166 for ; Fri, 6 Aug 2021 00:17:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232513AbhHFAR2 (ORCPT ); Thu, 5 Aug 2021 20:17:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:41536 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231253AbhHFAR2 (ORCPT ); Thu, 5 Aug 2021 20:17:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CA25E61164; Fri, 6 Aug 2021 00:17:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628209033; bh=defyoJDmegfMyeFnTrjoa+8oq8G0jbIQzX8vKY3OKZU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dwtd4yWqnJ5Rx5Er8eZcWMCoJ0IRjKudBJFW/nlQgIQI6qKuc3Ev11FGKoF8NJF3q Pj2GtkDSbkb5fm3yR056Tn/8OcyYRmaF4hJE8PyFe3lUCOdBbZ2wbUVRSNHK2pR4B9 nblnc1/XI7xQao2+/zE4MUmOM8YNJ7FmbTasr98H5CJuaxXY60TKxxFoN9rnf3wRxS 1P/H9Iig8vBkW4ITUyeJpEedSc5SUXuQStz032xiTVaNt+XWJ2DNyJEtDnHNOHhWhX 8uEbD9/Ak+FZDUoIys3m2ou7sublYIHD6ZjQb2RSvE3YPN5w8Sx9y8leT4IKrXUW/b FOjMKa1wcyUww== From: Jarkko Sakkinen To: Shuah Khan Cc: linux-kselftest@vger.kernel.org, linux-sgx@vger.kernel.org, Reinette Chatre , Borislav Petkov , Jarkko Sakkinen , Dave Hansen , Thomas Gleixner , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [PATCH v3 1/8] x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem Date: Fri, 6 Aug 2021 03:16:57 +0300 Message-Id: <20210806001704.667889-2-jarkko@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210806001704.667889-1-jarkko@kernel.org> References: <20210806001704.667889-1-jarkko@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Just like normal memory, SGX memory can be overcommitted. SGX has its own reclaim mechanism which kicks in when physical SGX memory (Enclave Page Cache / EPC) is exhausted. That reclaim mechanism is relatively rarely exercised and needs selftests to poke at it. The amount of EPC on the system is determined by the BIOS and it varies wildly between systems. It can be dozens of MB on desktops, or many GB on servers. To run in a reasonable amount of time, the selftest needs to know how much EPC there is in the system. Introduce a new debugfs file to export that information. Acked-by: Dave Hansen Signed-off-by: Jarkko Sakkinen v3: * Describe the units of sgx_total_mem in Dcumentation/x86/sgx.rst. * Rewrite of the commit message (suggested by Dave): https://lore.kernel.org/linux-sgx/5d3614af-2393-6744-9d85-7001241ca76e@intel.com/ v2: * sgx_nr_all_pages -> sgx_total_mem --- Documentation/x86/sgx.rst | 6 ++++++ arch/x86/kernel/cpu/sgx/main.c | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Documentation/x86/sgx.rst b/Documentation/x86/sgx.rst index dd0ac96ff9ef..f11bfb331b93 100644 --- a/Documentation/x86/sgx.rst +++ b/Documentation/x86/sgx.rst @@ -250,3 +250,9 @@ user wants to deploy SGX applications both on the host and in guests on the same machine, the user should reserve enough EPC (by taking out total virtual EPC size of all SGX VMs from the physical EPC size) for host SGX applications so they can run with acceptable performance. + +Debugging +========= + +*/sys/kernel/debug/x86/sgx_total_mem* contains an integer describing +the total SGX reserved memory in bytes, available in the system. diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 63d3de02bbcc..b65da19a53ee 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright(c) 2016-20 Intel Corporation. */ +#include #include #include #include @@ -28,7 +29,10 @@ static DECLARE_WAIT_QUEUE_HEAD(ksgxd_waitq); static LIST_HEAD(sgx_active_page_list); static DEFINE_SPINLOCK(sgx_reclaimer_lock); -/* The free page list lock protected variables prepend the lock. */ +/* Total EPC memory available in bytes. */ +static unsigned long sgx_total_mem; + +/* The number of free EPC pages in all nodes. */ static unsigned long sgx_nr_free_pages; /* Nodes with one or more EPC sections. */ @@ -656,6 +660,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size, list_add_tail(§ion->pages[i].list, &sgx_dirty_page_list); } + sgx_total_mem += nr_pages * PAGE_SIZE; + return true; } @@ -823,6 +829,8 @@ static int __init sgx_init(void) if (sgx_vepc_init() && ret) goto err_provision; + debugfs_create_ulong("sgx_total_mem", 0444, arch_debugfs_dir, &sgx_total_mem); + return 0; err_provision: From patchwork Fri Aug 6 00:16:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 493022 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80F5EC4320E for ; Fri, 6 Aug 2021 00:17:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6409E61166 for ; Fri, 6 Aug 2021 00:17:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237731AbhHFARf (ORCPT ); Thu, 5 Aug 2021 20:17:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:41664 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236860AbhHFARd (ORCPT ); Thu, 5 Aug 2021 20:17:33 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1B21F61165; Fri, 6 Aug 2021 00:17:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628209038; bh=BnQTvsR4vQ50eHLG1KZBQd1nNSSzVndxngtFP2th434=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V2OojP27JnfTt3KWhgc4gFgRTIMPgIDwOdA1bJI8L6LBFicQsdSNRqhpM5TTxBQ87 tUlfsYHMbob+KDGsCCJyseL/OnfGuY9vHQJoKbRgN+/tb269ScdXsP9MCFYAC5EMSe tBhRUrjvu3k+Is4wSErC6Wm07xudsMu6IvWm2PufRAZXtHbHSrWtSZ4Uu/UFvtvoxR G2aXYCJ38v6rIh1jmj9UPKg7A0FCb5CR1TCHoaLoGthW1ReazZz4j7D+rO/eqft3aN zuFHumLD8FPMtwsjf0643zcpt/fJVQfYcuwW7BaSp2fztG9jU8JE/08VWMYHytEMIm kKtJYwTMR+LRw== From: Jarkko Sakkinen To: Shuah Khan Cc: linux-kselftest@vger.kernel.org, linux-sgx@vger.kernel.org, Reinette Chatre , Borislav Petkov , Jarkko Sakkinen , Dave Hansen , linux-kernel@vger.kernel.org Subject: [PATCH v3 3/8] selftests/sgx: Make data measurement for an enclave segment optional Date: Fri, 6 Aug 2021 03:16:59 +0300 Message-Id: <20210806001704.667889-4-jarkko@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210806001704.667889-1-jarkko@kernel.org> References: <20210806001704.667889-1-jarkko@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org For a heap makes sense to leave its contents "unmeasured" in the SGX enclave build process, meaning that they won't contribute to the cryptographic signature (a RSA-3072 signed SHA56 hash) of the enclave. Enclaves are signed blobs where the signature is calculated both from page data and also from "structural properties" of the pages. For instance a page offset of *every* page added to the enclave is hashed. For data, this is optional, not least because hashing a page has a significant contribution to the enclave load time. Thus, where there is no reason to hash, do not. The SGX ioctl interface supports this with SGX_PAGE_MEASURE flag. Only when the flag is *set*, data is measured. Add seg->measure boolean flag to struct encl_segment. Only when the flag is set, include the segment data to the signature (represented by SIGSTRUCT architectural structure). Signed-off-by: Jarkko Sakkinen --- tools/testing/selftests/sgx/load.c | 6 +++++- tools/testing/selftests/sgx/main.h | 1 + tools/testing/selftests/sgx/sigstruct.c | 6 ++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/sgx/load.c b/tools/testing/selftests/sgx/load.c index 5605474aab73..f1be78984c50 100644 --- a/tools/testing/selftests/sgx/load.c +++ b/tools/testing/selftests/sgx/load.c @@ -111,7 +111,10 @@ static bool encl_ioc_add_pages(struct encl *encl, struct encl_segment *seg) ioc.offset = seg->offset; ioc.length = seg->size; ioc.secinfo = (unsigned long)&secinfo; - ioc.flags = SGX_PAGE_MEASURE; + if (seg->measure) + ioc.flags = SGX_PAGE_MEASURE; + else + ioc.flags = 0; rc = ioctl(encl->fd, SGX_IOC_ENCLAVE_ADD_PAGES, &ioc); if (rc < 0) { @@ -230,6 +233,7 @@ bool encl_load(const char *path, struct encl *encl) seg->offset = (phdr->p_offset & PAGE_MASK) - src_offset; seg->size = (phdr->p_filesz + PAGE_SIZE - 1) & PAGE_MASK; seg->src = encl->src + seg->offset; + seg->measure = true; j++; } diff --git a/tools/testing/selftests/sgx/main.h b/tools/testing/selftests/sgx/main.h index 452d11dc4889..aebc69e7cdc8 100644 --- a/tools/testing/selftests/sgx/main.h +++ b/tools/testing/selftests/sgx/main.h @@ -12,6 +12,7 @@ struct encl_segment { size_t size; unsigned int prot; unsigned int flags; + bool measure; }; struct encl { diff --git a/tools/testing/selftests/sgx/sigstruct.c b/tools/testing/selftests/sgx/sigstruct.c index 202a96fd81bf..50c5ab1aa6fa 100644 --- a/tools/testing/selftests/sgx/sigstruct.c +++ b/tools/testing/selftests/sgx/sigstruct.c @@ -296,8 +296,10 @@ static bool mrenclave_segment(EVP_MD_CTX *ctx, struct encl *encl, if (!mrenclave_eadd(ctx, seg->offset + offset, seg->flags)) return false; - if (!mrenclave_eextend(ctx, seg->offset + offset, seg->src + offset)) - return false; + if (seg->measure) { + if (!mrenclave_eextend(ctx, seg->offset + offset, seg->src + offset)) + return false; + } } return true; From patchwork Fri Aug 6 00:17:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 493021 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10BB1C4338F for ; Fri, 6 Aug 2021 00:17:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EB9EE61166 for ; Fri, 6 Aug 2021 00:17:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240392AbhHFARm (ORCPT ); Thu, 5 Aug 2021 20:17:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:41844 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238685AbhHFARi (ORCPT ); Thu, 5 Aug 2021 20:17:38 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4A21B61163; Fri, 6 Aug 2021 00:17:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628209043; bh=bnm02H3dfphxSdFEbJmZUOHQV7VRt4GzZNY+MRzq9O8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BZrAbngSGkrpLwmHesi3ghUWHKKCws5L0aHAcD/7P4ufjlhuOwKyCdQAv/tl3/40H WvCAqKLUi2XsoznSL8sKIcUYsQBVKJfa/XFad709d2XlpTc34CgbP5BcZz88oYPVht 6UecuA0I6Qo6cSfSL8w+Gn3iqhpmzD/z8AV6gUzTV5JOlDhK3xLw/d/qXhO1vceFJG GoM6w+Pg2paWzJ4iwrueo9od0IUG9O836VRTd6Wu3zmCCkal9vqRYKvhjxWSeAzKAl /kxcz01ZdTBgdiMPeQd6abcVu3p8cQuveKaPJRk3+w+1MkYUr7zUzY3eYWIAUO7zCl OMlq9pl5qFEJQ== From: Jarkko Sakkinen To: Shuah Khan Cc: linux-kselftest@vger.kernel.org, linux-sgx@vger.kernel.org, Reinette Chatre , Borislav Petkov , Jarkko Sakkinen , Dave Hansen , linux-kernel@vger.kernel.org Subject: [PATCH v3 5/8] selftests/sgx: Dump segments and /proc/self/maps only on failure Date: Fri, 6 Aug 2021 03:17:01 +0300 Message-Id: <20210806001704.667889-6-jarkko@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210806001704.667889-1-jarkko@kernel.org> References: <20210806001704.667889-1-jarkko@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Logging is always a compromise between clarity and detail. The main use case for dumping VMA's is when FIXTURE_SETUP() fails, and is less important for enclaves that do initialize correctly. Therefore, print the segments and /proc/self/maps only in the error case. Finally, if a single test ever creates multiple enclaves, the amount of log lines would become enormous. Signed-off-by: Jarkko Sakkinen --- tools/testing/selftests/sgx/main.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c index 6858a35fed20..deab02f2f3ce 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -127,12 +127,6 @@ FIXTURE_SETUP(enclave) ksft_exit_skip("cannot load enclaves\n"); } - for (i = 0; i < self->encl.nr_segments; i++) { - seg = &self->encl.segment_tbl[i]; - - TH_LOG("0x%016lx 0x%016lx 0x%02x", seg->offset, seg->size, seg->prot); - } - if (!encl_measure(&self->encl)) goto err; @@ -169,6 +163,17 @@ FIXTURE_SETUP(enclave) memset(&self->run, 0, sizeof(self->run)); self->run.tcs = self->encl.encl_base; + return; + +err: + encl_delete(&self->encl); + + for (i = 0; i < self->encl.nr_segments; i++) { + seg = &self->encl.segment_tbl[i]; + + TH_LOG("0x%016lx 0x%016lx 0x%02x", seg->offset, seg->size, seg->prot); + } + maps_file = fopen("/proc/self/maps", "r"); if (maps_file != NULL) { while (fgets(maps_line, sizeof(maps_line), maps_file) != NULL) { @@ -181,11 +186,7 @@ FIXTURE_SETUP(enclave) fclose(maps_file); } -err: - if (!sgx_enter_enclave_sym) - encl_delete(&self->encl); - - ASSERT_NE(sgx_enter_enclave_sym, NULL); + ASSERT_TRUE(false); } FIXTURE_TEARDOWN(enclave) From patchwork Fri Aug 6 00:17:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 493020 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D23BC4320A for ; Fri, 6 Aug 2021 00:17:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 603D861164 for ; Fri, 6 Aug 2021 00:17:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242709AbhHFARu (ORCPT ); Thu, 5 Aug 2021 20:17:50 -0400 Received: from mail.kernel.org ([198.145.29.99]:42026 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242720AbhHFARq (ORCPT ); Thu, 5 Aug 2021 20:17:46 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 01F0261165; Fri, 6 Aug 2021 00:17:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1628209051; bh=pBCuRbKWqZ8VXzwKr9l8E31otOpcFW6kulIyrKJFcrw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jUehiHdmaPA4C/NIvnesCu3Gp1aHv1JldJmON5ZbydPiryo2C0/M4F9mXoZ3gEo6s cAnvPkoulXUt9/KztN3VtMTPQuiSfcUaljmN+fldTXWjZQxAIvHYy0/EAwodIB+ohR 568lNt2P2jEaLHttaQTLAniDS3vdCnJ23XCqLwItr7jQI0M1fQjfJ/64HP6KQojTPQ 0fxpoZZ9c+dvPukzXjpZAB52Pq65YC21VZydGYOdmIvXQK/KObB3XsXkELCyVEk4TY QZvAa8ApLED7U8ZK90plKM4F9Q8U3+6dOAAgV06f5teBRFisUhW8JXjBIGRAdlgKvA IyF1ONYzhfyKQ== From: Jarkko Sakkinen To: Shuah Khan Cc: linux-kselftest@vger.kernel.org, linux-sgx@vger.kernel.org, Reinette Chatre , Borislav Petkov , Jarkko Sakkinen , Dave Hansen , linux-kernel@vger.kernel.org Subject: [PATCH v3 8/8] selftests/sgx: Add a new kselftest: unclobbered_vdso_oversubscribed Date: Fri, 6 Aug 2021 03:17:04 +0300 Message-Id: <20210806001704.667889-9-jarkko@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210806001704.667889-1-jarkko@kernel.org> References: <20210806001704.667889-1-jarkko@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add a variation of the unclobbered_vdso test. In the new test, create a heap for the test enclave, which has the same size as all available Enclave Page Cache (EPC) pages in the system. This will guarantee that all test_encl.elf pages *and* SGX Enclave Control Structure (SECS) have been swapped out by the page reclaimer during the load time.. This test will trigger both the page reclaimer and the page fault handler. The page reclaimer triggered, while the heap is being created during the load time. The page fault handler is triggered for all the required pages, while the test case is executing. Signed-off-by: Jarkko Sakkinen --- tools/testing/selftests/sgx/main.c | 62 ++++++++++++++++++++++++++++++ tools/testing/selftests/sgx/main.h | 1 + 2 files changed, 63 insertions(+) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c index f219768b4e9a..3be34c3db74a 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -246,6 +246,68 @@ TEST_F(enclave, unclobbered_vdso) EXPECT_EQ(self->run.user_data, 0); } +static bool sysfs_get_ulong(const char *path, unsigned long *value) +{ + struct stat sbuf; + char buf[128]; + ssize_t ret; + int fd; + + ret = stat(path, &sbuf); + if (ret) + return false; + + fd = open(path, O_RDONLY); + if (fd < 0) + return false; + + ret = read(fd, buf, sizeof(buf)); + if (ret < 0) { + close(fd); + return false; + } + + errno = 0; + *value = strtoul(buf, NULL, 0); + + close(fd); + + return errno ? false : true; +} + +TEST_F(enclave, unclobbered_vdso_oversubscribed) +{ + unsigned long total_mem; + struct encl_op op; + + if (!sysfs_get_ulong(SGX_TOTAL_MEM_PATH, &total_mem)) + ASSERT_TRUE(false); + + if (!setup_test_encl(total_mem, &self->encl, _metadata)) + ASSERT_TRUE(false); + + memset(&self->run, 0, sizeof(self->run)); + self->run.tcs = self->encl.encl_base; + + op.type = ENCL_OP_PUT; + op.buffer = MAGIC; + + EXPECT_EQ(ENCL_CALL(&op, &self->run, false), 0); + + EXPECT_EEXIT(&self->run); + EXPECT_EQ(self->run.user_data, 0); + + op.type = ENCL_OP_GET; + op.buffer = 0; + + EXPECT_EQ(ENCL_CALL(&op, &self->run, false), 0); + + EXPECT_EQ(op.buffer, MAGIC); + EXPECT_EEXIT(&self->run); + EXPECT_EQ(self->run.user_data, 0); + +} + TEST_F(enclave, clobbered_vdso) { struct encl_op op; diff --git a/tools/testing/selftests/sgx/main.h b/tools/testing/selftests/sgx/main.h index b45c52ec7ab3..dd7767364107 100644 --- a/tools/testing/selftests/sgx/main.h +++ b/tools/testing/selftests/sgx/main.h @@ -7,6 +7,7 @@ #define MAIN_H #define ENCL_HEAP_SIZE_DEFAULT 4096 +#define SGX_TOTAL_MEM_PATH "/sys/kernel/debug/x86/sgx_total_mem" struct encl_segment { void *src;