From 67df24a850b6dc070382289d09c70e369726cac4 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 15 Nov 2019 07:32:54 +0100 Subject: Add protection fault handler --- kernel/src/interrupts/exceptions.rs | 26 ++++++++++++++++++++------ kernel/src/interrupts/idt.rs | 2 ++ kernel/src/interrupts/mod.rs | 11 ++++++----- kernel/tests/int3.rs | 2 +- kernel/tests/stack_overflow.rs | 2 +- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/kernel/src/interrupts/exceptions.rs b/kernel/src/interrupts/exceptions.rs index f6d5176..a3ecf9d 100644 --- a/kernel/src/interrupts/exceptions.rs +++ b/kernel/src/interrupts/exceptions.rs @@ -13,10 +13,10 @@ macro_rules! exception_default { }; } -static mut expected_fault: Interrupts = Interrupts::NONE; +static mut expected_fault: Interrupts = Interrupts::None; pub unsafe fn expect_fault(int: Interrupts) { - if expected_fault == Interrupts::NONE { + if expected_fault == Interrupts::None { expected_fault = int; } } @@ -27,7 +27,7 @@ fn get_expected_fault() -> Interrupts { pub extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) { match get_expected_fault() { - Interrupts::BREAKPOINT => qemu::exit_qemu(qemu::QemuExitCode::Success), + Interrupts::Breakpoint => qemu::exit_qemu(qemu::QemuExitCode::Success), _ => panic!( "BREAKPOINT exception thrown\n\n{}", exception_default!(stack_frame) @@ -35,6 +35,20 @@ pub extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStac } } +pub extern "x86-interrupt" fn general_protection_fault_handler( + stack_frame: &mut InterruptStackFrame, + error_code: u64, +) { + match get_expected_fault() { + Interrupts::GeneralProtectionFault => qemu::exit_qemu(qemu::QemuExitCode::Success), + _ => panic!( + "GENERAL PROTECTION exception thrown\nerror code: {:x}\n{}", + error_code, + exception_default!(stack_frame) + ), + } +} + pub extern "x86-interrupt" fn page_fault_handler( stack_frame: &mut InterruptStackFrame, error_code: PageFaultErrorCode, @@ -42,7 +56,7 @@ pub extern "x86-interrupt" fn page_fault_handler( use x86_64::registers::control::Cr2; match get_expected_fault() { - Interrupts::PAGE_FAULT => qemu::exit_qemu(qemu::QemuExitCode::Success), + Interrupts::PageFault => qemu::exit_qemu(qemu::QemuExitCode::Success), _ => panic!( "PAGE FAULT while accessing address: 0x{:08x}\nerror code: {:?}(0x{:x})\n{}", Cr2::read().as_u64(), @@ -58,7 +72,7 @@ pub extern "x86-interrupt" fn segment_not_present_handler( error_code: u64, ) { match get_expected_fault() { - Interrupts::NOT_PRESENT => qemu::exit_qemu(qemu::QemuExitCode::Success), + Interrupts::NotPresent => qemu::exit_qemu(qemu::QemuExitCode::Success), _ => panic!( "SEGMENT NOT PRESENT exception\nerror code: 0x{:x}\n{}", error_code, @@ -72,7 +86,7 @@ pub extern "x86-interrupt" fn double_fault_handler( _error_code: u64, // code is always zero ) { match get_expected_fault() { - Interrupts::NOT_PRESENT => qemu::exit_qemu(qemu::QemuExitCode::Success), + Interrupts::DoubleFault => qemu::exit_qemu(qemu::QemuExitCode::Success), _ => panic!( "DOUBLE FAULT\nthis is a fatal exception\n{}", exception_default!(stack_frame) diff --git a/kernel/src/interrupts/idt.rs b/kernel/src/interrupts/idt.rs index 9541f22..e85ef5a 100644 --- a/kernel/src/interrupts/idt.rs +++ b/kernel/src/interrupts/idt.rs @@ -8,6 +8,8 @@ lazy_static! { .set_handler_fn(super::exceptions::breakpoint_handler); idt.segment_not_present .set_handler_fn(super::exceptions::segment_not_present_handler); + idt.general_protection_fault + .set_handler_fn(super::exceptions::general_protection_fault_handler); unsafe { idt.double_fault .set_handler_fn(super::exceptions::double_fault_handler) diff --git a/kernel/src/interrupts/mod.rs b/kernel/src/interrupts/mod.rs index 4ff3c4d..087a59c 100644 --- a/kernel/src/interrupts/mod.rs +++ b/kernel/src/interrupts/mod.rs @@ -5,9 +5,10 @@ pub mod idt; #[repr(usize)] #[derive(PartialEq, Clone, Copy)] pub enum Interrupts { - NONE, - PAGE_FAULT, - DOUBLE_FAULT, - NOT_PRESENT, - BREAKPOINT, + None, + PageFault, + DoubleFault, + NotPresent, + Breakpoint, + GeneralProtectionFault, } diff --git a/kernel/tests/int3.rs b/kernel/tests/int3.rs index f4e2116..f5b5470 100644 --- a/kernel/tests/int3.rs +++ b/kernel/tests/int3.rs @@ -13,7 +13,7 @@ use kernel::io::qemu; extern "C" fn _start() -> ! { kernel::init(); unsafe { - exceptions::expect_fault(interrupts::Interrupts::BREAKPOINT); + exceptions::expect_fault(interrupts::Interrupts::Breakpoint); } x86_64::instructions::interrupts::int3(); panic!("BREAKPOINT not caught"); diff --git a/kernel/tests/stack_overflow.rs b/kernel/tests/stack_overflow.rs index eedb3b5..9e75f59 100644 --- a/kernel/tests/stack_overflow.rs +++ b/kernel/tests/stack_overflow.rs @@ -14,7 +14,7 @@ use kernel::io::qemu; extern "C" fn _start() -> ! { kernel::init(); unsafe { - exceptions::expect_fault(interrupts::Interrupts::NOT_PRESENT); + exceptions::expect_fault(interrupts::Interrupts::PageFault); } _loop(0); panic!("PAGE_FAULT not caught"); -- cgit v1.2.3-70-g09d2 From 6f5f69cf0496b53b7a353cb414666802d7261ad6 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 15 Nov 2019 07:33:59 +0100 Subject: Rework build system --- kernel/.cargo/config | 8 ++++++-- kernel/Cargo.toml | 3 +++ kernel/run | 41 ++++++++++++----------------------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/kernel/.cargo/config b/kernel/.cargo/config index 4a80025..520097b 100644 --- a/kernel/.cargo/config +++ b/kernel/.cargo/config @@ -2,5 +2,9 @@ target = "x86_64-uff.json" rustflags = ["-Clink-arg=-r","-Clink-dead-code"] -[target.'cfg(target_os = "none")'] -runner = "./run test -serial -test" +[target.'cfg(all(target_os = "none", feature = "testing_qemu"))'] +runner = "./run -test -serial" + +[target.'cfg(all(target_os = "none", not(feature = "testing_qemu")))'] +runner = "./run -test" + diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 1baefdb..75f5503 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -13,6 +13,9 @@ harness = false name = "stack_overflow" harness = false +[features] +testing_qemu = [] + [package.metadata.cargo-xbuild] sysroot_path = "target/sysroot" diff --git a/kernel/run b/kernel/run index 8c52bd9..a1add1b 100755 --- a/kernel/run +++ b/kernel/run @@ -8,7 +8,7 @@ onerr() { trap onerr ERR name="uff" -build_mode=release +build_mode=debug action=help target=x86_64 test_mode=false @@ -28,19 +28,16 @@ function define_vars() { define_vars print_help() { - echo "usage: $0 (options) [action]" + echo "usage: $0 (options) [Path]" + echo " build and lauch the iso in qemu" + echo echo "options:" echo " -name= set application name (standard: $name)" 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" - echo " run build and lauch the iso in qemu" - echo " test build iso in test mode and run it in qemu" - echo " help show this help message" + echo " -help show this help message" exit } @@ -76,14 +73,6 @@ build() { if test ! -d "$iso_path/isofiles"; then prepare_iso fi - #if $test_mode; then - # RUSTFLAGS="-Clink-arg=-r -Clink-dead-code" cargo xtest --no-run - #else - # cargo xbuild - #fi - if ! $test_mode; then - cargo xbuild "--$build_mode" - fi build_iso } @@ -93,9 +82,9 @@ run() { 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 -no-reboot " - if $serial_mode; then - qemu="$qemu -display none" - fi + fi + if $serial_mode; then + qemu="$qemu -display none" fi $qemu case "$?" in @@ -122,21 +111,15 @@ for arg in "$@"; do test_mode=true; define_vars;; -serial) serial_mode=true; define_vars;; - "run") action=run;; - "build") action=build;; - "test") action=test;; - "help") action=help;; + -help) + print_help;; *) # echo "warn: ignoring unknown option '$arg'" kernel_libary=$arg;; esac done -case "$action" in - "help") print_help;; - "build") build;; - "test") test_mode=true; build; run;; - "run") build; run;; -esac +build +run echo -e "\x1b[1;32mdone\x1b[m" -- cgit v1.2.3-70-g09d2 From 6134ec21326f8312d09b69c6cfee749f85846e53 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 15 Nov 2019 19:17:54 +0100 Subject: Disable caching for first mem page --- kernel/src/asm/boot.asm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/src/asm/boot.asm b/kernel/src/asm/boot.asm index 9964636..6449d66 100644 --- a/kernel/src/asm/boot.asm +++ b/kernel/src/asm/boot.asm @@ -91,10 +91,13 @@ setup_page_tables: mov eax, p2_table or eax, 0b11 ; present + writable mov [p3_table], eax + + mov eax, 0b10000111 ; huge + no caching + writable + present + mov [p2_table], eax ; map each P2 entry to a huge 2MiB page - mov ecx, 0 ; counter variable - mov eax, 0b10000011 ; huge + writable + present + mov ecx, 8 ; counter variable + mov eax, 0x200000 + 0b10000011 ; huge + writable + present .map_p2_table: cmp eax, 0b10000011 + stack_bottom -- cgit v1.2.3-70-g09d2