Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[unstable]
build-std-features = ["compiler-builtins-mem"]
build-std = ["core", "compiler_builtins"]
profile-rustflags = true

[build]
target = "arch/riscv32imc-unknown-none-elf.json"
Expand All @@ -18,27 +19,38 @@ rustflags = [
# Print memory usage
"-C",
"link-arg=--print-memory-usage",
# Generate a memory map at compilation time
# "-C",
# "link-arg=-Map=memory.map",
# "link-arg=-Map=logs/memory.map",
]

[profile.dev]
opt-level = 0
debug = 2
debug-assertions = true
debug-assertions = true
lto = false
strip = false
codegen-units = 1
panic = "unwind"
panic = "abort"
rustflags = [
# Improve backtrace
"-C",
"force-frame-pointers=yes",
]

[profile.test]
opt-level = 0
debug = 2
debug-assertions = true
debug-assertions = true
lto = false
strip = false
codegen-units = 1
panic = "unwind"
panic = "abort"
rustflags = [
# Improve backtrace
"-C",
"force-frame-pointers=yes",
]

[profile.release]
opt-level = 2
Expand All @@ -47,13 +59,13 @@ strip = true
codegen-units = 1
panic = "abort"
debug = false
debug-assertions = false
debug-assertions = false

[alias]
b = "build --profile=dev -Zjson-target-spec --target arch/riscv32imc-unknown-none-elf.json"
rb = "build --profile=release -Zjson-target-spec --target arch/riscv32imc-unknown-none-elf.json"
tb = "build --profile=test --features=test -Zjson-target-spec --target arch/riscv32imc-unknown-none-elf.json"
b = "build --profile=dev -Zjson-target-spec"
rb = "build --profile=release -Zjson-target-spec"
tb = "build --profile=test --features=test -Zjson-target-spec"
c = "clean"
ft = "fmt --all -- --check"
w = "clippy -Zjson-target-spec --target arch/riscv32imc-unknown-none-elf.json -- -D warnings"
clip = "clippy -Zjson-target-spec --target arch/riscv32imc-unknown-none-elf.json"
w = "clippy -Zjson-target-spec -- -D warnings"
clip = "clippy -Zjson-target-spec"
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lrnrtos"
version = "0.4.1"
version = "0.4.2"
edition = "2024"

[[bin]]
Expand Down
2 changes: 0 additions & 2 deletions Documentation/kernel/test_framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ pub fn foo_test_suite() {
)],
// Name of the test suite
name: "Foo",
// Number of tests inside the suite
tests_nb: 1,
// Behavior of the test suite
// Default: run the suite normally
// Skipped: don't run the suite at all
Expand Down
6 changes: 6 additions & 0 deletions src/arch/riscv32/mem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::asm;

pub fn update_kernel_sp(sp: usize) {
unsafe { core::arch::asm!("mv a0, {}", in(reg) sp) };
unsafe { asm::set_kernel_sp() };
}
1 change: 1 addition & 0 deletions src/arch/riscv32/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod asm;
pub mod mem;
pub mod scheduler;
pub mod start;
pub mod task;
Expand Down
4 changes: 2 additions & 2 deletions src/arch/riscv32/scheduler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::primitives::stack::AlignedStack;
use crate::primitives::stack::AlignedStack16;

#[repr(C)]
pub struct SchedulerCtx {
Expand All @@ -10,7 +10,7 @@ pub struct SchedulerCtx {
sp: *mut u8, // Offset 132
}

pub static mut SCHEDULER_STACK: AlignedStack<4098> = AlignedStack::new();
pub static mut SCHEDULER_STACK: AlignedStack16<4098> = AlignedStack16::new();
pub static mut SCHEDULER_CTX: SchedulerCtx = unsafe { core::mem::zeroed() };

impl SchedulerCtx {
Expand Down
6 changes: 4 additions & 2 deletions src/arch/riscv32/traps/trap_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Tests files:

use core::{mem, ptr::null_mut};

use crate::primitives::stack::AlignedStack16;

#[repr(C)]
// Trap frame structure, used to store all global registers, give a stack for the trap handling
// to avoid using the kernel stack.
Expand Down Expand Up @@ -45,7 +47,7 @@ impl TrapFrame {
}

// Static buffer used as a stack for trap handling
pub static mut TRAP_STACK_BUFF: [u8; 1024] = [0u8; 1024];
pub static mut TRAP_STACK_BUFF: AlignedStack16<1024> = AlignedStack16::new();

// Init TrapFrame with 0 in mem
pub static mut KERNEL_TRAP_FRAME: TrapFrame = unsafe { mem::zeroed() };
Expand All @@ -55,6 +57,6 @@ pub fn init_trap_frame() {
// Static mut safe because it's only used in kernel boot
#[allow(static_mut_refs)]
unsafe {
KERNEL_TRAP_FRAME.trap_stack = TRAP_STACK_BUFF.as_mut_ptr()
KERNEL_TRAP_FRAME.trap_stack = TRAP_STACK_BUFF.buf.as_mut_ptr().wrapping_add(1024);
}
}
4 changes: 2 additions & 2 deletions src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
ktime::set_ktime_seconds,
log,
logs::LogLevel,
mem::{memory_init, update_kernel_sp},
mem::{mem_update_kernel_sp, memory_init},
platform::platform_init,
};

Expand All @@ -34,7 +34,7 @@ pub fn kernel_early_boot(core: usize, dtb_addr: usize) -> ! {
);
memory_init();
log!(LogLevel::Debug, "Switch to new kernel stack...");
update_kernel_sp();
mem_update_kernel_sp();
// Allow empty loop because it will never enter, just to make the fn never return without
// warning
#[allow(clippy::empty_loop)]
Expand Down
2 changes: 1 addition & 1 deletion src/info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// The kernel exposes build-time version information at runtime.
// This information is immutable and reflects the exact binary currently running.
// It is intended for debugging, logging, and diagnostic purposes.
pub static KERNEL_VERSION: &str = "0.4.1";
pub static KERNEL_VERSION: &str = "0.4.2";
16 changes: 8 additions & 8 deletions src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ Tests files:

mod kernel;

use core::{arch::asm, mem};
use core::mem;

use kernel::{__kernel_end, __kernel_start, KernelStack};

use crate::{
arch, config::KERNEL_STACK_SIZE, log, logs::LogLevel, platform::mem::platform_init_mem,
arch::mem::update_kernel_sp, config::KERNEL_STACK_SIZE, log, logs::LogLevel,
platform::mem::platform_init_mem,
};

pub struct Memory {
Expand Down Expand Up @@ -137,19 +138,18 @@ pub fn memory_init() {
}
}

#[unsafe(no_mangle)]
pub fn update_kernel_sp() {
unsafe { asm!("mv a0, {}", in(reg) MEMORY.kernel_stack.top) };
unsafe { arch::asm::set_kernel_sp() };
}

pub fn mem_kernel_stack_info<'a>() -> &'a KernelStack {
#[allow(static_mut_refs)]
unsafe {
&MEMORY.kernel_stack
}
}

pub fn mem_update_kernel_sp() {
let sp: usize = unsafe { MEMORY.kernel_stack.top };
update_kernel_sp(sp);
}

/// Return the hi and lo address of the RAM
/// first index is hi, second is lo
pub fn mem_reg_info() -> [usize; 2] {
Expand Down
7 changes: 4 additions & 3 deletions src/primitives/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ Tests files:
References:
*/

pub struct AlignedStack<const N: usize> {
#[repr(align(16))]
pub struct AlignedStack16<const N: usize> {
pub buf: [u8; N],
}

impl<const N: usize> AlignedStack<N> {
impl<const N: usize> AlignedStack16<N> {
// Don't bother with this warning
#[allow(clippy::new_without_default)]
pub const fn new() -> Self {
AlignedStack { buf: [0u8; N] }
AlignedStack16 { buf: [0u8; N] }
}
}
3 changes: 1 addition & 2 deletions src/tests/arch/riscv32/task/task_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,10 @@ pub fn task_context_test_suite() {
TestCase::init(
"Task context switch no invariants violated",
test_task_context_switch,
TestBehavior::Default,
TestBehavior::Skipped,
),
],
name: "RISC-V32 bit task context layout",
tests_nb: 3,
behavior: TestSuiteBehavior::Default,
};
#[allow(static_mut_refs)]
Expand Down
1 change: 0 additions & 1 deletion src/tests/arch/riscv32/traps/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub fn trap_handler_test_suite() {
TestBehavior::Default,
)],
name: "Trap handler",
tests_nb: 1,
behavior: TestSuiteBehavior::Default,
};
#[allow(static_mut_refs)]
Expand Down
1 change: 0 additions & 1 deletion src/tests/arch/riscv32/traps/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ pub fn interrupt_enabling_test_suite() {
// },
],
name: "Interruptions enabling",
tests_nb: 6,
behavior: TestSuiteBehavior::Default,
};
#[allow(static_mut_refs)]
Expand Down
5 changes: 3 additions & 2 deletions src/tests/arch/riscv32/traps/trap_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub fn test_trap_frame_init() -> u8 {
init_trap_frame();
// Ok because test env, no concurrency
#[allow(static_mut_refs)]
if unsafe { KERNEL_TRAP_FRAME.trap_stack } != unsafe { TRAP_STACK_BUFF.as_mut_ptr() } {
if unsafe { KERNEL_TRAP_FRAME.trap_stack }
!= unsafe { TRAP_STACK_BUFF.buf.as_mut_ptr().wrapping_add(1024) }
{
panic!("Trap frame trap_stack field should be initialized with ptr to TRAP_STACK_BUFF");
}
0
Expand All @@ -59,7 +61,6 @@ pub fn trap_frame_test_suite() {
),
],
name: "Trap frame",
tests_nb: 2,
behavior: TestSuiteBehavior::Default,
};
#[allow(static_mut_refs)]
Expand Down
1 change: 0 additions & 1 deletion src/tests/drivers/cpu_intc/subsystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ pub fn cpu_intc_subsystem_test_suite() {
),
],
name: "CPU interrupt-controller",
tests_nb: 3,
behavior: TestSuiteBehavior::Default,
};
#[allow(static_mut_refs)]
Expand Down
1 change: 0 additions & 1 deletion src/tests/drivers/serials/ns16550a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub fn ns16550_test_suite() {
TestBehavior::Default,
)],
name: "Ns16550",
tests_nb: 1,
behavior: TestSuiteBehavior::Skipped,
};
#[allow(static_mut_refs)]
Expand Down
1 change: 0 additions & 1 deletion src/tests/drivers/serials/subsystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ pub fn serial_subsystem_test_suite() {
),
],
name: "Serial sub-system",
tests_nb: 3,
behavior: TestSuiteBehavior::Default,
};
#[allow(static_mut_refs)]
Expand Down
1 change: 0 additions & 1 deletion src/tests/drivers/timer/subsystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ pub fn timer_subsystem_test_suite() {
),
],
name: "Timer sub-system",
tests_nb: 4,
behavior: TestSuiteBehavior::Default,
};

Expand Down
1 change: 0 additions & 1 deletion src/tests/ktime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ pub fn ktime_test_suite() {
),
],
name: "Ktime",
tests_nb: 3,
behavior: TestSuiteBehavior::Default,
};
#[allow(static_mut_refs)]
Expand Down
1 change: 0 additions & 1 deletion src/tests/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ pub fn memory_test_suite() {
),
],
name: "Kernel memory",
tests_nb: 2,
behavior: TestSuiteBehavior::Default,
};
#[allow(static_mut_refs)]
Expand Down
18 changes: 5 additions & 13 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ impl<'a> TestManager<'a> {
pub struct TestSuite<'a> {
pub tests: &'a [TestCase<'a>],
pub name: &'a str,
pub tests_nb: u32,
pub behavior: TestSuiteBehavior,
}

Expand All @@ -103,21 +102,14 @@ impl<'a> TestSuite<'a> {
TestSuite {
tests: &[],
name: "",
tests_nb: 0,
behavior: TestSuiteBehavior::Skipped,
}
}

pub fn init(
tests: &'a [TestCase],
name: &'a str,
tests_nb: u32,
behavior: TestSuiteBehavior,
) -> Self {
pub fn init(tests: &'a [TestCase], name: &'a str, behavior: TestSuiteBehavior) -> Self {
TestSuite {
tests,
name,
tests_nb,
behavior,
}
}
Expand Down Expand Up @@ -155,7 +147,6 @@ impl<'a> TestCase<'a> {

pub static mut TEST_MANAGER: TestManager = TestManager::init();

#[unsafe(no_mangle)]
pub fn test_runner(core: usize, dtb_addr: usize) -> ! {
// Basic test before running all test suites
kprint!("Starting kernel in test mode.\n");
Expand All @@ -182,7 +173,8 @@ pub fn test_runner(core: usize, dtb_addr: usize) -> ! {
let mut test_suites_skipped: usize = 0;
// Iterate over all test suite and run all test inside
for test_suite in unsafe { TEST_MANAGER.test_pool } {
if test_suite.tests_nb == 0 {
let test_nb = test_suite.tests.len();
if test_nb == 0 {
continue;
}
if test_suite.behavior == TestSuiteBehavior::Skipped {
Expand All @@ -191,10 +183,10 @@ pub fn test_runner(core: usize, dtb_addr: usize) -> ! {
}
kprint_fmt!(
"\nRunning {} tests from test suite: {}\n",
test_suite.tests_nb,
test_nb,
test_suite.name
);
let test_to_pass = test_suite.tests_nb;
let test_to_pass = test_nb;
let mut test_passed: usize = 0;
let mut test_failed: usize = 0;
let mut test_skipped: usize = 0;
Expand Down
Loading