xref: /optee_os/core/tee/tee_svc.c (revision 453a503090e315c0927146614797cf8aea49427c)
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>
36177603c7SJens 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 
488a1e7b89SJerome Forissier #if (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 
58*453a5030SJerome Forissier void syscall_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 
79*453a5030SJerome Forissier TEE_Result syscall_reserved(void)
80b7fc217fSPascal Brand {
81b7fc217fSPascal Brand 	return TEE_ERROR_GENERIC;
82b7fc217fSPascal Brand }
83b7fc217fSPascal Brand 
84*453a5030SJerome Forissier TEE_Result syscall_not_supported(void)
85197d17e7SSY Chiu {
86197d17e7SSY Chiu 	return TEE_ERROR_NOT_SUPPORTED;
87197d17e7SSY Chiu }
88197d17e7SSY Chiu 
89*453a5030SJerome Forissier uint32_t syscall_dummy(uint32_t *a __unused)
90b0104773SPascal Brand {
91851aa858SJens Wiklander 	DMSG("tee_svc_sys_dummy: a 0x%" PRIxVA, (vaddr_t)a);
92b0104773SPascal Brand 	return 0;
93b0104773SPascal Brand }
94b0104773SPascal Brand 
95*453a5030SJerome Forissier uint32_t syscall_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 
105*453a5030SJerome Forissier uint32_t syscall_nocall(void)
106b0104773SPascal Brand {
107b0104773SPascal Brand 	DMSG("No syscall");
108b0104773SPascal Brand 	return 0x1;
109b0104773SPascal Brand }
110b0104773SPascal Brand 
111ab35d7adSCedric Chaumont /* Configuration properties */
112ab35d7adSCedric Chaumont /* API implementation version */
113ab35d7adSCedric Chaumont static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION);
114ab35d7adSCedric Chaumont 
115ab35d7adSCedric Chaumont /* Implementation description (implementation-dependent) */
116ab35d7adSCedric Chaumont static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR);
117ab35d7adSCedric Chaumont 
118b0104773SPascal Brand /*
119ab35d7adSCedric Chaumont  * System time protection level
120ab35d7adSCedric Chaumont  * 100: System time based on REE-controlled timers (default).
121ab35d7adSCedric Chaumont  * Can be tampered by the REE
122ab35d7adSCedric Chaumont  * The implementation must still guarantee that the system time
123b0104773SPascal Brand  * is monotonous, i.e., successive calls to TEE_GetSystemTime must
124b0104773SPascal Brand  * return increasing values of the system time.
125ab35d7adSCedric Chaumont  * 1000: System time based on a TEE-controlled secure timer.
126ab35d7adSCedric Chaumont  * The REE cannot interfere with the system time. It may still
127ab35d7adSCedric Chaumont  * interfere with the scheduling of TEE tasks, but is not able to
128ab35d7adSCedric Chaumont  * hide delays from a TA calling TEE_GetSystemTime.
129b0104773SPascal Brand  */
130b0104773SPascal Brand static const uint32_t sys_time_prot_lvl = 100;
131ab35d7adSCedric Chaumont 
132ab35d7adSCedric Chaumont /*
133ab35d7adSCedric Chaumont  * TA persistent time protection level
134ab35d7adSCedric Chaumont  * 100: Persistent time based on an REE-controlled real-time clock
135ab35d7adSCedric Chaumont  * and on the TEE Trusted Storage for the storage of origins (default).
136ab35d7adSCedric Chaumont  * 1000: Persistent time based on a TEE-controlled real-time clock
137ab35d7adSCedric Chaumont  * and the TEE Trusted Storage.
138ab35d7adSCedric Chaumont  * The real-time clock MUST be out of reach of software attacks
139ab35d7adSCedric Chaumont  * from the REE.
140ab35d7adSCedric Chaumont  */
141b0104773SPascal Brand static const uint32_t ta_time_prot_lvl = 100;
142ab35d7adSCedric Chaumont 
143ab35d7adSCedric Chaumont /* Elliptic Curve Cryptographic support (false by default) */
144ab35d7adSCedric Chaumont static const bool crypto_ecc_en;
145ab35d7adSCedric Chaumont 
146ab35d7adSCedric Chaumont /*
147ab35d7adSCedric Chaumont  * Trusted storage anti rollback protection level
148ab35d7adSCedric Chaumont  * 0 (or missing): No antirollback protection (default)
149ab35d7adSCedric Chaumont  * 100: Antirollback enforced at REE level
150ab35d7adSCedric Chaumont  * 1000: Antirollback TEE-controlled hardware
151ab35d7adSCedric Chaumont  */
152ab35d7adSCedric Chaumont static const uint32_t ts_antiroll_prot_lvl;
153ab35d7adSCedric Chaumont 
154ab35d7adSCedric Chaumont /* Trusted OS implementation version */
155ab35d7adSCedric Chaumont static const char trustedos_impl_version[] = TO_STR(CFG_TEE_IMPL_VERSION);
156ab35d7adSCedric Chaumont 
157ab35d7adSCedric Chaumont /* Trusted OS implementation version (binary value) */
158ab35d7adSCedric Chaumont static const uint32_t trustedos_impl_bin_version; /* 0 by default */
159ab35d7adSCedric Chaumont 
160ab35d7adSCedric Chaumont /* Trusted OS implementation manufacturer name */
161ab35d7adSCedric Chaumont static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER);
162ab35d7adSCedric Chaumont 
163ab35d7adSCedric Chaumont /* Trusted firmware version */
164ab35d7adSCedric Chaumont static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION);
165ab35d7adSCedric Chaumont 
166ab35d7adSCedric Chaumont /* Trusted firmware version (binary value) */
167ab35d7adSCedric Chaumont static const uint32_t fw_impl_bin_version; /* 0 by default */
168ab35d7adSCedric Chaumont 
169ab35d7adSCedric Chaumont /* Trusted firmware manufacturer name */
170ab35d7adSCedric Chaumont static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER);
171ab35d7adSCedric Chaumont 
172ab35d7adSCedric Chaumont struct tee_props {
173ab35d7adSCedric Chaumont 	const void *data;
174ab35d7adSCedric Chaumont 	const size_t len;
175ab35d7adSCedric Chaumont };
176ab35d7adSCedric Chaumont 
177ab35d7adSCedric Chaumont /* Consistent with enum utee_property */
178ab35d7adSCedric Chaumont const struct tee_props tee_props_lut[] = {
179ab35d7adSCedric Chaumont 	{api_vers, sizeof(api_vers)},
180ab35d7adSCedric Chaumont 	{descr, sizeof(descr)},
181ab35d7adSCedric Chaumont 	{0, 0}, /* dev_id */
182ab35d7adSCedric Chaumont 	{&sys_time_prot_lvl, sizeof(sys_time_prot_lvl)},
183ab35d7adSCedric Chaumont 	{&ta_time_prot_lvl, sizeof(ta_time_prot_lvl)},
184ab35d7adSCedric Chaumont 	{&crypto_ecc_en, sizeof(crypto_ecc_en)},
185ab35d7adSCedric Chaumont 	{&ts_antiroll_prot_lvl, sizeof(ts_antiroll_prot_lvl)},
186ab35d7adSCedric Chaumont 	{trustedos_impl_version, sizeof(trustedos_impl_version)},
187ab35d7adSCedric Chaumont 	{&trustedos_impl_bin_version,
188ab35d7adSCedric Chaumont 		sizeof(trustedos_impl_bin_version)},
189ab35d7adSCedric Chaumont 	{trustedos_manufacturer, sizeof(trustedos_manufacturer)},
190ab35d7adSCedric Chaumont 	{fw_impl_version, sizeof(fw_impl_version)},
191ab35d7adSCedric Chaumont 	{&fw_impl_bin_version, sizeof(fw_impl_bin_version)},
192ab35d7adSCedric Chaumont 	{fw_manufacturer, sizeof(fw_manufacturer)},
193ab35d7adSCedric Chaumont 	{0, 0}, /* client_id */
194ab35d7adSCedric Chaumont 	{0, 0}, /* ta_app_id */
195ab35d7adSCedric Chaumont };
196ab35d7adSCedric Chaumont 
197*453a5030SJerome Forissier TEE_Result syscall_get_property(uint32_t prop, tee_uaddr_t buf, size_t blen)
198ab35d7adSCedric Chaumont {
199b0104773SPascal Brand 	struct tee_ta_session *sess;
200b0104773SPascal Brand 	TEE_Result res;
201b0104773SPascal Brand 
202ab35d7adSCedric Chaumont 	if (prop > ARRAY_SIZE(tee_props_lut)-1)
203ab35d7adSCedric Chaumont 		return TEE_ERROR_NOT_IMPLEMENTED;
204ab35d7adSCedric Chaumont 
205b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
206b0104773SPascal Brand 	if (res != TEE_SUCCESS)
207b0104773SPascal Brand 		return res;
208b0104773SPascal Brand 
209b0104773SPascal Brand 	switch (prop) {
210b0104773SPascal Brand 	case UTEE_PROP_TEE_DEV_ID:
211b0104773SPascal Brand 		{
212b0104773SPascal Brand 			TEE_UUID uuid;
213ab35d7adSCedric Chaumont 			const size_t nslen = 5;
214ab35d7adSCedric Chaumont 			uint8_t data[5 +
215b0104773SPascal Brand 				     FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
216ab35d7adSCedric Chaumont 			    'O', 'P', 'T', 'E', 'E' };
217b0104773SPascal Brand 
218b0104773SPascal Brand 			if (blen < sizeof(uuid))
219b0104773SPascal Brand 				return TEE_ERROR_SHORT_BUFFER;
220b0104773SPascal Brand 
221b0104773SPascal Brand 			if (tee_otp_get_die_id
222b0104773SPascal Brand 					(data + nslen, sizeof(data) - nslen))
223b0104773SPascal Brand 				return TEE_ERROR_BAD_STATE;
224b0104773SPascal Brand 
225ffe04039SJerome Forissier 			res = tee_hash_createdigest(TEE_ALG_SHA256, data,
226ffe04039SJerome Forissier 						    sizeof(data),
227ffe04039SJerome Forissier 						    (uint8_t *)&uuid,
228ffe04039SJerome Forissier 						    sizeof(uuid));
229b0104773SPascal Brand 			if (res != TEE_SUCCESS)
230b0104773SPascal Brand 				return TEE_ERROR_BAD_STATE;
231b0104773SPascal Brand 
232b0104773SPascal Brand 			/*
233b0104773SPascal Brand 			 * Changes the random value into and UUID as specifiec
234b0104773SPascal Brand 			 * in RFC 4122. The magic values are from the example
235b0104773SPascal Brand 			 * code in the RFC.
236b0104773SPascal Brand 			 *
237b0104773SPascal Brand 			 * TEE_UUID is defined slightly different from the RFC,
238b0104773SPascal Brand 			 * but close enough for our purpose.
239b0104773SPascal Brand 			 */
240b0104773SPascal Brand 
241b0104773SPascal Brand 			uuid.timeHiAndVersion &= 0x0fff;
242b0104773SPascal Brand 			uuid.timeHiAndVersion |= 5 << 12;
243b0104773SPascal Brand 
244b0104773SPascal Brand 			/* uuid.clock_seq_hi_and_reserved in the RFC */
245b0104773SPascal Brand 			uuid.clockSeqAndNode[0] &= 0x3f;
246b0104773SPascal Brand 			uuid.clockSeqAndNode[0] |= 0x80;
247b0104773SPascal Brand 
248b0104773SPascal Brand 			return tee_svc_copy_to_user(sess, (void *)buf, &uuid,
249b0104773SPascal Brand 						    sizeof(TEE_UUID));
250b0104773SPascal Brand 		}
251b0104773SPascal Brand 
252b0104773SPascal Brand 	case UTEE_PROP_CLIENT_ID:
253b0104773SPascal Brand 		if (blen < sizeof(TEE_Identity))
254b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
255b0104773SPascal Brand 		return tee_svc_copy_to_user(sess, (void *)buf,
256ab35d7adSCedric Chaumont 					    &sess->clnt_id,
257ab35d7adSCedric Chaumont 					    sizeof(TEE_Identity));
25837d6ae92SPascal Brand 
259b0104773SPascal Brand 	case UTEE_PROP_TA_APP_ID:
260b0104773SPascal Brand 		if (blen < sizeof(TEE_UUID))
261b0104773SPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
262b0104773SPascal Brand 		return tee_svc_copy_to_user(sess, (void *)buf,
263bc420748SJens Wiklander 					    &sess->ctx->uuid,
264ab35d7adSCedric Chaumont 					    sizeof(TEE_UUID));
265b0104773SPascal Brand 	default:
266ab35d7adSCedric Chaumont 		if (blen < tee_props_lut[prop].len)
267ab35d7adSCedric Chaumont 			return TEE_ERROR_SHORT_BUFFER;
268ab35d7adSCedric Chaumont 		return tee_svc_copy_to_user(sess, (void *)buf,
269ab35d7adSCedric Chaumont 					    tee_props_lut[prop].data,
270ab35d7adSCedric Chaumont 					    tee_props_lut[prop].len);
271b0104773SPascal Brand 	}
272b0104773SPascal Brand }
273b0104773SPascal Brand 
274b0104773SPascal Brand /*
275b0104773SPascal Brand  * TA invokes some TA with parameter.
276b0104773SPascal Brand  * If some parameters are memory references:
277b0104773SPascal Brand  * - either the memref is inside TA private RAM: TA is not allowed to expose
278b0104773SPascal Brand  *   its private RAM: use a temporary memory buffer and copy the data.
279b0104773SPascal Brand  * - or the memref is not in the TA private RAM:
280b0104773SPascal Brand  *   - if the memref was mapped to the TA, TA is allowed to expose it.
281b0104773SPascal Brand  *   - if so, converts memref virtual address into a physical address.
282b0104773SPascal Brand  */
283b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
284b0104773SPascal Brand 				     struct tee_ta_session *called_sess,
285b0104773SPascal Brand 				     uint32_t param_types,
286177603c7SJens Wiklander 				     struct abi_user32_param *callee_params,
287b0104773SPascal Brand 				     struct tee_ta_param *param,
288b0104773SPascal Brand 				     tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
289b0104773SPascal Brand 				     tee_mm_entry_t **mm)
290b0104773SPascal Brand {
291b0104773SPascal Brand 	size_t n;
292b0104773SPascal Brand 	TEE_Result res;
293b0104773SPascal Brand 	size_t req_mem = 0;
294b0104773SPascal Brand 	size_t s;
295b0104773SPascal Brand 	uint8_t *dst = 0;
296b0104773SPascal Brand 	tee_paddr_t dst_pa, src_pa = 0;
297b0104773SPascal Brand 	bool ta_private_memref[TEE_NUM_PARAMS];
298b0104773SPascal Brand 
299b7fc217fSPascal Brand 	/* fill 'param' input struct with caller params description buffer */
300b0104773SPascal Brand 	param->types = param_types;
301b7fc217fSPascal Brand 	if (!callee_params) {
302b0104773SPascal Brand 		if (param->types != 0)
303b0104773SPascal Brand 			return TEE_ERROR_BAD_PARAMETERS;
304b0104773SPascal Brand 		memset(param->params, 0, sizeof(param->params));
305b0104773SPascal Brand 	} else {
306177603c7SJens Wiklander 		res = tee_mmu_check_access_rights(sess->ctx,
307177603c7SJens Wiklander 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
308177603c7SJens Wiklander 			(tee_uaddr_t)callee_params,
309177603c7SJens Wiklander 			sizeof(struct abi_user32_param));
310177603c7SJens Wiklander 		if (res != TEE_SUCCESS)
311177603c7SJens Wiklander 			return res;
312177603c7SJens Wiklander 		abi_user32_param_to_param(param->params, callee_params,
313177603c7SJens Wiklander 					  param_types);
314b0104773SPascal Brand 	}
315b0104773SPascal Brand 
316b0104773SPascal Brand 	if ((called_sess != NULL) &&
317b0104773SPascal Brand 		(called_sess->ctx->static_ta == NULL) &&
318b0104773SPascal Brand 		(called_sess->ctx->flags & TA_FLAG_USER_MODE) == 0) {
319b0104773SPascal Brand 		/*
320b0104773SPascal Brand 		 * kernel TA, borrow the mapping of the calling
321b0104773SPascal Brand 		 * during this call.
322b0104773SPascal Brand 		 */
323b0104773SPascal Brand 		called_sess->calling_sess = sess;
324b0104773SPascal Brand 		return TEE_SUCCESS;
325b0104773SPascal Brand 	}
326b0104773SPascal Brand 
327b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
328b0104773SPascal Brand 
329b0104773SPascal Brand 		ta_private_memref[n] = false;
330b0104773SPascal Brand 
331b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
332b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
333b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
334b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
335b0104773SPascal Brand 			if (param->params[n].memref.buffer == NULL) {
336b0104773SPascal Brand 				if (param->params[n].memref.size != 0)
337b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
338b0104773SPascal Brand 				break;
339b0104773SPascal Brand 			}
340b0104773SPascal Brand 			/* uTA cannot expose its private memory */
341b0104773SPascal Brand 			if (tee_mmu_is_vbuf_inside_ta_private(sess->ctx,
342106d8aa6SPascal Brand 				    param->params[n].memref.buffer,
343b0104773SPascal Brand 				    param->params[n].memref.size)) {
344b0104773SPascal Brand 
345a17acc4cSSabrina Ni 				s = ROUNDUP(param->params[n].memref.size,
346b0104773SPascal Brand 						sizeof(uint32_t));
347b0104773SPascal Brand 				/* Check overflow */
348b0104773SPascal Brand 				if (req_mem + s < req_mem)
349b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
350b0104773SPascal Brand 				req_mem += s;
351b0104773SPascal Brand 				ta_private_memref[n] = true;
352b0104773SPascal Brand 				break;
353b0104773SPascal Brand 			}
354106d8aa6SPascal Brand 			if (tee_mmu_is_vbuf_intersect_ta_private(sess->ctx,
355106d8aa6SPascal Brand 				    param->params[n].memref.buffer,
356b0104773SPascal Brand 				    param->params[n].memref.size))
357b0104773SPascal Brand 				return TEE_ERROR_BAD_PARAMETERS;
358b0104773SPascal Brand 
359b0104773SPascal Brand 			if (tee_mmu_user_va2pa(sess->ctx,
360b0104773SPascal Brand 					(void *)param->params[n].memref.buffer,
3610e692b78SJens Wiklander 					&src_pa) != TEE_SUCCESS)
362b0104773SPascal Brand 				return TEE_ERROR_BAD_PARAMETERS;
363b0104773SPascal Brand 
364b0104773SPascal Brand 			param->param_attr[n] = tee_mmu_user_get_cache_attr(
365b0104773SPascal Brand 				sess->ctx,
366b0104773SPascal Brand 				(void *)param->params[n].memref.buffer);
367b0104773SPascal Brand 
368b0104773SPascal Brand 			param->params[n].memref.buffer = (void *)src_pa;
369b0104773SPascal Brand 			break;
370b0104773SPascal Brand 
371b0104773SPascal Brand 		default:
372b0104773SPascal Brand 			break;
373b0104773SPascal Brand 		}
374b0104773SPascal Brand 	}
375b0104773SPascal Brand 
376b0104773SPascal Brand 	if (req_mem == 0)
377b0104773SPascal Brand 		return TEE_SUCCESS;
378b0104773SPascal Brand 
379b0104773SPascal Brand 	/* Allocate section in secure DDR */
380b0104773SPascal Brand 	*mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem);
381b0104773SPascal Brand 	if (*mm == NULL) {
382b0104773SPascal Brand 		DMSG("tee_mm_alloc TEE_ERROR_GENERIC");
383b0104773SPascal Brand 		return TEE_ERROR_GENERIC;
384b0104773SPascal Brand 	}
385b0104773SPascal Brand 
386b0104773SPascal Brand 	/* Get the virtual address for the section in secure DDR */
387b0104773SPascal Brand 	res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst);
388b0104773SPascal Brand 	if (res != TEE_SUCCESS)
389b0104773SPascal Brand 		return res;
390b0104773SPascal Brand 	dst_pa = tee_mm_get_smem(*mm);
391b0104773SPascal Brand 
392b0104773SPascal Brand 	for (n = 0; n < 4; n++) {
393b0104773SPascal Brand 
394b0104773SPascal Brand 		if (ta_private_memref[n] == false)
395b0104773SPascal Brand 			continue;
396b0104773SPascal Brand 
397a17acc4cSSabrina Ni 		s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t));
398b0104773SPascal Brand 
399b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
400b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
401b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
402b0104773SPascal Brand 			if (param->params[n].memref.buffer != NULL) {
403b0104773SPascal Brand 				res = tee_svc_copy_from_user(sess, dst,
404b7fc217fSPascal Brand 						param->params[n].memref.buffer,
405b7fc217fSPascal Brand 						param->params[n].memref.size);
406b0104773SPascal Brand 				if (res != TEE_SUCCESS)
407b0104773SPascal Brand 					return res;
408b0104773SPascal Brand 				param->param_attr[n] =
409b0104773SPascal Brand 					tee_mmu_kmap_get_cache_attr(dst);
410b0104773SPascal Brand 				param->params[n].memref.buffer = (void *)dst_pa;
411b0104773SPascal Brand 				tmp_buf_pa[n] = dst_pa;
412b0104773SPascal Brand 				dst += s;
413b0104773SPascal Brand 				dst_pa += s;
414b0104773SPascal Brand 			}
415b0104773SPascal Brand 			break;
416b0104773SPascal Brand 
417b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
418b0104773SPascal Brand 			if (param->params[n].memref.buffer != NULL) {
419b0104773SPascal Brand 				param->param_attr[n] =
420b0104773SPascal Brand 					tee_mmu_kmap_get_cache_attr(dst);
421b0104773SPascal Brand 				param->params[n].memref.buffer = (void *)dst_pa;
422b0104773SPascal Brand 				tmp_buf_pa[n] = dst_pa;
423b0104773SPascal Brand 				dst += s;
424b0104773SPascal Brand 				dst_pa += s;
425b0104773SPascal Brand 			}
426b0104773SPascal Brand 			break;
427b0104773SPascal Brand 
428b0104773SPascal Brand 		default:
429b0104773SPascal Brand 			continue;
430b0104773SPascal Brand 		}
431b0104773SPascal Brand 	}
432b0104773SPascal Brand 
433b0104773SPascal Brand 	tee_mmu_kunmap(dst, req_mem);
434b0104773SPascal Brand 
435b0104773SPascal Brand 	return TEE_SUCCESS;
436b0104773SPascal Brand }
437b0104773SPascal Brand 
438b0104773SPascal Brand /*
439b0104773SPascal Brand  * Back from execution of service: update parameters passed from TA:
440b0104773SPascal Brand  * If some parameters were memory references:
441b0104773SPascal Brand  * - either the memref was temporary: copy back data and update size
442b0104773SPascal Brand  * - or it was the original TA memref: update only the size value.
443b0104773SPascal Brand  */
444b0104773SPascal Brand static TEE_Result tee_svc_update_out_param(
445b0104773SPascal Brand 		struct tee_ta_session *sess,
446b0104773SPascal Brand 		struct tee_ta_session *called_sess,
447b0104773SPascal Brand 		struct tee_ta_param *param,
448b0104773SPascal Brand 		tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
449177603c7SJens Wiklander 		struct abi_user32_param *usr_param)
450b0104773SPascal Brand {
451b0104773SPascal Brand 	size_t n;
452177603c7SJens Wiklander 	TEE_Param callee_params[TEE_NUM_PARAMS];
453b0104773SPascal Brand 	bool have_private_mem_map = (called_sess == NULL) ||
454b0104773SPascal Brand 		(called_sess->ctx->static_ta != NULL) ||
455b0104773SPascal Brand 		((called_sess->ctx->flags & TA_FLAG_USER_MODE) != 0);
456b0104773SPascal Brand 
457bc420748SJens Wiklander 
458b0104773SPascal Brand 	tee_ta_set_current_session(sess);
459177603c7SJens Wiklander 	abi_user32_param_to_param(callee_params, usr_param, param->types);
460b0104773SPascal Brand 
461b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
462b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
463b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
464b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
465b0104773SPascal Brand 
466b0104773SPascal Brand 			/* outside TA private => memref is valid, update size */
467b0104773SPascal Brand 			if (!tee_mmu_is_vbuf_inside_ta_private(sess->ctx,
468106d8aa6SPascal Brand 					callee_params[n].memref.buffer,
469b0104773SPascal Brand 					param->params[n].memref.size)) {
470b7fc217fSPascal Brand 				callee_params[n].memref.size =
471b0104773SPascal Brand 					param->params[n].memref.size;
472b0104773SPascal Brand 				break;
473b0104773SPascal Brand 			}
474b0104773SPascal Brand 
475b0104773SPascal Brand 			/*
476b0104773SPascal Brand 			 * If we called a kernel TA the parameters are in shared
477b0104773SPascal Brand 			 * memory and no copy is needed.
478b0104773SPascal Brand 			 */
479b0104773SPascal Brand 			if (have_private_mem_map &&
480b0104773SPascal Brand 			    param->params[n].memref.size <=
481b7fc217fSPascal Brand 			    callee_params[n].memref.size) {
482b0104773SPascal Brand 				uint8_t *src = 0;
483b0104773SPascal Brand 				TEE_Result res;
484b0104773SPascal Brand 
485b0104773SPascal Brand 				/* FIXME: TA_RAM is already mapped ! */
486b0104773SPascal Brand 				res = tee_mmu_kmap(tmp_buf_pa[n],
487b0104773SPascal Brand 					param->params[n].memref.size, &src);
488b0104773SPascal Brand 				if (res != TEE_SUCCESS)
489b0104773SPascal Brand 					return TEE_ERROR_GENERIC;
490b0104773SPascal Brand 
491b0104773SPascal Brand 				res = tee_svc_copy_to_user(sess,
492b7fc217fSPascal Brand 							 callee_params[n].memref.
493b0104773SPascal Brand 							 buffer, src,
494b0104773SPascal Brand 							 param->params[n].
495b0104773SPascal Brand 							 memref.size);
496b0104773SPascal Brand 				if (res != TEE_SUCCESS)
497b0104773SPascal Brand 					return res;
498b0104773SPascal Brand 				tee_mmu_kunmap(src,
499b0104773SPascal Brand 					       param->params[n].memref.size);
500b0104773SPascal Brand 
501b0104773SPascal Brand 			}
502b7fc217fSPascal Brand 			callee_params[n].memref.size = param->params[n].memref.size;
503b0104773SPascal Brand 			break;
504b0104773SPascal Brand 
505b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
506b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_INOUT:
507b7fc217fSPascal Brand 			callee_params[n].value = param->params[n].value;
508b0104773SPascal Brand 			break;
509b0104773SPascal Brand 
510b0104773SPascal Brand 		default:
511b0104773SPascal Brand 			continue;
512b0104773SPascal Brand 		}
513b0104773SPascal Brand 	}
514b0104773SPascal Brand 
515177603c7SJens Wiklander 	abi_param_to_user32_param(usr_param, callee_params, param->types);
516177603c7SJens Wiklander 
517b0104773SPascal Brand 	return TEE_SUCCESS;
518b0104773SPascal Brand }
519b0104773SPascal Brand 
520b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */
521*453a5030SJerome Forissier TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
522b0104773SPascal Brand 				   uint32_t cancel_req_to, uint32_t param_types,
523177603c7SJens Wiklander 				   struct abi_user32_param *usr_param,
524b0104773SPascal Brand 				   TEE_TASessionHandle *ta_sess,
525b0104773SPascal Brand 				   uint32_t *ret_orig)
526b0104773SPascal Brand {
527b0104773SPascal Brand 	TEE_Result res;
528b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
529b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
530b0104773SPascal Brand 	struct tee_ta_session *sess;
531b0104773SPascal Brand 	tee_mm_entry_t *mm_param = NULL;
532b0104773SPascal Brand 
533b0104773SPascal Brand 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
534b0104773SPascal Brand 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
535b0104773SPascal Brand 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
536b0104773SPascal Brand 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
537b0104773SPascal Brand 
538b0104773SPascal Brand 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
539b0104773SPascal Brand 		res = TEE_ERROR_OUT_OF_MEMORY;
540b0104773SPascal Brand 		goto out_free_only;
541b0104773SPascal Brand 	}
542b0104773SPascal Brand 
543b0104773SPascal Brand 	memset(param, 0, sizeof(struct tee_ta_param));
544b0104773SPascal Brand 
545b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
546b0104773SPascal Brand 	if (res != TEE_SUCCESS)
547b0104773SPascal Brand 		goto out_free_only;
548b0104773SPascal Brand 
549b0104773SPascal Brand 	res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID));
550b0104773SPascal Brand 	if (res != TEE_SUCCESS)
551b0104773SPascal Brand 		goto function_exit;
552b0104773SPascal Brand 
553b0104773SPascal Brand 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
554bc420748SJens Wiklander 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
555b0104773SPascal Brand 
556177603c7SJens Wiklander 	res = tee_svc_copy_param(sess, NULL, param_types, usr_param, param,
557b0104773SPascal Brand 				 tmp_buf_pa, &mm_param);
558b0104773SPascal Brand 	if (res != TEE_SUCCESS)
559b0104773SPascal Brand 		goto function_exit;
560b0104773SPascal Brand 
561b0104773SPascal Brand 	/*
562b0104773SPascal Brand 	 * Find session of a multi session TA or a static TA
563b0104773SPascal Brand 	 * In such a case, there is no need to ask the supplicant for the TA
564b0104773SPascal Brand 	 * code
565b0104773SPascal Brand 	 */
566b0104773SPascal Brand 	res = tee_ta_open_session(&ret_o, &s, &sess->ctx->open_sessions, uuid,
56727cbcc57SJens Wiklander 				  clnt_id, cancel_req_to, param);
568c0346845SJens Wiklander 	if (res != TEE_SUCCESS)
569b0104773SPascal Brand 		goto function_exit;
570b0104773SPascal Brand 
571177603c7SJens Wiklander 	res = tee_svc_update_out_param(sess, NULL, param, tmp_buf_pa,
572177603c7SJens Wiklander 				       usr_param);
573b0104773SPascal Brand 
574b0104773SPascal Brand function_exit:
575b0104773SPascal Brand 	tee_ta_set_current_session(sess);
576b0104773SPascal Brand 
577b0104773SPascal Brand 	if (mm_param != NULL) {
578b0104773SPascal Brand 		TEE_Result res2;
579b0104773SPascal Brand 		void *va = 0;
580b0104773SPascal Brand 
581b0104773SPascal Brand 		res2 =
582b0104773SPascal Brand 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
583b0104773SPascal Brand 		if (res2 == TEE_SUCCESS)
584b0104773SPascal Brand 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
585b0104773SPascal Brand 	}
586b0104773SPascal Brand 	tee_mm_free(mm_param);
5878707ec0fSJerome Forissier 	/*
5888707ec0fSJerome Forissier 	 * We know that sizeof(TEE_TASessionHandle) in user mode (TA) is 4,
5898707ec0fSJerome Forissier 	 * because we only support 32-bit TAs, so take care not to overflow it
5908707ec0fSJerome Forissier 	 * if kernel addresses are 64-bit
5918707ec0fSJerome Forissier 	 */
5928707ec0fSJerome Forissier 	tee_svc_copy_kaddr_to_user32(sess, (uint32_t *)ta_sess, s);
593b0104773SPascal Brand 	tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
594b0104773SPascal Brand 
595b0104773SPascal Brand out_free_only:
596b0104773SPascal Brand 	free(param);
597b0104773SPascal Brand 	free(uuid);
598b0104773SPascal Brand 	free(clnt_id);
599b0104773SPascal Brand 	return res;
600b0104773SPascal Brand }
601b0104773SPascal Brand 
602*453a5030SJerome Forissier TEE_Result syscall_close_ta_session(TEE_TASessionHandle ta_sess)
603b0104773SPascal Brand {
604b0104773SPascal Brand 	TEE_Result res;
605b0104773SPascal Brand 	struct tee_ta_session *sess;
60660699957SPascal Brand 	TEE_Identity clnt_id;
607b0104773SPascal Brand 
608b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
609b0104773SPascal Brand 	if (res != TEE_SUCCESS)
610b0104773SPascal Brand 		return res;
611b0104773SPascal Brand 
61260699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
613bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
614b0104773SPascal Brand 
61560699957SPascal Brand 	tee_ta_set_current_session(NULL);
616096cbcddSJean-Michel Delorme 	res = tee_ta_close_session((struct tee_ta_session *)ta_sess,
617096cbcddSJean-Michel Delorme 			&sess->ctx->open_sessions,
61860699957SPascal Brand 				   &clnt_id);
619b0104773SPascal Brand 	tee_ta_set_current_session(sess);
620b0104773SPascal Brand 	return res;
621b0104773SPascal Brand }
622b0104773SPascal Brand 
623*453a5030SJerome Forissier TEE_Result syscall_invoke_ta_command(TEE_TASessionHandle ta_sess,
624b0104773SPascal Brand 				     uint32_t cancel_req_to, uint32_t cmd_id,
625177603c7SJens Wiklander 				     uint32_t param_types,
626177603c7SJens Wiklander 				     struct abi_user32_param *usr_param,
627b0104773SPascal Brand 				     uint32_t *ret_orig)
628b0104773SPascal Brand {
629b0104773SPascal Brand 	TEE_Result res;
630b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
631b0104773SPascal Brand 	struct tee_ta_param param = { 0 };
63260699957SPascal Brand 	TEE_Identity clnt_id;
633b0104773SPascal Brand 	struct tee_ta_session *sess;
634b0104773SPascal Brand 	struct tee_ta_session *called_sess = (struct tee_ta_session *)ta_sess;
635b0104773SPascal Brand 	tee_mm_entry_t *mm_param = NULL;
636b0104773SPascal Brand 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
637b0104773SPascal Brand 
638b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
639b0104773SPascal Brand 	if (res != TEE_SUCCESS)
640b0104773SPascal Brand 		return res;
641b0104773SPascal Brand 
642b0104773SPascal Brand 	res =
643b0104773SPascal Brand 	    tee_ta_verify_session_pointer(called_sess,
644b0104773SPascal Brand 					  &sess->ctx->open_sessions);
645b0104773SPascal Brand 	if (res != TEE_SUCCESS)
646b0104773SPascal Brand 		return res;
647b0104773SPascal Brand 
64860699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
649bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
65060699957SPascal Brand 
651177603c7SJens Wiklander 	res = tee_svc_copy_param(sess, called_sess, param_types, usr_param,
652b0104773SPascal Brand 				 &param, tmp_buf_pa, &mm_param);
653b0104773SPascal Brand 	if (res != TEE_SUCCESS)
654b0104773SPascal Brand 		goto function_exit;
655b0104773SPascal Brand 
65660699957SPascal Brand 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
65760699957SPascal Brand 				    cancel_req_to, cmd_id, &param);
65860699957SPascal Brand 
659b0104773SPascal Brand 	if (res != TEE_SUCCESS)
660b0104773SPascal Brand 		goto function_exit;
661b0104773SPascal Brand 
662b0104773SPascal Brand 	res = tee_svc_update_out_param(sess, called_sess, &param, tmp_buf_pa,
663177603c7SJens Wiklander 				       usr_param);
664b0104773SPascal Brand 	if (res != TEE_SUCCESS)
665b0104773SPascal Brand 		goto function_exit;
666b0104773SPascal Brand 
667b0104773SPascal Brand function_exit:
668b0104773SPascal Brand 	tee_ta_set_current_session(sess);
669b0104773SPascal Brand 	called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */
670b0104773SPascal Brand 
671b0104773SPascal Brand 	if (mm_param != NULL) {
672b0104773SPascal Brand 		TEE_Result res2;
673b0104773SPascal Brand 		void *va = 0;
674b0104773SPascal Brand 
675b0104773SPascal Brand 		res2 =
676b0104773SPascal Brand 		    tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va);
677b0104773SPascal Brand 		if (res2 == TEE_SUCCESS)
678b0104773SPascal Brand 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
679b0104773SPascal Brand 	}
680b0104773SPascal Brand 	tee_mm_free(mm_param);
681b0104773SPascal Brand 	if (ret_orig)
682b0104773SPascal Brand 		tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
683b0104773SPascal Brand 	return res;
684b0104773SPascal Brand }
685b0104773SPascal Brand 
686*453a5030SJerome Forissier TEE_Result syscall_check_access_rights(uint32_t flags, const void *buf,
687b0104773SPascal Brand 				       size_t len)
688b0104773SPascal Brand {
689b0104773SPascal Brand 	TEE_Result res;
690b0104773SPascal Brand 	struct tee_ta_session *s;
691b0104773SPascal Brand 
692b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
693b0104773SPascal Brand 	if (res != TEE_SUCCESS)
694b0104773SPascal Brand 		return res;
695b0104773SPascal Brand 
696b0104773SPascal Brand 	return tee_mmu_check_access_rights(s->ctx, flags, (tee_uaddr_t)buf,
697b0104773SPascal Brand 					   len);
698b0104773SPascal Brand }
699b0104773SPascal Brand 
700b0104773SPascal Brand TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr,
701b0104773SPascal Brand 				  const void *uaddr, size_t len)
702b0104773SPascal Brand {
703b0104773SPascal Brand 	TEE_Result res;
704b0104773SPascal Brand 	struct tee_ta_session *s;
705b0104773SPascal Brand 
706b0104773SPascal Brand 	if (sess == NULL) {
707b0104773SPascal Brand 		res = tee_ta_get_current_session(&s);
708b0104773SPascal Brand 		if (res != TEE_SUCCESS)
709b0104773SPascal Brand 			return res;
710b0104773SPascal Brand 	} else {
711b0104773SPascal Brand 		s = sess;
712b0104773SPascal Brand 		tee_ta_set_current_session(s);
713b0104773SPascal Brand 	}
714b0104773SPascal Brand 	res =
715b0104773SPascal Brand 	    tee_mmu_check_access_rights(s->ctx,
716b0104773SPascal Brand 					TEE_MEMORY_ACCESS_READ |
717b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
718b0104773SPascal Brand 					(tee_uaddr_t)uaddr, len);
719b0104773SPascal Brand 	if (res != TEE_SUCCESS)
720b0104773SPascal Brand 		return res;
721b0104773SPascal Brand 
722b0104773SPascal Brand 	memcpy(kaddr, uaddr, len);
723b0104773SPascal Brand 	return TEE_SUCCESS;
724b0104773SPascal Brand }
725b0104773SPascal Brand 
726b0104773SPascal Brand TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr,
727b0104773SPascal Brand 				const void *kaddr, size_t len)
728b0104773SPascal Brand {
729b0104773SPascal Brand 	TEE_Result res;
730b0104773SPascal Brand 	struct tee_ta_session *s;
731b0104773SPascal Brand 
732b0104773SPascal Brand 	if (sess == NULL) {
733b0104773SPascal Brand 		res = tee_ta_get_current_session(&s);
734b0104773SPascal Brand 		if (res != TEE_SUCCESS)
735b0104773SPascal Brand 			return res;
736b0104773SPascal Brand 	} else {
737b0104773SPascal Brand 		s = sess;
738b0104773SPascal Brand 		tee_ta_set_current_session(s);
739b0104773SPascal Brand 	}
740b0104773SPascal Brand 
741b0104773SPascal Brand 	res =
742b0104773SPascal Brand 	    tee_mmu_check_access_rights(s->ctx,
743b0104773SPascal Brand 					TEE_MEMORY_ACCESS_WRITE |
744b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
745b0104773SPascal Brand 					(tee_uaddr_t)uaddr, len);
746b0104773SPascal Brand 	if (res != TEE_SUCCESS)
747b0104773SPascal Brand 		return res;
748b0104773SPascal Brand 
749b0104773SPascal Brand 	memcpy(uaddr, kaddr, len);
750b0104773SPascal Brand 	return TEE_SUCCESS;
751b0104773SPascal Brand }
752b0104773SPascal Brand 
7538707ec0fSJerome Forissier /*
7548707ec0fSJerome Forissier  * Copy a kernel address into a 32-bit user buffer. In 64-bit mode, this will
7558707ec0fSJerome Forissier  * fail if the address is not in the lower 4 GiB.
7568707ec0fSJerome Forissier  */
7578707ec0fSJerome Forissier TEE_Result tee_svc_copy_kaddr_to_user32(struct tee_ta_session *sess,
7588707ec0fSJerome Forissier 					uint32_t *uaddr, const void *kaddr)
7598707ec0fSJerome Forissier {
7608707ec0fSJerome Forissier 	uint32_t lo = (long)kaddr & 0xFFFFFFFF;
7618707ec0fSJerome Forissier 
7628707ec0fSJerome Forissier 	if ((long)lo != (long)kaddr) {
7638707ec0fSJerome Forissier 		EMSG("Unexpected high kernel address\n");
7648707ec0fSJerome Forissier 		return TEE_ERROR_GENERIC;
7658707ec0fSJerome Forissier 	}
7668707ec0fSJerome Forissier 	return tee_svc_copy_to_user(sess, uaddr, &lo, sizeof(lo));
7678707ec0fSJerome Forissier }
7688707ec0fSJerome Forissier 
769b0104773SPascal Brand static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time)
770b0104773SPascal Brand {
771b0104773SPascal Brand 	TEE_Time current_time;
772b0104773SPascal Brand 
773b0104773SPascal Brand 	if (s->cancel_mask)
774b0104773SPascal Brand 		return false;
775b0104773SPascal Brand 
776b0104773SPascal Brand 	if (s->cancel)
777b0104773SPascal Brand 		return true;
778b0104773SPascal Brand 
779b0104773SPascal Brand 	if (s->cancel_time.seconds == UINT32_MAX)
780b0104773SPascal Brand 		return false;
781b0104773SPascal Brand 
782b0104773SPascal Brand 	if (curr_time != NULL)
783b0104773SPascal Brand 		current_time = *curr_time;
784b0104773SPascal Brand 	else if (tee_time_get_sys_time(&current_time) != TEE_SUCCESS)
785b0104773SPascal Brand 		return false;
786b0104773SPascal Brand 
787b0104773SPascal Brand 	if (current_time.seconds > s->cancel_time.seconds ||
788b0104773SPascal Brand 	    (current_time.seconds == s->cancel_time.seconds &&
789b0104773SPascal Brand 	     current_time.millis >= s->cancel_time.millis)) {
790b0104773SPascal Brand 		return true;
791b0104773SPascal Brand 	}
792b0104773SPascal Brand 
793b0104773SPascal Brand 	return false;
794b0104773SPascal Brand }
795b0104773SPascal Brand 
796*453a5030SJerome Forissier TEE_Result syscall_get_cancellation_flag(bool *cancel)
797b0104773SPascal Brand {
798b0104773SPascal Brand 	TEE_Result res;
799b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
800b0104773SPascal Brand 	bool c;
801b0104773SPascal Brand 
802b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
803b0104773SPascal Brand 	if (res != TEE_SUCCESS)
804b0104773SPascal Brand 		return res;
805b0104773SPascal Brand 
806b0104773SPascal Brand 	c = session_is_cancelled(s, NULL);
807b0104773SPascal Brand 
808b0104773SPascal Brand 	return tee_svc_copy_to_user(s, cancel, &c, sizeof(c));
809b0104773SPascal Brand }
810b0104773SPascal Brand 
811*453a5030SJerome Forissier TEE_Result syscall_unmask_cancellation(bool *old_mask)
812b0104773SPascal Brand {
813b0104773SPascal Brand 	TEE_Result res;
814b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
815b0104773SPascal Brand 	bool m;
816b0104773SPascal Brand 
817b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
818b0104773SPascal Brand 	if (res != TEE_SUCCESS)
819b0104773SPascal Brand 		return res;
820b0104773SPascal Brand 
821b0104773SPascal Brand 	m = s->cancel_mask;
822b0104773SPascal Brand 	s->cancel_mask = false;
823b0104773SPascal Brand 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
824b0104773SPascal Brand }
825b0104773SPascal Brand 
826*453a5030SJerome Forissier TEE_Result syscall_mask_cancellation(bool *old_mask)
827b0104773SPascal Brand {
828b0104773SPascal Brand 	TEE_Result res;
829b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
830b0104773SPascal Brand 	bool m;
831b0104773SPascal Brand 
832b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
833b0104773SPascal Brand 	if (res != TEE_SUCCESS)
834b0104773SPascal Brand 		return res;
835b0104773SPascal Brand 
836b0104773SPascal Brand 	m = s->cancel_mask;
837b0104773SPascal Brand 	s->cancel_mask = true;
838b0104773SPascal Brand 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
839b0104773SPascal Brand }
840b0104773SPascal Brand 
841*453a5030SJerome Forissier TEE_Result syscall_wait(uint32_t timeout)
842b0104773SPascal Brand {
843b0104773SPascal Brand 	TEE_Result res = TEE_SUCCESS;
844b0104773SPascal Brand 	uint32_t mytime = 0;
845b0104773SPascal Brand 	struct tee_ta_session *s;
846b0104773SPascal Brand 	TEE_Time base_time;
847b0104773SPascal Brand 	TEE_Time current_time;
848b0104773SPascal Brand 
849b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
850b0104773SPascal Brand 	if (res != TEE_SUCCESS)
851b0104773SPascal Brand 		return res;
852b0104773SPascal Brand 
853b0104773SPascal Brand 	res = tee_time_get_sys_time(&base_time);
854b0104773SPascal Brand 	if (res != TEE_SUCCESS)
855b0104773SPascal Brand 		return res;
856b0104773SPascal Brand 
857b0104773SPascal Brand 	while (true) {
858b0104773SPascal Brand 		res = tee_time_get_sys_time(&current_time);
859b0104773SPascal Brand 		if (res != TEE_SUCCESS)
860b0104773SPascal Brand 			return res;
861b0104773SPascal Brand 
862b0104773SPascal Brand 		if (session_is_cancelled(s, &current_time))
863b0104773SPascal Brand 			return TEE_ERROR_CANCEL;
864b0104773SPascal Brand 
865b0104773SPascal Brand 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
866b0104773SPascal Brand 		    (int)current_time.millis - (int)base_time.millis;
867b0104773SPascal Brand 		if (mytime >= timeout)
868b0104773SPascal Brand 			return TEE_SUCCESS;
869b0104773SPascal Brand 
870d1aea08fSSY Chiu 		tee_time_wait(timeout - mytime);
871b0104773SPascal Brand 	}
872b0104773SPascal Brand 
873b0104773SPascal Brand 	return res;
874b0104773SPascal Brand }
875b0104773SPascal Brand 
876*453a5030SJerome Forissier TEE_Result syscall_get_time(enum utee_time_category cat, TEE_Time *mytime)
877b0104773SPascal Brand {
878b0104773SPascal Brand 	TEE_Result res, res2;
879b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
880b0104773SPascal Brand 	TEE_Time t;
881b0104773SPascal Brand 
882b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
883b0104773SPascal Brand 	if (res != TEE_SUCCESS)
884b0104773SPascal Brand 		return res;
885b0104773SPascal Brand 
886b0104773SPascal Brand 	switch (cat) {
887b0104773SPascal Brand 	case UTEE_TIME_CAT_SYSTEM:
888b0104773SPascal Brand 		res = tee_time_get_sys_time(&t);
889b0104773SPascal Brand 		break;
890b0104773SPascal Brand 	case UTEE_TIME_CAT_TA_PERSISTENT:
891bc420748SJens Wiklander 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
892b0104773SPascal Brand 		break;
893b0104773SPascal Brand 	case UTEE_TIME_CAT_REE:
894b0104773SPascal Brand 		res = tee_time_get_ree_time(&t);
895b0104773SPascal Brand 		break;
896b0104773SPascal Brand 	default:
897b0104773SPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
898b0104773SPascal Brand 		break;
899b0104773SPascal Brand 	}
900b0104773SPascal Brand 
901b0104773SPascal Brand 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
902b0104773SPascal Brand 		res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t));
903b0104773SPascal Brand 		if (res2 != TEE_SUCCESS)
904b0104773SPascal Brand 			res = res2;
905b0104773SPascal Brand 	}
906b0104773SPascal Brand 
907b0104773SPascal Brand 	return res;
908b0104773SPascal Brand }
909b0104773SPascal Brand 
910*453a5030SJerome Forissier TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
911b0104773SPascal Brand {
912b0104773SPascal Brand 	TEE_Result res;
913b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
914b0104773SPascal Brand 	TEE_Time t;
915b0104773SPascal Brand 
916b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
917b0104773SPascal Brand 	if (res != TEE_SUCCESS)
918b0104773SPascal Brand 		return res;
919b0104773SPascal Brand 
920b0104773SPascal Brand 	res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t));
921b0104773SPascal Brand 	if (res != TEE_SUCCESS)
922b0104773SPascal Brand 		return res;
923b0104773SPascal Brand 
924bc420748SJens Wiklander 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
925b0104773SPascal Brand }
926fa530828SPascal Brand 
927fa530828SPascal Brand #ifdef CFG_CACHE_API
928*453a5030SJerome Forissier TEE_Result syscall_cache_operation(void *va, size_t len,
929fa530828SPascal Brand 				   enum utee_cache_operation op)
930fa530828SPascal Brand {
931fa530828SPascal Brand 	TEE_Result res;
932fa530828SPascal Brand 	struct tee_ta_session *s = NULL;
933fa530828SPascal Brand 
934fa530828SPascal Brand 	res = tee_ta_get_current_session(&s);
935fa530828SPascal Brand 	if (res != TEE_SUCCESS)
936fa530828SPascal Brand 		return res;
937fa530828SPascal Brand 
938fa530828SPascal Brand 	if ((s->ctx->flags & TA_FLAG_CACHE_MAINTENANCE) == 0)
939fa530828SPascal Brand 		return TEE_ERROR_NOT_SUPPORTED;
940fa530828SPascal Brand 
941fa530828SPascal Brand 	return tee_uta_cache_operation(s, op, va, len);
942fa530828SPascal Brand }
943fa530828SPascal Brand #endif
944