summaryrefslogtreecommitdiff
path: root/src/kernel/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel/core')
-rw-r--r--src/kernel/core/kmain.c12
-rw-r--r--src/kernel/core/monitor.c27
-rw-r--r--src/kernel/core/monitor.h5
-rw-r--r--src/kernel/core/sys.c21
-rw-r--r--src/kernel/core/sys.h10
5 files changed, 52 insertions, 23 deletions
diff --git a/src/kernel/core/kmain.c b/src/kernel/core/kmain.c
index d8f059a..cfb9935 100644
--- a/src/kernel/core/kmain.c
+++ b/src/kernel/core/kmain.c
@@ -10,7 +10,16 @@
#include <mem/mem.h>
#include <linker/elf.h>
+/* The kernel's main procedure. This function is called in loader_.asm.
+ This function calls the initializer functions for all system parts.
+ It then loads the modules the kernel was given by the bootloader.
+ This function never returns : once multitasking is started for good,
+ the execution flow of this function is never returned to. */
void kmain(struct multiboot_info_t* mbd, int32_t magic) {
+ monitor_clear();
+
+ ASSERT(magic == MULTIBOOT_BOOTLOADER_MAGIC);
+
size_t totalRam = 0;
uint32_t i;
@@ -25,8 +34,6 @@ void kmain(struct multiboot_info_t* mbd, int32_t magic) {
mem_placementAddr = (mods[i].mod_end & 0xFFFFF000) + 0x1000;
}
- monitor_clear();
-
if (magic != MULTIBOOT_BOOTLOADER_MAGIC) {
PANIC("wrong multiboot magic number.");
}
@@ -62,4 +69,5 @@ void kmain(struct multiboot_info_t* mbd, int32_t magic) {
monitor_write("Modules now RULE THE WORLD !\n");
sti();
tasking_switch();
+ PANIC("Should never happen. Something probably went wrong with multitasking.");
}
diff --git a/src/kernel/core/monitor.c b/src/kernel/core/monitor.c
index 6660fe0..0d0f5eb 100644
--- a/src/kernel/core/monitor.c
+++ b/src/kernel/core/monitor.c
@@ -4,6 +4,9 @@
static int cursor_x = 0, cursor_y = 0;
static uint16_t *video_memory = (uint16_t*)0xE00B8000;
+static uint8_t attribute = 0x07; // 0 = background = black, 7 = foreground = white
+
+/* For internal use only. Tells hardware to move the cursor at (cursor_x, cursor_y). */
static void move_cursor() {
uint16_t cursor_location = cursor_y * 80 + cursor_x;
outb(0x3D4, 14); //Sending high cursor byte
@@ -12,9 +15,9 @@ static void move_cursor() {
outb(0x3D5, cursor_location);
}
+/* For internal use only. Scrolls everything up one line. */
static void scroll() {
- uint8_t attribute_byte = (0 /* black */ << 4) | (7/* white */ & 0x0F);
- uint16_t blank = (attribute_byte << 8) | 0x20;
+ uint16_t blank = (attribute << 8) | 0x20;
if (cursor_y >= 25) {
int i;
@@ -28,16 +31,11 @@ static void scroll() {
}
}
+/* Put one character on the screen. This function handles special characters \b, \t, \r and \n. */
void monitor_put(char c) {
- uint8_t fg = 7; //White
- uint8_t bg = 0; //Black
-
- uint16_t attribute = (fg & 0x0F) | (bg << 4);
- attribute = attribute << 8;
-
- if (c == 0x08 && cursor_x) { //Backspace
+ if (c == '\b' && cursor_x) { //Backspace
cursor_x--;
- } else if (c == 0x09) { //Tab
+ } else if (c == '\t') { //Tab
cursor_x = (cursor_x + 8) & ~(8 - 1);
} else if (c == '\r') { //Carriage return
cursor_x = 0;
@@ -45,7 +43,7 @@ void monitor_put(char c) {
cursor_x = 0;
cursor_y++;
} else if (c >= ' ') { //Any printable character
- video_memory[cursor_y * 80 + cursor_x] = c | attribute;
+ video_memory[cursor_y * 80 + cursor_x] = c | (attribute << 8);
cursor_x++;
}
if (cursor_x >= 80) {
@@ -57,9 +55,9 @@ void monitor_put(char c) {
move_cursor();
}
+/* Clears the screen and moves cursor to (0,0) (top left corner) */
void monitor_clear() {
- uint8_t attribute_byte = (0 /* black */ << 4) | (15 /* white */ & 0x0F);
- uint16_t blank = (attribute_byte << 8) | 0x20;
+ uint16_t blank = (attribute << 8) | 0x20;
int i;
@@ -71,12 +69,14 @@ void monitor_clear() {
move_cursor();
}
+/* Writes a string to the monitor */
void monitor_write(char *s) {
while (*s) {
monitor_put(*(s++));
}
}
+/* Writes a number in hexadecimal notation to the monitor */
void monitor_writeHex(uint32_t v) {
int i;
@@ -89,6 +89,7 @@ void monitor_writeHex(uint32_t v) {
}
}
+/* Writes a number in decimal notation to the monitor */
void monitor_writeDec(uint32_t v) {
if (v == 0) {
monitor_put('0');
diff --git a/src/kernel/core/monitor.h b/src/kernel/core/monitor.h
index f4605cf..c04632e 100644
--- a/src/kernel/core/monitor.h
+++ b/src/kernel/core/monitor.h
@@ -10,6 +10,11 @@ void monitor_writeHex(uint32_t v);
void monitor_writeDec(uint32_t v);
#define NL monitor_put('\n');
+#define WHERE { monitor_write("(kernel:"); \
+ monitor_write(__FILE__); \
+ monitor_write(":"); \
+ monitor_writeDec(__LINE__); \
+ monitor_write(") "); }
#endif
diff --git a/src/kernel/core/sys.c b/src/kernel/core/sys.c
index 1e07f7c..96d4908 100644
--- a/src/kernel/core/sys.c
+++ b/src/kernel/core/sys.c
@@ -1,6 +1,9 @@
#include "sys.h"
#include "monitor.h"
+/* These four functions are wrappers around ASM operations.
+ These functions are used when comunicating with the system hardware. */
+
void outb(uint16_t port, uint8_t value) {
asm volatile("outb %1, %0" : : "dN"(port), "a"(value));
}
@@ -21,14 +24,24 @@ uint16_t inw(uint16_t port) {
return ret;
}
+/* These two functions stop the system, reporting an error message, because something bad happenned. */
+
void panic(char* message, char* file, int line) {
- monitor_write("\n>> PANIC: >>");
- monitor_write(message); monitor_write("<< at "); monitor_write(file);
- monitor_write(":"); monitor_writeDec(line);
- monitor_write("\nSystem halted -_-'");
+ monitor_write("\n\nPANIC:\t\t"); monitor_write(message);
+ monitor_write("\n File:\t\t"); monitor_write(file); monitor_put(':'); monitor_writeDec(line);
+ monitor_write("\n\t\tSystem halted -_-'\n");
+ asm volatile("cli; hlt");
+}
+
+void panic_assert(char* assertion, char* file, int line) {
+ monitor_write("\n\nASSERT FAILED:\t"); monitor_write(assertion);
+ monitor_write("\n File:\t\t"); monitor_write(file); monitor_put(':'); monitor_writeDec(line);
+ monitor_write("\n\t\tSystem halted -_-'\n");
asm volatile("cli; hlt");
}
+/* Global system mutex. See comments in sys.h. */
+
static uint32_t if_locks = 1;
void cli() {
diff --git a/src/kernel/core/sys.h b/src/kernel/core/sys.h
index 2f8cd5e..f147922 100644
--- a/src/kernel/core/sys.h
+++ b/src/kernel/core/sys.h
@@ -9,11 +9,13 @@ uint8_t inb(uint16_t port);
uint16_t inw(uint16_t port);
#define PANIC(s) panic(s, __FILE__, __LINE__);
+#define ASSERT(s) { if (!(s)) panic_assert(#s, __FILE__, __LINE__); }
void panic(char* message, char* file, int line);
+void panic_assert(char* assertion, char* file, int line);
-void sti(); //GLOBAL SYSTEM MUTEX
-void cli();
-
-#define WHERE { monitor_write("(kernel:"); monitor_write(__FILE__); monitor_write(":"); monitor_writeDec(__LINE__); monitor_write(") "); }
+/* For some actions, we use cli and sti as a global system mutex.
+ These actions include : system initialization, process creation, interrupt handling. */
+void cli(); //lock
+void sti(); //unlock
#endif