2022-04-03 01:55:20 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/Arch/CPU.h>
|
|
|
|
#include <Kernel/Arch/RegisterState.h>
|
|
|
|
|
2023-02-24 21:10:59 +03:00
|
|
|
#include <Kernel/Library/Panic.h>
|
2023-02-24 20:45:37 +03:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
|
|
|
#include <Kernel/Tasks/Thread.h>
|
2022-04-03 01:55:20 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
void handle_crash(Kernel::RegisterState const& regs, char const* description, int signal, bool out_of_memory)
|
|
|
|
{
|
|
|
|
auto* current_thread = Thread::current();
|
2023-12-08 02:03:31 +03:00
|
|
|
if (!current_thread) {
|
|
|
|
VERIFY(regs.previous_mode() == ExecutionMode::Kernel);
|
|
|
|
|
|
|
|
dbgln("CRASH: CPU #{} {} in kernel", Processor::current_id(), description);
|
|
|
|
|
|
|
|
dump_registers(regs);
|
|
|
|
if (Memory::MemoryManager::is_initialized())
|
|
|
|
MM.dump_kernel_regions();
|
|
|
|
|
|
|
|
PANIC("Crash in kernel with !Thread::current()");
|
|
|
|
}
|
2022-04-03 01:55:20 +03:00
|
|
|
|
2023-01-08 18:16:08 +03:00
|
|
|
auto crashed_in_kernel = regs.previous_mode() == ExecutionMode::Kernel;
|
2022-04-03 01:55:20 +03:00
|
|
|
if (!crashed_in_kernel && current_thread->has_signal_handler(signal) && !current_thread->should_ignore_signal(signal) && !current_thread->is_signal_masked(signal)) {
|
|
|
|
current_thread->send_urgent_signal_to_self(signal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& process = current_thread->process();
|
|
|
|
|
|
|
|
// If a process crashed while inspecting another process,
|
|
|
|
// make sure we switch back to the right page tables.
|
|
|
|
Memory::MemoryManager::enter_process_address_space(process);
|
|
|
|
|
2023-01-08 18:16:08 +03:00
|
|
|
dmesgln("CRASH: CPU #{} {} in {}", Processor::current_id(), description, regs.previous_mode() == ExecutionMode::Kernel ? "kernel"sv : "userspace"sv);
|
2022-04-03 01:55:20 +03:00
|
|
|
dump_registers(regs);
|
|
|
|
|
|
|
|
if (crashed_in_kernel) {
|
2022-08-23 18:58:05 +03:00
|
|
|
process.address_space().with([&](auto& space) { space->dump_regions(); });
|
2023-01-30 13:05:59 +03:00
|
|
|
PANIC("Crash in kernel");
|
2022-04-03 01:55:20 +03:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:10:29 +03:00
|
|
|
process.crash(signal, { regs }, out_of_memory);
|
2022-04-03 01:55:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|