xref: /optee_os/core/tee/tee_svc.c (revision 4bf425c1335f8597c747f6f2ebbd4ec7106c1552)
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 <tee_api_types.h>
31b0104773SPascal Brand #include <kernel/tee_ta_manager.h>
32b0104773SPascal Brand #include <utee_types.h>
33b0104773SPascal Brand #include <tee/tee_svc.h>
34ffe04039SJerome Forissier #include <tee/tee_cryp_utl.h>
35b0104773SPascal Brand #include <mm/tee_mmu.h>
36b0104773SPascal Brand #include <mm/tee_mm.h>
37b0104773SPascal Brand #include <kernel/tee_rpc.h>
38b0104773SPascal Brand #include <kernel/tee_rpc_types.h>
39b0104773SPascal Brand #include <kernel/tee_time.h>
40b0104773SPascal Brand 
41b0104773SPascal Brand #include <user_ta_header.h>
424de4bebcSJens Wiklander #include <trace.h>
434de4bebcSJens Wiklander #include <kernel/trace_ta.h>
44b0104773SPascal Brand #include <kernel/chip_services.h>
458684fde8SJens Wiklander #include <kernel/static_ta.h>
46b0104773SPascal Brand 
47e86f1266SJens Wiklander #include <assert.h>
48e86f1266SJens Wiklander 
49e86f1266SJens Wiklander vaddr_t tee_svc_uref_base;
50e86f1266SJens Wiklander 
51453a5030SJerome Forissier void syscall_log(const void *buf __unused, size_t len __unused)
52b0104773SPascal Brand {
537c876f12SPascal Brand #ifdef CFG_TEE_CORE_TA_TRACE
54b0104773SPascal Brand 	char *kbuf;
55b0104773SPascal Brand 
56b0104773SPascal Brand 	if (len == 0)
57b0104773SPascal Brand 		return;
58b0104773SPascal Brand 
59b0104773SPascal Brand 	kbuf = malloc(len);
60b0104773SPascal Brand 	if (kbuf == NULL)
61b0104773SPascal Brand 		return;
62c0346845SJens Wiklander 	*kbuf = '\0';
63b0104773SPascal Brand 
64b0104773SPascal Brand 	/* log as Info/Raw traces */
65b0104773SPascal Brand 	if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS)
664de4bebcSJens Wiklander 		TAMSG_RAW("%.*s", (int)len, kbuf);
67b0104773SPascal Brand 
68b0104773SPascal Brand 	free(kbuf);
697c876f12SPascal Brand #endif
70b0104773SPascal Brand }
71b0104773SPascal Brand 
72453a5030SJerome Forissier TEE_Result syscall_not_supported(void)
73197d17e7SSY Chiu {
74197d17e7SSY Chiu 	return TEE_ERROR_NOT_SUPPORTED;
75197d17e7SSY Chiu }
76197d17e7SSY Chiu 
77453a5030SJerome Forissier uint32_t syscall_dummy(uint32_t *a __unused)
78b0104773SPascal Brand {
79851aa858SJens Wiklander 	DMSG("tee_svc_sys_dummy: a 0x%" PRIxVA, (vaddr_t)a);
80b0104773SPascal Brand 	return 0;
81b0104773SPascal Brand }
82b0104773SPascal Brand 
83e86f1266SJens Wiklander uint32_t syscall_dummy_7args(unsigned long a1 __unused,
84e86f1266SJens Wiklander 			unsigned long a2 __unused, unsigned long a3 __unused,
85e86f1266SJens Wiklander 			unsigned long a4 __unused, unsigned long a5 __unused,
86e86f1266SJens Wiklander 			unsigned long a6 __unused, unsigned long a7 __unused)
87b0104773SPascal Brand {
88e86f1266SJens Wiklander 	DMSG("tee_svc_sys_dummy_7args: 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, %lx, %lx\n",
89b0104773SPascal Brand 	     a1, a2, a3, a4, a5, a6, a7);
90b0104773SPascal Brand 	return 0;
91b0104773SPascal Brand }
92b0104773SPascal Brand 
93453a5030SJerome Forissier uint32_t syscall_nocall(void)
94b0104773SPascal Brand {
95b0104773SPascal Brand 	DMSG("No syscall");
96b0104773SPascal Brand 	return 0x1;
97b0104773SPascal Brand }
98b0104773SPascal Brand 
99ab35d7adSCedric Chaumont /* Configuration properties */
100ab35d7adSCedric Chaumont /* API implementation version */
101ab35d7adSCedric Chaumont static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION);
102ab35d7adSCedric Chaumont 
103ab35d7adSCedric Chaumont /* Implementation description (implementation-dependent) */
104ab35d7adSCedric Chaumont static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR);
105ab35d7adSCedric Chaumont 
106b0104773SPascal Brand /*
107ab35d7adSCedric Chaumont  * TA persistent time protection level
108ab35d7adSCedric Chaumont  * 100: Persistent time based on an REE-controlled real-time clock
109ab35d7adSCedric Chaumont  * and on the TEE Trusted Storage for the storage of origins (default).
110ab35d7adSCedric Chaumont  * 1000: Persistent time based on a TEE-controlled real-time clock
111ab35d7adSCedric Chaumont  * and the TEE Trusted Storage.
112ab35d7adSCedric Chaumont  * The real-time clock MUST be out of reach of software attacks
113ab35d7adSCedric Chaumont  * from the REE.
114ab35d7adSCedric Chaumont  */
115b0104773SPascal Brand static const uint32_t ta_time_prot_lvl = 100;
116ab35d7adSCedric Chaumont 
117ab35d7adSCedric Chaumont /* Elliptic Curve Cryptographic support (false by default) */
118ab35d7adSCedric Chaumont static const bool crypto_ecc_en;
119ab35d7adSCedric Chaumont 
120ab35d7adSCedric Chaumont /*
121ab35d7adSCedric Chaumont  * Trusted storage anti rollback protection level
122ab35d7adSCedric Chaumont  * 0 (or missing): No antirollback protection (default)
123ab35d7adSCedric Chaumont  * 100: Antirollback enforced at REE level
124ab35d7adSCedric Chaumont  * 1000: Antirollback TEE-controlled hardware
125ab35d7adSCedric Chaumont  */
126ab35d7adSCedric Chaumont static const uint32_t ts_antiroll_prot_lvl;
127ab35d7adSCedric Chaumont 
128ab35d7adSCedric Chaumont /* Trusted OS implementation version */
129*4bf425c1SJens Wiklander static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION);
130ab35d7adSCedric Chaumont 
131ab35d7adSCedric Chaumont /* Trusted OS implementation version (binary value) */
132ab35d7adSCedric Chaumont static const uint32_t trustedos_impl_bin_version; /* 0 by default */
133ab35d7adSCedric Chaumont 
134ab35d7adSCedric Chaumont /* Trusted OS implementation manufacturer name */
135ab35d7adSCedric Chaumont static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER);
136ab35d7adSCedric Chaumont 
137ab35d7adSCedric Chaumont /* Trusted firmware version */
138ab35d7adSCedric Chaumont static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION);
139ab35d7adSCedric Chaumont 
140ab35d7adSCedric Chaumont /* Trusted firmware version (binary value) */
141ab35d7adSCedric Chaumont static const uint32_t fw_impl_bin_version; /* 0 by default */
142ab35d7adSCedric Chaumont 
143ab35d7adSCedric Chaumont /* Trusted firmware manufacturer name */
144ab35d7adSCedric Chaumont static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER);
145ab35d7adSCedric Chaumont 
146ab35d7adSCedric Chaumont struct tee_props {
147ab35d7adSCedric Chaumont 	const void *data;
148ab35d7adSCedric Chaumont 	const size_t len;
149ab35d7adSCedric Chaumont };
150ab35d7adSCedric Chaumont 
151ab35d7adSCedric Chaumont /* Consistent with enum utee_property */
152ab35d7adSCedric Chaumont const struct tee_props tee_props_lut[] = {
153ab35d7adSCedric Chaumont 	{api_vers, sizeof(api_vers)},
154ab35d7adSCedric Chaumont 	{descr, sizeof(descr)},
155ab35d7adSCedric Chaumont 	{0, 0}, /* dev_id */
1562cdaaacbSJerome Forissier 	{0, 0}, /* system time protection level */
157ab35d7adSCedric Chaumont 	{&ta_time_prot_lvl, sizeof(ta_time_prot_lvl)},
158ab35d7adSCedric Chaumont 	{&crypto_ecc_en, sizeof(crypto_ecc_en)},
159ab35d7adSCedric Chaumont 	{&ts_antiroll_prot_lvl, sizeof(ts_antiroll_prot_lvl)},
160ab35d7adSCedric Chaumont 	{trustedos_impl_version, sizeof(trustedos_impl_version)},
161ab35d7adSCedric Chaumont 	{&trustedos_impl_bin_version,
162ab35d7adSCedric Chaumont 		sizeof(trustedos_impl_bin_version)},
163ab35d7adSCedric Chaumont 	{trustedos_manufacturer, sizeof(trustedos_manufacturer)},
164ab35d7adSCedric Chaumont 	{fw_impl_version, sizeof(fw_impl_version)},
165ab35d7adSCedric Chaumont 	{&fw_impl_bin_version, sizeof(fw_impl_bin_version)},
166ab35d7adSCedric Chaumont 	{fw_manufacturer, sizeof(fw_manufacturer)},
167ab35d7adSCedric Chaumont 	{0, 0}, /* client_id */
168ab35d7adSCedric Chaumont 	{0, 0}, /* ta_app_id */
169ab35d7adSCedric Chaumont };
170ab35d7adSCedric Chaumont 
171e86f1266SJens Wiklander TEE_Result syscall_get_property(unsigned long prop, void *buf, size_t blen)
172ab35d7adSCedric Chaumont {
173b0104773SPascal Brand 	struct tee_ta_session *sess;
174b0104773SPascal Brand 	TEE_Result res;
175b0104773SPascal Brand 
176ab35d7adSCedric Chaumont 	if (prop > ARRAY_SIZE(tee_props_lut)-1)
177ab35d7adSCedric Chaumont 		return TEE_ERROR_NOT_IMPLEMENTED;
178ab35d7adSCedric Chaumont 
179b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
180b0104773SPascal Brand 	if (res != TEE_SUCCESS)
181b0104773SPascal Brand 		return res;
182b0104773SPascal Brand 
183b0104773SPascal Brand 	switch (prop) {
184b0104773SPascal Brand 	case UTEE_PROP_TEE_DEV_ID:
185b0104773SPascal Brand 		{
186b0104773SPascal Brand 			TEE_UUID uuid;
187ab35d7adSCedric Chaumont 			const size_t nslen = 5;
188ab35d7adSCedric Chaumont 			uint8_t data[5 +
189b0104773SPascal Brand 				     FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
190ab35d7adSCedric Chaumont 			    'O', 'P', 'T', 'E', 'E' };
191b0104773SPascal Brand 
192b0104773SPascal Brand 			if (blen < sizeof(uuid))
193b0104773SPascal Brand 				return TEE_ERROR_SHORT_BUFFER;
194b0104773SPascal Brand 
195b0104773SPascal Brand 			if (tee_otp_get_die_id
196b0104773SPascal Brand 					(data + nslen, sizeof(data) - nslen))
197b0104773SPascal Brand 				return TEE_ERROR_BAD_STATE;
198b0104773SPascal Brand 
199ffe04039SJerome Forissier 			res = tee_hash_createdigest(TEE_ALG_SHA256, data,
200ffe04039SJerome Forissier 						    sizeof(data),
201ffe04039SJerome Forissier 						    (uint8_t *)&uuid,
202ffe04039SJerome Forissier 						    sizeof(uuid));
203b0104773SPascal Brand 			if (res != TEE_SUCCESS)
204b0104773SPascal Brand 				return TEE_ERROR_BAD_STATE;
205b0104773SPascal Brand 
206b0104773SPascal Brand 			/*
207b0104773SPascal Brand 			 * Changes the random value into and UUID as specifiec
208b0104773SPascal Brand 			 * in RFC 4122. The magic values are from the example
209b0104773SPascal Brand 			 * code in the RFC.
210b0104773SPascal Brand 			 *
211b0104773SPascal Brand 			 * TEE_UUID is defined slightly different from the RFC,
212b0104773SPascal Brand 			 * but close enough for our purpose.
213b0104773SPascal Brand 			 */
214b0104773SPascal Brand 
215b0104773SPascal Brand 			uuid.timeHiAndVersion &= 0x0fff;
216b0104773SPascal Brand 			uuid.timeHiAndVersion |= 5 << 12;
217b0104773SPascal Brand 
218b0104773SPascal Brand 			/* uuid.clock_seq_hi_and_reserved in the RFC */
219b0104773SPascal Brand 			uuid.clockSeqAndNode[0] &= 0x3f;
220b0104773SPascal Brand 			uuid.clockSeqAndNode[0] |= 0x80;
221b0104773SPascal Brand 
222e86f1266SJens Wiklander 			return tee_svc_copy_to_user(sess, buf, &uuid,
223b0104773SPascal Brand 						    sizeof(TEE_UUID));
224b0104773SPascal Brand 		}
225b0104773SPascal Brand 
2262cdaaacbSJerome Forissier 	case UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL:
2272cdaaacbSJerome Forissier 		{
2282cdaaacbSJerome Forissier 			uint32_t prot;
2292cdaaacbSJerome Forissier 
2302cdaaacbSJerome Forissier 			if (blen < sizeof(prot))
2312cdaaacbSJerome Forissier 				return TEE_ERROR_SHORT_BUFFER;
2322cdaaacbSJerome Forissier 			prot = tee_time_get_sys_time_protection_level();
2332cdaaacbSJerome Forissier 			return tee_svc_copy_to_user(sess, (void *)buf,
2342cdaaacbSJerome Forissier 						    &prot, sizeof(prot));
2352cdaaacbSJerome Forissier 		}
2362cdaaacbSJerome Forissier 
237b0104773SPascal Brand 	case UTEE_PROP_CLIENT_ID:
238b0104773SPascal Brand 		if (blen < sizeof(TEE_Identity))
239b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
240e86f1266SJens Wiklander 		return tee_svc_copy_to_user(sess, buf, &sess->clnt_id,
241ab35d7adSCedric Chaumont 					    sizeof(TEE_Identity));
24237d6ae92SPascal Brand 
243b0104773SPascal Brand 	case UTEE_PROP_TA_APP_ID:
244b0104773SPascal Brand 		if (blen < sizeof(TEE_UUID))
245b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
246e86f1266SJens Wiklander 		return tee_svc_copy_to_user(sess, buf, &sess->ctx->uuid,
247ab35d7adSCedric Chaumont 					    sizeof(TEE_UUID));
248b0104773SPascal Brand 	default:
249ab35d7adSCedric Chaumont 		if (blen < tee_props_lut[prop].len)
250ab35d7adSCedric Chaumont 			return TEE_ERROR_SHORT_BUFFER;
251e86f1266SJens Wiklander 		return tee_svc_copy_to_user(sess, buf, tee_props_lut[prop].data,
252ab35d7adSCedric Chaumont 					    tee_props_lut[prop].len);
253b0104773SPascal Brand 	}
254b0104773SPascal Brand }
255b0104773SPascal Brand 
256e86f1266SJens Wiklander static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up)
257e86f1266SJens Wiklander {
258e86f1266SJens Wiklander 	size_t n;
259e86f1266SJens Wiklander 	uint32_t types = up->types;
260e86f1266SJens Wiklander 
261e86f1266SJens Wiklander 	p->types = types;
262e86f1266SJens Wiklander 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
263e86f1266SJens Wiklander 		uintptr_t a = up->vals[n * 2];
264e86f1266SJens Wiklander 		size_t b = up->vals[n * 2 + 1];
265e86f1266SJens Wiklander 
266e86f1266SJens Wiklander 		switch (TEE_PARAM_TYPE_GET(types, n)) {
267e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_INPUT:
268e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
269e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_INOUT:
270e86f1266SJens Wiklander 			p->params[n].memref.buffer = (void *)a;
271e86f1266SJens Wiklander 			p->params[n].memref.size = b;
272e86f1266SJens Wiklander 			break;
273e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_VALUE_INPUT:
274e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_VALUE_INOUT:
275e86f1266SJens Wiklander 			p->params[n].value.a = a;
276e86f1266SJens Wiklander 			p->params[n].value.b = b;
277e86f1266SJens Wiklander 			break;
278e86f1266SJens Wiklander 		default:
279e86f1266SJens Wiklander 			p->params[n].value.a = 0;
280e86f1266SJens Wiklander 			p->params[n].value.b = 0;
281e86f1266SJens Wiklander 			break;
282e86f1266SJens Wiklander 		}
283e86f1266SJens Wiklander 	}
284e86f1266SJens Wiklander }
285e86f1266SJens Wiklander 
286b0104773SPascal Brand /*
287b0104773SPascal Brand  * TA invokes some TA with parameter.
288b0104773SPascal Brand  * If some parameters are memory references:
289b0104773SPascal Brand  * - either the memref is inside TA private RAM: TA is not allowed to expose
290b0104773SPascal Brand  *   its private RAM: use a temporary memory buffer and copy the data.
291b0104773SPascal Brand  * - or the memref is not in the TA private RAM:
292b0104773SPascal Brand  *   - if the memref was mapped to the TA, TA is allowed to expose it.
293b0104773SPascal Brand  *   - if so, converts memref virtual address into a physical address.
294b0104773SPascal Brand  */
295b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
296b0104773SPascal Brand 				     struct tee_ta_session *called_sess,
297e86f1266SJens Wiklander 				     struct utee_params *callee_params,
298b0104773SPascal Brand 				     struct tee_ta_param *param,
299b0104773SPascal Brand 				     tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
300b0104773SPascal Brand 				     tee_mm_entry_t **mm)
301b0104773SPascal Brand {
302b0104773SPascal Brand 	size_t n;
303b0104773SPascal Brand 	TEE_Result res;
304b0104773SPascal Brand 	size_t req_mem = 0;
305b0104773SPascal Brand 	size_t s;
306b0104773SPascal Brand 	uint8_t *dst = 0;
307b0104773SPascal Brand 	tee_paddr_t dst_pa, src_pa = 0;
308b0104773SPascal Brand 	bool ta_private_memref[TEE_NUM_PARAMS];
3098684fde8SJens Wiklander 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
310b0104773SPascal Brand 
311b7fc217fSPascal Brand 	/* fill 'param' input struct with caller params description buffer */
312b7fc217fSPascal Brand 	if (!callee_params) {
313e86f1266SJens Wiklander 		memset(param, 0, sizeof(*param));
314b0104773SPascal Brand 	} else {
3158684fde8SJens Wiklander 		res = tee_mmu_check_access_rights(utc,
316177603c7SJens Wiklander 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
317e86f1266SJens Wiklander 			(tee_uaddr_t)callee_params, sizeof(struct utee_params));
318177603c7SJens Wiklander 		if (res != TEE_SUCCESS)
319177603c7SJens Wiklander 			return res;
320e86f1266SJens Wiklander 		utee_param_to_param(param, callee_params);
321b0104773SPascal Brand 	}
322b0104773SPascal Brand 
3238684fde8SJens Wiklander 	if (called_sess && is_static_ta_ctx(called_sess->ctx)) {
324b0104773SPascal Brand 		/*
3258684fde8SJens Wiklander 		 * static TA, borrow the mapping of the calling
326b0104773SPascal Brand 		 * during this call.
327b0104773SPascal Brand 		 */
328b0104773SPascal Brand 		called_sess->calling_sess = sess;
329b0104773SPascal Brand 		return TEE_SUCCESS;
330b0104773SPascal Brand 	}
331b0104773SPascal Brand 
332b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
333b0104773SPascal Brand 
334b0104773SPascal Brand 		ta_private_memref[n] = false;
335b0104773SPascal Brand 
336b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
337b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
338b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
339b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
340b0104773SPascal Brand 			if (param->params[n].memref.buffer == NULL) {
341b0104773SPascal Brand 				if (param->params[n].memref.size != 0)
342b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
343b0104773SPascal Brand 				break;
344b0104773SPascal Brand 			}
345b0104773SPascal Brand 			/* uTA cannot expose its private memory */
3468684fde8SJens Wiklander 			if (tee_mmu_is_vbuf_inside_ta_private(utc,
347106d8aa6SPascal Brand 				    param->params[n].memref.buffer,
348b0104773SPascal Brand 				    param->params[n].memref.size)) {
349b0104773SPascal Brand 
350a17acc4cSSabrina Ni 				s = ROUNDUP(param->params[n].memref.size,
351b0104773SPascal Brand 						sizeof(uint32_t));
352b0104773SPascal Brand 				/* Check overflow */
353b0104773SPascal Brand 				if (req_mem + s < req_mem)
354b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
355b0104773SPascal Brand 				req_mem += s;
356b0104773SPascal Brand 				ta_private_memref[n] = true;
357b0104773SPascal Brand 				break;
358b0104773SPascal Brand 			}
3598684fde8SJens Wiklander 			if (tee_mmu_is_vbuf_intersect_ta_private(utc,
360106d8aa6SPascal Brand 				    param->params[n].memref.buffer,
361b0104773SPascal Brand 				    param->params[n].memref.size))
362b0104773SPascal Brand 				return TEE_ERROR_BAD_PARAMETERS;
363b0104773SPascal Brand 
3648684fde8SJens Wiklander 			if (tee_mmu_user_va2pa(utc,
365b0104773SPascal Brand 					(void *)param->params[n].memref.buffer,
3660e692b78SJens Wiklander 					&src_pa) != TEE_SUCCESS)
367b0104773SPascal Brand 				return TEE_ERROR_BAD_PARAMETERS;
368b0104773SPascal Brand 
369b0104773SPascal Brand 			param->param_attr[n] = tee_mmu_user_get_cache_attr(
3708684fde8SJens Wiklander 				utc, (void *)param->params[n].memref.buffer);
371b0104773SPascal Brand 
372b0104773SPascal Brand 			param->params[n].memref.buffer = (void *)src_pa;
373b0104773SPascal Brand 			break;
374b0104773SPascal Brand 
375b0104773SPascal Brand 		default:
376b0104773SPascal Brand 			break;
377b0104773SPascal Brand 		}
378b0104773SPascal Brand 	}
379b0104773SPascal Brand 
380b0104773SPascal Brand 	if (req_mem == 0)
381b0104773SPascal Brand 		return TEE_SUCCESS;
382b0104773SPascal Brand 
383b0104773SPascal Brand 	/* Allocate section in secure DDR */
384b0104773SPascal Brand 	*mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem);
385b0104773SPascal Brand 	if (*mm == NULL) {
386b0104773SPascal Brand 		DMSG("tee_mm_alloc TEE_ERROR_GENERIC");
387b0104773SPascal Brand 		return TEE_ERROR_GENERIC;
388b0104773SPascal Brand 	}
389b0104773SPascal Brand 
390b0104773SPascal Brand 	/* Get the virtual address for the section in secure DDR */
391b0104773SPascal Brand 	res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst);
392b0104773SPascal Brand 	if (res != TEE_SUCCESS)
393b0104773SPascal Brand 		return res;
394b0104773SPascal Brand 	dst_pa = tee_mm_get_smem(*mm);
395b0104773SPascal Brand 
396b0104773SPascal Brand 	for (n = 0; n < 4; n++) {
397b0104773SPascal Brand 
398b0104773SPascal Brand 		if (ta_private_memref[n] == false)
399b0104773SPascal Brand 			continue;
400b0104773SPascal Brand 
401a17acc4cSSabrina Ni 		s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t));
402b0104773SPascal Brand 
403b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
404b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
405b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
406b0104773SPascal Brand 			if (param->params[n].memref.buffer != NULL) {
407b0104773SPascal Brand 				res = tee_svc_copy_from_user(sess, dst,
408b7fc217fSPascal Brand 						param->params[n].memref.buffer,
409b7fc217fSPascal Brand 						param->params[n].memref.size);
410b0104773SPascal Brand 				if (res != TEE_SUCCESS)
411b0104773SPascal Brand 					return res;
412b0104773SPascal Brand 				param->param_attr[n] =
413b0104773SPascal Brand 					tee_mmu_kmap_get_cache_attr(dst);
414b0104773SPascal Brand 				param->params[n].memref.buffer = (void *)dst_pa;
415b0104773SPascal Brand 				tmp_buf_pa[n] = dst_pa;
416b0104773SPascal Brand 				dst += s;
417b0104773SPascal Brand 				dst_pa += s;
418b0104773SPascal Brand 			}
419b0104773SPascal Brand 			break;
420b0104773SPascal Brand 
421b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
422b0104773SPascal Brand 			if (param->params[n].memref.buffer != NULL) {
423b0104773SPascal Brand 				param->param_attr[n] =
424b0104773SPascal Brand 					tee_mmu_kmap_get_cache_attr(dst);
425b0104773SPascal Brand 				param->params[n].memref.buffer = (void *)dst_pa;
426b0104773SPascal Brand 				tmp_buf_pa[n] = dst_pa;
427b0104773SPascal Brand 				dst += s;
428b0104773SPascal Brand 				dst_pa += s;
429b0104773SPascal Brand 			}
430b0104773SPascal Brand 			break;
431b0104773SPascal Brand 
432b0104773SPascal Brand 		default:
433b0104773SPascal Brand 			continue;
434b0104773SPascal Brand 		}
435b0104773SPascal Brand 	}
436b0104773SPascal Brand 
437b0104773SPascal Brand 	tee_mmu_kunmap(dst, req_mem);
438b0104773SPascal Brand 
439b0104773SPascal Brand 	return TEE_SUCCESS;
440b0104773SPascal Brand }
441b0104773SPascal Brand 
442b0104773SPascal Brand /*
443b0104773SPascal Brand  * Back from execution of service: update parameters passed from TA:
444b0104773SPascal Brand  * If some parameters were memory references:
445b0104773SPascal Brand  * - either the memref was temporary: copy back data and update size
446b0104773SPascal Brand  * - or it was the original TA memref: update only the size value.
447b0104773SPascal Brand  */
448b0104773SPascal Brand static TEE_Result tee_svc_update_out_param(
449b0104773SPascal Brand 		struct tee_ta_session *sess,
450b0104773SPascal Brand 		struct tee_ta_session *called_sess,
451b0104773SPascal Brand 		struct tee_ta_param *param,
452b0104773SPascal Brand 		tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
453e86f1266SJens Wiklander 		struct utee_params *usr_param)
454b0104773SPascal Brand {
455b0104773SPascal Brand 	size_t n;
456e86f1266SJens Wiklander 	void *p;
4578684fde8SJens Wiklander 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
4588684fde8SJens Wiklander 	bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx);
459bc420748SJens Wiklander 
460b0104773SPascal Brand 	tee_ta_set_current_session(sess);
461b0104773SPascal Brand 
462b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
463b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
464b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
465b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
466e86f1266SJens Wiklander 			p = (void *)(uintptr_t)usr_param->vals[n * 2];
467b0104773SPascal Brand 
468b0104773SPascal Brand 			/* outside TA private => memref is valid, update size */
4698684fde8SJens Wiklander 			if (!tee_mmu_is_vbuf_inside_ta_private(utc, p,
470b0104773SPascal Brand 					param->params[n].memref.size)) {
471e86f1266SJens Wiklander 				usr_param->vals[n * 2 + 1] =
472b0104773SPascal Brand 					param->params[n].memref.size;
473b0104773SPascal Brand 				break;
474b0104773SPascal Brand 			}
475b0104773SPascal Brand 
476b0104773SPascal Brand 			/*
477b0104773SPascal Brand 			 * If we called a kernel TA the parameters are in shared
478b0104773SPascal Brand 			 * memory and no copy is needed.
479b0104773SPascal Brand 			 */
480b0104773SPascal Brand 			if (have_private_mem_map &&
481b0104773SPascal Brand 			    param->params[n].memref.size <=
482e86f1266SJens Wiklander 			    usr_param->vals[n * 2 + 1]) {
483b0104773SPascal Brand 				uint8_t *src = 0;
484b0104773SPascal Brand 				TEE_Result res;
485b0104773SPascal Brand 
486b0104773SPascal Brand 				/* FIXME: TA_RAM is already mapped ! */
487b0104773SPascal Brand 				res = tee_mmu_kmap(tmp_buf_pa[n],
488b0104773SPascal Brand 					param->params[n].memref.size, &src);
489b0104773SPascal Brand 				if (res != TEE_SUCCESS)
490b0104773SPascal Brand 					return TEE_ERROR_GENERIC;
491b0104773SPascal Brand 
492e86f1266SJens Wiklander 				res = tee_svc_copy_to_user(sess, p, src,
493e86f1266SJens Wiklander 						 param->params[n].memref.size);
494b0104773SPascal Brand 				if (res != TEE_SUCCESS)
495b0104773SPascal Brand 					return res;
496b0104773SPascal Brand 				tee_mmu_kunmap(src,
497b0104773SPascal Brand 					       param->params[n].memref.size);
498b0104773SPascal Brand 
499b0104773SPascal Brand 			}
500e86f1266SJens Wiklander 			usr_param->vals[n * 2 + 1] =
501e86f1266SJens Wiklander 				param->params[n].memref.size;
502b0104773SPascal Brand 			break;
503b0104773SPascal Brand 
504b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
505b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_INOUT:
506e86f1266SJens Wiklander 			usr_param->vals[n * 2] = param->params[n].value.a;
507e86f1266SJens Wiklander 			usr_param->vals[n * 2 + 1] = param->params[n].value.b;
508b0104773SPascal Brand 			break;
509b0104773SPascal Brand 
510b0104773SPascal Brand 		default:
511b0104773SPascal Brand 			continue;
512b0104773SPascal Brand 		}
513b0104773SPascal Brand 	}
514b0104773SPascal Brand 
515b0104773SPascal Brand 	return TEE_SUCCESS;
516b0104773SPascal Brand }
517b0104773SPascal Brand 
518b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */
519453a5030SJerome Forissier TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
520e86f1266SJens Wiklander 			unsigned long cancel_req_to,
521e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ta_sess,
522b0104773SPascal Brand 			uint32_t *ret_orig)
523b0104773SPascal Brand {
524b0104773SPascal Brand 	TEE_Result res;
525b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
526b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
527b0104773SPascal Brand 	struct tee_ta_session *sess;
528b0104773SPascal Brand 	tee_mm_entry_t *mm_param = NULL;
529b0104773SPascal Brand 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
530b0104773SPascal Brand 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
531b0104773SPascal Brand 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
532b0104773SPascal Brand 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
5338684fde8SJens Wiklander 	struct user_ta_ctx *utc;
534b0104773SPascal Brand 
535b0104773SPascal Brand 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
536b0104773SPascal Brand 		res = TEE_ERROR_OUT_OF_MEMORY;
537b0104773SPascal Brand 		goto out_free_only;
538b0104773SPascal Brand 	}
539b0104773SPascal Brand 
540b0104773SPascal Brand 	memset(param, 0, sizeof(struct tee_ta_param));
541b0104773SPascal Brand 
542b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
543b0104773SPascal Brand 	if (res != TEE_SUCCESS)
544b0104773SPascal Brand 		goto out_free_only;
5458684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
546b0104773SPascal Brand 
547b0104773SPascal Brand 	res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID));
548b0104773SPascal Brand 	if (res != TEE_SUCCESS)
549b0104773SPascal Brand 		goto function_exit;
550b0104773SPascal Brand 
551b0104773SPascal Brand 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
552bc420748SJens Wiklander 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
553b0104773SPascal Brand 
554e86f1266SJens Wiklander 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_pa,
555e86f1266SJens Wiklander 				 &mm_param);
556b0104773SPascal Brand 	if (res != TEE_SUCCESS)
557b0104773SPascal Brand 		goto function_exit;
558b0104773SPascal Brand 
559b0104773SPascal Brand 	/*
560b0104773SPascal Brand 	 * Find session of a multi session TA or a static TA
561b0104773SPascal Brand 	 * In such a case, there is no need to ask the supplicant for the TA
562b0104773SPascal Brand 	 * code
563b0104773SPascal Brand 	 */
5648684fde8SJens Wiklander 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
56527cbcc57SJens Wiklander 				  clnt_id, cancel_req_to, param);
566c0346845SJens Wiklander 	if (res != TEE_SUCCESS)
567b0104773SPascal Brand 		goto function_exit;
568b0104773SPascal Brand 
5698684fde8SJens Wiklander 	res = tee_svc_update_out_param(sess, s, param, tmp_buf_pa, usr_param);
570b0104773SPascal Brand 
571b0104773SPascal Brand function_exit:
572b0104773SPascal Brand 	tee_ta_set_current_session(sess);
5730dcea1a3SJens Wiklander 	sess->calling_sess = NULL; /* clear eventual borrowed mapping */
574b0104773SPascal Brand 
575b0104773SPascal Brand 	if (mm_param != NULL) {
576b0104773SPascal Brand 		TEE_Result res2;
577b0104773SPascal Brand 		void *va = 0;
578b0104773SPascal Brand 
579b0104773SPascal Brand 		res2 =
580b0104773SPascal Brand 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
581b0104773SPascal Brand 		if (res2 == TEE_SUCCESS)
582b0104773SPascal Brand 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
583b0104773SPascal Brand 	}
584b0104773SPascal Brand 	tee_mm_free(mm_param);
5852dcb3d36SJerome Forissier 	if (res == TEE_SUCCESS)
586e86f1266SJens Wiklander 		tee_svc_copy_kaddr_to_uref(sess, ta_sess, s);
587b0104773SPascal Brand 	tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
588b0104773SPascal Brand 
589b0104773SPascal Brand out_free_only:
590b0104773SPascal Brand 	free(param);
591b0104773SPascal Brand 	free(uuid);
592b0104773SPascal Brand 	free(clnt_id);
593b0104773SPascal Brand 	return res;
594b0104773SPascal Brand }
595b0104773SPascal Brand 
596e86f1266SJens Wiklander TEE_Result syscall_close_ta_session(unsigned long ta_sess)
597b0104773SPascal Brand {
598b0104773SPascal Brand 	TEE_Result res;
599b0104773SPascal Brand 	struct tee_ta_session *sess;
60060699957SPascal Brand 	TEE_Identity clnt_id;
601e86f1266SJens Wiklander 	struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess);
6028684fde8SJens Wiklander 	struct user_ta_ctx *utc;
603b0104773SPascal Brand 
604b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
605b0104773SPascal Brand 	if (res != TEE_SUCCESS)
606b0104773SPascal Brand 		return res;
6078684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
608b0104773SPascal Brand 
60960699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
610bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
611b0104773SPascal Brand 
61260699957SPascal Brand 	tee_ta_set_current_session(NULL);
6138684fde8SJens Wiklander 	res = tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
614b0104773SPascal Brand 	tee_ta_set_current_session(sess);
615b0104773SPascal Brand 	return res;
616b0104773SPascal Brand }
617b0104773SPascal Brand 
618e86f1266SJens Wiklander TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
619e86f1266SJens Wiklander 			unsigned long cancel_req_to, unsigned long cmd_id,
620e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ret_orig)
621b0104773SPascal Brand {
622b0104773SPascal Brand 	TEE_Result res;
623b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
624b0104773SPascal Brand 	struct tee_ta_param param = { 0 };
62560699957SPascal Brand 	TEE_Identity clnt_id;
626b0104773SPascal Brand 	struct tee_ta_session *sess;
627b666b6f2SJens Wiklander 	struct tee_ta_session *called_sess;
628b0104773SPascal Brand 	tee_mm_entry_t *mm_param = NULL;
629b0104773SPascal Brand 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
6308684fde8SJens Wiklander 	struct user_ta_ctx *utc;
631b0104773SPascal Brand 
632b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
633b0104773SPascal Brand 	if (res != TEE_SUCCESS)
634b0104773SPascal Brand 		return res;
6358684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
636b0104773SPascal Brand 
637e86f1266SJens Wiklander 	called_sess = tee_ta_get_session(
638e86f1266SJens Wiklander 				(vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true,
6398684fde8SJens Wiklander 				&utc->open_sessions);
640b666b6f2SJens Wiklander 	if (!called_sess)
641b666b6f2SJens Wiklander 		return TEE_ERROR_BAD_PARAMETERS;
642b0104773SPascal Brand 
64360699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
644bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
64560699957SPascal Brand 
646e86f1266SJens Wiklander 	res = tee_svc_copy_param(sess, called_sess, usr_param, &param,
647e86f1266SJens Wiklander 				 tmp_buf_pa, &mm_param);
648b0104773SPascal Brand 	if (res != TEE_SUCCESS)
649b0104773SPascal Brand 		goto function_exit;
650b0104773SPascal Brand 
65160699957SPascal Brand 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
65260699957SPascal Brand 				    cancel_req_to, cmd_id, &param);
65360699957SPascal Brand 
654b0104773SPascal Brand 	if (res != TEE_SUCCESS)
655b0104773SPascal Brand 		goto function_exit;
656b0104773SPascal Brand 
657b0104773SPascal Brand 	res = tee_svc_update_out_param(sess, called_sess, &param, tmp_buf_pa,
658177603c7SJens Wiklander 				       usr_param);
659b0104773SPascal Brand 	if (res != TEE_SUCCESS)
660b0104773SPascal Brand 		goto function_exit;
661b0104773SPascal Brand 
662b0104773SPascal Brand function_exit:
663b0104773SPascal Brand 	tee_ta_set_current_session(sess);
664b0104773SPascal Brand 	called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */
665b666b6f2SJens Wiklander 	tee_ta_put_session(called_sess);
666b0104773SPascal Brand 
667b0104773SPascal Brand 	if (mm_param != NULL) {
668b0104773SPascal Brand 		TEE_Result res2;
669b0104773SPascal Brand 		void *va = 0;
670b0104773SPascal Brand 
671b0104773SPascal Brand 		res2 =
672b0104773SPascal Brand 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
673b0104773SPascal Brand 		if (res2 == TEE_SUCCESS)
674b0104773SPascal Brand 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
675b0104773SPascal Brand 	}
676b0104773SPascal Brand 	tee_mm_free(mm_param);
677b0104773SPascal Brand 	if (ret_orig)
678b0104773SPascal Brand 		tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
679b0104773SPascal Brand 	return res;
680b0104773SPascal Brand }
681b0104773SPascal Brand 
682e86f1266SJens Wiklander TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
683b0104773SPascal Brand 				       size_t len)
684b0104773SPascal Brand {
685b0104773SPascal Brand 	TEE_Result res;
686b0104773SPascal Brand 	struct tee_ta_session *s;
687b0104773SPascal Brand 
688b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
689b0104773SPascal Brand 	if (res != TEE_SUCCESS)
690b0104773SPascal Brand 		return res;
691b0104773SPascal Brand 
6928684fde8SJens Wiklander 	return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags,
6938684fde8SJens Wiklander 					   (tee_uaddr_t)buf, len);
694b0104773SPascal Brand }
695b0104773SPascal Brand 
696b0104773SPascal Brand TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr,
697b0104773SPascal Brand 				  const void *uaddr, size_t len)
698b0104773SPascal Brand {
699b0104773SPascal Brand 	TEE_Result res;
700b0104773SPascal Brand 	struct tee_ta_session *s;
701b0104773SPascal Brand 
702b0104773SPascal Brand 	if (sess == NULL) {
703b0104773SPascal Brand 		res = tee_ta_get_current_session(&s);
704b0104773SPascal Brand 		if (res != TEE_SUCCESS)
705b0104773SPascal Brand 			return res;
706b0104773SPascal Brand 	} else {
707b0104773SPascal Brand 		s = sess;
708b0104773SPascal Brand 		tee_ta_set_current_session(s);
709b0104773SPascal Brand 	}
7108684fde8SJens Wiklander 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
711b0104773SPascal Brand 					TEE_MEMORY_ACCESS_READ |
712b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
713b0104773SPascal Brand 					(tee_uaddr_t)uaddr, len);
714b0104773SPascal Brand 	if (res != TEE_SUCCESS)
715b0104773SPascal Brand 		return res;
716b0104773SPascal Brand 
717b0104773SPascal Brand 	memcpy(kaddr, uaddr, len);
718b0104773SPascal Brand 	return TEE_SUCCESS;
719b0104773SPascal Brand }
720b0104773SPascal Brand 
721b0104773SPascal Brand TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr,
722b0104773SPascal Brand 				const void *kaddr, size_t len)
723b0104773SPascal Brand {
724b0104773SPascal Brand 	TEE_Result res;
725b0104773SPascal Brand 	struct tee_ta_session *s;
726b0104773SPascal Brand 
727b0104773SPascal Brand 	if (sess == NULL) {
728b0104773SPascal Brand 		res = tee_ta_get_current_session(&s);
729b0104773SPascal Brand 		if (res != TEE_SUCCESS)
730b0104773SPascal Brand 			return res;
731b0104773SPascal Brand 	} else {
732b0104773SPascal Brand 		s = sess;
733b0104773SPascal Brand 		tee_ta_set_current_session(s);
734b0104773SPascal Brand 	}
735b0104773SPascal Brand 
7368684fde8SJens Wiklander 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
737b0104773SPascal Brand 					TEE_MEMORY_ACCESS_WRITE |
738b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
739b0104773SPascal Brand 					(tee_uaddr_t)uaddr, len);
740b0104773SPascal Brand 	if (res != TEE_SUCCESS)
741b0104773SPascal Brand 		return res;
742b0104773SPascal Brand 
743b0104773SPascal Brand 	memcpy(uaddr, kaddr, len);
744b0104773SPascal Brand 	return TEE_SUCCESS;
745b0104773SPascal Brand }
746b0104773SPascal Brand 
747e86f1266SJens Wiklander TEE_Result tee_svc_copy_kaddr_to_uref(struct tee_ta_session *sess,
748e86f1266SJens Wiklander 			uint32_t *uref, void *kaddr)
7498707ec0fSJerome Forissier {
750e86f1266SJens Wiklander 	uint32_t ref = tee_svc_kaddr_to_uref(kaddr);
7518707ec0fSJerome Forissier 
752e86f1266SJens Wiklander 	return tee_svc_copy_to_user(sess, uref, &ref, sizeof(ref));
7538707ec0fSJerome Forissier }
7548707ec0fSJerome Forissier 
755b0104773SPascal Brand static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time)
756b0104773SPascal Brand {
757b0104773SPascal Brand 	TEE_Time current_time;
758b0104773SPascal Brand 
759b0104773SPascal Brand 	if (s->cancel_mask)
760b0104773SPascal Brand 		return false;
761b0104773SPascal Brand 
762b0104773SPascal Brand 	if (s->cancel)
763b0104773SPascal Brand 		return true;
764b0104773SPascal Brand 
765b0104773SPascal Brand 	if (s->cancel_time.seconds == UINT32_MAX)
766b0104773SPascal Brand 		return false;
767b0104773SPascal Brand 
768b0104773SPascal Brand 	if (curr_time != NULL)
769b0104773SPascal Brand 		current_time = *curr_time;
770b0104773SPascal Brand 	else if (tee_time_get_sys_time(&current_time) != TEE_SUCCESS)
771b0104773SPascal Brand 		return false;
772b0104773SPascal Brand 
773b0104773SPascal Brand 	if (current_time.seconds > s->cancel_time.seconds ||
774b0104773SPascal Brand 	    (current_time.seconds == s->cancel_time.seconds &&
775b0104773SPascal Brand 	     current_time.millis >= s->cancel_time.millis)) {
776b0104773SPascal Brand 		return true;
777b0104773SPascal Brand 	}
778b0104773SPascal Brand 
779b0104773SPascal Brand 	return false;
780b0104773SPascal Brand }
781b0104773SPascal Brand 
782e86f1266SJens Wiklander TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
783b0104773SPascal Brand {
784b0104773SPascal Brand 	TEE_Result res;
785b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
786e86f1266SJens Wiklander 	uint32_t c;
787b0104773SPascal Brand 
788b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
789b0104773SPascal Brand 	if (res != TEE_SUCCESS)
790b0104773SPascal Brand 		return res;
791b0104773SPascal Brand 
792b0104773SPascal Brand 	c = session_is_cancelled(s, NULL);
793b0104773SPascal Brand 
794b0104773SPascal Brand 	return tee_svc_copy_to_user(s, cancel, &c, sizeof(c));
795b0104773SPascal Brand }
796b0104773SPascal Brand 
797e86f1266SJens Wiklander TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
798b0104773SPascal Brand {
799b0104773SPascal Brand 	TEE_Result res;
800b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
801e86f1266SJens Wiklander 	uint32_t m;
802b0104773SPascal Brand 
803b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
804b0104773SPascal Brand 	if (res != TEE_SUCCESS)
805b0104773SPascal Brand 		return res;
806b0104773SPascal Brand 
807b0104773SPascal Brand 	m = s->cancel_mask;
808b0104773SPascal Brand 	s->cancel_mask = false;
809b0104773SPascal Brand 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
810b0104773SPascal Brand }
811b0104773SPascal Brand 
812e86f1266SJens Wiklander TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
813b0104773SPascal Brand {
814b0104773SPascal Brand 	TEE_Result res;
815b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
816e86f1266SJens Wiklander 	uint32_t m;
817b0104773SPascal Brand 
818b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
819b0104773SPascal Brand 	if (res != TEE_SUCCESS)
820b0104773SPascal Brand 		return res;
821b0104773SPascal Brand 
822b0104773SPascal Brand 	m = s->cancel_mask;
823b0104773SPascal Brand 	s->cancel_mask = true;
824b0104773SPascal Brand 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
825b0104773SPascal Brand }
826b0104773SPascal Brand 
827e86f1266SJens Wiklander TEE_Result syscall_wait(unsigned long timeout)
828b0104773SPascal Brand {
829b0104773SPascal Brand 	TEE_Result res = TEE_SUCCESS;
830b0104773SPascal Brand 	uint32_t mytime = 0;
831b0104773SPascal Brand 	struct tee_ta_session *s;
832b0104773SPascal Brand 	TEE_Time base_time;
833b0104773SPascal Brand 	TEE_Time current_time;
834b0104773SPascal Brand 
835b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
836b0104773SPascal Brand 	if (res != TEE_SUCCESS)
837b0104773SPascal Brand 		return res;
838b0104773SPascal Brand 
839b0104773SPascal Brand 	res = tee_time_get_sys_time(&base_time);
840b0104773SPascal Brand 	if (res != TEE_SUCCESS)
841b0104773SPascal Brand 		return res;
842b0104773SPascal Brand 
843b0104773SPascal Brand 	while (true) {
844b0104773SPascal Brand 		res = tee_time_get_sys_time(&current_time);
845b0104773SPascal Brand 		if (res != TEE_SUCCESS)
846b0104773SPascal Brand 			return res;
847b0104773SPascal Brand 
848b0104773SPascal Brand 		if (session_is_cancelled(s, &current_time))
849b0104773SPascal Brand 			return TEE_ERROR_CANCEL;
850b0104773SPascal Brand 
851b0104773SPascal Brand 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
852b0104773SPascal Brand 		    (int)current_time.millis - (int)base_time.millis;
853b0104773SPascal Brand 		if (mytime >= timeout)
854b0104773SPascal Brand 			return TEE_SUCCESS;
855b0104773SPascal Brand 
856d1aea08fSSY Chiu 		tee_time_wait(timeout - mytime);
857b0104773SPascal Brand 	}
858b0104773SPascal Brand 
859b0104773SPascal Brand 	return res;
860b0104773SPascal Brand }
861b0104773SPascal Brand 
862e86f1266SJens Wiklander TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
863b0104773SPascal Brand {
864b0104773SPascal Brand 	TEE_Result res, res2;
865b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
866b0104773SPascal Brand 	TEE_Time t;
867b0104773SPascal Brand 
868b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
869b0104773SPascal Brand 	if (res != TEE_SUCCESS)
870b0104773SPascal Brand 		return res;
871b0104773SPascal Brand 
872b0104773SPascal Brand 	switch (cat) {
873b0104773SPascal Brand 	case UTEE_TIME_CAT_SYSTEM:
874b0104773SPascal Brand 		res = tee_time_get_sys_time(&t);
875b0104773SPascal Brand 		break;
876b0104773SPascal Brand 	case UTEE_TIME_CAT_TA_PERSISTENT:
877bc420748SJens Wiklander 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
878b0104773SPascal Brand 		break;
879b0104773SPascal Brand 	case UTEE_TIME_CAT_REE:
880b0104773SPascal Brand 		res = tee_time_get_ree_time(&t);
881b0104773SPascal Brand 		break;
882b0104773SPascal Brand 	default:
883b0104773SPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
884b0104773SPascal Brand 		break;
885b0104773SPascal Brand 	}
886b0104773SPascal Brand 
887b0104773SPascal Brand 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
888b0104773SPascal Brand 		res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t));
889b0104773SPascal Brand 		if (res2 != TEE_SUCCESS)
890b0104773SPascal Brand 			res = res2;
891b0104773SPascal Brand 	}
892b0104773SPascal Brand 
893b0104773SPascal Brand 	return res;
894b0104773SPascal Brand }
895b0104773SPascal Brand 
896453a5030SJerome Forissier TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
897b0104773SPascal Brand {
898b0104773SPascal Brand 	TEE_Result res;
899b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
900b0104773SPascal Brand 	TEE_Time t;
901b0104773SPascal Brand 
902b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
903b0104773SPascal Brand 	if (res != TEE_SUCCESS)
904b0104773SPascal Brand 		return res;
905b0104773SPascal Brand 
906b0104773SPascal Brand 	res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t));
907b0104773SPascal Brand 	if (res != TEE_SUCCESS)
908b0104773SPascal Brand 		return res;
909b0104773SPascal Brand 
910bc420748SJens Wiklander 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
911b0104773SPascal Brand }
912fa530828SPascal Brand 
913fa530828SPascal Brand #ifdef CFG_CACHE_API
914e86f1266SJens Wiklander TEE_Result syscall_cache_operation(void *va, size_t len, unsigned long op)
915fa530828SPascal Brand {
916fa530828SPascal Brand 	TEE_Result res;
917fa530828SPascal Brand 	struct tee_ta_session *s = NULL;
918fa530828SPascal Brand 
919fa530828SPascal Brand 	res = tee_ta_get_current_session(&s);
920fa530828SPascal Brand 	if (res != TEE_SUCCESS)
921fa530828SPascal Brand 		return res;
922fa530828SPascal Brand 
923fa530828SPascal Brand 	if ((s->ctx->flags & TA_FLAG_CACHE_MAINTENANCE) == 0)
924fa530828SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
925fa530828SPascal Brand 
926fa530828SPascal Brand 	return tee_uta_cache_operation(s, op, va, len);
927fa530828SPascal Brand }
928fa530828SPascal Brand #endif
929