github.com/usbarmory/GoTEE@v0.0.0-20240405084336-c52770d9fcdb/monitor/exec_riscv64.s (about)

     1  // Copyright (c) WithSecure Corporation
     2  // https://foundry.withsecure.com
     3  //
     4  // Use of this source code is governed by the license
     5  // that can be found in the LICENSE file.
     6  
     7  #include "go_asm.h"
     8  #include "textflag.h"
     9  
    10  #include "go_asm_riscv64.h"
    11  
    12  // GoTEE exception handling relies on one exit point (Exec) and one return
    13  // point (monitor), both are used for execution of a Supervisor mode execution
    14  // context.
    15  //
    16  // An execution context (ExecCtx) structure is used to hold initial register
    17  // state at execution as well as store the updated state on re-entry.
    18  //
    19  // The exception handling uses the RISC-V Machine Scratch Register (mscratch)
    20  // in a similar manner to Go own use of TLS (https://golang.org/src/runtime/tls_arm.s).
    21  //
    22  // The handler must save and restore the following registers between Machine <>
    23  // Supervisor mode switches:
    24  //
    25  //  • x1-x31 general purpose registers:
    26  //
    27  //    TamaGo (and therefore GoTEE) runs in Machine mode and does not use
    28  //    Supervisor or User mode. The pre-exception x1-x31 registers are
    29  //    saved/restored.
    30  //
    31  //  • f0-f31 floating point registers:
    32  //
    33  //    TamaGo does not use the fcsr register, therefore only pre-exception
    34  //    f0-f31 registers are saved/restored.
    35  
    36  // func Exec(ctx *ExecCtx)
    37  TEXT ·Exec(SB),$0-8
    38  	// save general purpose registers
    39  	MOV	X1, -2*8(SP)
    40  	MOV	X3, -3*8(SP)
    41  	MOV	TP, -4*8(SP)
    42  	MOV	X5, -5*8(SP)
    43  	MOV	X6, -6*8(SP)
    44  	MOV	X7, -7*8(SP)
    45  	MOV	X8, -8*8(SP)
    46  	MOV	X9, -9*8(SP)
    47  	MOV	X10, -10*8(SP)
    48  	MOV	X11, -11*8(SP)
    49  	MOV	X12, -12*8(SP)
    50  	MOV	X13, -13*8(SP)
    51  	MOV	X14, -14*8(SP)
    52  	MOV	X15, -15*8(SP)
    53  	MOV	X16, -16*8(SP)
    54  	MOV	X17, -17*8(SP)
    55  	MOV	X18, -18*8(SP)
    56  	MOV	X19, -19*8(SP)
    57  	MOV	X20, -20*8(SP)
    58  	MOV	X21, -21*8(SP)
    59  	MOV	X22, -22*8(SP)
    60  	MOV	X23, -23*8(SP)
    61  	MOV	X24, -24*8(SP)
    62  	MOV	X25, -25*8(SP)
    63  	MOV	X26, -26*8(SP)
    64  	MOV	g,   -27*8(SP)
    65  	MOV	X28, -28*8(SP)
    66  	MOV	X29, -29*8(SP)
    67  	MOV	X30, -30*8(SP)
    68  	MOV	X31, -31*8(SP)
    69  
    70  	// get argument pointer
    71  	MOV	ctx+0(FP), T0
    72  
    73  	// save g stack pointer
    74  	MOV	SP, ExecCtx_g_sp(T0)
    75  
    76  	// save context pointer
    77  	CSRW(t0, mscratch)
    78  
    79  	// switch to supervisor mode
    80  	MOV	$const_Supervisor << 11, T1
    81  	CSRS(t1, mstatus)
    82  
    83  	// restore floating-point registers
    84  	MOVD	(1*8)+ExecCtx_F(T0), F0
    85  	MOVD	(2*8)+ExecCtx_F(T0), F1
    86  	MOVD	(3*8)+ExecCtx_F(T0), F2
    87  	MOVD	(4*8)+ExecCtx_F(T0), F3
    88  	MOVD	(5*8)+ExecCtx_F(T0), F4
    89  	MOVD	(6*8)+ExecCtx_F(T0), F5
    90  	MOVD	(7*8)+ExecCtx_F(T0), F6
    91  	MOVD	(8*8)+ExecCtx_F(T0), F7
    92  	MOVD	(9*8)+ExecCtx_F(T0), F8
    93  	MOVD	(10*8)+ExecCtx_F(T0), F9
    94  	MOVD	(11*8)+ExecCtx_F(T0), F10
    95  	MOVD	(12*8)+ExecCtx_F(T0), F11
    96  	MOVD	(13*8)+ExecCtx_F(T0), F12
    97  	MOVD	(14*8)+ExecCtx_F(T0), F13
    98  	MOVD	(15*8)+ExecCtx_F(T0), F14
    99  	MOVD	(16*8)+ExecCtx_F(T0), F15
   100  	MOVD	(17*8)+ExecCtx_F(T0), F16
   101  	MOVD	(18*8)+ExecCtx_F(T0), F17
   102  	MOVD	(19*8)+ExecCtx_F(T0), F18
   103  	MOVD	(20*8)+ExecCtx_F(T0), F19
   104  	MOVD	(21*8)+ExecCtx_F(T0), F20
   105  	MOVD	(22*8)+ExecCtx_F(T0), F21
   106  	MOVD	(23*8)+ExecCtx_F(T0), F22
   107  	MOVD	(24*8)+ExecCtx_F(T0), F23
   108  	MOVD	(25*8)+ExecCtx_F(T0), F24
   109  	MOVD	(26*8)+ExecCtx_F(T0), F25
   110  	MOVD	(27*8)+ExecCtx_F(T0), F26
   111  	MOVD	(28*8)+ExecCtx_F(T0), F27
   112  	MOVD	(29*8)+ExecCtx_F(T0), F28
   113  	MOVD	(30*8)+ExecCtx_F(T0), F29
   114  	MOVD	(31*8)+ExecCtx_F(T0), F30
   115  	MOVD	(32*8)+ExecCtx_F(T0), F31
   116  
   117  	// restore general purpose registers
   118  	MOV	ExecCtx_X1(T0), X1
   119  	MOV	ExecCtx_X2(T0), X2
   120  	MOV	ExecCtx_X4(T0), TP
   121  	MOV	ExecCtx_X3(T0), X3
   122  	MOV	ExecCtx_X7(T0), X7
   123  	MOV	ExecCtx_X8(T0), X8
   124  	MOV	ExecCtx_X9(T0), X9
   125  	MOV	ExecCtx_X10(T0), X10
   126  	MOV	ExecCtx_X11(T0), X11
   127  	MOV	ExecCtx_X12(T0), X12
   128  	MOV	ExecCtx_X13(T0), X13
   129  	MOV	ExecCtx_X14(T0), X14
   130  	MOV	ExecCtx_X15(T0), X15
   131  	MOV	ExecCtx_X16(T0), X16
   132  	MOV	ExecCtx_X17(T0), X17
   133  	MOV	ExecCtx_X18(T0), X18
   134  	MOV	ExecCtx_X19(T0), X19
   135  	MOV	ExecCtx_X20(T0), X20
   136  	MOV	ExecCtx_X21(T0), X21
   137  	MOV	ExecCtx_X22(T0), X22
   138  	MOV	ExecCtx_X23(T0), X23
   139  	MOV	ExecCtx_X24(T0), X24
   140  	MOV	ExecCtx_X25(T0), X25
   141  	MOV	ExecCtx_X26(T0), X26
   142  	MOV	ExecCtx_X27(T0), g
   143  	MOV	ExecCtx_X28(T0), X28
   144  	MOV	ExecCtx_X29(T0), X29
   145  	MOV	ExecCtx_X30(T0), X30
   146  	MOV	ExecCtx_X31(T0), X31
   147  
   148  	MOV	ExecCtx_PC(T0), T1
   149  	CSRW(t1, mepc)
   150  
   151  	// restore T1, T0
   152  	MOV	ExecCtx_X6(T0), T1
   153  	MOV	ExecCtx_X5(T0), T0
   154  
   155  	MRET
   156  
   157  TEXT ·monitor(SB),NOSPLIT|NOFRAME,$0
   158  	// restore context pointer
   159  	CSRRW(t0, mscratch, t0)
   160  
   161  	// save general purpose registers
   162  	MOV	X1, ExecCtx_X1(T0)
   163  	MOV	X2, ExecCtx_X2(T0)
   164  	MOV	X3, ExecCtx_X3(T0)
   165  	MOV	TP, ExecCtx_X4(T0)
   166  	MOV	X6, ExecCtx_X6(T0)
   167  	MOV	X7, ExecCtx_X7(T0)
   168  	MOV	X8, ExecCtx_X8(T0)
   169  	MOV	X9, ExecCtx_X9(T0)
   170  	MOV	X10, ExecCtx_X10(T0)
   171  	MOV	X11, ExecCtx_X11(T0)
   172  	MOV	X12, ExecCtx_X12(T0)
   173  	MOV	X13, ExecCtx_X13(T0)
   174  	MOV	X14, ExecCtx_X14(T0)
   175  	MOV	X15, ExecCtx_X15(T0)
   176  	MOV	X16, ExecCtx_X16(T0)
   177  	MOV	X17, ExecCtx_X17(T0)
   178  	MOV	X18, ExecCtx_X18(T0)
   179  	MOV	X19, ExecCtx_X19(T0)
   180  	MOV	X20, ExecCtx_X20(T0)
   181  	MOV	X21, ExecCtx_X21(T0)
   182  	MOV	X22, ExecCtx_X22(T0)
   183  	MOV	X23, ExecCtx_X23(T0)
   184  	MOV	X24, ExecCtx_X24(T0)
   185  	MOV	X25, ExecCtx_X25(T0)
   186  	MOV	X26, ExecCtx_X26(T0)
   187  	MOV	g,   ExecCtx_X27(T0)
   188  	MOV	X28, ExecCtx_X28(T0)
   189  	MOV	X29, ExecCtx_X29(T0)
   190  	MOV	X30, ExecCtx_X30(T0)
   191  	MOV	X31, ExecCtx_X31(T0)
   192  
   193  	// save floating-point registers
   194  	MOVD	F0, (1*8)+ExecCtx_F(T0)
   195  	MOVD	F1, (2*8)+ExecCtx_F(T0)
   196  	MOVD	F2, (3*8)+ExecCtx_F(T0)
   197  	MOVD	F3, (4*8)+ExecCtx_F(T0)
   198  	MOVD	F4, (5*8)+ExecCtx_F(T0)
   199  	MOVD	F5, (6*8)+ExecCtx_F(T0)
   200  	MOVD	F6, (7*8)+ExecCtx_F(T0)
   201  	MOVD	F7, (8*8)+ExecCtx_F(T0)
   202  	MOVD	F8, (9*8)+ExecCtx_F(T0)
   203  	MOVD	F9, (10*8)+ExecCtx_F(T0)
   204  	MOVD	F10, (11*8)+ExecCtx_F(T0)
   205  	MOVD	F11, (12*8)+ExecCtx_F(T0)
   206  	MOVD	F12, (13*8)+ExecCtx_F(T0)
   207  	MOVD	F13, (14*8)+ExecCtx_F(T0)
   208  	MOVD	F14, (15*8)+ExecCtx_F(T0)
   209  	MOVD	F15, (16*8)+ExecCtx_F(T0)
   210  	MOVD	F16, (17*8)+ExecCtx_F(T0)
   211  	MOVD	F17, (18*8)+ExecCtx_F(T0)
   212  	MOVD	F18, (19*8)+ExecCtx_F(T0)
   213  	MOVD	F19, (20*8)+ExecCtx_F(T0)
   214  	MOVD	F20, (21*8)+ExecCtx_F(T0)
   215  	MOVD	F21, (22*8)+ExecCtx_F(T0)
   216  	MOVD	F22, (23*8)+ExecCtx_F(T0)
   217  	MOVD	F23, (24*8)+ExecCtx_F(T0)
   218  	MOVD	F24, (25*8)+ExecCtx_F(T0)
   219  	MOVD	F25, (26*8)+ExecCtx_F(T0)
   220  	MOVD	F26, (27*8)+ExecCtx_F(T0)
   221  	MOVD	F27, (28*8)+ExecCtx_F(T0)
   222  	MOVD	F28, (29*8)+ExecCtx_F(T0)
   223  	MOVD	F29, (30*8)+ExecCtx_F(T0)
   224  	MOVD	F30, (31*8)+ExecCtx_F(T0)
   225  	MOVD	F31, (32*8)+ExecCtx_F(T0)
   226  
   227  	// save PC
   228  	CSRR(mepc, t1)
   229  	ADD	$(4), T1, T1
   230  	MOV	T1, ExecCtx_PC(T0)
   231  
   232  	// save PC from RA
   233  	//MOV	RA, ExecCtx_PC(T0)
   234  
   235  	// save T0
   236  	CSRR(mscratch, t1)
   237  	MOV	T1, ExecCtx_X5(T0)
   238  
   239  	// save MCAUSE
   240  	CSRR(mcause, t1)
   241  	MOV	T1, ExecCtx_MCAUSE(T0)
   242  
   243  	// restore g registers
   244  	MOV	ExecCtx_g_sp(T0), SP
   245  	MOV	-2*8(SP), X1
   246  	MOV	-3*8(SP), X3
   247  	MOV	-4*8(SP), TP
   248  	MOV	-5*8(SP), X5
   249  	MOV	-6*8(SP), X6
   250  	MOV	-7*8(SP), X7
   251  	MOV	-8*8(SP), X8
   252  	MOV	-9*8(SP), X9
   253  	MOV	-10*8(SP), X10
   254  	MOV	-11*8(SP), X11
   255  	MOV	-12*8(SP), X12
   256  	MOV	-13*8(SP), X13
   257  	MOV	-14*8(SP), X14
   258  	MOV	-15*8(SP), X15
   259  	MOV	-16*8(SP), X16
   260  	MOV	-17*8(SP), X17
   261  	MOV	-18*8(SP), X18
   262  	MOV	-19*8(SP), X19
   263  	MOV	-20*8(SP), X20
   264  	MOV	-21*8(SP), X21
   265  	MOV	-22*8(SP), X22
   266  	MOV	-23*8(SP), X23
   267  	MOV	-24*8(SP), X24
   268  	MOV	-25*8(SP), X25
   269  	MOV	-26*8(SP), X26
   270  	MOV	-27*8(SP), g
   271  	MOV	-28*8(SP), X28
   272  	MOV	-29*8(SP), X29
   273  	MOV	-30*8(SP), X30
   274  	MOV	-31*8(SP), X31
   275  
   276  	JMP	(RA)