diff --git a/Makefile b/Makefile index 47e253b..8d6ad9d 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,16 @@ # Define the object files -OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/gdt.o bin/disk.o +OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/disk.o # Define the compiler and assembler CC = gcc -D__is_katauos AS = nasm # Define the compiler flags -CFLAGS = -std=gnu99 -ffreestanding -O2 -Wall -Wextra -m32 +CFLAGS = -std=gnu99 -ffreestanding -O0 -Wall -Wextra -m32 -g ASFLAGS = -f elf32 # Define the linker flags -LDFLAGS = -ffreestanding -O2 -nostdlib -m32 +LDFLAGS = -ffreestanding -O0 -nostdlib -m32 -g LDSCRIPT = link.ld # Define the output file @@ -23,7 +23,7 @@ image: grub-mkrescue -o boot.iso grubshit test: - qemu-system-i386 -drive file=boot.iso,media=disk,format=raw -m 512M + qemu-system-i386 -drive file=boot.iso,media=disk,format=raw -m 512M -d int test-bochs: bochs -f bochsrc @@ -55,9 +55,6 @@ bin/string.o: src/string.c bin/stdio.o: src/stdio.c $(CC) $(CFLAGS) -c $< -o $@ -bin/gdt.o: src/gdt.c - $(CC) $(CFLAGS) -c $< -o $@ - bin/disk.o: src/disk.c $(CC) $(CFLAGS) -c $< -o $@ diff --git a/include/gdt.h b/include/gdt.h deleted file mode 100644 index bd8820e..0000000 --- a/include/gdt.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef GDT_H -#define GDT_H - -void gdt_install(); - -#endif diff --git a/link.ld b/link.ld index f398e54..2fe7329 100644 --- a/link.ld +++ b/link.ld @@ -1,10 +1,17 @@ OUTPUT_FORMAT(elf32-i386) ENTRY(start) SECTIONS - { - . = 0x100000; - .text : { *(.text) } - .rodata : { *(.rodata) } - .data : { *(.data) } - .bss : { *(.bss) } - } +{ + . = 0x100000; + .text BLOCK(4K) : ALIGN(4K) { + *(.multiboot) + *(.text) + } + .rodata BLOCK(4K) : ALIGN(4K) { *(.rodata) } + .data BLOCK(4K) : ALIGN(4K) { *(.data) } + .bss BLOCK(4K) : ALIGN(4K) { + *(.COMMON) + *(.bss) + } + kernel_end = .; +} diff --git a/src/boot.asm b/src/boot.asm index 6ae6fa3..4b73121 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -1,20 +1,39 @@ bits 32 -section .text +section .multiboot align 4 dd 0x1BADB002 dd 0x00 dd - (0x1BADB002+0x00) +section.data +align 4 +multiboot_info: + dd 0 ; flags + dd 0 ; mem_lower + dd 0 ; mem_upper + dd 0 ; boot_device + dd 0 ; cmdline + dd 0 ; mods_count + dd 0 ; mods_addr + +section .bss +stack_bottom: +resb 16384 +stack_top: + + section .gdt %include "src/gdt.asm" section .text -global start +;global start +global start:function (start.end - start) extern kmain ; this function is gonna be located in our c code(kernel.c) start: - cli ;clears the interrupts + mov esp, stack_top +; cli ;clears the interrupts lgdt [gdt] mov ax, DATA_SEG @@ -29,7 +48,6 @@ start: push ebx call kmain ;send processor to continue execution from the kamin funtion in c code - ;;;;;;;;;;;DIVISION BY ZERO TEST ; mov ax, 10 ; mov bx, 0 @@ -37,4 +55,4 @@ start: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; hlt ; halt the cpu(pause it from executing from this address - +.end: diff --git a/src/gdt.c b/src/gdt.c deleted file mode 100644 index 010eaea..0000000 --- a/src/gdt.c +++ /dev/null @@ -1,84 +0,0 @@ -#include "../include/gdt.h" -#include "../include/stdio.h" -#include - -struct gdt_entry { - uint64_t descriptor; -}; - -struct gdt_entry gdt[5]; - -// Each define here is for a specific flag in the descriptor. -// Refer to the intel documentation for a description of what each one does. -#define SEG_DESCTYPE(x) ((x) << 0x04) // Descriptor type (0 for system, 1 for code/data) -#define SEG_PRES(x) ((x) << 0x07) // Present -#define SEG_SAVL(x) ((x) << 0x0C) // Available for system use -#define SEG_LONG(x) ((x) << 0x0D) // Long mode -#define SEG_SIZE(x) ((x) << 0x0E) // Size (0 for 16-bit, 1 for 32) -#define SEG_GRAN(x) ((x) << 0x0F) // Granularity (0 for 1B - 1MB, 1 for 4KB - 4GB) -#define SEG_PRIV(x) (((x) & 0x03) << 0x05) // Set privilege level (0 - 3) - -#define SEG_DATA_RD 0x00 // Read-Only -#define SEG_DATA_RDA 0x01 // Read-Only, accessed -#define SEG_DATA_RDWR 0x02 // Read/Write -#define SEG_DATA_RDWRA 0x03 // Read/Write, accessed -#define SEG_DATA_RDEXPD 0x04 // Read-Only, expand-down -#define SEG_DATA_RDEXPDA 0x05 // Read-Only, expand-down, accessed -#define SEG_DATA_RDWREXPD 0x06 // Read/Write, expand-down -#define SEG_DATA_RDWREXPDA 0x07 // Read/Write, expand-down, accessed -#define SEG_CODE_EX 0x08 // Execute-Only -#define SEG_CODE_EXA 0x09 // Execute-Only, accessed -#define SEG_CODE_EXRD 0x0A // Execute/Read -#define SEG_CODE_EXRDA 0x0B // Execute/Read, accessed -#define SEG_CODE_EXC 0x0C // Execute-Only, conforming -#define SEG_CODE_EXCA 0x0D // Execute-Only, conforming, accessed -#define SEG_CODE_EXRDC 0x0E // Execute/Read, conforming -#define SEG_CODE_EXRDCA 0x0F // Execute/Read, conforming, accessed - -#define GDT_CODE_PL0 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \ - SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ - SEG_PRIV(0) | SEG_CODE_EXRD - -#define GDT_DATA_PL0 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \ - SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ - SEG_PRIV(0) | SEG_DATA_RDWR - -#define GDT_CODE_PL3 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \ - SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ - SEG_PRIV(3) | SEG_CODE_EXRD - -#define GDT_DATA_PL3 SEG_DESCTYPE(1) | SEG_PRES(1) | SEG_SAVL(0) | \ - SEG_LONG(0) | SEG_SIZE(1) | SEG_GRAN(1) | \ - SEG_PRIV(3) | SEG_DATA_RDWR - -void create_descriptor(uint32_t base, uint32_t limit, uint16_t flag) -{ - uint64_t descriptor; - - // Create the high 32 bit segment - descriptor = limit & 0x000F0000; // set limit bits 19:16 - descriptor |= (flag << 8) & 0x00F0FF00; // set type, p, dpl, s, g, d/b, l and avl fields - descriptor |= (base >> 16) & 0x000000FF; // set base bits 23:16 - descriptor |= base & 0xFF000000; // set base bits 31:24 - - // Shift by 32 to allow for low part of segment - descriptor <<= 32; - - // Create the low 32 bit segment - descriptor |= base << 16; // set base bits 15:0 - descriptor |= limit & 0x0000FFFF; // set limit bits 15:0 - - printf("GDT descriptor 0x%X flag: 0x%X\n", descriptor, flag); -} - -void gdt_install() -{ - create_descriptor(0, 0, 0); - create_descriptor(0, 0x000FFFFF, (GDT_CODE_PL0)); - create_descriptor(0, 0x000FFFFF, (GDT_DATA_PL0)); - create_descriptor(0, 0x000FFFFF, (GDT_CODE_PL3)); - create_descriptor(0, 0x000FFFFF, (GDT_DATA_PL3)); - - __asm__ __volatile__("lgdt (%0)" : : "r" (gdt)); - printf("gdt_install done\n"); -} diff --git a/src/idt.c b/src/idt.c index 2211b2e..7444d1f 100644 --- a/src/idt.c +++ b/src/idt.c @@ -9,7 +9,8 @@ extern void* isr_stub_table[]; void idt_set() { idtr.base = (uint32) &idt; idtr.limit = sizeof(idt) * sizeof(idt_entry_t) - 1; - __asm__ __volatile__("lidtl (%0)" : : "r" (&idtr)); + __asm__ __volatile__("lidt %0" : : "m" (idtr)); +// __asm__ __volatile__("sti"); //TODO: fixme, make this fucking stinking sti work } diff --git a/src/isr.c b/src/isr.c index 0fb4450..8fc142e 100644 --- a/src/isr.c +++ b/src/isr.c @@ -3,6 +3,8 @@ #include "../include/types.h" #include "../include/screen.h" +void isr_custom(); + void isr_install() { idt_set_descriptor(0, (uint32)isr0, 0x8E); idt_set_descriptor(1, (uint32)isr1, 0x8E); @@ -36,11 +38,18 @@ void isr_install() { idt_set_descriptor(29, (uint32)isr29, 0x8E); idt_set_descriptor(30, (uint32)isr30, 0x8E); idt_set_descriptor(31, (uint32)isr31, 0x8E); + idt_set_descriptor(0x21, (uint32)isr_custom, 0x8E); idt_set(); // Load with ASM terminal_writestring("isr_install done\n"); } +void isr_custom() +{ + terminal_writestring("YOOOOO!!!!!! I LOVE KATYA!!!!"); + asm("hlt"); +} + void isr0() { terminal_writestring(exception_messages[0]); diff --git a/src/kernel.c b/src/kernel.c index 0e43166..5da1a85 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,5 +1,4 @@ #include "../include/isr.h" -#include "../include/gdt.h" #include "../include/screen.h" #include "../include/stdio.h" #include "../include/utils.h" @@ -12,7 +11,6 @@ void kmain() { terminal_initialize(); printf("KatauOS booting up\n"); -// gdt_install(); isr_install(); printf("Kernel init sequence completed\n");