forked from keystone-enclave/sm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmprv.S
196 lines (168 loc) · 4 KB
/
mprv.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#if __riscv_xlen == 64
# define STORE sd
# define LOAD ld
# define LOG_REGBYTES 3
#elif __riscv_xlen == 32
# define STORE sw
# define LOAD lw
# define LOG_REGBYTES 2
#endif
#define REGBYTES (1 << LOG_REGBYTES)
.text
#define TRY(tryname, r_mtvec, r_mepc, r_mcause, r_mstatus) \
la r_mtvec, tryname##_err ;\
csrrw r_mtvec, mtvec, r_mtvec ;\
csrr r_mepc, mepc ;\
csrr r_mcause, mcause ;\
csrr r_mstatus, mstatus
#define CATCH_ERR(tryname, r_mepc) \
j tryname##_ok ;\
.align 4 ;\
tryname##_err : ;\
la r_mepc, tryname##_err_real ;\
csrw mepc, r_mepc ;\
mret ;\
.align 4 ;\
tryname##_err_real :
#define TRY_END(tryname, r_mtvec, r_mepc, r_mcause, r_mstatus) \
tryname##_ok : ;\
csrw mtvec, r_mtvec ;\
csrw mepc, r_mepc ;\
csrw mcause, r_mcause ;\
csrw mstatus, r_mstatus
#define SET_MPRV(reg) \
li reg, 1 ;\
slli reg, reg, 17 ;\
csrs mstatus, reg
#define UNSET_MPRV(reg) \
li reg, 1 ;\
slli reg, reg, 17 ;\
csrc mstatus, reg
.global copy_block_to_sm
copy_block_to_sm:
# Arguments:
# a0: dst
# a1: src
# Temporaries
# a2: mtvec
# a3: mepc
# a4: mcause
# a5: mstatus
TRY(copy_block_in_check, a2, a3, a4, a5)
SET_MPRV(t0)
LOAD t0, 0*REGBYTES(a1)
LOAD t1, 1*REGBYTES(a1)
LOAD t2, 2*REGBYTES(a1)
LOAD t3, 3*REGBYTES(a1)
LOAD t4, 4*REGBYTES(a1)
LOAD t5, 5*REGBYTES(a1)
LOAD t6, 6*REGBYTES(a1)
LOAD a6, 7*REGBYTES(a1)
UNSET_MPRV(a1)
STORE t0, 0*REGBYTES(a0)
STORE t1, 1*REGBYTES(a0)
STORE t2, 2*REGBYTES(a0)
STORE t3, 3*REGBYTES(a0)
STORE t4, 4*REGBYTES(a0)
STORE t5, 5*REGBYTES(a0)
STORE t6, 6*REGBYTES(a0)
STORE a6, 7*REGBYTES(a0)
li a0, 0
CATCH_ERR(copy_block_in_check, a1)
li a0, -1
TRY_END(copy_block_in_check, a2, a3, a4, a5)
UNSET_MPRV(t0)
ret
.global copy_word_to_sm
copy_word_to_sm:
# a0: dst
# a1: src
TRY(copy_word_in_check, t1, t2, t3, t4)
SET_MPRV(t5)
LOAD t0, 0x00(a1)
UNSET_MPRV(t5)
STORE t0, 0x00(a0)
li a0, 0
CATCH_ERR(copy_word_in_check, t5)
li a0, -1
TRY_END(copy_word_in_check, t1, t2, t3, t4)
UNSET_MPRV(t5)
ret
.global copy1_to_sm
copy1_to_sm:
# a0: dst
# a1: src
TRY(copy1in_check, t1, t2, t3, t4)
SET_MPRV(t5)
lb t0, 0x00(a1)
UNSET_MPRV(t5)
sb t0, 0x00(a0)
li a0, 0
CATCH_ERR(copy1in_check, t5)
li a0, -1
TRY_END(copy1in_check, t1, t2, t3, t4)
UNSET_MPRV(t5)
ret
.global copy_block_from_sm
copy_block_from_sm:
# Arguments:
# a0: dst
# a1: src
# Temporaries
# a2: mtvec
# a3: mepc
# a4: mcause
# a5: mstatus
TRY(copy_block_out_check, a2, a3, a4, a5)
LOAD t0, 0*REGBYTES(a1)
LOAD t1, 1*REGBYTES(a1)
LOAD t2, 2*REGBYTES(a1)
LOAD t3, 3*REGBYTES(a1)
LOAD t4, 4*REGBYTES(a1)
LOAD t5, 5*REGBYTES(a1)
LOAD t6, 6*REGBYTES(a1)
LOAD a6, 7*REGBYTES(a1)
SET_MPRV(a1)
STORE t0, 0*REGBYTES(a0)
STORE t1, 1*REGBYTES(a0)
STORE t2, 2*REGBYTES(a0)
STORE t3, 3*REGBYTES(a0)
STORE t4, 4*REGBYTES(a0)
STORE t5, 5*REGBYTES(a0)
STORE t6, 6*REGBYTES(a0)
STORE a6, 7*REGBYTES(a0)
UNSET_MPRV(a1)
li a0, 0
CATCH_ERR(copy_block_out_check, a1)
li a0, -1
TRY_END(copy_block_out_check, a2, a3, a4, a5)
UNSET_MPRV(a1)
ret
.global copy_word_from_sm
copy_word_from_sm:
# a0: dst
# a1: src
TRY(copy_word_out_check, t1, t2, t3, t4)
LOAD t0, 0x00(a1)
SET_MPRV(t5)
STORE t0, 0x00(a0)
li a0, 0
CATCH_ERR(copy_word_out_check, t5)
li a0, -1
TRY_END(copy_word_out_check, t1, t2, t3, t4)
UNSET_MPRV(t5)
ret
.global copy1_from_sm
copy1_from_sm:
# a0: dst
# a1: src
TRY(copy1out_check, t1, t2, t3, t4)
lb t0, 0x00(a1)
SET_MPRV(t5)
sb t0, 0x00(a0)
li a0, 0
CATCH_ERR(copy1out_check, t5)
li a0, -1
TRY_END(copy1out_check, t1, t2, t3, t4)
UNSET_MPRV(t5)
ret