forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
arch: riscv: core: run zephyr completely in user/supervisor mode
Before that fix, zephyr was able to start only in machine mode. With that fix, zephyr can start (as guest) in user or supervisor mode. Defined new board type qemu_riscv32/qemu_virt_riscv32/opensbi Drivers for running zephyr in supervisor with qemu included. Fixes zephyrproject-rtos#68133 Signed-off-by: Sven Ginka <[email protected]> Signed-off-by: Sven Ginka <[email protected]>
- Loading branch information
Showing
39 changed files
with
1,028 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
* Copyright (c) 2016 Jean-Paul Etienne <[email protected]> | ||
* Copyright (c) 2018 Foundries.io Ltd | ||
* Copyright (c) 2020 BayLibre, SAS | ||
* Copyright (c) 2024 sensry.io | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
@@ -189,12 +190,12 @@ SECTION_FUNC(exception.entry, _isr_wrapper) | |
get_current_cpu s0 | ||
|
||
/* Save MEPC register */ | ||
csrr t0, mepc | ||
sr t0, __struct_arch_esf_mepc_OFFSET(sp) | ||
csrr t0, xepc | ||
sr t0, __struct_arch_esf_xepc_OFFSET(sp) | ||
|
||
/* Save MSTATUS register */ | ||
csrr t2, mstatus | ||
sr t2, __struct_arch_esf_mstatus_OFFSET(sp) | ||
csrr t2, xstatus | ||
sr t2, __struct_arch_esf_xstatus_OFFSET(sp) | ||
|
||
#if defined(CONFIG_FPU_SHARING) | ||
/* determine if FPU access was disabled */ | ||
|
@@ -319,7 +320,7 @@ no_fp: /* increment _current->arch.exception_depth */ | |
jal ra, __soc_is_irq | ||
bnez a0, is_interrupt | ||
#else | ||
csrr t0, mcause | ||
csrr t0, xcause | ||
srli t0, t0, RISCV_MCAUSE_IRQ_POS | ||
bnez t0, is_interrupt | ||
#endif | ||
|
@@ -329,7 +330,7 @@ no_fp: /* increment _current->arch.exception_depth */ | |
* perform a context-switch or an IRQ offload. Otherwise call _Fault | ||
* to report the exception. | ||
*/ | ||
csrr t0, mcause | ||
csrr t0, xcause | ||
li t2, CONFIG_RISCV_MCAUSE_EXCEPTION_MASK | ||
and t0, t0, t2 | ||
|
||
|
@@ -385,9 +386,9 @@ is_kernel_syscall: | |
* It's safe to always increment by 4, even with compressed | ||
* instructions, because the ecall instruction is always 4 bytes. | ||
*/ | ||
lr t0, __struct_arch_esf_mepc_OFFSET(sp) | ||
lr t0, __struct_arch_esf_xepc_OFFSET(sp) | ||
addi t0, t0, 4 | ||
sr t0, __struct_arch_esf_mepc_OFFSET(sp) | ||
sr t0, __struct_arch_esf_xepc_OFFSET(sp) | ||
|
||
#ifdef CONFIG_PMP_STACK_GUARD | ||
/* Re-activate PMP for m-mode */ | ||
|
@@ -505,9 +506,9 @@ is_user_syscall: | |
* Same as for is_kernel_syscall: increment saved MEPC by 4 to | ||
* prevent triggering the same ecall again upon exiting the ISR. | ||
*/ | ||
lr t1, __struct_arch_esf_mepc_OFFSET(sp) | ||
lr t1, __struct_arch_esf_xepc_OFFSET(sp) | ||
addi t1, t1, 4 | ||
sr t1, __struct_arch_esf_mepc_OFFSET(sp) | ||
sr t1, __struct_arch_esf_xepc_OFFSET(sp) | ||
|
||
/* Restore argument registers from user stack */ | ||
lr a0, __struct_arch_esf_a0_OFFSET(sp) | ||
|
@@ -565,7 +566,7 @@ is_interrupt: | |
* If we came from userspace then we need to reconfigure the | ||
* PMP for kernel mode stack guard. | ||
*/ | ||
lr t0, __struct_arch_esf_mstatus_OFFSET(sp) | ||
lr t0, __struct_arch_esf_xstatus_OFFSET(sp) | ||
li t1, MSTATUS_MPP | ||
and t0, t0, t1 | ||
bnez t0, 1f | ||
|
@@ -609,7 +610,7 @@ on_irq_stack: | |
#endif | ||
|
||
/* Get IRQ causing interrupt */ | ||
csrr a0, mcause | ||
csrr a0, xcause | ||
li t0, CONFIG_RISCV_MCAUSE_EXCEPTION_MASK | ||
and a0, a0, t0 | ||
|
||
|
@@ -714,10 +715,10 @@ fp_trap_exit: | |
#endif | ||
|
||
/* Restore MEPC and MSTATUS registers */ | ||
lr t0, __struct_arch_esf_mepc_OFFSET(sp) | ||
lr t2, __struct_arch_esf_mstatus_OFFSET(sp) | ||
csrw mepc, t0 | ||
csrw mstatus, t2 | ||
lr t0, __struct_arch_esf_xepc_OFFSET(sp) | ||
lr t2, __struct_arch_esf_xstatus_OFFSET(sp) | ||
csrw xepc, t0 | ||
csrw xstatus, t2 | ||
|
||
#ifdef CONFIG_USERSPACE | ||
/* | ||
|
@@ -775,4 +776,4 @@ fp_trap_exit: | |
|
||
#endif /* CONFIG_RISCV_SOC_HAS_ISR_STACKING */ | ||
|
||
mret | ||
xret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ board: | |
- name: qemu_virt_riscv32 | ||
variants: | ||
- name: smp | ||
- name: opensbi |
39 changes: 39 additions & 0 deletions
39
boards/qemu/riscv32/qemu_riscv32_qemu_virt_riscv32_opensbi.dts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2024 sensry.io | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/dts-v1/; | ||
|
||
#include <qemu/virt-riscv32.dtsi> | ||
|
||
/ { | ||
console0: console { | ||
compatible = "opensbi,console-uart"; | ||
status = "okay"; | ||
}; | ||
|
||
chosen { | ||
zephyr,console = &console0; | ||
zephyr,shell-uart = &console0; | ||
zephyr,sram = &ram0; | ||
}; | ||
}; | ||
|
||
&ram0 { | ||
device_type = "memory"; | ||
reg = < 0x80400000 0x100000 >; | ||
}; | ||
|
||
&uart0 { | ||
status = "disabled"; | ||
}; | ||
|
||
&plic { | ||
status = "disabled"; | ||
}; | ||
|
||
&clint { | ||
status = "disabled"; | ||
}; |
15 changes: 15 additions & 0 deletions
15
boards/qemu/riscv32/qemu_riscv32_qemu_virt_riscv32_opensbi.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
identifier: qemu_riscv32/qemu_virt_riscv32/opensbi | ||
name: QEMU Emulation for RISC-V 32-bit openSBI | ||
type: qemu | ||
simulation: qemu | ||
arch: riscv | ||
toolchain: | ||
- zephyr | ||
- xtools | ||
supported: | ||
- netif | ||
testing: | ||
default: true | ||
ignore_tags: | ||
- net | ||
- bluetooth |
32 changes: 32 additions & 0 deletions
32
boards/qemu/riscv32/qemu_riscv32_qemu_virt_riscv32_opensbi_defconfig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright (c) 2024 sensry.io | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
CONFIG_CONSOLE=y | ||
CONFIG_SERIAL=y | ||
CONFIG_UART_CONSOLE=y | ||
CONFIG_STACK_SENTINEL=y | ||
CONFIG_XIP=n | ||
CONFIG_RISCV_PMP=y | ||
CONFIG_SMP=y | ||
CONFIG_MP_MAX_NUM_CPUS=1 | ||
CONFIG_IDLE_STACK_SIZE=1024 | ||
CONFIG_QEMU_ICOUNT=n | ||
|
||
# enable the openSBI support, requires a compiled version of openSBI binary in machine mode | ||
CONFIG_RISCV_OPENSBI=y | ||
|
||
# we run zephyr in supervisor mode | ||
CONFIG_RISCV_KERNEL_IN_SUPERVISOR_MODE=y | ||
CONFIG_RISCV_MACHINE_TIMER=n | ||
|
||
# options that have to be disabled by now | ||
CONFIG_USERSPACE=n | ||
CONFIG_RISCV_HAS_HART_ID=n | ||
CONFIG_SMP=n | ||
CONFIG_RISCV_PMP=n | ||
|
||
# build output for qemu | ||
CONFIG_BUILD_OUTPUT_BIN=y | ||
|
||
|
||
CONFIG_THREAD_MONITOR=n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -280,4 +280,6 @@ rsource "Kconfig.rzt2m" | |
|
||
rsource "Kconfig.renesas_ra8" | ||
|
||
rsource "Kconfig.opensbi_console" | ||
|
||
endif # SERIAL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# openSBI Console UART driver configuration options | ||
|
||
# Copyright (c) 2024 sensry.io | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
if RISCV_OPENSBI | ||
|
||
config UART_OPENSBI_CONSOLE | ||
bool "openSBI console serial driver" | ||
default y | ||
select SERIAL_HAS_DRIVER | ||
help | ||
This option enables the console driver for openSBI based RISCV family of | ||
processors. | ||
|
||
endif # RISCV_OPENSBI |
Oops, something went wrong.