From b17a0a41bea7789178919f4a0999ea8519e1f897 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sat, 9 Nov 2019 04:44:04 +0100 Subject: Introduce integration test crate --- kernel/Cargo.toml | 4 +- kernel/run | 16 ++-- kernel/src/asm/boot.asm | 172 ++++++++++++++++++++++++++++++++++++ kernel/src/asm/long_mode_init.asm | 19 ++++ kernel/src/asm/multiboot_header.asm | 15 ++++ kernel/src/boot.asm | 172 ------------------------------------ kernel/src/lib.rs | 36 +------- kernel/src/long_mode_init.asm | 19 ---- kernel/src/main.rs | 58 ++++++++++++ kernel/src/multiboot_header.asm | 15 ---- kernel/tests/test01.rs | 25 ++++++ 11 files changed, 306 insertions(+), 245 deletions(-) create mode 100644 kernel/src/asm/boot.asm create mode 100644 kernel/src/asm/long_mode_init.asm create mode 100644 kernel/src/asm/multiboot_header.asm delete mode 100644 kernel/src/boot.asm delete mode 100644 kernel/src/long_mode_init.asm create mode 100644 kernel/src/main.rs delete mode 100644 kernel/src/multiboot_header.asm create mode 100644 kernel/tests/test01.rs diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 9ac2848..a2ce08c 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -5,8 +5,8 @@ authors = ["Dennis Kobert ", "Jan Zwerschke"] edition = "2018" -[lib] -crate-type = ["staticlib"] +# [lib] +# crate-type = ["staticlib"] [package.metadata.cargo-xbuild] sysroot_path = "target/sysroot" diff --git a/kernel/run b/kernel/run index 1353d92..f9c8df3 100755 --- a/kernel/run +++ b/kernel/run @@ -12,6 +12,7 @@ build_mode=release action=help target=x86_64 test_mode=false +serial_mode=false function define_vars() { target_name="$target-$name" target_path="target/" @@ -20,7 +21,7 @@ function define_vars() { iso_path="$target_path/iso/" obj_path="$iso_path/obj/" src_path="src/" - asm_path="$src_path/" + asm_path="$src_path/asm/" link_script="$src_path/linker.ld" grub_cfg="$src_path/grub.cfg" } @@ -33,6 +34,7 @@ print_help() { echo " -mode= set build mode (standard: $build_mode)" echo " -target= set target (standard: $target)" echo " -test enable test mode (standard: disabled)" + echo " -serial enable serial mode (standard: disabled)" echo echo "actions:" echo " build build the iso" @@ -90,7 +92,10 @@ run() { "x86_64") qemu="qemu-system-x86_64 -cdrom $iso_path/$name.iso" if $test_mode; then - qemu="$qemu -device isa-debug-exit,iobase=0xf4,iosize=0x04 -serial stdio -display none" + qemu="$qemu -device isa-debug-exit,iobase=0xf4,iosize=0x04 -serial stdio" + if $serial_mode; then + qemu="$qemu -display none" + fi fi $qemu case "$?" in @@ -115,14 +120,15 @@ for arg in "$@"; do target="$(echo $arg | sed "s/^-target=//")"; define_vars;; -test) test_mode=true; define_vars;; - /*kernel-*) - kernel_libary=$arg;; + -serial) + serial_mode=true; define_vars;; "run") action=run;; "build") action=build;; "test") action=test;; "help") action=help;; *) - echo "warn: ignoring unknown option '$arg'";; + echo "warn: ignoring unknown option '$arg'" + kernel_libary=$arg;; esac done diff --git a/kernel/src/asm/boot.asm b/kernel/src/asm/boot.asm new file mode 100644 index 0000000..2ecaba8 --- /dev/null +++ b/kernel/src/asm/boot.asm @@ -0,0 +1,172 @@ +global start +extern long_mode_start + +section .text +bits 32 +start: + mov esp, stack_top + + call check_multiboot + call check_cpuid + call check_long_mode + + call setup_page_tables + call enable_paging + + + ; load the 64-bit GDT + lgdt [gdt64.pointer] + jmp gdt64.code:long_mode_start + + hlt + +check_multiboot: + cmp eax, 0x36d76289 + jne .no_multiboot + ret +.no_multiboot: + mov al, "0" + jmp error + +check_cpuid: + ; Check if CPUID is supported by attempting to flip the ID bit (bit 21) + ; in the FLAGS register. If we can flip it, CPUID is available. + + ; Copy EFLAGS in to EAX via stack + pushfd + pop eax + + ; Save the current flags + mov ecx, eax + + ; Flip the ID bit + xor eax, 1 << 21 + + ; push eax to eflags + push eax + popfd + + ; Copy EAX to FLAGS via the Stack + pushfd + pop eax + + ; Restore FLAGS from the old version stored in ECX (i.e. flipping the + ; ID bit back if it was ever flipped). + push ecx + popfd + + ; Check if the ID was changed + cmp eax, ecx + je .no_cpuid + ret +.no_cpuid: + mov al, "1" + jmp error + +check_long_mode: + ; test if extended processor info in available + mov eax, 0x80000000 ; implicit argument for cpuid + cpuid ; get highest supported argument + cmp eax, 0x80000001 ; it needs to be at least 0x80000001 + jb .no_long_mode ; if it's less, the CPU is too old for long mode + + ; extended info about long mode + mov eax, 0x80000001 ; argument for the cpuid function + cpuid ; Cpu id + test edx, 1 << 29 ; check long mode availablity + jz .no_long_mode + ret + +.no_long_mode: + mov al, "2" + jmp error + +setup_page_tables: + ; map first P4 entry to P3 table + mov eax, p3_table + or eax, 0b11 ; present + writable + mov [p4_table], eax + + ; map first P3 entry to P2 table + mov eax, p2_table + or eax, 0b11 ; present + writable + mov [p3_table], eax + + ; map each P2 entry to a huge 2MiB page + mov ecx, 0 ; counter variable + mov eax, 0b10000011 ; huge + writable + present + +.map_p2_table: + cmp eax, 0b10000011 + stack_bottom + jne .valid_mem + mov ebx, 0b10000010 + mov [p2_table + ecx], ebx + add ecx, 8 + cmp ecx, 4096 + jne .valid_mem + ret +.valid_mem: + mov [p2_table + ecx], eax + add eax, 0x200000 ; 2MiB + + add ecx, 8 + cmp ecx, 4096 + jne .map_p2_table + + ret + +enable_paging: + mov eax, p4_table + mov cr3, eax + + ; enable PAE-flag in cr4 (Physical Address Extension) + mov eax, cr4 + or eax, 1 << 5 + mov cr4, eax + + ; set the long mode bit in the EFER MSR (model specific register) + mov ecx, 0xC0000080 + rdmsr + or eax, 1 << 8 + wrmsr + + ; enable paging in the cr0 register + mov eax, cr0 + or eax, 1 << 31 + mov cr0, eax + + ; move stack pointer to accommodate for the guard page + add esp, 0x200000 + + ret + +section .rodata +gdt64: + dq 0 ; zero entry +.code: equ $ - gdt64 + dq (1<<43) | (1<<44) | (1<<47) | (1<<53) ; code segment +.pointer: + dw $ - gdt64 - 1 + dq gdt64 + +; Prints 'ERR: ' and the given error code to the screen and halts +; parameter: error code letter (ascii) in al +error: + mov dword [0xb8000], 0x4f524f45 + mov dword [0xb8004], 0x4f3a4f52 + mov dword [0xb8008], 0x4f504f20 + mov byte [0xb800a], al + hlt + +section .bss +align 4096 +p4_table: + resb 4096 +p3_table: + resb 4096 +p2_table: + resb 4096 +alignb 4096 * 512 ; align memory into huge memory page +stack_bottom: + resb 4096 * 512 +stack_top: diff --git a/kernel/src/asm/long_mode_init.asm b/kernel/src/asm/long_mode_init.asm new file mode 100644 index 0000000..8c1b1e7 --- /dev/null +++ b/kernel/src/asm/long_mode_init.asm @@ -0,0 +1,19 @@ +global long_mode_start + +section .text +bits 64 +long_mode_start: + ; load 0 into all data segment registers + mov ax, 0 + mov ss, ax + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + ; print `Uff!` to screen + mov rax, 0x8521846683668255 + mov qword [0xb8000], rax + + extern _start + jmp _start diff --git a/kernel/src/asm/multiboot_header.asm b/kernel/src/asm/multiboot_header.asm new file mode 100644 index 0000000..9c9d859 --- /dev/null +++ b/kernel/src/asm/multiboot_header.asm @@ -0,0 +1,15 @@ +section .multiboot_header +header_start: + dd 0xe85250d6 ; magic number (multiboot 2) + dd 0 ; architecture 0 + dd header_end - header_start ; header length + ; checksum + dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start)) + + ; insert optional multiboot tags here + + ; required end tag + dw 0 ; type + dw 0 ; flags + dd 8 ; size +header_end: diff --git a/kernel/src/boot.asm b/kernel/src/boot.asm deleted file mode 100644 index 36b1ef9..0000000 --- a/kernel/src/boot.asm +++ /dev/null @@ -1,172 +0,0 @@ -global start -extern long_mode_start - -section .text -bits 32 -start: - mov esp, stack_top - - call check_multiboot - call check_cpuid - call check_long_mode - - call setup_page_tables - call enable_paging - - - ; load the 64-bit GDT - lgdt [gdt64.pointer] - jmp gdt64.code:long_mode_start - - hlt - -check_multiboot: - cmp eax, 0x36d76289 - jne .no_multiboot - ret -.no_multiboot: - mov al, "0" - jmp error - -check_cpuid: - ; Check if CPUID is supported by attempting to flip the ID bit (bit 21) - ; in the FLAGS register. If we can flip it, CPUID is available. - - ; Copy EFLAGS in to EAX via stack - pushfd - pop eax - - ; Save the current flags - mov ecx, eax - - ; Flip the ID bit - xor eax, 1 << 21 - - ; push eax to eflags - push eax - popfd - - ; Copy EAX to FLAGS via the Stack - pushfd - pop eax - - ; Restore FLAGS from the old version stored in ECX (i.e. flipping the - ; ID bit back if it was ever flipped). - push ecx - popfd - - ; Check if the ID was changed - cmp eax, ecx - je .no_cpuid - ret -.no_cpuid: - mov al, "1" - jmp error - -check_long_mode: - ; test if extended processor info in available - mov eax, 0x80000000 ; implicit argument for cpuid - cpuid ; get highest supported argument - cmp eax, 0x80000001 ; it needs to be at least 0x80000001 - jb .no_long_mode ; if it's less, the CPU is too old for long mode - - ; extended info about long mode - mov eax, 0x80000001 ; argument for the cpuid function - cpuid ; Cpu id - test edx, 1 << 29 ; check long mode availablity - jz .no_long_mode - ret - -.no_long_mode: - mov al, "2" - jmp error - -setup_page_tables: - ; map first P4 entry to P3 table - mov eax, p3_table - or eax, 0b11 ; present + writable - mov [p4_table], eax - - ; map first P3 entry to P2 table - mov eax, p2_table - or eax, 0b11 ; present + writable - mov [p3_table], eax - - ; map each P2 entry to a huge 2MiB page - mov ecx, 0 ; counter variable - mov eax, 0b10000011 ; huge + writable + present - -.map_p2_table: - cmp eax, 0b10000011 + stack_bottom - jne .valid_mem - mov ebx, 0b10000010 - mov [p2_table + ecx], ebx - add ecx, 8 - cmp ecx, 4096 - jne .valid_mem - ret -.valid_mem: - mov [p2_table + ecx], eax - add eax, 0x200000 ; 2MiB - - add ecx, 8 - cmp ecx, 4096 - jne .map_p2_table - - ret - -enable_paging: - mov eax, p4_table - mov cr3, eax - - ; enable PAE-flag in cr4 (Physical Address Extension) - mov eax, cr4 - or eax, 1 << 5 - mov cr4, eax - - ; set the long mode bit in the EFER MSR (model specific register) - mov ecx, 0xC0000080 - rdmsr - or eax, 1 << 8 - wrmsr - - ; enable paging in the cr0 register - mov eax, cr0 - or eax, 1 << 31 - mov cr0, eax - - ; move stack pointer to accommodate for the guard page - add esp, 0x200000 - - ret - -section .rodata -gdt64: - dq 0 ; zero entry -.code: equ $ - gdt64 - dq (1<<43) | (1<<44) | (1<<47) | (1<<53) ; code segment -.pointer: - dw $ - gdt64 - 1 - dq gdt64 - -; Prints 'ERR: ' and the given error code to the screen and halts -; parameter: error code letter (ascii) in al -error: - mov dword [0xb8000], 0x4f524f45 - mov dword [0xb8004], 0x4f3a4f52 - mov dword [0xb8008], 0x4f504f20 - mov byte [0xb800a], al - hlt - -section .bss -align 4096 -p4_table: - resb 4096 -p3_table: - resb 4096 -p2_table: - resb 4096 -alignb 4096 * 512 ; align memory into huge memory page -stack_bottom: - resb 4096 * 42 -stack_top: diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 59888d1..e5ab3d3 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -11,47 +11,19 @@ pub mod interrupts; pub mod io; pub mod testing; -use core::fmt::Write; pub use io::qemu::{exit_qemu, QemuExitCode}; pub use io::vga_text::OStream; pub use io::{qemu, serial, vga_text}; +pub use qemu::*; +#[cfg(test)] #[no_mangle] pub extern "C" fn _start() -> ! { - interrupts::gdt::init(); - interrupts::table::init(); - if cfg!(test) { - #[cfg(test)] - test_main(); - qemu::exit_qemu(qemu::QemuExitCode::Success); - } - - x86_64::instructions::interrupts::int3(); - - /// TODO: write test - ///fn _loop(i: u64) -> u64 { - /// if i > 0b1010101001101101 { - /// return i; - /// } - /// let n = i; - /// let mut stdout = OStream::new(); - /// write!(&mut stdout, "{:x}", &n as *const u64 as u64).unwrap(); - /// _loop(i + 1) - ///} - ///_loop(0); - /// TODO: write test - ///x86_64::instructions::interrupts::int3(); loop {} } +#[cfg(test)] #[panic_handler] -#[no_mangle] -pub extern "C" fn panic_handler(info: &core::panic::PanicInfo) -> ! { - io::panic_screen::show(info.message()); - - if cfg!(test) { - write!(serial::SerialStream::new(), "Testing failed\n").unwrap(); - qemu::exit_qemu(qemu::QemuExitCode::Failed); - } +fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} } diff --git a/kernel/src/long_mode_init.asm b/kernel/src/long_mode_init.asm deleted file mode 100644 index 8c1b1e7..0000000 --- a/kernel/src/long_mode_init.asm +++ /dev/null @@ -1,19 +0,0 @@ -global long_mode_start - -section .text -bits 64 -long_mode_start: - ; load 0 into all data segment registers - mov ax, 0 - mov ss, ax - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - - ; print `Uff!` to screen - mov rax, 0x8521846683668255 - mov qword [0xb8000], rax - - extern _start - jmp _start diff --git a/kernel/src/main.rs b/kernel/src/main.rs new file mode 100644 index 0000000..ae78c93 --- /dev/null +++ b/kernel/src/main.rs @@ -0,0 +1,58 @@ +#![no_main] +#![feature(compiler_builtins_lib)] +#![feature(custom_test_frameworks)] +#![feature(abi_x86_interrupt)] +#![feature(panic_info_message)] +#![test_runner(crate::testing::serial_test_runner)] +#![reexport_test_harness_main = "test_main"] +#![no_std] + +pub mod interrupts; +pub mod io; +pub mod testing; + +use core::fmt::Write; +pub use io::qemu::{exit_qemu, QemuExitCode}; +pub use io::vga_text::OStream; +pub use io::{qemu, serial, vga_text}; + +#[cfg(not(test))] +#[no_mangle] +extern "C" fn _start() -> ! { + interrupts::gdt::init(); + interrupts::table::init(); + if cfg!(test) { + #[cfg(test)] + test_main(); + qemu::exit_qemu(qemu::QemuExitCode::Success); + } + + x86_64::instructions::interrupts::int3(); + + /// TODO: write test + ///fn _loop(i: u64) -> u64 { + /// if i > 0b1010101001101101 { + /// return i; + /// } + /// let n = i; + /// let mut stdout = OStream::new(); + /// write!(&mut stdout, "{:x}", &n as *const u64 as u64).unwrap(); + /// _loop(i + 1) + ///} + ///_loop(0); + /// TODO: write test + ///x86_64::instructions::interrupts::int3(); + loop {} +} + +#[panic_handler] +#[no_mangle] +extern "C" fn panic_handler(info: &core::panic::PanicInfo) -> ! { + io::panic_screen::show(info.message()); + + if cfg!(test) { + write!(serial::SerialStream::new(), "Testing failed\n").unwrap(); + qemu::exit_qemu(qemu::QemuExitCode::Failed); + } + loop {} +} diff --git a/kernel/src/multiboot_header.asm b/kernel/src/multiboot_header.asm deleted file mode 100644 index 9c9d859..0000000 --- a/kernel/src/multiboot_header.asm +++ /dev/null @@ -1,15 +0,0 @@ -section .multiboot_header -header_start: - dd 0xe85250d6 ; magic number (multiboot 2) - dd 0 ; architecture 0 - dd header_end - header_start ; header length - ; checksum - dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start)) - - ; insert optional multiboot tags here - - ; required end tag - dw 0 ; type - dw 0 ; flags - dd 8 ; size -header_end: diff --git a/kernel/tests/test01.rs b/kernel/tests/test01.rs new file mode 100644 index 0000000..933d689 --- /dev/null +++ b/kernel/tests/test01.rs @@ -0,0 +1,25 @@ +#![no_main] +#![feature(compiler_builtins_lib)] +#![feature(custom_test_frameworks)] +#![feature(abi_x86_interrupt)] +#![feature(panic_info_message)] +#![test_runner(crate::testing::serial_test_runner)] +#![no_std] + +use kernel; +use kernel::io::qemu; + +#[no_mangle] +extern "C" fn _start() -> ! { + kernel::io::panic_screen::show(Some(&format_args!("hey bro"))); + + + loop {} +} + +#[cfg(test)] +#[panic_handler] +#[no_mangle] +extern "C" fn panic_handler(info: &core::panic::PanicInfo) -> ! { + loop {} +} -- cgit v1.2.3-54-g00ecf