Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp2754677pxb; Sun, 8 Nov 2020 12:13:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJwpE9wOfxZDJzo7mihTJ+71h72DRPDB4LmsFE1lA+sO3Nj6FfDqb7jW7FeTzYw/9FUKpSFV X-Received: by 2002:a17:906:7cc:: with SMTP id m12mr11392862ejc.365.1604866386741; Sun, 08 Nov 2020 12:13:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1604866386; cv=pass; d=google.com; s=arc-20160816; b=B2XxcOz1ZY5Q9mz7uA0sllTnFylIXVgS81iNl/r3HrVaHU1IIcDUj6Z8ocKvZSzLDm rri7zZ0GzONLm4PyWXtq/N59VfhDvzC6Y9v8mLAX5JJou4hGxxFs7IitRyZBWYGlf8qz DSltepQyXLBWHRzw3xa/aoeAXrMEUUskUb1AdXHKUYzUNloz/4KC7g9SS29enP27/pOh p8hF3/v/4OerANPS6Pqo70n5sbiOpsqaj0Lng+Sss+amH0sbWE4ui3QEaptiWvT8Do/V LsY8UVZR3R55NXGfvPkB1R/iADBIbg094PjXpCBz0A6fWr3wXQU22DYnT6NPJEE5s091 Hyug== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:in-reply-to:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=mRk1flmRCaW2nS5gDE4Scm5RAvqDB7YG0i3Gt3+At7E=; b=imziAk5nUO4RNJN2uEOZmWhWVJptUbjG+4zb/mbDi5EjMWLhkhgrz9InAkAUpilsUV R1l/6V3E8vozaE2jul5qN1ThHROXzXN0ZO1Gc5lDwCxpfRSpqziu6dX+HIX6zceOxzrS UyQ0MHgwYgQBXRnZmpmFli6w4H6TFEKDPhJZhxOe4qTxyAJTcbvl4ZeUoKjtf2hqjsf8 MwR8KtW9wa+mJJVUgFIzmC25hhvzZzhRXx6cetxTGKwMRA0Rb6utHKhoi5Mbm+mO4qgp GNfZejZPbX1zqPLfeKWexQYXOBZQpKtK02iBZKyz3a732YWgwuARnElb9qOAIOWvM2A4 Ctkw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@fortanix.onmicrosoft.com header.s=selector2-fortanix-onmicrosoft-com header.b=FTFsiHXU; arc=pass (i=1 spf=pass spfdomain=fortanix.com dkim=pass dkdomain=fortanix.com dmarc=pass fromdomain=fortanix.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=fortanix.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n20si1593679edw.72.2020.11.08.12.12.40; Sun, 08 Nov 2020 12:13:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@fortanix.onmicrosoft.com header.s=selector2-fortanix-onmicrosoft-com header.b=FTFsiHXU; arc=pass (i=1 spf=pass spfdomain=fortanix.com dkim=pass dkdomain=fortanix.com dmarc=pass fromdomain=fortanix.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=fortanix.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728705AbgKHUI4 (ORCPT + 99 others); Sun, 8 Nov 2020 15:08:56 -0500 Received: from mail-bn8nam08on2121.outbound.protection.outlook.com ([40.107.100.121]:13728 "EHLO NAM04-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728006AbgKHUIz (ORCPT ); Sun, 8 Nov 2020 15:08:55 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NXejhx3CybKI4/y1c7tDimM5hSsBcY4WtZ260q1q4yuEqLcnvyH+Px1B/7L1FqLmsdG1L0xg0oF1EVss0xDd/klWnrprP0zMh00Tc7+/VLXnHtTn7oPatFovJZUchPTcbUt83ydIY342xq3+RyQHydGHLlWyQMKeZFT59ehkzRSVfPG1KMpqCZeX7tXvWDUv93PHthAliZLIhwyh/JUQsmPr6iZi+NfVJvy1PX8eF2dqQXJ+PYKyEGP9Hg7elk57NgF2E0FXuoEfhrffrNsSto4MQGwiGr2nToUDieRYAeVg6do8Ok+hYKngmnGGlf030SKScLW7d2g38vSELupRng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mRk1flmRCaW2nS5gDE4Scm5RAvqDB7YG0i3Gt3+At7E=; b=OlvDOpMjo55QgDFVxxfguNpcso5ImD/VJpHjqrt2fUdBe/CQ0WVNeIYjTozHAoohDNDFLhSONGabVwqLeU1dBE53DF18cKj1jC75HPNdxO/ub7OQaKhyVTZL8Z7bTXWzF3QTnrgZy/qOLMxCdvF6CUs4x5ASdJNBDl1ml5aMUX5evm9rPoroiX1lenZvI+ZEf2bHlOY/a1AWAIZw7K/NnXwGFIlJOBCezketqeTwv89Hw0ZBk9dP24W2pfc2QGQHiRXKV+WWrdNM1wgrPTu/nPiy0U3zh0Y65UaA78XWrxkBvAKLzoWT3225FzGTtVRMIZQ5RSi5V88dKONPUtOsbg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fortanix.com; dmarc=pass action=none header.from=fortanix.com; dkim=pass header.d=fortanix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fortanix.onmicrosoft.com; s=selector2-fortanix-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mRk1flmRCaW2nS5gDE4Scm5RAvqDB7YG0i3Gt3+At7E=; b=FTFsiHXUsoRhsryx0nvBBxS6FzXc9Ry6N+tKjr/kQUkEcn3SLLV4XF5zTf7mIIq50z2e92YzK5irHNxoZkznP/8opphiwRbkyR2K4QXh8dvWHU9yjGnQpyMS2OGHkGeJMgA167AYnC63auFfzOFD+HmyXh454Rv8ST3lveRlnI0= Authentication-Results: intel.com; dkim=none (message not signed) header.d=none;intel.com; dmarc=none action=none header.from=fortanix.com; Received: from CH2PR11MB4262.namprd11.prod.outlook.com (2603:10b6:610:42::13) by CH2PR11MB4341.namprd11.prod.outlook.com (2603:10b6:610:3c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3541.21; Sun, 8 Nov 2020 20:08:49 +0000 Received: from CH2PR11MB4262.namprd11.prod.outlook.com ([fe80::c4d:afdf:4dfe:2706]) by CH2PR11MB4262.namprd11.prod.outlook.com ([fe80::c4d:afdf:4dfe:2706%4]) with mapi id 15.20.3499.032; Sun, 8 Nov 2020 20:08:49 +0000 Subject: Re: [PATCH v40 19/24] x86/vdso: Implement a vDSO for Intel SGX enclave call To: Jarkko Sakkinen , x86@kernel.org, linux-sgx@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Sean Christopherson , Andy Lutomirski , Cedric Xing , akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, asapek@google.com, bp@alien8.de, chenalexchen@google.com, conradparker@google.com, cyhanish@google.com, dave.hansen@intel.com, haitao.huang@intel.com, kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com, ludloff@google.com, luto@kernel.org, nhorman@redhat.com, npmccallum@redhat.com, puiterwijk@redhat.com, rientjes@google.com, tglx@linutronix.de, yaozhangx@google.com, mikko.ylinen@intel.com References: <20201104145430.300542-1-jarkko.sakkinen@linux.intel.com> <20201104145430.300542-20-jarkko.sakkinen@linux.intel.com> From: Jethro Beekman Message-ID: Date: Sun, 8 Nov 2020 21:08:41 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 In-Reply-To: <20201104145430.300542-20-jarkko.sakkinen@linux.intel.com> Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha-256; boundary="------------ms070202020800020606060102" X-Originating-IP: [213.127.125.17] X-ClientProxiedBy: AM4PR0302CA0002.eurprd03.prod.outlook.com (2603:10a6:205:2::15) To CH2PR11MB4262.namprd11.prod.outlook.com (2603:10b6:610:42::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [192.168.4.219] (213.127.125.17) by AM4PR0302CA0002.eurprd03.prod.outlook.com (2603:10a6:205:2::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3541.21 via Frontend Transport; Sun, 8 Nov 2020 20:08:45 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: fc0e83b7-a368-42b4-b9b3-08d884221aac X-MS-TrafficTypeDiagnostic: CH2PR11MB4341: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LUmL/1AX9LU6SR5rQGrf/pCn+86IjRrjX34WgBKNSNZm2c89WibC+VPLk10z8dMBKQGsojyKUMRi+g4qmrKI2l9fnVCwNUSSn1T8p7JV0ZHrD7MNMpOVkJPCY4KPDQqnKtZN6pVgwK8zDwjwXC3/f1aAOwL4J+4RkCjYUKsCUMF/4NypkZGpRcZ2tv71ThwphjySt7RQVucvGNXB0INARXdelF91ljdyDAqfqCr0sn+wc91LzaYbDeYAqC3GHPzA2yLbYgfuKCyKHZTGFeWggW0qGYzbDFdvKjbw8nBBwvrAQf7+YmeRPKXl8eDqTYHha7tBvt+M/mibKBJo7b4qO35ZoO76iJhC0A3Cp+2EA332vFZPCelc6qUlv9zhoFcQ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR11MB4262.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(376002)(136003)(396003)(346002)(366004)(39830400003)(2616005)(8936002)(26005)(6486002)(86362001)(52116002)(66476007)(478600001)(66946007)(6666004)(66556008)(31686004)(2906002)(4326008)(31696002)(186003)(16526019)(33964004)(36756003)(16576012)(83380400001)(7416002)(53546011)(54906003)(8676002)(235185007)(316002)(30864003)(5660300002)(956004)(43740500002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData: r/jNaeFBQDu/ErBpIpMGbDfzJ0PAbGRPWWGF90E+dFb7YFLNWdt/VdYbacDm39i/d0XGV1x5MgZmxi6vr2OvS6u7Q2lXASn+MOoruosy11LFSQ/TOnNtgAeKpmYNSjYFleWvwzR53Kk8KuNlLszpv0oO0sWCBbZbxKD5fGPXUJHNmTJj2Ll9nDWJU3553EPHdxDOnsOpr4Gzk+/R7cxWHFQj848uwLipTscveh3M4ylW+/s8BpZyijUwoErai3o9Ywh0H2PZxrb0hea8aWsxummsu0ZqnYxjthrGPHucUpQEiJEykmAuADivTpSMkU7Ca+cjXa3BNbX+dgbrDtOVVluu/iajQttczNdmxVeu1jebWvQinFeV0WglEmRMK7pUVhWGvyRdlatG5MskM92pm5pfGNC7bIvI7h0NpnzAXsvZ9OCfBufdFT4QzSA6C9Jn5LwH9MzTDhNoeaf6lRH/bWZaYH4gjLOnQPCQvBBUW/eVaFR/Sc7/U4DhBlmFt7uq0LpcoLIy3SRjaG4kfI6aa+uIKhEEr5ou2x/Cl/QAJEm8VhiySjS7l/XhLAw2O0ffe/OcYO7QhIiRajYcebIrDQxR1mHRJhoTJ/JjU882jIzfwJ3JsXisiepuFqY4CV0uEB/xUrSbAPluo9qJgBeCDg== X-OriginatorOrg: fortanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: fc0e83b7-a368-42b4-b9b3-08d884221aac X-MS-Exchange-CrossTenant-AuthSource: CH2PR11MB4262.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2020 20:08:48.9993 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: de7becae-4883-43e8-82c7-7dbdbb988ae6 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: lkt7J8FO8JwOluHAuoWkmivOkkgAzx3qsVu+ntw7iMNDjG+XyNAnFumRwoan1a0WG1jyKoRXC+lmZOvsbxYYuQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR11MB4341 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --------------ms070202020800020606060102 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable On 2020-11-04 15:54, Jarkko Sakkinen wrote: > From: Sean Christopherson >=20 > Enclaves encounter exceptions for lots of reasons: everything from encl= ave > page faults to NULL pointer dereferences, to system calls that must be > =E2=80=9Cproxied=E2=80=9D to the kernel from outside the enclave. >=20 > In addition to the code contained inside an enclave, there is also > supporting code outside the enclave called an =E2=80=9CSGX runtime=E2=80= =9D, which is > virtually always implemented inside a shared library. The runtime help= s > build the enclave and handles things like *re*building the enclave if i= t > got destroyed by something like a suspend/resume cycle. >=20 > The rebuilding has traditionally been handled in SIGSEGV handlers, > registered by the library. But, being process-wide, shared state, sign= al > handling and shared libraries do not mix well. >=20 > Introduce a vDSO function call that wraps the enclave entry functions > (EENTER/ERESUME functions of the ENCLU instruciton) and returns informa= tion > about any exceptions to the caller in the SGX runtime. >=20 > Instead of generating a signal, the kernel places exception information= in > RDI, RSI and RDX. The kernel-provided userspace portion of the vDSO han= dler > will place this information in a user-provided buffer or trigger a > user-provided callback at the time of the exception. >=20 > The vDSO function calling convention uses the standard RDI RSI, RDX, RC= X, > R8 and R9 registers. This makes it possible to declare the vDSO as a C= > prototype, but other than that there is no specific support for SystemV= > ABI. Things like storing XSAVE are the responsibility of the enclave an= d > the runtime. >=20 > Suggested-by: Andy Lutomirski > Acked-by: Jethro Beekman > Tested-by: Jethro Beekman > Signed-off-by: Sean Christopherson > Co-developed-by: Cedric Xing > Signed-off-by: Cedric Xing > Co-developed-by: Jarkko Sakkinen > Signed-off-by: Jarkko Sakkinen > --- > Changes from v39: > * Relayout out the user handler documentation: return values are descri= bed > in sgx_enclave_user_handler_t keneldoc and broad description is given= > in struct sgx_enclave_run kerneldoc. > * Rename @leaf as @function, given that we want to speak consistently > about ENCLS and ENCLU functions. > * Reorder user_handler and user_data as the last fields in > sgx_enclave_run, as they are an extension to the basic functionality.= >=20 > arch/x86/entry/vdso/Makefile | 2 + > arch/x86/entry/vdso/vdso.lds.S | 1 + > arch/x86/entry/vdso/vsgx.S | 151 ++++++++++++++++++++++++++++++++= > arch/x86/include/asm/enclu.h | 9 ++ > arch/x86/include/uapi/asm/sgx.h | 91 +++++++++++++++++++ > 5 files changed, 254 insertions(+) > create mode 100644 arch/x86/entry/vdso/vsgx.S > create mode 100644 arch/x86/include/asm/enclu.h >=20 > diff --git a/arch/x86/entry/vdso/Makefile b/arch/x86/entry/vdso/Makefil= e > index 2ad757fb3c23..9915fbd34264 100644 > --- a/arch/x86/entry/vdso/Makefile > +++ b/arch/x86/entry/vdso/Makefile > @@ -27,6 +27,7 @@ VDSO32-$(CONFIG_IA32_EMULATION) :=3D y > vobjs-y :=3D vdso-note.o vclock_gettime.o vgetcpu.o > vobjs32-y :=3D vdso32/note.o vdso32/system_call.o vdso32/sigreturn.o > vobjs32-y +=3D vdso32/vclock_gettime.o > +vobjs-$(VDSO64-y) +=3D vsgx.o > =20 > # files to link into kernel > obj-y +=3D vma.o extable.o > @@ -98,6 +99,7 @@ $(vobjs): KBUILD_CFLAGS :=3D $(filter-out $(GCC_PLUGI= NS_CFLAGS) $(RETPOLINE_CFLAGS > CFLAGS_REMOVE_vclock_gettime.o =3D -pg > CFLAGS_REMOVE_vdso32/vclock_gettime.o =3D -pg > CFLAGS_REMOVE_vgetcpu.o =3D -pg > +CFLAGS_REMOVE_vsgx.o =3D -pg > =20 > # > # X32 processes use x32 vDSO to access 64bit kernel data. > diff --git a/arch/x86/entry/vdso/vdso.lds.S b/arch/x86/entry/vdso/vdso.= lds.S > index 36b644e16272..4bf48462fca7 100644 > --- a/arch/x86/entry/vdso/vdso.lds.S > +++ b/arch/x86/entry/vdso/vdso.lds.S > @@ -27,6 +27,7 @@ VERSION { > __vdso_time; > clock_getres; > __vdso_clock_getres; > + __vdso_sgx_enter_enclave; > local: *; > }; > } > diff --git a/arch/x86/entry/vdso/vsgx.S b/arch/x86/entry/vdso/vsgx.S > new file mode 100644 > index 000000000000..86a0e94f68df > --- /dev/null > +++ b/arch/x86/entry/vdso/vsgx.S > @@ -0,0 +1,151 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +#include > +#include > +#include > +#include > + > +#include "extable.h" > + > +/* Relative to %rbp. */ > +#define SGX_ENCLAVE_OFFSET_OF_RUN 16 > + > +/* The offsets relative to struct sgx_enclave_run. */ > +#define SGX_ENCLAVE_RUN_TCS 0 > +#define SGX_ENCLAVE_RUN_LEAF 8 > +#define SGX_ENCLAVE_RUN_EXCEPTION_VECTOR 12 > +#define SGX_ENCLAVE_RUN_EXCEPTION_ERROR_CODE 14 > +#define SGX_ENCLAVE_RUN_EXCEPTION_ADDR 16 > +#define SGX_ENCLAVE_RUN_USER_HANDLER 24 > +#define SGX_ENCLAVE_RUN_USER_DATA 32 /* not used */ > +#define SGX_ENCLAVE_RUN_RESERVED_START 40 > +#define SGX_ENCLAVE_RUN_RESERVED_END 256 > + > +.code64 > +.section .text, "ax" > + > +SYM_FUNC_START(__vdso_sgx_enter_enclave) > + /* Prolog */ > + .cfi_startproc > + push %rbp > + .cfi_adjust_cfa_offset 8 > + .cfi_rel_offset %rbp, 0 > + mov %rsp, %rbp > + .cfi_def_cfa_register %rbp > + push %rbx > + .cfi_rel_offset %rbx, -8 > + > + mov %ecx, %eax > +.Lenter_enclave: > + /* EENTER <=3D function <=3D ERESUME */ > + cmp $EENTER, %eax > + jb .Linvalid_input > + cmp $ERESUME, %eax > + ja .Linvalid_input > + > + mov SGX_ENCLAVE_OFFSET_OF_RUN(%rbp), %rcx > + > + /* Validate that the reserved area contains only zeros. */ > + mov $SGX_ENCLAVE_RUN_RESERVED_START, %rbx > +1: > + cmpq $0, (%rcx, %rbx) > + jne .Linvalid_input > + add $8, %rbx > + cmpq $SGX_ENCLAVE_RUN_RESERVED_END, %rbx > + jne 1b > + > + /* Load TCS and AEP */ > + mov SGX_ENCLAVE_RUN_TCS(%rcx), %rbx > + lea .Lasync_exit_pointer(%rip), %rcx > + > + /* Single ENCLU serving as both EENTER and AEP (ERESUME) */ > +.Lasync_exit_pointer: > +.Lenclu_eenter_eresume: > + enclu > + > + /* EEXIT jumps here unless the enclave is doing something fancy. */ > + mov SGX_ENCLAVE_OFFSET_OF_RUN(%rbp), %rbx > + > + /* Set exit_reason. */ > + movl $EEXIT, SGX_ENCLAVE_RUN_LEAF(%rbx) > + > + /* Invoke userspace's exit handler if one was provided. */ > +.Lhandle_exit: > + cmpq $0, SGX_ENCLAVE_RUN_USER_HANDLER(%rbx) > + jne .Linvoke_userspace_handler > + > + /* Success, in the sense that ENCLU was attempted. */ > + xor %eax, %eax > + > +.Lout: > + pop %rbx > + leave > + .cfi_def_cfa %rsp, 8 > + ret > + > + /* The out-of-line code runs with the pre-leave stack frame. */ > + .cfi_def_cfa %rbp, 16 > + > +.Linvalid_input: > + mov $(-EINVAL), %eax > + jmp .Lout > + > +.Lhandle_exception: > + mov SGX_ENCLAVE_OFFSET_OF_RUN(%rbp), %rbx > + > + /* Set the exception info. */ > + mov %eax, (SGX_ENCLAVE_RUN_LEAF)(%rbx) > + mov %di, (SGX_ENCLAVE_RUN_EXCEPTION_VECTOR)(%rbx) > + mov %si, (SGX_ENCLAVE_RUN_EXCEPTION_ERROR_CODE)(%rbx) > + mov %rdx, (SGX_ENCLAVE_RUN_EXCEPTION_ADDR)(%rbx) > + jmp .Lhandle_exit > + > +.Linvoke_userspace_handler: > + /* Pass the untrusted RSP (at exit) to the callback via %rcx. */ > + mov %rsp, %rcx > + > + /* Save struct sgx_enclave_exception %rbx is about to be clobbered. *= / > + mov %rbx, %rax > + > + /* Save the untrusted RSP offset in %rbx (non-volatile register). */ > + mov %rsp, %rbx > + and $0xf, %rbx > + > + /* > + * Align stack per x86_64 ABI. Note, %rsp needs to be 16-byte aligned= > + * _after_ pushing the parameters on the stack, hence the bonus push.= > + */ > + and $-0x10, %rsp > + push %rax > + > + /* Push struct sgx_enclave_exception as a param to the callback. */ > + push %rax > + > + /* Clear RFLAGS.DF per x86_64 ABI */ > + cld > + > + /* > + * Load the callback pointer to %rax and lfence for LVI (load value > + * injection) protection before making the call. > + */ > + mov SGX_ENCLAVE_RUN_USER_HANDLER(%rax), %rax > + lfence > + call *%rax > + > + /* Undo the post-exit %rsp adjustment. */ > + lea 0x10(%rsp, %rbx), %rsp > + > + /* > + * If the return from callback is zero or negative, return immediatel= y, > + * else re-execute ENCLU with the postive return value interpreted as= > + * the requested ENCLU function. > + */ > + cmp $0, %eax > + jle .Lout > + jmp .Lenter_enclave > + > + .cfi_endproc > + > +_ASM_VDSO_EXTABLE_HANDLE(.Lenclu_eenter_eresume, .Lhandle_exception) > + > +SYM_FUNC_END(__vdso_sgx_enter_enclave) > diff --git a/arch/x86/include/asm/enclu.h b/arch/x86/include/asm/enclu.= h > new file mode 100644 > index 000000000000..b1314e41a744 > --- /dev/null > +++ b/arch/x86/include/asm/enclu.h > @@ -0,0 +1,9 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef _ASM_X86_ENCLU_H > +#define _ASM_X86_ENCLU_H > + > +#define EENTER 0x02 > +#define ERESUME 0x03 > +#define EEXIT 0x04 > + > +#endif /* _ASM_X86_ENCLU_H */ > diff --git a/arch/x86/include/uapi/asm/sgx.h b/arch/x86/include/uapi/as= m/sgx.h > index c32210235bf5..791e45334a4a 100644 > --- a/arch/x86/include/uapi/asm/sgx.h > +++ b/arch/x86/include/uapi/asm/sgx.h > @@ -74,4 +74,95 @@ struct sgx_enclave_provision { > __u64 fd; > }; > =20 > +struct sgx_enclave_run; > + > +/** > + * typedef sgx_enclave_user_handler_t - Exit handler function accepted= by > + * __vdso_sgx_enter_enclave() > + * @run: The run instance given by the caller > + * > + * The register parameters contain the snapshot of their values at enc= lave > + * exit. An invalid ENCLU function number will cause -EINVAL to be ret= urned > + * to the caller. > + * > + * Return: > + * - <=3D 0: The given value is returned back to the caller. > + * - > 0: ENCLU function to invoke, either EENTER or ERESUME. > + */ > +typedef int (*sgx_enclave_user_handler_t)(long rdi, long rsi, long rdx= , > + long rsp, long r8, long r9, > + struct sgx_enclave_run *run); > + > +/** > + * struct sgx_enclave_run - the execution context of __vdso_sgx_enter_= enclave() > + * @tcs: TCS used to enter the enclave > + * @function: The last seen ENCLU function (EENTER, ERESUME or EEXIT= ) > + * @exception_vector: The interrupt vector of the exception > + * @exception_error_code: The exception error code pulled out of the s= tack > + * @exception_addr: The address that triggered the exception > + * @user_handler: User provided callback run on exception > + * @user_data: Data passed to the user handler > + * @reserved Reserved for future extensions > + * > + * If @user_handler is provided, the handler will be invoked on all re= turn paths > + * of the normal flow. The user handler may transfer control, e.g. vi= a a > + * longjmp() call or a C++ exception, without returning to > + * __vdso_sgx_enter_enclave(). > + */ > +struct sgx_enclave_run { > + __u64 tcs; > + __u32 function; > + __u16 exception_vector; > + __u16 exception_error_code; > + __u64 exception_addr; > + __u64 user_handler; > + __u64 user_data; > + __u8 reserved[216]; > +}; > + > +/** > + * typedef vdso_sgx_enter_enclave_t - Prototype for __vdso_sgx_enter_e= nclave(), > + * a vDSO function to enter an SGX enclave. > + * @rdi: Pass-through value for RDI > + * @rsi: Pass-through value for RSI > + * @rdx: Pass-through value for RDX > + * @function: ENCLU function, must be EENTER or ERESUME > + * @r8: Pass-through value for R8 > + * @r9: Pass-through value for R9 > + * @run: struct sgx_enclave_run, must be non-NULL > + * > + * NOTE: __vdso_sgx_enter_enclave() does not ensure full compliance wi= th the > + * x86-64 ABI, e.g. doesn't handle XSAVE state. Except for non-volati= le > + * general purpose registers, EFLAGS.DF, and RSP alignment, preserving= /setting > + * state in accordance with the x86-64 ABI is the responsibility of th= e enclave > + * and its runtime, i.e. __vdso_sgx_enter_enclave() cannot be called f= rom C > + * code without careful consideration by both the enclave and its runt= ime. > + * > + * All general purpose registers except RAX, RBX and RCX are passed as= -is to the > + * enclave. RAX, RBX and RCX are consumed by EENTER and ERESUME and a= re loaded > + * with @function, asynchronous exit pointer, and @run.tcs respectivel= y. > + * > + * RBP and the stack are used to anchor __vdso_sgx_enter_enclave() to = the > + * pre-enclave state, e.g. to retrieve @run.exception and @run.user_ha= ndler > + * after an enclave exit. All other registers are available for use b= y the > + * enclave and its runtime, e.g. an enclave can push additional data o= nto the > + * stack (and modify RSP) to pass information to the optional user han= dler (see > + * below). > + * > + * Most exceptions reported on ENCLU, including those that occur withi= n the > + * enclave, are fixed up and reported synchronously instead of being d= elivered > + * via a standard signal. Debug Exceptions (#DB) and Breakpoints (#BP)= are > + * never fixed up and are always delivered via standard signals. On sy= nchrously > + * reported exceptions, -EFAULT is returned This part about EFAULT is also bogus. -- Jethro Beekman | Fortanix --------------ms070202020800020606060102 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgEFADCABgkqhkiG9w0BBwEAAKCC DVUwggXgMIIDyKADAgECAhBukmvE8GLB9+EYd88699DiMA0GCSqGSIb3DQEBCwUAMIGBMQsw CQYDVQQGEwJJVDEQMA4GA1UECAwHQmVyZ2FtbzEZMBcGA1UEBwwQUG9udGUgU2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