Browse files

arch/riscv/kernel/head.S: Detect M-mode and switch to S-mode

Signed-off-by: Jonathan Neuschäfer <>
  • Loading branch information...
neuschaefer committed Aug 7, 2016
1 parent 9faea5d commit 84a4661e71cb7ca18d3c90a6559bbca8cb4927a9
Showing with 24 additions and 0 deletions.
  1. +24 −0 arch/riscv/kernel/head.S
@@ -9,7 +9,31 @@
* Before anything else, try to detect if we've been started in machine
* mode. We assume that the kernel image is loaded at
* 0xffffffff80000000 in virtual memory and an address with leading
* zeroes in physical memory.
* This heuristic won't work on RV32, if both the virtual and the
* physical address of the kernel are >= 0x80000000.
auipc t0, 0
bltz t0, .Lvirtual_entry
/* Jump to 0xffffffff80000000 in S-mode */
li t0, 0xffffffff80000000
csrw mepc, t0
csrr t0, mstatus
li t1, ~(3<<11)
and t0, t0, t1
li t2, (1<<11)
or t0, t0, t2
csrw mstatus, t0
/* Load the global pointer (before any use of la) */
1: auipc gp, %pcrel_hi(_gp)
addi gp, gp, %pcrel_lo(1b)

0 comments on commit 84a4661

Please sign in to comment.