xref: /optee_os/core/tee/tee_svc.c (revision e12c9f67d12c24a39ec0b98049e445bb3ae7a23e)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
4c40a6505SJens Wiklander  * Copyright (c) 2020, Linaro Limited
5b0104773SPascal Brand  */
6c40a6505SJens Wiklander 
7b0104773SPascal Brand #include <kernel/chip_services.h>
842fb5b2eSEtienne Carriere #include <kernel/pseudo_ta.h>
970b61310SJerome Forissier #include <kernel/tee_common.h>
1070b61310SJerome Forissier #include <kernel/tee_common_otp.h>
1170b61310SJerome Forissier #include <kernel/tee_ta_manager.h>
1270b61310SJerome Forissier #include <kernel/tee_time.h>
1370b61310SJerome Forissier #include <kernel/trace_ta.h>
14c40a6505SJens Wiklander #include <kernel/user_access.h>
1570b61310SJerome Forissier #include <mm/core_memprot.h>
160dcfa568SJens Wiklander #include <mm/mobj.h>
1770b61310SJerome Forissier #include <mm/tee_mm.h>
1870b61310SJerome Forissier #include <mm/tee_mmu.h>
1970b61310SJerome Forissier #include <stdlib_ext.h>
2070b61310SJerome Forissier #include <tee_api_types.h>
2170b61310SJerome Forissier #include <tee/tee_cryp_utl.h>
2270b61310SJerome Forissier #include <tee/tee_svc.h>
2370b61310SJerome Forissier #include <trace.h>
2470b61310SJerome Forissier #include <user_ta_header.h>
2570b61310SJerome Forissier #include <utee_types.h>
2670b61310SJerome Forissier #include <util.h>
27b0104773SPascal Brand 
28e86f1266SJens Wiklander vaddr_t tee_svc_uref_base;
29e86f1266SJens Wiklander 
303276098dSJerome Forissier void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused)
31b0104773SPascal Brand {
327c876f12SPascal Brand #ifdef CFG_TEE_CORE_TA_TRACE
33b0104773SPascal Brand 	char *kbuf;
34b0104773SPascal Brand 
35b0104773SPascal Brand 	if (len == 0)
36b0104773SPascal Brand 		return;
37b0104773SPascal Brand 
3889a3e9feSJens Wiklander 	kbuf = malloc(len + 1);
39b0104773SPascal Brand 	if (kbuf == NULL)
40b0104773SPascal Brand 		return;
41b0104773SPascal Brand 
42c40a6505SJens Wiklander 	if (copy_from_user(kbuf, buf, len) == TEE_SUCCESS) {
433095f61eSJerome Forissier 		kbuf[len] = '\0';
443095f61eSJerome Forissier 		trace_ext_puts(kbuf);
453095f61eSJerome Forissier 	}
46b0104773SPascal Brand 
4770b61310SJerome Forissier 	free_wipe(kbuf);
487c876f12SPascal Brand #endif
49b0104773SPascal Brand }
50b0104773SPascal Brand 
51453a5030SJerome Forissier TEE_Result syscall_not_supported(void)
52197d17e7SSY Chiu {
53197d17e7SSY Chiu 	return TEE_ERROR_NOT_SUPPORTED;
54197d17e7SSY Chiu }
55197d17e7SSY Chiu 
56ab35d7adSCedric Chaumont /* Configuration properties */
57ab35d7adSCedric Chaumont /* API implementation version */
58ab35d7adSCedric Chaumont static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION);
59ab35d7adSCedric Chaumont 
60ab35d7adSCedric Chaumont /* Implementation description (implementation-dependent) */
61ab35d7adSCedric Chaumont static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR);
62ab35d7adSCedric Chaumont 
63b0104773SPascal Brand /*
64ab35d7adSCedric Chaumont  * TA persistent time protection level
65ab35d7adSCedric Chaumont  * 100: Persistent time based on an REE-controlled real-time clock
66ab35d7adSCedric Chaumont  * and on the TEE Trusted Storage for the storage of origins (default).
67ab35d7adSCedric Chaumont  * 1000: Persistent time based on a TEE-controlled real-time clock
68ab35d7adSCedric Chaumont  * and the TEE Trusted Storage.
69ab35d7adSCedric Chaumont  * The real-time clock MUST be out of reach of software attacks
70ab35d7adSCedric Chaumont  * from the REE.
71ab35d7adSCedric Chaumont  */
72b0104773SPascal Brand static const uint32_t ta_time_prot_lvl = 100;
73ab35d7adSCedric Chaumont 
7464a5011eSPascal Brand /* Elliptic Curve Cryptographic support */
7564a5011eSPascal Brand #ifdef CFG_CRYPTO_ECC
76d5d50c3cSJens Wiklander static const bool crypto_ecc_en = 1;
7764a5011eSPascal Brand #else
78d5d50c3cSJens Wiklander static const bool crypto_ecc_en;
7964a5011eSPascal Brand #endif
8064a5011eSPascal Brand 
81ab35d7adSCedric Chaumont /*
82ab35d7adSCedric Chaumont  * Trusted storage anti rollback protection level
83ab35d7adSCedric Chaumont  * 0 (or missing): No antirollback protection (default)
84ab35d7adSCedric Chaumont  * 100: Antirollback enforced at REE level
85ab35d7adSCedric Chaumont  * 1000: Antirollback TEE-controlled hardware
86ab35d7adSCedric Chaumont  */
87b4b1a20cSJens Wiklander #ifdef CFG_RPMB_FS
88b4b1a20cSJens Wiklander static const uint32_t ts_antiroll_prot_lvl = 1000;
89b4b1a20cSJens Wiklander #else
90ab35d7adSCedric Chaumont static const uint32_t ts_antiroll_prot_lvl;
91b4b1a20cSJens Wiklander #endif
92ab35d7adSCedric Chaumont 
93ab35d7adSCedric Chaumont /* Trusted OS implementation version */
944bf425c1SJens Wiklander static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION);
95ab35d7adSCedric Chaumont 
96ab35d7adSCedric Chaumont /* Trusted OS implementation version (binary value) */
97ab35d7adSCedric Chaumont static const uint32_t trustedos_impl_bin_version; /* 0 by default */
98ab35d7adSCedric Chaumont 
99ab35d7adSCedric Chaumont /* Trusted OS implementation manufacturer name */
100ab35d7adSCedric Chaumont static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER);
101ab35d7adSCedric Chaumont 
102ab35d7adSCedric Chaumont /* Trusted firmware version */
103ab35d7adSCedric Chaumont static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION);
104ab35d7adSCedric Chaumont 
105ab35d7adSCedric Chaumont /* Trusted firmware version (binary value) */
106ab35d7adSCedric Chaumont static const uint32_t fw_impl_bin_version; /* 0 by default */
107ab35d7adSCedric Chaumont 
108ab35d7adSCedric Chaumont /* Trusted firmware manufacturer name */
109ab35d7adSCedric Chaumont static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER);
110ab35d7adSCedric Chaumont 
111eebf7990SJens Wiklander static TEE_Result get_prop_tee_dev_id(struct tee_ta_session *sess __unused,
112ff857a3aSPascal Brand 				      void *buf, size_t *blen)
113ab35d7adSCedric Chaumont {
114b0104773SPascal Brand 	TEE_Result res;
115b0104773SPascal Brand 	TEE_UUID uuid;
116ab35d7adSCedric Chaumont 	const size_t nslen = 5;
11764a5011eSPascal Brand 	uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
118ab35d7adSCedric Chaumont 	    'O', 'P', 'T', 'E', 'E' };
119b0104773SPascal Brand 
120ff857a3aSPascal Brand 	if (*blen < sizeof(uuid)) {
121ff857a3aSPascal Brand 		*blen = sizeof(uuid);
122b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
123ff857a3aSPascal Brand 	}
124ff857a3aSPascal Brand 	*blen = sizeof(uuid);
125b0104773SPascal Brand 
12664a5011eSPascal Brand 	if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen))
127b0104773SPascal Brand 		return TEE_ERROR_BAD_STATE;
128b0104773SPascal Brand 
12964a5011eSPascal Brand 	res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data),
13064a5011eSPascal Brand 				    (uint8_t *)&uuid, sizeof(uuid));
131b0104773SPascal Brand 	if (res != TEE_SUCCESS)
132b0104773SPascal Brand 		return TEE_ERROR_BAD_STATE;
133b0104773SPascal Brand 
134b0104773SPascal Brand 	/*
135b0104773SPascal Brand 	 * Changes the random value into and UUID as specifiec
136b0104773SPascal Brand 	 * in RFC 4122. The magic values are from the example
137b0104773SPascal Brand 	 * code in the RFC.
138b0104773SPascal Brand 	 *
139b0104773SPascal Brand 	 * TEE_UUID is defined slightly different from the RFC,
140b0104773SPascal Brand 	 * but close enough for our purpose.
141b0104773SPascal Brand 	 */
142b0104773SPascal Brand 
143b0104773SPascal Brand 	uuid.timeHiAndVersion &= 0x0fff;
144b0104773SPascal Brand 	uuid.timeHiAndVersion |= 5 << 12;
145b0104773SPascal Brand 
146b0104773SPascal Brand 	/* uuid.clock_seq_hi_and_reserved in the RFC */
147b0104773SPascal Brand 	uuid.clockSeqAndNode[0] &= 0x3f;
148b0104773SPascal Brand 	uuid.clockSeqAndNode[0] |= 0x80;
149b0104773SPascal Brand 
150c40a6505SJens Wiklander 	return copy_to_user(buf, &uuid, sizeof(TEE_UUID));
151b0104773SPascal Brand }
152b0104773SPascal Brand 
153eebf7990SJens Wiklander static TEE_Result get_prop_tee_sys_time_prot_level(
154eebf7990SJens Wiklander 			struct tee_ta_session *sess __unused,
155ff857a3aSPascal Brand 			void *buf, size_t *blen)
1562cdaaacbSJerome Forissier {
1572cdaaacbSJerome Forissier 	uint32_t prot;
1582cdaaacbSJerome Forissier 
159ff857a3aSPascal Brand 	if (*blen < sizeof(prot)) {
160ff857a3aSPascal Brand 		*blen = sizeof(prot);
1612cdaaacbSJerome Forissier 		return TEE_ERROR_SHORT_BUFFER;
162ff857a3aSPascal Brand 	}
163ff857a3aSPascal Brand 	*blen = sizeof(prot);
1642cdaaacbSJerome Forissier 	prot = tee_time_get_sys_time_protection_level();
165c40a6505SJens Wiklander 	return copy_to_user(buf, &prot, sizeof(prot));
1662cdaaacbSJerome Forissier }
1672cdaaacbSJerome Forissier 
168a07c12b2SJens Wiklander static TEE_Result get_prop_client_id(struct tee_ta_session *sess __unused,
169ff857a3aSPascal Brand 				     void *buf, size_t *blen)
17064a5011eSPascal Brand {
171ff857a3aSPascal Brand 	if (*blen < sizeof(TEE_Identity)) {
172ff857a3aSPascal Brand 		*blen = sizeof(TEE_Identity);
173b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
174ff857a3aSPascal Brand 	}
175ff857a3aSPascal Brand 	*blen = sizeof(TEE_Identity);
176c40a6505SJens Wiklander 	return copy_to_user(buf, &sess->clnt_id, sizeof(TEE_Identity));
17764a5011eSPascal Brand }
17837d6ae92SPascal Brand 
17964a5011eSPascal Brand static TEE_Result get_prop_ta_app_id(struct tee_ta_session *sess,
180ff857a3aSPascal Brand 				     void *buf, size_t *blen)
18164a5011eSPascal Brand {
182ff857a3aSPascal Brand 	if (*blen < sizeof(TEE_UUID)) {
183ff857a3aSPascal Brand 		*blen = sizeof(TEE_UUID);
184b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
185ff857a3aSPascal Brand 	}
186ff857a3aSPascal Brand 	*blen = sizeof(TEE_UUID);
187c40a6505SJens Wiklander 	return copy_to_user(buf, &sess->ctx->uuid, sizeof(TEE_UUID));
18864a5011eSPascal Brand }
18964a5011eSPascal Brand 
19064a5011eSPascal Brand /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */
19164a5011eSPascal Brand const struct tee_props tee_propset_client[] = {
19264a5011eSPascal Brand 	{
19364a5011eSPascal Brand 		.name = "gpd.client.identity",
19464a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_IDENTITY,
19564a5011eSPascal Brand 		.get_prop_func = get_prop_client_id
19664a5011eSPascal Brand 	},
19764a5011eSPascal Brand };
19864a5011eSPascal Brand 
19964a5011eSPascal Brand /* Properties of the set TEE_PROPSET_CURRENT_TA */
20064a5011eSPascal Brand const struct tee_props tee_propset_ta[] = {
20164a5011eSPascal Brand 	{
20264a5011eSPascal Brand 		.name = "gpd.ta.appID",
20364a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_UUID,
20464a5011eSPascal Brand 		.get_prop_func = get_prop_ta_app_id
20564a5011eSPascal Brand 	},
20664a5011eSPascal Brand 
20764a5011eSPascal Brand 	/*
20864a5011eSPascal Brand 	 * Following properties are processed directly in libutee:
20964a5011eSPascal Brand 	 *	TA_PROP_STR_SINGLE_INSTANCE
21064a5011eSPascal Brand 	 *	TA_PROP_STR_MULTI_SESSION
21164a5011eSPascal Brand 	 *	TA_PROP_STR_KEEP_ALIVE
21264a5011eSPascal Brand 	 *	TA_PROP_STR_DATA_SIZE
21364a5011eSPascal Brand 	 *	TA_PROP_STR_STACK_SIZE
21464a5011eSPascal Brand 	 *	TA_PROP_STR_VERSION
21564a5011eSPascal Brand 	 *	TA_PROP_STR_DESCRIPTION
21664a5011eSPascal Brand 	 *	USER_TA_PROP_TYPE_STRING,
21764a5011eSPascal Brand 	 *	TA_DESCRIPTION
21864a5011eSPascal Brand 	 */
21964a5011eSPascal Brand };
22064a5011eSPascal Brand 
22164a5011eSPascal Brand /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */
22264a5011eSPascal Brand const struct tee_props tee_propset_tee[] = {
22364a5011eSPascal Brand 	{
22464a5011eSPascal Brand 		.name = "gpd.tee.apiversion",
22564a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
22664a5011eSPascal Brand 		.data = api_vers,
22764a5011eSPascal Brand 		.len = sizeof(api_vers),
22864a5011eSPascal Brand 	},
22964a5011eSPascal Brand 	{
23064a5011eSPascal Brand 		.name = "gpd.tee.description",
23164a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
23264a5011eSPascal Brand 		.data = descr, .len = sizeof(descr)
23364a5011eSPascal Brand 	},
23464a5011eSPascal Brand 	{
23564a5011eSPascal Brand 		.name = "gpd.tee.deviceID",
23664a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_UUID,
23764a5011eSPascal Brand 		.get_prop_func = get_prop_tee_dev_id
23864a5011eSPascal Brand 	},
23964a5011eSPascal Brand 	{
24064a5011eSPascal Brand 		.name = "gpd.tee.systemTime.protectionLevel",
24164a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
24264a5011eSPascal Brand 		.get_prop_func = get_prop_tee_sys_time_prot_level
24364a5011eSPascal Brand 	},
24464a5011eSPascal Brand 	{
24564a5011eSPascal Brand 		.name = "gpd.tee.TAPersistentTime.protectionLevel",
24664a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
24764a5011eSPascal Brand 		.data = &ta_time_prot_lvl,
24864a5011eSPascal Brand 		.len = sizeof(ta_time_prot_lvl)
24964a5011eSPascal Brand 	},
25064a5011eSPascal Brand 	{
25164a5011eSPascal Brand 		.name = "gpd.tee.cryptography.ecc",
25264a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_BOOL,
25364a5011eSPascal Brand 		.data = &crypto_ecc_en,
25464a5011eSPascal Brand 		.len = sizeof(crypto_ecc_en)
25564a5011eSPascal Brand 	},
25664a5011eSPascal Brand 	{
25764a5011eSPascal Brand 		.name = "gpd.tee.trustedStorage.antiRollback.protectionLevel",
25864a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
25964a5011eSPascal Brand 		.data = &ts_antiroll_prot_lvl,
26064a5011eSPascal Brand 		.len = sizeof(ts_antiroll_prot_lvl)
26164a5011eSPascal Brand 	},
26264a5011eSPascal Brand 	{
26364a5011eSPascal Brand 		.name = "gpd.tee.trustedos.implementation.version",
26464a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
26564a5011eSPascal Brand 		.data = trustedos_impl_version,
26664a5011eSPascal Brand 		.len = sizeof(trustedos_impl_version)
26764a5011eSPascal Brand 	},
26864a5011eSPascal Brand 	{
26964a5011eSPascal Brand 		.name = "gpd.tee.trustedos.implementation.binaryversion",
27064a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
27164a5011eSPascal Brand 		.data = &trustedos_impl_bin_version,
27264a5011eSPascal Brand 		.len = sizeof(trustedos_impl_bin_version)
27364a5011eSPascal Brand 	},
27464a5011eSPascal Brand 	{
27564a5011eSPascal Brand 		.name = "gpd.tee.trustedos.manufacturer",
27664a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
27764a5011eSPascal Brand 		.data = trustedos_manufacturer,
27864a5011eSPascal Brand 		.len = sizeof(trustedos_manufacturer)
27964a5011eSPascal Brand 	},
28064a5011eSPascal Brand 	{
28164a5011eSPascal Brand 		.name = "gpd.tee.firmware.implementation.version",
28264a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
28364a5011eSPascal Brand 		.data = fw_impl_version,
28464a5011eSPascal Brand 		.len = sizeof(fw_impl_version)
28564a5011eSPascal Brand 	},
28664a5011eSPascal Brand 	{
28764a5011eSPascal Brand 		.name = "gpd.tee.firmware.implementation.binaryversion",
28864a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
28964a5011eSPascal Brand 		.data = &fw_impl_bin_version,
29064a5011eSPascal Brand 		.len = sizeof(fw_impl_bin_version)
29164a5011eSPascal Brand 	},
29264a5011eSPascal Brand 	{
29364a5011eSPascal Brand 		.name = "gpd.tee.firmware.manufacturer",
29464a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
29564a5011eSPascal Brand 		.data = fw_manufacturer,
29664a5011eSPascal Brand 		.len = sizeof(fw_manufacturer)
29764a5011eSPascal Brand 	},
29864a5011eSPascal Brand 
29964a5011eSPascal Brand 	/*
30064a5011eSPascal Brand 	 * Following properties are processed directly in libutee:
30164a5011eSPascal Brand 	 *	gpd.tee.arith.maxBigIntSize
30264a5011eSPascal Brand 	 */
30364a5011eSPascal Brand };
30464a5011eSPascal Brand 
30541d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_client;
30641d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_ta;
30741d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_tee;
30841d71430SPascal Brand 
30964a5011eSPascal Brand static void get_prop_set(unsigned long prop_set,
31064a5011eSPascal Brand 			 const struct tee_props **props,
31141d71430SPascal Brand 			 size_t *size,
31241d71430SPascal Brand 			 const struct tee_props **vendor_props,
31341d71430SPascal Brand 			 size_t *vendor_size)
31464a5011eSPascal Brand {
31564a5011eSPascal Brand 	if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) {
31664a5011eSPascal Brand 		*props = tee_propset_client;
31764a5011eSPascal Brand 		*size = ARRAY_SIZE(tee_propset_client);
31841d71430SPascal Brand 		*vendor_props = vendor_props_client.props;
31941d71430SPascal Brand 		*vendor_size = vendor_props_client.len;
32064a5011eSPascal Brand 	} else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) {
32164a5011eSPascal Brand 		*props = tee_propset_ta;
32264a5011eSPascal Brand 		*size = ARRAY_SIZE(tee_propset_ta);
32341d71430SPascal Brand 		*vendor_props = vendor_props_ta.props;
32441d71430SPascal Brand 		*vendor_size = vendor_props_ta.len;
32564a5011eSPascal Brand 	} else if ((TEE_PropSetHandle)prop_set ==
32664a5011eSPascal Brand 		   TEE_PROPSET_TEE_IMPLEMENTATION) {
32764a5011eSPascal Brand 		*props = tee_propset_tee;
32864a5011eSPascal Brand 		*size = ARRAY_SIZE(tee_propset_tee);
32941d71430SPascal Brand 		*vendor_props = vendor_props_tee.props;
33041d71430SPascal Brand 		*vendor_size = vendor_props_tee.len;
33164a5011eSPascal Brand 	} else {
33241d71430SPascal Brand 		*props = NULL;
33364a5011eSPascal Brand 		*size = 0;
33441d71430SPascal Brand 		*vendor_props = NULL;
33541d71430SPascal Brand 		*vendor_size = 0;
33664a5011eSPascal Brand 	}
33764a5011eSPascal Brand }
33864a5011eSPascal Brand 
33964a5011eSPascal Brand static const struct tee_props *get_prop_struct(unsigned long prop_set,
34064a5011eSPascal Brand 					       unsigned long index)
34164a5011eSPascal Brand {
34264a5011eSPascal Brand 	const struct tee_props *props;
34341d71430SPascal Brand 	const struct tee_props *vendor_props;
34441d71430SPascal Brand 	size_t size;
34541d71430SPascal Brand 	size_t vendor_size;
34664a5011eSPascal Brand 
34741d71430SPascal Brand 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
34864a5011eSPascal Brand 
34941d71430SPascal Brand 	if (index < size)
35064a5011eSPascal Brand 		return &(props[index]);
35141d71430SPascal Brand 	index -= size;
35241d71430SPascal Brand 
35341d71430SPascal Brand 	if (index < vendor_size)
35441d71430SPascal Brand 		return &(vendor_props[index]);
35541d71430SPascal Brand 
35664a5011eSPascal Brand 	return NULL;
35764a5011eSPascal Brand }
35864a5011eSPascal Brand 
35964a5011eSPascal Brand /*
36064a5011eSPascal Brand  * prop_set is part of TEE_PROPSET_xxx
36164a5011eSPascal Brand  * index is the index in the Property Set to retrieve
36264a5011eSPascal Brand  * if name is not NULL, the name of "index" property is returned
36364a5011eSPascal Brand  * if buf is not NULL, the property is returned
36464a5011eSPascal Brand  */
36564a5011eSPascal Brand TEE_Result syscall_get_property(unsigned long prop_set,
36664a5011eSPascal Brand 				unsigned long index,
36764a5011eSPascal Brand 				void *name, uint32_t *name_len,
36864a5011eSPascal Brand 				void *buf, uint32_t *blen,
36964a5011eSPascal Brand 				uint32_t *prop_type)
37064a5011eSPascal Brand {
37164a5011eSPascal Brand 	struct tee_ta_session *sess;
37264a5011eSPascal Brand 	TEE_Result res;
37364a5011eSPascal Brand 	TEE_Result res2;
37464a5011eSPascal Brand 	const struct tee_props *prop;
37564a5011eSPascal Brand 	uint32_t klen;
376ff857a3aSPascal Brand 	size_t klen_size;
37764a5011eSPascal Brand 	uint32_t elen;
37864a5011eSPascal Brand 
37964a5011eSPascal Brand 	prop = get_prop_struct(prop_set, index);
38064a5011eSPascal Brand 	if (!prop)
38164a5011eSPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
38264a5011eSPascal Brand 
38364a5011eSPascal Brand 	res = tee_ta_get_current_session(&sess);
38464a5011eSPascal Brand 	if (res != TEE_SUCCESS)
38564a5011eSPascal Brand 		return res;
38664a5011eSPascal Brand 
387ff857a3aSPascal Brand 	/* Get the property type */
388ff857a3aSPascal Brand 	if (prop_type) {
389c40a6505SJens Wiklander 		res = copy_to_user(prop_type, &prop->prop_type,
390ff857a3aSPascal Brand 				   sizeof(*prop_type));
391ff857a3aSPascal Brand 		if (res != TEE_SUCCESS)
392ff857a3aSPascal Brand 			return res;
393ff857a3aSPascal Brand 	}
394ff857a3aSPascal Brand 
39564a5011eSPascal Brand 	/* Get the property */
39664a5011eSPascal Brand 	if (buf && blen) {
397c40a6505SJens Wiklander 		res = copy_from_user(&klen, blen, sizeof(klen));
39864a5011eSPascal Brand 		if (res != TEE_SUCCESS)
39964a5011eSPascal Brand 			return res;
40064a5011eSPascal Brand 
40164a5011eSPascal Brand 		if (prop->get_prop_func) {
402ff857a3aSPascal Brand 			klen_size = klen;
403ff857a3aSPascal Brand 			res = prop->get_prop_func(sess, buf, &klen_size);
404ff857a3aSPascal Brand 			klen = klen_size;
405c40a6505SJens Wiklander 			res2 = copy_to_user(blen, &klen, sizeof(*blen));
40664a5011eSPascal Brand 		} else {
40764a5011eSPascal Brand 			if (klen < prop->len)
40864a5011eSPascal Brand 				res = TEE_ERROR_SHORT_BUFFER;
40964a5011eSPascal Brand 			else
410c40a6505SJens Wiklander 				res = copy_to_user(buf, prop->data, prop->len);
411c40a6505SJens Wiklander 			res2 = copy_to_user(blen, &prop->len, sizeof(*blen));
412ff857a3aSPascal Brand 		}
41364a5011eSPascal Brand 		if (res2 != TEE_SUCCESS)
41464a5011eSPascal Brand 			return res2;
41564a5011eSPascal Brand 		if (res != TEE_SUCCESS)
41664a5011eSPascal Brand 			return res;
41764a5011eSPascal Brand 	}
41864a5011eSPascal Brand 
41964a5011eSPascal Brand 	/* Get the property name */
42064a5011eSPascal Brand 	if (name && name_len) {
421c40a6505SJens Wiklander 		res = copy_from_user(&klen, name_len, sizeof(klen));
42264a5011eSPascal Brand 		if (res != TEE_SUCCESS)
42364a5011eSPascal Brand 			return res;
42464a5011eSPascal Brand 
42564a5011eSPascal Brand 		elen = strlen(prop->name) + 1;
42664a5011eSPascal Brand 
42764a5011eSPascal Brand 		if (klen < elen)
42864a5011eSPascal Brand 			res = TEE_ERROR_SHORT_BUFFER;
42964a5011eSPascal Brand 		else
430c40a6505SJens Wiklander 			res = copy_to_user(name, prop->name, elen);
431c40a6505SJens Wiklander 		res2 = copy_to_user(name_len, &elen, sizeof(*name_len));
43264a5011eSPascal Brand 		if (res2 != TEE_SUCCESS)
43364a5011eSPascal Brand 			return res2;
43464a5011eSPascal Brand 		if (res != TEE_SUCCESS)
43564a5011eSPascal Brand 			return res;
43664a5011eSPascal Brand 	}
43764a5011eSPascal Brand 
43864a5011eSPascal Brand 	return res;
43964a5011eSPascal Brand }
44064a5011eSPascal Brand 
44164a5011eSPascal Brand /*
44264a5011eSPascal Brand  * prop_set is part of TEE_PROPSET_xxx
44364a5011eSPascal Brand  */
44464a5011eSPascal Brand TEE_Result syscall_get_property_name_to_index(unsigned long prop_set,
44564a5011eSPascal Brand 					      void *name,
44664a5011eSPascal Brand 					      unsigned long name_len,
44764a5011eSPascal Brand 					      uint32_t *index)
44864a5011eSPascal Brand {
44964a5011eSPascal Brand 	TEE_Result res;
45064a5011eSPascal Brand 	struct tee_ta_session *sess;
45164a5011eSPascal Brand 	const struct tee_props *props;
45241d71430SPascal Brand 	size_t size;
45341d71430SPascal Brand 	const struct tee_props *vendor_props;
45441d71430SPascal Brand 	size_t vendor_size;
45564a5011eSPascal Brand 	char *kname = 0;
45664a5011eSPascal Brand 	uint32_t i;
45764a5011eSPascal Brand 
45841d71430SPascal Brand 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
45964a5011eSPascal Brand 	if (!props)
46064a5011eSPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
46164a5011eSPascal Brand 
46264a5011eSPascal Brand 	res = tee_ta_get_current_session(&sess);
46364a5011eSPascal Brand 	if (res != TEE_SUCCESS)
46464a5011eSPascal Brand 		goto out;
46564a5011eSPascal Brand 
46664a5011eSPascal Brand 	if (!name || !name_len) {
46764a5011eSPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
46864a5011eSPascal Brand 		goto out;
46964a5011eSPascal Brand 	}
47064a5011eSPascal Brand 
47164a5011eSPascal Brand 	kname = malloc(name_len);
47264a5011eSPascal Brand 	if (!kname)
47364a5011eSPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
474c40a6505SJens Wiklander 	res = copy_from_user(kname, name, name_len);
47564a5011eSPascal Brand 	if (res != TEE_SUCCESS)
47664a5011eSPascal Brand 		goto out;
47764a5011eSPascal Brand 	kname[name_len - 1] = 0;
47864a5011eSPascal Brand 
47964a5011eSPascal Brand 	res = TEE_ERROR_ITEM_NOT_FOUND;
48064a5011eSPascal Brand 	for (i = 0; i < size; i++) {
48164a5011eSPascal Brand 		if (!strcmp(kname, props[i].name)) {
482c40a6505SJens Wiklander 			res = copy_to_user(index, &i, sizeof(*index));
48341d71430SPascal Brand 			goto out;
48441d71430SPascal Brand 		}
48541d71430SPascal Brand 	}
48641d71430SPascal Brand 	for (i = size; i < size + vendor_size; i++) {
48741d71430SPascal Brand 		if (!strcmp(kname, vendor_props[i - size].name)) {
488c40a6505SJens Wiklander 			res = copy_to_user(index, &i, sizeof(*index));
48941d71430SPascal Brand 			goto out;
49064a5011eSPascal Brand 		}
49164a5011eSPascal Brand 	}
49264a5011eSPascal Brand 
49364a5011eSPascal Brand out:
49470b61310SJerome Forissier 	free_wipe(kname);
49564a5011eSPascal Brand 	return res;
49664a5011eSPascal Brand }
49764a5011eSPascal Brand 
498d5c5b0b7SJens Wiklander static TEE_Result utee_param_to_param(struct user_ta_ctx *utc,
499d5c5b0b7SJens Wiklander 				      struct tee_ta_param *p,
500d5c5b0b7SJens Wiklander 				      struct utee_params *up)
501e86f1266SJens Wiklander {
5021936dfc7SJens Wiklander 	size_t n = 0;
503e86f1266SJens Wiklander 	uint32_t types = up->types;
504e86f1266SJens Wiklander 
505e86f1266SJens Wiklander 	p->types = types;
506e86f1266SJens Wiklander 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
507e86f1266SJens Wiklander 		uintptr_t a = up->vals[n * 2];
508e86f1266SJens Wiklander 		size_t b = up->vals[n * 2 + 1];
509d5c5b0b7SJens Wiklander 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
510d5c5b0b7SJens Wiklander 				 TEE_MEMORY_ACCESS_ANY_OWNER;
511e86f1266SJens Wiklander 
512e86f1266SJens Wiklander 		switch (TEE_PARAM_TYPE_GET(types, n)) {
513e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
514e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_INOUT:
515d5c5b0b7SJens Wiklander 			flags |= TEE_MEMORY_ACCESS_WRITE;
516d5c5b0b7SJens Wiklander 			/*FALLTHROUGH*/
517d5c5b0b7SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_INPUT:
5180dcfa568SJens Wiklander 			p->u[n].mem.offs = a;
5190dcfa568SJens Wiklander 			p->u[n].mem.size = b;
520ee3e1c54SCedric Neveux 
521ee3e1c54SCedric Neveux 			if (!p->u[n].mem.offs) {
522ee3e1c54SCedric Neveux 				/* Allow NULL memrefs if of size 0 */
523ee3e1c54SCedric Neveux 				if (p->u[n].mem.size)
524ee3e1c54SCedric Neveux 					return TEE_ERROR_BAD_PARAMETERS;
525ee3e1c54SCedric Neveux 				p->u[n].mem.mobj = NULL;
526ee3e1c54SCedric Neveux 				break;
527ee3e1c54SCedric Neveux 			}
528ee3e1c54SCedric Neveux 
529ee3e1c54SCedric Neveux 			p->u[n].mem.mobj = &mobj_virt;
530ee3e1c54SCedric Neveux 
5311936dfc7SJens Wiklander 			if (tee_mmu_check_access_rights(&utc->uctx, flags, a,
5321936dfc7SJens Wiklander 							b))
533d5c5b0b7SJens Wiklander 				return TEE_ERROR_ACCESS_DENIED;
534e86f1266SJens Wiklander 			break;
535e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_VALUE_INPUT:
536e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_VALUE_INOUT:
5370dcfa568SJens Wiklander 			p->u[n].val.a = a;
5380dcfa568SJens Wiklander 			p->u[n].val.b = b;
539e86f1266SJens Wiklander 			break;
540e86f1266SJens Wiklander 		default:
5410dcfa568SJens Wiklander 			memset(&p->u[n], 0, sizeof(p->u[n]));
542e86f1266SJens Wiklander 			break;
543e86f1266SJens Wiklander 		}
544e86f1266SJens Wiklander 	}
545d5c5b0b7SJens Wiklander 
546d5c5b0b7SJens Wiklander 	return TEE_SUCCESS;
547e86f1266SJens Wiklander }
548e86f1266SJens Wiklander 
5490dcfa568SJens Wiklander static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj,
5506dbcd9ddSJens Wiklander 				     uint8_t **va)
5510dcfa568SJens Wiklander {
5520dcfa568SJens Wiklander 	/* Allocate section in secure DDR */
553d8555bddSJens Wiklander #ifdef CFG_PAGED_USER_TA
554d8555bddSJens Wiklander 	*mobj = mobj_seccpy_shm_alloc(size);
555d8555bddSJens Wiklander #else
5566dbcd9ddSJens Wiklander 	*mobj = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr);
557d8555bddSJens Wiklander #endif
5586dbcd9ddSJens Wiklander 	if (!*mobj)
5590dcfa568SJens Wiklander 		return TEE_ERROR_GENERIC;
5600dcfa568SJens Wiklander 
5616dbcd9ddSJens Wiklander 	*va = mobj_get_va(*mobj, 0);
5620dcfa568SJens Wiklander 	return TEE_SUCCESS;
5630dcfa568SJens Wiklander }
5640dcfa568SJens Wiklander 
565b0104773SPascal Brand /*
566b0104773SPascal Brand  * TA invokes some TA with parameter.
567b0104773SPascal Brand  * If some parameters are memory references:
568b0104773SPascal Brand  * - either the memref is inside TA private RAM: TA is not allowed to expose
569b0104773SPascal Brand  *   its private RAM: use a temporary memory buffer and copy the data.
570b0104773SPascal Brand  * - or the memref is not in the TA private RAM:
571b0104773SPascal Brand  *   - if the memref was mapped to the TA, TA is allowed to expose it.
572b0104773SPascal Brand  *   - if so, converts memref virtual address into a physical address.
573b0104773SPascal Brand  */
574b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
575b0104773SPascal Brand 				     struct tee_ta_session *called_sess,
576e86f1266SJens Wiklander 				     struct utee_params *callee_params,
577b0104773SPascal Brand 				     struct tee_ta_param *param,
57837070d93SJens Wiklander 				     void *tmp_buf_va[TEE_NUM_PARAMS],
579a401bcfbSBastien Simondi 				     size_t tmp_buf_size[TEE_NUM_PARAMS],
5806dbcd9ddSJens Wiklander 				     struct mobj **mobj_tmp)
581b0104773SPascal Brand {
5828684fde8SJens Wiklander 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
5831936dfc7SJens Wiklander 	bool ta_private_memref[TEE_NUM_PARAMS] = { false, };
5841936dfc7SJens Wiklander 	TEE_Result res = TEE_SUCCESS;
5851936dfc7SJens Wiklander 	size_t dst_offs = 0;
5861936dfc7SJens Wiklander 	size_t req_mem = 0;
5871936dfc7SJens Wiklander 	uint8_t *dst = 0;
5881936dfc7SJens Wiklander 	void *va = NULL;
5891936dfc7SJens Wiklander 	size_t n = 0;
5901936dfc7SJens Wiklander 	size_t s = 0;
591b0104773SPascal Brand 
592b7fc217fSPascal Brand 	/* fill 'param' input struct with caller params description buffer */
593b7fc217fSPascal Brand 	if (!callee_params) {
594e86f1266SJens Wiklander 		memset(param, 0, sizeof(*param));
595b0104773SPascal Brand 	} else {
5961936dfc7SJens Wiklander 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
59785daf48cSJens Wiklander 				 TEE_MEMORY_ACCESS_WRITE |
5981936dfc7SJens Wiklander 				 TEE_MEMORY_ACCESS_ANY_OWNER;
5991936dfc7SJens Wiklander 
6001936dfc7SJens Wiklander 		res = tee_mmu_check_access_rights(&utc->uctx, flags,
6011936dfc7SJens Wiklander 						  (uaddr_t)callee_params,
6021936dfc7SJens Wiklander 						  sizeof(struct utee_params));
603177603c7SJens Wiklander 		if (res != TEE_SUCCESS)
604177603c7SJens Wiklander 			return res;
605d5c5b0b7SJens Wiklander 		res = utee_param_to_param(utc, param, callee_params);
606d5c5b0b7SJens Wiklander 		if (res != TEE_SUCCESS)
607d5c5b0b7SJens Wiklander 			return res;
608b0104773SPascal Brand 	}
609b0104773SPascal Brand 
61042fb5b2eSEtienne Carriere 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
61135964dc9SEtienne Carriere 		/* pseudo TA borrows the mapping of the calling TA */
612b0104773SPascal Brand 		return TEE_SUCCESS;
613b0104773SPascal Brand 	}
614b0104773SPascal Brand 
6150dcfa568SJens Wiklander 	/* All mobj in param are of type MOJB_TYPE_VIRT */
6160dcfa568SJens Wiklander 
617b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
618b0104773SPascal Brand 
619b0104773SPascal Brand 		ta_private_memref[n] = false;
620b0104773SPascal Brand 
621b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
622b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
623b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
624b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
6250dcfa568SJens Wiklander 			va = (void *)param->u[n].mem.offs;
6260dcfa568SJens Wiklander 			s = param->u[n].mem.size;
6270dcfa568SJens Wiklander 			if (!va) {
628b0961f98SJerome Forissier 				if (s)
629b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
630b0104773SPascal Brand 				break;
631b0104773SPascal Brand 			}
632b0104773SPascal Brand 			/* uTA cannot expose its private memory */
6331936dfc7SJens Wiklander 			if (tee_mmu_is_vbuf_inside_um_private(&utc->uctx, va,
6341936dfc7SJens Wiklander 							      s)) {
635b0104773SPascal Brand 
6360dcfa568SJens Wiklander 				s = ROUNDUP(s, sizeof(uint32_t));
6378d0f8b46SJens Wiklander 				if (ADD_OVERFLOW(req_mem, s, &req_mem))
638b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
639b0104773SPascal Brand 				ta_private_memref[n] = true;
640b0104773SPascal Brand 				break;
641b0104773SPascal Brand 			}
642b0104773SPascal Brand 
6431936dfc7SJens Wiklander 			res = tee_mmu_vbuf_to_mobj_offs(&utc->uctx, va, s,
6440dcfa568SJens Wiklander 							&param->u[n].mem.mobj,
6450dcfa568SJens Wiklander 							&param->u[n].mem.offs);
6460dcfa568SJens Wiklander 			if (res != TEE_SUCCESS)
6470dcfa568SJens Wiklander 				return res;
648b0104773SPascal Brand 			break;
649b0104773SPascal Brand 		default:
650b0104773SPascal Brand 			break;
651b0104773SPascal Brand 		}
652b0104773SPascal Brand 	}
653b0104773SPascal Brand 
654b0104773SPascal Brand 	if (req_mem == 0)
655b0104773SPascal Brand 		return TEE_SUCCESS;
656b0104773SPascal Brand 
6576dbcd9ddSJens Wiklander 	res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst);
6580dcfa568SJens Wiklander 	if (res != TEE_SUCCESS)
6590dcfa568SJens Wiklander 		return res;
6606dbcd9ddSJens Wiklander 	dst_offs = 0;
661b0104773SPascal Brand 
66268540524SIgor Opaniuk 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
663b0104773SPascal Brand 
6640dcfa568SJens Wiklander 		if (!ta_private_memref[n])
665b0104773SPascal Brand 			continue;
666b0104773SPascal Brand 
6670dcfa568SJens Wiklander 		s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t));
668b0104773SPascal Brand 
669b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
670b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
671b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
6720dcfa568SJens Wiklander 			va = (void *)param->u[n].mem.offs;
6730dcfa568SJens Wiklander 			if (va) {
674c40a6505SJens Wiklander 				res = copy_from_user(dst, va,
6750dcfa568SJens Wiklander 						     param->u[n].mem.size);
676b0104773SPascal Brand 				if (res != TEE_SUCCESS)
677b0104773SPascal Brand 					return res;
6780dcfa568SJens Wiklander 				param->u[n].mem.offs = dst_offs;
6796dbcd9ddSJens Wiklander 				param->u[n].mem.mobj = *mobj_tmp;
68037070d93SJens Wiklander 				tmp_buf_va[n] = dst;
681a401bcfbSBastien Simondi 				tmp_buf_size[n] = param->u[n].mem.size;
682b0104773SPascal Brand 				dst += s;
6830dcfa568SJens Wiklander 				dst_offs += s;
684b0104773SPascal Brand 			}
685b0104773SPascal Brand 			break;
686b0104773SPascal Brand 
687b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
6880dcfa568SJens Wiklander 			va = (void *)param->u[n].mem.offs;
6890dcfa568SJens Wiklander 			if (va) {
6900dcfa568SJens Wiklander 				param->u[n].mem.offs = dst_offs;
6916dbcd9ddSJens Wiklander 				param->u[n].mem.mobj = *mobj_tmp;
69237070d93SJens Wiklander 				tmp_buf_va[n] = dst;
693a401bcfbSBastien Simondi 				tmp_buf_size[n] = param->u[n].mem.size;
694b0104773SPascal Brand 				dst += s;
6950dcfa568SJens Wiklander 				dst_offs += s;
696b0104773SPascal Brand 			}
697b0104773SPascal Brand 			break;
698b0104773SPascal Brand 
699b0104773SPascal Brand 		default:
700b0104773SPascal Brand 			continue;
701b0104773SPascal Brand 		}
702b0104773SPascal Brand 	}
703b0104773SPascal Brand 
704b0104773SPascal Brand 	return TEE_SUCCESS;
705b0104773SPascal Brand }
706b0104773SPascal Brand 
707b0104773SPascal Brand /*
708b0104773SPascal Brand  * Back from execution of service: update parameters passed from TA:
709b0104773SPascal Brand  * If some parameters were memory references:
710b0104773SPascal Brand  * - either the memref was temporary: copy back data and update size
711b0104773SPascal Brand  * - or it was the original TA memref: update only the size value.
712b0104773SPascal Brand  */
713b0104773SPascal Brand static TEE_Result tee_svc_update_out_param(
714b0104773SPascal Brand 		struct tee_ta_param *param,
71537070d93SJens Wiklander 		void *tmp_buf_va[TEE_NUM_PARAMS],
716a401bcfbSBastien Simondi 		size_t tmp_buf_size[TEE_NUM_PARAMS],
717e86f1266SJens Wiklander 		struct utee_params *usr_param)
718b0104773SPascal Brand {
719b0104773SPascal Brand 	size_t n;
720359324a2SJens Wiklander 	uint64_t *vals = usr_param->vals;
721c40a6505SJens Wiklander 	size_t sz = 0;
722bc420748SJens Wiklander 
723b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
724b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
725b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
726b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
727b0104773SPascal Brand 			/*
728359324a2SJens Wiklander 			 * Memory copy is only needed if there's a temporary
729359324a2SJens Wiklander 			 * buffer involved, tmp_buf_va[n] is only update if
730359324a2SJens Wiklander 			 * a temporary buffer is used. Otherwise only the
731359324a2SJens Wiklander 			 * size needs to be updated.
732b0104773SPascal Brand 			 */
733c40a6505SJens Wiklander 			sz = param->u[n].mem.size;
734c40a6505SJens Wiklander 			if (tmp_buf_va[n] && sz <= vals[n * 2 + 1]) {
735359324a2SJens Wiklander 				void *src = tmp_buf_va[n];
736359324a2SJens Wiklander 				void *dst = (void *)(uintptr_t)vals[n * 2];
737c40a6505SJens Wiklander 				TEE_Result res = TEE_SUCCESS;
738b0104773SPascal Brand 
739a401bcfbSBastien Simondi 				/*
740a401bcfbSBastien Simondi 				 * TA is allowed to return a size larger than
741a401bcfbSBastien Simondi 				 * the original size. However, in such cases no
742a401bcfbSBastien Simondi 				 * data should be synchronized as per TEE Client
743a401bcfbSBastien Simondi 				 * API spec.
744a401bcfbSBastien Simondi 				 */
745c40a6505SJens Wiklander 				if (sz <= tmp_buf_size[n]) {
746c40a6505SJens Wiklander 					res = copy_to_user(dst, src, sz);
747b0104773SPascal Brand 					if (res != TEE_SUCCESS)
748b0104773SPascal Brand 						return res;
749a401bcfbSBastien Simondi 				}
750b0104773SPascal Brand 			}
751c40a6505SJens Wiklander 			usr_param->vals[n * 2 + 1] = sz;
752b0104773SPascal Brand 			break;
753b0104773SPascal Brand 
754b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
755b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_INOUT:
756359324a2SJens Wiklander 			vals[n * 2] = param->u[n].val.a;
757359324a2SJens Wiklander 			vals[n * 2 + 1] = param->u[n].val.b;
758b0104773SPascal Brand 			break;
759b0104773SPascal Brand 
760b0104773SPascal Brand 		default:
761b0104773SPascal Brand 			continue;
762b0104773SPascal Brand 		}
763b0104773SPascal Brand 	}
764b0104773SPascal Brand 
765b0104773SPascal Brand 	return TEE_SUCCESS;
766b0104773SPascal Brand }
767b0104773SPascal Brand 
768b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */
769453a5030SJerome Forissier TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
770e86f1266SJens Wiklander 			unsigned long cancel_req_to,
771e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ta_sess,
772b0104773SPascal Brand 			uint32_t *ret_orig)
773b0104773SPascal Brand {
774b0104773SPascal Brand 	TEE_Result res;
775b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
776b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
777b0104773SPascal Brand 	struct tee_ta_session *sess;
7786dbcd9ddSJens Wiklander 	struct mobj *mobj_param = NULL;
779b0104773SPascal Brand 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
780b0104773SPascal Brand 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
781b0104773SPascal Brand 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
782359324a2SJens Wiklander 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
783a401bcfbSBastien Simondi 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { 0 };
7848684fde8SJens Wiklander 	struct user_ta_ctx *utc;
785b0104773SPascal Brand 
786b0104773SPascal Brand 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
787b0104773SPascal Brand 		res = TEE_ERROR_OUT_OF_MEMORY;
788b0104773SPascal Brand 		goto out_free_only;
789b0104773SPascal Brand 	}
790b0104773SPascal Brand 
791b0104773SPascal Brand 	memset(param, 0, sizeof(struct tee_ta_param));
792b0104773SPascal Brand 
793b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
794b0104773SPascal Brand 	if (res != TEE_SUCCESS)
795b0104773SPascal Brand 		goto out_free_only;
7968684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
797b0104773SPascal Brand 
798*e12c9f67SJens Wiklander 	res = copy_from_user_private(uuid, dest, sizeof(TEE_UUID));
799b0104773SPascal Brand 	if (res != TEE_SUCCESS)
800b0104773SPascal Brand 		goto function_exit;
801b0104773SPascal Brand 
802b0104773SPascal Brand 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
803bc420748SJens Wiklander 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
804b0104773SPascal Brand 
80537070d93SJens Wiklander 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va,
806a401bcfbSBastien Simondi 				 tmp_buf_size, &mobj_param);
807b0104773SPascal Brand 	if (res != TEE_SUCCESS)
808b0104773SPascal Brand 		goto function_exit;
809b0104773SPascal Brand 
8108684fde8SJens Wiklander 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
81127cbcc57SJens Wiklander 				  clnt_id, cancel_req_to, param);
8121936dfc7SJens Wiklander 	tee_mmu_set_ctx(&utc->uctx.ctx);
813c0346845SJens Wiklander 	if (res != TEE_SUCCESS)
814b0104773SPascal Brand 		goto function_exit;
815b0104773SPascal Brand 
816a401bcfbSBastien Simondi 	res = tee_svc_update_out_param(param, tmp_buf_va, tmp_buf_size,
817a401bcfbSBastien Simondi 				       usr_param);
818b0104773SPascal Brand 
819b0104773SPascal Brand function_exit:
820b9651492SJens Wiklander 	mobj_put_wipe(mobj_param);
8212dcb3d36SJerome Forissier 	if (res == TEE_SUCCESS)
822*e12c9f67SJens Wiklander 		copy_to_user_private(ta_sess, &s->id, sizeof(s->id));
823*e12c9f67SJens Wiklander 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
824b0104773SPascal Brand 
825b0104773SPascal Brand out_free_only:
82670b61310SJerome Forissier 	free_wipe(param);
82770b61310SJerome Forissier 	free_wipe(uuid);
82870b61310SJerome Forissier 	free_wipe(clnt_id);
829b0104773SPascal Brand 	return res;
830b0104773SPascal Brand }
831b0104773SPascal Brand 
832e86f1266SJens Wiklander TEE_Result syscall_close_ta_session(unsigned long ta_sess)
833b0104773SPascal Brand {
834b0104773SPascal Brand 	TEE_Result res;
835b0104773SPascal Brand 	struct tee_ta_session *sess;
83660699957SPascal Brand 	TEE_Identity clnt_id;
83799164a05SJerome Forissier 	struct tee_ta_session *s = NULL;
8388684fde8SJens Wiklander 	struct user_ta_ctx *utc;
839b0104773SPascal Brand 
840b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
841b0104773SPascal Brand 	if (res != TEE_SUCCESS)
842b0104773SPascal Brand 		return res;
8438684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
84499164a05SJerome Forissier 	s = tee_ta_find_session(ta_sess, &utc->open_sessions);
845b0104773SPascal Brand 
84660699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
847bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
848b0104773SPascal Brand 
849a07c12b2SJens Wiklander 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
850b0104773SPascal Brand }
851b0104773SPascal Brand 
852e86f1266SJens Wiklander TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
853e86f1266SJens Wiklander 			unsigned long cancel_req_to, unsigned long cmd_id,
854e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ret_orig)
855b0104773SPascal Brand {
856b0104773SPascal Brand 	TEE_Result res;
8577d82e180SJens Wiklander 	TEE_Result res2;
858b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
859b0104773SPascal Brand 	struct tee_ta_param param = { 0 };
86060699957SPascal Brand 	TEE_Identity clnt_id;
861b0104773SPascal Brand 	struct tee_ta_session *sess;
862b666b6f2SJens Wiklander 	struct tee_ta_session *called_sess;
8636dbcd9ddSJens Wiklander 	struct mobj *mobj_param = NULL;
864359324a2SJens Wiklander 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
865a401bcfbSBastien Simondi 	size_t tmp_buf_size[TEE_NUM_PARAMS] = { };
8668684fde8SJens Wiklander 	struct user_ta_ctx *utc;
867b0104773SPascal Brand 
868b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
869b0104773SPascal Brand 	if (res != TEE_SUCCESS)
870b0104773SPascal Brand 		return res;
8718684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
872b0104773SPascal Brand 
87399164a05SJerome Forissier 	called_sess = tee_ta_get_session((uint32_t)ta_sess, true,
8748684fde8SJens Wiklander 				&utc->open_sessions);
875b666b6f2SJens Wiklander 	if (!called_sess)
876b666b6f2SJens Wiklander 		return TEE_ERROR_BAD_PARAMETERS;
877b0104773SPascal Brand 
87860699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
879bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
88060699957SPascal Brand 
881e86f1266SJens Wiklander 	res = tee_svc_copy_param(sess, called_sess, usr_param, &param,
882a401bcfbSBastien Simondi 				 tmp_buf_va, tmp_buf_size, &mobj_param);
883b0104773SPascal Brand 	if (res != TEE_SUCCESS)
884b0104773SPascal Brand 		goto function_exit;
885b0104773SPascal Brand 
88660699957SPascal Brand 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
88760699957SPascal Brand 				    cancel_req_to, cmd_id, &param);
888fd10f62bSOvidiu Mihalachi 	if (res == TEE_ERROR_TARGET_DEAD)
889fd10f62bSOvidiu Mihalachi 		goto function_exit;
89060699957SPascal Brand 
891a401bcfbSBastien Simondi 	res2 = tee_svc_update_out_param(&param, tmp_buf_va, tmp_buf_size,
892a401bcfbSBastien Simondi 					usr_param);
8937d82e180SJens Wiklander 	if (res2 != TEE_SUCCESS) {
8947d82e180SJens Wiklander 		/*
8957d82e180SJens Wiklander 		 * Spec for TEE_InvokeTACommand() says:
8967d82e180SJens Wiklander 		 * "If the return origin is different from
8977d82e180SJens Wiklander 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
8987d82e180SJens Wiklander 		 * before it could reach the destination Trusted
8997d82e180SJens Wiklander 		 * Application."
9007d82e180SJens Wiklander 		 *
9017d82e180SJens Wiklander 		 * But if we can't update params to the caller we have no
9027d82e180SJens Wiklander 		 * choice we need to return some error to indicate that
9037d82e180SJens Wiklander 		 * parameters aren't updated as expected.
9047d82e180SJens Wiklander 		 */
9057d82e180SJens Wiklander 		ret_o = TEE_ORIGIN_TEE;
9067d82e180SJens Wiklander 		res = res2;
9077d82e180SJens Wiklander 	}
908b0104773SPascal Brand 
909b0104773SPascal Brand function_exit:
910b666b6f2SJens Wiklander 	tee_ta_put_session(called_sess);
911b9651492SJens Wiklander 	mobj_put_wipe(mobj_param);
912*e12c9f67SJens Wiklander 	copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o));
913b0104773SPascal Brand 	return res;
914b0104773SPascal Brand }
915b0104773SPascal Brand 
916e86f1266SJens Wiklander TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
917b0104773SPascal Brand 				       size_t len)
918b0104773SPascal Brand {
9191936dfc7SJens Wiklander 	struct tee_ta_session *s = NULL;
9201936dfc7SJens Wiklander 	TEE_Result res = TEE_SUCCESS;
921b0104773SPascal Brand 
922b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
923b0104773SPascal Brand 	if (res != TEE_SUCCESS)
924b0104773SPascal Brand 		return res;
925b0104773SPascal Brand 
9261936dfc7SJens Wiklander 	return tee_mmu_check_access_rights(&to_user_ta_ctx(s->ctx)->uctx, flags,
9272ffdd194SJens Wiklander 					   (uaddr_t)buf, len);
928b0104773SPascal Brand }
929b0104773SPascal Brand 
930e86f1266SJens Wiklander TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
931b0104773SPascal Brand {
932b0104773SPascal Brand 	TEE_Result res;
933b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
934e86f1266SJens Wiklander 	uint32_t c;
935b0104773SPascal Brand 
936b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
937b0104773SPascal Brand 	if (res != TEE_SUCCESS)
938b0104773SPascal Brand 		return res;
939b0104773SPascal Brand 
94063dc8d4aSJens Wiklander 	c = tee_ta_session_is_cancelled(s, NULL);
941b0104773SPascal Brand 
942c40a6505SJens Wiklander 	return copy_to_user(cancel, &c, sizeof(c));
943b0104773SPascal Brand }
944b0104773SPascal Brand 
945e86f1266SJens Wiklander TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
946b0104773SPascal Brand {
947b0104773SPascal Brand 	TEE_Result res;
948b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
949e86f1266SJens Wiklander 	uint32_t m;
950b0104773SPascal Brand 
951b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
952b0104773SPascal Brand 	if (res != TEE_SUCCESS)
953b0104773SPascal Brand 		return res;
954b0104773SPascal Brand 
955b0104773SPascal Brand 	m = s->cancel_mask;
956b0104773SPascal Brand 	s->cancel_mask = false;
957c40a6505SJens Wiklander 	return copy_to_user(old_mask, &m, sizeof(m));
958b0104773SPascal Brand }
959b0104773SPascal Brand 
960e86f1266SJens Wiklander TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
961b0104773SPascal Brand {
962b0104773SPascal Brand 	TEE_Result res;
963b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
964e86f1266SJens Wiklander 	uint32_t m;
965b0104773SPascal Brand 
966b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
967b0104773SPascal Brand 	if (res != TEE_SUCCESS)
968b0104773SPascal Brand 		return res;
969b0104773SPascal Brand 
970b0104773SPascal Brand 	m = s->cancel_mask;
971b0104773SPascal Brand 	s->cancel_mask = true;
972c40a6505SJens Wiklander 	return copy_to_user(old_mask, &m, sizeof(m));
973b0104773SPascal Brand }
974b0104773SPascal Brand 
975e86f1266SJens Wiklander TEE_Result syscall_wait(unsigned long timeout)
976b0104773SPascal Brand {
977b0104773SPascal Brand 	TEE_Result res = TEE_SUCCESS;
978b0104773SPascal Brand 	uint32_t mytime = 0;
979b0104773SPascal Brand 	struct tee_ta_session *s;
980b0104773SPascal Brand 	TEE_Time base_time;
981b0104773SPascal Brand 	TEE_Time current_time;
982b0104773SPascal Brand 
983b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
984b0104773SPascal Brand 	if (res != TEE_SUCCESS)
985b0104773SPascal Brand 		return res;
986b0104773SPascal Brand 
987b0104773SPascal Brand 	res = tee_time_get_sys_time(&base_time);
988b0104773SPascal Brand 	if (res != TEE_SUCCESS)
989b0104773SPascal Brand 		return res;
990b0104773SPascal Brand 
991b0104773SPascal Brand 	while (true) {
992b0104773SPascal Brand 		res = tee_time_get_sys_time(&current_time);
993b0104773SPascal Brand 		if (res != TEE_SUCCESS)
994b0104773SPascal Brand 			return res;
995b0104773SPascal Brand 
99663dc8d4aSJens Wiklander 		if (tee_ta_session_is_cancelled(s, &current_time))
997b0104773SPascal Brand 			return TEE_ERROR_CANCEL;
998b0104773SPascal Brand 
999b0104773SPascal Brand 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1000b0104773SPascal Brand 		    (int)current_time.millis - (int)base_time.millis;
1001b0104773SPascal Brand 		if (mytime >= timeout)
1002b0104773SPascal Brand 			return TEE_SUCCESS;
1003b0104773SPascal Brand 
1004d1aea08fSSY Chiu 		tee_time_wait(timeout - mytime);
1005b0104773SPascal Brand 	}
1006b0104773SPascal Brand 
1007b0104773SPascal Brand 	return res;
1008b0104773SPascal Brand }
1009b0104773SPascal Brand 
1010e86f1266SJens Wiklander TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1011b0104773SPascal Brand {
1012b0104773SPascal Brand 	TEE_Result res, res2;
1013b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1014b0104773SPascal Brand 	TEE_Time t;
1015b0104773SPascal Brand 
1016b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1017b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1018b0104773SPascal Brand 		return res;
1019b0104773SPascal Brand 
1020b0104773SPascal Brand 	switch (cat) {
1021b0104773SPascal Brand 	case UTEE_TIME_CAT_SYSTEM:
1022b0104773SPascal Brand 		res = tee_time_get_sys_time(&t);
1023b0104773SPascal Brand 		break;
1024b0104773SPascal Brand 	case UTEE_TIME_CAT_TA_PERSISTENT:
1025bc420748SJens Wiklander 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1026b0104773SPascal Brand 		break;
1027b0104773SPascal Brand 	case UTEE_TIME_CAT_REE:
1028b0104773SPascal Brand 		res = tee_time_get_ree_time(&t);
1029b0104773SPascal Brand 		break;
1030b0104773SPascal Brand 	default:
1031b0104773SPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
1032b0104773SPascal Brand 		break;
1033b0104773SPascal Brand 	}
1034b0104773SPascal Brand 
1035b0104773SPascal Brand 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1036*e12c9f67SJens Wiklander 		res2 = copy_to_user_private(mytime, &t, sizeof(t));
1037b0104773SPascal Brand 		if (res2 != TEE_SUCCESS)
1038b0104773SPascal Brand 			res = res2;
1039b0104773SPascal Brand 	}
1040b0104773SPascal Brand 
1041b0104773SPascal Brand 	return res;
1042b0104773SPascal Brand }
1043b0104773SPascal Brand 
1044453a5030SJerome Forissier TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1045b0104773SPascal Brand {
1046b0104773SPascal Brand 	TEE_Result res;
1047b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1048b0104773SPascal Brand 	TEE_Time t;
1049b0104773SPascal Brand 
1050b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1051b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1052b0104773SPascal Brand 		return res;
1053b0104773SPascal Brand 
1054*e12c9f67SJens Wiklander 	res = copy_from_user_private(&t, mytime, sizeof(t));
1055b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1056b0104773SPascal Brand 		return res;
1057b0104773SPascal Brand 
1058bc420748SJens Wiklander 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1059b0104773SPascal Brand }
1060