kernel: revamp IRQ handling

This commit is contained in:
eupn 2023-04-05 16:35:01 +04:00
parent 1ac31cf930
commit 90df83f885
No known key found for this signature in database
GPG key ID: 6A550BE33BE768A9
5 changed files with 101 additions and 50 deletions

View file

@ -26,11 +26,12 @@ xous-kernel = { package = "xous", version = "0.9.37", features = [
riscv = { version = "0.5.6", package = "xous-riscv" } # source is in "../imports/riscv-0.5.6" riscv = { version = "0.5.6", package = "xous-riscv" } # source is in "../imports/riscv-0.5.6"
[target.'cfg(target_arch = "arm")'.dependencies] [target.'cfg(target_arch = "arm")'.dependencies]
armv7 = { git = "https://github.com/Foundation-Devices/armv7.git", branch = "update" } armv7 = { git = "https://github.com/Foundation-Devices/armv7.git", branch = "update", features = ["critical-section"] }
atsama5d27 = { git = "https://github.com/Foundation-Devices/atsama5d27.git", branch = "master" } atsama5d27 = { git = "https://github.com/Foundation-Devices/atsama5d27.git", branch = "master" }
xous-kernel = { package = "xous", version = "0.9.37", features = [ xous-kernel = { package = "xous", version = "0.9.37", features = [
"v2p", "v2p",
] } ] }
critical-section = "1.1.1"
[features] [features]
atsama5d27 = ["utralib/atsama5d27"] atsama5d27 = ["utralib/atsama5d27"]

Binary file not shown.

View file

@ -22,6 +22,10 @@
#define I_BIT 0x80 #define I_BIT 0x80
#define F_BIT 0x40 #define F_BIT 0x40
#define EXCEPTION_STACK_TOP 0xffff0000
#define IRQ_STACK_TOP 0xfffd0000
#define THREAD_CONTEXT_AREA 0xff804000
.section .text.reset_vector, "ax" .section .text.reset_vector, "ax"
.global reset .global reset
@ -36,8 +40,8 @@ _exception_vectors_kernel:
b trap_handler /* Prefetch Abort */ b trap_handler /* Prefetch Abort */
b trap_handler /* Data Abort */ b trap_handler /* Data Abort */
.word 0 /* Empty */ .word 0 /* Empty */
b trap_handler /* IRQ : read the AIC */ b irq_handler /* IRQ : read the AIC */
b trap_handler /* FIQ */ b fiq_handler /* FIQ */
undef_vector: undef_vector:
bkpt /* Issue debugger breakpoint */ bkpt /* Issue debugger breakpoint */
@ -51,6 +55,21 @@ _branch_main:
ldr r4, =_exception_vectors_kernel ldr r4, =_exception_vectors_kernel
mcr p15, 0, r4, c12, c0, 0 mcr p15, 0, r4, c12, c0, 0
/* Clear all faults if any */
mov ip, #0
mcr p15, 0, ip, c6, c0, 0
mcr p15, 0, ip, c6, c0, 2
mcr p15, 0, ip, c5, c0, 0
mcr p15, 0, ip, c5, c0, 1
/* Configure IRQ/FIQ stack pointers */
msr cpsr_c, #ARM_MODE_IRQ
ldr sp, =(IRQ_STACK_TOP)
msr cpsr_c, #ARM_MODE_FIQ
ldr sp, =(IRQ_STACK_TOP)
msr cpsr_c, #ARM_MODE_SVC
/* Note that registers r0-r3 still contain the kernel arguments */ /* Note that registers r0-r3 still contain the kernel arguments */
bl init bl init
bl kmain bl kmain
@ -60,18 +79,18 @@ _branch_main:
Syscall/IRQ/Abort trap handler. Syscall/IRQ/Abort trap handler.
Saves all process context into a structure that is Saves all process context into a structure that is
mapped to each thread at 0xff804000 (THREAD_CONTEXT_AREA). mapped to each thread at THREAD_CONTEXT_AREA.
Return happens elsewhere. Return happens elsewhere.
*/ */
trap_handler: trap_handler:
ldr ip, =(0xff804000) ldr ip, =(THREAD_CONTEXT_AREA)
# If this is a syscall, then r0 contains a pointer to the syscall arguments structure # If this is a syscall, then r0 contains a pointer to the syscall arguments structure
# Due to it being clobbered by this routine's code, we have save it into the scratch area for future use # Due to it being clobbered by this routine's code, we have save it into the scratch area for future use
str r0, [ip] /* Preserve r0 in the scratch field */ str r0, [ip] /* Preserve r0 in the scratch field */
ldr r0, [ip, #4] /* Load current context number */ ldr r0, [ip, #4] /* Load current context number */
lsl r0, #7 /* Multiply by 32 */ lsl r0, #7 /* Multiply by 32 */
add ip, ip, r0 /* ip = 0xff804000 + (context_number * 32) */ add ip, ip, r0 /* ip = THREAD_CONTEXT_AREA + (context_number * 32) */
# If the trap has originated in the user/system mode, then we need to store # If the trap has originated in the user/system mode, then we need to store
# the user/system mode registers that are currently banked out # the user/system mode registers that are currently banked out
@ -82,7 +101,7 @@ trap_handler:
cmp r0, #ARM_MODE_USR /* Compare with 'User' mode */ cmp r0, #ARM_MODE_USR /* Compare with 'User' mode */
beq _store_banked /* Save banked out user registers if coming from User mode */ beq _store_banked /* Save banked out user registers if coming from User mode */
ldr r0, =(0xff804000) /* Restore r0 from the scratch field */ ldr r0, =(THREAD_CONTEXT_AREA) /* Restore r0 from the scratch field */
ldr r0, [r0] ldr r0, [r0]
stm ip, {r0 - r15} /* Save all the registers */ stm ip, {r0 - r15} /* Save all the registers */
@ -95,7 +114,7 @@ _store_banked:
str r0, [ip] str r0, [ip]
sub ip, ip, #(17 * 4) sub ip, ip, #(17 * 4)
ldr r0, =(0xff804000) /* Restore r0 from the scratch field */ ldr r0, =(THREAD_CONTEXT_AREA) /* Restore r0 from the scratch field */
ldr r0, [r0] ldr r0, [r0]
stm ip, {r0 - r7} /* Save the common registers */ stm ip, {r0 - r7} /* Save the common registers */
@ -108,8 +127,8 @@ _skip_store_banked:
str lr, [ip] str lr, [ip]
sub ip, #(4 * 19) sub ip, #(4 * 19)
# Set a default exception/irq stack pointer (EXCEPTION_STACK_TOP) # Set a default stack pointer
ldr sp, =(0xffff0000) ldr sp, =(EXCEPTION_STACK_TOP)
# Choose where to go next depending on the current mode: SVC (syscall), abort or IRQ # Choose where to go next depending on the current mode: SVC (syscall), abort or IRQ
mrs ip, cpsr /* Access the current mode register */ mrs ip, cpsr /* Access the current mode register */
@ -118,10 +137,6 @@ _skip_store_banked:
bleq _swi_handler_rust /* Jump to SVC handler */ bleq _swi_handler_rust /* Jump to SVC handler */
cmp ip, #ARM_MODE_ABT /* Compare with 'Abort' mode */ cmp ip, #ARM_MODE_ABT /* Compare with 'Abort' mode */
bleq _abort_handler_rust /* Jump to abort handler */ bleq _abort_handler_rust /* Jump to abort handler */
cmp ip, #ARM_MODE_IRQ /* Compare with 'IRQ' mode */
bleq irq_handler /* Jump to IRQ handler */
cmp ip, #ARM_MODE_FIQ /* Compare with 'FIQ' mode */
bleq fiq_handler /* Jump to FIQ handler */
# Any other unhandled exception goes here # Any other unhandled exception goes here
bkpt bkpt
@ -129,14 +144,49 @@ _skip_store_banked:
irq_handler: irq_handler:
fiq_handler: fiq_handler:
# Disable further IRQs /* Save interrupt context on stack to allow nesting */
mrs ip, cpsr sub lr, lr, #4
orr ip, ip, #I_BIT | F_BIT /* Disable interrupts */ stmfd sp!, {lr}
msr cpsr, ip stmfd sp, {r0 - r12, sp, lr}^
sub sp, sp, #(4 * 15)
mrs lr, spsr
stmfd sp!, {lr}
mrc p15, 0, lr, c13, c0, 2 /* Read hardware thread pointer register */
stmfd sp!, {lr}
mrc p15, 0, lr, c2, c0, 0 /* Read TTBR0 */
stmfd sp!, {lr}
mrc p15, 0, lr, c13, c0, 1 /* Read CONTEXTIDR */
stmfd sp!, {lr}
mrc p15, 0, lr, c13, c0, 3 /* Read thread resume address scratch register */
stmfd sp!, {lr}
bl _irq_handler_rust /* Handle IRQs in supervisor mode (IRQs disabled) */
bkpt msr cpsr_c, #(ARM_MODE_SVC | I_BIT | F_BIT)
b . # Set a default stack pointer
ldr sp, =(EXCEPTION_STACK_TOP)
b _irq_handler_rust
.global irq_handler_return
irq_handler_return:
/* As we're returning from ABORT mode, go back to IRQ mode (but with interrupts disabled) */
msr cpsr_c, #(ARM_MODE_IRQ | I_BIT | F_BIT)
/* Restore interrupt context and branch back to calling code */
ldmia sp!, {lr}
mcr p15, 0, lr, c13, c0, 3 /* Set thread resume address scratch register */
ldmia sp!, {lr}
mcr p15, 0, lr, c13, c0, 1 /* Set CONTEXTIDR */
ldmia sp!, {lr}
mcr p15, 0, lr, c2, c0, 0 /* Set TTBR0 */
bl flush_mmu
ldmia sp!, {lr}
mcr p15, 0, lr, c13, c0, 2 /* Set hardware thread pointer register */
ldmia sp!, {lr}
msr spsr_cxsf, lr
ldmia sp, {r0 - r12, sp, lr}^
add sp, sp, #(4 * 15)
ldmia sp!, {pc}^
/* /*
A thread resume routine. A thread resume routine.
@ -149,11 +199,11 @@ _resume_trampoline:
ldm ip, {sp, lr}^ ldm ip, {sp, lr}^
# Switch privilege modes. # Switch privilege modes.
# Interrupts have to be disabled if we're in the IRQ mode because the code we're resuming to # Keep interrupts disabled if we're going to switch to the user mode IRQ handler (r2 != 0) to prevent
# will have to service the interrupt source first before allowing more interrupts # endless interrupt firing loop
mrs ip, cpsr /* Access the current state register */ mrs ip, cpsr /* Access the current state register */
and ip, ip, #0x1f /* Extract the mode field */ and ip, ip, #0x1f /* Extract the mode field */
cmp ip, #ARM_MODE_IRQ cmp r2, #0 /* r2 contains a "keep IRQs enabled" flag */
mrs ip, spsr mrs ip, spsr
bic ip, ip, #I_BIT | F_BIT /* Allow interrupts. They'll stay enabled unless we're in IRQ mode */ bic ip, ip, #I_BIT | F_BIT /* Allow interrupts. They'll stay enabled unless we're in IRQ mode */
blne skip_disabling_interrupts blne skip_disabling_interrupts

View file

@ -14,6 +14,7 @@ use crate::SystemServices;
extern "Rust" { extern "Rust" {
fn _xous_syscall_return_result(context: &Thread) -> !; fn _xous_syscall_return_result(context: &Thread) -> !;
fn irq_handler_return() -> !;
} }
static mut PREVIOUS_PAIR: Option<(PID, TID)> = None; static mut PREVIOUS_PAIR: Option<(PID, TID)> = None;
@ -240,16 +241,7 @@ pub extern "C" fn abort_handler() {
// Resume the new thread within the same process. // Resume the new thread within the same process.
ArchProcess::with_current_mut(|p| { ArchProcess::with_current_mut(|p| {
// Adjust the program counter by the amount returned by the exception handler
/* FIXME let pc_adjust = a0 as isize;
if pc_adjust < 0 {
p.current_thread_mut().sepc -= pc_adjust.abs() as usize;
} else {
p.current_thread_mut().sepc += pc_adjust.abs() as usize;
}*/
clear_fault(); clear_fault();
crate::arch::syscall::resume(pid.get() == 1, p.current_thread()) crate::arch::syscall::resume(pid.get() == 1, p.current_thread())
}); });
} }
@ -272,7 +264,7 @@ pub extern "C" fn abort_handler() {
}); });
} }
RETURN_FROM_ISR if dfar == 0 => { RETURN_FROM_ISR => {
// If we hit this address, then an ISR has just returned. Since // If we hit this address, then an ISR has just returned. Since
// we're in an interrupt context, it is safe to access this // we're in an interrupt context, it is safe to access this
// global variable. // global variable.
@ -294,12 +286,9 @@ pub extern "C" fn abort_handler() {
clear_fault(); clear_fault();
ArchProcess::with_current_mut(|process| { unsafe {
let mut curr_thread = process.current_thread_mut(); irq_handler_return();
curr_thread.resume_addr -= 4; }
curr_thread.ret_addr = 0;
crate::arch::syscall::resume(current_pid().get() == 1, curr_thread)
});
} }
_ => { _ => {
@ -360,15 +349,18 @@ pub extern "C" fn _irq_handler_rust() {
}); });
klog!("Pending irqs mask: {:032b}", irqs_pending); klog!("Pending irqs mask: {:032b}", irqs_pending);
// Safe to access globals since interrupts are disabled critical_section::with(|_| {
unsafe { // Safe to access globals since interrupts are disabled (critical section)
if PREVIOUS_PAIR.is_none() { unsafe {
let tid = crate::arch::process::current_tid(); if PREVIOUS_PAIR.is_none() {
set_isr_return_pair(pid, tid); let tid = crate::arch::process::current_tid();
set_isr_return_pair(pid, tid);
}
} }
} });
crate::irq::handle(irqs_pending).expect("Couldn't handle IRQ"); crate::irq::handle(irqs_pending).expect("Couldn't handle IRQ");
ArchProcess::with_current_mut(|process| { ArchProcess::with_current_mut(|process| {
crate::arch::syscall::resume(current_pid().get() == 1, process.current_thread()) crate::arch::syscall::resume_no_irqs(current_pid().get() == 1, process.current_thread())
}) })
} }

View file

@ -4,7 +4,7 @@
use crate::services::Thread; use crate::services::Thread;
extern "C" { extern "C" {
fn _resume_trampoline(mode: usize, thread: &Thread) -> !; fn _resume_trampoline(mode: usize, thread: &Thread, enable_irqs: bool) -> !;
} }
pub fn invoke( pub fn invoke(
@ -37,8 +37,16 @@ pub fn invoke(
} }
pub fn resume(supervisor: bool, thread: &Thread) -> ! { pub fn resume(supervisor: bool, thread: &Thread) -> ! {
resume_inner(supervisor, thread, true)
}
pub fn resume_no_irqs(supervisor: bool, thread: &Thread) -> ! {
resume_inner(supervisor, thread, false)
}
fn resume_inner(supervisor: bool, thread: &Thread, enable_irqs: bool) -> ! {
// Restore thread stack and PC, pass resume arguments via r0-r4 // Restore thread stack and PC, pass resume arguments via r0-r4
klog!("resume: setting sp={:08x}, res_addr={:08x}, lr={:08x} | privileged: {:?} | ret: {:08x}", thread.sp, thread.resume_addr, thread.lr, supervisor, thread.ret_addr); klog!("resume: setting sp={:08x}, res_addr={:08x}, lr={:08x} | privileged: {:?} | irqs: {:?} | ret: {:08x}", thread.sp, thread.resume_addr, thread.lr, supervisor, enable_irqs, thread.ret_addr);
// See ARM ARM // See ARM ARM
// B1.3.1 ARM processor modes // B1.3.1 ARM processor modes
@ -49,6 +57,6 @@ pub fn resume(supervisor: bool, thread: &Thread) -> ! {
}; };
unsafe { unsafe {
_resume_trampoline(mode, thread); _resume_trampoline(mode, thread, enable_irqs);
} }
} }