From 55d5823bdeedc0288b02de646ebf917e1b258b80 Mon Sep 17 00:00:00 2001 From: lordtet Date: Sat, 28 Jun 2025 02:11:09 -0400 Subject: [PATCH] Added tons of documentation Still need to document a lot of functions, but I cleaned up and explained a lot of the code via comments. --- include/idt.h | 6 ++++++ include/interrupt_handlers.h | 5 +++++ include/kio.h | 2 +- include/kmultiboot.h | 5 +++++ include/kttools.h | 5 +++++ include/tss.h | 7 +++---- src/gdt.s | 2 ++ src/idt.c | 3 +++ src/{allocidt.s => idt_asm.s} | 9 ++++++++- src/interrupts.s | 9 +++++++-- src/kio.c | 12 ++++++++++++ src/kttools.c | 2 -- src/main.c | 9 --------- src/start.s | 4 +++- 14 files changed, 60 insertions(+), 20 deletions(-) rename src/{allocidt.s => idt_asm.s} (54%) diff --git a/include/idt.h b/include/idt.h index 527a926..5aeb41f 100644 --- a/include/idt.h +++ b/include/idt.h @@ -1,5 +1,11 @@ +/* IDT.h +* Interrupt Descriptor Table +* +* IDT related structures, globals and functions +*/ #ifndef IDT_H #define IDT_H + #include typedef struct InterruptDescriptor_s { diff --git a/include/interrupt_handlers.h b/include/interrupt_handlers.h index d298ca6..06308ba 100644 --- a/include/interrupt_handlers.h +++ b/include/interrupt_handlers.h @@ -1,3 +1,8 @@ +/* interrupt_handlers.h +* Interrupt Handlers +* +* Functions and structs for the C side of handling interrupts. +*/ #ifndef INTERRUPTHANDLERS_H #define INTERRUPTHANDLERS_H #include diff --git a/include/kio.h b/include/kio.h index cf2286c..c5ad053 100644 --- a/include/kio.h +++ b/include/kio.h @@ -1,7 +1,7 @@ /* kio.h * Kernel I/O * - * Handles any in/output + * Responsible for kernel related in/output, such as VGA and serial */ #ifndef KIO_H #define KIO_H diff --git a/include/kmultiboot.h b/include/kmultiboot.h index a8e6f91..e47f467 100644 --- a/include/kmultiboot.h +++ b/include/kmultiboot.h @@ -1,3 +1,8 @@ +/* kmultiboot.h +* Kernel Multiboot +* +* Data needed for multiboot compatibility. +*/ #ifndef KMULTIBOOT_H #define KMULTIBOOT_H diff --git a/include/kttools.h b/include/kttools.h index c15ce07..d69cfe1 100644 --- a/include/kttools.h +++ b/include/kttools.h @@ -1,3 +1,8 @@ +/* kttools.h +* Kernel Type Tools +* +* Library for converting types and transcribing data. +*/ #ifndef KTTOOLS_H #define KTTOOLS_H #include diff --git a/include/tss.h b/include/tss.h index 9f8a78b..81e52b4 100644 --- a/include/tss.h +++ b/include/tss.h @@ -1,8 +1,7 @@ -/* kdtables.h -* Kernel Discriptor Tables +/* tss.h +* Task State Segment * -* Contains GDT, IDT, and any relevant functions and structs pertaining to them -* +* Structure definitions for the TSS */ #ifndef TSS_H #define TSS_H diff --git a/src/gdt.s b/src/gdt.s index 1c1da6c..ccd8bcf 100644 --- a/src/gdt.s +++ b/src/gdt.s @@ -52,6 +52,8 @@ section .gdt_sect dw 0 gdt_end: gdtr: + ;size of the gdt dw gdt_end - gdt - 1 + ;location of the gdt dd gdt diff --git a/src/idt.c b/src/idt.c index d060fe2..54a6f7c 100644 --- a/src/idt.c +++ b/src/idt.c @@ -2,15 +2,18 @@ #include "interrupt_handlers.h" void setup_idt() { + //IDT is pretty big. Lets make it 256 IDT entries large, 00-FF. idtr.size = (sizeof(InterruptDescriptor_t) * 256) - 1; idtr.IDT = &idt_start; write_descriptors(); + //load_idt is in assembly, go find it in alloc_idt.s load_idt(); } void write_descriptors() { for(int i = 0; i <= num_interrupts; i++) { + // Grab the location of the ISR we're working with and the location of the IDT entry to write it to. uint32_t current_isr = (uint32_t)isr_ptrs[i]; InterruptDescriptor_t* current_idt_entry = idtr.IDT + i; diff --git a/src/allocidt.s b/src/idt_asm.s similarity index 54% rename from src/allocidt.s rename to src/idt_asm.s index 8d3df6a..cb4e845 100644 --- a/src/allocidt.s +++ b/src/idt_asm.s @@ -1,6 +1,10 @@ +;idt_asm.s +;Memory allocation for the IDT and any assembly-required functions for it to work. + %define NUM_INTERRUPTS 255 global idtr, idt_start, num_interrupts, load_idt +;Stub function that just loads the IDT. section .text load_idt: lidt [idtr] @@ -10,10 +14,13 @@ section .idtr align=8 num_interrupts: db NUM_INTERRUPTS idtr: - ;IDTR looks like the size (minus 1) and then the pointer to the start of it. + ;Word: size of idtr dw 0 + ;DWord: location of IDTR dd 0 + section .idt nobits + ;Reserve some space for the IDT. Actually defined later in C. idt_start: resb (NUM_INTERRUPTS+1) * 8 diff --git a/src/interrupts.s b/src/interrupts.s index e75a253..2e94d4c 100644 --- a/src/interrupts.s +++ b/src/interrupts.s @@ -21,23 +21,28 @@ isr_common: add esp,8 ;deallocate the error code from the stack before heading out iret +;Macro for the isr: 1 argument for the interrupt ID %macro ISR_ENTRY 1 +;isr_int# isr_%1: cli + ;Small table of interrupts that don't have an error code associated, so we push a dummy for stack consistency. %if %1 = 8 || %1 = 10 || %1 = 11 || %1 = 12 || %1 = 13 || %1 = 14 || %1 = 17 || %1 = 21 %else push dword 0 %endif - push dword %1 ; saving our interrupt so C knows + ;This is us pushing the isr #, which is also just the interrupt ID. Useful later. + push dword %1 jmp isr_common %endmacro - +;This is to trigger the above macro once for every ISR. %assign j 0 %rep 255 ISR_ENTRY j %assign j j+1 %endrep + ; Generate a table of pointers that point to each of our ISRs. This will be accessed from C to setup our IDT. isr_ptrs: %assign i 0 diff --git a/src/kio.c b/src/kio.c index 0b3dec6..24fa2a0 100644 --- a/src/kio.c +++ b/src/kio.c @@ -73,6 +73,18 @@ void vga_printdec(uint32_t out) { vga_print(buff); } +/** + * @brief Print a format string to the VGA output. + * + * Print some format string to the vga output cleanly. Supports a few format strings in C format. + * + * @param fmt: The format string to print + * @param ...: Values for the format specifiers + * + * @return No return value + * + */ + void vga_printf(const char* fmt, ...) { va_list args; va_start(args, fmt); diff --git a/src/kttools.c b/src/kttools.c index 4c9ac6e..0b3986c 100644 --- a/src/kttools.c +++ b/src/kttools.c @@ -11,8 +11,6 @@ */ -//convert digits from int num to buffer buf. in hex. This is 32 bit only for now! -//WARN: integer is written from right to left into the buffer, and will halt if the buffer is too small. /** * @brief Convert a hex int to string, up to 32 bits. diff --git a/src/main.c b/src/main.c index 76c92cd..28dfd22 100644 --- a/src/main.c +++ b/src/main.c @@ -10,18 +10,9 @@ void kern_main(uint32_t multiboot_magic, mb_info_t* multiboot_info) { //Hello C! Let's get to work in cleaning up our environment a bit and creating some safety. //First interrupts. - vga_clear(); - vga_printf("IDT base = %X", idtr.IDT); - - setup_idt(); - vga_printf("IDT test :)"); - int x = 5; - int y = 1; - x = x / (y-1); - vga_printf("%d", x); //wipe the screen vga_clear(); //We're going to use this buffer as our 8char hex representation for reading mem diff --git a/src/start.s b/src/start.s index d877dd8..a48d4c5 100644 --- a/src/start.s +++ b/src/start.s @@ -1,4 +1,6 @@ -; Bootloader places us in 32 bit mode :) +;start.s +;Entrypoint for kernel, hello world! + bits 32 ;Some symbols we'll need from other files... extern kern_main