diff --git a/CMakeLists.txt b/CMakeLists.txt index 56a3195..e25d0a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,140 +1,128 @@ -cmake_minimum_required(VERSION 3.13.4) -project(ClassicOS VERSION 0.0.1 LANGUAGES C CXX ASM_NASM) - -set(IS_OS_WINDOWS FALSE) -set(IS_OS_LINUX FALSE) -set(IS_OS_MAC FALSE) - -if (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") - set(IS_OS_WINDOWS TRUE) - message("Building on the Windows operating system.") -elseif (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux") - set(IS_OS_LINUX TRUE) - message("Building on the Linux operating system.") -elseif (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin") - set(IS_OS_MAC TRUE) - message("Building on the Mac operating system.") -endif () - -# Source files -set(BOOT_SOURCE_FILES - src/boot/boot.asm - src/boot/linker.ld -) - -#[[ -set(GRUB_SOURCE_FILES - src/boot/grub/grub.cfg - src/boot/grub/menu.lst -) -]] - -set(DRIVERS_SOURCE_FILES - src/drivers/audio/audio.c - src/drivers/audio/audio.h - src/drivers/bus/eisa.c - src/drivers/bus/eisa.h - src/drivers/bus/isa.c - src/drivers/bus/isa.h - src/drivers/bus/mca.c - src/drivers/bus/mca.h - #src/drivers/bus/pci.asm - src/drivers/bus/pci.c - src/drivers/bus/pci.h - src/drivers/bus/vesa.c - src/drivers/bus/vesa.h - src/drivers/display/display.c - src/drivers/display/display.h - src/drivers/io/io.c - src/drivers/io/io.asm - src/drivers/io/io.h - src/drivers/keyboard/keyboard.c - src/drivers/keyboard/keyboard.h - src/drivers/screen/screen.c - src/drivers/screen/screen.h - src/drivers/tty/tty.c - src/drivers/tty/tty.h -) -set(KERNEL_SOURCE_FILES - src/kernel/arch/x86/include/memory.h - src/kernel/arch/x86/include/types.h - src/kernel/arch/x86/include/types.h - src/kernel/arch/x86/memory/memory.c - src/kernel/arch/x86/gdt.c - src/kernel/arch/x86/gdt.c - src/kernel/arch/x86/idt.c - src/kernel/arch/x86/idt.asm - src/kernel/arch/x86/idt.h - src/kernel/arch/x86/isr/isr.c - src/kernel/arch/x86/isr/isr.h - src/kernel/arch/x86/isr/exceptions.c - src/kernel/arch/x86/isr/exceptions.h - src/kernel/kernel.c - src/kernel/kernel.h - src/kernel/linker.ld - src/kernel/print.c -) - -set(UTIL_SOURCE_FILES - src/EHS.h - src/sys/CPU.h src/sys/CPU.cpp src/sys/CPU_GCC_AMD64.asm - src/Util.h src/Util.cpp - src/Version.h src/Version.cpp - src/Serializer.h - src/Array.h - src/Vector.h - src/SArray.h - src/Str.h - src/PRNG.h - src/HRNG.h src/HRNG_GCC.s - src/Math.h src/Math.cpp src/Math_GCC_AMD64.s - src/Rect.h - src/Range.h src/Range.cpp - src/Color4.h src/Color4.cpp - src/Color3.h src/Color3.cpp - src/Quat.h - src/Vec4.h - src/Vec3.h - src/Vec2.h - src/Mat4.h - src/Mat3.h - src/Mat2.h -) - -add_executable(ClassicOS - ${GRUB_SOURCE_FILES} - ${DRIVERS_SOURCE_FILES} - ${KERNEL_SOURCE_FILES} -) - -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - -# Enable testing -enable_testing() - -target_link_libraries(ClassicOS PRIVATE) - - -set(CMAKE_SYSTEM_PROCESSOR i386) -set(CMAKE_SYSTEM_NAME None) -set(CMAKE_C_STANDARD 17) -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_BUILD_TYPE Debug) -set(CMAKE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) -set(CMAKE_BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build) - -if (IS_OS_LINUX) - set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -f elf") - set(CMAKE_C_COMPILER gcc) - set(CMAKE_LINKER ld) - set(CMAKE_EXE_LINKER_FLAGS "-g -s") - set(CMAKE_LINK_FLAGS "${CMAKE_LINK_FLAGS} -e kernel_main") - set(CMAKE_CXX_FLAGS "-g -Wall") - set(CMAKE_C_FLAGS "-g -Wall -m32") - set(CMAKE_C_FLAGS_DEBUG "-g") - set(CMAKE_GDB_COMMAND gdb) - set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") - set_target_properties(ClassicOS PROPERTIES LINK_FLAGS "-T ${CMAKE_CURRENT_SOURCE_DIR}/linker.ld") -elseif (IS_OS_WINDOWS) - set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -f win32") +cmake_minimum_required(VERSION 3.13.4) + +set(CMAKE_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/src) +set(CMAKE_BINARY_DIR ${CMAKE_CURRENT_LIST_DIR}/build) + +include(x86-baremetal-toolchain.cmake) + +project(ClassicOS VERSION 0.0.1 LANGUAGES C CXX ASM_NASM) + +set(IS_OS_WINDOWS FALSE) +set(IS_OS_LINUX FALSE) +set(IS_OS_MAC FALSE) + +if (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") + set(IS_OS_WINDOWS TRUE) + message("Building on the Windows operating system.") +elseif (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux") + set(IS_OS_LINUX TRUE) + message("Building on the Linux operating system.") +elseif (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin") + set(IS_OS_MAC TRUE) + message("Building on the Mac operating system.") +endif () + +# Source files +set(BOOT_SOURCE_FILES + src/boot/boot.asm + src/boot/linker.ld +) + +#[[ +set(GRUB_SOURCE_FILES + src/boot/grub/grub.cfg + src/boot/grub/menu.lst +) +]] + +set(DRIVERS_SOURCE_FILES + src/drivers/audio/audio.c + src/drivers/audio/audio.h + src/drivers/bus/eisa.c + src/drivers/bus/eisa.h + src/drivers/bus/isa.c + src/drivers/bus/isa.h + src/drivers/bus/mca.c + src/drivers/bus/mca.h + #src/drivers/bus/pci.asm + src/drivers/bus/pci.c + src/drivers/bus/pci.h + src/drivers/bus/vesa.c + src/drivers/bus/vesa.h + src/drivers/display/display.c + src/drivers/display/display.h + src/drivers/io/io.c + src/drivers/io/io.asm + src/drivers/io/io.h + src/drivers/keyboard/keyboard.c + src/drivers/keyboard/keyboard.h + src/drivers/screen/screen.c + src/drivers/screen/screen.h + src/drivers/tty/tty.c + src/drivers/tty/tty.h +) +set(KERNEL_SOURCE_FILES + src/kernel/arch/x86/include/memory.h + src/kernel/arch/x86/include/types.h + src/kernel/arch/x86/include/types.h + src/kernel/arch/x86/memory/memory.c + src/kernel/arch/x86/gdt.c + src/kernel/arch/x86/gdt.asm + src/kernel/arch/x86/gdt.h + src/kernel/arch/x86/idt.c + src/kernel/arch/x86/idt.asm + src/kernel/arch/x86/idt.h + src/kernel/arch/x86/isr/isr.c + src/kernel/arch/x86/isr/isr.h + src/kernel/arch/x86/isr/exceptions.c + src/kernel/arch/x86/isr/exceptions.h + src/kernel/kernel.c + src/kernel/kernel.h + src/kernel/linker.ld + src/kernel/print.c + src/kernel/stack.c + src/kernel/stack.h +) + +set(UTIL_SOURCE_FILES + src/EHS.h + src/sys/CPU.h src/sys/CPU.cpp src/sys/CPU_GCC_AMD64.asm + src/Util.h src/Util.cpp + src/Version.h src/Version.cpp + src/Serializer.h + src/Array.h + src/Vector.h + src/SArray.h + src/Str.h + src/PRNG.h + src/HRNG.h src/HRNG_GCC.s + src/Math.h src/Math.cpp src/Math_GCC_AMD64.s + src/Rect.h + src/Range.h src/Range.cpp + src/Color4.h src/Color4.cpp + src/Color3.h src/Color3.cpp + src/Quat.h + src/Vec4.h + src/Vec3.h + src/Vec2.h + src/Mat4.h + src/Mat3.h + src/Mat2.h +) + +add_executable(ClassicOS + ${GRUB_SOURCE_FILES} + ${KERNEL_SOURCE_FILES} + ${DRIVERS_SOURCE_FILES} +) + +if (IS_OS_LINUX) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32") + set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -f elf") + set(CMAKE_EXE_LINKER_FLAGS "-g -s") + set(CMAKE_LINK_FLAGS "${CMAKE_LINK_FLAGS} -e kernel_main") + + #set_target_properties(ClassicOS PROPERTIES LINK_FLAGS "-T ${CMAKE_CURRENT_SOURCE_DIR}/linker.ld") +elseif (IS_OS_WINDOWS) + set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -f win32") endif () \ No newline at end of file diff --git a/src/drivers/bus/eisa.c b/src/drivers/bus/eisa.c index 8fe35a0..b9a809c 100644 --- a/src/drivers/bus/eisa.c +++ b/src/drivers/bus/eisa.c @@ -51,8 +51,8 @@ void eisa_detect_devices() // This is my device, configure it uint32_t config1 = eisa_read_config_dword(address, 4); uint32_t config2 = eisa_read_config_dword(address, 8); - printf("Config1: %u\n", config1); - printf("Config2: %u\n", config2); + //printf("Config1: %u\n", config1); + //printf("Config2: %u\n", config2); // Do something with the configuration data } } diff --git a/src/drivers/io/io.asm b/src/drivers/io/io.asm index 92bae92..23b4e30 100644 --- a/src/drivers/io/io.asm +++ b/src/drivers/io/io.asm @@ -1,6 +1,7 @@ global inb global outb +section .text inb: MOV DX, WORD [ESP + 16] IN AL, DX diff --git a/src/drivers/keyboard/keyboard.c b/src/drivers/keyboard/keyboard.c index f7191fd..589c7a0 100644 --- a/src/drivers/keyboard/keyboard.c +++ b/src/drivers/keyboard/keyboard.c @@ -34,8 +34,8 @@ void KeyboardInterruptHandler() void keyboard_init() { // Install keyboard interrupt handler - set_interrupt_vector(KEYBOARD_INTERRUPT_VECTOR, KeyboardInterruptHandler); - enable_interrupt(KEYBOARD_INTERRUPT_VECTOR); + //set_interrupt_vector(KEYBOARD_INTERRUPT_VECTOR, KeyboardInterruptHandler); + //enable_interrupt(KEYBOARD_INTERRUPT_VECTOR); // Enable keyboard outb(KEYBOARD_COMMAND_PORT, KEYBOARD_ENABLE_COMMAND); diff --git a/src/drivers/tty/tty.c b/src/drivers/tty/tty.c index 7287f00..60efe05 100644 --- a/src/drivers/tty/tty.c +++ b/src/drivers/tty/tty.c @@ -16,19 +16,19 @@ // Example function to print a message to the console void tty_print(const char *message) { - printf("%s", message); + //printf("%s", message); } // Example function to read input from the console void tty_read(char *buffer, size_t size) { - fgets(buffer, size, stdin); + //fgets(buffer, size, stdin); } // Example function to clear the console void tty_clear() { - printf("\033[2J\033[H"); + //printf("\033[2J\033[H"); } // More function definitions... diff --git a/src/kernel/arch/x86/gdt.asm b/src/kernel/arch/x86/gdt.asm new file mode 100644 index 0000000..688f1ed --- /dev/null +++ b/src/kernel/arch/x86/gdt.asm @@ -0,0 +1,6 @@ +global LoadGDT + +section .text + LoadGDT: + LGDT [ESP + 32] + RET \ No newline at end of file diff --git a/src/kernel/arch/x86/gdt.c b/src/kernel/arch/x86/gdt.c index 752592f..202f7bf 100644 --- a/src/kernel/arch/x86/gdt.c +++ b/src/kernel/arch/x86/gdt.c @@ -38,6 +38,8 @@ enum GDT_BASE_LIMIT GDT_LIMIT_MASK = 0xFFFF }; +extern void LoadGDT(struct gdt_ptr* gdt); + // Initialize a GDT entry void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran, struct gdt_entry *const gdt) @@ -57,8 +59,8 @@ void gdt_init() // Set up GDT pointer struct gdt_ptr gp; gp.limit = (sizeof(struct gdt_entry) * 3) - 1; - gdt = (struct gdt_entry *)malloc(sizeof(struct gdt_entry) * 3); - memset(gdt, 0, sizeof(struct gdt_entry) * 3); + //gdt = (struct gdt_entry *)malloc(sizeof(struct gdt_entry) * 3); + //memset(gdt, 0, sizeof(struct gdt_entry) * 3); // Initialize GDT entries gdt_set_gate(0, 0, 0, 0, 0, gdt); // Null segment @@ -69,7 +71,10 @@ void gdt_init() struct gdt_ptr gdtp; gdtp.limit = gp.limit; gdtp.base = (uintptr_t)gdt; - __asm__ volatile("lgdt %0" : : "m"(gdtp)); + + LoadGDT(&gdtp); + + /* __asm__ volatile("mov $0x10, %ax\n\t" "mov %ax, %ds\n\t" "mov %ax, %es\n\t" @@ -77,6 +82,7 @@ void gdt_init() "mov %ax, %gs\n\t" "ljmp $0x08, $next_label\n\t" "next_label:"); + */ } // Exception handlers diff --git a/src/kernel/arch/x86/idt.asm b/src/kernel/arch/x86/idt.asm index 70c6361..3ca593a 100644 --- a/src/kernel/arch/x86/idt.asm +++ b/src/kernel/arch/x86/idt.asm @@ -1,5 +1,6 @@ global LoadIDT +section .text LoadIDT: LIDT [ESP + 32] RET \ No newline at end of file diff --git a/src/kernel/arch/x86/isr/exceptions.c b/src/kernel/arch/x86/isr/exceptions.c index 2cde176..0b35e7b 100644 --- a/src/kernel/arch/x86/isr/exceptions.c +++ b/src/kernel/arch/x86/isr/exceptions.c @@ -4,20 +4,20 @@ void DivideByZero() { - printf("Divide By Zero Exception"); + //printf("Divide By Zero Exception"); } void DoubleFault() { - printf("Double Fault Exception"); + //printf("Double Fault Exception"); } void PageFault() { - printf("Page Fault Exception"); + //printf("Page Fault Exception"); } void GeneralProtectionFault() { - printf("General Protection Fault Exception"); + //printf("General Protection Fault Exception"); } \ No newline at end of file diff --git a/src/kernel/print.c b/src/kernel/print.c index bb3f87b..14a3cf4 100644 --- a/src/kernel/print.c +++ b/src/kernel/print.c @@ -3,5 +3,5 @@ void print_string(const char *str) { - printf("%s", str); + //printf("%s", str); } \ No newline at end of file diff --git a/src/kernel/stack.c b/src/kernel/stack.c new file mode 100644 index 0000000..0192d05 --- /dev/null +++ b/src/kernel/stack.c @@ -0,0 +1,10 @@ +#include "stack.h" + +void __attribute__((noreturn)) __stack_chk_fail_local(void) +{ + // Your code to handle stack corruption + // This could be logging the error, halting the system, etc. + while(1) { + // Infinite loop to halt execution + } +} \ No newline at end of file diff --git a/src/kernel/stack.h b/src/kernel/stack.h new file mode 100644 index 0000000..7006d22 --- /dev/null +++ b/src/kernel/stack.h @@ -0,0 +1,6 @@ +#ifndef STACK_H +#define STACK_H + +void __attribute__((noreturn)) __stack_chk_fail_local(void); + +#endif \ No newline at end of file diff --git a/x86-baremetal-toolchain.cmake b/x86-baremetal-toolchain.cmake new file mode 100644 index 0000000..7a2cece --- /dev/null +++ b/x86-baremetal-toolchain.cmake @@ -0,0 +1,15 @@ +# x86-baremetal-toolchain.cmake + +# Specify the cross-compiler (If you need one; otherwise, your native GCC should work) +set(CMAKE_ASM_COMPILER nasm) +set(CMAKE_C_COMPILER gcc) +set(CMAKE_CXX_COMPILER g++) + +set(CMAKE_C_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) + +# Add compiler and linker flags for x86 bare metal +set(CMAKE_ASM_NASM_FLAGS "-f elf32") +set(CMAKE_C_FLAGS "-m32 -ffreestanding -nostdlib") +set(CMAKE_CXX_FLAGS "-m32 -ffreestanding -nostdlib") +set(CMAKE_EXE_LINKER_FLAGS "-T${CMAKE_CURRENT_LIST_DIR}/linker.ld -m32") \ No newline at end of file