From patchwork Tue May 21 21:25:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 10954533 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B2118933 for ; Tue, 21 May 2019 21:27:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A02C0289F7 for ; Tue, 21 May 2019 21:27:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 942F528A61; Tue, 21 May 2019 21:27:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 05340289F7 for ; Tue, 21 May 2019 21:27:19 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hTCGn-0008Da-IK; Tue, 21 May 2019 21:26:01 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hTCGm-0008DB-N2 for xen-devel@lists.xenproject.org; Tue, 21 May 2019 21:26:00 +0000 X-Inumbo-ID: 05fe37c6-7c0f-11e9-8b54-476a08d18266 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (unknown [40.107.6.84]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 05fe37c6-7c0f-11e9-8b54-476a08d18266; Tue, 21 May 2019 21:25:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=NfSTs40VbIC7VZV/MLKlx2qJ5cVSMchV+zk3M1uj+BY=; b=CTonHs/XoT5BGNTfGTsBQHaQdoMsx/lXmkDcypgtAGwxI7TbVc3vct4cuQQqtdblMNCIE/CpfDFf71RFhpS7ruvZZp9i4trlvN9KTL0WAMOsTKG4YuLLNQYD07GNNuziPIvX2jYLpc6YtVcPZLD6wES0TUVOUkzHW947MZqQbGyU4E9geNl14Kat9rYhPLImwHMAapBVNDF9BId/cjRErWKglFsWe1l6++sllLrmn52gZv1tvALqAD8Kf9FWGBKqGBH76l/JTuM9/60LpWJPyHwTc9Tai72muuecTCYXhNwUnv7o9+TkHPDOkk7MN684RfZBAlkUKvTjnIpIb46Tqg== Received: from AM0PR03MB4148.eurprd03.prod.outlook.com (20.176.214.210) by AM0PR03MB5698.eurprd03.prod.outlook.com (20.179.254.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1900.16; Tue, 21 May 2019 21:25:56 +0000 Received: from AM0PR03MB4148.eurprd03.prod.outlook.com ([fe80::55c5:599a:1f80:208a]) by AM0PR03MB4148.eurprd03.prod.outlook.com ([fe80::55c5:599a:1f80:208a%3]) with mapi id 15.20.1900.020; Tue, 21 May 2019 21:25:56 +0000 From: Volodymyr Babchuk To: "xen-devel@lists.xenproject.org" Thread-Topic: [PATCH v5 04/10] xen/arm: optee: add fast calls handling Thread-Index: AQHVEBvGPrXQFGAaGUmy4LWcqWGj5A== Date: Tue, 21 May 2019 21:25:56 +0000 Message-ID: <20190521212530.12706-5-volodymyr_babchuk@epam.com> References: <20190521212530.12706-1-volodymyr_babchuk@epam.com> In-Reply-To: <20190521212530.12706-1-volodymyr_babchuk@epam.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@epam.com; x-originating-ip: [85.223.209.22] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 646cf3e3-27d7-4962-b4ad-08d6de32e960 x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(5600141)(711020)(4605104)(2017052603328)(7193020); SRVR:AM0PR03MB5698; x-ms-traffictypediagnostic: AM0PR03MB5698: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:7691; x-forefront-prvs: 0044C17179 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(376002)(136003)(366004)(346002)(396003)(39860400002)(189003)(199004)(6512007)(6486002)(5640700003)(6436002)(68736007)(3846002)(2616005)(476003)(11346002)(6916009)(486006)(6116002)(76116006)(14444005)(256004)(64756008)(66476007)(478600001)(305945005)(66946007)(54906003)(316002)(7736002)(73956011)(71190400001)(72206003)(2501003)(66556008)(80792005)(71200400001)(66446008)(26005)(4326008)(14454004)(25786009)(102836004)(2906002)(55236004)(8936002)(81166006)(5660300002)(99286004)(66066001)(1076003)(36756003)(86362001)(53936002)(6506007)(81156014)(446003)(2351001)(186003)(8676002)(76176011); DIR:OUT; SFP:1101; SCL:1; SRVR:AM0PR03MB5698; H:AM0PR03MB4148.eurprd03.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: epam.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: algqNDD+eHYpT5LJOUtKPjVuHkHC9eQm81jH2dwsEEU8nvz3IYS3Fpi1voprqLkP3jwqvUdgkKtgok2BB221xKppq1n6fi7+C2Q2D+czur1DCNFSudC+5XV+C/tMBrCsEpmBeaF9frnIL0G9f3VZClBS65oBiTaOS1nGCg1Y/K13e8MTlpcl6az6QY3n1wK7cx5AxlPt02ZNWKSvt+Yw/qdIxlqI7O347+1gnNzkWh3Lco2unx/xSkPVFFhd5lhwHx7AVU/VOJmZZ/WQa8Gaw0MyeGkxdERW3V1U/Lc5viCXz91GdVOmJ1SYWX/zZYvrq2Rj3cQm/yLeUjt+T7R4xjbtSuj8EbwGw7iAuIMEFvM4/dD1Af3uM8R3nJDAoZctROB6m9TY04kSjcCLi33ue4aTjY9aMSrEm0Ss3HhKD7A= MIME-Version: 1.0 X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-Network-Message-Id: 646cf3e3-27d7-4962-b4ad-08d6de32e960 X-MS-Exchange-CrossTenant-originalarrivaltime: 21 May 2019 21:25:56.3714 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR03MB5698 Subject: [Xen-devel] [PATCH v5 04/10] xen/arm: optee: add fast calls handling X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: "tee-dev@lists.linaro.org" , Julien Grall , Stefano Stabellini , Volodymyr Babchuk Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch adds handling for the fast SMCs. As name suggests, those calls can't be preempted and are used for auxiliary tasks such as information retrieval. Most handlers are quite trivial, with exception for capabilities information. Capabilities exchange should be filtered out, so only caps known to mediator are used. Also mediator disables static SHM memory capability, because it can't share OP-TEE memory with a domain. Only domain can share memory with OP-TEE, so it ensures that OP-TEE supports dynamic SHM. Basically, static SHM is a reserved memory region which is always mapped into OP-TEE address space. It belongs to OP-TEE. Normally, NW is allowed to access there, so it can communicate with OP-TEE. On other hand, dynamic SHM is NW's own memory, which it can share with OP-TEE. OP-TEE maps this memory dynamically, when it wants to access it. Because mediator can't share one static SHM region with all guests, it just disables it for all of them. It is possible to make exception for Dom0, but it requires separate handling for buffers allocated from that region. Thus, it is not implemented yet. Signed-off-by: Volodymyr Babchuk Acked-by: Julien Gral --- All the patches to optee.c should be merged together. They were split to ease up review. But they depend heavily on each other. Changes from v4: - Handler does not use forward_call(). Instead it calls OP-TEE directly with arm_smccc_smc(). - Handler modifies only those guest registers that are should be touched according to OP-TEE protocol specification. - Added OPTEE_MEDIATOR_SMC_COUNT definition. Changes from v2: - Defined known capabilities explicitly - Fixed code style --- xen/arch/arm/tee/optee.c | 97 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/xen/arch/arm/tee/optee.c b/xen/arch/arm/tee/optee.c index e9b69bd2d2..6c51caa41a 100644 --- a/xen/arch/arm/tee/optee.c +++ b/xen/arch/arm/tee/optee.c @@ -32,9 +32,17 @@ #include #include +/* Number of SMCs known to the mediator */ +#define OPTEE_MEDIATOR_SMC_COUNT 11 + /* Client ID 0 is reserved for the hypervisor itself */ #define OPTEE_CLIENT_ID(domain) ((domain)->domain_id + 1) +#define OPTEE_KNOWN_NSEC_CAPS OPTEE_SMC_NSEC_CAP_UNIPROCESSOR +#define OPTEE_KNOWN_SEC_CAPS (OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM | \ + OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM | \ + OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) + /* Domain context */ struct optee_domain { }; @@ -120,22 +128,111 @@ static int optee_relinquish_resources(struct domain *d) return 0; } +static void handle_exchange_capabilities(struct cpu_user_regs *regs) +{ + struct arm_smccc_res resp; + uint32_t caps; + + /* Filter out unknown guest caps */ + caps = get_user_reg(regs, 1); + caps &= OPTEE_KNOWN_NSEC_CAPS; + + arm_smccc_smc(OPTEE_SMC_EXCHANGE_CAPABILITIES, caps, 0, 0, 0, 0, 0, + OPTEE_CLIENT_ID(current->domain), &resp); + if ( resp.a0 != OPTEE_SMC_RETURN_OK ) { + set_user_reg(regs, 0, resp.a0); + return; + } + + caps = resp.a1; + + /* Filter out unknown OP-TEE caps */ + caps &= OPTEE_KNOWN_SEC_CAPS; + + /* Drop static SHM_RPC cap */ + caps &= ~OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM; + + /* Don't allow guests to work without dynamic SHM */ + if ( !(caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) ) + { + set_user_reg(regs, 0, OPTEE_SMC_RETURN_ENOTAVAIL); + return; + } + + set_user_reg(regs, 0, OPTEE_SMC_RETURN_OK); + set_user_reg(regs, 1, caps); +} + static bool optee_handle_call(struct cpu_user_regs *regs) { + struct arm_smccc_res resp; + if ( !current->domain->arch.tee ) return false; switch ( get_user_reg(regs, 0) ) { case OPTEE_SMC_CALLS_COUNT: + set_user_reg(regs, 0, OPTEE_MEDIATOR_SMC_COUNT); + return true; + case OPTEE_SMC_CALLS_UID: + arm_smccc_smc(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, + OPTEE_CLIENT_ID(current->domain), &resp); + set_user_reg(regs, 0, resp.a0); + set_user_reg(regs, 1, resp.a1); + set_user_reg(regs, 2, resp.a2); + set_user_reg(regs, 3, resp.a3); + return true; + case OPTEE_SMC_CALLS_REVISION: + arm_smccc_smc(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, + OPTEE_CLIENT_ID(current->domain), &resp); + set_user_reg(regs, 0, resp.a0); + set_user_reg(regs, 1, resp.a1); + return true; + case OPTEE_SMC_CALL_GET_OS_UUID: + arm_smccc_smc(OPTEE_SMC_CALL_GET_OS_UUID, 0, 0, 0, 0, 0, 0, + OPTEE_CLIENT_ID(current->domain),&resp); + set_user_reg(regs, 0, resp.a0); + set_user_reg(regs, 1, resp.a1); + set_user_reg(regs, 2, resp.a2); + set_user_reg(regs, 3, resp.a3); + return true; + case OPTEE_SMC_CALL_GET_OS_REVISION: + arm_smccc_smc(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, + OPTEE_CLIENT_ID(current->domain), &resp); + set_user_reg(regs, 0, resp.a0); + set_user_reg(regs, 1, resp.a1); + return true; + case OPTEE_SMC_ENABLE_SHM_CACHE: + arm_smccc_smc(OPTEE_SMC_ENABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0, + OPTEE_CLIENT_ID(current->domain), &resp); + set_user_reg(regs, 0, resp.a0); + return true; + case OPTEE_SMC_DISABLE_SHM_CACHE: + arm_smccc_smc(OPTEE_SMC_ENABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0, + OPTEE_CLIENT_ID(current->domain), &resp); + set_user_reg(regs, 0, resp.a0); + if ( resp.a0 == OPTEE_SMC_RETURN_OK ) { + set_user_reg(regs, 1, resp.a1); + set_user_reg(regs, 2, resp.a2); + } + return true; + case OPTEE_SMC_GET_SHM_CONFIG: + /* No static SHM available for guests */ + set_user_reg(regs, 0, OPTEE_SMC_RETURN_ENOTAVAIL); + return true; + case OPTEE_SMC_EXCHANGE_CAPABILITIES: + handle_exchange_capabilities(regs); + return true; + case OPTEE_SMC_CALL_WITH_ARG: case OPTEE_SMC_CALL_RETURN_FROM_RPC: set_user_reg(regs, 0, OPTEE_SMC_RETURN_ENOTAVAIL);