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 */ 27b0104773SPascal Brand #include <kernel/tee_common_otp.h> 28b0104773SPascal Brand #include <kernel/tee_common.h> 29b0104773SPascal Brand #include <kernel/tee_compat.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> 34b0104773SPascal Brand #include <mm/tee_mmu.h> 35b0104773SPascal Brand #include <mm/tee_mm.h> 36b0104773SPascal Brand #include <kernel/tee_rpc.h> 37b0104773SPascal Brand #include <kernel/tee_rpc_types.h> 38b0104773SPascal Brand #include <kernel/tee_time.h> 39b0104773SPascal Brand 40b0104773SPascal Brand #include <user_ta_header.h> 41b0104773SPascal Brand #include <kernel/tee_core_trace.h> 42b0104773SPascal Brand #include <kernel/tee_kta_trace.h> 43b0104773SPascal Brand #include <kernel/chip_services.h> 44b0104773SPascal Brand #include <tee/tee_hash.h> 45b0104773SPascal Brand #include <tee_ltc_wrapper.h> 46b0104773SPascal Brand 47b0104773SPascal Brand 48b0104773SPascal Brand void tee_svc_sys_log(const void *buf, size_t len) 49b0104773SPascal Brand { 50b0104773SPascal Brand char *kbuf; 51b0104773SPascal Brand 52b0104773SPascal Brand if (len == 0) 53b0104773SPascal Brand return; 54b0104773SPascal Brand 55b0104773SPascal Brand kbuf = malloc(len); 56b0104773SPascal Brand if (kbuf == NULL) 57b0104773SPascal Brand return; 58*c0346845SJens Wiklander *kbuf = '\0'; 59b0104773SPascal Brand 60b0104773SPascal Brand /* log as Info/Raw traces */ 61b0104773SPascal Brand if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS) 62b0104773SPascal Brand ATAMSG_RAW("%s", kbuf); 63b0104773SPascal Brand 64b0104773SPascal Brand free(kbuf); 65b0104773SPascal Brand } 66b0104773SPascal Brand 67b0104773SPascal Brand void tee_svc_sys_panic(uint32_t code) 68b0104773SPascal Brand { 69b0104773SPascal Brand struct tee_ta_session *sess; 70b0104773SPascal Brand 71b0104773SPascal Brand if (tee_ta_get_current_session(&sess) == TEE_SUCCESS) { 72b0104773SPascal Brand EMSG("Set session 0x%x to panicked", sess); 73b0104773SPascal Brand sess->ctx->panicked = 1; 74b0104773SPascal Brand sess->ctx->panic_code = code; 75b0104773SPascal Brand 76b0104773SPascal Brand { 77b0104773SPascal Brand /* 78b0104773SPascal Brand * Force panicking. This memory error will be trapped by 79b0104773SPascal Brand * the error exception handler myErrorHandler() 80b0104773SPascal Brand */ 81b0104773SPascal Brand EMSG("Following 'DTLB exception in bundle'"); 82b0104773SPascal Brand EMSG(" is generated with code %d", code); 83b0104773SPascal Brand int *p = 0; 84b0104773SPascal Brand *p = 1; 85b0104773SPascal Brand } 86b0104773SPascal Brand } else { 87b0104773SPascal Brand DMSG("Panic called from unknown TA"); 88b0104773SPascal Brand } 89b0104773SPascal Brand } 90b0104773SPascal Brand 91b0104773SPascal Brand uint32_t tee_svc_sys_dummy(uint32_t *a) 92b0104773SPascal Brand { 93b0104773SPascal Brand DMSG("tee_svc_sys_dummy: a 0x%x", (unsigned int)a); 94b0104773SPascal Brand return 0; 95b0104773SPascal Brand } 96b0104773SPascal Brand 97b0104773SPascal Brand uint32_t tee_svc_sys_dummy_7args(uint32_t a1, uint32_t a2, uint32_t a3, 98b0104773SPascal Brand uint32_t a4, uint32_t a5, uint32_t a6, 99b0104773SPascal Brand uint32_t a7) 100b0104773SPascal Brand { 101b0104773SPascal Brand DMSG("tee_svc_sys_dummy_7args: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, %x, %x\n", 102b0104773SPascal Brand a1, a2, a3, a4, a5, a6, a7); 103b0104773SPascal Brand return 0; 104b0104773SPascal Brand } 105b0104773SPascal Brand 106b0104773SPascal Brand uint32_t tee_svc_sys_nocall(void) 107b0104773SPascal Brand { 108b0104773SPascal Brand DMSG("No syscall"); 109b0104773SPascal Brand return 0x1; 110b0104773SPascal Brand } 111b0104773SPascal Brand 112b0104773SPascal Brand TEE_Result tee_svc_sys_get_property(uint32_t prop, tee_uaddr_t buf, size_t blen) 113b0104773SPascal Brand { 114b0104773SPascal Brand static const char api_vers[] = "1.0"; 115b0104773SPascal Brand static const char descr[] = "Version N.N"; 116b0104773SPascal Brand /* 117b0104773SPascal Brand * Value 100 means: 118b0104773SPascal Brand * System time based on REE-controlled timers. Can be tampered by the 119b0104773SPascal Brand * REE. The implementation must still guarantee that the system time 120b0104773SPascal Brand * is monotonous, i.e., successive calls to TEE_GetSystemTime must 121b0104773SPascal Brand * return increasing values of the system time. 122b0104773SPascal Brand */ 123b0104773SPascal Brand static const uint32_t sys_time_prot_lvl = 100; 124b0104773SPascal Brand static const uint32_t ta_time_prot_lvl = 100; 125b0104773SPascal Brand struct tee_ta_session *sess; 126b0104773SPascal Brand TEE_Result res; 127b0104773SPascal Brand 128b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 129b0104773SPascal Brand if (res != TEE_SUCCESS) 130b0104773SPascal Brand return res; 131b0104773SPascal Brand 132b0104773SPascal Brand switch (prop) { 133b0104773SPascal Brand case UTEE_PROP_TEE_API_VERSION: 134b0104773SPascal Brand if (blen < sizeof(api_vers)) 135b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 136b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, api_vers, 137b0104773SPascal Brand sizeof(api_vers)); 138b0104773SPascal Brand 139b0104773SPascal Brand case UTEE_PROP_TEE_DESCR: 140b0104773SPascal Brand if (blen < sizeof(descr)) 141b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 142b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, descr, 143b0104773SPascal Brand sizeof(descr)); 144b0104773SPascal Brand 145b0104773SPascal Brand case UTEE_PROP_TEE_DEV_ID: 146b0104773SPascal Brand { 147b0104773SPascal Brand TEE_UUID uuid; 148b0104773SPascal Brand const size_t nslen = 4; 149b0104773SPascal Brand uint8_t data[4 + 150b0104773SPascal Brand FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = { 151b0104773SPascal Brand 'S', 'T', 'E', 'E' }; 152b0104773SPascal Brand 153b0104773SPascal Brand if (blen < sizeof(uuid)) 154b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 155b0104773SPascal Brand 156b0104773SPascal Brand if (tee_otp_get_die_id 157b0104773SPascal Brand (data + nslen, sizeof(data) - nslen)) 158b0104773SPascal Brand return TEE_ERROR_BAD_STATE; 159b0104773SPascal Brand 160b0104773SPascal Brand res = tee_hash_createdigest( 161b0104773SPascal Brand TEE_ALG_SHA256, 162b0104773SPascal Brand data, sizeof(data), 163b0104773SPascal Brand (uint8_t *)&uuid, sizeof(uuid)); 164b0104773SPascal Brand if (res != TEE_SUCCESS) 165b0104773SPascal Brand return TEE_ERROR_BAD_STATE; 166b0104773SPascal Brand 167b0104773SPascal Brand /* 168b0104773SPascal Brand * Changes the random value into and UUID as specifiec 169b0104773SPascal Brand * in RFC 4122. The magic values are from the example 170b0104773SPascal Brand * code in the RFC. 171b0104773SPascal Brand * 172b0104773SPascal Brand * TEE_UUID is defined slightly different from the RFC, 173b0104773SPascal Brand * but close enough for our purpose. 174b0104773SPascal Brand */ 175b0104773SPascal Brand 176b0104773SPascal Brand uuid.timeHiAndVersion &= 0x0fff; 177b0104773SPascal Brand uuid.timeHiAndVersion |= 5 << 12; 178b0104773SPascal Brand 179b0104773SPascal Brand /* uuid.clock_seq_hi_and_reserved in the RFC */ 180b0104773SPascal Brand uuid.clockSeqAndNode[0] &= 0x3f; 181b0104773SPascal Brand uuid.clockSeqAndNode[0] |= 0x80; 182b0104773SPascal Brand 183b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, &uuid, 184b0104773SPascal Brand sizeof(TEE_UUID)); 185b0104773SPascal Brand } 186b0104773SPascal Brand 187b0104773SPascal Brand case UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL: 188b0104773SPascal Brand if (blen < sizeof(sys_time_prot_lvl)) 189b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 190b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, 191b0104773SPascal Brand &sys_time_prot_lvl, 192b0104773SPascal Brand sizeof(sys_time_prot_lvl)); 193b0104773SPascal Brand 194b0104773SPascal Brand case UTEE_PROP_TEE_TA_TIME_PROT_LEVEL: 195b0104773SPascal Brand if (blen < sizeof(ta_time_prot_lvl)) 196b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 197b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, 198b0104773SPascal Brand &ta_time_prot_lvl, 199b0104773SPascal Brand sizeof(ta_time_prot_lvl)); 200b0104773SPascal Brand 201b0104773SPascal Brand case UTEE_PROP_TEE_ARITH_MAX_BIG_INT_SIZE: 202b0104773SPascal Brand { 203b0104773SPascal Brand uint32_t v = LTC_MAX_BITS_PER_VARIABLE / 2; 204b0104773SPascal Brand 205b0104773SPascal Brand if (blen < sizeof(v)) 206b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 207b0104773SPascal Brand 208b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, &v, 209b0104773SPascal Brand sizeof(v)); 210b0104773SPascal Brand } 211b0104773SPascal Brand 212b0104773SPascal Brand case UTEE_PROP_CLIENT_ID: 213b0104773SPascal Brand { 214b0104773SPascal Brand if (blen < sizeof(TEE_Identity)) 215b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 216b0104773SPascal Brand 217b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, 218b0104773SPascal Brand &sess->clnt_id, 219b0104773SPascal Brand sizeof(TEE_Identity)); 220b0104773SPascal Brand } 221b0104773SPascal Brand case UTEE_PROP_TA_APP_ID: 222b0104773SPascal Brand { 223b0104773SPascal Brand if (blen < sizeof(TEE_UUID)) 224b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 225b0104773SPascal Brand 226b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, 227b0104773SPascal Brand &sess->ctx->head->uuid, 228b0104773SPascal Brand sizeof(TEE_UUID)); 229b0104773SPascal Brand } 230b0104773SPascal Brand 231b0104773SPascal Brand default: 232b0104773SPascal Brand break; 233b0104773SPascal Brand } 234b0104773SPascal Brand return TEE_ERROR_NOT_IMPLEMENTED; 235b0104773SPascal Brand } 236b0104773SPascal Brand 237b0104773SPascal Brand /* 238b0104773SPascal Brand * TA invokes some TA with parameter. 239b0104773SPascal Brand * If some parameters are memory references: 240b0104773SPascal Brand * - either the memref is inside TA private RAM: TA is not allowed to expose 241b0104773SPascal Brand * its private RAM: use a temporary memory buffer and copy the data. 242b0104773SPascal Brand * - or the memref is not in the TA private RAM: 243b0104773SPascal Brand * - if the memref was mapped to the TA, TA is allowed to expose it. 244b0104773SPascal Brand * - if so, converts memref virtual address into a physical address. 245b0104773SPascal Brand */ 246b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 247b0104773SPascal Brand struct tee_ta_session *called_sess, 248b0104773SPascal Brand uint32_t param_types, 249b0104773SPascal Brand TEE_Param params[TEE_NUM_PARAMS], 250b0104773SPascal Brand struct tee_ta_param *param, 251b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 252b0104773SPascal Brand tee_mm_entry_t **mm) 253b0104773SPascal Brand { 254b0104773SPascal Brand size_t n; 255b0104773SPascal Brand TEE_Result res; 256b0104773SPascal Brand size_t req_mem = 0; 257b0104773SPascal Brand size_t s; 258b0104773SPascal Brand uint8_t *dst = 0; 259b0104773SPascal Brand tee_paddr_t dst_pa, src_pa = 0; 260b0104773SPascal Brand bool ta_private_memref[TEE_NUM_PARAMS]; 261b0104773SPascal Brand 262b0104773SPascal Brand param->types = param_types; 263b0104773SPascal Brand if (params == NULL) { 264b0104773SPascal Brand if (param->types != 0) 265b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 266b0104773SPascal Brand memset(param->params, 0, sizeof(param->params)); 267b0104773SPascal Brand } else { 268b0104773SPascal Brand tee_svc_copy_from_user(sess, param->params, params, 269b0104773SPascal Brand sizeof(param->params)); 270b0104773SPascal Brand } 271b0104773SPascal Brand 272b0104773SPascal Brand if ((called_sess != NULL) && 273b0104773SPascal Brand (called_sess->ctx->static_ta == NULL) && 274b0104773SPascal Brand (called_sess->ctx->flags & TA_FLAG_USER_MODE) == 0) { 275b0104773SPascal Brand /* 276b0104773SPascal Brand * kernel TA, borrow the mapping of the calling 277b0104773SPascal Brand * during this call. 278b0104773SPascal Brand */ 279b0104773SPascal Brand called_sess->calling_sess = sess; 280b0104773SPascal Brand return TEE_SUCCESS; 281b0104773SPascal Brand } 282b0104773SPascal Brand 283b0104773SPascal Brand for (n = 0; n < TEE_NUM_PARAMS; n++) { 284b0104773SPascal Brand 285b0104773SPascal Brand ta_private_memref[n] = false; 286b0104773SPascal Brand 287b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 288b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INPUT: 289b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 290b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 291b0104773SPascal Brand if (param->params[n].memref.buffer == NULL) { 292b0104773SPascal Brand if (param->params[n].memref.size != 0) 293b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 294b0104773SPascal Brand break; 295b0104773SPascal Brand } 296b0104773SPascal Brand /* uTA cannot expose its private memory */ 297b0104773SPascal Brand if (tee_mmu_is_vbuf_inside_ta_private(sess->ctx, 298b0104773SPascal Brand (uintptr_t)param->params[n].memref.buffer, 299b0104773SPascal Brand param->params[n].memref.size)) { 300b0104773SPascal Brand 301b0104773SPascal Brand s = TEE_ROUNDUP(param->params[n].memref.size, 302b0104773SPascal Brand sizeof(uint32_t)); 303b0104773SPascal Brand /* Check overflow */ 304b0104773SPascal Brand if (req_mem + s < req_mem) 305b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 306b0104773SPascal Brand req_mem += s; 307b0104773SPascal Brand ta_private_memref[n] = true; 308b0104773SPascal Brand break; 309b0104773SPascal Brand } 310b0104773SPascal Brand if (!tee_mmu_is_vbuf_outside_ta_private(sess->ctx, 311b0104773SPascal Brand (uintptr_t)param->params[n].memref.buffer, 312b0104773SPascal Brand param->params[n].memref.size)) 313b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 314b0104773SPascal Brand 315b0104773SPascal Brand if (tee_mmu_user_va2pa(sess->ctx, 316b0104773SPascal Brand (void *)param->params[n].memref.buffer, 317b0104773SPascal Brand (void **)&src_pa) != TEE_SUCCESS) 318b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 319b0104773SPascal Brand 320b0104773SPascal Brand param->param_attr[n] = tee_mmu_user_get_cache_attr( 321b0104773SPascal Brand sess->ctx, 322b0104773SPascal Brand (void *)param->params[n].memref.buffer); 323b0104773SPascal Brand 324b0104773SPascal Brand param->params[n].memref.buffer = (void *)src_pa; 325b0104773SPascal Brand break; 326b0104773SPascal Brand 327b0104773SPascal Brand default: 328b0104773SPascal Brand break; 329b0104773SPascal Brand } 330b0104773SPascal Brand } 331b0104773SPascal Brand 332b0104773SPascal Brand if (req_mem == 0) 333b0104773SPascal Brand return TEE_SUCCESS; 334b0104773SPascal Brand 335b0104773SPascal Brand /* Allocate section in secure DDR */ 336b0104773SPascal Brand *mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem); 337b0104773SPascal Brand if (*mm == NULL) { 338b0104773SPascal Brand DMSG("tee_mm_alloc TEE_ERROR_GENERIC"); 339b0104773SPascal Brand return TEE_ERROR_GENERIC; 340b0104773SPascal Brand } 341b0104773SPascal Brand 342b0104773SPascal Brand /* Get the virtual address for the section in secure DDR */ 343b0104773SPascal Brand res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst); 344b0104773SPascal Brand if (res != TEE_SUCCESS) 345b0104773SPascal Brand return res; 346b0104773SPascal Brand dst_pa = tee_mm_get_smem(*mm); 347b0104773SPascal Brand 348b0104773SPascal Brand for (n = 0; n < 4; n++) { 349b0104773SPascal Brand 350b0104773SPascal Brand if (ta_private_memref[n] == false) 351b0104773SPascal Brand continue; 352b0104773SPascal Brand 353b0104773SPascal Brand s = TEE_ROUNDUP(param->params[n].memref.size, sizeof(uint32_t)); 354b0104773SPascal Brand 355b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 356b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INPUT: 357b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 358b0104773SPascal Brand if (param->params[n].memref.buffer != NULL) { 359b0104773SPascal Brand res = tee_svc_copy_from_user(sess, dst, 360b0104773SPascal Brand param->params[n]. 361b0104773SPascal Brand memref.buffer, 362b0104773SPascal Brand param->params[n]. 363b0104773SPascal Brand memref.size); 364b0104773SPascal Brand if (res != TEE_SUCCESS) 365b0104773SPascal Brand return res; 366b0104773SPascal Brand 367b0104773SPascal Brand param->param_attr[n] = 368b0104773SPascal Brand tee_mmu_kmap_get_cache_attr(dst); 369b0104773SPascal Brand param->params[n].memref.buffer = (void *)dst_pa; 370b0104773SPascal Brand tmp_buf_pa[n] = dst_pa; 371b0104773SPascal Brand dst += s; 372b0104773SPascal Brand dst_pa += s; 373b0104773SPascal Brand } 374b0104773SPascal Brand break; 375b0104773SPascal Brand 376b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 377b0104773SPascal Brand if (param->params[n].memref.buffer != NULL) { 378b0104773SPascal Brand param->param_attr[n] = 379b0104773SPascal Brand tee_mmu_kmap_get_cache_attr(dst); 380b0104773SPascal Brand param->params[n].memref.buffer = (void *)dst_pa; 381b0104773SPascal Brand tmp_buf_pa[n] = dst_pa; 382b0104773SPascal Brand dst += s; 383b0104773SPascal Brand dst_pa += s; 384b0104773SPascal Brand } 385b0104773SPascal Brand break; 386b0104773SPascal Brand 387b0104773SPascal Brand default: 388b0104773SPascal Brand continue; 389b0104773SPascal Brand } 390b0104773SPascal Brand } 391b0104773SPascal Brand 392b0104773SPascal Brand tee_mmu_kunmap(dst, req_mem); 393b0104773SPascal Brand 394b0104773SPascal Brand return TEE_SUCCESS; 395b0104773SPascal Brand } 396b0104773SPascal Brand 397b0104773SPascal Brand /* 398b0104773SPascal Brand * Back from execution of service: update parameters passed from TA: 399b0104773SPascal Brand * If some parameters were memory references: 400b0104773SPascal Brand * - either the memref was temporary: copy back data and update size 401b0104773SPascal Brand * - or it was the original TA memref: update only the size value. 402b0104773SPascal Brand */ 403b0104773SPascal Brand static TEE_Result tee_svc_update_out_param( 404b0104773SPascal Brand struct tee_ta_session *sess, 405b0104773SPascal Brand struct tee_ta_session *called_sess, 406b0104773SPascal Brand struct tee_ta_param *param, 407b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 408b0104773SPascal Brand TEE_Param params[TEE_NUM_PARAMS]) 409b0104773SPascal Brand { 410b0104773SPascal Brand size_t n; 411b0104773SPascal Brand bool have_private_mem_map = (called_sess == NULL) || 412b0104773SPascal Brand (called_sess->ctx->static_ta != NULL) || 413b0104773SPascal Brand ((called_sess->ctx->flags & TA_FLAG_USER_MODE) != 0); 414b0104773SPascal Brand 415b0104773SPascal Brand tee_ta_set_current_session(sess); 416b0104773SPascal Brand 417b0104773SPascal Brand for (n = 0; n < TEE_NUM_PARAMS; n++) { 418b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 419b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 420b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 421b0104773SPascal Brand 422b0104773SPascal Brand /* outside TA private => memref is valid, update size */ 423b0104773SPascal Brand if (!tee_mmu_is_vbuf_inside_ta_private(sess->ctx, 424b0104773SPascal Brand (uintptr_t)params[n].memref.buffer, 425b0104773SPascal Brand param->params[n].memref.size)) { 426b0104773SPascal Brand params[n].memref.size = 427b0104773SPascal Brand param->params[n].memref.size; 428b0104773SPascal Brand break; 429b0104773SPascal Brand } 430b0104773SPascal Brand 431b0104773SPascal Brand /* 432b0104773SPascal Brand * If we called a kernel TA the parameters are in shared 433b0104773SPascal Brand * memory and no copy is needed. 434b0104773SPascal Brand */ 435b0104773SPascal Brand if (have_private_mem_map && 436b0104773SPascal Brand param->params[n].memref.size <= 437b0104773SPascal Brand params[n].memref.size) { 438b0104773SPascal Brand uint8_t *src = 0; 439b0104773SPascal Brand TEE_Result res; 440b0104773SPascal Brand 441b0104773SPascal Brand /* FIXME: TA_RAM is already mapped ! */ 442b0104773SPascal Brand res = tee_mmu_kmap(tmp_buf_pa[n], 443b0104773SPascal Brand param->params[n].memref.size, &src); 444b0104773SPascal Brand if (res != TEE_SUCCESS) 445b0104773SPascal Brand return TEE_ERROR_GENERIC; 446b0104773SPascal Brand 447b0104773SPascal Brand res = tee_svc_copy_to_user(sess, 448b0104773SPascal Brand params[n].memref. 449b0104773SPascal Brand buffer, src, 450b0104773SPascal Brand param->params[n]. 451b0104773SPascal Brand memref.size); 452b0104773SPascal Brand if (res != TEE_SUCCESS) 453b0104773SPascal Brand return res; 454b0104773SPascal Brand tee_mmu_kunmap(src, 455b0104773SPascal Brand param->params[n].memref.size); 456b0104773SPascal Brand 457b0104773SPascal Brand } 458b0104773SPascal Brand params[n].memref.size = param->params[n].memref.size; 459b0104773SPascal Brand break; 460b0104773SPascal Brand 461b0104773SPascal Brand case TEE_PARAM_TYPE_VALUE_OUTPUT: 462b0104773SPascal Brand case TEE_PARAM_TYPE_VALUE_INOUT: 463b0104773SPascal Brand params[n].value = param->params[n].value; 464b0104773SPascal Brand break; 465b0104773SPascal Brand 466b0104773SPascal Brand default: 467b0104773SPascal Brand continue; 468b0104773SPascal Brand } 469b0104773SPascal Brand } 470b0104773SPascal Brand 471b0104773SPascal Brand return TEE_SUCCESS; 472b0104773SPascal Brand } 473b0104773SPascal Brand 474b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */ 475b0104773SPascal Brand TEE_Result tee_svc_open_ta_session(const TEE_UUID *dest, 476b0104773SPascal Brand uint32_t cancel_req_to, uint32_t param_types, 477b0104773SPascal Brand TEE_Param params[4], 478b0104773SPascal Brand TEE_TASessionHandle *ta_sess, 479b0104773SPascal Brand uint32_t *ret_orig) 480b0104773SPascal Brand { 481b0104773SPascal Brand TEE_Result res; 482b0104773SPascal Brand uint32_t ret_o = TEE_ORIGIN_TEE; 483b0104773SPascal Brand struct tee_ta_session *s = NULL; 484b0104773SPascal Brand struct tee_ta_session *sess; 485b0104773SPascal Brand tee_mm_entry_t *mm_param = NULL; 486b0104773SPascal Brand 487b0104773SPascal Brand TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 488b0104773SPascal Brand struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 489b0104773SPascal Brand TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 490b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 491b0104773SPascal Brand 492b0104773SPascal Brand if (uuid == NULL || param == NULL || clnt_id == NULL) { 493b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 494b0104773SPascal Brand goto out_free_only; 495b0104773SPascal Brand } 496b0104773SPascal Brand 497b0104773SPascal Brand memset(param, 0, sizeof(struct tee_ta_param)); 498b0104773SPascal Brand 499b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 500b0104773SPascal Brand if (res != TEE_SUCCESS) 501b0104773SPascal Brand goto out_free_only; 502b0104773SPascal Brand 503b0104773SPascal Brand res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID)); 504b0104773SPascal Brand if (res != TEE_SUCCESS) 505b0104773SPascal Brand goto function_exit; 506b0104773SPascal Brand 507b0104773SPascal Brand clnt_id->login = TEE_LOGIN_TRUSTED_APP; 508b0104773SPascal Brand memcpy(&clnt_id->uuid, &sess->ctx->head->uuid, sizeof(TEE_UUID)); 509b0104773SPascal Brand 510b0104773SPascal Brand res = tee_svc_copy_param(sess, NULL, param_types, params, param, 511b0104773SPascal Brand tmp_buf_pa, &mm_param); 512b0104773SPascal Brand if (res != TEE_SUCCESS) 513b0104773SPascal Brand goto function_exit; 514b0104773SPascal Brand 515b0104773SPascal Brand /* 516b0104773SPascal Brand * Find session of a multi session TA or a static TA 517b0104773SPascal Brand * In such a case, there is no need to ask the supplicant for the TA 518b0104773SPascal Brand * code 519b0104773SPascal Brand */ 520b0104773SPascal Brand res = tee_ta_open_session(&ret_o, &s, &sess->ctx->open_sessions, uuid, 521b0104773SPascal Brand NULL, clnt_id, cancel_req_to, param); 522b0104773SPascal Brand 523b0104773SPascal Brand if (ret_o != TEE_ORIGIN_TEE || res != TEE_ERROR_ITEM_NOT_FOUND) 524b0104773SPascal Brand goto function_exit; 525b0104773SPascal Brand 526b0104773SPascal Brand if (ret_o == TEE_ORIGIN_TEE && res == TEE_ERROR_ITEM_NOT_FOUND) { 527b0104773SPascal Brand kta_signed_header_t *ta = NULL; 528b0104773SPascal Brand struct tee_ta_nwumap lp; 529b0104773SPascal Brand 530b0104773SPascal Brand /* Load TA */ 531b0104773SPascal Brand res = tee_ta_rpc_load(uuid, &ta, &lp, &ret_o); 532*c0346845SJens Wiklander if (res != TEE_SUCCESS) 533b0104773SPascal Brand goto function_exit; 534b0104773SPascal Brand 535b0104773SPascal Brand res = tee_ta_open_session(&ret_o, &s, &sess->ctx->open_sessions, 536b0104773SPascal Brand uuid, ta, clnt_id, cancel_req_to, 537b0104773SPascal Brand param); 538b0104773SPascal Brand if (res != TEE_SUCCESS) 539b0104773SPascal Brand goto function_exit; 540b0104773SPascal Brand 541b0104773SPascal Brand s->ctx->nwumap = lp; 542b0104773SPascal Brand } 543b0104773SPascal Brand 544b0104773SPascal Brand res = tee_svc_update_out_param(sess, NULL, param, tmp_buf_pa, params); 545b0104773SPascal Brand if (res != TEE_SUCCESS) 546b0104773SPascal Brand goto function_exit; 547b0104773SPascal Brand 548b0104773SPascal Brand function_exit: 549b0104773SPascal Brand tee_ta_set_current_session(sess); 550b0104773SPascal Brand 551b0104773SPascal Brand if (mm_param != NULL) { 552b0104773SPascal Brand TEE_Result res2; 553b0104773SPascal Brand void *va = 0; 554b0104773SPascal Brand 555b0104773SPascal Brand res2 = 556b0104773SPascal Brand tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 557b0104773SPascal Brand if (res2 == TEE_SUCCESS) 558b0104773SPascal Brand tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 559b0104773SPascal Brand } 560b0104773SPascal Brand tee_mm_free(mm_param); 561b0104773SPascal Brand tee_svc_copy_to_user(sess, ta_sess, &s, sizeof(s)); 562b0104773SPascal Brand tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 563b0104773SPascal Brand 564b0104773SPascal Brand out_free_only: 565b0104773SPascal Brand free(param); 566b0104773SPascal Brand free(uuid); 567b0104773SPascal Brand free(clnt_id); 568b0104773SPascal Brand return res; 569b0104773SPascal Brand } 570b0104773SPascal Brand 571b0104773SPascal Brand TEE_Result tee_svc_close_ta_session(TEE_TASessionHandle ta_sess) 572b0104773SPascal Brand { 573b0104773SPascal Brand TEE_Result res; 574b0104773SPascal Brand struct tee_ta_session *sess; 575b0104773SPascal Brand 576b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 577b0104773SPascal Brand if (res != TEE_SUCCESS) 578b0104773SPascal Brand return res; 579b0104773SPascal Brand 580b0104773SPascal Brand tee_ta_set_current_session(NULL); 581b0104773SPascal Brand 582b0104773SPascal Brand res = 583b0104773SPascal Brand tee_ta_close_session((uint32_t)ta_sess, &sess->ctx->open_sessions); 584b0104773SPascal Brand tee_ta_set_current_session(sess); 585b0104773SPascal Brand return res; 586b0104773SPascal Brand } 587b0104773SPascal Brand 588b0104773SPascal Brand TEE_Result tee_svc_invoke_ta_command(TEE_TASessionHandle ta_sess, 589b0104773SPascal Brand uint32_t cancel_req_to, uint32_t cmd_id, 590b0104773SPascal Brand uint32_t param_types, TEE_Param params[4], 591b0104773SPascal Brand uint32_t *ret_orig) 592b0104773SPascal Brand { 593b0104773SPascal Brand TEE_Result res; 594b0104773SPascal Brand uint32_t ret_o = TEE_ORIGIN_TEE; 595b0104773SPascal Brand struct tee_ta_param param = { 0 }; 596b0104773SPascal Brand TEE_Identity clnt_id; 597b0104773SPascal Brand struct tee_ta_session *sess; 598b0104773SPascal Brand struct tee_ta_session *called_sess = (struct tee_ta_session *)ta_sess; 599b0104773SPascal Brand tee_mm_entry_t *mm_param = NULL; 600b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 601b0104773SPascal Brand 602b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 603b0104773SPascal Brand if (res != TEE_SUCCESS) 604b0104773SPascal Brand return res; 605b0104773SPascal Brand 606b0104773SPascal Brand res = 607b0104773SPascal Brand tee_ta_verify_session_pointer(called_sess, 608b0104773SPascal Brand &sess->ctx->open_sessions); 609b0104773SPascal Brand if (res != TEE_SUCCESS) 610b0104773SPascal Brand return res; 611b0104773SPascal Brand 612b0104773SPascal Brand res = tee_svc_copy_param(sess, called_sess, param_types, params, 613b0104773SPascal Brand ¶m, tmp_buf_pa, &mm_param); 614b0104773SPascal Brand if (res != TEE_SUCCESS) 615b0104773SPascal Brand goto function_exit; 616b0104773SPascal Brand 617b0104773SPascal Brand res = 618b0104773SPascal Brand tee_ta_invoke_command(&ret_o, called_sess, &clnt_id, cancel_req_to, 619b0104773SPascal Brand cmd_id, ¶m); 620b0104773SPascal Brand if (res != TEE_SUCCESS) 621b0104773SPascal Brand goto function_exit; 622b0104773SPascal Brand 623b0104773SPascal Brand res = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_pa, 624b0104773SPascal Brand params); 625b0104773SPascal Brand if (res != TEE_SUCCESS) 626b0104773SPascal Brand goto function_exit; 627b0104773SPascal Brand 628b0104773SPascal Brand function_exit: 629b0104773SPascal Brand tee_ta_set_current_session(sess); 630b0104773SPascal Brand called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 631b0104773SPascal Brand 632b0104773SPascal Brand if (mm_param != NULL) { 633b0104773SPascal Brand TEE_Result res2; 634b0104773SPascal Brand void *va = 0; 635b0104773SPascal Brand 636b0104773SPascal Brand res2 = 637b0104773SPascal Brand tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 638b0104773SPascal Brand if (res2 == TEE_SUCCESS) 639b0104773SPascal Brand tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 640b0104773SPascal Brand } 641b0104773SPascal Brand tee_mm_free(mm_param); 642b0104773SPascal Brand if (ret_orig) 643b0104773SPascal Brand tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 644b0104773SPascal Brand return res; 645b0104773SPascal Brand } 646b0104773SPascal Brand 647b0104773SPascal Brand TEE_Result tee_svc_check_access_rights(uint32_t flags, const void *buf, 648b0104773SPascal Brand size_t len) 649b0104773SPascal Brand { 650b0104773SPascal Brand TEE_Result res; 651b0104773SPascal Brand struct tee_ta_session *s; 652b0104773SPascal Brand 653b0104773SPascal Brand res = tee_ta_get_current_session(&s); 654b0104773SPascal Brand if (res != TEE_SUCCESS) 655b0104773SPascal Brand return res; 656b0104773SPascal Brand 657b0104773SPascal Brand return tee_mmu_check_access_rights(s->ctx, flags, (tee_uaddr_t)buf, 658b0104773SPascal Brand len); 659b0104773SPascal Brand } 660b0104773SPascal Brand 661b0104773SPascal Brand TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr, 662b0104773SPascal Brand const void *uaddr, size_t len) 663b0104773SPascal Brand { 664b0104773SPascal Brand TEE_Result res; 665b0104773SPascal Brand struct tee_ta_session *s; 666b0104773SPascal Brand 667b0104773SPascal Brand if (sess == NULL) { 668b0104773SPascal Brand res = tee_ta_get_current_session(&s); 669b0104773SPascal Brand if (res != TEE_SUCCESS) 670b0104773SPascal Brand return res; 671b0104773SPascal Brand } else { 672b0104773SPascal Brand s = sess; 673b0104773SPascal Brand tee_ta_set_current_session(s); 674b0104773SPascal Brand } 675b0104773SPascal Brand res = 676b0104773SPascal Brand tee_mmu_check_access_rights(s->ctx, 677b0104773SPascal Brand TEE_MEMORY_ACCESS_READ | 678b0104773SPascal Brand TEE_MEMORY_ACCESS_ANY_OWNER, 679b0104773SPascal Brand (tee_uaddr_t)uaddr, len); 680b0104773SPascal Brand if (res != TEE_SUCCESS) 681b0104773SPascal Brand return res; 682b0104773SPascal Brand 683b0104773SPascal Brand memcpy(kaddr, uaddr, len); 684b0104773SPascal Brand return TEE_SUCCESS; 685b0104773SPascal Brand } 686b0104773SPascal Brand 687b0104773SPascal Brand TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr, 688b0104773SPascal Brand const void *kaddr, size_t len) 689b0104773SPascal Brand { 690b0104773SPascal Brand TEE_Result res; 691b0104773SPascal Brand struct tee_ta_session *s; 692b0104773SPascal Brand 693b0104773SPascal Brand if (sess == NULL) { 694b0104773SPascal Brand res = tee_ta_get_current_session(&s); 695b0104773SPascal Brand if (res != TEE_SUCCESS) 696b0104773SPascal Brand return res; 697b0104773SPascal Brand } else { 698b0104773SPascal Brand s = sess; 699b0104773SPascal Brand tee_ta_set_current_session(s); 700b0104773SPascal Brand } 701b0104773SPascal Brand 702b0104773SPascal Brand res = 703b0104773SPascal Brand tee_mmu_check_access_rights(s->ctx, 704b0104773SPascal Brand TEE_MEMORY_ACCESS_WRITE | 705b0104773SPascal Brand TEE_MEMORY_ACCESS_ANY_OWNER, 706b0104773SPascal Brand (tee_uaddr_t)uaddr, len); 707b0104773SPascal Brand if (res != TEE_SUCCESS) 708b0104773SPascal Brand return res; 709b0104773SPascal Brand 710b0104773SPascal Brand memcpy(uaddr, kaddr, len); 711b0104773SPascal Brand return TEE_SUCCESS; 712b0104773SPascal Brand } 713b0104773SPascal Brand 714b0104773SPascal Brand static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time) 715b0104773SPascal Brand { 716b0104773SPascal Brand TEE_Time current_time; 717b0104773SPascal Brand 718b0104773SPascal Brand if (s->cancel_mask) 719b0104773SPascal Brand return false; 720b0104773SPascal Brand 721b0104773SPascal Brand if (s->cancel) 722b0104773SPascal Brand return true; 723b0104773SPascal Brand 724b0104773SPascal Brand if (s->cancel_time.seconds == UINT32_MAX) 725b0104773SPascal Brand return false; 726b0104773SPascal Brand 727b0104773SPascal Brand if (curr_time != NULL) 728b0104773SPascal Brand current_time = *curr_time; 729b0104773SPascal Brand else if (tee_time_get_sys_time(¤t_time) != TEE_SUCCESS) 730b0104773SPascal Brand return false; 731b0104773SPascal Brand 732b0104773SPascal Brand if (current_time.seconds > s->cancel_time.seconds || 733b0104773SPascal Brand (current_time.seconds == s->cancel_time.seconds && 734b0104773SPascal Brand current_time.millis >= s->cancel_time.millis)) { 735b0104773SPascal Brand return true; 736b0104773SPascal Brand } 737b0104773SPascal Brand 738b0104773SPascal Brand return false; 739b0104773SPascal Brand } 740b0104773SPascal Brand 741b0104773SPascal Brand TEE_Result tee_svc_get_cancellation_flag(bool *cancel) 742b0104773SPascal Brand { 743b0104773SPascal Brand TEE_Result res; 744b0104773SPascal Brand struct tee_ta_session *s = NULL; 745b0104773SPascal Brand bool c; 746b0104773SPascal Brand 747b0104773SPascal Brand res = tee_ta_get_current_session(&s); 748b0104773SPascal Brand if (res != TEE_SUCCESS) 749b0104773SPascal Brand return res; 750b0104773SPascal Brand 751b0104773SPascal Brand c = session_is_cancelled(s, NULL); 752b0104773SPascal Brand 753b0104773SPascal Brand return tee_svc_copy_to_user(s, cancel, &c, sizeof(c)); 754b0104773SPascal Brand } 755b0104773SPascal Brand 756b0104773SPascal Brand TEE_Result tee_svc_unmask_cancellation(bool *old_mask) 757b0104773SPascal Brand { 758b0104773SPascal Brand TEE_Result res; 759b0104773SPascal Brand struct tee_ta_session *s = NULL; 760b0104773SPascal Brand bool m; 761b0104773SPascal Brand 762b0104773SPascal Brand res = tee_ta_get_current_session(&s); 763b0104773SPascal Brand if (res != TEE_SUCCESS) 764b0104773SPascal Brand return res; 765b0104773SPascal Brand 766b0104773SPascal Brand m = s->cancel_mask; 767b0104773SPascal Brand s->cancel_mask = false; 768b0104773SPascal Brand return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 769b0104773SPascal Brand } 770b0104773SPascal Brand 771b0104773SPascal Brand TEE_Result tee_svc_mask_cancellation(bool *old_mask) 772b0104773SPascal Brand { 773b0104773SPascal Brand TEE_Result res; 774b0104773SPascal Brand struct tee_ta_session *s = NULL; 775b0104773SPascal Brand bool m; 776b0104773SPascal Brand 777b0104773SPascal Brand res = tee_ta_get_current_session(&s); 778b0104773SPascal Brand if (res != TEE_SUCCESS) 779b0104773SPascal Brand return res; 780b0104773SPascal Brand 781b0104773SPascal Brand m = s->cancel_mask; 782b0104773SPascal Brand s->cancel_mask = true; 783b0104773SPascal Brand return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 784b0104773SPascal Brand } 785b0104773SPascal Brand 786b0104773SPascal Brand TEE_Result tee_svc_wait(uint32_t timeout) 787b0104773SPascal Brand { 788b0104773SPascal Brand TEE_Result res = TEE_SUCCESS; 789b0104773SPascal Brand uint32_t mytime = 0; 790b0104773SPascal Brand struct tee_ta_session *s; 791b0104773SPascal Brand TEE_Time base_time; 792b0104773SPascal Brand TEE_Time current_time; 793b0104773SPascal Brand 794b0104773SPascal Brand res = tee_ta_get_current_session(&s); 795b0104773SPascal Brand if (res != TEE_SUCCESS) 796b0104773SPascal Brand return res; 797b0104773SPascal Brand 798b0104773SPascal Brand res = tee_time_get_sys_time(&base_time); 799b0104773SPascal Brand if (res != TEE_SUCCESS) 800b0104773SPascal Brand return res; 801b0104773SPascal Brand 802b0104773SPascal Brand while (true) { 803b0104773SPascal Brand res = tee_time_get_sys_time(¤t_time); 804b0104773SPascal Brand if (res != TEE_SUCCESS) 805b0104773SPascal Brand return res; 806b0104773SPascal Brand 807b0104773SPascal Brand if (session_is_cancelled(s, ¤t_time)) 808b0104773SPascal Brand return TEE_ERROR_CANCEL; 809b0104773SPascal Brand 810b0104773SPascal Brand mytime = (current_time.seconds - base_time.seconds) * 1000 + 811b0104773SPascal Brand (int)current_time.millis - (int)base_time.millis; 812b0104773SPascal Brand if (mytime >= timeout) 813b0104773SPascal Brand return TEE_SUCCESS; 814b0104773SPascal Brand 815b0104773SPascal Brand tee_wait_specific(timeout - mytime); 816b0104773SPascal Brand } 817b0104773SPascal Brand 818b0104773SPascal Brand return res; 819b0104773SPascal Brand } 820b0104773SPascal Brand 821b0104773SPascal Brand TEE_Result tee_svc_get_time(enum utee_time_category cat, TEE_Time *mytime) 822b0104773SPascal Brand { 823b0104773SPascal Brand TEE_Result res, res2; 824b0104773SPascal Brand struct tee_ta_session *s = NULL; 825b0104773SPascal Brand TEE_Time t; 826b0104773SPascal Brand 827b0104773SPascal Brand res = tee_ta_get_current_session(&s); 828b0104773SPascal Brand if (res != TEE_SUCCESS) 829b0104773SPascal Brand return res; 830b0104773SPascal Brand 831b0104773SPascal Brand switch (cat) { 832b0104773SPascal Brand case UTEE_TIME_CAT_SYSTEM: 833b0104773SPascal Brand res = tee_time_get_sys_time(&t); 834b0104773SPascal Brand break; 835b0104773SPascal Brand case UTEE_TIME_CAT_TA_PERSISTENT: 836b0104773SPascal Brand res = 837b0104773SPascal Brand tee_time_get_ta_time((const void *)&s->ctx->head->uuid, &t); 838b0104773SPascal Brand break; 839b0104773SPascal Brand case UTEE_TIME_CAT_REE: 840b0104773SPascal Brand res = tee_time_get_ree_time(&t); 841b0104773SPascal Brand break; 842b0104773SPascal Brand default: 843b0104773SPascal Brand res = TEE_ERROR_BAD_PARAMETERS; 844b0104773SPascal Brand break; 845b0104773SPascal Brand } 846b0104773SPascal Brand 847b0104773SPascal Brand if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 848b0104773SPascal Brand res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t)); 849b0104773SPascal Brand if (res2 != TEE_SUCCESS) 850b0104773SPascal Brand res = res2; 851b0104773SPascal Brand } 852b0104773SPascal Brand 853b0104773SPascal Brand return res; 854b0104773SPascal Brand } 855b0104773SPascal Brand 856b0104773SPascal Brand TEE_Result tee_svc_set_ta_time(const TEE_Time *mytime) 857b0104773SPascal Brand { 858b0104773SPascal Brand TEE_Result res; 859b0104773SPascal Brand struct tee_ta_session *s = NULL; 860b0104773SPascal Brand TEE_Time t; 861b0104773SPascal Brand 862b0104773SPascal Brand res = tee_ta_get_current_session(&s); 863b0104773SPascal Brand if (res != TEE_SUCCESS) 864b0104773SPascal Brand return res; 865b0104773SPascal Brand 866b0104773SPascal Brand res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t)); 867b0104773SPascal Brand if (res != TEE_SUCCESS) 868b0104773SPascal Brand return res; 869b0104773SPascal Brand 870b0104773SPascal Brand return tee_time_set_ta_time((const void *)&s->ctx->head->uuid, &t); 871b0104773SPascal Brand } 872