|
|
@ -238,7 +238,7 @@ diff -uNr 02_runtime_init/src/main.rs 03_hacky_hello_world/src/main.rs
|
|
|
|
diff -uNr 02_runtime_init/src/panic_wait.rs 03_hacky_hello_world/src/panic_wait.rs
|
|
|
|
diff -uNr 02_runtime_init/src/panic_wait.rs 03_hacky_hello_world/src/panic_wait.rs
|
|
|
|
--- 02_runtime_init/src/panic_wait.rs
|
|
|
|
--- 02_runtime_init/src/panic_wait.rs
|
|
|
|
+++ 03_hacky_hello_world/src/panic_wait.rs
|
|
|
|
+++ 03_hacky_hello_world/src/panic_wait.rs
|
|
|
|
@@ -4,10 +4,16 @@
|
|
|
|
@@ -4,14 +4,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
//! A panic handler that infinitely waits.
|
|
|
|
//! A panic handler that infinitely waits.
|
|
|
|
|
|
|
|
|
|
|
@ -246,9 +246,45 @@ diff -uNr 02_runtime_init/src/panic_wait.rs 03_hacky_hello_world/src/panic_wait.
|
|
|
|
+use crate::{cpu, println};
|
|
|
|
+use crate::{cpu, println};
|
|
|
|
use core::panic::PanicInfo;
|
|
|
|
use core::panic::PanicInfo;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Private Code
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+/// Stop immediately if called a second time.
|
|
|
|
|
|
|
|
+///
|
|
|
|
|
|
|
|
+/// # Note
|
|
|
|
|
|
|
|
+///
|
|
|
|
|
|
|
|
+/// Using atomics here relieves us from needing to use `unsafe` for the static variable.
|
|
|
|
|
|
|
|
+///
|
|
|
|
|
|
|
|
+/// On `AArch64`, which is the only implemented architecture at the time of writing this,
|
|
|
|
|
|
|
|
+/// [`AtomicBool::load`] and [`AtomicBool::store`] are lowered to ordinary load and store
|
|
|
|
|
|
|
|
+/// instructions. They are therefore safe to use even with MMU + caching deactivated.
|
|
|
|
|
|
|
|
+///
|
|
|
|
|
|
|
|
+/// [`AtomicBool::load`]: core::sync::atomic::AtomicBool::load
|
|
|
|
|
|
|
|
+/// [`AtomicBool::store`]: core::sync::atomic::AtomicBool::store
|
|
|
|
|
|
|
|
+fn panic_prevent_reenter() {
|
|
|
|
|
|
|
|
+ use core::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
+ #[cfg(not(target_arch = "aarch64"))]
|
|
|
|
|
|
|
|
+ compile_error!("Add the target_arch to above's check if the following code is safe to use");
|
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
+ static PANIC_IN_PROGRESS: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
+ if !PANIC_IN_PROGRESS.load(Ordering::Relaxed) {
|
|
|
|
|
|
|
|
+ PANIC_IN_PROGRESS.store(true, Ordering::Relaxed);
|
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
+ cpu::wait_forever()
|
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
+
|
|
|
|
#[panic_handler]
|
|
|
|
#[panic_handler]
|
|
|
|
-fn panic(_info: &PanicInfo) -> ! {
|
|
|
|
-fn panic(_info: &PanicInfo) -> ! {
|
|
|
|
+fn panic(info: &PanicInfo) -> ! {
|
|
|
|
+fn panic(info: &PanicInfo) -> ! {
|
|
|
|
|
|
|
|
+ // Protect against panic infinite loops if any of the following code panics itself.
|
|
|
|
|
|
|
|
+ panic_prevent_reenter();
|
|
|
|
|
|
|
|
+
|
|
|
|
+ if let Some(args) = info.message() {
|
|
|
|
+ if let Some(args) = info.message() {
|
|
|
|
+ println!("\nKernel panic: {}", args);
|
|
|
|
+ println!("\nKernel panic: {}", args);
|
|
|
|
+ } else {
|
|
|
|
+ } else {
|
|
|
|