-
Notifications
You must be signed in to change notification settings - Fork 2
/
kmain.cpp
117 lines (94 loc) · 2.75 KB
/
kmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "klib/debug.h"
#include "klib/panic.h"
#include "klib/strings.h"
#include "sys/gdt.h"
#include "sys/halt.h"
#include "sys/idt.h"
#include "sys/isr.h"
#include "klib/macros.h"
#include "hal/serial_port.h"
#include "hal/text_ui.h"
#include "kernel/boot.h"
#include "kernel/memory2.h"
#include "shell/shell.h"
using klib::Debug;
using hal::Color;
using hal::TextUI;
extern "C" {
const uint32 kMultibootMagicNumber = 0x2BADB002;
// Handler for a C++ pure-virtual call. Panics.
void __cxa_pure_virtual();
// Kernel panic function handler.
void PanicHandler(const char* message);
// Friendly system shutdown screen.
void FriendlyShutdown(const char* message);
void kmain(uint32 multiboot_magic, const kernel::grub::multiboot_info* mbt) {
// Initialize device drivers.
hal::SerialPort::Initialize();
hal::TextUI::Initialize();
// Register the debug log. Debug messages are written to COM1, which
// the CPU emulator will kindly pipe to a file.
hal::SerialPortOutputFn serial_port_writer;
Debug::RegisterOutputFn(&serial_port_writer);
Debug::Log("Kernel started.");
klib::SetPanicFn(&PanicHandler);
if (multiboot_magic != kMultibootMagicNumber) {
klib::Panic("Not booted by GRUB. Not sure what happened.");
}
// Initialize core CPU-based systems.
sys::InstallGlobalDescriptorTable();
sys::InstallInterruptDescriptorTable();
sys::InstallInterruptServiceRoutines();
kernel::SetMultibootInfo(mbt);
// Initialize memory.
kernel::InitializeKernelPageDirectory();
kernel::InitializePageFrameManager();
kernel::SyncPhysicalAndVirtualMemory();
shell::Run();
Debug::Log("Kernel halted.");
FriendlyShutdown("Kernel Halted");
}
void __cxa_pure_virtual() {
klib::Panic("Pure virtual function call.");
}
void PanicHandler(const char* message) {
Debug::Log("************");
Debug::Log("KERNEL PANIC");
Debug::Log("************");
Debug::Log(message);
// Print a RSOD
for (int y = 0; y < 25; y++) {
for (int x = 0; x < 80; x++) {
TextUI::SetColor(x, y, Color::White, Color::Red);
TextUI::SetChar(x, y, ' ');
}
}
int pos = 40 - klib::length(message) / 2;
if (pos < 0) {
pos = 0;
}
TextUI::Print(message, pos, 12);
TextUI::ShowCursor(false);
system_halt();
}
void FriendlyShutdown(const char* message) {
Debug::Log("*****************");
Debug::Log("FRIENDLY SHUTDOWN");
Debug::Log("*****************");
Debug::Log(message);
// Print a BSO.. fun?
for (int y = 0; y < 25; y++) {
for (int x = 0; x < 80; x++) {
TextUI::SetColor(x, y, Color::White, Color::Blue);
TextUI::SetChar(x, y, ' ');
}
}
int pos = 40 - klib::length(message) / 2;
if (pos < 0) {
pos = 0;
}
TextUI::Print(message, pos, 12);
TextUI::ShowCursor(false);
system_halt();
}
} // extern "C"