1/*
2 * Copyright (c) 2006-2022, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date           Author       Notes
8 * 2019-10-25     tyx          first version
9 */
10
11.cpu cortex-m4
12.syntax unified
13.thumb
14.text
15
16/*
17 * int tzcall(int id, rt_ubase_t arg0, rt_ubase_t arg1, rt_ubase_t arg2);
18 */
19.global tzcall
20.type tzcall, %function
21tzcall:
22    SVC     1                       /* call SVC 1 */
23    BX      LR
24
25tzcall_entry:
26    PUSH    {R1, R4, LR}
27    MOV     R4, R1                  /* copy thread SP to R4 */
28    LDMFD   R4!, {r0 - r3}          /* pop user stack, get input arg0, arg1, arg2 */
29    STMFD   R4!, {r0 - r3}          /* push stack, user stack recovery */
30    BL      rt_secure_svc_handle    /* call fun */
31    POP     {R1, R4, LR}
32    STR     R0, [R1]                /* update return value */
33    BX      LR                      /* return to thread */
34
35syscall_entry:
36    BX      LR                      /* return to user app */
37
38.global SVC_Handler
39.type SVC_Handler, %function
40SVC_Handler:
41
42    /* get SP, save to R1 */
43    MRS     R1, MSP                 /* get fault context from handler. */
44    TST     LR, #0x04               /* if(!EXC_RETURN[2]) */
45    BEQ     get_sp_done
46    MRS     R1, PSP                 /* get fault context from thread. */
47get_sp_done:
48
49    /* get svc index */
50    LDR     R0, [R1, #24]
51    LDRB    R0, [R0, #-2]
52
53    /* if svc == 0, do system call */
54    CMP     R0, #0x0
55    BEQ    syscall_entry
56
57    /* if svc == 1, do TrustZone call */
58    CMP     R0, #0x1
59    BEQ    tzcall_entry
60