Panic.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <Kernel/Arch/Processor.h>
  8. #include <Kernel/Arch/x86/IO.h>
  9. #include <Kernel/CommandLine.h>
  10. #include <Kernel/KSyms.h>
  11. #include <Kernel/Panic.h>
  12. #include <Kernel/Thread.h>
  13. namespace Kernel {
  14. [[noreturn]] static void __shutdown()
  15. {
  16. // Note: This will invoke QEMU Shutdown, but for other platforms (or emulators),
  17. // this has no effect on the system, so we still need to halt afterwards.
  18. // We also try the Bochs/Old QEMU shutdown method, if the first didn't work.
  19. IO::out16(0x604, 0x2000);
  20. IO::out16(0xb004, 0x2000);
  21. Processor::halt();
  22. }
  23. void __panic(const char* file, unsigned int line, const char* function)
  24. {
  25. // Avoid lock ranking checks on crashing paths, just try to get some debugging messages out.
  26. auto* thread = Thread::current();
  27. if (thread)
  28. thread->set_crashing();
  29. critical_dmesgln("at {}:{} in {}", file, line, function);
  30. dump_backtrace(PrintToScreen::Yes);
  31. switch (kernel_command_line().panic_mode()) {
  32. case PanicMode::Shutdown:
  33. __shutdown();
  34. case PanicMode::Halt:
  35. [[fallthrough]];
  36. default:
  37. Processor::halt();
  38. }
  39. }
  40. }