Page 1 of 1

How to switch from HYP mode to SVC mode on Raspberry Pi 3 (AArch32)?

Posted: Thu Jul 03, 2025 1:31 pm
by lanpi
Hi,
I'm building my own RTOS from scratch for the Raspberry Pi 3 Model B (v1.2) in AArch32 mode.
My current focus is getting IRQ mode and ARM timer interrupts working.

Here is my setup:
  • I’m using AArch32 (not 64-bit).
  • My `config.txt` is:

Code: Select all

arm_64bit=0
kernel=kernel7.img
enable_uart=1
device_tree=
  • LED control via GPIO and UART output is working correctly.
  • In `step1_UART/`, I implemented basic UART and GPIO drivers.
  • In `step2_ARMTimet_IRQ/`, I’m working on ARM timer interrupt handling.
  • The issue described in this post occurs in the `step2_ARMTimet_IRQ/` directory.
The problem occurs when I try to switch CPU modes in `start.S` using the following code:

Code: Select all

 // Switch to IRQ mode and set IRQ stack
 mov r0, #0xD2 // CPSR: IRQ mode, FIQ/IRQ disabled
 msr cpsr_c, r0
 ldr sp, =__stack_top_irq
 // Switch back to Supervisor (SVC) mode and reset SP
 mov r0, #0xD3 // CPSR: SVC mode, FIQ/IRQ disabled
 msr cpsr_c, r0
 ldr sp, =__stack_top
→ This causes a crash.
If I comment out the msr cpsr_c, ... lines, the system proceeds to kernel_main, where I inspected the CPSR value and found it to be 0x6000015A.

From the mode bits (0b11010), I understand the CPU is in HYP mode.
I discovered that writing to CPSR using `msr cpsr_c, ...` doesn't work while in HYP mode.
This is unexpected, since I'm trying to boot into SVC mode in AArch32.

Here is my main question:

How can I safely exit HYP mode and switch to SVC mode early in the boot process?

(Adding kernel_old=1 seems to make things worse — it prevents even my LED/UART output from appearing. )

My goal is to set IRQ stacks using msr cpsr_c, ... and implement timer-based interrupts. But I need to escape HYP mode first. How should I do that?

Any help or clarification on how to properly enter SVC mode would be greatly appreciated.

Thanks in advance!

Re: How to switch from HYP mode to SVC mode on Raspberry Pi 3 (AArch32)?

Posted: Fri Jul 04, 2025 6:23 pm
by cleverca22
https://github.com/littlekernel/lk/comm ... c97031a21a
this is some changes i made to little-kernel ~5 years ago, to solve the same problem

Re: How to switch from HYP mode to SVC mode on Raspberry Pi 3 (AArch32)?

Posted: Sun Jul 06, 2025 7:27 am
by lanpi
Thank you very much! It worked perfectly — I really appreciate your help.
That was exactly what I needed! :D

AltStyle によって変換されたページ (->オリジナル) /