Skip to contentSkip to conversation

generic TEE subsystem

PATCH Superseded Jens Wiklander 4 participants 11 messages May 15, 2015 – Jun 18, 2015

Subsystems (4)

v3 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v3 cover generic TEE subsystem

Hi,

This patch set introduces a generic TEE subsystem. The TEE subsystem will be able contain drivers for various TEE implementations. A TEE (Trusted Execution Environment) is a trusted OS running in some secure environment, for example, TrustZone on ARM cpus, or a separate secure co-processor etc.

Regarding use cases, TrustZone has traditionally been used for offloading secure tasks to the secure world. Examples include banking applications, Digital Rights Management (DRM), or specific secure solutions.

This TEE subsystem can serve a TEE driver for a Global Platform compliant TEE, but it's not limited to only Global Platform TEEs. One reason why I'm doing this to be able to get an OP-TEE (https://github.com/OP-TEE/optee_os)
driver upstream.

The first patch brings in the generic TEE subsystem which helps when writing a driver for a specific TEE, for example, OP-TEE.

The second patch is an OP-TEE driver which uses the subsystem to do its work.

Javier is also working on a driver for another TEE so we will soon have at least two TEE drivers under the TEE subsystem.

Questions:
* Where should we put this in the tree? I'm proposing drivers/tee Another place could be drivers/firmware/tee. I don't have a strong opinion on either place.

* What should we have in the .compatible field in FDT for the OP-TEE driver? I'm proposing "optee,optee-tz" as OP-TEE doesn't really have a vendor. OP-TEE isn't limited to TrustZone, it can run in other environments too so "optee-tz" could be a way of keeping different options apart. Does it make sense or should I use some other scheme?

* Who will maintain this? I'm willing to do it together with Javier.

This patch set has been prepared in cooperation with Javier Gonz?lez who proposed "Generic TrustZone Driver in Linux Kernel" patches 28 Nov 2014,
https://lwn.net/Articles/623380/ . We've since then changed the scope to TEE instead of TrustZone.

We have discussed the design on tee-dev at lists.linaro.org (archive at
https://lists.linaro.org/pipermail/tee-dev/) with people from other companies, including Valentin Manea <[email protected]>, Emmanuel MICHEL <[email protected]>,
Jean-michel DELORME <[email protected]>,
and Joakim Bech <[email protected]>. Our main concern has been to agree on something that is generic enough to support many different TEEs while still keeping the interface together.

Open issues from previous review (on v1):
* Comment from Arnd Bergmann on several empty dma-buf callbacks:
I haven't planned fill in more in those functions for this patch set. Fixing those empty bma-buf callbacks requires changes in dma-buf, can we take that outside this patch set?

* There has been some concerns about TEE_IOC_CMD, hopefully the OP-TEE driver will make the purpose more clear now.

v3:
* Rebased on 4.1-rc3 (dma_buf_export() API change)
* A couple of small sparse fixes
* Documents bindings for OP-TEE driver
* Updated MAINTAINERS

v2:
* Replaced the stubbed OP-TEE driver with a real OP-TEE driver
* Removed most APIs not needed by OP-TEE in current state
* Update Documentation/ioctl/ioctl-number.txt with correct path to tee.h
* Rename tee_shm_pool_alloc_cma() to tee_shm_pool_alloc()
* Moved tee.h into include/uapi/linux/
* Redefined tee.h IOCTL macros to be directly based on _IOR and friends
* Removed version info on the API to user space, a data blob which can contain an UUID is left for user space to be able to tell which protocol to use in TEE_IOC_CMD
* Changed user space exposed structures to only have types with __ prefix
* Dropped THIS_MODULE from tee_fops
* Reworked how the driver is registered and ref counted:
- moved from using an embedded struct miscdevice to an embedded struct device.
- uses an struct rw_semaphore as synchronization for driver detachment
- uses alloc/register pattern from TPM

Regards,
Jens

Jens Wiklander (2):
tee: generic TEE subsystem
tee: add OP-TEE driver

 Documentation/devicetree/bindings/optee/optee.txt  |  17 +
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 Documentation/ioctl/ioctl-number.txt               |   1 +
 MAINTAINERS                                        |  14 +
 drivers/Kconfig                                    |   2 +
 drivers/Makefile                                   |   1 +
 drivers/tee/Kconfig                                |  18 +
 drivers/tee/Makefile                               |   4 +
 drivers/tee/optee/Kconfig                          |  19 +
 drivers/tee/optee/Makefile                         |  13 +
 drivers/tee/optee/call.c                           | 294 ++++++++++++
 drivers/tee/optee/core.c                           | 509 ++++++++++++++++++++
 drivers/tee/optee/optee_private.h                  | 138 ++++++
 drivers/tee/optee/optee_smc.h                      | 510 +++++++++++++++++++++
 drivers/tee/optee/rpc.c                            | 282 ++++++++++++
 drivers/tee/optee/smc_a32.S                        |  30 ++
 drivers/tee/optee/smc_a64.S                        |  37 ++
 drivers/tee/optee/supp.c                           | 327 +++++++++++++
 drivers/tee/tee.c                                  | 338 ++++++++++++++
 drivers/tee/tee_private.h                          |  74 +++
 drivers/tee/tee_shm.c                              | 327 +++++++++++++
 drivers/tee/tee_shm_pool.c                         | 246 ++++++++++
 include/linux/tee_drv.h                            | 281 ++++++++++++
 include/uapi/linux/optee_msg.h                     | 368 +++++++++++++++
 include/uapi/linux/tee.h                           | 118 +++++
 25 files changed, 3969 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/optee/optee.txt
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/call.c
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpc.c
 create mode 100644 drivers/tee/optee/smc_a32.S
 create mode 100644 drivers/tee/optee/smc_a64.S
 create mode 100644 drivers/tee/optee/supp.c
 create mode 100644 drivers/tee/tee.c
 create mode 100644 drivers/tee/tee_private.h
 create mode 100644 drivers/tee/tee_shm.c
 create mode 100644 drivers/tee/tee_shm_pool.c
 create mode 100644 include/linux/tee_drv.h
 create mode 100644 include/uapi/linux/optee_msg.h
 create mode 100644 include/uapi/linux/tee.h
Signature · 1 line
1.9.1
#1 lore raw

PATCH v3 1/2 tee: generic TEE subsystem

Initial patch for generic TEE subsystem.
This subsystem provides:
* Registration/un-registration of TEE drivers.
* Shared memory between normal world and secure world.
* Ioctl interface for interaction with user space.

A TEE (Trusted Execution Environment) driver is a driver that interfaces with a trusted OS running in some secure environment, for example, TrustZone on ARM cpus, or a separate secure co-processor etc.

To avoid putting unnecessary restrictions on the TEE driver and the trusted OS the TEE_IOC_CMD passes an opaque buffer to the TEE driver to facilitate a communication channel between user space and the trusted OS.

The TEE subsystem can serve a TEE driver for a Global Platform compliant TEE, but it's not limited to only Global Platform TEEs.

This patch builds on other similar implementations trying to solve the same problem:
* "optee_linuxdriver" by among others
Jean-michel DELORME<[email protected]> and Emmanuel MICHEL <[email protected]>
* "Generic TrustZone Driver" by Javier Gonz?lez <[email protected]>

---
 Documentation/ioctl/ioctl-number.txt |   1 +
 MAINTAINERS                          |   8 +
 drivers/Kconfig                      |   2 +
 drivers/Makefile                     |   1 +
 drivers/tee/Kconfig                  |   8 +
 drivers/tee/Makefile                 |   3 +
 drivers/tee/tee.c                    | 338 +++++++++++++++++++++++++++++++++++
 drivers/tee/tee_private.h            |  74 ++++++++
 drivers/tee/tee_shm.c                | 327 +++++++++++++++++++++++++++++++++
 drivers/tee/tee_shm_pool.c           | 246 +++++++++++++++++++++++++
 include/linux/tee_drv.h              | 281 +++++++++++++++++++++++++++++
 include/uapi/linux/tee.h             | 118 ++++++++++++
 12 files changed, 1407 insertions(+)
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/tee.c
 create mode 100644 drivers/tee/tee_private.h
 create mode 100644 drivers/tee/tee_shm.c
 create mode 100644 drivers/tee/tee_shm_pool.c
 create mode 100644 include/linux/tee_drv.h
 create mode 100644 include/uapi/linux/tee.h
Documentation/ioctl/ioctl-number.txt +1 -0
index 51f4221..f54dfc02 100644
@@ -301,6 +301,7 @@ Code  Seq#(hex)	Include File		Comments
 0xA3	80-8F	Port ACL		in development:
 					<mailto:[email protected]>
 0xA3	90-9F	linux/dtlk.h
+0xA4	00-1F	uapi/linux/tee.h	Generic TEE subsystem
 0xAB	00-1F	linux/nbd.h
 0xAC	00-1F	linux/raw.h
 0xAD	00	Netfilter device	in development:
MAINTAINERS +8 -0
index 590304b..dfcc9cc 100644
@@ -8613,6 +8613,14 @@ S:	Maintained
 F:	include/linux/mmc/dw_mmc.h
 F:	drivers/mmc/host/dw_mmc*
 
+TEE SUBSYSTEM
+M:	Jens Wiklander <[email protected]>
+M:	Javier Gonz?lez <[email protected]>
+S:	Maintained
+F:	include/linux/tee_drv.h
+F:	include/uapi/linux/tee.h
+F:	drivers/tee/
+
 THUNDERBOLT DRIVER
 M:	Andreas Noever <[email protected]>
 S:	Maintained
drivers/Kconfig +2 -0
index c0cc96b..7510f69 100644
@@ -182,4 +182,6 @@ source "drivers/thunderbolt/Kconfig"
 
 source "drivers/android/Kconfig"
 
+source "drivers/tee/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index 46d2554..852f0af 100644
@@ -165,3 +165,4 @@ obj-$(CONFIG_RAS)		+= ras/
 obj-$(CONFIG_THUNDERBOLT)	+= thunderbolt/
 obj-$(CONFIG_CORESIGHT)		+= hwtracing/coresight/
 obj-$(CONFIG_ANDROID)		+= android/
+obj-$(CONFIG_TEE)		+= tee/
drivers/tee/Kconfig new file +8 -0
new file mode 100644
index 0000000..64a8cd7
@@ -0,0 +1,8 @@
+# Generic Trusted Execution Environment Configuration
+config TEE
+	bool "Trusted Execution Environment support"
+	default n
+	select DMA_SHARED_BUFFER
+	help
+	  This implements a generic interface towards a Trusted Execution
+	  Environment (TEE).
drivers/tee/Makefile new file +3 -0
new file mode 100644
index 0000000..60d2dab
@@ -0,0 +1,3 @@
+obj-y += tee.o
+obj-y += tee_shm.o
+obj-y += tee_shm_pool.o
drivers/tee/tee.c new file +338 -0

338 lines not on this page · raw message

drivers/tee/tee_private.h new file +74 -0

74 lines not on this page · raw message

drivers/tee/tee_shm.c new file +327 -0

327 lines not on this page · raw message

drivers/tee/tee_shm_pool.c new file +246 -0

246 lines not on this page · raw message

include/linux/tee_drv.h new file +281 -0

281 lines not on this page · raw message

include/uapi/linux/tee.h new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw

PATCH v3 2/2 tee: add OP-TEE driver

In reply to Jens Wiklander

Adds a OP-TEE driver which also can be compiled as a loadable module.

* Targets ARM and ARM64
* Supports using reserved memory from OP-TEE as shared memory
* CMA as shared memory is optional and only tried if OP-TEE doesn't supply a reserved shared memory region
* Probes OP-TEE version using SMCs
* Accepts requests on privileged and unprivileged device
* Uses OPTEE message protocol version 2

---
 Documentation/devicetree/bindings/optee/optee.txt  |  17 +
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 MAINTAINERS                                        |   6 +
 drivers/tee/Kconfig                                |  10 +
 drivers/tee/Makefile                               |   1 +
 drivers/tee/optee/Kconfig                          |  19 +
 drivers/tee/optee/Makefile                         |  13 +
 drivers/tee/optee/call.c                           | 294 ++++++++++++
 drivers/tee/optee/core.c                           | 509 ++++++++++++++++++++
 drivers/tee/optee/optee_private.h                  | 138 ++++++
 drivers/tee/optee/optee_smc.h                      | 510 +++++++++++++++++++++
 drivers/tee/optee/rpc.c                            | 282 ++++++++++++
 drivers/tee/optee/smc_a32.S                        |  30 ++
 drivers/tee/optee/smc_a64.S                        |  37 ++
 drivers/tee/optee/supp.c                           | 327 +++++++++++++
 include/uapi/linux/optee_msg.h                     | 368 +++++++++++++++
 16 files changed, 2562 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/optee/optee.txt
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/call.c
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpc.c
 create mode 100644 drivers/tee/optee/smc_a32.S
 create mode 100644 drivers/tee/optee/smc_a64.S
 create mode 100644 drivers/tee/optee/supp.c
 create mode 100644 include/uapi/linux/optee_msg.h
Documentation/devicetree/bindings/optee/optee.txt new file +17 -0
new file mode 100644
index 0000000..8cea829
@@ -0,0 +1,17 @@
+OP-TEE Device Tree Bindings
+
+OP-TEE is a piece of software using hardware features to provide a Trusted
+Execution Environment. The security can be provided with ARM TrustZone, but
+also by virtualization or a separate chip. As there's no single OP-TEE
+vendor we're using "optee" as the first part of compatible propterty,
+indicating the OP-TEE protocol is used when communicating with the secure
+world.
+
+* OP-TEE based on ARM TrustZone required properties:
+
+- compatible="optee,optee-tz"
+
+Example:
+	optee {
+		compatible="optee,optee-tz";
+	};
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 8033919..17c2a7e 100644
@@ -141,6 +141,7 @@ nvidia	NVIDIA
 nxp	NXP Semiconductors
 onnn	ON Semiconductor Corp.
 opencores	OpenCores.org
+optee	OP-TEE, Open Portable Trusted Execution Environment
 ortustech	Ortus Technology Co., Ltd.
 ovti	OmniVision Technologies
 panasonic	Panasonic Corporation
MAINTAINERS +6 -0
index dfcc9cc..1234695 100644
@@ -7295,6 +7295,12 @@ F:	arch/*/oprofile/
 F:	drivers/oprofile/
 F:	include/linux/oprofile.h
 
+OP-TEE DRIVER
+M:	Jens Wiklander <[email protected]>
+S:	Maintained
+F:	include/uapi/linux/optee_msg.h
+F:	drivers/tee/optee/
+
 ORACLE CLUSTER FILESYSTEM 2 (OCFS2)
 M:	Mark Fasheh <[email protected]>
 M:	Joel Becker <[email protected]>
drivers/tee/Kconfig +10 -0
index 64a8cd7..b269276 100644
@@ -6,3 +6,13 @@ config TEE
 	help
 	  This implements a generic interface towards a Trusted Execution
 	  Environment (TEE).
+
+if TEE
+
+menu "TEE drivers"
+
+source "drivers/tee/optee/Kconfig"
+
+endmenu
+
+endif
drivers/tee/Makefile +1 -0
index 60d2dab..53f3c76 100644
@@ -1,3 +1,4 @@
 obj-y += tee.o
 obj-y += tee_shm.o
 obj-y += tee_shm_pool.o
+obj-$(CONFIG_OPTEE) += optee/
drivers/tee/optee/Kconfig new file +19 -0
new file mode 100644
index 0000000..3faa855
@@ -0,0 +1,19 @@
+# OP-TEE Trusted Execution Environment Configuration
+config OPTEE
+	tristate "OP-TEE"
+	default n
+	depends on ARM || ARM64
+	help
+	  This implements the OP-TEE Trusted Execution Environment (TEE)
+	  driver.
+
+if OPTEE
+menu "OP-TEE options"
+config OPTEE_USE_CMA
+	bool "Use CMA"
+	default n
+	select DMA_CMA
+	help
+	  Configures OP-TEE driver to use CMA for shared memory allocations.
+endmenu
+endif
drivers/tee/optee/Makefile new file +13 -0
new file mode 100644
index 0000000..096651d
@@ -0,0 +1,13 @@
+obj-$(CONFIG_OPTEE) += optee.o
+optee-objs += core.o
+optee-objs += call.o
+ifdef CONFIG_ARM
+plus_sec := $(call as-instr,.arch_extension sec,+sec)
+AFLAGS_smc_a32.o := -Wa,-march=armv7-a$(plus_sec)
+optee-objs += smc_a32.o
+endif
+ifdef CONFIG_ARM64
+optee-objs += smc_a64.o
+endif
+optee-objs += rpc.o
+optee-objs += supp.o
drivers/tee/optee/call.c new file +294 -0

294 lines not on this page · raw message

drivers/tee/optee/core.c new file +509 -0

509 lines not on this page · raw message

drivers/tee/optee/optee_private.h new file +138 -0

138 lines not on this page · raw message

drivers/tee/optee/optee_smc.h new file +510 -0

510 lines not on this page · raw message

drivers/tee/optee/rpc.c new file +282 -0

282 lines not on this page · raw message

drivers/tee/optee/smc_a32.S new file +30 -0
new file mode 100644
index 0000000..30fe0e5
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#include <linux/linkage.h>
+
+	.text
+	.balign 4
+	.code 32
+
+	/* void optee_smc(struct optee_smc_param *param); */
+	.globl	optee_smc
+ENTRY(optee_smc)
+	push	{r4-r8, lr}
+	mov	r8, r0
+	ldm	r8, {r0-r7}
+.arch_extension sec
+	smc	#0
+	stm	r8, {r0-r7}
+	pop	{r4-r8, pc}
+ENDPROC(optee_smc)
drivers/tee/optee/smc_a64.S new file +37 -0
new file mode 100644
index 0000000..458a138
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License Version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+#include <linux/linkage.h>
+
+	.text
+
+#define SMC_PARAM_W0_OFFS	0
+#define SMC_PARAM_W2_OFFS	8
+#define SMC_PARAM_W4_OFFS	16
+#define SMC_PARAM_W6_OFFS	24
+
+	/* void optee_smc(struct smc_param *param); */
+	.globl	optee_smc
+ENTRY(optee_smc)
+	stp	x28, x30, [sp, #-16]!
+	mov	x28, x0
+	ldp	w0, w1, [x28, #SMC_PARAM_W0_OFFS]
+	ldp	w2, w3, [x28, #SMC_PARAM_W2_OFFS]
+	ldp	w4, w5, [x28, #SMC_PARAM_W4_OFFS]
+	ldp	w6, w7, [x28, #SMC_PARAM_W6_OFFS]
+	smc	#0
+	stp	w0, w1, [x28, #SMC_PARAM_W0_OFFS]
+	stp	w2, w3, [x28, #SMC_PARAM_W2_OFFS]
+	ldp	x28, x30, [sp], #16
+	ret
+ENDPROC(optee_smc)
drivers/tee/optee/supp.c new file +327 -0

327 lines not on this page · raw message

include/uapi/linux/optee_msg.h new file +368 -0

368 lines not on this page · raw message

Signature · 1 line
1.9.1
#3 lore raw

Re: [PATCH V3 2/2] tee: add OP-TEE driver

Hi,

Quoted 6 lines · Jens Wiklander

Adds a OP-TEE driver which also can be compiled as a loadable module.

* Targets ARM and ARM64
* Supports using reserved memory from OP-TEE as shared memory
* CMA as shared memory is optional and only tried if OP-TEE doesn't supply a reserved shared memory region

How does OP-TEE provide that reserved memory? How is that described in DT/UEFI to the OS (e.g. is there a memreserve, or is the memory not described at all)?

Quoted 7 lines

* Probes OP-TEE version using SMCs
* Accepts requests on privileged and unprivileged device
* Uses OPTEE message protocol version 2

---
 Documentation/devicetree/bindings/optee/optee.txt  |  17 +

I'm concerned that there's no documentation regarding the interface exposed to userspace, for neither rationale nor usage.

I'm also very concerned that the interface exposed to userspace is hideously low-level. Surely we'd expect kernel-side drivers to be doing the bulk of direct communication to the OP-TEE instance? In the lack of a provided rationale I don't see why the current messaging interface would make sense.

Quoted 35 lines
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 MAINTAINERS                                        |   6 +
 drivers/tee/Kconfig                                |  10 +
 drivers/tee/Makefile                               |   1 +
 drivers/tee/optee/Kconfig                          |  19 +
 drivers/tee/optee/Makefile                         |  13 +
 drivers/tee/optee/call.c                           | 294 ++++++++++++
 drivers/tee/optee/core.c                           | 509 ++++++++++++++++++++
 drivers/tee/optee/optee_private.h                  | 138 ++++++
 drivers/tee/optee/optee_smc.h                      | 510 +++++++++++++++++++++
 drivers/tee/optee/rpc.c                            | 282 ++++++++++++
 drivers/tee/optee/smc_a32.S                        |  30 ++
 drivers/tee/optee/smc_a64.S                        |  37 ++
 drivers/tee/optee/supp.c                           | 327 +++++++++++++
 include/uapi/linux/optee_msg.h                     | 368 +++++++++++++++
 16 files changed, 2562 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/optee/optee.txt
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/call.c
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpc.c
 create mode 100644 drivers/tee/optee/smc_a32.S
 create mode 100644 drivers/tee/optee/smc_a64.S
 create mode 100644 drivers/tee/optee/supp.c
 create mode 100644 include/uapi/linux/optee_msg.h
Documentation/devicetree/bindings/optee/optee.txt new file +0 -0
new file mode 100644
index 0000000..8cea829

Documentation/devicetree/bindings/optee/optee.txt line 6

Quoted 6 lines
Documentation/devicetree/bindings/optee/optee.txt new file +6 -0
@@ -0,0 +1,17 @@
+OP-TEE Device Tree Bindings
+
+OP-TEE is a piece of software using hardware features to provide a Trusted
+Execution Environment. The security can be provided with ARM TrustZone, but
+also by virtualization or a separate chip. As there's no single OP-TEE
+vendor we're using "optee" as the first part of compatible propterty,

s/propterty/property/

Documentation/devicetree/bindings/optee/optee.txt line 17

Quoted 11 lines
Documentation/devicetree/bindings/optee/optee.txt new file +11 -0
+indicating the OP-TEE protocol is used when communicating with the secure
+world.
+
+* OP-TEE based on ARM TrustZone required properties:
+
+- compatible="optee,optee-tz"
+
+Example:
+       optee {
+               compatible="optee,optee-tz";
+       };

What does the OP-TEE protocol give in the way of discoverability? Is it expected that the specific implementation and/or features will be detected dynamically?

Where can I find documentation on the protocol?

MAINTAINERS

Quoted 14 lines
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 8033919..17c2a7e 100644
@@ -141,6 +141,7 @@ nvidia      NVIDIA
 nxp    NXP Semiconductors
 onnn   ON Semiconductor Corp.
 opencores      OpenCores.org
+optee  OP-TEE, Open Portable Trusted Execution Environment
 ortustech      Ortus Technology Co., Ltd.
 ovti   OmniVision Technologies
 panasonic      Panasonic Corporation
MAINTAINERS +0 -0
index dfcc9cc..1234695 100644

Please split the DT binding parts into a separate patch, at the start of the series.

Quoted 7 lines
drivers/tee/optee/Makefile new file +1 -0
new file mode 100644
index 0000000..096651d
@@ -0,0 +1,13 @@
+obj-$(CONFIG_OPTEE) += optee.o

drivers/tee/optee/Makefile line 11

Quoted 10 lines
drivers/tee/optee/Makefile new file +10 -0
+optee-objs += core.o
+optee-objs += call.o
+ifdef CONFIG_ARM
+plus_sec := $(call as-instr,.arch_extension sec,+sec)
+AFLAGS_smc_a32.o := -Wa,-march=armv7-a$(plus_sec)
+optee-objs += smc_a32.o
+endif
+ifdef CONFIG_ARM64
+optee-objs += smc_a64.o
+endif

The assembly objects should probably live under the relevant arch/ folders, and can probably be shared with clients for other services compliant with the SMC Calling Conventions.

drivers/tee/optee/Makefile

Quoted 10 lines
drivers/tee/optee/Makefile new file +10 -0
+static void optee_call_lock(struct optee_call_sync *callsync)
+{
+       mutex_lock(&callsync->mutex);
+}
+
+static void optee_call_lock_wait_completion(struct optee_call_sync *callsync)
+{
+       /*
+        * Release the lock until "something happens" and then reacquire it
+        * again.

When you say you're waiting until "something happens", you really are waiting until something happens. The quotes aren't helpful, please drop them.

Quoted 16 lines
drivers/tee/optee/Makefile new file +16 -0
+        *
+        * This is needed when TEE returns "busy" and we need to try again
+        * later.
+        */
+       callsync->c_waiters++;
+       mutex_unlock(&callsync->mutex);
+       /*
+        * Wait at most one second. Secure world is normally never busy
+        * more than that so we should normally never timeout.
+        */
+       wait_for_completion_timeout(&callsync->c, HZ);
+       mutex_lock(&callsync->mutex);
+       callsync->c_waiters--;
+}
+
+static void optee_call_unlock(struct optee_call_sync *callsync)

drivers/tee/optee/Makefile

Quoted 10 lines
drivers/tee/optee/Makefile new file +10 -0
+{
+       /*
+        * If at least one thread is waiting for "something to happen" let
+        * one thread know that "something has happened".
+        */
+       if (callsync->c_waiters)
+               complete(&callsync->c);
+       mutex_unlock(&callsync->mutex);
+}
+

You can get rid of the c_waiters variable entirely, as complete() is safe to call when the completion has an empty waiters list (and the manipulation of c_waiters is racy anyway...).

Also, I think you need complete_all(&callsync->c) if more than two concurrent calls are possible. Otherwise one call might block another indefinitely.

drivers/tee/optee/Makefile

Quoted 10 lines
drivers/tee/optee/Makefile new file +10 -0
+static int optee_arg_from_user(struct opteem_arg *arg, size_t size,
+                       struct tee_shm **put_shm)
+{
+       struct opteem_param *param;
+       size_t n;
+
+       if (!arg->num_params || !put_shm)
+               return -EINVAL;
+
+       param = OPTEEM_GET_PARAMS(arg);

OPTEEM is a little opaque. OPTEE_MSG would be easier to grasp.

[...]

Quoted 20 lines
drivers/tee/optee/core.c new file +14 -0
new file mode 100644
index 0000000..b3f8b92d
@@ -0,0 +1,509 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#include <linux/types.h>

drivers/tee/optee/core.c line 24

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+#include <linux/string.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+#include <linux/dma-contiguous.h>
+#ifdef CONFIG_OPTEE_USE_CMA
+#include <linux/cma.h>
+#endif

Surely this ifdeffery isn't necessary?

[...]

drivers/tee/optee/core.c line 337

Quoted 6 lines
drivers/tee/optee/core.c new file +6 -0
+static struct tee_shm_pool *optee_config_shm_ioremap(struct device *dev,
+                       void __iomem **ioremaped_shm)
+{
+       struct optee_smc_param param = { .a0 = OPTEE_SMC_GET_SHM_CONFIG };
+       struct tee_shm_pool *pool;
+       u_long vaddr;

Why not plain unsigned long?

[...]

drivers/tee/optee/core.c line 35

Quoted 5 lines
drivers/tee/optee/core.c new file +5 -0
+/*
+ * This file is exported by OP-TEE and is in kept in sync between secure
+ * world and normal world kernel driver. We're following ARM SMC Calling
+ * Convention as specified in
+ * http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html

The values defined in the SMC Calling Conventions (which have nothing to do with OP-TEE as such), we should probably prefix with SMCCC_, and have in a shared file.

drivers/tee/optee/core.c line 44

Quoted 9 lines
drivers/tee/optee/core.c new file +9 -0
+ *
+ * This file depends on optee_msg.h being included to expand the SMC id
+ * macros below.
+ */
+
+#define OPTEE_SMC_32                   0
+#define OPTEE_SMC_64                   0x40000000
+#define OPTEE_SMC_FAST_CALL            0x80000000
+#define OPTEE_SMC_STD_CALL             0

The zero cases look a bit odd here. They might be better-documented if you defined them with shifts, e.g.

#define SMCCC_SMC_32                    (0 << 30)
#define SMCCC_SMC_64                    (1 << 30)
#define SMCCC_FAST_CALL                 (1 << 31)
#define SMCCC_STD_CALL                  (0 << 31)

[...]

drivers/tee/optee/core.c

Quoted 5 lines
drivers/tee/optee/core.c new file +5 -0
+/*
+ * Cache settings for shared memory
+ */
+#define OPTEE_SMC_SHM_NONCACHED                0ULL
+#define OPTEE_SMC_SHM_CACHED           1ULL

What precise set of memory attributes do these imply?

[...]

drivers/tee/optee/core.c

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+/*
+ * Same values as TEE_PARAM_* from TEE Internal API
+ */
+#define OPTEEM_ATTR_TYPE_NONE          0
+#define OPTEEM_ATTR_TYPE_VALUE_INPUT   1
+#define OPTEEM_ATTR_TYPE_VALUE_OUTPUT  2
+#define OPTEEM_ATTR_TYPE_VALUE_INOUT   3
+#define OPTEEM_ATTR_TYPE_MEMREF_INPUT  5
+#define OPTEEM_ATTR_TYPE_MEMREF_OUTPUT 6
+#define OPTEEM_ATTR_TYPE_MEMREF_INOUT  7

Are these well-defined ABI values?

As mentioned previously, OPTEEM_* is opaque, and OPTEE_MSG_* would be far clearer.

Quoted 8 lines
drivers/tee/optee/core.c new file +8 -0
+/**
+ * struct opteem_param_memref - memory reference
+ * @buf_ptr:   Address of the buffer
+ * @size:      Size of the buffer
+ * @shm_ref:   Shared memory reference only used by normal world
+ *
+ * Secure and normal world communicates pointers as physical address
+ * instead of the virtual address. This is because secure and normal world

drivers/tee/optee/core.c

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+ * have completely independent memory mapping. Normal world can even have a
+ * hypervisor which need to translate the guest physical address (AKA IPA
+ * in ARM documentation) to a real physical address before passing the
+ * structure to secure world.
+ */
+struct opteem_param_memref {
+       __u64 buf_ptr;
+       __u64 size;
+       __u64 shm_ref;
+};

Why does this mention physical addresses at all? What does that have to do with userspace?

What is the shm_ref, and who allocates it?

There should really be some documentation for this.

drivers/tee/optee/core.c

Quoted 11 lines
drivers/tee/optee/core.c new file +11 -0
+/**
+ * struct opteem_param_value - values
+ * @a: first value
+ * @b: second value
+ * @c: third value
+ */
+struct opteem_param_value {
+       __u64 a;
+       __u64 b;
+       __u64 c;
+};

Are OP-TEE messages defined to only ever take three parameters?

[...]

drivers/tee/optee/core.c

Quoted 12 lines
drivers/tee/optee/core.c new file +12 -0
+/**
+ * struct optee_cmd_prefix - initial header for all user space buffers
+ * @func_id:   Function Id OPTEEM_FUNCID_* below
+ * @pad:       padding to make the struct size a multiple of 8 bytes
+ *
+ * This struct is 8 byte aligned since it's always followed by a struct
+ * opteem_arg which requires 8 byte alignment.
+ */
+struct opteem_cmd_prefix {
+       __u32 func_id;
+       __u32 pad __aligned(8);
+};

Shouldn't the alignment be applied to the struct as a whole rather than the final field?

Quoted 12 lines
drivers/tee/optee/core.c new file +12 -0
+/*
+ * Sleep mutex, helper for secure world to implement a sleeping mutex.
+ * struct opteem_arg::func     one of OPTEEM_RPC_SLEEP_MUTEX_* below
+ *
+ * OPTEEM_RPC_SLEEP_MUTEX_WAIT
+ * [in] param[0].value .a sleep mutex key
+ *                     .b wait tick
+ * [not used] param[1]
+ *
+ * OPTEEM_RPC_SLEEP_MUTEX_WAKEUP
+ * [in] param[0].value .a sleep mutex key
+ *                     .b wait after

drivers/tee/optee/core.c

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+ * [not used] param[1]
+ *
+ * OPTEEM_RPC_SLEEP_MUTEX_DELETE
+ * [in] param[0].value .a sleep mutex key
+ * [not used] param[1]
+ */
+#define OPTEEM_RPC_SLEEP_MUTEX_WAIT    0
+#define OPTEEM_RPC_SLEEP_MUTEX_WAKEUP  1
+#define OPTEEM_RPC_SLEEP_MUTEX_DELETE  2
+#define OPTEEM_RPC_CMD_SLEEP_MUTEX     4

I'm lost. Why are mutexes exposed to userspace or the secure world in such a manner?

Thanks,
Mark.

#4 lore raw

Re: [PATCH V3 2/2] tee: add OP-TEE driver

Hi,

Quoted 5 lines · Mark Rutland

Hi,

Quoted 2 lines · Jens Wiklander

Adds a OP-TEE driver which also can be compiled as a loadable module.

Quoted 8 lines
Quoted 4 lines

* Targets ARM and ARM64
* Supports using reserved memory from OP-TEE as shared memory
* CMA as shared memory is optional and only tried if OP-TEE doesn't supply a reserved shared memory region

How does OP-TEE provide that reserved memory? How is that described in DT/UEFI to the OS (e.g. is there a memreserve, or is the memory not described at all)?

It's either memreserve or not described at all. This should only be needed when secure world is limited in which memory it can use for shared memory. Currently all OP-TEE ports uses reserved shared memory, but we're moving away from it to avoid the problem with updating DT.

Quoted 11 lines
Quoted 8 lines

* Probes OP-TEE version using SMCs
* Accepts requests on privileged and unprivileged device
* Uses OPTEE message protocol version 2

---
 Documentation/devicetree/bindings/optee/optee.txt  |  17 +

I'm concerned that there's no documentation regarding the interface exposed to userspace, for neither rationale nor usage.

OK, I'll add something.

Quoted 6 lines

I'm also very concerned that the interface exposed to userspace is hideously low-level. Surely we'd expect kernel-side drivers to be doing the bulk of direct communication to the OP-TEE instance? In the lack of a provided rationale I don't see why the current messaging interface would make sense.

The kernel-side does all the direct communication since there's where the SMC is done, but one level above most of the communication is terminated in user space. Loading of Trusted Applications and other file system access is done in by a helper process in user space, tee-supplicant. A large part of the OP-TEE message protocol is transparent to the kernel.

We're trying to not exclude any TEE implementation by having this low level TEE_IOC_CMD instead of a high level interface. The problem with the different TEEs is that they are designed differently and we haven't been able to find a high level interface that suits all. Applications aren't expected to use TEE_IOC_CMD directly, instead there's supposed to be a client lib wich wraps the kernel interface and provides a higher level interface, for instance a Global Platform Client API in the case of a GP TEE.

For OP-TEE we're using the same protocol all the way down to user space, the advantage is that it's only one protocol to keep track of and we don't need to translate the entire message (we do need to copy it, excluding the payload) each time the message is passed to the next memory space. In the presence of a hypervisor we have
user space -> kernel -> hypervisor -> secure world Unfortunately some fields has a different meaning in user space and kernel space. I'll address this in the documentation.

The OP-TEE helper process, tee-supplicant, is specific to only OP-TEE. Other TEEs uses helper processes too, but what they do depend on the design of the TEE. As a consequence more or less all TEEs needs something specific for that particular TEE in user space to be fully functional.

To summarize, the current assumption is that all TEEs can't use the same high level interface. Instead we need to provide a way for each TEE to have their own low level interface which should be wrapped in a user space library to provide a more reasonable interface to the client application.

Quoted 45 lines
Quoted 36 lines
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 MAINTAINERS                                        |   6 +
 drivers/tee/Kconfig                                |  10 +
 drivers/tee/Makefile                               |   1 +
 drivers/tee/optee/Kconfig                          |  19 +
 drivers/tee/optee/Makefile                         |  13 +
 drivers/tee/optee/call.c                           | 294 ++++++++++++
 drivers/tee/optee/core.c                           | 509 ++++++++++++++++++++
 drivers/tee/optee/optee_private.h                  | 138 ++++++
 drivers/tee/optee/optee_smc.h                      | 510 +++++++++++++++++++++
 drivers/tee/optee/rpc.c                            | 282 ++++++++++++
 drivers/tee/optee/smc_a32.S                        |  30 ++
 drivers/tee/optee/smc_a64.S                        |  37 ++
 drivers/tee/optee/supp.c                           | 327 +++++++++++++
 include/uapi/linux/optee_msg.h                     | 368 +++++++++++++++
 16 files changed, 2562 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/optee/optee.txt
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/call.c
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpc.c
 create mode 100644 drivers/tee/optee/smc_a32.S
 create mode 100644 drivers/tee/optee/smc_a64.S
 create mode 100644 drivers/tee/optee/supp.c
 create mode 100644 include/uapi/linux/optee_msg.h
Documentation/devicetree/bindings/optee/optee.txt new file +0 -0
new file mode 100644
index 0000000..8cea829

Documentation/devicetree/bindings/optee/optee.txt line 6

Quoted 6 lines
Documentation/devicetree/bindings/optee/optee.txt new file +6 -0
@@ -0,0 +1,17 @@
+OP-TEE Device Tree Bindings
+
+OP-TEE is a piece of software using hardware features to provide a Trusted
+Execution Environment. The security can be provided with ARM TrustZone, but
+also by virtualization or a separate chip. As there's no single OP-TEE
+vendor we're using "optee" as the first part of compatible propterty,

s/propterty/property/

Quoted 15 lines

Documentation/devicetree/bindings/optee/optee.txt line 17

Quoted 11 lines
Documentation/devicetree/bindings/optee/optee.txt new file +11 -0
+indicating the OP-TEE protocol is used when communicating with the secure
+world.
+
+* OP-TEE based on ARM TrustZone required properties:
+
+- compatible="optee,optee-tz"
+
+Example:
+       optee {
+               compatible="optee,optee-tz";
+       };

What does the OP-TEE protocol give in the way of discoverability? Is it expected that the specific implementation and/or features will be detected dynamically?

We have OPTEEM_FUNCID_GET_OS_UUID and OPTEEM_FUNCID_GET_OS_REVISION which the client can use to identify which particular OP-TEE it's talking to. This is not so interesting for the driver, but the client may care when there's more than one TEE using the OP-TEE message protocol in a single system.

There's also OPTEEM_FUNCID_CALLS_UID and OPTEEM_FUNCID_CALLS_REVISION (required by SMC Calling Convention), but those are expected to return static values except OPTEEM_REVISION_MINOR which would be increased if some new message type is added in the future.

To summarize, OPTEEM_FUNCID_CALLS_* identifies the OP-TEE message protocol and OPTEEM_FUNCID_GET_OS_* identifies the OP-TEE OS implementation.

Quoted 2 lines

Where can I find documentation on the protocol?

The documentation is currently include/uapi/linux/optee_msg.h and drivers/tee/optee/optee_smc.h. I'll add something under Documentation.

There's more details at http://shorl.com/lubopribokygy , but that's not entirely updated with this driver.

Quoted 10 lines
Quoted 10 lines
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 8033919..17c2a7e 100644
@@ -141,6 +141,7 @@ nvidia      NVIDIA
 nxp    NXP Semiconductors
 onnn   ON Semiconductor Corp.
 opencores      OpenCores.org
+optee  OP-TEE, Open Portable Trusted Execution Environment
Quoted 8 lines

MAINTAINERS

Quoted 5 lines
Documentation/devicetree/bindings/vendor-prefixes.txt +0 -0
 ortustech      Ortus Technology Co., Ltd.
 ovti   OmniVision Technologies
 panasonic      Panasonic Corporation
MAINTAINERS +0 -0
index dfcc9cc..1234695 100644

Please split the DT binding parts into a separate patch, at the start of the series.

OK

Quoted 14 lines
Quoted 14 lines
drivers/tee/optee/Makefile new file +7 -0
new file mode 100644
index 0000000..096651d
@@ -0,0 +1,13 @@
+obj-$(CONFIG_OPTEE) += optee.o
+optee-objs += core.o
+optee-objs += call.o
+ifdef CONFIG_ARM
+plus_sec := $(call as-instr,.arch_extension sec,+sec)
+AFLAGS_smc_a32.o := -Wa,-march=armv7-a$(plus_sec)
+optee-objs += smc_a32.o
Quoted 8 lines

drivers/tee/optee/Makefile line 11

Quoted 4 lines
drivers/tee/optee/Makefile new file +4 -0
+endif
+ifdef CONFIG_ARM64
+optee-objs += smc_a64.o
+endif

The assembly objects should probably live under the relevant arch/ folders, and can probably be shared with clients for other services compliant with the SMC Calling Conventions.

OK, sounds good. Where should I put the smccc.h file to be able to share it between arch/arm and arch/arm64, under include/asm-generic?

Quoted 15 lines

drivers/tee/optee/Makefile

Quoted 11 lines
drivers/tee/optee/Makefile new file +10 -0
+static void optee_call_lock(struct optee_call_sync *callsync)
+{
+       mutex_lock(&callsync->mutex);
+}
+
+static void optee_call_lock_wait_completion(struct optee_call_sync *callsync)
+{
+       /*
+        * Release the lock until "something happens" and then reacquire it
+        * again.

When you say you're waiting until "something happens", you really are waiting until something happens. The quotes aren't helpful, please drop them.

OK

Quoted 23 lines
Quoted 23 lines
drivers/tee/optee/Makefile new file +22 -0
+        *
+        * This is needed when TEE returns "busy" and we need to try again
+        * later.
+        */
+       callsync->c_waiters++;
+       mutex_unlock(&callsync->mutex);
+       /*
+        * Wait at most one second. Secure world is normally never busy
+        * more than that so we should normally never timeout.
+        */
+       wait_for_completion_timeout(&callsync->c, HZ);
+       mutex_lock(&callsync->mutex);
+       callsync->c_waiters--;
+}
+
+static void optee_call_unlock(struct optee_call_sync *callsync)
+{
+       /*
+        * If at least one thread is waiting for "something to happen" let
+        * one thread know that "something has happened".
+        */
+       if (callsync->c_waiters)
Quoted 8 lines

drivers/tee/optee/Makefile

Quoted 4 lines
drivers/tee/optee/Makefile new file +4 -0
+               complete(&callsync->c);
+       mutex_unlock(&callsync->mutex);
+}
+

You can get rid of the c_waiters variable entirely, as complete() is safe to call when the completion has an empty waiters list (and the manipulation of c_waiters is racy anyway...).

OK

Quoted 4 lines

Also, I think you need complete_all(&callsync->c) if more than two concurrent calls are possible. Otherwise one call might block another indefinitely.

Thanks, I'll do that.

Quoted 13 lines

drivers/tee/optee/Makefile

Quoted 11 lines
drivers/tee/optee/Makefile new file +10 -0
+static int optee_arg_from_user(struct opteem_arg *arg, size_t size,
+                       struct tee_shm **put_shm)
+{
+       struct opteem_param *param;
+       size_t n;
+
+       if (!arg->num_params || !put_shm)
+               return -EINVAL;
+
+       param = OPTEEM_GET_PARAMS(arg);

OPTEEM is a little opaque. OPTEE_MSG would be easier to grasp.

OK

Quoted 38 lines

[...]

Quoted 20 lines
drivers/tee/optee/core.c new file +14 -0
new file mode 100644
index 0000000..b3f8b92d
@@ -0,0 +1,509 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#include <linux/types.h>

drivers/tee/optee/core.c line 24

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+#include <linux/string.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+#include <linux/dma-contiguous.h>
+#ifdef CONFIG_OPTEE_USE_CMA
+#include <linux/cma.h>
+#endif

Surely this ifdeffery isn't necessary?

[...]

Quoted 8 lines

drivers/tee/optee/core.c line 337

Quoted 6 lines
drivers/tee/optee/core.c new file +6 -0
+static struct tee_shm_pool *optee_config_shm_ioremap(struct device *dev,
+                       void __iomem **ioremaped_shm)
+{
+       struct optee_smc_param param = { .a0 = OPTEE_SMC_GET_SHM_CONFIG };
+       struct tee_shm_pool *pool;
+       u_long vaddr;

Why not plain unsigned long?

OK, I'll change.

Quoted 26 lines

[...]

Quoted 5 lines

+/*
+ * This file is exported by OP-TEE and is in kept in sync between secure
+ * world and normal world kernel driver. We're following ARM SMC Calling
+ * Convention as specified in
+ * http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html

The values defined in the SMC Calling Conventions (which have nothing to do with OP-TEE as such), we should probably prefix with SMCCC_, and have in a shared file.

drivers/tee/optee/core.c

Quoted 9 lines

+ *
+ * This file depends on optee_msg.h being included to expand the SMC id
+ * macros below.
+ */

drivers/tee/optee/core.c new file +5 -0
+
+#define OPTEE_SMC_32                   0
+#define OPTEE_SMC_64                   0x40000000
+#define OPTEE_SMC_FAST_CALL            0x80000000
+#define OPTEE_SMC_STD_CALL             0

The zero cases look a bit odd here. They might be better-documented if you defined them with shifts, e.g.

Quoted 4 lines
#define SMCCC_SMC_32                    (0 << 30)
#define SMCCC_SMC_64                    (1 << 30)
#define SMCCC_FAST_CALL                 (1 << 31)
#define SMCCC_STD_CALL                  (0 << 31)

OK

Quoted 10 lines

[...]

drivers/tee/optee/core.c

Quoted 5 lines
drivers/tee/optee/core.c new file +5 -0
+/*
+ * Cache settings for shared memory
+ */
+#define OPTEE_SMC_SHM_NONCACHED                0ULL
+#define OPTEE_SMC_SHM_CACHED           1ULL

What precise set of memory attributes do these imply?

OPTEE_SMC_SHM_NONCACHED is generally not used, but supposed to match how the kernel maps noncached memory. OP-TEE maps this as Device-nGnRE Outer sharable memory (MAIR ATTR = 0x04)

OPTEE_SMC_SHM_CACHED is cached memory with settings matching how the kernel maps cached memory. OP-TEE maps this as as Normal Memory, Outer Write-back non-transient Outer Read Allocate Outer Write Allocate Inner Write-back non-transient Inner Read Allocate Inner Write Allocate Inner sharable (MAIR ATTR = 0xff).

OP-TEE is more or less always compiled for a specific platform so if the kernel uses some other mapping for a particular platform we'll change the OP-TEE settings to be compatible with the kernel on that platform.

Quoted 15 lines

[...]

drivers/tee/optee/core.c

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+/*
+ * Same values as TEE_PARAM_* from TEE Internal API
+ */
+#define OPTEEM_ATTR_TYPE_NONE          0
+#define OPTEEM_ATTR_TYPE_VALUE_INPUT   1
+#define OPTEEM_ATTR_TYPE_VALUE_OUTPUT  2
+#define OPTEEM_ATTR_TYPE_VALUE_INOUT   3
+#define OPTEEM_ATTR_TYPE_MEMREF_INPUT  5
+#define OPTEEM_ATTR_TYPE_MEMREF_OUTPUT 6
+#define OPTEEM_ATTR_TYPE_MEMREF_INOUT  7

Are these well-defined ABI values?

Yes.

Quoted 26 lines

As mentioned previously, OPTEEM_* is opaque, and OPTEE_MSG_* would be far clearer.

Quoted 8 lines
drivers/tee/optee/core.c new file +8 -0
+/**
+ * struct opteem_param_memref - memory reference
+ * @buf_ptr:   Address of the buffer
+ * @size:      Size of the buffer
+ * @shm_ref:   Shared memory reference only used by normal world
+ *
+ * Secure and normal world communicates pointers as physical address
+ * instead of the virtual address. This is because secure and normal world

drivers/tee/optee/core.c

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+ * have completely independent memory mapping. Normal world can even have a
+ * hypervisor which need to translate the guest physical address (AKA IPA
+ * in ARM documentation) to a real physical address before passing the
+ * structure to secure world.
+ */
+struct opteem_param_memref {
+       __u64 buf_ptr;
+       __u64 size;
+       __u64 shm_ref;
+};

Why does this mention physical addresses at all? What does that have to do with userspace?

Quoted 3 lines

What is the shm_ref, and who allocates it?

There should really be some documentation for this.

Agree.

buf_ptr is a physical address (IPA or PA depending on context) outside user space, in user space it's an offset into the shm_ref.

shm_ref is a pointer to struct tee_shm in the kernel, an opaque handle in secure world, and a file descriptor (connected to a struct tee_shm) in user space.

Quoted 14 lines

drivers/tee/optee/core.c

Quoted 12 lines
drivers/tee/optee/core.c new file +11 -0
+/**
+ * struct opteem_param_value - values
+ * @a: first value
+ * @b: second value
+ * @c: third value
+ */
+struct opteem_param_value {
+       __u64 a;
+       __u64 b;
+       __u64 c;
+};

Are OP-TEE messages defined to only ever take three parameters?

No, this is a value parameter, each value parameter can carry three value. An OP-TEE message carry the number of parameters specified with num_params in struct opteem_arg.

Quoted 18 lines

[...]

drivers/tee/optee/core.c

Quoted 12 lines
drivers/tee/optee/core.c new file +12 -0
+/**
+ * struct optee_cmd_prefix - initial header for all user space buffers
+ * @func_id:   Function Id OPTEEM_FUNCID_* below
+ * @pad:       padding to make the struct size a multiple of 8 bytes
+ *
+ * This struct is 8 byte aligned since it's always followed by a struct
+ * opteem_arg which requires 8 byte alignment.
+ */
+struct opteem_cmd_prefix {
+       __u32 func_id;
+       __u32 pad __aligned(8);
+};

Shouldn't the alignment be applied to the struct as a whole rather than the final field?

OK, I didn't know which was the preferred location to apply the aligned attribute.

Quoted 18 lines
Quoted 18 lines

+/*
+ * Sleep mutex, helper for secure world to implement a sleeping mutex.
+ * struct opteem_arg::func one of OPTEEM_RPC_SLEEP_MUTEX_* below
+ *
+ * OPTEEM_RPC_SLEEP_MUTEX_WAIT
+ * [in] param[0].value .a sleep mutex key
+ * .b wait tick
+ * [not used] param[1]
+ *
+ * OPTEEM_RPC_SLEEP_MUTEX_WAKEUP
+ * [in] param[0].value .a sleep mutex key
+ * .b wait after
+ * [not used] param[1]
+ *
+ * OPTEEM_RPC_SLEEP_MUTEX_DELETE
+ * [in] param[0].value .a sleep mutex key
+ * [not used] param[1]

Quoted 8 lines

drivers/tee/optee/core.c

Quoted 5 lines

+ */

drivers/tee/optee/core.c new file +4 -0
+#define OPTEEM_RPC_SLEEP_MUTEX_WAIT    0
+#define OPTEEM_RPC_SLEEP_MUTEX_WAKEUP  1
+#define OPTEEM_RPC_SLEEP_MUTEX_DELETE  2
+#define OPTEEM_RPC_CMD_SLEEP_MUTEX     4

I'm lost. Why are mutexes exposed to userspace or the secure world in such a manner?

You're right it should go into another file not exposed to user space.

Thanks for taking the time to review this.

Regards,
Jens

#5 lore raw

Re: [PATCH V3 2/2] tee: add OP-TEE driver

Hi,
Just for completeness,

Quoted 9 lines · Jens Wiklander

Hi,

Quoted 3 lines · Mark Rutland

Hi,

On Fri, May 15, 2015 at 07:34:27AM +0100, Jens Wiklander wrote:

[?]

Quoted 37 lines
Quoted 6 lines

I'm also very concerned that the interface exposed to userspace is hideously low-level. Surely we'd expect kernel-side drivers to be doing the bulk of direct communication to the OP-TEE instance? In the lack of a provided rationale I don't see why the current messaging interface would make sense.

The kernel-side does all the direct communication since there's where the SMC is done, but one level above most of the communication is terminated in user space. Loading of Trusted Applications and other file system access is done in by a helper process in user space, tee-supplicant. A large part of the OP-TEE message protocol is transparent to the kernel.

We're trying to not exclude any TEE implementation by having this low level TEE_IOC_CMD instead of a high level interface. The problem with the different TEEs is that they are designed differently and we haven't been able to find a high level interface that suits all. Applications aren't expected to use TEE_IOC_CMD directly, instead there's supposed to be a client lib wich wraps the kernel interface and provides a higher level interface, for instance a Global Platform Client API in the case of a GP TEE.

For OP-TEE we're using the same protocol all the way down to user space, the advantage is that it's only one protocol to keep track of and we don't need to translate the entire message (we do need to copy it, excluding the payload) each time the message is passed to the next memory space. In the presence of a hypervisor we have
user space -> kernel -> hypervisor -> secure world Unfortunately some fields has a different meaning in user space and kernel space. I'll address this in the documentation.

The OP-TEE helper process, tee-supplicant, is specific to only OP-TEE. Other TEEs uses helper processes too, but what they do depend on the design of the TEE. As a consequence more or less all TEEs needs something specific for that particular TEE in user space to be fully functional.

Quoted 7 lines

To summarize, the current assumption is that all TEEs can't use the same high level interface. Instead we need to provide a way for each TEE to have their own low level interface which should be wrapped in a user space library to provide a more reasonable interface to the client application.

This design aims to be TEE agnostic.

As Jens mentioned, user space applications are supposed to use client-side API?s (e.g., Global Platform). It is convenient that these APIs reside in user space together with the wrapper using the kernel TEE interface. Note that GP?s APIs have changed much since they were first released. It would be a lot of work to keep them updated in the kernel and we would eventually run into the issue that they are not backwards compatible, thus making user space applications break.

Also, we want to eventually support kernel submodules to use the TEE subsystem. In this case, having a simple in-kernel interface makes things easier. Again, having a rich API such as GP in the kernel would not add any value.

[?]

Javier.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 842 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150520/12cbfa54/attachment.sig>

#6 lore raw

Re: [PATCH V3 2/2] tee: add OP-TEE driver

In reply to Jens Wiklander
Quoted 13 lines · Jens Wiklander

Hi,

Quoted 10 lines · Mark Rutland

Hi,

Quoted 6 lines · Jens Wiklander

Adds a OP-TEE driver which also can be compiled as a loadable module.

* Targets ARM and ARM64
* Supports using reserved memory from OP-TEE as shared memory
* CMA as shared memory is optional and only tried if OP-TEE doesn't supply a reserved shared memory region

Quoted 7 lines
Quoted 3 lines

How does OP-TEE provide that reserved memory? How is that described in DT/UEFI to the OS (e.g. is there a memreserve, or is the memory not described at all)?

It's either memreserve or not described at all. This should only be needed when secure world is limited in which memory it can use for shared memory. Currently all OP-TEE ports uses reserved shared memory, but we're moving away from it to avoid the problem with updating DT.

Ok. It's worth noting that memreserves allow the kernel to map the memory with cacheable attributes, which can result in coherency problems if it's expected to access any buffer with non-cacheable attributes.

Quoted 18 lines
Quoted 10 lines
Quoted 7 lines

* Probes OP-TEE version using SMCs
* Accepts requests on privileged and unprivileged device
* Uses OPTEE message protocol version 2

---
 Documentation/devicetree/bindings/optee/optee.txt  |  17 +

I'm concerned that there's no documentation regarding the interface exposed to userspace, for neither rationale nor usage.

OK, I'll add something.

Quoted 6 lines

I'm also very concerned that the interface exposed to userspace is hideously low-level. Surely we'd expect kernel-side drivers to be doing the bulk of direct communication to the OP-TEE instance? In the lack of a provided rationale I don't see why the current messaging interface would make sense.

Quoted 6 lines

The kernel-side does all the direct communication since there's where the SMC is done, but one level above most of the communication is terminated in user space. Loading of Trusted Applications and other file system access is done in by a helper process in user space, tee-supplicant. A large part of the OP-TEE message protocol is transparent to the kernel.

So you expect userspace clients rather than kernel drivers plugging into this framework?

Quoted 18 lines

We're trying to not exclude any TEE implementation by having this low level TEE_IOC_CMD instead of a high level interface. The problem with the different TEEs is that they are designed differently and we haven't been able to find a high level interface that suits all. Applications aren't expected to use TEE_IOC_CMD directly, instead there's supposed to be a client lib wich wraps the kernel interface and provides a higher level interface, for instance a Global Platform Client API in the case of a GP TEE.

For OP-TEE we're using the same protocol all the way down to user space, the advantage is that it's only one protocol to keep track of and we don't need to translate the entire message (we do need to copy it, excluding the payload) each time the message is passed to the next memory space. In the presence of a hypervisor we have
user space -> kernel -> hypervisor -> secure world Unfortunately some fields has a different meaning in user space and kernel space. I'll address this in the documentation.

Quoted 5 lines

The OP-TEE helper process, tee-supplicant, is specific to only OP-TEE. Other TEEs uses helper processes too, but what they do depend on the design of the TEE. As a consequence more or less all TEEs needs something specific for that particular TEE in user space to be fully functional.

I'm not sure that your proposed kernel/user split is ideal. How does userspace determine the appropriate TEE client to use? What's required in the way of arbitration between clients?

Quoted 5 lines

To summarize, the current assumption is that all TEEs can't use the same high level interface. Instead we need to provide a way for each TEE to have their own low level interface which should be wrapped in a user space library to provide a more reasonable interface to the client application.

[...]

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Documentation/devicetree/bindings/optee/optee.txt new file +4 -0
+* OP-TEE based on ARM TrustZone required properties:
+
+- compatible="optee,optee-tz"
+
Quoted 8 lines

Documentation/devicetree/bindings/optee/optee.txt line 17

Quoted 4 lines
Documentation/devicetree/bindings/optee/optee.txt new file +4 -0
+Example:
+       optee {
+               compatible="optee,optee-tz";
+       };

What does the OP-TEE protocol give in the way of discoverability? Is it expected that the specific implementation and/or features will be detected dynamically?

Quoted 3 lines

We have OPTEEM_FUNCID_GET_OS_UUID and OPTEEM_FUNCID_GET_OS_REVISION which the client can use to identify which particular OP-TEE it's talking to.

Ok.

Quoted 3 lines

This is not so interesting for the driver, but the client may care when there's more than one TEE using the OP-TEE message protocol in a single system.

How does having more than one TEE work given there's a single conduit?

Quoted 59 lines

There's also OPTEEM_FUNCID_CALLS_UID and OPTEEM_FUNCID_CALLS_REVISION (required by SMC Calling Convention), but those are expected to return static values except OPTEEM_REVISION_MINOR which would be increased if some new message type is added in the future.

To summarize, OPTEEM_FUNCID_CALLS_* identifies the OP-TEE message protocol and OPTEEM_FUNCID_GET_OS_* identifies the OP-TEE OS implementation.

Quoted 2 lines

Where can I find documentation on the protocol?

The documentation is currently include/uapi/linux/optee_msg.h and drivers/tee/optee/optee_smc.h. I'll add something under Documentation.

There's more details at http://shorl.com/lubopribokygy , but that's not entirely updated with this driver.

Quoted 10 lines
Quoted 10 lines
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 8033919..17c2a7e 100644
@@ -141,6 +141,7 @@ nvidia      NVIDIA
 nxp    NXP Semiconductors
 onnn   ON Semiconductor Corp.
 opencores      OpenCores.org
+optee  OP-TEE, Open Portable Trusted Execution Environment
Quoted 8 lines

MAINTAINERS

Quoted 5 lines
Documentation/devicetree/bindings/vendor-prefixes.txt +0 -0
 ortustech      Ortus Technology Co., Ltd.
 ovti   OmniVision Technologies
 panasonic      Panasonic Corporation
MAINTAINERS +0 -0
index dfcc9cc..1234695 100644

Please split the DT binding parts into a separate patch, at the start of the series.

OK

Quoted 14 lines
Quoted 14 lines
drivers/tee/optee/Makefile new file +7 -0
new file mode 100644
index 0000000..096651d
@@ -0,0 +1,13 @@
+obj-$(CONFIG_OPTEE) += optee.o
+optee-objs += core.o
+optee-objs += call.o
+ifdef CONFIG_ARM
+plus_sec := $(call as-instr,.arch_extension sec,+sec)
+AFLAGS_smc_a32.o := -Wa,-march=armv7-a$(plus_sec)
+optee-objs += smc_a32.o
Quoted 8 lines

drivers/tee/optee/Makefile line 11

Quoted 4 lines
drivers/tee/optee/Makefile new file +4 -0
+endif
+ifdef CONFIG_ARM64
+optee-objs += smc_a64.o
+endif

The assembly objects should probably live under the relevant arch/ folders, and can probably be shared with clients for other services compliant with the SMC Calling Conventions.

Quoted 2 lines

OK, sounds good. Where should I put the smccc.h file to be able to share it between arch/arm and arch/arm64, under include/asm-generic?

I'd imagine the SMCCC stuff could live at include/linux/arm_smccc.h (following the example of efi.h).

Quoted 17 lines
Quoted 7 lines

drivers/tee/optee/Makefile

Quoted 5 lines
drivers/tee/optee/Makefile new file +5 -0
+/*
+ * Cache settings for shared memory
+ */
+#define OPTEE_SMC_SHM_NONCACHED                0ULL
+#define OPTEE_SMC_SHM_CACHED           1ULL

What precise set of memory attributes do these imply?

OPTEE_SMC_SHM_NONCACHED is generally not used, but supposed to match how the kernel maps noncached memory. OP-TEE maps this as Device-nGnRE Outer sharable memory (MAIR ATTR = 0x04)

OPTEE_SMC_SHM_CACHED is cached memory with settings matching how the kernel maps cached memory. OP-TEE maps this as as Normal Memory, Outer Write-back non-transient Outer Read Allocate Outer Write Allocate Inner Write-back non-transient Inner Read Allocate Inner Write Allocate Inner sharable (MAIR ATTR = 0xff).

Quoted 3 lines

OP-TEE is more or less always compiled for a specific platform so if the kernel uses some other mapping for a particular platform we'll change the OP-TEE settings to be compatible with the kernel on that platform.

That assumes that the TEE has to know about any kernel that might run. It also implies that a kernel needs to know what each TEE thinks the kernel will be mapping memory as, so it can work around whatever decision has been made by the TEE.

So as it stands I think that's a broken design. The attributes you need should be strictly specified. It's perfectly valid for that strict specification to be the same attributes the kernel uses now, but the spec can't change later.

Otherwise mismatched attributes will get in the way on some platform, and it's going to be close to impossible to fix things up.

Quoted 25 lines
Quoted 22 lines
Quoted 8 lines
drivers/tee/optee/Makefile new file +8 -0
+/**
+ * struct opteem_param_memref - memory reference
+ * @buf_ptr:   Address of the buffer
+ * @size:      Size of the buffer
+ * @shm_ref:   Shared memory reference only used by normal world
+ *
+ * Secure and normal world communicates pointers as physical address
+ * instead of the virtual address. This is because secure and normal world

drivers/tee/optee/Makefile

Quoted 10 lines
drivers/tee/optee/Makefile new file +10 -0
+ * have completely independent memory mapping. Normal world can even have a
+ * hypervisor which need to translate the guest physical address (AKA IPA
+ * in ARM documentation) to a real physical address before passing the
+ * structure to secure world.
+ */
+struct opteem_param_memref {
+       __u64 buf_ptr;
+       __u64 size;
+       __u64 shm_ref;
+};

Why does this mention physical addresses at all? What does that have to do with userspace?

Quoted 3 lines

What is the shm_ref, and who allocates it?

There should really be some documentation for this.

Quoted 8 lines

Agree.

buf_ptr is a physical address (IPA or PA depending on context) outside user space, in user space it's an offset into the shm_ref.

shm_ref is a pointer to struct tee_shm in the kernel, an opaque handle in secure world, and a file descriptor (connected to a struct tee_shm) in user space.

If this is the header for userspace, the comments should be useful to userspace. Surely you can have a different structure kernel-side if you need to encode different values?

Thanks,
Mark.

#7 lore raw

Re: [PATCH V3 2/2] tee: add OP-TEE driver

Quoted 17 lines · Mark Rutland
Quoted 16 lines · Jens Wiklander

Hi,

Quoted 13 lines · Mark Rutland

Hi,

Quoted 6 lines · Jens Wiklander

Adds a OP-TEE driver which also can be compiled as a loadable module.

* Targets ARM and ARM64
* Supports using reserved memory from OP-TEE as shared memory
* CMA as shared memory is optional and only tried if OP-TEE doesn't supply a reserved shared memory region

How does OP-TEE provide that reserved memory? How is that described in DT/UEFI to the OS (e.g. is there a memreserve, or is the memory not described at all)?

Quoted 8 lines
Quoted 4 lines

It's either memreserve or not described at all. This should only be needed when secure world is limited in which memory it can use for shared memory. Currently all OP-TEE ports uses reserved shared memory, but we're moving away from it to avoid the problem with updating DT.

Ok. It's worth noting that memreserves allow the kernel to map the memory with cacheable attributes, which can result in coherency problems if it's expected to access any buffer with non-cacheable attributes.

Thanks, that rules out memreserve for this.

Quoted 25 lines
Quoted 18 lines
Quoted 10 lines
Quoted 7 lines

* Probes OP-TEE version using SMCs
* Accepts requests on privileged and unprivileged device
* Uses OPTEE message protocol version 2

---
 Documentation/devicetree/bindings/optee/optee.txt  |  17 +

I'm concerned that there's no documentation regarding the interface exposed to userspace, for neither rationale nor usage.

OK, I'll add something.

Quoted 6 lines

I'm also very concerned that the interface exposed to userspace is hideously low-level. Surely we'd expect kernel-side drivers to be doing the bulk of direct communication to the OP-TEE instance? In the lack of a provided rationale I don't see why the current messaging interface would make sense.

Quoted 6 lines

The kernel-side does all the direct communication since there's where the SMC is done, but one level above most of the communication is terminated in user space. Loading of Trusted Applications and other file system access is done in by a helper process in user space, tee-supplicant. A large part of the OP-TEE message protocol is transparent to the kernel.

Quoted 2 lines

So you expect userspace clients rather than kernel drivers plugging into this framework?

The OP-TEE message protocol is primarily for the OP-TEE driver. Other TEE drivers plugging into this framwork may use this protocol too, but I guess that most will use their own message protocol.

Provided that each TEE driver rolls their own protocol I'm expecting one counter part in user space for each TEE driver. The user space client will know which kind of TEE it's talking to through TEE_IOC_VERSION.

Quoted 25 lines
Quoted 19 lines

We're trying to not exclude any TEE implementation by having this low level TEE_IOC_CMD instead of a high level interface. The problem with the different TEEs is that they are designed differently and we haven't been able to find a high level interface that suits all. Applications aren't expected to use TEE_IOC_CMD directly, instead there's supposed to be a client lib wich wraps the kernel interface and provides a higher level interface, for instance a Global Platform Client API in the case of a GP TEE.

For OP-TEE we're using the same protocol all the way down to user space, the advantage is that it's only one protocol to keep track of and we don't need to translate the entire message (we do need to copy it, excluding the payload) each time the message is passed to the next memory space. In the presence of a hypervisor we have
user space -> kernel -> hypervisor -> secure world Unfortunately some fields has a different meaning in user space and kernel space. I'll address this in the documentation.

Quoted 5 lines

The OP-TEE helper process, tee-supplicant, is specific to only OP-TEE. Other TEEs uses helper processes too, but what they do depend on the design of the TEE. As a consequence more or less all TEEs needs something specific for that particular TEE in user space to be fully functional.

Quoted 3 lines

I'm not sure that your proposed kernel/user split is ideal. How does userspace determine the appropriate TEE client to use? What's required in the way of arbitration between clients?

Each client loops through /dev/tee[0-9]* until it finds a TEE it can communicate with, or if the client is looking for a specific TEE until it's found.

TEE_IOC_VERSION is used to tell which kind of TEE the client is talking to. For a library that implements Global Platforms TEE Client API I imagine that in TEEC_InitializeContext() the lib will detect which TEE it's talking to and initialize the TEEC_Context appropriately.

For clients that doesn't care about Global Platform APIs I guess that they will search for a specific TEE and give up if it's not found.

tee-supplicant is a special case since it's a helper process for the TEE. The will likely be one tee-supplicant implementation (tee-supplicant-optee, tee-supplicant-xyz, etc) for each TEE that user space can support. tee-supplicant is looking for a TEE to connect to through /dev/teepriv[0-9]*.

The reason for having /dev/teeX for normal clients and /dev/teeprivX for tee-supplicants we'd like to have any easy way to set different permission on the devices.

Quoted 16 lines
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Documentation/devicetree/bindings/optee/optee.txt new file +4 -0
+* OP-TEE based on ARM TrustZone required properties:
+
+- compatible="optee,optee-tz"
+
Quoted 8 lines

Documentation/devicetree/bindings/optee/optee.txt line 17

Quoted 4 lines
Documentation/devicetree/bindings/optee/optee.txt new file +4 -0
+Example:
+       optee {
+               compatible="optee,optee-tz";
+       };

What does the OP-TEE protocol give in the way of discoverability? Is it expected that the specific implementation and/or features will be detected dynamically?

Quoted 3 lines

We have OPTEEM_FUNCID_GET_OS_UUID and OPTEEM_FUNCID_GET_OS_REVISION which the client can use to identify which particular OP-TEE it's talking to.

Quoted 7 lines

Ok.

Quoted 3 lines

This is not so interesting for the driver, but the client may care when there's more than one TEE using the OP-TEE message protocol in a single system.

How does having more than one TEE work given there's a single conduit?

Each TEE gets its own /dev/teeXX when the specific TEE driver registers in the framework.

[...]

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 7 lines
drivers/tee/optee/Makefile new file +1 -0
new file mode 100644
index 0000000..096651d
@@ -0,0 +1,13 @@
+obj-$(CONFIG_OPTEE) += optee.o

drivers/tee/optee/Makefile line 11

Quoted 10 lines
drivers/tee/optee/Makefile new file +10 -0
+optee-objs += core.o
+optee-objs += call.o
+ifdef CONFIG_ARM
+plus_sec := $(call as-instr,.arch_extension sec,+sec)
+AFLAGS_smc_a32.o := -Wa,-march=armv7-a$(plus_sec)
+optee-objs += smc_a32.o
+endif
+ifdef CONFIG_ARM64
+optee-objs += smc_a64.o
+endif

The assembly objects should probably live under the relevant arch/ folders, and can probably be shared with clients for other services compliant with the SMC Calling Conventions.

Quoted 5 lines
Quoted 2 lines

OK, sounds good. Where should I put the smccc.h file to be able to share it between arch/arm and arch/arm64, under include/asm-generic?

I'd imagine the SMCCC stuff could live at include/linux/arm_smccc.h (following the example of efi.h).

Thanks.

Quoted 26 lines
Quoted 17 lines
Quoted 7 lines

drivers/tee/optee/Makefile

Quoted 5 lines
drivers/tee/optee/Makefile new file +5 -0
+/*
+ * Cache settings for shared memory
+ */
+#define OPTEE_SMC_SHM_NONCACHED                0ULL
+#define OPTEE_SMC_SHM_CACHED           1ULL

What precise set of memory attributes do these imply?

OPTEE_SMC_SHM_NONCACHED is generally not used, but supposed to match how the kernel maps noncached memory. OP-TEE maps this as Device-nGnRE Outer sharable memory (MAIR ATTR = 0x04)

OPTEE_SMC_SHM_CACHED is cached memory with settings matching how the kernel maps cached memory. OP-TEE maps this as as Normal Memory, Outer Write-back non-transient Outer Read Allocate Outer Write Allocate Inner Write-back non-transient Inner Read Allocate Inner Write Allocate Inner sharable (MAIR ATTR = 0xff).

Quoted 3 lines

OP-TEE is more or less always compiled for a specific platform so if the kernel uses some other mapping for a particular platform we'll change the OP-TEE settings to be compatible with the kernel on that platform.

That assumes that the TEE has to know about any kernel that might run. It also implies that a kernel needs to know what each TEE thinks the kernel will be mapping memory as, so it can work around whatever decision has been made by the TEE.

Quoted 7 lines

So as it stands I think that's a broken design. The attributes you need should be strictly specified. It's perfectly valid for that strict specification to be the same attributes the kernel uses now, but the spec can't change later.

Otherwise mismatched attributes will get in the way on some platform, and it's going to be close to impossible to fix things up.

OK, I see the problem. Is it OK only specify the attributes that need to be compatible like:

#define OPTEE_SMC_SHM_ICACHED           (1 << 0)
#define OPTEE_SMC_SHM_IWRITE_THROUGH    (1 << 1)
#define OPTEE_SMC_SHM_IWRITE_BACK       (1 << 2)
#define OPTEE_SMC_SHM_ISHARABLE         (1 << 3)
#define OPTEE_SMC_SHM_OCACHED           (1 << 4)
#define OPTEE_SMC_SHM_OWRITE_THROUGH    (1 << 5)
#define OPTEE_SMC_SHM_OWRITE_BACK       (1 << 6)
#define OPTEE_SMC_SHM_OSHARABLE         (1 << 7)
#define OPTEE_SMC_SHM_CACHED \
        (OPTEE_SMC_SHM_ICACHED | OPTEE_SMC_SHM_IWRITE_BACK | \
         OPTEE_SMC_SHM_ISHARABLE | OPTEE_SMC_SHM_OCACHED | \
         OPTEE_SMC_SHM_OWRITE_BACK)

I'll drop the OPTEE_SMC_SHM_NONCACHED define as it's currently not used.

Quoted 30 lines
Quoted 30 lines
Quoted 22 lines
Quoted 8 lines
drivers/tee/optee/Makefile new file +8 -0
+/**
+ * struct opteem_param_memref - memory reference
+ * @buf_ptr:   Address of the buffer
+ * @size:      Size of the buffer
+ * @shm_ref:   Shared memory reference only used by normal world
+ *
+ * Secure and normal world communicates pointers as physical address
+ * instead of the virtual address. This is because secure and normal world

drivers/tee/optee/Makefile

Quoted 10 lines
drivers/tee/optee/Makefile new file +10 -0
+ * have completely independent memory mapping. Normal world can even have a
+ * hypervisor which need to translate the guest physical address (AKA IPA
+ * in ARM documentation) to a real physical address before passing the
+ * structure to secure world.
+ */
+struct opteem_param_memref {
+       __u64 buf_ptr;
+       __u64 size;
+       __u64 shm_ref;
+};

Why does this mention physical addresses at all? What does that have to do with userspace?

Quoted 3 lines

What is the shm_ref, and who allocates it?

There should really be some documentation for this.

Agree.

buf_ptr is a physical address (IPA or PA depending on context) outside user space, in user space it's an offset into the shm_ref.

Quoted 7 lines
Quoted 3 lines

shm_ref is a pointer to struct tee_shm in the kernel, an opaque handle in secure world, and a file descriptor (connected to a struct tee_shm) in user space.

If this is the header for userspace, the comments should be useful to userspace. Surely you can have a different structure kernel-side if you need to encode different values?

OK, I'll make separate structures.

Thanks,
Jens

#8 lore raw

Re: [PATCH V3 2/2] tee: add OP-TEE driver

Quoted 15 lines
Quoted 12 lines
Quoted 11 lines
Quoted 5 lines

I'm also very concerned that the interface exposed to userspace is hideously low-level. Surely we'd expect kernel-side drivers to be doing the bulk of direct communication to the OP-TEE instance? In the lack of a provided rationale I don't see why the current messaging interface would make sense.

The kernel-side does all the direct communication since there's where the SMC is done, but one level above most of the communication is terminated in user space. Loading of Trusted Applications and other file system access is done in by a helper process in user space, tee-supplicant. A large part of the OP-TEE message protocol is transparent to the kernel.

Quoted 2 lines

So you expect userspace clients rather than kernel drivers plugging into this framework?

Quoted 7 lines

The OP-TEE message protocol is primarily for the OP-TEE driver. Other TEE drivers plugging into this framwork may use this protocol too, but I guess that most will use their own message protocol.

Provided that each TEE driver rolls their own protocol I'm expecting one counter part in user space for each TEE driver. The user space client will know which kind of TEE it's talking to through TEE_IOC_VERSION.

Surely that means you need to have every possible user-space client present in a given filesystem, and you need to have all of them try to probe the FW to figure out whether appropriate FW is present? That sounds somewhat heavyweight.

To me it would seem a lot better to have the minimal drivers in the kernel that get probed based on information from the FW. The main TEE driver would query the generic APIs to discover which features are exposed, then instantiate the relevant set of TEE-specific drivers based on TEE_IOC_VERSION and friends. To handle a need for userspace components you could emit uevents as necessary, though I'm still unclear on what the userspace components would do.

Quoted 32 lines
Quoted 24 lines
Quoted 18 lines

We're trying to not exclude any TEE implementation by having this low level TEE_IOC_CMD instead of a high level interface. The problem with the different TEEs is that they are designed differently and we haven't been able to find a high level interface that suits all. Applications aren't expected to use TEE_IOC_CMD directly, instead there's supposed to be a client lib wich wraps the kernel interface and provides a higher level interface, for instance a Global Platform Client API in the case of a GP TEE.

For OP-TEE we're using the same protocol all the way down to user space, the advantage is that it's only one protocol to keep track of and we don't need to translate the entire message (we do need to copy it, excluding the payload) each time the message is passed to the next memory space. In the presence of a hypervisor we have
user space -> kernel -> hypervisor -> secure world Unfortunately some fields has a different meaning in user space and kernel space. I'll address this in the documentation.

Quoted 5 lines

The OP-TEE helper process, tee-supplicant, is specific to only OP-TEE. Other TEEs uses helper processes too, but what they do depend on the design of the TEE. As a consequence more or less all TEEs needs something specific for that particular TEE in user space to be fully functional.

Quoted 3 lines

I'm not sure that your proposed kernel/user split is ideal. How does userspace determine the appropriate TEE client to use? What's required in the way of arbitration between clients?

Each client loops through /dev/tee[0-9]* until it finds a TEE it can communicate with, or if the client is looking for a specific TEE until it's found.

Quoted 7 lines

TEE_IOC_VERSION is used to tell which kind of TEE the client is talking to. For a library that implements Global Platforms TEE Client API I imagine that in TEEC_InitializeContext() the lib will detect which TEE it's talking to and initialize the TEEC_Context appropriately.

For clients that doesn't care about Global Platform APIs I guess that they will search for a specific TEE and give up if it's not found.

That covers detection, but what about arbitrartion?

What happens when I have multiple clients which want to communicate with the same TEE simultaneously?

Quoted 9 lines

tee-supplicant is a special case since it's a helper process for the TEE. The will likely be one tee-supplicant implementation (tee-supplicant-optee, tee-supplicant-xyz, etc) for each TEE that user space can support. tee-supplicant is looking for a TEE to connect to through /dev/teepriv[0-9]*.

The reason for having /dev/teeX for normal clients and /dev/teeprivX for tee-supplicants we'd like to have any easy way to set different permission on the devices.

What do TEE supplicants do?

[...]

Quoted 4 lines
Quoted 1 line

How does having more than one TEE work given there's a single conduit?

Each TEE gets its own /dev/teeXX when the specific TEE driver registers in the framework.

Ok.

Quoted 45 lines
Quoted 26 lines
Quoted 17 lines
Quoted 7 lines

drivers/tee/optee/optee_smc.h line 94

Quoted 5 lines
drivers/tee/optee/optee_smc.h new file +5 -0
+/*
+ * Cache settings for shared memory
+ */
+#define OPTEE_SMC_SHM_NONCACHED                0ULL
+#define OPTEE_SMC_SHM_CACHED           1ULL

What precise set of memory attributes do these imply?

OPTEE_SMC_SHM_NONCACHED is generally not used, but supposed to match how the kernel maps noncached memory. OP-TEE maps this as Device-nGnRE Outer sharable memory (MAIR ATTR = 0x04)

OPTEE_SMC_SHM_CACHED is cached memory with settings matching how the kernel maps cached memory. OP-TEE maps this as as Normal Memory, Outer Write-back non-transient Outer Read Allocate Outer Write Allocate Inner Write-back non-transient Inner Read Allocate Inner Write Allocate Inner sharable (MAIR ATTR = 0xff).

Quoted 3 lines

OP-TEE is more or less always compiled for a specific platform so if the kernel uses some other mapping for a particular platform we'll change the OP-TEE settings to be compatible with the kernel on that platform.

That assumes that the TEE has to know about any kernel that might run. It also implies that a kernel needs to know what each TEE thinks the kernel will be mapping memory as, so it can work around whatever decision has been made by the TEE.

Quoted 7 lines

So as it stands I think that's a broken design. The attributes you need should be strictly specified. It's perfectly valid for that strict specification to be the same attributes the kernel uses now, but the spec can't change later.

Otherwise mismatched attributes will get in the way on some platform, and it's going to be close to impossible to fix things up.

OK, I see the problem. Is it OK only specify the attributes that need to be compatible like:

#define OPTEE_SMC_SHM_ICACHED           (1 << 0)
#define OPTEE_SMC_SHM_IWRITE_THROUGH    (1 << 1)
#define OPTEE_SMC_SHM_IWRITE_BACK       (1 << 2)
#define OPTEE_SMC_SHM_ISHARABLE         (1 << 3)
#define OPTEE_SMC_SHM_OCACHED           (1 << 4)
#define OPTEE_SMC_SHM_OWRITE_THROUGH    (1 << 5)
#define OPTEE_SMC_SHM_OWRITE_BACK       (1 << 6)
#define OPTEE_SMC_SHM_OSHARABLE         (1 << 7)
Quoted 4 lines
#define OPTEE_SMC_SHM_CACHED \
        (OPTEE_SMC_SHM_ICACHED | OPTEE_SMC_SHM_IWRITE_BACK | \
         OPTEE_SMC_SHM_ISHARABLE | OPTEE_SMC_SHM_OCACHED | \
         OPTEE_SMC_SHM_OWRITE_BACK)

I'm not sure I follow the question. Will these specific attributes be mandated by the OP-TEE spec? The set of attributes above are certainly better specified than simply "CACHED", though it would be nice to have an architectural definition rather than just a bag of bits.

The architecture maintainers will need to look at the memory attributes too. I don't think that current APIs offer fine-grained control over attributes and a UP kernel may not map memory as shareable, for example.

Thanks,
Mark.

#9 lore raw

Re: [PATCH V3 2/2] tee: add OP-TEE driver

On Fri, Jun 05, 2015 at 11:48:14AM +0100, Mark Rutland wrote:
[...]

Quoted 13 lines
Quoted 7 lines

The OP-TEE message protocol is primarily for the OP-TEE driver. Other TEE drivers plugging into this framwork may use this protocol too, but I guess that most will use their own message protocol.

Provided that each TEE driver rolls their own protocol I'm expecting one counter part in user space for each TEE driver. The user space client will know which kind of TEE it's talking to through TEE_IOC_VERSION.

Surely that means you need to have every possible user-space client present in a given filesystem, and you need to have all of them try to probe the FW to figure out whether appropriate FW is present? That sounds somewhat heavyweight.

Quoted 7 lines

To me it would seem a lot better to have the minimal drivers in the kernel that get probed based on information from the FW. The main TEE driver would query the generic APIs to discover which features are exposed, then instantiate the relevant set of TEE-specific drivers based on TEE_IOC_VERSION and friends. To handle a need for userspace components you could emit uevents as necessary, though I'm still unclear on what the userspace components would do.

I'm not 100% sure what you mean. Given this and other comments on TEE_IOC_CMD, I give up on TEE_IOC_CMD. I'll replace it with several more specific TEE_IOC_* that will give a less complex and unified interface to user space.

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 3 lines

I'm not sure that your proposed kernel/user split is ideal. How does userspace determine the appropriate TEE client to use? What's required in the way of arbitration between clients?

Each client loops through /dev/tee[0-9]* until it finds a TEE it can communicate with, or if the client is looking for a specific TEE until it's found.

TEE_IOC_VERSION is used to tell which kind of TEE the client is talking to. For a library that implements Global Platforms TEE Client API I imagine that in TEEC_InitializeContext() the lib will detect which TEE it's talking to and initialize the TEEC_Context appropriately.

Quoted 7 lines
Quoted 2 lines

For clients that doesn't care about Global Platform APIs I guess that they will search for a specific TEE and give up if it's not found.

That covers detection, but what about arbitrartion?

What happens when I have multiple clients which want to communicate with the same TEE simultaneously?

Each client opens a the same /dev/teeX and communicates over their own file descriptor.

Quoted 7 lines
Quoted 7 lines

tee-supplicant is a special case since it's a helper process for the TEE. The will likely be one tee-supplicant implementation (tee-supplicant-optee, tee-supplicant-xyz, etc) for each TEE that user space can support. tee-supplicant is looking for a TEE to connect to through /dev/teepriv[0-9]*.

Quoted 5 lines
Quoted 3 lines

The reason for having /dev/teeX for normal clients and /dev/teeprivX for tee-supplicants we'd like to have any easy way to set different permission on the devices.

What do TEE supplicants do?

For OP-TEE (and I guess most other TEEs) it handles file system access. Having a separate user for tee-supplicant makes it easier to have strict permissions for created files etc.

[...]

Quoted 50 lines
Quoted 45 lines
Quoted 26 lines
Quoted 17 lines
Quoted 7 lines

drivers/tee/optee/optee_smc.h line 94

Quoted 5 lines
drivers/tee/optee/optee_smc.h new file +5 -0
+/*
+ * Cache settings for shared memory
+ */
+#define OPTEE_SMC_SHM_NONCACHED                0ULL
+#define OPTEE_SMC_SHM_CACHED           1ULL

What precise set of memory attributes do these imply?

OPTEE_SMC_SHM_NONCACHED is generally not used, but supposed to match how the kernel maps noncached memory. OP-TEE maps this as Device-nGnRE Outer sharable memory (MAIR ATTR = 0x04)

OPTEE_SMC_SHM_CACHED is cached memory with settings matching how the kernel maps cached memory. OP-TEE maps this as as Normal Memory, Outer Write-back non-transient Outer Read Allocate Outer Write Allocate Inner Write-back non-transient Inner Read Allocate Inner Write Allocate Inner sharable (MAIR ATTR = 0xff).

Quoted 3 lines

OP-TEE is more or less always compiled for a specific platform so if the kernel uses some other mapping for a particular platform we'll change the OP-TEE settings to be compatible with the kernel on that platform.

That assumes that the TEE has to know about any kernel that might run. It also implies that a kernel needs to know what each TEE thinks the kernel will be mapping memory as, so it can work around whatever decision has been made by the TEE.

Quoted 7 lines

So as it stands I think that's a broken design. The attributes you need should be strictly specified. It's perfectly valid for that strict specification to be the same attributes the kernel uses now, but the spec can't change later.

Otherwise mismatched attributes will get in the way on some platform, and it's going to be close to impossible to fix things up.

OK, I see the problem. Is it OK only specify the attributes that need to be compatible like:

#define OPTEE_SMC_SHM_ICACHED           (1 << 0)
#define OPTEE_SMC_SHM_IWRITE_THROUGH    (1 << 1)
#define OPTEE_SMC_SHM_IWRITE_BACK       (1 << 2)
#define OPTEE_SMC_SHM_ISHARABLE         (1 << 3)
#define OPTEE_SMC_SHM_OCACHED           (1 << 4)
#define OPTEE_SMC_SHM_OWRITE_THROUGH    (1 << 5)
#define OPTEE_SMC_SHM_OWRITE_BACK       (1 << 6)
#define OPTEE_SMC_SHM_OSHARABLE         (1 << 7)
Quoted 4 lines
#define OPTEE_SMC_SHM_CACHED \
        (OPTEE_SMC_SHM_ICACHED | OPTEE_SMC_SHM_IWRITE_BACK | \
         OPTEE_SMC_SHM_ISHARABLE | OPTEE_SMC_SHM_OCACHED | \
         OPTEE_SMC_SHM_OWRITE_BACK)
Quoted 8 lines

I'm not sure I follow the question. Will these specific attributes be mandated by the OP-TEE spec? The set of attributes above are certainly better specified than simply "CACHED", though it would be nice to have an architectural definition rather than just a bag of bits.

The architecture maintainers will need to look at the memory attributes too. I don't think that current APIs offer fine-grained control over attributes and a UP kernel may not map memory as shareable, for example.

Defining all those bits for OPTEE_SMC_SHM_CACHED didn't help much. I took the liberty to contact Catalin directly on this and my interpretation of his advice is:

/*
* Normal cached memory (write-back), shareable for SMP systems and not
* shareable for UP systems.
*/
#define OPTEE_SMC_SHM_CACHED 1

This is closer to my original proposal, but with the crucial difference that OP-TEE doesn't need to know how the kernel maps other memory. OP-TEE requires the kernel to map memory shared with secure world with the attributes specified in the comment.

--
Thanks,
Jens

#10 lore raw

Re: [PATCH V3 2/2] tee: add OP-TEE driver

In reply to Jens Wiklander
Quoted 4 lines · Jens Wiklander
Quoted 4 lines

drivers/tee/optee/optee_smc.h line 94

Quoted 2 lines
drivers/tee/optee/optee_smc.h new file +2 -0
+#define OPTEE_SMC_SHM_NONCACHED                0ULL
+#define OPTEE_SMC_SHM_CACHED           1ULL

What precise set of memory attributes do these imply?

...

Quoted 3 lines

OP-TEE is more or less always compiled for a specific platform so if the kernel uses some other mapping for a particular platform we'll change the OP-TEE settings to be compatible with the kernel on that platform.

I'm not convinced that's a realistic assumption going forwards - I
certainly hope it isn't. Currently TEE code tends to be only built for
verticial systems because of this lack of standardiation (and the fact
that so much is proprietary at the minute) but hopefully one of the
results of having a reusable solution in the standard kernel with an
open platform for the secure side will be that people will be more able
to use this, perhaps in applications where things like single kernel
support are much more important than they are for the environments where
TEEs are currently deployed.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150604/da1dc07b/attachment-0001.sig>