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