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> 37b0104773SPascal Brand #include <kernel/tee_rpc.h> 38b0104773SPascal Brand #include <kernel/tee_rpc_types.h> 39b0104773SPascal Brand #include <kernel/tee_time.h> 40b0104773SPascal Brand 41b0104773SPascal Brand #include <user_ta_header.h> 424de4bebcSJens Wiklander #include <trace.h> 434de4bebcSJens Wiklander #include <kernel/trace_ta.h> 44b0104773SPascal Brand #include <kernel/chip_services.h> 458684fde8SJens Wiklander #include <kernel/static_ta.h> 46b0104773SPascal Brand 47e86f1266SJens Wiklander #include <assert.h> 48e86f1266SJens Wiklander 49e86f1266SJens Wiklander vaddr_t tee_svc_uref_base; 50e86f1266SJens Wiklander 51*3276098dSJerome Forissier void syscall_log(const void *buf __maybe_unused, size_t len __maybe_unused) 52b0104773SPascal Brand { 537c876f12SPascal Brand #ifdef CFG_TEE_CORE_TA_TRACE 54b0104773SPascal Brand char *kbuf; 55b0104773SPascal Brand 56b0104773SPascal Brand if (len == 0) 57b0104773SPascal Brand return; 58b0104773SPascal Brand 59b0104773SPascal Brand kbuf = malloc(len); 60b0104773SPascal Brand if (kbuf == NULL) 61b0104773SPascal Brand return; 62c0346845SJens Wiklander *kbuf = '\0'; 63b0104773SPascal Brand 64b0104773SPascal Brand /* log as Info/Raw traces */ 65b0104773SPascal Brand if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS) 664de4bebcSJens Wiklander TAMSG_RAW("%.*s", (int)len, kbuf); 67b0104773SPascal Brand 68b0104773SPascal Brand free(kbuf); 697c876f12SPascal Brand #endif 70b0104773SPascal Brand } 71b0104773SPascal Brand 72453a5030SJerome Forissier TEE_Result syscall_not_supported(void) 73197d17e7SSY Chiu { 74197d17e7SSY Chiu return TEE_ERROR_NOT_SUPPORTED; 75197d17e7SSY Chiu } 76197d17e7SSY Chiu 77*3276098dSJerome Forissier uint32_t syscall_dummy(uint32_t *a __maybe_unused) 78b0104773SPascal Brand { 79851aa858SJens Wiklander DMSG("tee_svc_sys_dummy: a 0x%" PRIxVA, (vaddr_t)a); 80b0104773SPascal Brand return 0; 81b0104773SPascal Brand } 82b0104773SPascal Brand 83*3276098dSJerome Forissier uint32_t syscall_dummy_7args(unsigned long a1 __maybe_unused, 84*3276098dSJerome Forissier unsigned long a2 __maybe_unused, 85*3276098dSJerome Forissier unsigned long a3 __maybe_unused, 86*3276098dSJerome Forissier unsigned long a4 __maybe_unused, 87*3276098dSJerome Forissier unsigned long a5 __maybe_unused, 88*3276098dSJerome Forissier unsigned long a6 __maybe_unused, 89*3276098dSJerome Forissier unsigned long a7 __maybe_unused) 90b0104773SPascal Brand { 91e86f1266SJens Wiklander DMSG("tee_svc_sys_dummy_7args: 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, %lx, %lx\n", 92b0104773SPascal Brand a1, a2, a3, a4, a5, a6, a7); 93b0104773SPascal Brand return 0; 94b0104773SPascal Brand } 95b0104773SPascal Brand 96453a5030SJerome Forissier uint32_t syscall_nocall(void) 97b0104773SPascal Brand { 98b0104773SPascal Brand DMSG("No syscall"); 99b0104773SPascal Brand return 0x1; 100b0104773SPascal Brand } 101b0104773SPascal Brand 102ab35d7adSCedric Chaumont /* Configuration properties */ 103ab35d7adSCedric Chaumont /* API implementation version */ 104ab35d7adSCedric Chaumont static const char api_vers[] = TO_STR(CFG_TEE_API_VERSION); 105ab35d7adSCedric Chaumont 106ab35d7adSCedric Chaumont /* Implementation description (implementation-dependent) */ 107ab35d7adSCedric Chaumont static const char descr[] = TO_STR(CFG_TEE_IMPL_DESCR); 108ab35d7adSCedric Chaumont 109b0104773SPascal Brand /* 110ab35d7adSCedric Chaumont * TA persistent time protection level 111ab35d7adSCedric Chaumont * 100: Persistent time based on an REE-controlled real-time clock 112ab35d7adSCedric Chaumont * and on the TEE Trusted Storage for the storage of origins (default). 113ab35d7adSCedric Chaumont * 1000: Persistent time based on a TEE-controlled real-time clock 114ab35d7adSCedric Chaumont * and the TEE Trusted Storage. 115ab35d7adSCedric Chaumont * The real-time clock MUST be out of reach of software attacks 116ab35d7adSCedric Chaumont * from the REE. 117ab35d7adSCedric Chaumont */ 118b0104773SPascal Brand static const uint32_t ta_time_prot_lvl = 100; 119ab35d7adSCedric Chaumont 120ab35d7adSCedric Chaumont /* Elliptic Curve Cryptographic support (false by default) */ 121ab35d7adSCedric Chaumont static const bool crypto_ecc_en; 122ab35d7adSCedric Chaumont 123ab35d7adSCedric Chaumont /* 124ab35d7adSCedric Chaumont * Trusted storage anti rollback protection level 125ab35d7adSCedric Chaumont * 0 (or missing): No antirollback protection (default) 126ab35d7adSCedric Chaumont * 100: Antirollback enforced at REE level 127ab35d7adSCedric Chaumont * 1000: Antirollback TEE-controlled hardware 128ab35d7adSCedric Chaumont */ 129ab35d7adSCedric Chaumont static const uint32_t ts_antiroll_prot_lvl; 130ab35d7adSCedric Chaumont 131ab35d7adSCedric Chaumont /* Trusted OS implementation version */ 1324bf425c1SJens Wiklander static const char trustedos_impl_version[] = TO_STR(TEE_IMPL_VERSION); 133ab35d7adSCedric Chaumont 134ab35d7adSCedric Chaumont /* Trusted OS implementation version (binary value) */ 135ab35d7adSCedric Chaumont static const uint32_t trustedos_impl_bin_version; /* 0 by default */ 136ab35d7adSCedric Chaumont 137ab35d7adSCedric Chaumont /* Trusted OS implementation manufacturer name */ 138ab35d7adSCedric Chaumont static const char trustedos_manufacturer[] = TO_STR(CFG_TEE_MANUFACTURER); 139ab35d7adSCedric Chaumont 140ab35d7adSCedric Chaumont /* Trusted firmware version */ 141ab35d7adSCedric Chaumont static const char fw_impl_version[] = TO_STR(CFG_TEE_FW_IMPL_VERSION); 142ab35d7adSCedric Chaumont 143ab35d7adSCedric Chaumont /* Trusted firmware version (binary value) */ 144ab35d7adSCedric Chaumont static const uint32_t fw_impl_bin_version; /* 0 by default */ 145ab35d7adSCedric Chaumont 146ab35d7adSCedric Chaumont /* Trusted firmware manufacturer name */ 147ab35d7adSCedric Chaumont static const char fw_manufacturer[] = TO_STR(CFG_TEE_FW_MANUFACTURER); 148ab35d7adSCedric Chaumont 149ab35d7adSCedric Chaumont struct tee_props { 150ab35d7adSCedric Chaumont const void *data; 151ab35d7adSCedric Chaumont const size_t len; 152ab35d7adSCedric Chaumont }; 153ab35d7adSCedric Chaumont 154ab35d7adSCedric Chaumont /* Consistent with enum utee_property */ 155ab35d7adSCedric Chaumont const struct tee_props tee_props_lut[] = { 156ab35d7adSCedric Chaumont {api_vers, sizeof(api_vers)}, 157ab35d7adSCedric Chaumont {descr, sizeof(descr)}, 158ab35d7adSCedric Chaumont {0, 0}, /* dev_id */ 1592cdaaacbSJerome Forissier {0, 0}, /* system time protection level */ 160ab35d7adSCedric Chaumont {&ta_time_prot_lvl, sizeof(ta_time_prot_lvl)}, 161ab35d7adSCedric Chaumont {&crypto_ecc_en, sizeof(crypto_ecc_en)}, 162ab35d7adSCedric Chaumont {&ts_antiroll_prot_lvl, sizeof(ts_antiroll_prot_lvl)}, 163ab35d7adSCedric Chaumont {trustedos_impl_version, sizeof(trustedos_impl_version)}, 164ab35d7adSCedric Chaumont {&trustedos_impl_bin_version, 165ab35d7adSCedric Chaumont sizeof(trustedos_impl_bin_version)}, 166ab35d7adSCedric Chaumont {trustedos_manufacturer, sizeof(trustedos_manufacturer)}, 167ab35d7adSCedric Chaumont {fw_impl_version, sizeof(fw_impl_version)}, 168ab35d7adSCedric Chaumont {&fw_impl_bin_version, sizeof(fw_impl_bin_version)}, 169ab35d7adSCedric Chaumont {fw_manufacturer, sizeof(fw_manufacturer)}, 170ab35d7adSCedric Chaumont {0, 0}, /* client_id */ 171ab35d7adSCedric Chaumont {0, 0}, /* ta_app_id */ 172ab35d7adSCedric Chaumont }; 173ab35d7adSCedric Chaumont 174e86f1266SJens Wiklander TEE_Result syscall_get_property(unsigned long prop, void *buf, size_t blen) 175ab35d7adSCedric Chaumont { 176b0104773SPascal Brand struct tee_ta_session *sess; 177b0104773SPascal Brand TEE_Result res; 178b0104773SPascal Brand 179ab35d7adSCedric Chaumont if (prop > ARRAY_SIZE(tee_props_lut)-1) 180ab35d7adSCedric Chaumont return TEE_ERROR_NOT_IMPLEMENTED; 181ab35d7adSCedric Chaumont 182b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 183b0104773SPascal Brand if (res != TEE_SUCCESS) 184b0104773SPascal Brand return res; 185b0104773SPascal Brand 186b0104773SPascal Brand switch (prop) { 187b0104773SPascal Brand case UTEE_PROP_TEE_DEV_ID: 188b0104773SPascal Brand { 189b0104773SPascal Brand TEE_UUID uuid; 190ab35d7adSCedric Chaumont const size_t nslen = 5; 191ab35d7adSCedric Chaumont uint8_t data[5 + 192b0104773SPascal Brand FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = { 193ab35d7adSCedric Chaumont 'O', 'P', 'T', 'E', 'E' }; 194b0104773SPascal Brand 195b0104773SPascal Brand if (blen < sizeof(uuid)) 196b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 197b0104773SPascal Brand 198b0104773SPascal Brand if (tee_otp_get_die_id 199b0104773SPascal Brand (data + nslen, sizeof(data) - nslen)) 200b0104773SPascal Brand return TEE_ERROR_BAD_STATE; 201b0104773SPascal Brand 202ffe04039SJerome Forissier res = tee_hash_createdigest(TEE_ALG_SHA256, data, 203ffe04039SJerome Forissier sizeof(data), 204ffe04039SJerome Forissier (uint8_t *)&uuid, 205ffe04039SJerome Forissier sizeof(uuid)); 206b0104773SPascal Brand if (res != TEE_SUCCESS) 207b0104773SPascal Brand return TEE_ERROR_BAD_STATE; 208b0104773SPascal Brand 209b0104773SPascal Brand /* 210b0104773SPascal Brand * Changes the random value into and UUID as specifiec 211b0104773SPascal Brand * in RFC 4122. The magic values are from the example 212b0104773SPascal Brand * code in the RFC. 213b0104773SPascal Brand * 214b0104773SPascal Brand * TEE_UUID is defined slightly different from the RFC, 215b0104773SPascal Brand * but close enough for our purpose. 216b0104773SPascal Brand */ 217b0104773SPascal Brand 218b0104773SPascal Brand uuid.timeHiAndVersion &= 0x0fff; 219b0104773SPascal Brand uuid.timeHiAndVersion |= 5 << 12; 220b0104773SPascal Brand 221b0104773SPascal Brand /* uuid.clock_seq_hi_and_reserved in the RFC */ 222b0104773SPascal Brand uuid.clockSeqAndNode[0] &= 0x3f; 223b0104773SPascal Brand uuid.clockSeqAndNode[0] |= 0x80; 224b0104773SPascal Brand 225e86f1266SJens Wiklander return tee_svc_copy_to_user(sess, buf, &uuid, 226b0104773SPascal Brand sizeof(TEE_UUID)); 227b0104773SPascal Brand } 228b0104773SPascal Brand 2292cdaaacbSJerome Forissier case UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL: 2302cdaaacbSJerome Forissier { 2312cdaaacbSJerome Forissier uint32_t prot; 2322cdaaacbSJerome Forissier 2332cdaaacbSJerome Forissier if (blen < sizeof(prot)) 2342cdaaacbSJerome Forissier return TEE_ERROR_SHORT_BUFFER; 2352cdaaacbSJerome Forissier prot = tee_time_get_sys_time_protection_level(); 2362cdaaacbSJerome Forissier return tee_svc_copy_to_user(sess, (void *)buf, 2372cdaaacbSJerome Forissier &prot, sizeof(prot)); 2382cdaaacbSJerome Forissier } 2392cdaaacbSJerome Forissier 240b0104773SPascal Brand case UTEE_PROP_CLIENT_ID: 241b0104773SPascal Brand if (blen < sizeof(TEE_Identity)) 242b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 243e86f1266SJens Wiklander return tee_svc_copy_to_user(sess, buf, &sess->clnt_id, 244ab35d7adSCedric Chaumont sizeof(TEE_Identity)); 24537d6ae92SPascal Brand 246b0104773SPascal Brand case UTEE_PROP_TA_APP_ID: 247b0104773SPascal Brand if (blen < sizeof(TEE_UUID)) 248b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 249e86f1266SJens Wiklander return tee_svc_copy_to_user(sess, buf, &sess->ctx->uuid, 250ab35d7adSCedric Chaumont sizeof(TEE_UUID)); 251b0104773SPascal Brand default: 252ab35d7adSCedric Chaumont if (blen < tee_props_lut[prop].len) 253ab35d7adSCedric Chaumont return TEE_ERROR_SHORT_BUFFER; 254e86f1266SJens Wiklander return tee_svc_copy_to_user(sess, buf, tee_props_lut[prop].data, 255ab35d7adSCedric Chaumont tee_props_lut[prop].len); 256b0104773SPascal Brand } 257b0104773SPascal Brand } 258b0104773SPascal Brand 259e86f1266SJens Wiklander static void utee_param_to_param(struct tee_ta_param *p, struct utee_params *up) 260e86f1266SJens Wiklander { 261e86f1266SJens Wiklander size_t n; 262e86f1266SJens Wiklander uint32_t types = up->types; 263e86f1266SJens Wiklander 264e86f1266SJens Wiklander p->types = types; 265e86f1266SJens Wiklander for (n = 0; n < TEE_NUM_PARAMS; n++) { 266e86f1266SJens Wiklander uintptr_t a = up->vals[n * 2]; 267e86f1266SJens Wiklander size_t b = up->vals[n * 2 + 1]; 268e86f1266SJens Wiklander 269e86f1266SJens Wiklander switch (TEE_PARAM_TYPE_GET(types, n)) { 270e86f1266SJens Wiklander case TEE_PARAM_TYPE_MEMREF_INPUT: 271e86f1266SJens Wiklander case TEE_PARAM_TYPE_MEMREF_OUTPUT: 272e86f1266SJens Wiklander case TEE_PARAM_TYPE_MEMREF_INOUT: 273e86f1266SJens Wiklander p->params[n].memref.buffer = (void *)a; 274e86f1266SJens Wiklander p->params[n].memref.size = b; 275e86f1266SJens Wiklander break; 276e86f1266SJens Wiklander case TEE_PARAM_TYPE_VALUE_INPUT: 277e86f1266SJens Wiklander case TEE_PARAM_TYPE_VALUE_INOUT: 278e86f1266SJens Wiklander p->params[n].value.a = a; 279e86f1266SJens Wiklander p->params[n].value.b = b; 280e86f1266SJens Wiklander break; 281e86f1266SJens Wiklander default: 282e86f1266SJens Wiklander p->params[n].value.a = 0; 283e86f1266SJens Wiklander p->params[n].value.b = 0; 284e86f1266SJens Wiklander break; 285e86f1266SJens Wiklander } 286e86f1266SJens Wiklander } 287e86f1266SJens Wiklander } 288e86f1266SJens Wiklander 289b0104773SPascal Brand /* 290b0104773SPascal Brand * TA invokes some TA with parameter. 291b0104773SPascal Brand * If some parameters are memory references: 292b0104773SPascal Brand * - either the memref is inside TA private RAM: TA is not allowed to expose 293b0104773SPascal Brand * its private RAM: use a temporary memory buffer and copy the data. 294b0104773SPascal Brand * - or the memref is not in the TA private RAM: 295b0104773SPascal Brand * - if the memref was mapped to the TA, TA is allowed to expose it. 296b0104773SPascal Brand * - if so, converts memref virtual address into a physical address. 297b0104773SPascal Brand */ 298b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 299b0104773SPascal Brand struct tee_ta_session *called_sess, 300e86f1266SJens Wiklander struct utee_params *callee_params, 301b0104773SPascal Brand struct tee_ta_param *param, 302b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 303b0104773SPascal Brand tee_mm_entry_t **mm) 304b0104773SPascal Brand { 305b0104773SPascal Brand size_t n; 306b0104773SPascal Brand TEE_Result res; 307b0104773SPascal Brand size_t req_mem = 0; 308b0104773SPascal Brand size_t s; 309b0104773SPascal Brand uint8_t *dst = 0; 310b0104773SPascal Brand tee_paddr_t dst_pa, src_pa = 0; 311b0104773SPascal Brand bool ta_private_memref[TEE_NUM_PARAMS]; 3128684fde8SJens Wiklander struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 313b0104773SPascal Brand 314b7fc217fSPascal Brand /* fill 'param' input struct with caller params description buffer */ 315b7fc217fSPascal Brand if (!callee_params) { 316e86f1266SJens Wiklander memset(param, 0, sizeof(*param)); 317b0104773SPascal Brand } else { 3188684fde8SJens Wiklander res = tee_mmu_check_access_rights(utc, 319177603c7SJens Wiklander TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER, 320e86f1266SJens Wiklander (tee_uaddr_t)callee_params, sizeof(struct utee_params)); 321177603c7SJens Wiklander if (res != TEE_SUCCESS) 322177603c7SJens Wiklander return res; 323e86f1266SJens Wiklander utee_param_to_param(param, callee_params); 324b0104773SPascal Brand } 325b0104773SPascal Brand 3268684fde8SJens Wiklander if (called_sess && is_static_ta_ctx(called_sess->ctx)) { 327b0104773SPascal Brand /* 3288684fde8SJens Wiklander * static TA, borrow the mapping of the calling 329b0104773SPascal Brand * during this call. 330b0104773SPascal Brand */ 331b0104773SPascal Brand called_sess->calling_sess = sess; 332b0104773SPascal Brand return TEE_SUCCESS; 333b0104773SPascal Brand } 334b0104773SPascal Brand 335b0104773SPascal Brand for (n = 0; n < TEE_NUM_PARAMS; n++) { 336b0104773SPascal Brand 337b0104773SPascal Brand ta_private_memref[n] = false; 338b0104773SPascal Brand 339b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 340b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INPUT: 341b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 342b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 343b0104773SPascal Brand if (param->params[n].memref.buffer == NULL) { 344b0104773SPascal Brand if (param->params[n].memref.size != 0) 345b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 346b0104773SPascal Brand break; 347b0104773SPascal Brand } 348b0104773SPascal Brand /* uTA cannot expose its private memory */ 3498684fde8SJens Wiklander if (tee_mmu_is_vbuf_inside_ta_private(utc, 350106d8aa6SPascal Brand param->params[n].memref.buffer, 351b0104773SPascal Brand param->params[n].memref.size)) { 352b0104773SPascal Brand 353a17acc4cSSabrina Ni s = ROUNDUP(param->params[n].memref.size, 354b0104773SPascal Brand sizeof(uint32_t)); 355b0104773SPascal Brand /* Check overflow */ 356b0104773SPascal Brand if (req_mem + s < req_mem) 357b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 358b0104773SPascal Brand req_mem += s; 359b0104773SPascal Brand ta_private_memref[n] = true; 360b0104773SPascal Brand break; 361b0104773SPascal Brand } 3628684fde8SJens Wiklander if (tee_mmu_is_vbuf_intersect_ta_private(utc, 363106d8aa6SPascal Brand param->params[n].memref.buffer, 364b0104773SPascal Brand param->params[n].memref.size)) 365b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 366b0104773SPascal Brand 3678684fde8SJens Wiklander if (tee_mmu_user_va2pa(utc, 368b0104773SPascal Brand (void *)param->params[n].memref.buffer, 3690e692b78SJens Wiklander &src_pa) != TEE_SUCCESS) 370b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 371b0104773SPascal Brand 372b0104773SPascal Brand param->param_attr[n] = tee_mmu_user_get_cache_attr( 3738684fde8SJens Wiklander utc, (void *)param->params[n].memref.buffer); 374b0104773SPascal Brand 375b0104773SPascal Brand param->params[n].memref.buffer = (void *)src_pa; 376b0104773SPascal Brand break; 377b0104773SPascal Brand 378b0104773SPascal Brand default: 379b0104773SPascal Brand break; 380b0104773SPascal Brand } 381b0104773SPascal Brand } 382b0104773SPascal Brand 383b0104773SPascal Brand if (req_mem == 0) 384b0104773SPascal Brand return TEE_SUCCESS; 385b0104773SPascal Brand 386b0104773SPascal Brand /* Allocate section in secure DDR */ 387b0104773SPascal Brand *mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem); 388b0104773SPascal Brand if (*mm == NULL) { 389b0104773SPascal Brand DMSG("tee_mm_alloc TEE_ERROR_GENERIC"); 390b0104773SPascal Brand return TEE_ERROR_GENERIC; 391b0104773SPascal Brand } 392b0104773SPascal Brand 393b0104773SPascal Brand /* Get the virtual address for the section in secure DDR */ 394b0104773SPascal Brand res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst); 395b0104773SPascal Brand if (res != TEE_SUCCESS) 396b0104773SPascal Brand return res; 397b0104773SPascal Brand dst_pa = tee_mm_get_smem(*mm); 398b0104773SPascal Brand 399b0104773SPascal Brand for (n = 0; n < 4; n++) { 400b0104773SPascal Brand 401b0104773SPascal Brand if (ta_private_memref[n] == false) 402b0104773SPascal Brand continue; 403b0104773SPascal Brand 404a17acc4cSSabrina Ni s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t)); 405b0104773SPascal Brand 406b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 407b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INPUT: 408b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 409b0104773SPascal Brand if (param->params[n].memref.buffer != NULL) { 410b0104773SPascal Brand res = tee_svc_copy_from_user(sess, dst, 411b7fc217fSPascal Brand param->params[n].memref.buffer, 412b7fc217fSPascal Brand param->params[n].memref.size); 413b0104773SPascal Brand if (res != TEE_SUCCESS) 414b0104773SPascal Brand return res; 415b0104773SPascal Brand param->param_attr[n] = 416b0104773SPascal Brand tee_mmu_kmap_get_cache_attr(dst); 417b0104773SPascal Brand param->params[n].memref.buffer = (void *)dst_pa; 418b0104773SPascal Brand tmp_buf_pa[n] = dst_pa; 419b0104773SPascal Brand dst += s; 420b0104773SPascal Brand dst_pa += s; 421b0104773SPascal Brand } 422b0104773SPascal Brand break; 423b0104773SPascal Brand 424b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 425b0104773SPascal Brand if (param->params[n].memref.buffer != NULL) { 426b0104773SPascal Brand param->param_attr[n] = 427b0104773SPascal Brand tee_mmu_kmap_get_cache_attr(dst); 428b0104773SPascal Brand param->params[n].memref.buffer = (void *)dst_pa; 429b0104773SPascal Brand tmp_buf_pa[n] = dst_pa; 430b0104773SPascal Brand dst += s; 431b0104773SPascal Brand dst_pa += s; 432b0104773SPascal Brand } 433b0104773SPascal Brand break; 434b0104773SPascal Brand 435b0104773SPascal Brand default: 436b0104773SPascal Brand continue; 437b0104773SPascal Brand } 438b0104773SPascal Brand } 439b0104773SPascal Brand 440b0104773SPascal Brand tee_mmu_kunmap(dst, req_mem); 441b0104773SPascal Brand 442b0104773SPascal Brand return TEE_SUCCESS; 443b0104773SPascal Brand } 444b0104773SPascal Brand 445b0104773SPascal Brand /* 446b0104773SPascal Brand * Back from execution of service: update parameters passed from TA: 447b0104773SPascal Brand * If some parameters were memory references: 448b0104773SPascal Brand * - either the memref was temporary: copy back data and update size 449b0104773SPascal Brand * - or it was the original TA memref: update only the size value. 450b0104773SPascal Brand */ 451b0104773SPascal Brand static TEE_Result tee_svc_update_out_param( 452b0104773SPascal Brand struct tee_ta_session *sess, 453b0104773SPascal Brand struct tee_ta_session *called_sess, 454b0104773SPascal Brand struct tee_ta_param *param, 455b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 456e86f1266SJens Wiklander struct utee_params *usr_param) 457b0104773SPascal Brand { 458b0104773SPascal Brand size_t n; 459e86f1266SJens Wiklander void *p; 4608684fde8SJens Wiklander struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx); 4618684fde8SJens Wiklander bool have_private_mem_map = is_user_ta_ctx(called_sess->ctx); 462bc420748SJens Wiklander 463b0104773SPascal Brand tee_ta_set_current_session(sess); 464b0104773SPascal Brand 465b0104773SPascal Brand for (n = 0; n < TEE_NUM_PARAMS; n++) { 466b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 467b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 468b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 469e86f1266SJens Wiklander p = (void *)(uintptr_t)usr_param->vals[n * 2]; 470b0104773SPascal Brand 471b0104773SPascal Brand /* outside TA private => memref is valid, update size */ 4728684fde8SJens Wiklander if (!tee_mmu_is_vbuf_inside_ta_private(utc, p, 473b0104773SPascal Brand param->params[n].memref.size)) { 474e86f1266SJens Wiklander usr_param->vals[n * 2 + 1] = 475b0104773SPascal Brand param->params[n].memref.size; 476b0104773SPascal Brand break; 477b0104773SPascal Brand } 478b0104773SPascal Brand 479b0104773SPascal Brand /* 480b0104773SPascal Brand * If we called a kernel TA the parameters are in shared 481b0104773SPascal Brand * memory and no copy is needed. 482b0104773SPascal Brand */ 483b0104773SPascal Brand if (have_private_mem_map && 484b0104773SPascal Brand param->params[n].memref.size <= 485e86f1266SJens Wiklander usr_param->vals[n * 2 + 1]) { 486b0104773SPascal Brand uint8_t *src = 0; 487b0104773SPascal Brand TEE_Result res; 488b0104773SPascal Brand 489b0104773SPascal Brand /* FIXME: TA_RAM is already mapped ! */ 490b0104773SPascal Brand res = tee_mmu_kmap(tmp_buf_pa[n], 491b0104773SPascal Brand param->params[n].memref.size, &src); 492b0104773SPascal Brand if (res != TEE_SUCCESS) 493b0104773SPascal Brand return TEE_ERROR_GENERIC; 494b0104773SPascal Brand 495e86f1266SJens Wiklander res = tee_svc_copy_to_user(sess, p, src, 496e86f1266SJens Wiklander param->params[n].memref.size); 497b0104773SPascal Brand if (res != TEE_SUCCESS) 498b0104773SPascal Brand return res; 499b0104773SPascal Brand tee_mmu_kunmap(src, 500b0104773SPascal Brand param->params[n].memref.size); 501b0104773SPascal Brand 502b0104773SPascal Brand } 503e86f1266SJens Wiklander usr_param->vals[n * 2 + 1] = 504e86f1266SJens Wiklander param->params[n].memref.size; 505b0104773SPascal Brand break; 506b0104773SPascal Brand 507b0104773SPascal Brand case TEE_PARAM_TYPE_VALUE_OUTPUT: 508b0104773SPascal Brand case TEE_PARAM_TYPE_VALUE_INOUT: 509e86f1266SJens Wiklander usr_param->vals[n * 2] = param->params[n].value.a; 510e86f1266SJens Wiklander usr_param->vals[n * 2 + 1] = param->params[n].value.b; 511b0104773SPascal Brand break; 512b0104773SPascal Brand 513b0104773SPascal Brand default: 514b0104773SPascal Brand continue; 515b0104773SPascal Brand } 516b0104773SPascal Brand } 517b0104773SPascal Brand 518b0104773SPascal Brand return TEE_SUCCESS; 519b0104773SPascal Brand } 520b0104773SPascal Brand 521b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */ 522453a5030SJerome Forissier TEE_Result syscall_open_ta_session(const TEE_UUID *dest, 523e86f1266SJens Wiklander unsigned long cancel_req_to, 524e86f1266SJens Wiklander struct utee_params *usr_param, uint32_t *ta_sess, 525b0104773SPascal Brand uint32_t *ret_orig) 526b0104773SPascal Brand { 527b0104773SPascal Brand TEE_Result res; 528b0104773SPascal Brand uint32_t ret_o = TEE_ORIGIN_TEE; 529b0104773SPascal Brand struct tee_ta_session *s = NULL; 530b0104773SPascal Brand struct tee_ta_session *sess; 531b0104773SPascal Brand tee_mm_entry_t *mm_param = NULL; 532b0104773SPascal Brand TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 533b0104773SPascal Brand struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 534b0104773SPascal Brand TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 535b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 5368684fde8SJens Wiklander struct user_ta_ctx *utc; 537b0104773SPascal Brand 538b0104773SPascal Brand if (uuid == NULL || param == NULL || clnt_id == NULL) { 539b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 540b0104773SPascal Brand goto out_free_only; 541b0104773SPascal Brand } 542b0104773SPascal Brand 543b0104773SPascal Brand memset(param, 0, sizeof(struct tee_ta_param)); 544b0104773SPascal Brand 545b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 546b0104773SPascal Brand if (res != TEE_SUCCESS) 547b0104773SPascal Brand goto out_free_only; 5488684fde8SJens Wiklander utc = to_user_ta_ctx(sess->ctx); 549b0104773SPascal Brand 550b0104773SPascal Brand res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID)); 551b0104773SPascal Brand if (res != TEE_SUCCESS) 552b0104773SPascal Brand goto function_exit; 553b0104773SPascal Brand 554b0104773SPascal Brand clnt_id->login = TEE_LOGIN_TRUSTED_APP; 555bc420748SJens Wiklander memcpy(&clnt_id->uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 556b0104773SPascal Brand 557e86f1266SJens Wiklander res = tee_svc_copy_param(sess, NULL, usr_param, param, tmp_buf_pa, 558e86f1266SJens Wiklander &mm_param); 559b0104773SPascal Brand if (res != TEE_SUCCESS) 560b0104773SPascal Brand goto function_exit; 561b0104773SPascal Brand 562b0104773SPascal Brand /* 563b0104773SPascal Brand * Find session of a multi session TA or a static TA 564b0104773SPascal Brand * In such a case, there is no need to ask the supplicant for the TA 565b0104773SPascal Brand * code 566b0104773SPascal Brand */ 5678684fde8SJens Wiklander res = tee_ta_open_session(&ret_o, &s, &utc->open_sessions, uuid, 56827cbcc57SJens Wiklander clnt_id, cancel_req_to, param); 569c0346845SJens Wiklander if (res != TEE_SUCCESS) 570b0104773SPascal Brand goto function_exit; 571b0104773SPascal Brand 5728684fde8SJens Wiklander res = tee_svc_update_out_param(sess, s, param, tmp_buf_pa, usr_param); 573b0104773SPascal Brand 574b0104773SPascal Brand function_exit: 575b0104773SPascal Brand tee_ta_set_current_session(sess); 5760dcea1a3SJens Wiklander sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 577b0104773SPascal Brand 578b0104773SPascal Brand if (mm_param != NULL) { 579b0104773SPascal Brand TEE_Result res2; 580b0104773SPascal Brand void *va = 0; 581b0104773SPascal Brand 582b0104773SPascal Brand res2 = 583b0104773SPascal Brand tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 584b0104773SPascal Brand if (res2 == TEE_SUCCESS) 585b0104773SPascal Brand tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 586b0104773SPascal Brand } 587b0104773SPascal Brand tee_mm_free(mm_param); 5882dcb3d36SJerome Forissier if (res == TEE_SUCCESS) 589e86f1266SJens Wiklander tee_svc_copy_kaddr_to_uref(sess, ta_sess, s); 590b0104773SPascal Brand tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 591b0104773SPascal Brand 592b0104773SPascal Brand out_free_only: 593b0104773SPascal Brand free(param); 594b0104773SPascal Brand free(uuid); 595b0104773SPascal Brand free(clnt_id); 596b0104773SPascal Brand return res; 597b0104773SPascal Brand } 598b0104773SPascal Brand 599e86f1266SJens Wiklander TEE_Result syscall_close_ta_session(unsigned long ta_sess) 600b0104773SPascal Brand { 601b0104773SPascal Brand TEE_Result res; 602b0104773SPascal Brand struct tee_ta_session *sess; 60360699957SPascal Brand TEE_Identity clnt_id; 604e86f1266SJens Wiklander struct tee_ta_session *s = tee_svc_uref_to_kaddr(ta_sess); 6058684fde8SJens Wiklander struct user_ta_ctx *utc; 606b0104773SPascal Brand 607b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 608b0104773SPascal Brand if (res != TEE_SUCCESS) 609b0104773SPascal Brand return res; 6108684fde8SJens Wiklander utc = to_user_ta_ctx(sess->ctx); 611b0104773SPascal Brand 61260699957SPascal Brand clnt_id.login = TEE_LOGIN_TRUSTED_APP; 613bc420748SJens Wiklander memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 614b0104773SPascal Brand 61560699957SPascal Brand tee_ta_set_current_session(NULL); 6168684fde8SJens Wiklander res = tee_ta_close_session(s, &utc->open_sessions, &clnt_id); 617b0104773SPascal Brand tee_ta_set_current_session(sess); 618b0104773SPascal Brand return res; 619b0104773SPascal Brand } 620b0104773SPascal Brand 621e86f1266SJens Wiklander TEE_Result syscall_invoke_ta_command(unsigned long ta_sess, 622e86f1266SJens Wiklander unsigned long cancel_req_to, unsigned long cmd_id, 623e86f1266SJens Wiklander struct utee_params *usr_param, uint32_t *ret_orig) 624b0104773SPascal Brand { 625b0104773SPascal Brand TEE_Result res; 626b0104773SPascal Brand uint32_t ret_o = TEE_ORIGIN_TEE; 627b0104773SPascal Brand struct tee_ta_param param = { 0 }; 62860699957SPascal Brand TEE_Identity clnt_id; 629b0104773SPascal Brand struct tee_ta_session *sess; 630b666b6f2SJens Wiklander struct tee_ta_session *called_sess; 631b0104773SPascal Brand tee_mm_entry_t *mm_param = NULL; 632b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 6338684fde8SJens Wiklander struct user_ta_ctx *utc; 634b0104773SPascal Brand 635b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 636b0104773SPascal Brand if (res != TEE_SUCCESS) 637b0104773SPascal Brand return res; 6388684fde8SJens Wiklander utc = to_user_ta_ctx(sess->ctx); 639b0104773SPascal Brand 640e86f1266SJens Wiklander called_sess = tee_ta_get_session( 641e86f1266SJens Wiklander (vaddr_t)tee_svc_uref_to_kaddr(ta_sess), true, 6428684fde8SJens Wiklander &utc->open_sessions); 643b666b6f2SJens Wiklander if (!called_sess) 644b666b6f2SJens Wiklander return TEE_ERROR_BAD_PARAMETERS; 645b0104773SPascal Brand 64660699957SPascal Brand clnt_id.login = TEE_LOGIN_TRUSTED_APP; 647bc420748SJens Wiklander memcpy(&clnt_id.uuid, &sess->ctx->uuid, sizeof(TEE_UUID)); 64860699957SPascal Brand 649e86f1266SJens Wiklander res = tee_svc_copy_param(sess, called_sess, usr_param, ¶m, 650e86f1266SJens Wiklander tmp_buf_pa, &mm_param); 651b0104773SPascal Brand if (res != TEE_SUCCESS) 652b0104773SPascal Brand goto function_exit; 653b0104773SPascal Brand 65460699957SPascal Brand res = tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, 65560699957SPascal Brand cancel_req_to, cmd_id, ¶m); 65660699957SPascal Brand 657b0104773SPascal Brand if (res != TEE_SUCCESS) 658b0104773SPascal Brand goto function_exit; 659b0104773SPascal Brand 660b0104773SPascal Brand res = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_pa, 661177603c7SJens Wiklander usr_param); 662b0104773SPascal Brand if (res != TEE_SUCCESS) 663b0104773SPascal Brand goto function_exit; 664b0104773SPascal Brand 665b0104773SPascal Brand function_exit: 666b0104773SPascal Brand tee_ta_set_current_session(sess); 667b0104773SPascal Brand called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 668b666b6f2SJens Wiklander tee_ta_put_session(called_sess); 669b0104773SPascal Brand 670b0104773SPascal Brand if (mm_param != NULL) { 671b0104773SPascal Brand TEE_Result res2; 672b0104773SPascal Brand void *va = 0; 673b0104773SPascal Brand 674b0104773SPascal Brand res2 = 675b0104773SPascal Brand tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 676b0104773SPascal Brand if (res2 == TEE_SUCCESS) 677b0104773SPascal Brand tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 678b0104773SPascal Brand } 679b0104773SPascal Brand tee_mm_free(mm_param); 680b0104773SPascal Brand if (ret_orig) 681b0104773SPascal Brand tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 682b0104773SPascal Brand return res; 683b0104773SPascal Brand } 684b0104773SPascal Brand 685e86f1266SJens Wiklander TEE_Result syscall_check_access_rights(unsigned long flags, const void *buf, 686b0104773SPascal Brand size_t len) 687b0104773SPascal Brand { 688b0104773SPascal Brand TEE_Result res; 689b0104773SPascal Brand struct tee_ta_session *s; 690b0104773SPascal Brand 691b0104773SPascal Brand res = tee_ta_get_current_session(&s); 692b0104773SPascal Brand if (res != TEE_SUCCESS) 693b0104773SPascal Brand return res; 694b0104773SPascal Brand 6958684fde8SJens Wiklander return tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), flags, 6968684fde8SJens Wiklander (tee_uaddr_t)buf, len); 697b0104773SPascal Brand } 698b0104773SPascal Brand 699b0104773SPascal Brand TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr, 700b0104773SPascal Brand const void *uaddr, size_t len) 701b0104773SPascal Brand { 702b0104773SPascal Brand TEE_Result res; 703b0104773SPascal Brand struct tee_ta_session *s; 704b0104773SPascal Brand 705b0104773SPascal Brand if (sess == NULL) { 706b0104773SPascal Brand res = tee_ta_get_current_session(&s); 707b0104773SPascal Brand if (res != TEE_SUCCESS) 708b0104773SPascal Brand return res; 709b0104773SPascal Brand } else { 710b0104773SPascal Brand s = sess; 711b0104773SPascal Brand tee_ta_set_current_session(s); 712b0104773SPascal Brand } 7138684fde8SJens Wiklander res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 714b0104773SPascal Brand TEE_MEMORY_ACCESS_READ | 715b0104773SPascal Brand TEE_MEMORY_ACCESS_ANY_OWNER, 716b0104773SPascal Brand (tee_uaddr_t)uaddr, len); 717b0104773SPascal Brand if (res != TEE_SUCCESS) 718b0104773SPascal Brand return res; 719b0104773SPascal Brand 720b0104773SPascal Brand memcpy(kaddr, uaddr, len); 721b0104773SPascal Brand return TEE_SUCCESS; 722b0104773SPascal Brand } 723b0104773SPascal Brand 724b0104773SPascal Brand TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr, 725b0104773SPascal Brand const void *kaddr, size_t len) 726b0104773SPascal Brand { 727b0104773SPascal Brand TEE_Result res; 728b0104773SPascal Brand struct tee_ta_session *s; 729b0104773SPascal Brand 730b0104773SPascal Brand if (sess == NULL) { 731b0104773SPascal Brand res = tee_ta_get_current_session(&s); 732b0104773SPascal Brand if (res != TEE_SUCCESS) 733b0104773SPascal Brand return res; 734b0104773SPascal Brand } else { 735b0104773SPascal Brand s = sess; 736b0104773SPascal Brand tee_ta_set_current_session(s); 737b0104773SPascal Brand } 738b0104773SPascal Brand 7398684fde8SJens Wiklander res = tee_mmu_check_access_rights(to_user_ta_ctx(s->ctx), 740b0104773SPascal Brand TEE_MEMORY_ACCESS_WRITE | 741b0104773SPascal Brand TEE_MEMORY_ACCESS_ANY_OWNER, 742b0104773SPascal Brand (tee_uaddr_t)uaddr, len); 743b0104773SPascal Brand if (res != TEE_SUCCESS) 744b0104773SPascal Brand return res; 745b0104773SPascal Brand 746b0104773SPascal Brand memcpy(uaddr, kaddr, len); 747b0104773SPascal Brand return TEE_SUCCESS; 748b0104773SPascal Brand } 749b0104773SPascal Brand 750e86f1266SJens Wiklander TEE_Result tee_svc_copy_kaddr_to_uref(struct tee_ta_session *sess, 751e86f1266SJens Wiklander uint32_t *uref, void *kaddr) 7528707ec0fSJerome Forissier { 753e86f1266SJens Wiklander uint32_t ref = tee_svc_kaddr_to_uref(kaddr); 7548707ec0fSJerome Forissier 755e86f1266SJens Wiklander return tee_svc_copy_to_user(sess, uref, &ref, sizeof(ref)); 7568707ec0fSJerome Forissier } 7578707ec0fSJerome Forissier 758b0104773SPascal Brand static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time) 759b0104773SPascal Brand { 760b0104773SPascal Brand TEE_Time current_time; 761b0104773SPascal Brand 762b0104773SPascal Brand if (s->cancel_mask) 763b0104773SPascal Brand return false; 764b0104773SPascal Brand 765b0104773SPascal Brand if (s->cancel) 766b0104773SPascal Brand return true; 767b0104773SPascal Brand 768b0104773SPascal Brand if (s->cancel_time.seconds == UINT32_MAX) 769b0104773SPascal Brand return false; 770b0104773SPascal Brand 771b0104773SPascal Brand if (curr_time != NULL) 772b0104773SPascal Brand current_time = *curr_time; 773b0104773SPascal Brand else if (tee_time_get_sys_time(¤t_time) != TEE_SUCCESS) 774b0104773SPascal Brand return false; 775b0104773SPascal Brand 776b0104773SPascal Brand if (current_time.seconds > s->cancel_time.seconds || 777b0104773SPascal Brand (current_time.seconds == s->cancel_time.seconds && 778b0104773SPascal Brand current_time.millis >= s->cancel_time.millis)) { 779b0104773SPascal Brand return true; 780b0104773SPascal Brand } 781b0104773SPascal Brand 782b0104773SPascal Brand return false; 783b0104773SPascal Brand } 784b0104773SPascal Brand 785e86f1266SJens Wiklander TEE_Result syscall_get_cancellation_flag(uint32_t *cancel) 786b0104773SPascal Brand { 787b0104773SPascal Brand TEE_Result res; 788b0104773SPascal Brand struct tee_ta_session *s = NULL; 789e86f1266SJens Wiklander uint32_t c; 790b0104773SPascal Brand 791b0104773SPascal Brand res = tee_ta_get_current_session(&s); 792b0104773SPascal Brand if (res != TEE_SUCCESS) 793b0104773SPascal Brand return res; 794b0104773SPascal Brand 795b0104773SPascal Brand c = session_is_cancelled(s, NULL); 796b0104773SPascal Brand 797b0104773SPascal Brand return tee_svc_copy_to_user(s, cancel, &c, sizeof(c)); 798b0104773SPascal Brand } 799b0104773SPascal Brand 800e86f1266SJens Wiklander TEE_Result syscall_unmask_cancellation(uint32_t *old_mask) 801b0104773SPascal Brand { 802b0104773SPascal Brand TEE_Result res; 803b0104773SPascal Brand struct tee_ta_session *s = NULL; 804e86f1266SJens Wiklander uint32_t m; 805b0104773SPascal Brand 806b0104773SPascal Brand res = tee_ta_get_current_session(&s); 807b0104773SPascal Brand if (res != TEE_SUCCESS) 808b0104773SPascal Brand return res; 809b0104773SPascal Brand 810b0104773SPascal Brand m = s->cancel_mask; 811b0104773SPascal Brand s->cancel_mask = false; 812b0104773SPascal Brand return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 813b0104773SPascal Brand } 814b0104773SPascal Brand 815e86f1266SJens Wiklander TEE_Result syscall_mask_cancellation(uint32_t *old_mask) 816b0104773SPascal Brand { 817b0104773SPascal Brand TEE_Result res; 818b0104773SPascal Brand struct tee_ta_session *s = NULL; 819e86f1266SJens Wiklander uint32_t m; 820b0104773SPascal Brand 821b0104773SPascal Brand res = tee_ta_get_current_session(&s); 822b0104773SPascal Brand if (res != TEE_SUCCESS) 823b0104773SPascal Brand return res; 824b0104773SPascal Brand 825b0104773SPascal Brand m = s->cancel_mask; 826b0104773SPascal Brand s->cancel_mask = true; 827b0104773SPascal Brand return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 828b0104773SPascal Brand } 829b0104773SPascal Brand 830e86f1266SJens Wiklander TEE_Result syscall_wait(unsigned long timeout) 831b0104773SPascal Brand { 832b0104773SPascal Brand TEE_Result res = TEE_SUCCESS; 833b0104773SPascal Brand uint32_t mytime = 0; 834b0104773SPascal Brand struct tee_ta_session *s; 835b0104773SPascal Brand TEE_Time base_time; 836b0104773SPascal Brand TEE_Time current_time; 837b0104773SPascal Brand 838b0104773SPascal Brand res = tee_ta_get_current_session(&s); 839b0104773SPascal Brand if (res != TEE_SUCCESS) 840b0104773SPascal Brand return res; 841b0104773SPascal Brand 842b0104773SPascal Brand res = tee_time_get_sys_time(&base_time); 843b0104773SPascal Brand if (res != TEE_SUCCESS) 844b0104773SPascal Brand return res; 845b0104773SPascal Brand 846b0104773SPascal Brand while (true) { 847b0104773SPascal Brand res = tee_time_get_sys_time(¤t_time); 848b0104773SPascal Brand if (res != TEE_SUCCESS) 849b0104773SPascal Brand return res; 850b0104773SPascal Brand 851b0104773SPascal Brand if (session_is_cancelled(s, ¤t_time)) 852b0104773SPascal Brand return TEE_ERROR_CANCEL; 853b0104773SPascal Brand 854b0104773SPascal Brand mytime = (current_time.seconds - base_time.seconds) * 1000 + 855b0104773SPascal Brand (int)current_time.millis - (int)base_time.millis; 856b0104773SPascal Brand if (mytime >= timeout) 857b0104773SPascal Brand return TEE_SUCCESS; 858b0104773SPascal Brand 859d1aea08fSSY Chiu tee_time_wait(timeout - mytime); 860b0104773SPascal Brand } 861b0104773SPascal Brand 862b0104773SPascal Brand return res; 863b0104773SPascal Brand } 864b0104773SPascal Brand 865e86f1266SJens Wiklander TEE_Result syscall_get_time(unsigned long cat, TEE_Time *mytime) 866b0104773SPascal Brand { 867b0104773SPascal Brand TEE_Result res, res2; 868b0104773SPascal Brand struct tee_ta_session *s = NULL; 869b0104773SPascal Brand TEE_Time t; 870b0104773SPascal Brand 871b0104773SPascal Brand res = tee_ta_get_current_session(&s); 872b0104773SPascal Brand if (res != TEE_SUCCESS) 873b0104773SPascal Brand return res; 874b0104773SPascal Brand 875b0104773SPascal Brand switch (cat) { 876b0104773SPascal Brand case UTEE_TIME_CAT_SYSTEM: 877b0104773SPascal Brand res = tee_time_get_sys_time(&t); 878b0104773SPascal Brand break; 879b0104773SPascal Brand case UTEE_TIME_CAT_TA_PERSISTENT: 880bc420748SJens Wiklander res = tee_time_get_ta_time((const void *)&s->ctx->uuid, &t); 881b0104773SPascal Brand break; 882b0104773SPascal Brand case UTEE_TIME_CAT_REE: 883b0104773SPascal Brand res = tee_time_get_ree_time(&t); 884b0104773SPascal Brand break; 885b0104773SPascal Brand default: 886b0104773SPascal Brand res = TEE_ERROR_BAD_PARAMETERS; 887b0104773SPascal Brand break; 888b0104773SPascal Brand } 889b0104773SPascal Brand 890b0104773SPascal Brand if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 891b0104773SPascal Brand res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t)); 892b0104773SPascal Brand if (res2 != TEE_SUCCESS) 893b0104773SPascal Brand res = res2; 894b0104773SPascal Brand } 895b0104773SPascal Brand 896b0104773SPascal Brand return res; 897b0104773SPascal Brand } 898b0104773SPascal Brand 899453a5030SJerome Forissier TEE_Result syscall_set_ta_time(const TEE_Time *mytime) 900b0104773SPascal Brand { 901b0104773SPascal Brand TEE_Result res; 902b0104773SPascal Brand struct tee_ta_session *s = NULL; 903b0104773SPascal Brand TEE_Time t; 904b0104773SPascal Brand 905b0104773SPascal Brand res = tee_ta_get_current_session(&s); 906b0104773SPascal Brand if (res != TEE_SUCCESS) 907b0104773SPascal Brand return res; 908b0104773SPascal Brand 909b0104773SPascal Brand res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t)); 910b0104773SPascal Brand if (res != TEE_SUCCESS) 911b0104773SPascal Brand return res; 912b0104773SPascal Brand 913bc420748SJens Wiklander return tee_time_set_ta_time((const void *)&s->ctx->uuid, &t); 914b0104773SPascal Brand } 915