MonkOS  v0.1
A simple 64-bit operating system (x86_64)
exception.c
Go to the documentation of this file.
1 //============================================================================
2 /// @file exception.c
3 /// @brief CPU exceptions.
4 //
5 // Copyright 2016 Brett Vickers.
6 // Use of this source code is governed by a BSD-style license
7 // that can be found in the MonkOS LICENSE file.
8 //============================================================================
9 
10 #include <core.h>
11 #include <libc/stdio.h>
12 #include <kernel/x86/cpu.h>
13 #include <kernel/debug/dump.h>
14 #include <kernel/device/tty.h>
17 
18 static const char *exceptionstr[] =
19 {
20  "#DE: Divide by zero exception",
21  "#DB: Debug exception",
22  "Non-maskable interrupt",
23  "#BP: Breakpoint exception",
24  "#OF: Overflow exception",
25  "#BR: BOUND Range exceeded exception",
26  "#UD: Invalid opcode exception",
27  "#NM: Device not available exception",
28  "#DF: Double fault exception",
29  "Coprocessor segment overrun",
30  "#TS: Invalid TSS exception",
31  "#NP: Segment not present exception",
32  "#SS: Stack fault exception",
33  "#GP: General protection exception",
34  "#PF: Page fault exception",
35  "Unknown exception",
36  "#MF: x87 FPU floating-point error",
37  "#AC: Alignment check exception",
38  "#MC: Machine-check exception",
39  "#XM: SIMD floating-point exception",
40  "#VE: Virtualization exception",
41 };
42 
43 static void
44 dump_context(int id, const interrupt_context_t *context)
45 {
46  tty_printf(
47  id,
48  "INT: %02x Error: %08x\n\n",
49  context->interrupt, context->error);
50  tty_printf(
51  id,
52  "CS:RIP: %04x:%016lx SS:RSP: %04x:%016lx\n\n",
53  context->cs, context->retaddr, context->ss, context->rsp);
54 
55  char buf[640];
56 
57  dump_registers(buf, sizeof(buf), &context->regs);
58  tty_print(id, buf);
59  tty_print(id, "\n");
60 
61  dump_cpuflags(buf, sizeof(buf), context->rflags);
62  tty_print(id, buf);
63  tty_print(id, "\n");
64 
65  tty_print(id, "Stack:\n");
66  void *stack = (void *)context->rsp;
67  dump_memory(buf, sizeof(buf), stack, 8 * 16, DUMPSTYLE_ADDR);
68  tty_print(id, buf);
69 }
70 
71 static void
73 {
74  for (;;) {
76  halt();
77  }
78 }
79 
80 static void
81 isr_fatal(const interrupt_context_t *context)
82 {
83  int i = context->interrupt;
84 
85  const char *exstr = i < arrsize(exceptionstr)
86  ? exceptionstr[i] : "Unknown exception.";
87 
88  tty_activate(0);
90  tty_clear(0);
91  tty_printf(0, "%s\n\n", exstr);
92 
93  dump_context(0, context);
94 
95  hang();
96 }
97 
98 static void
99 isr_breakpoint(const interrupt_context_t *context)
100 {
101  (void)context;
102 
103  tty_print(0, "Breakpoint hit.\n");
104 }
105 
106 void
108 {
109  for (int i = 0; i < 32; i++)
110  isr_set(i, isr_fatal); // fatal for now. temporary.
111  isr_set(0xff, isr_fatal);
112 
114 }
void tty_set_textcolor(int id, textcolor_t fg, textcolor_t bg)
Set the foreground and background colors used to display text on the virtual console.
Definition: tty.c:122
int dump_registers(char *buf, size_t bufsize, const registers_t *regs)
Dump the contents of a CPU registers structure to a string buffer.
Definition: dump.c:16
__forceinline void halt()
Definition: cpu_inl.h:146
static void dump_context(int id, const interrupt_context_t *context)
Definition: exception.c:44
void isr_set(int interrupt, isr_handler handler)
Set an interrupt service routine for the given interrupt number.
Core include file.
static void isr_breakpoint(const interrupt_context_t *context)
Definition: exception.c:99
void exceptions_init()
Initialize all exception handling routines.
Definition: exception.c:107
CPU exceptions.
void tty_activate(int id)
Activate the requested virtual console.
Definition: tty.c:107
int dump_memory(char *buf, size_t bufsize, const void *mem, size_t memsize, enum dumpstyle style)
Dump the contents of memory.
Definition: dump.c:50
__forceinline void disable_interrupts()
Definition: cpu_inl.h:140
int dump_cpuflags(char *buf, size_t bufsize, uint64_t rflags)
Dump the contents of the CPU flags register.
Definition: dump.c:33
static const char * exceptionstr[]
Definition: exception.c:18
static void hang()
Definition: exception.c:72
Interrupt handling operations.
x86 CPU-specific function implementations.
void tty_print(int id, const char *str)
Output a null-terminated string to the virtual console using the console&#39;s current text color and scr...
Definition: tty.c:343
Debugging memory and CPU state dump routines.
static void isr_fatal(const interrupt_context_t *context)
Definition: exception.c:81
Standard i/o library.
int tty_printf(int id, const char *format,...)
Output a printf-formatted string to the virtual console using the console&#39;s current text color and sc...
Definition: tty.c:372
Include full address in memory dump.
Definition: dump.h:51
Teletype (console) screen text manipulation routines.
#define arrsize(x)
Return the number of elements in the C array.
Definition: core.h:26
The log context describes the state of the log buffers.
Definition: log.c:46
void tty_clear(int id)
Clear the virtual console screen&#39;s contents using the current text background color.
Definition: tty.c:174
#define EXCEPTION_BREAKPOINT
Definition: exception.h:18