xref: /optee_os/core/tee/tee_svc.c (revision b96514926b8ec7d98c36c4943df452b73bcc04a4)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
4b0104773SPascal Brand  */
5b0104773SPascal Brand #include <kernel/chip_services.h>
642fb5b2eSEtienne Carriere #include <kernel/pseudo_ta.h>
770b61310SJerome Forissier #include <kernel/tee_common.h>
870b61310SJerome Forissier #include <kernel/tee_common_otp.h>
970b61310SJerome Forissier #include <kernel/tee_ta_manager.h>
1070b61310SJerome Forissier #include <kernel/tee_time.h>
1170b61310SJerome Forissier #include <kernel/trace_ta.h>
1270b61310SJerome Forissier #include <mm/core_memprot.h>
130dcfa568SJens Wiklander #include <mm/mobj.h>
1470b61310SJerome Forissier #include <mm/tee_mm.h>
1570b61310SJerome Forissier #include <mm/tee_mmu.h>
1670b61310SJerome Forissier #include <stdlib_ext.h>
1770b61310SJerome Forissier #include <tee_api_types.h>
1870b61310SJerome Forissier #include <tee/tee_cryp_utl.h>
1970b61310SJerome Forissier #include <tee/tee_svc.h>
2070b61310SJerome Forissier #include <trace.h>
2170b61310SJerome Forissier #include <user_ta_header.h>
2270b61310SJerome Forissier #include <utee_types.h>
2370b61310SJerome Forissier #include <util.h>
24b0104773SPascal Brand 
25e86f1266SJens Wiklander vaddr_t tee_svc_uref_base;
26e86f1266SJens Wiklander 
273276098dSJerome Forissier void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused)
28b0104773SPascal Brand {
297c876f12SPascal Brand #ifdef CFG_TEE_CORE_TA_TRACE
30b0104773SPascal Brand 	char *kbuf;
31b0104773SPascal Brand 
32b0104773SPascal Brand 	if (len == 0)
33b0104773SPascal Brand 		return;
34b0104773SPascal Brand 
3589a3e9feSJens Wiklander 	kbuf = malloc(len + 1);
36b0104773SPascal Brand 	if (kbuf == NULL)
37b0104773SPascal Brand 		return;
38b0104773SPascal Brand 
393095f61eSJerome Forissier 	if (tee_svc_copy_from_user(kbuf, buf, len) == TEE_SUCCESS) {
403095f61eSJerome Forissier 		kbuf[len] = '\0';
413095f61eSJerome Forissier 		trace_ext_puts(kbuf);
423095f61eSJerome Forissier 	}
43b0104773SPascal Brand 
4470b61310SJerome Forissier 	free_wipe(kbuf);
457c876f12SPascal Brand #endif
46b0104773SPascal Brand }
47b0104773SPascal Brand 
48453a5030SJerome Forissier TEE_Result syscall_not_supported(void)
49197d17e7SSY Chiu {
50197d17e7SSY Chiu 	return TEE_ERROR_NOT_SUPPORTED;
51197d17e7SSY Chiu }
52197d17e7SSY Chiu 
53ab35d7adSCedric Chaumont /* Configuration properties */
54ab35d7adSCedric Chaumont /* API implementation version */
55ab35d7adSCedric Chaumont static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION);
56ab35d7adSCedric Chaumont 
57ab35d7adSCedric Chaumont /* Implementation description (implementation-dependent) */
58ab35d7adSCedric Chaumont static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR);
59ab35d7adSCedric Chaumont 
60b0104773SPascal Brand /*
61ab35d7adSCedric Chaumont  * TA persistent time protection level
62ab35d7adSCedric Chaumont  * 100: Persistent time based on an REE-controlled real-time clock
63ab35d7adSCedric Chaumont  * and on the TEE Trusted Storage for the storage of origins (default).
64ab35d7adSCedric Chaumont  * 1000: Persistent time based on a TEE-controlled real-time clock
65ab35d7adSCedric Chaumont  * and the TEE Trusted Storage.
66ab35d7adSCedric Chaumont  * The real-time clock MUST be out of reach of software attacks
67ab35d7adSCedric Chaumont  * from the REE.
68ab35d7adSCedric Chaumont  */
69b0104773SPascal Brand static const uint32_t ta_time_prot_lvl = 100;
70ab35d7adSCedric Chaumont 
7164a5011eSPascal Brand /* Elliptic Curve Cryptographic support */
7264a5011eSPascal Brand #ifdef CFG_CRYPTO_ECC
73d5d50c3cSJens Wiklander static const bool crypto_ecc_en = 1;
7464a5011eSPascal Brand #else
75d5d50c3cSJens Wiklander static const bool crypto_ecc_en;
7664a5011eSPascal Brand #endif
7764a5011eSPascal Brand 
78ab35d7adSCedric Chaumont /*
79ab35d7adSCedric Chaumont  * Trusted storage anti rollback protection level
80ab35d7adSCedric Chaumont  * 0 (or missing): No antirollback protection (default)
81ab35d7adSCedric Chaumont  * 100: Antirollback enforced at REE level
82ab35d7adSCedric Chaumont  * 1000: Antirollback TEE-controlled hardware
83ab35d7adSCedric Chaumont  */
84b4b1a20cSJens Wiklander #ifdef CFG_RPMB_FS
85b4b1a20cSJens Wiklander static const uint32_t ts_antiroll_prot_lvl = 1000;
86b4b1a20cSJens Wiklander #else
87ab35d7adSCedric Chaumont static const uint32_t ts_antiroll_prot_lvl;
88b4b1a20cSJens Wiklander #endif
89ab35d7adSCedric Chaumont 
90ab35d7adSCedric Chaumont /* Trusted OS implementation version */
914bf425c1SJens Wiklander static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION);
92ab35d7adSCedric Chaumont 
93ab35d7adSCedric Chaumont /* Trusted OS implementation version (binary value) */
94ab35d7adSCedric Chaumont static const uint32_t trustedos_impl_bin_version; /* 0 by default */
95ab35d7adSCedric Chaumont 
96ab35d7adSCedric Chaumont /* Trusted OS implementation manufacturer name */
97ab35d7adSCedric Chaumont static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER);
98ab35d7adSCedric Chaumont 
99ab35d7adSCedric Chaumont /* Trusted firmware version */
100ab35d7adSCedric Chaumont static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION);
101ab35d7adSCedric Chaumont 
102ab35d7adSCedric Chaumont /* Trusted firmware version (binary value) */
103ab35d7adSCedric Chaumont static const uint32_t fw_impl_bin_version; /* 0 by default */
104ab35d7adSCedric Chaumont 
105ab35d7adSCedric Chaumont /* Trusted firmware manufacturer name */
106ab35d7adSCedric Chaumont static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER);
107ab35d7adSCedric Chaumont 
108eebf7990SJens Wiklander static TEE_Result get_prop_tee_dev_id(struct tee_ta_session *sess __unused,
109ff857a3aSPascal Brand 				      void *buf, size_t *blen)
110ab35d7adSCedric Chaumont {
111b0104773SPascal Brand 	TEE_Result res;
112b0104773SPascal Brand 	TEE_UUID uuid;
113ab35d7adSCedric Chaumont 	const size_t nslen = 5;
11464a5011eSPascal Brand 	uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
115ab35d7adSCedric Chaumont 	    'O', 'P', 'T', 'E', 'E' };
116b0104773SPascal Brand 
117ff857a3aSPascal Brand 	if (*blen < sizeof(uuid)) {
118ff857a3aSPascal Brand 		*blen = sizeof(uuid);
119b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
120ff857a3aSPascal Brand 	}
121ff857a3aSPascal Brand 	*blen = sizeof(uuid);
122b0104773SPascal Brand 
12364a5011eSPascal Brand 	if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen))
124b0104773SPascal Brand 		return TEE_ERROR_BAD_STATE;
125b0104773SPascal Brand 
12664a5011eSPascal Brand 	res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data),
12764a5011eSPascal Brand 				    (uint8_t *)&uuid, sizeof(uuid));
128b0104773SPascal Brand 	if (res != TEE_SUCCESS)
129b0104773SPascal Brand 		return TEE_ERROR_BAD_STATE;
130b0104773SPascal Brand 
131b0104773SPascal Brand 	/*
132b0104773SPascal Brand 	 * Changes the random value into and UUID as specifiec
133b0104773SPascal Brand 	 * in RFC 4122. The magic values are from the example
134b0104773SPascal Brand 	 * code in the RFC.
135b0104773SPascal Brand 	 *
136b0104773SPascal Brand 	 * TEE_UUID is defined slightly different from the RFC,
137b0104773SPascal Brand 	 * but close enough for our purpose.
138b0104773SPascal Brand 	 */
139b0104773SPascal Brand 
140b0104773SPascal Brand 	uuid.timeHiAndVersion &= 0x0fff;
141b0104773SPascal Brand 	uuid.timeHiAndVersion |= 5 << 12;
142b0104773SPascal Brand 
143b0104773SPascal Brand 	/* uuid.clock_seq_hi_and_reserved in the RFC */
144b0104773SPascal Brand 	uuid.clockSeqAndNode[0] &= 0x3f;
145b0104773SPascal Brand 	uuid.clockSeqAndNode[0] |= 0x80;
146b0104773SPascal Brand 
147eebf7990SJens Wiklander 	return tee_svc_copy_to_user(buf, &uuid, sizeof(TEE_UUID));
148b0104773SPascal Brand }
149b0104773SPascal Brand 
150eebf7990SJens Wiklander static TEE_Result get_prop_tee_sys_time_prot_level(
151eebf7990SJens Wiklander 			struct tee_ta_session *sess __unused,
152ff857a3aSPascal Brand 			void *buf, size_t *blen)
1532cdaaacbSJerome Forissier {
1542cdaaacbSJerome Forissier 	uint32_t prot;
1552cdaaacbSJerome Forissier 
156ff857a3aSPascal Brand 	if (*blen < sizeof(prot)) {
157ff857a3aSPascal Brand 		*blen = sizeof(prot);
1582cdaaacbSJerome Forissier 		return TEE_ERROR_SHORT_BUFFER;
159ff857a3aSPascal Brand 	}
160ff857a3aSPascal Brand 	*blen = sizeof(prot);
1612cdaaacbSJerome Forissier 	prot = tee_time_get_sys_time_protection_level();
162a07c12b2SJens Wiklander 	return tee_svc_copy_to_user(buf, &prot, sizeof(prot));
1632cdaaacbSJerome Forissier }
1642cdaaacbSJerome Forissier 
165a07c12b2SJens Wiklander static TEE_Result get_prop_client_id(struct tee_ta_session *sess __unused,
166ff857a3aSPascal Brand 				     void *buf, size_t *blen)
16764a5011eSPascal Brand {
168ff857a3aSPascal Brand 	if (*blen < sizeof(TEE_Identity)) {
169ff857a3aSPascal Brand 		*blen = sizeof(TEE_Identity);
170b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
171ff857a3aSPascal Brand 	}
172ff857a3aSPascal Brand 	*blen = sizeof(TEE_Identity);
173eebf7990SJens Wiklander 	return tee_svc_copy_to_user(buf, &sess->clnt_id, sizeof(TEE_Identity));
17464a5011eSPascal Brand }
17537d6ae92SPascal Brand 
17664a5011eSPascal Brand static TEE_Result get_prop_ta_app_id(struct tee_ta_session *sess,
177ff857a3aSPascal Brand 				     void *buf, size_t *blen)
17864a5011eSPascal Brand {
179ff857a3aSPascal Brand 	if (*blen < sizeof(TEE_UUID)) {
180ff857a3aSPascal Brand 		*blen = sizeof(TEE_UUID);
181b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
182ff857a3aSPascal Brand 	}
183ff857a3aSPascal Brand 	*blen = sizeof(TEE_UUID);
184eebf7990SJens Wiklander 	return tee_svc_copy_to_user(buf, &sess->ctx->uuid, sizeof(TEE_UUID));
18564a5011eSPascal Brand }
18664a5011eSPascal Brand 
18764a5011eSPascal Brand /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */
18864a5011eSPascal Brand const struct tee_props tee_propset_client[] = {
18964a5011eSPascal Brand 	{
19064a5011eSPascal Brand 		.name = "gpd.client.identity",
19164a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_IDENTITY,
19264a5011eSPascal Brand 		.get_prop_func = get_prop_client_id
19364a5011eSPascal Brand 	},
19464a5011eSPascal Brand };
19564a5011eSPascal Brand 
19664a5011eSPascal Brand /* Properties of the set TEE_PROPSET_CURRENT_TA */
19764a5011eSPascal Brand const struct tee_props tee_propset_ta[] = {
19864a5011eSPascal Brand 	{
19964a5011eSPascal Brand 		.name = "gpd.ta.appID",
20064a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_UUID,
20164a5011eSPascal Brand 		.get_prop_func = get_prop_ta_app_id
20264a5011eSPascal Brand 	},
20364a5011eSPascal Brand 
20464a5011eSPascal Brand 	/*
20564a5011eSPascal Brand 	 * Following properties are processed directly in libutee:
20664a5011eSPascal Brand 	 *	TA_PROP_STR_SINGLE_INSTANCE
20764a5011eSPascal Brand 	 *	TA_PROP_STR_MULTI_SESSION
20864a5011eSPascal Brand 	 *	TA_PROP_STR_KEEP_ALIVE
20964a5011eSPascal Brand 	 *	TA_PROP_STR_DATA_SIZE
21064a5011eSPascal Brand 	 *	TA_PROP_STR_STACK_SIZE
21164a5011eSPascal Brand 	 *	TA_PROP_STR_VERSION
21264a5011eSPascal Brand 	 *	TA_PROP_STR_DESCRIPTION
21364a5011eSPascal Brand 	 *	USER_TA_PROP_TYPE_STRING,
21464a5011eSPascal Brand 	 *	TA_DESCRIPTION
21564a5011eSPascal Brand 	 */
21664a5011eSPascal Brand };
21764a5011eSPascal Brand 
21864a5011eSPascal Brand /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */
21964a5011eSPascal Brand const struct tee_props tee_propset_tee[] = {
22064a5011eSPascal Brand 	{
22164a5011eSPascal Brand 		.name = "gpd.tee.apiversion",
22264a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
22364a5011eSPascal Brand 		.data = api_vers,
22464a5011eSPascal Brand 		.len = sizeof(api_vers),
22564a5011eSPascal Brand 	},
22664a5011eSPascal Brand 	{
22764a5011eSPascal Brand 		.name = "gpd.tee.description",
22864a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
22964a5011eSPascal Brand 		.data = descr, .len = sizeof(descr)
23064a5011eSPascal Brand 	},
23164a5011eSPascal Brand 	{
23264a5011eSPascal Brand 		.name = "gpd.tee.deviceID",
23364a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_UUID,
23464a5011eSPascal Brand 		.get_prop_func = get_prop_tee_dev_id
23564a5011eSPascal Brand 	},
23664a5011eSPascal Brand 	{
23764a5011eSPascal Brand 		.name = "gpd.tee.systemTime.protectionLevel",
23864a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
23964a5011eSPascal Brand 		.get_prop_func = get_prop_tee_sys_time_prot_level
24064a5011eSPascal Brand 	},
24164a5011eSPascal Brand 	{
24264a5011eSPascal Brand 		.name = "gpd.tee.TAPersistentTime.protectionLevel",
24364a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
24464a5011eSPascal Brand 		.data = &ta_time_prot_lvl,
24564a5011eSPascal Brand 		.len = sizeof(ta_time_prot_lvl)
24664a5011eSPascal Brand 	},
24764a5011eSPascal Brand 	{
24864a5011eSPascal Brand 		.name = "gpd.tee.cryptography.ecc",
24964a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_BOOL,
25064a5011eSPascal Brand 		.data = &crypto_ecc_en,
25164a5011eSPascal Brand 		.len = sizeof(crypto_ecc_en)
25264a5011eSPascal Brand 	},
25364a5011eSPascal Brand 	{
25464a5011eSPascal Brand 		.name = "gpd.tee.trustedStorage.antiRollback.protectionLevel",
25564a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
25664a5011eSPascal Brand 		.data = &ts_antiroll_prot_lvl,
25764a5011eSPascal Brand 		.len = sizeof(ts_antiroll_prot_lvl)
25864a5011eSPascal Brand 	},
25964a5011eSPascal Brand 	{
26064a5011eSPascal Brand 		.name = "gpd.tee.trustedos.implementation.version",
26164a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
26264a5011eSPascal Brand 		.data = trustedos_impl_version,
26364a5011eSPascal Brand 		.len = sizeof(trustedos_impl_version)
26464a5011eSPascal Brand 	},
26564a5011eSPascal Brand 	{
26664a5011eSPascal Brand 		.name = "gpd.tee.trustedos.implementation.binaryversion",
26764a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
26864a5011eSPascal Brand 		.data = &trustedos_impl_bin_version,
26964a5011eSPascal Brand 		.len = sizeof(trustedos_impl_bin_version)
27064a5011eSPascal Brand 	},
27164a5011eSPascal Brand 	{
27264a5011eSPascal Brand 		.name = "gpd.tee.trustedos.manufacturer",
27364a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
27464a5011eSPascal Brand 		.data = trustedos_manufacturer,
27564a5011eSPascal Brand 		.len = sizeof(trustedos_manufacturer)
27664a5011eSPascal Brand 	},
27764a5011eSPascal Brand 	{
27864a5011eSPascal Brand 		.name = "gpd.tee.firmware.implementation.version",
27964a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
28064a5011eSPascal Brand 		.data = fw_impl_version,
28164a5011eSPascal Brand 		.len = sizeof(fw_impl_version)
28264a5011eSPascal Brand 	},
28364a5011eSPascal Brand 	{
28464a5011eSPascal Brand 		.name = "gpd.tee.firmware.implementation.binaryversion",
28564a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
28664a5011eSPascal Brand 		.data = &fw_impl_bin_version,
28764a5011eSPascal Brand 		.len = sizeof(fw_impl_bin_version)
28864a5011eSPascal Brand 	},
28964a5011eSPascal Brand 	{
29064a5011eSPascal Brand 		.name = "gpd.tee.firmware.manufacturer",
29164a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
29264a5011eSPascal Brand 		.data = fw_manufacturer,
29364a5011eSPascal Brand 		.len = sizeof(fw_manufacturer)
29464a5011eSPascal Brand 	},
29564a5011eSPascal Brand 
29664a5011eSPascal Brand 	/*
29764a5011eSPascal Brand 	 * Following properties are processed directly in libutee:
29864a5011eSPascal Brand 	 *	gpd.tee.arith.maxBigIntSize
29964a5011eSPascal Brand 	 */
30064a5011eSPascal Brand };
30164a5011eSPascal Brand 
30241d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_client;
30341d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_ta;
30441d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_tee;
30541d71430SPascal Brand 
30664a5011eSPascal Brand static void get_prop_set(unsigned long prop_set,
30764a5011eSPascal Brand 			 const struct tee_props **props,
30841d71430SPascal Brand 			 size_t *size,
30941d71430SPascal Brand 			 const struct tee_props **vendor_props,
31041d71430SPascal Brand 			 size_t *vendor_size)
31164a5011eSPascal Brand {
31264a5011eSPascal Brand 	if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) {
31364a5011eSPascal Brand 		*props = tee_propset_client;
31464a5011eSPascal Brand 		*size = ARRAY_SIZE(tee_propset_client);
31541d71430SPascal Brand 		*vendor_props = vendor_props_client.props;
31641d71430SPascal Brand 		*vendor_size = vendor_props_client.len;
31764a5011eSPascal Brand 	} else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) {
31864a5011eSPascal Brand 		*props = tee_propset_ta;
31964a5011eSPascal Brand 		*size = ARRAY_SIZE(tee_propset_ta);
32041d71430SPascal Brand 		*vendor_props = vendor_props_ta.props;
32141d71430SPascal Brand 		*vendor_size = vendor_props_ta.len;
32264a5011eSPascal Brand 	} else if ((TEE_PropSetHandle)prop_set ==
32364a5011eSPascal Brand 		   TEE_PROPSET_TEE_IMPLEMENTATION) {
32464a5011eSPascal Brand 		*props = tee_propset_tee;
32564a5011eSPascal Brand 		*size = ARRAY_SIZE(tee_propset_tee);
32641d71430SPascal Brand 		*vendor_props = vendor_props_tee.props;
32741d71430SPascal Brand 		*vendor_size = vendor_props_tee.len;
32864a5011eSPascal Brand 	} else {
32941d71430SPascal Brand 		*props = NULL;
33064a5011eSPascal Brand 		*size = 0;
33141d71430SPascal Brand 		*vendor_props = NULL;
33241d71430SPascal Brand 		*vendor_size = 0;
33364a5011eSPascal Brand 	}
33464a5011eSPascal Brand }
33564a5011eSPascal Brand 
33664a5011eSPascal Brand static const struct tee_props *get_prop_struct(unsigned long prop_set,
33764a5011eSPascal Brand 					       unsigned long index)
33864a5011eSPascal Brand {
33964a5011eSPascal Brand 	const struct tee_props *props;
34041d71430SPascal Brand 	const struct tee_props *vendor_props;
34141d71430SPascal Brand 	size_t size;
34241d71430SPascal Brand 	size_t vendor_size;
34364a5011eSPascal Brand 
34441d71430SPascal Brand 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
34564a5011eSPascal Brand 
34641d71430SPascal Brand 	if (index < size)
34764a5011eSPascal Brand 		return &(props[index]);
34841d71430SPascal Brand 	index -= size;
34941d71430SPascal Brand 
35041d71430SPascal Brand 	if (index < vendor_size)
35141d71430SPascal Brand 		return &(vendor_props[index]);
35241d71430SPascal Brand 
35364a5011eSPascal Brand 	return NULL;
35464a5011eSPascal Brand }
35564a5011eSPascal Brand 
35664a5011eSPascal Brand /*
35764a5011eSPascal Brand  * prop_set is part of TEE_PROPSET_xxx
35864a5011eSPascal Brand  * index is the index in the Property Set to retrieve
35964a5011eSPascal Brand  * if name is not NULL, the name of "index" property is returned
36064a5011eSPascal Brand  * if buf is not NULL, the property is returned
36164a5011eSPascal Brand  */
36264a5011eSPascal Brand TEE_Result syscall_get_property(unsigned long prop_set,
36364a5011eSPascal Brand 				unsigned long index,
36464a5011eSPascal Brand 				void *name, uint32_t *name_len,
36564a5011eSPascal Brand 				void *buf, uint32_t *blen,
36664a5011eSPascal Brand 				uint32_t *prop_type)
36764a5011eSPascal Brand {
36864a5011eSPascal Brand 	struct tee_ta_session *sess;
36964a5011eSPascal Brand 	TEE_Result res;
37064a5011eSPascal Brand 	TEE_Result res2;
37164a5011eSPascal Brand 	const struct tee_props *prop;
37264a5011eSPascal Brand 	uint32_t klen;
373ff857a3aSPascal Brand 	size_t klen_size;
37464a5011eSPascal Brand 	uint32_t elen;
37564a5011eSPascal Brand 
37664a5011eSPascal Brand 	prop = get_prop_struct(prop_set, index);
37764a5011eSPascal Brand 	if (!prop)
37864a5011eSPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
37964a5011eSPascal Brand 
38064a5011eSPascal Brand 	res = tee_ta_get_current_session(&sess);
38164a5011eSPascal Brand 	if (res != TEE_SUCCESS)
38264a5011eSPascal Brand 		return res;
38364a5011eSPascal Brand 
384ff857a3aSPascal Brand 	/* Get the property type */
385ff857a3aSPascal Brand 	if (prop_type) {
386eebf7990SJens Wiklander 		res = tee_svc_copy_to_user(prop_type, &prop->prop_type,
387ff857a3aSPascal Brand 					   sizeof(*prop_type));
388ff857a3aSPascal Brand 		if (res != TEE_SUCCESS)
389ff857a3aSPascal Brand 			return res;
390ff857a3aSPascal Brand 	}
391ff857a3aSPascal Brand 
39264a5011eSPascal Brand 	/* Get the property */
39364a5011eSPascal Brand 	if (buf && blen) {
394eebf7990SJens Wiklander 		res = tee_svc_copy_from_user(&klen, blen, sizeof(klen));
39564a5011eSPascal Brand 		if (res != TEE_SUCCESS)
39664a5011eSPascal Brand 			return res;
39764a5011eSPascal Brand 
39864a5011eSPascal Brand 		if (prop->get_prop_func) {
399ff857a3aSPascal Brand 			klen_size = klen;
400ff857a3aSPascal Brand 			res = prop->get_prop_func(sess, buf, &klen_size);
401ff857a3aSPascal Brand 			klen = klen_size;
402eebf7990SJens Wiklander 			res2 = tee_svc_copy_to_user(blen, &klen, sizeof(*blen));
40364a5011eSPascal Brand 		} else {
40464a5011eSPascal Brand 			if (klen < prop->len)
40564a5011eSPascal Brand 				res = TEE_ERROR_SHORT_BUFFER;
40664a5011eSPascal Brand 			else
407eebf7990SJens Wiklander 				res = tee_svc_copy_to_user(buf, prop->data,
40864a5011eSPascal Brand 							   prop->len);
409eebf7990SJens Wiklander 			res2 = tee_svc_copy_to_user(blen, &prop->len,
410eebf7990SJens Wiklander 						    sizeof(*blen));
411ff857a3aSPascal Brand 		}
41264a5011eSPascal Brand 		if (res2 != TEE_SUCCESS)
41364a5011eSPascal Brand 			return res2;
41464a5011eSPascal Brand 		if (res != TEE_SUCCESS)
41564a5011eSPascal Brand 			return res;
41664a5011eSPascal Brand 	}
41764a5011eSPascal Brand 
41864a5011eSPascal Brand 	/* Get the property name */
41964a5011eSPascal Brand 	if (name && name_len) {
420eebf7990SJens Wiklander 		res = tee_svc_copy_from_user(&klen, name_len, sizeof(klen));
42164a5011eSPascal Brand 		if (res != TEE_SUCCESS)
42264a5011eSPascal Brand 			return res;
42364a5011eSPascal Brand 
42464a5011eSPascal Brand 		elen = strlen(prop->name) + 1;
42564a5011eSPascal Brand 
42664a5011eSPascal Brand 		if (klen < elen)
42764a5011eSPascal Brand 			res = TEE_ERROR_SHORT_BUFFER;
42864a5011eSPascal Brand 		else
429eebf7990SJens Wiklander 			res = tee_svc_copy_to_user(name, prop->name, elen);
430eebf7990SJens Wiklander 		res2 = tee_svc_copy_to_user(name_len, &elen, sizeof(*name_len));
43164a5011eSPascal Brand 		if (res2 != TEE_SUCCESS)
43264a5011eSPascal Brand 			return res2;
43364a5011eSPascal Brand 		if (res != TEE_SUCCESS)
43464a5011eSPascal Brand 			return res;
43564a5011eSPascal Brand 	}
43664a5011eSPascal Brand 
43764a5011eSPascal Brand 	return res;
43864a5011eSPascal Brand }
43964a5011eSPascal Brand 
44064a5011eSPascal Brand /*
44164a5011eSPascal Brand  * prop_set is part of TEE_PROPSET_xxx
44264a5011eSPascal Brand  */
44364a5011eSPascal Brand TEE_Result syscall_get_property_name_to_index(unsigned long prop_set,
44464a5011eSPascal Brand 					      void *name,
44564a5011eSPascal Brand 					      unsigned long name_len,
44664a5011eSPascal Brand 					      uint32_t *index)
44764a5011eSPascal Brand {
44864a5011eSPascal Brand 	TEE_Result res;
44964a5011eSPascal Brand 	struct tee_ta_session *sess;
45064a5011eSPascal Brand 	const struct tee_props *props;
45141d71430SPascal Brand 	size_t size;
45241d71430SPascal Brand 	const struct tee_props *vendor_props;
45341d71430SPascal Brand 	size_t vendor_size;
45464a5011eSPascal Brand 	char *kname = 0;
45564a5011eSPascal Brand 	uint32_t i;
45664a5011eSPascal Brand 
45741d71430SPascal Brand 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
45864a5011eSPascal Brand 	if (!props)
45964a5011eSPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
46064a5011eSPascal Brand 
46164a5011eSPascal Brand 	res = tee_ta_get_current_session(&sess);
46264a5011eSPascal Brand 	if (res != TEE_SUCCESS)
46364a5011eSPascal Brand 		goto out;
46464a5011eSPascal Brand 
46564a5011eSPascal Brand 	if (!name || !name_len) {
46664a5011eSPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
46764a5011eSPascal Brand 		goto out;
46864a5011eSPascal Brand 	}
46964a5011eSPascal Brand 
47064a5011eSPascal Brand 	kname = malloc(name_len);
47164a5011eSPascal Brand 	if (!kname)
47264a5011eSPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
473eebf7990SJens Wiklander 	res = tee_svc_copy_from_user(kname, name, name_len);
47464a5011eSPascal Brand 	if (res != TEE_SUCCESS)
47564a5011eSPascal Brand 		goto out;
47664a5011eSPascal Brand 	kname[name_len - 1] = 0;
47764a5011eSPascal Brand 
47864a5011eSPascal Brand 	res = TEE_ERROR_ITEM_NOT_FOUND;
47964a5011eSPascal Brand 	for (i = 0; i < size; i++) {
48064a5011eSPascal Brand 		if (!strcmp(kname, props[i].name)) {
481eebf7990SJens Wiklander 			res = tee_svc_copy_to_user(index, &i, sizeof(*index));
48241d71430SPascal Brand 			goto out;
48341d71430SPascal Brand 		}
48441d71430SPascal Brand 	}
48541d71430SPascal Brand 	for (i = size; i < size + vendor_size; i++) {
48641d71430SPascal Brand 		if (!strcmp(kname, vendor_props[i - size].name)) {
487eebf7990SJens Wiklander 			res = tee_svc_copy_to_user(index, &i, sizeof(*index));
48841d71430SPascal Brand 			goto out;
48964a5011eSPascal Brand 		}
49064a5011eSPascal Brand 	}
49164a5011eSPascal Brand 
49264a5011eSPascal Brand out:
49370b61310SJerome Forissier 	free_wipe(kname);
49464a5011eSPascal Brand 	return res;
49564a5011eSPascal Brand }
49664a5011eSPascal Brand 
497d5c5b0b7SJens Wiklander static TEE_Result utee_param_to_param(struct user_ta_ctx *utc,
498d5c5b0b7SJens Wiklander 				      struct tee_ta_param *p,
499d5c5b0b7SJens Wiklander 				      struct utee_params *up)
500e86f1266SJens Wiklander {
501e86f1266SJens Wiklander 	size_t n;
502e86f1266SJens Wiklander 	uint32_t types = up->types;
503e86f1266SJens Wiklander 
504e86f1266SJens Wiklander 	p->types = types;
505e86f1266SJens Wiklander 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
506e86f1266SJens Wiklander 		uintptr_t a = up->vals[n * 2];
507e86f1266SJens Wiklander 		size_t b = up->vals[n * 2 + 1];
508d5c5b0b7SJens Wiklander 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
509d5c5b0b7SJens Wiklander 				 TEE_MEMORY_ACCESS_ANY_OWNER;
510e86f1266SJens Wiklander 
511e86f1266SJens Wiklander 		switch (TEE_PARAM_TYPE_GET(types, n)) {
512e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
513e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_INOUT:
514d5c5b0b7SJens Wiklander 			flags |= TEE_MEMORY_ACCESS_WRITE;
515d5c5b0b7SJens Wiklander 			/*FALLTHROUGH*/
516d5c5b0b7SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_INPUT:
5170dcfa568SJens Wiklander 			p->u[n].mem.mobj = &mobj_virt;
5180dcfa568SJens Wiklander 			p->u[n].mem.offs = a;
5190dcfa568SJens Wiklander 			p->u[n].mem.size = b;
520d5c5b0b7SJens Wiklander 			if (tee_mmu_check_access_rights(utc, flags, a, b))
521d5c5b0b7SJens Wiklander 				return TEE_ERROR_ACCESS_DENIED;
522e86f1266SJens Wiklander 			break;
523e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_VALUE_INPUT:
524e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_VALUE_INOUT:
5250dcfa568SJens Wiklander 			p->u[n].val.a = a;
5260dcfa568SJens Wiklander 			p->u[n].val.b = b;
527e86f1266SJens Wiklander 			break;
528e86f1266SJens Wiklander 		default:
5290dcfa568SJens Wiklander 			memset(&p->u[n], 0, sizeof(p->u[n]));
530e86f1266SJens Wiklander 			break;
531e86f1266SJens Wiklander 		}
532e86f1266SJens Wiklander 	}
533d5c5b0b7SJens Wiklander 
534d5c5b0b7SJens Wiklander 	return TEE_SUCCESS;
535e86f1266SJens Wiklander }
536e86f1266SJens Wiklander 
5370dcfa568SJens Wiklander static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj,
5386dbcd9ddSJens Wiklander 				     uint8_t **va)
5390dcfa568SJens Wiklander {
5400dcfa568SJens Wiklander 	/* Allocate section in secure DDR */
541d8555bddSJens Wiklander #ifdef CFG_PAGED_USER_TA
542d8555bddSJens Wiklander 	*mobj = mobj_seccpy_shm_alloc(size);
543d8555bddSJens Wiklander #else
5446dbcd9ddSJens Wiklander 	*mobj = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr);
545d8555bddSJens Wiklander #endif
5466dbcd9ddSJens Wiklander 	if (!*mobj)
5470dcfa568SJens Wiklander 		return TEE_ERROR_GENERIC;
5480dcfa568SJens Wiklander 
5496dbcd9ddSJens Wiklander 	*va = mobj_get_va(*mobj, 0);
5500dcfa568SJens Wiklander 	return TEE_SUCCESS;
5510dcfa568SJens Wiklander }
5520dcfa568SJens Wiklander 
553b0104773SPascal Brand /*
554b0104773SPascal Brand  * TA invokes some TA with parameter.
555b0104773SPascal Brand  * If some parameters are memory references:
556b0104773SPascal Brand  * - either the memref is inside TA private RAM: TA is not allowed to expose
557b0104773SPascal Brand  *   its private RAM: use a temporary memory buffer and copy the data.
558b0104773SPascal Brand  * - or the memref is not in the TA private RAM:
559b0104773SPascal Brand  *   - if the memref was mapped to the TA, TA is allowed to expose it.
560b0104773SPascal Brand  *   - if so, converts memref virtual address into a physical address.
561b0104773SPascal Brand  */
562b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
563b0104773SPascal Brand 				     struct tee_ta_session *called_sess,
564e86f1266SJens Wiklander 				     struct utee_params *callee_params,
565b0104773SPascal Brand 				     struct tee_ta_param *param,
56637070d93SJens Wiklander 				     void *tmp_buf_va[TEE_NUM_PARAMS],
567a401bcfbSBastien Simondi 				     size_t tmp_buf_size[TEE_NUM_PARAMS],
5686dbcd9ddSJens Wiklander 				     struct mobj **mobj_tmp)
569b0104773SPascal Brand {
570b0104773SPascal Brand 	size_t n;
571b0104773SPascal Brand 	TEE_Result res;
572b0104773SPascal Brand 	size_t req_mem = 0;
573b0104773SPascal Brand 	size_t s;
574b0104773SPascal Brand 	uint8_t *dst = 0;
575b0104773SPascal Brand 	bool ta_private_memref[TEE_NUM_PARAMS];
5768684fde8SJens Wiklander 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
5770dcfa568SJens Wiklander 	void *va;
5780dcfa568SJens Wiklander 	size_t dst_offs;
579b0104773SPascal Brand 
580b7fc217fSPascal Brand 	/* fill 'param' input struct with caller params description buffer */
581b7fc217fSPascal Brand 	if (!callee_params) {
582e86f1266SJens Wiklander 		memset(param, 0, sizeof(*param));
583b0104773SPascal Brand 	} else {
5848684fde8SJens Wiklander 		res = tee_mmu_check_access_rights(utc,
585177603c7SJens Wiklander 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
5862ffdd194SJens Wiklander 			(uaddr_t)callee_params, sizeof(struct utee_params));
587177603c7SJens Wiklander 		if (res != TEE_SUCCESS)
588177603c7SJens Wiklander 			return res;
589d5c5b0b7SJens Wiklander 		res = utee_param_to_param(utc, param, callee_params);
590d5c5b0b7SJens Wiklander 		if (res != TEE_SUCCESS)
591d5c5b0b7SJens Wiklander 			return res;
592b0104773SPascal Brand 	}
593b0104773SPascal Brand 
59442fb5b2eSEtienne Carriere 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
59535964dc9SEtienne Carriere 		/* pseudo TA borrows the mapping of the calling TA */
596b0104773SPascal Brand 		return TEE_SUCCESS;
597b0104773SPascal Brand 	}
598b0104773SPascal Brand 
5990dcfa568SJens Wiklander 	/* All mobj in param are of type MOJB_TYPE_VIRT */
6000dcfa568SJens Wiklander 
601b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
602b0104773SPascal Brand 
603b0104773SPascal Brand 		ta_private_memref[n] = false;
604b0104773SPascal Brand 
605b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
606b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
607b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
608b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
6090dcfa568SJens Wiklander 			va = (void *)param->u[n].mem.offs;
6100dcfa568SJens Wiklander 			s = param->u[n].mem.size;
6110dcfa568SJens Wiklander 			if (!va) {
612b0961f98SJerome Forissier 				if (s)
613b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
614b0104773SPascal Brand 				break;
615b0104773SPascal Brand 			}
616b0104773SPascal Brand 			/* uTA cannot expose its private memory */
6170dcfa568SJens Wiklander 			if (tee_mmu_is_vbuf_inside_ta_private(utc, va, s)) {
618b0104773SPascal Brand 
6190dcfa568SJens Wiklander 				s = ROUNDUP(s, sizeof(uint32_t));
6208d0f8b46SJens Wiklander 				if (ADD_OVERFLOW(req_mem, s, &req_mem))
621b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
622b0104773SPascal Brand 				ta_private_memref[n] = true;
623b0104773SPascal Brand 				break;
624b0104773SPascal Brand 			}
625b0104773SPascal Brand 
6260dcfa568SJens Wiklander 			res = tee_mmu_vbuf_to_mobj_offs(utc, va, s,
6270dcfa568SJens Wiklander 							&param->u[n].mem.mobj,
6280dcfa568SJens Wiklander 							&param->u[n].mem.offs);
6290dcfa568SJens Wiklander 			if (res != TEE_SUCCESS)
6300dcfa568SJens Wiklander 				return res;
631b0104773SPascal Brand 			break;
632b0104773SPascal Brand 		default:
633b0104773SPascal Brand 			break;
634b0104773SPascal Brand 		}
635b0104773SPascal Brand 	}
636b0104773SPascal Brand 
637b0104773SPascal Brand 	if (req_mem == 0)
638b0104773SPascal Brand 		return TEE_SUCCESS;
639b0104773SPascal Brand 
6406dbcd9ddSJens Wiklander 	res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst);
6410dcfa568SJens Wiklander 	if (res != TEE_SUCCESS)
6420dcfa568SJens Wiklander 		return res;
6436dbcd9ddSJens Wiklander 	dst_offs = 0;
644b0104773SPascal Brand 
64568540524SIgor Opaniuk 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
646b0104773SPascal Brand 
6470dcfa568SJens Wiklander 		if (!ta_private_memref[n])
648b0104773SPascal Brand 			continue;
649b0104773SPascal Brand 
6500dcfa568SJens Wiklander 		s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t));
651b0104773SPascal Brand 
652b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
653b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
654b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
6550dcfa568SJens Wiklander 			va = (void *)param->u[n].mem.offs;
6560dcfa568SJens Wiklander 			if (va) {
6570dcfa568SJens Wiklander 				res = tee_svc_copy_from_user(dst, va,
6580dcfa568SJens Wiklander 						param->u[n].mem.size);
659b0104773SPascal Brand 				if (res != TEE_SUCCESS)
660b0104773SPascal Brand 					return res;
6610dcfa568SJens Wiklander 				param->u[n].mem.offs = dst_offs;
6626dbcd9ddSJens Wiklander 				param->u[n].mem.mobj = *mobj_tmp;
66337070d93SJens Wiklander 				tmp_buf_va[n] = dst;
664a401bcfbSBastien Simondi 				tmp_buf_size[n] = param->u[n].mem.size;
665b0104773SPascal Brand 				dst += s;
6660dcfa568SJens Wiklander 				dst_offs += s;
667b0104773SPascal Brand 			}
668b0104773SPascal Brand 			break;
669b0104773SPascal Brand 
670b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
6710dcfa568SJens Wiklander 			va = (void *)param->u[n].mem.offs;
6720dcfa568SJens Wiklander 			if (va) {
6730dcfa568SJens Wiklander 				param->u[n].mem.offs = dst_offs;
6746dbcd9ddSJens Wiklander 				param->u[n].mem.mobj = *mobj_tmp;
67537070d93SJens Wiklander 				tmp_buf_va[n] = dst;
676a401bcfbSBastien Simondi 				tmp_buf_size[n] = param->u[n].mem.size;
677b0104773SPascal Brand 				dst += s;
6780dcfa568SJens Wiklander 				dst_offs += s;
679b0104773SPascal Brand 			}
680b0104773SPascal Brand 			break;
681b0104773SPascal Brand 
682b0104773SPascal Brand 		default:
683b0104773SPascal Brand 			continue;
684b0104773SPascal Brand 		}
685b0104773SPascal Brand 	}
686b0104773SPascal Brand 
687b0104773SPascal Brand 	return TEE_SUCCESS;
688b0104773SPascal Brand }
689b0104773SPascal Brand 
690b0104773SPascal Brand /*
691b0104773SPascal Brand  * Back from execution of service: update parameters passed from TA:
692b0104773SPascal Brand  * If some parameters were memory references:
693b0104773SPascal Brand  * - either the memref was temporary: copy back data and update size
694b0104773SPascal Brand  * - or it was the original TA memref: update only the size value.
695b0104773SPascal Brand  */
696b0104773SPascal Brand static TEE_Result tee_svc_update_out_param(
697b0104773SPascal Brand 		struct tee_ta_param *param,
69837070d93SJens Wiklander 		void *tmp_buf_va[TEE_NUM_PARAMS],
699a401bcfbSBastien Simondi 		size_t tmp_buf_size[TEE_NUM_PARAMS],
700e86f1266SJens Wiklander 		struct utee_params *usr_param)
701b0104773SPascal Brand {
702b0104773SPascal Brand 	size_t n;
703359324a2SJens Wiklander 	uint64_t *vals = usr_param->vals;
704bc420748SJens Wiklander 
705b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
706b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
707b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
708b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
709b0104773SPascal Brand 			/*
710359324a2SJens Wiklander 			 * Memory copy is only needed if there's a temporary
711359324a2SJens Wiklander 			 * buffer involved, tmp_buf_va[n] is only update if
712359324a2SJens Wiklander 			 * a temporary buffer is used. Otherwise only the
713359324a2SJens Wiklander 			 * size needs to be updated.
714b0104773SPascal Brand 			 */
715359324a2SJens Wiklander 			if (tmp_buf_va[n] &&
716359324a2SJens Wiklander 			    param->u[n].mem.size <= vals[n * 2 + 1]) {
717359324a2SJens Wiklander 				void *src = tmp_buf_va[n];
718359324a2SJens Wiklander 				void *dst = (void *)(uintptr_t)vals[n * 2];
719b0104773SPascal Brand 				TEE_Result res;
720b0104773SPascal Brand 
721a401bcfbSBastien Simondi 				/*
722a401bcfbSBastien Simondi 				 * TA is allowed to return a size larger than
723a401bcfbSBastien Simondi 				 * the original size. However, in such cases no
724a401bcfbSBastien Simondi 				 * data should be synchronized as per TEE Client
725a401bcfbSBastien Simondi 				 * API spec.
726a401bcfbSBastien Simondi 				 */
727a401bcfbSBastien Simondi 				if (param->u[n].mem.size <= tmp_buf_size[n]) {
728359324a2SJens Wiklander 					res = tee_svc_copy_to_user(dst, src,
7290dcfa568SJens Wiklander 							param->u[n].mem.size);
730b0104773SPascal Brand 					if (res != TEE_SUCCESS)
731b0104773SPascal Brand 						return res;
732a401bcfbSBastien Simondi 				}
733b0104773SPascal Brand 			}
7340dcfa568SJens Wiklander 			usr_param->vals[n * 2 + 1] = param->u[n].mem.size;
735b0104773SPascal Brand 			break;
736b0104773SPascal Brand 
737b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
738b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_INOUT:
739359324a2SJens Wiklander 			vals[n * 2] = param->u[n].val.a;
740359324a2SJens Wiklander 			vals[n * 2 + 1] = param->u[n].val.b;
741b0104773SPascal Brand 			break;
742b0104773SPascal Brand 
743b0104773SPascal Brand 		default:
744b0104773SPascal Brand 			continue;
745b0104773SPascal Brand 		}
746b0104773SPascal Brand 	}
747b0104773SPascal Brand 
748b0104773SPascal Brand 	return TEE_SUCCESS;
749b0104773SPascal Brand }
750b0104773SPascal Brand 
751b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */
752453a5030SJerome Forissier TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
753e86f1266SJens Wiklander 			unsigned long cancel_req_to,
754e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ta_sess,
755b0104773SPascal Brand 			uint32_t *ret_orig)
756b0104773SPascal Brand {
757b0104773SPascal Brand 	TEE_Result res;
758b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
759b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
760b0104773SPascal Brand 	struct tee_ta_session *sess;
7616dbcd9ddSJens Wiklander 	struct mobj *mobj_param = NULL;
762b0104773SPascal Brand 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
763b0104773SPascal Brand 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
764b0104773SPascal Brand 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
765359324a2SJens Wiklander 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
766a401bcfbSBastien Simondi 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { 0 };
7678684fde8SJens Wiklander 	struct user_ta_ctx *utc;
768b0104773SPascal Brand 
769b0104773SPascal Brand 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
770b0104773SPascal Brand 		res = TEE_ERROR_OUT_OF_MEMORY;
771b0104773SPascal Brand 		goto out_free_only;
772b0104773SPascal Brand 	}
773b0104773SPascal Brand 
774b0104773SPascal Brand 	memset(param, 0, sizeof(struct tee_ta_param));
775b0104773SPascal Brand 
776b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
777b0104773SPascal Brand 	if (res != TEE_SUCCESS)
778b0104773SPascal Brand 		goto out_free_only;
7798684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
780b0104773SPascal Brand 
781eebf7990SJens Wiklander 	res = tee_svc_copy_from_user(uuid, dest, sizeof(TEE_UUID));
782b0104773SPascal Brand 	if (res != TEE_SUCCESS)
783b0104773SPascal Brand 		goto function_exit;
784b0104773SPascal Brand 
785b0104773SPascal Brand 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
786bc420748SJens Wiklander 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
787b0104773SPascal Brand 
78837070d93SJens Wiklander 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va,
789a401bcfbSBastien Simondi 				 tmp_buf_size, &mobj_param);
790b0104773SPascal Brand 	if (res != TEE_SUCCESS)
791b0104773SPascal Brand 		goto function_exit;
792b0104773SPascal Brand 
7938684fde8SJens Wiklander 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
79427cbcc57SJens Wiklander 				  clnt_id, cancel_req_to, param);
7955c39e05dSJerome Forissier 	tee_mmu_set_ctx(&utc->ctx);
796c0346845SJens Wiklander 	if (res != TEE_SUCCESS)
797b0104773SPascal Brand 		goto function_exit;
798b0104773SPascal Brand 
799a401bcfbSBastien Simondi 	res = tee_svc_update_out_param(param, tmp_buf_va, tmp_buf_size,
800a401bcfbSBastien Simondi 				       usr_param);
801b0104773SPascal Brand 
802b0104773SPascal Brand function_exit:
803*b9651492SJens Wiklander 	mobj_put_wipe(mobj_param);
8042dcb3d36SJerome Forissier 	if (res == TEE_SUCCESS)
80599164a05SJerome Forissier 		tee_svc_copy_to_user(ta_sess, &s->id, sizeof(s->id));
806eebf7990SJens Wiklander 	tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o));
807b0104773SPascal Brand 
808b0104773SPascal Brand out_free_only:
80970b61310SJerome Forissier 	free_wipe(param);
81070b61310SJerome Forissier 	free_wipe(uuid);
81170b61310SJerome Forissier 	free_wipe(clnt_id);
812b0104773SPascal Brand 	return res;
813b0104773SPascal Brand }
814b0104773SPascal Brand 
815e86f1266SJens Wiklander TEE_Result syscall_close_ta_session(unsigned long ta_sess)
816b0104773SPascal Brand {
817b0104773SPascal Brand 	TEE_Result res;
818b0104773SPascal Brand 	struct tee_ta_session *sess;
81960699957SPascal Brand 	TEE_Identity clnt_id;
82099164a05SJerome Forissier 	struct tee_ta_session *s = NULL;
8218684fde8SJens Wiklander 	struct user_ta_ctx *utc;
822b0104773SPascal Brand 
823b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
824b0104773SPascal Brand 	if (res != TEE_SUCCESS)
825b0104773SPascal Brand 		return res;
8268684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
82799164a05SJerome Forissier 	s = tee_ta_find_session(ta_sess, &utc->open_sessions);
828b0104773SPascal Brand 
82960699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
830bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
831b0104773SPascal Brand 
832a07c12b2SJens Wiklander 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
833b0104773SPascal Brand }
834b0104773SPascal Brand 
835e86f1266SJens Wiklander TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
836e86f1266SJens Wiklander 			unsigned long cancel_req_to, unsigned long cmd_id,
837e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ret_orig)
838b0104773SPascal Brand {
839b0104773SPascal Brand 	TEE_Result res;
8407d82e180SJens Wiklander 	TEE_Result res2;
841b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
842b0104773SPascal Brand 	struct tee_ta_param param = { 0 };
84360699957SPascal Brand 	TEE_Identity clnt_id;
844b0104773SPascal Brand 	struct tee_ta_session *sess;
845b666b6f2SJens Wiklander 	struct tee_ta_session *called_sess;
8466dbcd9ddSJens Wiklander 	struct mobj *mobj_param = NULL;
847359324a2SJens Wiklander 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
848a401bcfbSBastien Simondi 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { };
8498684fde8SJens Wiklander 	struct user_ta_ctx *utc;
850b0104773SPascal Brand 
851b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
852b0104773SPascal Brand 	if (res != TEE_SUCCESS)
853b0104773SPascal Brand 		return res;
8548684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
855b0104773SPascal Brand 
85699164a05SJerome Forissier 	called_sess = tee_ta_get_session((uint32_t)ta_sess, true,
8578684fde8SJens Wiklander 				&utc->open_sessions);
858b666b6f2SJens Wiklander 	if (!called_sess)
859b666b6f2SJens Wiklander 		return TEE_ERROR_BAD_PARAMETERS;
860b0104773SPascal Brand 
86160699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
862bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
86360699957SPascal Brand 
864e86f1266SJens Wiklander 	res = tee_svc_copy_param(sess, called_sess, usr_param, &param,
865a401bcfbSBastien Simondi 				 tmp_buf_va, tmp_buf_size, &mobj_param);
866b0104773SPascal Brand 	if (res != TEE_SUCCESS)
867b0104773SPascal Brand 		goto function_exit;
868b0104773SPascal Brand 
86960699957SPascal Brand 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
87060699957SPascal Brand 				    cancel_req_to, cmd_id, &param);
871fd10f62bSOvidiu Mihalachi 	if (res == TEE_ERROR_TARGET_DEAD)
872fd10f62bSOvidiu Mihalachi 		goto function_exit;
87360699957SPascal Brand 
874a401bcfbSBastien Simondi 	res2 = tee_svc_update_out_param(&param, tmp_buf_va, tmp_buf_size,
875a401bcfbSBastien Simondi 					usr_param);
8767d82e180SJens Wiklander 	if (res2 != TEE_SUCCESS) {
8777d82e180SJens Wiklander 		/*
8787d82e180SJens Wiklander 		 * Spec for TEE_InvokeTACommand() says:
8797d82e180SJens Wiklander 		 * "If the return origin is different from
8807d82e180SJens Wiklander 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
8817d82e180SJens Wiklander 		 * before it could reach the destination Trusted
8827d82e180SJens Wiklander 		 * Application."
8837d82e180SJens Wiklander 		 *
8847d82e180SJens Wiklander 		 * But if we can't update params to the caller we have no
8857d82e180SJens Wiklander 		 * choice we need to return some error to indicate that
8867d82e180SJens Wiklander 		 * parameters aren't updated as expected.
8877d82e180SJens Wiklander 		 */
8887d82e180SJens Wiklander 		ret_o = TEE_ORIGIN_TEE;
8897d82e180SJens Wiklander 		res = res2;
8907d82e180SJens Wiklander 	}
891b0104773SPascal Brand 
892b0104773SPascal Brand function_exit:
893b666b6f2SJens Wiklander 	tee_ta_put_session(called_sess);
894*b9651492SJens Wiklander 	mobj_put_wipe(mobj_param);
895b0104773SPascal Brand 	if (ret_orig)
896eebf7990SJens Wiklander 		tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o));
897b0104773SPascal Brand 	return res;
898b0104773SPascal Brand }
899b0104773SPascal Brand 
900e86f1266SJens Wiklander TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
901b0104773SPascal Brand 				       size_t len)
902b0104773SPascal Brand {
903b0104773SPascal Brand 	TEE_Result res;
904b0104773SPascal Brand 	struct tee_ta_session *s;
905b0104773SPascal Brand 
906b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
907b0104773SPascal Brand 	if (res != TEE_SUCCESS)
908b0104773SPascal Brand 		return res;
909b0104773SPascal Brand 
9108684fde8SJens Wiklander 	return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags,
9112ffdd194SJens Wiklander 					   (uaddr_t)buf, len);
912b0104773SPascal Brand }
913b0104773SPascal Brand 
914eebf7990SJens Wiklander TEE_Result tee_svc_copy_from_user(void *kaddr, const void *uaddr, size_t len)
915b0104773SPascal Brand {
916b0104773SPascal Brand 	TEE_Result res;
917b0104773SPascal Brand 	struct tee_ta_session *s;
918b0104773SPascal Brand 
919b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
920b0104773SPascal Brand 	if (res != TEE_SUCCESS)
921b0104773SPascal Brand 		return res;
922eebf7990SJens Wiklander 
9238684fde8SJens Wiklander 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
924b0104773SPascal Brand 					TEE_MEMORY_ACCESS_READ |
925b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
9262ffdd194SJens Wiklander 					(uaddr_t)uaddr, len);
927b0104773SPascal Brand 	if (res != TEE_SUCCESS)
928b0104773SPascal Brand 		return res;
929b0104773SPascal Brand 
930b0104773SPascal Brand 	memcpy(kaddr, uaddr, len);
931b0104773SPascal Brand 	return TEE_SUCCESS;
932b0104773SPascal Brand }
933b0104773SPascal Brand 
934eebf7990SJens Wiklander TEE_Result tee_svc_copy_to_user(void *uaddr, const void *kaddr, size_t len)
935b0104773SPascal Brand {
936b0104773SPascal Brand 	TEE_Result res;
937b0104773SPascal Brand 	struct tee_ta_session *s;
938b0104773SPascal Brand 
939b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
940b0104773SPascal Brand 	if (res != TEE_SUCCESS)
941b0104773SPascal Brand 		return res;
942b0104773SPascal Brand 
9438684fde8SJens Wiklander 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
944b0104773SPascal Brand 					TEE_MEMORY_ACCESS_WRITE |
945b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
9462ffdd194SJens Wiklander 					(uaddr_t)uaddr, len);
947b0104773SPascal Brand 	if (res != TEE_SUCCESS)
948b0104773SPascal Brand 		return res;
949b0104773SPascal Brand 
950b0104773SPascal Brand 	memcpy(uaddr, kaddr, len);
951b0104773SPascal Brand 	return TEE_SUCCESS;
952b0104773SPascal Brand }
953b0104773SPascal Brand 
954eebf7990SJens Wiklander TEE_Result tee_svc_copy_kaddr_to_uref(uint32_t *uref, void *kaddr)
9558707ec0fSJerome Forissier {
956e86f1266SJens Wiklander 	uint32_t ref = tee_svc_kaddr_to_uref(kaddr);
9578707ec0fSJerome Forissier 
958eebf7990SJens Wiklander 	return tee_svc_copy_to_user(uref, &ref, sizeof(ref));
9598707ec0fSJerome Forissier }
9608707ec0fSJerome Forissier 
961e86f1266SJens Wiklander TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
962b0104773SPascal Brand {
963b0104773SPascal Brand 	TEE_Result res;
964b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
965e86f1266SJens Wiklander 	uint32_t c;
966b0104773SPascal Brand 
967b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
968b0104773SPascal Brand 	if (res != TEE_SUCCESS)
969b0104773SPascal Brand 		return res;
970b0104773SPascal Brand 
97163dc8d4aSJens Wiklander 	c = tee_ta_session_is_cancelled(s, NULL);
972b0104773SPascal Brand 
973eebf7990SJens Wiklander 	return tee_svc_copy_to_user(cancel, &c, sizeof(c));
974b0104773SPascal Brand }
975b0104773SPascal Brand 
976e86f1266SJens Wiklander TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
977b0104773SPascal Brand {
978b0104773SPascal Brand 	TEE_Result res;
979b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
980e86f1266SJens Wiklander 	uint32_t m;
981b0104773SPascal Brand 
982b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
983b0104773SPascal Brand 	if (res != TEE_SUCCESS)
984b0104773SPascal Brand 		return res;
985b0104773SPascal Brand 
986b0104773SPascal Brand 	m = s->cancel_mask;
987b0104773SPascal Brand 	s->cancel_mask = false;
988eebf7990SJens Wiklander 	return tee_svc_copy_to_user(old_mask, &m, sizeof(m));
989b0104773SPascal Brand }
990b0104773SPascal Brand 
991e86f1266SJens Wiklander TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
992b0104773SPascal Brand {
993b0104773SPascal Brand 	TEE_Result res;
994b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
995e86f1266SJens Wiklander 	uint32_t m;
996b0104773SPascal Brand 
997b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
998b0104773SPascal Brand 	if (res != TEE_SUCCESS)
999b0104773SPascal Brand 		return res;
1000b0104773SPascal Brand 
1001b0104773SPascal Brand 	m = s->cancel_mask;
1002b0104773SPascal Brand 	s->cancel_mask = true;
1003eebf7990SJens Wiklander 	return tee_svc_copy_to_user(old_mask, &m, sizeof(m));
1004b0104773SPascal Brand }
1005b0104773SPascal Brand 
1006e86f1266SJens Wiklander TEE_Result syscall_wait(unsigned long timeout)
1007b0104773SPascal Brand {
1008b0104773SPascal Brand 	TEE_Result res = TEE_SUCCESS;
1009b0104773SPascal Brand 	uint32_t mytime = 0;
1010b0104773SPascal Brand 	struct tee_ta_session *s;
1011b0104773SPascal Brand 	TEE_Time base_time;
1012b0104773SPascal Brand 	TEE_Time current_time;
1013b0104773SPascal Brand 
1014b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1015b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1016b0104773SPascal Brand 		return res;
1017b0104773SPascal Brand 
1018b0104773SPascal Brand 	res = tee_time_get_sys_time(&base_time);
1019b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1020b0104773SPascal Brand 		return res;
1021b0104773SPascal Brand 
1022b0104773SPascal Brand 	while (true) {
1023b0104773SPascal Brand 		res = tee_time_get_sys_time(&current_time);
1024b0104773SPascal Brand 		if (res != TEE_SUCCESS)
1025b0104773SPascal Brand 			return res;
1026b0104773SPascal Brand 
102763dc8d4aSJens Wiklander 		if (tee_ta_session_is_cancelled(s, &current_time))
1028b0104773SPascal Brand 			return TEE_ERROR_CANCEL;
1029b0104773SPascal Brand 
1030b0104773SPascal Brand 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1031b0104773SPascal Brand 		    (int)current_time.millis - (int)base_time.millis;
1032b0104773SPascal Brand 		if (mytime >= timeout)
1033b0104773SPascal Brand 			return TEE_SUCCESS;
1034b0104773SPascal Brand 
1035d1aea08fSSY Chiu 		tee_time_wait(timeout - mytime);
1036b0104773SPascal Brand 	}
1037b0104773SPascal Brand 
1038b0104773SPascal Brand 	return res;
1039b0104773SPascal Brand }
1040b0104773SPascal Brand 
1041e86f1266SJens Wiklander TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1042b0104773SPascal Brand {
1043b0104773SPascal Brand 	TEE_Result res, res2;
1044b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1045b0104773SPascal Brand 	TEE_Time t;
1046b0104773SPascal Brand 
1047b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1048b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1049b0104773SPascal Brand 		return res;
1050b0104773SPascal Brand 
1051b0104773SPascal Brand 	switch (cat) {
1052b0104773SPascal Brand 	case UTEE_TIME_CAT_SYSTEM:
1053b0104773SPascal Brand 		res = tee_time_get_sys_time(&t);
1054b0104773SPascal Brand 		break;
1055b0104773SPascal Brand 	case UTEE_TIME_CAT_TA_PERSISTENT:
1056bc420748SJens Wiklander 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1057b0104773SPascal Brand 		break;
1058b0104773SPascal Brand 	case UTEE_TIME_CAT_REE:
1059b0104773SPascal Brand 		res = tee_time_get_ree_time(&t);
1060b0104773SPascal Brand 		break;
1061b0104773SPascal Brand 	default:
1062b0104773SPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
1063b0104773SPascal Brand 		break;
1064b0104773SPascal Brand 	}
1065b0104773SPascal Brand 
1066b0104773SPascal Brand 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1067eebf7990SJens Wiklander 		res2 = tee_svc_copy_to_user(mytime, &t, sizeof(t));
1068b0104773SPascal Brand 		if (res2 != TEE_SUCCESS)
1069b0104773SPascal Brand 			res = res2;
1070b0104773SPascal Brand 	}
1071b0104773SPascal Brand 
1072b0104773SPascal Brand 	return res;
1073b0104773SPascal Brand }
1074b0104773SPascal Brand 
1075453a5030SJerome Forissier TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1076b0104773SPascal Brand {
1077b0104773SPascal Brand 	TEE_Result res;
1078b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1079b0104773SPascal Brand 	TEE_Time t;
1080b0104773SPascal Brand 
1081b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1082b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1083b0104773SPascal Brand 		return res;
1084b0104773SPascal Brand 
1085eebf7990SJens Wiklander 	res = tee_svc_copy_from_user(&t, mytime, sizeof(t));
1086b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1087b0104773SPascal Brand 		return res;
1088b0104773SPascal Brand 
1089bc420748SJens Wiklander 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1090b0104773SPascal Brand }
1091