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