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