Skip to content

Commit 9e80635

Browse files
Christoph Hellwigpaul-walmsley-sifive
authored andcommitted
riscv: clear the instruction cache and all registers when booting
When we get booted we want a clear slate without any leaks from previous supervisors or the firmware. Flush the instruction cache and then clear all registers to known good values. This is really important for the upcoming nommu support that runs on M-mode, but can't really harm when running in S-mode either. Vaguely based on the concepts from opensbi. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Paul Walmsley <[email protected]>
1 parent accb9db commit 9e80635

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

arch/riscv/include/asm/csr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#define CSR_SATP 0x180
9393

9494
#define CSR_MSTATUS 0x300
95+
#define CSR_MISA 0x301
9596
#define CSR_MIE 0x304
9697
#define CSR_MTVEC 0x305
9798
#define CSR_MSCRATCH 0x340

arch/riscv/kernel/head.S

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <asm/thread_info.h>
1212
#include <asm/page.h>
1313
#include <asm/csr.h>
14+
#include <asm/hwcap.h>
1415
#include <asm/image.h>
1516

1617
__INIT
@@ -51,12 +52,18 @@ _start_kernel:
5152
csrw CSR_IP, zero
5253

5354
#ifdef CONFIG_RISCV_M_MODE
55+
/* flush the instruction cache */
56+
fence.i
57+
58+
/* Reset all registers except ra, a0, a1 */
59+
call reset_regs
60+
5461
/*
5562
* The hartid in a0 is expected later on, and we have no firmware
5663
* to hand it to us.
5764
*/
5865
csrr a0, CSR_MHARTID
59-
#endif
66+
#endif /* CONFIG_RISCV_M_MODE */
6067

6168
/* Load the global pointer */
6269
.option push
@@ -203,6 +210,85 @@ relocate:
203210
j .Lsecondary_park
204211
END(_start)
205212

213+
#ifdef CONFIG_RISCV_M_MODE
214+
ENTRY(reset_regs)
215+
li sp, 0
216+
li gp, 0
217+
li tp, 0
218+
li t0, 0
219+
li t1, 0
220+
li t2, 0
221+
li s0, 0
222+
li s1, 0
223+
li a2, 0
224+
li a3, 0
225+
li a4, 0
226+
li a5, 0
227+
li a6, 0
228+
li a7, 0
229+
li s2, 0
230+
li s3, 0
231+
li s4, 0
232+
li s5, 0
233+
li s6, 0
234+
li s7, 0
235+
li s8, 0
236+
li s9, 0
237+
li s10, 0
238+
li s11, 0
239+
li t3, 0
240+
li t4, 0
241+
li t5, 0
242+
li t6, 0
243+
csrw sscratch, 0
244+
245+
#ifdef CONFIG_FPU
246+
csrr t0, CSR_MISA
247+
andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)
248+
bnez t0, .Lreset_regs_done
249+
250+
li t1, SR_FS
251+
csrs CSR_STATUS, t1
252+
fmv.s.x f0, zero
253+
fmv.s.x f1, zero
254+
fmv.s.x f2, zero
255+
fmv.s.x f3, zero
256+
fmv.s.x f4, zero
257+
fmv.s.x f5, zero
258+
fmv.s.x f6, zero
259+
fmv.s.x f7, zero
260+
fmv.s.x f8, zero
261+
fmv.s.x f9, zero
262+
fmv.s.x f10, zero
263+
fmv.s.x f11, zero
264+
fmv.s.x f12, zero
265+
fmv.s.x f13, zero
266+
fmv.s.x f14, zero
267+
fmv.s.x f15, zero
268+
fmv.s.x f16, zero
269+
fmv.s.x f17, zero
270+
fmv.s.x f18, zero
271+
fmv.s.x f19, zero
272+
fmv.s.x f20, zero
273+
fmv.s.x f21, zero
274+
fmv.s.x f22, zero
275+
fmv.s.x f23, zero
276+
fmv.s.x f24, zero
277+
fmv.s.x f25, zero
278+
fmv.s.x f26, zero
279+
fmv.s.x f27, zero
280+
fmv.s.x f28, zero
281+
fmv.s.x f29, zero
282+
fmv.s.x f30, zero
283+
fmv.s.x f31, zero
284+
csrw fcsr, 0
285+
/* note that the caller must clear SR_FS */
286+
#endif /* CONFIG_FPU */
287+
.Lreset_regs_done:
288+
ret
289+
END(reset_regs)
290+
#endif /* CONFIG_RISCV_M_MODE */
291+
206292
__PAGE_ALIGNED_BSS
207293
/* Empty zero page */
208294
.balign PAGE_SIZE

0 commit comments

Comments
 (0)