2727
2828 .global esp_tee_global_interrupt_handler
2929 .global esp_tee_service_dispatcher
30+ .global _tee_s_entry
3031
3132 .section .data
33+
3234 .align 4
3335 .global _ns_sp
3436_ns_sp:
3537 .word 0
3638
37- .section .data
3839 .align 4
3940 .global _s_sp
4041_s_sp:
9192 sw t0, RV_STK_MTVAL(sp)
9293 csrr t0, mhartid
9394 sw t0, RV_STK_MHARTID(sp)
95+ csrr t0, mcause
96+ sw t0, RV_STK_MCAUSE(sp)
9497.endm
9598
9699/* Restore the general purpose registers (excluding gp) from the context on
@@ -169,16 +172,17 @@ _s_sp:
169172 .section .exception_vectors.text, "ax"
170173
171174 /* Exception handler. */
172- .global _panic_handler
173- .type _panic_handler , @function
174- _panic_handler :
175- /* Backup t0 on the stack before using it */
175+ .global _tee_panic_handler
176+ .type _tee_panic_handler , @function
177+ _tee_panic_handler :
178+ /* Backup t0, t1 on the stack before using it */
176179 addi sp, sp, -16
177180 sw t0, 0 (sp)
181+ sw t1, 4 (sp)
178182
179183 /* Read mcause */
180184 csrr t0, mcause
181- li t1, VECTORS_MCAUSE_INTBIT_MASK | VECTORS_MCAUSE_REASON_MASK
185+ li t1, VECTORS_MCAUSE_REASON_MASK
182186 and t0, t0, t1
183187
184188 /* Check whether the exception is an M-mode ecall */
@@ -189,10 +193,12 @@ _panic_handler:
189193 li t1, ECALL_U_MODE
190194 beq t0, t1, _user_ecall
191195
192- /* Restore t0 from the stack */
196+ /* Restore t0, t1 from the stack */
193197 lw t0, 0 (sp)
198+ lw t1, 4 (sp)
194199 addi sp, sp, 16
195200
201+ _actual_panic:
196202 /* Not an ecall, proceed to the panic handler */
197203 /* Allocate space on the stack and store general purpose registers */
198204 save_general_regs RV_STK_FRMSZ
@@ -245,13 +251,17 @@ _return_from_exception:
245251 restore_general_regs RV_STK_FRMSZ
246252 mret
247253
248- .size _panic_handler , .-_panic_handler
254+ .size _tee_panic_handler , .-_tee_panic_handler
249255
250256 /* ECALL handler. */
251257 .type _ecall_handler, @function
252258_ecall_handler:
253259 /* M-mode ecall handler */
254260_machine_ecall:
261+ /* Enable the U-mode delegation of all interrupts (except the TEE secure interrupt) */
262+ li t0, TEE_INTR_DELEG_MASK
263+ csrs mideleg, t0
264+
255265 /* Set the privilege mode to transition to after mret to U-mode */
256266 li t0, MSTATUS_MPP
257267 csrc mstatus, t0
@@ -270,7 +280,7 @@ _machine_ecall:
270280 * The A0 register contains the return value of the corresponding service.
271281 * After restoring the entire register context, we assign A0 the value back to the return value. */
272282 csrw mscratch, a0
273- restore_general_regs RV_STK_FRMSZ
283+ restore_general_regs
274284 csrrw a0, mscratch, zero
275285
276286_skip_ctx_restore:
@@ -284,17 +294,17 @@ _skip_ctx_restore:
284294_user_ecall:
285295 /* Check whether we are returning after servicing an U-mode interrupt */
286296 lui t0, RTNVAL
287- csrr t1, mscratch
297+ csrrw t1, mscratch, zero
288298 beq t0, t1, _rtn_from_ns_int
289- csrwi mscratch, 0
290299
291- /* Restore t0 from the stack */
300+ /* Restore t0, t1 from the stack */
292301 lw t0, 0 (sp)
302+ lw t1, 4 (sp)
293303 addi sp, sp, 16
294304
295305 /* This point is reached when a secure service call is issued from the REE */
296306 /* Save register context and mepc */
297- save_general_regs RV_STK_FRMSZ
307+ save_general_regs
298308 save_mepc
299309
300310 /* Save the U-mode (i.e. REE) stack pointer */
@@ -304,22 +314,18 @@ _user_ecall:
304314 /* Switch to the M-mode (i.e. TEE) stack */
305315 la sp, _tee_stack
306316
317+ /* Load the TEE entry point (see _tee_s_entry) in the mepc */
318+ la t0, _tee_s_entry
319+ csrw mepc, t0
320+
307321 /* Disable the U-mode delegation of all interrupts */
308322 csrwi mideleg, 0
309323
310- /* Enable interrupts */
311- csrsi mstatus, MSTATUS_MIE
312-
313- /* Jump to the secure service dispatcher */
314- jal esp_tee_service_dispatcher
315-
316- /* Enable the U-mode delegation of all interrupts (except the TEE secure interrupt) */
317- li t0, TEE_INTR_DELEG_MASK
318- csrs mideleg, t0
324+ /* Set the privilege mode to transition to after mret to M-mode */
325+ li t0, MSTATUS_MPP
326+ csrs mstatus, t0
319327
320- /* Fire an M-ecall */
321- mv a1, zero
322- ecall
328+ mret
323329
324330 /* This point is reached after servicing a U-mode interrupt occurred
325331 * while executing a secure service */
@@ -331,16 +337,13 @@ _rtn_from_ns_int:
331337 la t0, _s_sp
332338 lw sp, 0 (t0)
333339
334- /* Clear the flag set marking the completion of interrupt service */
335- csrwi mscratch, 0
336-
337340 /* Set the privilege mode to transition to after mret to M-mode */
338341 li t0, MSTATUS_MPP
339342 csrs mstatus, t0
340343
341344 /* Restore register context and resume the secure service */
342345 restore_mepc
343- restore_general_regs RV_STK_FRMSZ
346+ restore_general_regs
344347
345348 mret
346349
@@ -354,7 +357,7 @@ _rtn_from_ns_int:
354357_tee_ns_intr_handler:
355358 /* Start by saving the general purpose registers and the PC value before
356359 * the interrupt happened. */
357- save_general_regs RV_STK_FRMSZ
360+ save_general_regs
358361 save_mepc
359362
360363 /* Though it is not necessary we save GP and SP here.
@@ -364,7 +367,7 @@ _tee_ns_intr_handler:
364367 /* As gp register is not saved by the macro, save it here */
365368 sw gp, RV_STK_GP(sp)
366369 /* Same goes for the SP value before trapping */
367- addi t0, sp, RV_STK_FRMSZ /* restore sp with the value when interrupt happened */
370+ addi t0, sp, CONTEXT_SIZE /* restore sp with the value when interrupt happened */
368371 /* Save SP */
369372 sw t0, RV_STK_SP(sp)
370373
@@ -391,7 +394,7 @@ _tee_ns_intr_handler:
391394 csrc mstatus, t1
392395
393396 /* Save the current secure stack pointer and switch to the U-mode interrupt stack
394- * saved while entering the secure service call routine (see `sec_world_entry `) */
397+ * saved while entering the secure service call routine (see `_tee_s_entry `) */
395398 la t0, _s_sp
396399 sw sp, 0 (t0)
397400 la t1, _ns_sp
@@ -461,8 +464,6 @@ _tee_s_intr_handler:
461464_save_reg_ctx:
462465 /* Save CSR context here */
463466 save_mcsr
464- csrr t0, mcause
465- sw t0, RV_STK_MCAUSE(sp)
466467 /* NOTE: With ESP-TEE, since APM violations trigger a panic, it's safe to use the mscratch
467468 * register to pass on the stack pointer to the APM violation handler */
468469 csrw mscratch, sp
@@ -519,7 +520,7 @@ _intr_hdlr_exec:
519520 mv a0, sp /* argument 1, stack pointer */
520521 mv a1, s1 /* argument 2, interrupt number (mcause) */
521522 /* mask off the interrupt flag of mcause */
522- li t0, 0x7fffffff
523+ li t0, VECTORS_MCAUSE_REASON_MASK
523524 and a1, a1, t0
524525
525526 jal esp_tee_global_interrupt_handler
@@ -546,3 +547,18 @@ _intr_hdlr_exec:
546547 mret
547548
548549 .size _tee_s_intr_handler, .-_tee_s_intr_handler
550+
551+ .section .text , "ax"
552+
553+ .align 4
554+ .type _tee_s_entry, @function
555+ _tee_s_entry:
556+ /* Jump to the secure service dispatcher */
557+ jal esp_tee_service_dispatcher
558+
559+ /* Fire an M-ecall */
560+ mv a1, zero
561+ ecall
562+ fence
563+
564+ .size _tee_s_entry, .-_tee_s_entry
0 commit comments