2013-07-10 09:26:01 -04:00
|
|
|
/*******************************************************************************
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-06-20 10:37:25 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2013.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
panic.cpp
|
|
|
|
Displays an error whenever something critical happens.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
*******************************************************************************/
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 15:37:07 -04:00
|
|
|
#include <brand.h>
|
2013-05-13 08:52:15 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-06-20 10:37:25 -04:00
|
|
|
#include <sortix/kernel/calltrace.h>
|
2013-01-09 17:30:36 -05:00
|
|
|
#include <sortix/kernel/interrupt.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/kernel.h>
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/log.h>
|
|
|
|
#include <sortix/kernel/panic.h>
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-06-20 10:37:25 -04:00
|
|
|
namespace Sortix {
|
|
|
|
|
|
|
|
#if defined(PANIC_SHORT)
|
|
|
|
const bool longpanic = false;
|
2011-09-07 11:48:01 -04:00
|
|
|
#else
|
2013-06-20 10:37:25 -04:00
|
|
|
const bool longpanic = true;
|
2011-09-07 11:48:01 -04:00
|
|
|
#endif
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-06-20 10:37:25 -04:00
|
|
|
static bool panicing = false;
|
|
|
|
static bool doublepanic = false;
|
2013-11-11 16:21:56 -05:00
|
|
|
static bool logrecovering = false;
|
2012-03-02 08:08:25 -05:00
|
|
|
|
2013-06-20 10:37:25 -04:00
|
|
|
static void PanicLogoLong()
|
|
|
|
{
|
2013-07-10 15:37:07 -04:00
|
|
|
Log::PrintF(BRAND_PANIC_LONG);
|
2013-06-20 10:37:25 -04:00
|
|
|
}
|
2012-03-02 08:08:25 -05:00
|
|
|
|
2013-06-20 10:37:25 -04:00
|
|
|
static void PanicLogoShort()
|
|
|
|
{
|
2013-07-10 15:37:07 -04:00
|
|
|
Log::Print(BRAND_PANIC_SHORT);
|
2013-06-20 10:37:25 -04:00
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-06-20 10:37:25 -04:00
|
|
|
void PanicInit()
|
|
|
|
{
|
2013-11-11 16:21:56 -05:00
|
|
|
// This is a kernel emergency. We will need to disable preemption, such that
|
|
|
|
// this is the only thread running. This means that we cannot acquire locks
|
|
|
|
// and the data protected by them may be inconsistent.
|
2013-06-20 10:37:25 -04:00
|
|
|
Interrupt::Disable();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-11-11 16:21:56 -05:00
|
|
|
// Detect whether a panic happened during the log recovery.
|
|
|
|
if ( logrecovering )
|
|
|
|
{
|
|
|
|
// Oh no! We paniced during the log recovery that we will do momentarily
|
|
|
|
// - this means that there probably isn't anything we can do but halt.
|
|
|
|
HaltKernel();
|
|
|
|
}
|
|
|
|
logrecovering = true;
|
|
|
|
|
|
|
|
// The kernel log normally uses locks internally and the console may be
|
|
|
|
// rendered by a background thread. This means that we cannot use the normal
|
|
|
|
// kernel log, but that we rather need to switch to the kernel emergency
|
|
|
|
// log, which is able to cope with the potential inconsistencies.
|
|
|
|
|
|
|
|
Log::device_callback = Log::emergency_device_callback;
|
|
|
|
Log::device_width = Log::emergency_device_width;
|
|
|
|
Log::device_height = Log::emergency_device_height;
|
2013-12-20 15:55:05 -05:00
|
|
|
Log::device_get_cursor = Log::emergency_device_get_cursor;
|
2013-11-11 16:21:56 -05:00
|
|
|
Log::device_sync = Log::emergency_device_sync;
|
|
|
|
Log::device_pointer = Log::emergency_device_pointer;
|
|
|
|
|
|
|
|
// Check whether the panic condition left the kernel log unharmed.
|
|
|
|
if ( !Log::emergency_device_is_impaired(Log::emergency_device_pointer) )
|
|
|
|
{
|
|
|
|
// The kernel log device transitioned ideally to the emergency state.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to repair inconsistent state of the emergency log device.
|
|
|
|
else if ( Log::emergency_device_recoup(Log::emergency_device_pointer) )
|
|
|
|
{
|
|
|
|
// The kernel log was successfully repaired and is ready for use in the
|
|
|
|
// current emergency state.
|
|
|
|
}
|
|
|
|
|
|
|
|
// It was not possible to repair the emergency device properly, so instead
|
|
|
|
// we will need to perform a hard reset of the emergency device.
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::emergency_device_reset(Log::emergency_device_pointer);
|
|
|
|
// The kernel log was successfully repaired and is ready for use in the
|
|
|
|
// current emergency state.
|
|
|
|
}
|
|
|
|
|
2013-06-20 10:37:25 -04:00
|
|
|
// Handle the case where the panic code caused another system crash.
|
|
|
|
if ( panicing )
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2013-06-20 10:37:25 -04:00
|
|
|
Log::PrintF("Panic while panicing:\n");
|
|
|
|
doublepanic = true;
|
|
|
|
return;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
2013-06-20 10:37:25 -04:00
|
|
|
panicing = true;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-06-20 10:37:25 -04:00
|
|
|
// Render a notice that the system has crashed and forcefully shut down.
|
|
|
|
longpanic ? PanicLogoLong() : PanicLogoShort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PanicCalltrace()
|
|
|
|
{
|
|
|
|
Log::Print("\n");
|
|
|
|
Calltrace::Perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PanicHooks()
|
|
|
|
{
|
|
|
|
if ( doublepanic )
|
|
|
|
return;
|
|
|
|
if ( ENABLE_CALLTRACE )
|
|
|
|
PanicCalltrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" __attribute__((noreturn)) void Panic(const char* error)
|
|
|
|
{
|
|
|
|
PanicInit();
|
|
|
|
Log::Print(error);
|
|
|
|
PanicHooks();
|
|
|
|
HaltKernel();
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
2013-06-20 10:37:25 -04:00
|
|
|
|
|
|
|
extern "C" __attribute__((noreturn)) void PanicF(const char* format, ...)
|
|
|
|
{
|
|
|
|
PanicInit();
|
|
|
|
va_list list;
|
|
|
|
va_start(list, format);
|
|
|
|
Log::PrintFV(format, list);
|
|
|
|
va_end(list);
|
|
|
|
PanicHooks();
|
|
|
|
HaltKernel();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Sortix
|