Fixed all errors in OS.

This commit is contained in:
Arron David Nelson 2023-10-31 16:21:54 -07:00
parent 07c0c99a48
commit cd323834ff
13 changed files with 187 additions and 154 deletions

View File

@ -1,4 +1,10 @@
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)
@ -61,7 +67,8 @@ set(KERNEL_SOURCE_FILES
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/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
@ -73,6 +80,8 @@ set(KERNEL_SOURCE_FILES
src/kernel/kernel.h
src/kernel/linker.ld
src/kernel/print.c
src/kernel/stack.c
src/kernel/stack.h
)
set(UTIL_SOURCE_FILES
@ -103,38 +112,17 @@ set(UTIL_SOURCE_FILES
add_executable(ClassicOS
${GRUB_SOURCE_FILES}
${DRIVERS_SOURCE_FILES}
${KERNEL_SOURCE_FILES}
${DRIVERS_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_C_FLAGS "${CMAKE_C_FLAGS} -m32")
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")
#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 ()

View File

@ -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
}
}

View File

@ -1,6 +1,7 @@
global inb
global outb
section .text
inb:
MOV DX, WORD [ESP + 16]
IN AL, DX

View File

@ -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);

View File

@ -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...

View File

@ -0,0 +1,6 @@
global LoadGDT
section .text
LoadGDT:
LGDT [ESP + 32]
RET

View File

@ -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

View File

@ -1,5 +1,6 @@
global LoadIDT
section .text
LoadIDT:
LIDT [ESP + 32]
RET

View File

@ -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");
}

View File

@ -3,5 +3,5 @@
void print_string(const char *str)
{
printf("%s", str);
//printf("%s", str);
}

10
src/kernel/stack.c Normal file
View File

@ -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
}
}

6
src/kernel/stack.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef STACK_H
#define STACK_H
void __attribute__((noreturn)) __stack_chk_fail_local(void);
#endif

View File

@ -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")