From fb75e250c49f28eda5c99a62422f1b4028d820b8 Mon Sep 17 00:00:00 2001 From: ZhiyuanSue <2262387848@qq.com> Date: Tue, 2 Jul 2024 14:48:26 +0800 Subject: [PATCH 01/10] try to use aarch64-toolchain --- build.py | 28 +++++++++++++++++++++++----- src/.cargo/config | 5 ++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/build.py b/build.py index c874b6c..bc96569 100755 --- a/build.py +++ b/build.py @@ -23,6 +23,10 @@ def parse_args(): help="baseline switch") parser.add_argument('-c', '--cpu', dest="cpu_nums", type=int, help="kernel & qemu cpu nums", default=1) + # parser.add_argument('-a','--arch', dest="arch",type=str, + # help="kernel arch",default="riscv64imac-unknown-none-elf") + parser.add_argument('-a','--arch', dest="arch",type=str, + help="kernel arch",default="aarch64-unknown-linux-gnu") args = parser.parse_args() return args @@ -31,7 +35,7 @@ def exec_shell(shell_command): return ret_code == 0 def clean_config(): - shell_command = "cd ../kernel && git checkout 552f173d3d7780b33184ebedefc58329ea5de3ba" + shell_command = "cd ../kernel && git checkout 37a3dc8f26da380e539470aac6d2d53286a92d3c" exec_shell(shell_command) if __name__ == "__main__": @@ -39,6 +43,12 @@ def clean_config(): clean_config() progname = sys.argv[0] + if args.arch != "riscv64imac-unknown-none-elf" and args.arch != "aarch64-unknown-linux-gnu": + clean_config() + sys.exit(-1) + + print("target is:" + args.arch) + if os.path.exists(build_dir): shutil.rmtree(build_dir) os.makedirs(build_dir) @@ -49,21 +59,29 @@ def clean_config(): sys.exit(-1) else: if args.cpu_nums > 1: - if not exec_shell("cargo build --release --target riscv64imac-unknown-none-elf --features ENABLE_SMP"): + if not exec_shell("cargo build --release --target " + args.arch + " --features ENABLE_SMP"): clean_config() sys.exit(-1) else: - if not exec_shell("cargo build --release --target riscv64imac-unknown-none-elf"): + if not exec_shell("cargo build --release --target " + args.arch): clean_config() sys.exit(-1) if args.cpu_nums > 1: - shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=spike -DSIMULATION=TRUE -DSMP=TRUE && ninja" + shell_command = "" + if args.arch == "riscv64imac-unknown-none-elf": + shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=spike -DSIMULATION=TRUE -DSMP=TRUE && ninja" + elif args.arch == "aarch64-unknown-linux-gnu": + shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=qemu-arm-virt -DSIMULATION=TRUE -DSMP=TRUE && ninja" if not exec_shell(shell_command): clean_config() sys.exit(-1) sys.exit(0) - shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=spike -DSIMULATION=TRUE && ninja" + shell_command = "" + if args.arch == "riscv64imac-unknown-none-elf": + shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=spike -DSIMULATION=TRUE && ninja" + elif args.arch == "aarch64-unknown-linux-gnu": + shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=qemu-arm-virt -DSIMULATION=TRUE && ninja" if not exec_shell(shell_command): clean_config() sys.exit(-1) diff --git a/src/.cargo/config b/src/.cargo/config index e041379..3fb8e1e 100644 --- a/src/.cargo/config +++ b/src/.cargo/config @@ -1,2 +1,5 @@ [build] -target = "riscv64imac-unknown-none-elf" \ No newline at end of file +target = 'aarch64-unknown-linux-gnu' +# target = "riscv64imac-unknown-none-elf" + +[target.aarch64-unknown-linux-gnu] \ No newline at end of file From 9d2ee64abe7cc8b3d41aac0a33bcd03d534c54fc Mon Sep 17 00:00:00 2001 From: ZhiyuanSue <2262387848@qq.com> Date: Tue, 2 Jul 2024 15:13:26 +0800 Subject: [PATCH 02/10] fix the abort panic problem --- Cargo.toml | 6 +++++- src/.cargo/config | 2 -- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d5cdb03..ba5d6d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,4 +24,8 @@ sel4_ipc = { path = "./sel4_ipc" } ENABLE_SMP = ["sel4_common/ENABLE_SMP", "sel4_task/ENABLE_SMP", "sel4_vspace/ENABLE_SMP"] [profile.release] -lto = true \ No newline at end of file +lto = true +panic = "abort" + +[profile.dev] +panic = "abort" \ No newline at end of file diff --git a/src/.cargo/config b/src/.cargo/config index 3fb8e1e..4a80fe8 100644 --- a/src/.cargo/config +++ b/src/.cargo/config @@ -1,5 +1,3 @@ [build] target = 'aarch64-unknown-linux-gnu' # target = "riscv64imac-unknown-none-elf" - -[target.aarch64-unknown-linux-gnu] \ No newline at end of file From ba5c1eb3d8821b83f8a29a3a16e068143bbad6f5 Mon Sep 17 00:00:00 2001 From: ZhiyuanSue <2262387848@qq.com> Date: Tue, 2 Jul 2024 17:45:53 +0800 Subject: [PATCH 03/10] try to update something --- sel4_common/src/console.rs | 6 ++++ sel4_common/src/lib.rs | 1 + sel4_vspace/src/satp.rs | 6 ++++ src/.cargo/config | 3 ++ src/boot/mod.rs | 70 +++++++++++++++++++++++++++++++------- src/kernel/c_traps.rs | 2 ++ src/kernel/fastpath.rs | 2 +- src/lang_items.rs | 2 +- src/lib.rs | 1 + src/riscv.rs | 1 + 10 files changed, 80 insertions(+), 14 deletions(-) diff --git a/sel4_common/src/console.rs b/sel4_common/src/console.rs index 6b40deb..88c866b 100644 --- a/sel4_common/src/console.rs +++ b/sel4_common/src/console.rs @@ -1,10 +1,16 @@ //! SBI console driver, for text output +#[cfg(target_arch = "riscv64")] use super::sbi::console_putchar; use core::fmt::{self, Write}; struct Stdout; +#[cfg(target_arch = "aarch64")] +pub fn console_putchar(c: usize) { + // tmp empty funcion and useless, TODO: clean +} + impl Write for Stdout { fn write_str(&mut self, s: &str) -> fmt::Result { for c in s.chars() { diff --git a/sel4_common/src/lib.rs b/sel4_common/src/lib.rs index d62ed6b..ad0e723 100644 --- a/sel4_common/src/lib.rs +++ b/sel4_common/src/lib.rs @@ -9,6 +9,7 @@ mod deps; pub mod sel4_config; pub mod structures; pub mod utils; +#[cfg(target_arch = "riscv64")] pub mod sbi; mod console; pub mod logging; diff --git a/sel4_vspace/src/satp.rs b/sel4_vspace/src/satp.rs index 5f0a5aa..3e90487 100644 --- a/sel4_vspace/src/satp.rs +++ b/sel4_vspace/src/satp.rs @@ -1,7 +1,13 @@ use riscv::register::satp; +#[cfg(target_arch = "riscv64")] use sel4_common::sbi::remote_sfence_vma; use super::structures::paddr_t; +#[cfg(target_arch = "aarch64")] +pub fn remote_sfence_vma(hart_mask: usize, start: usize, size: usize) { + // tmp empty funcion and useless, TODO: clean +} + #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct satp_t { diff --git a/src/.cargo/config b/src/.cargo/config index 4a80fe8..7024cc6 100644 --- a/src/.cargo/config +++ b/src/.cargo/config @@ -1,3 +1,6 @@ [build] target = 'aarch64-unknown-linux-gnu' # target = "riscv64imac-unknown-none-elf" + +[target.aarch64-unknown-linux-gnu] +[target.riscv64imac-unknown-none-elf] \ No newline at end of file diff --git a/src/boot/mod.rs b/src/boot/mod.rs index f250b69..998ce67 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -21,6 +21,7 @@ use crate::boot::root_server::root_server_init; use crate::boot::untyped::create_untypeds; use crate::boot::utils::paddr_to_pptr_reg; use crate::interrupt::set_sie_mask; +#[cfg(target_arch = "riscv64")] use sel4_common::sbi::{set_timer, get_time}; use crate::structures::{ndks_boot_t, region_t, p_region_t, seL4_BootInfo, seL4_BootInfoHeader, seL4_SlotRegion, v_region_t}; use crate::config::*; @@ -56,20 +57,65 @@ pub static mut ndks_boot: ndks_boot_t = ndks_boot_t { fn init_cpu() { + let haveHWFPU:bool; activate_kernel_vspace(); - extern "C" { - fn trap_entry(); - } - unsafe { - stvec::write(trap_entry as usize, TrapMode::Direct); - } - #[cfg(feature = "ENABLE_SMP")] { - set_sie_mask(BIT!(SIE_SEIE) | BIT!(SIE_STIE) | BIT!(SIE_SSIE)); - } - #[cfg(not(feature = "ENABLE_SMP"))] { - set_sie_mask(BIT!(SIE_SEIE) | BIT!(SIE_STIE)); - } + // if (config_set(CONFIG_ARM_HYPERVISOR_SUPPORT)) { // copied from C sel4, no arm hypervisor, so no change + // vcpu_boot_init(); + // } + // #ifdef CONFIG_HARDWARE_DEBUG_API + // if (!Arch_initHardwareBreakpoints()) { + // printf("Kernel built with CONFIG_HARDWARE_DEBUG_API, but this board doesn't " + // "reliably support it.\n"); + // return false; + // } + // #endif + #[cfg(target_arch = "riscv64")] + { + extern "C" { + fn trap_entry(); + } + unsafe { + stvec::write(trap_entry as usize, TrapMode::Direct); + } + } + #[cfg(target_arch = "aarch64")] + { + // Setup kernel stack pointer. + } + + #[cfg(target_arch = "aarch64")] + { + // CPU's exception vector table + } + + #[cfg(target_arch = "aarch64")] + { + // disable fpu + } + + #[cfg(target_arch = "riscv64")] + { + #[cfg(feature = "ENABLE_SMP")] { + set_sie_mask(BIT!(SIE_SEIE) | BIT!(SIE_STIE) | BIT!(SIE_SSIE)); + } + #[cfg(not(feature = "ENABLE_SMP"))] { + set_sie_mask(BIT!(SIE_SEIE) | BIT!(SIE_STIE)); + } + } + #[cfg(target_arch = "aarch64")] + { + // initLocalIRQController + } + #[cfg(target_arch = "aarch64")] + { + // armv_init_user_access + } + #[cfg(target_arch = "riscv64")] set_timer(get_time() + RESET_CYCLES); + #[cfg(target_arch = "aarch64")] + { + //initTimer + } } fn calculate_extra_bi_size_bits(size: usize) -> usize { diff --git a/src/kernel/c_traps.rs b/src/kernel/c_traps.rs index e8aba06..c7fb301 100644 --- a/src/kernel/c_traps.rs +++ b/src/kernel/c_traps.rs @@ -23,6 +23,8 @@ use sel4_common::utils::cpu_id; #[no_mangle] pub fn restore_user_context() { + + #[cfg(target_arch = "riscv64")] unsafe { // debug!("restore_user_context"); let cur_thread_reg = get_currenct_thread().tcbArch.registers.as_ptr() as usize; diff --git a/src/kernel/fastpath.rs b/src/kernel/fastpath.rs index ee4c962..c87308a 100644 --- a/src/kernel/fastpath.rs +++ b/src/kernel/fastpath.rs @@ -114,7 +114,7 @@ pub fn fastpath_copy_mrs(length: usize, src: &mut tcb_t, dest: &mut tcb_t) { dest.set_register(reg, src.get_register(reg)); } } - +#[cfg(target_arch = "riscv64")] core::arch::global_asm!(include_str!("restore_fp.S")); // #[inline] diff --git a/src/lang_items.rs b/src/lang_items.rs index 6061333..58e735b 100644 --- a/src/lang_items.rs +++ b/src/lang_items.rs @@ -1,5 +1,5 @@ use log::error; - +#[cfg(target_arch = "riscv64")] use sel4_common::sbi::shutdown; use core::panic::PanicInfo; diff --git a/src/lib.rs b/src/lib.rs index 806e92b..0f307c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ extern crate core; +#[cfg(target_arch = "riscv64")] use sel4_common::sbi::shutdown; mod config; // mod console; diff --git a/src/riscv.rs b/src/riscv.rs index b71376e..1d8db2b 100644 --- a/src/riscv.rs +++ b/src/riscv.rs @@ -1,6 +1,7 @@ use core::arch::asm; use crate::config::RESET_CYCLES; +#[cfg(target_arch = "riscv64")] use sel4_common::sbi::set_timer; pub fn read_stval() -> usize { From 05c5865290fc1b703d50e21994391257500cf6ad Mon Sep 17 00:00:00 2001 From: ZhiyuanSue <2262387848@qq.com> Date: Wed, 3 Jul 2024 09:49:56 +0800 Subject: [PATCH 04/10] start change the init_cpu function --- Cargo.toml | 2 ++ sel4_common/Cargo.toml | 3 ++- sel4_common/src/sel4_config.rs | 3 +++ sel4_common/src/smp.rs | 22 +++++++++++++++- src/aarch64.rs | 19 ++++++++++++++ src/boot/mod.rs | 48 ++++++++++++++++++++++++---------- src/config.rs | 1 + src/lib.rs | 3 +++ 8 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 src/aarch64.rs diff --git a/Cargo.toml b/Cargo.toml index ba5d6d0..ac611a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,8 @@ sel4_task = { path = "./sel4_task" } sel4_ipc = { path = "./sel4_ipc" } [features] ENABLE_SMP = ["sel4_common/ENABLE_SMP", "sel4_task/ENABLE_SMP", "sel4_vspace/ENABLE_SMP"] +ARM_HYPERVISOR_SUPPORT = ["sel4_common/ARM_HYPERVISOR_SUPPORT"] +HARDWARE_DEBUG_API = [] [profile.release] lto = true diff --git a/sel4_common/Cargo.toml b/sel4_common/Cargo.toml index d39c306..16247c1 100644 --- a/sel4_common/Cargo.toml +++ b/sel4_common/Cargo.toml @@ -9,4 +9,5 @@ log = "0.4.21" spin = { version = "0.9", features = ["use_ticket_mutex"] } [features] -ENABLE_SMP = [] \ No newline at end of file +ENABLE_SMP = [] +ARM_HYPERVISOR_SUPPORT = [] \ No newline at end of file diff --git a/sel4_common/src/sel4_config.rs b/sel4_common/src/sel4_config.rs index 8da48ee..a890373 100644 --- a/sel4_common/src/sel4_config.rs +++ b/sel4_common/src/sel4_config.rs @@ -103,3 +103,6 @@ pub const CONFIG_RESET_CHUNK_BITS: usize = 8; pub const CONFIG_KERNEL_STACK_BITS: usize = 12; +// aarch64 porting TODO + +pub const CURRENT_CPU_INDEX: usize = 0; \ No newline at end of file diff --git a/sel4_common/src/smp.rs b/sel4_common/src/smp.rs index e20701f..2589a58 100644 --- a/sel4_common/src/smp.rs +++ b/sel4_common/src/smp.rs @@ -1,7 +1,7 @@ use core::arch::asm; use crate::BIT; use crate::deps::{coreMap, kernel_stack_alloc}; -use super::sel4_config::{CONFIG_MAX_NUM_NODES, CONFIG_KERNEL_STACK_BITS}; +use super::sel4_config::{CONFIG_MAX_NUM_NODES, CONFIG_KERNEL_STACK_BITS, CPUID_MASK}; #[inline] @@ -30,6 +30,7 @@ pub fn hart_id_to_core_id(hart_id: usize) -> usize { #[inline] pub fn get_currenct_cpu_index() -> usize { + #[cfg(target_arch = "riscv64")] unsafe { let mut cur_sp: usize; asm!( @@ -39,6 +40,25 @@ pub fn get_currenct_cpu_index() -> usize { cur_sp -= kernel_stack_alloc as usize + 8; cur_sp >> CONFIG_KERNEL_STACK_BITS } + #[cfg(target_arch = "aarch64")] + unsafe { + let mut id: usize; + #[cfg(not(feature = "ARM_HYPERVISOR_SUPPORT"))] + { + asm!( + "mrs {},tpidr_el1", + out(reg) id, + ); + } + #[cfg(feature = "ARM_HYPERVISOR_SUPPORT")] + { + asm!( + "mrs {},tpidr_el2", + out(reg) id, + ); + } + id & CPUID_MASK + } } #[inline] diff --git a/src/aarch64.rs b/src/aarch64.rs new file mode 100644 index 0000000..3564e6e --- /dev/null +++ b/src/aarch64.rs @@ -0,0 +1,19 @@ +use core::arch::asm; + +#[inline] +pub fn set_kernel_stack(stack_address: usize) { + #[cfg(feature = "ARM_HYPERVISOR_SUPPORT")] + { + // TODO + } + #[cfg(not(feature = "ARM_HYPERVISOR_SUPPORT"))] + { + writeTPIDR_EL1(stack_address) + } +} +pub fn writeTPIDR_EL1(reg: usize){ + asm!( + "msr tpidr_el1,{}", + inreg(reg), + ); +} \ No newline at end of file diff --git a/src/boot/mod.rs b/src/boot/mod.rs index 998ce67..e9833ca 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -9,12 +9,14 @@ use core::mem::size_of; use crate::deps::{tcbDebugAppend, init_plat}; use crate::{BIT, ROUND_UP}; -use sel4_common::sel4_config::{PADDR_TOP, KERNEL_ELF_BASE, seL4_PageBits, PAGE_BITS}; +use sel4_common::sel4_config::{seL4_PageBits, CONFIG_KERNEL_STACK_BITS, CURRENT_CPU_INDEX, KERNEL_ELF_BASE, PADDR_TOP, PAGE_BITS}; use log::debug; +use sel4_common::utils::cpu_id; use spin::Mutex; -use riscv::register::stvec; -use riscv::register::utvec::TrapMode; - +#[cfg(target_arch = "riscv")] +use riscv::register::{stvec,utvec::TrapMode}; +#[cfg(target_arch = "riscv")] +use aarch64::set_kernel_stack; use crate::boot::mm::init_freemem; use crate::boot::root_server::root_server_init; @@ -59,16 +61,30 @@ pub static mut ndks_boot: ndks_boot_t = ndks_boot_t { fn init_cpu() { let haveHWFPU:bool; activate_kernel_vspace(); - // if (config_set(CONFIG_ARM_HYPERVISOR_SUPPORT)) { // copied from C sel4, no arm hypervisor, so no change - // vcpu_boot_init(); - // } - // #ifdef CONFIG_HARDWARE_DEBUG_API - // if (!Arch_initHardwareBreakpoints()) { - // printf("Kernel built with CONFIG_HARDWARE_DEBUG_API, but this board doesn't " - // "reliably support it.\n"); - // return false; - // } - // #endif + #[cfg(target_arch = "aarch64")] + { + #[cfg(feature = "ARM_HYPERVISOR_SUPPORT")] + { + // TODO + // copied from C sel4, no arm hypervisor, so no change + // if (config_set(CONFIG_ARM_HYPERVISOR_SUPPORT)) { + // vcpu_boot_init(); + // } + } + #[cfg(feature = "HARDWARE_DEBUG_API")] + { + // TODO + // copied from C sel4, no arm hypervisor, so no change + // #ifdef CONFIG_HARDWARE_DEBUG_API + // if (!Arch_initHardwareBreakpoints()) { + // printf("Kernel built with CONFIG_HARDWARE_DEBUG_API, but this board doesn't " + // "reliably support it.\n"); + // return false; + // } + // #endif + } + } + #[cfg(target_arch = "riscv64")] { extern "C" { @@ -81,11 +97,15 @@ fn init_cpu() { #[cfg(target_arch = "aarch64")] { // Setup kernel stack pointer. + let mut stack_top:usize = kernel_stack_alloc[CURRENT_CPU_INDEX] + 1< Date: Wed, 3 Jul 2024 10:33:29 +0800 Subject: [PATCH 05/10] try to use the aarch64 module and continue write init_cpu function --- Cargo.toml | 1 + build.py | 2 +- src/aarch64.rs | 19 ------------------- src/boot/fpu.rs | 0 src/boot/mod.rs | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 34 insertions(+), 24 deletions(-) delete mode 100644 src/aarch64.rs create mode 100644 src/boot/fpu.rs diff --git a/Cargo.toml b/Cargo.toml index ac611a7..aaee8a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ crate-type = ["staticlib"] [dependencies] buddy_system_allocator = "0.6" riscv = { git = "https://gitee.com/rcore-os/riscv", features = ["inline-asm"] } +aarch64 = { git = "https://gitee.com/rcore-os/aarch64" } log = "0.4" spin = { version = "0.9", features = ["use_ticket_mutex"] } sel4_common = { path = "./sel4_common" } diff --git a/build.py b/build.py index bc96569..30a9248 100755 --- a/build.py +++ b/build.py @@ -35,7 +35,7 @@ def exec_shell(shell_command): return ret_code == 0 def clean_config(): - shell_command = "cd ../kernel && git checkout 37a3dc8f26da380e539470aac6d2d53286a92d3c" + shell_command = "cd ../kernel && git checkout 5ddda4ebdae7523830259d5b1f676d25d5de125b" exec_shell(shell_command) if __name__ == "__main__": diff --git a/src/aarch64.rs b/src/aarch64.rs deleted file mode 100644 index 3564e6e..0000000 --- a/src/aarch64.rs +++ /dev/null @@ -1,19 +0,0 @@ -use core::arch::asm; - -#[inline] -pub fn set_kernel_stack(stack_address: usize) { - #[cfg(feature = "ARM_HYPERVISOR_SUPPORT")] - { - // TODO - } - #[cfg(not(feature = "ARM_HYPERVISOR_SUPPORT"))] - { - writeTPIDR_EL1(stack_address) - } -} -pub fn writeTPIDR_EL1(reg: usize){ - asm!( - "msr tpidr_el1,{}", - inreg(reg), - ); -} \ No newline at end of file diff --git a/src/boot/fpu.rs b/src/boot/fpu.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/boot/mod.rs b/src/boot/mod.rs index e9833ca..fab103d 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -4,6 +4,7 @@ mod root_server; mod untyped; mod utils; mod interface; +mod fpu; use core::mem::size_of; @@ -15,8 +16,12 @@ use sel4_common::utils::cpu_id; use spin::Mutex; #[cfg(target_arch = "riscv")] use riscv::register::{stvec,utvec::TrapMode}; -#[cfg(target_arch = "riscv")] -use aarch64::set_kernel_stack; +#[cfg(target_arch = "aarch64")] +use aarch64::regs::{TPIDR_EL1,VBAR_EL1}; +#[cfg(target_arch = "aarch64")] +use aarch64::barrier::{dsb, isb}; +#[cfg(target_arch = "aarch64")] +use aarch64::cache::SY; use crate::boot::mm::init_freemem; use crate::boot::root_server::root_server_init; @@ -99,18 +104,41 @@ fn init_cpu() { // Setup kernel stack pointer. let mut stack_top:usize = kernel_stack_alloc[CURRENT_CPU_INDEX] + 1< Date: Wed, 3 Jul 2024 10:53:12 +0800 Subject: [PATCH 06/10] adjust some codes --- Cargo.toml | 1 + src/boot/fpu.rs | 17 +++++ src/boot/mod.rs | 165 ++++++++++++++++++++++++------------------------ 3 files changed, 100 insertions(+), 83 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aaee8a0..cfc1a8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ sel4_ipc = { path = "./sel4_ipc" } ENABLE_SMP = ["sel4_common/ENABLE_SMP", "sel4_task/ENABLE_SMP", "sel4_vspace/ENABLE_SMP"] ARM_HYPERVISOR_SUPPORT = ["sel4_common/ARM_HYPERVISOR_SUPPORT"] HARDWARE_DEBUG_API = [] +HAVE_FPU = [] [profile.release] lto = true diff --git a/src/boot/fpu.rs b/src/boot/fpu.rs index e69de29..35a5409 100644 --- a/src/boot/fpu.rs +++ b/src/boot/fpu.rs @@ -0,0 +1,17 @@ +#[cfg(target_arch = "aarch64")] +use aarch64::barrier::{dsb,isb}; +#[cfg(target_arch = "aarch64")] +pub fn fpsimd_HWCapTest() -> bool { + // TODO + false +} +#[cfg(target_arch = "aarch64")] +pub fn disableFpu() +{ + // TODO +} +#[cfg(target_arch = "aarch64")] +pub fn fpsimd_init() -> bool { + // TODO + false +} \ No newline at end of file diff --git a/src/boot/mod.rs b/src/boot/mod.rs index fab103d..f79a89c 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -62,108 +62,101 @@ pub static mut ndks_boot: ndks_boot_t = ndks_boot_t { slot_pos_cur: seL4_NumInitialCaps, }; - +#[cfg(target_arch = "riscv64")] fn init_cpu() { - let haveHWFPU:bool; activate_kernel_vspace(); - #[cfg(target_arch = "aarch64")] + extern "C" { + fn trap_entry(); + } + unsafe { + stvec::write(trap_entry as usize, TrapMode::Direct); + } + #[cfg(feature = "ENABLE_SMP")] { + set_sie_mask(BIT!(SIE_SEIE) | BIT!(SIE_STIE) | BIT!(SIE_SSIE)); + } + #[cfg(not(feature = "ENABLE_SMP"))] { + set_sie_mask(BIT!(SIE_SEIE) | BIT!(SIE_STIE)); + } + set_timer(get_time() + RESET_CYCLES); +} + +#[cfg(target_arch = "aarch64")] +fn init_cpu() -> bool { + #[cfg(feature = "ARM_HYPERVISOR_SUPPORT")] { - #[cfg(feature = "ARM_HYPERVISOR_SUPPORT")] - { - // TODO - // copied from C sel4, no arm hypervisor, so no change - // if (config_set(CONFIG_ARM_HYPERVISOR_SUPPORT)) { - // vcpu_boot_init(); - // } - } - #[cfg(feature = "HARDWARE_DEBUG_API")] - { - // TODO - // copied from C sel4, no arm hypervisor, so no change - // #ifdef CONFIG_HARDWARE_DEBUG_API - // if (!Arch_initHardwareBreakpoints()) { - // printf("Kernel built with CONFIG_HARDWARE_DEBUG_API, but this board doesn't " - // "reliably support it.\n"); - // return false; - // } - // #endif - } + // TODO + // copied from C sel4, no arm hypervisor, so no change + // if (config_set(CONFIG_ARM_HYPERVISOR_SUPPORT)) { + // vcpu_boot_init(); + // } } - - #[cfg(target_arch = "riscv64")] + #[cfg(feature = "HARDWARE_DEBUG_API")] { - extern "C" { - fn trap_entry(); - } - unsafe { - stvec::write(trap_entry as usize, TrapMode::Direct); - } + // TODO + // copied from C sel4, no arm hypervisor, so no change + // #ifdef CONFIG_HARDWARE_DEBUG_API + // if (!Arch_initHardwareBreakpoints()) { + // printf("Kernel built with CONFIG_HARDWARE_DEBUG_API, but this board doesn't " + // "reliably support it.\n"); + // return false; + // } + // #endif } - #[cfg(target_arch = "aarch64")] + // Setup kernel stack pointer. + let mut stack_top:usize = kernel_stack_alloc[CURRENT_CPU_INDEX] + 1< usize { @@ -302,7 +295,13 @@ pub fn try_init_kernel( let bi_frame_vptr = ipcbuf_vptr + BIT!(PAGE_BITS); let extra_bi_frame_vptr = bi_frame_vptr + BIT!(BI_FRAME_SIZE_BITS); rust_map_kernel_window(); + #[cfg(target_arch = "riscv")] init_cpu(); + #[cfg(target_arch = "riscv")] + if init_cpu() == false { + debug!("ERROR: CPU init failed"); + return false; + } unsafe { init_plat(); From 46142d02308aa8072c2a1edf18b928e235f36103 Mon Sep 17 00:00:00 2001 From: ZhiyuanSue <2262387848@qq.com> Date: Wed, 3 Jul 2024 14:09:54 +0800 Subject: [PATCH 07/10] change depends to the aarch64-cpu --- Cargo.toml | 2 +- sel4_common/src/lib.rs | 2 +- src/boot/fpu.rs | 3 ++- src/boot/gic_v2.rs | 5 +++++ src/boot/mod.rs | 26 ++++++++++++++------------ src/lib.rs | 2 -- 6 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 src/boot/gic_v2.rs diff --git a/Cargo.toml b/Cargo.toml index cfc1a8b..6b4e3dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ crate-type = ["staticlib"] [dependencies] buddy_system_allocator = "0.6" riscv = { git = "https://gitee.com/rcore-os/riscv", features = ["inline-asm"] } -aarch64 = { git = "https://gitee.com/rcore-os/aarch64" } +aarch64-cpu = "9.4.0" log = "0.4" spin = { version = "0.9", features = ["use_ticket_mutex"] } sel4_common = { path = "./sel4_common" } diff --git a/sel4_common/src/lib.rs b/sel4_common/src/lib.rs index ad0e723..5260b0b 100644 --- a/sel4_common/src/lib.rs +++ b/sel4_common/src/lib.rs @@ -5,7 +5,7 @@ #[cfg(feature = "ENABLE_SMP")] pub mod smp; -mod deps; +pub mod deps; pub mod sel4_config; pub mod structures; pub mod utils; diff --git a/src/boot/fpu.rs b/src/boot/fpu.rs index 35a5409..4eff707 100644 --- a/src/boot/fpu.rs +++ b/src/boot/fpu.rs @@ -1,5 +1,6 @@ +// This file is for aarch64 only #[cfg(target_arch = "aarch64")] -use aarch64::barrier::{dsb,isb}; +use aarch64_cpu::barrier::{dsb,isb}; #[cfg(target_arch = "aarch64")] pub fn fpsimd_HWCapTest() -> bool { // TODO diff --git a/src/boot/gic_v2.rs b/src/boot/gic_v2.rs new file mode 100644 index 0000000..687c8d9 --- /dev/null +++ b/src/boot/gic_v2.rs @@ -0,0 +1,5 @@ + +// This is for aarch64 only +pub fn cpu_iface_init() { + +} \ No newline at end of file diff --git a/src/boot/mod.rs b/src/boot/mod.rs index f79a89c..79decf5 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -4,7 +4,10 @@ mod root_server; mod untyped; mod utils; mod interface; +#[cfg(target_arch = "aarch64")] mod fpu; +#[cfg(target_arch = "aarch64")] +mod gic_v2; use core::mem::size_of; @@ -13,15 +16,14 @@ use crate::{BIT, ROUND_UP}; use sel4_common::sel4_config::{seL4_PageBits, CONFIG_KERNEL_STACK_BITS, CURRENT_CPU_INDEX, KERNEL_ELF_BASE, PADDR_TOP, PAGE_BITS}; use log::debug; use sel4_common::utils::cpu_id; +use sel4_common::deps::kernel_stack_alloc; use spin::Mutex; -#[cfg(target_arch = "riscv")] +#[cfg(target_arch = "riscv64")] use riscv::register::{stvec,utvec::TrapMode}; #[cfg(target_arch = "aarch64")] -use aarch64::regs::{TPIDR_EL1,VBAR_EL1}; -#[cfg(target_arch = "aarch64")] -use aarch64::barrier::{dsb, isb}; +use aarch64_cpu::registers::*; #[cfg(target_arch = "aarch64")] -use aarch64::cache::SY; +use aarch64_cpu::asm::barrier::{dsb,isb,SY}; use crate::boot::mm::init_freemem; use crate::boot::root_server::root_server_init; @@ -111,7 +113,7 @@ fn init_cpu() -> bool { } #[cfg(not(feature = "ARM_HYPERVISOR_SUPPORT"))] { - TPIDR_EL1::write(stack_address) + TPIDR_EL1.set(stack_top) } // CPU's exception vector table extern "C" { @@ -121,16 +123,16 @@ fn init_cpu() -> bool { dsb(SY); #[cfg(not(feature = "ARM_HYPERVISOR_SUPPORT"))] { - VBAR_EL1::write(arm_vector_table as usize); + VBAR_EL1.set(arm_vector_table as u64); } #[cfg(feature = "ARM_HYPERVISOR_SUPPORT")] { // TODO: the rcore-os/aarh64 module have no vbar_el2 - // VBAR_EL2::write(arm_vector_table as usize); + VBAR_EL2.set(arm_vector_table as u64); } - isb(); + isb(SY); } - // disable fpu + // fpu, TODO, we haven't realized it now let mut haveHWFPU:bool; // detect have fpu haveHWFPU = fpu::fpsimd_HWCapTest(); @@ -295,9 +297,9 @@ pub fn try_init_kernel( let bi_frame_vptr = ipcbuf_vptr + BIT!(PAGE_BITS); let extra_bi_frame_vptr = bi_frame_vptr + BIT!(BI_FRAME_SIZE_BITS); rust_map_kernel_window(); - #[cfg(target_arch = "riscv")] + #[cfg(target_arch = "riscv64")] init_cpu(); - #[cfg(target_arch = "riscv")] + #[cfg(target_arch = "aarch64")] if init_cpu() == false { debug!("ERROR: CPU init failed"); return false; diff --git a/src/lib.rs b/src/lib.rs index 813759a..565df81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,8 +24,6 @@ mod structures; mod object; #[cfg(target_arch = "riscv64")] mod riscv; -#[cfg(target_arch = "aarch64")] -mod aarch64; mod syscall; mod boot; mod interrupt; From 680ab9738175ca88cd1bd994efbe595c1d9d313f Mon Sep 17 00:00:00 2001 From: ZhiyuanSue <2262387848@qq.com> Date: Wed, 3 Jul 2024 16:23:21 +0800 Subject: [PATCH 08/10] try to add gic_v2 definations --- src/arm_gic/gic_v2/consts.rs | 7 +++ src/arm_gic/gic_v2/gic_v2.rs | 0 src/arm_gic/gic_v2/mod.rs | 95 ++++++++++++++++++++++++++++++++++++ src/arm_gic/gic_v3/consts.rs | 0 src/arm_gic/gic_v3/gic_v3.rs | 0 src/arm_gic/gic_v3/mod.rs | 2 + src/arm_gic/mod.rs | 2 + src/boot/gic_v2.rs | 5 -- src/boot/mod.rs | 4 +- src/lib.rs | 2 + 10 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 src/arm_gic/gic_v2/consts.rs create mode 100644 src/arm_gic/gic_v2/gic_v2.rs create mode 100644 src/arm_gic/gic_v2/mod.rs create mode 100644 src/arm_gic/gic_v3/consts.rs create mode 100644 src/arm_gic/gic_v3/gic_v3.rs create mode 100644 src/arm_gic/gic_v3/mod.rs create mode 100644 src/arm_gic/mod.rs delete mode 100644 src/boot/gic_v2.rs diff --git a/src/arm_gic/gic_v2/consts.rs b/src/arm_gic/gic_v2/consts.rs new file mode 100644 index 0000000..253658c --- /dev/null +++ b/src/arm_gic/gic_v2/consts.rs @@ -0,0 +1,7 @@ +// the reg start physical addr of gic v2 +// but I don't know the virtual addr now +// TODO +pub const GIC_V2_PPTR:usize = 0x8000000; +pub const GIC_V2_DISTRIBUTOR_PPTR: usize = GIC_V2_PPTR; +pub const GIC_V2_CONTROLLER_PPTR:usize = GIC_V2_DISTRIBUTOR_PPTR + 0x10000; +pub const GIC_V2_VCPUCTRL_PPTR:usize = GIC_V2_CONTROLLER_PPTR + 0x10000; \ No newline at end of file diff --git a/src/arm_gic/gic_v2/gic_v2.rs b/src/arm_gic/gic_v2/gic_v2.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/arm_gic/gic_v2/mod.rs b/src/arm_gic/gic_v2/mod.rs new file mode 100644 index 0000000..7e2d875 --- /dev/null +++ b/src/arm_gic/gic_v2/mod.rs @@ -0,0 +1,95 @@ +pub mod consts; +pub mod gic_v2; + +use core::ptr::NonNull; + +use crate::{TriggerMode, GIC_MAX_IRQ, SPI_RANGE}; +use tock_registers::interfaces::{Readable, Writeable}; +use tock_registers::register_structs; +use tock_registers::registers::{ReadOnly, ReadWrite, WriteOnly}; + +register_structs!{ + /// GIC Distributor registers. + #[allow(non_snake_case)] + gic_dist_map_regs{ + (0x000 => enable:ReadWrite), + (0x0004 => ic_type: ReadOnly), + (0x0008 => dist_ident: ReadOnly), + (0x000c => _reserved_1), + (0x0080 => security: [ReadWrite; 0x20]), + (0x0100 => enable_set: [ReadWrite; 0x20]), + (0x0180 => enable_clr: [ReadWrite; 0x20]), + (0x0200 => pending_set: [ReadWrite; 0x20]), + (0x0280 => pending_clr: [ReadWrite; 0x20]), + (0x0300 => active: [ReadWrite; 0x20]), + (0x0380 => res2: [ReadWrite; 0x20]), + (0x0400 => priority: [ReadWrite; 0xff]), + (0x07fC => _reserved_3), + (0x0800 => targets: [ReadWrite; 0xff]), + (0x0bfc => _reserved_4), + (0x0c00 => config: [ReadWrite; 0x40]), + (0x0d00 => spi: [ReadWrite; 0x20]), + (0x0d80 => _reserved_5), + (0x0dd4 => legacy_int: [ReadWrite]), + (0x0dd8 => _reserved_7), + (0x0de0 => match_d: [ReadWrite]), + (0x0de4 => enable_d: [ReadWrite]), + (0x0de8 => _reserved_8), + (0x0f00 => sgi_control: WriteOnly), + (0x0f04 => _reserved_9), + (0x0f10 => sgi_pending_clr: [ReadWrite; 0x4]), + (0x0f20 => _reserved_10), + (0x0fc0 => periph_id: [ReadWrite; 0x12]), + (0x0ff0 => component_id: [ReadWrite; 0x4]), + (0x1000 => @END), + } +} + +register_structs! { + /// GIC CPU Interface registers. + #[allow(non_snake_case)] + struct gic_cpu_iface_map_regs { + (0x0000 => icontrol: ReadWrite), + (0x0004 => pri_msk_c: ReadWrite), + (0x0008 => pb_c: ReadWrite), + (0x000c => int_ack: ReadOnly), + (0x0010 => eoi: WriteOnly), + (0x0014 => run_priority: ReadOnly), + (0x0018 => hi_pend: ReadOnly), + (0x001c => ns_alias_bp_c: ReadWrite), + + (0x0020 => ns_alias_ack: ReadWrite), + (0x0024 => ns_alias_eoi: ReadWrite), + (0x0028 => ns_alias_hi_pend: ReadWrite), + (0x002c => _reserved_1), + + (0x0040 => integ_en_c: ReadWrite), + (0x0044 => interrupt_out: ReadWrite), + (0x0048 => _reserved_2), + + (0x0050 => match_c: ReadWrite), + (0x0054 => enable_c: ReadWrite), + (0x0058 => _reserved_3), + + (0x00D0 => active_priority: [ReadWrite, 0x4]), + (0x00E0 => ns_active_priority: [ReadWrite, 0x4]), + + (0x00f0 => _reserved_4), + + (0x00fc => cpu_if_ident: ReadOnly), + (0x0100 => _reserved_5), + + (0x0fc0 => periph_id: [ReadWrite; 0x8]), //PL390 only + (0x0ff0 => component_id: [ReadWrite; 0x4]), //PL390 only + /// Deactivate Interrupt Register. + (0x1000 => @END), + } +} + +use spin::Mutex; + +static GIC_CPUIFACE : Mutex = Mutex::new(GicCpuInterface::new(GIC_V2_CONTROLLER_PPTR)); +// This is for aarch64 only +pub fn cpu_iface_init() { + GIC_CPUIFACE +} \ No newline at end of file diff --git a/src/arm_gic/gic_v3/consts.rs b/src/arm_gic/gic_v3/consts.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/arm_gic/gic_v3/gic_v3.rs b/src/arm_gic/gic_v3/gic_v3.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/arm_gic/gic_v3/mod.rs b/src/arm_gic/gic_v3/mod.rs new file mode 100644 index 0000000..51ad737 --- /dev/null +++ b/src/arm_gic/gic_v3/mod.rs @@ -0,0 +1,2 @@ +pub mod gic_v3; +pub mod const; \ No newline at end of file diff --git a/src/arm_gic/mod.rs b/src/arm_gic/mod.rs new file mode 100644 index 0000000..d217850 --- /dev/null +++ b/src/arm_gic/mod.rs @@ -0,0 +1,2 @@ +pub mod gic_v2; +pub mod gic_v3; \ No newline at end of file diff --git a/src/boot/gic_v2.rs b/src/boot/gic_v2.rs deleted file mode 100644 index 687c8d9..0000000 --- a/src/boot/gic_v2.rs +++ /dev/null @@ -1,5 +0,0 @@ - -// This is for aarch64 only -pub fn cpu_iface_init() { - -} \ No newline at end of file diff --git a/src/boot/mod.rs b/src/boot/mod.rs index 79decf5..3935cce 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -84,6 +84,8 @@ fn init_cpu() { #[cfg(target_arch = "aarch64")] fn init_cpu() -> bool { + use arm_gic::gic_v2; + #[cfg(feature = "ARM_HYPERVISOR_SUPPORT")] { // TODO @@ -153,7 +155,7 @@ fn init_cpu() -> bool { } } // initLocalIRQController - + gic_v2::cpu_iface_init(); // armv_init_user_access //initTimer diff --git a/src/lib.rs b/src/lib.rs index 565df81..7b33534 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,8 @@ mod structures; mod object; #[cfg(target_arch = "riscv64")] mod riscv; +#[cfg(target_arch = "aarch64")] +mod arm_gic; mod syscall; mod boot; mod interrupt; From 950b9575c6cd3b38d961c045a2d943d084e4d30d Mon Sep 17 00:00:00 2001 From: ZhiyuanSue <2262387848@qq.com> Date: Wed, 3 Jul 2024 17:33:44 +0800 Subject: [PATCH 09/10] seems finish init cpu --- Cargo.toml | 2 ++ build.py | 15 +++++----- src/arm_gic/gic_v2/consts.rs | 6 +++- src/arm_gic/gic_v2/gic_v2.rs | 43 +++++++++++++++++++++++++++ src/arm_gic/gic_v2/mod.rs | 57 ++++++++++++++++++++++++++++-------- src/arm_gic/gic_v3/mod.rs | 2 +- src/boot/fpu.rs | 2 +- src/boot/mod.rs | 17 ++++++----- src/boot/user_access.rs | 12 ++++++++ src/deps.rs | 3 ++ 10 files changed, 129 insertions(+), 30 deletions(-) create mode 100644 src/boot/user_access.rs diff --git a/Cargo.toml b/Cargo.toml index 6b4e3dd..cf9df6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ buddy_system_allocator = "0.6" riscv = { git = "https://gitee.com/rcore-os/riscv", features = ["inline-asm"] } aarch64-cpu = "9.4.0" log = "0.4" +tock-registers = "0.8" spin = { version = "0.9", features = ["use_ticket_mutex"] } sel4_common = { path = "./sel4_common" } sel4_cspace = { path = "./sel4_cspace" } @@ -26,6 +27,7 @@ ENABLE_SMP = ["sel4_common/ENABLE_SMP", "sel4_task/ENABLE_SMP", "sel4_vspace/ENA ARM_HYPERVISOR_SUPPORT = ["sel4_common/ARM_HYPERVISOR_SUPPORT"] HARDWARE_DEBUG_API = [] HAVE_FPU = [] +PLAT_QEMU_ARM_VIRT = [] [profile.release] lto = true diff --git a/build.py b/build.py index 30a9248..6d58991 100755 --- a/build.py +++ b/build.py @@ -58,14 +58,15 @@ def clean_config(): clean_config() sys.exit(-1) else: + shell_command = "cargo build --release --target " + args.arch + if args.cpu_nums > 1: - if not exec_shell("cargo build --release --target " + args.arch + " --features ENABLE_SMP"): - clean_config() - sys.exit(-1) - else: - if not exec_shell("cargo build --release --target " + args.arch): - clean_config() - sys.exit(-1) + shell_command += " --features ENABLE_SMP" + if args.arch == "aarch64-unknown-linux-gnu": + shell_command += " --features PLAT_QEMU_ARM_VIRT" + if not exec_shell(shell_command): + clean_config() + sys.exit(-1) if args.cpu_nums > 1: shell_command = "" diff --git a/src/arm_gic/gic_v2/consts.rs b/src/arm_gic/gic_v2/consts.rs index 253658c..9dc8435 100644 --- a/src/arm_gic/gic_v2/consts.rs +++ b/src/arm_gic/gic_v2/consts.rs @@ -4,4 +4,8 @@ pub const GIC_V2_PPTR:usize = 0x8000000; pub const GIC_V2_DISTRIBUTOR_PPTR: usize = GIC_V2_PPTR; pub const GIC_V2_CONTROLLER_PPTR:usize = GIC_V2_DISTRIBUTOR_PPTR + 0x10000; -pub const GIC_V2_VCPUCTRL_PPTR:usize = GIC_V2_CONTROLLER_PPTR + 0x10000; \ No newline at end of file +pub const GIC_V2_VCPUCTRL_PPTR:usize = GIC_V2_CONTROLLER_PPTR + 0x10000; + +pub const IRQ_SET_ALL:usize = 0xffffffff; +pub const IRQ_MASK:usize = (1 << (10)) - 1; +pub const IRQ_NONE:usize = 1023; \ No newline at end of file diff --git a/src/arm_gic/gic_v2/gic_v2.rs b/src/arm_gic/gic_v2/gic_v2.rs index e69de29..8ab10f8 100644 --- a/src/arm_gic/gic_v2/gic_v2.rs +++ b/src/arm_gic/gic_v2/gic_v2.rs @@ -0,0 +1,43 @@ +use spin::Mutex; +use super::{Gic_Dist_Map,Gic_Cpu_Iface_Map}; +use super::consts::*; + +static GIC_DIST : Gic_Dist_Map = Gic_Dist_Map::new(GIC_V2_DISTRIBUTOR_PPTR as *mut u8); +static GIC_CPUIFACE : Gic_Cpu_Iface_Map = Gic_Cpu_Iface_Map::new(GIC_V2_CONTROLLER_PPTR as *mut u8); +// This is for aarch64 only +pub fn cpu_iface_init() { + GIC_DIST.regs().enable_clr[0].set(IRQ_SET_ALL); + GIC_DIST.regs().pending_clr[0].set(IRQ_SET_ALL); + + #[cfg(all(feature = "ARM_HYPERVISOR_SUPPORT",not(feature = "PLAT_QEMU_ARM_VIRT")))] + { + GIC_DIST.regs().security[0].set(0xffffffff); + GIC_DIST.regs().priority[0].set(0x80808080); + } + #[cfg(any(not(feature = "ARM_HYPERVISOR_SUPPORT"),feature = "PLAT_QEMU_ARM_VIRT"))] + { + GIC_DIST.regs().security[0].set(0); + GIC_DIST.regs().priority[0].set(0x0); + } + + let mut i=0; + while i<16 { + GIC_DIST.regs().sgi_pending_clr[i>>2].set(IRQ_SET_ALL); + i+=4; + } + + GIC_CPUIFACE.regs().icontrol.set(0); + GIC_CPUIFACE.regs().pri_msk_c.set(0); + GIC_CPUIFACE.regs().pb_c.set(0); + let mut i = GIC_CPUIFACE.regs().int_ack.set(0); + while ((i & IRQ_MASK) != IRQ_NONE){ + GIC_CPUIFACE.regs().eoi.set(0); + i = GIC_CPUIFACE.regs().int_ack.set(0); + } + GIC_CPUIFACE.regs().icontrol.set(1); +} + +pub fn cpu_initLocalIRQController() +{ + cpu_iface_init(); +} \ No newline at end of file diff --git a/src/arm_gic/gic_v2/mod.rs b/src/arm_gic/gic_v2/mod.rs index 7e2d875..f5797b3 100644 --- a/src/arm_gic/gic_v2/mod.rs +++ b/src/arm_gic/gic_v2/mod.rs @@ -3,7 +3,6 @@ pub mod gic_v2; use core::ptr::NonNull; -use crate::{TriggerMode, GIC_MAX_IRQ, SPI_RANGE}; use tock_registers::interfaces::{Readable, Writeable}; use tock_registers::register_structs; use tock_registers::registers::{ReadOnly, ReadWrite, WriteOnly}; @@ -11,7 +10,7 @@ use tock_registers::registers::{ReadOnly, ReadWrite, WriteOnly}; register_structs!{ /// GIC Distributor registers. #[allow(non_snake_case)] - gic_dist_map_regs{ + Gic_Dist_Map_Regs{ (0x000 => enable:ReadWrite), (0x0004 => ic_type: ReadOnly), (0x0008 => dist_ident: ReadOnly), @@ -30,10 +29,10 @@ register_structs!{ (0x0c00 => config: [ReadWrite; 0x40]), (0x0d00 => spi: [ReadWrite; 0x20]), (0x0d80 => _reserved_5), - (0x0dd4 => legacy_int: [ReadWrite]), + (0x0dd4 => legacy_int: ReadWrite), (0x0dd8 => _reserved_7), - (0x0de0 => match_d: [ReadWrite]), - (0x0de4 => enable_d: [ReadWrite]), + (0x0de0 => match_d: ReadWrite), + (0x0de4 => enable_d: ReadWrite), (0x0de8 => _reserved_8), (0x0f00 => sgi_control: WriteOnly), (0x0f04 => _reserved_9), @@ -48,7 +47,7 @@ register_structs!{ register_structs! { /// GIC CPU Interface registers. #[allow(non_snake_case)] - struct gic_cpu_iface_map_regs { + Gic_Cpu_Iface_Map_Regs { (0x0000 => icontrol: ReadWrite), (0x0004 => pri_msk_c: ReadWrite), (0x0008 => pb_c: ReadWrite), @@ -71,8 +70,8 @@ register_structs! { (0x0054 => enable_c: ReadWrite), (0x0058 => _reserved_3), - (0x00D0 => active_priority: [ReadWrite, 0x4]), - (0x00E0 => ns_active_priority: [ReadWrite, 0x4]), + (0x00D0 => active_priority: [ReadWrite; 0x4]), + (0x00E0 => ns_active_priority: [ReadWrite; 0x4]), (0x00f0 => _reserved_4), @@ -86,10 +85,42 @@ register_structs! { } } -use spin::Mutex; +pub struct Gic_Dist_Map { + base: NonNull, +} + +pub struct Gic_Cpu_Iface_Map { + base: NonNull, +} + +unsafe impl Send for Gic_Dist_Map {} +unsafe impl Sync for Gic_Dist_Map {} + +unsafe impl Send for Gic_Cpu_Iface_Map {} +unsafe impl Sync for Gic_Cpu_Iface_Map {} + +impl Gic_Dist_Map { + /// Construct a new GIC distributor instance from the base address. + pub const fn new(base: *mut u8) -> Self { + Self { + base: NonNull::new(base).unwrap().cast(), + } + } + + pub const fn regs(&self) -> &Gic_Dist_Map_Regs { + unsafe { self.base.as_ref() } + } +} + +impl Gic_Cpu_Iface_Map { + /// Construct a new GIC CPU interface instance from the base address. + pub const fn new(base: *mut u8) -> Self { + Self { + base: NonNull::new(base).unwrap().cast(), + } + } -static GIC_CPUIFACE : Mutex = Mutex::new(GicCpuInterface::new(GIC_V2_CONTROLLER_PPTR)); -// This is for aarch64 only -pub fn cpu_iface_init() { - GIC_CPUIFACE + pub const fn regs(&self) -> &Gic_Cpu_Iface_Map_Regs { + unsafe { self.base.as_ref() } + } } \ No newline at end of file diff --git a/src/arm_gic/gic_v3/mod.rs b/src/arm_gic/gic_v3/mod.rs index 51ad737..e0b71c9 100644 --- a/src/arm_gic/gic_v3/mod.rs +++ b/src/arm_gic/gic_v3/mod.rs @@ -1,2 +1,2 @@ pub mod gic_v3; -pub mod const; \ No newline at end of file +pub mod consts; \ No newline at end of file diff --git a/src/boot/fpu.rs b/src/boot/fpu.rs index 4eff707..3232746 100644 --- a/src/boot/fpu.rs +++ b/src/boot/fpu.rs @@ -1,6 +1,6 @@ // This file is for aarch64 only #[cfg(target_arch = "aarch64")] -use aarch64_cpu::barrier::{dsb,isb}; +use aarch64_cpu::asm::barrier::{dsb,isb}; #[cfg(target_arch = "aarch64")] pub fn fpsimd_HWCapTest() -> bool { // TODO diff --git a/src/boot/mod.rs b/src/boot/mod.rs index 3935cce..bdd24f4 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -6,12 +6,13 @@ mod utils; mod interface; #[cfg(target_arch = "aarch64")] mod fpu; + #[cfg(target_arch = "aarch64")] -mod gic_v2; +mod user_access; use core::mem::size_of; -use crate::deps::{tcbDebugAppend, init_plat}; +use crate::deps::{tcbDebugAppend, init_plat, initTimer}; use crate::{BIT, ROUND_UP}; use sel4_common::sel4_config::{seL4_PageBits, CONFIG_KERNEL_STACK_BITS, CURRENT_CPU_INDEX, KERNEL_ELF_BASE, PADDR_TOP, PAGE_BITS}; use log::debug; @@ -24,6 +25,8 @@ use riscv::register::{stvec,utvec::TrapMode}; use aarch64_cpu::registers::*; #[cfg(target_arch = "aarch64")] use aarch64_cpu::asm::barrier::{dsb,isb,SY}; +#[cfg(target_arch = "aarch64")] +use crate::arm_gic::gic_v2; use crate::boot::mm::init_freemem; use crate::boot::root_server::root_server_init; @@ -107,8 +110,8 @@ fn init_cpu() -> bool { // #endif } // Setup kernel stack pointer. - let mut stack_top:usize = kernel_stack_alloc[CURRENT_CPU_INDEX] + 1< bool { } } // initLocalIRQController - gic_v2::cpu_iface_init(); + gic_v2::gic_v2::cpu_initLocalIRQController(); // armv_init_user_access - + user_access::armv_init_user_access(); //initTimer - + initTimer(); true } diff --git a/src/boot/user_access.rs b/src/boot/user_access.rs new file mode 100644 index 0000000..0ece957 --- /dev/null +++ b/src/boot/user_access.rs @@ -0,0 +1,12 @@ + +pub fn check_export_pmu(){ + // TODO +} +pub fn check_export_arch_timer(){ + // TODO +} + +pub fn armv_init_user_access(){ + check_export_pmu(); + check_export_arch_timer(); +} \ No newline at end of file diff --git a/src/deps.rs b/src/deps.rs index c7bda28..6cdb594 100644 --- a/src/deps.rs +++ b/src/deps.rs @@ -1,6 +1,9 @@ use sel4_task::tcb_t; extern "C" { + // in arm sel4,init timer have different realization on different platform + // so I think no need to realization them all + pub fn initTimer(); pub fn init_plat(); pub fn tcbDebugAppend(action: *mut tcb_t); pub fn tcbDebugRemove(tcb: *mut tcb_t); From 7a97a18a54317f374327390d3587833d51a35247 Mon Sep 17 00:00:00 2001 From: ZhiyuanSue <2262387848@qq.com> Date: Wed, 3 Jul 2024 18:18:12 +0800 Subject: [PATCH 10/10] try to rebase on the mi_dev --- .cargo/config.toml | 21 ++ Cargo.toml | 15 +- build.py | 39 +-- makefile | 15 +- rel4kernel.code-workspace | 55 +++ rust-toolchain.toml | 4 + sel4_common/src/arch/aarch64.rs | 33 ++ sel4_common/src/arch/mod.rs | 9 + sel4_common/src/{ => arch}/sbi.rs | 5 +- sel4_common/src/console.rs | 10 +- sel4_common/src/deps.rs | 5 +- sel4_common/src/fault.rs | 13 +- sel4_common/src/lib.rs | 19 +- sel4_common/src/logging.rs | 4 +- sel4_common/src/message_info.rs | 10 +- sel4_common/src/object.rs | 10 +- sel4_common/src/sel4_config.rs | 8 - sel4_common/src/smp.rs | 14 +- sel4_common/src/structures.rs | 3 +- sel4_common/src/utils.rs | 22 +- sel4_cspace/src/cap/mod.rs | 54 +-- sel4_cspace/src/cap/zombie.rs | 8 +- sel4_cspace/src/cap_rights.rs | 5 +- sel4_cspace/src/compatibility.rs | 8 +- sel4_cspace/src/cte.rs | 102 ++++-- sel4_cspace/src/deps.rs | 4 +- sel4_cspace/src/interface.rs | 7 +- sel4_cspace/src/lib.rs | 8 +- sel4_cspace/src/structures.rs | 4 +- sel4_ipc/src/endpoint.rs | 45 ++- sel4_ipc/src/lib.rs | 3 +- sel4_ipc/src/notification.rs | 24 +- sel4_ipc/src/transfer.rs | 142 ++++++-- sel4_task/src/deps.rs | 3 +- sel4_task/src/lib.rs | 17 +- sel4_task/src/scheduler.rs | 158 +++++---- sel4_task/src/structures.rs | 2 +- sel4_task/src/tcb.rs | 171 ++++++---- sel4_task/src/tcb_queue.rs | 3 +- sel4_task/src/thread_state.rs | 6 +- sel4_vspace/src/arch/aarch64.rs | 1 + sel4_vspace/src/arch/mod.rs | 4 + sel4_vspace/src/arch/riscv64.rs | 1 + sel4_vspace/src/asid.rs | 35 +- sel4_vspace/src/interface.rs | 47 ++- sel4_vspace/src/lib.rs | 30 +- sel4_vspace/src/pte.rs | 47 ++- sel4_vspace/src/satp.rs | 15 +- sel4_vspace/src/structures.rs | 7 +- sel4_vspace/src/utils.rs | 11 +- sel4_vspace/src/vm_rights.rs | 5 +- src/.cargo/config | 6 +- .../aarch64}/arm_gic/gic_v2/consts.rs | 0 .../aarch64}/arm_gic/gic_v2/gic_v2.rs | 0 src/{ => arch/aarch64}/arm_gic/gic_v2/mod.rs | 0 .../aarch64}/arm_gic/gic_v3/consts.rs | 0 .../aarch64}/arm_gic/gic_v3/gic_v3.rs | 0 src/{ => arch/aarch64}/arm_gic/gic_v3/mod.rs | 0 src/{ => arch/aarch64}/arm_gic/mod.rs | 0 src/arch/aarch64/c_traps.rs | 154 +++++++++ src/{ => arch/aarch64}/exception.rs | 8 +- src/arch/aarch64/mod.rs | 80 +++++ src/arch/mod.rs | 9 + src/{kernel => arch/riscv}/c_traps.rs | 47 +-- src/arch/riscv/exception.rs | 55 +++ src/{riscv.rs => arch/riscv/mod.rs} | 11 +- src/{kernel => arch/riscv}/restore_fp.S | 0 src/boot/interface.rs | 23 +- src/boot/mm.rs | 91 +++-- src/boot/mod.rs | 90 ++--- src/boot/root_server.rs | 144 ++++---- src/boot/untyped.rs | 9 +- src/boot/utils.rs | 38 ++- src/compatibility.rs | 20 +- src/config.rs | 10 +- src/console.rs | 35 -- src/deps.rs | 7 +- src/interfaces_impl/cspace.rs | 59 ++-- src/interfaces_impl/mod.rs | 1 - src/interfaces_impl/task.rs | 0 src/interrupt/handler.rs | 15 +- src/interrupt/mod.rs | 39 +-- src/kernel/boot.rs | 21 +- src/kernel/fastpath.rs | 73 ++-- src/kernel/fault.rs | 7 +- src/kernel/mod.rs | 3 +- src/lang_items.rs | 6 +- src/lib.rs | 27 +- src/object/mod.rs | 26 +- src/sbi.rs | 0 src/structures.rs | 8 +- .../decode/decode_cnode_invocation.rs | 103 ++++-- .../decode/decode_domain_invocation.rs | 38 ++- .../decode/decode_irq_invocation.rs | 88 +++-- .../decode/decode_mmu_invocation.rs | 193 ++++++++--- .../decode/decode_tcb_invocation.rs | 317 ++++++++++++++---- .../decode/decode_untyped_invocation.rs | 97 ++++-- src/syscall/invocation/decode/mod.rs | 77 +++-- src/syscall/invocation/invoke_cnode.rs | 74 ++-- src/syscall/invocation/invoke_irq.rs | 15 +- src/syscall/invocation/invoke_mmu_op.rs | 81 +++-- src/syscall/invocation/invoke_tcb.rs | 96 ++++-- src/syscall/invocation/invoke_untyped.rs | 105 ++++-- src/syscall/invocation/mod.rs | 20 +- src/syscall/mod.rs | 40 ++- src/syscall/syscall_reply.rs | 24 +- src/syscall/utils.rs | 71 ++-- src/utils.rs | 3 +- 108 files changed, 2557 insertions(+), 1298 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 rel4kernel.code-workspace create mode 100644 sel4_common/src/arch/aarch64.rs create mode 100644 sel4_common/src/arch/mod.rs rename sel4_common/src/{ => arch}/sbi.rs (97%) create mode 100644 sel4_vspace/src/arch/aarch64.rs create mode 100644 sel4_vspace/src/arch/mod.rs create mode 100644 sel4_vspace/src/arch/riscv64.rs rename src/{ => arch/aarch64}/arm_gic/gic_v2/consts.rs (100%) rename src/{ => arch/aarch64}/arm_gic/gic_v2/gic_v2.rs (100%) rename src/{ => arch/aarch64}/arm_gic/gic_v2/mod.rs (100%) rename src/{ => arch/aarch64}/arm_gic/gic_v3/consts.rs (100%) rename src/{ => arch/aarch64}/arm_gic/gic_v3/gic_v3.rs (100%) rename src/{ => arch/aarch64}/arm_gic/gic_v3/mod.rs (100%) rename src/{ => arch/aarch64}/arm_gic/mod.rs (100%) create mode 100644 src/arch/aarch64/c_traps.rs rename src/{ => arch/aarch64}/exception.rs (98%) create mode 100644 src/arch/aarch64/mod.rs create mode 100644 src/arch/mod.rs rename src/{kernel => arch/riscv}/c_traps.rs (80%) create mode 100644 src/arch/riscv/exception.rs rename src/{riscv.rs => arch/riscv/mod.rs} (87%) rename src/{kernel => arch/riscv}/restore_fp.S (100%) delete mode 100644 src/console.rs delete mode 100644 src/interfaces_impl/task.rs delete mode 100644 src/sbi.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..dfbe992 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,21 @@ +# +# Copyright 2023, Colias Group, LLC +# +# SPDX-License-Identifier: BSD-2-Clause +# + +[build] +# target = "riscv64imac-unknown-none-elf" +# target = "riscv64imac-sel4" +target = "aarch64-unknown-none-softfloat" + +[unstable] +unstable-options = true + +[target.riscv64imac-unknown-none-elf] +rustflags = [ + "-Ztls-model=local-exec" +] + +[env] +RUST_TARGET_PATH = { value = "support/targets", relative = true } diff --git a/Cargo.toml b/Cargo.toml index cf9df6c..3459b6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,16 @@ name = "rustlib" path = "src/lib.rs" crate-type = ["staticlib"] +[workspace] +members = [ + ".", + "sel4_common", + "sel4_cspace", + "sel4_ipc", + "sel4_vspace", + "sel4_task", +] + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] @@ -22,6 +32,7 @@ sel4_cspace = { path = "./sel4_cspace" } sel4_vspace = { path = "./sel4_vspace"} sel4_task = { path = "./sel4_task" } sel4_ipc = { path = "./sel4_ipc" } + [features] ENABLE_SMP = ["sel4_common/ENABLE_SMP", "sel4_task/ENABLE_SMP", "sel4_vspace/ENABLE_SMP"] ARM_HYPERVISOR_SUPPORT = ["sel4_common/ARM_HYPERVISOR_SUPPORT"] @@ -31,7 +42,3 @@ PLAT_QEMU_ARM_VIRT = [] [profile.release] lto = true -panic = "abort" - -[profile.dev] -panic = "abort" \ No newline at end of file diff --git a/build.py b/build.py index 6d58991..c874b6c 100755 --- a/build.py +++ b/build.py @@ -23,10 +23,6 @@ def parse_args(): help="baseline switch") parser.add_argument('-c', '--cpu', dest="cpu_nums", type=int, help="kernel & qemu cpu nums", default=1) - # parser.add_argument('-a','--arch', dest="arch",type=str, - # help="kernel arch",default="riscv64imac-unknown-none-elf") - parser.add_argument('-a','--arch', dest="arch",type=str, - help="kernel arch",default="aarch64-unknown-linux-gnu") args = parser.parse_args() return args @@ -35,7 +31,7 @@ def exec_shell(shell_command): return ret_code == 0 def clean_config(): - shell_command = "cd ../kernel && git checkout 5ddda4ebdae7523830259d5b1f676d25d5de125b" + shell_command = "cd ../kernel && git checkout 552f173d3d7780b33184ebedefc58329ea5de3ba" exec_shell(shell_command) if __name__ == "__main__": @@ -43,12 +39,6 @@ def clean_config(): clean_config() progname = sys.argv[0] - if args.arch != "riscv64imac-unknown-none-elf" and args.arch != "aarch64-unknown-linux-gnu": - clean_config() - sys.exit(-1) - - print("target is:" + args.arch) - if os.path.exists(build_dir): shutil.rmtree(build_dir) os.makedirs(build_dir) @@ -58,31 +48,22 @@ def clean_config(): clean_config() sys.exit(-1) else: - shell_command = "cargo build --release --target " + args.arch - if args.cpu_nums > 1: - shell_command += " --features ENABLE_SMP" - if args.arch == "aarch64-unknown-linux-gnu": - shell_command += " --features PLAT_QEMU_ARM_VIRT" - if not exec_shell(shell_command): - clean_config() - sys.exit(-1) + if not exec_shell("cargo build --release --target riscv64imac-unknown-none-elf --features ENABLE_SMP"): + clean_config() + sys.exit(-1) + else: + if not exec_shell("cargo build --release --target riscv64imac-unknown-none-elf"): + clean_config() + sys.exit(-1) if args.cpu_nums > 1: - shell_command = "" - if args.arch == "riscv64imac-unknown-none-elf": - shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=spike -DSIMULATION=TRUE -DSMP=TRUE && ninja" - elif args.arch == "aarch64-unknown-linux-gnu": - shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=qemu-arm-virt -DSIMULATION=TRUE -DSMP=TRUE && ninja" + shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=spike -DSIMULATION=TRUE -DSMP=TRUE && ninja" if not exec_shell(shell_command): clean_config() sys.exit(-1) sys.exit(0) - shell_command = "" - if args.arch == "riscv64imac-unknown-none-elf": - shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=spike -DSIMULATION=TRUE && ninja" - elif args.arch == "aarch64-unknown-linux-gnu": - shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=qemu-arm-virt -DSIMULATION=TRUE && ninja" + shell_command = "cd ./build && ../../init-build.sh -DPLATFORM=spike -DSIMULATION=TRUE && ninja" if not exec_shell(shell_command): clean_config() sys.exit(-1) diff --git a/makefile b/makefile index 94179c5..ba80de4 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,20 @@ +ARCH := riscv64 + +ifeq ($(ARCH), riscv64) +TARGET := riscv64imac-unknown-none-elf +else ifeq ($(ARCH), aarch64) +TARGET := aarch64-unknown-none-softfloat +endif + +all: build + @echo $(ARCH) env: rustup install nightly-2023-05-01 rustup default nightly-2023-05-01 rustup target add riscv64imac-unknown-none-elf rustup component add rust-src +build: + cargo build --release --target $(TARGET) run: - cargo build --release --target riscv64imac-unknown-none-elf \ No newline at end of file + cargo build --release --target $(TARGET) +.PHONY: all build env run diff --git a/rel4kernel.code-workspace b/rel4kernel.code-workspace new file mode 100644 index 0000000..d0bc455 --- /dev/null +++ b/rel4kernel.code-workspace @@ -0,0 +1,55 @@ +{ + "folders": [ + { + "path": "." + }, + { + "path": "../kernel/" + } + ], + "settings": { + "rust-analyzer.check.allTargets": false, + "rust-analyzer.cargo.extraEnv": {}, + "rust-analyzer.cargo.cfgs": { + "debug_assertions": null, + "miri": null + }, + "files.associations": { + "*.json.liquid": "json", + "*.yaml.liquid": "yaml", + "*.md.liquid": "markdown", + "*.js.liquid": "liquid-javascript", + "*.css.liquid": "liquid-css", + "*.scss.liquid": "liquid-scss", + "config.h": "c", + "types.h": "c", + "compound_types.h": "c", + "bootinfo.h": "c", + "boot.h": "c", + "structures.h": "c", + "stdint.h": "c", + "autoconf.h": "c", + "basic_types.h": "c", + "thread.h": "c", + "registerset.h": "c", + "gen_config.h": "c", + "structures_gen.h": "c", + "hardware.h": "c", + "assert.h": "c", + "objecttype.h": "c", + "invocation.h": "c", + "syscall.h": "c", + "interrupt.h": "c", + "constants.h": "c", + "macros.h": "c", + "stdint-gcc.h": "c", + "lock.h": "c", + "failures.h": "c", + "shared_types_gen.h": "c", + "shared_types.h": "c", + "statedata.h": "c", + "vspace.h": "c", + "cnode.h": "c" + } + } +} diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 3f8b98d..47cb9e4 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -9,3 +9,7 @@ [toolchain] channel = "nightly-2024-02-01" components = [ "rustfmt", "rust-src" ] +targets = [ + "riscv64imac-unknown-none-elf", + "aarch64-unknown-none-softfloat" +] diff --git a/sel4_common/src/arch/aarch64.rs b/sel4_common/src/arch/aarch64.rs new file mode 100644 index 0000000..e19d160 --- /dev/null +++ b/sel4_common/src/arch/aarch64.rs @@ -0,0 +1,33 @@ +#![allow(unused)] + +pub fn set_timer(timer: usize) { + todo!("set_timer") +} + +pub fn console_putchar(c: usize) { + todo!("console_putchar") +} + +pub fn console_getchar() -> usize { + todo!("console_getchar") +} + +pub fn clear_ipi() { + todo!("clear_ipi") +} +pub fn shutdown() -> ! { + todo!("shutdown"); + panic!("It should shutdown!"); +} + +pub fn sys_write(fd: usize, buffer: &[u8]) { + todo!("sys_write"); +} + +pub fn remote_sfence_vma(hart_mask: usize, start: usize, size: usize) { + todo!("remote_sfence_vma"); +} + +pub fn get_time() -> usize { + todo!("get_time") +} diff --git a/sel4_common/src/arch/mod.rs b/sel4_common/src/arch/mod.rs new file mode 100644 index 0000000..c57405a --- /dev/null +++ b/sel4_common/src/arch/mod.rs @@ -0,0 +1,9 @@ +#[cfg(target_arch = "riscv64")] +pub mod sbi; +#[cfg(target_arch = "riscv64")] +pub use sbi::{console_putchar, get_time, set_timer, shutdown}; + +#[cfg(target_arch = "aarch64")] +mod aarch64; +#[cfg(target_arch = "aarch64")] +pub use aarch64::{console_putchar, get_time, set_timer, shutdown}; diff --git a/sel4_common/src/sbi.rs b/sel4_common/src/arch/sbi.rs similarity index 97% rename from sel4_common/src/sbi.rs rename to sel4_common/src/arch/sbi.rs index 9bd794b..9b66d22 100644 --- a/sel4_common/src/sbi.rs +++ b/sel4_common/src/arch/sbi.rs @@ -9,7 +9,7 @@ const SBI_CONSOLE_GETCHAR: usize = 2; const SBI_CLEAR_IPI: usize = 3; const SBI_REMOTE_SFENCE_VMA: usize = 6; const SBI_SHUTDOWN: usize = 8; -const SYSCALL_WRITE:usize =64; +const SYSCALL_WRITE: usize = 64; #[no_mangle] pub fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize { @@ -26,8 +26,6 @@ pub fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize { ret } - - pub fn set_timer(timer: usize) { sbi_call(SBI_SET_TIMER, timer, 0, 0); } @@ -52,7 +50,6 @@ pub fn sys_write(fd: usize, buffer: &[u8]) { sbi_call(SYSCALL_WRITE, fd, buffer.as_ptr() as usize, buffer.len()); } - pub fn remote_sfence_vma(hart_mask: usize, start: usize, size: usize) { let virt_addr_hart_mask = (&hart_mask) as *const usize as usize; sbi_call(SBI_REMOTE_SFENCE_VMA, virt_addr_hart_mask, 0, 0); diff --git a/sel4_common/src/console.rs b/sel4_common/src/console.rs index 88c866b..abb5b4a 100644 --- a/sel4_common/src/console.rs +++ b/sel4_common/src/console.rs @@ -1,16 +1,10 @@ //! SBI console driver, for text output -#[cfg(target_arch = "riscv64")] -use super::sbi::console_putchar; +use super::arch::console_putchar; use core::fmt::{self, Write}; struct Stdout; -#[cfg(target_arch = "aarch64")] -pub fn console_putchar(c: usize) { - // tmp empty funcion and useless, TODO: clean -} - impl Write for Stdout { fn write_str(&mut self, s: &str) -> fmt::Result { for c in s.chars() { @@ -38,4 +32,4 @@ macro_rules! println { ($fmt: literal $(, $($arg: tt)+)?) => { $crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)); } -} \ No newline at end of file +} diff --git a/sel4_common/src/deps.rs b/sel4_common/src/deps.rs index f4e8c90..c47bf9b 100644 --- a/sel4_common/src/deps.rs +++ b/sel4_common/src/deps.rs @@ -1,11 +1,8 @@ - extern "C" { pub fn kernel_stack_alloc(); } - - #[cfg(feature = "ENABLE_SMP")] extern "C" { pub fn coreMap(); -} \ No newline at end of file +} diff --git a/sel4_common/src/fault.rs b/sel4_common/src/fault.rs index 90cc1db..9069403 100644 --- a/sel4_common/src/fault.rs +++ b/sel4_common/src/fault.rs @@ -33,13 +33,10 @@ pub enum FaultType { impl seL4_Fault_t { pub fn get_fault_type(&self) -> FaultType { - unsafe { - core::mem::transmute::(self.get_type() as u8) - } + unsafe { core::mem::transmute::(self.get_type() as u8) } } } - pub const seL4_Fault_NullFault: usize = FaultType::NullFault as usize; pub const seL4_Fault_CapFault: usize = FaultType::CapFault as usize; pub const seL4_Fault_UnknownSyscall: usize = FaultType::UnknownSyscall as usize; @@ -62,9 +59,6 @@ pub const seL4_CapFault_DepthMismatch_BitsFound: usize = 5; pub const seL4_CapFault_GuardMismatch_GuardFound: usize = seL4_CapFault_DepthMismatch_BitsFound; pub const seL4_CapFault_GuardMismatch_BitsFound: usize = 6; - - - // lookup_fault #[derive(PartialEq, Eq)] pub enum LookupFaultType { @@ -79,7 +73,6 @@ pub const lookup_fault_missing_capability: usize = LookupFaultType::MissingCap a pub const lookup_fault_depth_mismatch: usize = LookupFaultType::DepthMismatch as usize; pub const lookup_fault_guard_mismatch: usize = LookupFaultType::GuardMismatch as usize; - plus_define_bitfield! { lookup_fault_t, 2, 0, 0, 2 => { new_root_invalid, lookup_fault_invalid_root => {}, @@ -100,8 +93,6 @@ plus_define_bitfield! { impl lookup_fault_t { pub fn get_lookup_fault_type(&self) -> LookupFaultType { - unsafe { - core::mem::transmute::(self.get_type() as u8) - } + unsafe { core::mem::transmute::(self.get_type() as u8) } } } diff --git a/sel4_common/src/lib.rs b/sel4_common/src/lib.rs index 5260b0b..3e6c3f0 100644 --- a/sel4_common/src/lib.rs +++ b/sel4_common/src/lib.rs @@ -3,17 +3,16 @@ #![allow(non_camel_case_types)] #![allow(non_upper_case_globals)] -#[cfg(feature = "ENABLE_SMP")] -pub mod smp; -pub mod deps; -pub mod sel4_config; -pub mod structures; -pub mod utils; -#[cfg(target_arch = "riscv64")] -pub mod sbi; +pub mod arch; mod console; +mod deps; +pub mod fault; pub mod logging; pub mod message_info; pub mod object; -pub mod fault; -pub mod registers; \ No newline at end of file +pub mod registers; +pub mod sel4_config; +#[cfg(feature = "ENABLE_SMP")] +pub mod smp; +pub mod structures; +pub mod utils; diff --git a/sel4_common/src/logging.rs b/sel4_common/src/logging.rs index 7da320c..b71d086 100644 --- a/sel4_common/src/logging.rs +++ b/sel4_common/src/logging.rs @@ -6,8 +6,8 @@ use log::{self, Level, LevelFilter, Log, Metadata, Record}; -use crate::println; use super::utils::cpu_id; +use crate::println; use spin::Mutex; @@ -54,4 +54,4 @@ pub fn init() { Some("TRACE") => LevelFilter::Trace, _ => LevelFilter::Debug, }); -} \ No newline at end of file +} diff --git a/sel4_common/src/message_info.rs b/sel4_common/src/message_info.rs index 75e8ec5..230dd56 100644 --- a/sel4_common/src/message_info.rs +++ b/sel4_common/src/message_info.rs @@ -3,7 +3,7 @@ use crate::plus_define_bitfield; #[derive(Eq, PartialEq, Debug, Clone, Copy, PartialOrd, Ord)] pub enum MessageLabel { - InvalidInvocation = 0, + InvalidInvocation = 0, UntypedRetype, TCBReadRegisters, TCBWriteRegisters, @@ -60,9 +60,7 @@ plus_define_bitfield! { impl seL4_MessageInfo_t { #[inline] pub fn from_word(w: usize) -> Self { - Self { - words: [w] - } + Self { words: [w] } } #[inline] @@ -81,8 +79,6 @@ impl seL4_MessageInfo_t { #[inline] pub fn get_label(&self) -> MessageLabel { - unsafe { - core::mem::transmute::(self.get_usize_label() as u8) - } + unsafe { core::mem::transmute::(self.get_usize_label() as u8) } } } diff --git a/sel4_common/src/object.rs b/sel4_common/src/object.rs index 16830f4..208feb3 100644 --- a/sel4_common/src/object.rs +++ b/sel4_common/src/object.rs @@ -15,7 +15,6 @@ pub enum ObjectType { pub const seL4_ObjectTypeCount: usize = ObjectType::PageTableObject as usize + 1; - impl ObjectType { pub fn get_object_size(&self, user_object_size: usize) -> usize { match self { @@ -46,16 +45,13 @@ impl ObjectType { if value >= seL4_ObjectTypeCount { return None; } - unsafe { - Some(core::mem::transmute::(value as u8)) - } + unsafe { Some(core::mem::transmute::(value as u8)) } } pub fn is_arch_type(self) -> bool { match self { Self::GigaPageObject | Self::NormalPageObject | Self::MegaPageObject => true, - _ => false + _ => false, } } - -} \ No newline at end of file +} diff --git a/sel4_common/src/sel4_config.rs b/sel4_common/src/sel4_config.rs index a890373..e2718ca 100644 --- a/sel4_common/src/sel4_config.rs +++ b/sel4_common/src/sel4_config.rs @@ -28,7 +28,6 @@ pub const seL4_PageTableBits: usize = 12; pub const seL4_HugePageBits: usize = 30; pub const seL4_LargePageBits: usize = 21; - // ASID relevant pub const asidLowBits: usize = 9; pub const asidHighBits: usize = 7; @@ -48,7 +47,6 @@ pub const PADDR_BASE: usize = 0x0; pub const PPTR_BASE_OFFSET: usize = PPTR_BASE - PADDR_BASE; pub const PADDR_TOP: usize = PPTR_TOP - PPTR_BASE_OFFSET; - // scheduler relevant pub const CONFIG_NUM_DOMAINS: usize = 1; pub const CONFIG_NUM_PRIORITIES: usize = 256; @@ -88,7 +86,6 @@ pub const seL4_RevokeFirst: usize = 9; pub const seL4_NotEnoughMemory: usize = 10; pub const seL4_NumErrors: usize = 11; - // msg info pub const seL4_MsgMaxLength: usize = 120; pub const seL4_MsgExtraCapBits: usize = 2; @@ -98,11 +95,6 @@ pub const MessageID_Exception: usize = 1; pub const seL4_IPCBufferSizeBits: usize = 10; - pub const CONFIG_RESET_CHUNK_BITS: usize = 8; pub const CONFIG_KERNEL_STACK_BITS: usize = 12; - -// aarch64 porting TODO - -pub const CURRENT_CPU_INDEX: usize = 0; \ No newline at end of file diff --git a/sel4_common/src/smp.rs b/sel4_common/src/smp.rs index 2589a58..3f32aba 100644 --- a/sel4_common/src/smp.rs +++ b/sel4_common/src/smp.rs @@ -1,14 +1,11 @@ -use core::arch::asm; -use crate::BIT; +use super::sel4_config::{CONFIG_KERNEL_STACK_BITS, CONFIG_MAX_NUM_NODES, CPUID_MASK}; use crate::deps::{coreMap, kernel_stack_alloc}; -use super::sel4_config::{CONFIG_MAX_NUM_NODES, CONFIG_KERNEL_STACK_BITS, CPUID_MASK}; - +use crate::BIT; +use core::arch::asm; #[inline] fn get_core_map_ref() -> &'static [usize; CONFIG_MAX_NUM_NODES] { - unsafe { - &*(coreMap as usize as *const [usize; CONFIG_MAX_NUM_NODES]) - } + unsafe { &*(coreMap as usize as *const [usize; CONFIG_MAX_NUM_NODES]) } } #[inline] @@ -24,13 +21,12 @@ pub fn hart_id_to_core_id(hart_id: usize) -> usize { Some(core_id) => core_id, _ => 0, } - } } #[inline] pub fn get_currenct_cpu_index() -> usize { - #[cfg(target_arch = "riscv64")] + #[cfg(target_arch = "riscv64")] unsafe { let mut cur_sp: usize; asm!( diff --git a/sel4_common/src/structures.rs b/sel4_common/src/structures.rs index 14c5fb3..ff6e063 100644 --- a/sel4_common/src/structures.rs +++ b/sel4_common/src/structures.rs @@ -11,7 +11,6 @@ pub enum exception_t { padding = isize::MAX - 1, } - #[repr(C)] #[derive(Copy, Clone)] pub struct seL4_IPCBuffer { @@ -28,4 +27,4 @@ impl seL4_IPCBuffer { pub fn get_extra_cptr(&self, i: usize) -> usize { self.caps_or_badges[i] } -} \ No newline at end of file +} diff --git a/sel4_common/src/utils.rs b/sel4_common/src/utils.rs index a076bae..872c6d0 100644 --- a/sel4_common/src/utils.rs +++ b/sel4_common/src/utils.rs @@ -3,6 +3,7 @@ use crate::sel4_config::*; macro_rules! plus_define_bitfield { ($name:ident, $total_words:expr, $type_index:expr, $type_offset:expr, $type_bits:expr => { $($variant:ident, $type_value:expr => { $($field:ident, $get_field:ident, $set_field:ident, $index:expr, $offset:expr, $bits:expr, $shift:expr, $sign_ext: expr),* }),* }) => { + #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Default)] pub struct $name { pub words: [usize; $total_words], @@ -80,7 +81,6 @@ macro_rules! IS_ALIGNED { }}; } - #[macro_export] macro_rules! BIT { ($e:expr) => { @@ -98,24 +98,18 @@ pub fn MAX_FREE_INDEX(bits: usize) -> usize { #[inline] pub fn convert_to_type_ref(addr: usize) -> &'static T { assert_ne!(addr, 0); - unsafe { - & *(addr as *mut T) - } + unsafe { &*(addr as *mut T) } } #[inline] pub fn convert_to_mut_type_ref(addr: usize) -> &'static mut T { assert_ne!(addr, 0); - unsafe { - &mut *(addr as *mut T) - } + unsafe { &mut *(addr as *mut T) } } #[inline] pub fn convert_to_mut_type_ref_unsafe(addr: usize) -> &'static mut T { - unsafe { - &mut *(addr as *mut T) - } + unsafe { &mut *(addr as *mut T) } } #[inline] @@ -136,11 +130,13 @@ pub fn convert_to_option_mut_type_ref(addr: usize) -> Option<&'static mut T> #[inline] pub fn cpu_id() -> usize { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { use crate::smp::get_currenct_cpu_index; get_currenct_cpu_index() } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { 0 } } @@ -154,4 +150,4 @@ pub fn pageBitsForSize(page_size: usize) -> usize { RISCV_Giga_Page => RISCVGigaPageBits, _ => panic!("Invalid page size!"), } -} \ No newline at end of file +} diff --git a/sel4_cspace/src/cap/mod.rs b/sel4_cspace/src/cap/mod.rs index cf3b55c..cb434b0 100644 --- a/sel4_cspace/src/cap/mod.rs +++ b/sel4_cspace/src/cap/mod.rs @@ -1,6 +1,6 @@ pub mod zombie; -use sel4_common::{MASK, plus_define_bitfield, sel4_config::*, utils::pageBitsForSize}; +use sel4_common::{plus_define_bitfield, sel4_config::*, utils::pageBitsForSize, MASK}; #[repr(C)] #[derive(Clone, Copy, Debug)] @@ -42,7 +42,7 @@ pub enum CapTag { CapFrameCap = 1, CapPageTableCap = 3, CapASIDControlCap = 11, - CapASIDPoolCap = 13 + CapASIDPoolCap = 13, } /// cap_t 表示一个capability,由两个机器字组成,包含了类型、对象元数据以及指向内核对象的指针。 @@ -115,7 +115,6 @@ plus_define_bitfield! { } } - /// cap 的公用方法 impl cap_t { pub fn update_data(&self, preserve: bool, new_data: usize) -> Self { @@ -155,14 +154,12 @@ impl cap_t { new_cap.set_cnode_guard_size(guard_size); new_cap } - _ => { self.clone() } + _ => self.clone(), } } pub fn get_cap_type(&self) -> CapTag { - unsafe { - core::mem::transmute::(self.get_type() as u8) - } + unsafe { core::mem::transmute::(self.get_type() as u8) } } pub fn get_cap_ptr(&self) -> usize { @@ -176,9 +173,7 @@ impl cap_t { CapTag::CapFrameCap => self.get_frame_base_ptr(), CapTag::CapPageTableCap => self.get_pt_base_ptr(), CapTag::CapASIDPoolCap => self.get_asid_pool(), - _ => { - 0 - } + _ => 0, } } @@ -196,8 +191,15 @@ impl cap_t { pub fn get_cap_is_physical(&self) -> bool { match self.get_cap_type() { - CapTag::CapUntypedCap | CapTag::CapEndpointCap | CapTag::CapNotificationCap | CapTag::CapCNodeCap | CapTag::CapFrameCap | CapTag::CapASIDPoolCap | - CapTag::CapPageTableCap | CapTag::CapZombieCap | CapTag::CapThreadCap => true, + CapTag::CapUntypedCap + | CapTag::CapEndpointCap + | CapTag::CapNotificationCap + | CapTag::CapCNodeCap + | CapTag::CapFrameCap + | CapTag::CapASIDPoolCap + | CapTag::CapPageTableCap + | CapTag::CapZombieCap + | CapTag::CapThreadCap => true, _ => false, } } @@ -207,7 +209,6 @@ impl cap_t { } } - pub fn same_region_as(cap1: &cap_t, cap2: &cap_t) -> bool { match cap1.get_cap_type() { CapTag::CapUntypedCap => { @@ -232,7 +233,10 @@ pub fn same_region_as(cap1: &cap_t, cap2: &cap_t) -> bool { } false } - CapTag::CapEndpointCap | CapTag::CapNotificationCap | CapTag::CapPageTableCap | CapTag::CapASIDPoolCap + CapTag::CapEndpointCap + | CapTag::CapNotificationCap + | CapTag::CapPageTableCap + | CapTag::CapASIDPoolCap | CapTag::CapThreadCap => { if cap2.get_cap_type() == cap1.get_cap_type() { return cap1.get_cap_ptr() == cap2.get_cap_ptr(); @@ -252,14 +256,10 @@ pub fn same_region_as(cap1: &cap_t, cap2: &cap_t) -> bool { } false } - CapTag::CapIrqControlCap => { - match cap2.get_cap_type() { - CapTag::CapIrqControlCap | CapTag::CapIrqHandlerCap => { - true - } - _ => false - } - } + CapTag::CapIrqControlCap => match cap2.get_cap_type() { + CapTag::CapIrqControlCap | CapTag::CapIrqHandlerCap => true, + _ => false, + }, CapTag::CapIrqHandlerCap => { if cap2.get_cap_type() == CapTag::CapIrqHandlerCap { return cap1.get_irq_handler() == cap2.get_irq_handler(); @@ -277,7 +277,9 @@ pub fn same_object_as(cap1: &cap_t, cap2: &cap_t) -> bool { if cap1.get_cap_type() == CapTag::CapUntypedCap { return false; } - if cap1.get_cap_type() == CapTag::CapIrqControlCap && cap2.get_cap_type() == CapTag::CapIrqHandlerCap { + if cap1.get_cap_type() == CapTag::CapIrqControlCap + && cap2.get_cap_type() == CapTag::CapIrqHandlerCap + { return false; } if cap1.isArchCap() && cap2.isArchCap() { @@ -290,7 +292,7 @@ fn arch_same_object_as(cap1: &cap_t, cap2: &cap_t) -> bool { if cap1.get_cap_type() == CapTag::CapFrameCap && cap2.get_cap_type() == CapTag::CapFrameCap { return cap1.get_frame_base_ptr() == cap2.get_frame_base_ptr() && cap1.get_frame_size() == cap2.get_frame_size() - && (cap1.get_frame_is_device() == 0) == (cap2.get_frame_is_device() == 0) + && (cap1.get_frame_is_device() == 0) == (cap2.get_frame_is_device() == 0); } same_region_as(cap1, cap2) } @@ -319,6 +321,6 @@ pub fn is_cap_revocable(derived_cap: &cap_t, src_cap: &cap_t) -> bool { return true; } - _ => false + _ => false, } -} \ No newline at end of file +} diff --git a/sel4_cspace/src/cap/zombie.rs b/sel4_cspace/src/cap/zombie.rs index f9dbae9..5d04f56 100644 --- a/sel4_cspace/src/cap/zombie.rs +++ b/sel4_cspace/src/cap/zombie.rs @@ -1,7 +1,6 @@ -use sel4_common::MASK; -use sel4_common::sel4_config::wordRadix; use crate::cte::cte_t; - +use sel4_common::sel4_config::wordRadix; +use sel4_common::MASK; use super::{cap_t, CapTag}; @@ -37,7 +36,6 @@ impl cap_t { let ptr = self.get_zombie_id() & !MASK!(radix + 1); self.set_zombie_id(ptr | (n & MASK!(radix + 1))); } - } #[inline] @@ -60,4 +58,4 @@ pub fn ZombieType_ZombieCNode(n: usize) -> usize { pub fn capCyclicZombie(cap: &cap_t, slot: *mut cte_t) -> bool { let ptr = cap.get_zombie_ptr() as *mut cte_t; (cap.get_cap_type() == CapTag::CapZombieCap) && (ptr == slot) -} \ No newline at end of file +} diff --git a/sel4_cspace/src/cap_rights.rs b/sel4_cspace/src/cap_rights.rs index 60cfeda..4f0fb44 100644 --- a/sel4_cspace/src/cap_rights.rs +++ b/sel4_cspace/src/cap_rights.rs @@ -12,12 +12,9 @@ plus_define_bitfield! { } } - impl seL4_CapRights_t { #[inline] pub fn from_word(word: usize) -> Self { - Self { - words: [word] - } + Self { words: [word] } } } diff --git a/sel4_cspace/src/compatibility.rs b/sel4_cspace/src/compatibility.rs index f0a8ec6..832abcf 100644 --- a/sel4_cspace/src/compatibility.rs +++ b/sel4_cspace/src/compatibility.rs @@ -1,7 +1 @@ - -pub use super::cap::zombie::{ - Zombie_new, - ZombieType_ZombieTCB -}; - - +pub use super::cap::zombie::{ZombieType_ZombieTCB, Zombie_new}; diff --git a/sel4_cspace/src/cte.rs b/sel4_cspace/src/cte.rs index 4cacf9d..f908175 100644 --- a/sel4_cspace/src/cte.rs +++ b/sel4_cspace/src/cte.rs @@ -1,10 +1,19 @@ -use core::intrinsics::{unlikely, likely}; +use super::{ + cap::{cap_t, is_cap_revocable, same_object_as, same_region_as, CapTag}, + deps::{finaliseCap, post_cap_deletion, preemptionPoint}, + mdb::mdb_node_t, + structures::{finaliseSlot_ret, resolveAddressBits_ret_t}, +}; +use crate::cap::zombie::capCyclicZombie; +use core::intrinsics::{likely, unlikely}; use core::ptr; -use sel4_common::{structures::exception_t, utils::{convert_to_type_ref, convert_to_mut_type_ref}, sel4_config::wordRadix, MASK}; use sel4_common::utils::{convert_to_option_mut_type_ref, MAX_FREE_INDEX}; -use crate::cap::zombie::capCyclicZombie; -use super::{cap::{cap_t, CapTag, same_region_as, same_object_as, is_cap_revocable}, mdb::mdb_node_t, structures::{finaliseSlot_ret, resolveAddressBits_ret_t}, - deps::{finaliseCap, preemptionPoint, post_cap_deletion}}; +use sel4_common::{ + sel4_config::wordRadix, + structures::exception_t, + utils::{convert_to_mut_type_ref, convert_to_type_ref}, + MASK, +}; #[repr(C)] #[derive(Clone, Copy)] @@ -120,8 +129,8 @@ impl cte_t { if badge == 0 { return true; } - return badge == next.cap.get_ep_badge() && - !(next.cteMDBNode.get_first_badged() != 0); + return badge == next.cap.get_ep_badge() + && !(next.cteMDBNode.get_first_badged() != 0); } CapTag::CapNotificationCap => { assert_eq!(next.cap.get_cap_type(), CapTag::CapNotificationCap); @@ -129,10 +138,10 @@ impl cte_t { if badge == 0 { return true; } - return badge == next.cap.get_nf_badge() && - !(next.cteMDBNode.get_first_badged() != 0); + return badge == next.cap.get_nf_badge() + && !(next.cteMDBNode.get_first_badged() != 0); } - _ => true + _ => true, } } @@ -201,7 +210,6 @@ impl cte_t { } } ret - } pub fn delete_all(&mut self, exposed: bool) -> exception_t { @@ -219,7 +227,8 @@ impl cte_t { if self.cap.get_cap_type() != CapTag::CapNullCap { let fc_ret = unsafe { finaliseCap(&self.cap, self.is_final_cap(), true) }; assert!( - cap_removable(&fc_ret.remainder, self) && fc_ret.cleanupInfo.get_cap_type() == CapTag::CapNullCap + cap_removable(&fc_ret.remainder, self) + && fc_ret.cleanupInfo.get_cap_type() == CapTag::CapNullCap ); self.set_empty(&cap_t::new_null_cap()); } @@ -238,7 +247,9 @@ impl cte_t { if next_addr != 0 { let next_node = convert_to_mut_type_ref::(next_addr); next_node.cteMDBNode.set_prev(prev_addr); - let first_badged = ((next_node.cteMDBNode.get_first_badged() != 0) || (mdb_node.get_first_badged() != 0)) as usize; + let first_badged = ((next_node.cteMDBNode.get_first_badged() != 0) + || (mdb_node.get_first_badged() != 0)) + as usize; next_node.cteMDBNode.set_first_badged(first_badged); } self.cap = cap_t::new_null_cap(); @@ -266,11 +277,13 @@ impl cte_t { } CapTag::CapZombieCap => { let ptr2 = self.cap.get_zombie_ptr(); - if ptr == ptr2 && self.cap.get_zombie_number() == n && self.cap.get_zombie_type() == zombie_type { + if ptr == ptr2 + && self.cap.get_zombie_number() == n + && self.cap.get_zombie_type() == zombie_type + { assert_eq!(end_slot.cap.get_cap_type(), CapTag::CapNullCap); self.cap.set_zombie_number(n - 1); } else { - assert!(ptr2 == self_ptr && ptr != self_ptr); } } @@ -342,18 +355,26 @@ pub fn cte_insert(new_cap: &cap_t, src_slot: &mut cte_t, dest_slot: &mut cte_t) (*dest_slot).cap = new_cap.clone(); (*dest_slot).cteMDBNode = newMDB; - src_slot.cteMDBNode.set_next(dest_slot as *const cte_t as usize); + src_slot + .cteMDBNode + .set_next(dest_slot as *const cte_t as usize); if newMDB.get_next() != 0 { let cte_ref = convert_to_mut_type_ref::(newMDB.get_next()); - cte_ref.cteMDBNode.set_prev(dest_slot as *const cte_t as usize); + cte_ref + .cteMDBNode + .set_prev(dest_slot as *const cte_t as usize); } } pub fn insert_new_cap(parent: &mut cte_t, slot: &mut cte_t, cap: &cap_t) { let next = parent.cteMDBNode.get_next(); slot.cap = cap.clone(); - slot.cteMDBNode = mdb_node_t::new(next as usize, 1usize, 1usize, - parent as *const cte_t as usize); + slot.cteMDBNode = mdb_node_t::new( + next as usize, + 1usize, + 1usize, + parent as *const cte_t as usize, + ); if next != 0 { let next_ref = convert_to_mut_type_ref::(next); next_ref.cteMDBNode.set_prev(slot as *const cte_t as usize); @@ -368,10 +389,7 @@ pub fn cte_move(new_cap: &cap_t, src_slot: &mut cte_t, dest_slot: &mut cte_t) { /* Haskell error: "cteInsert to non-empty destination" */ assert_eq!(dest_slot.cap.get_cap_type(), CapTag::CapNullCap); /* Haskell error: "cteInsert: mdb entry must be empty" */ - assert!( - dest_slot.cteMDBNode.get_next() == 0 - && dest_slot.cteMDBNode.get_prev() == 0 - ); + assert!(dest_slot.cteMDBNode.get_next() == 0 && dest_slot.cteMDBNode.get_prev() == 0); let mdb = src_slot.cteMDBNode; dest_slot.cap = new_cap.clone(); src_slot.cap = cap_t::new_null_cap(); @@ -381,12 +399,16 @@ pub fn cte_move(new_cap: &cap_t, src_slot: &mut cte_t, dest_slot: &mut cte_t) { let prev_ptr = mdb.get_prev(); if prev_ptr != 0 { let prev_ref = convert_to_mut_type_ref::(prev_ptr); - prev_ref.cteMDBNode.set_next(dest_slot as *const cte_t as usize); + prev_ref + .cteMDBNode + .set_next(dest_slot as *const cte_t as usize); } let next_ptr = mdb.get_next(); if next_ptr != 0 { let next_ref = convert_to_mut_type_ref::(next_ptr); - next_ref.cteMDBNode.set_prev(dest_slot as *const cte_t as usize); + next_ref + .cteMDBNode + .set_prev(dest_slot as *const cte_t as usize); } } @@ -397,11 +419,15 @@ pub fn cte_swap(cap1: &cap_t, slot1: &mut cte_t, cap2: &cap_t, slot2: &mut cte_t { let prev_ptr = mdb1.get_prev(); if prev_ptr != 0 { - convert_to_mut_type_ref::(prev_ptr).cteMDBNode.set_next(slot2 as *const cte_t as usize); + convert_to_mut_type_ref::(prev_ptr) + .cteMDBNode + .set_next(slot2 as *const cte_t as usize); } let next_ptr = mdb1.get_next(); if next_ptr != 0 { - convert_to_mut_type_ref::(next_ptr).cteMDBNode.set_prev(slot2 as *const cte_t as usize); + convert_to_mut_type_ref::(next_ptr) + .cteMDBNode + .set_prev(slot2 as *const cte_t as usize); } } @@ -414,16 +440,19 @@ pub fn cte_swap(cap1: &cap_t, slot1: &mut cte_t, cap2: &cap_t, slot2: &mut cte_t { let prev_ptr = mdb2.get_prev(); if prev_ptr != 0 { - convert_to_mut_type_ref::(prev_ptr).cteMDBNode.set_next(slot1 as *const cte_t as usize); + convert_to_mut_type_ref::(prev_ptr) + .cteMDBNode + .set_next(slot1 as *const cte_t as usize); } let next_ptr = mdb2.get_next(); if next_ptr != 0 { - convert_to_mut_type_ref::(next_ptr).cteMDBNode.set_prev(slot1 as *const cte_t as usize); + convert_to_mut_type_ref::(next_ptr) + .cteMDBNode + .set_prev(slot1 as *const cte_t as usize); } } } - #[inline] fn cap_removable(cap: &cap_t, slot: *mut cte_t) -> bool { match cap.get_cap_type() { @@ -442,7 +471,6 @@ fn cap_removable(cap: &cap_t, slot: *mut cte_t) -> bool { } } - fn setUntypedCapAsFull(srcCap: &cap_t, newCap: &cap_t, srcSlot: &mut cte_t) { if srcCap.get_cap_type() == CapTag::CapUntypedCap && newCap.get_cap_type() == CapTag::CapUntypedCap @@ -451,9 +479,9 @@ fn setUntypedCapAsFull(srcCap: &cap_t, newCap: &cap_t, srcSlot: &mut cte_t) { if srcCap.get_untyped_ptr() == newCap.get_untyped_ptr() && srcCap.get_untyped_block_size() == newCap.get_untyped_block_size() { - srcSlot.cap.set_untyped_free_index( - MAX_FREE_INDEX(srcCap.get_untyped_block_size()) - ); + srcSlot + .cap + .set_untyped_free_index(MAX_FREE_INDEX(srcCap.get_untyped_block_size())); } } } @@ -462,7 +490,11 @@ fn setUntypedCapAsFull(srcCap: &cap_t, newCap: &cap_t, srcSlot: &mut cte_t) { /// /// 从给定的cnode、cap index、和depth中找到对应cap的slot,成功则返回slot指针,失败返回找到的最深的cnode #[allow(unreachable_code)] -pub fn resolve_address_bits(node_cap: &cap_t, cap_ptr: usize, _n_bits: usize) -> resolveAddressBits_ret_t { +pub fn resolve_address_bits( + node_cap: &cap_t, + cap_ptr: usize, + _n_bits: usize, +) -> resolveAddressBits_ret_t { let mut ret = resolveAddressBits_ret_t::default(); let mut n_bits = _n_bits; ret.bitsRemaining = n_bits; diff --git a/sel4_cspace/src/deps.rs b/sel4_cspace/src/deps.rs index d2d5c02..e2b09b1 100644 --- a/sel4_cspace/src/deps.rs +++ b/sel4_cspace/src/deps.rs @@ -1,6 +1,6 @@ -use sel4_common::structures::exception_t; use crate::cap::cap_t; use crate::structures::finaliseCap_ret; +use sel4_common::structures::exception_t; extern "C" { pub fn Arch_finaliseCap(cap: &cap_t, final_: bool) -> finaliseCap_ret; @@ -10,4 +10,4 @@ extern "C" { pub fn post_cap_deletion(cap: &cap_t); pub fn preemptionPoint() -> exception_t; -} \ No newline at end of file +} diff --git a/sel4_cspace/src/interface.rs b/sel4_cspace/src/interface.rs index 4d70b07..207be5e 100644 --- a/sel4_cspace/src/interface.rs +++ b/sel4_cspace/src/interface.rs @@ -1,8 +1,7 @@ - +pub use super::cap::CapTag; +pub use super::cap::{cap_t, same_object_as}; pub use super::cap_rights::seL4_CapRights_t; pub use super::mdb::mdb_node_t; -pub use super::cap::{cap_t, same_object_as}; -pub use super::cap::CapTag; +pub use super::cte::{cte_insert, cte_move, cte_swap, cte_t, insert_new_cap, resolve_address_bits}; pub use super::structures::finaliseCap_ret; -pub use super::cte::{resolve_address_bits, cte_insert, cte_t, cte_move, cte_swap, insert_new_cap}; diff --git a/sel4_cspace/src/lib.rs b/sel4_cspace/src/lib.rs index 29c083e..a09d3d8 100644 --- a/sel4_cspace/src/lib.rs +++ b/sel4_cspace/src/lib.rs @@ -6,14 +6,14 @@ mod cap; mod cap_rights; +mod cte; mod mdb; mod structures; -mod cte; -/// 暴露给外部的接口 -pub mod interface; /// 需要外部实现的接口 pub mod deps; +/// 暴露给外部的接口 +pub mod interface; /// 兼容c风格的接口,后续会删除 -pub mod compatibility; \ No newline at end of file +pub mod compatibility; diff --git a/sel4_cspace/src/structures.rs b/sel4_cspace/src/structures.rs index 6d3fcda..e2a4c0c 100644 --- a/sel4_cspace/src/structures.rs +++ b/sel4_cspace/src/structures.rs @@ -1,9 +1,8 @@ -use sel4_common::structures::exception_t; use crate::cte::cte_t; +use sel4_common::structures::exception_t; use super::cap::cap_t; - #[repr(C)] #[derive(Debug, PartialEq, Clone, Copy)] pub struct finaliseSlot_ret { @@ -22,7 +21,6 @@ impl Default for finaliseSlot_ret { } } - #[repr(C)] #[derive(Debug, PartialEq, Clone, Copy)] pub struct finaliseCap_ret { diff --git a/sel4_ipc/src/endpoint.rs b/sel4_ipc/src/endpoint.rs index 3e57c26..a5ad42c 100644 --- a/sel4_ipc/src/endpoint.rs +++ b/sel4_ipc/src/endpoint.rs @@ -1,10 +1,12 @@ +use crate::transfer::Transfer; use sel4_common::plus_define_bitfield; use sel4_common::registers::badgeRegister; use sel4_common::utils::{convert_to_mut_type_ref, convert_to_option_mut_type_ref}; -use sel4_task::{possible_switch_to, rescheduleRequired, schedule_tcb, set_thread_state, tcb_queue_t, tcb_t, ThreadState}; +use sel4_task::{ + possible_switch_to, rescheduleRequired, schedule_tcb, set_thread_state, tcb_queue_t, tcb_t, + ThreadState, +}; use sel4_vspace::pptr_t; -use crate::transfer::Transfer; - pub const EPState_Idle: usize = EPState::Idle as usize; pub const EPState_Send: usize = EPState::Send as usize; @@ -35,14 +37,15 @@ impl endpoint_t { #[inline] pub fn get_state(&self) -> EPState { - unsafe { - core::mem::transmute::(self.get_usize_state() as u8) - } + unsafe { core::mem::transmute::(self.get_usize_state() as u8) } } #[inline] pub fn get_queue(&self) -> tcb_queue_t { - tcb_queue_t { head: self.get_queue_head(), tail: self.get_queue_tail() } + tcb_queue_t { + head: self.get_queue_head(), + tail: self.get_queue_tail(), + } } #[inline] @@ -108,17 +111,32 @@ impl endpoint_t { } } - pub fn send_ipc(&mut self, src_thread: &mut tcb_t, blocking: bool, - do_call: bool, can_grant: bool, badge: usize, can_grant_reply: bool) { + pub fn send_ipc( + &mut self, + src_thread: &mut tcb_t, + blocking: bool, + do_call: bool, + can_grant: bool, + badge: usize, + can_grant_reply: bool, + ) { match self.get_state() { EPState::Idle | EPState::Send => { if blocking { - src_thread.tcbState.set_ts_type(ThreadState::ThreadStateBlockedOnSend as usize); + src_thread + .tcbState + .set_ts_type(ThreadState::ThreadStateBlockedOnSend as usize); src_thread.tcbState.set_blocking_object(self.get_ptr()); - src_thread.tcbState.set_blocking_ipc_can_grant(can_grant as usize); + src_thread + .tcbState + .set_blocking_ipc_can_grant(can_grant as usize); src_thread.tcbState.set_blocking_ipc_badge(badge); - src_thread.tcbState.set_blocking_ipc_can_grant_reply(can_grant_reply as usize); - src_thread.tcbState.set_blocking_ipc_is_call(do_call as usize); + src_thread + .tcbState + .set_blocking_ipc_can_grant_reply(can_grant_reply as usize); + src_thread + .tcbState + .set_blocking_ipc_is_call(do_call as usize); schedule_tcb(src_thread); let mut queue = self.get_queue(); @@ -198,6 +216,5 @@ impl endpoint_t { } } } - } } diff --git a/sel4_ipc/src/lib.rs b/sel4_ipc/src/lib.rs index 778db59..9c30b3d 100644 --- a/sel4_ipc/src/lib.rs +++ b/sel4_ipc/src/lib.rs @@ -1,5 +1,6 @@ #![feature(core_intrinsics)] #![no_std] +#![allow(internal_features)] #![allow(non_snake_case)] #![allow(non_camel_case_types)] #![allow(non_upper_case_globals)] @@ -10,4 +11,4 @@ mod transfer; pub use endpoint::*; pub use notification::*; -pub use transfer::*; \ No newline at end of file +pub use transfer::*; diff --git a/sel4_ipc/src/notification.rs b/sel4_ipc/src/notification.rs index b2b7dc6..4950bd6 100644 --- a/sel4_ipc/src/notification.rs +++ b/sel4_ipc/src/notification.rs @@ -1,9 +1,10 @@ +use crate::transfer::Transfer; use sel4_common::plus_define_bitfield; use sel4_common::registers::badgeRegister; use sel4_common::utils::{convert_to_mut_type_ref, convert_to_option_mut_type_ref}; -use sel4_task::{possible_switch_to, rescheduleRequired, set_thread_state, tcb_queue_t, tcb_t, ThreadState}; -use crate::transfer::Transfer; - +use sel4_task::{ + possible_switch_to, rescheduleRequired, set_thread_state, tcb_queue_t, tcb_t, ThreadState, +}; #[derive(PartialEq, Eq)] pub enum NtfnState { @@ -27,14 +28,15 @@ plus_define_bitfield! { impl notification_t { #[inline] pub fn get_state(&self) -> NtfnState { - unsafe { - core::mem::transmute::(self.get_usize_state() as u8) - } + unsafe { core::mem::transmute::(self.get_usize_state() as u8) } } #[inline] pub fn get_queue(&self) -> tcb_queue_t { - tcb_queue_t { head: self.get_queue_head(), tail: self.get_queue_tail() } + tcb_queue_t { + head: self.get_queue_head(), + tail: self.get_queue_tail(), + } } #[inline] @@ -62,12 +64,12 @@ impl notification_t { #[inline] pub fn cacncel_all_signal(&mut self) { - if self.get_state() == NtfnState::Waiting { + if self.get_state() == NtfnState::Waiting { let mut op_thread = convert_to_option_mut_type_ref::(self.get_queue_head()); self.set_state(NtfnState::Idle as usize); self.set_queue_head(0); self.set_queue_tail(0); - while let Some(thread) = op_thread { + while let Some(thread) = op_thread { set_thread_state(thread, ThreadState::ThreadStateRestart); thread.sched_enqueue(); op_thread = convert_to_option_mut_type_ref::(thread.tcbEPNext); @@ -105,7 +107,7 @@ impl notification_t { match self.get_state() { NtfnState::Idle => { if let Some(tcb) = convert_to_option_mut_type_ref::(self.get_bound_tcb()) { - if tcb.get_state() == ThreadState::ThreadStateBlockedOnReceive{ + if tcb.get_state() == ThreadState::ThreadStateBlockedOnReceive { tcb.cancel_ipc(); set_thread_state(tcb, ThreadState::ThreadStateRunning); tcb.set_register(badgeRegister, badge); @@ -161,4 +163,4 @@ impl notification_t { } } } -} \ No newline at end of file +} diff --git a/sel4_ipc/src/transfer.rs b/sel4_ipc/src/transfer.rs index 957f653..4d8a129 100644 --- a/sel4_ipc/src/transfer.rs +++ b/sel4_ipc/src/transfer.rs @@ -3,11 +3,13 @@ use core::intrinsics::likely; use super::endpoint::*; use super::notification::*; -use sel4_common::structures::*; -use sel4_common::sel4_config::*; use sel4_common::fault::*; use sel4_common::message_info::*; -use sel4_common::registers::{badgeRegister, FaultIP, msgInfoRegister, n_exceptionMessage, n_syscallMessage}; +use sel4_common::registers::{ + badgeRegister, msgInfoRegister, n_exceptionMessage, n_syscallMessage, FaultIP, +}; +use sel4_common::sel4_config::*; +use sel4_common::structures::*; use sel4_common::utils::*; use sel4_cspace::interface::*; use sel4_task::{possible_switch_to, set_thread_state, tcb_t, ThreadState}; @@ -16,19 +18,42 @@ use sel4_vspace::pptr_t; pub trait Transfer { fn cancel_ipc(&mut self); - fn set_transfer_caps(&mut self, endpoint: Option<&endpoint_t>, info: &mut seL4_MessageInfo_t, current_extra_caps: &[pptr_t; seL4_MsgMaxExtraCaps]); + fn set_transfer_caps( + &mut self, + endpoint: Option<&endpoint_t>, + info: &mut seL4_MessageInfo_t, + current_extra_caps: &[pptr_t; seL4_MsgMaxExtraCaps], + ); - fn set_transfer_caps_with_buf(&mut self, endpoint: Option<&endpoint_t>, info: &mut seL4_MessageInfo_t, current_extra_caps: &[pptr_t; seL4_MsgMaxExtraCaps], ipc_buffer: Option<&mut seL4_IPCBuffer>); + fn set_transfer_caps_with_buf( + &mut self, + endpoint: Option<&endpoint_t>, + info: &mut seL4_MessageInfo_t, + current_extra_caps: &[pptr_t; seL4_MsgMaxExtraCaps], + ipc_buffer: Option<&mut seL4_IPCBuffer>, + ); fn do_fault_transfer(&self, receiver: &mut tcb_t, badge: usize); - fn do_normal_transfer(&self, receiver: &mut tcb_t, endpoint: Option<&endpoint_t>, badge: usize, can_grant: bool); + fn do_normal_transfer( + &self, + receiver: &mut tcb_t, + endpoint: Option<&endpoint_t>, + badge: usize, + can_grant: bool, + ); fn do_fault_reply_transfer(&self, receiver: &mut tcb_t) -> bool; fn complete_signal(&mut self) -> bool; - fn do_ipc_transfer(&self, receiver: &mut tcb_t, endpoint: Option<&endpoint_t>, badge: usize, grant: bool); + fn do_ipc_transfer( + &self, + receiver: &mut tcb_t, + endpoint: Option<&endpoint_t>, + badge: usize, + grant: bool, + ); fn do_reply(&self, receiver: &mut tcb_t, slot: &mut cte_t, grant: bool); } @@ -59,7 +84,12 @@ impl Transfer for tcb_t { } } - fn set_transfer_caps(&mut self, endpoint: Option<&endpoint_t>, info: &mut seL4_MessageInfo_t, current_extra_caps: &[pptr_t; seL4_MsgMaxExtraCaps]) { + fn set_transfer_caps( + &mut self, + endpoint: Option<&endpoint_t>, + info: &mut seL4_MessageInfo_t, + current_extra_caps: &[pptr_t; seL4_MsgMaxExtraCaps], + ) { info.set_extra_caps(0); info.set_caps_unwrapped(0); let ipc_buffer = self.lookup_mut_ipc_buffer(true); @@ -72,7 +102,10 @@ impl Transfer for tcb_t { while i < seL4_MsgMaxExtraCaps && current_extra_caps[i] as usize != 0 { let slot = convert_to_mut_type_ref::(current_extra_caps[i]); let cap = &slot.cap.clone(); - if cap.get_cap_type() == CapTag::CapEndpointCap && endpoint.is_some() && cap.get_ep_ptr() == endpoint.unwrap().get_ptr() { + if cap.get_cap_type() == CapTag::CapEndpointCap + && endpoint.is_some() + && cap.get_ep_ptr() == endpoint.unwrap().get_ptr() + { buffer.caps_or_badges[i] = cap.get_ep_badge(); info.set_caps_unwrapped(info.get_caps_unwrapped() | (1 << i)); } else { @@ -81,7 +114,9 @@ impl Transfer for tcb_t { } else { let dest = dest_slot.take(); let dc_ret = slot.derive_cap(cap); - if dc_ret.status != exception_t::EXCEPTION_NONE || dc_ret.cap.get_cap_type() == CapTag::CapNullCap { + if dc_ret.status != exception_t::EXCEPTION_NONE + || dc_ret.cap.get_cap_type() == CapTag::CapNullCap + { break; } cte_insert(&dc_ret.cap, slot, dest.unwrap()); @@ -92,7 +127,13 @@ impl Transfer for tcb_t { info.set_extra_caps(i); } - fn set_transfer_caps_with_buf(&mut self, endpoint: Option<&endpoint_t>, info: &mut seL4_MessageInfo_t, current_extra_caps: &[pptr_t; seL4_MsgMaxExtraCaps], ipc_buffer: Option<&mut seL4_IPCBuffer>) { + fn set_transfer_caps_with_buf( + &mut self, + endpoint: Option<&endpoint_t>, + info: &mut seL4_MessageInfo_t, + current_extra_caps: &[pptr_t; seL4_MsgMaxExtraCaps], + ipc_buffer: Option<&mut seL4_IPCBuffer>, + ) { info.set_extra_caps(0); info.set_caps_unwrapped(0); // let ipc_buffer = self.lookup_mut_ipc_buffer(true); @@ -105,7 +146,10 @@ impl Transfer for tcb_t { while i < seL4_MsgMaxExtraCaps && current_extra_caps[i] as usize != 0 { let slot = convert_to_mut_type_ref::(current_extra_caps[i]); let cap = &slot.cap.clone(); - if cap.get_cap_type() == CapTag::CapEndpointCap && endpoint.is_some() && cap.get_ep_ptr() == endpoint.unwrap().get_ptr() { + if cap.get_cap_type() == CapTag::CapEndpointCap + && endpoint.is_some() + && cap.get_ep_ptr() == endpoint.unwrap().get_ptr() + { buffer.caps_or_badges[i] = cap.get_ep_badge(); info.set_caps_unwrapped(info.get_caps_unwrapped() | (1 << i)); } else { @@ -114,7 +158,9 @@ impl Transfer for tcb_t { } else { let dest = dest_slot.take(); let dc_ret = slot.derive_cap(cap); - if dc_ret.status != exception_t::EXCEPTION_NONE || dc_ret.cap.get_cap_type() == CapTag::CapNullCap { + if dc_ret.status != exception_t::EXCEPTION_NONE + || dc_ret.cap.get_cap_type() == CapTag::CapNullCap + { break; } cte_insert(&dc_ret.cap, slot, dest.unwrap()); @@ -130,24 +176,40 @@ impl Transfer for tcb_t { FaultType::CapFault => { receiver.set_mr(seL4_CapFault_IP, self.get_register(FaultIP)); receiver.set_mr(seL4_CapFault_Addr, self.tcbFault.cap_fault_get_address()); - receiver.set_mr(seL4_CapFault_InRecvPhase, self.tcbFault.cap_fault_get_in_receive_phase()); - receiver.set_lookup_fault_mrs(seL4_CapFault_LookupFailureType, &self.tcbLookupFailure) - }, + receiver.set_mr( + seL4_CapFault_InRecvPhase, + self.tcbFault.cap_fault_get_in_receive_phase(), + ); + receiver + .set_lookup_fault_mrs(seL4_CapFault_LookupFailureType, &self.tcbLookupFailure) + } FaultType::UnknownSyscall => { self.copy_syscall_fault_mrs(receiver); - receiver.set_mr(n_syscallMessage, self.tcbFault.unknown_syscall_get_syscall_number()) - }, + receiver.set_mr( + n_syscallMessage, + self.tcbFault.unknown_syscall_get_syscall_number(), + ) + } FaultType::UserException => { self.copy_exeception_fault_mrs(receiver); - receiver.set_mr(n_exceptionMessage, self.tcbFault.user_exeception_get_number()); - receiver.set_mr(n_exceptionMessage + 1, self.tcbFault.user_exeception_get_code()) - }, + receiver.set_mr( + n_exceptionMessage, + self.tcbFault.user_exeception_get_number(), + ); + receiver.set_mr( + n_exceptionMessage + 1, + self.tcbFault.user_exeception_get_code(), + ) + } FaultType::VMFault => { receiver.set_mr(seL4_VMFault_IP, self.get_register(FaultIP)); receiver.set_mr(seL4_VMFault_Addr, self.tcbFault.vm_fault_get_address()); - receiver.set_mr(seL4_VMFault_PrefetchFault, self.tcbFault.vm_fault_get_instruction_fault()); + receiver.set_mr( + seL4_VMFault_PrefetchFault, + self.tcbFault.vm_fault_get_instruction_fault(), + ); receiver.set_mr(seL4_VMFault_FSR, self.tcbFault.vm_fault_get_fsr()) - }, + } _ => { panic!("invalid fault") } @@ -157,7 +219,13 @@ impl Transfer for tcb_t { receiver.set_register(badgeRegister, badge); } - fn do_normal_transfer(&self, receiver: &mut tcb_t, endpoint: Option<&endpoint_t>, badge: usize, can_grant: bool) { + fn do_normal_transfer( + &self, + receiver: &mut tcb_t, + endpoint: Option<&endpoint_t>, + badge: usize, + can_grant: bool, + ) { let mut tag = seL4_MessageInfo_t::from_word_security(self.get_register(msgInfoRegister)); let mut current_extra_caps = [0; seL4_MsgMaxExtraCaps]; if can_grant { @@ -176,19 +244,29 @@ impl Transfer for tcb_t { let length = tag.get_length(); match receiver.tcbFault.get_fault_type() { FaultType::UnknownSyscall => { - self.copy_fault_mrs_for_reply(receiver, MessageID_Syscall, core::cmp::min(length, n_syscallMessage)); + self.copy_fault_mrs_for_reply( + receiver, + MessageID_Syscall, + core::cmp::min(length, n_syscallMessage), + ); return label as usize == 0; } FaultType::UserException => { - self.copy_fault_mrs_for_reply(receiver, MessageID_Exception, core::cmp::min(length, n_exceptionMessage)); + self.copy_fault_mrs_for_reply( + receiver, + MessageID_Exception, + core::cmp::min(length, n_exceptionMessage), + ); return label as usize == 0; } - _ => true + _ => true, } } fn complete_signal(&mut self) -> bool { - if let Some(ntfn) = convert_to_option_mut_type_ref::(self.tcbBoundNotification) { + if let Some(ntfn) = + convert_to_option_mut_type_ref::(self.tcbBoundNotification) + { if likely(ntfn.get_state() == NtfnState::Active) { self.set_register(badgeRegister, ntfn.get_msg_identifier()); ntfn.set_state(NtfnState::Idle as usize); @@ -198,7 +276,13 @@ impl Transfer for tcb_t { false } - fn do_ipc_transfer(&self, receiver: &mut tcb_t, endpoint: Option<&endpoint_t>, badge: usize, grant: bool) { + fn do_ipc_transfer( + &self, + receiver: &mut tcb_t, + endpoint: Option<&endpoint_t>, + badge: usize, + grant: bool, + ) { if likely(self.tcbFault.get_fault_type() == FaultType::NullFault) { self.do_normal_transfer(receiver, endpoint, badge, grant) } else { diff --git a/sel4_task/src/deps.rs b/sel4_task/src/deps.rs index b33d418..7df7e20 100644 --- a/sel4_task/src/deps.rs +++ b/sel4_task/src/deps.rs @@ -1,6 +1,5 @@ - extern "C" { pub fn ksIdleThreadTCB(); pub fn doMaskReschedule(mask: usize); pub fn kernel_stack_alloc(); -} \ No newline at end of file +} diff --git a/sel4_task/src/lib.rs b/sel4_task/src/lib.rs index 495da8d..ce56ff5 100644 --- a/sel4_task/src/lib.rs +++ b/sel4_task/src/lib.rs @@ -1,18 +1,19 @@ #![feature(core_intrinsics)] #![no_std] +#![allow(internal_features)] #![allow(non_snake_case)] #![allow(non_camel_case_types)] #![allow(non_upper_case_globals)] -mod thread_state; -mod tcb_queue; -pub mod tcb; -mod structures; -mod scheduler; mod deps; +mod scheduler; +mod structures; +pub mod tcb; +mod tcb_queue; +mod thread_state; -pub use tcb::*; pub use scheduler::*; -pub use thread_state::*; +pub use structures::*; +pub use tcb::*; pub use tcb_queue::*; -pub use structures::*; \ No newline at end of file +pub use thread_state::*; diff --git a/sel4_task/src/scheduler.rs b/sel4_task/src/scheduler.rs index 3d0da66..4676ce6 100644 --- a/sel4_task/src/scheduler.rs +++ b/sel4_task/src/scheduler.rs @@ -1,18 +1,21 @@ +#[cfg(feature = "ENABLE_SMP")] +use crate::deps::{doMaskReschedule, kernel_stack_alloc, ksIdleThreadTCB}; use core::arch::asm; use core::intrinsics::{likely, unlikely}; -use sel4_common::{BIT, MASK}; -use sel4_common::registers::{FaultIP, NextIP, sp, SSTATUS, SSTATUS_SPIE, SSTATUS_SPP}; -use sel4_common::sel4_config::{CONFIG_KERNEL_STACK_BITS, CONFIG_MAX_NUM_NODES, CONFIG_NUM_DOMAINS, CONFIG_NUM_PRIORITIES, CONFIG_TIME_SLICE, L2_BITMAP_SIZE, NUM_READY_QUEUES, seL4_TCBBits, TCB_OFFSET, wordBits, wordRadix}; +use sel4_common::registers::{sp, FaultIP, NextIP, SSTATUS, SSTATUS_SPIE, SSTATUS_SPP}; +use sel4_common::sel4_config::{ + seL4_TCBBits, wordBits, wordRadix, CONFIG_KERNEL_STACK_BITS, CONFIG_MAX_NUM_NODES, + CONFIG_NUM_DOMAINS, CONFIG_NUM_PRIORITIES, CONFIG_TIME_SLICE, L2_BITMAP_SIZE, NUM_READY_QUEUES, + TCB_OFFSET, +}; use sel4_common::utils::{convert_to_mut_type_ref, convert_to_mut_type_ref_unsafe}; -#[cfg(feature = "ENABLE_SMP")] -use crate::deps::{doMaskReschedule, ksIdleThreadTCB, kernel_stack_alloc}; +use sel4_common::{BIT, MASK}; -#[cfg(feature = "ENABLE_SMP")] -use sel4_common::utils::cpu_id; use crate::tcb::{set_thread_state, tcb_t}; use crate::tcb_queue::tcb_queue_t; use crate::thread_state::ThreadState; - +#[cfg(feature = "ENABLE_SMP")] +use sel4_common::utils::cpu_id; #[cfg(feature = "ENABLE_SMP")] #[derive(Debug, Copy, Clone)] @@ -26,14 +29,13 @@ pub struct SmpStateData { pub ksSchedulerAction: usize, pub ksDebugTCBs: usize, // TODO: Cache Line 对齐 - } #[cfg(feature = "ENABLE_SMP")] #[no_mangle] pub static mut ksSMP: [SmpStateData; CONFIG_MAX_NUM_NODES] = [SmpStateData { ipiReschedulePending: 0, - ksReadyQueues: [tcb_queue_t {head: 0, tail: 0}; CONFIG_NUM_DOMAINS * CONFIG_NUM_PRIORITIES], + ksReadyQueues: [tcb_queue_t { head: 0, tail: 0 }; CONFIG_NUM_DOMAINS * CONFIG_NUM_PRIORITIES], ksReadyQueuesL1Bitmap: [0; CONFIG_NUM_DOMAINS], ksReadyQueuesL2Bitmap: [[0; L2_BITMAP_SIZE]; CONFIG_NUM_DOMAINS], ksCurThread: 0, @@ -42,7 +44,6 @@ pub static mut ksSMP: [SmpStateData; CONFIG_MAX_NUM_NODES] = [SmpStateData { ksDebugTCBs: 0, }; CONFIG_MAX_NUM_NODES]; - #[repr(C)] #[derive(Debug, PartialEq, Clone, Copy)] pub struct dschedule_t { @@ -54,7 +55,6 @@ pub const SchedulerAction_ResumeCurrentThread: usize = 0; pub const SchedulerAction_ChooseNewThread: usize = 1; pub const ksDomScheduleLength: usize = 1; - #[no_mangle] pub static mut ksDomainTime: usize = 0; @@ -74,10 +74,8 @@ pub static mut ksIdleThread: usize = 0; pub static mut ksSchedulerAction: usize = 1; #[no_mangle] -pub static mut ksReadyQueues: [tcb_queue_t; NUM_READY_QUEUES] = [tcb_queue_t { - head: 0, - tail: 0, -}; NUM_READY_QUEUES]; +pub static mut ksReadyQueues: [tcb_queue_t; NUM_READY_QUEUES] = + [tcb_queue_t { head: 0, tail: 0 }; NUM_READY_QUEUES]; #[no_mangle] pub static mut ksReadyQueuesL2Bitmap: [[usize; L2_BITMAP_SIZE]; CONFIG_NUM_DOMAINS] = @@ -98,14 +96,15 @@ pub static mut ksDomSchedule: [dschedule_t; ksDomScheduleLength] = [dschedule_t type prio_t = usize; - #[inline] pub fn get_idle_thread() -> &'static mut tcb_t { unsafe { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { convert_to_mut_type_ref::(ksSMP[cpu_id()].ksIdleThread) } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { convert_to_mut_type_ref::(ksIdleThread) } } @@ -114,10 +113,12 @@ pub fn get_idle_thread() -> &'static mut tcb_t { #[inline] pub fn get_ks_scheduler_action() -> usize { unsafe { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { ksSMP[cpu_id()].ksSchedulerAction } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { ksSchedulerAction } } @@ -129,10 +130,12 @@ pub fn set_ks_scheduler_action(action: usize) { // debug!("set_ks_scheduler_action: {}", action); // } unsafe { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { ksSMP[cpu_id()].ksSchedulerAction = action; } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { ksSchedulerAction = action } } @@ -141,10 +144,12 @@ pub fn set_ks_scheduler_action(action: usize) { #[inline] pub fn get_currenct_thread() -> &'static mut tcb_t { unsafe { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { convert_to_mut_type_ref::(ksSMP[cpu_id()].ksCurThread) } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { convert_to_mut_type_ref::(ksCurThread) } } @@ -153,10 +158,12 @@ pub fn get_currenct_thread() -> &'static mut tcb_t { #[inline] pub fn get_currenct_thread_unsafe() -> &'static mut tcb_t { unsafe { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { convert_to_mut_type_ref_unsafe::(ksSMP[cpu_id()].ksCurThread) } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { convert_to_mut_type_ref_unsafe::(ksCurThread) } } @@ -165,29 +172,31 @@ pub fn get_currenct_thread_unsafe() -> &'static mut tcb_t { #[inline] pub fn set_current_scheduler_action(action: usize) { unsafe { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { ksSMP[cpu_id()].ksSchedulerAction = action; } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { ksSchedulerAction = action; } } } - #[inline] pub fn set_current_thread(thread: &tcb_t) { unsafe { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { ksSMP[cpu_id()].ksCurThread = thread.get_ptr(); } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { ksCurThread = thread.get_ptr() } } } - #[inline] pub fn get_current_domain() -> usize { unsafe { ksCurDomain } @@ -230,20 +239,25 @@ fn getHighestPrio(dom: usize) -> prio_t { #[inline] fn getHighestPrio(dom: usize) -> prio_t { unsafe { - let l1index = wordBits - 1 - ksSMP[cpu_id()].ksReadyQueuesL1Bitmap[dom].leading_zeros() as usize; + let l1index = + wordBits - 1 - ksSMP[cpu_id()].ksReadyQueuesL1Bitmap[dom].leading_zeros() as usize; let l1index_inverted = invert_l1index(l1index); - let l2index = - wordBits - 1 - (ksSMP[cpu_id()].ksReadyQueuesL2Bitmap[dom])[l1index_inverted].leading_zeros() as usize; + let l2index = wordBits + - 1 + - (ksSMP[cpu_id()].ksReadyQueuesL2Bitmap[dom])[l1index_inverted].leading_zeros() + as usize; l1index_to_prio(l1index) | l2index } } #[inline] pub fn isHighestPrio(dom: usize, prio: prio_t) -> bool { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { unsafe { ksSMP[cpu_id()].ksReadyQueuesL1Bitmap[dom] == 0 || prio >= getHighestPrio(dom) } } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { unsafe { ksReadyQueuesL1Bitmap[dom] == 0 || prio >= getHighestPrio(dom) } } } @@ -253,11 +267,14 @@ pub fn addToBitmap(_cpu: usize, dom: usize, prio: usize) { unsafe { let l1index = prio_to_l1index(prio); let l1index_inverted = invert_l1index(l1index); - #[cfg(feature = "ENABLE_SMP")] { - ksSMP[_cpu].ksReadyQueuesL1Bitmap[dom]|= BIT!(l1index); - ksSMP[_cpu].ksReadyQueuesL2Bitmap[dom][l1index_inverted] |= BIT!(prio & MASK!(wordRadix)); + #[cfg(feature = "ENABLE_SMP")] + { + ksSMP[_cpu].ksReadyQueuesL1Bitmap[dom] |= BIT!(l1index); + ksSMP[_cpu].ksReadyQueuesL2Bitmap[dom][l1index_inverted] |= + BIT!(prio & MASK!(wordRadix)); } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { ksReadyQueuesL1Bitmap[dom] |= BIT!(l1index); ksReadyQueuesL2Bitmap[dom][l1index_inverted] |= BIT!(prio & MASK!(wordRadix)); } @@ -269,13 +286,16 @@ pub fn removeFromBitmap(_cpu: usize, dom: usize, prio: usize) { unsafe { let l1index = prio_to_l1index(prio); let l1index_inverted = invert_l1index(l1index); - #[cfg(feature = "ENABLE_SMP")] { - ksSMP[_cpu].ksReadyQueuesL2Bitmap[dom][l1index_inverted] &= !BIT!(prio & MASK!(wordRadix)); + #[cfg(feature = "ENABLE_SMP")] + { + ksSMP[_cpu].ksReadyQueuesL2Bitmap[dom][l1index_inverted] &= + !BIT!(prio & MASK!(wordRadix)); if unlikely(ksSMP[_cpu].ksReadyQueuesL2Bitmap[dom][l1index_inverted] == 0) { ksSMP[_cpu].ksReadyQueuesL1Bitmap[dom] &= !(BIT!((l1index))); } } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { ksReadyQueuesL2Bitmap[dom][l1index_inverted] &= !BIT!(prio & MASK!(wordRadix)); if unlikely(ksReadyQueuesL2Bitmap[dom][l1index_inverted] == 0) { ksReadyQueuesL1Bitmap[dom] &= !(BIT!((l1index))); @@ -298,7 +318,6 @@ fn nextDomain() { } } - fn scheduleChooseNewThread() { // if hart_id() == 0 { // debug!("scheduleChooseNewThread"); @@ -316,23 +335,26 @@ fn chooseThread() { unsafe { let dom = 0; let ks_l1_bit = { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { ksSMP[cpu_id()].ksReadyQueuesL1Bitmap[dom] } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { ksReadyQueuesL1Bitmap[dom] } }; if likely(ks_l1_bit != 0) { let prio = getHighestPrio(dom); let thread = { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { ksSMP[cpu_id()].ksReadyQueues[ready_queues_index(dom, prio)].head } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { ksReadyQueues[ready_queues_index(dom, prio)].head } - }; assert_ne!(thread, 0); convert_to_mut_type_ref::(thread).switch_to_this(); @@ -377,9 +399,7 @@ pub fn schedule() { // ksSchedulerAction = SchedulerAction_ChooseNewThread; set_ks_scheduler_action(SchedulerAction_ChooseNewThread); scheduleChooseNewThread(); - } else if was_runnable - && candidate.tcbPriority == get_currenct_thread().tcbPriority - { + } else if was_runnable && candidate.tcbPriority == get_currenct_thread().tcbPriority { candidate.sched_append(); set_ks_scheduler_action(SchedulerAction_ChooseNewThread); scheduleChooseNewThread(); @@ -390,11 +410,11 @@ pub fn schedule() { } set_ks_scheduler_action(SchedulerAction_ResumeCurrentThread); unsafe { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { doMaskReschedule(ksSMP[cpu_id()].ipiReschedulePending); ksSMP[cpu_id()].ipiReschedulePending = 0; } - } } @@ -459,10 +479,8 @@ pub fn timerTick() { } } - #[no_mangle] pub fn activateThread() { - let thread = get_currenct_thread(); // debug!("current: {:#x}", thread.get_ptr()); match thread.get_state() { @@ -476,9 +494,8 @@ pub fn activateThread() { // setThreadState(thread, ThreadStateRunning); set_thread_state(thread, ThreadState::ThreadStateRunning); } - ThreadState::ThreadStateIdleThreadState => return { - - }, + // 诡异的语法... + ThreadState::ThreadStateIdleThreadState => return {}, _ => panic!( "current thread is blocked , state id :{}", thread.get_state() as usize @@ -486,14 +503,13 @@ pub fn activateThread() { } } - // #[no_mangle] // pub static mut kernel_stack_alloc: [[u8; BIT!(CONFIG_KERNEL_STACK_BITS)]; CONFIG_MAX_NUM_NODES] = // [[0; BIT!(CONFIG_KERNEL_STACK_BITS)]; CONFIG_MAX_NUM_NODES]; #[cfg(not(feature = "ENABLE_SMP"))] pub fn create_idle_thread() { - use crate::deps::{ksIdleThreadTCB, kernel_stack_alloc}; + use crate::deps::{kernel_stack_alloc, ksIdleThreadTCB}; unsafe { let pptr = ksIdleThreadTCB as usize as *mut usize; @@ -502,7 +518,10 @@ pub fn create_idle_thread() { let tcb = get_idle_thread(); tcb.set_register(NextIP, idle_thread as usize); tcb.set_register(SSTATUS, SSTATUS_SPP | SSTATUS_SPIE); - tcb.set_register(sp, kernel_stack_alloc as usize + BIT!(CONFIG_KERNEL_STACK_BITS)); + tcb.set_register( + sp, + kernel_stack_alloc as usize + BIT!(CONFIG_KERNEL_STACK_BITS), + ); set_thread_state(tcb, ThreadState::ThreadStateIdleThreadState); } } @@ -512,21 +531,22 @@ pub fn create_idle_thread() { use log::debug; unsafe { for i in 0..CONFIG_MAX_NUM_NODES { - let pptr = (ksIdleThreadTCB as usize + i * BIT!(seL4_TCBBits))as *mut usize; + let pptr = (ksIdleThreadTCB as usize + i * BIT!(seL4_TCBBits)) as *mut usize; ksSMP[i].ksIdleThread = pptr.add(TCB_OFFSET) as usize; debug!("ksIdleThread: {:#x}", ksSMP[i].ksIdleThread); let tcb = convert_to_mut_type_ref::(ksSMP[i].ksIdleThread); tcb.set_register(NextIP, idle_thread as usize); tcb.set_register(SSTATUS, SSTATUS_SPP | SSTATUS_SPIE); - tcb.set_register(sp, kernel_stack_alloc as usize + (i + 1) * BIT!(CONFIG_KERNEL_STACK_BITS)); + tcb.set_register( + sp, + kernel_stack_alloc as usize + (i + 1) * BIT!(CONFIG_KERNEL_STACK_BITS), + ); set_thread_state(tcb, ThreadState::ThreadStateIdleThreadState); tcb.tcbAffinity = i; } } } - - fn idle_thread() { unsafe { loop { @@ -534,4 +554,4 @@ fn idle_thread() { asm!("wfi"); } } -} \ No newline at end of file +} diff --git a/sel4_task/src/structures.rs b/sel4_task/src/structures.rs index a7fc98b..a947055 100644 --- a/sel4_task/src/structures.rs +++ b/sel4_task/src/structures.rs @@ -32,4 +32,4 @@ impl Default for lookupSlot_ret_t { slot: 0 as *mut cte_t, } } -} \ No newline at end of file +} diff --git a/sel4_task/src/tcb.rs b/sel4_task/src/tcb.rs index d5f6a6b..279d7ca 100644 --- a/sel4_task/src/tcb.rs +++ b/sel4_task/src/tcb.rs @@ -1,24 +1,30 @@ use core::intrinsics::{likely, unlikely}; use sel4_common::fault::*; -use sel4_common::{BIT, MASK}; use sel4_common::message_info::seL4_MessageInfo_t; -use sel4_common::registers::{fault_messages, FaultIP, msgInfoRegister, msgRegister, n_contextRegisters, n_exceptionMessage, n_msgRegisters, n_syscallMessage, NextIP, SSTATUS}; -use sel4_common::utils::{pageBitsForSize, convert_to_mut_type_ref}; -use sel4_cspace::interface::{cte_t, resolve_address_bits, CapTag, cap_t, mdb_node_t, cte_insert}; -use sel4_vspace::{set_vm_root, pptr_t, VMReadWrite, VMReadOnly}; +use sel4_common::registers::{ + fault_messages, msgInfoRegister, msgRegister, n_contextRegisters, n_exceptionMessage, + n_msgRegisters, n_syscallMessage, FaultIP, NextIP, SSTATUS, +}; +use sel4_common::utils::{convert_to_mut_type_ref, pageBitsForSize}; +#[cfg(feature = "ENABLE_SMP")] +use sel4_common::BIT; +use sel4_common::MASK; +use sel4_cspace::interface::{cap_t, cte_insert, cte_t, mdb_node_t, resolve_address_bits, CapTag}; +use sel4_vspace::{pptr_t, set_vm_root, VMReadOnly, VMReadWrite}; -use sel4_common::sel4_config::*; -use sel4_common::structures::{seL4_IPCBuffer, exception_t}; use crate::tcb_queue::tcb_queue_t; +use sel4_common::sel4_config::*; +use sel4_common::structures::{exception_t, seL4_IPCBuffer}; - -use super::scheduler::{addToBitmap, get_currenct_thread, possible_switch_to, ready_queues_index, removeFromBitmap, rescheduleRequired, schedule_tcb, set_current_thread}; +use super::scheduler::{ + addToBitmap, get_currenct_thread, possible_switch_to, ready_queues_index, removeFromBitmap, + rescheduleRequired, schedule_tcb, set_current_thread, +}; use super::structures::lookupSlot_raw_ret_t; use super::thread_state::*; - #[repr(C)] #[derive(Debug, PartialEq, Clone, Copy)] pub struct arch_tcb_t { @@ -29,7 +35,7 @@ impl Default for arch_tcb_t { fn default() -> Self { let mut registers = [0; n_contextRegisters]; registers[SSTATUS] = 0x00040020; - Self {registers } + Self { registers } } } @@ -85,18 +91,21 @@ impl tcb_t { #[inline] pub fn is_stopped(&self) -> bool { match self.get_state() { - ThreadState::ThreadStateInactive | ThreadState::ThreadStateBlockedOnNotification | ThreadState::ThreadStateBlockedOnReceive - | ThreadState::ThreadStateBlockedOnReply | ThreadState::ThreadStateBlockedOnSend => true, + ThreadState::ThreadStateInactive + | ThreadState::ThreadStateBlockedOnNotification + | ThreadState::ThreadStateBlockedOnReceive + | ThreadState::ThreadStateBlockedOnReply + | ThreadState::ThreadStateBlockedOnSend => true, - _ => false + _ => false, } } #[inline] pub fn is_runnable(&self) -> bool { match self.get_state() { - ThreadState::ThreadStateRunning | ThreadState::ThreadStateRestart => true, - _ => false, + ThreadState::ThreadStateRunning | ThreadState::ThreadStateRestart => true, + _ => false, } } @@ -180,13 +189,15 @@ impl tcb_t { } #[inline] - pub fn get_sched_queue(&mut self, index: usize) -> &'static mut tcb_queue_t { + pub fn get_sched_queue(&mut self, index: usize) -> &'static mut tcb_queue_t { unsafe { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { use super::scheduler::ksSMP; &mut ksSMP[self.tcbAffinity].ksReadyQueues[index] } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { use super::ksReadyQueues; &mut ksReadyQueues[index] } @@ -195,10 +206,12 @@ impl tcb_t { #[inline] pub fn get_cpu(&self) -> usize { - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { self.tcbAffinity } - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { 0 } } @@ -210,7 +223,8 @@ impl tcb_t { let idx = ready_queues_index(dom, prio); let queue = self.get_sched_queue(idx); if self.tcbSchedPrev != 0 { - convert_to_mut_type_ref::(self.tcbSchedPrev).tcbSchedNext = self.tcbSchedNext; + convert_to_mut_type_ref::(self.tcbSchedPrev).tcbSchedNext = + self.tcbSchedNext; } else { queue.head = self.tcbSchedNext as *mut tcb_t as usize; if likely(self.tcbSchedNext == 0) { @@ -218,7 +232,8 @@ impl tcb_t { } } if self.tcbSchedNext != 0 { - convert_to_mut_type_ref::(self.tcbSchedNext).tcbSchedPrev = self.tcbSchedPrev; + convert_to_mut_type_ref::(self.tcbSchedNext).tcbSchedPrev = + self.tcbSchedPrev; } else { queue.tail = self.tcbSchedPrev as *mut tcb_t as usize; } @@ -257,19 +272,21 @@ impl tcb_t { #[cfg(feature = "ENABLE_SMP")] #[inline] fn update_queue(&self) { - use super::scheduler::{ksSMP, ksCurDomain}; - use sel4_common::utils::{cpu_id, convert_to_type_ref}; + use super::scheduler::{ksCurDomain, ksSMP}; + use sel4_common::utils::{convert_to_type_ref, cpu_id}; unsafe { if self.tcbAffinity != cpu_id() && self.domain == ksCurDomain { - let target_current = convert_to_type_ref::(ksSMP[self.tcbAffinity].ksCurThread); - if ksSMP[self.tcbAffinity].ksIdleThread == ksSMP[self.tcbAffinity].ksCurThread || self.tcbPriority > target_current.tcbPriority { + let target_current = + convert_to_type_ref::(ksSMP[self.tcbAffinity].ksCurThread); + if ksSMP[self.tcbAffinity].ksIdleThread == ksSMP[self.tcbAffinity].ksCurThread + || self.tcbPriority > target_current.tcbPriority + { ksSMP[cpu_id()].ipiReschedulePending |= BIT!(self.tcbAffinity); } } } } - pub fn set_vm_root(&self) -> Result<(), lookup_fault_t> { // let threadRoot = &(*getCSpace(thread as usize, tcbVTable)).cap; let thread_root = self.get_cspace(tcbVTable).cap; @@ -295,7 +312,10 @@ impl tcb_t { pub fn lookup_slot(&self, cap_ptr: usize) -> lookupSlot_raw_ret_t { let thread_root = self.get_cspace(tcbCTable).cap; let res_ret = resolve_address_bits(&thread_root, cap_ptr, wordBits); - lookupSlot_raw_ret_t { status: res_ret.status, slot: res_ret.slot } + lookupSlot_raw_ret_t { + status: res_ret.status, + slot: res_ret.slot, + } } #[inline] @@ -341,8 +361,11 @@ impl tcb_t { let caller_slot = self.get_cspace_mut_ref(tcbCaller); assert_eq!(caller_slot.cap.get_cap_type(), CapTag::CapNullCap); - cte_insert(&cap_t::new_reply_cap(can_grant as usize, 0, sender.get_ptr()), - reply_slot, caller_slot); + cte_insert( + &cap_t::new_reply_cap(can_grant as usize, 0, sender.get_ptr()), + reply_slot, + caller_slot, + ); } #[inline] @@ -366,12 +389,17 @@ impl tcb_t { if likely(vm_rights == VMReadWrite || (!is_receiver && vm_rights == VMReadOnly)) { let base_ptr = buffer_cap.get_frame_base_ptr(); let page_bits = pageBitsForSize(buffer_cap.get_frame_size()); - return Some(convert_to_mut_type_ref::(base_ptr + (w_buffer_ptr & MASK!(page_bits)))); + return Some(convert_to_mut_type_ref::( + base_ptr + (w_buffer_ptr & MASK!(page_bits)), + )); } return None; } - pub fn lookup_extra_caps(&self, res: &mut [pptr_t; seL4_MsgMaxExtraCaps]) -> Result<(), seL4_Fault_t>{ + pub fn lookup_extra_caps( + &self, + res: &mut [pptr_t; seL4_MsgMaxExtraCaps], + ) -> Result<(), seL4_Fault_t> { let info = seL4_MessageInfo_t::from_word_security(self.get_register(msgInfoRegister)); if let Some(buffer) = self.lookup_ipc_buffer(false) { let length = info.get_extra_caps(); @@ -379,7 +407,7 @@ impl tcb_t { while i < length { let cptr = buffer.get_extra_cptr(i); let lu_ret = self.lookup_slot(cptr); - if unlikely(lu_ret.status != exception_t::EXCEPTION_NONE) { + if unlikely(lu_ret.status != exception_t::EXCEPTION_NONE) { return Err(seL4_Fault_t::new_cap_fault(cptr, false as usize)); } res[i] = lu_ret.slot as usize; @@ -392,7 +420,11 @@ impl tcb_t { Ok(()) } - pub fn lookup_extra_caps_with_buf(&self, res: &mut [pptr_t; seL4_MsgMaxExtraCaps], buf: Option<&seL4_IPCBuffer>) -> Result<(), seL4_Fault_t>{ + pub fn lookup_extra_caps_with_buf( + &self, + res: &mut [pptr_t; seL4_MsgMaxExtraCaps], + buf: Option<&seL4_IPCBuffer>, + ) -> Result<(), seL4_Fault_t> { let info = seL4_MessageInfo_t::from_word_security(self.get_register(msgInfoRegister)); if let Some(buffer) = buf { let length = info.get_extra_caps(); @@ -400,7 +432,7 @@ impl tcb_t { while i < length { let cptr = buffer.get_extra_cptr(i); let lu_ret = self.lookup_slot(cptr); - if unlikely(lu_ret.status != exception_t::EXCEPTION_NONE) { + if unlikely(lu_ret.status != exception_t::EXCEPTION_NONE) { return Err(seL4_Fault_t::new_cap_fault(cptr, false as usize)); } res[i] = lu_ret.slot as usize; @@ -413,7 +445,10 @@ impl tcb_t { Ok(()) } - pub fn lookup_mut_ipc_buffer(&mut self, is_receiver: bool) -> Option<&'static mut seL4_IPCBuffer> { + pub fn lookup_mut_ipc_buffer( + &mut self, + is_receiver: bool, + ) -> Option<&'static mut seL4_IPCBuffer> { let w_buffer_ptr = self.tcbIPCBuffer; let buffer_cap = self.get_cspace(tcbBuffer).cap; if buffer_cap.get_cap_type() != CapTag::CapFrameCap { @@ -424,7 +459,9 @@ impl tcb_t { if vm_rights == VMReadWrite || (!is_receiver && vm_rights == VMReadOnly) { let base_ptr = buffer_cap.get_frame_base_ptr(); let page_bits = pageBitsForSize(buffer_cap.get_frame_size()); - return Some(convert_to_mut_type_ref::(base_ptr + (w_buffer_ptr & MASK!(page_bits)))); + return Some(convert_to_mut_type_ref::( + base_ptr + (w_buffer_ptr & MASK!(page_bits)), + )); } return None; } @@ -444,7 +481,6 @@ impl tcb_t { } } - pub fn set_lookup_fault_mrs(&mut self, offset: usize, fault: &lookup_fault_t) -> usize { let luf_type = fault.get_type(); let i = self.set_mr(offset, luf_type + 1); @@ -460,11 +496,11 @@ impl tcb_t { LookupFaultType::InvaildRoot => i, LookupFaultType::MissingCap => { self.set_mr(offset + 1, fault.missing_cap_get_bits_left()) - }, + } LookupFaultType::DepthMismatch => { self.set_mr(offset + 1, fault.depth_mismatch_get_bits_left()); self.set_mr(offset + 2, fault.depth_mismatch_get_bits_found()) - }, + } LookupFaultType::GuardMismatch => { self.set_mr(offset + 1, fault.guard_mismatch_get_bits_left()); self.set_mr(offset + 2, fault.guard_mismatch_get_guard_found()); @@ -475,19 +511,20 @@ impl tcb_t { pub fn get_receive_slot(&mut self) -> Option<&'static mut cte_t> { if let Some(buffer) = self.lookup_ipc_buffer(true) { - let cptr= buffer.receiveCNode; + let cptr = buffer.receiveCNode; let lu_ret = self.lookup_slot(cptr); if lu_ret.status != exception_t::EXCEPTION_NONE { return None; } let cnode_cap = unsafe { &(*lu_ret.slot).cap }; let lus_ret = resolve_address_bits(cnode_cap, buffer.receiveIndex, buffer.receiveDepth); - if unlikely(lus_ret.status != exception_t::EXCEPTION_NONE || lus_ret.bitsRemaining != 0) { + if unlikely(lus_ret.status != exception_t::EXCEPTION_NONE || lus_ret.bitsRemaining != 0) + { return None; } - return Some(convert_to_mut_type_ref::(lus_ret.slot as usize)) + return Some(convert_to_mut_type_ref::(lus_ret.slot as usize)); } - return None + return None; } #[inline] @@ -497,7 +534,10 @@ impl tcb_t { receiver.set_register(msgRegister[i], self.get_register(msgRegister[i])); i += 1; } - if let (Some(send_buffer), Some(recv_buffer)) = (self.lookup_ipc_buffer(false), receiver.lookup_mut_ipc_buffer(true)) { + if let (Some(send_buffer), Some(recv_buffer)) = ( + self.lookup_ipc_buffer(false), + receiver.lookup_mut_ipc_buffer(true), + ) { unsafe { let recv_ptr = recv_buffer as *mut seL4_IPCBuffer as *mut usize; let send_ptr = send_buffer as *const seL4_IPCBuffer as *const usize; @@ -566,34 +606,49 @@ impl tcb_t { FaultType::CapFault => { receiver.set_mr(seL4_CapFault_IP, self.get_register(FaultIP)); receiver.set_mr(seL4_CapFault_Addr, self.tcbFault.cap_fault_get_address()); - receiver.set_mr(seL4_CapFault_InRecvPhase, self.tcbFault.cap_fault_get_in_receive_phase()); - receiver.set_lookup_fault_mrs(seL4_CapFault_LookupFailureType, &self.tcbLookupFailure) - }, + receiver.set_mr( + seL4_CapFault_InRecvPhase, + self.tcbFault.cap_fault_get_in_receive_phase(), + ); + receiver + .set_lookup_fault_mrs(seL4_CapFault_LookupFailureType, &self.tcbLookupFailure) + } FaultType::UnknownSyscall => { self.copy_syscall_fault_mrs(receiver); - receiver.set_mr(n_syscallMessage, self.tcbFault.unknown_syscall_get_syscall_number()) - }, + receiver.set_mr( + n_syscallMessage, + self.tcbFault.unknown_syscall_get_syscall_number(), + ) + } FaultType::UserException => { self.copy_exeception_fault_mrs(receiver); - receiver.set_mr(n_exceptionMessage, self.tcbFault.user_exeception_get_number()); - receiver.set_mr(n_exceptionMessage + 1, self.tcbFault.user_exeception_get_code()) - }, + receiver.set_mr( + n_exceptionMessage, + self.tcbFault.user_exeception_get_number(), + ); + receiver.set_mr( + n_exceptionMessage + 1, + self.tcbFault.user_exeception_get_code(), + ) + } FaultType::VMFault => { receiver.set_mr(seL4_VMFault_IP, self.get_register(FaultIP)); receiver.set_mr(seL4_VMFault_Addr, self.tcbFault.vm_fault_get_address()); - receiver.set_mr(seL4_VMFault_PrefetchFault, self.tcbFault.vm_fault_get_instruction_fault()); + receiver.set_mr( + seL4_VMFault_PrefetchFault, + self.tcbFault.vm_fault_get_instruction_fault(), + ); receiver.set_mr(seL4_VMFault_FSR, self.tcbFault.vm_fault_get_fsr()) - }, + } _ => { panic!("invalid fault") } } } - } #[inline] pub fn set_thread_state(tcb: &mut tcb_t, state: ThreadState) { tcb.tcbState.set_ts_type(state as usize); schedule_tcb(tcb); -} \ No newline at end of file +} diff --git a/sel4_task/src/tcb_queue.rs b/sel4_task/src/tcb_queue.rs index 9907ea3..00a4522 100644 --- a/sel4_task/src/tcb_queue.rs +++ b/sel4_task/src/tcb_queue.rs @@ -2,7 +2,6 @@ use sel4_common::utils::convert_to_mut_type_ref; use super::tcb::tcb_t; - #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub struct tcb_queue_t { @@ -39,6 +38,6 @@ impl tcb_queue_t { #[inline] pub fn empty(&self) -> bool { - return self.head == 0 + return self.head == 0; } } diff --git a/sel4_task/src/thread_state.rs b/sel4_task/src/thread_state.rs index 1c49847..ac691e8 100644 --- a/sel4_task/src/thread_state.rs +++ b/sel4_task/src/thread_state.rs @@ -29,8 +29,6 @@ plus_define_bitfield! { impl thread_state_t { pub fn get_state(&self) -> ThreadState { - unsafe { - core::mem::transmute::(self.get_ts_type() as u8) - } + unsafe { core::mem::transmute::(self.get_ts_type() as u8) } } -} \ No newline at end of file +} diff --git a/sel4_vspace/src/arch/aarch64.rs b/sel4_vspace/src/arch/aarch64.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/sel4_vspace/src/arch/aarch64.rs @@ -0,0 +1 @@ + diff --git a/sel4_vspace/src/arch/mod.rs b/sel4_vspace/src/arch/mod.rs new file mode 100644 index 0000000..52d719c --- /dev/null +++ b/sel4_vspace/src/arch/mod.rs @@ -0,0 +1,4 @@ +#[cfg(target_arch = "aarch64")] +mod aarch64; +#[cfg(target_arch = "riscv64")] +mod riscv64; diff --git a/sel4_vspace/src/arch/riscv64.rs b/sel4_vspace/src/arch/riscv64.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/sel4_vspace/src/arch/riscv64.rs @@ -0,0 +1 @@ + diff --git a/sel4_vspace/src/asid.rs b/sel4_vspace/src/asid.rs index 36d7b4e..211892b 100644 --- a/sel4_vspace/src/asid.rs +++ b/sel4_vspace/src/asid.rs @@ -1,10 +1,13 @@ use core::arch::asm; use core::intrinsics::unlikely; -use sel4_common::{structures::exception_t, sel4_config::*, utils::convert_to_option_mut_type_ref, fault::*, BIT, MASK}; -use sel4_cspace::interface::cap_t; +use super::{interface::set_vm_root, pte::pte_t}; use crate::structures::pptr_t; -use super::{pte::pte_t, interface::set_vm_root}; +use sel4_common::{ + fault::*, sel4_config::*, structures::exception_t, utils::convert_to_option_mut_type_ref, BIT, + MASK, +}; +use sel4_cspace::interface::cap_t; #[no_mangle] pub static mut riscvKSASIDTable: [*mut asid_pool_t; BIT!(asidHighBits)] = @@ -49,7 +52,7 @@ pub fn get_asid_pool_by_index(index: usize) -> Option<&'static mut asid_pool_t> if unlikely(index >= BIT!(asidHighBits)) { return None; } - return convert_to_option_mut_type_ref::(riscvKSASIDTable[index] as usize) + return convert_to_option_mut_type_ref::(riscvKSASIDTable[index] as usize); } } @@ -68,18 +71,14 @@ pub fn find_vspace_for_asid(asid: asid_t) -> findVSpaceForASID_ret { lookup_fault: None, }; - let poolPtr = unsafe { - riscvKSASIDTable[asid >> asidLowBits] - }; + let poolPtr = unsafe { riscvKSASIDTable[asid >> asidLowBits] }; if poolPtr as usize == 0 { ret.lookup_fault = Some(lookup_fault_t::new_root_invalid()); ret.vspace_root = None; ret.status = exception_t::EXCEPTION_LOOKUP_FAULT; return ret; } - let vspace_root = unsafe { - (*poolPtr).array[asid & MASK!(asidLowBits)] - }; + let vspace_root = unsafe { (*poolPtr).array[asid & MASK!(asidLowBits)] }; if vspace_root as usize == 0 { ret.lookup_fault = Some(lookup_fault_t::new_root_invalid()); ret.vspace_root = None; @@ -99,12 +98,19 @@ pub fn findVSpaceForASID(_asid: asid_t) -> findVSpaceForASID_ret { #[inline] fn hwASIDFlush(asid: asid_t) { + #[cfg(target_arch = "aarch64")] + todo!("hwASIDFlush"); + #[cfg(target_arch = "riscv64")] unsafe { asm!("sfence.vma x0, {0}",in(reg) asid); } } -pub fn delete_asid_pool(asid_base: asid_t, pool: *mut asid_pool_t, default_vspace_cap: &cap_t) -> Result<(), lookup_fault_t> { +pub fn delete_asid_pool( + asid_base: asid_t, + pool: *mut asid_pool_t, + default_vspace_cap: &cap_t, +) -> Result<(), lookup_fault_t> { unsafe { if riscvKSASIDTable[asid_base >> asidLowBits] == pool { riscvKSASIDTable[asid_base >> asidLowBits] = 0 as *mut asid_pool_t; @@ -115,7 +121,11 @@ pub fn delete_asid_pool(asid_base: asid_t, pool: *mut asid_pool_t, default_vspac } } -pub fn delete_asid(asid: asid_t, vspace: *mut pte_t, default_vspace_cap: &cap_t) -> Result<(), lookup_fault_t> { +pub fn delete_asid( + asid: asid_t, + vspace: *mut pte_t, + default_vspace_cap: &cap_t, +) -> Result<(), lookup_fault_t> { unsafe { let poolPtr = riscvKSASIDTable[asid >> asidLowBits]; if poolPtr as usize != 0 && (*poolPtr).array[asid & MASK!(asidLowBits)] == vspace { @@ -127,4 +137,3 @@ pub fn delete_asid(asid: asid_t, vspace: *mut pte_t, default_vspace_cap: &cap_t) } } } - diff --git a/sel4_vspace/src/interface.rs b/sel4_vspace/src/interface.rs index 4819893..96f9ee8 100644 --- a/sel4_vspace/src/interface.rs +++ b/sel4_vspace/src/interface.rs @@ -1,15 +1,25 @@ // use crate::{common::{sel4_config::*, structures::exception_t, utils::{convert_to_mut_type_ref, pageBitsForSize}, fault::*}, BIT, ROUND_DOWN}; -use sel4_cspace::interface::{cap_t, CapTag}; +use super::pte::pte_t; +use super::utils::{ + kpptr_to_paddr, RISCV_GET_LVL_PGSIZE, RISCV_GET_LVL_PGSIZE_BITS, RISCV_GET_PT_INDEX, +}; use core::intrinsics::unlikely; -use sel4_common::{BIT, ROUND_DOWN}; use sel4_common::fault::lookup_fault_t; -use sel4_common::sel4_config::{KERNEL_ELF_BASE, KERNEL_ELF_PADDR_BASE, PADDR_BASE, PPTR_BASE, PPTR_BASE_OFFSET, PPTR_TOP, PT_INDEX_BITS, seL4_PageBits}; +use sel4_common::sel4_config::{ + seL4_PageBits, KERNEL_ELF_BASE, KERNEL_ELF_PADDR_BASE, PADDR_BASE, PPTR_BASE, PPTR_BASE_OFFSET, + PPTR_TOP, PT_INDEX_BITS, +}; use sel4_common::structures::exception_t; use sel4_common::utils::{convert_to_mut_type_ref, pageBitsForSize}; -use super::pte::pte_t; -use super::utils::{RISCV_GET_PT_INDEX, RISCV_GET_LVL_PGSIZE, RISCV_GET_LVL_PGSIZE_BITS, kpptr_to_paddr}; +use sel4_common::{BIT, ROUND_DOWN}; +use sel4_cspace::interface::{cap_t, CapTag}; -use super::{satp::{setVSpaceRoot, sfence}, asid::{find_vspace_for_asid, asid_t}, utils::pptr_to_paddr, structures::{vptr_t, pptr_t}}; +use super::{ + asid::{asid_t, find_vspace_for_asid}, + satp::{setVSpaceRoot, sfence}, + structures::{pptr_t, vptr_t}, + utils::pptr_to_paddr, +}; #[no_mangle] #[link_section = ".page_table"] @@ -21,7 +31,6 @@ pub static mut kernel_root_pageTable: [pte_t; BIT!(PT_INDEX_BITS)] = pub static mut kernel_image_level2_pt: [pte_t; BIT!(PT_INDEX_BITS)] = [pte_t { words: [0] }; BIT!(PT_INDEX_BITS)]; - #[no_mangle] pub fn rust_map_kernel_window() { let mut pptr = PPTR_BASE; @@ -78,7 +87,6 @@ pub fn copyGlobalMappings(Lvl1pt: usize) { } } - pub fn set_vm_root(vspace_root: &cap_t) -> Result<(), lookup_fault_t> { if vspace_root.get_cap_type() != CapTag::CapPageTableCap { unsafe { @@ -91,7 +99,9 @@ pub fn set_vm_root(vspace_root: &cap_t) -> Result<(), lookup_fault_t> { let find_ret = find_vspace_for_asid(asid); let mut ret = Ok(()); if unlikely( - find_ret.status != exception_t::EXCEPTION_NONE || find_ret.vspace_root.is_none() || find_ret.vspace_root.unwrap() != lvl1pt, + find_ret.status != exception_t::EXCEPTION_NONE + || find_ret.vspace_root.is_none() + || find_ret.vspace_root.unwrap() != lvl1pt, ) { unsafe { if let Some(lookup_fault) = find_ret.lookup_fault { @@ -104,23 +114,30 @@ pub fn set_vm_root(vspace_root: &cap_t) -> Result<(), lookup_fault_t> { ret } - #[no_mangle] -pub fn unmapPage(page_size: usize, asid: asid_t, vptr: vptr_t, pptr: pptr_t) -> Result<(), lookup_fault_t> { +pub fn unmapPage( + page_size: usize, + asid: asid_t, + vptr: vptr_t, + pptr: pptr_t, +) -> Result<(), lookup_fault_t> { let find_ret = find_vspace_for_asid(asid); if find_ret.status != exception_t::EXCEPTION_NONE { return Err(find_ret.lookup_fault.unwrap()); } - let lu_ret = unsafe {(*find_ret.vspace_root.unwrap()).lookup_pt_slot(vptr)}; + let lu_ret = unsafe { (*find_ret.vspace_root.unwrap()).lookup_pt_slot(vptr) }; if lu_ret.ptBitsLeft != pageBitsForSize(page_size) { return Ok(()); } - let slot = unsafe {&(*lu_ret.ptSlot)}; + let slot = unsafe { &(*lu_ret.ptSlot) }; - if slot.get_vaild() == 0 || slot.is_pte_table() || slot.get_ppn() << seL4_PageBits != pptr_to_paddr(pptr) { + if slot.get_vaild() == 0 + || slot.is_pte_table() + || slot.get_ppn() << seL4_PageBits != pptr_to_paddr(pptr) + { return Ok(()); } @@ -130,4 +147,4 @@ pub fn unmapPage(page_size: usize, asid: asid_t, vptr: vptr_t, pptr: pptr_t) -> sfence(); } Ok(()) -} \ No newline at end of file +} diff --git a/sel4_vspace/src/lib.rs b/sel4_vspace/src/lib.rs index d8a98ea..3159868 100644 --- a/sel4_vspace/src/lib.rs +++ b/sel4_vspace/src/lib.rs @@ -4,21 +4,27 @@ #![allow(non_camel_case_types)] #![allow(non_upper_case_globals)] -mod structures; -mod vm_rights; -mod satp; -mod utils; -mod pte; +pub mod arch; mod asid; pub mod interface; +mod pte; +mod satp; +mod structures; +mod utils; +mod vm_rights; -pub use structures::*; -pub use interface::{activate_kernel_vspace, rust_map_kernel_window, copyGlobalMappings, set_vm_root, unmapPage}; -pub use vm_rights::{VMReadWrite, VMReadOnly, maskVMRights}; pub use asid::{ - asid_t, asid_pool_t, riscvKSASIDTable, delete_asid_pool, delete_asid, - find_vspace_for_asid, get_asid_pool_by_index, set_asid_pool_by_index + asid_pool_t, asid_t, delete_asid, delete_asid_pool, find_vspace_for_asid, + get_asid_pool_by_index, riscvKSASIDTable, set_asid_pool_by_index, +}; +pub use interface::{ + activate_kernel_vspace, copyGlobalMappings, rust_map_kernel_window, set_vm_root, unmapPage, }; -pub use utils::{pptr_to_paddr, paddr_to_pptr, kpptr_to_paddr, RISCV_GET_LVL_PGSIZE_BITS, RISCV_GET_LVL_PGSIZE, checkVPAlignment}; pub use pte::pte_t; -pub use satp::{sfence, setVSpaceRoot}; \ No newline at end of file +pub use satp::{setVSpaceRoot, sfence}; +pub use structures::*; +pub use utils::{ + checkVPAlignment, kpptr_to_paddr, paddr_to_pptr, pptr_to_paddr, RISCV_GET_LVL_PGSIZE, + RISCV_GET_LVL_PGSIZE_BITS, +}; +pub use vm_rights::{maskVMRights, VMReadOnly, VMReadWrite}; diff --git a/sel4_vspace/src/pte.rs b/sel4_vspace/src/pte.rs index 0e2fc09..1e436ec 100644 --- a/sel4_vspace/src/pte.rs +++ b/sel4_vspace/src/pte.rs @@ -1,14 +1,15 @@ use core::intrinsics::unlikely; -use sel4_common::MASK; -use sel4_common::sel4_config::{CONFIG_PT_LEVELS, PT_INDEX_BITS, seL4_PageBits, seL4_PageTableBits}; +use sel4_common::sel4_config::{ + seL4_PageBits, seL4_PageTableBits, CONFIG_PT_LEVELS, PT_INDEX_BITS, +}; use sel4_common::structures::exception_t; use sel4_common::utils::{convert_to_mut_type_ref, convert_to_type_ref}; +use sel4_common::MASK; - -use super::{structures::vptr_t, satp::sfence}; -use super::utils::{paddr_to_pptr, RISCV_GET_PT_INDEX}; use super::asid::{asid_t, find_vspace_for_asid}; -use super::vm_rights::{RISCVGetWriteFromVMRights, RISCVGetReadFromVMRights}; +use super::utils::{paddr_to_pptr, RISCV_GET_PT_INDEX}; +use super::vm_rights::{RISCVGetReadFromVMRights, RISCVGetWriteFromVMRights}; +use super::{satp::sfence, structures::vptr_t}; #[repr(C)] #[derive(Copy, Clone)] @@ -30,9 +31,18 @@ impl pte_t { } #[inline] - pub fn new(ppn: usize, sw: usize, dirty: usize, accessed: usize, global: usize, user: usize, execute: usize, write: usize, - read: usize, valid: usize) -> Self { - + pub fn new( + ppn: usize, + sw: usize, + dirty: usize, + accessed: usize, + global: usize, + user: usize, + execute: usize, + write: usize, + read: usize, + valid: usize, + ) -> Self { pte_t { words: [0 | (ppn & 0xfffffffffffusize) << 10 @@ -77,8 +87,16 @@ impl pte_t { let write = read; let exec = read; Self::new( - ppn, 0, is_leaf as usize, is_leaf as usize, 1, - 0, exec as usize, write as usize,read as usize, 1 + ppn, + 0, + is_leaf as usize, + is_leaf as usize, + 1, + 0, + exec as usize, + write as usize, + read as usize, + 1, ) } @@ -121,7 +139,8 @@ impl pte_t { #[inline] pub fn is_pte_table(&self) -> bool { - self.get_vaild() != 0 && !(self.get_read() != 0 ||self.get_write() != 0 || self.get_execute() != 0) + self.get_vaild() != 0 + && !(self.get_read() != 0 || self.get_write() != 0 || self.get_execute() != 0) } #[inline] @@ -144,10 +163,10 @@ impl pte_t { }, }; - while unsafe {(*ret.ptSlot).is_pte_table()} && level > 0 { + while unsafe { (*ret.ptSlot).is_pte_table() } && level > 0 { level -= 1; ret.ptBitsLeft -= PT_INDEX_BITS; - pt = unsafe {(*ret.ptSlot).get_pte_from_ppn_mut() as *mut pte_t}; + pt = unsafe { (*ret.ptSlot).get_pte_from_ppn_mut() as *mut pte_t }; ret.ptSlot = unsafe { pt.add((vptr >> ret.ptBitsLeft) & MASK!(PT_INDEX_BITS)) }; } ret diff --git a/sel4_vspace/src/satp.rs b/sel4_vspace/src/satp.rs index 3e90487..0a412ce 100644 --- a/sel4_vspace/src/satp.rs +++ b/sel4_vspace/src/satp.rs @@ -1,12 +1,7 @@ +use super::structures::paddr_t; use riscv::register::satp; -#[cfg(target_arch = "riscv64")] +#[cfg(feature = "ENABLE_SMP")] use sel4_common::sbi::remote_sfence_vma; -use super::structures::paddr_t; - -#[cfg(target_arch = "aarch64")] -pub fn remote_sfence_vma(hart_mask: usize, start: usize, size: usize) { - // tmp empty funcion and useless, TODO: clean -} #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -49,12 +44,14 @@ pub fn sfence_local() { #[cfg(not(feature = "ENABLE_SMP"))] #[inline] pub fn sfence() { + #[cfg(target_arch = "riscv64")] unsafe { core::arch::asm!("sfence.vma"); } + #[cfg(target_arch = "aarch64")] + todo!("sfence") } - #[inline] #[no_mangle] pub fn setVSpaceRoot(addr: paddr_t, asid: usize) { @@ -64,4 +61,4 @@ pub fn setVSpaceRoot(addr: paddr_t, asid: usize) { sfence(); #[cfg(feature = "ENABLE_SMP")] sfence_local(); -} \ No newline at end of file +} diff --git a/sel4_vspace/src/structures.rs b/sel4_vspace/src/structures.rs index 335a080..b0c1d0b 100644 --- a/sel4_vspace/src/structures.rs +++ b/sel4_vspace/src/structures.rs @@ -8,7 +8,6 @@ pub struct vm_attributes_t { pub words: [usize; 1], } - impl vm_attributes_t { pub fn new(value: usize) -> Self { Self { @@ -17,9 +16,7 @@ impl vm_attributes_t { } pub fn from_word(w: usize) -> Self { - Self { - words: [w] - } + Self { words: [w] } } pub fn get_execute_never(&self) -> usize { @@ -30,4 +27,4 @@ impl vm_attributes_t { self.words[0] &= !0x1usize; self.words[0] |= (v64 << 0) & 0x1usize; } -} \ No newline at end of file +} diff --git a/sel4_vspace/src/utils.rs b/sel4_vspace/src/utils.rs index a074680..1522ea0 100644 --- a/sel4_vspace/src/utils.rs +++ b/sel4_vspace/src/utils.rs @@ -1,8 +1,9 @@ -use sel4_common::{BIT, MASK}; -use sel4_common::sel4_config::{CONFIG_PT_LEVELS, KERNEL_ELF_BASE_OFFSET, PPTR_BASE_OFFSET, PT_INDEX_BITS, seL4_PageBits}; -use sel4_common::utils::pageBitsForSize; use super::structures::paddr_t; - +use sel4_common::sel4_config::{ + seL4_PageBits, CONFIG_PT_LEVELS, KERNEL_ELF_BASE_OFFSET, PPTR_BASE_OFFSET, PT_INDEX_BITS, +}; +use sel4_common::utils::pageBitsForSize; +use sel4_common::{BIT, MASK}; #[inline] pub fn RISCV_GET_PT_INDEX(addr: usize, n: usize) -> usize { @@ -39,4 +40,4 @@ pub fn paddr_to_pptr(x: usize) -> paddr_t { #[no_mangle] pub fn checkVPAlignment(sz: usize, w: usize) -> bool { w & MASK!(pageBitsForSize(sz)) == 0 -} \ No newline at end of file +} diff --git a/sel4_vspace/src/vm_rights.rs b/sel4_vspace/src/vm_rights.rs index ff1b404..b552065 100644 --- a/sel4_vspace/src/vm_rights.rs +++ b/sel4_vspace/src/vm_rights.rs @@ -4,7 +4,6 @@ pub const VMKernelOnly: usize = 1; pub const VMReadOnly: usize = 2; pub const VMReadWrite: usize = 3; - pub fn RISCVGetWriteFromVMRights(vm_rights: usize) -> bool { return vm_rights == VMReadWrite; } @@ -23,7 +22,7 @@ pub fn maskVMRights(vm_rights: usize, rights: seL4_CapRights_t) -> usize { VMReadOnly } else { VMReadWrite - } + }; } VMKernelOnly -} \ No newline at end of file +} diff --git a/src/.cargo/config b/src/.cargo/config index 7024cc6..e041379 100644 --- a/src/.cargo/config +++ b/src/.cargo/config @@ -1,6 +1,2 @@ [build] -target = 'aarch64-unknown-linux-gnu' -# target = "riscv64imac-unknown-none-elf" - -[target.aarch64-unknown-linux-gnu] -[target.riscv64imac-unknown-none-elf] \ No newline at end of file +target = "riscv64imac-unknown-none-elf" \ No newline at end of file diff --git a/src/arm_gic/gic_v2/consts.rs b/src/arch/aarch64/arm_gic/gic_v2/consts.rs similarity index 100% rename from src/arm_gic/gic_v2/consts.rs rename to src/arch/aarch64/arm_gic/gic_v2/consts.rs diff --git a/src/arm_gic/gic_v2/gic_v2.rs b/src/arch/aarch64/arm_gic/gic_v2/gic_v2.rs similarity index 100% rename from src/arm_gic/gic_v2/gic_v2.rs rename to src/arch/aarch64/arm_gic/gic_v2/gic_v2.rs diff --git a/src/arm_gic/gic_v2/mod.rs b/src/arch/aarch64/arm_gic/gic_v2/mod.rs similarity index 100% rename from src/arm_gic/gic_v2/mod.rs rename to src/arch/aarch64/arm_gic/gic_v2/mod.rs diff --git a/src/arm_gic/gic_v3/consts.rs b/src/arch/aarch64/arm_gic/gic_v3/consts.rs similarity index 100% rename from src/arm_gic/gic_v3/consts.rs rename to src/arch/aarch64/arm_gic/gic_v3/consts.rs diff --git a/src/arm_gic/gic_v3/gic_v3.rs b/src/arch/aarch64/arm_gic/gic_v3/gic_v3.rs similarity index 100% rename from src/arm_gic/gic_v3/gic_v3.rs rename to src/arch/aarch64/arm_gic/gic_v3/gic_v3.rs diff --git a/src/arm_gic/gic_v3/mod.rs b/src/arch/aarch64/arm_gic/gic_v3/mod.rs similarity index 100% rename from src/arm_gic/gic_v3/mod.rs rename to src/arch/aarch64/arm_gic/gic_v3/mod.rs diff --git a/src/arm_gic/mod.rs b/src/arch/aarch64/arm_gic/mod.rs similarity index 100% rename from src/arm_gic/mod.rs rename to src/arch/aarch64/arm_gic/mod.rs diff --git a/src/arch/aarch64/c_traps.rs b/src/arch/aarch64/c_traps.rs new file mode 100644 index 0000000..0f8ad9f --- /dev/null +++ b/src/arch/aarch64/c_traps.rs @@ -0,0 +1,154 @@ +use core::arch::asm; + +use super::read_scause; +use crate::{ + config::{ + RISCVInstructionAccessFault, RISCVInstructionPageFault, RISCVLoadAccessFault, + RISCVLoadPageFault, RISCVStoreAccessFault, RISCVStorePageFault, + }, + syscall::slowpath, +}; + +use sel4_task::*; + +use super::exception::{handleUserLevelFault, handleVMFaultEvent}; +use crate::interrupt::handler::handleInterruptEntry; + +#[cfg(feature = "ENABLE_SMP")] +use crate::{ + deps::{clh_is_self_in_queue, clh_lock_acquire, clh_lock_release}, + interrupt::getActiveIRQ, +}; + +#[cfg(feature = "ENABLE_SMP")] +use sel4_common::utils::cpu_id; + +#[no_mangle] +pub fn restore_user_context() { + unsafe { + // debug!("restore_user_context"); + let cur_thread_reg: usize = get_currenct_thread().tcbArch.registers.as_ptr() as usize; + #[cfg(feature = "ENABLE_SMP")] + { + if clh_is_self_in_queue() { + clh_lock_release(cpu_id()); + } + // debug!("restore_user_context2"); + let mut cur_sp: usize = 8; + asm!( + "csrr {}, sscratch", + out(reg) cur_sp, + ); + // debug!("cur_sp: {:#x}", cur_sp); + *((cur_sp - 8) as *mut usize) = cur_thread_reg; + } + // debug!("restore_user_context3"); + unimplemented!("restore_user_context"); + // asm!("mv t0, {0} \n", + // "ld ra, (0*8)(t0) \n", + // "ld sp, (1*8)(t0) \n", + // "ld gp, (2*8)(t0) \n", + // "ld t2, (6*8)(t0) \n", + // "ld s0, (7*8)(t0) \n", + // "ld s1, (8*8)(t0) \n", + // "ld a0, (9*8)(t0) \n", + // "ld a1, (10*8)(t0) \n", + // "ld a2, (11*8)(t0) \n", + // "ld a3, (12*8)(t0) \n", + // "ld a4, (13*8)(t0) \n", + // "ld a5, (14*8)(t0) \n", + // "ld a6, (15*8)(t0) \n", + // "ld a7, (16*8)(t0) \n", + // "ld s2, (17*8)(t0) \n", + // "ld s3, (18*8)(t0) \n", + // "ld s4, (19*8)(t0) \n", + // "ld s5, (20*8)(t0) \n", + // "ld s6, (21*8)(t0) \n", + // "ld s7, (22*8)(t0) \n", + // "ld s8, (23*8)(t0) \n", + // "ld s9, (24*8)(t0) \n", + // "ld s10, (25*8)(t0)\n", + // "ld s11, (26*8)(t0)\n", + // "ld t3, (27*8)(t0) \n", + // "ld t4, (28*8)(t0) \n", + // "ld t5, (29*8)(t0) \n", + // "ld t6, (30*8)(t0) \n", + // "ld t1, (3*8)(t0) \n", + // "add tp, t1, x0 \n", + // "ld t1, (34*8)(t0)\n", + // "csrw sepc, t1", in(reg) cur_thread_reg); + + // #[cfg(not(feature = "ENABLE_SMP"))] { + // asm!( + // "csrw sscratch, t0" + // ) + // } + // asm!( + // "ld t1, (32*8)(t0) \n", + // "csrw sstatus, t1\n", + // "ld t1, (5*8)(t0) \n", + // "ld t0, (4*8)(t0) \n", + // "sret"); + panic!("unreachable") + } +} + +#[no_mangle] +pub fn c_handle_interrupt() { + // debug!("c_handle_interrupt"); + // if hart_id() != 0 { + // debug!("c_handle_interrupt"); + // } + #[cfg(feature = "ENABLE_SMP")] + { + use crate::config::INTERRUPT_IPI_0; + if getActiveIRQ() != INTERRUPT_IPI_0 { + unsafe { + clh_lock_acquire(cpu_id(), true); + } + } + } + // debug!("c_handle_interrupt"); + handleInterruptEntry(); + restore_user_context(); +} + +#[no_mangle] +pub fn c_handle_exception() { + #[cfg(feature = "ENABLE_SMP")] + unsafe { + clh_lock_acquire(cpu_id(), false); + } + // if hart_id() == 0 { + // debug!("c_handle_exception"); + // } + + let cause = read_scause(); + match cause { + RISCVInstructionAccessFault + | RISCVLoadAccessFault + | RISCVStoreAccessFault + | RISCVLoadPageFault + | RISCVStorePageFault + | RISCVInstructionPageFault => { + handleVMFaultEvent(cause); + } + _ => { + handleUserLevelFault(cause, 0); + } + } + restore_user_context(); +} + +#[no_mangle] +pub fn c_handle_syscall(_cptr: usize, _msgInfo: usize, syscall: usize) { + #[cfg(feature = "ENABLE_SMP")] + unsafe { + clh_lock_acquire(cpu_id(), false); + } + // if hart_id() == 0 { + // debug!("c_handle_syscall: syscall: {},", syscall as isize); + // } + slowpath(syscall); + // debug!("c_handle_syscall complete"); +} diff --git a/src/exception.rs b/src/arch/aarch64/exception.rs similarity index 98% rename from src/exception.rs rename to src/arch/aarch64/exception.rs index f00c243..5eff028 100644 --- a/src/exception.rs +++ b/src/arch/aarch64/exception.rs @@ -1,10 +1,10 @@ +use super::read_stval; +use crate::config::*; +use crate::kernel::boot::current_fault; +use crate::syscall::handle_fault; use sel4_common::fault::seL4_Fault_t; use sel4_common::structures::exception_t; use sel4_task::{activateThread, get_currenct_thread, schedule}; -use crate::kernel::boot::current_fault; -use crate::config::*; -use crate::riscv::read_stval; -use crate::syscall::handle_fault; #[no_mangle] pub fn handleUserLevelFault(w_a: usize, w_b: usize) -> exception_t { diff --git a/src/arch/aarch64/mod.rs b/src/arch/aarch64/mod.rs new file mode 100644 index 0000000..ea68bbd --- /dev/null +++ b/src/arch/aarch64/mod.rs @@ -0,0 +1,80 @@ +mod c_traps; +mod exception; +pub mod arm_gic; + +use core::arch::asm; + +use crate::config::RESET_CYCLES; +pub use c_traps::restore_user_context; +use sel4_common::arch::set_timer; + +pub fn read_stval() -> usize { + // let temp: usize; + // unsafe { + // asm!("csrr {}, stval",out(reg)temp); + // } + // temp + todo!("read_stval") +} + +pub extern "C" fn write_stvec(val: usize) { + // unsafe { + // asm!("csrw stvec , {}",in(reg) val); + // } + todo!("write_stvec") +} + +pub fn read_sip() -> usize { + // let temp: usize; + // unsafe { + // asm!("csrr {}, sip",out(reg)temp); + // } + // temp + todo!("read_sip") +} + +pub fn read_time() -> usize { + // let temp: usize; + // unsafe { + // asm!("rdtime {}",out(reg)temp); + // } + // temp + todo!("read_time") +} + +pub fn read_scause() -> usize { + // let temp: usize; + // unsafe { + // asm!("csrr {}, scause",out(reg)temp); + // } + // temp + todo!("read_scause") +} + +pub fn read_sepc() -> usize { + // let temp: usize; + // unsafe { + // asm!("csrr {}, sepc",out(reg)temp); + // } + // temp + todo!("read_sepc") +} + +pub fn read_sstatus() -> usize { + // let temp: usize; + // unsafe { + // asm!("csrr {}, sstatus",out(reg)temp); + // } + // temp + todo!("read_sstatus") +} + +#[no_mangle] +pub fn resetTimer() { + let mut target = read_time() + RESET_CYCLES; + set_timer(target); + while read_time() > target { + target = read_time() + RESET_CYCLES; + set_timer(target); + } +} diff --git a/src/arch/mod.rs b/src/arch/mod.rs new file mode 100644 index 0000000..83578a9 --- /dev/null +++ b/src/arch/mod.rs @@ -0,0 +1,9 @@ +#[cfg(target_arch = "aarch64")] +mod aarch64; +#[cfg(target_arch = "aarch64")] +pub use aarch64::*; + +#[cfg(target_arch = "riscv64")] +mod riscv; +#[cfg(target_arch = "riscv64")] +pub use riscv::*; diff --git a/src/kernel/c_traps.rs b/src/arch/riscv/c_traps.rs similarity index 80% rename from src/kernel/c_traps.rs rename to src/arch/riscv/c_traps.rs index c7fb301..77b39cd 100644 --- a/src/kernel/c_traps.rs +++ b/src/arch/riscv/c_traps.rs @@ -1,33 +1,33 @@ use core::arch::asm; +use super::read_scause; use crate::{ config::{ RISCVInstructionAccessFault, RISCVInstructionPageFault, RISCVLoadAccessFault, RISCVLoadPageFault, RISCVStoreAccessFault, RISCVStorePageFault, }, - riscv::read_scause, syscall::slowpath + syscall::slowpath, }; use sel4_task::*; -use crate::exception::{handleUserLevelFault, handleVMFaultEvent}; +use super::exception::{handleUserLevelFault, handleVMFaultEvent}; use crate::interrupt::handler::handleInterruptEntry; #[cfg(feature = "ENABLE_SMP")] use crate::{ + deps::{clh_is_self_in_queue, clh_lock_acquire, clh_lock_release}, interrupt::getActiveIRQ, - deps::{clh_is_self_in_queue, clh_lock_release, clh_lock_acquire} }; +#[cfg(feature = "ENABLE_SMP")] use sel4_common::utils::cpu_id; #[no_mangle] pub fn restore_user_context() { - - #[cfg(target_arch = "riscv64")] unsafe { // debug!("restore_user_context"); - let cur_thread_reg = get_currenct_thread().tcbArch.registers.as_ptr() as usize; + let cur_thread_reg: usize = get_currenct_thread().tcbArch.registers.as_ptr() as usize; #[cfg(feature = "ENABLE_SMP")] { if clh_is_self_in_queue() { @@ -77,33 +77,34 @@ pub fn restore_user_context() { "ld t1, (34*8)(t0)\n", "csrw sepc, t1", in(reg) cur_thread_reg); - #[cfg(not(feature = "ENABLE_SMP"))] { - asm!( - "csrw sscratch, t0" - ) + #[cfg(not(feature = "ENABLE_SMP"))] + { + asm!("csrw sscratch, t0") } asm!( - "ld t1, (32*8)(t0) \n", - "csrw sstatus, t1\n", - "ld t1, (5*8)(t0) \n", - "ld t0, (4*8)(t0) \n", - "sret"); + "ld t1, (32*8)(t0) \n", + "csrw sstatus, t1\n", + "ld t1, (5*8)(t0) \n", + "ld t0, (4*8)(t0) \n", + "sret" + ); panic!("unreachable") } } - - #[no_mangle] pub fn c_handle_interrupt() { // debug!("c_handle_interrupt"); // if hart_id() != 0 { // debug!("c_handle_interrupt"); // } - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { use crate::config::INTERRUPT_IPI_0; if getActiveIRQ() != INTERRUPT_IPI_0 { - unsafe { clh_lock_acquire(cpu_id(), true); } + unsafe { + clh_lock_acquire(cpu_id(), true); + } } } // debug!("c_handle_interrupt"); @@ -114,7 +115,9 @@ pub fn c_handle_interrupt() { #[no_mangle] pub fn c_handle_exception() { #[cfg(feature = "ENABLE_SMP")] - unsafe { clh_lock_acquire(cpu_id(), false); } + unsafe { + clh_lock_acquire(cpu_id(), false); + } // if hart_id() == 0 { // debug!("c_handle_exception"); // } @@ -139,7 +142,9 @@ pub fn c_handle_exception() { #[no_mangle] pub fn c_handle_syscall(_cptr: usize, _msgInfo: usize, syscall: usize) { #[cfg(feature = "ENABLE_SMP")] - unsafe { clh_lock_acquire(cpu_id(), false); } + unsafe { + clh_lock_acquire(cpu_id(), false); + } // if hart_id() == 0 { // debug!("c_handle_syscall: syscall: {},", syscall as isize); // } diff --git a/src/arch/riscv/exception.rs b/src/arch/riscv/exception.rs new file mode 100644 index 0000000..5eff028 --- /dev/null +++ b/src/arch/riscv/exception.rs @@ -0,0 +1,55 @@ +use super::read_stval; +use crate::config::*; +use crate::kernel::boot::current_fault; +use crate::syscall::handle_fault; +use sel4_common::fault::seL4_Fault_t; +use sel4_common::structures::exception_t; +use sel4_task::{activateThread, get_currenct_thread, schedule}; + +#[no_mangle] +pub fn handleUserLevelFault(w_a: usize, w_b: usize) -> exception_t { + unsafe { + current_fault = seL4_Fault_t::new_user_exeception(w_a, w_b); + handle_fault(get_currenct_thread()); + } + schedule(); + activateThread(); + exception_t::EXCEPTION_NONE +} + +#[no_mangle] +pub fn handleVMFaultEvent(vm_faultType: usize) -> exception_t { + let status = handle_vm_fault(vm_faultType); + if status != exception_t::EXCEPTION_NONE { + // debug!("handle_fault: {}", vm_faultType); + handle_fault(get_currenct_thread()); + } + schedule(); + activateThread(); + exception_t::EXCEPTION_NONE +} + +pub fn handle_vm_fault(type_: usize) -> exception_t { + let addr = read_stval(); + match type_ { + RISCVLoadPageFault | RISCVLoadAccessFault => { + unsafe { + current_fault = seL4_Fault_t::new_vm_fault(addr, RISCVLoadAccessFault, 0); + } + exception_t::EXCEPTION_FAULT + } + RISCVStorePageFault | RISCVStoreAccessFault => { + unsafe { + current_fault = seL4_Fault_t::new_vm_fault(addr, RISCVStoreAccessFault, 0); + } + exception_t::EXCEPTION_FAULT + } + RISCVInstructionAccessFault | RISCVInstructionPageFault => { + unsafe { + current_fault = seL4_Fault_t::new_vm_fault(addr, RISCVInstructionAccessFault, 1); + } + exception_t::EXCEPTION_FAULT + } + _ => panic!("Invalid VM fault type:{}", type_), + } +} diff --git a/src/riscv.rs b/src/arch/riscv/mod.rs similarity index 87% rename from src/riscv.rs rename to src/arch/riscv/mod.rs index 1d8db2b..5f65102 100644 --- a/src/riscv.rs +++ b/src/arch/riscv/mod.rs @@ -1,8 +1,13 @@ +mod c_traps; +mod exception; + use core::arch::asm; use crate::config::RESET_CYCLES; -#[cfg(target_arch = "riscv64")] -use sel4_common::sbi::set_timer; +pub use c_traps::restore_user_context; +use sel4_common::arch::sbi::set_timer; + +core::arch::global_asm!(include_str!("restore_fp.S")); pub fn read_stval() -> usize { let temp: usize; @@ -66,4 +71,4 @@ pub fn resetTimer() { target = read_time() + RESET_CYCLES; set_timer(target); } -} \ No newline at end of file +} diff --git a/src/kernel/restore_fp.S b/src/arch/riscv/restore_fp.S similarity index 100% rename from src/kernel/restore_fp.S rename to src/arch/riscv/restore_fp.S diff --git a/src/boot/interface.rs b/src/boot/interface.rs index ed6ae4c..2a787ad 100644 --- a/src/boot/interface.rs +++ b/src/boot/interface.rs @@ -1,6 +1,9 @@ use sel4_task::{get_idle_thread, set_current_thread, tcb_t}; -use super::{mm::{avail_p_regs_addr, avail_p_regs_size}, try_init_kernel}; +use super::{ + mm::{avail_p_regs_addr, avail_p_regs_size}, + try_init_kernel, +}; #[no_mangle] pub fn pRegsToR(ptr: *const usize, size: usize) { @@ -16,14 +19,23 @@ extern "C" { } #[no_mangle] -pub fn rust_try_init_kernel(ui_p_reg_start: usize, +pub fn rust_try_init_kernel( + ui_p_reg_start: usize, ui_p_reg_end: usize, pv_offset: isize, v_entry: usize, dtb_phys_addr: usize, - dtb_size: usize) -> bool { - - try_init_kernel(ui_p_reg_start, ui_p_reg_end, pv_offset, v_entry, dtb_phys_addr, dtb_size, ki_boot_end as usize) + dtb_size: usize, +) -> bool { + try_init_kernel( + ui_p_reg_start, + ui_p_reg_end, + pv_offset, + v_entry, + dtb_phys_addr, + dtb_size, + ki_boot_end as usize, + ) } #[cfg(feature = "ENABLE_SMP")] @@ -46,5 +58,4 @@ pub fn switchToIdleThread() { // panic!("should not be invoke!"); let _ = get_idle_thread().set_vm_root(); set_current_thread(get_idle_thread()); - } diff --git a/src/boot/mm.rs b/src/boot/mm.rs index 10eb458..8469d6c 100644 --- a/src/boot/mm.rs +++ b/src/boot/mm.rs @@ -1,7 +1,7 @@ - -use sel4_common::sel4_config::KERNEL_ELF_BASE; use log::debug; +use sel4_common::sel4_config::KERNEL_ELF_BASE; +use super::ndks_boot; use crate::boot::utils::ceiling_kernel_window; use crate::boot::utils::is_reg_empty; use crate::boot::utils::paddr_to_pptr_reg; @@ -9,7 +9,6 @@ use crate::boot::utils::pptr_to_paddr_reg; use crate::config::*; use crate::structures::*; use sel4_vspace::*; -use super::ndks_boot; #[link_section = ".boot.bss"] static mut res_reg: [region_t; NUM_RESERVED_REGIONS] = [region_t { start: 0, end: 0 }; NUM_RESERVED_REGIONS]; @@ -24,7 +23,6 @@ pub static mut avail_p_regs_addr: usize = 0; #[link_section = ".boot.bss"] pub static mut avail_p_regs_size: usize = 0; - pub fn init_freemem(ui_reg: region_t, dtb_p_reg: p_region_t) -> bool { extern "C" { fn ki_end(); @@ -53,16 +51,10 @@ pub fn init_freemem(ui_reg: region_t, dtb_p_reg: p_region_t) -> bool { unsafe { res_reg[index] = ui_reg; index += 1; - rust_init_freemem( - avail_p_regs_size, - avail_p_regs_addr, - index, - res_reg.clone(), - ) + rust_init_freemem(avail_p_regs_size, avail_p_regs_addr, index, res_reg.clone()) } } - fn rust_init_freemem( n_available: usize, available: usize, @@ -152,7 +144,6 @@ fn rust_init_freemem( ); return false; } - } true } @@ -188,11 +179,7 @@ fn check_available_memory(n_available: usize, available: usize) -> bool { return true; } - -fn check_reserved_memory( - n_reserved: usize, - reserved: [region_t; NUM_RESERVED_REGIONS], -) -> bool { +fn check_reserved_memory(n_reserved: usize, reserved: [region_t; NUM_RESERVED_REGIONS]) -> bool { debug!("reserved virt address space regions: {}", n_reserved); let mut last: region_t = reserved[0].clone(); for i in 0..n_reserved { @@ -241,45 +228,45 @@ fn insert_region(reg: region_t) -> bool { unsafe fn reserve_region(reg: p_region_t) -> bool { assert!(reg.start <= reg.end); - if reg.start == reg.end { + if reg.start == reg.end { + return true; + } + + let mut i = 0; + while i < ndks_boot.resv_count { + if ndks_boot.reserved[i].start == reg.end { + ndks_boot.reserved[i].start = reg.start; + merge_regions(); return true; } - - let mut i = 0; - while i < ndks_boot.resv_count { - if ndks_boot.reserved[i].start == reg.end { - ndks_boot.reserved[i].start = reg.start; - merge_regions(); - return true; + if ndks_boot.reserved[i].end == reg.start { + ndks_boot.reserved[i].end = reg.end; + merge_regions(); + return true; + } + if ndks_boot.reserved[i].start > reg.end { + if ndks_boot.resv_count + 1 >= MAX_NUM_RESV_REG { + debug!("Can't mark region {:#x}-{:#x} as reserved, try increasing MAX_NUM_RESV_REG (currently {})\n",reg.start,reg.end,MAX_NUM_RESV_REG); + return false; } - if ndks_boot.reserved[i].end == reg.start { - ndks_boot.reserved[i].end = reg.end; - merge_regions(); - return true; + let mut j = ndks_boot.resv_count; + while j > i { + ndks_boot.reserved[j] = ndks_boot.reserved[j - 1]; + j -= 1; } - if ndks_boot.reserved[i].start > reg.end { - if ndks_boot.resv_count + 1 >= MAX_NUM_RESV_REG { - debug!("Can't mark region {:#x}-{:#x} as reserved, try increasing MAX_NUM_RESV_REG (currently {})\n",reg.start,reg.end,MAX_NUM_RESV_REG); - return false; - } - let mut j = ndks_boot.resv_count; - while j > i { - ndks_boot.reserved[j] = ndks_boot.reserved[j - 1]; - j -= 1; - } - ndks_boot.reserved[i] = reg; - ndks_boot.resv_count += 1; - return true; - } - i += 1; - } - if i + 1 == MAX_NUM_RESV_REG { - debug!("Can't mark region 0x{}-0x{} as reserved, try increasing MAX_NUM_RESV_REG (currently {})\n",reg.start,reg.end,MAX_NUM_RESV_REG); - return false; + ndks_boot.reserved[i] = reg; + ndks_boot.resv_count += 1; + return true; } - ndks_boot.reserved[i] = reg; - ndks_boot.resv_count += 1; - return true; + i += 1; + } + if i + 1 == MAX_NUM_RESV_REG { + debug!("Can't mark region 0x{}-0x{} as reserved, try increasing MAX_NUM_RESV_REG (currently {})\n",reg.start,reg.end,MAX_NUM_RESV_REG); + return false; + } + ndks_boot.reserved[i] = reg; + ndks_boot.resv_count += 1; + return true; } unsafe fn merge_regions() { @@ -297,4 +284,4 @@ unsafe fn merge_regions() { i += 1; } } -} \ No newline at end of file +} diff --git a/src/boot/mod.rs b/src/boot/mod.rs index bdd24f4..1583a6b 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -1,24 +1,19 @@ - +mod interface; mod mm; mod root_server; mod untyped; mod utils; -mod interface; + +use core::mem::size_of; + #[cfg(target_arch = "aarch64")] mod fpu; - #[cfg(target_arch = "aarch64")] mod user_access; -use core::mem::size_of; - -use crate::deps::{tcbDebugAppend, init_plat, initTimer}; +use crate::deps::{init_plat, tcbDebugAppend}; use crate::{BIT, ROUND_UP}; -use sel4_common::sel4_config::{seL4_PageBits, CONFIG_KERNEL_STACK_BITS, CURRENT_CPU_INDEX, KERNEL_ELF_BASE, PADDR_TOP, PAGE_BITS}; use log::debug; -use sel4_common::utils::cpu_id; -use sel4_common::deps::kernel_stack_alloc; -use spin::Mutex; #[cfg(target_arch = "riscv64")] use riscv::register::{stvec,utvec::TrapMode}; #[cfg(target_arch = "aarch64")] @@ -27,26 +22,30 @@ use aarch64_cpu::registers::*; use aarch64_cpu::asm::barrier::{dsb,isb,SY}; #[cfg(target_arch = "aarch64")] use crate::arm_gic::gic_v2; +use sel4_common::sel4_config::{seL4_PageBits, KERNEL_ELF_BASE, PADDR_TOP, PAGE_BITS}; +use spin::Mutex; use crate::boot::mm::init_freemem; use crate::boot::root_server::root_server_init; use crate::boot::untyped::create_untypeds; use crate::boot::utils::paddr_to_pptr_reg; -use crate::interrupt::set_sie_mask; -#[cfg(target_arch = "riscv64")] -use sel4_common::sbi::{set_timer, get_time}; -use crate::structures::{ndks_boot_t, region_t, p_region_t, seL4_BootInfo, seL4_BootInfoHeader, seL4_SlotRegion, v_region_t}; use crate::config::*; +use crate::interrupt::set_sie_mask; +use crate::structures::{ + ndks_boot_t, p_region_t, region_t, seL4_BootInfo, seL4_BootInfoHeader, seL4_SlotRegion, + v_region_t, +}; +use sel4_common::arch::{get_time, set_timer}; -use sel4_vspace::*; -use sel4_task::*; pub use root_server::rootserver; -pub use utils::{write_slot, provide_cap}; +use sel4_task::*; +use sel4_vspace::*; #[cfg(feature = "ENABLE_SMP")] -use crate::{ - deps::{clh_lock_init, clh_lock_acquire} -}; +pub use utils::{provide_cap, write_slot}; + +#[cfg(feature = "ENABLE_SMP")] +use crate::deps::{clh_lock_acquire, clh_lock_init}; #[cfg(feature = "ENABLE_SMP")] use core::arch::asm; @@ -67,6 +66,7 @@ pub static mut ndks_boot: ndks_boot_t = ndks_boot_t { slot_pos_cur: seL4_NumInitialCaps, }; + #[cfg(target_arch = "riscv64")] fn init_cpu() { activate_kernel_vspace(); @@ -179,7 +179,11 @@ fn calculate_extra_bi_size_bits(size: usize) -> usize { return msb; } -fn init_dtb(dtb_size: usize, dtb_phys_addr: usize, extra_bi_size:&mut usize) -> Option { +fn init_dtb( + dtb_size: usize, + dtb_phys_addr: usize, + extra_bi_size: &mut usize, +) -> Option { let mut dtb_p_reg = p_region_t { start: 0, end: 0 }; if dtb_size > 0 { let dtb_phys_end = dtb_phys_addr + dtb_size; @@ -208,7 +212,6 @@ fn init_dtb(dtb_size: usize, dtb_phys_addr: usize, extra_bi_size:&mut usize) -> Some(dtb_p_reg) } - fn init_bootinfo(dtb_size: usize, dtb_phys_addr: usize, extra_bi_size: usize) { let mut extra_bi_offset = 0; let mut header: seL4_BootInfoHeader = seL4_BootInfoHeader { id: 0, len: 0 }; @@ -239,7 +242,7 @@ fn init_bootinfo(dtb_size: usize, dtb_phys_addr: usize, extra_bi_size: usize) { } } -fn bi_finalise(dtb_size: usize, dtb_phys_addr: usize, extra_bi_size: usize,) { +fn bi_finalise(dtb_size: usize, dtb_phys_addr: usize, extra_bi_size: usize) { unsafe { (*ndks_boot.bi_frame).empty = seL4_SlotRegion { start: ndks_boot.slot_pos_cur, @@ -256,21 +259,22 @@ fn init_core_state(scheduler_action: *mut tcb_t) { tcbDebugAppend(scheduler_action); } let idle_thread = { - #[cfg(not(feature = "ENABLE_SMP"))] { + #[cfg(not(feature = "ENABLE_SMP"))] + { ksIdleThread as *mut tcb_t } - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { ksSMP[cpu_id()].ksIdleThread as *mut tcb_t } }; - + tcbDebugAppend(idle_thread); set_current_scheduler_action(scheduler_action as usize); set_current_thread(get_idle_thread()); } } - pub fn try_init_kernel( ui_p_reg_start: usize, ui_p_reg_end: usize, @@ -278,7 +282,7 @@ pub fn try_init_kernel( v_entry: usize, dtb_phys_addr: usize, dtb_size: usize, - ki_boot_end: usize + ki_boot_end: usize, ) -> bool { sel4_common::logging::init(); debug!("hello logging"); @@ -302,7 +306,7 @@ pub fn try_init_kernel( let bi_frame_vptr = ipcbuf_vptr + BIT!(PAGE_BITS); let extra_bi_frame_vptr = bi_frame_vptr + BIT!(BI_FRAME_SIZE_BITS); rust_map_kernel_window(); - #[cfg(target_arch = "riscv64")] + #[cfg(target_arch = "riscv64")] init_cpu(); #[cfg(target_arch = "aarch64")] if init_cpu() == false { @@ -334,16 +338,22 @@ pub fn try_init_kernel( ); return false; } - if !init_freemem( - ui_reg.clone(), - dtb_p_reg.unwrap().clone(), - ) { + if !init_freemem(ui_reg.clone(), dtb_p_reg.unwrap().clone()) { debug!("ERROR: free memory management initialization failed\n"); return false; } - if let Some((initial_thread, root_cnode_cap)) = root_server_init(it_v_reg, extra_bi_size_bits, ipcbuf_vptr, - bi_frame_vptr, extra_bi_size, extra_bi_frame_vptr, ui_reg, pv_offset, v_entry) { + if let Some((initial_thread, root_cnode_cap)) = root_server_init( + it_v_reg, + extra_bi_size_bits, + ipcbuf_vptr, + bi_frame_vptr, + extra_bi_size, + extra_bi_frame_vptr, + ui_reg, + pv_offset, + v_entry, + ) { create_idle_thread(); init_core_state(initial_thread); if !create_untypeds(&root_cnode_cap, boot_mem_reuse_reg) { @@ -351,13 +361,13 @@ pub fn try_init_kernel( } unsafe { (*ndks_boot.bi_frame).sharedFrames = seL4_SlotRegion { start: 0, end: 0 }; - + bi_finalise(dtb_size, dtb_phys_addr, extra_bi_size); - } // debug!("release_secondary_cores start"); *ksNumCPUs.lock() = 1; - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { unsafe { clh_lock_init(); release_secondary_cores(); @@ -370,7 +380,7 @@ pub fn try_init_kernel( } else { return false; } - + true } @@ -400,4 +410,4 @@ fn release_secondary_cores() { asm!("fence rw, rw"); } while ksNumCPUs.lock().ne(&CONFIG_MAX_NUM_NODES) {} -} \ No newline at end of file +} diff --git a/src/boot/root_server.rs b/src/boot/root_server.rs index f52cdca..e450beb 100644 --- a/src/boot/root_server.rs +++ b/src/boot/root_server.rs @@ -1,17 +1,24 @@ use super::calculate_extra_bi_size_bits; -use super::utils::{arch_get_n_paging, write_slot, provide_cap, create_it_pt_cap, map_it_frame_cap}; +use super::utils::{ + arch_get_n_paging, create_it_pt_cap, map_it_frame_cap, provide_cap, write_slot, +}; use super::{ndks_boot, utils::is_reg_empty}; +use crate::interrupt::{setIRQState, IRQState}; +use crate::structures::{ + create_frames_of_region_ret_t, region_t, rootserver_mem_t, seL4_BootInfo, seL4_SlotRegion, + v_region_t, +}; use crate::{BIT, ROUND_DOWN}; -use sel4_common::sel4_config::{wordBits, seL4_SlotBits, IT_ASID, asidLowBits, seL4_PageBits, seL4_PageTableBits, CONFIG_PT_LEVELS, - PAGE_BITS, CONFIG_MAX_NUM_NODES, TCB_OFFSET, CONFIG_TIME_SLICE, tcbCTable, tcbVTable, tcbBuffer, CONFIG_NUM_DOMAINS, seL4_TCBBits}; +use log::debug; +use sel4_common::registers::{capRegister, NextIP}; +use sel4_common::sel4_config::{ + asidLowBits, seL4_PageBits, seL4_PageTableBits, seL4_SlotBits, seL4_TCBBits, tcbBuffer, + tcbCTable, tcbVTable, wordBits, CONFIG_MAX_NUM_NODES, CONFIG_NUM_DOMAINS, CONFIG_PT_LEVELS, + CONFIG_TIME_SLICE, IT_ASID, PAGE_BITS, TCB_OFFSET, +}; use sel4_common::structures::{exception_t, seL4_IPCBuffer}; use sel4_common::utils::convert_to_mut_type_ref; use sel4_cspace::interface::*; -use log::debug; -use sel4_common::registers::{capRegister, NextIP}; -use crate::interrupt::{setIRQState, IRQState}; -use crate::structures::{region_t, rootserver_mem_t, v_region_t, seL4_SlotRegion, create_frames_of_region_ret_t, - seL4_BootInfo}; use crate::config::*; use crate::utils::clear_memory; @@ -38,16 +45,22 @@ pub static mut rootserver: rootserver_mem_t = rootserver_mem_t { }, }; -pub fn root_server_init(it_v_reg: v_region_t, extra_bi_size_bits: usize, ipcbuf_vptr: usize, bi_frame_vptr: usize, - extra_bi_size: usize, extra_bi_frame_vptr: usize, ui_reg: region_t, pv_offset: isize, v_entry: usize) -> Option<(*mut tcb_t, cap_t)> { - +pub fn root_server_init( + it_v_reg: v_region_t, + extra_bi_size_bits: usize, + ipcbuf_vptr: usize, + bi_frame_vptr: usize, + extra_bi_size: usize, + extra_bi_frame_vptr: usize, + ui_reg: region_t, + pv_offset: isize, + v_entry: usize, +) -> Option<(*mut tcb_t, cap_t)> { unsafe { root_server_mem_init(it_v_reg, extra_bi_size_bits); } - - let root_cnode_cap = unsafe { - create_root_cnode() - }; + + let root_cnode_cap = unsafe { create_root_cnode() }; if root_cnode_cap.get_cap_type() == CapTag::CapNullCap { debug!("ERROR: root c-node creation failed\n"); return None; @@ -58,20 +71,22 @@ pub fn root_server_init(it_v_reg: v_region_t, extra_bi_size_bits: usize, ipcbuf_ unsafe { rust_populate_bi_frame(0, CONFIG_MAX_NUM_NODES, ipcbuf_vptr, extra_bi_size); } - let it_pd_cap = unsafe { - rust_create_it_address_space(&root_cnode_cap, it_v_reg) - }; + let it_pd_cap = unsafe { rust_create_it_address_space(&root_cnode_cap, it_v_reg) }; if it_pd_cap.get_cap_type() == CapTag::CapNullCap { debug!("ERROR: address space creation for initial thread failed"); return None; } - if !init_bi_frame_cap(root_cnode_cap, it_pd_cap, bi_frame_vptr, extra_bi_size, extra_bi_frame_vptr) { + if !init_bi_frame_cap( + root_cnode_cap, + it_pd_cap, + bi_frame_vptr, + extra_bi_size, + extra_bi_frame_vptr, + ) { return None; } - let ipcbuf_cap = unsafe { - create_ipcbuf_frame_cap(&root_cnode_cap, &it_pd_cap, ipcbuf_vptr) - }; + let ipcbuf_cap = unsafe { create_ipcbuf_frame_cap(&root_cnode_cap, &it_pd_cap, ipcbuf_vptr) }; if ipcbuf_cap.get_cap_type() == CapTag::CapNullCap { debug!("ERROR: could not create IPC buffer for initial thread"); return None; @@ -88,7 +103,7 @@ pub fn root_server_init(it_v_reg: v_region_t, extra_bi_size_bits: usize, ipcbuf_ if !asid_init(root_cnode_cap, it_pd_cap) { return None; } - + let initial = unsafe { create_initial_thread( &root_cnode_cap, @@ -107,8 +122,6 @@ pub fn root_server_init(it_v_reg: v_region_t, extra_bi_size_bits: usize, ipcbuf_ Some((initial, root_cnode_cap)) } - - unsafe fn create_initial_thread( root_cnode_cap: &cap_t, it_pd_cap: &cap_t, @@ -122,18 +135,30 @@ unsafe fn create_initial_thread( tcb.tcbArch = arch_tcb_t::default(); let cnode = convert_to_mut_type_ref::(root_cnode_cap.get_cap_ptr()); - let ipc_buf_slot =cnode.get_offset_slot(seL4_CapInitThreadIPCBuffer); + let ipc_buf_slot = cnode.get_offset_slot(seL4_CapInitThreadIPCBuffer); let dc_ret = ipc_buf_slot.derive_cap(&ipcbuf_cap.clone()); if dc_ret.status != exception_t::EXCEPTION_NONE { debug!("Failed to derive copy of IPC Buffer\n"); return 0 as *mut tcb_t; } - cte_insert(root_cnode_cap, cnode.get_offset_slot(seL4_CapInitThreadCNode), tcb.get_cspace_mut_ref(tcbCTable)); + cte_insert( + root_cnode_cap, + cnode.get_offset_slot(seL4_CapInitThreadCNode), + tcb.get_cspace_mut_ref(tcbCTable), + ); - cte_insert(it_pd_cap, cnode.get_offset_slot(seL4_CapInitThreadVspace), tcb.get_cspace_mut_ref(tcbVTable)); + cte_insert( + it_pd_cap, + cnode.get_offset_slot(seL4_CapInitThreadVspace), + tcb.get_cspace_mut_ref(tcbVTable), + ); - cte_insert(&dc_ret.cap, cnode.get_offset_slot(seL4_CapInitThreadIPCBuffer), tcb.get_cspace_mut_ref(tcbBuffer)); + cte_insert( + &dc_ret.cap, + cnode.get_offset_slot(seL4_CapInitThreadIPCBuffer), + tcb.get_cspace_mut_ref(tcbBuffer), + ); tcb.tcbIPCBuffer = ipcbuf_vptr; tcb.set_register(capRegister, bi_frame_vptr); @@ -152,7 +177,10 @@ unsafe fn create_initial_thread( } let cap = cap_t::new_thread_cap(tcb.get_ptr()); - write_slot(cnode.get_offset_slot(seL4_CapInitThreadTCB) as *mut cte_t, cap); + write_slot( + cnode.get_offset_slot(seL4_CapInitThreadTCB) as *mut cte_t, + cap, + ); // forget(*tcb); tcb as *mut tcb_t } @@ -163,7 +191,7 @@ fn asid_init(root_cnode_cap: cap_t, it_pd_cap: cap_t) -> bool { debug!("ERROR: could not create ASID pool for initial thread"); return false; } - + unsafe { let ap = it_ap_cap.get_cap_ptr(); let ptr = (ap + 8 * IT_ASID) as *mut usize; @@ -173,7 +201,6 @@ fn asid_init(root_cnode_cap: cap_t, it_pd_cap: cap_t) -> bool { true } - fn create_it_asid_pool(root_cnode_cap: &cap_t) -> cap_t { let ap_cap = unsafe { cap_t::new_asid_pool_cap(IT_ASID >> asidLowBits, rootserver.asid_pool) }; unsafe { @@ -184,7 +211,12 @@ fn create_it_asid_pool(root_cnode_cap: &cap_t) -> cap_t { ap_cap } -fn create_frame_ui_frames(root_cnode_cap: cap_t, it_pd_cap: cap_t, ui_reg: region_t, pv_offset: isize) -> bool { +fn create_frame_ui_frames( + root_cnode_cap: cap_t, + it_pd_cap: cap_t, + ui_reg: region_t, + pv_offset: isize, +) -> bool { let create_frames_ret = rust_create_frames_of_region( &root_cnode_cap, &it_pd_cap, @@ -214,8 +246,8 @@ unsafe fn root_server_mem_init(it_v_reg: v_region_t, extra_bi_size_bits: usize) /* Invariant: both i and (i + 1) are valid indices in ndks_boot.freemem. */ assert!(i < (ndks_boot.freemem.len() - 1)); /* Invariant; the region at index i is the current candidate. - * Invariant: regions 0 up to (i - 1), if any, are additional candidates. - * Invariant: region (i + 1) is empty. */ + * Invariant: regions 0 up to (i - 1), if any, are additional candidates. + * Invariant: region (i + 1) is empty. */ assert!(is_reg_empty(&ndks_boot.freemem[i + 1])); let empty_index = i + 1; @@ -223,7 +255,7 @@ unsafe fn root_server_mem_init(it_v_reg: v_region_t, extra_bi_size_bits: usize) let start = ROUND_DOWN!(unaligned_start, max); /* if unaligned_start didn't underflow, and start fits in the region, - * then we've found a region that fits the root server objects. */ + * then we've found a region that fits the root server objects. */ if unaligned_start <= ndks_boot.freemem[i].end && start >= ndks_boot.freemem[i].start { create_rootserver_objects(start, it_v_reg, extra_bi_size_bits); ndks_boot.freemem[empty_index] = region_t { @@ -234,7 +266,7 @@ unsafe fn root_server_mem_init(it_v_reg: v_region_t, extra_bi_size_bits: usize) return; } /* Region i isn't big enough, so shuffle it up to slot (i + 1), - * which we know is unused. */ + * which we know is unused. */ ndks_boot.freemem[empty_index] = ndks_boot.freemem[i]; ndks_boot.freemem[i] = region_t { start: 0, end: 0 }; i -= 1; @@ -354,7 +386,8 @@ fn init_irqs(root_cnode_cap: &cap_t) { } } setIRQState(IRQState::IRQTimer, KERNEL_TIMER_IRQ); - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { setIRQState(IRQState::IRQIPI, INTERRUPT_IPI_0); setIRQState(IRQState::IRQIPI, INTERRUPT_IPI_1); } @@ -392,8 +425,13 @@ unsafe fn rust_create_it_address_space(root_cnode_cap: &cap_t, it_v_reg: v_regio lvl1pt_cap } - -fn init_bi_frame_cap(root_cnode_cap: cap_t, it_pd_cap: cap_t, bi_frame_vptr: usize, extra_bi_size: usize, extra_bi_frame_vptr: usize) -> bool { +fn init_bi_frame_cap( + root_cnode_cap: cap_t, + it_pd_cap: cap_t, + bi_frame_vptr: usize, + extra_bi_size: usize, + extra_bi_frame_vptr: usize, +) -> bool { unsafe { create_bi_frame_cap(&root_cnode_cap, &it_pd_cap, bi_frame_vptr); } @@ -421,10 +459,8 @@ fn init_bi_frame_cap(root_cnode_cap: cap_t, it_pd_cap: cap_t, bi_frame_vptr: usi } } true - } - fn rust_create_frames_of_region( root_cnode_cap: &cap_t, pd_cap: &cap_t, @@ -470,14 +506,8 @@ fn rust_create_frames_of_region( } unsafe fn create_bi_frame_cap(root_cnode_cap: &cap_t, pd_cap: &cap_t, vptr: usize) { - let cap = rust_create_mapped_it_frame_cap( - pd_cap, - rootserver.boot_info, - vptr, - IT_ASID, - false, - false, - ); + let cap = + rust_create_mapped_it_frame_cap(pd_cap, rootserver.boot_info, vptr, IT_ASID, false, false); let ptr = root_cnode_cap.get_cap_ptr() as *mut cte_t; write_slot(ptr.add(seL4_CapBootInfoFrame), cap); } @@ -501,12 +531,10 @@ pub fn rust_create_mapped_it_frame_cap( cap } - fn rust_create_unmapped_it_frame_cap(pptr: pptr_t, _use_large: bool) -> cap_t { cap_t::new_frame_cap(0, pptr, 0, 0, 0, 0) } - unsafe fn rust_populate_bi_frame( node_id: usize, num_nodes: usize, @@ -535,15 +563,9 @@ unsafe fn rust_populate_bi_frame( unsafe fn create_ipcbuf_frame_cap(root_cnode_cap: &cap_t, pd_cap: &cap_t, vptr: usize) -> cap_t { clear_memory(rootserver.ipc_buf as *mut u8, PAGE_BITS); - let cap = rust_create_mapped_it_frame_cap( - pd_cap, - rootserver.ipc_buf, - vptr, - IT_ASID, - false, - false, - ); + let cap = + rust_create_mapped_it_frame_cap(pd_cap, rootserver.ipc_buf, vptr, IT_ASID, false, false); let ptr = root_cnode_cap.get_cap_ptr() as *mut cte_t; write_slot(ptr.add(seL4_CapInitThreadIPCBuffer), cap.clone()); return cap; -} \ No newline at end of file +} diff --git a/src/boot/untyped.rs b/src/boot/untyped.rs index 890b03e..455788f 100644 --- a/src/boot/untyped.rs +++ b/src/boot/untyped.rs @@ -1,14 +1,14 @@ use super::{ndks_boot, utils::*}; use crate::{ config::*, - structures::{region_t, p_region_t, seL4_SlotRegion, seL4_SlotPos, seL4_UntypedDesc} + structures::{p_region_t, region_t, seL4_SlotPos, seL4_SlotRegion, seL4_UntypedDesc}, }; +use crate::{BIT, IS_ALIGNED, MASK}; +use log::debug; use sel4_common::sel4_config::{seL4_MaxUntypedBits, seL4_MinUntypedBits}; -use crate::{MASK, BIT, IS_ALIGNED}; use sel4_common::utils::MAX_FREE_INDEX; use sel4_cspace::interface::*; -use log::debug; use sel4_vspace::*; pub fn create_untypeds(root_cnode_cap: &cap_t, boot_mem_reuse_reg: region_t) -> bool { @@ -78,7 +78,6 @@ pub fn create_untypeds(root_cnode_cap: &cap_t, boot_mem_reuse_reg: region_t) -> } } - fn create_untypeds_for_region( root_cnode_cap: &cap_t, device_memory: bool, @@ -172,4 +171,4 @@ fn provide_untyped_cap( } } ret -} \ No newline at end of file +} diff --git a/src/boot/utils.rs b/src/boot/utils.rs index 2e3da9f..235eebf 100644 --- a/src/boot/utils.rs +++ b/src/boot/utils.rs @@ -1,13 +1,11 @@ - - -use sel4_common::{utils::convert_to_mut_type_ref, sel4_config::*}; +use super::ndks_boot; use crate::config::CONFIG_ROOT_CNODE_SIZE_BITS; -use crate::{ROUND_DOWN, ROUND_UP, BIT}; -use sel4_cspace::interface::*; -use log::debug; use crate::structures::{p_region_t, region_t, v_region_t}; +use crate::{BIT, ROUND_DOWN, ROUND_UP}; +use log::debug; +use sel4_common::{sel4_config::*, utils::convert_to_mut_type_ref}; +use sel4_cspace::interface::*; use sel4_vspace::*; -use super::ndks_boot; #[inline] pub fn is_reg_empty(reg: ®ion_t) -> bool { @@ -82,7 +80,6 @@ pub fn provide_cap(root_cnode_cap: &cap_t, cap: cap_t) -> bool { } } - #[no_mangle] pub extern "C" fn map_it_pt_cap(_vspace_cap: &cap_t, _pt_cap: &cap_t) { let vptr = _pt_cap.get_pt_mapped_address(); @@ -91,8 +88,16 @@ pub extern "C" fn map_it_pt_cap(_vspace_cap: &cap_t, _pt_cap: &cap_t) { let pt_ret = lvl1pt.lookup_pt_slot(vptr); let targetSlot = convert_to_mut_type_ref::(pt_ret.ptSlot as usize); *targetSlot = pte_t::new( - pptr_to_paddr(pt) >> seL4_PageBits, 0, 0, 0, 0, 0, 0, - 0, 0, 1 + pptr_to_paddr(pt) >> seL4_PageBits, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, ); sfence(); } @@ -112,13 +117,20 @@ pub fn map_it_frame_cap(_vspace_cap: &cap_t, _frame_cap: &cap_t) { let targetSlot = convert_to_mut_type_ref::(pt_ret.ptSlot as usize); *targetSlot = pte_t::new( - pptr_to_paddr(frame_pptr) >> seL4_PageBits, 0, 1, 1, 0, 1, - 1, 1, 1, 1 + pptr_to_paddr(frame_pptr) >> seL4_PageBits, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, ); sfence(); } - pub fn rust_create_unmapped_it_frame_cap(pptr: pptr_t, _use_large: bool) -> cap_t { cap_t::new_frame_cap(0, pptr, 0, 0, 0, 0) } diff --git a/src/compatibility.rs b/src/compatibility.rs index 4dd8b89..41317b4 100644 --- a/src/compatibility.rs +++ b/src/compatibility.rs @@ -14,12 +14,14 @@ pub fn deriveCap(_slot: *mut cte_t, _cap: &cap_t) { panic!("should not be invoked!") } - #[no_mangle] pub fn setThreadState(tptr: *mut tcb_t, ts: usize) { // panic!("should not be invoked!") unsafe { - set_thread_state(&mut *tptr, core::mem::transmute::(ts as u8)) + set_thread_state( + &mut *tptr, + core::mem::transmute::(ts as u8), + ) } } @@ -28,33 +30,27 @@ pub fn setupReplyMaster(_thread: *mut tcb_t) { panic!("should not be invoked") } - #[no_mangle] pub fn lookupIPCBuffer(isReceiver: bool, thread: *mut tcb_t) -> usize { unsafe { match (*thread).lookup_ipc_buffer(isReceiver) { - Some(ipc_buffer) => { - return ipc_buffer as *const seL4_IPCBuffer as usize - } - _ => 0 + Some(ipc_buffer) => return ipc_buffer as *const seL4_IPCBuffer as usize, + _ => 0, } } } - #[no_mangle] pub fn pte_next(_phys_addr: usize, _is_leaf: bool) -> pte_t { panic!("should not be invoked!") } - #[no_mangle] pub fn isPTEPageTable(_pte: *mut pte_t) -> bool { panic!("should not be invoked!") } - #[no_mangle] -pub extern "C" fn lookupPTSlot(_lvl1pt: *mut pte_t, _vptr: vptr_t){ +pub extern "C" fn lookupPTSlot(_lvl1pt: *mut pte_t, _vptr: vptr_t) { panic!("should not be invoked!") -} \ No newline at end of file +} diff --git a/src/config.rs b/src/config.rs index 336bfa5..c18caee 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,7 +22,6 @@ pub const RISCVPageBits: usize = 12; pub const RISCVMegaPageBits: usize = 21; pub const RISCVGigaPageBits: usize = 30; pub const KERNEL_STACK_ALIGNMENT: usize = 4096; -pub const CPUID_MASK: usize = KERNEL_STACK_ALIGNMENT - 1; pub const tcbCNodeEntries: usize = 5; //FIXME:this constant is generated , maybe need to transfer from C code @@ -69,7 +68,7 @@ pub const SIE_SEIE: usize = 9; pub const SIE_MEIE: usize = 11; pub const badgeRegister: usize = 9; -pub const seL4_MsgLengthBits:usize =7; +pub const seL4_MsgLengthBits: usize = 7; pub const RISCVInstructionMisaligned: usize = 0; pub const RISCVInstructionAccessFault: usize = 1; @@ -87,7 +86,6 @@ pub const RISCVSupervisorTimer: usize = 9223372036854775813; pub const n_frameRegisters: usize = 16; pub const n_gpRegisters: usize = 16; - pub const frameRegisters: [usize; n_frameRegisters] = [33, 0, 1, 2, 7, 8, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]; pub const gpRegisters: [usize; n_gpRegisters] = @@ -97,7 +95,6 @@ pub const thread_control_update_ipc_buffer: usize = 0x2; pub const thread_control_update_space: usize = 0x4; pub const thread_control_update_mcp: usize = 0x8; - pub const seL4_WordBits: usize = 64; pub const seL4_UserTop: usize = 0x00007fffffffffff; @@ -118,7 +115,6 @@ pub const KERNEL_TIMER_IRQ: usize = 3; #[cfg(not(feature = "ENABLE_SMP"))] pub const KERNEL_TIMER_IRQ: usize = 1; - pub const maxIRQ: usize = KERNEL_TIMER_IRQ; pub const irqInvalid: usize = 0; @@ -133,11 +129,7 @@ pub const TIMER_CLOCK_HZ: usize = 10000000; pub const MS_IN_S: usize = 1000; pub const RESET_CYCLES: usize = (TIMER_CLOCK_HZ / MS_IN_S) * 2; - - pub const seL4_MinPrio: usize = 0; pub const CONFIG_MAX_NUM_WORK_UNITS_PER_PREEMPTION: usize = 100; pub const CONFIG_RETYPE_FAN_OUT_LIMIT: usize = 256; - - diff --git a/src/console.rs b/src/console.rs deleted file mode 100644 index de3148b..0000000 --- a/src/console.rs +++ /dev/null @@ -1,35 +0,0 @@ -//! SBI console driver, for text output - -use crate::sbi::console_putchar; -use core::fmt::{self, Write}; - -struct Stdout; - -impl Write for Stdout { - fn write_str(&mut self, s: &str) -> fmt::Result { - for c in s.chars() { - console_putchar(c as usize); - } - Ok(()) - } -} - -pub fn print(args: fmt::Arguments) { - Stdout.write_fmt(args).unwrap(); -} - -/// print string macro -#[macro_export] -macro_rules! print { - ($fmt: literal $(, $($arg: tt)+)?) => { - $crate::console::print(format_args!($fmt $(, $($arg)+)?)); - } -} - -/// println string macro -#[macro_export] -macro_rules! println { - ($fmt: literal $(, $($arg: tt)+)?) => { - $crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)); - } -} \ No newline at end of file diff --git a/src/deps.rs b/src/deps.rs index 6cdb594..55f96b8 100644 --- a/src/deps.rs +++ b/src/deps.rs @@ -1,9 +1,6 @@ use sel4_task::tcb_t; extern "C" { - // in arm sel4,init timer have different realization on different platform - // so I think no need to realization them all - pub fn initTimer(); pub fn init_plat(); pub fn tcbDebugAppend(action: *mut tcb_t); pub fn tcbDebugRemove(tcb: *mut tcb_t); @@ -23,5 +20,5 @@ extern "C" { pub fn clh_is_self_in_queue() -> bool; pub fn clh_lock_release(cpu: usize); pub fn clh_lock_acquire(cpu_idx: usize, irq_path: bool); - -} \ No newline at end of file + +} diff --git a/src/interfaces_impl/cspace.rs b/src/interfaces_impl/cspace.rs index 77be7bd..5b24c15 100644 --- a/src/interfaces_impl/cspace.rs +++ b/src/interfaces_impl/cspace.rs @@ -1,16 +1,18 @@ -use sel4_common::sel4_config::{tcbCNodeEntries, tcbCTable, tcbVTable}; -use sel4_common::structures::exception_t; -use sel4_common::utils::convert_to_mut_type_ref; use crate::config::CONFIG_MAX_NUM_WORK_UNITS_PER_PREEMPTION; -use sel4_cspace::compatibility::{Zombie_new, ZombieType_ZombieTCB}; -use sel4_cspace::interface::{cap_t, CapTag, finaliseCap_ret}; use crate::deps::tcbDebugRemove; -use crate::interrupt::{deletingIRQHandler, IRQState, isIRQPending, setIRQState}; +use crate::interrupt::{deletingIRQHandler, isIRQPending, setIRQState, IRQState}; use crate::kernel::boot::current_lookup_fault; use crate::syscall::safe_unbind_notification; +use sel4_common::sel4_config::{tcbCNodeEntries, tcbCTable, tcbVTable}; +use sel4_common::structures::exception_t; +use sel4_common::utils::convert_to_mut_type_ref; +use sel4_cspace::compatibility::{ZombieType_ZombieTCB, Zombie_new}; +use sel4_cspace::interface::{cap_t, finaliseCap_ret, CapTag}; use sel4_ipc::{endpoint_t, notification_t, Transfer}; use sel4_task::{get_currenct_thread, ksWorkUnitsCompleted, tcb_t}; -use sel4_vspace::{asid_pool_t, asid_t, delete_asid, delete_asid_pool, find_vspace_for_asid, pte_t, unmapPage}; +use sel4_vspace::{ + asid_pool_t, asid_t, delete_asid, delete_asid_pool, find_vspace_for_asid, pte_t, unmapPage, +}; #[no_mangle] pub fn Arch_finaliseCap(cap: &cap_t, final_: bool) -> finaliseCap_ret { @@ -18,13 +20,13 @@ pub fn Arch_finaliseCap(cap: &cap_t, final_: bool) -> finaliseCap_ret { match cap.get_cap_type() { CapTag::CapFrameCap => { if cap.get_frame_mapped_asid() != 0 { - match unmapPage(cap.get_frame_size(), cap.get_frame_mapped_asid(), - cap.get_frame_mapped_address(), cap.get_frame_base_ptr()) { - Err(lookup_fault) => { - unsafe { - current_lookup_fault = lookup_fault - } - } + match unmapPage( + cap.get_frame_size(), + cap.get_frame_mapped_asid(), + cap.get_frame_mapped_address(), + cap.get_frame_base_ptr(), + ) { + Err(lookup_fault) => unsafe { current_lookup_fault = lookup_fault }, _ => {} } } @@ -35,10 +37,13 @@ pub fn Arch_finaliseCap(cap: &cap_t, final_: bool) -> finaliseCap_ret { let asid = cap.get_pt_mapped_asid(); let find_ret = find_vspace_for_asid(asid); let pte = cap.get_pt_base_ptr(); - if find_ret.status == exception_t::EXCEPTION_NONE && find_ret.vspace_root.unwrap() as usize == pte { + if find_ret.status == exception_t::EXCEPTION_NONE + && find_ret.vspace_root.unwrap() as usize == pte + { deleteASID(asid, pte as *mut pte_t); } else { - convert_to_mut_type_ref::(pte).unmap_page_table(asid, cap.get_pt_mapped_address()); + convert_to_mut_type_ref::(pte) + .unmap_page_table(asid, cap.get_pt_mapped_address()); } if let Some(lookup_fault) = find_ret.lookup_fault { unsafe { @@ -50,10 +55,7 @@ pub fn Arch_finaliseCap(cap: &cap_t, final_: bool) -> finaliseCap_ret { CapTag::CapASIDPoolCap => { if final_ { - deleteASIDPool( - cap.get_asid_base(), - cap.get_asid_pool() as *mut asid_pool_t, - ); + deleteASIDPool(cap.get_asid_base(), cap.get_asid_pool() as *mut asid_pool_t); } } _ => {} @@ -82,7 +84,7 @@ pub fn finaliseCap(cap: &cap_t, _final: bool, _exposed: bool) -> finaliseCap_ret } CapTag::CapNotificationCap => { if _final { - let ntfn = convert_to_mut_type_ref::(cap.get_nf_ptr()); + let ntfn = convert_to_mut_type_ref::(cap.get_nf_ptr()); ntfn.safe_unbind_tcb(); ntfn.cacncel_all_signal(); } @@ -171,7 +173,6 @@ pub fn post_cap_deletion(cap: &cap_t) { } } - #[no_mangle] pub fn preemptionPoint() -> exception_t { unsafe { @@ -190,7 +191,11 @@ pub fn preemptionPoint() -> exception_t { #[no_mangle] fn deleteASID(asid: asid_t, vspace: *mut pte_t) { unsafe { - if let Err(lookup_fault) = delete_asid(asid, vspace, &get_currenct_thread().get_cspace(tcbVTable).cap) { + if let Err(lookup_fault) = delete_asid( + asid, + vspace, + &get_currenct_thread().get_cspace(tcbVTable).cap, + ) { current_lookup_fault = lookup_fault; } } @@ -199,8 +204,12 @@ fn deleteASID(asid: asid_t, vspace: *mut pte_t) { #[no_mangle] fn deleteASIDPool(asid_base: asid_t, pool: *mut asid_pool_t) { unsafe { - if let Err(lookup_fault) = delete_asid_pool(asid_base, pool, &get_currenct_thread().get_cspace(tcbVTable).cap) { + if let Err(lookup_fault) = delete_asid_pool( + asid_base, + pool, + &get_currenct_thread().get_cspace(tcbVTable).cap, + ) { current_lookup_fault = lookup_fault; } } -} \ No newline at end of file +} diff --git a/src/interfaces_impl/mod.rs b/src/interfaces_impl/mod.rs index 53eebfa..6e5b1ee 100644 --- a/src/interfaces_impl/mod.rs +++ b/src/interfaces_impl/mod.rs @@ -1,2 +1 @@ mod cspace; -mod task; \ No newline at end of file diff --git a/src/interfaces_impl/task.rs b/src/interfaces_impl/task.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/interrupt/handler.rs b/src/interrupt/handler.rs index f3ea1b1..2ec9dd6 100644 --- a/src/interrupt/handler.rs +++ b/src/interrupt/handler.rs @@ -1,13 +1,12 @@ +use crate::arch::resetTimer; +use crate::config::{irqInvalid, maxIRQ}; +use crate::interrupt::*; use core::intrinsics::unlikely; +use log::debug; use sel4_common::structures::exception_t; use sel4_cspace::interface::CapTag; -use log::debug; -use sel4_task::{activateThread, schedule, timerTick}; use sel4_ipc::notification_t; -use crate::config::{irqInvalid, maxIRQ}; -use crate::interrupt::*; -use crate::riscv::resetTimer; - +use sel4_task::{activateThread, schedule, timerTick}; #[no_mangle] pub fn handleInterruptEntry() -> exception_t { @@ -47,13 +46,13 @@ pub fn handleInterrupt(irq: usize) { let handler_slot = get_irq_handler_slot(irq); let handler_cap = &handler_slot.cap; if handler_cap.get_cap_type() == CapTag::CapNotificationCap - && handler_cap.get_nf_can_send() != 0 { + && handler_cap.get_nf_can_send() != 0 + { let nf = convert_to_mut_type_ref::(handler_cap.get_nf_ptr()); nf.send_signal(handler_cap.get_nf_badge()); } } IRQState::IRQTimer => { - timerTick(); resetTimer(); } diff --git a/src/interrupt/mod.rs b/src/interrupt/mod.rs index 54a0baa..df8c8a5 100644 --- a/src/interrupt/mod.rs +++ b/src/interrupt/mod.rs @@ -1,14 +1,13 @@ pub mod handler; use core::arch::asm; - +use crate::BIT; use sel4_common::sel4_config::CONFIG_MAX_NUM_NODES; use sel4_common::utils::{convert_to_mut_type_ref, cpu_id}; -use crate::BIT; use sel4_cspace::interface::cte_t; use sel4_vspace::pptr_t; -use crate::{config::*, riscv::read_sip}; +use crate::{arch::read_sip, config::*}; #[cfg(feature = "ENABLE_SMP")] use crate::deps::{ipi_clear_irq, ipi_get_irq}; @@ -43,16 +42,12 @@ pub enum IRQState { #[inline] pub fn get_irq_state(irq: usize) -> IRQState { - unsafe { - core::mem::transmute::(intStateIRQTable[irq] as u8) - } + unsafe { core::mem::transmute::(intStateIRQTable[irq] as u8) } } #[inline] pub fn get_irq_handler_slot(irq: usize) -> &'static mut cte_t { - unsafe { - convert_to_mut_type_ref::(intStateIRQNode).get_offset_slot(irq) - } + unsafe { convert_to_mut_type_ref::(intStateIRQNode).get_offset_slot(irq) } } pub fn deletingIRQHandler(irq: usize) { @@ -88,6 +83,9 @@ pub fn deletedIRQHandler(irq: usize) { } #[inline] pub fn set_sie_mask(mask_high: usize) { + #[cfg(target_arch = "aarch64")] + todo!(); + #[cfg(target_arch = "riscv64")] unsafe { let _temp: usize; asm!("csrrs {0},sie,{1}",out(reg)_temp,in(reg)mask_high); @@ -95,6 +93,9 @@ pub fn set_sie_mask(mask_high: usize) { } #[inline] pub fn clear_sie_mask(mask_low: usize) { + #[cfg(target_arch = "aarch64")] + todo!(); + #[cfg(target_arch = "riscv64")] unsafe { let _temp: usize; asm!("csrrc {0},sie,{1}",out(reg)_temp,in(reg)mask_low); @@ -129,9 +130,12 @@ pub fn ackInterrupt(irq: usize) { if irq == KERNEL_TIMER_IRQ { return; } - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { if irq == INTERRUPT_IPI_0 || irq == INTERRUPT_IPI_1 { - unsafe { ipi_clear_irq(irq); } + unsafe { + ipi_clear_irq(irq); + } } } } @@ -155,7 +159,8 @@ pub fn getActiveIRQ() -> usize { } let sip = read_sip(); - #[cfg(feature = "ENABLE_SMP")] { + #[cfg(feature = "ENABLE_SMP")] + { use sel4_common::sbi::clear_ipi; if (sip & BIT!(SIP_SEIP)) != 0 { irq = 0; @@ -165,8 +170,7 @@ pub fn getActiveIRQ() -> usize { // debug!("irq: {}", irq); } else if (sip & BIT!(SIP_STIP)) != 0 { irq = KERNEL_TIMER_IRQ; - } - else { + } else { irq = irqInvalid; } } @@ -175,8 +179,7 @@ pub fn getActiveIRQ() -> usize { irq = 0; } else if (sip & BIT!(SIP_STIP)) != 0 { irq = KERNEL_TIMER_IRQ; - } - else { + } else { irq = irqInvalid; } unsafe { @@ -185,8 +188,6 @@ pub fn getActiveIRQ() -> usize { return irq; } - - pub fn IS_IRQ_VALID(x: usize) -> bool { (x <= maxIRQ) && (x != irqInvalid) -} \ No newline at end of file +} diff --git a/src/kernel/boot.rs b/src/kernel/boot.rs index 3eda354..641cb70 100644 --- a/src/kernel/boot.rs +++ b/src/kernel/boot.rs @@ -1,20 +1,21 @@ extern crate core; -use sel4_common::{sel4_config::seL4_MsgMaxExtraCaps, utils::convert_to_option_mut_type_ref, fault::{seL4_Fault_t, lookup_fault_t}}; -use sel4_cspace::interface::cte_t; - -use crate::structures::{ - extra_caps_t, syscall_error_t, +use sel4_common::{ + fault::{lookup_fault_t, seL4_Fault_t}, + sel4_config::seL4_MsgMaxExtraCaps, + utils::convert_to_option_mut_type_ref, }; +use sel4_cspace::interface::cte_t; +use crate::structures::{extra_caps_t, syscall_error_t}; #[no_mangle] #[link_section = ".boot.bss"] -pub static mut current_lookup_fault: lookup_fault_t = lookup_fault_t { words:[0; 2] }; +pub static mut current_lookup_fault: lookup_fault_t = lookup_fault_t { words: [0; 2] }; #[no_mangle] #[link_section = ".boot.bss"] -pub static mut current_fault: seL4_Fault_t = seL4_Fault_t { words:[0; 2] }; +pub static mut current_fault: seL4_Fault_t = seL4_Fault_t { words: [0; 2] }; #[no_mangle] #[link_section = ".boot.bss"] @@ -28,8 +29,6 @@ pub static mut current_syscall_error: syscall_error_t = syscall_error_t { _type: 0, }; - - #[no_mangle] #[link_section = ".boot.bss"] pub static mut current_extra_caps: extra_caps_t = extra_caps_t { @@ -39,7 +38,5 @@ pub static mut current_extra_caps: extra_caps_t = extra_caps_t { #[inline] pub fn get_extra_cap_by_index(index: usize) -> Option<&'static mut cte_t> { assert!(index < seL4_MsgMaxExtraCaps); - unsafe { - convert_to_option_mut_type_ref::(current_extra_caps.excaprefs[index] as usize) - } + unsafe { convert_to_option_mut_type_ref::(current_extra_caps.excaprefs[index] as usize) } } diff --git a/src/kernel/fastpath.rs b/src/kernel/fastpath.rs index c87308a..f5c3d08 100644 --- a/src/kernel/fastpath.rs +++ b/src/kernel/fastpath.rs @@ -1,13 +1,22 @@ -use crate::{config::seL4_MsgLengthBits, syscall::{slowpath, SysCall, SysReplyRecv}, deps::fastpath_restore}; -use sel4_cspace::compatibility::*; -use sel4_task::*; -use sel4_ipc::*; -use sel4_vspace::*; +use crate::MASK; +use crate::{ + config::seL4_MsgLengthBits, + deps::fastpath_restore, + syscall::{slowpath, SysCall, SysReplyRecv}, +}; use core::intrinsics::{likely, unlikely}; -use sel4_common::{sel4_config::*, message_info::*, fault::*, utils::{convert_to_mut_type_ref, convert_to_option_mut_type_ref}}; use sel4_common::registers::msgRegister; +use sel4_common::{ + fault::*, + message_info::*, + sel4_config::*, + utils::{convert_to_mut_type_ref, convert_to_option_mut_type_ref}, +}; +use sel4_cspace::compatibility::*; use sel4_cspace::interface::*; -use crate::MASK; +use sel4_ipc::*; +use sel4_task::*; +use sel4_vspace::*; #[inline] #[no_mangle] @@ -47,7 +56,6 @@ pub fn lookup_fp(_cap: &cap_t, cptr: usize) -> cap_t { return cap; } - #[inline] #[no_mangle] pub fn thread_state_ptr_mset_blockingObject_tsType( @@ -58,7 +66,6 @@ pub fn thread_state_ptr_mset_blockingObject_tsType( (*ptr).words[0] = ep | tsType; } - #[inline] #[no_mangle] pub fn endpoint_ptr_mset_epQueue_tail_state(ptr: *mut endpoint_t, tail: usize, state: usize) { @@ -90,7 +97,6 @@ pub fn mdb_node_ptr_mset_mdbNext_mdbRevocable_mdbFirstBadged( ptr.words[1] = mdbNext | (mdbRevocable << 1) | mdbFirstBadged; } - #[inline] #[no_mangle] pub fn isValidVTableRoot_fp(cap: &cap_t) -> bool { @@ -104,7 +110,6 @@ pub fn fastpath_mi_check(msgInfo: usize) -> bool { (msgInfo & MASK!(seL4_MsgLengthBits + seL4_MsgExtraCapBits)) > 4 } - #[inline] #[no_mangle] pub fn fastpath_copy_mrs(length: usize, src: &mut tcb_t, dest: &mut tcb_t) { @@ -114,8 +119,6 @@ pub fn fastpath_copy_mrs(length: usize, src: &mut tcb_t, dest: &mut tcb_t) { dest.set_register(reg, src.get_register(reg)); } } -#[cfg(target_arch = "riscv64")] -core::arch::global_asm!(include_str!("restore_fp.S")); // #[inline] // #[no_mangle] @@ -130,7 +133,6 @@ core::arch::global_asm!(include_str!("restore_fp.S")); // } // } - #[inline] #[no_mangle] pub fn fastpath_call(cptr: usize, msgInfo: usize) { @@ -144,8 +146,7 @@ pub fn fastpath_call(cptr: usize, msgInfo: usize) { } let ep_cap = lookup_fp(¤t.get_cspace(tcbCTable).cap, cptr); if unlikely( - !(ep_cap.get_cap_type() == CapTag::CapEndpointCap) - || (ep_cap.get_ep_can_send() == 0), + !(ep_cap.get_cap_type() == CapTag::CapEndpointCap) || (ep_cap.get_ep_can_send() == 0), ) { slowpath(SysCall as usize); } @@ -158,22 +159,15 @@ pub fn fastpath_call(cptr: usize, msgInfo: usize) { let dest = convert_to_mut_type_ref::(ep.get_queue_head()); let new_vtable = dest.get_cspace(tcbVTable).cap; - if unlikely(!isValidVTableRoot_fp(&new_vtable)) { slowpath(SysCall as usize); } let dom = 0; - if unlikely( - dest.tcbPriority < current.tcbPriority - && !isHighestPrio(dom, dest.tcbPriority), - ) { + if unlikely(dest.tcbPriority < current.tcbPriority && !isHighestPrio(dom, dest.tcbPriority)) { slowpath(SysCall as usize); } - if unlikely( - (ep_cap.get_ep_can_grant() == 0) - && (ep_cap.get_ep_can_grant_reply() == 0), - ) { + if unlikely((ep_cap.get_ep_can_grant() == 0) && (ep_cap.get_ep_can_grant_reply() == 0)) { slowpath(SysCall as usize); } #[cfg(feature = "ENABLE_SMP")] @@ -208,7 +202,9 @@ pub fn fastpath_call(cptr: usize, msgInfo: usize) { fastpath_copy_mrs(length, current, dest); dest.tcbState.words[0] = ThreadState::ThreadStateRunning as usize; let cap_pd = new_vtable.get_pt_base_ptr() as *mut pte_t; - let stored_hw_asid: pte_t = pte_t { words: [new_vtable.get_pt_mapped_asid()] }; + let stored_hw_asid: pte_t = pte_t { + words: [new_vtable.get_pt_mapped_asid()], + }; switchToThread_fp(dest as *mut tcb_t, cap_pd, stored_hw_asid); info.set_caps_unwrapped(0); let msgInfo1 = info.to_word(); @@ -233,14 +229,13 @@ pub fn fastpath_reply_recv(cptr: usize, msgInfo: usize) { let ep_cap = lookup_fp(¤t.get_cspace(tcbCTable).cap, cptr); - if unlikely( - ep_cap.get_cap_type() != CapTag::CapEndpointCap - || ep_cap.get_ep_can_send() == 0 - ) { + if unlikely(ep_cap.get_cap_type() != CapTag::CapEndpointCap || ep_cap.get_ep_can_send() == 0) { slowpath(SysReplyRecv as usize); } - if let Some(ntfn) = convert_to_option_mut_type_ref::(current.tcbBoundNotification) { + if let Some(ntfn) = + convert_to_option_mut_type_ref::(current.tcbBoundNotification) + { if ntfn.get_state() == NtfnState::Active { slowpath(SysReplyRecv as usize); } @@ -278,8 +273,9 @@ pub fn fastpath_reply_recv(cptr: usize, msgInfo: usize) { ep.get_ptr(), ThreadState::ThreadStateBlockedOnReceive as usize, ); - current.tcbState.set_blocking_ipc_can_grant(ep_cap.get_ep_can_grant()); - + current + .tcbState + .set_blocking_ipc_can_grant(ep_cap.get_ep_can_grant()); if let Some(ep_tail_tcb) = convert_to_option_mut_type_ref::(ep.get_queue_tail()) { ep_tail_tcb.tcbEPNext = current.get_ptr(); @@ -290,7 +286,11 @@ pub fn fastpath_reply_recv(cptr: usize, msgInfo: usize) { current.tcbEPNext = 0; ep.set_queue_head(current.get_ptr()); } - endpoint_ptr_mset_epQueue_tail_state(ep as *mut endpoint_t, get_currenct_thread().get_ptr(), EPState_Recv); + endpoint_ptr_mset_epQueue_tail_state( + ep as *mut endpoint_t, + get_currenct_thread().get_ptr(), + EPState_Recv, + ); unsafe { let node = convert_to_mut_type_ref::(caller_slot.cteMDBNode.get_prev()); @@ -301,11 +301,12 @@ pub fn fastpath_reply_recv(cptr: usize, msgInfo: usize) { caller.tcbState.words[0] = ThreadState::ThreadStateRunning as usize; let cap_pd = new_vtable.get_pt_base_ptr() as *mut pte_t; - let stored_hw_asid: pte_t = pte_t { words: [new_vtable.get_pt_mapped_asid()] }; + let stored_hw_asid: pte_t = pte_t { + words: [new_vtable.get_pt_mapped_asid()], + }; switchToThread_fp(caller, cap_pd, stored_hw_asid); info.set_caps_unwrapped(0); let msg_info1 = info.to_word(); fastpath_restore(0, msg_info1, get_currenct_thread() as *mut tcb_t); } } - \ No newline at end of file diff --git a/src/kernel/fault.rs b/src/kernel/fault.rs index 2caa044..41087b2 100644 --- a/src/kernel/fault.rs +++ b/src/kernel/fault.rs @@ -5,6 +5,9 @@ use sel4_task::*; pub fn process3(sender: *mut tcb_t, receiver: *mut tcb_t, _receiveIPCBuffer: *mut usize) -> usize { unsafe { (*sender).copy_syscall_fault_mrs(&mut *receiver); - (*receiver).set_mr(n_syscallMessage, (*sender).tcbFault.unknown_syscall_get_syscall_number()) + (*receiver).set_mr( + n_syscallMessage, + (*sender).tcbFault.unknown_syscall_get_syscall_number(), + ) } -} \ No newline at end of file +} diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index 87ad76f..a4dcaec 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -1,4 +1,3 @@ pub mod boot; +pub mod fastpath; pub mod fault; -pub mod c_traps; -pub mod fastpath; \ No newline at end of file diff --git a/src/lang_items.rs b/src/lang_items.rs index 58e735b..5e3b0af 100644 --- a/src/lang_items.rs +++ b/src/lang_items.rs @@ -1,7 +1,7 @@ use log::error; -#[cfg(target_arch = "riscv64")] -use sel4_common::sbi::shutdown; + use core::panic::PanicInfo; +use sel4_common::arch::shutdown; #[panic_handler] fn panic(info: &PanicInfo) -> ! { @@ -16,4 +16,4 @@ fn panic(info: &PanicInfo) -> ! { error!("[kernel] Panicked: {}", info.message().unwrap()); } shutdown() -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 7b33534..2a7ca15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ #![feature(core_intrinsics)] #![no_main] #![allow(dead_code)] +#![allow(internal_features)] #![allow(non_snake_case)] #![allow(non_camel_case_types)] #![allow(non_upper_case_globals)] @@ -11,33 +12,25 @@ #![feature(panic_info_message)] #![feature(linkage)] - extern crate core; -#[cfg(target_arch = "riscv64")] -use sel4_common::sbi::shutdown; +use sel4_common::arch::shutdown; mod config; // mod console; -mod lang_items; -mod utils; +mod arch; +mod boot; +mod interrupt; mod kernel; -mod structures; +mod lang_items; mod object; -#[cfg(target_arch = "riscv64")] -mod riscv; -#[cfg(target_arch = "aarch64")] -mod arm_gic; +mod structures; mod syscall; -mod boot; -mod interrupt; -mod exception; +mod utils; +mod compatibility; mod deps; mod interfaces_impl; -mod compatibility; - -pub use sel4_common::{BIT, MASK, plus_define_bitfield, ROUND_UP, ROUND_DOWN, IS_ALIGNED}; - +pub use sel4_common::{plus_define_bitfield, BIT, IS_ALIGNED, MASK, ROUND_DOWN, ROUND_UP}; #[no_mangle] pub extern "C" fn halt() { diff --git a/src/object/mod.rs b/src/object/mod.rs index 305f98e..59105b1 100644 --- a/src/object/mod.rs +++ b/src/object/mod.rs @@ -1,25 +1,35 @@ +use crate::structures::lookupCapAndSlot_ret_t; +use crate::syscall::handle_fault; use sel4_common::message_info::MessageLabel; use sel4_common::structures::exception_t; use sel4_cspace::interface::{cap_t, cte_t}; use sel4_task::tcb_t; -use crate::structures::lookupCapAndSlot_ret_t; -use crate::syscall::handle_fault; #[no_mangle] -pub fn decodeRISCVMMUInvocation(_label: MessageLabel, _length: usize, _cptr: usize, _cte: *mut cte_t, - _cap: &mut cap_t, _call: bool, _buffer: *mut usize, +pub fn decodeRISCVMMUInvocation( + _label: MessageLabel, + _length: usize, + _cptr: usize, + _cte: *mut cte_t, + _cap: &mut cap_t, + _call: bool, + _buffer: *mut usize, ) -> exception_t { panic!("should not be invoked!") } - #[no_mangle] pub fn configureIdleThread(_tcb: *const tcb_t) { panic!("should not be invoked!") } #[no_mangle] -pub fn setMR(_receiver: *mut tcb_t, _receivedBuffer: *mut usize, _offset: usize, _reg: usize) -> usize { +pub fn setMR( + _receiver: *mut tcb_t, + _receivedBuffer: *mut usize, + _offset: usize, + _reg: usize, +) -> usize { panic!("should not be invoked!") } @@ -33,9 +43,7 @@ pub fn handleFault(tptr: *mut tcb_t) { #[no_mangle] pub extern "C" fn lookupCapAndSlot(thread: *const tcb_t, cPtr: usize) -> lookupCapAndSlot_ret_t { // let lu_ret = lookupSlot(thread, cPtr); - let lu_ret = unsafe { - (*thread).lookup_slot(cPtr) - }; + let lu_ret = unsafe { (*thread).lookup_slot(cPtr) }; if lu_ret.status != exception_t::EXCEPTION_NONE { let ret = lookupCapAndSlot_ret_t { status: lu_ret.status, diff --git a/src/sbi.rs b/src/sbi.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/structures.rs b/src/structures.rs index 1807122..86c0a45 100644 --- a/src/structures.rs +++ b/src/structures.rs @@ -1,11 +1,8 @@ -use sel4_common::structures::{exception_t, seL4_IPCBuffer}; +use crate::config::{CONFIG_MAX_NUM_BOOTINFO_UNTYPED_CAPS, MAX_NUM_FREEMEM_REG, MAX_NUM_RESV_REG}; use sel4_common::sel4_config::seL4_MsgMaxExtraCaps; +use sel4_common::structures::{exception_t, seL4_IPCBuffer}; use sel4_cspace::interface::{cap_t, cte_t}; use sel4_vspace::pptr_t; -use crate::config::{ - CONFIG_MAX_NUM_BOOTINFO_UNTYPED_CAPS, - MAX_NUM_FREEMEM_REG, MAX_NUM_RESV_REG, -}; #[repr(C)] #[derive(Copy, Clone)] @@ -170,4 +167,3 @@ pub struct syscall_error_t { pub struct extra_caps_t { pub excaprefs: [pptr_t; seL4_MsgMaxExtraCaps], } - diff --git a/src/syscall/invocation/decode/decode_cnode_invocation.rs b/src/syscall/invocation/decode/decode_cnode_invocation.rs index af0b36e..536c676 100644 --- a/src/syscall/invocation/decode/decode_cnode_invocation.rs +++ b/src/syscall/invocation/decode/decode_cnode_invocation.rs @@ -1,37 +1,55 @@ -use sel4_common::{message_info::MessageLabel, sel4_config::{seL4_IllegalOperation, seL4_TruncatedMessage, seL4_FailedLookup, seL4_DeleteFirst}, -structures::{exception_t, seL4_IPCBuffer}, utils::convert_to_mut_type_ref}; -use sel4_cspace::interface::{cap_t, cte_t, CapTag, seL4_CapRights_t}; use log::debug; use sel4_common::fault::lookup_fault_t; +use sel4_common::{ + message_info::MessageLabel, + sel4_config::{ + seL4_DeleteFirst, seL4_FailedLookup, seL4_IllegalOperation, seL4_TruncatedMessage, + }, + structures::{exception_t, seL4_IPCBuffer}, + utils::convert_to_mut_type_ref, +}; +use sel4_cspace::interface::{cap_t, cte_t, seL4_CapRights_t, CapTag}; use crate::{ - kernel::boot::{current_syscall_error, current_lookup_fault, get_extra_cap_by_index}, - syscall::{get_syscall_arg, lookup_slot_for_cnode_op, invocation::invoke_cnode::*} - }; - -pub fn decode_cnode_invocation(invLabel: MessageLabel, length: usize, cap: &cap_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { + kernel::boot::{current_lookup_fault, current_syscall_error, get_extra_cap_by_index}, + syscall::{get_syscall_arg, invocation::invoke_cnode::*, lookup_slot_for_cnode_op}, +}; + +pub fn decode_cnode_invocation( + invLabel: MessageLabel, + length: usize, + cap: &cap_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if invLabel < MessageLabel::CNodeRevoke || invLabel > MessageLabel::CNodeSaveCaller { debug!("CNodeCap: Illegal Operation attempted."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } if length < 2 { debug!("CNode operation: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let index = get_syscall_arg(0, buffer); let w_bits = get_syscall_arg(1, buffer); let lu_ret = lookup_slot_for_cnode_op(false, cap, index, w_bits); - + if lu_ret.status != exception_t::EXCEPTION_NONE { debug!("CNode operation: Target slot invalid."); return lu_ret.status; } let dest_slot = convert_to_mut_type_ref::(lu_ret.slot as usize); match invLabel { - MessageLabel::CNodeCopy | MessageLabel::CNodeMint | MessageLabel::CNodeMove | MessageLabel::CNodeMutate => { + MessageLabel::CNodeCopy + | MessageLabel::CNodeMint + | MessageLabel::CNodeMove + | MessageLabel::CNodeMutate => { return decode_cnode_invoke_with_two_slot(invLabel, dest_slot, length, buffer); } MessageLabel::CNodeRevoke => invoke_cnode_revoke(dest_slot), @@ -43,10 +61,17 @@ pub fn decode_cnode_invocation(invLabel: MessageLabel, length: usize, cap: &cap_ } } -fn decode_cnode_invoke_with_two_slot(label: MessageLabel, dest_slot: &mut cte_t, length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_cnode_invoke_with_two_slot( + label: MessageLabel, + dest_slot: &mut cte_t, + length: usize, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if length < 4 || get_extra_cap_by_index(0).is_none() { debug!("CNode Copy/Mint/Move/Mutate: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -55,7 +80,9 @@ fn decode_cnode_invoke_with_two_slot(label: MessageLabel, dest_slot: &mut cte_t, let src_root = get_extra_cap_by_index(0).unwrap().cap; if dest_slot.cap.get_cap_type() != CapTag::CapNullCap { debug!("CNode Copy/Mint/Move/Mutate: Destination not empty."); - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -73,12 +100,14 @@ fn decode_cnode_invoke_with_two_slot(label: MessageLabel, dest_slot: &mut cte_t, } return exception_t::EXCEPTION_SYSCALL_ERROR; } - + match label { MessageLabel::CNodeCopy => { if length < 5 { debug!("Truncated message for CNode Copy operation."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let cap_right = seL4_CapRights_t::from_word(get_syscall_arg(4, buffer)); @@ -88,7 +117,9 @@ fn decode_cnode_invoke_with_two_slot(label: MessageLabel, dest_slot: &mut cte_t, MessageLabel::CNodeMint => { if length < 6 { debug!("Truncated message for CNode Mint operation."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let cap_right = seL4_CapRights_t::from_word(get_syscall_arg(4, buffer)); @@ -103,7 +134,9 @@ fn decode_cnode_invoke_with_two_slot(label: MessageLabel, dest_slot: &mut cte_t, MessageLabel::CNodeMutate => { if length < 5 { debug!("Truncated message for CNode Mutate operation."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let cap_data = get_syscall_arg(4, buffer); @@ -115,10 +148,16 @@ fn decode_cnode_invoke_with_two_slot(label: MessageLabel, dest_slot: &mut cte_t, } } -fn decode_cnode_rotate(dest_slot: &mut cte_t, length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_cnode_rotate( + dest_slot: &mut cte_t, + length: usize, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if length < 8 || get_extra_cap_by_index(0).is_none() || get_extra_cap_by_index(1).is_none() { debug!("CNode Rotate: Target cap invalid."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -129,7 +168,7 @@ fn decode_cnode_rotate(dest_slot: &mut cte_t, length: usize, buffer: Option<&seL let src_idnex = get_syscall_arg(6, buffer); let src_depth = get_syscall_arg(7, buffer); - let pivot_root= get_extra_cap_by_index(0).unwrap().cap; + let pivot_root = get_extra_cap_by_index(0).unwrap().cap; let src_root = get_extra_cap_by_index(1).unwrap().cap; let lu_ret = lookup_slot_for_cnode_op(true, &src_root, src_idnex, src_depth); @@ -138,7 +177,7 @@ fn decode_cnode_rotate(dest_slot: &mut cte_t, length: usize, buffer: Option<&seL } let src_slot = convert_to_mut_type_ref::(lu_ret.slot as usize); - let lu_ret= lookup_slot_for_cnode_op(true, &pivot_root , pivot_index, pivot_depth); + let lu_ret = lookup_slot_for_cnode_op(true, &pivot_root, pivot_index, pivot_depth); if lu_ret.status != exception_t::EXCEPTION_NONE { return lu_ret.status; } @@ -146,12 +185,16 @@ fn decode_cnode_rotate(dest_slot: &mut cte_t, length: usize, buffer: Option<&seL if pivot_slot.get_ptr() == src_slot.get_ptr() || pivot_slot.get_ptr() == dest_slot.get_ptr() { debug!("CNode Rotate: Pivot slot the same as source or dest slot."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } if src_slot.get_ptr() != dest_slot.get_ptr() { if dest_slot.cap.get_cap_type() != CapTag::CapNullCap { - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } } @@ -173,5 +216,11 @@ fn decode_cnode_rotate(dest_slot: &mut cte_t, length: usize, buffer: Option<&seL } return exception_t::EXCEPTION_SYSCALL_ERROR; } - return invoke_cnode_rotate(src_slot, pivot_slot, dest_slot, src_new_data, pivot_new_data); -} \ No newline at end of file + return invoke_cnode_rotate( + src_slot, + pivot_slot, + dest_slot, + src_new_data, + pivot_new_data, + ); +} diff --git a/src/syscall/invocation/decode/decode_domain_invocation.rs b/src/syscall/invocation/decode/decode_domain_invocation.rs index 1c15aa2..6310130 100644 --- a/src/syscall/invocation/decode/decode_domain_invocation.rs +++ b/src/syscall/invocation/decode/decode_domain_invocation.rs @@ -1,20 +1,36 @@ use core::intrinsics::unlikely; -use sel4_common::{message_info::MessageLabel, structures::{exception_t, seL4_IPCBuffer}, sel4_config::*, utils::convert_to_mut_type_ref}; -use sel4_cspace::interface::CapTag; use log::debug; -use sel4_task::{set_thread_state, get_currenct_thread, ThreadState, tcb_t}; +use sel4_common::{ + message_info::MessageLabel, + sel4_config::*, + structures::{exception_t, seL4_IPCBuffer}, + utils::convert_to_mut_type_ref, +}; +use sel4_cspace::interface::CapTag; +use sel4_task::{get_currenct_thread, set_thread_state, tcb_t, ThreadState}; -use crate::{kernel::boot::{current_syscall_error, get_extra_cap_by_index}, syscall::get_syscall_arg}; +use crate::{ + kernel::boot::{current_syscall_error, get_extra_cap_by_index}, + syscall::get_syscall_arg, +}; -pub fn decode_domain_invocation(invLabel: MessageLabel, length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +pub fn decode_domain_invocation( + invLabel: MessageLabel, + length: usize, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if invLabel != MessageLabel::DomainSetSet { - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } if length == 0 { debug!("Domain Configure: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let domain = get_syscall_arg(0, buffer); @@ -22,13 +38,15 @@ pub fn decode_domain_invocation(invLabel: MessageLabel, length: usize, buffer: O debug!("Domain Configure: invalid domain ({} >= 1).", domain); unsafe { current_syscall_error._type = seL4_InvalidArgument; - current_syscall_error.invalidArgumentNumber = 0; + current_syscall_error.invalidArgumentNumber = 0; } return exception_t::EXCEPTION_SYSCALL_ERROR; } if get_extra_cap_by_index(0).is_none() { debug!("Domain Configure: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let thread_cap = get_extra_cap_by_index(0).unwrap().cap; @@ -44,4 +62,4 @@ pub fn decode_domain_invocation(invLabel: MessageLabel, length: usize, buffer: O set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); convert_to_mut_type_ref::(thread_cap.get_tcb_ptr()).set_domain(domain); exception_t::EXCEPTION_NONE -} \ No newline at end of file +} diff --git a/src/syscall/invocation/decode/decode_irq_invocation.rs b/src/syscall/invocation/decode/decode_irq_invocation.rs index 01f7091..85274b4 100644 --- a/src/syscall/invocation/decode/decode_irq_invocation.rs +++ b/src/syscall/invocation/decode/decode_irq_invocation.rs @@ -1,20 +1,32 @@ -use sel4_common::{message_info::MessageLabel, structures::{seL4_IPCBuffer, exception_t}, sel4_config::*, utils::convert_to_mut_type_ref}; -use sel4_cspace::interface::{cte_t, CapTag}; use log::debug; -use sel4_task::{set_thread_state, get_currenct_thread, ThreadState}; +use sel4_common::{ + message_info::MessageLabel, + sel4_config::*, + structures::{exception_t, seL4_IPCBuffer}, + utils::convert_to_mut_type_ref, +}; +use sel4_cspace::interface::{cte_t, CapTag}; +use sel4_task::{get_currenct_thread, set_thread_state, ThreadState}; +use crate::syscall::invocation::invoke_irq::{invoke_clear_irq_handler, invoke_set_irq_handler}; use crate::{ - kernel::boot::{get_extra_cap_by_index, current_syscall_error}, - syscall::{get_syscall_arg, lookupSlotForCNodeOp, invocation::invoke_irq::invoke_irq_control}, - interrupt::is_irq_active, config::{maxIRQ, irqInvalid} + config::{irqInvalid, maxIRQ}, + interrupt::is_irq_active, + kernel::boot::{current_syscall_error, get_extra_cap_by_index}, + syscall::{get_syscall_arg, invocation::invoke_irq::invoke_irq_control, lookupSlotForCNodeOp}, }; -use crate::syscall::invocation::invoke_irq::{invoke_clear_irq_handler, invoke_set_irq_handler}; - -pub fn decode_irq_control_invocation(label: MessageLabel, length: usize, src_slot: &mut cte_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +pub fn decode_irq_control_invocation( + label: MessageLabel, + length: usize, + src_slot: &mut cte_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if label == MessageLabel::IRQIssueIRQHandler { if length < 3 || get_extra_cap_by_index(0).is_none() { - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let irq = get_syscall_arg(0, buffer); @@ -27,7 +39,9 @@ pub fn decode_irq_control_invocation(label: MessageLabel, length: usize, src_slo return status; } if is_irq_active(irq) { - unsafe { current_syscall_error._type = seL4_RevokeFirst; } + unsafe { + current_syscall_error._type = seL4_RevokeFirst; + } debug!("Rejecting request for IRQ {}. Already active.", irq); return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -38,18 +52,23 @@ pub fn decode_irq_control_invocation(label: MessageLabel, length: usize, src_slo } let dest_slot = convert_to_mut_type_ref::(lu_ret.slot as usize); if dest_slot.cap.get_cap_type() != CapTag::CapNullCap { - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } debug!("Target slot for new IRQ Handler cap not empty"); return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - invoke_irq_control(irq, convert_to_mut_type_ref::(lu_ret.slot as usize), src_slot) + invoke_irq_control( + irq, + convert_to_mut_type_ref::(lu_ret.slot as usize), + src_slot, + ) } else { arch_decode_irq_control_invocation(label, length, src_slot, buffer) } } - pub fn decode_irq_handler_invocation(label: MessageLabel, irq: usize) -> exception_t { return match label { MessageLabel::IRQAckIRQ => { @@ -59,18 +78,21 @@ pub fn decode_irq_handler_invocation(label: MessageLabel, irq: usize) -> excepti MessageLabel::IRQSetIRQHandler => { if get_extra_cap_by_index(0).is_none() { - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let slot = get_extra_cap_by_index(0).unwrap(); let ntfn_cap = slot.cap; if ntfn_cap.get_cap_type() != CapTag::CapNotificationCap - || ntfn_cap.get_nf_can_send() == 0 { + || ntfn_cap.get_nf_can_send() == 0 + { unsafe { current_syscall_error._type = seL4_InvalidCapability; current_syscall_error.invalidCapNumber = 0; } - return exception_t::EXCEPTION_SYSCALL_ERROR + return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); invoke_set_irq_handler(irq, &ntfn_cap, slot); @@ -83,17 +105,25 @@ pub fn decode_irq_handler_invocation(label: MessageLabel, irq: usize) -> excepti } _ => { debug!("IRQHandler: Illegal operation."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } exception_t::EXCEPTION_SYSCALL_ERROR } - } - + }; } -fn arch_decode_irq_control_invocation(label: MessageLabel, length: usize, src_slot: &mut cte_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn arch_decode_irq_control_invocation( + label: MessageLabel, + length: usize, + src_slot: &mut cte_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if label == MessageLabel::RISCVIRQIssueIRQHandlerTrigger { if length < 4 || get_extra_cap_by_index(0).is_none() { - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let irq = get_syscall_arg(0, buffer); @@ -106,7 +136,9 @@ fn arch_decode_irq_control_invocation(label: MessageLabel, length: usize, src_sl return status; } if is_irq_active(irq) { - unsafe { current_syscall_error._type = seL4_RevokeFirst; } + unsafe { + current_syscall_error._type = seL4_RevokeFirst; + } debug!("Rejecting request for IRQ {}. Already active.", irq); return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -116,9 +148,15 @@ fn arch_decode_irq_control_invocation(label: MessageLabel, length: usize, src_sl return lu_ret.status; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - invoke_irq_control(irq, convert_to_mut_type_ref::(lu_ret.slot as usize), src_slot) + invoke_irq_control( + irq, + convert_to_mut_type_ref::(lu_ret.slot as usize), + src_slot, + ) } else { - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } } diff --git a/src/syscall/invocation/decode/decode_mmu_invocation.rs b/src/syscall/invocation/decode/decode_mmu_invocation.rs index 1d2e571..3bddf6c 100644 --- a/src/syscall/invocation/decode/decode_mmu_invocation.rs +++ b/src/syscall/invocation/decode/decode_mmu_invocation.rs @@ -5,26 +5,43 @@ use core::intrinsics::unlikely; // sel4_config::*, utils::{convert_to_mut_type_ref, pageBitsForSize}, fault::*, // }, BIT, MASK}; - -use sel4_cspace::interface::{cte_t, CapTag, cap_t}; use log::debug; -use sel4_common::{BIT, MASK}; use sel4_common::fault::lookup_fault_t; use sel4_common::message_info::MessageLabel; -use sel4_common::sel4_config::{asidInvalid, asidLowBits, nASIDPools, seL4_AlignmentError, seL4_DeleteFirst, seL4_FailedLookup, seL4_IllegalOperation, seL4_InvalidArgument, seL4_InvalidCapability, seL4_PageBits, seL4_RevokeFirst, seL4_TruncatedMessage}; +use sel4_common::sel4_config::{ + asidInvalid, asidLowBits, nASIDPools, seL4_AlignmentError, seL4_DeleteFirst, seL4_FailedLookup, + seL4_IllegalOperation, seL4_InvalidArgument, seL4_InvalidCapability, seL4_PageBits, + seL4_RevokeFirst, seL4_TruncatedMessage, +}; use sel4_common::structures::{exception_t, seL4_IPCBuffer}; use sel4_common::utils::{convert_to_mut_type_ref, pageBitsForSize}; -use sel4_task::{set_thread_state, get_currenct_thread, ThreadState}; -use sel4_vspace::{find_vspace_for_asid, pte_t, vm_attributes_t, checkVPAlignment, get_asid_pool_by_index}; +use sel4_common::{BIT, MASK}; +use sel4_cspace::interface::{cap_t, cte_t, CapTag}; +use sel4_task::{get_currenct_thread, set_thread_state, ThreadState}; +use sel4_vspace::{ + checkVPAlignment, find_vspace_for_asid, get_asid_pool_by_index, pte_t, vm_attributes_t, +}; use crate::{ - kernel::boot::{current_syscall_error, current_lookup_fault, get_extra_cap_by_index}, - syscall::{invocation::invoke_mmu_op::{invoke_page_table_unmap, invoke_page_table_map, invoke_page_map, invoke_page_unmap, invoke_page_get_address, invoke_asid_control, invoke_asid_pool}, get_syscall_arg, lookup_slot_for_cnode_op}, - config::{USER_TOP, seL4_ASIDPoolBits} + config::{seL4_ASIDPoolBits, USER_TOP}, + kernel::boot::{current_lookup_fault, current_syscall_error, get_extra_cap_by_index}, + syscall::{ + get_syscall_arg, + invocation::invoke_mmu_op::{ + invoke_asid_control, invoke_asid_pool, invoke_page_get_address, invoke_page_map, + invoke_page_table_map, invoke_page_table_unmap, invoke_page_unmap, + }, + lookup_slot_for_cnode_op, + }, }; - -pub fn decode_mmu_invocation(label: MessageLabel, length: usize, slot: &mut cte_t, call: bool, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +pub fn decode_mmu_invocation( + label: MessageLabel, + length: usize, + slot: &mut cte_t, + call: bool, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { match slot.cap.get_cap_type() { CapTag::CapPageTableCap => decode_page_table_invocation(label, length, slot, buffer), CapTag::CapFrameCap => decode_frame_invocation(label, length, slot, call, buffer), @@ -36,20 +53,33 @@ pub fn decode_mmu_invocation(label: MessageLabel, length: usize, slot: &mut cte_ } } -fn decode_page_table_invocation(label: MessageLabel, length: usize, cte: &mut cte_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_page_table_invocation( + label: MessageLabel, + length: usize, + cte: &mut cte_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { match label { MessageLabel::RISCVPageTableUnmap => decode_page_table_unmap(cte), MessageLabel::RISCVPageTableMap => decode_page_table_map(length, cte, buffer), _ => { debug!("RISCVPageTable: Illegal Operation"); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } } } -fn decode_frame_invocation(label: MessageLabel, length: usize, frame_slot: &mut cte_t, call: bool, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_frame_invocation( + label: MessageLabel, + length: usize, + frame_slot: &mut cte_t, + call: bool, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { match label { MessageLabel::RISCVPageMap => decode_frame_map(length, frame_slot, buffer), MessageLabel::RISCVPageUnmap => { @@ -62,21 +92,33 @@ fn decode_frame_invocation(label: MessageLabel, length: usize, frame_slot: &mut } _ => { debug!("invalid operation label:{:?}", label); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } exception_t::EXCEPTION_SYSCALL_ERROR } } } -fn decode_asid_control(label: MessageLabel, length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_asid_control( + label: MessageLabel, + length: usize, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if label != MessageLabel::RISCVASIDControlMakePool { - unsafe { current_syscall_error._type = seL4_IllegalOperation; } - return exception_t::EXCEPTION_SYSCALL_ERROR; + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } + return exception_t::EXCEPTION_SYSCALL_ERROR; } - if unlikely(length < 2 || get_extra_cap_by_index(0).is_none() || get_extra_cap_by_index(1).is_none()) { - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } - return exception_t::EXCEPTION_SYSCALL_ERROR; + if unlikely( + length < 2 || get_extra_cap_by_index(0).is_none() || get_extra_cap_by_index(1).is_none(), + ) { + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } + return exception_t::EXCEPTION_SYSCALL_ERROR; } let index = get_syscall_arg(0, buffer); let depth = get_syscall_arg(1, buffer); @@ -90,13 +132,17 @@ fn decode_asid_control(label: MessageLabel, length: usize, buffer: Option<&seL4_ } if i == nASIDPools { - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let asid_base = i << asidLowBits; - if untyped_cap.get_cap_type() != CapTag::CapUntypedCap || untyped_cap.get_untyped_block_size() != seL4_ASIDPoolBits - || untyped_cap.get_untyped_is_device() != 0 { + if untyped_cap.get_cap_type() != CapTag::CapUntypedCap + || untyped_cap.get_untyped_block_size() != seL4_ASIDPoolBits + || untyped_cap.get_untyped_is_device() != 0 + { unsafe { current_syscall_error._type = seL4_InvalidCapability; current_syscall_error.invalidCapNumber = 1; @@ -106,7 +152,9 @@ fn decode_asid_control(label: MessageLabel, length: usize, buffer: Option<&seL4_ let status = parent_slot.ensure_no_children(); if status != exception_t::EXCEPTION_NONE { - unsafe { current_syscall_error._type = seL4_RevokeFirst; } + unsafe { + current_syscall_error._type = seL4_RevokeFirst; + } return status; } @@ -119,7 +167,9 @@ fn decode_asid_control(label: MessageLabel, length: usize, buffer: Option<&seL4_ let dest_slot = convert_to_mut_type_ref::(lu_ret.slot as usize); if dest_slot.cap.get_cap_type() != CapTag::CapNullCap { - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); @@ -127,21 +177,27 @@ fn decode_asid_control(label: MessageLabel, length: usize, buffer: Option<&seL4_ } fn decode_asid_pool(label: MessageLabel, cte: &mut cte_t) -> exception_t { - // debug!("in cap_asid_pool_cap"); + // debug!("in cap_asid_pool_cap"); if label != MessageLabel::RISCVASIDPoolAssign { - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } if unlikely(get_extra_cap_by_index(0).is_none()) { - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let vspace_slot = get_extra_cap_by_index(0).unwrap(); let vspace_cap = vspace_slot.cap; - if unlikely(vspace_cap.get_cap_type() != CapTag::CapPageTableCap || vspace_cap.get_pt_is_mapped() != 0) { + if unlikely( + vspace_cap.get_cap_type() != CapTag::CapPageTableCap || vspace_cap.get_pt_is_mapped() != 0, + ) { debug!("RISCVASIDPool: Invalid vspace root."); unsafe { current_syscall_error._type = seL4_InvalidCapability; @@ -159,14 +215,16 @@ fn decode_asid_pool(label: MessageLabel, cte: &mut cte_t) -> exception_t { } return exception_t::EXCEPTION_SYSCALL_ERROR; } - + let mut i = 0; while i < BIT!(asidLowBits) && (asid + i == 0 || pool.get_vspace_by_index(i).is_some()) { i += 1; } if i == BIT!(asidLowBits) { - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -183,10 +241,16 @@ fn decode_asid_pool(label: MessageLabel, cte: &mut cte_t) -> exception_t { } } -fn decode_frame_map(length: usize, frame_slot: &mut cte_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_frame_map( + length: usize, + frame_slot: &mut cte_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if length < 3 || get_extra_cap_by_index(0).is_none() { debug!("RISCVPageMap: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -206,7 +270,9 @@ fn decode_frame_map(length: usize, frame_slot: &mut cte_t, buffer: Option<&seL4_ } if unlikely(!checkVPAlignment(frame_size, vaddr)) { - unsafe { current_syscall_error._type = seL4_AlignmentError; } + unsafe { + current_syscall_error._type = seL4_AlignmentError; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -243,26 +309,40 @@ fn decode_frame_map(length: usize, frame_slot: &mut cte_t, buffer: Option<&seL4_ if pt_slot.is_pte_table() { debug!("RISCVPageMap: no mapping to remap."); - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } } else { if pt_slot.get_vaild() != 0 { debug!("Virtual address already mapped"); - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } } - invoke_page_map(&mut frame_slot.cap.clone(), w_rights_mask, vaddr, asid, attr, pt_slot, frame_slot) + invoke_page_map( + &mut frame_slot.cap.clone(), + w_rights_mask, + vaddr, + asid, + attr, + pt_slot, + frame_slot, + ) } else { - return exception_t::EXCEPTION_SYSCALL_ERROR; + return exception_t::EXCEPTION_SYSCALL_ERROR; } } fn decode_page_table_unmap(pt_cte: &mut cte_t) -> exception_t { if !pt_cte.is_final_cap() { debug!("RISCVPageTableUnmap: cannot unmap if more than once cap exists"); - unsafe { current_syscall_error._type = seL4_RevokeFirst; } + unsafe { + current_syscall_error._type = seL4_RevokeFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let cap = &mut pt_cte.cap; @@ -270,22 +350,34 @@ fn decode_page_table_unmap(pt_cte: &mut cte_t) -> exception_t { let asid = cap.get_pt_mapped_asid(); let find_ret = find_vspace_for_asid(asid); let pte_ptr = cap.get_pt_base_ptr() as *mut pte_t; - if find_ret.status == exception_t::EXCEPTION_NONE && find_ret.vspace_root.unwrap() == pte_ptr { + if find_ret.status == exception_t::EXCEPTION_NONE + && find_ret.vspace_root.unwrap() == pte_ptr + { debug!("RISCVPageTableUnmap: cannot call unmap on top level PageTable"); - unsafe { current_syscall_error._type = seL4_RevokeFirst; } + unsafe { + current_syscall_error._type = seL4_RevokeFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } else { - unsafe { current_lookup_fault = find_ret.lookup_fault.unwrap(); } + unsafe { + current_lookup_fault = find_ret.lookup_fault.unwrap(); + } } } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); return invoke_page_table_unmap(cap); } -fn decode_page_table_map(length: usize, pt_cte: &mut cte_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_page_table_map( + length: usize, + pt_cte: &mut cte_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if unlikely(length < 2 || get_extra_cap_by_index(0).is_none()) { debug!("RISCVPageTableMap: truncated message"); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let cap = &mut pt_cte.cap; @@ -306,7 +398,6 @@ fn decode_page_table_map(length: usize, pt_cte: &mut cte_t, buffer: Option<&seL4 current_syscall_error.invalidCapNumber = 0; } return exception_t::EXCEPTION_SYSCALL_ERROR; - } let lvl1pt_cap = get_extra_cap_by_index(0).unwrap().cap; @@ -315,18 +406,22 @@ fn decode_page_table_map(length: usize, pt_cte: &mut cte_t, buffer: Option<&seL4 let lu_slot = convert_to_mut_type_ref::(lu_ret.ptSlot as usize); if lu_ret.ptBitsLeft == seL4_PageBits || lu_slot.get_vaild() != 0 { debug!("RISCVPageTableMap: All objects mapped at this address"); - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); return invoke_page_table_map(cap, lu_slot, asid, vaddr & !MASK!(lu_ret.ptBitsLeft)); } else { return exception_t::EXCEPTION_SYSCALL_ERROR; - } + } } fn get_vspace(lvl1pt_cap: &cap_t) -> Option<(&mut pte_t, usize)> { - if lvl1pt_cap.get_cap_type() != CapTag::CapPageTableCap || lvl1pt_cap.get_pt_is_mapped() == asidInvalid { + if lvl1pt_cap.get_cap_type() != CapTag::CapPageTableCap + || lvl1pt_cap.get_pt_is_mapped() == asidInvalid + { debug!("RISCVMMUInvocation: Invalid top-level PageTable."); unsafe { current_syscall_error._type = seL4_InvalidCapability; diff --git a/src/syscall/invocation/decode/decode_tcb_invocation.rs b/src/syscall/invocation/decode/decode_tcb_invocation.rs index 44513cd..2fb7460 100644 --- a/src/syscall/invocation/decode/decode_tcb_invocation.rs +++ b/src/syscall/invocation/decode/decode_tcb_invocation.rs @@ -1,21 +1,29 @@ /*use crate::{common::{message_info::MessageLabel, structures::{exception_t, seL4_IPCBuffer}, - sel4_config::{seL4_IllegalOperation, seL4_TruncatedMessage, seL4_RangeError, tcbCTable, tcbVTable, seL4_InvalidCapability}, + sel4_config::{seL4_IllegalOperation, seL4_TruncatedMessage, seL4_RangeError, tcbCTable, tcbVTable, seL4_InvalidCapability}, utils::convert_to_mut_type_ref, }, BIT};*/ -use sel4_cspace::interface::{cap_t, cte_t, CapTag}; -use sel4_ipc::notification_t; use log::debug; -use sel4_common::BIT; use sel4_common::message_info::MessageLabel; -use sel4_common::sel4_config::{seL4_IllegalOperation, seL4_InvalidCapability, seL4_RangeError, seL4_TruncatedMessage, tcbCTable, tcbVTable}; +use sel4_common::sel4_config::{ + seL4_IllegalOperation, seL4_InvalidCapability, seL4_RangeError, seL4_TruncatedMessage, + tcbCTable, tcbVTable, +}; use sel4_common::structures::{exception_t, seL4_IPCBuffer}; use sel4_common::utils::convert_to_mut_type_ref; -use sel4_task::{tcb_t, set_thread_state, get_currenct_thread, ThreadState}; +use sel4_common::BIT; +use sel4_cspace::interface::{cap_t, cte_t, CapTag}; +use sel4_ipc::notification_t; +use sel4_task::{get_currenct_thread, set_thread_state, tcb_t, ThreadState}; use crate::{ + config::{n_frameRegisters, n_gpRegisters}, kernel::boot::{current_syscall_error, get_extra_cap_by_index}, - config::{n_frameRegisters, n_gpRegisters}, syscall::{utils::{get_syscall_arg, check_prio, check_ipc_buffer_vaild}, is_valid_vtable_root}}; + syscall::{ + is_valid_vtable_root, + utils::{check_ipc_buffer_vaild, check_prio, get_syscall_arg}, + }, +}; use super::super::invoke_tcb::*; @@ -38,7 +46,9 @@ pub fn decode_tcb_invocation( call: bool, buffer: Option<&seL4_IPCBuffer>, ) -> exception_t { - unsafe { remoteTCBStall(convert_to_mut_type_ref::(cap.get_tcb_ptr())); } + unsafe { + remoteTCBStall(convert_to_mut_type_ref::(cap.get_tcb_ptr())); + } match invLabel { MessageLabel::TCBReadRegisters => decode_read_registers(cap, length, call, buffer), MessageLabel::TCBWriteRegisters => decode_write_registers(cap, length, buffer), @@ -51,8 +61,7 @@ pub fn decode_tcb_invocation( set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); invoke_tcb_resume(convert_to_mut_type_ref::(cap.get_tcb_ptr())) } - MessageLabel::TCBConfigure => decode_tcb_configure(cap, length, slot, - buffer), + MessageLabel::TCBConfigure => decode_tcb_configure(cap, length, slot, buffer), MessageLabel::TCBSetPriority => decode_set_priority(cap, length, buffer), MessageLabel::TCBSetMCPriority => decode_set_mc_priority(cap, length, buffer), MessageLabel::TCBSetSchedParams => decode_set_sched_params(cap, length, buffer), @@ -92,8 +101,7 @@ pub fn decode_tcb_invocation( set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); invoke_tcb_resume(convert_to_mut_type_ref::(cap.get_tcb_ptr())) } - MessageLabel::TCBConfigure => decode_tcb_configure(cap, length, slot, - buffer), + MessageLabel::TCBConfigure => decode_tcb_configure(cap, length, slot, buffer), MessageLabel::TCBSetPriority => decode_set_priority(cap, length, buffer), MessageLabel::TCBSetMCPriority => decode_set_mc_priority(cap, length, buffer), MessageLabel::TCBSetSchedParams => decode_set_sched_params(cap, length, buffer), @@ -110,16 +118,26 @@ pub fn decode_tcb_invocation( } } -fn decode_read_registers(cap: &cap_t, length: usize, call: bool, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_read_registers( + cap: &cap_t, + length: usize, + call: bool, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if length < 2 { debug!("TCB CopyRegisters: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let flags = get_syscall_arg(0, buffer); let n = get_syscall_arg(1, buffer); if n < 1 || n > n_frameRegisters + n_gpRegisters { - debug!("TCB ReadRegisters: Attempted to read an invalid number of registers:{}", n); + debug!( + "TCB ReadRegisters: Attempted to read an invalid number of registers:{}", + n + ); unsafe { current_syscall_error._type = seL4_RangeError; current_syscall_error.rangeErrorMin = 1; @@ -128,16 +146,22 @@ fn decode_read_registers(cap: &cap_t, length: usize, call: bool, buffer: Option< } } let thread = convert_to_mut_type_ref::(cap.get_tcb_ptr()); - if thread .is_current() { + if thread.is_current() { debug!("TCB ReadRegisters: Attempted to read our own registers."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); invoke_tcb_read_registers(thread, flags & BIT!(ReadRegisters_suspend), n, 0, call) } -fn decode_write_registers(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_write_registers( + cap: &cap_t, + length: usize, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if length < 2 { unsafe { debug!("TCB CopyRegisters: Truncated message."); @@ -149,42 +173,73 @@ fn decode_write_registers(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBu let w = get_syscall_arg(1, buffer); if length - 2 < w { - debug!("TCB WriteRegisters: Message too short for requested write size {}/{}", length - 2, w); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + debug!( + "TCB WriteRegisters: Message too short for requested write size {}/{}", + length - 2, + w + ); + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let thread = convert_to_mut_type_ref::(cap.get_tcb_ptr()); if thread.is_current() { debug!("TCB WriteRegisters: Attempted to write our own registers."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); invoke_tcb_write_registers(thread, flags & BIT!(0), w, 0, buffer) } -fn decode_copy_registers(cap: &cap_t, _length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_copy_registers( + cap: &cap_t, + _length: usize, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { let flags = get_syscall_arg(0, buffer); let source_cap = get_extra_cap_by_index(0).unwrap().cap; if cap.get_cap_type() != CapTag::CapThreadCap { debug!("TCB CopyRegisters: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let src_tcb = convert_to_mut_type_ref::(source_cap.get_tcb_ptr()); - return invoke_tcb_copy_registers(convert_to_mut_type_ref::(cap.get_tcb_ptr()), src_tcb, - flags & BIT!(CopyRegisters_suspendSource), flags & BIT!(CopyRegisters_resumeTarget), - flags & BIT!(CopyRegisters_transferFrame), flags & BIT!(CopyRegisters_transferInteger), 0) + return invoke_tcb_copy_registers( + convert_to_mut_type_ref::(cap.get_tcb_ptr()), + src_tcb, + flags & BIT!(CopyRegisters_suspendSource), + flags & BIT!(CopyRegisters_resumeTarget), + flags & BIT!(CopyRegisters_transferFrame), + flags & BIT!(CopyRegisters_transferInteger), + 0, + ); } -fn decode_tcb_configure(target_thread_cap: &cap_t, msg_length: usize, target_thread_slot: &mut cte_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { - if msg_length < 4 || get_extra_cap_by_index(0).is_none() || get_extra_cap_by_index(1).is_none() || get_extra_cap_by_index(2).is_none() { +fn decode_tcb_configure( + target_thread_cap: &cap_t, + msg_length: usize, + target_thread_slot: &mut cte_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { + if msg_length < 4 + || get_extra_cap_by_index(0).is_none() + || get_extra_cap_by_index(1).is_none() + || get_extra_cap_by_index(2).is_none() + { debug!("TCB CopyRegisters: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -199,11 +254,17 @@ fn decode_tcb_configure(target_thread_cap: &cap_t, msg_length: usize, target_thr let (buffer_slot, buffer_cap) = { let mut cap = get_extra_cap_by_index(2).unwrap().cap; - let mut buffer_slot_inner = if new_buffer_addr == 0 { None } else { get_extra_cap_by_index(2) }; + let mut buffer_slot_inner = if new_buffer_addr == 0 { + None + } else { + get_extra_cap_by_index(2) + }; if let Some(buffer_slot) = buffer_slot_inner.as_deref_mut() { let dc_ret = buffer_slot.derive_cap(&cap); if dc_ret.status != exception_t::EXCEPTION_NONE { - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return dc_ret.status; } cap = dc_ret.cap; @@ -216,9 +277,12 @@ fn decode_tcb_configure(target_thread_cap: &cap_t, msg_length: usize, target_thr }; let target_thread = convert_to_mut_type_ref::(target_thread_cap.get_tcb_ptr()); if target_thread.get_cspace(tcbCTable).is_long_running_delete() - || target_thread.get_cspace(tcbVTable).is_long_running_delete() { + || target_thread.get_cspace(tcbVTable).is_long_running_delete() + { debug!("TCB Configure: CSpace or VSpace currently being deleted."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -228,7 +292,9 @@ fn decode_tcb_configure(target_thread_cap: &cap_t, msg_length: usize, target_thr } if croot_cap.get_cap_type() != CapTag::CapCNodeCap { debug!("TCB Configure: CSpace cap is invalid."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -238,23 +304,41 @@ fn decode_tcb_configure(target_thread_cap: &cap_t, msg_length: usize, target_thr } if !is_valid_vtable_root(&vroot_cap) { debug!("TCB Configure: VSpace cap is invalid."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } - + set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - let status = invoke_tcb_set_space(target_thread, target_thread_slot, fault_ep, croot_cap, croot_slot, vroot_cap, vroot_slot); + let status = invoke_tcb_set_space( + target_thread, + target_thread_slot, + fault_ep, + croot_cap, + croot_slot, + vroot_cap, + vroot_slot, + ); if status != exception_t::EXCEPTION_NONE { return status; } - invoke_tcb_set_ipc_buffer(target_thread, target_thread_slot, new_buffer_addr, buffer_cap, buffer_slot) + invoke_tcb_set_ipc_buffer( + target_thread, + target_thread_slot, + new_buffer_addr, + buffer_cap, + buffer_slot, + ) } fn decode_set_priority(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { if length < 1 || get_extra_cap_by_index(0).is_none() { debug!("TCB SetPriority: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let new_prio = get_syscall_arg(0, buffer); @@ -273,17 +357,26 @@ fn decode_set_priority(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBuffe return status; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - invoke_tcb_set_priority(convert_to_mut_type_ref::(cap.get_tcb_ptr()), new_prio) + invoke_tcb_set_priority( + convert_to_mut_type_ref::(cap.get_tcb_ptr()), + new_prio, + ) } -fn decode_set_mc_priority(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_set_mc_priority( + cap: &cap_t, + length: usize, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if length < 1 || get_extra_cap_by_index(0).is_none() { debug!("TCB SetMCPPriority: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let new_mcp = get_syscall_arg(0, buffer); - let auth_cap= get_extra_cap_by_index(0).unwrap().cap; + let auth_cap = get_extra_cap_by_index(0).unwrap().cap; if auth_cap.get_cap_type() != CapTag::CapThreadCap { debug!("SetMCPriority: authority cap not a TCB."); unsafe { @@ -296,17 +389,26 @@ fn decode_set_mc_priority(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBu let auth_tcb = convert_to_mut_type_ref::(auth_cap.get_tcb_ptr()); let status = check_prio(new_mcp, auth_tcb); if status != exception_t::EXCEPTION_NONE { - debug!("TCB SetMCPriority: Requested maximum controlled priority {} too high (max {}).", new_mcp, auth_tcb.tcbMCP); + debug!( + "TCB SetMCPriority: Requested maximum controlled priority {} too high (max {}).", + new_mcp, auth_tcb.tcbMCP + ); return status; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); invoke_tcb_set_mcp(convert_to_mut_type_ref::(cap.get_tcb_ptr()), new_mcp) } -fn decode_set_sched_params(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_set_sched_params( + cap: &cap_t, + length: usize, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if length < 2 || get_extra_cap_by_index(0).is_some() { debug!("TCB SetSchedParams: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let new_mcp = get_syscall_arg(0, buffer); @@ -324,35 +426,52 @@ fn decode_set_sched_params(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCB let auth_tcb = convert_to_mut_type_ref::(auth_cap.get_tcb_ptr()); let status = check_prio(new_mcp, auth_tcb); if status != exception_t::EXCEPTION_NONE { - debug!("TCB SetSchedParams: Requested maximum controlled priority {} too high (max {}).", new_mcp, auth_tcb.tcbMCP); + debug!( + "TCB SetSchedParams: Requested maximum controlled priority {} too high (max {}).", + new_mcp, auth_tcb.tcbMCP + ); return status; } let status = check_prio(new_prio, auth_tcb); if status != exception_t::EXCEPTION_NONE { - debug!("TCB SetSchedParams: Requested priority {} too high (max {}).", new_prio, auth_tcb.tcbMCP); + debug!( + "TCB SetSchedParams: Requested priority {} too high (max {}).", + new_prio, auth_tcb.tcbMCP + ); return status; } - + set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); let target = convert_to_mut_type_ref::(cap.get_tcb_ptr()); invoke_tcb_set_mcp(target, new_mcp); invoke_tcb_set_priority(target, new_prio) } -fn decode_set_ipc_buffer(cap: &cap_t, length: usize, slot: &mut cte_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_set_ipc_buffer( + cap: &cap_t, + length: usize, + slot: &mut cte_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if length < 1 || get_extra_cap_by_index(0).is_none() { debug!("TCB SetIPCBuffer: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let buffer_addr = get_syscall_arg(0, buffer); - let (buffer_slot, buffer_cap) = if buffer_addr == 0 { (None, cap_t::new_null_cap()) } else { + let (buffer_slot, buffer_cap) = if buffer_addr == 0 { + (None, cap_t::new_null_cap()) + } else { let slot = get_extra_cap_by_index(0).unwrap(); let cap = slot.cap; let dc_ret = slot.derive_cap(&cap); if dc_ret.status != exception_t::EXCEPTION_NONE { - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return dc_ret.status; } let status = check_ipc_buffer_vaild(buffer_addr, &dc_ret.cap); @@ -363,13 +482,26 @@ fn decode_set_ipc_buffer(cap: &cap_t, length: usize, slot: &mut cte_t, buffer: O }; set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - invoke_tcb_set_ipc_buffer(convert_to_mut_type_ref::(cap.get_tcb_ptr()), slot, buffer_addr, buffer_cap, buffer_slot) + invoke_tcb_set_ipc_buffer( + convert_to_mut_type_ref::(cap.get_tcb_ptr()), + slot, + buffer_addr, + buffer_cap, + buffer_slot, + ) } -fn decode_set_space(cap: &cap_t, length: usize, slot: &mut cte_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +fn decode_set_space( + cap: &cap_t, + length: usize, + slot: &mut cte_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if length < 3 || get_extra_cap_by_index(0).is_none() || get_extra_cap_by_index(1).is_none() { debug!("TCB SetSpace: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let fault_ep = get_syscall_arg(0, buffer); @@ -381,9 +513,12 @@ fn decode_set_space(cap: &cap_t, length: usize, slot: &mut cte_t, buffer: Option let mut vroot_cap = vroot_slot.cap; let target_thread = convert_to_mut_type_ref::(cap.get_tcb_ptr()); if target_thread.get_cspace(tcbCTable).is_long_running_delete() - || target_thread.get_cspace(tcbVTable).is_long_running_delete() { + || target_thread.get_cspace(tcbVTable).is_long_running_delete() + { debug!("TCB Configure: CSpace or VSpace currently being deleted."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -393,7 +528,9 @@ fn decode_set_space(cap: &cap_t, length: usize, slot: &mut cte_t, buffer: Option } if croot_cap.get_cap_type() != CapTag::CapCNodeCap { debug!("TCB Configure: CSpace cap is invalid."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -403,32 +540,48 @@ fn decode_set_space(cap: &cap_t, length: usize, slot: &mut cte_t, buffer: Option } if !is_valid_vtable_root(&vroot_cap) { debug!("TCB Configure: VSpace cap is invalid."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - invoke_tcb_set_space(target_thread, slot, fault_ep, croot_cap, croot_slot, vroot_cap, vroot_slot) + invoke_tcb_set_space( + target_thread, + slot, + fault_ep, + croot_cap, + croot_slot, + vroot_cap, + vroot_slot, + ) } fn decode_bind_notification(cap: &cap_t) -> exception_t { if get_extra_cap_by_index(0).is_none() { debug!("TCB BindNotification: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let tcb = convert_to_mut_type_ref::(cap.get_tcb_ptr()); if tcb.tcbBoundNotification != 0 { debug!("TCB BindNotification: TCB already has a bound notification."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let ntfn_cap = get_extra_cap_by_index(0).unwrap().cap; if ntfn_cap.get_cap_type() != CapTag::CapNotificationCap { debug!("TCB BindNotification: Notification is invalid."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -436,13 +589,17 @@ fn decode_bind_notification(cap: &cap_t) -> exception_t { if ntfn_cap.get_nf_can_receive() == 0 { debug!("TCB BindNotification: Insufficient access rights"); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } if ntfn.get_queue_head() != 0 || ntfn.get_queue_tail() != 0 { debug!("TCB BindNotification: Notification cannot be bound."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -454,7 +611,9 @@ fn decode_unbind_notification(cap: &cap_t) -> exception_t { let tcb = convert_to_mut_type_ref::(cap.get_tcb_ptr()); if tcb.tcbBoundNotification == 0 { debug!("TCB BindNotification: TCB already has no bound Notification."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); @@ -467,14 +626,18 @@ fn decode_set_affinity(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBuffe if length < 1 { debug!("TCB SetAffinity: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let affinity = get_syscall_arg(0, buffer); if affinity > CONFIG_MAX_NUM_NODES { debug!("TCB SetAffinity: Requested CPU does not exist."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); @@ -485,7 +648,9 @@ fn decode_set_affinity(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBuffe fn decode_set_tls_base(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { if length < 1 { debug!("TCB SetTLSBase: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let base = get_syscall_arg(0, buffer); @@ -494,18 +659,22 @@ fn decode_set_tls_base(cap: &cap_t, length: usize, buffer: Option<&seL4_IPCBuffe } #[inline] -fn decode_set_space_args(root_data: usize, root_cap: cap_t, root_slot: &mut cte_t) -> Result { +fn decode_set_space_args( + root_data: usize, + root_cap: cap_t, + root_slot: &mut cte_t, +) -> Result { let mut ret_root_cap = root_cap; if root_data != 0 { ret_root_cap = root_cap.update_data(false, root_data); } let dc_ret = root_slot.derive_cap(&ret_root_cap); if dc_ret.status != exception_t::EXCEPTION_NONE { - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return Err(dc_ret.status); } ret_root_cap = dc_ret.cap; return Ok(ret_root_cap); } - - diff --git a/src/syscall/invocation/decode/decode_untyped_invocation.rs b/src/syscall/invocation/decode/decode_untyped_invocation.rs index 9103d6b..859698b 100644 --- a/src/syscall/invocation/decode/decode_untyped_invocation.rs +++ b/src/syscall/invocation/decode/decode_untyped_invocation.rs @@ -1,33 +1,52 @@ use crate::BIT; -use sel4_common::{message_info::MessageLabel, sel4_config::*, structures::*, object::ObjectType, utils::convert_to_mut_type_ref}; -use sel4_cspace::interface::{cap_t, cte_t, CapTag}; use log::debug; use sel4_common::fault::lookup_fault_t; -use sel4_task::{set_thread_state, get_currenct_thread, ThreadState}; - -use crate::{kernel::boot::{current_syscall_error, current_lookup_fault, get_extra_cap_by_index}, - syscall::{invocation::invoke_untyped::invoke_untyped_retype, get_syscall_arg, lookup_slot_for_cnode_op}, - config::CONFIG_RETYPE_FAN_OUT_LIMIT +use sel4_common::{ + message_info::MessageLabel, object::ObjectType, sel4_config::*, structures::*, + utils::convert_to_mut_type_ref, }; -use crate::syscall::{alignUp, FREE_INDEX_TO_OFFSET, GET_FREE_REF}; +use sel4_cspace::interface::{cap_t, cte_t, CapTag}; +use sel4_task::{get_currenct_thread, set_thread_state, ThreadState}; +use crate::syscall::{alignUp, FREE_INDEX_TO_OFFSET, GET_FREE_REF}; +use crate::{ + config::CONFIG_RETYPE_FAN_OUT_LIMIT, + kernel::boot::{current_lookup_fault, current_syscall_error, get_extra_cap_by_index}, + syscall::{ + get_syscall_arg, invocation::invoke_untyped::invoke_untyped_retype, + lookup_slot_for_cnode_op, + }, +}; -pub fn decode_untyed_invocation(inv_label: MessageLabel, length: usize, slot: &mut cte_t, cap: &cap_t, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +pub fn decode_untyed_invocation( + inv_label: MessageLabel, + length: usize, + slot: &mut cte_t, + cap: &cap_t, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if inv_label != MessageLabel::UntypedRetype { debug!("Untyped cap: Illegal operation attempted."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } if length < 6 || get_extra_cap_by_index(0).is_none() { debug!("Untyped invocation: Truncated message."); - unsafe { current_syscall_error._type = seL4_TruncatedMessage; } + unsafe { + current_syscall_error._type = seL4_TruncatedMessage; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } let op_new_type = ObjectType::from_usize(get_syscall_arg(0, buffer)); if op_new_type.is_none() { - debug!("Untyped Retype: Invalid object type. {}", get_syscall_arg(0, buffer)); + debug!( + "Untyped Retype: Invalid object type. {}", + get_syscall_arg(0, buffer) + ); unsafe { current_syscall_error._type = seL4_InvalidArgument; current_syscall_error.invalidArgumentNumber = 0; @@ -42,7 +61,10 @@ pub fn decode_untyed_invocation(inv_label: MessageLabel, length: usize, slot: &m let node_window = get_syscall_arg(5, buffer); let obj_size = new_type.get_object_size(user_obj_size); if user_obj_size >= wordBits || obj_size > seL4_MaxUntypedBits { - debug!("Untyped Retype: Invalid object size. {} : {}", user_obj_size, obj_size); + debug!( + "Untyped Retype: Invalid object size. {} : {}", + user_obj_size, obj_size + ); unsafe { current_syscall_error._type = seL4_RangeError; current_syscall_error.rangeErrorMin = 0; @@ -67,7 +89,7 @@ pub fn decode_untyed_invocation(inv_label: MessageLabel, length: usize, slot: &m } let status = slot.ensure_no_children(); - let (free_index, reset) = if status != exception_t::EXCEPTION_NONE { + let (free_index, reset) = if status != exception_t::EXCEPTION_NONE { // 原始 untype 有子节点 unsafe { current_syscall_error._type = seL4_RevokeFirst; @@ -81,8 +103,16 @@ pub fn decode_untyed_invocation(inv_label: MessageLabel, length: usize, slot: &m let untyped_free_bytes = BIT!(cap.get_untyped_block_size()) - FREE_INDEX_TO_OFFSET(free_index); if (untyped_free_bytes >> obj_size) < node_window { - debug!("Untyped Retype: Insufficient memory({} * {} bytes needed, {} bytes available)", node_window, - if obj_size >= wordBits { -1 } else { 1i64 << obj_size }, untyped_free_bytes); + debug!( + "Untyped Retype: Insufficient memory({} * {} bytes needed, {} bytes available)", + node_window, + if obj_size >= wordBits { + -1 + } else { + 1i64 << obj_size + }, + untyped_free_bytes + ); unsafe { current_syscall_error._type = seL4_NotEnoughMemory; current_syscall_error.memoryLeft = untyped_free_bytes; @@ -102,12 +132,19 @@ pub fn decode_untyed_invocation(inv_label: MessageLabel, length: usize, slot: &m let aligned_free_ref = alignUp(free_ref, obj_size); set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - invoke_untyped_retype(slot, reset, aligned_free_ref, new_type, user_obj_size, + invoke_untyped_retype( + slot, + reset, + aligned_free_ref, + new_type, + user_obj_size, convert_to_mut_type_ref::(node_cap.get_cnode_ptr()), - node_offset, node_window, device_mem as usize) + node_offset, + node_window, + device_mem as usize, + ) } - #[inline] fn check_object_type(new_type: ObjectType, user_obj_size: usize) -> exception_t { if new_type == ObjectType::CapTableObject && user_obj_size == 0 { @@ -157,12 +194,14 @@ fn get_target_cnode(node_index: usize, node_depth: usize, node_cap: &mut cap_t) exception_t::EXCEPTION_NONE } - #[inline] fn check_cnode_slot(node_cap: &cap_t, node_offset: usize, node_window: usize) -> exception_t { let node_size = 1 << node_cap.get_cnode_radix(); if node_offset > (node_size - 1) { - debug!("Untyped Retype: Destination node offset {} too large.", node_offset); + debug!( + "Untyped Retype: Destination node offset {} too large.", + node_offset + ); unsafe { current_syscall_error._type = seL4_RangeError; current_syscall_error.rangeErrorMin = 0; @@ -172,7 +211,10 @@ fn check_cnode_slot(node_cap: &cap_t, node_offset: usize, node_window: usize) -> } if node_window < 1 || node_window > CONFIG_RETYPE_FAN_OUT_LIMIT { - debug!("Untyped Retype: Number of requested objects {} too small or large.", node_window); + debug!( + "Untyped Retype: Number of requested objects {} too small or large.", + node_window + ); unsafe { current_syscall_error._type = seL4_RangeError; current_syscall_error.rangeErrorMin = 1; @@ -194,10 +236,15 @@ fn check_cnode_slot(node_cap: &cap_t, node_offset: usize, node_window: usize) -> let dest_cnode = convert_to_mut_type_ref::(node_cap.get_cnode_ptr()); for i in node_offset..(node_offset + node_window) { if dest_cnode.get_offset_slot(i).cap.get_cap_type() != CapTag::CapNullCap { - debug!("Untyped Retype: Slot {:#x} in destination window non-empty.", i); - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + debug!( + "Untyped Retype: Slot {:#x} in destination window non-empty.", + i + ); + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } } exception_t::EXCEPTION_NONE -} \ No newline at end of file +} diff --git a/src/syscall/invocation/decode/mod.rs b/src/syscall/invocation/decode/mod.rs index 269410e..6a5d7d4 100644 --- a/src/syscall/invocation/decode/mod.rs +++ b/src/syscall/invocation/decode/mod.rs @@ -1,36 +1,51 @@ -mod decode_tcb_invocation; -mod decode_domain_invocation; mod decode_cnode_invocation; -mod decode_untyped_invocation; -mod decode_mmu_invocation; +mod decode_domain_invocation; pub mod decode_irq_invocation; +mod decode_mmu_invocation; +mod decode_tcb_invocation; +mod decode_untyped_invocation; use core::intrinsics::unlikely; -use sel4_common::{structures::{exception_t, seL4_IPCBuffer}, sel4_config::seL4_InvalidCapability, utils::convert_to_mut_type_ref, message_info::MessageLabel}; -use sel4_cspace::interface::{cte_t, cap_t, CapTag}; -use sel4_ipc::{endpoint_t, notification_t, Transfer}; use log::debug; -use sel4_task::{set_thread_state, get_currenct_thread, ThreadState, tcb_t}; +use sel4_common::{ + message_info::MessageLabel, + sel4_config::seL4_InvalidCapability, + structures::{exception_t, seL4_IPCBuffer}, + utils::convert_to_mut_type_ref, +}; +use sel4_cspace::interface::{cap_t, cte_t, CapTag}; +use sel4_ipc::{endpoint_t, notification_t, Transfer}; +use sel4_task::{get_currenct_thread, set_thread_state, tcb_t, ThreadState}; use crate::kernel::boot::current_syscall_error; use crate::syscall::invocation::decode::decode_irq_invocation::decode_irq_handler_invocation; use self::{ - decode_tcb_invocation::decode_tcb_invocation, - decode_domain_invocation::decode_domain_invocation, decode_cnode_invocation::decode_cnode_invocation, - decode_untyped_invocation::decode_untyed_invocation, - decode_mmu_invocation::decode_mmu_invocation, + decode_domain_invocation::decode_domain_invocation, decode_irq_invocation::decode_irq_control_invocation, + decode_mmu_invocation::decode_mmu_invocation, decode_tcb_invocation::decode_tcb_invocation, + decode_untyped_invocation::decode_untyed_invocation, }; - -pub fn decode_invocation(label: MessageLabel, length: usize, slot: &mut cte_t, cap: &cap_t, cap_index: usize, - block: bool, call: bool, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +pub fn decode_invocation( + label: MessageLabel, + length: usize, + slot: &mut cte_t, + cap: &cap_t, + cap_index: usize, + block: bool, + call: bool, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { match cap.get_cap_type() { - CapTag::CapNullCap | CapTag::CapZombieCap => { - debug!("Attempted to invoke a null or zombie cap {:#x}, {:?}.", cap_index, cap.get_cap_type()); + CapTag::CapNullCap | CapTag::CapZombieCap => { + debug!( + "Attempted to invoke a null or zombie cap {:#x}, {:?}.", + cap_index, + cap.get_cap_type() + ); unsafe { current_syscall_error._type = seL4_InvalidCapability; current_syscall_error.invalidCapNumber = 0; @@ -40,7 +55,10 @@ pub fn decode_invocation(label: MessageLabel, length: usize, slot: &mut cte_t, c CapTag::CapEndpointCap => { if unlikely(cap.get_ep_can_send() == 0) { - debug!("Attempted to invoke a read-only endpoint cap {}.", cap_index); + debug!( + "Attempted to invoke a read-only endpoint cap {}.", + cap_index + ); unsafe { current_syscall_error._type = seL4_InvalidCapability; current_syscall_error.invalidCapNumber = 0; @@ -48,11 +66,14 @@ pub fn decode_invocation(label: MessageLabel, length: usize, slot: &mut cte_t, c return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - convert_to_mut_type_ref::(cap.get_ep_ptr()).send_ipc(get_currenct_thread(), - block, - call, cap.get_ep_can_grant() != 0, - cap.get_ep_badge(), - cap.get_ep_can_grant_reply() != 0); + convert_to_mut_type_ref::(cap.get_ep_ptr()).send_ipc( + get_currenct_thread(), + block, + call, + cap.get_ep_can_grant() != 0, + cap.get_ep_badge(), + cap.get_ep_can_grant_reply() != 0, + ); return exception_t::EXCEPTION_NONE; } @@ -69,7 +90,8 @@ pub fn decode_invocation(label: MessageLabel, length: usize, slot: &mut cte_t, c return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - convert_to_mut_type_ref::(cap.get_nf_ptr()).send_signal(cap.get_nf_badge()); + convert_to_mut_type_ref::(cap.get_nf_ptr()) + .send_signal(cap.get_nf_badge()); exception_t::EXCEPTION_NONE } @@ -86,7 +108,7 @@ pub fn decode_invocation(label: MessageLabel, length: usize, slot: &mut cte_t, c get_currenct_thread().do_reply( convert_to_mut_type_ref::(cap.get_reply_tcb_ptr()), slot, - cap.get_reply_can_grant() != 0 + cap.get_reply_can_grant() != 0, ); exception_t::EXCEPTION_NONE } @@ -96,7 +118,6 @@ pub fn decode_invocation(label: MessageLabel, length: usize, slot: &mut cte_t, c CapTag::CapUntypedCap => decode_untyed_invocation(label, length, slot, cap, buffer), CapTag::CapIrqControlCap => decode_irq_control_invocation(label, length, slot, buffer), CapTag::CapIrqHandlerCap => decode_irq_handler_invocation(label, cap.get_irq_handler()), - _ => decode_mmu_invocation(label, length, slot, call, buffer) + _ => decode_mmu_invocation(label, length, slot, call, buffer), } - -} \ No newline at end of file +} diff --git a/src/syscall/invocation/invoke_cnode.rs b/src/syscall/invocation/invoke_cnode.rs index d645003..592197e 100644 --- a/src/syscall/invocation/invoke_cnode.rs +++ b/src/syscall/invocation/invoke_cnode.rs @@ -1,13 +1,23 @@ -use sel4_common::{structures::exception_t, sel4_config::{tcbCaller, seL4_IllegalOperation, seL4_DeleteFirst}, utils::convert_to_mut_type_ref}; -use sel4_cspace::interface::{cte_t, CapTag, cte_move, cap_t, cte_insert, cte_swap, seL4_CapRights_t}; -use sel4_ipc::endpoint_t; use log::debug; +use sel4_common::{ + sel4_config::{seL4_DeleteFirst, seL4_IllegalOperation, tcbCaller}, + structures::exception_t, + utils::convert_to_mut_type_ref, +}; +use sel4_cspace::interface::{ + cap_t, cte_insert, cte_move, cte_swap, cte_t, seL4_CapRights_t, CapTag, +}; +use sel4_ipc::endpoint_t; use sel4_task::{get_currenct_thread, set_thread_state, ThreadState}; -use crate::{syscall::mask_cap_rights, kernel::boot::current_syscall_error}; +use crate::{kernel::boot::current_syscall_error, syscall::mask_cap_rights}; #[inline] -pub fn invoke_cnode_copy(src_slot: &mut cte_t, dest_slot: &mut cte_t, cap_right: seL4_CapRights_t) -> exception_t { +pub fn invoke_cnode_copy( + src_slot: &mut cte_t, + dest_slot: &mut cte_t, + cap_right: seL4_CapRights_t, +) -> exception_t { let src_cap = mask_cap_rights(cap_right, &src_slot.cap); let dc_ret = src_slot.derive_cap(&src_cap); if dc_ret.status != exception_t::EXCEPTION_NONE { @@ -16,7 +26,9 @@ pub fn invoke_cnode_copy(src_slot: &mut cte_t, dest_slot: &mut cte_t, cap_right: } if dc_ret.cap.get_cap_type() == CapTag::CapNullCap { debug!("CNode Copy:Copy cap would be invalid."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); @@ -26,7 +38,12 @@ pub fn invoke_cnode_copy(src_slot: &mut cte_t, dest_slot: &mut cte_t, cap_right: } #[inline] -pub fn invoke_cnode_mint(src_slot: &mut cte_t, dest_slot: &mut cte_t, cap_right: seL4_CapRights_t, cap_data: usize) -> exception_t { +pub fn invoke_cnode_mint( + src_slot: &mut cte_t, + dest_slot: &mut cte_t, + cap_right: seL4_CapRights_t, + cap_data: usize, +) -> exception_t { let src_cap = mask_cap_rights(cap_right, &src_slot.cap); let new_cap = src_cap.update_data(false, cap_data); let dc_ret = src_slot.derive_cap(&new_cap); @@ -36,7 +53,9 @@ pub fn invoke_cnode_mint(src_slot: &mut cte_t, dest_slot: &mut cte_t, cap_right: } if dc_ret.cap.get_cap_type() == CapTag::CapNullCap { debug!("CNode Mint:Mint cap would be invalid."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); @@ -46,11 +65,17 @@ pub fn invoke_cnode_mint(src_slot: &mut cte_t, dest_slot: &mut cte_t, cap_right: } #[inline] -pub fn invoke_cnode_mutate(src_slot: &mut cte_t, dest_slot: &mut cte_t, cap_data: usize) -> exception_t { +pub fn invoke_cnode_mutate( + src_slot: &mut cte_t, + dest_slot: &mut cte_t, + cap_data: usize, +) -> exception_t { let new_cap = src_slot.cap.update_data(true, cap_data); if new_cap.get_cap_type() == CapTag::CapNullCap { debug!("CNode Mint:Mint cap would be invalid."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); @@ -62,7 +87,9 @@ pub fn invoke_cnode_mutate(src_slot: &mut cte_t, dest_slot: &mut cte_t, cap_data pub fn invoke_cnode_save_caller(dest_slot: &mut cte_t) -> exception_t { if dest_slot.cap.get_cap_type() != CapTag::CapNullCap { debug!("CNode SaveCaller: Destination slot not empty."); - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); @@ -81,20 +108,29 @@ pub fn invoke_cnode_save_caller(dest_slot: &mut cte_t) -> exception_t { } #[inline] -pub fn invoke_cnode_rotate(slot1: &mut cte_t, slot2: &mut cte_t, slot3: &mut cte_t, - src_new_data: usize, pivot_new_data: usize) -> exception_t { +pub fn invoke_cnode_rotate( + slot1: &mut cte_t, + slot2: &mut cte_t, + slot3: &mut cte_t, + src_new_data: usize, + pivot_new_data: usize, +) -> exception_t { let new_src_cap = slot1.cap.update_data(true, src_new_data); let new_pivot_cap = slot2.cap.update_data(true, pivot_new_data); if new_src_cap.get_cap_type() == CapTag::CapNullCap { debug!("CNode Rotate: Source cap invalid"); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } if new_pivot_cap.get_cap_type() == CapTag::CapNullCap { debug!("CNode Rotate: Pivot cap invalid"); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -106,11 +142,10 @@ pub fn invoke_cnode_rotate(slot1: &mut cte_t, slot2: &mut cte_t, slot3: &mut cte cte_move(&new_pivot_cap, slot2, slot3); cte_move(&new_src_cap, slot1, slot2); } - + exception_t::EXCEPTION_NONE } - #[inline] pub fn invoke_cnode_move(src_slot: &mut cte_t, dest_slot: &mut cte_t) -> exception_t { let src_cap = src_slot.cap; @@ -131,7 +166,9 @@ pub fn invoke_cnode_cancel_badged_sends(dest_slot: &mut cte_t) -> exception_t { let dest_cap: cap_t = dest_slot.cap; if !hasCancelSendRight(&dest_cap) { debug!("CNode CancelBadgedSends: Target cap invalid."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); @@ -154,7 +191,6 @@ pub fn invoke_cnode_delete(dest_slot: &mut cte_t) -> exception_t { dest_slot.delete_all(true) } - fn hasCancelSendRight(cap: &cap_t) -> bool { match cap.get_cap_type() { CapTag::CapEndpointCap => { diff --git a/src/syscall/invocation/invoke_irq.rs b/src/syscall/invocation/invoke_irq.rs index c4561ec..3fa70f7 100644 --- a/src/syscall/invocation/invoke_irq.rs +++ b/src/syscall/invocation/invoke_irq.rs @@ -1,17 +1,18 @@ - - use sel4_common::structures::exception_t; -use sel4_cspace::interface::{cte_t, cte_insert, cap_t}; +use sel4_cspace::interface::{cap_t, cte_insert, cte_t}; -use crate::interrupt::{set_irq_state, IRQState, get_irq_handler_slot}; +use crate::interrupt::{get_irq_handler_slot, set_irq_state, IRQState}; -pub fn invoke_irq_control(irq: usize, handler_slot: &mut cte_t, control_slot: &mut cte_t) -> exception_t { +pub fn invoke_irq_control( + irq: usize, + handler_slot: &mut cte_t, + control_slot: &mut cte_t, +) -> exception_t { set_irq_state(IRQState::IRQSignal, irq); cte_insert(&cap_t::new_irq_handler_cap(irq), control_slot, handler_slot); exception_t::EXCEPTION_NONE } - #[inline] pub fn invoke_set_irq_handler(irq: usize, cap: &cap_t, slot: &mut cte_t) { let irq_slot = get_irq_handler_slot(irq); @@ -22,4 +23,4 @@ pub fn invoke_set_irq_handler(irq: usize, cap: &cap_t, slot: &mut cte_t) { #[inline] pub fn invoke_clear_irq_handler(irq: usize) { get_irq_handler_slot(irq).delete_one(); -} \ No newline at end of file +} diff --git a/src/syscall/invocation/invoke_mmu_op.rs b/src/syscall/invocation/invoke_mmu_op.rs index 36d96af..bbdd4de 100644 --- a/src/syscall/invocation/invoke_mmu_op.rs +++ b/src/syscall/invocation/invoke_mmu_op.rs @@ -1,12 +1,20 @@ -use sel4_common::{structures::exception_t, utils::{convert_to_mut_type_ref, pageBitsForSize}, sel4_config::*, message_info::seL4_MessageInfo_t}; -use sel4_common::MASK; use sel4_common::registers::msgInfoRegister; use sel4_common::utils::MAX_FREE_INDEX; -use sel4_cspace::interface::{cap_t, cte_t, seL4_CapRights_t, cte_insert}; +use sel4_common::MASK; +use sel4_common::{ + message_info::seL4_MessageInfo_t, + sel4_config::*, + structures::exception_t, + utils::{convert_to_mut_type_ref, pageBitsForSize}, +}; +use sel4_cspace::interface::{cap_t, cte_insert, cte_t, seL4_CapRights_t}; use sel4_task::{get_currenct_thread, set_thread_state, ThreadState}; -use sel4_vspace::{pte_t, sfence, pptr_to_paddr, unmapPage, vm_attributes_t, maskVMRights, pptr_t, set_asid_pool_by_index, asid_pool_t, copyGlobalMappings}; +use sel4_vspace::{ + asid_pool_t, copyGlobalMappings, maskVMRights, pptr_t, pptr_to_paddr, pte_t, + set_asid_pool_by_index, sfence, unmapPage, vm_attributes_t, +}; -use crate::{utils::clear_memory, config::badgeRegister, kernel::boot::current_lookup_fault}; +use crate::{config::badgeRegister, kernel::boot::current_lookup_fault, utils::clear_memory}; pub fn invoke_page_table_unmap(cap: &mut cap_t) -> exception_t { if cap.get_pt_is_mapped() != 0 { @@ -18,7 +26,12 @@ pub fn invoke_page_table_unmap(cap: &mut cap_t) -> exception_t { exception_t::EXCEPTION_NONE } -pub fn invoke_page_table_map(pt_cap: &mut cap_t, pt_slot: &mut pte_t, asid: usize, vaddr: usize) -> exception_t { +pub fn invoke_page_table_map( + pt_cap: &mut cap_t, + pt_slot: &mut pte_t, + asid: usize, + vaddr: usize, +) -> exception_t { let paddr = pptr_to_paddr(pt_cap.get_pt_base_ptr()); let pte = pte_t::new( paddr >> seL4_PageBits, @@ -40,13 +53,15 @@ pub fn invoke_page_table_map(pt_cap: &mut cap_t, pt_slot: &mut pte_t, asid: usiz exception_t::EXCEPTION_NONE } - pub fn invoke_page_get_address(vbase_ptr: usize, call: bool) -> exception_t { let thread = get_currenct_thread(); if call { thread.set_register(badgeRegister, 0); let length = thread.set_mr(0, vbase_ptr); - thread.set_register(msgInfoRegister, seL4_MessageInfo_t::new(0, 0, 0, length).to_word()); + thread.set_register( + msgInfoRegister, + seL4_MessageInfo_t::new(0, 0, 0, length).to_word(), + ); } set_thread_state(thread, ThreadState::ThreadStateRestart); exception_t::EXCEPTION_NONE @@ -54,11 +69,15 @@ pub fn invoke_page_get_address(vbase_ptr: usize, call: bool) -> exception_t { pub fn invoke_page_unmap(frame_slot: &mut cte_t) -> exception_t { if frame_slot.cap.get_pt_mapped_asid() != asidInvalid { - match unmapPage(frame_slot.cap.get_frame_size(), frame_slot.cap.get_frame_mapped_asid(), - frame_slot.cap.get_pt_mapped_address(), frame_slot.cap.get_frame_base_ptr()) { - Err(lookup_fault) => { - unsafe { current_lookup_fault = lookup_fault; } - } + match unmapPage( + frame_slot.cap.get_frame_size(), + frame_slot.cap.get_frame_mapped_asid(), + frame_slot.cap.get_pt_mapped_address(), + frame_slot.cap.get_frame_base_ptr(), + ) { + Err(lookup_fault) => unsafe { + current_lookup_fault = lookup_fault; + }, _ => {} } } @@ -67,8 +86,15 @@ pub fn invoke_page_unmap(frame_slot: &mut cte_t) -> exception_t { exception_t::EXCEPTION_NONE } -pub fn invoke_page_map(_frame_cap: &mut cap_t, w_rights_mask: usize, vaddr: usize, asid: usize, attr: vm_attributes_t, - pt_slot: &mut pte_t, frame_slot: &mut cte_t) -> exception_t { +pub fn invoke_page_map( + _frame_cap: &mut cap_t, + w_rights_mask: usize, + vaddr: usize, + asid: usize, + attr: vm_attributes_t, + pt_slot: &mut pte_t, + frame_slot: &mut cte_t, +) -> exception_t { let frame_vm_rights = frame_slot.cap.get_frame_vm_rights(); let vm_rights = maskVMRights(frame_vm_rights, seL4_CapRights_t::from_word(w_rights_mask)); let frame_addr = pptr_to_paddr(frame_slot.cap.get_frame_base_ptr()); @@ -81,16 +107,31 @@ pub fn invoke_page_map(_frame_cap: &mut cap_t, w_rights_mask: usize, vaddr: usiz exception_t::EXCEPTION_NONE } -pub fn invoke_asid_control(frame_ptr: pptr_t, slot: &mut cte_t, parent_slot: &mut cte_t, asid_base: usize) -> exception_t { - parent_slot.cap.set_untyped_free_index(MAX_FREE_INDEX(parent_slot.cap.get_untyped_block_size())); +pub fn invoke_asid_control( + frame_ptr: pptr_t, + slot: &mut cte_t, + parent_slot: &mut cte_t, + asid_base: usize, +) -> exception_t { + parent_slot + .cap + .set_untyped_free_index(MAX_FREE_INDEX(parent_slot.cap.get_untyped_block_size())); clear_memory(frame_ptr as *mut u8, pageBitsForSize(RISCV_4K_Page)); - cte_insert(&cap_t::new_asid_pool_cap(asid_base, frame_ptr), parent_slot, slot); + cte_insert( + &cap_t::new_asid_pool_cap(asid_base, frame_ptr), + parent_slot, + slot, + ); assert_eq!(asid_base & MASK!(asidLowBits), 0); set_asid_pool_by_index(asid_base >> asidLowBits, frame_ptr); exception_t::EXCEPTION_NONE } -pub fn invoke_asid_pool(asid: usize, pool: &mut asid_pool_t, vspace_slot: &mut cte_t) -> exception_t { +pub fn invoke_asid_pool( + asid: usize, + pool: &mut asid_pool_t, + vspace_slot: &mut cte_t, +) -> exception_t { let region_base = vspace_slot.cap.get_pt_base_ptr(); vspace_slot.cap.set_pt_is_mapped(1); vspace_slot.cap.set_pt_mapped_address(0); @@ -98,4 +139,4 @@ pub fn invoke_asid_pool(asid: usize, pool: &mut asid_pool_t, vspace_slot: &mut c copyGlobalMappings(region_base); pool.set_vspace_by_index(asid & MASK!(asidLowBits), region_base); exception_t::EXCEPTION_NONE -} \ No newline at end of file +} diff --git a/src/syscall/invocation/invoke_tcb.rs b/src/syscall/invocation/invoke_tcb.rs index cddee5c..027b7bb 100644 --- a/src/syscall/invocation/invoke_tcb.rs +++ b/src/syscall/invocation/invoke_tcb.rs @@ -1,15 +1,25 @@ -use sel4_common::{structures::{exception_t, seL4_IPCBuffer}, message_info::seL4_MessageInfo_t, sel4_config::{tcbCTable, tcbVTable, tcbBuffer}}; use sel4_common::registers::*; -use sel4_cspace::interface::{cap_t, cte_t, same_object_as, cte_insert}; +use sel4_common::{ + message_info::seL4_MessageInfo_t, + sel4_config::{tcbBuffer, tcbCTable, tcbVTable}, + structures::{exception_t, seL4_IPCBuffer}, +}; +use sel4_cspace::interface::{cap_t, cte_insert, cte_t, same_object_as}; use sel4_ipc::{notification_t, Transfer}; -use sel4_task::{tcb_t, get_currenct_thread, set_thread_state, ThreadState, rescheduleRequired}; +use sel4_task::{get_currenct_thread, rescheduleRequired, set_thread_state, tcb_t, ThreadState}; use crate::{ - config::{n_frameRegisters, frameRegisters, n_gpRegisters, gpRegisters}, - syscall::{utils::get_syscall_arg, do_bind_notification, safe_unbind_notification} + config::{frameRegisters, gpRegisters, n_frameRegisters, n_gpRegisters}, + syscall::{do_bind_notification, safe_unbind_notification, utils::get_syscall_arg}, }; -pub fn invoke_tcb_read_registers(src: &mut tcb_t, suspend_source: usize, n: usize, _arch: usize, call: bool) -> exception_t { +pub fn invoke_tcb_read_registers( + src: &mut tcb_t, + suspend_source: usize, + n: usize, + _arch: usize, + call: bool, +) -> exception_t { let thread = get_currenct_thread(); if suspend_source != 0 { // cancel_ipc(src); @@ -36,7 +46,10 @@ pub fn invoke_tcb_read_registers(src: &mut tcb_t, suspend_source: usize, n: usiz i = 0; while i < n_gpRegisters && i + n_frameRegisters < n && i + n_frameRegisters < n_msgRegisters { - thread.set_register(msgRegister[i + n_frameRegisters], src.get_register(gpRegisters[i])); + thread.set_register( + msgRegister[i + n_frameRegisters], + src.get_register(gpRegisters[i]), + ); i += 1; } @@ -46,13 +59,22 @@ pub fn invoke_tcb_read_registers(src: &mut tcb_t, suspend_source: usize, n: usiz i += 1; } } - thread.set_register(msgInfoRegister, seL4_MessageInfo_t::new(0, 0, 0, i + j).to_word()); + thread.set_register( + msgInfoRegister, + seL4_MessageInfo_t::new(0, 0, 0, i + j).to_word(), + ); } set_thread_state(thread, ThreadState::ThreadStateRunning); exception_t::EXCEPTION_NONE } -pub fn invoke_tcb_write_registers(dest: &mut tcb_t, resumeTarget: usize, mut n: usize, _arch: usize, buffer: Option<&seL4_IPCBuffer>) -> exception_t { +pub fn invoke_tcb_write_registers( + dest: &mut tcb_t, + resumeTarget: usize, + mut n: usize, + _arch: usize, + buffer: Option<&seL4_IPCBuffer>, +) -> exception_t { if n > n_frameRegisters + n_gpRegisters { n = n_frameRegisters + n_gpRegisters; } @@ -64,7 +86,10 @@ pub fn invoke_tcb_write_registers(dest: &mut tcb_t, resumeTarget: usize, mut n: } i = 0; while i < n_gpRegisters && i + n_frameRegisters < n { - dest.set_register(gpRegisters[i], get_syscall_arg(i + n_frameRegisters + 2, buffer)); + dest.set_register( + gpRegisters[i], + get_syscall_arg(i + n_frameRegisters + 2, buffer), + ); i += 1; } @@ -81,8 +106,15 @@ pub fn invoke_tcb_write_registers(dest: &mut tcb_t, resumeTarget: usize, mut n: exception_t::EXCEPTION_NONE } -pub fn invoke_tcb_copy_registers(dest: &mut tcb_t, src: &mut tcb_t, suspendSource: usize, resumeTarget: usize, transferFrame: usize, - _transferInteger: usize, _transferArch: usize) -> exception_t { +pub fn invoke_tcb_copy_registers( + dest: &mut tcb_t, + src: &mut tcb_t, + suspendSource: usize, + resumeTarget: usize, + transferFrame: usize, + _transferInteger: usize, + _transferArch: usize, +) -> exception_t { if suspendSource != 0 { // cancel_ipc(src); src.cancel_ipc(); @@ -132,8 +164,15 @@ pub fn invoke_tcb_set_priority(target: &mut tcb_t, prio: usize) -> exception_t { exception_t::EXCEPTION_NONE } -pub fn invoke_tcb_set_space(target: &mut tcb_t, slot: &mut cte_t, fault_ep: usize, - croot_new_cap: cap_t, croot_src_slot: &mut cte_t, vroot_new_cap: cap_t, vroot_src_slot: &mut cte_t) -> exception_t { +pub fn invoke_tcb_set_space( + target: &mut tcb_t, + slot: &mut cte_t, + fault_ep: usize, + croot_new_cap: cap_t, + croot_src_slot: &mut cte_t, + vroot_new_cap: cap_t, + vroot_src_slot: &mut cte_t, +) -> exception_t { let target_cap = cap_t::new_thread_cap(target.get_ptr()); target.tcbFaultHandler = fault_ep; let root_slot = target.get_cspace_mut_ref(tcbCTable); @@ -141,22 +180,30 @@ pub fn invoke_tcb_set_space(target: &mut tcb_t, slot: &mut cte_t, fault_ep: usiz if status != exception_t::EXCEPTION_NONE { return status; } - if same_object_as(&croot_new_cap, &croot_src_slot.cap) && same_object_as(&target_cap, &slot.cap) { + if same_object_as(&croot_new_cap, &croot_src_slot.cap) && same_object_as(&target_cap, &slot.cap) + { cte_insert(&croot_new_cap, croot_src_slot, root_slot); } - + let root_vslot = target.get_cspace_mut_ref(tcbVTable); let status = root_vslot.delete_all(true); if status != exception_t::EXCEPTION_NONE { return status; } - if same_object_as(&vroot_new_cap, &vroot_src_slot.cap) && same_object_as(&target_cap, &slot.cap) { + if same_object_as(&vroot_new_cap, &vroot_src_slot.cap) && same_object_as(&target_cap, &slot.cap) + { cte_insert(&vroot_new_cap, vroot_src_slot, root_vslot); } exception_t::EXCEPTION_NONE } -pub fn invoke_tcb_set_ipc_buffer(target: &mut tcb_t, slot: &mut cte_t, buffer_addr: usize, buffer_cap: cap_t, buffer_src_slot: Option<&mut cte_t>) -> exception_t { +pub fn invoke_tcb_set_ipc_buffer( + target: &mut tcb_t, + slot: &mut cte_t, + buffer_addr: usize, + buffer_cap: cap_t, + buffer_src_slot: Option<&mut cte_t>, +) -> exception_t { let target_cap = cap_t::new_thread_cap(target.get_ptr()); let buffer_slot = target.get_cspace_mut_ref(tcbBuffer); let status = buffer_slot.delete_all(true); @@ -164,8 +211,10 @@ pub fn invoke_tcb_set_ipc_buffer(target: &mut tcb_t, slot: &mut cte_t, buffer_ad return status; } target.tcbIPCBuffer = buffer_addr; - if let Some(buffer_src_slot) = buffer_src_slot { - if same_object_as(&buffer_cap, &buffer_src_slot.cap) && same_object_as(&target_cap, &slot.cap) { + if let Some(buffer_src_slot) = buffer_src_slot { + if same_object_as(&buffer_cap, &buffer_src_slot.cap) + && same_object_as(&target_cap, &slot.cap) + { cte_insert(&buffer_cap, buffer_src_slot, buffer_slot); } } @@ -190,7 +239,7 @@ pub fn invoke_tcb_unbind_notification(tcb: &mut tcb_t) -> exception_t { #[inline] pub fn invoke_tcb_set_tls_base(thread: &mut tcb_t, base: usize) -> exception_t { thread.set_register(TLS_BASE, base); - if thread.is_current(){ + if thread.is_current() { rescheduleRequired(); } exception_t::EXCEPTION_NONE @@ -200,7 +249,9 @@ pub fn invoke_tcb_set_tls_base(thread: &mut tcb_t, base: usize) -> exception_t { #[inline] pub fn invoke_tcb_set_affinity(thread: &mut tcb_t, affinitiy: usize) -> exception_t { thread.sched_dequeue(); - unsafe { crate::deps::migrateTCB(thread, affinitiy); } + unsafe { + crate::deps::migrateTCB(thread, affinitiy); + } // debug!("tcb migrate: {}", thread.tcbAffinity); if thread.is_runnable() { thread.sched_append(); @@ -211,4 +262,3 @@ pub fn invoke_tcb_set_affinity(thread: &mut tcb_t, affinitiy: usize) -> exceptio } exception_t::EXCEPTION_NONE } - diff --git a/src/syscall/invocation/invoke_untyped.rs b/src/syscall/invocation/invoke_untyped.rs index 54078cb..ec00063 100644 --- a/src/syscall/invocation/invoke_untyped.rs +++ b/src/syscall/invocation/invoke_untyped.rs @@ -1,57 +1,77 @@ -use sel4_common::{object::ObjectType, utils::convert_to_mut_type_ref, sel4_config::*, structures::exception_t, ROUND_DOWN, BIT}; use crate::deps::tcbDebugAppend; -use sel4_task::{tcb_t, get_current_domain}; -use sel4_vspace::{pptr_t, VMReadWrite}; +use crate::syscall::{ + FREE_INDEX_TO_OFFSET, GET_FREE_INDEX, GET_OFFSET_FREE_PTR, OFFSET_TO_FREE_IDNEX, +}; +use sel4_common::{ + object::ObjectType, sel4_config::*, structures::exception_t, utils::convert_to_mut_type_ref, + BIT, ROUND_DOWN, +}; use sel4_cspace::interface::{cap_t, cte_t, insert_new_cap}; -use crate::syscall::{FREE_INDEX_TO_OFFSET, GET_FREE_INDEX, GET_OFFSET_FREE_PTR, OFFSET_TO_FREE_IDNEX}; +use sel4_task::{get_current_domain, tcb_t}; +use sel4_vspace::{pptr_t, VMReadWrite}; use crate::utils::*; - -fn create_new_objects(obj_type: ObjectType, parent: &mut cte_t, dest_cnode: &mut cte_t, dest_offset: usize, - dest_length: usize, region_base: usize, user_size: usize, device_mem: usize) { +fn create_new_objects( + obj_type: ObjectType, + parent: &mut cte_t, + dest_cnode: &mut cte_t, + dest_offset: usize, + dest_length: usize, + region_base: usize, + user_size: usize, + device_mem: usize, +) { // debug!("create_new_object: {:?}", obj_type); let object_size = obj_type.get_object_size(user_size); for i in 0..dest_length { - let cap = create_object(obj_type, region_base + (i << object_size), user_size, device_mem); + let cap = create_object( + obj_type, + region_base + (i << object_size), + user_size, + device_mem, + ); insert_new_cap(parent, dest_cnode.get_offset_slot(dest_offset + i), &cap); } } -fn create_object(obj_type: ObjectType, region_base: pptr_t, user_size: usize, device_mem: usize) -> cap_t { +fn create_object( + obj_type: ObjectType, + region_base: pptr_t, + user_size: usize, + device_mem: usize, +) -> cap_t { match obj_type { ObjectType::TCBObject => { let tcb = convert_to_mut_type_ref::(region_base + TCB_OFFSET); tcb.init(); tcb.tcbTimeSlice = CONFIG_TIME_SLICE; tcb.domain = get_current_domain(); - unsafe { tcbDebugAppend(tcb as *mut tcb_t); } + unsafe { + tcbDebugAppend(tcb as *mut tcb_t); + } return cap_t::new_thread_cap(tcb.get_ptr()); } - ObjectType::EndpointObject => { - cap_t::new_endpoint_cap(0, 1, 1, 1, 1, region_base) - } + ObjectType::EndpointObject => cap_t::new_endpoint_cap(0, 1, 1, 1, 1, region_base), - ObjectType::NotificationObject => { - cap_t::new_notification_cap(0, 1, 1, region_base) - } + ObjectType::NotificationObject => cap_t::new_notification_cap(0, 1, 1, region_base), - ObjectType::CapTableObject => { - cap_t::new_cnode_cap(user_size, 0, 0, region_base) - } + ObjectType::CapTableObject => cap_t::new_cnode_cap(user_size, 0, 0, region_base), - ObjectType::UnytpedObject => { - cap_t::new_untyped_cap(0, device_mem, user_size, region_base) - } + ObjectType::UnytpedObject => cap_t::new_untyped_cap(0, device_mem, user_size, region_base), - ObjectType::PageTableObject => { - cap_t::new_page_table_cap(asidInvalid, region_base, 0, 0) - } + ObjectType::PageTableObject => cap_t::new_page_table_cap(asidInvalid, region_base, 0, 0), ObjectType::NormalPageObject | ObjectType::GigaPageObject | ObjectType::MegaPageObject => { - cap_t::new_frame_cap(asidInvalid, region_base, obj_type.get_frame_type(), - VMReadWrite, device_mem as usize, 0) + cap_t::new_frame_cap( + asidInvalid, + region_base, + obj_type.get_frame_type(), + VMReadWrite, + device_mem as usize, + 0, + ) } } } @@ -86,10 +106,17 @@ pub fn reset_untyped_cap(srcSlot: &mut cte_t) -> exception_t { exception_t::EXCEPTION_NONE } - -pub fn invoke_untyped_retype(src_slot: &mut cte_t, reset: bool, retype_base: pptr_t, - new_type: ObjectType, user_size: usize, dest_cnode: &mut cte_t, - dest_offset: usize, dest_length: usize, device_mem: usize) -> exception_t { +pub fn invoke_untyped_retype( + src_slot: &mut cte_t, + reset: bool, + retype_base: pptr_t, + new_type: ObjectType, + user_size: usize, + dest_cnode: &mut cte_t, + dest_offset: usize, + dest_length: usize, + device_mem: usize, +) -> exception_t { let region_base = src_slot.cap.get_untyped_ptr(); if reset { let status = reset_untyped_cap(src_slot); @@ -100,8 +127,18 @@ pub fn invoke_untyped_retype(src_slot: &mut cte_t, reset: bool, retype_base: ppt let total_object_size = dest_length << new_type.get_object_size(user_size); let free_ref = retype_base + total_object_size; - src_slot.cap.set_untyped_free_index(GET_FREE_INDEX(region_base, free_ref)); - create_new_objects(new_type, src_slot, dest_cnode, dest_offset, dest_length, - retype_base, user_size, device_mem); + src_slot + .cap + .set_untyped_free_index(GET_FREE_INDEX(region_base, free_ref)); + create_new_objects( + new_type, + src_slot, + dest_cnode, + dest_offset, + dest_length, + retype_base, + user_size, + device_mem, + ); exception_t::EXCEPTION_NONE } diff --git a/src/syscall/invocation/mod.rs b/src/syscall/invocation/mod.rs index d380b1c..7298599 100644 --- a/src/syscall/invocation/mod.rs +++ b/src/syscall/invocation/mod.rs @@ -1,21 +1,21 @@ pub mod decode; -mod invoke_tcb; mod invoke_cnode; -mod invoke_untyped; -mod invoke_mmu_op; pub mod invoke_irq; +mod invoke_mmu_op; +mod invoke_tcb; +mod invoke_untyped; use core::intrinsics::unlikely; -use sel4_common::{structures::exception_t, message_info::seL4_MessageInfo_t, fault::seL4_Fault_t}; use log::debug; use sel4_common::registers::{capRegister, msgInfoRegister, n_msgRegisters}; -use sel4_task::{get_currenct_thread, ThreadState, set_thread_state}; +use sel4_common::{fault::seL4_Fault_t, message_info::seL4_MessageInfo_t, structures::exception_t}; +use sel4_task::{get_currenct_thread, set_thread_state, ThreadState}; use crate::kernel::boot::current_fault; use crate::syscall::invocation::decode::decode_invocation; -use crate::syscall::{handle_fault, lookup_extra_caps_with_buf}; use crate::syscall::syscall_reply::{reply_error_from_kernel, reply_success_from_kernel}; +use crate::syscall::{handle_fault, lookup_extra_caps_with_buf}; #[no_mangle] pub fn handleInvocation(isCall: bool, isBlocking: bool) -> exception_t { @@ -49,16 +49,16 @@ pub fn handleInvocation(isCall: bool, isBlocking: bool) -> exception_t { length = n_msgRegisters; } - let cap = unsafe {(*(lu_ret.slot)).cap}; + let cap = unsafe { (*(lu_ret.slot)).cap }; let status = decode_invocation( info.get_label(), length, - unsafe {&mut *lu_ret.slot }, + unsafe { &mut *lu_ret.slot }, &cap, cptr, isBlocking, isCall, - buffer + buffer, ); if status == exception_t::EXCEPTION_PREEMTED { return status; @@ -78,4 +78,4 @@ pub fn handleInvocation(isCall: bool, isBlocking: bool) -> exception_t { set_thread_state(thread, ThreadState::ThreadStateRunning); } return exception_t::EXCEPTION_NONE; -} \ No newline at end of file +} diff --git a/src/syscall/mod.rs b/src/syscall/mod.rs index 1b1b7f4..72be763 100644 --- a/src/syscall/mod.rs +++ b/src/syscall/mod.rs @@ -1,9 +1,9 @@ -pub mod utils; pub mod invocation; pub mod syscall_reply; +pub mod utils; use core::intrinsics::unlikely; -use sel4_common::fault::{FaultType, lookup_fault_t, seL4_Fault_t}; +use sel4_common::fault::{lookup_fault_t, seL4_Fault_t, FaultType}; use sel4_common::registers::capRegister; use sel4_common::sel4_config::tcbCaller; @@ -15,22 +15,24 @@ pub const SysRecv: isize = -5; pub const SysReply: isize = -6; pub const SysYield: isize = -7; pub const SysNBRecv: isize = -8; +use crate::deps::handleUnknownSyscall; use sel4_common::structures::exception_t; use sel4_common::utils::convert_to_mut_type_ref; use sel4_cspace::interface::CapTag; -use crate::deps::handleUnknownSyscall; -use sel4_task::{schedule, activateThread, tcb_t, set_thread_state, ThreadState, get_currenct_thread, rescheduleRequired}; use sel4_ipc::{endpoint_t, notification_t, Transfer}; +use sel4_task::{ + activateThread, get_currenct_thread, rescheduleRequired, schedule, set_thread_state, tcb_t, + ThreadState, +}; pub use utils::*; -use crate::{kernel::c_traps::restore_user_context, config::irqInvalid, interrupt::getActiveIRQ}; +use crate::arch::restore_user_context; use crate::interrupt::handler::handleInterrupt; use crate::kernel::boot::{current_fault, current_lookup_fault}; +use crate::{config::irqInvalid, interrupt::getActiveIRQ}; use self::invocation::handleInvocation; - - #[no_mangle] pub fn slowpath(syscall: usize) { // debug!("enter slow path: {}", syscall as isize); @@ -44,7 +46,6 @@ pub fn slowpath(syscall: usize) { restore_user_context(); } - #[no_mangle] pub fn handleSyscall(_syscall: usize) -> exception_t { let syscall: isize = _syscall as isize; @@ -101,13 +102,15 @@ fn send_fault_ipc(thread: &mut tcb_t) -> exception_t { let origin_lookup_fault = unsafe { current_lookup_fault }; let lu_ret = thread.lookup_slot(thread.tcbFaultHandler); if lu_ret.status != exception_t::EXCEPTION_NONE { - unsafe { current_fault = seL4_Fault_t::new_cap_fault(thread.tcbFaultHandler, 0); } + unsafe { + current_fault = seL4_Fault_t::new_cap_fault(thread.tcbFaultHandler, 0); + } return exception_t::EXCEPTION_FAULT; } let handler_cap = &unsafe { (*lu_ret.slot).cap }; if handler_cap.get_cap_type() == CapTag::CapEndpointCap - && (handler_cap.get_ep_can_grant() != 0 - || handler_cap.get_ep_can_grant_reply() != 0) { + && (handler_cap.get_ep_can_grant() != 0 || handler_cap.get_ep_can_grant_reply() != 0) + { thread.tcbFault = unsafe { current_fault }; if thread.tcbFault.get_fault_type() == FaultType::CapFault { thread.tcbLookupFailure = origin_lookup_fault; @@ -156,7 +159,9 @@ fn handle_recv(block: bool) { let ep_cptr = current_thread.get_register(capRegister); let lu_ret = current_thread.lookup_slot(ep_cptr); if lu_ret.status != exception_t::EXCEPTION_NONE { - unsafe { current_fault = seL4_Fault_t::new_cap_fault(ep_cptr, 1); } + unsafe { + current_fault = seL4_Fault_t::new_cap_fault(ep_cptr, 1); + } return handle_fault(current_thread); } let ipc_cap = unsafe { (*lu_ret.slot).cap }; @@ -173,21 +178,24 @@ fn handle_recv(block: bool) { convert_to_mut_type_ref::(ipc_cap.get_ep_ptr()).receive_ipc( current_thread, block, - ipc_cap.get_ep_can_grant() != 0 + ipc_cap.get_ep_can_grant() != 0, ); } CapTag::CapNotificationCap => { let ntfn = convert_to_mut_type_ref::(ipc_cap.get_nf_ptr()); let bound_tcb_ptr = ntfn.get_bound_tcb(); - if unlikely(ipc_cap.get_nf_can_receive() == 0 || (bound_tcb_ptr != 0 && bound_tcb_ptr != current_thread.get_ptr())) { + if unlikely( + ipc_cap.get_nf_can_receive() == 0 + || (bound_tcb_ptr != 0 && bound_tcb_ptr != current_thread.get_ptr()), + ) { unsafe { current_lookup_fault = lookup_fault_t::new_missing_cap(0); current_fault = seL4_Fault_t::new_cap_fault(ep_cptr, 1); } return handle_fault(current_thread); } - return ntfn.receive_signal(current_thread, block) + return ntfn.receive_signal(current_thread, block); } _ => { unsafe { @@ -203,4 +211,4 @@ fn handle_yield() { get_currenct_thread().sched_dequeue(); get_currenct_thread().sched_append(); rescheduleRequired(); -} \ No newline at end of file +} diff --git a/src/syscall/syscall_reply.rs b/src/syscall/syscall_reply.rs index 584fbf4..115e4d8 100644 --- a/src/syscall/syscall_reply.rs +++ b/src/syscall/syscall_reply.rs @@ -1,23 +1,29 @@ +use crate::config::badgeRegister; +use crate::kernel::boot::{current_lookup_fault, current_syscall_error}; use sel4_common::message_info::seL4_MessageInfo_t; use sel4_common::registers::msgInfoRegister; -use sel4_task::tcb_t; use sel4_common::sel4_config::*; -use crate::config::badgeRegister; -use crate::kernel::boot::{current_lookup_fault, current_syscall_error}; +use sel4_task::tcb_t; #[inline] pub fn reply_error_from_kernel(thread: &mut tcb_t) { thread.set_register(badgeRegister, 0); unsafe { let len = set_mrs_for_syscall_error(thread); - thread.set_register(msgInfoRegister, seL4_MessageInfo_t::new(current_syscall_error._type, 0, 0, len).to_word()); + thread.set_register( + msgInfoRegister, + seL4_MessageInfo_t::new(current_syscall_error._type, 0, 0, len).to_word(), + ); } } #[inline] pub fn reply_success_from_kernel(thread: &mut tcb_t) { thread.set_register(badgeRegister, 0); - thread.set_register(msgInfoRegister, seL4_MessageInfo_t::new(0, 0, 0, 0).to_word()); + thread.set_register( + msgInfoRegister, + seL4_MessageInfo_t::new(0, 0, 0, 0).to_word(), + ); } pub unsafe fn set_mrs_for_syscall_error(thread: &mut tcb_t) -> usize { @@ -34,10 +40,10 @@ pub unsafe fn set_mrs_for_syscall_error(thread: &mut tcb_t) -> usize { return thread.set_lookup_fault_mrs(1, ¤t_lookup_fault); } seL4_IllegalOperation - | seL4_AlignmentError - | seL4_TruncatedMessage - | seL4_DeleteFirst - | seL4_RevokeFirst => 0, + | seL4_AlignmentError + | seL4_TruncatedMessage + | seL4_DeleteFirst + | seL4_RevokeFirst => 0, seL4_NotEnoughMemory => thread.set_mr(0, current_syscall_error.memoryLeft), _ => { panic!("invalid syscall error") diff --git a/src/syscall/utils.rs b/src/syscall/utils.rs index a6999ad..977d570 100644 --- a/src/syscall/utils.rs +++ b/src/syscall/utils.rs @@ -1,18 +1,29 @@ use core::intrinsics::unlikely; -use crate::{config::seL4_MinPrio, kernel::boot::{current_syscall_error, current_lookup_fault}, MASK, BIT, IS_ALIGNED}; -use sel4_common::{sel4_config::{seL4_IPCBufferSizeBits, seL4_AlignmentError, seL4_FailedLookup, wordBits, seL4_DeleteFirst}, utils::convert_to_mut_type_ref}; -use sel4_common::{structures::{seL4_IPCBuffer, exception_t}, sel4_config::*}; +use crate::kernel::boot::{current_extra_caps, current_fault}; +use crate::{ + config::seL4_MinPrio, + kernel::boot::{current_lookup_fault, current_syscall_error}, + BIT, IS_ALIGNED, MASK, +}; +use log::debug; use sel4_common::fault::*; +use sel4_common::registers::{msgRegister, n_msgRegisters}; use sel4_common::sel4_config::seL4_MinUntypedBits; -use sel4_cspace::interface::{cap_t, CapTag, resolve_address_bits, cte_t, seL4_CapRights_t}; +use sel4_common::{ + sel4_config::*, + structures::{exception_t, seL4_IPCBuffer}, +}; +use sel4_common::{ + sel4_config::{ + seL4_AlignmentError, seL4_DeleteFirst, seL4_FailedLookup, seL4_IPCBufferSizeBits, wordBits, + }, + utils::convert_to_mut_type_ref, +}; +use sel4_cspace::interface::{cap_t, cte_t, resolve_address_bits, seL4_CapRights_t, CapTag}; use sel4_ipc::notification_t; -use log::debug; -use sel4_common::registers::{msgRegister, n_msgRegisters}; use sel4_task::{get_currenct_thread, lookupSlot_ret_t, tcb_t}; use sel4_vspace::maskVMRights; -use crate::kernel::boot::{current_extra_caps, current_fault}; - pub fn alignUp(baseValue: usize, alignment: usize) -> usize { (baseValue + BIT!(alignment) - 1) & !MASK!(alignment) @@ -34,8 +45,6 @@ pub fn OFFSET_TO_FREE_IDNEX(offset: usize) -> usize { offset >> seL4_MinUntypedBits } - - #[inline] #[no_mangle] pub fn getSyscallArg(i: usize, ipc_buffer: *const usize) -> usize { @@ -47,7 +56,7 @@ pub fn getSyscallArg(i: usize, ipc_buffer: *const usize) -> usize { assert_ne!(ipc_buffer as usize, 0); let ptr = ipc_buffer.add(i + 1); *ptr - } + }; } } @@ -55,11 +64,11 @@ pub fn getSyscallArg(i: usize, ipc_buffer: *const usize) -> usize { pub fn lookup_extra_caps_with_buf(thread: &tcb_t, buf: Option<&seL4_IPCBuffer>) -> exception_t { unsafe { match thread.lookup_extra_caps_with_buf(&mut current_extra_caps.excaprefs, buf) { - Ok(()) =>{}, + Ok(()) => {} Err(fault) => { current_fault = fault; return exception_t::EXCEPTION_LOOKUP_FAULT; - }, + } } } return exception_t::EXCEPTION_NONE; @@ -90,19 +99,25 @@ pub fn check_prio(prio: usize, auth_tcb: &tcb_t) -> exception_t { pub fn check_ipc_buffer_vaild(vptr: usize, cap: &cap_t) -> exception_t { if cap.get_cap_type() != CapTag::CapFrameCap { debug!("Requested IPC Buffer is not a frame cap."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } if cap.get_frame_is_device() != 0 { debug!("Specifying a device frame as an IPC buffer is not permitted."); - unsafe { current_syscall_error._type = seL4_IllegalOperation; } + unsafe { + current_syscall_error._type = seL4_IllegalOperation; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } if !IS_ALIGNED!(vptr, seL4_IPCBufferSizeBits) { debug!("Requested IPC Buffer location 0x%x is not aligned."); - unsafe { current_syscall_error._type = seL4_AlignmentError; } + unsafe { + current_syscall_error._type = seL4_AlignmentError; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } exception_t::EXCEPTION_NONE @@ -133,13 +148,17 @@ pub fn is_valid_vtable_root(cap: &cap_t) -> bool { cap.get_cap_type() == CapTag::CapPageTableCap && cap.get_pt_is_mapped() != 0 } - #[no_mangle] pub fn isValidVTableRoot(_cap: &cap_t) -> bool { panic!("should not be invoked!") } -pub fn lookup_slot_for_cnode_op(is_source: bool, root: &cap_t, cap_ptr: usize, depth: usize) -> lookupSlot_ret_t { +pub fn lookup_slot_for_cnode_op( + is_source: bool, + root: &cap_t, + cap_ptr: usize, + depth: usize, +) -> lookupSlot_ret_t { let mut ret: lookupSlot_ret_t = lookupSlot_ret_t::default(); if unlikely(root.get_cap_type() != CapTag::CapCNodeCap) { unsafe { @@ -186,7 +205,7 @@ pub fn lookup_slot_for_cnode_op(is_source: bool, root: &cap_t, cap_ptr: usize, d } pub fn lookupSlotForCNodeOp( -isSource: bool, + isSource: bool, root: &cap_t, capptr: usize, depth: usize, @@ -197,7 +216,9 @@ isSource: bool, #[inline] pub fn ensure_empty_slot(slot: &cte_t) -> exception_t { if slot.cap.get_cap_type() != CapTag::CapNullCap { - unsafe { current_syscall_error._type = seL4_DeleteFirst; } + unsafe { + current_syscall_error._type = seL4_DeleteFirst; + } return exception_t::EXCEPTION_SYSCALL_ERROR; } exception_t::EXCEPTION_NONE @@ -205,9 +226,7 @@ pub fn ensure_empty_slot(slot: &cte_t) -> exception_t { #[no_mangle] pub fn ensureEmptySlot(slot: *mut cte_t) -> exception_t { - unsafe { - ensure_empty_slot(&*slot) - } + unsafe { ensure_empty_slot(&*slot) } } pub fn mask_cap_rights(rights: seL4_CapRights_t, cap: &cap_t) -> cap_t { @@ -217,7 +236,9 @@ pub fn mask_cap_rights(rights: seL4_CapRights_t, cap: &cap_t) -> cap_t { new_cap.set_ep_can_send(cap.get_ep_can_send() & rights.get_allow_write()); new_cap.set_ep_can_receive(cap.get_ep_can_receive() & rights.get_allow_read()); new_cap.set_ep_can_grant(cap.get_ep_can_grant() & rights.get_allow_grant()); - new_cap.set_ep_can_grant_reply(cap.get_ep_can_grant_reply() & rights.get_allow_grant_reply()); + new_cap.set_ep_can_grant_reply( + cap.get_ep_can_grant_reply() & rights.get_allow_grant_reply(), + ); } CapTag::CapNotificationCap => { new_cap.set_nf_can_send(cap.get_nf_can_send() & rights.get_allow_write()); @@ -234,4 +255,4 @@ pub fn mask_cap_rights(rights: seL4_CapRights_t, cap: &cap_t) -> cap_t { _ => {} } new_cap -} \ No newline at end of file +} diff --git a/src/utils.rs b/src/utils.rs index 7b2de03..138973d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,9 +1,8 @@ use crate::BIT; - #[inline] pub fn clear_memory(ptr: *mut u8, bits: usize) { unsafe { core::slice::from_raw_parts_mut(ptr, BIT!(bits)).fill(0); } -} \ No newline at end of file +}