diff --git a/.gitignore b/.gitignore index 6ffb6c2..ce3f0df 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ Makefile cmake_install.cmake .vscode/ .vs/ +bx_enh_dbg.ini diff --git a/CMakeLists.txt b/CMakeLists.txt index 53966cc..74e30ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,12 +22,14 @@ file(GLOB DRIV_DISP_VGA "kernel/drivers/terminal/*.cpp" "kernel/drivers/termina # Lib source file(GLOB LIB_STD "lib/std/*.cpp" "lib/std/*.asm") +file(GLOB LIB_KERN_LOG "lib/kernel/logger/*.cpp" "lib/kernel/logger/*.asm") add_executable(OwOS ${KERN_SRC} ${KERN_LIB_SRC} ${DRIV_DISP_VGA} - ${LIB_STD}) + ${LIB_STD} ${LIB_KERN_LOG}) + set_target_properties(OwOS PROPERTIES OUTPUT_NAME "OwOS.bin") diff --git a/bochsrc.bxrc b/bochsrc.bxrc new file mode 100644 index 0000000..8522a71 --- /dev/null +++ b/bochsrc.bxrc @@ -0,0 +1,56 @@ +# configuration file generated by Bochs +plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1, gameport=1 +config_interface: win32config +display_library: win32, options="gui_debug" +memory: host=32, guest=32 +romimage: file="C:\Program Files (x86)\Bochs-2.6.9/BIOS-bochs-latest", address=0x0, options=none +vgaromimage: file="C:\Program Files (x86)\Bochs-2.6.9/VGABIOS-lgpl-latest" +boot: cdrom +floppy_bootsig_check: disabled=0 +# no floppya +# no floppyb +ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 +ata0-master: type=cdrom, path="E:\OS\OwOS\build\OwOS.iso", status=inserted, model="Generic 1234", biosdetect=auto +ata0-slave: type=none +ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15 +ata1-master: type=none +ata1-slave: type=none +ata2: enabled=0 +ata3: enabled=0 +optromimage1: file=none +optromimage2: file=none +optromimage3: file=none +optromimage4: file=none +optramimage1: file=none +optramimage2: file=none +optramimage3: file=none +optramimage4: file=none +pci: enabled=1, chipset=i440fx +vga: extension=vbe, update_freq=5, realtime=1 +cpu: count=1, ips=4000000, model=bx_generic, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0 +cpuid: level=6, stepping=3, model=3, family=6, vendor_string="GenuineIntel", brand_string=" Intel(R) Pentium(R) 4 CPU " +cpuid: mmx=1, apic=xapic, simd=sse2, sse4a=0, misaligned_sse=0, sep=1, movbe=0, adx=0 +cpuid: aes=0, sha=0, xsave=0, xsaveopt=0, x86_64=1, 1g_pages=0, pcid=0, fsgsbase=0 +cpuid: smep=0, smap=0, mwait=1, vmx=1 +print_timestamps: enabled=0 +port_e9_hack: enabled=0 +private_colormap: enabled=0 +clock: sync=none, time0=local, rtc_sync=0 +# no cmosimage +# no loader +log: - +logprefix: %t%e%d +debug: action=ignore +info: action=report +error: action=report +panic: action=ask +keyboard: type=mf, serial_delay=250, paste_delay=100000, user_shortcut=none +mouse: type=ps2, enabled=0, toggle=ctrl+mbutton +sound: waveoutdrv=win, waveout=none, waveindrv=win, wavein=none, midioutdrv=win, midiout=none +speaker: enabled=1, mode=sound +parport1: enabled=1, file=none +parport2: enabled=0 +com1: enabled=1, mode=null +com2: enabled=0 +com3: enabled=0 +com4: enabled=0 \ No newline at end of file diff --git a/build/OwOS.bin b/build/OwOS.bin index 9e41ad3..d9402b5 100755 Binary files a/build/OwOS.bin and b/build/OwOS.bin differ diff --git a/build/OwOS.iso b/build/OwOS.iso index 604d1a6..d4a0ab8 100644 Binary files a/build/OwOS.iso and b/build/OwOS.iso differ diff --git a/iso/boot/OwOS.bin b/iso/boot/OwOS.bin index 1ee74e1..d9402b5 100644 Binary files a/iso/boot/OwOS.bin and b/iso/boot/OwOS.bin differ diff --git a/kernel.asm b/kernel.asm index df88945..bcf65a5 100644 --- a/kernel.asm +++ b/kernel.asm @@ -37,10 +37,10 @@ _start: jmp 1b -.global ASM_INIT_FPU -.type ASM_INIT_FPU, @function +.global INIT_FPU +.type INIT_FPU, @function -ASM_INIT_FPU: +INIT_FPU: # FPU Config VAL_037F: .hword 0x037F diff --git a/kernel.cpp b/kernel.cpp index 016c514..c342aa8 100644 --- a/kernel.cpp +++ b/kernel.cpp @@ -1,10 +1,17 @@ -#include #include -#include + #include +#include +#include + +#include +#include +#include + +#define BochsBreak() outw(0x8A00,0x8A00); outw(0x8A00,0x08AE0); extern "C" { - extern void ASM_INIT_FPU(void); + extern void INIT_FPU(void); } extern "C" @@ -13,23 +20,29 @@ int kernel_main(uint32_t magic, multibootInfo_t* multiboot) { cls(); showCursor(); setFGColour(VGA_GREEN); - write("OwOS V0.2 "); - writeln("Starting Up..."); + writeln("OwOS V0.2 Starting Up..."); setFGColour(VGA_WHITE); nline(); + // Init systems - write("OwO, What's This? "); - write("*notices "); - write(itoa(multiboot->mem_upper / 1024)); - write("mb of ram*"); + initGDT(); + loggerOK("GDT Loaded"); + + + + nline(); nline(); + write("OwO, What's This?"); setFGColour(VGA_BRIGHT_MAGENTA); - nline(); nline(); nline(); - - - ASM_INIT_FPU(); - + write(" *notices "); + write(itoa(multiboot->mem_upper / 1024)); + writeln("mb of ram*"); + setFGColour(VGA_WHITE); + nline(); + setFGColour(VGA_YELLOW); + writeln("Welcome to OwOS"); + setFGColour(VGA_BRIGHT_MAGENTA); write("~#"); for (;;) diff --git a/kernel/gdt.cpp b/kernel/gdt.cpp index e69de29..1b670f3 100644 --- a/kernel/gdt.cpp +++ b/kernel/gdt.cpp @@ -0,0 +1,50 @@ +#include "gdt.h" + +#include +#include + +struct SegmentDescriptor_t { + uint16_t limit_0; + uint16_t base_0; + uint8_t base_1; + uint8_t access; + uint8_t limit_1:4; + uint8_t flags:4; + uint8_t base_2; +} __attribute__((packed)); + +SegmentDescriptor_t _GDT[256]; + +void lgdt(void* GDT, uint16_t size) { + struct { + uint16_t size; + void* GDT; + } __attribute__((packed)) GDTR = { size, GDT }; + + asm ( "lgdt %0" : : "m"(GDTR) ); +} + +void initGDT() { + memset(&_GDT, 0, sizeof(_GDT)); + + // Leave 0 blank for null segment + // Code Base 0 limit 32 Access EXEC + RW + setGDT(1, 0x00000000, 0xFFFFFFFF, GDT_ACCESS_PRESENT | GDT_ACCESS_EXEC | GDT_ACCESS_RW, GDT_FLAG_GR_PAGE | GDT_FLAG_SZ_32B); + // Data Base 0 Limit 32 Access RW + setGDT(2, 0x00000000, 0xFFFFFFFF, GDT_ACCESS_PRESENT | GDT_ACCESS_RW, GDT_FLAG_GR_PAGE | GDT_FLAG_SZ_32B); + + lgdt(&_GDT, sizeof(_GDT)); + SEGMENTS_RELOAD(); +} + +void setGDT(uint32_t index, uint32_t baseAddr, uint32_t limitAddr, uint8_t accessLvl, uint8_t flags) { + if (index > 256) return; + _GDT[index].base_0 = (baseAddr >> 0) & 0xFFFF; + _GDT[index].base_1 = (baseAddr >> 16) & 0xFF; + _GDT[index].base_2 = (baseAddr>> 24) & 0xFF; + _GDT[index].limit_0 = (limitAddr >> 0) & 0xFFFF; + _GDT[index].limit_1 = (limitAddr >> 16) & 0x0F; + _GDT[index].access = accessLvl; + _GDT[index].flags = flags & 0x0F; +} + diff --git a/kernel/gdt.h b/kernel/gdt.h index e69de29..e3af32c 100644 --- a/kernel/gdt.h +++ b/kernel/gdt.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +#define GDT_ACCESS_PRESENT 0x90 +#define GDT_ACCESS_PR_TSS 0x80 +#define GDT_ACCESS_PRIV_3 0x60 +#define GDT_ACCESS_PRIV_2 0x40 +#define GDT_ACCESS_PRIV_1 0x20 +#define GDT_ACCESS_EXEC 0x08 +#define GDT_ACCESS_DIR_D 0x04 +#define GDT_ACCESS_RW 0x02 +#define GDT_ACCESS_ACCESSED 0x01 + +#define GDT_FLAG_GR_PAGE 0x08 +#define GDT_FLAG_SZ_32B 0x04 + +extern "C" { + extern void SEGMENTS_RELOAD(void); +} + +void initGDT(); +void setGDT(uint32_t index, uint32_t baseAddr, uint32_t limitAddr, uint8_t accessLvl, uint8_t flags); diff --git a/kernel/kernio.cpp b/kernel/kernio.cpp deleted file mode 100644 index 17c0573..0000000 --- a/kernel/kernio.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "kernio.h" diff --git a/kernel/kernio.h b/kernel/kernio.h index aa4d0c4..440fdd0 100644 --- a/kernel/kernio.h +++ b/kernel/kernio.h @@ -14,3 +14,33 @@ inline uint8_t inb(uint16_t port) { return ret; } +static inline uint16_t inw(uint16_t port) { + uint16_t ret; + asm ( "inw %1, %0" + : "=a"(ret) + : "Nd"(port) ); + return ret; +} + +static inline void outw(uint16_t port, uint16_t val) { + asm ( "outw %0, %1" : : "a"(val), "Nd"(port) ); +} + +static inline uint32_t inl(uint16_t port) { + uint32_t ret; + asm ( "inl %1, %0" + : "=a"(ret) + : "Nd"(port) ); + return ret; +} + +static inline void outl(uint16_t port, uint32_t val) { + asm ( "outl %0, %1" : : "a"(val), "Nd"(port) ); +} + +static inline void io_wait(void) { + /* TODO: This is probably fragile. */ + asm ( "jmp 1f\n\t" + "1:jmp 2f\n\t" + "2:" ); +} diff --git a/kernel/segment.asm b/kernel/segment.asm new file mode 100644 index 0000000..cc491bf --- /dev/null +++ b/kernel/segment.asm @@ -0,0 +1,13 @@ +.global SEGMENTS_RELOAD + +SEGMENTS_RELOAD: + ljmp $0x08, $reloadCS + +reloadCS: + movw $0x10, %ax + movw %ax, %es + movw %ax, %ss + movw %ax, %ds + movw %ax, %fs + movw %ax, %gs + ret diff --git a/lib/kernel/logger/logger.cpp b/lib/kernel/logger/logger.cpp index e69de29..4a4995a 100644 --- a/lib/kernel/logger/logger.cpp +++ b/lib/kernel/logger/logger.cpp @@ -0,0 +1,26 @@ +#include "logger.h" + +#include + +void loggerLog(char* str) { + write(" "); + writeln(str); +} + +void loggerOK(char* str) { + write("["); + setFGColour(VGA_BRIGHT_GREEN); + write(" OK "); + setFGColour(VGA_WHITE); + write("] "); + writeln(str); +} + +void loggerFailed(char* str) { + write("["); + setFGColour(VGA_BRIGHT_RED); + write("FAILED"); + setFGColour(VGA_WHITE); + write("] "); + writeln(str); +} diff --git a/lib/kernel/logger/logger.h b/lib/kernel/logger/logger.h index 269272f..30713ca 100644 --- a/lib/kernel/logger/logger.h +++ b/lib/kernel/logger/logger.h @@ -1,4 +1,8 @@ // [FAILED] // [ OK ] +#pragma once +void loggerLog(char* str); +void loggerOK(char* str); +void loggerFailed(char* str);