From 90df83f885f0bc3eb70d8ef9014c3e75eac7122a Mon Sep 17 00:00:00 2001 From: eupn Date: Wed, 5 Apr 2023 16:35:01 +0400 Subject: [PATCH 1/6] kernel: revamp IRQ handling --- kernel/Cargo.toml | 3 +- kernel/bin/armv7a-unknown-xous-elf.a | Bin 3080 -> 3456 bytes kernel/src/arch/arm/asm.S | 96 ++++++++++++++++++++------- kernel/src/arch/arm/irq.rs | 38 +++++------ kernel/src/arch/arm/syscall.rs | 14 +++- 5 files changed, 101 insertions(+), 50 deletions(-) diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 32e1c7915..171d73140 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -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" [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" } xous-kernel = { package = "xous", version = "0.9.37", features = [ "v2p", ] } +critical-section = "1.1.1" [features] atsama5d27 = ["utralib/atsama5d27"] diff --git a/kernel/bin/armv7a-unknown-xous-elf.a b/kernel/bin/armv7a-unknown-xous-elf.a index 609d6824d799245a7dd67f8cbd41602ed5f76657..b3ba04fa0684e40c2ca621bad772a22e9bc74d18 100644 GIT binary patch delta 1589 zcmYjRUuaup6hHT0(yncCn@-!A#N4dgh+=bZ+O%7xgCG`*I2$*8Q_|Yrt;MFTH;s(p zNClNWC^)|Q;!7tKd=dwHo&@ntWpxVrFk$VeWEInbpbxHIf9Lk5&4J(f{myqz&iTH3 zzng14%h6As2%fQqh6npI*ORXt_hrT~1}!+F--0tTmNk^k8pgnYWnEtxsr=eXY+BZ% z6m?O=Qw`Tbw5Q*QC8E_Z)Ki6&h1CFsB`$qIjPfNSv4Jje5i>p$vbW?mTMJqgk3)K5B zZ&I%Bp<6lkaAf`5Cfy(3;5fe%uQTgA5sxiDUiA4VOv?Yby2U;2LR0Q`ZE>dqwYO`Q z&-#vB=Z)}M-d_vtE^N4!-;CPG`p28JyjPQ|H#V}>k2Y!IUX7~ZjYPG5ljyuqMhN;x5!u$Rn?nKj{H z>?|>+q>PL?zzYY4_c`E|_$%UyGFo|bHLbL#m9DhXmzHG=HD4>)PRT68o746?rc*90 znU@#JrM{^J$2Q-xoq2nXO3suy=}gVQ6=||mOkbcRHm8xKf_VG;wt@>mxG6-#fV3=i zlHT1ZMZ!ub8af&3ig|gW7_#1p@H85>BI0UV<@w@8HKq)wEitP;E7sJ@(u{~}QEd#B zk3^pqt-Q*r@jJAbfKD~V4)j`6T!j9 z3oF;@^VsW%*ev+sL zjwgtAnGK>5w2miH2afgbA5F%KIF7vBAL*>^B^6LtFQkQ9-R z5k+|cjG18ZVavtPwEbrJQekd(-Y%4uoLk#1_yAVKpRK*E*U;_^3UzVpNJ9G=kK`Y5 z_DFA~iuDw)0|9_<3QxWOVB_BIK=hrmf7@8IagK4xXOL$q8k}qdV6I`C0{E{k2e8#J z1ShX}gE`R{T!L==hzp-W0)Ki3*2sN;m4@bI4s`|#-}n%0?*Yw&dl2wp8SV7^;ouc4 za6|0Mvk5eO!PiWjW2^#r7>gj!zEH)Lwo@J7qMUCUzowY@vMs8e2og<-AKH3*QkeOb KU(UpB*!~Aiv?)CR delta 1325 zcmZ8gO-NKx6h3#}%;-4d_(r3K#-Ep_jDhoJ#+PXrs3o=tt^6q@C=JwCQL1&iLN=@W8p}`_6gyyZ4@ZUotcuoI99v ztiC=PHyY*-ohvtCT+@z5W16PLqZqVUG+JjEU=49&exdQ|bS|;Ej7$hqAq6tX8c^PF z!F_NqJP*z_&gZ~KNn81wPH(6k#akbn7r(0=zO8@h^rq<>sn@bs91#ohfhZFb^0}xG zU2<83+6w8?nnL6aAPPhl!lVsK6|AIEGi$!>YUZ0(UZj>*jMU63UHQDu-;||%tI&}5 z9KSl6l4+uJdKvpts2Za~{PyRQfPuip_*lQ*H`YIhVVJ0Id^pm{c^urqqoN5Nc1Dls zhOWymN;r>sOt01TsH}9hw!XLE?hT%Krq+WnK79}Tj13$CzLEvvTKCy=+oLD04NZ;@ULGEv#B{!y;$otDjwM7X#FA2?Hyq1| zwmGluoYa9s)`4^6{X6y(;Xfe90o=Bk-)F`kM+}h8M9qwtC*OJco;KzQ-24e|g=Y}I zO%&AvdEHy)I*X2ekPp2TiO0xu7YpFL8h4oa1$g!zfJ^Y#R3Tbs)jE;zz?;m%67=|# ztj9W*UO%uk2arxe52r{l&X`r?c3GGU7xAISz%&+Ry9Ku|{0R$ldw5~KR%_w=z^uoI zXPbgv%MQed#&MB-7LM8|LdISx@~5w=<`Z&FXcudT;H=A-*G4I8L3FQOw&qs^*Rhku JY<~AN{{h>Iw$=au diff --git a/kernel/src/arch/arm/asm.S b/kernel/src/arch/arm/asm.S index fd3e361cb..b5159d88a 100644 --- a/kernel/src/arch/arm/asm.S +++ b/kernel/src/arch/arm/asm.S @@ -22,6 +22,10 @@ #define I_BIT 0x80 #define F_BIT 0x40 +#define EXCEPTION_STACK_TOP 0xffff0000 +#define IRQ_STACK_TOP 0xfffd0000 +#define THREAD_CONTEXT_AREA 0xff804000 + .section .text.reset_vector, "ax" .global reset @@ -36,8 +40,8 @@ _exception_vectors_kernel: b trap_handler /* Prefetch Abort */ b trap_handler /* Data Abort */ .word 0 /* Empty */ - b trap_handler /* IRQ : read the AIC */ - b trap_handler /* FIQ */ + b irq_handler /* IRQ : read the AIC */ + b fiq_handler /* FIQ */ undef_vector: bkpt /* Issue debugger breakpoint */ @@ -51,6 +55,21 @@ _branch_main: ldr r4, =_exception_vectors_kernel 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 */ bl init bl kmain @@ -60,18 +79,18 @@ _branch_main: Syscall/IRQ/Abort trap handler. 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. */ 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 # 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 */ ldr r0, [ip, #4] /* Load current context number */ 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 # 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 */ 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] stm ip, {r0 - r15} /* Save all the registers */ @@ -95,7 +114,7 @@ _store_banked: str r0, [ip] 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] stm ip, {r0 - r7} /* Save the common registers */ @@ -108,8 +127,8 @@ _skip_store_banked: str lr, [ip] sub ip, #(4 * 19) - # Set a default exception/irq stack pointer (EXCEPTION_STACK_TOP) - ldr sp, =(0xffff0000) + # Set a default stack pointer + ldr sp, =(EXCEPTION_STACK_TOP) # Choose where to go next depending on the current mode: SVC (syscall), abort or IRQ mrs ip, cpsr /* Access the current mode register */ @@ -118,10 +137,6 @@ _skip_store_banked: bleq _swi_handler_rust /* Jump to SVC handler */ cmp ip, #ARM_MODE_ABT /* Compare with 'Abort' mode */ 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 bkpt @@ -129,14 +144,49 @@ _skip_store_banked: irq_handler: fiq_handler: - # Disable further IRQs - mrs ip, cpsr - orr ip, ip, #I_BIT | F_BIT /* Disable interrupts */ - msr cpsr, ip + /* Save interrupt context on stack to allow nesting */ + sub lr, lr, #4 + stmfd sp!, {lr} + 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 - bkpt - b . + /* Handle IRQs in supervisor mode (IRQs disabled) */ + msr cpsr_c, #(ARM_MODE_SVC | I_BIT | F_BIT) + # 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. @@ -149,11 +199,11 @@ _resume_trampoline: ldm ip, {sp, lr}^ # Switch privilege modes. - # Interrupts have to be disabled if we're in the IRQ mode because the code we're resuming to - # will have to service the interrupt source first before allowing more interrupts + # Keep interrupts disabled if we're going to switch to the user mode IRQ handler (r2 != 0) to prevent + # endless interrupt firing loop mrs ip, cpsr /* Access the current state register */ 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 bic ip, ip, #I_BIT | F_BIT /* Allow interrupts. They'll stay enabled unless we're in IRQ mode */ blne skip_disabling_interrupts diff --git a/kernel/src/arch/arm/irq.rs b/kernel/src/arch/arm/irq.rs index 2d92a48d6..a443eddfb 100644 --- a/kernel/src/arch/arm/irq.rs +++ b/kernel/src/arch/arm/irq.rs @@ -14,6 +14,7 @@ use crate::SystemServices; extern "Rust" { fn _xous_syscall_return_result(context: &Thread) -> !; + fn irq_handler_return() -> !; } 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. 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(); - 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 // we're in an interrupt context, it is safe to access this // global variable. @@ -294,12 +286,9 @@ pub extern "C" fn abort_handler() { clear_fault(); - ArchProcess::with_current_mut(|process| { - let mut curr_thread = process.current_thread_mut(); - curr_thread.resume_addr -= 4; - curr_thread.ret_addr = 0; - crate::arch::syscall::resume(current_pid().get() == 1, curr_thread) - }); + unsafe { + irq_handler_return(); + } } _ => { @@ -360,15 +349,18 @@ pub extern "C" fn _irq_handler_rust() { }); klog!("Pending irqs mask: {:032b}", irqs_pending); - // Safe to access globals since interrupts are disabled - unsafe { - if PREVIOUS_PAIR.is_none() { - let tid = crate::arch::process::current_tid(); - set_isr_return_pair(pid, tid); + critical_section::with(|_| { + // Safe to access globals since interrupts are disabled (critical section) + unsafe { + if PREVIOUS_PAIR.is_none() { + let tid = crate::arch::process::current_tid(); + set_isr_return_pair(pid, tid); + } } - } + }); + crate::irq::handle(irqs_pending).expect("Couldn't handle IRQ"); 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()) }) } diff --git a/kernel/src/arch/arm/syscall.rs b/kernel/src/arch/arm/syscall.rs index a4b01cf50..05cf905c5 100644 --- a/kernel/src/arch/arm/syscall.rs +++ b/kernel/src/arch/arm/syscall.rs @@ -4,7 +4,7 @@ use crate::services::Thread; extern "C" { - fn _resume_trampoline(mode: usize, thread: &Thread) -> !; + fn _resume_trampoline(mode: usize, thread: &Thread, enable_irqs: bool) -> !; } pub fn invoke( @@ -37,8 +37,16 @@ pub fn invoke( } 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 - 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 // B1.3.1 ARM processor modes @@ -49,6 +57,6 @@ pub fn resume(supervisor: bool, thread: &Thread) -> ! { }; unsafe { - _resume_trampoline(mode, thread); + _resume_trampoline(mode, thread, enable_irqs); } } From 6ac2713f0a332ee8fab6bbbf9142b1e39ac08657 Mon Sep 17 00:00:00 2001 From: eupn Date: Wed, 5 Apr 2023 16:36:13 +0400 Subject: [PATCH 2/6] loader/arm: allocate separate IRQ stack --- loader/src/minielf.rs | 14 +++++++----- loader/src/phase2.rs | 5 ++++- loader/src/platform/atsama5d27/boot.rs | 18 +++++++++++----- loader/src/platform/atsama5d27/consts.rs | 2 ++ loader/src/platform/atsama5d27/debug.rs | 21 +++++++++--------- loader/src/platform/atsama5d27/load.rs | 27 +++++++++++++++++++++--- 6 files changed, 63 insertions(+), 24 deletions(-) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index 8fe8caa66..2552cac8a 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -1,14 +1,18 @@ use core::{mem, slice}; #[cfg(feature = "atsama5d27")] +use armv7::structures::paging::TranslationTableMemory; + +#[cfg(all(feature = "atsama5d27", feature = "debug-print"))] use armv7::{ VirtualAddress, structures::paging::{ InMemoryRegister, PageTableDescriptor, - Readable, TranslationTableDescriptor, TranslationTableMemory, - TranslationTableType, SMALL_PAGE_FLAGS, + Readable, TranslationTableDescriptor, TranslationTableType, + SMALL_PAGE_FLAGS, } }; + use crate::*; pub const FLG_VALID: usize = 0x1; @@ -109,7 +113,7 @@ impl MiniElf { // Allocate a page to handle the top-level memory translation #[cfg(not(feature = "atsama5d27"))] - let (tt, tt_address) = { + let (tt, _tt_address) = { let tt_address = allocator.alloc() as usize; let tt = unsafe { &mut *(tt_address as *mut PageTable) }; @@ -119,7 +123,7 @@ impl MiniElf { (tt, tt_address) }; #[cfg(feature = "atsama5d27")] - let (tt, tt_address) = { + let (tt, _tt_address) = { let pid_idx = pid as usize - 1; // Allocate a page to handle the top-level memory translation @@ -142,7 +146,7 @@ impl MiniElf { }; // Turn the satp address into a pointer - println!(" Pagetable @ {:08x}", tt_address); + println!(" Pagetable @ {:08x}", _tt_address); // Allocate thread 1 for this process let thread_address = allocator.alloc() as usize; diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index ecd39b2b8..6afc1dd0f 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -28,6 +28,8 @@ pub fn phase_2(cfg: &mut BootConfig) { let mut kdata_size = 0; #[cfg(feature = "atsama5d27")] let mut kernel_exception_sp = 0; + #[cfg(feature = "atsama5d27")] + let mut kernel_irq_sp = 0; for tag in args.iter() { if tag.name == u32::from_le_bytes(*b"IniE") { let inie = MiniElf::new(&tag); @@ -54,7 +56,7 @@ pub fn phase_2(cfg: &mut BootConfig) { } #[cfg(feature = "atsama5d27")] { - (ktext_offset, kdata_offset, kernel_exception_sp) = xkrn.load(cfg, process_offset - load_size_rounded, 1); + (ktext_offset, kdata_offset, kernel_exception_sp, kernel_irq_sp) = xkrn.load(cfg, process_offset - load_size_rounded, 1); (ktext_size, kdata_size, ktext_virt_offset, kdata_virt_offset) = ( (xkrn.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1), (((xkrn.data_size + xkrn.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1)), @@ -117,6 +119,7 @@ pub fn phase_2(cfg: &mut BootConfig) { kdata_size, kdata_virt_offset, kernel_exception_sp, + kernel_irq_sp, krn_struct_start, ); } diff --git a/loader/src/platform/atsama5d27/boot.rs b/loader/src/platform/atsama5d27/boot.rs index ec65e60de..ce0e43c2f 100644 --- a/loader/src/platform/atsama5d27/boot.rs +++ b/loader/src/platform/atsama5d27/boot.rs @@ -12,11 +12,7 @@ use armv7::structures::paging::{ PAGE_TABLE_FLAGS, SMALL_PAGE_FLAGS, }; use armv7::{PhysicalAddress, VirtualAddress}; -use crate::consts::{ - FLG_R, FLG_U, FLG_VALID, FLG_W, FLG_X, GUARD_MEMORY_BYTES, KERNEL_ARGUMENT_OFFSET, - LOADER_CODE_ADDRESS, PAGE_TABLE_OFFSET, KERNEL_STACK_PAGE_COUNT, - EXCEPTION_STACK_TOP, -}; +use crate::consts::{FLG_R, FLG_U, FLG_VALID, FLG_W, FLG_X, GUARD_MEMORY_BYTES, KERNEL_ARGUMENT_OFFSET, LOADER_CODE_ADDRESS, PAGE_TABLE_OFFSET, KERNEL_STACK_PAGE_COUNT, EXCEPTION_STACK_TOP, IRQ_STACK_TOP}; use crate::{ bzero, println, BootConfig, XousPid, PAGE_SIZE, WORD_SIZE, }; @@ -394,6 +390,7 @@ pub fn map_kernel_to_processes( kdata_size: usize, kdata_virt_offset: usize, kernel_exception_sp: usize, + kernel_irq_sp: usize, krn_struct_start: usize, ) { let processes = unsafe { core::mem::transmute::<_, &[InitialProcess]>( &*cfg.processes) }; @@ -444,6 +441,17 @@ pub fn map_kernel_to_processes( ); } + println!("Mapping irq stack page to the process PID{}", process.asid); + let virt = IRQ_STACK_TOP; + let phys = kernel_irq_sp; + println!("MAP ({:08x}): {:08x} -> {:08x}", translation_table as usize, virt, phys); + cfg.map_page( + translation_table, + phys, + virt, + FLG_VALID | FLG_R | FLG_W, + ); + // TODO: For now, make the UART visible for all the processes. // Later on we may reuse the mapping made by the kernel. println!("Mapping UART registers to the process PID{}", process.asid); diff --git a/loader/src/platform/atsama5d27/consts.rs b/loader/src/platform/atsama5d27/consts.rs index 7ab990ce2..d77b632fc 100644 --- a/loader/src/platform/atsama5d27/consts.rs +++ b/loader/src/platform/atsama5d27/consts.rs @@ -14,6 +14,7 @@ pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000; pub const CONTEXT_OFFSET: usize = 0xff80_4000; pub const USER_AREA_END: usize = 0xfe00_0000; pub const EXCEPTION_STACK_TOP: usize = 0xffff_0000; +pub const IRQ_STACK_TOP: usize = 0xfffd_0000; pub const KERNEL_LOAD_OFFSET: usize = 0xffd0_0000; pub const KERNEL_STACK_TOP: usize = 0xfff8_0000; pub const KERNEL_ARGUMENT_OFFSET: usize = 0xffc0_0000; @@ -21,6 +22,7 @@ pub const GUARD_MEMORY_BYTES: usize = 2 * PAGE_SIZE; // Allocate more pages for the kernel stacks pub const KERNEL_STACK_PAGE_COUNT: usize = 4; +pub const IRQ_STACK_PAGE_COUNT: usize = 1; pub const FLG_VALID: usize = 0x1; pub const FLG_X: usize = 0x8; diff --git a/loader/src/platform/atsama5d27/debug.rs b/loader/src/platform/atsama5d27/debug.rs index f3702011a..7e17e3125 100644 --- a/loader/src/platform/atsama5d27/debug.rs +++ b/loader/src/platform/atsama5d27/debug.rs @@ -17,6 +17,7 @@ pub struct Uart { } impl Uart { + #[allow(dead_code)] pub fn new() -> Self { Self { inner: UartType::new(), @@ -89,7 +90,7 @@ fn print_l2_pagetable(vpn1: usize, phys_addr: PhysicalAddress) { let mut no_valid_items = true; for (i, pt_desc) in l2_pt.iter().enumerate() { - let virt_addr = (vpn1 << 20) | (i << 12); + let _virt_addr = (vpn1 << 20) | (i << 12); if let PageTableType::Invalid = pt_desc.get_type() { continue; @@ -97,16 +98,16 @@ fn print_l2_pagetable(vpn1: usize, phys_addr: PhysicalAddress) { no_valid_items = false; - let phys_addr = pt_desc.get_addr().expect("addr"); + let _phys_addr = pt_desc.get_addr().expect("addr"); match pt_desc.get_type() { PageTableType::LargePage => println!( " - {:02x} (64K) Large Page {:08x} -> {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, _phys_addr ), PageTableType::SmallPage => println!( " - {:02x} (4K) Small Page {:08x} -> {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, _phys_addr ), _ => (), } @@ -132,25 +133,25 @@ pub fn print_pagetable(root: usize) { match tt_desc.get_type() { TranslationTableType::Page => { - let virt_addr = i << 20; + let _virt_addr = i << 20; println!( " - {:03x} (1MB) {:08x} L2 page table @ {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, phys_addr ); print_l2_pagetable(i, phys_addr); } TranslationTableType::Section => { - let virt_addr = i * 1024; // 1 MB + let _virt_addr = i * 1024; // 1 MB println!( " - {:03x} (1MB) section {:08x} -> {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, phys_addr ); } TranslationTableType::Supersection => { - let virt_addr = i * (1024 * 16); // 16 MB + let _virt_addr = i * (1024 * 16); // 16 MB println!( " - {:03x} (16MB) supersection {:08x} -> {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, phys_addr ); } diff --git a/loader/src/platform/atsama5d27/load.rs b/loader/src/platform/atsama5d27/load.rs index 4ce36d8d0..b78590b5d 100644 --- a/loader/src/platform/atsama5d27/load.rs +++ b/loader/src/platform/atsama5d27/load.rs @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: 2023 Foundation Devices, Inc // SPDX-License-Identifier: Apache-2.0 -use crate::consts::{CONTEXT_OFFSET, EXCEPTION_STACK_TOP, FLG_R, FLG_U, FLG_VALID, FLG_W, FLG_X, KERNEL_LOAD_OFFSET, PAGE_TABLE_ROOT_OFFSET, USER_AREA_END, USER_STACK_TOP, KERNEL_STACK_PAGE_COUNT, KERNEL_STACK_TOP}; +use crate::consts::{CONTEXT_OFFSET, EXCEPTION_STACK_TOP, FLG_R, FLG_U, FLG_VALID, FLG_W, FLG_X, KERNEL_LOAD_OFFSET, PAGE_TABLE_ROOT_OFFSET, USER_AREA_END, USER_STACK_TOP, KERNEL_STACK_PAGE_COUNT, KERNEL_STACK_TOP, IRQ_STACK_PAGE_COUNT, IRQ_STACK_TOP}; use crate::{ println, BootConfig, ProgramDescription, XousPid, PAGE_SIZE, STACK_PAGE_COUNT, VDBG, @@ -33,7 +33,7 @@ impl ProgramDescription { /// that is passed in should be used instead of `self.load_offset` /// for this reason. pub fn load(&self, allocator: &mut BootConfig, load_offset: usize, pid: XousPid) - -> (usize, usize, usize) { + -> (usize, usize, usize, usize) { assert_ne!(pid, 0, "PID must not be 0"); println!("Mapping PID {} into offset {:08x}", pid, load_offset); @@ -151,6 +151,27 @@ impl ProgramDescription { } } + // Allocate IRQ stack pages + let mut irq_sp = 0; + for i in 0..IRQ_STACK_PAGE_COUNT { + // If it's the kernel, also allocate an exception page + if is_kernel { + let sp_page = allocator.alloc() as usize; + // Remember only the first (top) exception stack page + if irq_sp == 0 { + irq_sp = sp_page; + } + println!("Allocated IRQ stack page: {:08x}", sp_page); + allocator.map_page( + translation_table, + sp_page, + (IRQ_STACK_TOP - 16 - PAGE_SIZE * i) & !(PAGE_SIZE - 1), + flag_defaults, + ); + allocator.change_owner(pid as XousPid, sp_page); + } + } + assert_eq!((self.text_offset as usize & (PAGE_SIZE - 1)), 0); assert_eq!((self.data_offset as usize & (PAGE_SIZE - 1)), 0); if allocator.no_copy { @@ -207,6 +228,6 @@ impl ProgramDescription { allocator.processes[pid_idx].sp = stack_addr; allocator.processes[pid_idx].asid = pid; - (text_phys_offset, data_phys_offset, exception_sp) + (text_phys_offset, data_phys_offset, exception_sp, irq_sp) } } From d55ea419b294d0a82ca7bedff70036f4c6e2ddc9 Mon Sep 17 00:00:00 2001 From: eupn Date: Wed, 5 Apr 2023 16:37:55 +0400 Subject: [PATCH 3/6] Add ticktimer test client --- services/ticktimer-test-client/Cargo.toml | 15 +++++++++++++++ services/ticktimer-test-client/README.md | 1 + services/ticktimer-test-client/src/main.rs | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 services/ticktimer-test-client/Cargo.toml create mode 100644 services/ticktimer-test-client/README.md create mode 100644 services/ticktimer-test-client/src/main.rs diff --git a/services/ticktimer-test-client/Cargo.toml b/services/ticktimer-test-client/Cargo.toml new file mode 100644 index 000000000..e663004ed --- /dev/null +++ b/services/ticktimer-test-client/Cargo.toml @@ -0,0 +1,15 @@ +[package] +authors = ["Sean Cross "] +description = "Program to test the log server" +edition = "2021" +name = "ticktimer-test-client" +version = "0.1.0" + +# Dependency versions enforced by Cargo.lock. +[dependencies] +xous = "0.9.28" +log = "0.4.14" +log-server = {package = "xous-api-log", version = "0.1.22"} + +[features] +default = [] diff --git a/services/ticktimer-test-client/README.md b/services/ticktimer-test-client/README.md new file mode 100644 index 000000000..9d54a124e --- /dev/null +++ b/services/ticktimer-test-client/README.md @@ -0,0 +1 @@ +# `xous-ticktimer` test client diff --git a/services/ticktimer-test-client/src/main.rs b/services/ticktimer-test-client/src/main.rs new file mode 100644 index 000000000..0315f0d62 --- /dev/null +++ b/services/ticktimer-test-client/src/main.rs @@ -0,0 +1,21 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +use log::info; +use std::thread::sleep; +use std::time::Duration; + +fn main() -> ! { + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Trace); + info!("my PID is {}", xous::process::id()); + + const DELAY_MS: u64 = 5000; + + for i in 0.. { + info!("Loop #{}, waiting {} ms", i, DELAY_MS); + sleep(Duration::from_millis(DELAY_MS)); + } + + panic!("Finished endless loop"); +} From d968a4d746746ea4c33f4077cdadec22303d4211 Mon Sep 17 00:00:00 2001 From: eupn Date: Wed, 5 Apr 2023 16:38:11 +0400 Subject: [PATCH 4/6] xtask/arm: add more services to the image --- xtask/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 0c1b910e7..000aaaf6c 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -359,6 +359,9 @@ fn main() -> Result<(), Box> { builder.target_arm() .add_services(&vec![ "xous-log".to_string(), + "xous-ticktimer".to_string(), + "xous-names".to_string(), + "ticktimer-test-client".to_string(), ]) .add_kernel_feature("v2p") // required to use LCD DMA with lcd-console .add_feature("atsama5d27") From 0273c50039e9dd8984da65b61f6a83420f1d376a Mon Sep 17 00:00:00 2001 From: eupn Date: Wed, 5 Apr 2023 16:38:41 +0400 Subject: [PATCH 5/6] Cargo.toml: add ticktimer-test-client to the workspace --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 978940e38..ada1be467 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ members = [ "services/com", "services/keyboard", "services/kernel-test", + "services/ticktimer-test-client", "services/trng", "services/gam", "services/status", From 387c12d1ee1b4fa76b0308cd42f3d64864d79437 Mon Sep 17 00:00:00 2001 From: eupn Date: Wed, 5 Apr 2023 16:42:58 +0400 Subject: [PATCH 6/6] loader: fix precursor build --- loader/src/minielf.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index 2552cac8a..8175ea06d 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -333,7 +333,7 @@ impl MiniElf { process.sp = stack_addr; #[cfg(not(feature = "atsama5d27"))] { - process.satp = 0x8000_0000 | ((pid as usize) << 22) | (tt_address >> 12); + process.satp = 0x8000_0000 | ((pid as usize) << 22) | (_tt_address >> 12); } #[cfg(feature = "atsama5d27")] {