xref: /optee_os/core/tee/tee_svc.c (revision 177603c71b1c11c78da9087d0f64dfeeef31571a)
1b0104773SPascal Brand /*
2b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
3b0104773SPascal Brand  * All rights reserved.
4b0104773SPascal Brand  *
5b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
6b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
7b0104773SPascal Brand  *
8b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
9b0104773SPascal Brand  * this list of conditions and the following disclaimer.
10b0104773SPascal Brand  *
11b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
12b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
13b0104773SPascal Brand  * and/or other materials provided with the distribution.
14b0104773SPascal Brand  *
15b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
26b0104773SPascal Brand  */
272eb765fcSJens Wiklander #include <util.h>
28b0104773SPascal Brand #include <kernel/tee_common_otp.h>
29b0104773SPascal Brand #include <kernel/tee_common.h>
30b0104773SPascal Brand #include <kernel/tee_compat.h>
31b0104773SPascal Brand #include <tee_api_types.h>
32b0104773SPascal Brand #include <kernel/tee_ta_manager.h>
33b0104773SPascal Brand #include <utee_types.h>
34b0104773SPascal Brand #include <tee/tee_svc.h>
35ffe04039SJerome Forissier #include <tee/tee_cryp_utl.h>
36*177603c7SJens Wiklander #include <tee/abi.h>
37b0104773SPascal Brand #include <mm/tee_mmu.h>
38b0104773SPascal Brand #include <mm/tee_mm.h>
39b0104773SPascal Brand #include <kernel/tee_rpc.h>
40b0104773SPascal Brand #include <kernel/tee_rpc_types.h>
41b0104773SPascal Brand #include <kernel/tee_time.h>
42b0104773SPascal Brand 
43b0104773SPascal Brand #include <user_ta_header.h>
444de4bebcSJens Wiklander #include <trace.h>
454de4bebcSJens Wiklander #include <kernel/trace_ta.h>
46b0104773SPascal Brand #include <kernel/chip_services.h>
47b0104773SPascal Brand 
487c876f12SPascal Brand #if (CFG_TRACE_LEVEL == TRACE_FLOW) && defined(CFG_TEE_CORE_TA_TRACE)
494de4bebcSJens Wiklander void tee_svc_trace_syscall(int num)
504de4bebcSJens Wiklander {
514de4bebcSJens Wiklander 	/* #0 is syscall return, not really interesting */
524de4bebcSJens Wiklander 	if (num == 0)
534de4bebcSJens Wiklander 		return;
544de4bebcSJens Wiklander 	FMSG("syscall #%d", num);
554de4bebcSJens Wiklander }
564de4bebcSJens Wiklander #endif
574de4bebcSJens Wiklander 
587c876f12SPascal Brand void tee_svc_sys_log(const void *buf __unused, size_t len __unused)
59b0104773SPascal Brand {
607c876f12SPascal Brand #ifdef CFG_TEE_CORE_TA_TRACE
61b0104773SPascal Brand 	char *kbuf;
62b0104773SPascal Brand 
63b0104773SPascal Brand 	if (len == 0)
64b0104773SPascal Brand 		return;
65b0104773SPascal Brand 
66b0104773SPascal Brand 	kbuf = malloc(len);
67b0104773SPascal Brand 	if (kbuf == NULL)
68b0104773SPascal Brand 		return;
69c0346845SJens Wiklander 	*kbuf = '\0';
70b0104773SPascal Brand 
71b0104773SPascal Brand 	/* log as Info/Raw traces */
72b0104773SPascal Brand 	if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS)
734de4bebcSJens Wiklander 		TAMSG_RAW("%.*s", (int)len, kbuf);
74b0104773SPascal Brand 
75b0104773SPascal Brand 	free(kbuf);
767c876f12SPascal Brand #endif
77b0104773SPascal Brand }
78b0104773SPascal Brand 
79b7fc217fSPascal Brand TEE_Result tee_svc_reserved(void)
80b7fc217fSPascal Brand {
81b7fc217fSPascal Brand 	return TEE_ERROR_GENERIC;
82b7fc217fSPascal Brand }
83b7fc217fSPascal Brand 
84197d17e7SSY Chiu TEE_Result tee_svc_not_supported(void)
85197d17e7SSY Chiu {
86197d17e7SSY Chiu 	return TEE_ERROR_NOT_SUPPORTED;
87197d17e7SSY Chiu }
88197d17e7SSY Chiu 
89cebdec51SJens Wiklander uint32_t tee_svc_sys_dummy(uint32_t *a __unused)
90b0104773SPascal Brand {
91b0104773SPascal Brand 	DMSG("tee_svc_sys_dummy: a 0x%x", (unsigned int)a);
92b0104773SPascal Brand 	return 0;
93b0104773SPascal Brand }
94b0104773SPascal Brand 
95cebdec51SJens Wiklander uint32_t tee_svc_sys_dummy_7args(uint32_t a1 __unused, uint32_t a2 __unused,
96cebdec51SJens Wiklander 				uint32_t a3 __unused, uint32_t a4 __unused,
97cebdec51SJens Wiklander 				uint32_t a5 __unused, uint32_t a6 __unused,
98cebdec51SJens Wiklander 				uint32_t a7 __unused)
99b0104773SPascal Brand {
100b0104773SPascal Brand 	DMSG("tee_svc_sys_dummy_7args: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, %x, %x\n",
101b0104773SPascal Brand 	     a1, a2, a3, a4, a5, a6, a7);
102b0104773SPascal Brand 	return 0;
103b0104773SPascal Brand }
104b0104773SPascal Brand 
105b0104773SPascal Brand uint32_t tee_svc_sys_nocall(void)
106b0104773SPascal Brand {
107b0104773SPascal Brand 	DMSG("No syscall");
108b0104773SPascal Brand 	return 0x1;
109b0104773SPascal Brand }
110b0104773SPascal Brand 
111b0104773SPascal Brand TEE_Result tee_svc_sys_get_property(uint32_t prop, tee_uaddr_t buf, size_t blen)
112b0104773SPascal Brand {
113b0104773SPascal Brand 	static const char api_vers[] = "1.0";
114b0104773SPascal Brand 	static const char descr[] = "Version N.N";
115b0104773SPascal Brand 	/*
116b0104773SPascal Brand 	 * Value 100 means:
117b0104773SPascal Brand 	 * System time based on REE-controlled timers. Can be tampered by the
118b0104773SPascal Brand 	 * REE.  The implementation must still guarantee that the system time
119b0104773SPascal Brand 	 * is monotonous, i.e., successive calls to TEE_GetSystemTime must
120b0104773SPascal Brand 	 * return increasing values of the system time.
121b0104773SPascal Brand 	 */
122b0104773SPascal Brand 	static const uint32_t sys_time_prot_lvl = 100;
123b0104773SPascal Brand 	static const uint32_t ta_time_prot_lvl = 100;
124b0104773SPascal Brand 	struct tee_ta_session *sess;
125b0104773SPascal Brand 	TEE_Result res;
126b0104773SPascal Brand 
127b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
128b0104773SPascal Brand 	if (res != TEE_SUCCESS)
129b0104773SPascal Brand 		return res;
130b0104773SPascal Brand 
131b0104773SPascal Brand 	switch (prop) {
132b0104773SPascal Brand 	case UTEE_PROP_TEE_API_VERSION:
133b0104773SPascal Brand 		if (blen < sizeof(api_vers))
134b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
135b0104773SPascal Brand 		return tee_svc_copy_to_user(sess, (void *)buf, api_vers,
136b0104773SPascal Brand 					    sizeof(api_vers));
137b0104773SPascal Brand 
138b0104773SPascal Brand 	case UTEE_PROP_TEE_DESCR:
139b0104773SPascal Brand 		if (blen < sizeof(descr))
140b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
141b0104773SPascal Brand 		return tee_svc_copy_to_user(sess, (void *)buf, descr,
142b0104773SPascal Brand 					    sizeof(descr));
143b0104773SPascal Brand 
144b0104773SPascal Brand 	case UTEE_PROP_TEE_DEV_ID:
145b0104773SPascal Brand 		{
146b0104773SPascal Brand 			TEE_UUID uuid;
147b0104773SPascal Brand 			const size_t nslen = 4;
148b0104773SPascal Brand 			uint8_t data[4 +
149b0104773SPascal Brand 				     FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
150b0104773SPascal Brand 			    'S', 'T', 'E', 'E' };
151b0104773SPascal Brand 
152b0104773SPascal Brand 			if (blen < sizeof(uuid))
153b0104773SPascal Brand 				return TEE_ERROR_SHORT_BUFFER;
154b0104773SPascal Brand 
155b0104773SPascal Brand 			if (tee_otp_get_die_id
156b0104773SPascal Brand 					(data + nslen, sizeof(data) - nslen))
157b0104773SPascal Brand 				return TEE_ERROR_BAD_STATE;
158b0104773SPascal Brand 
159ffe04039SJerome Forissier 			res = tee_hash_createdigest(TEE_ALG_SHA256, data,
160ffe04039SJerome Forissier 						    sizeof(data),
161ffe04039SJerome Forissier 						    (uint8_t *)&uuid,
162ffe04039SJerome Forissier 						    sizeof(uuid));
163b0104773SPascal Brand 			if (res != TEE_SUCCESS)
164b0104773SPascal Brand 				return TEE_ERROR_BAD_STATE;
165b0104773SPascal Brand 
166b0104773SPascal Brand 			/*
167b0104773SPascal Brand 			 * Changes the random value into and UUID as specifiec
168b0104773SPascal Brand 			 * in RFC 4122. The magic values are from the example
169b0104773SPascal Brand 			 * code in the RFC.
170b0104773SPascal Brand 			 *
171b0104773SPascal Brand 			 * TEE_UUID is defined slightly different from the RFC,
172b0104773SPascal Brand 			 * but close enough for our purpose.
173b0104773SPascal Brand 			 */
174b0104773SPascal Brand 
175b0104773SPascal Brand 			uuid.timeHiAndVersion &= 0x0fff;
176b0104773SPascal Brand 			uuid.timeHiAndVersion |= 5 << 12;
177b0104773SPascal Brand 
178b0104773SPascal Brand 			/* uuid.clock_seq_hi_and_reserved in the RFC */
179b0104773SPascal Brand 			uuid.clockSeqAndNode[0] &= 0x3f;
180b0104773SPascal Brand 			uuid.clockSeqAndNode[0] |= 0x80;
181b0104773SPascal Brand 
182b0104773SPascal Brand 			return tee_svc_copy_to_user(sess, (void *)buf, &uuid,
183b0104773SPascal Brand 						    sizeof(TEE_UUID));
184b0104773SPascal Brand 		}
185b0104773SPascal Brand 
186b0104773SPascal Brand 	case UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL:
187b0104773SPascal Brand 		if (blen < sizeof(sys_time_prot_lvl))
188b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
189b0104773SPascal Brand 		return tee_svc_copy_to_user(sess, (void *)buf,
190b0104773SPascal Brand 					    &sys_time_prot_lvl,
191b0104773SPascal Brand 					    sizeof(sys_time_prot_lvl));
192b0104773SPascal Brand 
193b0104773SPascal Brand 	case UTEE_PROP_TEE_TA_TIME_PROT_LEVEL:
194b0104773SPascal Brand 		if (blen < sizeof(ta_time_prot_lvl))
195b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
196b0104773SPascal Brand 		return tee_svc_copy_to_user(sess, (void *)buf,
197b0104773SPascal Brand 					    &ta_time_prot_lvl,
198b0104773SPascal Brand 					    sizeof(ta_time_prot_lvl));
199b0104773SPascal Brand 
200b0104773SPascal Brand 	case UTEE_PROP_CLIENT_ID:
201b0104773SPascal Brand 		if (blen < sizeof(TEE_Identity))
202b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
203b0104773SPascal Brand 
204b0104773SPascal Brand 		return tee_svc_copy_to_user(sess, (void *)buf,
20537d6ae92SPascal Brand 			&sess->clnt_id, sizeof(TEE_Identity));
20637d6ae92SPascal Brand 
207b0104773SPascal Brand 	case UTEE_PROP_TA_APP_ID:
208b0104773SPascal Brand 		if (blen < sizeof(TEE_UUID))
209b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
210b0104773SPascal Brand 
211b0104773SPascal Brand 		return tee_svc_copy_to_user(sess, (void *)buf,
21237d6ae92SPascal Brand 			&sess->ctx->head->uuid, sizeof(TEE_UUID));
213b0104773SPascal Brand 
214b0104773SPascal Brand 	default:
215b0104773SPascal Brand 		break;
216b0104773SPascal Brand 	}
217b0104773SPascal Brand 	return TEE_ERROR_NOT_IMPLEMENTED;
218b0104773SPascal Brand }
219b0104773SPascal Brand 
220b0104773SPascal Brand /*
221b0104773SPascal Brand  * TA invokes some TA with parameter.
222b0104773SPascal Brand  * If some parameters are memory references:
223b0104773SPascal Brand  * - either the memref is inside TA private RAM: TA is not allowed to expose
224b0104773SPascal Brand  *   its private RAM: use a temporary memory buffer and copy the data.
225b0104773SPascal Brand  * - or the memref is not in the TA private RAM:
226b0104773SPascal Brand  *   - if the memref was mapped to the TA, TA is allowed to expose it.
227b0104773SPascal Brand  *   - if so, converts memref virtual address into a physical address.
228b0104773SPascal Brand  */
229b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
230b0104773SPascal Brand 				     struct tee_ta_session *called_sess,
231b0104773SPascal Brand 				     uint32_t param_types,
232*177603c7SJens Wiklander 				     struct abi_user32_param *callee_params,
233b0104773SPascal Brand 				     struct tee_ta_param *param,
234b0104773SPascal Brand 				     tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
235b0104773SPascal Brand 				     tee_mm_entry_t **mm)
236b0104773SPascal Brand {
237b0104773SPascal Brand 	size_t n;
238b0104773SPascal Brand 	TEE_Result res;
239b0104773SPascal Brand 	size_t req_mem = 0;
240b0104773SPascal Brand 	size_t s;
241b0104773SPascal Brand 	uint8_t *dst = 0;
242b0104773SPascal Brand 	tee_paddr_t dst_pa, src_pa = 0;
243b0104773SPascal Brand 	bool ta_private_memref[TEE_NUM_PARAMS];
244b0104773SPascal Brand 
245b7fc217fSPascal Brand 	/* fill 'param' input struct with caller params description buffer */
246b0104773SPascal Brand 	param->types = param_types;
247b7fc217fSPascal Brand 	if (!callee_params) {
248b0104773SPascal Brand 		if (param->types != 0)
249b0104773SPascal Brand 			return TEE_ERROR_BAD_PARAMETERS;
250b0104773SPascal Brand 		memset(param->params, 0, sizeof(param->params));
251b0104773SPascal Brand 	} else {
252*177603c7SJens Wiklander 		res = tee_mmu_check_access_rights(sess->ctx,
253*177603c7SJens Wiklander 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
254*177603c7SJens Wiklander 			(tee_uaddr_t)callee_params,
255*177603c7SJens Wiklander 			sizeof(struct abi_user32_param));
256*177603c7SJens Wiklander 		if (res != TEE_SUCCESS)
257*177603c7SJens Wiklander 			return res;
258*177603c7SJens Wiklander 		abi_user32_param_to_param(param->params, callee_params,
259*177603c7SJens Wiklander 					  param_types);
260b0104773SPascal Brand 	}
261b0104773SPascal Brand 
262b0104773SPascal Brand 	if ((called_sess != NULL) &&
263b0104773SPascal Brand 		(called_sess->ctx->static_ta == NULL) &&
264b0104773SPascal Brand 		(called_sess->ctx->flags & TA_FLAG_USER_MODE) == 0) {
265b0104773SPascal Brand 		/*
266b0104773SPascal Brand 		 * kernel TA, borrow the mapping of the calling
267b0104773SPascal Brand 		 * during this call.
268b0104773SPascal Brand 		 */
269b0104773SPascal Brand 		called_sess->calling_sess = sess;
270b0104773SPascal Brand 		return TEE_SUCCESS;
271b0104773SPascal Brand 	}
272b0104773SPascal Brand 
273b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
274b0104773SPascal Brand 
275b0104773SPascal Brand 		ta_private_memref[n] = false;
276b0104773SPascal Brand 
277b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
278b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
279b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
280b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
281b0104773SPascal Brand 			if (param->params[n].memref.buffer == NULL) {
282b0104773SPascal Brand 				if (param->params[n].memref.size != 0)
283b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
284b0104773SPascal Brand 				break;
285b0104773SPascal Brand 			}
286b0104773SPascal Brand 			/* uTA cannot expose its private memory */
287b0104773SPascal Brand 			if (tee_mmu_is_vbuf_inside_ta_private(sess->ctx,
288106d8aa6SPascal Brand 				    param->params[n].memref.buffer,
289b0104773SPascal Brand 				    param->params[n].memref.size)) {
290b0104773SPascal Brand 
291a17acc4cSSabrina Ni 				s = ROUNDUP(param->params[n].memref.size,
292b0104773SPascal Brand 						sizeof(uint32_t));
293b0104773SPascal Brand 				/* Check overflow */
294b0104773SPascal Brand 				if (req_mem + s < req_mem)
295b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
296b0104773SPascal Brand 				req_mem += s;
297b0104773SPascal Brand 				ta_private_memref[n] = true;
298b0104773SPascal Brand 				break;
299b0104773SPascal Brand 			}
300106d8aa6SPascal Brand 			if (tee_mmu_is_vbuf_intersect_ta_private(sess->ctx,
301106d8aa6SPascal Brand 				    param->params[n].memref.buffer,
302b0104773SPascal Brand 				    param->params[n].memref.size))
303b0104773SPascal Brand 				return TEE_ERROR_BAD_PARAMETERS;
304b0104773SPascal Brand 
305b0104773SPascal Brand 			if (tee_mmu_user_va2pa(sess->ctx,
306b0104773SPascal Brand 					(void *)param->params[n].memref.buffer,
3070e692b78SJens Wiklander 					&src_pa) != TEE_SUCCESS)
308b0104773SPascal Brand 				return TEE_ERROR_BAD_PARAMETERS;
309b0104773SPascal Brand 
310b0104773SPascal Brand 			param->param_attr[n] = tee_mmu_user_get_cache_attr(
311b0104773SPascal Brand 				sess->ctx,
312b0104773SPascal Brand 				(void *)param->params[n].memref.buffer);
313b0104773SPascal Brand 
314b0104773SPascal Brand 			param->params[n].memref.buffer = (void *)src_pa;
315b0104773SPascal Brand 			break;
316b0104773SPascal Brand 
317b0104773SPascal Brand 		default:
318b0104773SPascal Brand 			break;
319b0104773SPascal Brand 		}
320b0104773SPascal Brand 	}
321b0104773SPascal Brand 
322b0104773SPascal Brand 	if (req_mem == 0)
323b0104773SPascal Brand 		return TEE_SUCCESS;
324b0104773SPascal Brand 
325b0104773SPascal Brand 	/* Allocate section in secure DDR */
326b0104773SPascal Brand 	*mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem);
327b0104773SPascal Brand 	if (*mm == NULL) {
328b0104773SPascal Brand 		DMSG("tee_mm_alloc TEE_ERROR_GENERIC");
329b0104773SPascal Brand 		return TEE_ERROR_GENERIC;
330b0104773SPascal Brand 	}
331b0104773SPascal Brand 
332b0104773SPascal Brand 	/* Get the virtual address for the section in secure DDR */
333b0104773SPascal Brand 	res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst);
334b0104773SPascal Brand 	if (res != TEE_SUCCESS)
335b0104773SPascal Brand 		return res;
336b0104773SPascal Brand 	dst_pa = tee_mm_get_smem(*mm);
337b0104773SPascal Brand 
338b0104773SPascal Brand 	for (n = 0; n < 4; n++) {
339b0104773SPascal Brand 
340b0104773SPascal Brand 		if (ta_private_memref[n] == false)
341b0104773SPascal Brand 			continue;
342b0104773SPascal Brand 
343a17acc4cSSabrina Ni 		s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t));
344b0104773SPascal Brand 
345b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
346b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
347b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
348b0104773SPascal Brand 			if (param->params[n].memref.buffer != NULL) {
349b0104773SPascal Brand 				res = tee_svc_copy_from_user(sess, dst,
350b7fc217fSPascal Brand 						param->params[n].memref.buffer,
351b7fc217fSPascal Brand 						param->params[n].memref.size);
352b0104773SPascal Brand 				if (res != TEE_SUCCESS)
353b0104773SPascal Brand 					return res;
354b0104773SPascal Brand 				param->param_attr[n] =
355b0104773SPascal Brand 					tee_mmu_kmap_get_cache_attr(dst);
356b0104773SPascal Brand 				param->params[n].memref.buffer = (void *)dst_pa;
357b0104773SPascal Brand 				tmp_buf_pa[n] = dst_pa;
358b0104773SPascal Brand 				dst += s;
359b0104773SPascal Brand 				dst_pa += s;
360b0104773SPascal Brand 			}
361b0104773SPascal Brand 			break;
362b0104773SPascal Brand 
363b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
364b0104773SPascal Brand 			if (param->params[n].memref.buffer != NULL) {
365b0104773SPascal Brand 				param->param_attr[n] =
366b0104773SPascal Brand 					tee_mmu_kmap_get_cache_attr(dst);
367b0104773SPascal Brand 				param->params[n].memref.buffer = (void *)dst_pa;
368b0104773SPascal Brand 				tmp_buf_pa[n] = dst_pa;
369b0104773SPascal Brand 				dst += s;
370b0104773SPascal Brand 				dst_pa += s;
371b0104773SPascal Brand 			}
372b0104773SPascal Brand 			break;
373b0104773SPascal Brand 
374b0104773SPascal Brand 		default:
375b0104773SPascal Brand 			continue;
376b0104773SPascal Brand 		}
377b0104773SPascal Brand 	}
378b0104773SPascal Brand 
379b0104773SPascal Brand 	tee_mmu_kunmap(dst, req_mem);
380b0104773SPascal Brand 
381b0104773SPascal Brand 	return TEE_SUCCESS;
382b0104773SPascal Brand }
383b0104773SPascal Brand 
384b0104773SPascal Brand /*
385b0104773SPascal Brand  * Back from execution of service: update parameters passed from TA:
386b0104773SPascal Brand  * If some parameters were memory references:
387b0104773SPascal Brand  * - either the memref was temporary: copy back data and update size
388b0104773SPascal Brand  * - or it was the original TA memref: update only the size value.
389b0104773SPascal Brand  */
390b0104773SPascal Brand static TEE_Result tee_svc_update_out_param(
391b0104773SPascal Brand 		struct tee_ta_session *sess,
392b0104773SPascal Brand 		struct tee_ta_session *called_sess,
393b0104773SPascal Brand 		struct tee_ta_param *param,
394b0104773SPascal Brand 		tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
395*177603c7SJens Wiklander 		struct abi_user32_param *usr_param)
396b0104773SPascal Brand {
397b0104773SPascal Brand 	size_t n;
398*177603c7SJens Wiklander 	TEE_Param callee_params[TEE_NUM_PARAMS];
399b0104773SPascal Brand 	bool have_private_mem_map = (called_sess == NULL) ||
400b0104773SPascal Brand 		(called_sess->ctx->static_ta != NULL) ||
401b0104773SPascal Brand 		((called_sess->ctx->flags & TA_FLAG_USER_MODE) != 0);
402b0104773SPascal Brand 
403b0104773SPascal Brand 	tee_ta_set_current_session(sess);
404*177603c7SJens Wiklander 	abi_user32_param_to_param(callee_params, usr_param, param->types);
405b0104773SPascal Brand 
406b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
407b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
408b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
409b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
410b0104773SPascal Brand 
411b0104773SPascal Brand 			/* outside TA private => memref is valid, update size */
412b0104773SPascal Brand 			if (!tee_mmu_is_vbuf_inside_ta_private(sess->ctx,
413106d8aa6SPascal Brand 					callee_params[n].memref.buffer,
414b0104773SPascal Brand 					param->params[n].memref.size)) {
415b7fc217fSPascal Brand 				callee_params[n].memref.size =
416b0104773SPascal Brand 					param->params[n].memref.size;
417b0104773SPascal Brand 				break;
418b0104773SPascal Brand 			}
419b0104773SPascal Brand 
420b0104773SPascal Brand 			/*
421b0104773SPascal Brand 			 * If we called a kernel TA the parameters are in shared
422b0104773SPascal Brand 			 * memory and no copy is needed.
423b0104773SPascal Brand 			 */
424b0104773SPascal Brand 			if (have_private_mem_map &&
425b0104773SPascal Brand 			    param->params[n].memref.size <=
426b7fc217fSPascal Brand 			    callee_params[n].memref.size) {
427b0104773SPascal Brand 				uint8_t *src = 0;
428b0104773SPascal Brand 				TEE_Result res;
429b0104773SPascal Brand 
430b0104773SPascal Brand 				/* FIXME: TA_RAM is already mapped ! */
431b0104773SPascal Brand 				res = tee_mmu_kmap(tmp_buf_pa[n],
432b0104773SPascal Brand 					param->params[n].memref.size, &src);
433b0104773SPascal Brand 				if (res != TEE_SUCCESS)
434b0104773SPascal Brand 					return TEE_ERROR_GENERIC;
435b0104773SPascal Brand 
436b0104773SPascal Brand 				res = tee_svc_copy_to_user(sess,
437b7fc217fSPascal Brand 							 callee_params[n].memref.
438b0104773SPascal Brand 							 buffer, src,
439b0104773SPascal Brand 							 param->params[n].
440b0104773SPascal Brand 							 memref.size);
441b0104773SPascal Brand 				if (res != TEE_SUCCESS)
442b0104773SPascal Brand 					return res;
443b0104773SPascal Brand 				tee_mmu_kunmap(src,
444b0104773SPascal Brand 					       param->params[n].memref.size);
445b0104773SPascal Brand 
446b0104773SPascal Brand 			}
447b7fc217fSPascal Brand 			callee_params[n].memref.size = param->params[n].memref.size;
448b0104773SPascal Brand 			break;
449b0104773SPascal Brand 
450b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
451b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_INOUT:
452b7fc217fSPascal Brand 			callee_params[n].value = param->params[n].value;
453b0104773SPascal Brand 			break;
454b0104773SPascal Brand 
455b0104773SPascal Brand 		default:
456b0104773SPascal Brand 			continue;
457b0104773SPascal Brand 		}
458b0104773SPascal Brand 	}
459b0104773SPascal Brand 
460*177603c7SJens Wiklander 	abi_param_to_user32_param(usr_param, callee_params, param->types);
461*177603c7SJens Wiklander 
462b0104773SPascal Brand 	return TEE_SUCCESS;
463b0104773SPascal Brand }
464b0104773SPascal Brand 
465b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */
466b0104773SPascal Brand TEE_Result tee_svc_open_ta_session(const TEE_UUID *dest,
467b0104773SPascal Brand 				   uint32_t cancel_req_to, uint32_t param_types,
468*177603c7SJens Wiklander 				   struct abi_user32_param *usr_param,
469b0104773SPascal Brand 				   TEE_TASessionHandle *ta_sess,
470b0104773SPascal Brand 				   uint32_t *ret_orig)
471b0104773SPascal Brand {
472b0104773SPascal Brand 	TEE_Result res;
473b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
474b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
475b0104773SPascal Brand 	struct tee_ta_session *sess;
476b0104773SPascal Brand 	tee_mm_entry_t *mm_param = NULL;
477b0104773SPascal Brand 
478b0104773SPascal Brand 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
479b0104773SPascal Brand 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
480b0104773SPascal Brand 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
481b0104773SPascal Brand 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
482b0104773SPascal Brand 
483b0104773SPascal Brand 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
484b0104773SPascal Brand 		res = TEE_ERROR_OUT_OF_MEMORY;
485b0104773SPascal Brand 		goto out_free_only;
486b0104773SPascal Brand 	}
487b0104773SPascal Brand 
488b0104773SPascal Brand 	memset(param, 0, sizeof(struct tee_ta_param));
489b0104773SPascal Brand 
490b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
491b0104773SPascal Brand 	if (res != TEE_SUCCESS)
492b0104773SPascal Brand 		goto out_free_only;
493b0104773SPascal Brand 
494b0104773SPascal Brand 	res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID));
495b0104773SPascal Brand 	if (res != TEE_SUCCESS)
496b0104773SPascal Brand 		goto function_exit;
497b0104773SPascal Brand 
498b0104773SPascal Brand 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
499b0104773SPascal Brand 	memcpy(&clnt_id->uuid, &sess->ctx->head->uuid, sizeof(TEE_UUID));
500b0104773SPascal Brand 
501*177603c7SJens Wiklander 	res = tee_svc_copy_param(sess, NULL, param_types, usr_param, param,
502b0104773SPascal Brand 				 tmp_buf_pa, &mm_param);
503b0104773SPascal Brand 	if (res != TEE_SUCCESS)
504b0104773SPascal Brand 		goto function_exit;
505b0104773SPascal Brand 
506b0104773SPascal Brand 	/*
507b0104773SPascal Brand 	 * Find session of a multi session TA or a static TA
508b0104773SPascal Brand 	 * In such a case, there is no need to ask the supplicant for the TA
509b0104773SPascal Brand 	 * code
510b0104773SPascal Brand 	 */
511b0104773SPascal Brand 	res = tee_ta_open_session(&ret_o, &s, &sess->ctx->open_sessions, uuid,
51227cbcc57SJens Wiklander 				  clnt_id, cancel_req_to, param);
513c0346845SJens Wiklander 	if (res != TEE_SUCCESS)
514b0104773SPascal Brand 		goto function_exit;
515b0104773SPascal Brand 
516*177603c7SJens Wiklander 	res = tee_svc_update_out_param(sess, NULL, param, tmp_buf_pa,
517*177603c7SJens Wiklander 				       usr_param);
518b0104773SPascal Brand 
519b0104773SPascal Brand function_exit:
520b0104773SPascal Brand 	tee_ta_set_current_session(sess);
521b0104773SPascal Brand 
522b0104773SPascal Brand 	if (mm_param != NULL) {
523b0104773SPascal Brand 		TEE_Result res2;
524b0104773SPascal Brand 		void *va = 0;
525b0104773SPascal Brand 
526b0104773SPascal Brand 		res2 =
527b0104773SPascal Brand 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
528b0104773SPascal Brand 		if (res2 == TEE_SUCCESS)
529b0104773SPascal Brand 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
530b0104773SPascal Brand 	}
531b0104773SPascal Brand 	tee_mm_free(mm_param);
532b0104773SPascal Brand 	tee_svc_copy_to_user(sess, ta_sess, &s, sizeof(s));
533b0104773SPascal Brand 	tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
534b0104773SPascal Brand 
535b0104773SPascal Brand out_free_only:
536b0104773SPascal Brand 	free(param);
537b0104773SPascal Brand 	free(uuid);
538b0104773SPascal Brand 	free(clnt_id);
539b0104773SPascal Brand 	return res;
540b0104773SPascal Brand }
541b0104773SPascal Brand 
542b0104773SPascal Brand TEE_Result tee_svc_close_ta_session(TEE_TASessionHandle ta_sess)
543b0104773SPascal Brand {
544b0104773SPascal Brand 	TEE_Result res;
545b0104773SPascal Brand 	struct tee_ta_session *sess;
54660699957SPascal Brand 	TEE_Identity clnt_id;
547b0104773SPascal Brand 
548b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
549b0104773SPascal Brand 	if (res != TEE_SUCCESS)
550b0104773SPascal Brand 		return res;
551b0104773SPascal Brand 
55260699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
55360699957SPascal Brand 	memcpy(&clnt_id.uuid, &sess->ctx->head->uuid, sizeof(TEE_UUID));
554b0104773SPascal Brand 
55560699957SPascal Brand 	tee_ta_set_current_session(NULL);
55660699957SPascal Brand 	res = tee_ta_close_session((uint32_t)ta_sess, &sess->ctx->open_sessions,
55760699957SPascal Brand 				   &clnt_id);
558b0104773SPascal Brand 	tee_ta_set_current_session(sess);
559b0104773SPascal Brand 	return res;
560b0104773SPascal Brand }
561b0104773SPascal Brand 
562b0104773SPascal Brand TEE_Result tee_svc_invoke_ta_command(TEE_TASessionHandle ta_sess,
563b0104773SPascal Brand 				     uint32_t cancel_req_to, uint32_t cmd_id,
564*177603c7SJens Wiklander 				     uint32_t param_types,
565*177603c7SJens Wiklander 				     struct abi_user32_param *usr_param,
566b0104773SPascal Brand 				     uint32_t *ret_orig)
567b0104773SPascal Brand {
568b0104773SPascal Brand 	TEE_Result res;
569b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
570b0104773SPascal Brand 	struct tee_ta_param param = { 0 };
57160699957SPascal Brand 	TEE_Identity clnt_id;
572b0104773SPascal Brand 	struct tee_ta_session *sess;
573b0104773SPascal Brand 	struct tee_ta_session *called_sess = (struct tee_ta_session *)ta_sess;
574b0104773SPascal Brand 	tee_mm_entry_t *mm_param = NULL;
575b0104773SPascal Brand 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
576b0104773SPascal Brand 
577b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
578b0104773SPascal Brand 	if (res != TEE_SUCCESS)
579b0104773SPascal Brand 		return res;
580b0104773SPascal Brand 
581b0104773SPascal Brand 	res =
582b0104773SPascal Brand 	    tee_ta_verify_session_pointer(called_sess,
583b0104773SPascal Brand 					  &sess->ctx->open_sessions);
584b0104773SPascal Brand 	if (res != TEE_SUCCESS)
585b0104773SPascal Brand 		return res;
586b0104773SPascal Brand 
58760699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
58860699957SPascal Brand 	memcpy(&clnt_id.uuid, &sess->ctx->head->uuid, sizeof(TEE_UUID));
58960699957SPascal Brand 
590*177603c7SJens Wiklander 	res = tee_svc_copy_param(sess, called_sess, param_types, usr_param,
591b0104773SPascal Brand 				 &param, tmp_buf_pa, &mm_param);
592b0104773SPascal Brand 	if (res != TEE_SUCCESS)
593b0104773SPascal Brand 		goto function_exit;
594b0104773SPascal Brand 
59560699957SPascal Brand 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
59660699957SPascal Brand 				    cancel_req_to, cmd_id, &param);
59760699957SPascal Brand 
598b0104773SPascal Brand 	if (res != TEE_SUCCESS)
599b0104773SPascal Brand 		goto function_exit;
600b0104773SPascal Brand 
601b0104773SPascal Brand 	res = tee_svc_update_out_param(sess, called_sess, &param, tmp_buf_pa,
602*177603c7SJens Wiklander 				       usr_param);
603b0104773SPascal Brand 	if (res != TEE_SUCCESS)
604b0104773SPascal Brand 		goto function_exit;
605b0104773SPascal Brand 
606b0104773SPascal Brand function_exit:
607b0104773SPascal Brand 	tee_ta_set_current_session(sess);
608b0104773SPascal Brand 	called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */
609b0104773SPascal Brand 
610b0104773SPascal Brand 	if (mm_param != NULL) {
611b0104773SPascal Brand 		TEE_Result res2;
612b0104773SPascal Brand 		void *va = 0;
613b0104773SPascal Brand 
614b0104773SPascal Brand 		res2 =
615b0104773SPascal Brand 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
616b0104773SPascal Brand 		if (res2 == TEE_SUCCESS)
617b0104773SPascal Brand 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
618b0104773SPascal Brand 	}
619b0104773SPascal Brand 	tee_mm_free(mm_param);
620b0104773SPascal Brand 	if (ret_orig)
621b0104773SPascal Brand 		tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
622b0104773SPascal Brand 	return res;
623b0104773SPascal Brand }
624b0104773SPascal Brand 
625b0104773SPascal Brand TEE_Result tee_svc_check_access_rights(uint32_t flags, const void *buf,
626b0104773SPascal Brand 				       size_t len)
627b0104773SPascal Brand {
628b0104773SPascal Brand 	TEE_Result res;
629b0104773SPascal Brand 	struct tee_ta_session *s;
630b0104773SPascal Brand 
631b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
632b0104773SPascal Brand 	if (res != TEE_SUCCESS)
633b0104773SPascal Brand 		return res;
634b0104773SPascal Brand 
635b0104773SPascal Brand 	return tee_mmu_check_access_rights(s->ctx, flags, (tee_uaddr_t)buf,
636b0104773SPascal Brand 					   len);
637b0104773SPascal Brand }
638b0104773SPascal Brand 
639b0104773SPascal Brand TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr,
640b0104773SPascal Brand 				  const void *uaddr, size_t len)
641b0104773SPascal Brand {
642b0104773SPascal Brand 	TEE_Result res;
643b0104773SPascal Brand 	struct tee_ta_session *s;
644b0104773SPascal Brand 
645b0104773SPascal Brand 	if (sess == NULL) {
646b0104773SPascal Brand 		res = tee_ta_get_current_session(&s);
647b0104773SPascal Brand 		if (res != TEE_SUCCESS)
648b0104773SPascal Brand 			return res;
649b0104773SPascal Brand 	} else {
650b0104773SPascal Brand 		s = sess;
651b0104773SPascal Brand 		tee_ta_set_current_session(s);
652b0104773SPascal Brand 	}
653b0104773SPascal Brand 	res =
654b0104773SPascal Brand 	    tee_mmu_check_access_rights(s->ctx,
655b0104773SPascal Brand 					TEE_MEMORY_ACCESS_READ |
656b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
657b0104773SPascal Brand 					(tee_uaddr_t)uaddr, len);
658b0104773SPascal Brand 	if (res != TEE_SUCCESS)
659b0104773SPascal Brand 		return res;
660b0104773SPascal Brand 
661b0104773SPascal Brand 	memcpy(kaddr, uaddr, len);
662b0104773SPascal Brand 	return TEE_SUCCESS;
663b0104773SPascal Brand }
664b0104773SPascal Brand 
665b0104773SPascal Brand TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr,
666b0104773SPascal Brand 				const void *kaddr, size_t len)
667b0104773SPascal Brand {
668b0104773SPascal Brand 	TEE_Result res;
669b0104773SPascal Brand 	struct tee_ta_session *s;
670b0104773SPascal Brand 
671b0104773SPascal Brand 	if (sess == NULL) {
672b0104773SPascal Brand 		res = tee_ta_get_current_session(&s);
673b0104773SPascal Brand 		if (res != TEE_SUCCESS)
674b0104773SPascal Brand 			return res;
675b0104773SPascal Brand 	} else {
676b0104773SPascal Brand 		s = sess;
677b0104773SPascal Brand 		tee_ta_set_current_session(s);
678b0104773SPascal Brand 	}
679b0104773SPascal Brand 
680b0104773SPascal Brand 	res =
681b0104773SPascal Brand 	    tee_mmu_check_access_rights(s->ctx,
682b0104773SPascal Brand 					TEE_MEMORY_ACCESS_WRITE |
683b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
684b0104773SPascal Brand 					(tee_uaddr_t)uaddr, len);
685b0104773SPascal Brand 	if (res != TEE_SUCCESS)
686b0104773SPascal Brand 		return res;
687b0104773SPascal Brand 
688b0104773SPascal Brand 	memcpy(uaddr, kaddr, len);
689b0104773SPascal Brand 	return TEE_SUCCESS;
690b0104773SPascal Brand }
691b0104773SPascal Brand 
692b0104773SPascal Brand static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time)
693b0104773SPascal Brand {
694b0104773SPascal Brand 	TEE_Time current_time;
695b0104773SPascal Brand 
696b0104773SPascal Brand 	if (s->cancel_mask)
697b0104773SPascal Brand 		return false;
698b0104773SPascal Brand 
699b0104773SPascal Brand 	if (s->cancel)
700b0104773SPascal Brand 		return true;
701b0104773SPascal Brand 
702b0104773SPascal Brand 	if (s->cancel_time.seconds == UINT32_MAX)
703b0104773SPascal Brand 		return false;
704b0104773SPascal Brand 
705b0104773SPascal Brand 	if (curr_time != NULL)
706b0104773SPascal Brand 		current_time = *curr_time;
707b0104773SPascal Brand 	else if (tee_time_get_sys_time(&current_time) != TEE_SUCCESS)
708b0104773SPascal Brand 		return false;
709b0104773SPascal Brand 
710b0104773SPascal Brand 	if (current_time.seconds > s->cancel_time.seconds ||
711b0104773SPascal Brand 	    (current_time.seconds == s->cancel_time.seconds &&
712b0104773SPascal Brand 	     current_time.millis >= s->cancel_time.millis)) {
713b0104773SPascal Brand 		return true;
714b0104773SPascal Brand 	}
715b0104773SPascal Brand 
716b0104773SPascal Brand 	return false;
717b0104773SPascal Brand }
718b0104773SPascal Brand 
719b0104773SPascal Brand TEE_Result tee_svc_get_cancellation_flag(bool *cancel)
720b0104773SPascal Brand {
721b0104773SPascal Brand 	TEE_Result res;
722b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
723b0104773SPascal Brand 	bool c;
724b0104773SPascal Brand 
725b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
726b0104773SPascal Brand 	if (res != TEE_SUCCESS)
727b0104773SPascal Brand 		return res;
728b0104773SPascal Brand 
729b0104773SPascal Brand 	c = session_is_cancelled(s, NULL);
730b0104773SPascal Brand 
731b0104773SPascal Brand 	return tee_svc_copy_to_user(s, cancel, &c, sizeof(c));
732b0104773SPascal Brand }
733b0104773SPascal Brand 
734b0104773SPascal Brand TEE_Result tee_svc_unmask_cancellation(bool *old_mask)
735b0104773SPascal Brand {
736b0104773SPascal Brand 	TEE_Result res;
737b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
738b0104773SPascal Brand 	bool m;
739b0104773SPascal Brand 
740b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
741b0104773SPascal Brand 	if (res != TEE_SUCCESS)
742b0104773SPascal Brand 		return res;
743b0104773SPascal Brand 
744b0104773SPascal Brand 	m = s->cancel_mask;
745b0104773SPascal Brand 	s->cancel_mask = false;
746b0104773SPascal Brand 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
747b0104773SPascal Brand }
748b0104773SPascal Brand 
749b0104773SPascal Brand TEE_Result tee_svc_mask_cancellation(bool *old_mask)
750b0104773SPascal Brand {
751b0104773SPascal Brand 	TEE_Result res;
752b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
753b0104773SPascal Brand 	bool m;
754b0104773SPascal Brand 
755b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
756b0104773SPascal Brand 	if (res != TEE_SUCCESS)
757b0104773SPascal Brand 		return res;
758b0104773SPascal Brand 
759b0104773SPascal Brand 	m = s->cancel_mask;
760b0104773SPascal Brand 	s->cancel_mask = true;
761b0104773SPascal Brand 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
762b0104773SPascal Brand }
763b0104773SPascal Brand 
764b0104773SPascal Brand TEE_Result tee_svc_wait(uint32_t timeout)
765b0104773SPascal Brand {
766b0104773SPascal Brand 	TEE_Result res = TEE_SUCCESS;
767b0104773SPascal Brand 	uint32_t mytime = 0;
768b0104773SPascal Brand 	struct tee_ta_session *s;
769b0104773SPascal Brand 	TEE_Time base_time;
770b0104773SPascal Brand 	TEE_Time current_time;
771b0104773SPascal Brand 
772b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
773b0104773SPascal Brand 	if (res != TEE_SUCCESS)
774b0104773SPascal Brand 		return res;
775b0104773SPascal Brand 
776b0104773SPascal Brand 	res = tee_time_get_sys_time(&base_time);
777b0104773SPascal Brand 	if (res != TEE_SUCCESS)
778b0104773SPascal Brand 		return res;
779b0104773SPascal Brand 
780b0104773SPascal Brand 	while (true) {
781b0104773SPascal Brand 		res = tee_time_get_sys_time(&current_time);
782b0104773SPascal Brand 		if (res != TEE_SUCCESS)
783b0104773SPascal Brand 			return res;
784b0104773SPascal Brand 
785b0104773SPascal Brand 		if (session_is_cancelled(s, &current_time))
786b0104773SPascal Brand 			return TEE_ERROR_CANCEL;
787b0104773SPascal Brand 
788b0104773SPascal Brand 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
789b0104773SPascal Brand 		    (int)current_time.millis - (int)base_time.millis;
790b0104773SPascal Brand 		if (mytime >= timeout)
791b0104773SPascal Brand 			return TEE_SUCCESS;
792b0104773SPascal Brand 
793d1aea08fSSY Chiu 		tee_time_wait(timeout - mytime);
794b0104773SPascal Brand 	}
795b0104773SPascal Brand 
796b0104773SPascal Brand 	return res;
797b0104773SPascal Brand }
798b0104773SPascal Brand 
799b0104773SPascal Brand TEE_Result tee_svc_get_time(enum utee_time_category cat, TEE_Time *mytime)
800b0104773SPascal Brand {
801b0104773SPascal Brand 	TEE_Result res, res2;
802b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
803b0104773SPascal Brand 	TEE_Time t;
804b0104773SPascal Brand 
805b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
806b0104773SPascal Brand 	if (res != TEE_SUCCESS)
807b0104773SPascal Brand 		return res;
808b0104773SPascal Brand 
809b0104773SPascal Brand 	switch (cat) {
810b0104773SPascal Brand 	case UTEE_TIME_CAT_SYSTEM:
811b0104773SPascal Brand 		res = tee_time_get_sys_time(&t);
812b0104773SPascal Brand 		break;
813b0104773SPascal Brand 	case UTEE_TIME_CAT_TA_PERSISTENT:
814b0104773SPascal Brand 		res =
815b0104773SPascal Brand 		    tee_time_get_ta_time((const void *)&s->ctx->head->uuid, &t);
816b0104773SPascal Brand 		break;
817b0104773SPascal Brand 	case UTEE_TIME_CAT_REE:
818b0104773SPascal Brand 		res = tee_time_get_ree_time(&t);
819b0104773SPascal Brand 		break;
820b0104773SPascal Brand 	default:
821b0104773SPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
822b0104773SPascal Brand 		break;
823b0104773SPascal Brand 	}
824b0104773SPascal Brand 
825b0104773SPascal Brand 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
826b0104773SPascal Brand 		res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t));
827b0104773SPascal Brand 		if (res2 != TEE_SUCCESS)
828b0104773SPascal Brand 			res = res2;
829b0104773SPascal Brand 	}
830b0104773SPascal Brand 
831b0104773SPascal Brand 	return res;
832b0104773SPascal Brand }
833b0104773SPascal Brand 
834b0104773SPascal Brand TEE_Result tee_svc_set_ta_time(const TEE_Time *mytime)
835b0104773SPascal Brand {
836b0104773SPascal Brand 	TEE_Result res;
837b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
838b0104773SPascal Brand 	TEE_Time t;
839b0104773SPascal Brand 
840b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
841b0104773SPascal Brand 	if (res != TEE_SUCCESS)
842b0104773SPascal Brand 		return res;
843b0104773SPascal Brand 
844b0104773SPascal Brand 	res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t));
845b0104773SPascal Brand 	if (res != TEE_SUCCESS)
846b0104773SPascal Brand 		return res;
847b0104773SPascal Brand 
848b0104773SPascal Brand 	return tee_time_set_ta_time((const void *)&s->ctx->head->uuid, &t);
849b0104773SPascal Brand }
850fa530828SPascal Brand 
851fa530828SPascal Brand #ifdef CFG_CACHE_API
852fa530828SPascal Brand TEE_Result tee_svc_cache_operation(void *va, size_t len,
853fa530828SPascal Brand 				   enum utee_cache_operation op)
854fa530828SPascal Brand {
855fa530828SPascal Brand 	TEE_Result res;
856fa530828SPascal Brand 	struct tee_ta_session *s = NULL;
857fa530828SPascal Brand 
858fa530828SPascal Brand 	res = tee_ta_get_current_session(&s);
859fa530828SPascal Brand 	if (res != TEE_SUCCESS)
860fa530828SPascal Brand 		return res;
861fa530828SPascal Brand 
862fa530828SPascal Brand 	if ((s->ctx->flags & TA_FLAG_CACHE_MAINTENANCE) == 0)
863fa530828SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
864fa530828SPascal Brand 
865fa530828SPascal Brand 	return tee_uta_cache_operation(s, op, va, len);
866fa530828SPascal Brand }
867fa530828SPascal Brand #endif
868