xref: /optee_os/core/include/optee_msg.h (revision 78b7c7c7653f8bff42fe44d31a79d7f6bbfd4d47)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2015-2017, Linaro Limited
4  * All rights reserved.
5  */
6 #ifndef _OPTEE_MSG_H
7 #define _OPTEE_MSG_H
8 
9 #include <compiler.h>
10 #include <types_ext.h>
11 #include <util.h>
12 
13 /*
14  * This file defines the OP-TEE message protocol used to communicate
15  * with an instance of OP-TEE running in secure world.
16  *
17  * This file is divided into three sections.
18  * 1. Formatting of messages.
19  * 2. Requests from normal world
20  * 3. Requests from secure world, Remote Procedure Call (RPC), handled by
21  *    tee-supplicant.
22  */
23 
24 /*****************************************************************************
25  * Part 1 - formatting of messages
26  *****************************************************************************/
27 
28 #define OPTEE_MSG_ATTR_TYPE_NONE		0x0
29 #define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT		0x1
30 #define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT	0x2
31 #define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT		0x3
32 #define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT		0x5
33 #define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT		0x6
34 #define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT		0x7
35 #define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT		0x9
36 #define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT		0xa
37 #define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT		0xb
38 
39 #define OPTEE_MSG_ATTR_TYPE_MASK		GENMASK_32(7, 0)
40 
41 /*
42  * Meta parameter to be absorbed by the Secure OS and not passed
43  * to the Trusted Application.
44  *
45  * Currently only used with OPTEE_MSG_CMD_OPEN_SESSION.
46  */
47 #define OPTEE_MSG_ATTR_META			BIT(8)
48 
49 /*
50  * Pointer to a list of pages used to register user-defined SHM buffer.
51  * Used with OPTEE_MSG_ATTR_TYPE_TMEM_*.
52  * buf_ptr should point to the beginning of the buffer. Buffer will contain
53  * list of page addresses. OP-TEE core can reconstruct contiguous buffer from
54  * that page addresses list. Page addresses are stored as 64 bit values.
55  * Last entry on a page should point to the next page of buffer.
56  * Every entry in buffer should point to a 4k page beginning (12 least
57  * significant bits must be equal to zero).
58  *
59  * 12 least significant of optee_msg_param.u.tmem.buf_ptr should hold page
60  * offset of user buffer.
61  *
62  * So, entries should be placed like members of this structure:
63  *
64  * struct page_data {
65  *   uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];
66  *   uint64_t next_page_data;
67  * };
68  *
69  * Structure is designed to exactly fit into the page size
70  * OPTEE_MSG_NONCONTIG_PAGE_SIZE which is a standard 4KB page.
71  *
72  * The size of 4KB is chosen because this is the smallest page size for ARM
73  * architectures. If REE uses larger pages, it should divide them to 4KB ones.
74  */
75 #define OPTEE_MSG_ATTR_NONCONTIG		BIT(9)
76 
77 /*
78  * Memory attributes for caching passed with temp memrefs. The actual value
79  * used is defined outside the message protocol with the exception of
80  * OPTEE_MSG_ATTR_CACHE_PREDEFINED which means the attributes already
81  * defined for the memory range should be used. If optee_smc.h is used as
82  * bearer of this protocol OPTEE_SMC_SHM_* is used for values.
83  */
84 #define OPTEE_MSG_ATTR_CACHE_SHIFT		16
85 #define OPTEE_MSG_ATTR_CACHE_MASK		GENMASK_32(2, 0)
86 #define OPTEE_MSG_ATTR_CACHE_PREDEFINED		0
87 
88 /*
89  * Same values as TEE_LOGIN_* from TEE Internal API
90  */
91 #define OPTEE_MSG_LOGIN_PUBLIC			0x00000000
92 #define OPTEE_MSG_LOGIN_USER			0x00000001
93 #define OPTEE_MSG_LOGIN_GROUP			0x00000002
94 #define OPTEE_MSG_LOGIN_APPLICATION		0x00000004
95 #define OPTEE_MSG_LOGIN_APPLICATION_USER	0x00000005
96 #define OPTEE_MSG_LOGIN_APPLICATION_GROUP	0x00000006
97 
98 /*
99  * Page size used in non-contiguous buffer entries
100  */
101 #define OPTEE_MSG_NONCONTIG_PAGE_SIZE		4096
102 
103 #ifndef ASM
104 /**
105  * struct optee_msg_param_tmem - temporary memory reference parameter
106  * @buf_ptr:	Address of the buffer
107  * @size:	Size of the buffer
108  * @shm_ref:	Temporary shared memory reference, pointer to a struct tee_shm
109  *
110  * Secure and normal world communicates pointers as physical address
111  * instead of the virtual address. This is because secure and normal world
112  * have completely independent memory mapping. Normal world can even have a
113  * hypervisor which need to translate the guest physical address (AKA IPA
114  * in ARM documentation) to a real physical address before passing the
115  * structure to secure world.
116  */
117 struct optee_msg_param_tmem {
118 	uint64_t buf_ptr;
119 	uint64_t size;
120 	uint64_t shm_ref;
121 };
122 
123 /**
124  * struct optee_msg_param_rmem - registered memory reference parameter
125  * @offs:	Offset into shared memory reference
126  * @size:	Size of the buffer
127  * @shm_ref:	Shared memory reference, pointer to a struct tee_shm
128  */
129 struct optee_msg_param_rmem {
130 	uint64_t offs;
131 	uint64_t size;
132 	uint64_t shm_ref;
133 };
134 
135 /**
136  * struct optee_msg_param_value - values
137  * @a: first value
138  * @b: second value
139  * @c: third value
140  */
141 struct optee_msg_param_value {
142 	uint64_t a;
143 	uint64_t b;
144 	uint64_t c;
145 };
146 
147 /**
148  * struct optee_msg_param - parameter
149  * @attr: attributes
150  * @memref: a memory reference
151  * @value: a value
152  *
153  * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
154  * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value,
155  * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates tmem and
156  * OPTEE_MSG_ATTR_TYPE_RMEM_* indicates rmem.
157  * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
158  */
159 struct optee_msg_param {
160 	uint64_t attr;
161 	union {
162 		struct optee_msg_param_tmem tmem;
163 		struct optee_msg_param_rmem rmem;
164 		struct optee_msg_param_value value;
165 	} u;
166 };
167 
168 /**
169  * struct optee_msg_arg - call argument
170  * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
171  * @func: Trusted Application function, specific to the Trusted Application,
172  *	     used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
173  * @session: In parameter for all OPTEE_MSG_CMD_* except
174  *	     OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
175  * @cancel_id: Cancellation id, a unique value to identify this request
176  * @ret: return value
177  * @ret_origin: origin of the return value
178  * @num_params: number of parameters supplied to the OS Command
179  * @params: the parameters supplied to the OS Command
180  *
181  * All normal calls to Trusted OS uses this struct. If cmd requires further
182  * information than what these fields hold it can be passed as a parameter
183  * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
184  * attrs field). All parameters tagged as meta have to come first.
185  *
186  * Temp memref parameters can be fragmented if supported by the Trusted OS
187  * (when optee_smc.h is bearer of this protocol this is indicated with
188  * OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM). If a logical memref parameter is
189  * fragmented then all but the last fragment have the
190  * OPTEE_MSG_ATTR_FRAGMENT bit set in attrs. Even if a memref is fragmented
191  * it will still be presented as a single logical memref to the Trusted
192  * Application.
193  */
194 struct optee_msg_arg {
195 	uint32_t cmd;
196 	uint32_t func;
197 	uint32_t session;
198 	uint32_t cancel_id;
199 	uint32_t pad;
200 	uint32_t ret;
201 	uint32_t ret_origin;
202 	uint32_t num_params;
203 
204 	/* num_params tells the actual number of element in params */
205 	struct optee_msg_param params[];
206 };
207 
208 /**
209  * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
210  *
211  * @num_params: Number of parameters embedded in the struct optee_msg_arg
212  *
213  * Returns the size of the struct optee_msg_arg together with the number
214  * of embedded parameters.
215  */
216 #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
217 	(sizeof(struct optee_msg_arg) + \
218 	 sizeof(struct optee_msg_param) * (num_params))
219 #endif /*ASM*/
220 
221 /*****************************************************************************
222  * Part 2 - requests from normal world
223  *****************************************************************************/
224 
225 /*
226  * Return the following UID if using API specified in this file without
227  * further extensions:
228  * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
229  * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
230  * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
231  */
232 #define OPTEE_MSG_UID_0			0x384fb3e0
233 #define OPTEE_MSG_UID_1			0xe7f811e3
234 #define OPTEE_MSG_UID_2			0xaf630002
235 #define OPTEE_MSG_UID_3			0xa5d5c51b
236 #define OPTEE_MSG_FUNCID_CALLS_UID	0xFF01
237 
238 /*
239  * Returns 2.0 if using API specified in this file without further
240  * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
241  * and OPTEE_MSG_REVISION_MINOR
242  */
243 #define OPTEE_MSG_REVISION_MAJOR	2
244 #define OPTEE_MSG_REVISION_MINOR	0
245 #define OPTEE_MSG_FUNCID_CALLS_REVISION	0xFF03
246 
247 /*
248  * Get UUID of Trusted OS.
249  *
250  * Used by non-secure world to figure out which Trusted OS is installed.
251  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
252  *
253  * Returns UUID in 4 32-bit words in the same way as
254  * OPTEE_MSG_FUNCID_CALLS_UID described above.
255  */
256 #define OPTEE_MSG_OS_OPTEE_UUID_0	0x486178e0
257 #define OPTEE_MSG_OS_OPTEE_UUID_1	0xe7f811e3
258 #define OPTEE_MSG_OS_OPTEE_UUID_2	0xbc5e0002
259 #define OPTEE_MSG_OS_OPTEE_UUID_3	0xa5d5c51b
260 #define OPTEE_MSG_FUNCID_GET_OS_UUID	0x0000
261 
262 /*
263  * Get revision of Trusted OS.
264  *
265  * Used by non-secure world to figure out which version of the Trusted OS
266  * is installed. Note that the returned revision is the revision of the
267  * Trusted OS, not of the API.
268  *
269  * Returns revision in 2 32-bit words in the same way as
270  * OPTEE_MSG_CALLS_REVISION described above.
271  */
272 #define OPTEE_MSG_FUNCID_GET_OS_REVISION	0x0001
273 
274 /*
275  * Do a secure call with struct optee_msg_arg as argument
276  * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
277  *
278  * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
279  * The first two parameters are tagged as meta, holding two value
280  * parameters to pass the following information:
281  * param[0].u.value.a-b uuid of Trusted Application
282  * param[1].u.value.a-b uuid of Client
283  * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
284  *
285  * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
286  * session to a Trusted Application.  struct optee_msg_arg::func is Trusted
287  * Application function, specific to the Trusted Application.
288  *
289  * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
290  * Trusted Application.
291  *
292  * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
293  *
294  * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
295  * information is passed as:
296  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
297  *					[| OPTEE_MSG_ATTR_FRAGMENT]
298  * [in] param[0].u.tmem.buf_ptr		physical address (of first fragment)
299  * [in] param[0].u.tmem.size		size (of first fragment)
300  * [in] param[0].u.tmem.shm_ref		holds shared memory reference
301  * ...
302  * The shared memory can optionally be fragmented, temp memrefs can follow
303  * each other with all but the last with the OPTEE_MSG_ATTR_FRAGMENT bit set.
304  *
305  * OPTEE_MSG_CMD_UNREGISTER_SHM unregisteres a previously registered shared
306  * memory reference. The information is passed as:
307  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
308  * [in] param[0].u.rmem.shm_ref		holds shared memory reference
309  * [in] param[0].u.rmem.offs		0
310  * [in] param[0].u.rmem.size		0
311  */
312 #define OPTEE_MSG_CMD_OPEN_SESSION	0
313 #define OPTEE_MSG_CMD_INVOKE_COMMAND	1
314 #define OPTEE_MSG_CMD_CLOSE_SESSION	2
315 #define OPTEE_MSG_CMD_CANCEL		3
316 #define OPTEE_MSG_CMD_REGISTER_SHM	4
317 #define OPTEE_MSG_CMD_UNREGISTER_SHM	5
318 #define OPTEE_MSG_FUNCID_CALL_WITH_ARG	0x0004
319 
320 /*****************************************************************************
321  * Part 3 - Requests from secure world, RPC
322  *****************************************************************************/
323 
324 /*
325  * All RPC is done with a struct optee_msg_arg as bearer of information,
326  * struct optee_msg_arg::arg holds values defined by OPTEE_MSG_RPC_CMD_* below.
327  * Only the commands handled by the kernel driver are defined here.
328  *
329  * RPC communication with tee-supplicant is reversed compared to normal
330  * client communication described above. The supplicant receives requests
331  * and sends responses. The command codes and exact protocol are defined in an
332  * external header file.
333  */
334 
335 /*
336  * Get time
337  *
338  * Returns number of seconds and nano seconds since the Epoch,
339  * 1970-01-01 00:00:00 +0000 (UTC).
340  *
341  * [out] param[0].u.value.a	Number of seconds
342  * [out] param[0].u.value.b	Number of nano seconds.
343  */
344 #define OPTEE_MSG_RPC_CMD_GET_TIME	3
345 
346 /*
347  * Wait queue primitive, helper for secure world to implement a wait queue.
348  *
349  * If secure world needs to wait for a secure world mutex it issues a sleep
350  * request instead of spinning in secure world. Conversely is a wakeup
351  * request issued when a secure world mutex with a thread waiting thread is
352  * unlocked.
353  *
354  * Waiting on a key
355  * [in] param[0].u.value.a OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP
356  * [in] param[0].u.value.b wait key
357  *
358  * Waking up a key
359  * [in] param[0].u.value.a OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP
360  * [in] param[0].u.value.b wakeup key
361  */
362 #define OPTEE_MSG_RPC_CMD_WAIT_QUEUE	4
363 #define OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP	0
364 #define OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP	1
365 
366 /*
367  * Suspend execution
368  *
369  * [in] param[0].value	.a number of milliseconds to suspend
370  */
371 #define OPTEE_MSG_RPC_CMD_SUSPEND	5
372 
373 /*
374  * Allocate a piece of shared memory
375  *
376  * Shared memory can optionally be fragmented, to support that additional
377  * spare param entries are allocated to make room for eventual fragments.
378  * The spare param entries has .attr = OPTEE_MSG_ATTR_TYPE_NONE when
379  * unused. All returned temp memrefs except the last should have the
380  * OPTEE_MSG_ATTR_FRAGMENT bit set in the attr field.
381  *
382  * [in]  param[0].u.value.a		type of memory one of
383  *					OPTEE_MSG_RPC_SHM_TYPE_* below
384  * [in]  param[0].u.value.b		requested size
385  * [in]  param[0].u.value.c		required alignment
386  *
387  * [out] param[0].u.tmem.buf_ptr	physical address (of first fragment)
388  * [out] param[0].u.tmem.size		size (of first fragment)
389  * [out] param[0].u.tmem.shm_ref	shared memory reference
390  * ...
391  * [out] param[n].u.tmem.buf_ptr	physical address
392  * [out] param[n].u.tmem.size		size
393  * [out] param[n].u.tmem.shm_ref	shared memory reference (same value
394  *					as in param[n-1].u.tmem.shm_ref)
395  */
396 #define OPTEE_MSG_RPC_CMD_SHM_ALLOC	6
397 /* Memory that can be shared with a non-secure user space application */
398 #define OPTEE_MSG_RPC_SHM_TYPE_APPL	0
399 /* Memory only shared with non-secure kernel */
400 #define OPTEE_MSG_RPC_SHM_TYPE_KERNEL	1
401 
402 /*
403  * Free shared memory previously allocated with OPTEE_MSG_RPC_CMD_SHM_ALLOC
404  *
405  * [in]  param[0].u.value.a		type of memory one of
406  *					OPTEE_MSG_RPC_SHM_TYPE_* above
407  * [in]  param[0].u.value.b		value of shared memory reference
408  *					returned in param[0].u.tmem.shm_ref
409  *					above
410  */
411 #define OPTEE_MSG_RPC_CMD_SHM_FREE	7
412 
413 /*
414  * Register timestamp buffer in the linux kernel optee driver
415  *
416  * [in] param[0].u.value.a	Subcommand (register buffer, unregister buffer)
417  * [in] param[0].u.value.b	Physical address of timestamp buffer
418  * [in] param[0].u.value.c	Size of buffer
419  */
420 #define OPTEE_MSG_RPC_CMD_BENCH_REG	20
421 
422 #endif /* _OPTEE_MSG_H */
423