xref: /optee_os/core/tee/tee_svc.c (revision 36bf7ea889946ea981ead44ad43940f3b7e5d74c)
1b0104773SPascal Brand /*
2b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
3b0104773SPascal Brand  * All rights reserved.
4b0104773SPascal Brand  *
5b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
6b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
7b0104773SPascal Brand  *
8b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
9b0104773SPascal Brand  * this list of conditions and the following disclaimer.
10b0104773SPascal Brand  *
11b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
12b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
13b0104773SPascal Brand  * and/or other materials provided with the distribution.
14b0104773SPascal Brand  *
15b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
26b0104773SPascal Brand  */
272eb765fcSJens Wiklander #include <util.h>
28b0104773SPascal Brand #include <kernel/tee_common_otp.h>
29b0104773SPascal Brand #include <kernel/tee_common.h>
30b0104773SPascal Brand #include <tee_api_types.h>
31b0104773SPascal Brand #include <kernel/tee_ta_manager.h>
32b0104773SPascal Brand #include <utee_types.h>
33b0104773SPascal Brand #include <tee/tee_svc.h>
34ffe04039SJerome Forissier #include <tee/tee_cryp_utl.h>
35b0104773SPascal Brand #include <mm/tee_mmu.h>
36b0104773SPascal Brand #include <mm/tee_mm.h>
3743e30efdSJens Wiklander #include <mm/core_memprot.h>
38b0104773SPascal Brand #include <kernel/tee_time.h>
39b0104773SPascal Brand 
40b0104773SPascal Brand #include <user_ta_header.h>
414de4bebcSJens Wiklander #include <trace.h>
424de4bebcSJens Wiklander #include <kernel/trace_ta.h>
43b0104773SPascal Brand #include <kernel/chip_services.h>
448684fde8SJens Wiklander #include <kernel/static_ta.h>
45b0104773SPascal Brand 
46e86f1266SJens Wiklander #include <assert.h>
47e86f1266SJens Wiklander 
48e86f1266SJens Wiklander vaddr_t tee_svc_uref_base;
49e86f1266SJens Wiklander 
503276098dSJerome Forissier void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused)
51b0104773SPascal Brand {
527c876f12SPascal Brand #ifdef CFG_TEE_CORE_TA_TRACE
53b0104773SPascal Brand 	char *kbuf;
54b0104773SPascal Brand 
55b0104773SPascal Brand 	if (len == 0)
56b0104773SPascal Brand 		return;
57b0104773SPascal Brand 
58b0104773SPascal Brand 	kbuf = malloc(len);
59b0104773SPascal Brand 	if (kbuf == NULL)
60b0104773SPascal Brand 		return;
61c0346845SJens Wiklander 	*kbuf = '\0';
62b0104773SPascal Brand 
63b0104773SPascal Brand 	/* log as Info/Raw traces */
64b0104773SPascal Brand 	if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS)
654de4bebcSJens Wiklander 		TAMSG_RAW("%.*s", (int)len, kbuf);
66b0104773SPascal Brand 
67b0104773SPascal Brand 	free(kbuf);
687c876f12SPascal Brand #endif
69b0104773SPascal Brand }
70b0104773SPascal Brand 
71453a5030SJerome Forissier TEE_Result syscall_not_supported(void)
72197d17e7SSY Chiu {
73197d17e7SSY Chiu 	return TEE_ERROR_NOT_SUPPORTED;
74197d17e7SSY Chiu }
75197d17e7SSY Chiu 
76ab35d7adSCedric Chaumont /* Configuration properties */
77ab35d7adSCedric Chaumont /* API implementation version */
78ab35d7adSCedric Chaumont static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION);
79ab35d7adSCedric Chaumont 
80ab35d7adSCedric Chaumont /* Implementation description (implementation-dependent) */
81ab35d7adSCedric Chaumont static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR);
82ab35d7adSCedric Chaumont 
83b0104773SPascal Brand /*
84ab35d7adSCedric Chaumont  * TA persistent time protection level
85ab35d7adSCedric Chaumont  * 100: Persistent time based on an REE-controlled real-time clock
86ab35d7adSCedric Chaumont  * and on the TEE Trusted Storage for the storage of origins (default).
87ab35d7adSCedric Chaumont  * 1000: Persistent time based on a TEE-controlled real-time clock
88ab35d7adSCedric Chaumont  * and the TEE Trusted Storage.
89ab35d7adSCedric Chaumont  * The real-time clock MUST be out of reach of software attacks
90ab35d7adSCedric Chaumont  * from the REE.
91ab35d7adSCedric Chaumont  */
92b0104773SPascal Brand static const uint32_t ta_time_prot_lvl = 100;
93ab35d7adSCedric Chaumont 
9464a5011eSPascal Brand /* Elliptic Curve Cryptographic support */
9564a5011eSPascal Brand #ifdef CFG_CRYPTO_ECC
9664a5011eSPascal Brand static const uint32_t crypto_ecc_en = 1;
9764a5011eSPascal Brand #else
9864a5011eSPascal Brand static const uint32_t crypto_ecc_en;
9964a5011eSPascal Brand #endif
10064a5011eSPascal Brand 
101ab35d7adSCedric Chaumont /*
102ab35d7adSCedric Chaumont  * Trusted storage anti rollback protection level
103ab35d7adSCedric Chaumont  * 0 (or missing): No antirollback protection (default)
104ab35d7adSCedric Chaumont  * 100: Antirollback enforced at REE level
105ab35d7adSCedric Chaumont  * 1000: Antirollback TEE-controlled hardware
106ab35d7adSCedric Chaumont  */
107ab35d7adSCedric Chaumont static const uint32_t ts_antiroll_prot_lvl;
108ab35d7adSCedric Chaumont 
109ab35d7adSCedric Chaumont /* Trusted OS implementation version */
1104bf425c1SJens Wiklander static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION);
111ab35d7adSCedric Chaumont 
112ab35d7adSCedric Chaumont /* Trusted OS implementation version (binary value) */
113ab35d7adSCedric Chaumont static const uint32_t trustedos_impl_bin_version; /* 0 by default */
114ab35d7adSCedric Chaumont 
115ab35d7adSCedric Chaumont /* Trusted OS implementation manufacturer name */
116ab35d7adSCedric Chaumont static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER);
117ab35d7adSCedric Chaumont 
118ab35d7adSCedric Chaumont /* Trusted firmware version */
119ab35d7adSCedric Chaumont static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION);
120ab35d7adSCedric Chaumont 
121ab35d7adSCedric Chaumont /* Trusted firmware version (binary value) */
122ab35d7adSCedric Chaumont static const uint32_t fw_impl_bin_version; /* 0 by default */
123ab35d7adSCedric Chaumont 
124ab35d7adSCedric Chaumont /* Trusted firmware manufacturer name */
125ab35d7adSCedric Chaumont static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER);
126ab35d7adSCedric Chaumont 
12764a5011eSPascal Brand static TEE_Result get_prop_tee_dev_id(struct tee_ta_session *sess,
128ff857a3aSPascal Brand 				      void *buf, size_t *blen)
129ab35d7adSCedric Chaumont {
130b0104773SPascal Brand 	TEE_Result res;
131b0104773SPascal Brand 	TEE_UUID uuid;
132ab35d7adSCedric Chaumont 	const size_t nslen = 5;
13364a5011eSPascal Brand 	uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = {
134ab35d7adSCedric Chaumont 	    'O', 'P', 'T', 'E', 'E' };
135b0104773SPascal Brand 
136ff857a3aSPascal Brand 	if (*blen < sizeof(uuid)) {
137ff857a3aSPascal Brand 		*blen = sizeof(uuid);
138b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
139ff857a3aSPascal Brand 	}
140ff857a3aSPascal Brand 	*blen = sizeof(uuid);
141b0104773SPascal Brand 
14264a5011eSPascal Brand 	if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen))
143b0104773SPascal Brand 		return TEE_ERROR_BAD_STATE;
144b0104773SPascal Brand 
14564a5011eSPascal Brand 	res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data),
14664a5011eSPascal Brand 				    (uint8_t *)&uuid, sizeof(uuid));
147b0104773SPascal Brand 	if (res != TEE_SUCCESS)
148b0104773SPascal Brand 		return TEE_ERROR_BAD_STATE;
149b0104773SPascal Brand 
150b0104773SPascal Brand 	/*
151b0104773SPascal Brand 	 * Changes the random value into and UUID as specifiec
152b0104773SPascal Brand 	 * in RFC 4122. The magic values are from the example
153b0104773SPascal Brand 	 * code in the RFC.
154b0104773SPascal Brand 	 *
155b0104773SPascal Brand 	 * TEE_UUID is defined slightly different from the RFC,
156b0104773SPascal Brand 	 * but close enough for our purpose.
157b0104773SPascal Brand 	 */
158b0104773SPascal Brand 
159b0104773SPascal Brand 	uuid.timeHiAndVersion &= 0x0fff;
160b0104773SPascal Brand 	uuid.timeHiAndVersion |= 5 << 12;
161b0104773SPascal Brand 
162b0104773SPascal Brand 	/* uuid.clock_seq_hi_and_reserved in the RFC */
163b0104773SPascal Brand 	uuid.clockSeqAndNode[0] &= 0x3f;
164b0104773SPascal Brand 	uuid.clockSeqAndNode[0] |= 0x80;
165b0104773SPascal Brand 
16664a5011eSPascal Brand 	return tee_svc_copy_to_user(sess, buf, &uuid, sizeof(TEE_UUID));
167b0104773SPascal Brand }
168b0104773SPascal Brand 
16964a5011eSPascal Brand static TEE_Result get_prop_tee_sys_time_prot_level(struct tee_ta_session *sess,
170ff857a3aSPascal Brand 						   void *buf, size_t *blen)
1712cdaaacbSJerome Forissier {
1722cdaaacbSJerome Forissier 	uint32_t prot;
1732cdaaacbSJerome Forissier 
174ff857a3aSPascal Brand 	if (*blen < sizeof(prot)) {
175ff857a3aSPascal Brand 		*blen = sizeof(prot);
1762cdaaacbSJerome Forissier 		return TEE_ERROR_SHORT_BUFFER;
177ff857a3aSPascal Brand 	}
178ff857a3aSPascal Brand 	*blen = sizeof(prot);
1792cdaaacbSJerome Forissier 	prot = tee_time_get_sys_time_protection_level();
18064a5011eSPascal Brand 	return tee_svc_copy_to_user(sess, (void *)buf, &prot, sizeof(prot));
1812cdaaacbSJerome Forissier }
1822cdaaacbSJerome Forissier 
18364a5011eSPascal Brand static TEE_Result get_prop_client_id(struct tee_ta_session *sess,
184ff857a3aSPascal Brand 				     void *buf, size_t *blen)
18564a5011eSPascal Brand {
186ff857a3aSPascal Brand 	if (*blen < sizeof(TEE_Identity)) {
187ff857a3aSPascal Brand 		*blen = sizeof(TEE_Identity);
188b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
189ff857a3aSPascal Brand 	}
190ff857a3aSPascal Brand 	*blen = sizeof(TEE_Identity);
191e86f1266SJens Wiklander 	return tee_svc_copy_to_user(sess, buf, &sess->clnt_id,
192ab35d7adSCedric Chaumont 				    sizeof(TEE_Identity));
19364a5011eSPascal Brand }
19437d6ae92SPascal Brand 
19564a5011eSPascal Brand static TEE_Result get_prop_ta_app_id(struct tee_ta_session *sess,
196ff857a3aSPascal Brand 				     void *buf, size_t *blen)
19764a5011eSPascal Brand {
198ff857a3aSPascal Brand 	if (*blen < sizeof(TEE_UUID)) {
199ff857a3aSPascal Brand 		*blen = sizeof(TEE_UUID);
200b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
201ff857a3aSPascal Brand 	}
202ff857a3aSPascal Brand 	*blen = sizeof(TEE_UUID);
203e86f1266SJens Wiklander 	return tee_svc_copy_to_user(sess, buf, &sess->ctx->uuid,
204ab35d7adSCedric Chaumont 				    sizeof(TEE_UUID));
20564a5011eSPascal Brand }
20664a5011eSPascal Brand 
20764a5011eSPascal Brand /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */
20864a5011eSPascal Brand const struct tee_props tee_propset_client[] = {
20964a5011eSPascal Brand 	{
21064a5011eSPascal Brand 		.name = "gpd.client.identity",
21164a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_IDENTITY,
21264a5011eSPascal Brand 		.get_prop_func = get_prop_client_id
21364a5011eSPascal Brand 	},
21464a5011eSPascal Brand };
21564a5011eSPascal Brand 
21664a5011eSPascal Brand /* Properties of the set TEE_PROPSET_CURRENT_TA */
21764a5011eSPascal Brand const struct tee_props tee_propset_ta[] = {
21864a5011eSPascal Brand 	{
21964a5011eSPascal Brand 		.name = "gpd.ta.appID",
22064a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_UUID,
22164a5011eSPascal Brand 		.get_prop_func = get_prop_ta_app_id
22264a5011eSPascal Brand 	},
22364a5011eSPascal Brand 
22464a5011eSPascal Brand 	/*
22564a5011eSPascal Brand 	 * Following properties are processed directly in libutee:
22664a5011eSPascal Brand 	 *	TA_PROP_STR_SINGLE_INSTANCE
22764a5011eSPascal Brand 	 *	TA_PROP_STR_MULTI_SESSION
22864a5011eSPascal Brand 	 *	TA_PROP_STR_KEEP_ALIVE
22964a5011eSPascal Brand 	 *	TA_PROP_STR_DATA_SIZE
23064a5011eSPascal Brand 	 *	TA_PROP_STR_STACK_SIZE
23164a5011eSPascal Brand 	 *	TA_PROP_STR_VERSION
23264a5011eSPascal Brand 	 *	TA_PROP_STR_DESCRIPTION
23364a5011eSPascal Brand 	 *	USER_TA_PROP_TYPE_STRING,
23464a5011eSPascal Brand 	 *	TA_DESCRIPTION
23564a5011eSPascal Brand 	 */
23664a5011eSPascal Brand };
23764a5011eSPascal Brand 
23864a5011eSPascal Brand /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */
23964a5011eSPascal Brand const struct tee_props tee_propset_tee[] = {
24064a5011eSPascal Brand 	{
24164a5011eSPascal Brand 		.name = "gpd.tee.apiversion",
24264a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
24364a5011eSPascal Brand 		.data = api_vers,
24464a5011eSPascal Brand 		.len = sizeof(api_vers),
24564a5011eSPascal Brand 	},
24664a5011eSPascal Brand 	{
24764a5011eSPascal Brand 		.name = "gpd.tee.description",
24864a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
24964a5011eSPascal Brand 		.data = descr, .len = sizeof(descr)
25064a5011eSPascal Brand 	},
25164a5011eSPascal Brand 	{
25264a5011eSPascal Brand 		.name = "gpd.tee.deviceID",
25364a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_UUID,
25464a5011eSPascal Brand 		.get_prop_func = get_prop_tee_dev_id
25564a5011eSPascal Brand 	},
25664a5011eSPascal Brand 	{
25764a5011eSPascal Brand 		.name = "gpd.tee.systemTime.protectionLevel",
25864a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
25964a5011eSPascal Brand 		.get_prop_func = get_prop_tee_sys_time_prot_level
26064a5011eSPascal Brand 	},
26164a5011eSPascal Brand 	{
26264a5011eSPascal Brand 		.name = "gpd.tee.TAPersistentTime.protectionLevel",
26364a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
26464a5011eSPascal Brand 		.data = &ta_time_prot_lvl,
26564a5011eSPascal Brand 		.len = sizeof(ta_time_prot_lvl)
26664a5011eSPascal Brand 	},
26764a5011eSPascal Brand 	{
26864a5011eSPascal Brand 		.name = "gpd.tee.cryptography.ecc",
26964a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_BOOL,
27064a5011eSPascal Brand 		.data = &crypto_ecc_en,
27164a5011eSPascal Brand 		.len = sizeof(crypto_ecc_en)
27264a5011eSPascal Brand 	},
27364a5011eSPascal Brand 	{
27464a5011eSPascal Brand 		.name = "gpd.tee.trustedStorage.antiRollback.protectionLevel",
27564a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
27664a5011eSPascal Brand 		.data = &ts_antiroll_prot_lvl,
27764a5011eSPascal Brand 		.len = sizeof(ts_antiroll_prot_lvl)
27864a5011eSPascal Brand 	},
27964a5011eSPascal Brand 	{
28064a5011eSPascal Brand 		.name = "gpd.tee.trustedos.implementation.version",
28164a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
28264a5011eSPascal Brand 		.data = trustedos_impl_version,
28364a5011eSPascal Brand 		.len = sizeof(trustedos_impl_version)
28464a5011eSPascal Brand 	},
28564a5011eSPascal Brand 	{
28664a5011eSPascal Brand 		.name = "gpd.tee.trustedos.implementation.binaryversion",
28764a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
28864a5011eSPascal Brand 		.data = &trustedos_impl_bin_version,
28964a5011eSPascal Brand 		.len = sizeof(trustedos_impl_bin_version)
29064a5011eSPascal Brand 	},
29164a5011eSPascal Brand 	{
29264a5011eSPascal Brand 		.name = "gpd.tee.trustedos.manufacturer",
29364a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
29464a5011eSPascal Brand 		.data = trustedos_manufacturer,
29564a5011eSPascal Brand 		.len = sizeof(trustedos_manufacturer)
29664a5011eSPascal Brand 	},
29764a5011eSPascal Brand 	{
29864a5011eSPascal Brand 		.name = "gpd.tee.firmware.implementation.version",
29964a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
30064a5011eSPascal Brand 		.data = fw_impl_version,
30164a5011eSPascal Brand 		.len = sizeof(fw_impl_version)
30264a5011eSPascal Brand 	},
30364a5011eSPascal Brand 	{
30464a5011eSPascal Brand 		.name = "gpd.tee.firmware.implementation.binaryversion",
30564a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_U32,
30664a5011eSPascal Brand 		.data = &fw_impl_bin_version,
30764a5011eSPascal Brand 		.len = sizeof(fw_impl_bin_version)
30864a5011eSPascal Brand 	},
30964a5011eSPascal Brand 	{
31064a5011eSPascal Brand 		.name = "gpd.tee.firmware.manufacturer",
31164a5011eSPascal Brand 		.prop_type = USER_TA_PROP_TYPE_STRING,
31264a5011eSPascal Brand 		.data = fw_manufacturer,
31364a5011eSPascal Brand 		.len = sizeof(fw_manufacturer)
31464a5011eSPascal Brand 	},
31564a5011eSPascal Brand 
31664a5011eSPascal Brand 	/*
31764a5011eSPascal Brand 	 * Following properties are processed directly in libutee:
31864a5011eSPascal Brand 	 *	gpd.tee.arith.maxBigIntSize
31964a5011eSPascal Brand 	 */
32064a5011eSPascal Brand };
32164a5011eSPascal Brand 
32241d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_client;
32341d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_ta;
32441d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_tee;
32541d71430SPascal Brand 
32664a5011eSPascal Brand static void get_prop_set(unsigned long prop_set,
32764a5011eSPascal Brand 			 const struct tee_props **props,
32841d71430SPascal Brand 			 size_t *size,
32941d71430SPascal Brand 			 const struct tee_props **vendor_props,
33041d71430SPascal Brand 			 size_t *vendor_size)
33164a5011eSPascal Brand {
33264a5011eSPascal Brand 	if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) {
33364a5011eSPascal Brand 		*props = tee_propset_client;
33464a5011eSPascal Brand 		*size = ARRAY_SIZE(tee_propset_client);
33541d71430SPascal Brand 		*vendor_props = vendor_props_client.props;
33641d71430SPascal Brand 		*vendor_size = vendor_props_client.len;
33764a5011eSPascal Brand 	} else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) {
33864a5011eSPascal Brand 		*props = tee_propset_ta;
33964a5011eSPascal Brand 		*size = ARRAY_SIZE(tee_propset_ta);
34041d71430SPascal Brand 		*vendor_props = vendor_props_ta.props;
34141d71430SPascal Brand 		*vendor_size = vendor_props_ta.len;
34264a5011eSPascal Brand 	} else if ((TEE_PropSetHandle)prop_set ==
34364a5011eSPascal Brand 		   TEE_PROPSET_TEE_IMPLEMENTATION) {
34464a5011eSPascal Brand 		*props = tee_propset_tee;
34564a5011eSPascal Brand 		*size = ARRAY_SIZE(tee_propset_tee);
34641d71430SPascal Brand 		*vendor_props = vendor_props_tee.props;
34741d71430SPascal Brand 		*vendor_size = vendor_props_tee.len;
34864a5011eSPascal Brand 	} else {
34941d71430SPascal Brand 		*props = NULL;
35064a5011eSPascal Brand 		*size = 0;
35141d71430SPascal Brand 		*vendor_props = NULL;
35241d71430SPascal Brand 		*vendor_size = 0;
35364a5011eSPascal Brand 	}
35464a5011eSPascal Brand }
35564a5011eSPascal Brand 
35664a5011eSPascal Brand static const struct tee_props *get_prop_struct(unsigned long prop_set,
35764a5011eSPascal Brand 					       unsigned long index)
35864a5011eSPascal Brand {
35964a5011eSPascal Brand 	const struct tee_props *props;
36041d71430SPascal Brand 	const struct tee_props *vendor_props;
36141d71430SPascal Brand 	size_t size;
36241d71430SPascal Brand 	size_t vendor_size;
36364a5011eSPascal Brand 
36441d71430SPascal Brand 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
36564a5011eSPascal Brand 
36641d71430SPascal Brand 	if (index < size)
36764a5011eSPascal Brand 		return &(props[index]);
36841d71430SPascal Brand 	index -= size;
36941d71430SPascal Brand 
37041d71430SPascal Brand 	if (index < vendor_size)
37141d71430SPascal Brand 		return &(vendor_props[index]);
37241d71430SPascal Brand 
37364a5011eSPascal Brand 	return NULL;
37464a5011eSPascal Brand }
37564a5011eSPascal Brand 
37664a5011eSPascal Brand /*
37764a5011eSPascal Brand  * prop_set is part of TEE_PROPSET_xxx
37864a5011eSPascal Brand  * index is the index in the Property Set to retrieve
37964a5011eSPascal Brand  * if name is not NULL, the name of "index" property is returned
38064a5011eSPascal Brand  * if buf is not NULL, the property is returned
38164a5011eSPascal Brand  */
38264a5011eSPascal Brand TEE_Result syscall_get_property(unsigned long prop_set,
38364a5011eSPascal Brand 				unsigned long index,
38464a5011eSPascal Brand 				void *name, uint32_t *name_len,
38564a5011eSPascal Brand 				void *buf, uint32_t *blen,
38664a5011eSPascal Brand 				uint32_t *prop_type)
38764a5011eSPascal Brand {
38864a5011eSPascal Brand 	struct tee_ta_session *sess;
38964a5011eSPascal Brand 	TEE_Result res;
39064a5011eSPascal Brand 	TEE_Result res2;
39164a5011eSPascal Brand 	const struct tee_props *prop;
39264a5011eSPascal Brand 	uint32_t klen;
393ff857a3aSPascal Brand 	size_t klen_size;
39464a5011eSPascal Brand 	uint32_t elen;
39564a5011eSPascal Brand 
39664a5011eSPascal Brand 	prop = get_prop_struct(prop_set, index);
39764a5011eSPascal Brand 	if (!prop)
39864a5011eSPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
39964a5011eSPascal Brand 
40064a5011eSPascal Brand 	res = tee_ta_get_current_session(&sess);
40164a5011eSPascal Brand 	if (res != TEE_SUCCESS)
40264a5011eSPascal Brand 		return res;
40364a5011eSPascal Brand 
404ff857a3aSPascal Brand 	/* Get the property type */
405ff857a3aSPascal Brand 	if (prop_type) {
406ff857a3aSPascal Brand 		res = tee_svc_copy_to_user(sess, prop_type, &prop->prop_type,
407ff857a3aSPascal Brand 					   sizeof(*prop_type));
408ff857a3aSPascal Brand 		if (res != TEE_SUCCESS)
409ff857a3aSPascal Brand 			return res;
410ff857a3aSPascal Brand 	}
411ff857a3aSPascal Brand 
41264a5011eSPascal Brand 	/* Get the property */
41364a5011eSPascal Brand 	if (buf && blen) {
41464a5011eSPascal Brand 		res = tee_svc_copy_from_user(sess, &klen, blen, sizeof(klen));
41564a5011eSPascal Brand 		if (res != TEE_SUCCESS)
41664a5011eSPascal Brand 			return res;
41764a5011eSPascal Brand 
41864a5011eSPascal Brand 		if (prop->get_prop_func) {
419ff857a3aSPascal Brand 			klen_size = klen;
420ff857a3aSPascal Brand 			res = prop->get_prop_func(sess, buf, &klen_size);
421ff857a3aSPascal Brand 			klen = klen_size;
422ff857a3aSPascal Brand 			res2 = tee_svc_copy_to_user(sess, blen,
423ff857a3aSPascal Brand 						    &klen, sizeof(*blen));
42464a5011eSPascal Brand 		} else {
42564a5011eSPascal Brand 			if (klen < prop->len)
42664a5011eSPascal Brand 				res = TEE_ERROR_SHORT_BUFFER;
42764a5011eSPascal Brand 			else
42864a5011eSPascal Brand 				res = tee_svc_copy_to_user(sess, buf,
42964a5011eSPascal Brand 							   prop->data,
43064a5011eSPascal Brand 							   prop->len);
43164a5011eSPascal Brand 			res2 = tee_svc_copy_to_user(sess, blen,
432ff857a3aSPascal Brand 						    &prop->len, sizeof(*blen));
433ff857a3aSPascal Brand 		}
43464a5011eSPascal Brand 		if (res2 != TEE_SUCCESS)
43564a5011eSPascal Brand 			return res2;
43664a5011eSPascal Brand 		if (res != TEE_SUCCESS)
43764a5011eSPascal Brand 			return res;
43864a5011eSPascal Brand 	}
43964a5011eSPascal Brand 
44064a5011eSPascal Brand 	/* Get the property name */
44164a5011eSPascal Brand 	if (name && name_len) {
44264a5011eSPascal Brand 		res = tee_svc_copy_from_user(sess, &klen,
44364a5011eSPascal Brand 					     name_len, sizeof(klen));
44464a5011eSPascal Brand 		if (res != TEE_SUCCESS)
44564a5011eSPascal Brand 			return res;
44664a5011eSPascal Brand 
44764a5011eSPascal Brand 		elen = strlen(prop->name) + 1;
44864a5011eSPascal Brand 
44964a5011eSPascal Brand 		if (klen < elen)
45064a5011eSPascal Brand 			res = TEE_ERROR_SHORT_BUFFER;
45164a5011eSPascal Brand 		else
45264a5011eSPascal Brand 			res = tee_svc_copy_to_user(sess, name,
45364a5011eSPascal Brand 						   prop->name, elen);
45464a5011eSPascal Brand 		res2 = tee_svc_copy_to_user(sess, name_len,
45564a5011eSPascal Brand 					    &elen, sizeof(*name_len));
45664a5011eSPascal Brand 		if (res2 != TEE_SUCCESS)
45764a5011eSPascal Brand 			return res2;
45864a5011eSPascal Brand 		if (res != TEE_SUCCESS)
45964a5011eSPascal Brand 			return res;
46064a5011eSPascal Brand 	}
46164a5011eSPascal Brand 
46264a5011eSPascal Brand 	return res;
46364a5011eSPascal Brand }
46464a5011eSPascal Brand 
46564a5011eSPascal Brand /*
46664a5011eSPascal Brand  * prop_set is part of TEE_PROPSET_xxx
46764a5011eSPascal Brand  */
46864a5011eSPascal Brand TEE_Result syscall_get_property_name_to_index(unsigned long prop_set,
46964a5011eSPascal Brand 					      void *name,
47064a5011eSPascal Brand 					      unsigned long name_len,
47164a5011eSPascal Brand 					      uint32_t *index)
47264a5011eSPascal Brand {
47364a5011eSPascal Brand 	TEE_Result res;
47464a5011eSPascal Brand 	struct tee_ta_session *sess;
47564a5011eSPascal Brand 	const struct tee_props *props;
47641d71430SPascal Brand 	size_t size;
47741d71430SPascal Brand 	const struct tee_props *vendor_props;
47841d71430SPascal Brand 	size_t vendor_size;
47964a5011eSPascal Brand 	char *kname = 0;
48064a5011eSPascal Brand 	uint32_t i;
48164a5011eSPascal Brand 
48241d71430SPascal Brand 	get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size);
48364a5011eSPascal Brand 	if (!props)
48464a5011eSPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
48564a5011eSPascal Brand 
48664a5011eSPascal Brand 	res = tee_ta_get_current_session(&sess);
48764a5011eSPascal Brand 	if (res != TEE_SUCCESS)
48864a5011eSPascal Brand 		goto out;
48964a5011eSPascal Brand 
49064a5011eSPascal Brand 	if (!name || !name_len) {
49164a5011eSPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
49264a5011eSPascal Brand 		goto out;
49364a5011eSPascal Brand 	}
49464a5011eSPascal Brand 
49564a5011eSPascal Brand 	kname = malloc(name_len);
49664a5011eSPascal Brand 	if (!kname)
49764a5011eSPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
49864a5011eSPascal Brand 	res = tee_svc_copy_from_user(sess, kname, name, name_len);
49964a5011eSPascal Brand 	if (res != TEE_SUCCESS)
50064a5011eSPascal Brand 		goto out;
50164a5011eSPascal Brand 	kname[name_len - 1] = 0;
50264a5011eSPascal Brand 
50364a5011eSPascal Brand 	res = TEE_ERROR_ITEM_NOT_FOUND;
50464a5011eSPascal Brand 	for (i = 0; i < size; i++) {
50564a5011eSPascal Brand 		if (!strcmp(kname, props[i].name)) {
50664a5011eSPascal Brand 			res = tee_svc_copy_to_user(sess, index, &i,
50764a5011eSPascal Brand 						   sizeof(*index));
50841d71430SPascal Brand 			goto out;
50941d71430SPascal Brand 		}
51041d71430SPascal Brand 	}
51141d71430SPascal Brand 	for (i = size; i < size + vendor_size; i++) {
51241d71430SPascal Brand 		if (!strcmp(kname, vendor_props[i - size].name)) {
51341d71430SPascal Brand 			res = tee_svc_copy_to_user(sess, index, &i,
51441d71430SPascal Brand 						   sizeof(*index));
51541d71430SPascal Brand 			goto out;
51664a5011eSPascal Brand 		}
51764a5011eSPascal Brand 	}
51864a5011eSPascal Brand 
51964a5011eSPascal Brand out:
52064a5011eSPascal Brand 	free(kname);
52164a5011eSPascal Brand 	return res;
52264a5011eSPascal Brand }
52364a5011eSPascal Brand 
524e86f1266SJens Wiklander static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up)
525e86f1266SJens Wiklander {
526e86f1266SJens Wiklander 	size_t n;
527e86f1266SJens Wiklander 	uint32_t types = up->types;
528e86f1266SJens Wiklander 
529e86f1266SJens Wiklander 	p->types = types;
530e86f1266SJens Wiklander 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
531e86f1266SJens Wiklander 		uintptr_t a = up->vals[n * 2];
532e86f1266SJens Wiklander 		size_t b = up->vals[n * 2 + 1];
533e86f1266SJens Wiklander 
534e86f1266SJens Wiklander 		switch (TEE_PARAM_TYPE_GET(types, n)) {
535e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_INPUT:
536e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
537e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_MEMREF_INOUT:
538e86f1266SJens Wiklander 			p->params[n].memref.buffer = (void *)a;
539e86f1266SJens Wiklander 			p->params[n].memref.size = b;
540*36bf7ea8SJens Wiklander 			p->param_attr[n] = TEE_MATTR_VIRTUAL;
541e86f1266SJens Wiklander 			break;
542e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_VALUE_INPUT:
543e86f1266SJens Wiklander 		case TEE_PARAM_TYPE_VALUE_INOUT:
544e86f1266SJens Wiklander 			p->params[n].value.a = a;
545e86f1266SJens Wiklander 			p->params[n].value.b = b;
546e86f1266SJens Wiklander 			break;
547e86f1266SJens Wiklander 		default:
548e86f1266SJens Wiklander 			p->params[n].value.a = 0;
549e86f1266SJens Wiklander 			p->params[n].value.b = 0;
550e86f1266SJens Wiklander 			break;
551e86f1266SJens Wiklander 		}
552e86f1266SJens Wiklander 	}
553e86f1266SJens Wiklander }
554e86f1266SJens Wiklander 
555b0104773SPascal Brand /*
556b0104773SPascal Brand  * TA invokes some TA with parameter.
557b0104773SPascal Brand  * If some parameters are memory references:
558b0104773SPascal Brand  * - either the memref is inside TA private RAM: TA is not allowed to expose
559b0104773SPascal Brand  *   its private RAM: use a temporary memory buffer and copy the data.
560b0104773SPascal Brand  * - or the memref is not in the TA private RAM:
561b0104773SPascal Brand  *   - if the memref was mapped to the TA, TA is allowed to expose it.
562b0104773SPascal Brand  *   - if so, converts memref virtual address into a physical address.
563b0104773SPascal Brand  */
564b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess,
565b0104773SPascal Brand 				     struct tee_ta_session *called_sess,
566e86f1266SJens Wiklander 				     struct utee_params *callee_params,
567b0104773SPascal Brand 				     struct tee_ta_param *param,
568b0104773SPascal Brand 				     tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
569b0104773SPascal Brand 				     tee_mm_entry_t **mm)
570b0104773SPascal Brand {
571b0104773SPascal Brand 	size_t n;
572b0104773SPascal Brand 	TEE_Result res;
573b0104773SPascal Brand 	size_t req_mem = 0;
574b0104773SPascal Brand 	size_t s;
575b0104773SPascal Brand 	uint8_t *dst = 0;
576b0104773SPascal Brand 	tee_paddr_t dst_pa, src_pa = 0;
577b0104773SPascal Brand 	bool ta_private_memref[TEE_NUM_PARAMS];
5788684fde8SJens Wiklander 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
579b0104773SPascal Brand 
580b7fc217fSPascal Brand 	/* fill 'param' input struct with caller params description buffer */
581b7fc217fSPascal Brand 	if (!callee_params) {
582e86f1266SJens Wiklander 		memset(param, 0, sizeof(*param));
583b0104773SPascal Brand 	} else {
5848684fde8SJens Wiklander 		res = tee_mmu_check_access_rights(utc,
585177603c7SJens Wiklander 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
586e86f1266SJens Wiklander 			(tee_uaddr_t)callee_params, sizeof(struct utee_params));
587177603c7SJens Wiklander 		if (res != TEE_SUCCESS)
588177603c7SJens Wiklander 			return res;
589e86f1266SJens Wiklander 		utee_param_to_param(param, callee_params);
590b0104773SPascal Brand 	}
591b0104773SPascal Brand 
5928684fde8SJens Wiklander 	if (called_sess && is_static_ta_ctx(called_sess->ctx)) {
593b0104773SPascal Brand 		/*
5948684fde8SJens Wiklander 		 * static TA, borrow the mapping of the calling
595b0104773SPascal Brand 		 * during this call.
596b0104773SPascal Brand 		 */
597b0104773SPascal Brand 		called_sess->calling_sess = sess;
598b0104773SPascal Brand 		return TEE_SUCCESS;
599b0104773SPascal Brand 	}
600b0104773SPascal Brand 
601b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
602b0104773SPascal Brand 
603b0104773SPascal Brand 		ta_private_memref[n] = false;
604b0104773SPascal Brand 
605b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
606b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
607b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
608b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
609b0104773SPascal Brand 			if (param->params[n].memref.buffer == NULL) {
610b0104773SPascal Brand 				if (param->params[n].memref.size != 0)
611b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
612b0104773SPascal Brand 				break;
613b0104773SPascal Brand 			}
614b0104773SPascal Brand 			/* uTA cannot expose its private memory */
6158684fde8SJens Wiklander 			if (tee_mmu_is_vbuf_inside_ta_private(utc,
616106d8aa6SPascal Brand 				    param->params[n].memref.buffer,
617b0104773SPascal Brand 				    param->params[n].memref.size)) {
618b0104773SPascal Brand 
619a17acc4cSSabrina Ni 				s = ROUNDUP(param->params[n].memref.size,
620b0104773SPascal Brand 						sizeof(uint32_t));
621b0104773SPascal Brand 				/* Check overflow */
622b0104773SPascal Brand 				if (req_mem + s < req_mem)
623b0104773SPascal Brand 					return TEE_ERROR_BAD_PARAMETERS;
624b0104773SPascal Brand 				req_mem += s;
625b0104773SPascal Brand 				ta_private_memref[n] = true;
626b0104773SPascal Brand 				break;
627b0104773SPascal Brand 			}
6288684fde8SJens Wiklander 			if (tee_mmu_is_vbuf_intersect_ta_private(utc,
629106d8aa6SPascal Brand 				    param->params[n].memref.buffer,
630b0104773SPascal Brand 				    param->params[n].memref.size))
631b0104773SPascal Brand 				return TEE_ERROR_BAD_PARAMETERS;
632b0104773SPascal Brand 
63343e30efdSJens Wiklander 			src_pa = virt_to_phys(param->params[n].memref.buffer);
63443e30efdSJens Wiklander 			if (!src_pa)
635b0104773SPascal Brand 				return TEE_ERROR_BAD_PARAMETERS;
636b0104773SPascal Brand 
637b0104773SPascal Brand 			param->param_attr[n] = tee_mmu_user_get_cache_attr(
6388684fde8SJens Wiklander 				utc, (void *)param->params[n].memref.buffer);
639b0104773SPascal Brand 
640b0104773SPascal Brand 			param->params[n].memref.buffer = (void *)src_pa;
641b0104773SPascal Brand 			break;
642b0104773SPascal Brand 
643b0104773SPascal Brand 		default:
644b0104773SPascal Brand 			break;
645b0104773SPascal Brand 		}
646b0104773SPascal Brand 	}
647b0104773SPascal Brand 
648b0104773SPascal Brand 	if (req_mem == 0)
649b0104773SPascal Brand 		return TEE_SUCCESS;
650b0104773SPascal Brand 
651b0104773SPascal Brand 	/* Allocate section in secure DDR */
652b0104773SPascal Brand 	*mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem);
653b0104773SPascal Brand 	if (*mm == NULL) {
654b0104773SPascal Brand 		DMSG("tee_mm_alloc TEE_ERROR_GENERIC");
655b0104773SPascal Brand 		return TEE_ERROR_GENERIC;
656b0104773SPascal Brand 	}
657b0104773SPascal Brand 
658b0104773SPascal Brand 	/* Get the virtual address for the section in secure DDR */
659b0104773SPascal Brand 	res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst);
660b0104773SPascal Brand 	if (res != TEE_SUCCESS)
661b0104773SPascal Brand 		return res;
662b0104773SPascal Brand 	dst_pa = tee_mm_get_smem(*mm);
663b0104773SPascal Brand 
664b0104773SPascal Brand 	for (n = 0; n < 4; n++) {
665b0104773SPascal Brand 
666b0104773SPascal Brand 		if (ta_private_memref[n] == false)
667b0104773SPascal Brand 			continue;
668b0104773SPascal Brand 
669a17acc4cSSabrina Ni 		s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t));
670b0104773SPascal Brand 
671b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
672b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INPUT:
673b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
674b0104773SPascal Brand 			if (param->params[n].memref.buffer != NULL) {
675b0104773SPascal Brand 				res = tee_svc_copy_from_user(sess, dst,
676b7fc217fSPascal Brand 						param->params[n].memref.buffer,
677b7fc217fSPascal Brand 						param->params[n].memref.size);
678b0104773SPascal Brand 				if (res != TEE_SUCCESS)
679b0104773SPascal Brand 					return res;
680b0104773SPascal Brand 				param->param_attr[n] =
681b0104773SPascal Brand 					tee_mmu_kmap_get_cache_attr(dst);
682b0104773SPascal Brand 				param->params[n].memref.buffer = (void *)dst_pa;
683b0104773SPascal Brand 				tmp_buf_pa[n] = dst_pa;
684b0104773SPascal Brand 				dst += s;
685b0104773SPascal Brand 				dst_pa += s;
686b0104773SPascal Brand 			}
687b0104773SPascal Brand 			break;
688b0104773SPascal Brand 
689b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
690b0104773SPascal Brand 			if (param->params[n].memref.buffer != NULL) {
691b0104773SPascal Brand 				param->param_attr[n] =
692b0104773SPascal Brand 					tee_mmu_kmap_get_cache_attr(dst);
693b0104773SPascal Brand 				param->params[n].memref.buffer = (void *)dst_pa;
694b0104773SPascal Brand 				tmp_buf_pa[n] = dst_pa;
695b0104773SPascal Brand 				dst += s;
696b0104773SPascal Brand 				dst_pa += s;
697b0104773SPascal Brand 			}
698b0104773SPascal Brand 			break;
699b0104773SPascal Brand 
700b0104773SPascal Brand 		default:
701b0104773SPascal Brand 			continue;
702b0104773SPascal Brand 		}
703b0104773SPascal Brand 	}
704b0104773SPascal Brand 
705b0104773SPascal Brand 	tee_mmu_kunmap(dst, req_mem);
706b0104773SPascal Brand 
707b0104773SPascal Brand 	return TEE_SUCCESS;
708b0104773SPascal Brand }
709b0104773SPascal Brand 
710b0104773SPascal Brand /*
711b0104773SPascal Brand  * Back from execution of service: update parameters passed from TA:
712b0104773SPascal Brand  * If some parameters were memory references:
713b0104773SPascal Brand  * - either the memref was temporary: copy back data and update size
714b0104773SPascal Brand  * - or it was the original TA memref: update only the size value.
715b0104773SPascal Brand  */
716b0104773SPascal Brand static TEE_Result tee_svc_update_out_param(
717b0104773SPascal Brand 		struct tee_ta_session *sess,
718b0104773SPascal Brand 		struct tee_ta_session *called_sess,
719b0104773SPascal Brand 		struct tee_ta_param *param,
720b0104773SPascal Brand 		tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS],
721e86f1266SJens Wiklander 		struct utee_params *usr_param)
722b0104773SPascal Brand {
723b0104773SPascal Brand 	size_t n;
724e86f1266SJens Wiklander 	void *p;
7258684fde8SJens Wiklander 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
7268684fde8SJens Wiklander 	bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx);
727bc420748SJens Wiklander 
728b0104773SPascal Brand 	tee_ta_set_current_session(sess);
729b0104773SPascal Brand 
730b0104773SPascal Brand 	for (n = 0; n < TEE_NUM_PARAMS; n++) {
731b0104773SPascal Brand 		switch (TEE_PARAM_TYPE_GET(param->types, n)) {
732b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
733b0104773SPascal Brand 		case TEE_PARAM_TYPE_MEMREF_INOUT:
734e86f1266SJens Wiklander 			p = (void *)(uintptr_t)usr_param->vals[n * 2];
735b0104773SPascal Brand 
736b0104773SPascal Brand 			/* outside TA private => memref is valid, update size */
7378684fde8SJens Wiklander 			if (!tee_mmu_is_vbuf_inside_ta_private(utc, p,
738b0104773SPascal Brand 					param->params[n].memref.size)) {
739e86f1266SJens Wiklander 				usr_param->vals[n * 2 + 1] =
740b0104773SPascal Brand 					param->params[n].memref.size;
741b0104773SPascal Brand 				break;
742b0104773SPascal Brand 			}
743b0104773SPascal Brand 
744b0104773SPascal Brand 			/*
745b0104773SPascal Brand 			 * If we called a kernel TA the parameters are in shared
746b0104773SPascal Brand 			 * memory and no copy is needed.
747b0104773SPascal Brand 			 */
748b0104773SPascal Brand 			if (have_private_mem_map &&
749b0104773SPascal Brand 			    param->params[n].memref.size <=
750e86f1266SJens Wiklander 			    usr_param->vals[n * 2 + 1]) {
751b0104773SPascal Brand 				uint8_t *src = 0;
752b0104773SPascal Brand 				TEE_Result res;
753b0104773SPascal Brand 
754b0104773SPascal Brand 				/* FIXME: TA_RAM is already mapped ! */
755b0104773SPascal Brand 				res = tee_mmu_kmap(tmp_buf_pa[n],
756b0104773SPascal Brand 					param->params[n].memref.size, &src);
757b0104773SPascal Brand 				if (res != TEE_SUCCESS)
758b0104773SPascal Brand 					return TEE_ERROR_GENERIC;
759b0104773SPascal Brand 
760e86f1266SJens Wiklander 				res = tee_svc_copy_to_user(sess, p, src,
761e86f1266SJens Wiklander 						 param->params[n].memref.size);
762b0104773SPascal Brand 				if (res != TEE_SUCCESS)
763b0104773SPascal Brand 					return res;
764b0104773SPascal Brand 				tee_mmu_kunmap(src,
765b0104773SPascal Brand 					       param->params[n].memref.size);
766b0104773SPascal Brand 
767b0104773SPascal Brand 			}
768e86f1266SJens Wiklander 			usr_param->vals[n * 2 + 1] =
769e86f1266SJens Wiklander 				param->params[n].memref.size;
770b0104773SPascal Brand 			break;
771b0104773SPascal Brand 
772b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_OUTPUT:
773b0104773SPascal Brand 		case TEE_PARAM_TYPE_VALUE_INOUT:
774e86f1266SJens Wiklander 			usr_param->vals[n * 2] = param->params[n].value.a;
775e86f1266SJens Wiklander 			usr_param->vals[n * 2 + 1] = param->params[n].value.b;
776b0104773SPascal Brand 			break;
777b0104773SPascal Brand 
778b0104773SPascal Brand 		default:
779b0104773SPascal Brand 			continue;
780b0104773SPascal Brand 		}
781b0104773SPascal Brand 	}
782b0104773SPascal Brand 
783b0104773SPascal Brand 	return TEE_SUCCESS;
784b0104773SPascal Brand }
785b0104773SPascal Brand 
786b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */
787453a5030SJerome Forissier TEE_Result syscall_open_ta_session(const TEE_UUID *dest,
788e86f1266SJens Wiklander 			unsigned long cancel_req_to,
789e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ta_sess,
790b0104773SPascal Brand 			uint32_t *ret_orig)
791b0104773SPascal Brand {
792b0104773SPascal Brand 	TEE_Result res;
793b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
794b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
795b0104773SPascal Brand 	struct tee_ta_session *sess;
796b0104773SPascal Brand 	tee_mm_entry_t *mm_param = NULL;
797b0104773SPascal Brand 	TEE_UUID *uuid = malloc(sizeof(TEE_UUID));
798b0104773SPascal Brand 	struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param));
799b0104773SPascal Brand 	TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity));
800b0104773SPascal Brand 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
8018684fde8SJens Wiklander 	struct user_ta_ctx *utc;
802b0104773SPascal Brand 
803b0104773SPascal Brand 	if (uuid == NULL || param == NULL || clnt_id == NULL) {
804b0104773SPascal Brand 		res = TEE_ERROR_OUT_OF_MEMORY;
805b0104773SPascal Brand 		goto out_free_only;
806b0104773SPascal Brand 	}
807b0104773SPascal Brand 
808b0104773SPascal Brand 	memset(param, 0, sizeof(struct tee_ta_param));
809b0104773SPascal Brand 
810b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
811b0104773SPascal Brand 	if (res != TEE_SUCCESS)
812b0104773SPascal Brand 		goto out_free_only;
8138684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
814b0104773SPascal Brand 
815b0104773SPascal Brand 	res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID));
816b0104773SPascal Brand 	if (res != TEE_SUCCESS)
817b0104773SPascal Brand 		goto function_exit;
818b0104773SPascal Brand 
819b0104773SPascal Brand 	clnt_id->login = TEE_LOGIN_TRUSTED_APP;
820bc420748SJens Wiklander 	memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
821b0104773SPascal Brand 
822e86f1266SJens Wiklander 	res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_pa,
823e86f1266SJens Wiklander 				 &mm_param);
824b0104773SPascal Brand 	if (res != TEE_SUCCESS)
825b0104773SPascal Brand 		goto function_exit;
826b0104773SPascal Brand 
827b0104773SPascal Brand 	/*
828b0104773SPascal Brand 	 * Find session of a multi session TA or a static TA
829b0104773SPascal Brand 	 * In such a case, there is no need to ask the supplicant for the TA
830b0104773SPascal Brand 	 * code
831b0104773SPascal Brand 	 */
8328684fde8SJens Wiklander 	res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid,
83327cbcc57SJens Wiklander 				  clnt_id, cancel_req_to, param);
834c0346845SJens Wiklander 	if (res != TEE_SUCCESS)
835b0104773SPascal Brand 		goto function_exit;
836b0104773SPascal Brand 
8378684fde8SJens Wiklander 	res = tee_svc_update_out_param(sess, s, param, tmp_buf_pa, usr_param);
838b0104773SPascal Brand 
839b0104773SPascal Brand function_exit:
840b0104773SPascal Brand 	tee_ta_set_current_session(sess);
8410dcea1a3SJens Wiklander 	sess->calling_sess = NULL; /* clear eventual borrowed mapping */
842b0104773SPascal Brand 
843b0104773SPascal Brand 	if (mm_param != NULL) {
84443e30efdSJens Wiklander 		void *va = phys_to_virt(tee_mm_get_smem(mm_param),
84543e30efdSJens Wiklander 					MEM_AREA_KMAP_VASPACE);
846b0104773SPascal Brand 
84743e30efdSJens Wiklander 		if (va)
848b0104773SPascal Brand 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
849b0104773SPascal Brand 	}
850b0104773SPascal Brand 	tee_mm_free(mm_param);
8512dcb3d36SJerome Forissier 	if (res == TEE_SUCCESS)
852e86f1266SJens Wiklander 		tee_svc_copy_kaddr_to_uref(sess, ta_sess, s);
853b0104773SPascal Brand 	tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
854b0104773SPascal Brand 
855b0104773SPascal Brand out_free_only:
856b0104773SPascal Brand 	free(param);
857b0104773SPascal Brand 	free(uuid);
858b0104773SPascal Brand 	free(clnt_id);
859b0104773SPascal Brand 	return res;
860b0104773SPascal Brand }
861b0104773SPascal Brand 
862e86f1266SJens Wiklander TEE_Result syscall_close_ta_session(unsigned long ta_sess)
863b0104773SPascal Brand {
864b0104773SPascal Brand 	TEE_Result res;
865b0104773SPascal Brand 	struct tee_ta_session *sess;
86660699957SPascal Brand 	TEE_Identity clnt_id;
867e86f1266SJens Wiklander 	struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess);
8688684fde8SJens Wiklander 	struct user_ta_ctx *utc;
869b0104773SPascal Brand 
870b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
871b0104773SPascal Brand 	if (res != TEE_SUCCESS)
872b0104773SPascal Brand 		return res;
8738684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
874b0104773SPascal Brand 
87560699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
876bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
877b0104773SPascal Brand 
87860699957SPascal Brand 	tee_ta_set_current_session(NULL);
8798684fde8SJens Wiklander 	res = tee_ta_close_session(s, &utc->open_sessions, &clnt_id);
880b0104773SPascal Brand 	tee_ta_set_current_session(sess);
881b0104773SPascal Brand 	return res;
882b0104773SPascal Brand }
883b0104773SPascal Brand 
884e86f1266SJens Wiklander TEE_Result syscall_invoke_ta_command(unsigned long ta_sess,
885e86f1266SJens Wiklander 			unsigned long cancel_req_to, unsigned long cmd_id,
886e86f1266SJens Wiklander 			struct utee_params *usr_param, uint32_t *ret_orig)
887b0104773SPascal Brand {
888b0104773SPascal Brand 	TEE_Result res;
8897d82e180SJens Wiklander 	TEE_Result res2;
890b0104773SPascal Brand 	uint32_t ret_o = TEE_ORIGIN_TEE;
891b0104773SPascal Brand 	struct tee_ta_param param = { 0 };
89260699957SPascal Brand 	TEE_Identity clnt_id;
893b0104773SPascal Brand 	struct tee_ta_session *sess;
894b666b6f2SJens Wiklander 	struct tee_ta_session *called_sess;
895b0104773SPascal Brand 	tee_mm_entry_t *mm_param = NULL;
896b0104773SPascal Brand 	tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS];
8978684fde8SJens Wiklander 	struct user_ta_ctx *utc;
898b0104773SPascal Brand 
899b0104773SPascal Brand 	res = tee_ta_get_current_session(&sess);
900b0104773SPascal Brand 	if (res != TEE_SUCCESS)
901b0104773SPascal Brand 		return res;
9028684fde8SJens Wiklander 	utc = to_user_ta_ctx(sess->ctx);
903b0104773SPascal Brand 
904e86f1266SJens Wiklander 	called_sess = tee_ta_get_session(
905e86f1266SJens Wiklander 				(vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true,
9068684fde8SJens Wiklander 				&utc->open_sessions);
907b666b6f2SJens Wiklander 	if (!called_sess)
908b666b6f2SJens Wiklander 		return TEE_ERROR_BAD_PARAMETERS;
909b0104773SPascal Brand 
91060699957SPascal Brand 	clnt_id.login = TEE_LOGIN_TRUSTED_APP;
911bc420748SJens Wiklander 	memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID));
91260699957SPascal Brand 
913e86f1266SJens Wiklander 	res = tee_svc_copy_param(sess, called_sess, usr_param, &param,
914e86f1266SJens Wiklander 				 tmp_buf_pa, &mm_param);
915b0104773SPascal Brand 	if (res != TEE_SUCCESS)
916b0104773SPascal Brand 		goto function_exit;
917b0104773SPascal Brand 
91860699957SPascal Brand 	res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id,
91960699957SPascal Brand 				    cancel_req_to, cmd_id, &param);
92060699957SPascal Brand 
9217d82e180SJens Wiklander 	res2 = tee_svc_update_out_param(sess, called_sess, &param, tmp_buf_pa,
922177603c7SJens Wiklander 					usr_param);
9237d82e180SJens Wiklander 	if (res2 != TEE_SUCCESS) {
9247d82e180SJens Wiklander 		/*
9257d82e180SJens Wiklander 		 * Spec for TEE_InvokeTACommand() says:
9267d82e180SJens Wiklander 		 * "If the return origin is different from
9277d82e180SJens Wiklander 		 * TEE_ORIGIN_TRUSTED_APP, then the function has failed
9287d82e180SJens Wiklander 		 * before it could reach the destination Trusted
9297d82e180SJens Wiklander 		 * Application."
9307d82e180SJens Wiklander 		 *
9317d82e180SJens Wiklander 		 * But if we can't update params to the caller we have no
9327d82e180SJens Wiklander 		 * choice we need to return some error to indicate that
9337d82e180SJens Wiklander 		 * parameters aren't updated as expected.
9347d82e180SJens Wiklander 		 */
9357d82e180SJens Wiklander 		ret_o = TEE_ORIGIN_TEE;
9367d82e180SJens Wiklander 		res = res2;
9377d82e180SJens Wiklander 	}
938b0104773SPascal Brand 
939b0104773SPascal Brand function_exit:
940b0104773SPascal Brand 	tee_ta_set_current_session(sess);
941b0104773SPascal Brand 	called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */
942b666b6f2SJens Wiklander 	tee_ta_put_session(called_sess);
943b0104773SPascal Brand 
944b0104773SPascal Brand 	if (mm_param != NULL) {
94543e30efdSJens Wiklander 		void *va = phys_to_virt(tee_mm_get_smem(mm_param),
94643e30efdSJens Wiklander 					MEM_AREA_KMAP_VASPACE);
947b0104773SPascal Brand 
94843e30efdSJens Wiklander 		if (va)
949b0104773SPascal Brand 			tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param));
950b0104773SPascal Brand 	}
951b0104773SPascal Brand 	tee_mm_free(mm_param);
952b0104773SPascal Brand 	if (ret_orig)
953b0104773SPascal Brand 		tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o));
954b0104773SPascal Brand 	return res;
955b0104773SPascal Brand }
956b0104773SPascal Brand 
957e86f1266SJens Wiklander TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf,
958b0104773SPascal Brand 				       size_t len)
959b0104773SPascal Brand {
960b0104773SPascal Brand 	TEE_Result res;
961b0104773SPascal Brand 	struct tee_ta_session *s;
962b0104773SPascal Brand 
963b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
964b0104773SPascal Brand 	if (res != TEE_SUCCESS)
965b0104773SPascal Brand 		return res;
966b0104773SPascal Brand 
9678684fde8SJens Wiklander 	return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags,
9688684fde8SJens Wiklander 					   (tee_uaddr_t)buf, len);
969b0104773SPascal Brand }
970b0104773SPascal Brand 
971b0104773SPascal Brand TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr,
972b0104773SPascal Brand 				  const void *uaddr, size_t len)
973b0104773SPascal Brand {
974b0104773SPascal Brand 	TEE_Result res;
975b0104773SPascal Brand 	struct tee_ta_session *s;
976b0104773SPascal Brand 
977b0104773SPascal Brand 	if (sess == NULL) {
978b0104773SPascal Brand 		res = tee_ta_get_current_session(&s);
979b0104773SPascal Brand 		if (res != TEE_SUCCESS)
980b0104773SPascal Brand 			return res;
981b0104773SPascal Brand 	} else {
982b0104773SPascal Brand 		s = sess;
983b0104773SPascal Brand 		tee_ta_set_current_session(s);
984b0104773SPascal Brand 	}
9858684fde8SJens Wiklander 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
986b0104773SPascal Brand 					TEE_MEMORY_ACCESS_READ |
987b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
988b0104773SPascal Brand 					(tee_uaddr_t)uaddr, len);
989b0104773SPascal Brand 	if (res != TEE_SUCCESS)
990b0104773SPascal Brand 		return res;
991b0104773SPascal Brand 
992b0104773SPascal Brand 	memcpy(kaddr, uaddr, len);
993b0104773SPascal Brand 	return TEE_SUCCESS;
994b0104773SPascal Brand }
995b0104773SPascal Brand 
996b0104773SPascal Brand TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr,
997b0104773SPascal Brand 				const void *kaddr, size_t len)
998b0104773SPascal Brand {
999b0104773SPascal Brand 	TEE_Result res;
1000b0104773SPascal Brand 	struct tee_ta_session *s;
1001b0104773SPascal Brand 
1002b0104773SPascal Brand 	if (sess == NULL) {
1003b0104773SPascal Brand 		res = tee_ta_get_current_session(&s);
1004b0104773SPascal Brand 		if (res != TEE_SUCCESS)
1005b0104773SPascal Brand 			return res;
1006b0104773SPascal Brand 	} else {
1007b0104773SPascal Brand 		s = sess;
1008b0104773SPascal Brand 		tee_ta_set_current_session(s);
1009b0104773SPascal Brand 	}
1010b0104773SPascal Brand 
10118684fde8SJens Wiklander 	res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx),
1012b0104773SPascal Brand 					TEE_MEMORY_ACCESS_WRITE |
1013b0104773SPascal Brand 					TEE_MEMORY_ACCESS_ANY_OWNER,
1014b0104773SPascal Brand 					(tee_uaddr_t)uaddr, len);
1015b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1016b0104773SPascal Brand 		return res;
1017b0104773SPascal Brand 
1018b0104773SPascal Brand 	memcpy(uaddr, kaddr, len);
1019b0104773SPascal Brand 	return TEE_SUCCESS;
1020b0104773SPascal Brand }
1021b0104773SPascal Brand 
1022e86f1266SJens Wiklander TEE_Result tee_svc_copy_kaddr_to_uref(struct tee_ta_session *sess,
1023e86f1266SJens Wiklander 			uint32_t *uref, void *kaddr)
10248707ec0fSJerome Forissier {
1025e86f1266SJens Wiklander 	uint32_t ref = tee_svc_kaddr_to_uref(kaddr);
10268707ec0fSJerome Forissier 
1027e86f1266SJens Wiklander 	return tee_svc_copy_to_user(sess, uref, &ref, sizeof(ref));
10288707ec0fSJerome Forissier }
10298707ec0fSJerome Forissier 
1030b0104773SPascal Brand static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time)
1031b0104773SPascal Brand {
1032b0104773SPascal Brand 	TEE_Time current_time;
1033b0104773SPascal Brand 
1034b0104773SPascal Brand 	if (s->cancel_mask)
1035b0104773SPascal Brand 		return false;
1036b0104773SPascal Brand 
1037b0104773SPascal Brand 	if (s->cancel)
1038b0104773SPascal Brand 		return true;
1039b0104773SPascal Brand 
1040b0104773SPascal Brand 	if (s->cancel_time.seconds == UINT32_MAX)
1041b0104773SPascal Brand 		return false;
1042b0104773SPascal Brand 
1043b0104773SPascal Brand 	if (curr_time != NULL)
1044b0104773SPascal Brand 		current_time = *curr_time;
1045b0104773SPascal Brand 	else if (tee_time_get_sys_time(&current_time) != TEE_SUCCESS)
1046b0104773SPascal Brand 		return false;
1047b0104773SPascal Brand 
1048b0104773SPascal Brand 	if (current_time.seconds > s->cancel_time.seconds ||
1049b0104773SPascal Brand 	    (current_time.seconds == s->cancel_time.seconds &&
1050b0104773SPascal Brand 	     current_time.millis >= s->cancel_time.millis)) {
1051b0104773SPascal Brand 		return true;
1052b0104773SPascal Brand 	}
1053b0104773SPascal Brand 
1054b0104773SPascal Brand 	return false;
1055b0104773SPascal Brand }
1056b0104773SPascal Brand 
1057e86f1266SJens Wiklander TEE_Result syscall_get_cancellation_flag(uint32_t *cancel)
1058b0104773SPascal Brand {
1059b0104773SPascal Brand 	TEE_Result res;
1060b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1061e86f1266SJens Wiklander 	uint32_t c;
1062b0104773SPascal Brand 
1063b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1064b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1065b0104773SPascal Brand 		return res;
1066b0104773SPascal Brand 
1067b0104773SPascal Brand 	c = session_is_cancelled(s, NULL);
1068b0104773SPascal Brand 
1069b0104773SPascal Brand 	return tee_svc_copy_to_user(s, cancel, &c, sizeof(c));
1070b0104773SPascal Brand }
1071b0104773SPascal Brand 
1072e86f1266SJens Wiklander TEE_Result syscall_unmask_cancellation(uint32_t *old_mask)
1073b0104773SPascal Brand {
1074b0104773SPascal Brand 	TEE_Result res;
1075b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1076e86f1266SJens Wiklander 	uint32_t m;
1077b0104773SPascal Brand 
1078b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1079b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1080b0104773SPascal Brand 		return res;
1081b0104773SPascal Brand 
1082b0104773SPascal Brand 	m = s->cancel_mask;
1083b0104773SPascal Brand 	s->cancel_mask = false;
1084b0104773SPascal Brand 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
1085b0104773SPascal Brand }
1086b0104773SPascal Brand 
1087e86f1266SJens Wiklander TEE_Result syscall_mask_cancellation(uint32_t *old_mask)
1088b0104773SPascal Brand {
1089b0104773SPascal Brand 	TEE_Result res;
1090b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1091e86f1266SJens Wiklander 	uint32_t m;
1092b0104773SPascal Brand 
1093b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1094b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1095b0104773SPascal Brand 		return res;
1096b0104773SPascal Brand 
1097b0104773SPascal Brand 	m = s->cancel_mask;
1098b0104773SPascal Brand 	s->cancel_mask = true;
1099b0104773SPascal Brand 	return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m));
1100b0104773SPascal Brand }
1101b0104773SPascal Brand 
1102e86f1266SJens Wiklander TEE_Result syscall_wait(unsigned long timeout)
1103b0104773SPascal Brand {
1104b0104773SPascal Brand 	TEE_Result res = TEE_SUCCESS;
1105b0104773SPascal Brand 	uint32_t mytime = 0;
1106b0104773SPascal Brand 	struct tee_ta_session *s;
1107b0104773SPascal Brand 	TEE_Time base_time;
1108b0104773SPascal Brand 	TEE_Time current_time;
1109b0104773SPascal Brand 
1110b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1111b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1112b0104773SPascal Brand 		return res;
1113b0104773SPascal Brand 
1114b0104773SPascal Brand 	res = tee_time_get_sys_time(&base_time);
1115b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1116b0104773SPascal Brand 		return res;
1117b0104773SPascal Brand 
1118b0104773SPascal Brand 	while (true) {
1119b0104773SPascal Brand 		res = tee_time_get_sys_time(&current_time);
1120b0104773SPascal Brand 		if (res != TEE_SUCCESS)
1121b0104773SPascal Brand 			return res;
1122b0104773SPascal Brand 
1123b0104773SPascal Brand 		if (session_is_cancelled(s, &current_time))
1124b0104773SPascal Brand 			return TEE_ERROR_CANCEL;
1125b0104773SPascal Brand 
1126b0104773SPascal Brand 		mytime = (current_time.seconds - base_time.seconds) * 1000 +
1127b0104773SPascal Brand 		    (int)current_time.millis - (int)base_time.millis;
1128b0104773SPascal Brand 		if (mytime >= timeout)
1129b0104773SPascal Brand 			return TEE_SUCCESS;
1130b0104773SPascal Brand 
1131d1aea08fSSY Chiu 		tee_time_wait(timeout - mytime);
1132b0104773SPascal Brand 	}
1133b0104773SPascal Brand 
1134b0104773SPascal Brand 	return res;
1135b0104773SPascal Brand }
1136b0104773SPascal Brand 
1137e86f1266SJens Wiklander TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime)
1138b0104773SPascal Brand {
1139b0104773SPascal Brand 	TEE_Result res, res2;
1140b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1141b0104773SPascal Brand 	TEE_Time t;
1142b0104773SPascal Brand 
1143b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1144b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1145b0104773SPascal Brand 		return res;
1146b0104773SPascal Brand 
1147b0104773SPascal Brand 	switch (cat) {
1148b0104773SPascal Brand 	case UTEE_TIME_CAT_SYSTEM:
1149b0104773SPascal Brand 		res = tee_time_get_sys_time(&t);
1150b0104773SPascal Brand 		break;
1151b0104773SPascal Brand 	case UTEE_TIME_CAT_TA_PERSISTENT:
1152bc420748SJens Wiklander 		res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t);
1153b0104773SPascal Brand 		break;
1154b0104773SPascal Brand 	case UTEE_TIME_CAT_REE:
1155b0104773SPascal Brand 		res = tee_time_get_ree_time(&t);
1156b0104773SPascal Brand 		break;
1157b0104773SPascal Brand 	default:
1158b0104773SPascal Brand 		res = TEE_ERROR_BAD_PARAMETERS;
1159b0104773SPascal Brand 		break;
1160b0104773SPascal Brand 	}
1161b0104773SPascal Brand 
1162b0104773SPascal Brand 	if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) {
1163b0104773SPascal Brand 		res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t));
1164b0104773SPascal Brand 		if (res2 != TEE_SUCCESS)
1165b0104773SPascal Brand 			res = res2;
1166b0104773SPascal Brand 	}
1167b0104773SPascal Brand 
1168b0104773SPascal Brand 	return res;
1169b0104773SPascal Brand }
1170b0104773SPascal Brand 
1171453a5030SJerome Forissier TEE_Result syscall_set_ta_time(const TEE_Time *mytime)
1172b0104773SPascal Brand {
1173b0104773SPascal Brand 	TEE_Result res;
1174b0104773SPascal Brand 	struct tee_ta_session *s = NULL;
1175b0104773SPascal Brand 	TEE_Time t;
1176b0104773SPascal Brand 
1177b0104773SPascal Brand 	res = tee_ta_get_current_session(&s);
1178b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1179b0104773SPascal Brand 		return res;
1180b0104773SPascal Brand 
1181b0104773SPascal Brand 	res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t));
1182b0104773SPascal Brand 	if (res != TEE_SUCCESS)
1183b0104773SPascal Brand 		return res;
1184b0104773SPascal Brand 
1185bc420748SJens Wiklander 	return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t);
1186b0104773SPascal Brand }
1187