1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2019, Linaro Limited 4 * Copyright (c) 2020, Arm Limited. 5 */ 6 7 #include <crypto/crypto.h> 8 #include <ffa.h> 9 #include <kernel/abort.h> 10 #include <kernel/stmm_sp.h> 11 #include <kernel/user_mode_ctx.h> 12 #include <mm/fobj.h> 13 #include <mm/mobj.h> 14 #include <mm/vm.h> 15 #include <pta_stmm.h> 16 #include <tee_api_defines_extensions.h> 17 #include <tee/tee_pobj.h> 18 #include <tee/tee_svc.h> 19 #include <tee/tee_svc_storage.h> 20 #include <zlib.h> 21 22 #include "thread_private.h" 23 24 #ifdef ARM64 25 #define SVC_REGS_A0(_regs) ((_regs)->x0) 26 #define SVC_REGS_A1(_regs) ((_regs)->x1) 27 #define SVC_REGS_A2(_regs) ((_regs)->x2) 28 #define SVC_REGS_A3(_regs) ((_regs)->x3) 29 #define SVC_REGS_A4(_regs) ((_regs)->x4) 30 #define SVC_REGS_A5(_regs) ((_regs)->x5) 31 #define SVC_REGS_A6(_regs) ((_regs)->x6) 32 #define SVC_REGS_A7(_regs) ((_regs)->x7) 33 #define __FFA_SVC_RPMB_READ FFA_SVC_RPMB_READ 34 #define __FFA_SVC_RPMB_WRITE FFA_SVC_RPMB_WRITE 35 #define __FFA_SVC_MEMORY_ATTRIBUTES_GET FFA_SVC_MEMORY_ATTRIBUTES_GET_64 36 #define __FFA_SVC_MEMORY_ATTRIBUTES_SET FFA_SVC_MEMORY_ATTRIBUTES_SET_64 37 #define __FFA_MSG_SEND_DIRECT_RESP FFA_MSG_SEND_DIRECT_RESP_64 38 #define __FFA_MSG_SEND_DIRECT_REQ FFA_MSG_SEND_DIRECT_REQ_64 39 #endif 40 #ifdef ARM32 41 #define SVC_REGS_A0(_regs) ((_regs)->r0) 42 #define SVC_REGS_A1(_regs) ((_regs)->r1) 43 #define SVC_REGS_A2(_regs) ((_regs)->r2) 44 #define SVC_REGS_A3(_regs) ((_regs)->r3) 45 #define SVC_REGS_A4(_regs) ((_regs)->r4) 46 #define SVC_REGS_A5(_regs) ((_regs)->r5) 47 #define SVC_REGS_A6(_regs) ((_regs)->r6) 48 #define SVC_REGS_A7(_regs) ((_regs)->r7) 49 #define __FFA_SVC_RPMB_READ FFA_SVC_RPMB_READ_32 50 #define __FFA_SVC_RPMB_WRITE FFA_SVC_RPMB_WRITE_32 51 #define __FFA_SVC_MEMORY_ATTRIBUTES_GET FFA_SVC_MEMORY_ATTRIBUTES_GET_32 52 #define __FFA_SVC_MEMORY_ATTRIBUTES_SET FFA_SVC_MEMORY_ATTRIBUTES_SET_32 53 #define __FFA_MSG_SEND_DIRECT_RESP FFA_MSG_SEND_DIRECT_RESP_32 54 #define __FFA_MSG_SEND_DIRECT_REQ FFA_MSG_SEND_DIRECT_REQ_32 55 #endif 56 57 static const TEE_UUID stmm_uuid = PTA_STMM_UUID; 58 59 /* 60 * Once a complete FFA spec is added, these will become discoverable. 61 * Until then these are considered part of the internal ABI between 62 * OP-TEE and StMM. 63 */ 64 static const uint16_t stmm_id = 1U; 65 static const uint16_t stmm_pta_id = 2U; 66 static const uint16_t mem_mgr_id = 3U; 67 static const uint16_t ffa_storage_id = 4U; 68 69 static const unsigned int stmm_stack_size = 4 * SMALL_PAGE_SIZE; 70 static const unsigned int stmm_heap_size = 398 * SMALL_PAGE_SIZE; 71 static const unsigned int stmm_sec_buf_size = SMALL_PAGE_SIZE; 72 static const unsigned int stmm_ns_comm_buf_size = SMALL_PAGE_SIZE; 73 74 extern unsigned char stmm_image[]; 75 extern const unsigned int stmm_image_size; 76 extern const unsigned int stmm_image_uncompressed_size; 77 78 static struct stmm_ctx *stmm_alloc_ctx(const TEE_UUID *uuid) 79 { 80 TEE_Result res = TEE_SUCCESS; 81 struct stmm_ctx *spc = NULL; 82 83 spc = calloc(1, sizeof(*spc)); 84 if (!spc) 85 return NULL; 86 87 spc->ta_ctx.ts_ctx.ops = &stmm_sp_ops; 88 spc->ta_ctx.ts_ctx.uuid = *uuid; 89 spc->ta_ctx.flags = TA_FLAG_SINGLE_INSTANCE | 90 TA_FLAG_INSTANCE_KEEP_ALIVE; 91 spc->uctx.ts_ctx = &spc->ta_ctx.ts_ctx; 92 93 res = vm_info_init(&spc->uctx); 94 if (res) { 95 free(spc); 96 return NULL; 97 } 98 99 spc->ta_ctx.ref_count = 1; 100 condvar_init(&spc->ta_ctx.busy_cv); 101 102 return spc; 103 } 104 105 static TEE_Result stmm_enter_user_mode(struct stmm_ctx *spc) 106 { 107 uint32_t exceptions = 0; 108 uint32_t panic_code = 0; 109 uint32_t panicked = 0; 110 uint64_t cntkctl = 0; 111 112 exceptions = thread_mask_exceptions(THREAD_EXCP_ALL); 113 cntkctl = read_cntkctl(); 114 write_cntkctl(cntkctl | CNTKCTL_PL0PCTEN); 115 __thread_enter_user_mode(&spc->regs, &panicked, &panic_code); 116 write_cntkctl(cntkctl); 117 thread_unmask_exceptions(exceptions); 118 119 thread_user_clear_vfp(&spc->uctx); 120 121 if (panicked) { 122 abort_print_current_ta(); 123 DMSG("stmm panicked with code %#"PRIx32, panic_code); 124 return TEE_ERROR_TARGET_DEAD; 125 } 126 127 return TEE_SUCCESS; 128 } 129 130 #ifdef ARM64 131 static void init_stmm_regs(struct stmm_ctx *spc, unsigned long a0, 132 unsigned long a1, unsigned long sp, unsigned long pc) 133 { 134 spc->regs.x[0] = a0; 135 spc->regs.x[1] = a1; 136 spc->regs.sp = sp; 137 spc->regs.pc = pc; 138 } 139 #endif 140 141 #ifdef ARM32 142 static uint32_t __maybe_unused get_spsr(void) 143 { 144 uint32_t s = 0; 145 146 s = read_cpsr(); 147 s &= ~(CPSR_MODE_MASK | CPSR_T | ARM32_CPSR_IT_MASK); 148 s |= CPSR_MODE_USR; 149 150 return s; 151 } 152 153 static void init_stmm_regs(struct stmm_ctx *spc, unsigned long a0, 154 unsigned long a1, unsigned long sp, unsigned long pc) 155 { 156 spc->regs.r0 = a0; 157 spc->regs.r1 = a1; 158 spc->regs.usr_sp = sp; 159 spc->regs.cpsr = get_spsr(); 160 spc->regs.pc = pc; 161 } 162 #endif 163 164 static TEE_Result alloc_and_map_sp_fobj(struct stmm_ctx *spc, size_t sz, 165 uint32_t prot, vaddr_t *va) 166 { 167 size_t num_pgs = ROUNDUP(sz, SMALL_PAGE_SIZE) / SMALL_PAGE_SIZE; 168 struct fobj *fobj = fobj_ta_mem_alloc(num_pgs); 169 struct mobj *mobj = mobj_with_fobj_alloc(fobj, NULL); 170 TEE_Result res = TEE_SUCCESS; 171 172 fobj_put(fobj); 173 if (!mobj) 174 return TEE_ERROR_OUT_OF_MEMORY; 175 176 res = vm_map(&spc->uctx, va, num_pgs * SMALL_PAGE_SIZE, 177 prot, 0, mobj, 0); 178 if (res) 179 mobj_put(mobj); 180 181 return TEE_SUCCESS; 182 } 183 184 static void *zalloc(void *opaque __unused, unsigned int items, 185 unsigned int size) 186 { 187 return malloc(items * size); 188 } 189 190 static void zfree(void *opaque __unused, void *address) 191 { 192 free(address); 193 } 194 195 static void uncompress_image(void *dst, size_t dst_size, void *src, 196 size_t src_size) 197 { 198 z_stream strm = { 199 .next_in = src, 200 .avail_in = src_size, 201 .next_out = dst, 202 .avail_out = dst_size, 203 .zalloc = zalloc, 204 .zfree = zfree, 205 }; 206 207 if (inflateInit(&strm) != Z_OK) 208 panic("inflateInit"); 209 210 if (inflate(&strm, Z_SYNC_FLUSH) != Z_STREAM_END) 211 panic("inflate"); 212 213 if (inflateEnd(&strm) != Z_OK) 214 panic("inflateEnd"); 215 } 216 217 static TEE_Result load_stmm(struct stmm_ctx *spc) 218 { 219 struct stmm_boot_info *boot_info = NULL; 220 struct stmm_mp_info *mp_info = NULL; 221 TEE_Result res = TEE_SUCCESS; 222 vaddr_t sp_addr = 0; 223 vaddr_t image_addr = 0; 224 vaddr_t heap_addr = 0; 225 vaddr_t stack_addr = 0; 226 vaddr_t sec_buf_addr = 0; 227 vaddr_t comm_buf_addr = 0; 228 unsigned int sp_size = 0; 229 unsigned int uncompressed_size_roundup = 0; 230 231 uncompressed_size_roundup = ROUNDUP(stmm_image_uncompressed_size, 232 SMALL_PAGE_SIZE); 233 sp_size = uncompressed_size_roundup + stmm_stack_size + 234 stmm_heap_size + stmm_sec_buf_size; 235 res = alloc_and_map_sp_fobj(spc, sp_size, 236 TEE_MATTR_PRW, &sp_addr); 237 if (res) 238 return res; 239 240 res = alloc_and_map_sp_fobj(spc, stmm_ns_comm_buf_size, 241 TEE_MATTR_URW | TEE_MATTR_PRW, 242 &comm_buf_addr); 243 /* 244 * We don't need to free the previous instance here, they'll all be 245 * handled during the destruction call (stmm_ctx_destroy()) 246 */ 247 if (res) 248 return res; 249 250 image_addr = sp_addr; 251 heap_addr = image_addr + uncompressed_size_roundup; 252 stack_addr = heap_addr + stmm_heap_size; 253 sec_buf_addr = stack_addr + stmm_stack_size; 254 255 vm_set_ctx(&spc->ta_ctx.ts_ctx); 256 uncompress_image((void *)image_addr, stmm_image_uncompressed_size, 257 stmm_image, stmm_image_size); 258 259 res = vm_set_prot(&spc->uctx, image_addr, uncompressed_size_roundup, 260 TEE_MATTR_URX | TEE_MATTR_PR); 261 if (res) 262 return res; 263 264 res = vm_set_prot(&spc->uctx, heap_addr, stmm_heap_size, 265 TEE_MATTR_URW | TEE_MATTR_PRW); 266 if (res) 267 return res; 268 269 res = vm_set_prot(&spc->uctx, stack_addr, stmm_stack_size, 270 TEE_MATTR_URW | TEE_MATTR_PRW); 271 if (res) 272 return res; 273 274 res = vm_set_prot(&spc->uctx, sec_buf_addr, stmm_sec_buf_size, 275 TEE_MATTR_URW | TEE_MATTR_PRW); 276 if (res) 277 return res; 278 279 DMSG("stmm load address %#"PRIxVA, image_addr); 280 281 boot_info = (struct stmm_boot_info *)sec_buf_addr; 282 mp_info = (struct stmm_mp_info *)(boot_info + 1); 283 *boot_info = (struct stmm_boot_info){ 284 .h.type = STMM_PARAM_SP_IMAGE_BOOT_INFO, 285 .h.version = STMM_PARAM_VERSION_1, 286 .h.size = sizeof(struct stmm_boot_info), 287 .h.attr = 0, 288 .sp_mem_base = sp_addr, 289 .sp_mem_limit = sp_addr + sp_size, 290 .sp_image_base = image_addr, 291 .sp_stack_base = stack_addr, 292 .sp_heap_base = heap_addr, 293 .sp_ns_comm_buf_base = comm_buf_addr, 294 .sp_shared_buf_base = sec_buf_addr, 295 .sp_image_size = stmm_image_size, 296 .sp_pcpu_stack_size = stmm_stack_size, 297 .sp_heap_size = stmm_heap_size, 298 .sp_ns_comm_buf_size = stmm_ns_comm_buf_size, 299 .sp_shared_buf_size = stmm_sec_buf_size, 300 .num_sp_mem_regions = 6, 301 .num_cpus = 1, 302 .mp_info = mp_info, 303 }; 304 mp_info->mpidr = read_mpidr(); 305 mp_info->linear_id = 0; 306 mp_info->flags = MP_INFO_FLAG_PRIMARY_CPU; 307 spc->ns_comm_buf_addr = comm_buf_addr; 308 spc->ns_comm_buf_size = stmm_ns_comm_buf_size; 309 310 init_stmm_regs(spc, sec_buf_addr, 311 (vaddr_t)(mp_info + 1) - sec_buf_addr, 312 stack_addr + stmm_stack_size, image_addr); 313 314 return stmm_enter_user_mode(spc); 315 } 316 317 TEE_Result stmm_init_session(const TEE_UUID *uuid, struct tee_ta_session *sess) 318 { 319 struct stmm_ctx *spc = NULL; 320 TEE_Result res = TEE_SUCCESS; 321 322 if (memcmp(uuid, &stmm_uuid, sizeof(*uuid))) 323 return TEE_ERROR_ITEM_NOT_FOUND; 324 325 spc = stmm_alloc_ctx(uuid); 326 if (!spc) 327 return TEE_ERROR_OUT_OF_MEMORY; 328 329 spc->is_initializing = true; 330 331 mutex_lock(&tee_ta_mutex); 332 sess->ts_sess.ctx = &spc->ta_ctx.ts_ctx; 333 sess->ts_sess.handle_svc = sess->ts_sess.ctx->ops->handle_svc; 334 mutex_unlock(&tee_ta_mutex); 335 336 ts_push_current_session(&sess->ts_sess); 337 res = load_stmm(spc); 338 ts_pop_current_session(); 339 vm_set_ctx(NULL); 340 if (res) { 341 sess->ts_sess.ctx = NULL; 342 spc->ta_ctx.ts_ctx.ops->destroy(&spc->ta_ctx.ts_ctx); 343 344 return res; 345 } 346 347 mutex_lock(&tee_ta_mutex); 348 spc->is_initializing = false; 349 TAILQ_INSERT_TAIL(&tee_ctxes, &spc->ta_ctx, link); 350 mutex_unlock(&tee_ta_mutex); 351 352 return TEE_SUCCESS; 353 } 354 355 static TEE_Result stmm_enter_open_session(struct ts_session *s) 356 { 357 struct stmm_ctx *spc = to_stmm_ctx(s->ctx); 358 struct tee_ta_session *ta_sess = to_ta_session(s); 359 const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, 360 TEE_PARAM_TYPE_NONE, 361 TEE_PARAM_TYPE_NONE, 362 TEE_PARAM_TYPE_NONE); 363 364 if (ta_sess->param->types != exp_pt) 365 return TEE_ERROR_BAD_PARAMETERS; 366 367 if (spc->is_initializing) { 368 /* StMM is initialized in stmm_init_session() */ 369 ta_sess->err_origin = TEE_ORIGIN_TEE; 370 return TEE_ERROR_BAD_STATE; 371 } 372 373 return TEE_SUCCESS; 374 } 375 376 static TEE_Result stmm_enter_invoke_cmd(struct ts_session *s, uint32_t cmd) 377 { 378 struct stmm_ctx *spc = to_stmm_ctx(s->ctx); 379 struct tee_ta_session *ta_sess = to_ta_session(s); 380 TEE_Result res = TEE_SUCCESS; 381 TEE_Result __maybe_unused tmp_res = TEE_SUCCESS; 382 unsigned int ns_buf_size = 0; 383 struct param_mem *mem = NULL; 384 void *va = NULL; 385 const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT, 386 TEE_PARAM_TYPE_VALUE_OUTPUT, 387 TEE_PARAM_TYPE_NONE, 388 TEE_PARAM_TYPE_NONE); 389 390 if (cmd != PTA_STMM_CMD_COMMUNICATE) 391 return TEE_ERROR_BAD_PARAMETERS; 392 393 if (ta_sess->param->types != exp_pt) 394 return TEE_ERROR_BAD_PARAMETERS; 395 396 mem = &ta_sess->param->u[0].mem; 397 ns_buf_size = mem->size; 398 if (ns_buf_size > spc->ns_comm_buf_size) { 399 mem->size = spc->ns_comm_buf_size; 400 return TEE_ERROR_EXCESS_DATA; 401 } 402 403 res = mobj_inc_map(mem->mobj); 404 if (res) 405 return res; 406 407 va = mobj_get_va(mem->mobj, mem->offs); 408 if (!va) { 409 EMSG("Can't get a valid VA for NS buffer"); 410 res = TEE_ERROR_BAD_PARAMETERS; 411 goto out_va; 412 } 413 414 #ifdef ARM64 415 spc->regs.x[0] = __FFA_MSG_SEND_DIRECT_REQ; 416 spc->regs.x[1] = (stmm_pta_id << 16) | stmm_id; 417 spc->regs.x[2] = FFA_PARAM_MBZ; 418 spc->regs.x[3] = spc->ns_comm_buf_addr; 419 spc->regs.x[4] = ns_buf_size; 420 spc->regs.x[5] = 0; 421 spc->regs.x[6] = 0; 422 spc->regs.x[7] = 0; 423 #endif 424 #ifdef ARM32 425 spc->regs.r0 = __FFA_MSG_SEND_DIRECT_REQ; 426 spc->regs.r1 = (stmm_pta_id << 16) | stmm_id; 427 spc->regs.r2 = FFA_PARAM_MBZ; 428 spc->regs.r3 = spc->ns_comm_buf_addr; 429 spc->regs.r4 = ns_buf_size; 430 spc->regs.r5 = 0; 431 spc->regs.r6 = 0; 432 spc->regs.r7 = 0; 433 #endif 434 435 ts_push_current_session(s); 436 437 memcpy((void *)spc->ns_comm_buf_addr, va, ns_buf_size); 438 439 res = stmm_enter_user_mode(spc); 440 if (res) 441 goto out_session; 442 /* 443 * Copy the SPM response from secure partition back to the non-secure 444 * buffer of the client that called us. 445 */ 446 #ifdef ARM64 447 ta_sess->param->u[1].val.a = spc->regs.x[4]; 448 #endif 449 #ifdef ARM32 450 ta_sess->param->u[1].val.a = spc->regs.r4; 451 #endif 452 453 memcpy(va, (void *)spc->ns_comm_buf_addr, ns_buf_size); 454 455 out_session: 456 ts_pop_current_session(); 457 out_va: 458 tmp_res = mobj_dec_map(mem->mobj); 459 assert(!tmp_res); 460 461 return res; 462 } 463 464 static void stmm_enter_close_session(struct ts_session *s __unused) 465 { 466 } 467 468 static void stmm_dump_state(struct ts_ctx *ctx) 469 { 470 user_mode_ctx_print_mappings(to_user_mode_ctx(ctx)); 471 } 472 473 static uint32_t stmm_get_instance_id(struct ts_ctx *ctx) 474 { 475 return to_stmm_ctx(ctx)->uctx.vm_info.asid; 476 } 477 478 static void stmm_ctx_destroy(struct ts_ctx *ctx) 479 { 480 struct stmm_ctx *spc = to_stmm_ctx(ctx); 481 482 tee_pager_rem_um_areas(&spc->uctx); 483 vm_info_final(&spc->uctx); 484 free(spc); 485 } 486 487 static uint32_t sp_svc_get_mem_attr(vaddr_t va) 488 { 489 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 490 struct ts_session *sess = NULL; 491 struct stmm_ctx *spc = NULL; 492 uint16_t attrs = 0; 493 uint16_t perm = 0; 494 495 if (!va) 496 goto err; 497 498 sess = ts_get_current_session(); 499 spc = to_stmm_ctx(sess->ctx); 500 501 res = vm_get_prot(&spc->uctx, va, SMALL_PAGE_SIZE, &attrs); 502 if (res) 503 goto err; 504 505 if (attrs & TEE_MATTR_UR) 506 perm |= STMM_MEM_ATTR_ACCESS_RO; 507 else if (attrs & TEE_MATTR_UW) 508 perm |= STMM_MEM_ATTR_ACCESS_RW; 509 510 if (attrs & TEE_MATTR_UX) 511 perm |= STMM_MEM_ATTR_EXEC; 512 513 return perm; 514 err: 515 return STMM_RET_DENIED; 516 } 517 518 static int sp_svc_set_mem_attr(vaddr_t va, unsigned int nr_pages, uint32_t perm) 519 { 520 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 521 struct ts_session *sess = NULL; 522 struct stmm_ctx *spc = NULL; 523 size_t sz = 0; 524 uint32_t prot = 0; 525 526 if (!va || !nr_pages || MUL_OVERFLOW(nr_pages, SMALL_PAGE_SIZE, &sz)) 527 return STMM_RET_INVALID_PARAM; 528 529 if (perm & ~STMM_MEM_ATTR_ALL) 530 return STMM_RET_INVALID_PARAM; 531 532 sess = ts_get_current_session(); 533 spc = to_stmm_ctx(sess->ctx); 534 535 if ((perm & STMM_MEM_ATTR_ACCESS_MASK) == STMM_MEM_ATTR_ACCESS_RO) 536 prot |= TEE_MATTR_UR; 537 else if ((perm & STMM_MEM_ATTR_ACCESS_MASK) == STMM_MEM_ATTR_ACCESS_RW) 538 prot |= TEE_MATTR_URW; 539 540 if ((perm & STMM_MEM_ATTR_EXEC_NEVER) == STMM_MEM_ATTR_EXEC) 541 prot |= TEE_MATTR_UX; 542 543 res = vm_set_prot(&spc->uctx, va, sz, prot); 544 if (res) 545 return STMM_RET_DENIED; 546 547 return STMM_RET_SUCCESS; 548 } 549 550 #ifdef ARM64 551 static void save_sp_ctx(struct stmm_ctx *spc, struct thread_svc_regs *svc_regs) 552 { 553 size_t n = 0; 554 555 /* Save the return values from StMM */ 556 for (n = 0; n <= 7; n++) 557 spc->regs.x[n] = *(&svc_regs->x0 + n); 558 559 spc->regs.sp = svc_regs->sp_el0; 560 spc->regs.pc = svc_regs->elr; 561 spc->regs.cpsr = svc_regs->spsr; 562 } 563 #endif 564 565 #ifdef ARM32 566 static void save_sp_ctx(struct stmm_ctx *spc, struct thread_svc_regs *svc_regs) 567 { 568 spc->regs.r0 = svc_regs->r0; 569 spc->regs.r1 = svc_regs->r1; 570 spc->regs.r2 = svc_regs->r2; 571 spc->regs.r3 = svc_regs->r3; 572 spc->regs.r4 = svc_regs->r4; 573 spc->regs.r5 = svc_regs->r5; 574 spc->regs.r6 = svc_regs->r6; 575 spc->regs.r7 = svc_regs->r7; 576 spc->regs.pc = svc_regs->lr; 577 spc->regs.cpsr = svc_regs->spsr; 578 spc->regs.usr_sp = thread_get_usr_sp(); 579 } 580 #endif 581 582 static void return_from_sp_helper(bool panic, uint32_t panic_code, 583 struct thread_svc_regs *svc_regs) 584 { 585 struct ts_session *sess = ts_get_current_session(); 586 struct stmm_ctx *spc = to_stmm_ctx(sess->ctx); 587 588 if (panic) 589 spc->ta_ctx.panicked = true; 590 else 591 save_sp_ctx(spc, svc_regs); 592 593 SVC_REGS_A0(svc_regs) = 0; 594 SVC_REGS_A1(svc_regs) = panic; 595 SVC_REGS_A2(svc_regs) = panic_code; 596 } 597 598 static void service_compose_direct_resp(struct thread_svc_regs *regs, 599 uint32_t ret_val) 600 { 601 uint16_t src_id = 0; 602 uint16_t dst_id = 0; 603 604 /* extract from request */ 605 src_id = (SVC_REGS_A1(regs) >> 16) & UINT16_MAX; 606 dst_id = SVC_REGS_A1(regs) & UINT16_MAX; 607 608 /* compose message */ 609 SVC_REGS_A0(regs) = __FFA_MSG_SEND_DIRECT_RESP; 610 /* swap endpoint ids */ 611 SVC_REGS_A1(regs) = SHIFT_U32(dst_id, 16) | src_id; 612 SVC_REGS_A2(regs) = FFA_PARAM_MBZ; 613 SVC_REGS_A3(regs) = ret_val; 614 SVC_REGS_A4(regs) = 0; 615 SVC_REGS_A5(regs) = 0; 616 SVC_REGS_A6(regs) = 0; 617 SVC_REGS_A7(regs) = 0; 618 } 619 620 /* 621 * Combined read from secure partition, this will open, read and 622 * close the file object. 623 */ 624 static TEE_Result sec_storage_obj_read(unsigned long storage_id, char *obj_id, 625 unsigned long obj_id_len, void *data, 626 unsigned long len, unsigned long offset, 627 unsigned long flags) 628 { 629 const struct tee_file_operations *fops = NULL; 630 TEE_Result res = TEE_ERROR_BAD_STATE; 631 struct ts_session *sess = NULL; 632 struct tee_file_handle *fh = NULL; 633 struct stmm_ctx *spc = NULL; 634 struct tee_pobj *po = NULL; 635 size_t file_size = 0; 636 size_t read_len = 0; 637 638 fops = tee_svc_storage_file_ops(storage_id); 639 if (!fops) 640 return TEE_ERROR_ITEM_NOT_FOUND; 641 642 if (obj_id_len > TEE_OBJECT_ID_MAX_LEN) 643 return TEE_ERROR_BAD_PARAMETERS; 644 645 sess = ts_get_current_session(); 646 spc = to_stmm_ctx(sess->ctx); 647 res = vm_check_access_rights(&spc->uctx, 648 TEE_MEMORY_ACCESS_WRITE | 649 TEE_MEMORY_ACCESS_ANY_OWNER, 650 (uaddr_t)data, len); 651 if (res != TEE_SUCCESS) 652 return res; 653 654 res = tee_pobj_get(&sess->ctx->uuid, obj_id, obj_id_len, flags, 655 false, fops, &po); 656 if (res != TEE_SUCCESS) 657 return res; 658 659 res = po->fops->open(po, &file_size, &fh); 660 if (res != TEE_SUCCESS) 661 goto out; 662 663 read_len = len; 664 res = po->fops->read(fh, offset, data, &read_len); 665 if (res == TEE_ERROR_CORRUPT_OBJECT) { 666 EMSG("Object corrupt"); 667 po->fops->remove(po); 668 } else if (res == TEE_SUCCESS && len != read_len) { 669 res = TEE_ERROR_CORRUPT_OBJECT; 670 } 671 672 po->fops->close(&fh); 673 674 out: 675 tee_pobj_release(po); 676 677 return res; 678 } 679 680 /* 681 * Combined write from secure partition, this will create/open, write and 682 * close the file object. 683 */ 684 static TEE_Result sec_storage_obj_write(unsigned long storage_id, char *obj_id, 685 unsigned long obj_id_len, void *data, 686 unsigned long len, unsigned long offset, 687 unsigned long flags) 688 689 { 690 const struct tee_file_operations *fops = NULL; 691 struct ts_session *sess = NULL; 692 struct tee_file_handle *fh = NULL; 693 struct stmm_ctx *spc = NULL; 694 TEE_Result res = TEE_SUCCESS; 695 struct tee_pobj *po = NULL; 696 697 fops = tee_svc_storage_file_ops(storage_id); 698 if (!fops) 699 return TEE_ERROR_ITEM_NOT_FOUND; 700 701 if (obj_id_len > TEE_OBJECT_ID_MAX_LEN) 702 return TEE_ERROR_BAD_PARAMETERS; 703 704 sess = ts_get_current_session(); 705 spc = to_stmm_ctx(sess->ctx); 706 res = vm_check_access_rights(&spc->uctx, 707 TEE_MEMORY_ACCESS_READ | 708 TEE_MEMORY_ACCESS_ANY_OWNER, 709 (uaddr_t)data, len); 710 if (res != TEE_SUCCESS) 711 return res; 712 713 res = tee_pobj_get(&sess->ctx->uuid, obj_id, obj_id_len, flags, 714 false, fops, &po); 715 if (res != TEE_SUCCESS) 716 return res; 717 718 res = po->fops->open(po, NULL, &fh); 719 if (res == TEE_ERROR_ITEM_NOT_FOUND) 720 res = po->fops->create(po, false, NULL, 0, NULL, 0, NULL, 0, 721 &fh); 722 if (res == TEE_SUCCESS) { 723 res = po->fops->write(fh, offset, data, len); 724 po->fops->close(&fh); 725 } 726 727 tee_pobj_release(po); 728 729 return res; 730 } 731 732 static void stmm_handle_mem_mgr_service(struct thread_svc_regs *regs) 733 { 734 uint32_t action = SVC_REGS_A3(regs); 735 uintptr_t va = SVC_REGS_A4(regs); 736 uint32_t nr_pages = SVC_REGS_A5(regs); 737 uint32_t perm = SVC_REGS_A6(regs); 738 739 switch (action) { 740 case __FFA_SVC_MEMORY_ATTRIBUTES_GET: 741 service_compose_direct_resp(regs, sp_svc_get_mem_attr(va)); 742 break; 743 case __FFA_SVC_MEMORY_ATTRIBUTES_SET: 744 service_compose_direct_resp(regs, 745 sp_svc_set_mem_attr(va, nr_pages, 746 perm)); 747 break; 748 default: 749 EMSG("Undefined service id %#"PRIx32, action); 750 service_compose_direct_resp(regs, STMM_RET_INVALID_PARAM); 751 break; 752 } 753 } 754 755 static uint32_t tee2stmm_ret_val(TEE_Result res) 756 { 757 switch (res) { 758 case TEE_SUCCESS: 759 return STMM_RET_SUCCESS; 760 case TEE_ERROR_NOT_SUPPORTED: 761 return STMM_RET_NOT_SUPPORTED; 762 case TEE_ERROR_ACCESS_DENIED: 763 return STMM_RET_DENIED; 764 case TEE_ERROR_OUT_OF_MEMORY: 765 return STMM_RET_NO_MEM; 766 case TEE_ERROR_BAD_PARAMETERS: 767 default: 768 return STMM_RET_INVALID_PARAM; 769 } 770 } 771 772 #define FILENAME "EFI_VARS" 773 static void stmm_handle_storage_service(struct thread_svc_regs *regs) 774 { 775 uint32_t flags = TEE_DATA_FLAG_ACCESS_READ | 776 TEE_DATA_FLAG_ACCESS_WRITE | 777 TEE_DATA_FLAG_SHARE_READ | 778 TEE_DATA_FLAG_SHARE_WRITE; 779 uint32_t action = SVC_REGS_A3(regs); 780 void *va = (void *)SVC_REGS_A4(regs); 781 unsigned long len = SVC_REGS_A5(regs); 782 unsigned long offset = SVC_REGS_A6(regs); 783 char obj_id[] = FILENAME; 784 size_t obj_id_len = strlen(obj_id); 785 TEE_Result res = TEE_SUCCESS; 786 uint32_t stmm_rc = STMM_RET_INVALID_PARAM; 787 788 switch (action) { 789 case __FFA_SVC_RPMB_READ: 790 DMSG("RPMB read"); 791 res = sec_storage_obj_read(TEE_STORAGE_PRIVATE_RPMB, obj_id, 792 obj_id_len, va, len, offset, flags); 793 stmm_rc = tee2stmm_ret_val(res); 794 break; 795 case __FFA_SVC_RPMB_WRITE: 796 DMSG("RPMB write"); 797 res = sec_storage_obj_write(TEE_STORAGE_PRIVATE_RPMB, obj_id, 798 obj_id_len, va, len, offset, flags); 799 stmm_rc = tee2stmm_ret_val(res); 800 break; 801 default: 802 EMSG("Undefined service id %#"PRIx32, action); 803 break; 804 } 805 806 service_compose_direct_resp(regs, stmm_rc); 807 } 808 809 static void spm_eret_error(int32_t error_code, struct thread_svc_regs *regs) 810 { 811 SVC_REGS_A0(regs) = FFA_ERROR; 812 SVC_REGS_A1(regs) = FFA_PARAM_MBZ; 813 SVC_REGS_A2(regs) = error_code; 814 SVC_REGS_A3(regs) = FFA_PARAM_MBZ; 815 SVC_REGS_A4(regs) = FFA_PARAM_MBZ; 816 SVC_REGS_A5(regs) = FFA_PARAM_MBZ; 817 SVC_REGS_A6(regs) = FFA_PARAM_MBZ; 818 SVC_REGS_A7(regs) = FFA_PARAM_MBZ; 819 } 820 821 static void spm_handle_direct_req(struct thread_svc_regs *regs) 822 { 823 uint16_t dst_id = SVC_REGS_A1(regs) & UINT16_MAX; 824 825 if (dst_id == mem_mgr_id) { 826 stmm_handle_mem_mgr_service(regs); 827 } else if (dst_id == ffa_storage_id) { 828 stmm_handle_storage_service(regs); 829 } else { 830 EMSG("Undefined endpoint id %#"PRIx16, dst_id); 831 spm_eret_error(STMM_RET_INVALID_PARAM, regs); 832 } 833 } 834 835 /* Return true if returning to SP, false if returning to caller */ 836 static bool spm_handle_svc(struct thread_svc_regs *regs) 837 { 838 #ifdef ARM64 839 uint64_t *a0 = ®s->x0; 840 #endif 841 #ifdef ARM32 842 uint32_t *a0 = ®s->r0; 843 #endif 844 845 switch (*a0) { 846 case FFA_VERSION: 847 DMSG("Received FFA version"); 848 *a0 = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR); 849 return true; 850 case __FFA_MSG_SEND_DIRECT_RESP: 851 DMSG("Received FFA direct response"); 852 return_from_sp_helper(false, 0, regs); 853 return false; 854 case __FFA_MSG_SEND_DIRECT_REQ: 855 DMSG("Received FFA direct request"); 856 spm_handle_direct_req(regs); 857 return true; 858 default: 859 EMSG("Undefined syscall %#"PRIx32, (uint32_t)*a0); 860 return_from_sp_helper(true /*panic*/, 0xabcd, regs); 861 return false; 862 } 863 } 864 865 const struct ts_ops stmm_sp_ops __rodata_unpaged = { 866 .enter_open_session = stmm_enter_open_session, 867 .enter_invoke_cmd = stmm_enter_invoke_cmd, 868 .enter_close_session = stmm_enter_close_session, 869 .dump_state = stmm_dump_state, 870 .destroy = stmm_ctx_destroy, 871 .get_instance_id = stmm_get_instance_id, 872 .handle_svc = spm_handle_svc, 873 }; 874