11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 2b0104773SPascal Brand /* 3b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 4c40a6505SJens Wiklander * Copyright (c) 2020, Linaro Limited 5b0104773SPascal Brand */ 6c40a6505SJens Wiklander 7*919a5a68SJerome Forissier #include <compiler.h> 8b0104773SPascal Brand #include <kernel/chip_services.h> 942fb5b2eSEtienne Carriere #include <kernel/pseudo_ta.h> 1070b61310SJerome Forissier #include <kernel/tee_common.h> 1170b61310SJerome Forissier #include <kernel/tee_common_otp.h> 1270b61310SJerome Forissier #include <kernel/tee_ta_manager.h> 1370b61310SJerome Forissier #include <kernel/tee_time.h> 1470b61310SJerome Forissier #include <kernel/trace_ta.h> 15c40a6505SJens Wiklander #include <kernel/user_access.h> 1670b61310SJerome Forissier #include <mm/core_memprot.h> 170dcfa568SJens Wiklander #include <mm/mobj.h> 1870b61310SJerome Forissier #include <mm/tee_mm.h> 1970b61310SJerome Forissier #include <mm/tee_mmu.h> 2070b61310SJerome Forissier #include <stdlib_ext.h> 2170b61310SJerome Forissier #include <tee_api_types.h> 2270b61310SJerome Forissier #include <tee/tee_cryp_utl.h> 2370b61310SJerome Forissier #include <tee/tee_svc.h> 2470b61310SJerome Forissier #include <trace.h> 2570b61310SJerome Forissier #include <user_ta_header.h> 2670b61310SJerome Forissier #include <utee_types.h> 2770b61310SJerome Forissier #include <util.h> 28b0104773SPascal Brand 29e86f1266SJens Wiklander vaddr_t tee_svc_uref_base; 30e86f1266SJens Wiklander 313276098dSJerome Forissier void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused) 32b0104773SPascal Brand { 337c876f12SPascal Brand #ifdef CFG_TEE_CORE_TA_TRACE 34b0104773SPascal Brand char *kbuf; 35b0104773SPascal Brand 36b0104773SPascal Brand if (len == 0) 37b0104773SPascal Brand return; 38b0104773SPascal Brand 3989a3e9feSJens Wiklander kbuf = malloc(len + 1); 40b0104773SPascal Brand if (kbuf == NULL) 41b0104773SPascal Brand return; 42b0104773SPascal Brand 43c40a6505SJens Wiklander if (copy_from_user(kbuf, buf, len) == TEE_SUCCESS) { 443095f61eSJerome Forissier kbuf[len] = '\0'; 453095f61eSJerome Forissier trace_ext_puts(kbuf); 463095f61eSJerome Forissier } 47b0104773SPascal Brand 4870b61310SJerome Forissier free_wipe(kbuf); 497c876f12SPascal Brand #endif 50b0104773SPascal Brand } 51b0104773SPascal Brand 52453a5030SJerome Forissier TEE_Result syscall_not_supported(void) 53197d17e7SSY Chiu { 54197d17e7SSY Chiu return TEE_ERROR_NOT_SUPPORTED; 55197d17e7SSY Chiu } 56197d17e7SSY Chiu 57ab35d7adSCedric Chaumont /* Configuration properties */ 58ab35d7adSCedric Chaumont /* API implementation version */ 59ab35d7adSCedric Chaumont static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION); 60ab35d7adSCedric Chaumont 61ab35d7adSCedric Chaumont /* Implementation description (implementation-dependent) */ 62ab35d7adSCedric Chaumont static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR); 63ab35d7adSCedric Chaumont 64b0104773SPascal Brand /* 65ab35d7adSCedric Chaumont * TA persistent time protection level 66ab35d7adSCedric Chaumont * 100: Persistent time based on an REE-controlled real-time clock 67ab35d7adSCedric Chaumont * and on the TEE Trusted Storage for the storage of origins (default). 68ab35d7adSCedric Chaumont * 1000: Persistent time based on a TEE-controlled real-time clock 69ab35d7adSCedric Chaumont * and the TEE Trusted Storage. 70ab35d7adSCedric Chaumont * The real-time clock MUST be out of reach of software attacks 71ab35d7adSCedric Chaumont * from the REE. 72ab35d7adSCedric Chaumont */ 73b0104773SPascal Brand static const uint32_t ta_time_prot_lvl = 100; 74ab35d7adSCedric Chaumont 7564a5011eSPascal Brand /* Elliptic Curve Cryptographic support */ 7664a5011eSPascal Brand #ifdef CFG_CRYPTO_ECC 77d5d50c3cSJens Wiklander static const bool crypto_ecc_en = 1; 7864a5011eSPascal Brand #else 79d5d50c3cSJens Wiklander static const bool crypto_ecc_en; 8064a5011eSPascal Brand #endif 8164a5011eSPascal Brand 82ab35d7adSCedric Chaumont /* 83ab35d7adSCedric Chaumont * Trusted storage anti rollback protection level 84ab35d7adSCedric Chaumont * 0 (or missing): No antirollback protection (default) 85ab35d7adSCedric Chaumont * 100: Antirollback enforced at REE level 86ab35d7adSCedric Chaumont * 1000: Antirollback TEE-controlled hardware 87ab35d7adSCedric Chaumont */ 88b4b1a20cSJens Wiklander #ifdef CFG_RPMB_FS 89b4b1a20cSJens Wiklander static const uint32_t ts_antiroll_prot_lvl = 1000; 90b4b1a20cSJens Wiklander #else 91ab35d7adSCedric Chaumont static const uint32_t ts_antiroll_prot_lvl; 92b4b1a20cSJens Wiklander #endif 93ab35d7adSCedric Chaumont 94ab35d7adSCedric Chaumont /* Trusted OS implementation version */ 954bf425c1SJens Wiklander static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION); 96ab35d7adSCedric Chaumont 97ab35d7adSCedric Chaumont /* Trusted OS implementation version (binary value) */ 98ab35d7adSCedric Chaumont static const uint32_t trustedos_impl_bin_version; /* 0 by default */ 99ab35d7adSCedric Chaumont 100ab35d7adSCedric Chaumont /* Trusted OS implementation manufacturer name */ 101ab35d7adSCedric Chaumont static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER); 102ab35d7adSCedric Chaumont 103ab35d7adSCedric Chaumont /* Trusted firmware version */ 104ab35d7adSCedric Chaumont static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION); 105ab35d7adSCedric Chaumont 106ab35d7adSCedric Chaumont /* Trusted firmware version (binary value) */ 107ab35d7adSCedric Chaumont static const uint32_t fw_impl_bin_version; /* 0 by default */ 108ab35d7adSCedric Chaumont 109ab35d7adSCedric Chaumont /* Trusted firmware manufacturer name */ 110ab35d7adSCedric Chaumont static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER); 111ab35d7adSCedric Chaumont 112eebf7990SJens Wiklander static TEE_Result get_prop_tee_dev_id(struct tee_ta_session *sess __unused, 113ff857a3aSPascal Brand void *buf, size_t *blen) 114ab35d7adSCedric Chaumont { 115b0104773SPascal Brand TEE_Result res; 116b0104773SPascal Brand TEE_UUID uuid; 117ab35d7adSCedric Chaumont const size_t nslen = 5; 11864a5011eSPascal Brand uint8_t data[5 + FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = { 119ab35d7adSCedric Chaumont 'O', 'P', 'T', 'E', 'E' }; 120b0104773SPascal Brand 121ff857a3aSPascal Brand if (*blen < sizeof(uuid)) { 122ff857a3aSPascal Brand *blen = sizeof(uuid); 123b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 124ff857a3aSPascal Brand } 125ff857a3aSPascal Brand *blen = sizeof(uuid); 126b0104773SPascal Brand 12764a5011eSPascal Brand if (tee_otp_get_die_id(data + nslen, sizeof(data) - nslen)) 128b0104773SPascal Brand return TEE_ERROR_BAD_STATE; 129b0104773SPascal Brand 13064a5011eSPascal Brand res = tee_hash_createdigest(TEE_ALG_SHA256, data, sizeof(data), 13164a5011eSPascal Brand (uint8_t *)&uuid, sizeof(uuid)); 132b0104773SPascal Brand if (res != TEE_SUCCESS) 133b0104773SPascal Brand return TEE_ERROR_BAD_STATE; 134b0104773SPascal Brand 135b0104773SPascal Brand /* 136b0104773SPascal Brand * Changes the random value into and UUID as specifiec 137b0104773SPascal Brand * in RFC 4122. The magic values are from the example 138b0104773SPascal Brand * code in the RFC. 139b0104773SPascal Brand * 140b0104773SPascal Brand * TEE_UUID is defined slightly different from the RFC, 141b0104773SPascal Brand * but close enough for our purpose. 142b0104773SPascal Brand */ 143b0104773SPascal Brand 144b0104773SPascal Brand uuid.timeHiAndVersion &= 0x0fff; 145b0104773SPascal Brand uuid.timeHiAndVersion |= 5 << 12; 146b0104773SPascal Brand 147b0104773SPascal Brand /* uuid.clock_seq_hi_and_reserved in the RFC */ 148b0104773SPascal Brand uuid.clockSeqAndNode[0] &= 0x3f; 149b0104773SPascal Brand uuid.clockSeqAndNode[0] |= 0x80; 150b0104773SPascal Brand 151c40a6505SJens Wiklander return copy_to_user(buf, &uuid, sizeof(TEE_UUID)); 152b0104773SPascal Brand } 153b0104773SPascal Brand 154eebf7990SJens Wiklander static TEE_Result get_prop_tee_sys_time_prot_level( 155eebf7990SJens Wiklander struct tee_ta_session *sess __unused, 156ff857a3aSPascal Brand void *buf, size_t *blen) 1572cdaaacbSJerome Forissier { 1582cdaaacbSJerome Forissier uint32_t prot; 1592cdaaacbSJerome Forissier 160ff857a3aSPascal Brand if (*blen < sizeof(prot)) { 161ff857a3aSPascal Brand *blen = sizeof(prot); 1622cdaaacbSJerome Forissier return TEE_ERROR_SHORT_BUFFER; 163ff857a3aSPascal Brand } 164ff857a3aSPascal Brand *blen = sizeof(prot); 1652cdaaacbSJerome Forissier prot = tee_time_get_sys_time_protection_level(); 166c40a6505SJens Wiklander return copy_to_user(buf, &prot, sizeof(prot)); 1672cdaaacbSJerome Forissier } 1682cdaaacbSJerome Forissier 169a07c12b2SJens Wiklander static TEE_Result get_prop_client_id(struct tee_ta_session *sess __unused, 170ff857a3aSPascal Brand void *buf, size_t *blen) 17164a5011eSPascal Brand { 172ff857a3aSPascal Brand if (*blen < sizeof(TEE_Identity)) { 173ff857a3aSPascal Brand *blen = sizeof(TEE_Identity); 174b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 175ff857a3aSPascal Brand } 176ff857a3aSPascal Brand *blen = sizeof(TEE_Identity); 177c40a6505SJens Wiklander return copy_to_user(buf, &sess->clnt_id, sizeof(TEE_Identity)); 17864a5011eSPascal Brand } 17937d6ae92SPascal Brand 18064a5011eSPascal Brand static TEE_Result get_prop_ta_app_id(struct tee_ta_session *sess, 181ff857a3aSPascal Brand void *buf, size_t *blen) 18264a5011eSPascal Brand { 183ff857a3aSPascal Brand if (*blen < sizeof(TEE_UUID)) { 184ff857a3aSPascal Brand *blen = sizeof(TEE_UUID); 185b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 186ff857a3aSPascal Brand } 187ff857a3aSPascal Brand *blen = sizeof(TEE_UUID); 188c40a6505SJens Wiklander return copy_to_user(buf, &sess->ctx->uuid, sizeof(TEE_UUID)); 18964a5011eSPascal Brand } 19064a5011eSPascal Brand 19164a5011eSPascal Brand /* Properties of the set TEE_PROPSET_CURRENT_CLIENT */ 19264a5011eSPascal Brand const struct tee_props tee_propset_client[] = { 19364a5011eSPascal Brand { 19464a5011eSPascal Brand .name = "gpd.client.identity", 19564a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_IDENTITY, 19664a5011eSPascal Brand .get_prop_func = get_prop_client_id 19764a5011eSPascal Brand }, 19864a5011eSPascal Brand }; 19964a5011eSPascal Brand 20064a5011eSPascal Brand /* Properties of the set TEE_PROPSET_CURRENT_TA */ 20164a5011eSPascal Brand const struct tee_props tee_propset_ta[] = { 20264a5011eSPascal Brand { 20364a5011eSPascal Brand .name = "gpd.ta.appID", 20464a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_UUID, 20564a5011eSPascal Brand .get_prop_func = get_prop_ta_app_id 20664a5011eSPascal Brand }, 20764a5011eSPascal Brand 20864a5011eSPascal Brand /* 20964a5011eSPascal Brand * Following properties are processed directly in libutee: 21064a5011eSPascal Brand * TA_PROP_STR_SINGLE_INSTANCE 21164a5011eSPascal Brand * TA_PROP_STR_MULTI_SESSION 21264a5011eSPascal Brand * TA_PROP_STR_KEEP_ALIVE 21364a5011eSPascal Brand * TA_PROP_STR_DATA_SIZE 21464a5011eSPascal Brand * TA_PROP_STR_STACK_SIZE 21564a5011eSPascal Brand * TA_PROP_STR_VERSION 21664a5011eSPascal Brand * TA_PROP_STR_DESCRIPTION 21764a5011eSPascal Brand * USER_TA_PROP_TYPE_STRING, 21864a5011eSPascal Brand * TA_DESCRIPTION 21964a5011eSPascal Brand */ 22064a5011eSPascal Brand }; 22164a5011eSPascal Brand 22264a5011eSPascal Brand /* Properties of the set TEE_PROPSET_TEE_IMPLEMENTATION */ 22364a5011eSPascal Brand const struct tee_props tee_propset_tee[] = { 22464a5011eSPascal Brand { 22564a5011eSPascal Brand .name = "gpd.tee.apiversion", 22664a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_STRING, 22764a5011eSPascal Brand .data = api_vers, 22864a5011eSPascal Brand .len = sizeof(api_vers), 22964a5011eSPascal Brand }, 23064a5011eSPascal Brand { 23164a5011eSPascal Brand .name = "gpd.tee.description", 23264a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_STRING, 23364a5011eSPascal Brand .data = descr, .len = sizeof(descr) 23464a5011eSPascal Brand }, 23564a5011eSPascal Brand { 23664a5011eSPascal Brand .name = "gpd.tee.deviceID", 23764a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_UUID, 23864a5011eSPascal Brand .get_prop_func = get_prop_tee_dev_id 23964a5011eSPascal Brand }, 24064a5011eSPascal Brand { 24164a5011eSPascal Brand .name = "gpd.tee.systemTime.protectionLevel", 24264a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_U32, 24364a5011eSPascal Brand .get_prop_func = get_prop_tee_sys_time_prot_level 24464a5011eSPascal Brand }, 24564a5011eSPascal Brand { 24664a5011eSPascal Brand .name = "gpd.tee.TAPersistentTime.protectionLevel", 24764a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_U32, 24864a5011eSPascal Brand .data = &ta_time_prot_lvl, 24964a5011eSPascal Brand .len = sizeof(ta_time_prot_lvl) 25064a5011eSPascal Brand }, 25164a5011eSPascal Brand { 25264a5011eSPascal Brand .name = "gpd.tee.cryptography.ecc", 25364a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_BOOL, 25464a5011eSPascal Brand .data = &crypto_ecc_en, 25564a5011eSPascal Brand .len = sizeof(crypto_ecc_en) 25664a5011eSPascal Brand }, 25764a5011eSPascal Brand { 25864a5011eSPascal Brand .name = "gpd.tee.trustedStorage.antiRollback.protectionLevel", 25964a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_U32, 26064a5011eSPascal Brand .data = &ts_antiroll_prot_lvl, 26164a5011eSPascal Brand .len = sizeof(ts_antiroll_prot_lvl) 26264a5011eSPascal Brand }, 26364a5011eSPascal Brand { 26464a5011eSPascal Brand .name = "gpd.tee.trustedos.implementation.version", 26564a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_STRING, 26664a5011eSPascal Brand .data = trustedos_impl_version, 26764a5011eSPascal Brand .len = sizeof(trustedos_impl_version) 26864a5011eSPascal Brand }, 26964a5011eSPascal Brand { 27064a5011eSPascal Brand .name = "gpd.tee.trustedos.implementation.binaryversion", 27164a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_U32, 27264a5011eSPascal Brand .data = &trustedos_impl_bin_version, 27364a5011eSPascal Brand .len = sizeof(trustedos_impl_bin_version) 27464a5011eSPascal Brand }, 27564a5011eSPascal Brand { 27664a5011eSPascal Brand .name = "gpd.tee.trustedos.manufacturer", 27764a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_STRING, 27864a5011eSPascal Brand .data = trustedos_manufacturer, 27964a5011eSPascal Brand .len = sizeof(trustedos_manufacturer) 28064a5011eSPascal Brand }, 28164a5011eSPascal Brand { 28264a5011eSPascal Brand .name = "gpd.tee.firmware.implementation.version", 28364a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_STRING, 28464a5011eSPascal Brand .data = fw_impl_version, 28564a5011eSPascal Brand .len = sizeof(fw_impl_version) 28664a5011eSPascal Brand }, 28764a5011eSPascal Brand { 28864a5011eSPascal Brand .name = "gpd.tee.firmware.implementation.binaryversion", 28964a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_U32, 29064a5011eSPascal Brand .data = &fw_impl_bin_version, 29164a5011eSPascal Brand .len = sizeof(fw_impl_bin_version) 29264a5011eSPascal Brand }, 29364a5011eSPascal Brand { 29464a5011eSPascal Brand .name = "gpd.tee.firmware.manufacturer", 29564a5011eSPascal Brand .prop_type = USER_TA_PROP_TYPE_STRING, 29664a5011eSPascal Brand .data = fw_manufacturer, 29764a5011eSPascal Brand .len = sizeof(fw_manufacturer) 29864a5011eSPascal Brand }, 29964a5011eSPascal Brand 30064a5011eSPascal Brand /* 30164a5011eSPascal Brand * Following properties are processed directly in libutee: 30264a5011eSPascal Brand * gpd.tee.arith.maxBigIntSize 30364a5011eSPascal Brand */ 30464a5011eSPascal Brand }; 30564a5011eSPascal Brand 30641d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_client; 30741d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_ta; 30841d71430SPascal Brand __weak const struct tee_vendor_props vendor_props_tee; 30941d71430SPascal Brand 31064a5011eSPascal Brand static void get_prop_set(unsigned long prop_set, 31164a5011eSPascal Brand const struct tee_props **props, 31241d71430SPascal Brand size_t *size, 31341d71430SPascal Brand const struct tee_props **vendor_props, 31441d71430SPascal Brand size_t *vendor_size) 31564a5011eSPascal Brand { 31664a5011eSPascal Brand if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_CLIENT) { 31764a5011eSPascal Brand *props = tee_propset_client; 31864a5011eSPascal Brand *size = ARRAY_SIZE(tee_propset_client); 31941d71430SPascal Brand *vendor_props = vendor_props_client.props; 32041d71430SPascal Brand *vendor_size = vendor_props_client.len; 32164a5011eSPascal Brand } else if ((TEE_PropSetHandle)prop_set == TEE_PROPSET_CURRENT_TA) { 32264a5011eSPascal Brand *props = tee_propset_ta; 32364a5011eSPascal Brand *size = ARRAY_SIZE(tee_propset_ta); 32441d71430SPascal Brand *vendor_props = vendor_props_ta.props; 32541d71430SPascal Brand *vendor_size = vendor_props_ta.len; 32664a5011eSPascal Brand } else if ((TEE_PropSetHandle)prop_set == 32764a5011eSPascal Brand TEE_PROPSET_TEE_IMPLEMENTATION) { 32864a5011eSPascal Brand *props = tee_propset_tee; 32964a5011eSPascal Brand *size = ARRAY_SIZE(tee_propset_tee); 33041d71430SPascal Brand *vendor_props = vendor_props_tee.props; 33141d71430SPascal Brand *vendor_size = vendor_props_tee.len; 33264a5011eSPascal Brand } else { 33341d71430SPascal Brand *props = NULL; 33464a5011eSPascal Brand *size = 0; 33541d71430SPascal Brand *vendor_props = NULL; 33641d71430SPascal Brand *vendor_size = 0; 33764a5011eSPascal Brand } 33864a5011eSPascal Brand } 33964a5011eSPascal Brand 34064a5011eSPascal Brand static const struct tee_props *get_prop_struct(unsigned long prop_set, 34164a5011eSPascal Brand unsigned long index) 34264a5011eSPascal Brand { 34364a5011eSPascal Brand const struct tee_props *props; 34441d71430SPascal Brand const struct tee_props *vendor_props; 34541d71430SPascal Brand size_t size; 34641d71430SPascal Brand size_t vendor_size; 34764a5011eSPascal Brand 34841d71430SPascal Brand get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 34964a5011eSPascal Brand 35041d71430SPascal Brand if (index < size) 35164a5011eSPascal Brand return &(props[index]); 35241d71430SPascal Brand index -= size; 35341d71430SPascal Brand 35441d71430SPascal Brand if (index < vendor_size) 35541d71430SPascal Brand return &(vendor_props[index]); 35641d71430SPascal Brand 35764a5011eSPascal Brand return NULL; 35864a5011eSPascal Brand } 35964a5011eSPascal Brand 36064a5011eSPascal Brand /* 36164a5011eSPascal Brand * prop_set is part of TEE_PROPSET_xxx 36264a5011eSPascal Brand * index is the index in the Property Set to retrieve 36364a5011eSPascal Brand * if name is not NULL, the name of "index" property is returned 36464a5011eSPascal Brand * if buf is not NULL, the property is returned 36564a5011eSPascal Brand */ 36664a5011eSPascal Brand TEE_Result syscall_get_property(unsigned long prop_set, 36764a5011eSPascal Brand unsigned long index, 36864a5011eSPascal Brand void *name, uint32_t *name_len, 36964a5011eSPascal Brand void *buf, uint32_t *blen, 37064a5011eSPascal Brand uint32_t *prop_type) 37164a5011eSPascal Brand { 37264a5011eSPascal Brand struct tee_ta_session *sess; 37364a5011eSPascal Brand TEE_Result res; 37464a5011eSPascal Brand TEE_Result res2; 37564a5011eSPascal Brand const struct tee_props *prop; 37664a5011eSPascal Brand uint32_t klen; 377ff857a3aSPascal Brand size_t klen_size; 37864a5011eSPascal Brand uint32_t elen; 37964a5011eSPascal Brand 38064a5011eSPascal Brand prop = get_prop_struct(prop_set, index); 38164a5011eSPascal Brand if (!prop) 38264a5011eSPascal Brand return TEE_ERROR_ITEM_NOT_FOUND; 38364a5011eSPascal Brand 38464a5011eSPascal Brand res = tee_ta_get_current_session(&sess); 38564a5011eSPascal Brand if (res != TEE_SUCCESS) 38664a5011eSPascal Brand return res; 38764a5011eSPascal Brand 388ff857a3aSPascal Brand /* Get the property type */ 389ff857a3aSPascal Brand if (prop_type) { 390c40a6505SJens Wiklander res = copy_to_user(prop_type, &prop->prop_type, 391ff857a3aSPascal Brand sizeof(*prop_type)); 392ff857a3aSPascal Brand if (res != TEE_SUCCESS) 393ff857a3aSPascal Brand return res; 394ff857a3aSPascal Brand } 395ff857a3aSPascal Brand 39664a5011eSPascal Brand /* Get the property */ 39764a5011eSPascal Brand if (buf && blen) { 398c40a6505SJens Wiklander res = copy_from_user(&klen, blen, sizeof(klen)); 39964a5011eSPascal Brand if (res != TEE_SUCCESS) 40064a5011eSPascal Brand return res; 40164a5011eSPascal Brand 40264a5011eSPascal Brand if (prop->get_prop_func) { 403ff857a3aSPascal Brand klen_size = klen; 404ff857a3aSPascal Brand res = prop->get_prop_func(sess, buf, &klen_size); 405ff857a3aSPascal Brand klen = klen_size; 406c40a6505SJens Wiklander res2 = copy_to_user(blen, &klen, sizeof(*blen)); 40764a5011eSPascal Brand } else { 40864a5011eSPascal Brand if (klen < prop->len) 40964a5011eSPascal Brand res = TEE_ERROR_SHORT_BUFFER; 41064a5011eSPascal Brand else 411c40a6505SJens Wiklander res = copy_to_user(buf, prop->data, prop->len); 412c40a6505SJens Wiklander res2 = copy_to_user(blen, &prop->len, sizeof(*blen)); 413ff857a3aSPascal Brand } 41464a5011eSPascal Brand if (res2 != TEE_SUCCESS) 41564a5011eSPascal Brand return res2; 41664a5011eSPascal Brand if (res != TEE_SUCCESS) 41764a5011eSPascal Brand return res; 41864a5011eSPascal Brand } 41964a5011eSPascal Brand 42064a5011eSPascal Brand /* Get the property name */ 42164a5011eSPascal Brand if (name && name_len) { 422c40a6505SJens Wiklander res = copy_from_user(&klen, name_len, sizeof(klen)); 42364a5011eSPascal Brand if (res != TEE_SUCCESS) 42464a5011eSPascal Brand return res; 42564a5011eSPascal Brand 42664a5011eSPascal Brand elen = strlen(prop->name) + 1; 42764a5011eSPascal Brand 42864a5011eSPascal Brand if (klen < elen) 42964a5011eSPascal Brand res = TEE_ERROR_SHORT_BUFFER; 43064a5011eSPascal Brand else 431c40a6505SJens Wiklander res = copy_to_user(name, prop->name, elen); 432c40a6505SJens Wiklander res2 = copy_to_user(name_len, &elen, sizeof(*name_len)); 43364a5011eSPascal Brand if (res2 != TEE_SUCCESS) 43464a5011eSPascal Brand return res2; 43564a5011eSPascal Brand if (res != TEE_SUCCESS) 43664a5011eSPascal Brand return res; 43764a5011eSPascal Brand } 43864a5011eSPascal Brand 43964a5011eSPascal Brand return res; 44064a5011eSPascal Brand } 44164a5011eSPascal Brand 44264a5011eSPascal Brand /* 44364a5011eSPascal Brand * prop_set is part of TEE_PROPSET_xxx 44464a5011eSPascal Brand */ 44564a5011eSPascal Brand TEE_Result syscall_get_property_name_to_index(unsigned long prop_set, 44664a5011eSPascal Brand void *name, 44764a5011eSPascal Brand unsigned long name_len, 44864a5011eSPascal Brand uint32_t *index) 44964a5011eSPascal Brand { 45064a5011eSPascal Brand TEE_Result res; 45164a5011eSPascal Brand struct tee_ta_session *sess; 45264a5011eSPascal Brand const struct tee_props *props; 45341d71430SPascal Brand size_t size; 45441d71430SPascal Brand const struct tee_props *vendor_props; 45541d71430SPascal Brand size_t vendor_size; 45664a5011eSPascal Brand char *kname = 0; 45764a5011eSPascal Brand uint32_t i; 45864a5011eSPascal Brand 45941d71430SPascal Brand get_prop_set(prop_set, &props, &size, &vendor_props, &vendor_size); 46064a5011eSPascal Brand if (!props) 46164a5011eSPascal Brand return TEE_ERROR_ITEM_NOT_FOUND; 46264a5011eSPascal Brand 46364a5011eSPascal Brand res = tee_ta_get_current_session(&sess); 46464a5011eSPascal Brand if (res != TEE_SUCCESS) 46564a5011eSPascal Brand goto out; 46664a5011eSPascal Brand 46764a5011eSPascal Brand if (!name || !name_len) { 46864a5011eSPascal Brand res = TEE_ERROR_BAD_PARAMETERS; 46964a5011eSPascal Brand goto out; 47064a5011eSPascal Brand } 47164a5011eSPascal Brand 47264a5011eSPascal Brand kname = malloc(name_len); 47364a5011eSPascal Brand if (!kname) 47464a5011eSPascal Brand return TEE_ERROR_OUT_OF_MEMORY; 475c40a6505SJens Wiklander res = copy_from_user(kname, name, name_len); 47664a5011eSPascal Brand if (res != TEE_SUCCESS) 47764a5011eSPascal Brand goto out; 47864a5011eSPascal Brand kname[name_len - 1] = 0; 47964a5011eSPascal Brand 48064a5011eSPascal Brand res = TEE_ERROR_ITEM_NOT_FOUND; 48164a5011eSPascal Brand for (i = 0; i < size; i++) { 48264a5011eSPascal Brand if (!strcmp(kname, props[i].name)) { 483c40a6505SJens Wiklander res = copy_to_user(index, &i, sizeof(*index)); 48441d71430SPascal Brand goto out; 48541d71430SPascal Brand } 48641d71430SPascal Brand } 48741d71430SPascal Brand for (i = size; i < size + vendor_size; i++) { 48841d71430SPascal Brand if (!strcmp(kname, vendor_props[i - size].name)) { 489c40a6505SJens Wiklander res = copy_to_user(index, &i, sizeof(*index)); 49041d71430SPascal Brand goto out; 49164a5011eSPascal Brand } 49264a5011eSPascal Brand } 49364a5011eSPascal Brand 49464a5011eSPascal Brand out: 49570b61310SJerome Forissier free_wipe(kname); 49664a5011eSPascal Brand return res; 49764a5011eSPascal Brand } 49864a5011eSPascal Brand 499d5c5b0b7SJens Wiklander static TEE_Result utee_param_to_param(struct user_ta_ctx *utc, 500d5c5b0b7SJens Wiklander struct tee_ta_param *p, 501d5c5b0b7SJens Wiklander struct utee_params *up) 502e86f1266SJens Wiklander { 5031936dfc7SJens Wiklander size_t n = 0; 504e86f1266SJens Wiklander uint32_t types = up->types; 505e86f1266SJens Wiklander 506e86f1266SJens Wiklander p->types = types; 507e86f1266SJens Wiklander for (n = 0; n < TEE_NUM_PARAMS; n++) { 508e86f1266SJens Wiklander uintptr_t a = up->vals[n * 2]; 509e86f1266SJens Wiklander size_t b = up->vals[n * 2 + 1]; 510d5c5b0b7SJens Wiklander uint32_t flags = TEE_MEMORY_ACCESS_READ | 511d5c5b0b7SJens Wiklander TEE_MEMORY_ACCESS_ANY_OWNER; 512e86f1266SJens Wiklander 513e86f1266SJens Wiklander switch (TEE_PARAM_TYPE_GET(types, n)) { 514e86f1266SJens Wiklander case TEE_PARAM_TYPE_MEMREF_OUTPUT: 515e86f1266SJens Wiklander case TEE_PARAM_TYPE_MEMREF_INOUT: 516d5c5b0b7SJens Wiklander flags |= TEE_MEMORY_ACCESS_WRITE; 517*919a5a68SJerome Forissier fallthrough; 518d5c5b0b7SJens Wiklander case TEE_PARAM_TYPE_MEMREF_INPUT: 5190dcfa568SJens Wiklander p->u[n].mem.offs = a; 5200dcfa568SJens Wiklander p->u[n].mem.size = b; 521ee3e1c54SCedric Neveux 522ee3e1c54SCedric Neveux if (!p->u[n].mem.offs) { 523ee3e1c54SCedric Neveux /* Allow NULL memrefs if of size 0 */ 524ee3e1c54SCedric Neveux if (p->u[n].mem.size) 525ee3e1c54SCedric Neveux return TEE_ERROR_BAD_PARAMETERS; 526ee3e1c54SCedric Neveux p->u[n].mem.mobj = NULL; 527ee3e1c54SCedric Neveux break; 528ee3e1c54SCedric Neveux } 529ee3e1c54SCedric Neveux 530ee3e1c54SCedric Neveux p->u[n].mem.mobj = &mobj_virt; 531ee3e1c54SCedric Neveux 5321936dfc7SJens Wiklander if (tee_mmu_check_access_rights(&utc->uctx, flags, a, 5331936dfc7SJens Wiklander b)) 534d5c5b0b7SJens Wiklander return TEE_ERROR_ACCESS_DENIED; 535e86f1266SJens Wiklander break; 536e86f1266SJens Wiklander case TEE_PARAM_TYPE_VALUE_INPUT: 537e86f1266SJens Wiklander case TEE_PARAM_TYPE_VALUE_INOUT: 5380dcfa568SJens Wiklander p->u[n].val.a = a; 5390dcfa568SJens Wiklander p->u[n].val.b = b; 540e86f1266SJens Wiklander break; 541e86f1266SJens Wiklander default: 5420dcfa568SJens Wiklander memset(&p->u[n], 0, sizeof(p->u[n])); 543e86f1266SJens Wiklander break; 544e86f1266SJens Wiklander } 545e86f1266SJens Wiklander } 546d5c5b0b7SJens Wiklander 547d5c5b0b7SJens Wiklander return TEE_SUCCESS; 548e86f1266SJens Wiklander } 549e86f1266SJens Wiklander 5500dcfa568SJens Wiklander static TEE_Result alloc_temp_sec_mem(size_t size, struct mobj **mobj, 5516dbcd9ddSJens Wiklander uint8_t **va) 5520dcfa568SJens Wiklander { 5530dcfa568SJens Wiklander /* Allocate section in secure DDR */ 554d8555bddSJens Wiklander #ifdef CFG_PAGED_USER_TA 555d8555bddSJens Wiklander *mobj = mobj_seccpy_shm_alloc(size); 556d8555bddSJens Wiklander #else 5576dbcd9ddSJens Wiklander *mobj = mobj_mm_alloc(mobj_sec_ddr, size, &tee_mm_sec_ddr); 558d8555bddSJens Wiklander #endif 5596dbcd9ddSJens Wiklander if (!*mobj) 5600dcfa568SJens Wiklander return TEE_ERROR_GENERIC; 5610dcfa568SJens Wiklander 5626dbcd9ddSJens Wiklander *va = mobj_get_va(*mobj, 0); 5630dcfa568SJens Wiklander return TEE_SUCCESS; 5640dcfa568SJens Wiklander } 5650dcfa568SJens Wiklander 566b0104773SPascal Brand /* 567b0104773SPascal Brand * TA invokes some TA with parameter. 568b0104773SPascal Brand * If some parameters are memory references: 569b0104773SPascal Brand * - either the memref is inside TA private RAM: TA is not allowed to expose 570b0104773SPascal Brand * its private RAM: use a temporary memory buffer and copy the data. 571b0104773SPascal Brand * - or the memref is not in the TA private RAM: 572b0104773SPascal Brand * - if the memref was mapped to the TA, TA is allowed to expose it. 573b0104773SPascal Brand * - if so, converts memref virtual address into a physical address. 574b0104773SPascal Brand */ 575b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 576b0104773SPascal Brand struct tee_ta_session *called_sess, 577e86f1266SJens Wiklander struct utee_params *callee_params, 578b0104773SPascal Brand struct tee_ta_param *param, 57937070d93SJens Wiklander void *tmp_buf_va[TEE_NUM_PARAMS], 580a401bcfbSBastien Simondi size_t tmp_buf_size[TEE_NUM_PARAMS], 5816dbcd9ddSJens Wiklander struct mobj **mobj_tmp) 582b0104773SPascal Brand { 5838684fde8SJens Wiklander struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 5841936dfc7SJens Wiklander bool ta_private_memref[TEE_NUM_PARAMS] = { false, }; 5851936dfc7SJens Wiklander TEE_Result res = TEE_SUCCESS; 5861936dfc7SJens Wiklander size_t dst_offs = 0; 5871936dfc7SJens Wiklander size_t req_mem = 0; 5881936dfc7SJens Wiklander uint8_t *dst = 0; 5891936dfc7SJens Wiklander void *va = NULL; 5901936dfc7SJens Wiklander size_t n = 0; 5911936dfc7SJens Wiklander size_t s = 0; 592b0104773SPascal Brand 593b7fc217fSPascal Brand /* fill 'param' input struct with caller params description buffer */ 594b7fc217fSPascal Brand if (!callee_params) { 595e86f1266SJens Wiklander memset(param, 0, sizeof(*param)); 596b0104773SPascal Brand } else { 5971936dfc7SJens Wiklander uint32_t flags = TEE_MEMORY_ACCESS_READ | 59885daf48cSJens Wiklander TEE_MEMORY_ACCESS_WRITE | 5991936dfc7SJens Wiklander TEE_MEMORY_ACCESS_ANY_OWNER; 6001936dfc7SJens Wiklander 6011936dfc7SJens Wiklander res = tee_mmu_check_access_rights(&utc->uctx, flags, 6021936dfc7SJens Wiklander (uaddr_t)callee_params, 6031936dfc7SJens Wiklander sizeof(struct utee_params)); 604177603c7SJens Wiklander if (res != TEE_SUCCESS) 605177603c7SJens Wiklander return res; 606d5c5b0b7SJens Wiklander res = utee_param_to_param(utc, param, callee_params); 607d5c5b0b7SJens Wiklander if (res != TEE_SUCCESS) 608d5c5b0b7SJens Wiklander return res; 609b0104773SPascal Brand } 610b0104773SPascal Brand 61142fb5b2eSEtienne Carriere if (called_sess && is_pseudo_ta_ctx(called_sess->ctx)) { 61235964dc9SEtienne Carriere /* pseudo TA borrows the mapping of the calling TA */ 613b0104773SPascal Brand return TEE_SUCCESS; 614b0104773SPascal Brand } 615b0104773SPascal Brand 6160dcfa568SJens Wiklander /* All mobj in param are of type MOJB_TYPE_VIRT */ 6170dcfa568SJens Wiklander 618b0104773SPascal Brand for (n = 0; n < TEE_NUM_PARAMS; n++) { 619b0104773SPascal Brand 620b0104773SPascal Brand ta_private_memref[n] = false; 621b0104773SPascal Brand 622b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 623b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INPUT: 624b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 625b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 6260dcfa568SJens Wiklander va = (void *)param->u[n].mem.offs; 6270dcfa568SJens Wiklander s = param->u[n].mem.size; 6280dcfa568SJens Wiklander if (!va) { 629b0961f98SJerome Forissier if (s) 630b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 631b0104773SPascal Brand break; 632b0104773SPascal Brand } 633b0104773SPascal Brand /* uTA cannot expose its private memory */ 6341936dfc7SJens Wiklander if (tee_mmu_is_vbuf_inside_um_private(&utc->uctx, va, 6351936dfc7SJens Wiklander s)) { 636b0104773SPascal Brand 6370dcfa568SJens Wiklander s = ROUNDUP(s, sizeof(uint32_t)); 6388d0f8b46SJens Wiklander if (ADD_OVERFLOW(req_mem, s, &req_mem)) 639b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 640b0104773SPascal Brand ta_private_memref[n] = true; 641b0104773SPascal Brand break; 642b0104773SPascal Brand } 643b0104773SPascal Brand 6441936dfc7SJens Wiklander res = tee_mmu_vbuf_to_mobj_offs(&utc->uctx, va, s, 6450dcfa568SJens Wiklander ¶m->u[n].mem.mobj, 6460dcfa568SJens Wiklander ¶m->u[n].mem.offs); 6470dcfa568SJens Wiklander if (res != TEE_SUCCESS) 6480dcfa568SJens Wiklander return res; 649b0104773SPascal Brand break; 650b0104773SPascal Brand default: 651b0104773SPascal Brand break; 652b0104773SPascal Brand } 653b0104773SPascal Brand } 654b0104773SPascal Brand 655b0104773SPascal Brand if (req_mem == 0) 656b0104773SPascal Brand return TEE_SUCCESS; 657b0104773SPascal Brand 6586dbcd9ddSJens Wiklander res = alloc_temp_sec_mem(req_mem, mobj_tmp, &dst); 6590dcfa568SJens Wiklander if (res != TEE_SUCCESS) 6600dcfa568SJens Wiklander return res; 6616dbcd9ddSJens Wiklander dst_offs = 0; 662b0104773SPascal Brand 66368540524SIgor Opaniuk for (n = 0; n < TEE_NUM_PARAMS; n++) { 664b0104773SPascal Brand 6650dcfa568SJens Wiklander if (!ta_private_memref[n]) 666b0104773SPascal Brand continue; 667b0104773SPascal Brand 6680dcfa568SJens Wiklander s = ROUNDUP(param->u[n].mem.size, sizeof(uint32_t)); 669b0104773SPascal Brand 670b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 671b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INPUT: 672b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 6730dcfa568SJens Wiklander va = (void *)param->u[n].mem.offs; 6740dcfa568SJens Wiklander if (va) { 675c40a6505SJens Wiklander res = copy_from_user(dst, va, 6760dcfa568SJens Wiklander param->u[n].mem.size); 677b0104773SPascal Brand if (res != TEE_SUCCESS) 678b0104773SPascal Brand return res; 6790dcfa568SJens Wiklander param->u[n].mem.offs = dst_offs; 6806dbcd9ddSJens Wiklander param->u[n].mem.mobj = *mobj_tmp; 68137070d93SJens Wiklander tmp_buf_va[n] = dst; 682a401bcfbSBastien Simondi tmp_buf_size[n] = param->u[n].mem.size; 683b0104773SPascal Brand dst += s; 6840dcfa568SJens Wiklander dst_offs += s; 685b0104773SPascal Brand } 686b0104773SPascal Brand break; 687b0104773SPascal Brand 688b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 6890dcfa568SJens Wiklander va = (void *)param->u[n].mem.offs; 6900dcfa568SJens Wiklander if (va) { 6910dcfa568SJens Wiklander param->u[n].mem.offs = dst_offs; 6926dbcd9ddSJens Wiklander param->u[n].mem.mobj = *mobj_tmp; 69337070d93SJens Wiklander tmp_buf_va[n] = dst; 694a401bcfbSBastien Simondi tmp_buf_size[n] = param->u[n].mem.size; 695b0104773SPascal Brand dst += s; 6960dcfa568SJens Wiklander dst_offs += s; 697b0104773SPascal Brand } 698b0104773SPascal Brand break; 699b0104773SPascal Brand 700b0104773SPascal Brand default: 701b0104773SPascal Brand continue; 702b0104773SPascal Brand } 703b0104773SPascal Brand } 704b0104773SPascal Brand 705b0104773SPascal Brand return TEE_SUCCESS; 706b0104773SPascal Brand } 707b0104773SPascal Brand 708b0104773SPascal Brand /* 709b0104773SPascal Brand * Back from execution of service: update parameters passed from TA: 710b0104773SPascal Brand * If some parameters were memory references: 711b0104773SPascal Brand * - either the memref was temporary: copy back data and update size 712b0104773SPascal Brand * - or it was the original TA memref: update only the size value. 713b0104773SPascal Brand */ 714b0104773SPascal Brand static TEE_Result tee_svc_update_out_param( 715b0104773SPascal Brand struct tee_ta_param *param, 71637070d93SJens Wiklander void *tmp_buf_va[TEE_NUM_PARAMS], 717a401bcfbSBastien Simondi size_t tmp_buf_size[TEE_NUM_PARAMS], 718e86f1266SJens Wiklander struct utee_params *usr_param) 719b0104773SPascal Brand { 720b0104773SPascal Brand size_t n; 721359324a2SJens Wiklander uint64_t *vals = usr_param->vals; 722c40a6505SJens Wiklander size_t sz = 0; 723bc420748SJens Wiklander 724b0104773SPascal Brand for (n = 0; n < TEE_NUM_PARAMS; n++) { 725b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 726b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 727b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 728b0104773SPascal Brand /* 729359324a2SJens Wiklander * Memory copy is only needed if there's a temporary 730359324a2SJens Wiklander * buffer involved, tmp_buf_va[n] is only update if 731359324a2SJens Wiklander * a temporary buffer is used. Otherwise only the 732359324a2SJens Wiklander * size needs to be updated. 733b0104773SPascal Brand */ 734c40a6505SJens Wiklander sz = param->u[n].mem.size; 735c40a6505SJens Wiklander if (tmp_buf_va[n] && sz <= vals[n * 2 + 1]) { 736359324a2SJens Wiklander void *src = tmp_buf_va[n]; 737359324a2SJens Wiklander void *dst = (void *)(uintptr_t)vals[n * 2]; 738c40a6505SJens Wiklander TEE_Result res = TEE_SUCCESS; 739b0104773SPascal Brand 740a401bcfbSBastien Simondi /* 741a401bcfbSBastien Simondi * TA is allowed to return a size larger than 742a401bcfbSBastien Simondi * the original size. However, in such cases no 743a401bcfbSBastien Simondi * data should be synchronized as per TEE Client 744a401bcfbSBastien Simondi * API spec. 745a401bcfbSBastien Simondi */ 746c40a6505SJens Wiklander if (sz <= tmp_buf_size[n]) { 747c40a6505SJens Wiklander res = copy_to_user(dst, src, sz); 748b0104773SPascal Brand if (res != TEE_SUCCESS) 749b0104773SPascal Brand return res; 750a401bcfbSBastien Simondi } 751b0104773SPascal Brand } 752c40a6505SJens Wiklander usr_param->vals[n * 2 + 1] = sz; 753b0104773SPascal Brand break; 754b0104773SPascal Brand 755b0104773SPascal Brand case TEE_PARAM_TYPE_VALUE_OUTPUT: 756b0104773SPascal Brand case TEE_PARAM_TYPE_VALUE_INOUT: 757359324a2SJens Wiklander vals[n * 2] = param->u[n].val.a; 758359324a2SJens Wiklander vals[n * 2 + 1] = param->u[n].val.b; 759b0104773SPascal Brand break; 760b0104773SPascal Brand 761b0104773SPascal Brand default: 762b0104773SPascal Brand continue; 763b0104773SPascal Brand } 764b0104773SPascal Brand } 765b0104773SPascal Brand 766b0104773SPascal Brand return TEE_SUCCESS; 767b0104773SPascal Brand } 768b0104773SPascal Brand 769b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */ 770453a5030SJerome Forissier TEE_Result syscall_open_ta_session(const TEE_UUID *dest, 771e86f1266SJens Wiklander unsigned long cancel_req_to, 772e86f1266SJens Wiklander struct utee_params *usr_param, uint32_t *ta_sess, 773b0104773SPascal Brand uint32_t *ret_orig) 774b0104773SPascal Brand { 775b0104773SPascal Brand TEE_Result res; 776b0104773SPascal Brand uint32_t ret_o = TEE_ORIGIN_TEE; 777b0104773SPascal Brand struct tee_ta_session *s = NULL; 778b0104773SPascal Brand struct tee_ta_session *sess; 7796dbcd9ddSJens Wiklander struct mobj *mobj_param = NULL; 780b0104773SPascal Brand TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 781b0104773SPascal Brand struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 782b0104773SPascal Brand TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 783359324a2SJens Wiklander void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL }; 784a401bcfbSBastien Simondi size_t tmp_buf_size[TEE_NUM_PARAMS] = { 0 }; 7858684fde8SJens Wiklander struct user_ta_ctx *utc; 786b0104773SPascal Brand 787b0104773SPascal Brand if (uuid == NULL || param == NULL || clnt_id == NULL) { 788b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 789b0104773SPascal Brand goto out_free_only; 790b0104773SPascal Brand } 791b0104773SPascal Brand 792b0104773SPascal Brand memset(param, 0, sizeof(struct tee_ta_param)); 793b0104773SPascal Brand 794b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 795b0104773SPascal Brand if (res != TEE_SUCCESS) 796b0104773SPascal Brand goto out_free_only; 7978684fde8SJens Wiklander utc = to_user_ta_ctx(sess->ctx); 798b0104773SPascal Brand 799e12c9f67SJens Wiklander res = copy_from_user_private(uuid, dest, sizeof(TEE_UUID)); 800b0104773SPascal Brand if (res != TEE_SUCCESS) 801b0104773SPascal Brand goto function_exit; 802b0104773SPascal Brand 803b0104773SPascal Brand clnt_id->login = TEE_LOGIN_TRUSTED_APP; 804bc420748SJens Wiklander memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 805b0104773SPascal Brand 80637070d93SJens Wiklander res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_va, 807a401bcfbSBastien Simondi tmp_buf_size, &mobj_param); 808b0104773SPascal Brand if (res != TEE_SUCCESS) 809b0104773SPascal Brand goto function_exit; 810b0104773SPascal Brand 8118684fde8SJens Wiklander res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid, 81227cbcc57SJens Wiklander clnt_id, cancel_req_to, param); 8131936dfc7SJens Wiklander tee_mmu_set_ctx(&utc->uctx.ctx); 814c0346845SJens Wiklander if (res != TEE_SUCCESS) 815b0104773SPascal Brand goto function_exit; 816b0104773SPascal Brand 817a401bcfbSBastien Simondi res = tee_svc_update_out_param(param, tmp_buf_va, tmp_buf_size, 818a401bcfbSBastien Simondi usr_param); 819b0104773SPascal Brand 820b0104773SPascal Brand function_exit: 821b9651492SJens Wiklander mobj_put_wipe(mobj_param); 8222dcb3d36SJerome Forissier if (res == TEE_SUCCESS) 823e12c9f67SJens Wiklander copy_to_user_private(ta_sess, &s->id, sizeof(s->id)); 824e12c9f67SJens Wiklander copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o)); 825b0104773SPascal Brand 826b0104773SPascal Brand out_free_only: 82770b61310SJerome Forissier free_wipe(param); 82870b61310SJerome Forissier free_wipe(uuid); 82970b61310SJerome Forissier free_wipe(clnt_id); 830b0104773SPascal Brand return res; 831b0104773SPascal Brand } 832b0104773SPascal Brand 833e86f1266SJens Wiklander TEE_Result syscall_close_ta_session(unsigned long ta_sess) 834b0104773SPascal Brand { 835b0104773SPascal Brand TEE_Result res; 836b0104773SPascal Brand struct tee_ta_session *sess; 83760699957SPascal Brand TEE_Identity clnt_id; 83899164a05SJerome Forissier struct tee_ta_session *s = NULL; 8398684fde8SJens Wiklander struct user_ta_ctx *utc; 840b0104773SPascal Brand 841b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 842b0104773SPascal Brand if (res != TEE_SUCCESS) 843b0104773SPascal Brand return res; 8448684fde8SJens Wiklander utc = to_user_ta_ctx(sess->ctx); 84599164a05SJerome Forissier s = tee_ta_find_session(ta_sess, &utc->open_sessions); 846b0104773SPascal Brand 84760699957SPascal Brand clnt_id.login = TEE_LOGIN_TRUSTED_APP; 848bc420748SJens Wiklander memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 849b0104773SPascal Brand 850a07c12b2SJens Wiklander return tee_ta_close_session(s, &utc->open_sessions, &clnt_id); 851b0104773SPascal Brand } 852b0104773SPascal Brand 853e86f1266SJens Wiklander TEE_Result syscall_invoke_ta_command(unsigned long ta_sess, 854e86f1266SJens Wiklander unsigned long cancel_req_to, unsigned long cmd_id, 855e86f1266SJens Wiklander struct utee_params *usr_param, uint32_t *ret_orig) 856b0104773SPascal Brand { 857b0104773SPascal Brand TEE_Result res; 8587d82e180SJens Wiklander TEE_Result res2; 859b0104773SPascal Brand uint32_t ret_o = TEE_ORIGIN_TEE; 860b0104773SPascal Brand struct tee_ta_param param = { 0 }; 86160699957SPascal Brand TEE_Identity clnt_id; 862b0104773SPascal Brand struct tee_ta_session *sess; 863b666b6f2SJens Wiklander struct tee_ta_session *called_sess; 8646dbcd9ddSJens Wiklander struct mobj *mobj_param = NULL; 865359324a2SJens Wiklander void *tmp_buf_va[TEE_NUM_PARAMS] = { NULL }; 866a401bcfbSBastien Simondi size_t tmp_buf_size[TEE_NUM_PARAMS] = { }; 8678684fde8SJens Wiklander struct user_ta_ctx *utc; 868b0104773SPascal Brand 869b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 870b0104773SPascal Brand if (res != TEE_SUCCESS) 871b0104773SPascal Brand return res; 8728684fde8SJens Wiklander utc = to_user_ta_ctx(sess->ctx); 873b0104773SPascal Brand 87499164a05SJerome Forissier called_sess = tee_ta_get_session((uint32_t)ta_sess, true, 8758684fde8SJens Wiklander &utc->open_sessions); 876b666b6f2SJens Wiklander if (!called_sess) 877b666b6f2SJens Wiklander return TEE_ERROR_BAD_PARAMETERS; 878b0104773SPascal Brand 87960699957SPascal Brand clnt_id.login = TEE_LOGIN_TRUSTED_APP; 880bc420748SJens Wiklander memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 88160699957SPascal Brand 882e86f1266SJens Wiklander res = tee_svc_copy_param(sess, called_sess, usr_param, ¶m, 883a401bcfbSBastien Simondi tmp_buf_va, tmp_buf_size, &mobj_param); 884b0104773SPascal Brand if (res != TEE_SUCCESS) 885b0104773SPascal Brand goto function_exit; 886b0104773SPascal Brand 88760699957SPascal Brand res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 88860699957SPascal Brand cancel_req_to, cmd_id, ¶m); 889fd10f62bSOvidiu Mihalachi if (res == TEE_ERROR_TARGET_DEAD) 890fd10f62bSOvidiu Mihalachi goto function_exit; 89160699957SPascal Brand 892a401bcfbSBastien Simondi res2 = tee_svc_update_out_param(¶m, tmp_buf_va, tmp_buf_size, 893a401bcfbSBastien Simondi usr_param); 8947d82e180SJens Wiklander if (res2 != TEE_SUCCESS) { 8957d82e180SJens Wiklander /* 8967d82e180SJens Wiklander * Spec for TEE_InvokeTACommand() says: 8977d82e180SJens Wiklander * "If the return origin is different from 8987d82e180SJens Wiklander * TEE_ORIGIN_TRUSTED_APP, then the function has failed 8997d82e180SJens Wiklander * before it could reach the destination Trusted 9007d82e180SJens Wiklander * Application." 9017d82e180SJens Wiklander * 9027d82e180SJens Wiklander * But if we can't update params to the caller we have no 9037d82e180SJens Wiklander * choice we need to return some error to indicate that 9047d82e180SJens Wiklander * parameters aren't updated as expected. 9057d82e180SJens Wiklander */ 9067d82e180SJens Wiklander ret_o = TEE_ORIGIN_TEE; 9077d82e180SJens Wiklander res = res2; 9087d82e180SJens Wiklander } 909b0104773SPascal Brand 910b0104773SPascal Brand function_exit: 911b666b6f2SJens Wiklander tee_ta_put_session(called_sess); 912b9651492SJens Wiklander mobj_put_wipe(mobj_param); 913e12c9f67SJens Wiklander copy_to_user_private(ret_orig, &ret_o, sizeof(ret_o)); 914b0104773SPascal Brand return res; 915b0104773SPascal Brand } 916b0104773SPascal Brand 917e86f1266SJens Wiklander TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 918b0104773SPascal Brand size_t len) 919b0104773SPascal Brand { 9201936dfc7SJens Wiklander struct tee_ta_session *s = NULL; 9211936dfc7SJens Wiklander TEE_Result res = TEE_SUCCESS; 922b0104773SPascal Brand 923b0104773SPascal Brand res = tee_ta_get_current_session(&s); 924b0104773SPascal Brand if (res != TEE_SUCCESS) 925b0104773SPascal Brand return res; 926b0104773SPascal Brand 9271936dfc7SJens Wiklander return tee_mmu_check_access_rights(&to_user_ta_ctx(s->ctx)->uctx, flags, 9282ffdd194SJens Wiklander (uaddr_t)buf, len); 929b0104773SPascal Brand } 930b0104773SPascal Brand 931e86f1266SJens Wiklander TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 932b0104773SPascal Brand { 933b0104773SPascal Brand TEE_Result res; 934b0104773SPascal Brand struct tee_ta_session *s = NULL; 935e86f1266SJens Wiklander uint32_t c; 936b0104773SPascal Brand 937b0104773SPascal Brand res = tee_ta_get_current_session(&s); 938b0104773SPascal Brand if (res != TEE_SUCCESS) 939b0104773SPascal Brand return res; 940b0104773SPascal Brand 94163dc8d4aSJens Wiklander c = tee_ta_session_is_cancelled(s, NULL); 942b0104773SPascal Brand 943c40a6505SJens Wiklander return copy_to_user(cancel, &c, sizeof(c)); 944b0104773SPascal Brand } 945b0104773SPascal Brand 946e86f1266SJens Wiklander TEE_Result syscall_unmask_cancellation(uint32_t *old_mask) 947b0104773SPascal Brand { 948b0104773SPascal Brand TEE_Result res; 949b0104773SPascal Brand struct tee_ta_session *s = NULL; 950e86f1266SJens Wiklander uint32_t m; 951b0104773SPascal Brand 952b0104773SPascal Brand res = tee_ta_get_current_session(&s); 953b0104773SPascal Brand if (res != TEE_SUCCESS) 954b0104773SPascal Brand return res; 955b0104773SPascal Brand 956b0104773SPascal Brand m = s->cancel_mask; 957b0104773SPascal Brand s->cancel_mask = false; 958c40a6505SJens Wiklander return copy_to_user(old_mask, &m, sizeof(m)); 959b0104773SPascal Brand } 960b0104773SPascal Brand 961e86f1266SJens Wiklander TEE_Result syscall_mask_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 = true; 973c40a6505SJens Wiklander return copy_to_user(old_mask, &m, sizeof(m)); 974b0104773SPascal Brand } 975b0104773SPascal Brand 976e86f1266SJens Wiklander TEE_Result syscall_wait(unsigned long timeout) 977b0104773SPascal Brand { 978b0104773SPascal Brand TEE_Result res = TEE_SUCCESS; 979b0104773SPascal Brand uint32_t mytime = 0; 980b0104773SPascal Brand struct tee_ta_session *s; 981b0104773SPascal Brand TEE_Time base_time; 982b0104773SPascal Brand TEE_Time current_time; 983b0104773SPascal Brand 984b0104773SPascal Brand res = tee_ta_get_current_session(&s); 985b0104773SPascal Brand if (res != TEE_SUCCESS) 986b0104773SPascal Brand return res; 987b0104773SPascal Brand 988b0104773SPascal Brand res = tee_time_get_sys_time(&base_time); 989b0104773SPascal Brand if (res != TEE_SUCCESS) 990b0104773SPascal Brand return res; 991b0104773SPascal Brand 992b0104773SPascal Brand while (true) { 993b0104773SPascal Brand res = tee_time_get_sys_time(¤t_time); 994b0104773SPascal Brand if (res != TEE_SUCCESS) 995b0104773SPascal Brand return res; 996b0104773SPascal Brand 99763dc8d4aSJens Wiklander if (tee_ta_session_is_cancelled(s, ¤t_time)) 998b0104773SPascal Brand return TEE_ERROR_CANCEL; 999b0104773SPascal Brand 1000b0104773SPascal Brand mytime = (current_time.seconds - base_time.seconds) * 1000 + 1001b0104773SPascal Brand (int)current_time.millis - (int)base_time.millis; 1002b0104773SPascal Brand if (mytime >= timeout) 1003b0104773SPascal Brand return TEE_SUCCESS; 1004b0104773SPascal Brand 1005d1aea08fSSY Chiu tee_time_wait(timeout - mytime); 1006b0104773SPascal Brand } 1007b0104773SPascal Brand 1008b0104773SPascal Brand return res; 1009b0104773SPascal Brand } 1010b0104773SPascal Brand 1011e86f1266SJens Wiklander TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime) 1012b0104773SPascal Brand { 1013b0104773SPascal Brand TEE_Result res, res2; 1014b0104773SPascal Brand struct tee_ta_session *s = NULL; 1015b0104773SPascal Brand TEE_Time t; 1016b0104773SPascal Brand 1017b0104773SPascal Brand res = tee_ta_get_current_session(&s); 1018b0104773SPascal Brand if (res != TEE_SUCCESS) 1019b0104773SPascal Brand return res; 1020b0104773SPascal Brand 1021b0104773SPascal Brand switch (cat) { 1022b0104773SPascal Brand case UTEE_TIME_CAT_SYSTEM: 1023b0104773SPascal Brand res = tee_time_get_sys_time(&t); 1024b0104773SPascal Brand break; 1025b0104773SPascal Brand case UTEE_TIME_CAT_TA_PERSISTENT: 1026bc420748SJens Wiklander res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 1027b0104773SPascal Brand break; 1028b0104773SPascal Brand case UTEE_TIME_CAT_REE: 1029b0104773SPascal Brand res = tee_time_get_ree_time(&t); 1030b0104773SPascal Brand break; 1031b0104773SPascal Brand default: 1032b0104773SPascal Brand res = TEE_ERROR_BAD_PARAMETERS; 1033b0104773SPascal Brand break; 1034b0104773SPascal Brand } 1035b0104773SPascal Brand 1036b0104773SPascal Brand if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 1037e12c9f67SJens Wiklander res2 = copy_to_user_private(mytime, &t, sizeof(t)); 1038b0104773SPascal Brand if (res2 != TEE_SUCCESS) 1039b0104773SPascal Brand res = res2; 1040b0104773SPascal Brand } 1041b0104773SPascal Brand 1042b0104773SPascal Brand return res; 1043b0104773SPascal Brand } 1044b0104773SPascal Brand 1045453a5030SJerome Forissier TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 1046b0104773SPascal Brand { 1047b0104773SPascal Brand TEE_Result res; 1048b0104773SPascal Brand struct tee_ta_session *s = NULL; 1049b0104773SPascal Brand TEE_Time t; 1050b0104773SPascal Brand 1051b0104773SPascal Brand res = tee_ta_get_current_session(&s); 1052b0104773SPascal Brand if (res != TEE_SUCCESS) 1053b0104773SPascal Brand return res; 1054b0104773SPascal Brand 1055e12c9f67SJens Wiklander res = copy_from_user_private(&t, mytime, sizeof(t)); 1056b0104773SPascal Brand if (res != TEE_SUCCESS) 1057b0104773SPascal Brand return res; 1058b0104773SPascal Brand 1059bc420748SJens Wiklander return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 1060b0104773SPascal Brand } 1061