Serenity Operating System
1/*
2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <Kernel/Arch/CPU.h>
8#include <Kernel/Arch/RegisterState.h>
9
10#include <Kernel/Panic.h>
11#include <Kernel/Process.h>
12#include <Kernel/Thread.h>
13
14namespace Kernel {
15
16void handle_crash(Kernel::RegisterState const& regs, char const* description, int signal, bool out_of_memory)
17{
18 auto* current_thread = Thread::current();
19 if (!current_thread)
20 PANIC("{} with !Thread::current()", description);
21
22 auto crashed_in_kernel = regs.previous_mode() == ExecutionMode::Kernel;
23 if (!crashed_in_kernel && current_thread->has_signal_handler(signal) && !current_thread->should_ignore_signal(signal) && !current_thread->is_signal_masked(signal)) {
24 current_thread->send_urgent_signal_to_self(signal);
25 return;
26 }
27
28 auto& process = current_thread->process();
29
30 // If a process crashed while inspecting another process,
31 // make sure we switch back to the right page tables.
32 Memory::MemoryManager::enter_process_address_space(process);
33
34 dmesgln("CRASH: CPU #{} {} in {}", Processor::current_id(), description, regs.previous_mode() == ExecutionMode::Kernel ? "kernel"sv : "userspace"sv);
35 dump_registers(regs);
36
37 if (crashed_in_kernel) {
38 process.address_space().with([&](auto& space) { space->dump_regions(); });
39 PANIC("Crash in kernel");
40 }
41
42 process.crash(signal, { regs }, out_of_memory);
43}
44
45}