diff --git a/CMakeLists.txt b/CMakeLists.txt index 6686564..7fad308 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,26 +9,46 @@ project(chroma) SET(src_files - chroma/kernel.c - chroma/video/draw.c - chroma/video/print.c - chroma/system/cpu.c - chroma/system/rw.c - chroma/system/serial.c - chroma/system/memory/paging.c - chroma/system/memory/physmem.c - chroma/system/drivers/keyboard.c + ${CMAKE_SOURCE_DIR}/chroma/kernel.c + ${CMAKE_SOURCE_DIR}/chroma/video/draw.c + ${CMAKE_SOURCE_DIR}/chroma/video/print.c + ${CMAKE_SOURCE_DIR}/chroma/system/cpu.c + ${CMAKE_SOURCE_DIR}/chroma/system/rw.c + ${CMAKE_SOURCE_DIR}/chroma/system/serial.c + ${CMAKE_SOURCE_DIR}/chroma/system/pci.c + ${CMAKE_SOURCE_DIR}/chroma/system/memory/paging.c + ${CMAKE_SOURCE_DIR}/chroma/system/memory/physmem.c + ${CMAKE_SOURCE_DIR}/chroma/system/drivers/keyboard.c ) include_directories("chroma/inc") SET(src_no_sse - chroma/system/interrupts.c + ${CMAKE_SOURCE_DIR}/chroma/system/interrupts.c +) + +SET(src_preamble + ${CMAKE_SOURCE_DIR}/global/crt0.o + ${CMAKE_SOURCE_DIR}/global/crti.o + ${CMAKE_SOURCE_DIR}/global/crtbegin.o +) + +set(src_epilogue + ${CMAKE_SOURCE_DIR}/global/crtend.o + ${CMAKE_SOURCE_DIR}/global/crtn.o ) set_property(SOURCE ${src_no_sse} PROPERTY COMPILE_FLAGS -mgeneral-regs-only) -add_executable(kernel.elf ${src_files} ${src_no_sse} font.o) -target_compile_options(kernel.elf PRIVATE -ffreestanding -O2 -Wall -Wextra -Wall -Werror -pedantic -fPIC) -target_link_options(kernel.elf PRIVATE -T linker.ld -ffreestanding -O2 -nostdlib -lgcc) +add_executable(kernel) +target_sources(kernel PUBLIC ${src_preamble} PUBLIC ${src_files} PUBLIC ${src_no_sse} PUBLIC ${CMAKE_SOURCE_DIR}/font.o PUBLIC ${src_epilogue}) +target_compile_options(kernel PRIVATE -ffreestanding -O2 -Wall -Wextra -Wall -Werror -pedantic -fPIC -fno-exceptions) +target_link_options(kernel PRIVATE -T linker.ld -ffreestanding -O2 -nostdlib -nostartfiles -lgcc) + +add_custom_command( + TARGET kernel + POST_BUILD + COMMAND cp ${CMAKE_SOURCE_DIR}/kernel ${CMAKE_SOURCE_DIR}/img/boot/exe + COMMAND ${CMAKE_SOURCE_DIR}/mkbootimg chroma.json chroma.img +) \ No newline at end of file diff --git a/chroma/inc/kernel/chroma.h b/chroma/inc/kernel/chroma.h index b9e1eb7..e22fa31 100644 --- a/chroma/inc/kernel/chroma.h +++ b/chroma/inc/kernel/chroma.h @@ -58,3 +58,7 @@ void InitPrint(); void SetupInitialGDT(); void SetupIDT(); + +int Main(); + +void Exit(); diff --git a/chroma/kernel.c b/chroma/kernel.c index 9205766..b82b5c2 100644 --- a/chroma/kernel.c +++ b/chroma/kernel.c @@ -16,12 +16,16 @@ size_t KernelAddr = (size_t) &LoadAddr; size_t KernelEnd = (size_t) &end; -void _start(void) { +int Main(void) { SerialPrintf("\r\nBooting Chroma..\r\n"); SerialPrintf("Kernel loaded at 0x%p, ends at 0x%p, is %d bytes long.\r\n", KernelAddr, KernelEnd, KernelEnd - KernelAddr); + SerialPrintf("The bootloader has put the paging tables at 0x%p.\r\n", ReadControlRegister(3)); + + TraversePageTables(); + ListMemoryMap(); InitPrint(); @@ -41,5 +45,13 @@ void _start(void) { for(;;) { } + + return 0; } + + +void Exit(int ExitCode) { + SerialPrintf("Kernel stopped with code %x\r\n", ExitCode); + +} \ No newline at end of file diff --git a/global/a.out b/global/a.out new file mode 100644 index 0000000..ee9c269 Binary files /dev/null and b/global/a.out differ diff --git a/global/crt0.c b/global/crt0.c new file mode 100644 index 0000000..5f3ba44 --- /dev/null +++ b/global/crt0.c @@ -0,0 +1,11 @@ + +extern void Exit(int code); +extern int Main(); + +extern void _init(); + +void _start() { + _init(); + int ex = Main(); + Exit(ex); +} \ No newline at end of file diff --git a/global/crt0.o b/global/crt0.o new file mode 100644 index 0000000..af9b40e Binary files /dev/null and b/global/crt0.o differ diff --git a/global/crt0.s b/global/crt0.s new file mode 100644 index 0000000..d50d2cd --- /dev/null +++ b/global/crt0.s @@ -0,0 +1,10 @@ +.section .text +.global _start +.global _init +.global main +.global _fini +.type _start, @function +_start: + call _init + call main + call _fini \ No newline at end of file diff --git a/global/crtbegin.o b/global/crtbegin.o new file mode 100644 index 0000000..72d5531 Binary files /dev/null and b/global/crtbegin.o differ diff --git a/global/crtend.o b/global/crtend.o new file mode 100644 index 0000000..1e31c40 Binary files /dev/null and b/global/crtend.o differ diff --git a/global/crti.o b/global/crti.o new file mode 100644 index 0000000..4407887 Binary files /dev/null and b/global/crti.o differ diff --git a/global/crti.s b/global/crti.s new file mode 100644 index 0000000..e9ab988 --- /dev/null +++ b/global/crti.s @@ -0,0 +1,13 @@ +.section .init +.global _init +.type _init, @function +_init: + push %rbp + movq %rsp, %rbp + +.section .fini +.global _fini +.type _fini, @function +_fini: + push %rbp + movq %rsp, %rbp \ No newline at end of file diff --git a/global/crtn.o b/global/crtn.o new file mode 100644 index 0000000..7223e2f Binary files /dev/null and b/global/crtn.o differ diff --git a/global/crtn.s b/global/crtn.s new file mode 100644 index 0000000..9c0665c --- /dev/null +++ b/global/crtn.s @@ -0,0 +1,9 @@ +.section .init + + popq %rbp + ret + +.section .fini + + popq %rbp + ret \ No newline at end of file diff --git a/linker.ld b/linker.ld index 6e0c8d4..b2b153f 100644 --- a/linker.ld +++ b/linker.ld @@ -26,7 +26,13 @@ SECTIONS *(COMMON) } :boot - /DISCARD/ : { *(.eh_frame) *(.comment) } + .eh_frame_hdr : { + *(.eh_frame_hdr) + *(.eh_frame) + *(.gcc_except_table) + } :boot + + /DISCARD/ : {*(.comment) } end = .; }