From 626bd9a53b1085b56ee54c900bfafdad79967725 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Mon, 11 Dec 2023 04:47:44 -0600 Subject: added a couple small things, nothing really changed --- src/header/boot.s | 30 +++++---- src/include/gdt.h | 4 +- src/include/print.h | 33 +++++----- src/kernel/gdt.c | 12 ---- src/kernel/gen_gdt.c | 18 ++++-- src/kernel/kernel.c | 8 +-- src/kernel/print.c | 168 +++++++++++++++++++++++++-------------------------- 7 files changed, 135 insertions(+), 138 deletions(-) (limited to 'src') diff --git a/src/header/boot.s b/src/header/boot.s index 5eaa794..b66b249 100644 --- a/src/header/boot.s +++ b/src/header/boot.s @@ -3,7 +3,8 @@ MBMEMINFO equ 1<<1 ; Provide memory map*/ MBFLAGS equ MBALIGN | MBMEMINFO ; Multiboot flag field*/ MAGIC equ 0x1BADB002 ;Lets bootloader find header*/ CHECKSUM equ -(MAGIC + MBFLAGS) ; Proves we are multiboot via checksum*/ - +CODESEG equ 0x08 +DATASEG equ 0x10 section .multiboot align 4 @@ -18,19 +19,23 @@ resb 16384 stack_top: section .text +global gdtr +gdtr: + dw 0 + dd 0 global _start:function (_start.end - _start) _start: mov esp, stack_top extern get_gdtr call get_gdtr - extern gdtr - mov [gdtr], eax cli lgdt [gdtr] + mov eax, cr0 + or al, 1 + mov cr0, eax - - call reloadSegments ; this motherfucker is what is causing so many issues + call reloadSegments [bits 32] extern kernel_main @@ -42,14 +47,13 @@ _start: jmp .hang .end: - - reloadSegments: - JMP 0x0008:.reload_CS ; should def define a CODESEG +reloadSegments: + JMP CODESEG:.reload_CS ; should def define a CODESEG .reload_CS: - MOV AX, 0x0010 + MOV AX, DATASEG MOV DS, AX - MOV ES, AX - MOV FS, AX - MOV GS, AX - MOV SS, AX + MOV ES, AX // TODO: Setup proper extra segment + MOV FS, AX // TODO: Setup proper General purpose segments + MOV GS, AX // ... + MOV SS, AX // TODO: Setup proper stack segment RET diff --git a/src/include/gdt.h b/src/include/gdt.h index 05f426e..06a0d96 100644 --- a/src/include/gdt.h +++ b/src/include/gdt.h @@ -1,6 +1,7 @@ #pragma once #include #define FLAG 0b1100 + typedef struct Segment_Descriptor { uint16_t lim_low; uint16_t base_low; @@ -21,9 +22,6 @@ typedef struct GDT { Segment_Descriptor_t datadesc; } __attribute__((packed)) GDT_t; -uint8_t make_access(uint8_t DPL, uint8_t type, uint8_t exec, uint8_t direction, - uint8_t read_write); - uint16_t make_code(uint8_t priv, uint8_t dc, uint8_t rw); uint16_t make_data(uint8_t priv, uint8_t dc, uint8_t rw); diff --git a/src/include/print.h b/src/include/print.h index a8e228b..3b94c6e 100644 --- a/src/include/print.h +++ b/src/include/print.h @@ -1,26 +1,25 @@ #pragma once -#include #include // Define colors for printing enum { - PRINT_COLOR_BLACK = 0, - PRINT_COLOR_BLUE = 1, - PRINT_COLOR_GREEN = 2, - PRINT_COLOR_CYAN = 3, - PRINT_COLOR_RED = 4, - PRINT_COLOR_MAGENTA = 5, - PRINT_COLOR_BROWN = 6, - PRINT_COLOR_LIGHT_GRAY = 7, - PRINT_COLOR_DARK_GRAY = 8, - PRINT_COLOR_LIGHT_BLUE = 9, - PRINT_COLOR_LIGHT_GREEN = 10, - PRINT_COLOR_LIGHT_CYAN = 11, - PRINT_COLOR_LIGHT_RED = 12, - PRINT_COLOR_PINK = 13, - PRINT_COLOR_YELLOW = 14, - PRINT_COLOR_WHITE = 15, + PRINT_COLOR_BLACK = 0, + PRINT_COLOR_BLUE = 1, + PRINT_COLOR_GREEN = 2, + PRINT_COLOR_CYAN = 3, + PRINT_COLOR_RED = 4, + PRINT_COLOR_MAGENTA = 5, + PRINT_COLOR_BROWN = 6, + PRINT_COLOR_LIGHT_GRAY = 7, + PRINT_COLOR_DARK_GRAY = 8, + PRINT_COLOR_LIGHT_BLUE = 9, + PRINT_COLOR_LIGHT_GREEN = 10, + PRINT_COLOR_LIGHT_CYAN = 11, + PRINT_COLOR_LIGHT_RED = 12, + PRINT_COLOR_PINK = 13, + PRINT_COLOR_YELLOW = 14, + PRINT_COLOR_WHITE = 15, }; // Prototypes diff --git a/src/kernel/gdt.c b/src/kernel/gdt.c index cf0f487..96ddf67 100644 --- a/src/kernel/gdt.c +++ b/src/kernel/gdt.c @@ -1,18 +1,6 @@ #include "gdt.h" #include -uint8_t make_access(uint8_t DPL, uint8_t type, uint8_t exec, uint8_t direction, - uint8_t read_write) { - uint8_t access = 0; - access |= (1 << 7); - access |= (DPL << 5); - access |= (type << 4); - access |= (exec << 3); - access |= (direction << 2); - access |= (read_write << 1); // 10 - return access; -} - uint16_t make_code(uint8_t priv, uint8_t dc, uint8_t rw) { uint16_t access = 0; access |= (1 << 7); diff --git a/src/kernel/gen_gdt.c b/src/kernel/gen_gdt.c index 05eedaa..ccfb461 100644 --- a/src/kernel/gen_gdt.c +++ b/src/kernel/gen_gdt.c @@ -1,11 +1,19 @@ #include "gdt.h" +#include GDT_t *table = 0x0; -GDTR_t gdtr = {0, 0}; +extern GDTR_t gdtr; void get_gdtr() { - *table = (GDT_t){make_descriptor(0, 0, 0), - make_descriptor(0x0000, 0xFFFFF, make_code(0, 0, 1)), - make_descriptor(0x0000, 0xFFFFF, make_data(0, 0, 1))}; + uint32_t base = 0x0000; + + *table = (GDT_t){ + // NULL + make_descriptor(0, 0, 0), + // Code segment, read + make_descriptor(base, 0x8FFFF, make_code(0, 0, 1)), + // Data segment, read write + make_descriptor(base, 0x8FFFF, make_data(0, 0, 1)), + }; gdtr.size = sizeof(Segment_Descriptor_t) * 3 - 1; - gdtr.offset = 0x0; + gdtr.offset = base; } diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 01c017b..5f6f5ae 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -20,10 +20,10 @@ void kernel_main(void) { } printf("%n"); } - print_set_color(PRINT_COLOR_YELLOW, PRINT_COLOR_BLACK); - printf("printf\n\tint: %d\n\tstring: %s\n\tchar: %c\n\tpercent: " - "%%\n\tnothing: %n", - 99, "World! ", 't'); + // print_set_color(PRINT_COLOR_YELLOW, PRINT_COLOR_BLACK); + // printf("printf\n\tint: %d\n\tstring: %s\n\tchar: %c\n\tpercent: " + // "%%\n\tnothing: %n", + // 99, "World! ", 't'); // printf("\n0x%d\n", *((int *)(0x0010))); } diff --git a/src/kernel/print.c b/src/kernel/print.c index 89f6722..d2857bc 100644 --- a/src/kernel/print.c +++ b/src/kernel/print.c @@ -7,8 +7,8 @@ static const size_t NUM_ROWS = 25; // Char struct struct Char { - uint8_t character; - uint8_t color; + uint8_t character; + uint8_t color; }; // Set video mem buffer @@ -22,112 +22,112 @@ uint8_t color = PRINT_COLOR_WHITE | PRINT_COLOR_BLACK << 4; // clear row void clear_row(size_t row) { - // Define empty character - struct Char empty = (struct Char){ - ' ', - color, - }; - - // For every col in row, set vmem to empty - for (size_t col = 0; col < NUM_COLS; col++) { - buffer[col + NUM_COLS * row] = empty; - } + // Define empty character + struct Char empty = (struct Char){ + ' ', + color, + }; + + // For every col in row, set vmem to empty + for (size_t col = 0; col < NUM_COLS; col++) { + buffer[col + NUM_COLS * row] = empty; + } } // Call clear_row for every row void print_clear() { - for (size_t i = 0; i < NUM_ROWS; i++) { - clear_row(i); - } + for (size_t i = 0; i < NUM_ROWS; i++) { + clear_row(i); + } } // newline void print_newline() { - // Reset col and iterate row if not at bottom of screen - col = 0; - if (row < NUM_ROWS - 1) { - row++; - return; - } - - // If at bottom of screen, move all the text up one row - for (size_t row = 1; row < NUM_ROWS; row++) { - for (size_t col = 0; col < NUM_COLS; col++) { - struct Char character = buffer[col + NUM_COLS * row]; - buffer[col + NUM_COLS * (row - 1)] = character; + // Reset col and iterate row if not at bottom of screen + col = 0; + if (row < NUM_ROWS - 1) { + row++; + return; + } + + // If at bottom of screen, move all the text up one row + for (size_t row = 1; row < NUM_ROWS; row++) { + for (size_t col = 0; col < NUM_COLS; col++) { + struct Char character = buffer[col + NUM_COLS * row]; + buffer[col + NUM_COLS * (row - 1)] = character; + } } - } - // Clear - clear_row(NUM_ROWS - 1); + // Clear + clear_row(NUM_ROWS - 1); } // printchar void print_char(char character) { - // If \n, call newline - if (character == '\n') { - print_newline(); - return; - } - - if (character == '\t') { - print_str(" \0"); - return; - } - - // if cols is too large, overflow - if (col > NUM_COLS) { - print_newline(); - } - - // set video memeory to requested character - buffer[col + NUM_COLS * row] = (struct Char){ - (uint8_t)character, - color, - }; - - // iterate col - col++; + // If \n, call newline + if (character == '\n') { + print_newline(); + return; + } + + if (character == '\t') { + print_str(" \0"); + return; + } + + // if cols is too large, overflow + if (col > NUM_COLS) { + print_newline(); + } + + // set video memeory to requested character + buffer[col + NUM_COLS * row] = (struct Char){ + (uint8_t)character, + color, + }; + + // iterate col + col++; } // print_str void print_str(char *str) { - // while character != '\0' print char - for (size_t i = 0; str[i] != '\0'; ++i) - print_char(str[i]); + // while character != '\0' print char + for (size_t i = 0; str[i] != '\0'; ++i) + print_char(str[i]); } // Set color byte to requested colors using color enum from print.h void print_set_color(uint8_t foreground, uint8_t background) { - color = foreground + (background << 4); + color = foreground + (background << 4); } void printf(const char *str, ...) { - va_list args; - va_start(args, str); - char temp_str[256]; - - for (size_t i = 0; str[i] != '\0'; ++i) { - if (str[i] == '%') { - switch (str[++i]) { - case 'i': - case 'd': - print_str(itoa(va_arg(args, int), temp_str)); - break; - case 's': - print_str(va_arg(args, char *)); - break; - case 'c': - print_char(va_arg(args, int)); - break; - case '%': - print_char('%'); - case 'n': - break; - } - continue; + va_list args; + va_start(args, str); + char temp_str[256]; + + for (size_t i = 0; str[i] != '\0'; ++i) { + if (str[i] == '%') { + switch (str[++i]) { + case 'i': + case 'd': + print_str(itoa(va_arg(args, int), temp_str)); + break; + case 's': + print_str(va_arg(args, char *)); + break; + case 'c': + print_char(va_arg(args, int)); + break; + case '%': + print_char('%'); + case 'n': + break; + } + continue; + } + print_char(str[i]); } - print_char(str[i]); - } - va_end(args); + va_end(args); } -- cgit v1.2.3