1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/pc/except.c
2019-06-18 22:56:41 +02:00

117 lines
2.0 KiB
C

// The OS/K Team licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <pc/dev.h>
int dying = 0;
void _except(ctx_t *ctx, int _code, char *fmt, ...)
{
va_list ap;
ulong handler;
uint code = _code;
ulong orig_frame;
log("\nException %u - ", code);
va_start(ap, fmt);
vlog(fmt, ap);
va_end(ap);
log("\n\n");
//
// Interrupted earlier?
//
if (dying)
{
log("Exception thrown while dying=1\n");
enable_stdin_echoing();
exit(-12);
}
if (code >= IDT_SLOTS)
goto actually_die;
//
// Check for E/I handlers
//
if (idt[code] == 0)
{
if (code < 512 && code != E_DBF)
handler = idt[0];
else if (code < 768)
handler = idt[512];
else
handler = idt[768];
}
else
handler = idt[code];
// Found nothing?
if (handler == 0)
goto actually_die; // oh well
// Is another handler already running?
if (idt_handling[code] > 0)
{
// Triple fault?
if (code == E_DBF)
goto actually_die;
// Double fault then?
if (code < 512)
_except(ctx, E_DBF, "Double Fault; previously handling: #%u", code);
// XXX Queue TRAPs and INTs
else
goto actually_die;
}
// Is this a valid frame?
if (rfs[handler] != NULL)
{
orig_frame = rfs_current_idx;
ctx->rf = rfs[handler];
rfs_current_idx = handler;
rax = code;
R(CR6) = rip;
R(CR7) = orig_frame;
idt_handling[code]++;
main_loop();
}
actually_die:
dying = 1;
enable_stdin_echoing();
dumpregs(ctx);
log("\n");
if (ctx->mp)
free(ctx->mp);
//
// Shut down devices
//
if (devfiniall(ctx) < 0)
{
log("Couldn't deinitialize devices\n");
exit(-100 - code);
}
exit(code);
}