diff --git a/Cargo.toml b/Cargo.toml index 7fcaa17..5e7189b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ test-timeout = 30 run-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio", "-accel", "kvm"] [dependencies] -bootloader= { version = "0.9.23", features = ["map_physical_memory"] } -volatile="0.2.6" +bootloader = { version = "0.9.23", features = ["map_physical_memory"] } +volatile = "0.2.6" spin = "0.5.2" x86_64 = "0.14.2" uart_16550 = "0.2.0" @@ -56,10 +56,3 @@ version = "0.3.4" default-features = false features = ["alloc"] -[[test]] -name = "should_panic" -harness = false - -[[test]] -name = "stack_overflow" -harness = false diff --git a/Dockerfile b/Dockerfile index 3aae11e..f4e293d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,4 +21,4 @@ COPY . . RUN cargo update -p proc-macro2 -CMD ["cargo", "build", "--release", "--verbose"] +CMD ["cargo", "build", "--release;", "cargo", "test"] diff --git a/src/lib.rs b/src/lib.rs index edee1dd..f1e7cc2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,4 @@ - #![no_std] - #![cfg_attr(test, no_main)] #![feature(custom_test_frameworks)] #![test_runner(crate::test_runner)] @@ -11,23 +9,42 @@ #![feature(async_closure)] #![feature(global_asm)] + +use alloc::string::String; +use alloc::vec; +use bootloader::{entry_point, BootInfo}; use core::panic::PanicInfo; +extern crate alloc; pub mod system; pub mod user; - pub use system::std as std; pub use user::bin::*; - -extern crate alloc; -//extern crate fatfs; - -use bootloader::{entry_point, BootInfo}; +use crate::calc::Calculator; +use crate::std::application::Application; -#[cfg(test)] -entry_point!(test_kernel_main); +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u32)] +pub enum QemuExitCode { + Ok = 0x10, + Err = 0x11, +} + +pub fn poweroff() { + exit(QemuExitCode::Ok); +} + +pub fn exit(code: QemuExitCode) { + use x86_64::instructions::port::Port; + + unsafe { + let mut port = Port::new(0xf4); + port.write(code as u32); + } + println!("e"); +} #[alloc_error_handler] fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { @@ -44,6 +61,17 @@ pub fn hlt() -> ! { } } + +#[cfg(test)] +entry_point!(test_kernel_main); + +#[cfg(test)] +fn test_kernel_main(boot_info: &'static BootInfo) -> ! { + system::init(boot_info); + test_main(); + hlt(); +} + pub trait Testable { fn run(&self) -> (); } @@ -71,12 +99,6 @@ pub fn test_panic_handler(info: &PanicInfo) -> ! { hlt(); } -#[cfg(test)] -fn test_kernel_main(_boot_info: &'static BootInfo) -> ! { - start(); - test_main(); - hlt(); -} #[cfg(test)] #[panic_handler] @@ -84,23 +106,10 @@ fn panic(info: &PanicInfo) -> ! { test_panic_handler(info) } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[repr(u32)] -pub enum QemuExitCode { - Ok = 0x10, - Err = 0x11, -} -pub fn poweroff() { - exit(QemuExitCode::Ok); -} -pub fn exit(code: QemuExitCode) { - use x86_64::instructions::port::Port; - - unsafe { - let mut port = Port::new(0xf4); - port.write(code as u32); - } - println!("e"); +#[cfg(test)] +#[test_case] +fn trivial_assertion() { + assert_eq!(1, 1); } diff --git a/src/main.rs b/src/main.rs index 6f3de3a..4dfea64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,11 +25,15 @@ fn panic(info: &PanicInfo) -> ! { CrystalOS::test_panic_handler(info) } + entry_point!(main); fn main(boot_info: &'static BootInfo) -> ! { CrystalOS::start(boot_info); + #[cfg(test)] + test_main(); + // runs the 'mainloop' of the OS; let mut executor = Executor::new(); executor.spawn(Task::new(shell::command_handler())); @@ -37,8 +41,8 @@ fn main(boot_info: &'static BootInfo) -> ! { executor.try_run(); } - #[cfg(test)] - test_main(); + loop {} } + diff --git a/tests/allocation.rs b/tests/allocation.rs deleted file mode 100644 index 8513163..0000000 --- a/tests/allocation.rs +++ /dev/null @@ -1,69 +0,0 @@ - -#![no_std] -#![no_main] -#![feature(custom_test_frameworks)] -#![test_runner(CrystalOS::test_runner)] -#![reexport_test_harness_main = "test_main"] - -extern crate alloc; - -use bootloader::{entry_point, BootInfo}; -use core::panic::PanicInfo; - -use alloc::{ boxed::Box, vec::Vec }; - - -entry_point!(main); - -fn main(boot_info: &'static BootInfo) -> ! { - use CrystalOS::kernel::allocator; - use CrystalOS::kernel::memory::{self, BootInfoFrameAllocator}; - use x86_64::VirtAddr; - - CrystalOS::start(); - - let physical_memory_offset = VirtAddr::new(boot_info.physical_memory_offset); - let mut mapper = unsafe { memory::init(physical_memory_offset)}; - let mut frame_allocator = unsafe { - BootInfoFrameAllocator::init(&boot_info.memory_map) - }; - allocator::init_heap(&mut mapper, &mut frame_allocator).expect("failed to initialise heap"); - - test_main(); - - loop {} -} - -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - CrystalOS::test_panic_handler(info) -} - -#[test_case] -fn box_allocation() { - let heap_1 = Box::new(69); - let heap_2 = Box::new(420); - assert_eq!(*heap_1, 69); - assert_eq!(*heap_2, 420); -} - -#[test_case] -fn vec_allocation() { - let x = 1000; - let mut vector = Vec::new(); - for i in 0..x { - vector.push(i); - } - assert_eq!(vector.iter().sum::(), (x-1) * x/2); - -} - -#[test_case] -fn reallocation() { - use CrystalOS::kernel::allocator::HEAP_SIZE; - - for i in 0..HEAP_SIZE { - let x = Box::new(i); - assert_eq!(*x, i); - } -} diff --git a/tests/should_panic.rs b/tests/should_panic.rs deleted file mode 100644 index 3dfaec8..0000000 --- a/tests/should_panic.rs +++ /dev/null @@ -1,29 +0,0 @@ - -#![no_std] -#![no_main] - - -use core::panic::PanicInfo; -use CrystalOS::{QemuExitCode, exit, serial_println, serial_print}; - -#[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - serial_println!("OK"); - exit(QemuExitCode::Ok); - - loop {} -} - - -#[no_mangle] -pub extern "C" fn _start() -> ! { - shouldpanic(); - serial_println!("Err: Test did not panic"); - exit(QemuExitCode::Err); - loop {} -} - -fn shouldpanic() { - serial_print!("{}...\t", "should_panic::should_panic"); - assert_eq!(1,2); -} diff --git a/tests/stack_overflow.rs b/tests/stack_overflow.rs deleted file mode 100644 index 0b70491..0000000 --- a/tests/stack_overflow.rs +++ /dev/null @@ -1,57 +0,0 @@ - -#![no_std] -#![no_main] -#![feature(abi_x86_interrupt)] - -use core::panic::PanicInfo; -use lazy_static::lazy_static; -use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; -use CrystalOS::{exit, QemuExitCode, serial_println, serial_print}; - - -#[no_mangle] -pub extern "C" fn _start() -> ! { - serial_print!("stack_overflow::stack_overflow...\t"); - - CrystalOS::kernel::gdt::init(); - init_test_idt(); - - stack_overflow(); - - panic!("stack overflow did not stop execution!"); -} - -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - CrystalOS::test_panic_handler(info); -} - -#[allow(unconditional_recursion)] -fn stack_overflow() { - stack_overflow(); - volatile::Volatile::new(0).read(); -} - -lazy_static! { - static ref TEST_IDT: InterruptDescriptorTable = { - let mut idt = InterruptDescriptorTable::new(); - unsafe { - idt.double_fault.set_handler_fn(test_double_fault_handler).set_stack_index(CrystalOS::kernel::gdt::DOUBLE_FAULT_IST_INDEX); - } - idt - }; -} - -extern "x86-interrupt" fn test_double_fault_handler( - _stack_frame: InterruptStackFrame, - _error_code: u64, -) -> ! { - serial_println!("OK"); - exit(QemuExitCode::Ok); - loop {} -} - - -pub fn init_test_idt() { - TEST_IDT.load(); -} diff --git a/tests/startup.rs b/tests/startup.rs deleted file mode 100644 index cfa0a90..0000000 --- a/tests/startup.rs +++ /dev/null @@ -1,26 +0,0 @@ - -#![no_std] -#![no_main] -#![feature(custom_test_frameworks)] -#![test_runner(CrystalOS::test_runner)] -#![reexport_test_harness_main = "test_main"] - -use core::panic::PanicInfo; -use CrystalOS::println; - -#[no_mangle] -pub extern "C" fn _start() -> ! { - test_main(); - - loop {} -} - -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - CrystalOS::test_panic_handler(info) -} - -#[test_case] -fn test_println() { - println!("testing println output"); -}