xref: /optee_os/core/tee/tee_svc.c (revision fd10f62b821033f20d6fdc73cfd804d18806b46d)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
4b0104773SPascal Brand  */
52eb765fcSJens Wiklander #include <util.h>
6b0104773SPascal Brand #include <kernel/tee_common_otp.h>
7b0104773SPascal Brand #include <kernel/tee_common.h>
8b0104773SPascal Brand #include <tee_api_types.h>
9b0104773SPascal Brand #include <kernel/tee_ta_manager.h>
10b0104773SPascal Brand #include <utee_types.h>
11b0104773SPascal Brand #include <tee/tee_svc.h>
12ffe04039SJerome Forissier #include <tee/tee_cryp_utl.h>
13b0104773SPascal Brand #include <mm/tee_mmu.h>
14b0104773SPascal Brand #include <mm/tee_mm.h>
1543e30efdSJens Wiklander #include <mm/core_memprot.h>
16b0104773SPascal Brand #include <kernel/tee_time.h>
17b0104773SPascal Brand 
18b0104773SPascal Brand #include <user_ta_header.h>
194de4bebcSJens Wiklander #include <trace.h>
204de4bebcSJens Wiklander #include <kernel/trace_ta.h>
21b0104773SPascal Brand #include <kernel/chip_services.h>
2242fb5b2eSEtienne Carriere #include <kernel/pseudo_ta.h>
230dcfa568SJens Wiklander #include <mm/mobj.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 
44b0104773SPascal Brand 	free(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:
49364a5011eSPascal Brand 	free(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],
5676dbcd9ddSJens Wiklander 				     struct mobj **mobj_tmp)
568b0104773SPascal Brand {
569b0104773SPascal Brand 	size_t n;
570b0104773SPascal Brand 	TEE_Result res;
571b0104773SPascal Brand 	size_t req_mem = 0;
572b0104773SPascal Brand 	size_t s;
573b0104773SPascal Brand 	uint8_t *dst = 0;
574b0104773SPascal Brand 	bool ta_private_memref[TEE_NUM_PARAMS];
5758684fde8SJens Wiklander 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
5760dcfa568SJens Wiklander 	void *va;
5770dcfa568SJens Wiklander 	size_t dst_offs;
578b0104773SPascal Brand 
579b7fc217fSPascal Brand 	/* fill 'param' input struct with caller params description buffer */
580b7fc217fSPascal Brand 	if (!callee_params) {
581e86f1266SJens Wiklander 		memset(param, 0, sizeof(*param));
582b0104773SPascal Brand 	} else {
5838684fde8SJens Wiklander 		res = tee_mmu_check_access_rights(utc,
584177603c7SJens Wiklander 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
5852ffdd194SJens Wiklander 			(uaddr_t)callee_params, sizeof(struct utee_params));
586177603c7SJens Wiklander 		if (res != TEE_SUCCESS)
587177603c7SJens Wiklander 			return res;
588d5c5b0b7SJens Wiklander 		res = utee_param_to_param(utc, param, callee_params);
589d5c5b0b7SJens Wiklander 		if (res != TEE_SUCCESS)
590d5c5b0b7SJens Wiklander 			return res;
591b0104773SPascal Brand 	}
592b0104773SPascal Brand 
59342fb5b2eSEtienne Carriere 	if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) {
59435964dc9SEtienne Carriere 		/* pseudo TA borrows the mapping of the calling TA */
595b0104773SPascal Brand 		return TEE_SUCCESS;
596b0104773SPascal Brand 	}
597b0104773SPascal Brand 
5980dcfa568SJens Wiklander 	/* All mobj in param are of type MOJB_TYPE_VIRT */
5990dcfa568SJens Wiklander 
600b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
601b0104773SPascal Brand 
602b0104773SPascal Brand 		ta_private_memref[n] = false;
603b0104773SPascal Brand 
604b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
605b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
606b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
607b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
6080dcfa568SJens Wiklander 			va = (void *)param->u[n].mem.offs;
6090dcfa568SJens Wiklander 			s = param->u[n].mem.size;
6100dcfa568SJens Wiklander 			if (!va) {
611b0961f98SJerome Forissier 				if (s)
612b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
613b0104773SPascal Brand 				break;
614b0104773SPascal Brand 			}
615b0104773SPascal Brand 			/* uTA cannot expose its private memory */
6160dcfa568SJens Wiklander 			if (tee_mmu_is_vbuf_inside_ta_private(utc, va, s)) {
617b0104773SPascal Brand 
6180dcfa568SJens Wiklander 				s = ROUNDUP(s, sizeof(uint32_t));
6198d0f8b46SJens Wiklander 				if (ADD_OVERFLOW(req_mem, s, &req_mem))
620b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
621b0104773SPascal Brand 				ta_private_memref[n] = true;
622b0104773SPascal Brand 				break;
623b0104773SPascal Brand 			}
624b0104773SPascal Brand 
6250dcfa568SJens Wiklander 			res = tee_mmu_vbuf_to_mobj_offs(utc, va, s,
6260dcfa568SJens Wiklander 							&param->u[n].mem.mobj,
6270dcfa568SJens Wiklander 							&param->u[n].mem.offs);
6280dcfa568SJens Wiklander 			if (res != TEE_SUCCESS)
6290dcfa568SJens Wiklander 				return res;
630b0104773SPascal Brand 			break;
631b0104773SPascal Brand 		default:
632b0104773SPascal Brand 			break;
633b0104773SPascal Brand 		}
634b0104773SPascal Brand 	}
635b0104773SPascal Brand 
636b0104773SPascal Brand 	if (req_mem == 0)
637b0104773SPascal Brand 		return TEE_SUCCESS;
638b0104773SPascal Brand 
6396dbcd9ddSJens Wiklander 	res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst);
6400dcfa568SJens Wiklander 	if (res != TEE_SUCCESS)
6410dcfa568SJens Wiklander 		return res;
6426dbcd9ddSJens Wiklander 	dst_offs = 0;
643b0104773SPascal Brand 
64468540524SIgor Opaniuk 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
645b0104773SPascal Brand 
6460dcfa568SJens Wiklander 		if (!ta_private_memref[n])
647b0104773SPascal Brand 			continue;
648b0104773SPascal Brand 
6490dcfa568SJens Wiklander 		s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t));
650b0104773SPascal Brand 
651b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
652b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
653b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
6540dcfa568SJens Wiklander 			va = (void *)param->u[n].mem.offs;
6550dcfa568SJens Wiklander 			if (va) {
6560dcfa568SJens Wiklander 				res = tee_svc_copy_from_user(dst, va,
6570dcfa568SJens Wiklander 						param->u[n].mem.size);
658b0104773SPascal Brand 				if (res != TEE_SUCCESS)
659b0104773SPascal Brand 					return res;
6600dcfa568SJens Wiklander 				param->u[n].mem.offs = dst_offs;
6616dbcd9ddSJens Wiklander 				param->u[n].mem.mobj = *mobj_tmp;
66237070d93SJens Wiklander 				tmp_buf_va[n] = dst;
663b0104773SPascal Brand 				dst += s;
6640dcfa568SJens Wiklander 				dst_offs += s;
665b0104773SPascal Brand 			}
666b0104773SPascal Brand 			break;
667b0104773SPascal Brand 
668b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
6690dcfa568SJens Wiklander 			va = (void *)param->u[n].mem.offs;
6700dcfa568SJens Wiklander 			if (va) {
6710dcfa568SJens Wiklander 				param->u[n].mem.offs = dst_offs;
6726dbcd9ddSJens Wiklander 				param->u[n].mem.mobj = *mobj_tmp;
67337070d93SJens Wiklander 				tmp_buf_va[n] = dst;
674b0104773SPascal Brand 				dst += s;
6750dcfa568SJens Wiklander 				dst_offs += s;
676b0104773SPascal Brand 			}
677b0104773SPascal Brand 			break;
678b0104773SPascal Brand 
679b0104773SPascal Brand 		default:
680b0104773SPascal Brand 			continue;
681b0104773SPascal Brand 		}
682b0104773SPascal Brand 	}
683b0104773SPascal Brand 
684b0104773SPascal Brand 	return TEE_SUCCESS;
685b0104773SPascal Brand }
686b0104773SPascal Brand 
687b0104773SPascal Brand /*
688b0104773SPascal Brand  * Back from execution of service: update parameters passed from TA:
689b0104773SPascal Brand  * If some parameters were memory references:
690b0104773SPascal Brand  * - either the memref was temporary: copy back data and update size
691b0104773SPascal Brand  * - or it was the original TA memref: update only the size value.
692b0104773SPascal Brand  */
693b0104773SPascal Brand static TEE_Result tee_svc_update_out_param(
694b0104773SPascal Brand 		struct tee_ta_param *param,
69537070d93SJens Wiklander 		void *tmp_buf_va[TEE_NUM_PARAMS],
696e86f1266SJens Wiklander 		struct utee_params *usr_param)
697b0104773SPascal Brand {
698b0104773SPascal Brand 	size_t n;
699359324a2SJens Wiklander 	uint64_t *vals = usr_param->vals;
700bc420748SJens Wiklander 
701b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
702b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
703b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
704b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
705b0104773SPascal Brand 			/*
706359324a2SJens Wiklander 			 * Memory copy is only needed if there's a temporary
707359324a2SJens Wiklander 			 * buffer involved, tmp_buf_va[n] is only update if
708359324a2SJens Wiklander 			 * a temporary buffer is used. Otherwise only the
709359324a2SJens Wiklander 			 * size needs to be updated.
710b0104773SPascal Brand 			 */
711359324a2SJens Wiklander 			if (tmp_buf_va[n] &&
712359324a2SJens Wiklander 			    param->u[n].mem.size <= vals[n * 2 + 1]) {
713359324a2SJens Wiklander 				void *src = tmp_buf_va[n];
714359324a2SJens Wiklander 				void *dst = (void *)(uintptr_t)vals[n * 2];
715b0104773SPascal Brand 				TEE_Result res;
716b0104773SPascal Brand 
717359324a2SJens Wiklander 				res = tee_svc_copy_to_user(dst, src,
7180dcfa568SJens Wiklander 						 param->u[n].mem.size);
719b0104773SPascal Brand 				if (res != TEE_SUCCESS)
720b0104773SPascal Brand 					return res;
721b0104773SPascal Brand 
722b0104773SPascal Brand 			}
7230dcfa568SJens Wiklander 			usr_param->vals[n * 2 + 1] = param->u[n].mem.size;
724b0104773SPascal Brand 			break;
725b0104773SPascal Brand 
726b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
727b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_INOUT:
728359324a2SJens Wiklander 			vals[n * 2] = param->u[n].val.a;
729359324a2SJens Wiklander 			vals[n * 2 + 1] = param->u[n].val.b;
730b0104773SPascal Brand 			break;
731b0104773SPascal Brand 
732b0104773SPascal Brand 		default:
733b0104773SPascal Brand 			continue;
734b0104773SPascal Brand 		}
735b0104773SPascal Brand 	}
736b0104773SPascal Brand 
737b0104773SPascal Brand 	return TEE_SUCCESS;
738b0104773SPascal Brand }
739b0104773SPascal Brand 
740b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */
741453a5030SJerome Forissier TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
742e86f1266SJens Wiklander 			unsigned long cancel_req_to,
743e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ta_sess,
744b0104773SPascal Brand 			uint32_t *ret_orig)
745b0104773SPascal Brand {
746b0104773SPascal Brand 	TEE_Result res;
747b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
748b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
749b0104773SPascal Brand 	struct tee_ta_session *sess;
7506dbcd9ddSJens Wiklander 	struct mobj *mobj_param = NULL;
751b0104773SPascal Brand 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
752b0104773SPascal Brand 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
753b0104773SPascal Brand 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
754359324a2SJens Wiklander 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
7558684fde8SJens Wiklander 	struct user_ta_ctx *utc;
756b0104773SPascal Brand 
757b0104773SPascal Brand 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
758b0104773SPascal Brand 		res = TEE_ERROR_OUT_OF_MEMORY;
759b0104773SPascal Brand 		goto out_free_only;
760b0104773SPascal Brand 	}
761b0104773SPascal Brand 
762b0104773SPascal Brand 	memset(param, 0, sizeof(struct tee_ta_param));
763b0104773SPascal Brand 
764b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
765b0104773SPascal Brand 	if (res != TEE_SUCCESS)
766b0104773SPascal Brand 		goto out_free_only;
7678684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
768b0104773SPascal Brand 
769eebf7990SJens Wiklander 	res = tee_svc_copy_from_user(uuid, dest, sizeof(TEE_UUID));
770b0104773SPascal Brand 	if (res != TEE_SUCCESS)
771b0104773SPascal Brand 		goto function_exit;
772b0104773SPascal Brand 
773b0104773SPascal Brand 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
774bc420748SJens Wiklander 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
775b0104773SPascal Brand 
77637070d93SJens Wiklander 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va,
7776dbcd9ddSJens Wiklander 				 &mobj_param);
778b0104773SPascal Brand 	if (res != TEE_SUCCESS)
779b0104773SPascal Brand 		goto function_exit;
780b0104773SPascal Brand 
7818684fde8SJens Wiklander 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
78227cbcc57SJens Wiklander 				  clnt_id, cancel_req_to, param);
7835c39e05dSJerome Forissier 	tee_mmu_set_ctx(&utc->ctx);
784c0346845SJens Wiklander 	if (res != TEE_SUCCESS)
785b0104773SPascal Brand 		goto function_exit;
786b0104773SPascal Brand 
787359324a2SJens Wiklander 	res = tee_svc_update_out_param(param, tmp_buf_va, usr_param);
788b0104773SPascal Brand 
789b0104773SPascal Brand function_exit:
7906dbcd9ddSJens Wiklander 	mobj_free(mobj_param);
7912dcb3d36SJerome Forissier 	if (res == TEE_SUCCESS)
79299164a05SJerome Forissier 		tee_svc_copy_to_user(ta_sess, &s->id, sizeof(s->id));
793eebf7990SJens Wiklander 	tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o));
794b0104773SPascal Brand 
795b0104773SPascal Brand out_free_only:
796b0104773SPascal Brand 	free(param);
797b0104773SPascal Brand 	free(uuid);
798b0104773SPascal Brand 	free(clnt_id);
799b0104773SPascal Brand 	return res;
800b0104773SPascal Brand }
801b0104773SPascal Brand 
802e86f1266SJens Wiklander TEE_Result syscall_close_ta_session(unsigned long ta_sess)
803b0104773SPascal Brand {
804b0104773SPascal Brand 	TEE_Result res;
805b0104773SPascal Brand 	struct tee_ta_session *sess;
80660699957SPascal Brand 	TEE_Identity clnt_id;
80799164a05SJerome Forissier 	struct tee_ta_session *s = NULL;
8088684fde8SJens Wiklander 	struct user_ta_ctx *utc;
809b0104773SPascal Brand 
810b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
811b0104773SPascal Brand 	if (res != TEE_SUCCESS)
812b0104773SPascal Brand 		return res;
8138684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
81499164a05SJerome Forissier 	s = tee_ta_find_session(ta_sess, &utc->open_sessions);
815b0104773SPascal Brand 
81660699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
817bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
818b0104773SPascal Brand 
819a07c12b2SJens Wiklander 	return tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
820b0104773SPascal Brand }
821b0104773SPascal Brand 
822e86f1266SJens Wiklander TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
823e86f1266SJens Wiklander 			unsigned long cancel_req_to, unsigned long cmd_id,
824e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ret_orig)
825b0104773SPascal Brand {
826b0104773SPascal Brand 	TEE_Result res;
8277d82e180SJens Wiklander 	TEE_Result res2;
828b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
829b0104773SPascal Brand 	struct tee_ta_param param = { 0 };
83060699957SPascal Brand 	TEE_Identity clnt_id;
831b0104773SPascal Brand 	struct tee_ta_session *sess;
832b666b6f2SJens Wiklander 	struct tee_ta_session *called_sess;
8336dbcd9ddSJens Wiklander 	struct mobj *mobj_param = NULL;
834359324a2SJens Wiklander 	void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL };
8358684fde8SJens Wiklander 	struct user_ta_ctx *utc;
836b0104773SPascal Brand 
837b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
838b0104773SPascal Brand 	if (res != TEE_SUCCESS)
839b0104773SPascal Brand 		return res;
8408684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
841b0104773SPascal Brand 
84299164a05SJerome Forissier 	called_sess = tee_ta_get_session((uint32_t)ta_sess, true,
8438684fde8SJens Wiklander 				&utc->open_sessions);
844b666b6f2SJens Wiklander 	if (!called_sess)
845b666b6f2SJens Wiklander 		return TEE_ERROR_BAD_PARAMETERS;
846b0104773SPascal Brand 
84760699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
848bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
84960699957SPascal Brand 
850e86f1266SJens Wiklander 	res = tee_svc_copy_param(sess, called_sess, usr_param, &param,
8516dbcd9ddSJens Wiklander 				 tmp_buf_va, &mobj_param);
852b0104773SPascal Brand 	if (res != TEE_SUCCESS)
853b0104773SPascal Brand 		goto function_exit;
854b0104773SPascal Brand 
85560699957SPascal Brand 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
85660699957SPascal Brand 				    cancel_req_to, cmd_id, &param);
857*fd10f62bSOvidiu Mihalachi 	if (res == TEE_ERROR_TARGET_DEAD)
858*fd10f62bSOvidiu Mihalachi 		goto function_exit;
85960699957SPascal Brand 
860359324a2SJens Wiklander 	res2 = tee_svc_update_out_param(&param, tmp_buf_va, usr_param);
8617d82e180SJens Wiklander 	if (res2 != TEE_SUCCESS) {
8627d82e180SJens Wiklander 		/*
8637d82e180SJens Wiklander 		 * Spec for TEE_InvokeTACommand() says:
8647d82e180SJens Wiklander 		 * "If the return origin is different from
8657d82e180SJens Wiklander 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
8667d82e180SJens Wiklander 		 * before it could reach the destination Trusted
8677d82e180SJens Wiklander 		 * Application."
8687d82e180SJens Wiklander 		 *
8697d82e180SJens Wiklander 		 * But if we can't update params to the caller we have no
8707d82e180SJens Wiklander 		 * choice we need to return some error to indicate that
8717d82e180SJens Wiklander 		 * parameters aren't updated as expected.
8727d82e180SJens Wiklander 		 */
8737d82e180SJens Wiklander 		ret_o = TEE_ORIGIN_TEE;
8747d82e180SJens Wiklander 		res = res2;
8757d82e180SJens Wiklander 	}
876b0104773SPascal Brand 
877b0104773SPascal Brand function_exit:
878b666b6f2SJens Wiklander 	tee_ta_put_session(called_sess);
8796dbcd9ddSJens Wiklander 	mobj_free(mobj_param);
880b0104773SPascal Brand 	if (ret_orig)
881eebf7990SJens Wiklander 		tee_svc_copy_to_user(ret_orig, &ret_o, sizeof(ret_o));
882b0104773SPascal Brand 	return res;
883b0104773SPascal Brand }
884b0104773SPascal Brand 
885e86f1266SJens Wiklander TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
886b0104773SPascal Brand 				       size_t len)
887b0104773SPascal Brand {
888b0104773SPascal Brand 	TEE_Result res;
889b0104773SPascal Brand 	struct tee_ta_session *s;
890b0104773SPascal Brand 
891b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
892b0104773SPascal Brand 	if (res != TEE_SUCCESS)
893b0104773SPascal Brand 		return res;
894b0104773SPascal Brand 
8958684fde8SJens Wiklander 	return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags,
8962ffdd194SJens Wiklander 					   (uaddr_t)buf, len);
897b0104773SPascal Brand }
898b0104773SPascal Brand 
899eebf7990SJens Wiklander TEE_Result tee_svc_copy_from_user(void *kaddr, const void *uaddr, size_t len)
900b0104773SPascal Brand {
901b0104773SPascal Brand 	TEE_Result res;
902b0104773SPascal Brand 	struct tee_ta_session *s;
903b0104773SPascal Brand 
904b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
905b0104773SPascal Brand 	if (res != TEE_SUCCESS)
906b0104773SPascal Brand 		return res;
907eebf7990SJens Wiklander 
9088684fde8SJens Wiklander 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
909b0104773SPascal Brand 					TEE_MEMORY_ACCESS_READ |
910b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
9112ffdd194SJens Wiklander 					(uaddr_t)uaddr, len);
912b0104773SPascal Brand 	if (res != TEE_SUCCESS)
913b0104773SPascal Brand 		return res;
914b0104773SPascal Brand 
915b0104773SPascal Brand 	memcpy(kaddr, uaddr, len);
916b0104773SPascal Brand 	return TEE_SUCCESS;
917b0104773SPascal Brand }
918b0104773SPascal Brand 
919eebf7990SJens Wiklander TEE_Result tee_svc_copy_to_user(void *uaddr, const void *kaddr, size_t len)
920b0104773SPascal Brand {
921b0104773SPascal Brand 	TEE_Result res;
922b0104773SPascal Brand 	struct tee_ta_session *s;
923b0104773SPascal Brand 
924b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
925b0104773SPascal Brand 	if (res != TEE_SUCCESS)
926b0104773SPascal Brand 		return res;
927b0104773SPascal Brand 
9288684fde8SJens Wiklander 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
929b0104773SPascal Brand 					TEE_MEMORY_ACCESS_WRITE |
930b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
9312ffdd194SJens Wiklander 					(uaddr_t)uaddr, len);
932b0104773SPascal Brand 	if (res != TEE_SUCCESS)
933b0104773SPascal Brand 		return res;
934b0104773SPascal Brand 
935b0104773SPascal Brand 	memcpy(uaddr, kaddr, len);
936b0104773SPascal Brand 	return TEE_SUCCESS;
937b0104773SPascal Brand }
938b0104773SPascal Brand 
939eebf7990SJens Wiklander TEE_Result tee_svc_copy_kaddr_to_uref(uint32_t *uref, void *kaddr)
9408707ec0fSJerome Forissier {
941e86f1266SJens Wiklander 	uint32_t ref = tee_svc_kaddr_to_uref(kaddr);
9428707ec0fSJerome Forissier 
943eebf7990SJens Wiklander 	return tee_svc_copy_to_user(uref, &ref, sizeof(ref));
9448707ec0fSJerome Forissier }
9458707ec0fSJerome Forissier 
946e86f1266SJens Wiklander TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
947b0104773SPascal Brand {
948b0104773SPascal Brand 	TEE_Result res;
949b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
950e86f1266SJens Wiklander 	uint32_t c;
951b0104773SPascal Brand 
952b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
953b0104773SPascal Brand 	if (res != TEE_SUCCESS)
954b0104773SPascal Brand 		return res;
955b0104773SPascal Brand 
95663dc8d4aSJens Wiklander 	c = tee_ta_session_is_cancelled(s, NULL);
957b0104773SPascal Brand 
958eebf7990SJens Wiklander 	return tee_svc_copy_to_user(cancel, &c, sizeof(c));
959b0104773SPascal Brand }
960b0104773SPascal Brand 
961e86f1266SJens Wiklander TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
962b0104773SPascal Brand {
963b0104773SPascal Brand 	TEE_Result res;
964b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
965e86f1266SJens Wiklander 	uint32_t m;
966b0104773SPascal Brand 
967b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
968b0104773SPascal Brand 	if (res != TEE_SUCCESS)
969b0104773SPascal Brand 		return res;
970b0104773SPascal Brand 
971b0104773SPascal Brand 	m = s->cancel_mask;
972b0104773SPascal Brand 	s->cancel_mask = false;
973eebf7990SJens Wiklander 	return tee_svc_copy_to_user(old_mask, &m, sizeof(m));
974b0104773SPascal Brand }
975b0104773SPascal Brand 
976e86f1266SJens Wiklander TEE_Result syscall_mask_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 = true;
988eebf7990SJens Wiklander 	return tee_svc_copy_to_user(old_mask, &m, sizeof(m));
989b0104773SPascal Brand }
990b0104773SPascal Brand 
991e86f1266SJens Wiklander TEE_Result syscall_wait(unsigned long timeout)
992b0104773SPascal Brand {
993b0104773SPascal Brand 	TEE_Result res = TEE_SUCCESS;
994b0104773SPascal Brand 	uint32_t mytime = 0;
995b0104773SPascal Brand 	struct tee_ta_session *s;
996b0104773SPascal Brand 	TEE_Time base_time;
997b0104773SPascal Brand 	TEE_Time current_time;
998b0104773SPascal Brand 
999b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1000b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1001b0104773SPascal Brand 		return res;
1002b0104773SPascal Brand 
1003b0104773SPascal Brand 	res = tee_time_get_sys_time(&base_time);
1004b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1005b0104773SPascal Brand 		return res;
1006b0104773SPascal Brand 
1007b0104773SPascal Brand 	while (true) {
1008b0104773SPascal Brand 		res = tee_time_get_sys_time(&current_time);
1009b0104773SPascal Brand 		if (res != TEE_SUCCESS)
1010b0104773SPascal Brand 			return res;
1011b0104773SPascal Brand 
101263dc8d4aSJens Wiklander 		if (tee_ta_session_is_cancelled(s, &current_time))
1013b0104773SPascal Brand 			return TEE_ERROR_CANCEL;
1014b0104773SPascal Brand 
1015b0104773SPascal Brand 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1016b0104773SPascal Brand 		    (int)current_time.millis - (int)base_time.millis;
1017b0104773SPascal Brand 		if (mytime >= timeout)
1018b0104773SPascal Brand 			return TEE_SUCCESS;
1019b0104773SPascal Brand 
1020d1aea08fSSY Chiu 		tee_time_wait(timeout - mytime);
1021b0104773SPascal Brand 	}
1022b0104773SPascal Brand 
1023b0104773SPascal Brand 	return res;
1024b0104773SPascal Brand }
1025b0104773SPascal Brand 
1026e86f1266SJens Wiklander TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1027b0104773SPascal Brand {
1028b0104773SPascal Brand 	TEE_Result res, res2;
1029b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1030b0104773SPascal Brand 	TEE_Time t;
1031b0104773SPascal Brand 
1032b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1033b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1034b0104773SPascal Brand 		return res;
1035b0104773SPascal Brand 
1036b0104773SPascal Brand 	switch (cat) {
1037b0104773SPascal Brand 	case UTEE_TIME_CAT_SYSTEM:
1038b0104773SPascal Brand 		res = tee_time_get_sys_time(&t);
1039b0104773SPascal Brand 		break;
1040b0104773SPascal Brand 	case UTEE_TIME_CAT_TA_PERSISTENT:
1041bc420748SJens Wiklander 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1042b0104773SPascal Brand 		break;
1043b0104773SPascal Brand 	case UTEE_TIME_CAT_REE:
1044b0104773SPascal Brand 		res = tee_time_get_ree_time(&t);
1045b0104773SPascal Brand 		break;
1046b0104773SPascal Brand 	default:
1047b0104773SPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
1048b0104773SPascal Brand 		break;
1049b0104773SPascal Brand 	}
1050b0104773SPascal Brand 
1051b0104773SPascal Brand 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1052eebf7990SJens Wiklander 		res2 = tee_svc_copy_to_user(mytime, &t, sizeof(t));
1053b0104773SPascal Brand 		if (res2 != TEE_SUCCESS)
1054b0104773SPascal Brand 			res = res2;
1055b0104773SPascal Brand 	}
1056b0104773SPascal Brand 
1057b0104773SPascal Brand 	return res;
1058b0104773SPascal Brand }
1059b0104773SPascal Brand 
1060453a5030SJerome Forissier TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1061b0104773SPascal Brand {
1062b0104773SPascal Brand 	TEE_Result res;
1063b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1064b0104773SPascal Brand 	TEE_Time t;
1065b0104773SPascal Brand 
1066b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1067b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1068b0104773SPascal Brand 		return res;
1069b0104773SPascal Brand 
1070eebf7990SJens Wiklander 	res = tee_svc_copy_from_user(&t, mytime, sizeof(t));
1071b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1072b0104773SPascal Brand 		return res;
1073b0104773SPascal Brand 
1074bc420748SJens Wiklander 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1075b0104773SPascal Brand }
1076