1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright 2022-2023 NXP 4 * Copyright (c) 2016-2022, Linaro Limited 5 * Copyright (c) 2014, STMicroelectronics International N.V. 6 * Copyright (c) 2020-2021, Arm Limited 7 */ 8 9 #include <platform_config.h> 10 11 #include <assert.h> 12 #include <config.h> 13 #include <io.h> 14 #include <keep.h> 15 #include <kernel/asan.h> 16 #include <kernel/boot.h> 17 #include <kernel/interrupt.h> 18 #include <kernel/linker.h> 19 #include <kernel/lockdep.h> 20 #include <kernel/misc.h> 21 #include <kernel/panic.h> 22 #include <kernel/spinlock.h> 23 #include <kernel/tee_ta_manager.h> 24 #include <kernel/thread.h> 25 #include <kernel/thread_private.h> 26 #include <kernel/user_mode_ctx_struct.h> 27 #include <kernel/virtualization.h> 28 #include <mm/core_memprot.h> 29 #include <mm/mobj.h> 30 #include <mm/tee_mm.h> 31 #include <mm/vm.h> 32 #include <riscv.h> 33 #include <trace.h> 34 #include <util.h> 35 36 /* 37 * This function is called as a guard after each ABI call which is not 38 * supposed to return. 39 */ 40 void __noreturn __panic_at_abi_return(void) 41 { 42 panic(); 43 } 44 45 /* This function returns current masked exception bits. */ 46 uint32_t __nostackcheck thread_get_exceptions(void) 47 { 48 uint32_t xie = read_csr(CSR_XIE) & THREAD_EXCP_ALL; 49 50 return xie ^ THREAD_EXCP_ALL; 51 } 52 53 void __nostackcheck thread_set_exceptions(uint32_t exceptions) 54 { 55 /* Foreign interrupts must not be unmasked while holding a spinlock */ 56 if (!(exceptions & THREAD_EXCP_FOREIGN_INTR)) 57 assert_have_no_spinlock(); 58 59 /* 60 * In ARM, the bits in DAIF register are used to mask the exceptions. 61 * While in RISC-V, the bits in CSR XIE are used to enable(unmask) 62 * corresponding interrupt sources. To not modify the function of 63 * thread_set_exceptions(), we should "invert" the bits in "exceptions". 64 * The corresponding bits in "exceptions" will be inverted so they will 65 * be cleared when we write the final value into CSR XIE. So that we 66 * can mask those exceptions. 67 */ 68 exceptions &= THREAD_EXCP_ALL; 69 exceptions ^= THREAD_EXCP_ALL; 70 71 barrier(); 72 write_csr(CSR_XIE, exceptions); 73 barrier(); 74 } 75 76 uint32_t __nostackcheck thread_mask_exceptions(uint32_t exceptions) 77 { 78 uint32_t state = thread_get_exceptions(); 79 80 thread_set_exceptions(state | (exceptions & THREAD_EXCP_ALL)); 81 return state; 82 } 83 84 void __nostackcheck thread_unmask_exceptions(uint32_t state) 85 { 86 thread_set_exceptions(state & THREAD_EXCP_ALL); 87 } 88 89 static void thread_lazy_save_ns_vfp(void) 90 { 91 static_assert(!IS_ENABLED(CFG_WITH_VFP)); 92 } 93 94 static void thread_lazy_restore_ns_vfp(void) 95 { 96 static_assert(!IS_ENABLED(CFG_WITH_VFP)); 97 } 98 99 static void setup_unwind_user_mode(struct thread_scall_regs *regs) 100 { 101 regs->ra = (uintptr_t)thread_unwind_user_mode; 102 regs->status = xstatus_for_xret(true, PRV_S); 103 /* 104 * We are going to exit user mode. The stack pointer must be set as the 105 * original value it had before allocating space of scall "regs" and 106 * calling thread_scall_handler(). Thus, we can simply set stack pointer 107 * as (regs + 1) value. 108 */ 109 regs->sp = (uintptr_t)(regs + 1); 110 } 111 112 static void thread_unhandled_trap(struct thread_ctx_regs *regs __unused, 113 unsigned long cause __unused) 114 { 115 DMSG("Unhandled trap xepc:0x%016lx xcause:0x%016lx xtval:0x%016lx", 116 read_csr(CSR_XEPC), read_csr(CSR_XCAUSE), read_csr(CSR_XTVAL)); 117 panic(); 118 } 119 120 void thread_scall_handler(struct thread_scall_regs *regs) 121 { 122 struct ts_session *sess = NULL; 123 uint32_t state = 0; 124 125 /* Enable native interrupts */ 126 state = thread_get_exceptions(); 127 thread_unmask_exceptions(state & ~THREAD_EXCP_NATIVE_INTR); 128 129 thread_user_save_vfp(); 130 131 sess = ts_get_current_session(); 132 133 /* Restore foreign interrupts which are disabled on exception entry */ 134 thread_restore_foreign_intr(); 135 136 assert(sess && sess->handle_scall); 137 138 if (!sess->handle_scall(regs)) { 139 setup_unwind_user_mode(regs); 140 thread_exit_user_mode(regs->a0, regs->a1, regs->a2, 141 regs->a3, regs->sp, regs->ra, 142 regs->status); 143 } 144 } 145 146 static void thread_irq_handler(void) 147 { 148 interrupt_main_handler(); 149 } 150 151 void thread_native_interrupt_handler(struct thread_ctx_regs *regs, 152 unsigned long cause) 153 { 154 switch (cause & LONG_MAX) { 155 case IRQ_XTIMER: 156 clear_csr(CSR_XIE, CSR_XIE_TIE); 157 break; 158 case IRQ_XSOFT: 159 thread_unhandled_trap(regs, cause); 160 break; 161 case IRQ_XEXT: 162 thread_irq_handler(); 163 break; 164 default: 165 thread_unhandled_trap(regs, cause); 166 } 167 } 168 169 unsigned long xstatus_for_xret(uint8_t pie, uint8_t pp) 170 { 171 unsigned long xstatus = read_csr(CSR_XSTATUS); 172 173 assert(pp == PRV_M || pp == PRV_S || pp == PRV_U); 174 175 #ifdef RV32 176 xstatus = set_field_u32(xstatus, CSR_XSTATUS_IE, 0); 177 xstatus = set_field_u32(xstatus, CSR_XSTATUS_PIE, pie); 178 xstatus = set_field_u32(xstatus, CSR_XSTATUS_SPP, pp); 179 #else /* RV64 */ 180 xstatus = set_field_u64(xstatus, CSR_XSTATUS_IE, 0); 181 xstatus = set_field_u64(xstatus, CSR_XSTATUS_PIE, pie); 182 xstatus = set_field_u64(xstatus, CSR_XSTATUS_SPP, pp); 183 #endif 184 185 return xstatus; 186 } 187 188 static void init_regs(struct thread_ctx *thread, uint32_t a0, uint32_t a1, 189 uint32_t a2, uint32_t a3, uint32_t a4, uint32_t a5, 190 uint32_t a6, uint32_t a7, void *pc) 191 { 192 memset(&thread->regs, 0, sizeof(thread->regs)); 193 194 thread->regs.epc = (uintptr_t)pc; 195 196 /* Set up xstatus */ 197 thread->regs.status = xstatus_for_xret(true, PRV_S); 198 199 /* Enable native interrupt */ 200 thread->regs.ie = THREAD_EXCP_NATIVE_INTR; 201 202 /* Reinitialize stack pointer */ 203 thread->regs.sp = thread->stack_va_end; 204 205 /* Set up GP and TP */ 206 thread->regs.gp = read_gp(); 207 thread->regs.tp = read_tp(); 208 209 /* 210 * Copy arguments into context. This will make the 211 * arguments appear in a0-a7 when thread is started. 212 */ 213 thread->regs.a0 = a0; 214 thread->regs.a1 = a1; 215 thread->regs.a2 = a2; 216 thread->regs.a3 = a3; 217 thread->regs.a4 = a4; 218 thread->regs.a5 = a5; 219 thread->regs.a6 = a6; 220 thread->regs.a7 = a7; 221 } 222 223 static void __thread_alloc_and_run(uint32_t a0, uint32_t a1, uint32_t a2, 224 uint32_t a3, uint32_t a4, uint32_t a5, 225 uint32_t a6, uint32_t a7, 226 void *pc) 227 { 228 struct thread_core_local *l = thread_get_core_local(); 229 bool found_thread = false; 230 size_t n = 0; 231 232 assert(l->curr_thread == THREAD_ID_INVALID); 233 234 thread_lock_global(); 235 236 for (n = 0; n < CFG_NUM_THREADS; n++) { 237 if (threads[n].state == THREAD_STATE_FREE) { 238 threads[n].state = THREAD_STATE_ACTIVE; 239 found_thread = true; 240 break; 241 } 242 } 243 244 thread_unlock_global(); 245 246 if (!found_thread) 247 return; 248 249 l->curr_thread = n; 250 251 threads[n].flags = 0; 252 init_regs(threads + n, a0, a1, a2, a3, a4, a5, a6, a7, pc); 253 254 thread_lazy_save_ns_vfp(); 255 256 l->flags &= ~THREAD_CLF_TMP; 257 258 thread_resume(&threads[n].regs); 259 /*NOTREACHED*/ 260 panic(); 261 } 262 263 void thread_alloc_and_run(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3, 264 uint32_t a4, uint32_t a5) 265 { 266 __thread_alloc_and_run(a0, a1, a2, a3, a4, a5, 0, 0, 267 thread_std_abi_entry); 268 } 269 270 static void copy_a0_to_a3(struct thread_ctx_regs *regs, uint32_t a0, 271 uint32_t a1, uint32_t a2, uint32_t a3) 272 { 273 regs->a0 = a0; 274 regs->a1 = a1; 275 regs->a2 = a2; 276 regs->a3 = a3; 277 } 278 279 static bool is_from_user(unsigned long status) 280 { 281 return (status & CSR_XSTATUS_SPP) == 0; 282 } 283 284 #ifdef CFG_SYSCALL_FTRACE 285 static void __noprof ftrace_suspend(void) 286 { 287 struct ts_session *s = TAILQ_FIRST(&thread_get_tsd()->sess_stack); 288 289 if (s && s->fbuf) 290 s->fbuf->syscall_trace_suspended = true; 291 } 292 293 static void __noprof ftrace_resume(void) 294 { 295 struct ts_session *s = TAILQ_FIRST(&thread_get_tsd()->sess_stack); 296 297 if (s && s->fbuf) 298 s->fbuf->syscall_trace_suspended = false; 299 } 300 #else 301 static void __maybe_unused __noprof ftrace_suspend(void) 302 { 303 } 304 305 static void __noprof ftrace_resume(void) 306 { 307 } 308 #endif 309 310 static bool is_user_mode(struct thread_ctx_regs *regs) 311 { 312 return is_from_user((uint32_t)regs->status); 313 } 314 315 vaddr_t thread_get_saved_thread_sp(void) 316 { 317 struct thread_core_local *l = thread_get_core_local(); 318 int ct = l->curr_thread; 319 320 assert(ct != THREAD_ID_INVALID); 321 return threads[ct].kern_sp; 322 } 323 324 void thread_resume_from_rpc(uint32_t thread_id, uint32_t a0, uint32_t a1, 325 uint32_t a2, uint32_t a3) 326 { 327 size_t n = thread_id; 328 struct thread_core_local *l = thread_get_core_local(); 329 bool found_thread = false; 330 331 assert(l->curr_thread == THREAD_ID_INVALID); 332 333 thread_lock_global(); 334 335 if (n < CFG_NUM_THREADS && threads[n].state == THREAD_STATE_SUSPENDED) { 336 threads[n].state = THREAD_STATE_ACTIVE; 337 found_thread = true; 338 } 339 340 thread_unlock_global(); 341 342 if (!found_thread) 343 return; 344 345 l->curr_thread = n; 346 347 if (threads[n].have_user_map) { 348 core_mmu_set_user_map(&threads[n].user_map); 349 if (threads[n].flags & THREAD_FLAGS_EXIT_ON_FOREIGN_INTR) 350 tee_ta_ftrace_update_times_resume(); 351 } 352 353 if (is_user_mode(&threads[n].regs)) 354 tee_ta_update_session_utime_resume(); 355 356 /* 357 * We may resume thread at another hart, so we need to re-assign value 358 * of tp to be current hart's thread_core_local. 359 */ 360 if (!is_user_mode(&threads[n].regs)) 361 threads[n].regs.tp = read_tp(); 362 363 /* 364 * Return from RPC to request service of a foreign interrupt must not 365 * get parameters from non-secure world. 366 */ 367 if (threads[n].flags & THREAD_FLAGS_COPY_ARGS_ON_RETURN) { 368 copy_a0_to_a3(&threads[n].regs, a0, a1, a2, a3); 369 threads[n].flags &= ~THREAD_FLAGS_COPY_ARGS_ON_RETURN; 370 } 371 372 thread_lazy_save_ns_vfp(); 373 374 if (threads[n].have_user_map) 375 ftrace_resume(); 376 377 l->flags &= ~THREAD_CLF_TMP; 378 thread_resume(&threads[n].regs); 379 /*NOTREACHED*/ 380 panic(); 381 } 382 383 void thread_state_free(void) 384 { 385 struct thread_core_local *l = thread_get_core_local(); 386 int ct = l->curr_thread; 387 388 assert(ct != THREAD_ID_INVALID); 389 390 thread_lazy_restore_ns_vfp(); 391 392 thread_lock_global(); 393 394 assert(threads[ct].state == THREAD_STATE_ACTIVE); 395 threads[ct].state = THREAD_STATE_FREE; 396 threads[ct].flags = 0; 397 l->curr_thread = THREAD_ID_INVALID; 398 399 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) 400 virt_unset_guest(); 401 thread_unlock_global(); 402 } 403 404 int thread_state_suspend(uint32_t flags, unsigned long status, vaddr_t pc) 405 { 406 struct thread_core_local *l = thread_get_core_local(); 407 int ct = l->curr_thread; 408 409 assert(ct != THREAD_ID_INVALID); 410 411 if (core_mmu_user_mapping_is_active()) 412 ftrace_suspend(); 413 414 thread_check_canaries(); 415 416 if (is_from_user(status)) { 417 thread_user_save_vfp(); 418 tee_ta_update_session_utime_suspend(); 419 tee_ta_gprof_sample_pc(pc); 420 } 421 thread_lazy_restore_ns_vfp(); 422 423 thread_lock_global(); 424 425 assert(threads[ct].state == THREAD_STATE_ACTIVE); 426 threads[ct].flags |= flags; 427 threads[ct].regs.status = status; 428 threads[ct].regs.epc = pc; 429 threads[ct].state = THREAD_STATE_SUSPENDED; 430 431 threads[ct].have_user_map = core_mmu_user_mapping_is_active(); 432 if (threads[ct].have_user_map) { 433 if (threads[ct].flags & THREAD_FLAGS_EXIT_ON_FOREIGN_INTR) 434 tee_ta_ftrace_update_times_suspend(); 435 core_mmu_get_user_map(&threads[ct].user_map); 436 core_mmu_set_user_map(NULL); 437 } 438 439 l->curr_thread = THREAD_ID_INVALID; 440 441 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) 442 virt_unset_guest(); 443 444 thread_unlock_global(); 445 446 return ct; 447 } 448 449 static void init_user_kcode(void) 450 { 451 } 452 453 void thread_init_primary(void) 454 { 455 /* Initialize canaries around the stacks */ 456 thread_init_canaries(); 457 458 init_user_kcode(); 459 } 460 461 static vaddr_t get_trap_vect(void) 462 { 463 return (vaddr_t)thread_trap_vect; 464 } 465 466 void thread_init_tvec(void) 467 { 468 unsigned long tvec = (unsigned long)get_trap_vect(); 469 470 write_csr(CSR_XTVEC, tvec); 471 assert(read_csr(CSR_XTVEC) == tvec); 472 } 473 474 void thread_init_per_cpu(void) 475 { 476 thread_init_tvec(); 477 /* 478 * We may receive traps from now, therefore, zeroize xSCRATCH such 479 * that thread_trap_vect() can distinguish between user traps 480 * and kernel traps. 481 */ 482 write_csr(CSR_XSCRATCH, 0); 483 #ifndef CFG_PAN 484 /* 485 * Allow access to user pages. When CFG_PAN is enabled, the SUM bit will 486 * be set and clear at runtime when necessary. 487 */ 488 set_csr(CSR_XSTATUS, CSR_XSTATUS_SUM); 489 #endif 490 } 491 492 static void set_ctx_regs(struct thread_ctx_regs *regs, unsigned long a0, 493 unsigned long a1, unsigned long a2, unsigned long a3, 494 unsigned long user_sp, unsigned long entry_func, 495 unsigned long status, unsigned long ie, 496 struct thread_pauth_keys *keys __unused) 497 { 498 *regs = (struct thread_ctx_regs){ 499 .a0 = a0, 500 .a1 = a1, 501 .a2 = a2, 502 .a3 = a3, 503 .s0 = 0, 504 .sp = user_sp, 505 .ra = entry_func, 506 .status = status, 507 .ie = ie, 508 }; 509 } 510 511 uint32_t thread_enter_user_mode(unsigned long a0, unsigned long a1, 512 unsigned long a2, unsigned long a3, 513 unsigned long user_sp, 514 unsigned long entry_func, 515 bool is_32bit __unused, 516 uint32_t *exit_status0, 517 uint32_t *exit_status1) 518 { 519 unsigned long status = 0; 520 unsigned long ie = 0; 521 uint32_t exceptions = 0; 522 uint32_t rc = 0; 523 struct thread_ctx_regs *regs = NULL; 524 525 tee_ta_update_session_utime_resume(); 526 527 /* Read current interrupt masks */ 528 ie = read_csr(CSR_XIE); 529 530 /* 531 * Mask all exceptions, the CSR_XSTATUS.IE will be set from 532 * setup_unwind_user_mode() after exiting. 533 */ 534 exceptions = thread_mask_exceptions(THREAD_EXCP_ALL); 535 regs = thread_get_ctx_regs(); 536 status = xstatus_for_xret(true, PRV_U); 537 set_ctx_regs(regs, a0, a1, a2, a3, user_sp, entry_func, status, ie, 538 NULL); 539 rc = __thread_enter_user_mode(regs, exit_status0, exit_status1); 540 thread_unmask_exceptions(exceptions); 541 542 return rc; 543 } 544 545 void __thread_rpc(uint32_t rv[THREAD_RPC_NUM_ARGS]) 546 { 547 thread_rpc_xstatus(rv, xstatus_for_xret(false, PRV_S)); 548 } 549