From 4cc532648ed107d275fa93378acbdc0b130fb9a1 Mon Sep 17 00:00:00 2001 From: jwt2706 Date: Sun, 25 Aug 2024 23:38:50 -0400 Subject: [PATCH 1/6] notes --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e360cf4..1678f48 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ BaboscOS: Bare And Basic Open Source Console-based Operating System (insane naming, i know) -Super bare and basic 64bit operating system featuring a limine protocol compliant (higher half) kernel. +Super simple 64bit operating system featuring a limine protocol compliant (higher half) kernel. It's just built for x86 right now I wanted to build an os one day, and so now im finally attempting that. I decided to start off with this [limine template](https://github.com/limine-bootloader/limine-c-template) after reading the osdev wiki for so dang long. I figured it was a better place to start than to write my own bootloader lol (but i do plan on doing that one day). From a8b0caa25ef6d7a8b796c44e6906247bf3b8ae90 Mon Sep 17 00:00:00 2001 From: jwt2706 Date: Sun, 25 Aug 2024 23:46:04 -0400 Subject: [PATCH 2/6] font fixes --- kernel/include/font.h | 6 +++--- kernel/src/kernel.c | 7 ++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/kernel/include/font.h b/kernel/include/font.h index 3024cc2..7e4cd0c 100644 --- a/kernel/include/font.h +++ b/kernel/include/font.h @@ -68,7 +68,7 @@ static const uint8_t font[96][8] = { {0x03,0x06,0x0C,0x18,0x30,0x60,0x40,0x00}, // '\' {0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00}, // ']' {0x08,0x1C,0x36,0x63,0x00,0x00,0x00,0x00}, // '^' - {0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // '_' + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF}, // '_' {0x0C,0x18,0x30,0x00,0x00,0x00,0x00,0x00}, // '`' {0x00,0x00,0x3E,0x60,0x7E,0x63,0x7E,0x00}, // 'a' {0x07,0x06,0x3E,0x66,0x66,0x66,0x3B,0x00}, // 'b' @@ -96,10 +96,10 @@ static const uint8_t font[96][8] = { {0x00,0x00,0x63,0x36,0x1C,0x36,0x63,0x00}, // 'x' {0x00,0x00,0x63,0x63,0x63,0x7E,0x60,0x3F}, // 'y' {0x00,0x00,0x7F,0x30,0x18,0x06,0x7F,0x00}, // 'z' - {0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18}, // '|' {0x70,0x18,0x18,0x0E,0x18,0x18,0x70,0x00}, // '{' - {0x00,0x00,0x00,0x76,0xDC,0x00,0x00,0x00}, // '~' + {0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18}, // '|' {0x0E,0x18,0x18,0x70,0x18,0x18,0x0E,0x00}, // '}' + {0x00,0x00,0x00,0x76,0xDC,0x00,0x00,0x00}, // '~' }; // TODO: reverse bits so that the characters aren't flipped diff --git a/kernel/src/kernel.c b/kernel/src/kernel.c index e312189..0ff9a08 100644 --- a/kernel/src/kernel.c +++ b/kernel/src/kernel.c @@ -65,12 +65,9 @@ void _start(void) { // init the terminal struct terminal term; - terminal_init(&term, framebuffer, COLOR_WHITE, 2); - - terminal_write(&term, "BaboscOS booted up successfully!BaboscOS booted up successfully!BaboscOS booted up successfully!"); - terminal_edit(&term, COLOR_RED, 3); - terminal_write(&term, "red color and scalesadasdasdsads 3"); + terminal_init(&term, framebuffer, COLOR_GREEN, 2); + terminal_write(&term, "BaboscOS booted up successfully!\nuser@babosc:~$ _"); // We're done, just hang... hcf(); } \ No newline at end of file From ec1dc849967345336f9cb6abdf28b5bcd75f38cd Mon Sep 17 00:00:00 2001 From: jwt2706 Date: Mon, 26 Aug 2024 21:46:24 -0400 Subject: [PATCH 3/6] notes --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1678f48..697c936 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,12 @@ BaboscOS: Bare And Basic Open Source Console-based Operating System (insane naming, i know) -Super simple 64bit operating system featuring a limine protocol compliant (higher half) kernel. It's just built for x86 right now +## What's this? -I wanted to build an os one day, and so now im finally attempting that. I decided to start off with this [limine template](https://github.com/limine-bootloader/limine-c-template) after reading the osdev wiki for so dang long. I figured it was a better place to start than to write my own bootloader lol (but i do plan on doing that one day). +This is just a tinkering project (so expect thing to be messy)
+It's basically a tiny useless 64-bit operating system that only works on x86 arch. +I have some cool ideas for this project, but I'm taking things one at a time, so we'll see where this goes. -I got some cool ideas for this project, but we're taking things one at a time. idk if im going to write seperate docs on this or just really take the time to just write out my thought process in comments but yeah +## What's the reason? + +I wanted to build an os, and so now im finally attempting that. I decided to start off with this [limine template](https://github.com/limine-bootloader/limine-c-template) after reading the osdev wiki for a while. I figured it was a better place to start than to write my own bootloader lol (but i do plan on doing that one day). From 1cfb241cedabb0204fee5d8935b3a0cd480e5539 Mon Sep 17 00:00:00 2001 From: jwt2706 Date: Mon, 26 Aug 2024 23:25:27 -0400 Subject: [PATCH 4/6] keyboard stuff, not sure if it works though :l --- kernel/asm/idt.asm | 8 ++++ kernel/include/drivers/idt.h | 24 +++++++++++ kernel/include/drivers/io.h | 9 ++++ kernel/include/drivers/keyboard.h | 7 +++ kernel/include/terminal.h | 2 + kernel/src/drivers/idt.c | 38 ++++++++++++++++ kernel/src/drivers/io.c | 14 ++++++ kernel/src/drivers/keyboard.c | 72 +++++++++++++++++++++++++++++++ kernel/src/kernel.c | 13 +++++- kernel/src/terminal.c | 6 +++ 10 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 kernel/asm/idt.asm create mode 100644 kernel/include/drivers/idt.h create mode 100644 kernel/include/drivers/io.h create mode 100644 kernel/include/drivers/keyboard.h create mode 100644 kernel/src/drivers/idt.c create mode 100644 kernel/src/drivers/io.c create mode 100644 kernel/src/drivers/keyboard.c diff --git a/kernel/asm/idt.asm b/kernel/asm/idt.asm new file mode 100644 index 0000000..cb4d260 --- /dev/null +++ b/kernel/asm/idt.asm @@ -0,0 +1,8 @@ +; This is just the assembly refrence for the load_idt function. It's actually done with inline assembly in "../src/drivers/idt.c" + +section .text +global load_idt + +load_idt: + lidt [rdi] + ret \ No newline at end of file diff --git a/kernel/include/drivers/idt.h b/kernel/include/drivers/idt.h new file mode 100644 index 0000000..89de0b7 --- /dev/null +++ b/kernel/include/drivers/idt.h @@ -0,0 +1,24 @@ +#ifndef IDT_H +#define IDT_H + +#include + +struct idt_entry { + uint16_t offset_low; + uint16_t selector; + uint8_t ist; + uint8_t type_attr; + uint16_t offset_mid; + uint32_t offset_high; + uint32_t zero; +} __attribute__((packed)); + +struct idt_ptr { + uint16_t limit; + uint64_t base; +} __attribute__((packed)); + +void idt_set_gate(int n, uint64_t handler); +void idt_install(); + +#endif \ No newline at end of file diff --git a/kernel/include/drivers/io.h b/kernel/include/drivers/io.h new file mode 100644 index 0000000..2f07f48 --- /dev/null +++ b/kernel/include/drivers/io.h @@ -0,0 +1,9 @@ +#ifndef IO_H +#define IO_H + +#include + +uint8_t inb(uint16_t port); +void outb(uint16_t port, uint8_t data); + +#endif \ No newline at end of file diff --git a/kernel/include/drivers/keyboard.h b/kernel/include/drivers/keyboard.h new file mode 100644 index 0000000..c91425e --- /dev/null +++ b/kernel/include/drivers/keyboard.h @@ -0,0 +1,7 @@ +#ifndef KEYBOARD_H +#define KEYBOARD_H + +void keyboard_init(); +void keyboard_interrupt_handler(); + +#endif \ No newline at end of file diff --git a/kernel/include/terminal.h b/kernel/include/terminal.h index 7bcd4bc..9c4a84c 100644 --- a/kernel/include/terminal.h +++ b/kernel/include/terminal.h @@ -14,6 +14,8 @@ struct terminal { int scale; }; +extern struct terminal term; + void terminal_init(struct terminal *term, struct limine_framebuffer *framebuffer, uint32_t color, int scale); void terminal_edit(struct terminal *term, uint32_t color, int scale); void terminal_write(struct terminal *term, const char *str); diff --git a/kernel/src/drivers/idt.c b/kernel/src/drivers/idt.c new file mode 100644 index 0000000..154af8a --- /dev/null +++ b/kernel/src/drivers/idt.c @@ -0,0 +1,38 @@ +#include "../../include/drivers/idt.h" +#include + +#define IDT_SIZE 256 + +extern void keyboard_interrupt_handler(); + +struct idt_entry idt[IDT_SIZE]; +struct idt_ptr idt_ptr; + +struct terminal* global_term; + +void idt_set_gate(int n, uint64_t handler) { + idt[n].offset_low = handler & 0xFFFF; + idt[n].selector = 0x08; // kernel code segment + idt[n].ist = 0; + idt[n].type_attr = 0x8E; // interrupt gate + idt[n].offset_mid = (handler >> 16) & 0xFFFF; + idt[n].offset_high = (handler >> 32) & 0xFFFFFFFF; + idt[n].zero = 0; +} + +void load_idt(struct idt_ptr* idt_ptr) { + asm volatile ("lidt (%0)" : : "r"(idt_ptr)); +} + +void idt_install(struct terminal* term) { + idt_ptr.limit = sizeof(struct idt_entry) * IDT_SIZE - 1; + idt_ptr.base = (uint64_t)&idt; + + memset(&idt, 0, sizeof(struct idt_entry) * IDT_SIZE); + + global_term = term; + + idt_set_gate(33, (uint64_t)keyboard_interrupt_handler); // IRQ1 + + load_idt(&idt_ptr); +} \ No newline at end of file diff --git a/kernel/src/drivers/io.c b/kernel/src/drivers/io.c new file mode 100644 index 0000000..7414678 --- /dev/null +++ b/kernel/src/drivers/io.c @@ -0,0 +1,14 @@ +#include "../../include/drivers/io.h" +#include + +// ins +uint8_t inb(uint16_t port) { + uint8_t result; + asm volatile ("inb %1, %0" : "=a"(result) : "Nd"(port)); + return result; +} + +// outs +void outb(uint16_t port, uint8_t data) { + asm volatile ("outb %0, %1" : : "a"(data), "Nd"(port)); +} \ No newline at end of file diff --git a/kernel/src/drivers/keyboard.c b/kernel/src/drivers/keyboard.c new file mode 100644 index 0000000..a118819 --- /dev/null +++ b/kernel/src/drivers/keyboard.c @@ -0,0 +1,72 @@ +#include "../../include/drivers/keyboard.h" +#include "../../include/terminal.h" +#include "../../include/drivers/io.h" +#include + +extern struct terminal term; + +#define KEYBOARD_DATA_PORT 0x60 +#define KEYBOARD_STATUS_PORT 0x64 +#define PIC1_COMMAND 0x20 +#define PIC1_DATA 0x21 +#define PIC_EOI 0x20 + +static uint8_t keyboard_map[128] = { + 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */ + '9', '0', '-', '=', '\b', /* Backspace */ + '\t', /* Tab */ + 'q', 'w', 'e', 'r', /* 19 */ + 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */ + 0, /* 29 - Control */ + 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */ + '\'', '`', 0, /* Left shift */ + '\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */ + 'm', ',', '.', '/', 0, /* Right shift */ + '*', + 0, /* Alt */ + ' ', /* Space bar */ + 0, /* Caps lock */ + 0, /* 59 - F1 key ... > */ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, /* < ... F10 */ + 0, /* 69 - Num lock*/ + 0, /* Scroll Lock */ + 0, /* Home key */ + 0, /* Up Arrow */ + 0, /* Page Up */ + '-', + 0, /* Left Arrow */ + 0, + 0, /* Right Arrow */ + '+', + 0, /* 79 - End key*/ + 0, /* Down Arrow */ + 0, /* Page Down */ + 0, /* Insert Key */ + 0, /* Delete Key */ + 0, 0, 0, 0, /* F11 Key */ + 0, /* F12 Key */ + 0, /* All other keys are undefined */ +}; + +void keyboard_init() { + // unmask the keyboard interrupt (IRQ1) + uint8_t mask = inb(PIC1_DATA); + mask &= ~(1 << 1); + outb(PIC1_DATA, mask); +} + +void keyboard_interrupt_handler(struct terminal* term) { + uint8_t status = inb(KEYBOARD_STATUS_PORT); + if (status & 0x01) { + uint8_t keycode = inb(KEYBOARD_DATA_PORT); + if (keycode < 128) { + char c = keyboard_map[keycode]; + if (c) { + terminal_write_char(&term, c); + } + } + } + // send End of Interrupt (EOI) signal to PIC + outb(PIC1_COMMAND, PIC_EOI); +} \ No newline at end of file diff --git a/kernel/src/kernel.c b/kernel/src/kernel.c index 0ff9a08..b593917 100644 --- a/kernel/src/kernel.c +++ b/kernel/src/kernel.c @@ -4,6 +4,8 @@ #include #include "../include/colors.h" #include "../include/terminal.h" +#include "../include/drivers/idt.h" +#include "../include/drivers/keyboard.h" // Set the base revision to 2, this is recommended as this is the latest // base revision described by the Limine boot protocol specification. @@ -65,8 +67,17 @@ void _start(void) { // init the terminal struct terminal term; - terminal_init(&term, framebuffer, COLOR_GREEN, 2); + terminal_init(&term, framebuffer, COLOR_WHITE, 1); + terminal_write(&term, "Terminal initialized..\n"); + + idt_install(&term); + terminal_write(&term, "IDT installed..\n"); + keyboard_init(); + terminal_write(&term, "Keyboard initialized..\n"); + // write out everything else that inits here + + terminal_edit(&term, COLOR_GREEN, 2); terminal_write(&term, "BaboscOS booted up successfully!\nuser@babosc:~$ _"); // We're done, just hang... hcf(); diff --git a/kernel/src/terminal.c b/kernel/src/terminal.c index fec1b4a..53e415e 100644 --- a/kernel/src/terminal.c +++ b/kernel/src/terminal.c @@ -1,6 +1,8 @@ #include "../include/terminal.h" #include "../include/font.h" +struct terminal term; + void terminal_init(struct terminal *term, struct limine_framebuffer *framebuffer, uint32_t color, int scale) { term->framebuffer = framebuffer; term->start_x = 4; @@ -11,6 +13,10 @@ void terminal_init(struct terminal *term, struct limine_framebuffer *framebuffer term->scale = scale; } +/* + This allows us to change the color and scale of the terminal, + if we change our minds from what we initiallized the terminal with +*/ void terminal_edit(struct terminal *term, uint32_t color, int scale) { term->color = color; term->scale = scale; From 0c8ef0c284e4207285f96adc383d9f1a961f9ea8 Mon Sep 17 00:00:00 2001 From: jwt2706 Date: Mon, 26 Aug 2024 23:37:43 -0400 Subject: [PATCH 5/6] styled bootup screen --- kernel/src/kernel.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/kernel/src/kernel.c b/kernel/src/kernel.c index b593917..961833c 100644 --- a/kernel/src/kernel.c +++ b/kernel/src/kernel.c @@ -65,20 +65,24 @@ void _start(void) { // Fetch the first framebuffer. struct limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0]; - // init the terminal struct terminal term; terminal_init(&term, framebuffer, COLOR_WHITE, 1); - terminal_write(&term, "Terminal initialized..\n"); - + terminal_write(&term, "Terminal initialized...\n"); idt_install(&term); - terminal_write(&term, "IDT installed..\n"); + terminal_write(&term, "IDT installed...\n"); keyboard_init(); - terminal_write(&term, "Keyboard initialized..\n"); - // write out everything else that inits here - + terminal_write(&term, "Keyboard initialized...\n"); + + terminal_edit(&term, COLOR_WHITE, 1); + terminal_write(&term, "BaboscOS booted up successfully!\n\n\n"); + terminal_edit(&term, COLOR_YELLOW, 8); + terminal_write(&term, "BaboscOS"); + terminal_edit(&term, COLOR_YELLOW, 2); + terminal_write(&term, " v0.0.1\n\n\n\n"); terminal_edit(&term, COLOR_GREEN, 2); - terminal_write(&term, "BaboscOS booted up successfully!\nuser@babosc:~$ _"); + terminal_write(&term, "user@babosc:~$_"); + // We're done, just hang... hcf(); } \ No newline at end of file From 3b0ca2be54852adf5638a9209e4d608f7da6d1c3 Mon Sep 17 00:00:00 2001 From: jwt2706 Date: Mon, 26 Aug 2024 23:51:46 -0400 Subject: [PATCH 6/6] enable interrupts on IDT loading --- kernel/src/drivers/idt.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/src/drivers/idt.c b/kernel/src/drivers/idt.c index 154af8a..202ecc4 100644 --- a/kernel/src/drivers/idt.c +++ b/kernel/src/drivers/idt.c @@ -21,7 +21,8 @@ void idt_set_gate(int n, uint64_t handler) { } void load_idt(struct idt_ptr* idt_ptr) { - asm volatile ("lidt (%0)" : : "r"(idt_ptr)); + asm volatile ("lidt (%0)" : : "r"(idt_ptr)); // load idt + asm volatile ("sti"); // enable interrupts } void idt_install(struct terminal* term) {