xref: /optee_os/core/tee/tee_svc.c (revision 385e3901a6f10bc2bb000fa283909db3bdbd9809)
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <util.h>
28 #include <kernel/tee_common_otp.h>
29 #include <kernel/tee_common.h>
30 #include <tee_api_types.h>
31 #include <kernel/tee_ta_manager.h>
32 #include <utee_types.h>
33 #include <tee/tee_svc.h>
34 #include <tee/tee_cryp_utl.h>
35 #include <mm/tee_mmu.h>
36 #include <mm/tee_mm.h>
37 #include <mm/core_memprot.h>
38 #include <kernel/tee_time.h>
39 
40 #include <user_ta_header.h>
41 #include <trace.h>
42 #include <kernel/trace_ta.h>
43 #include <kernel/chip_services.h>
44 #include <kernel/static_ta.h>
45 
46 #include <assert.h>
47 
48 vaddr_t tee_svc_uref_base;
49 
50 void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused)
51 {
52 #ifdef CFG_TEE_CORE_TA_TRACE
53 	char *kbuf;
54 
55 	if (len == 0)
56 		return;
57 
58 	kbuf = malloc(len);
59 	if (kbuf == NULL)
60 		return;
61 	*kbuf = '\0';
62 
63 	/* log as Info/Raw traces */
64 	if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS)
65 		TAMSG_RAW("%.*s", (int)len, kbuf);
66 
67 	free(kbuf);
68 #endif
69 }
70 
71 TEE_Result syscall_not_supported(void)
72 {
73 	return TEE_ERROR_NOT_SUPPORTED;
74 }
75 
76 /* Configuration properties */
77 /* API implementation version */
78 static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION);
79 
80 /* Implementation description (implementation-dependent) */
81 static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR);
82 
83 /*
84  * TA persistent time protection level
85  * 100: Persistent time based on an REE-controlled real-time clock
86  * and on the TEE Trusted Storage for the storage of origins (default).
87  * 1000: Persistent time based on a TEE-controlled real-time clock
88  * and the TEE Trusted Storage.
89  * The real-time clock MUST be out of reach of software attacks
90  * from the REE.
91  */
92 static const uint32_t ta_time_prot_lvl = 100;
93 
94 /* Elliptic Curve Cryptographic support */
95 #ifdef CFG_CRYPTO_ECC
96 static const uint32_t crypto_ecc_en = 1;
97 #else
98 static const uint32_t crypto_ecc_en;
99 #endif
100 
101 /*
102  * Trusted storage anti rollback protection level
103  * 0 (or missing): No antirollback protection (default)
104  * 100: Antirollback enforced at REE level
105  * 1000: Antirollback TEE-controlled hardware
106  */
107 static const uint32_t ts_antiroll_prot_lvl;
108 
109 /* Trusted OS implementation version */
110 static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION);
111 
112 /* Trusted OS implementation version (binary value) */
113 static const uint32_t trustedos_impl_bin_version; /* 0 by default */
114 
115 /* Trusted OS implementation manufacturer name */
116 static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER);
117 
118 /* Trusted firmware version */
119 static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION);
120 
121 /* Trusted firmware version (binary value) */
122 static const uint32_t fw_impl_bin_version; /* 0 by default */
123 
124 /* Trusted firmware manufacturer name */
125 static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER);
126 
127 static TEE_Result get_prop_tee_dev_id(struct tee_ta_session *sess,
128 				      void *buf, size_t *blen)
129 {
130 	TEE_Result res;
131 	TEE_UUID uuid;
132 	const size_t nslen = 5;
133 	uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
134 	    'O', 'P', 'T', 'E', 'E' };
135 
136 	if (*blen < sizeof(uuid)) {
137 		*blen = sizeof(uuid);
138 		return TEE_ERROR_SHORT_BUFFER;
139 	}
140 	*blen = sizeof(uuid);
141 
142 	if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen))
143 		return TEE_ERROR_BAD_STATE;
144 
145 	res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data),
146 				    (uint8_t *)&uuid, sizeof(uuid));
147 	if (res != TEE_SUCCESS)
148 		return TEE_ERROR_BAD_STATE;
149 
150 	/*
151 	 * Changes the random value into and UUID as specifiec
152 	 * in RFC 4122. The magic values are from the example
153 	 * code in the RFC.
154 	 *
155 	 * TEE_UUID is defined slightly different from the RFC,
156 	 * but close enough for our purpose.
157 	 */
158 
159 	uuid.timeHiAndVersion &= 0x0fff;
160 	uuid.timeHiAndVersion |= 5 << 12;
161 
162 	/* uuid.clock_seq_hi_and_reserved in the RFC */
163 	uuid.clockSeqAndNode[0] &= 0x3f;
164 	uuid.clockSeqAndNode[0] |= 0x80;
165 
166 	return tee_svc_copy_to_user(sess, buf, &uuid, sizeof(TEE_UUID));
167 }
168 
169 static TEE_Result get_prop_tee_sys_time_prot_level(struct tee_ta_session *sess,
170 						   void *buf, size_t *blen)
171 {
172 	uint32_t prot;
173 
174 	if (*blen < sizeof(prot)) {
175 		*blen = sizeof(prot);
176 		return TEE_ERROR_SHORT_BUFFER;
177 	}
178 	*blen = sizeof(prot);
179 	prot = tee_time_get_sys_time_protection_level();
180 	return tee_svc_copy_to_user(sess, (void *)buf, &prot, sizeof(prot));
181 }
182 
183 static TEE_Result get_prop_client_id(struct tee_ta_session *sess,
184 				     void *buf, size_t *blen)
185 {
186 	if (*blen < sizeof(TEE_Identity)) {
187 		*blen = sizeof(TEE_Identity);
188 		return TEE_ERROR_SHORT_BUFFER;
189 	}
190 	*blen = sizeof(TEE_Identity);
191 	return tee_svc_copy_to_user(sess, buf, &sess->clnt_id,
192 				    sizeof(TEE_Identity));
193 }
194 
195 static TEE_Result get_prop_ta_app_id(struct tee_ta_session *sess,
196 				     void *buf, size_t *blen)
197 {
198 	if (*blen < sizeof(TEE_UUID)) {
199 		*blen = sizeof(TEE_UUID);
200 		return TEE_ERROR_SHORT_BUFFER;
201 	}
202 	*blen = sizeof(TEE_UUID);
203 	return tee_svc_copy_to_user(sess, buf, &sess->ctx->uuid,
204 				    sizeof(TEE_UUID));
205 }
206 
207 /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */
208 const struct tee_props tee_propset_client[] = {
209 	{
210 		.name = "gpd.client.identity",
211 		.prop_type = USER_TA_PROP_TYPE_IDENTITY,
212 		.get_prop_func = get_prop_client_id
213 	},
214 };
215 
216 /* Properties of the set TEE_PROPSET_CURRENT_TA */
217 const struct tee_props tee_propset_ta[] = {
218 	{
219 		.name = "gpd.ta.appID",
220 		.prop_type = USER_TA_PROP_TYPE_UUID,
221 		.get_prop_func = get_prop_ta_app_id
222 	},
223 
224 	/*
225 	 * Following properties are processed directly in libutee:
226 	 *	TA_PROP_STR_SINGLE_INSTANCE
227 	 *	TA_PROP_STR_MULTI_SESSION
228 	 *	TA_PROP_STR_KEEP_ALIVE
229 	 *	TA_PROP_STR_DATA_SIZE
230 	 *	TA_PROP_STR_STACK_SIZE
231 	 *	TA_PROP_STR_VERSION
232 	 *	TA_PROP_STR_DESCRIPTION
233 	 *	USER_TA_PROP_TYPE_STRING,
234 	 *	TA_DESCRIPTION
235 	 */
236 };
237 
238 /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */
239 const struct tee_props tee_propset_tee[] = {
240 	{
241 		.name = "gpd.tee.apiversion",
242 		.prop_type = USER_TA_PROP_TYPE_STRING,
243 		.data = api_vers,
244 		.len = sizeof(api_vers),
245 	},
246 	{
247 		.name = "gpd.tee.description",
248 		.prop_type = USER_TA_PROP_TYPE_STRING,
249 		.data = descr, .len = sizeof(descr)
250 	},
251 	{
252 		.name = "gpd.tee.deviceID",
253 		.prop_type = USER_TA_PROP_TYPE_UUID,
254 		.get_prop_func = get_prop_tee_dev_id
255 	},
256 	{
257 		.name = "gpd.tee.systemTime.protectionLevel",
258 		.prop_type = USER_TA_PROP_TYPE_U32,
259 		.get_prop_func = get_prop_tee_sys_time_prot_level
260 	},
261 	{
262 		.name = "gpd.tee.TAPersistentTime.protectionLevel",
263 		.prop_type = USER_TA_PROP_TYPE_U32,
264 		.data = &ta_time_prot_lvl,
265 		.len = sizeof(ta_time_prot_lvl)
266 	},
267 	{
268 		.name = "gpd.tee.cryptography.ecc",
269 		.prop_type = USER_TA_PROP_TYPE_BOOL,
270 		.data = &crypto_ecc_en,
271 		.len = sizeof(crypto_ecc_en)
272 	},
273 	{
274 		.name = "gpd.tee.trustedStorage.antiRollback.protectionLevel",
275 		.prop_type = USER_TA_PROP_TYPE_U32,
276 		.data = &ts_antiroll_prot_lvl,
277 		.len = sizeof(ts_antiroll_prot_lvl)
278 	},
279 	{
280 		.name = "gpd.tee.trustedos.implementation.version",
281 		.prop_type = USER_TA_PROP_TYPE_STRING,
282 		.data = trustedos_impl_version,
283 		.len = sizeof(trustedos_impl_version)
284 	},
285 	{
286 		.name = "gpd.tee.trustedos.implementation.binaryversion",
287 		.prop_type = USER_TA_PROP_TYPE_U32,
288 		.data = &trustedos_impl_bin_version,
289 		.len = sizeof(trustedos_impl_bin_version)
290 	},
291 	{
292 		.name = "gpd.tee.trustedos.manufacturer",
293 		.prop_type = USER_TA_PROP_TYPE_STRING,
294 		.data = trustedos_manufacturer,
295 		.len = sizeof(trustedos_manufacturer)
296 	},
297 	{
298 		.name = "gpd.tee.firmware.implementation.version",
299 		.prop_type = USER_TA_PROP_TYPE_STRING,
300 		.data = fw_impl_version,
301 		.len = sizeof(fw_impl_version)
302 	},
303 	{
304 		.name = "gpd.tee.firmware.implementation.binaryversion",
305 		.prop_type = USER_TA_PROP_TYPE_U32,
306 		.data = &fw_impl_bin_version,
307 		.len = sizeof(fw_impl_bin_version)
308 	},
309 	{
310 		.name = "gpd.tee.firmware.manufacturer",
311 		.prop_type = USER_TA_PROP_TYPE_STRING,
312 		.data = fw_manufacturer,
313 		.len = sizeof(fw_manufacturer)
314 	},
315 
316 	/*
317 	 * Following properties are processed directly in libutee:
318 	 *	gpd.tee.arith.maxBigIntSize
319 	 */
320 };
321 
322 __weak const struct tee_vendor_props vendor_props_client;
323 __weak const struct tee_vendor_props vendor_props_ta;
324 __weak const struct tee_vendor_props vendor_props_tee;
325 
326 static void get_prop_set(unsigned long prop_set,
327 			 const struct tee_props **props,
328 			 size_t *size,
329 			 const struct tee_props **vendor_props,
330 			 size_t *vendor_size)
331 {
332 	if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) {
333 		*props = tee_propset_client;
334 		*size = ARRAY_SIZE(tee_propset_client);
335 		*vendor_props = vendor_props_client.props;
336 		*vendor_size = vendor_props_client.len;
337 	} else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) {
338 		*props = tee_propset_ta;
339 		*size = ARRAY_SIZE(tee_propset_ta);
340 		*vendor_props = vendor_props_ta.props;
341 		*vendor_size = vendor_props_ta.len;
342 	} else if ((TEE_PropSetHandle)prop_set ==
343 		   TEE_PROPSET_TEE_IMPLEMENTATION) {
344 		*props = tee_propset_tee;
345 		*size = ARRAY_SIZE(tee_propset_tee);
346 		*vendor_props = vendor_props_tee.props;
347 		*vendor_size = vendor_props_tee.len;
348 	} else {
349 		*props = NULL;
350 		*size = 0;
351 		*vendor_props = NULL;
352 		*vendor_size = 0;
353 	}
354 }
355 
356 static const struct tee_props *get_prop_struct(unsigned long prop_set,
357 					       unsigned long index)
358 {
359 	const struct tee_props *props;
360 	const struct tee_props *vendor_props;
361 	size_t size;
362 	size_t vendor_size;
363 
364 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
365 
366 	if (index < size)
367 		return &(props[index]);
368 	index -= size;
369 
370 	if (index < vendor_size)
371 		return &(vendor_props[index]);
372 
373 	return NULL;
374 }
375 
376 /*
377  * prop_set is part of TEE_PROPSET_xxx
378  * index is the index in the Property Set to retrieve
379  * if name is not NULL, the name of "index" property is returned
380  * if buf is not NULL, the property is returned
381  */
382 TEE_Result syscall_get_property(unsigned long prop_set,
383 				unsigned long index,
384 				void *name, uint32_t *name_len,
385 				void *buf, uint32_t *blen,
386 				uint32_t *prop_type)
387 {
388 	struct tee_ta_session *sess;
389 	TEE_Result res;
390 	TEE_Result res2;
391 	const struct tee_props *prop;
392 	uint32_t klen;
393 	size_t klen_size;
394 	uint32_t elen;
395 
396 	prop = get_prop_struct(prop_set, index);
397 	if (!prop)
398 		return TEE_ERROR_ITEM_NOT_FOUND;
399 
400 	res = tee_ta_get_current_session(&sess);
401 	if (res != TEE_SUCCESS)
402 		return res;
403 
404 	/* Get the property type */
405 	if (prop_type) {
406 		res = tee_svc_copy_to_user(sess, prop_type, &prop->prop_type,
407 					   sizeof(*prop_type));
408 		if (res != TEE_SUCCESS)
409 			return res;
410 	}
411 
412 	/* Get the property */
413 	if (buf && blen) {
414 		res = tee_svc_copy_from_user(sess, &klen, blen, sizeof(klen));
415 		if (res != TEE_SUCCESS)
416 			return res;
417 
418 		if (prop->get_prop_func) {
419 			klen_size = klen;
420 			res = prop->get_prop_func(sess, buf, &klen_size);
421 			klen = klen_size;
422 			res2 = tee_svc_copy_to_user(sess, blen,
423 						    &klen, sizeof(*blen));
424 		} else {
425 			if (klen < prop->len)
426 				res = TEE_ERROR_SHORT_BUFFER;
427 			else
428 				res = tee_svc_copy_to_user(sess, buf,
429 							   prop->data,
430 							   prop->len);
431 			res2 = tee_svc_copy_to_user(sess, blen,
432 						    &prop->len, sizeof(*blen));
433 		}
434 		if (res2 != TEE_SUCCESS)
435 			return res2;
436 		if (res != TEE_SUCCESS)
437 			return res;
438 	}
439 
440 	/* Get the property name */
441 	if (name && name_len) {
442 		res = tee_svc_copy_from_user(sess, &klen,
443 					     name_len, sizeof(klen));
444 		if (res != TEE_SUCCESS)
445 			return res;
446 
447 		elen = strlen(prop->name) + 1;
448 
449 		if (klen < elen)
450 			res = TEE_ERROR_SHORT_BUFFER;
451 		else
452 			res = tee_svc_copy_to_user(sess, name,
453 						   prop->name, elen);
454 		res2 = tee_svc_copy_to_user(sess, name_len,
455 					    &elen, sizeof(*name_len));
456 		if (res2 != TEE_SUCCESS)
457 			return res2;
458 		if (res != TEE_SUCCESS)
459 			return res;
460 	}
461 
462 	return res;
463 }
464 
465 /*
466  * prop_set is part of TEE_PROPSET_xxx
467  */
468 TEE_Result syscall_get_property_name_to_index(unsigned long prop_set,
469 					      void *name,
470 					      unsigned long name_len,
471 					      uint32_t *index)
472 {
473 	TEE_Result res;
474 	struct tee_ta_session *sess;
475 	const struct tee_props *props;
476 	size_t size;
477 	const struct tee_props *vendor_props;
478 	size_t vendor_size;
479 	char *kname = 0;
480 	uint32_t i;
481 
482 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
483 	if (!props)
484 		return TEE_ERROR_ITEM_NOT_FOUND;
485 
486 	res = tee_ta_get_current_session(&sess);
487 	if (res != TEE_SUCCESS)
488 		goto out;
489 
490 	if (!name || !name_len) {
491 		res = TEE_ERROR_BAD_PARAMETERS;
492 		goto out;
493 	}
494 
495 	kname = malloc(name_len);
496 	if (!kname)
497 		return TEE_ERROR_OUT_OF_MEMORY;
498 	res = tee_svc_copy_from_user(sess, kname, name, name_len);
499 	if (res != TEE_SUCCESS)
500 		goto out;
501 	kname[name_len - 1] = 0;
502 
503 	res = TEE_ERROR_ITEM_NOT_FOUND;
504 	for (i = 0; i < size; i++) {
505 		if (!strcmp(kname, props[i].name)) {
506 			res = tee_svc_copy_to_user(sess, index, &i,
507 						   sizeof(*index));
508 			goto out;
509 		}
510 	}
511 	for (i = size; i < size + vendor_size; i++) {
512 		if (!strcmp(kname, vendor_props[i - size].name)) {
513 			res = tee_svc_copy_to_user(sess, index, &i,
514 						   sizeof(*index));
515 			goto out;
516 		}
517 	}
518 
519 out:
520 	free(kname);
521 	return res;
522 }
523 
524 static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up)
525 {
526 	size_t n;
527 	uint32_t types = up->types;
528 
529 	p->types = types;
530 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
531 		uintptr_t a = up->vals[n * 2];
532 		size_t b = up->vals[n * 2 + 1];
533 
534 		switch (TEE_PARAM_TYPE_GET(types, n)) {
535 		case TEE_PARAM_TYPE_MEMREF_INPUT:
536 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
537 		case TEE_PARAM_TYPE_MEMREF_INOUT:
538 			p->params[n].memref.buffer = (void *)a;
539 			p->params[n].memref.size = b;
540 			p->param_attr[n] = TEE_MATTR_VIRTUAL;
541 			break;
542 		case TEE_PARAM_TYPE_VALUE_INPUT:
543 		case TEE_PARAM_TYPE_VALUE_INOUT:
544 			p->params[n].value.a = a;
545 			p->params[n].value.b = b;
546 			break;
547 		default:
548 			p->params[n].value.a = 0;
549 			p->params[n].value.b = 0;
550 			break;
551 		}
552 	}
553 }
554 
555 /*
556  * TA invokes some TA with parameter.
557  * If some parameters are memory references:
558  * - either the memref is inside TA private RAM: TA is not allowed to expose
559  *   its private RAM: use a temporary memory buffer and copy the data.
560  * - or the memref is not in the TA private RAM:
561  *   - if the memref was mapped to the TA, TA is allowed to expose it.
562  *   - if so, converts memref virtual address into a physical address.
563  */
564 static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
565 				     struct tee_ta_session *called_sess,
566 				     struct utee_params *callee_params,
567 				     struct tee_ta_param *param,
568 				     void *tmp_buf_va[TEE_NUM_PARAMS],
569 				     tee_mm_entry_t **mm)
570 {
571 	size_t n;
572 	TEE_Result res;
573 	size_t req_mem = 0;
574 	size_t s;
575 	uint8_t *dst = 0;
576 	tee_paddr_t dst_pa, src_pa = 0;
577 	bool ta_private_memref[TEE_NUM_PARAMS];
578 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
579 	const uint32_t sec_ddr_attr = TEE_MATTR_CACHE_CACHED;
580 
581 	/* fill 'param' input struct with caller params description buffer */
582 	if (!callee_params) {
583 		memset(param, 0, sizeof(*param));
584 	} else {
585 		res = tee_mmu_check_access_rights(utc,
586 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
587 			(tee_uaddr_t)callee_params, sizeof(struct utee_params));
588 		if (res != TEE_SUCCESS)
589 			return res;
590 		utee_param_to_param(param, callee_params);
591 	}
592 
593 	if (called_sess && is_static_ta_ctx(called_sess->ctx)) {
594 		/*
595 		 * static TA, borrow the mapping of the calling
596 		 * during this call.
597 		 */
598 		called_sess->calling_sess = sess;
599 		return TEE_SUCCESS;
600 	}
601 
602 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
603 
604 		ta_private_memref[n] = false;
605 
606 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
607 		case TEE_PARAM_TYPE_MEMREF_INPUT:
608 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
609 		case TEE_PARAM_TYPE_MEMREF_INOUT:
610 			if (param->params[n].memref.buffer == NULL) {
611 				if (param->params[n].memref.size != 0)
612 					return TEE_ERROR_BAD_PARAMETERS;
613 				break;
614 			}
615 			/* uTA cannot expose its private memory */
616 			if (tee_mmu_is_vbuf_inside_ta_private(utc,
617 				    param->params[n].memref.buffer,
618 				    param->params[n].memref.size)) {
619 
620 				s = ROUNDUP(param->params[n].memref.size,
621 						sizeof(uint32_t));
622 				/* Check overflow */
623 				if (req_mem + s < req_mem)
624 					return TEE_ERROR_BAD_PARAMETERS;
625 				req_mem += s;
626 				ta_private_memref[n] = true;
627 				break;
628 			}
629 			if (tee_mmu_is_vbuf_intersect_ta_private(utc,
630 				    param->params[n].memref.buffer,
631 				    param->params[n].memref.size))
632 				return TEE_ERROR_BAD_PARAMETERS;
633 
634 			src_pa = virt_to_phys(param->params[n].memref.buffer);
635 			if (!src_pa)
636 				return TEE_ERROR_BAD_PARAMETERS;
637 
638 			param->param_attr[n] = tee_mmu_user_get_cache_attr(
639 				utc, (void *)param->params[n].memref.buffer);
640 
641 			param->params[n].memref.buffer = (void *)src_pa;
642 			break;
643 
644 		default:
645 			break;
646 		}
647 	}
648 
649 	if (req_mem == 0)
650 		return TEE_SUCCESS;
651 
652 	/* Allocate section in secure DDR */
653 	*mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem);
654 	if (*mm == NULL) {
655 		DMSG("tee_mm_alloc TEE_ERROR_GENERIC");
656 		return TEE_ERROR_GENERIC;
657 	}
658 	dst_pa = tee_mm_get_smem(*mm);
659 
660 	/* Get the virtual address for the section in secure DDR */
661 	dst = phys_to_virt(dst_pa, MEM_AREA_TA_RAM);
662 
663 	for (n = 0; n < 4; n++) {
664 
665 		if (ta_private_memref[n] == false)
666 			continue;
667 
668 		s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t));
669 
670 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
671 		case TEE_PARAM_TYPE_MEMREF_INPUT:
672 		case TEE_PARAM_TYPE_MEMREF_INOUT:
673 			if (param->params[n].memref.buffer != NULL) {
674 				res = tee_svc_copy_from_user(sess, dst,
675 						param->params[n].memref.buffer,
676 						param->params[n].memref.size);
677 				if (res != TEE_SUCCESS)
678 					return res;
679 				param->param_attr[n] = sec_ddr_attr;
680 				param->params[n].memref.buffer = (void *)dst_pa;
681 				tmp_buf_va[n] = dst;
682 				dst += s;
683 				dst_pa += s;
684 			}
685 			break;
686 
687 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
688 			if (param->params[n].memref.buffer != NULL) {
689 				param->param_attr[n] = sec_ddr_attr;
690 				param->params[n].memref.buffer = (void *)dst_pa;
691 				tmp_buf_va[n] = dst;
692 				dst += s;
693 				dst_pa += s;
694 			}
695 			break;
696 
697 		default:
698 			continue;
699 		}
700 	}
701 
702 	return TEE_SUCCESS;
703 }
704 
705 /*
706  * Back from execution of service: update parameters passed from TA:
707  * If some parameters were memory references:
708  * - either the memref was temporary: copy back data and update size
709  * - or it was the original TA memref: update only the size value.
710  */
711 static TEE_Result tee_svc_update_out_param(
712 		struct tee_ta_session *sess,
713 		struct tee_ta_session *called_sess,
714 		struct tee_ta_param *param,
715 		void *tmp_buf_va[TEE_NUM_PARAMS],
716 		struct utee_params *usr_param)
717 {
718 	size_t n;
719 	void *p;
720 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
721 	bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx);
722 
723 	tee_ta_set_current_session(sess);
724 
725 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
726 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
727 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
728 		case TEE_PARAM_TYPE_MEMREF_INOUT:
729 			p = (void *)(uintptr_t)usr_param->vals[n * 2];
730 
731 			/* outside TA private => memref is valid, update size */
732 			if (!tee_mmu_is_vbuf_inside_ta_private(utc, p,
733 					param->params[n].memref.size)) {
734 				usr_param->vals[n * 2 + 1] =
735 					param->params[n].memref.size;
736 				break;
737 			}
738 
739 			/*
740 			 * If we called a kernel TA the parameters are in shared
741 			 * memory and no copy is needed.
742 			 */
743 			if (have_private_mem_map &&
744 			    param->params[n].memref.size <=
745 			    usr_param->vals[n * 2 + 1]) {
746 				uint8_t *src = tmp_buf_va[n];
747 				TEE_Result res;
748 
749 				res = tee_svc_copy_to_user(sess, p, src,
750 						 param->params[n].memref.size);
751 				if (res != TEE_SUCCESS)
752 					return res;
753 
754 			}
755 			usr_param->vals[n * 2 + 1] =
756 				param->params[n].memref.size;
757 			break;
758 
759 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
760 		case TEE_PARAM_TYPE_VALUE_INOUT:
761 			usr_param->vals[n * 2] = param->params[n].value.a;
762 			usr_param->vals[n * 2 + 1] = param->params[n].value.b;
763 			break;
764 
765 		default:
766 			continue;
767 		}
768 	}
769 
770 	return TEE_SUCCESS;
771 }
772 
773 /* Called when a TA calls an OpenSession on another TA */
774 TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
775 			unsigned long cancel_req_to,
776 			struct utee_params *usr_param, uint32_t *ta_sess,
777 			uint32_t *ret_orig)
778 {
779 	TEE_Result res;
780 	uint32_t ret_o = TEE_ORIGIN_TEE;
781 	struct tee_ta_session *s = NULL;
782 	struct tee_ta_session *sess;
783 	tee_mm_entry_t *mm_param = NULL;
784 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
785 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
786 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
787 	void *tmp_buf_va[TEE_NUM_PARAMS];
788 	struct user_ta_ctx *utc;
789 
790 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
791 		res = TEE_ERROR_OUT_OF_MEMORY;
792 		goto out_free_only;
793 	}
794 
795 	memset(param, 0, sizeof(struct tee_ta_param));
796 
797 	res = tee_ta_get_current_session(&sess);
798 	if (res != TEE_SUCCESS)
799 		goto out_free_only;
800 	utc = to_user_ta_ctx(sess->ctx);
801 
802 	res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID));
803 	if (res != TEE_SUCCESS)
804 		goto function_exit;
805 
806 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
807 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
808 
809 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va,
810 				 &mm_param);
811 	if (res != TEE_SUCCESS)
812 		goto function_exit;
813 
814 	/*
815 	 * Find session of a multi session TA or a static TA
816 	 * In such a case, there is no need to ask the supplicant for the TA
817 	 * code
818 	 */
819 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
820 				  clnt_id, cancel_req_to, param);
821 	if (res != TEE_SUCCESS)
822 		goto function_exit;
823 
824 	res = tee_svc_update_out_param(sess, s, param, tmp_buf_va, usr_param);
825 
826 function_exit:
827 	tee_ta_set_current_session(sess);
828 	sess->calling_sess = NULL; /* clear eventual borrowed mapping */
829 	tee_mm_free(mm_param);
830 	if (res == TEE_SUCCESS)
831 		tee_svc_copy_kaddr_to_uref(sess, ta_sess, s);
832 	tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
833 
834 out_free_only:
835 	free(param);
836 	free(uuid);
837 	free(clnt_id);
838 	return res;
839 }
840 
841 TEE_Result syscall_close_ta_session(unsigned long ta_sess)
842 {
843 	TEE_Result res;
844 	struct tee_ta_session *sess;
845 	TEE_Identity clnt_id;
846 	struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess);
847 	struct user_ta_ctx *utc;
848 
849 	res = tee_ta_get_current_session(&sess);
850 	if (res != TEE_SUCCESS)
851 		return res;
852 	utc = to_user_ta_ctx(sess->ctx);
853 
854 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
855 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
856 
857 	tee_ta_set_current_session(NULL);
858 	res = tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
859 	tee_ta_set_current_session(sess);
860 	return res;
861 }
862 
863 TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
864 			unsigned long cancel_req_to, unsigned long cmd_id,
865 			struct utee_params *usr_param, uint32_t *ret_orig)
866 {
867 	TEE_Result res;
868 	TEE_Result res2;
869 	uint32_t ret_o = TEE_ORIGIN_TEE;
870 	struct tee_ta_param param = { 0 };
871 	TEE_Identity clnt_id;
872 	struct tee_ta_session *sess;
873 	struct tee_ta_session *called_sess;
874 	tee_mm_entry_t *mm_param = NULL;
875 	void *tmp_buf_va[TEE_NUM_PARAMS];
876 	struct user_ta_ctx *utc;
877 
878 	res = tee_ta_get_current_session(&sess);
879 	if (res != TEE_SUCCESS)
880 		return res;
881 	utc = to_user_ta_ctx(sess->ctx);
882 
883 	called_sess = tee_ta_get_session(
884 				(vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true,
885 				&utc->open_sessions);
886 	if (!called_sess)
887 		return TEE_ERROR_BAD_PARAMETERS;
888 
889 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
890 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
891 
892 	res = tee_svc_copy_param(sess, called_sess, usr_param, &param,
893 				 tmp_buf_va, &mm_param);
894 	if (res != TEE_SUCCESS)
895 		goto function_exit;
896 
897 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
898 				    cancel_req_to, cmd_id, &param);
899 
900 	res2 = tee_svc_update_out_param(sess, called_sess, &param, tmp_buf_va,
901 					usr_param);
902 	if (res2 != TEE_SUCCESS) {
903 		/*
904 		 * Spec for TEE_InvokeTACommand() says:
905 		 * "If the return origin is different from
906 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
907 		 * before it could reach the destination Trusted
908 		 * Application."
909 		 *
910 		 * But if we can't update params to the caller we have no
911 		 * choice we need to return some error to indicate that
912 		 * parameters aren't updated as expected.
913 		 */
914 		ret_o = TEE_ORIGIN_TEE;
915 		res = res2;
916 	}
917 
918 function_exit:
919 	tee_ta_set_current_session(sess);
920 	called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */
921 	tee_ta_put_session(called_sess);
922 	tee_mm_free(mm_param);
923 	if (ret_orig)
924 		tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
925 	return res;
926 }
927 
928 TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
929 				       size_t len)
930 {
931 	TEE_Result res;
932 	struct tee_ta_session *s;
933 
934 	res = tee_ta_get_current_session(&s);
935 	if (res != TEE_SUCCESS)
936 		return res;
937 
938 	return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags,
939 					   (tee_uaddr_t)buf, len);
940 }
941 
942 TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr,
943 				  const void *uaddr, size_t len)
944 {
945 	TEE_Result res;
946 	struct tee_ta_session *s;
947 
948 	if (sess == NULL) {
949 		res = tee_ta_get_current_session(&s);
950 		if (res != TEE_SUCCESS)
951 			return res;
952 	} else {
953 		s = sess;
954 		tee_ta_set_current_session(s);
955 	}
956 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
957 					TEE_MEMORY_ACCESS_READ |
958 					TEE_MEMORY_ACCESS_ANY_OWNER,
959 					(tee_uaddr_t)uaddr, len);
960 	if (res != TEE_SUCCESS)
961 		return res;
962 
963 	memcpy(kaddr, uaddr, len);
964 	return TEE_SUCCESS;
965 }
966 
967 TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr,
968 				const void *kaddr, size_t len)
969 {
970 	TEE_Result res;
971 	struct tee_ta_session *s;
972 
973 	if (sess == NULL) {
974 		res = tee_ta_get_current_session(&s);
975 		if (res != TEE_SUCCESS)
976 			return res;
977 	} else {
978 		s = sess;
979 		tee_ta_set_current_session(s);
980 	}
981 
982 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
983 					TEE_MEMORY_ACCESS_WRITE |
984 					TEE_MEMORY_ACCESS_ANY_OWNER,
985 					(tee_uaddr_t)uaddr, len);
986 	if (res != TEE_SUCCESS)
987 		return res;
988 
989 	memcpy(uaddr, kaddr, len);
990 	return TEE_SUCCESS;
991 }
992 
993 TEE_Result tee_svc_copy_kaddr_to_uref(struct tee_ta_session *sess,
994 			uint32_t *uref, void *kaddr)
995 {
996 	uint32_t ref = tee_svc_kaddr_to_uref(kaddr);
997 
998 	return tee_svc_copy_to_user(sess, uref, &ref, sizeof(ref));
999 }
1000 
1001 TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
1002 {
1003 	TEE_Result res;
1004 	struct tee_ta_session *s = NULL;
1005 	uint32_t c;
1006 
1007 	res = tee_ta_get_current_session(&s);
1008 	if (res != TEE_SUCCESS)
1009 		return res;
1010 
1011 	c = tee_ta_session_is_cancelled(s, NULL);
1012 
1013 	return tee_svc_copy_to_user(s, cancel, &c, sizeof(c));
1014 }
1015 
1016 TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
1017 {
1018 	TEE_Result res;
1019 	struct tee_ta_session *s = NULL;
1020 	uint32_t m;
1021 
1022 	res = tee_ta_get_current_session(&s);
1023 	if (res != TEE_SUCCESS)
1024 		return res;
1025 
1026 	m = s->cancel_mask;
1027 	s->cancel_mask = false;
1028 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
1029 }
1030 
1031 TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
1032 {
1033 	TEE_Result res;
1034 	struct tee_ta_session *s = NULL;
1035 	uint32_t m;
1036 
1037 	res = tee_ta_get_current_session(&s);
1038 	if (res != TEE_SUCCESS)
1039 		return res;
1040 
1041 	m = s->cancel_mask;
1042 	s->cancel_mask = true;
1043 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
1044 }
1045 
1046 TEE_Result syscall_wait(unsigned long timeout)
1047 {
1048 	TEE_Result res = TEE_SUCCESS;
1049 	uint32_t mytime = 0;
1050 	struct tee_ta_session *s;
1051 	TEE_Time base_time;
1052 	TEE_Time current_time;
1053 
1054 	res = tee_ta_get_current_session(&s);
1055 	if (res != TEE_SUCCESS)
1056 		return res;
1057 
1058 	res = tee_time_get_sys_time(&base_time);
1059 	if (res != TEE_SUCCESS)
1060 		return res;
1061 
1062 	while (true) {
1063 		res = tee_time_get_sys_time(&current_time);
1064 		if (res != TEE_SUCCESS)
1065 			return res;
1066 
1067 		if (tee_ta_session_is_cancelled(s, &current_time))
1068 			return TEE_ERROR_CANCEL;
1069 
1070 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1071 		    (int)current_time.millis - (int)base_time.millis;
1072 		if (mytime >= timeout)
1073 			return TEE_SUCCESS;
1074 
1075 		tee_time_wait(timeout - mytime);
1076 	}
1077 
1078 	return res;
1079 }
1080 
1081 TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1082 {
1083 	TEE_Result res, res2;
1084 	struct tee_ta_session *s = NULL;
1085 	TEE_Time t;
1086 
1087 	res = tee_ta_get_current_session(&s);
1088 	if (res != TEE_SUCCESS)
1089 		return res;
1090 
1091 	switch (cat) {
1092 	case UTEE_TIME_CAT_SYSTEM:
1093 		res = tee_time_get_sys_time(&t);
1094 		break;
1095 	case UTEE_TIME_CAT_TA_PERSISTENT:
1096 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1097 		break;
1098 	case UTEE_TIME_CAT_REE:
1099 		res = tee_time_get_ree_time(&t);
1100 		break;
1101 	default:
1102 		res = TEE_ERROR_BAD_PARAMETERS;
1103 		break;
1104 	}
1105 
1106 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1107 		res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t));
1108 		if (res2 != TEE_SUCCESS)
1109 			res = res2;
1110 	}
1111 
1112 	return res;
1113 }
1114 
1115 TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1116 {
1117 	TEE_Result res;
1118 	struct tee_ta_session *s = NULL;
1119 	TEE_Time t;
1120 
1121 	res = tee_ta_get_current_session(&s);
1122 	if (res != TEE_SUCCESS)
1123 		return res;
1124 
1125 	res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t));
1126 	if (res != TEE_SUCCESS)
1127 		return res;
1128 
1129 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1130 }
1131