Skip to content

Commit

Permalink
Kernel: panic takes a panic origin
Browse files Browse the repository at this point in the history
Simplifying the panic function by taking a clear cause for the panic.

This way we can easily filter what's interesting to display, and what's not.
The caller of kernel panic shouldn't have to compute what to display himself.

Also adding Blue Screen Of Death so the user knows we have panicked.
  • Loading branch information
Orycterope committed Jul 20, 2019
1 parent c610eb9 commit 03472ce
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 123 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rustc-demangle = "0.1"
failure = { version = "0.1", default-features = false, features = ["derive"] }
bitfield = { git = "https://github.com/sunriseos/rust-bitfield" }
mashup = "0.1.9"
tinybmp = "0.1.0"
acpi = { git = "https://github.com/sunriseos/acpi.git" }

[dependencies.smallvec]
Expand Down
Binary file added kernel/res/bsod.bmp
Binary file not shown.
55 changes: 55 additions & 0 deletions kernel/res/double_fault.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@


.
...

. ....',;:::cccclllllcccc::;,'''..
...';:loodxO0KKXNWWWMMMMMMMMMMMMMMMWWWNXX0x'
..';cox0KNNWMMMMMMMMMMMMWWMMMMMMMMMMMMMMMMMMMMWWO'
..,:ok0XNWMMMMWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWMMMWWk.
..;lx0NWWMMWMMMMMMMMMWWWNXK0OkdollllcccclloodxO0XNWMMMMMMd.
.. .. ..,ckKNMMWWMWWMMMMMWXKOkdc:;,'... . ..';cok0NWWo.
.. . .,lOXWMMMMWWMMWNX0kdl:'... . .,c:'. ..;lx:.
..'lOXWMMMWMMWNXOdc;'.. . . .dWWX0xoc,... ..
. . .:kXWMMWWWMWXOo:'. .. . . .kMWWMMMWN0kdc,. ..
.. .'lONWMWMMMWKx:.. .. . ... 'OWWMMMMMMMMWWN0d:'.. ..
..l0WMMWMMWXkc.. .. .. ... . .lO0k, 'x0KXXNWMMWMMMMMMWXOo'. ..
..c0NMMMMMWKo,. . .. ...... ,0MMX: ....'';:ldxOKNWMMMMMWXk:.
.;ONWWMMMW0l.. . . . .;dO0K0xo;. . .OMMNc... ...,cdOXWMMWWNk;.
.oXMWMMMWKo. . . .':c:,. . ,kNWWWWMMMK; .. .,kMMWl . .,cxXWWWWXd. . ..
. . ,kNWWMMWNx'. .... .lOXWWWXo..,0MNOo:kMWWWx. .;xKNWMWo. .'l0WWWWO, . ..
. .;0WMWMWNO;. .. .ok0d. .xNWN00WMK,.xMWx..'kMWWWO' .lXWOxNMWd. . 'oKWWW0;... .
. . .:0MMMMW0c. . . ;KWWK; .xWWWk'cNWO..d0d;:kXNMWMM0,.lNMK;,0MMd. . .:KMMW0, .
. . ,0MMMWWk, . . '0MMK; cNMWNc,0WK; ..,kNKoxNMWWX:'OWMx.,0WMx. . .. . . .lNMMWx. .
.. 'kWMMMNx. . ...,OMMX;.xWMWXxOWO; ,OWNl.:XWWWMl'OMMd,kNMMx. . . .OMMMNc ..
. . .oNWMMWx... . .;dKNMMN:.OWWMN0kl..',..oWWXooKWMMMMx'xWWKK0OXMk. . .,codl;. . oWWWMk. .
. ,KWMMWk' .oXWOkWWWc.kMMWk'..:ONNc.oNWWWWWWWWWWO',kKKd''dkc. .';,. .:kXWMMWXl. ;KWWMK, .
oWWWMX: . .dNM0,:NMWl.dWWWk,'xXWWK: .cxkxoc:okOOo. .'.. .. 'dXWK; .lXMMKx0WWk. ,0MMMK;
.kMMMWx. . ,KMWd.;XMWo ,0WWWKXWWNO;... .. ......'...,dl,. 'OWWWo.:NMMNl.xWNd. ... '0MMMX;
,KMMMX: . ,KMNo;kWMWd. ,dKNNXXOc. . ... . .'o0Xk,:KWNd. .. .xWMWo.kMMMO,lXWx' .. '0MMMK,
;XWMMO' . '0MWKKOONWd. .';,,'. ... ..lKWWW0;.cdc. . .,OMMWocXMWM0kNXo. . . ;XMMW0' .
. :XWMMk. . . .cOKKo.,kk;. . .. ..:lod'.;dOx, .dKNWWNO, .codl. .:OXNWMWdoNMMWXOd,..;'. . lWMMMx. .
;KMMMx. . .''. ... 'x0xcxNWWNxxXWWW0;lNMMWXl.. ,0MMK; .dXWkoXWWxoNMWXl. .l0NK: .kMMMNc .
'OMMMk. .. .. . .. .lXWO;oWWWMWNWWWWNkkNWMW0:.. .OWMX:.dWW0;,0WMx:0MMNl.;OWMW0, . cNMMMO'
.xMWM0, ...;l;.cNMMMNdcOWWMKc:lxKNX0x:.dMWNc,0WWd.,0WMk'oNMWNKXWWNk,.. ,0WWMWo.
lWWWNc .. . .:O0Xl:KWMMO' cNMWNl 'oKMWNkOWWWl;KMWd,kWWWO..l0WWWNXk:. ,OWMMWO,
. ,0WWMk. cNWWk;OWMMd. '0MMWd. . .xMMMXOXMMo'kMWXKOkXWO' ..;::,'. ;0WWWMX:.
..lNMMNl. . ;KWWO;xWMMd. .dWWWk. .oNMMXl;0WNx.:0XKx''xOl. . . ..cXMWWWXl. .
'OWMMK:. 'OWM0;oWMMx. .:XWWO' 'kNMW0: .d0x; .'.. .... .:ONMMWWXl. . .
. ;0WWMK:. .. . .kWWX;cNWWk. .'kWW0:'dWWXx' .... .. .,xXMMWWW0;.
. .. ;0WWWXl. . oWMNc;KMWk. .,od:..;c:,. . .;xXWMWWWNx'
. 'xNWWNk;. .. . cNWK:.:ol,. .. . . ...cONMMMMWN0:. .
. .. .cKWWWXx;. . .. .. .ll;. .. . . .;o0WMMMMMWKl.
.'oKWMMNkc'. ... . . ..;o0NMMMWWMW0l' ..
.. .'oKWMMWKkc'. ... .. ..,lxKNMMMMWMWXk:..
.'lONMWWWXOdc,... ..,:ok0NWMMMMMMMN0d;. ... ..
. .;d0NWWMMWNKOxol:;'.... ...',:loxOKNMMMMMWWMMMN0d;. . .. ..
..:d0NWWWWWMWWWNXK0OOkxddooooooodddxxkO0KXWWMMMMMMMMMWWWN0xl,..
..;ldOXWWWWMMWWMMMMMMMMMMMMMMMMMMMMMMMMMMMWWMMWWWKOdc,..
. .':lxOKXWMMMMMMMMMMMMWWMMMMMMWMMMWWNNX0kxl:,.. ...
.. ..';clodxOOO000KKK000OOkkxdolc;,...
................ .




40 changes: 40 additions & 0 deletions kernel/res/kernel_fault.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

````` ` ``` ```````
``` ````````````````````````````` ``````````` ````````````
``` ``````` ``````````````` `````````` ```````````````` ``` ```` `````
```````````` ``` ````` ++. .#@@@@ ```````` ````` `` ``` ``````````
``````` ````` ``````` :@@@@@@@@@@@@@@@@@@@@@@::::::,:,,:::: `````` .@@:
```` ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@,,:`````````.::@@@@##@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#';,,@@@@@@@@@@@@@@@:::`````````.: @@@@@@@@@@@@@@@@
@@@@@@@@@@@@ @@@@@@@@@@@@@@@:::`` ``````,:`@@@@@@@@@@@@@@@@
@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@,::```` ````:: @@@@@@@@@@@@@@@@
@@@@@@ #@@@@ @@@@@@@@#@@@@@@@@@ @@@@@@@@@@@@@@@:::`````` ``,, @@@@@@@@@@@@@@@@
@@@@@ @@@@ @@@@@` @@@@@@@ :;; @@@@@@@@@@@@@@@:::`````````:, @@@@@@@@@@ @@@@@
@@@@ @@@ @@@` +@@@ @@ ;;;:. ;,#@@@@@@@@@@@:::````` `` :: @@@@@@@@@@ ;@@@
@@@ @@@ @@` @@@@ @@: :;;:#@@@@@@@@@@@:,:,,,,,,.`:::+@@@@@@@@@@+ #@@
@@ `@@@ @ @@@ ;;;;;;;;`.::: @@@@@@@@`:::::::::::: @@@@@@@@@@@@ @@
@@ @@@ ' @@: ;;;:. ::;;;; ,`+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@' @@
+ ' @@@ ''; @ ;' @ ::;.`::;.@@ @@@@@@@@@@@@@@@@@@@,@@@@@@@@@ #
' @,@ ''' ''' :.@++@ : @ .@@@@@@@@@@@@@@@@@ @@@@@@@@@
' @@ ., ''''' '' ;@, @ .#@ ` .@@@@@@@: `@@@@@@@@ ;
`' @@ `'''''''; '' ;@ @`;:.;;;;: @@@@@@ ` @@@@@@@ ''
' ' @@ ;''''''''' :.@@@# ::;:;;;;:: @@@@@# ', .@@@@@` `''. '
'' '' @' ''''''''; ;;;;;;;;;:.``;::::;;..@'@@@ ' ......` @ ;''' ''
''''' .@ '''''''.,, `::;;;;;:;:;;;: @@++#@@@ @@ '' ': .' ++ '''''''
''''' @ '''''';.,, , :;;::;::;;;;;;; @@@@@@@ @ ` '''`'++++++ ''@@@@@@@@@
''''' ` ''''''',,, , ::: : :;;;;;;::, @@@@@+ ''@@ '++++++ ,@@@@@@@@@@
''''', ' ''''';;.,, , :::.: :;;;;;;;::,@@@@@, ; :@@@@ ;++++'+ @@@@@@@@@@@
'''''' ' ;;;;;;' ,, ,,,::;.:`;;;:;;;:.;'@@@@; '' ''@@@@@@@@@@@@@@@@@@@@@@@
''''''; '' ;;;;;;; ,,,,. ,:::`:, ,`;` `@@@# ;''''''''''+''''''''''''''''
'''''''. ''' ::::,,'; ,,,,,, `:::::` :: ,,`;:@'@# .'''''''''''''''''''''''''''
'''' ''';''; ;;;;''';;; ,,,,,,,,,,,, :;;` :;:: + `''''''''+''+'' ''''''''''
'''':''''''; ;;;; '';;' ```` :;::` ` @@ '''''''' `` ````````
'''';''''''' ,;; ;;;;;;;`,,..... `` ` ''' ''' ```````
'''''''''''' ; ;';;;;.,,.;;'' ,,;. ;;; '': ; '''' `````` ;
'':''.''''''' , ';' '';,,.;;;; ,,;, ;;';;;;;' `.;'` ````` .'
''.'' ''`' ''' ' ' ;;;,,.;;;' ,,;; ;;;;;;;;;;;; ` .' ```` ''
''''':'';'''''' '' ;;;,,.;;;; ,,;; ;;;;;;;';;` ''' `` '''
'''''',''`':''''; '''` ;;;` ;;;;;'; ,,;; ';;;;;;;;: :'''' ;''''
''''''''''''''''''''''' ``;'';;;;;;;;';;;;;;;;;;;;;;' ` ;'''''; '''''''
''''''''''''''''''''''', ;;;;;;;;;;;';;;;;;;;;;;;;;' '''''''', '''''''''
'''''''''''''''''''''; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;' ''''''''''':`''''''''''
Binary file added kernel/res/kernel_panic_doc.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 52 additions & 35 deletions kernel/src/interrupts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::i386::instructions::interrupts::sti;
use crate::mem::VirtualAddress;
use crate::paging::kernel_memory::get_kernel_memory;
use crate::i386::PrivilegeLevel;
use crate::i386::gdt;
use crate::scheduler::get_current_thread;
use crate::process::{ProcessStruct, ThreadState};
use crate::sync::SpinLockIRQ;
Expand All @@ -21,9 +20,49 @@ use crate::sync::SpinLock;
use crate::scheduler;
use crate::i386::gdt::GdtIndex;
use crate::i386::gdt::DOUBLE_FAULT_TASK;
use crate::panic::{kernel_panic, PanicOrigin};
use crate::i386::structures::gdt::SegmentSelector;
use crate::i386::registers::eflags::EFlags;

mod irq;
mod syscalls;
pub mod syscalls;

/// Represents a register backup.
#[repr(C)]
#[derive(Debug, Clone, Default)]
#[allow(clippy::missing_docs_in_private_items)]
#[allow(missing_docs)]
pub struct UserspaceHardwareContext {
pub esp: usize,
pub ebp: usize,
pub edi: usize,
pub esi: usize,
pub edx: usize,
pub ecx: usize,
pub ebx: usize,
pub eax: usize,
// pushed by cpu:
pub errcode: usize,
pub eip: usize,
pub cs: usize,
pub eflags: usize,
}

impl core::fmt::Display for UserspaceHardwareContext {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
writeln!(f, "EIP={:#010x} ESP={:#010x} EBP={:#010x}\n\
EAX={:#010x} EBX={:#010x} ECX={:#010x} EDX={:#010x}\n\
ESI={:#010x} EDI={:#010X}\n\
EFLAGS={:?}\n\
CS={:?}",
self.eip, self.esp, self.ebp,
self.eax, self.ebx, self.ecx, self.edx,
self.esi, self.edi,
EFlags::from_bits_truncate(self.eflags as u32),
SegmentSelector(self.cs as u16),
)
}
}

/// Checks if our thread was killed, in which case unschedule ourselves.
///
Expand All @@ -42,17 +81,16 @@ pub fn check_thread_killed() {

/// Panics with an informative message.
fn panic_on_exception(exception_string: Arguments<'_>, exception_stack_frame: &ExceptionStackFrame) -> ! {
unsafe {
// safe: we're not passing a stackdump_source
// so it will use our current kernel stack, which is safe.
crate::do_panic(
format_args!("{} in {:?}: {:?}",
exception_string,
scheduler::try_get_current_process().as_ref().map(|p| &p.name),
exception_stack_frame),
None,
)
}
kernel_panic(&PanicOrigin::UserspaceFault {
exception_message: exception_string,
userspace_hardware_context: UserspaceHardwareContext {
cs: exception_stack_frame.code_segment as usize,
eflags: exception_stack_frame.cpu_flags as usize,
eip: exception_stack_frame.instruction_pointer.addr(),
esp: exception_stack_frame.stack_pointer.addr(),
.. UserspaceHardwareContext::default()
}
});
}

/// Divide by zero interruption handler. Kills the process unconditionally.
Expand Down Expand Up @@ -170,28 +208,7 @@ extern "x86-interrupt" fn device_not_available_handler(stack_frame: &mut Excepti

/// Double fault handler. Panics the kernel unconditionally.
fn double_fault_handler() {
// Get the Main TSS so I can recover some information about what happened.
unsafe {
// Safety: gdt::MAIN_TASK should always point to a valid TssStruct.
if let Some(tss_main) = gdt::MAIN_TASK.try_lock() {

// safe: we're in an exception handler, nobody can modify the faulty thread's stack.
crate::do_panic(format_args!("Double fault!
EIP={:#010x} CR3={:#010x}
EAX={:#010x} EBX={:#010x} ECX={:#010x} EDX={:#010x}
ESI={:#010x} EDI={:#010X} ESP={:#010x} EBP={:#010x}",
tss_main.tss.eip, tss_main.tss.cr3,
tss_main.tss.eax, tss_main.tss.ebx, tss_main.tss.ecx, tss_main.tss.edx,
tss_main.tss.esi, tss_main.tss.edi, tss_main.tss.esp, tss_main.tss.ebp),
Some(crate::stack::StackDumpSource::new(
tss_main.tss.esp as usize, tss_main.tss.ebp as usize, tss_main.tss.eip as usize
)));
} else {
// safe: we're not passing a stackdump_source
// so it will use our current stack, which is safe.
crate::do_panic(format_args!("Doudble fault! Cannot get main TSS, good luck"), None)
}
}
kernel_panic(&PanicOrigin::DoubleFault);
}

/// Invalid tss interruption handler. Panics the kernel unconditionally.
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/interrupts/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn set_heap_size(new_size: usize) -> Result<usize, UserspaceError> {
}

/// Maps the vga frame buffer mmio in userspace memory
fn map_framebuffer() -> Result<(usize, usize, usize, usize), UserspaceError> {
pub fn map_framebuffer() -> Result<(usize, usize, usize, usize), UserspaceError> {
let tag = i386::multiboot::get_boot_information().framebuffer_tag()
.expect("Framebuffer to be provided");
let framebuffer_size = tag.bpp as usize
Expand Down
Loading

0 comments on commit 03472ce

Please sign in to comment.