VERY SAFE TESTING and also runner is not needed for now

This commit is contained in:
Bryan McShea
2024-02-22 23:24:06 -05:00
parent df670e9998
commit d95a73203b
9 changed files with 152 additions and 163 deletions

View File

@@ -3,7 +3,7 @@ use core::{arch::asm, ops::Range};
use crate::{
arch::{asm, csr, interrupts, paging, wait},
fdt::FDT,
main,
start,
};
#[no_mangle]
@@ -70,5 +70,5 @@ pub unsafe fn init() -> ! {
end: raw_mem_range.end(),
};
to_supervisor();
main(heap_mem, fdt);
start(heap_mem, fdt)
}

View File

@@ -8,5 +8,5 @@ pub fn init() {
pub fn stuff() -> ! {
let mcause = csr::mcause::read();
crate::println!("interrupt triggered: {mcause:?}");
super::qemu::exit();
super::qemu::exit(1);
}

View File

@@ -1,6 +1,7 @@
use core::fmt::{self, Write};
use crate::util::mutex::Mutex;
use core::arch::asm;
// --machine sifive_u
// const UART_BASE: u32 = 0x10010000;
@@ -28,22 +29,27 @@ impl fmt::Write for Uart {
}
}
pub fn exit() -> ! {
pub fn exit(code: usize) -> ! {
let data = [0x20026, code];
unsafe {
core::arch::asm!(
"li t0, 0x20026",
"sw t0, 0(sp)",
"move a1, sp",
"li a0, 0x18",
".balign 16",
".option push",
".option norvc",
"slli zero, zero, 0x1f",
"ebreak",
"srai zero, zero, 0x7",
options(noreturn)
);
semihost(0x18, data.as_ptr() as *const u8);
}
super::wait()
}
unsafe fn semihost(call: usize, data: *const u8) {
asm!(
"mv a0, {call}",
"mv a1, {data}",
".balign 16",
".option push",
".option norvc",
"slli zero, zero, 0x1f",
"ebreak",
"srai zero, zero, 0x7",
call = in(reg) call,
data = in(reg) data
)
}
pub fn _print(args: core::fmt::Arguments<'_>) {

View File

@@ -3,12 +3,13 @@
#![feature(abi_x86_interrupt)]
#![feature(naked_functions)]
#![feature(fn_align)]
use core::ops::Range;
use fdt::FDT;
#![feature(custom_test_frameworks)]
#![test_runner(crate::test::test_runner)]
#![reexport_test_harness_main = "test_main"]
use crate::allocator::ALLOCATOR;
use core::ops::Range;
use fdt::FDT;
extern crate alloc;
@@ -19,9 +20,19 @@ pub mod fdt;
pub mod heap;
pub mod log;
pub mod qemu;
#[cfg(test)]
mod test;
pub mod util;
pub fn main(heap_mem: Range<*mut u8>, fdt: FDT) -> ! {
pub fn start(heap_mem: Range<*mut u8>, fdt: FDT) -> ! {
#[cfg(test)]
test_main();
#[cfg(not(test))]
main(heap_mem, fdt);
qemu::exit(0)
}
pub fn main(heap_mem: Range<*mut u8>, fdt: FDT) {
println!("we out here vibin");
println!("memory range: {:?}", fdt.mem_range());
for node in &fdt {}
@@ -42,8 +53,8 @@ pub fn main(heap_mem: Range<*mut u8>, fdt: FDT) -> ! {
println!("----------- vec vec test:");
let mut test = alloc::vec::Vec::new();
for i in 0..4 {
let n = i*4;
test.push(alloc::vec![n, n+1, n+2, n+3]);
let n = i * 4;
test.push(alloc::vec![n, n + 1, n + 2, n + 3]);
}
ALLOCATOR.print();
println!("{:?}", test);
@@ -57,11 +68,17 @@ pub fn main(heap_mem: Range<*mut u8>, fdt: FDT) -> ! {
// for _ in 0..40000000 {}
// let x = unsafe { *(0x10000000000 as *mut u8) };
// println!("we got {x}");
qemu::exit();
}
#[panic_handler]
pub fn panic(info: &core::panic::PanicInfo) -> ! {
println!("{}", info);
qemu::exit()
#[cfg(test)]
crate::test::test_panic(info);
#[cfg(not(test))]
main_panic(info);
}
pub fn main_panic(info: &core::panic::PanicInfo) -> ! {
println!("{}", info);
qemu::exit(1);
}

69
kernel/src/test.rs Normal file
View File

@@ -0,0 +1,69 @@
use crate::{print, println, qemu};
// SURELY I will not have threading tests that cause problems
// SURELY I don't need to pin the input to test_runner
static mut TESTS: &[&dyn Testable] = &[];
static mut TEST: usize = 0;
static mut FAILED: usize = 0;
pub trait Testable {
fn run(&self) -> ();
}
impl<T: Fn()> Testable for T {
fn run(&self) {
print!("test {}... ", core::any::type_name::<T>());
self();
println!("\x1b[92mok\x1b[0m");
}
}
pub fn test_runner(tests: &[&dyn Testable]) {
unsafe { TESTS = core::mem::transmute(tests) };
println!("Running {} tests", tests.len());
run_tests();
}
pub fn run_tests() -> ! {
unsafe {
for i in TEST..TESTS.len() {
let test = TESTS[i];
TEST += 1;
test.run();
}
print!(
"results: {}. {} passed; {} failed",
if FAILED > 0 {
"\x1b[91mFAILED\x1b[0m"
} else {
"\x1b[92mok\x1b[0m"
},
TEST - FAILED,
FAILED
);
}
println!();
qemu::exit(0)
}
pub fn test_panic(info: &core::panic::PanicInfo) -> ! {
println!("\x1b[91mFAILED\x1b[0m");
println!("\x1b[93m{}\x1b[0m", info);
unsafe { FAILED += 1 };
run_tests();
}
#[test_case]
fn test1() {
assert_eq!(1, 1);
}
#[test_case]
fn test2() {
assert_eq!(1, 0);
}
#[test_case]
fn test3() {
assert_eq!(5, 4);
}