diff --git a/CMakeLists.txt b/CMakeLists.txt index c5d015f..dd939d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,15 +3,19 @@ project(secX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11") -file(GLOB_RECURSE hal_src ./hal/*.h ./hal/*.c ./common/*.h ./common/*.c) -file(GLOB_RECURSE kernel_src ./kernel/*.h ./kernel/*.c ./common/*.h ./common/*.c) -file(GLOB_RECURSE test_src ./test/*.h ./test/*.c ./common/*.h ./common/*.c) +file(GLOB_RECURSE hal_src ./inc/*.h ./hal/*.c ./hal/*.h ./common/*.c) +file(GLOB_RECURSE kernel_src ./inc/*.h ./kernel/*.c ./kernel/*.h ./common/*.c) +file(GLOB_RECURSE test_src ./inc/*.h ./test/*.c ./test/*.h ./common/*.c) include_directories(inc) -#KERNEL + HAL +# HAL +#include_directories(hal/inc) +#add_executable(hal ${hal_src}) + + +# KERNEL include_directories(kernel/inc) -include_directories(hal/inc) -add_executable(kernel ${kernel_src} ${hal_src}) +add_executable(kernel ${kernel_src}) # KERNEL + TESTS #include_directories(test/inc) diff --git a/Makefile b/Makefile index 9fa0f8f..b876a2e 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,8 @@ OUT := out C_IGNORED_WARNINGS = -Wno-cast-align \ -Wno-padded +# generic freestanding cflags used for target +# each submodule can append to this flag C_FLAGS = -xc\ -g \ -c \ @@ -31,32 +33,28 @@ C_FLAGS = -xc\ -mno-sse3 \ -mno-3dnow \ -target x86_64-pc-none-elf \ - -I$(INC_COMMON) + -I$(INC_COMMON) \ + $(C_FLAGS_$(MOD)) +# generic asm flags used for target +# each submodule can append to this flag AS_FLAGS = -w+all \ -w+error \ -f elf64 \ -F dwarf \ -g \ -I$(INC_COMMON) \ - $(INC_$(d)) - -LD_FLAGS = -fuse-ld=lld \ - -nostdlib \ - -Wl,-T,$(LD_SCRIPT) \ - -Wl,--fatal-warnings - -DUMP_FLAGS = -x86-asm-syntax=intel \ - -disassemble \ - -r \ - -t \ - -triple=x86_64-elf + $(AS_FLAGS_$(MOD)) +# generic pre-processing flags used for target PREP_FLAGS = -E \ -xc\ -P \ - $(C_FLAGS) + -I$(INC_COMMON) \ + $(C_FLAGS_$(MOD)) +# generic generate dependency flags used for target +# each submodule can append to this flag GDEP_FLAGS = $(PREP_FLAGS) \ -MMD \ -MT $@ @@ -64,8 +62,6 @@ GDEP_FLAGS = $(PREP_FLAGS) \ MKDIR = mkdir -p $(dir $@) COMP = $(CC) $(C_FLAGS) -o $@ $< COMPAS = $(AS) $(AS_FLAGS) -o $@ $< -LINK = $(LD) $(LD_FLAGS) -o $@ $^ -DUMP = $(DAS) $(DUMP_FLAGS) $< > $@ PREP = $(CC) $(PREP_FLAGS) $< > $@ GDEP = $(CC) $(GDEP_FLAGS) -MF $(addsuffix .d, $@) $< > /dev/null diff --git a/README.md b/README.md index e7b40f5..68be90d 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,24 @@ All platforms where the required packages are available. ### Required packages -nasm, clang (6.0+), make +For compiling kernel only (make compile): + +nasm, clang, lld, llvm + +To make bootable ISO (make all): + +xorriso; grub-pc-bin for bios; grub-efi-amd64-bin, mtools for UEFI. ### Compiling -Run "make" in the root directory. +Run "make all" or "make compile" in the root directory. -This will generate secxkrnl.elf (kernel executable) and secxkrnl.dmp (kernel dump). +This will generate secxkrnl.elf, secxkrnl.dmp, (and secxkrnl.iso) in "out" folder Run "make clean" to clean a build. -# Running -secX requires bootloader [secboot](https://github.com/secXsQuared/secboot). -See secboot repository for more information. +# Running +Load the iso with your favorite simulator or use "-kernel" option with QEMU. + +For UEFI simulation, use qemu_bios.bin in the root dir with QEMU. + diff --git a/Rules.top b/Rules.top index c83e8ad..a33739f 100644 --- a/Rules.top +++ b/Rules.top @@ -2,7 +2,7 @@ include $(MK)/prologue.mk .DEFAULT_GOAL := all -#OBJ var holds all object files +# OBJ var holds all OBJS required to link the kernel dir := hal include $(dir)/Rules.mk @@ -13,20 +13,43 @@ include $(dir)/Rules.mk dir := common include $(dir)/Rules.mk + LD_SCRIPT := $(OUT)/$(MK)/linker.ld +GRUB_CFG = $(MK)/grub.cfg + TGT := $(OUT)/secxkrnl.elf DMP := $(OUT)/secxkrnl.dmp +ISO := $(OUT)/secxkrnl.iso + +DUMP_FLAGS = -x86-asm-syntax=intel \ + -disassemble \ + -r \ + -t \ + -triple=x86_64-pc-none-elf + +LD_FLAGS = -fuse-ld=lld \ + -nostdlib \ + -Wl,-T,$(LD_SCRIPT) \ + -Wl,--fatal-warnings $(TGT): $(OBJ) $(LD_SCRIPT) - $(LINK) + $(LD) $(LD_FLAGS) -o $@ $^ $(DMP): $(TGT) - $(DUMP) + $(DAS) $(DUMP_FLAGS) $< > $@ + +.PHONY: iso +iso: $(TGT) $(GRUB_CFG) + mkdir -p $(OUT)/temp/secX + mkdir -p $(OUT)/temp/boot + mkdir -p $(OUT)/temp/boot/grub + cp $(TGT) $(OUT)/temp/secX/ + cp $(GRUB_CFG) $(OUT)/temp/boot/grub/ + grub-mkrescue -o $(ISO) $(OUT)/temp .PHONY: clean clean: - rm -f $(CLEAN) $(TGT) $(DMP) $(ISO) - find $(OUT) -empty -type d -delete + rm -rf $(OUT) .PHONY: compile compile: $(TGT) @@ -35,6 +58,6 @@ compile: $(TGT) dump: $(DMP) .PHONY: all -all: compile dump +all: compile dump iso -include $(MK)/epilogue.mk \ No newline at end of file +include $(MK)/epilogue.mk diff --git a/common/Rules.mk b/common/Rules.mk index ba06d24..2340d0a 100644 --- a/common/Rules.mk +++ b/common/Rules.mk @@ -1,6 +1,8 @@ include $(MK)/prologue.mk -SRC_$(d) := $(d)/common.c +MOD:=COMMON + +SRC_$(d) := $(d)/clib.c include $(MK)/stdrules.mk diff --git a/common/common.c b/common/clib.c similarity index 98% rename from common/common.c rename to common/clib.c index 9239847..7211919 100644 --- a/common/common.c +++ b/common/clib.c @@ -1,5 +1,6 @@ -#include "common.h" +#include "cdef.h" +#include "clib.h" void mem_cpy(void *src, void *dst, uint64 size) diff --git a/hal/Rules.mk b/hal/Rules.mk index 33bfddb..067ee76 100644 --- a/hal/Rules.mk +++ b/hal/Rules.mk @@ -1,14 +1,34 @@ include $(MK)/prologue.mk +MOD:=HAL +C_FLAGS_$(MOD):=$(addprefix -I, $(d)/inc) +AS_FLAGS_$(MOD):=$(addprefix -I, $(d)/inc) + SRC_$(d) := $(d)/boot.c \ $(d)/intr.c \ $(d)/mem.c \ - $(d)/print.c + $(d)/print.c \ + $(d)/hal.c SRCAS_$(d) := $(d)/cpu.asm \ - $(d)/intr.asm + $(d)/intr.asm \ + $(d)/io.asm \ + $(d)/atomic.asm -SRCIN_$(d) := $(d)/boot.asm.in +SRCIN_$(d) := $(d)/boot.asm.in \ + $(d)/mb_hdr.asm.in + +#special rules for preprocessed asm files +$(OUT)/$(d)/mb_hdr.a: $(OUT)/$(d)/mb_hdr.asm + $(MKDIR) + $(COMPAS) + +$(OUT)/$(d)/boot.a: $(OUT)/$(d)/boot.asm + $(MKDIR) + $(COMPAS) + +OBJ := $(OBJ) $(OUT)/$(d)/boot.a $(OUT)/$(d)/mb_hdr.a +CLEAN := $(CLEAN) $(OUT)/$(d)/boot.a $(OUT)/$(d)/mb_hdr.a # include this at last include $(MK)/stdrules.mk diff --git a/hal/atomic.asm b/hal/atomic.asm new file mode 100644 index 0000000..827472e --- /dev/null +++ b/hal/atomic.asm @@ -0,0 +1,28 @@ + +section .text +bits 64 +; ============================ +; int32 KAPI hal_interlocked_exchange_32(int32 *target, int32 val) +global hal_interlocked_exchange_32 +hal_interlocked_exchange_32: +lock xchg dword [rdi], esi +xor rax, rax +mov eax, esi +ret + +; ============================ +; int32 KAPI hal_interlocked_compare_exchange_32(int32 *dst, int32 test_node_compare, int32 val); +global hal_interlocked_compare_exchange_32 +hal_interlocked_compare_exchange_32: +mov eax, esi; eax = test_node_compare +lock cmpxchg dword [rdi], edx ; edx = val, rdi = ptr to dst +ret + +; ============================ +; int32 KAPI hal_interlocked_increment_32(int32 *target, int32 increment); +global hal_interlocked_increment_32 +hal_interlocked_increment_32: +lock xadd dword [rdi], esi ; [rdi] = [rdi] + esi, esi = old [rdi] +xor rax, rax +mov eax, esi +ret diff --git a/hal/atomic.c b/hal/atomic.c deleted file mode 100644 index 5a886ec..0000000 --- a/hal/atomic.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "" - -int32_t KABI ke_interlocked_exchange_32(int32_t *target, int32_t val) -{ -return hal_interlocked_exchange_32(target, val); -} - -int32_t KABI ke_interlocked_increment_32(int32_t *target, int32_t increment) -{ -return hal_interlocked_increment_32(target, increment); -} - -int32_t KABI ke_interlocked_compare_exchange_32(int32_t *target, int32_t compare, int32_t val) -{ -return hal_interlocked_compare_exchange_32(target, compare, val); -} diff --git a/hal/boot.asm.in b/hal/boot.asm.in index 7ffcc16..4368a54 100644 --- a/hal/boot.asm.in +++ b/hal/boot.asm.in @@ -1,57 +1,16 @@ #define ASM_FILE +#include "mlayout.h" #include "multiboot2.h" %define GET_PADDR(x) ((x) - KERNEL_IMAGE_VADDR + KERNEL_IMAGE_PADDR) %define BOCHS_BREAK xchg bx,bx -extern hal_main -global hal_main_32 - -section .multiboot_header -bits 32 -MULTIBOOT_ARCH equ 0 -MULTIBOOT_CHECK_SUM equ (0xFFFFFFFF - (MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_HEADER_SIZE + MULTIBOOT_ARCH) + 1) -MULTIBOOT_REQ_LOADERNAME equ 2 -MULTIBOOT_REQ_MMAP equ 6 -MULTIBOOT_REQ_ACPI_RSDP equ 15 -;==================== -;header tag -align MULTIBOOT_HEADER_ALIGN -multiboot_header_tag: - dd MULTIBOOT2_HEADER_MAGIC - dd MULTIBOOT_ARCH - dd MULTIBOOT_HEADER_SIZE - dd MULTIBOOT_CHECK_SUM -;==================== -;INFO_REQUEST_TAG -align MULTIBOOT_INFO_ALIGN -multiboot_info_tag: - dw 0x1 ; type=1 - dw 0x0 ; flag=0 - dd MULTIBOOT_INFO_TAG_SIZE - dd MULTIBOOT_REQ_LOADERNAME - dd MULTIBOOT_REQ_MMAP -MULTIBOOT_INFO_TAG_SIZE equ ($ - multiboot_info_tag) -;==================== -;MODULE ALIGNMENT TAG -align MULTIBOOT_INFO_ALIGN - dw 0x6; type=6 - dw 0x0; flag=0 - dd 0x8 -;==================== -align MULTIBOOT_INFO_ALIGN - ;End_tag - dw 0x0 - dw 0x0 - dd 0x8 -;==================== -MULTIBOOT_HEADER_SIZE equ ($ - multiboot_header_tag) - +global sys_entry +extern hmain section .text bits 32 -align KERNEL_PAGE_SIZE -hal_main_32: +sys_entry: cli cld cmp eax,MULTIBOOT2_BOOTLOADER_MAGIC @@ -119,7 +78,7 @@ hal_main_32: ; enter long mode lgdt [GET_PADDR(_gdt.ptr)] - jmp _gdt.code:GET_PADDR(halp_entry_64) + jmp _gdt.code:GET_PADDR(hmain_stub) hlt halp_check_long_mode: @@ -144,7 +103,7 @@ halp_check_long_mode: mov eax, 0x80000001 ; Set the A-register to 0x80000001. cpuid ; CPU identification. test edx, 1 << 29 ; Test if the LM-bit, which is bit 29, is set in the D-register. - jz .not_supported ; They aren't, there is no long mode. + jz .not_supported ; They arent, there is no long mode. mov eax,1 jmp .end .not_supported: @@ -156,7 +115,7 @@ halp_check_long_mode: section .text bits 64 -halp_entry_64: +hmain_stub: ; note that we are still at the identity mapping mov ax,_gdt.data mov ds,ax @@ -167,10 +126,7 @@ halp_entry_64: mov rsp, GET_PADDR(_stack) mov rdi, rsi ; multiboot_info* - call hal_write_initial_page_table - test rax,rax - jne .end - call hal_main + call hmain .end: hlt @@ -211,4 +167,4 @@ _gdt: ; Global Descriptor Table (long mode). .ptr: ; GDT PTR dw $ - _gdt - 1 ; Limit. - dq GET_PADDR(_gdt) ; Base. \ No newline at end of file + dq GET_PADDR(_gdt) ; Base. diff --git a/hal/boot.c b/hal/boot.c index d98d5dc..855263d 100644 --- a/hal/boot.c +++ b/hal/boot.c @@ -1,9 +1,9 @@ -#include "print.h" #include "mem.h" +#include "print.h" #include "intr.h" #include "cpu.h" -#include "call.h" -#include "hal_export.h" +#include "kernel.h" +#include "hal.h" //static void //halp_obtain_cpu_info(struct boot_info *hal_info) @@ -21,10 +21,10 @@ //} void HABI -hal_main(void *m_info); +hmain(void *m_info); void HABI -hal_main(void *m_info) +hmain(void *m_info) { if (m_info == NULL || (uint64) m_info & bit_field_mask(0, 2)) { @@ -47,5 +47,5 @@ hal_main(void *m_info) hal_halt_cpu(); } - ke_main(boot_info); + kmain(boot_info); } diff --git a/hal/cpu.asm b/hal/cpu.asm index 2f60726..fe305be 100644 --- a/hal/cpu.asm +++ b/hal/cpu.asm @@ -2,10 +2,23 @@ ;rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11 are scratch registers. ;function parameter: rdi,rsi,rdx,rcx,r8,r9 -[SECTION .text] -[BITS 64] -;====================== global hal_flush_gdt +global hal_flush_tlb +global hal_flush_idt +global hal_read_idt +global hal_read_cr3 +global hal_write_cr3 +global hal_read_cr8 +global hal_write_cr8 +global hal_cpuid +global hal_halt_cpu +global hal_read_msr +global hal_write_msr + + +section .text +bits 64 + hal_flush_gdt: push rbp mov rbp,rsp @@ -31,16 +44,13 @@ mov ds,dx pop rbp ret -;====================== -global hal_flush_tlb -;void flush_tlb(void) + hal_flush_tlb: mov rax,cr3 mov cr3,rax ret -;====================== -global hal_flush_idt + hal_flush_idt: lidt [rdi] ret @@ -75,32 +85,6 @@ hal_write_cr8: mov cr8,rdi ret -; ============================ -; int32 KAPI hal_interlocked_exchange_32(int32 *target, int32 val) -global hal_interlocked_exchange_32 -hal_interlocked_exchange_32: -lock xchg dword [rdi], esi -xor rax, rax -mov eax, esi -ret - -; ============================ -; int32 KAPI hal_interlocked_compare_exchange_32(int32 *dst, int32 test_node_compare, int32 val); -global hal_interlocked_compare_exchange_32 -hal_interlocked_compare_exchange_32: -mov eax, esi; eax = test_node_compare -lock cmpxchg dword [rdi], edx ; edx = val, rdi = ptr to dst -ret - -; ============================ -; int32 KAPI hal_interlocked_increment_32(int32 *target, int32 increment); -global hal_interlocked_increment_32 -hal_interlocked_increment_32: -lock xadd dword [rdi], esi ; [rdi] = [rdi] + esi, esi = old [rdi] -xor rax, rax -mov eax, esi -ret - ; ============================ ; extern void KAPI hal_cpuid(uint32* eax, uint32* ebx, uint32* ecx, uint32* edx); global hal_cpuid @@ -127,99 +111,6 @@ mov rsp,rbp pop rbp ret -;==================== -global hal_write_port_32 -hal_write_port_32: -mov rdx,rdi -mov rax,rsi -out dx,eax -nop -nop -nop -ret - -;==================== -global hal_write_port_16 -hal_write_port_16: -mov rdx,rdi -mov rax,rsi -out dx,ax -nop -nop -nop -ret - - -;==================== -global hal_write_port_8 -hal_write_port_8: -mov rdx,rdi -mov rax,rsi -out dx,al -nop -nop -nop -ret - -;==================== -global hal_read_port_8 -hal_read_port_8: -mov rdx,rdi -xor rax,rax -in al,dx -nop -nop -nop -ret - -;==================== -global hal_read_port_16 -hal_read_port_16: -mov rdx,rdi -xor rax,rax -in ax,dx -nop -nop -nop -ret - -;==================== -global hal_read_port_32 -hal_read_port_32: -mov rdx,rdi -xor rax,rax -in eax,dx -nop -nop -nop -ret - -;==================== -global hal_write_mem_32 -; (void* target, uint32* data) -hal_write_mem_32: -mov dword [rdi], esi -ret - -;==================== -global hal_write_mem_64 -; (void* target, u64 data) -hal_write_mem_64: -mov qword [rdi], rsi -ret - -;==================== -global hal_disable_interrupt -hal_disable_interrupt: -cli -ret - -;==================== -global hal_enable_interrupt -hal_enable_interrupt: -sti -ret - ;==================== global hal_halt_cpu hal_halt_cpu: diff --git a/hal/hal.c b/hal/hal.c new file mode 100644 index 0000000..f4c0971 --- /dev/null +++ b/hal/hal.c @@ -0,0 +1,71 @@ +#include "hal.h" +#include "cpu.h" +#include "intr.h" +#include "io.h" +#include "mem.h" +#include "atomic.h" + +/** + * Provides implementations to HAL functions + * required by kernel in hal.h + */ + +int32 KABI +hal_atomic_xchg_32(int32 *target, int32 val) +{ + return hal_interlocked_exchange_32(target, val); +} + +int32 KABI +hal_atomic_inc_32(int32 *target, int32 increment) +{ + return hal_interlocked_increment_32(target, increment); +} + +int32 KABI +hal_atomic_cmpxchg_32(int32 *target, int32 compare, int32 val) +{ + return hal_interlocked_compare_exchange_32(target, compare, val); +} + +uint32 KABI +hal_set_irql(uint32 irql) +{ + return impl_hal_set_irql(irql); +} + +uint32 KABI +hal_get_irql(void) +{ + return impl_hal_get_irql(); +} + +void KABI +hal_halt(void) +{ + hal_halt_cpu(); +} + +void KABI +hal_issue_intr(uint32 core, uint32 vector) +{ + impl_hal_issue_intr(core, vector); +} + +void KABI +hal_set_intr_dispatcher(k_intr_dispatcher handler) +{ + impl_hal_set_intr_dispatcher(handler); +} + +void KABI +hal_set_exc_dispatcher(k_exc_dispatcher handler) +{ + impl_hal_set_exc_dispatcher(handler); +} + +uint32 KABI +hal_get_core_id(void) +{ + return impl_hal_get_core_id(); +} diff --git a/hal/hp.h b/hal/hp.h deleted file mode 100644 index 0b84f20..0000000 --- a/hal/hp.h +++ /dev/null @@ -1,80 +0,0 @@ -#include "common.h" - -#define HABI KABI - -/** - * ASM Functions - */ - - -/** - * CPU Instructions - */ -void HABI hal_cpuid(uint32 *eax, uint32 *ebx, uint32 *ecx, uint32 *edx); - -void HABI hal_halt_cpu(void); - -void HABI hal_enable_interrupt(void); - -void HABI hal_disable_interrupt(void); - -/** - * IO Port Operations - */ - -int8 HABI hal_read_port_8(uint16 port); - -int16 HABI hal_read_port_16(uint16 port); - -int32 HABI hal_read_port_32(uint16 port); - -void HABI hal_write_port_8(uint16 port, uint8 data); - -void HABI hal_write_port_16(uint16 port, uint16 data); - -void HABI hal_write_port_32(uint16 port, uint32 data); - - -/** - * CPU Structure Operations - */ -void HABI hal_flush_gdt(struct hal_gdt_ptr *gdt_ptr, uint64 code_slct, uint64 data_slct); - -void HABI hal_flush_tlb(void); - -void HABI hal_flush_idt(struct hal_idt_ptr *idt_ptr); - -void HABI hal_read_idt(struct hal_idt_ptr **idt_ptr); - -/** - * Control Register Operations - */ -#define MSR_IA32_APIC_BASE 0x1B - -void HABI hal_read_msr(uint32 *ecx, uint32 *edx, uint32 *eax); - -void HABI hal_write_msr(uint32 *ecx, uint32 *edx, uint32 *eax); - -void HABI hal_write_cr3(uint64 base); - -uint64 HABI hal_read_cr3(void); - -void HABI hal_write_cr8(uint64 pri); - -uint64 HABI hal_read_cr8(void); - - -#define HAL_CORE_COUNT 1 - - -struct STRUCT_PACKED hal_gdt_ptr -{ - uint16 limit; - uint64 base; -}; - -struct STRUCT_PACKED hal_idt_ptr -{ - uint16 limit; - uint64 base; -}; diff --git a/hal/inc/atomic.h b/hal/inc/atomic.h new file mode 100644 index 0000000..aedd6cb --- /dev/null +++ b/hal/inc/atomic.h @@ -0,0 +1,13 @@ +#pragma once + +#include "hdef.h" + +/** + * ASM declaration + */ + +int32 HABI hal_interlocked_exchange_32(int32 *target, int32 val); + +int32 HABI hal_interlocked_compare_exchange_32(int32 *dst, int32 test_node_compare, int32 val); + +int32 HABI hal_interlocked_increment_32(int32 *target, int32 increment); diff --git a/hal/cpu.h b/hal/inc/cpu.h similarity index 61% rename from hal/cpu.h rename to hal/inc/cpu.h index ebe1095..07330b9 100644 --- a/hal/cpu.h +++ b/hal/inc/cpu.h @@ -1,10 +1,9 @@ #pragma once -#include "common.h" +#include "hdef.h" #define HAL_CORE_COUNT 1 - struct STRUCT_PACKED hal_gdt_ptr { uint16 limit; @@ -17,40 +16,14 @@ struct STRUCT_PACKED hal_idt_ptr uint64 base; }; - /** - * CPU Instructions + * ASM declaration */ + void HABI hal_cpuid(uint32 *eax, uint32 *ebx, uint32 *ecx, uint32 *edx); void HABI hal_halt_cpu(void); -void HABI hal_enable_interrupt(void); - -void HABI hal_disable_interrupt(void); - - -/** - * IO Port Operations - */ - -int8 HABI hal_read_port_8(uint16 port); - -int16 HABI hal_read_port_16(uint16 port); - -int32 HABI hal_read_port_32(uint16 port); - -void HABI hal_write_port_8(uint16 port, uint8 data); - -void HABI hal_write_port_16(uint16 port, uint16 data); - -void HABI hal_write_port_32(uint16 port, uint32 data); - - -/** - * CPU Structure Operations - */ - void HABI hal_flush_gdt(struct hal_gdt_ptr *gdt_ptr, uint64 code_slct, uint64 data_slct); void HABI hal_flush_tlb(void); @@ -59,9 +32,6 @@ void HABI hal_flush_idt(struct hal_idt_ptr *idt_ptr); void HABI hal_read_idt(struct hal_idt_ptr **idt_ptr); -/** - * Control Register Operations - */ #define MSR_IA32_APIC_BASE 0x1B void HABI hal_read_msr(uint32 *ecx, uint32 *edx, uint32 *eax); diff --git a/hal/inc/hdef.h b/hal/inc/hdef.h new file mode 100644 index 0000000..5063525 --- /dev/null +++ b/hal/inc/hdef.h @@ -0,0 +1,5 @@ +#pragma once + +#include "cdef.h" + +#define HABI KABI diff --git a/hal/intr.h b/hal/inc/intr.h similarity index 94% rename from hal/intr.h rename to hal/inc/intr.h index 6579d30..fdc7756 100644 --- a/hal/intr.h +++ b/hal/inc/intr.h @@ -1,20 +1,22 @@ #pragma once -#include "common.h" +#include "hdef.h" #include "intr.h" -#include "call.h" +#include "kernel.h" +#include "clib.h" +#include "hal.h" /** * Interrupt context structure */ -typedef struct +struct interrupt_context { const uint64 rip; const uint64 cs; const uint64 rflags; const uint64 rsp; const uint64 ss; -} hal_interrupt_context_t; +}; /** * IDT Defns @@ -32,10 +34,8 @@ typedef struct #define IDT_ENTRY_SIZE 16 /** - * intr.h + * C declaration */ - - int32 hal_interrupt_init(void); @@ -45,16 +45,40 @@ hal_write_gate(void * gate, uint64 offset, uint32 selector, uint32 attr); void hal_set_interrupt_handler(uint64 index, void (*handler)(void)); +uint32 +impl_hal_set_irql(uint32 irql); + +uint32 +impl_hal_get_irql(void); + +void +impl_hal_issue_intr(uint32 target_core, uint32 vector); + +void +impl_hal_set_intr_dispatcher(k_intr_dispatcher handler); + +void +impl_hal_set_exc_dispatcher(k_exc_dispatcher handler); + +uint32 +impl_hal_get_core_id(void); /** - * Dispatchers for asm code + * Exported Dispatchers for asm code */ void HABI -hal_interrupt_dispatcher(uint64 int_vec, hal_interrupt_context_t *context); +hal_interrupt_dispatcher(uint64 int_vec, struct interrupt_context *context); void HABI -hal_exception_dispatcher(uint64 exc_vec, hal_interrupt_context_t *context, uint32 errorcode); +hal_exception_dispatcher(uint64 exc_vec, struct interrupt_context *context, uint32 errorcode); +/** + * ASM declaration + */ + +void HABI hal_enable_interrupt(void); + +void HABI hal_disable_interrupt(void); /** * System exception Handlers diff --git a/hal/inc/io.h b/hal/inc/io.h new file mode 100644 index 0000000..72058b4 --- /dev/null +++ b/hal/inc/io.h @@ -0,0 +1,19 @@ +#pragma once + +#include "hdef.h" + +/** + * ASM declarations + */ + +int8 HABI hal_read_port_8(uint16 port); + +int16 HABI hal_read_port_16(uint16 port); + +int32 HABI hal_read_port_32(uint16 port); + +void HABI hal_write_port_8(uint16 port, uint8 data); + +void HABI hal_write_port_16(uint16 port, uint16 data); + +void HABI hal_write_port_32(uint16 port, uint32 data); diff --git a/hal/mem.h b/hal/inc/mem.h similarity index 97% rename from hal/mem.h rename to hal/inc/mem.h index 1ab4ac3..ff6749b 100644 --- a/hal/mem.h +++ b/hal/inc/mem.h @@ -1,8 +1,7 @@ #pragma once -#include "common.h" +#include "cdef.h" #include "mem.h" -#include "kernel/status.h" /** Global Descriptors Table Definitions @@ -93,7 +92,7 @@ hal_write_pd(void *base, uintptr pt_addr, uint64 attr); void hal_write_pt(void *base, uintptr p_addr, uint64 attr); -k_status +uint32 hal_write_initial_page_table(void *multiboot_info); /** diff --git a/hal/multiboot2.h b/hal/inc/multiboot2.h similarity index 94% rename from hal/multiboot2.h rename to hal/inc/multiboot2.h index 60c83e3..3691fe5 100644 --- a/hal/multiboot2.h +++ b/hal/inc/multiboot2.h @@ -21,22 +21,6 @@ #pragma once -/** - * Kernel memory layout - */ -#define KERNEL_IMAGE_PADDR (0x1000000) -#define KERNEL_PAGE_SIZE (0x1000) - -#define KERNEL_SPACE_VADDR (0xFFFF800000000000) -#define KERNEL_RESERVED_VADDR KERNEL_SPACE_VADDR -#define KERNEL_RESERVED_SIZE (0x00007F0000000000) -#define KERNEL_PAGE_TABLE_VADDR (0xFFFFFF0000000000) -#define KERNEL_PAGE_TABLE_SIZE (0x0000008000000000) -#define KERNEL_DYNAMIC_VADDR (0xFFFFFF8000000000) -#define KERNEL_DYNAMIC_SIZE (0x0000007F80000000) -#define KERNEL_IMAGE_VADDR (0xFFFFFFFF80000000) -#define KERNEL_IMAGE_SIZE (0x0000000080000000) - /* How many bytes from the start of the file we search for the header. */ #define MULTIBOOT_SEARCH 32768 #define MULTIBOOT_HEADER_ALIGN 8 diff --git a/hal/print.h b/hal/inc/print.h similarity index 90% rename from hal/print.h rename to hal/inc/print.h index 4be8478..0a73d08 100644 --- a/hal/print.h +++ b/hal/inc/print.h @@ -1,5 +1,5 @@ #pragma once -#include "common.h" +#include "cdef.h" #include "print.h" void diff --git a/hal/intr.asm b/hal/intr.asm index 39cee10..d6db533 100644 --- a/hal/intr.asm +++ b/hal/intr.asm @@ -1,3 +1,15 @@ +global hal_disable_interrupt +global hal_enable_interrupt + +hal_disable_interrupt: +cli +ret + +hal_enable_interrupt: +sti +ret + + %macro PUSHAQ 0 push rax ;save current rax push rbx ;save current rbx diff --git a/hal/intr.c b/hal/intr.c index fe9ff63..aa45f13 100644 --- a/hal/intr.c +++ b/hal/intr.c @@ -1,30 +1,30 @@ -#include "common.h" -#include "intr.h" #include "cpu.h" +#include "hdef.h" +#include "hal.h" #include "intr.h" -#include "print.h" #include "mem.h" -#include "hal_export.h" +#include "print.h" +#include "io.h" -static uint8 _idts[HAL_CORE_COUNT][IDT_ENTRY_NUM * IDT_ENTRY_SIZE]; -static struct hal_idt_ptr _idt_ptrs[HAL_CORE_COUNT]; -static intr_handler_fp _intr_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM]; -static void *_intr_handler_context_table[HAL_CORE_COUNT][IDT_ENTRY_NUM]; -static exc_handler_fp _exc_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM]; +static uint8 cpu_idts[HAL_CORE_COUNT][IDT_ENTRY_NUM * IDT_ENTRY_SIZE]; +static struct hal_idt_ptr cpu_idt_ptrs[HAL_CORE_COUNT]; + +static k_exc_dispatcher k_exc_disps[HAL_CORE_COUNT]; +static k_intr_dispatcher k_intr_disps[HAL_CORE_COUNT]; uint32 -hal_set_irql(uint32 irql) +impl_hal_set_irql(uint32 irql) { UNREFERENCED(irql) - hal_assert(FALSE, "Unimplemented function called."); + hal_halt_cpu(); return 0; } uint32 -hal_get_irql(void) +impl_hal_get_irql(void) { - hal_assert(FALSE, "Unimplemented function called."); + hal_halt_cpu(); return 0; } @@ -55,92 +55,57 @@ hal_set_interrupt_handler(uint64 index, void (*handler)(void)) { if (index < IDT_ENTRY_NUM) { - hal_write_gate(_idts[hal_get_core_id()] + 16 * index, (uintptr) handler, seg_selector(1, 0), + hal_write_gate(cpu_idts[hal_get_core_id()] + 16 * index, (uintptr) handler, seg_selector(1, 0), GATE_DPL_0 | GATE_PRESENT | GATE_TYPE_INTERRUPT); } } -void KABI -hal_issue_intr(uint32 target_core, uint32 vector) +void +impl_hal_issue_intr(uint32 target_core, uint32 vector) { UNREFERENCED(target_core); UNREFERENCED(vector); - hal_assert(FALSE, "Unimplemented function called."); -} - -void -hal_reg_intr(uint32 index, intr_handler_fp handler) -{ - // TODO: FIX CONTEXT - - if (index < IDT_ENTRY_NUM && hal_get_core_id() < HAL_CORE_COUNT) - { - _intr_handler_table[hal_get_core_id()][index] = handler; - _intr_handler_context_table[hal_get_core_id()][index] = NULL; - } -} - -void -hal_dereg_intr(uint32 index) -{ - // TODO: FIX CONTEXT - if (index < IDT_ENTRY_NUM && hal_get_core_id() < HAL_CORE_COUNT) - { - _intr_handler_table[hal_get_core_id()][index] = NULL; - } -} - - -void -hal_reg_exc(uint32 index, exc_handler_fp handler) -{ - if (index < IDT_ENTRY_NUM && hal_get_core_id() < HAL_CORE_COUNT) - { - _exc_handler_table[hal_get_core_id()][index] = handler; - } -} - - -void -hal_dereg_exc(uint32 index) -{ - if (index < IDT_ENTRY_NUM && hal_get_core_id() < HAL_CORE_COUNT) - { - _exc_handler_table[hal_get_core_id()][index] = NULL; - } -} - -void KABI -hal_halt(void) -{ hal_halt_cpu(); } +void +impl_hal_set_intr_dispatcher(k_intr_dispatcher handler) +{ + k_intr_disps[hal_get_core_id()] = handler; +} + +void +impl_hal_set_exc_dispatcher(k_exc_dispatcher handler) +{ + k_exc_disps[hal_get_core_id()] = handler; +} + + void HABI -hal_interrupt_dispatcher(uint64 int_vec, hal_interrupt_context_t *context) +hal_interrupt_dispatcher(uint64 int_vec, struct interrupt_context *context) { uint32 coreid = hal_get_core_id(); - if (_intr_handler_table[int_vec] == NULL) + if (k_intr_disps[coreid] == NULL) { hal_printf("Unhandled interrupt %d at 0x%X.\n", int_vec, context->rip); } else { - _intr_handler_table[coreid][int_vec](context->rip, context->rsp, 0); + k_intr_disps[coreid]((uint32) int_vec, context); } } void HABI -hal_exception_dispatcher(uint64 exc_vec, hal_interrupt_context_t *context, uint32 errorcode) +hal_exception_dispatcher(uint64 exc_vec, struct interrupt_context *context, uint32 errorcode) { uint32 coreid = hal_get_core_id(); - if (_exc_handler_table[exc_vec] == NULL) + if (k_exc_disps[coreid] == NULL) { hal_printf("Unhandled exception %d at 0x%X.\n", exc_vec, context->rip); } else { - _exc_handler_table[coreid][exc_vec](context->rip, context->rsp, errorcode); + k_exc_disps[coreid]((uint32)exc_vec, context->rip, errorcode, context); } } @@ -406,7 +371,7 @@ halp_populate_idt(void) } uint32 -hal_get_core_id(void) +impl_hal_get_core_id(void) { // TODO return 0; @@ -426,21 +391,17 @@ hal_interrupt_init(void) } // get idt ptr ready - _idt_ptrs[coreid].base = (uint64) &_idts[coreid]; - _idt_ptrs[coreid].limit = IDT_ENTRY_NUM * IDT_ENTRY_SIZE - 1; + cpu_idt_ptrs[coreid].base = (uint64) &cpu_idts[coreid]; + cpu_idt_ptrs[coreid].limit = IDT_ENTRY_NUM * IDT_ENTRY_SIZE - 1; // clear dispatch table - for (uint64 i = 0; i < IDT_ENTRY_NUM; i++) - { - _intr_handler_table[coreid][i] = NULL; - _exc_handler_table[coreid][i] = NULL; - _intr_handler_context_table[coreid][i] = NULL; - } + k_exc_disps[coreid] = NULL; + k_intr_disps[coreid] = NULL; // hook asm interrupt handlers halp_populate_idt(); - hal_flush_idt(&_idt_ptrs[coreid]); + hal_flush_idt(&cpu_idt_ptrs[coreid]); // disable PIC hal_write_port_8(0xa1, 0xff); diff --git a/hal/io.asm b/hal/io.asm new file mode 100644 index 0000000..057fe47 --- /dev/null +++ b/hal/io.asm @@ -0,0 +1,67 @@ +section .text +bits 64 + +global hal_write_port_16 +global hal_write_port_32 +global hal_write_port_8 +global hal_read_port_8 +global hal_read_port_16 +global hal_read_port_32 + +hal_write_port_32: +mov rdx,rdi +mov rax,rsi +out dx,eax +nop +nop +nop +ret + + +hal_write_port_16: +mov rdx,rdi +mov rax,rsi +out dx,ax +nop +nop +nop +ret + + +hal_write_port_8: +mov rdx,rdi +mov rax,rsi +out dx,al +nop +nop +nop +ret + + +hal_read_port_8: +mov rdx,rdi +xor rax,rax +in al,dx +nop +nop +nop +ret + +hal_read_port_16: +mov rdx,rdi +xor rax,rax +in ax,dx +nop +nop +nop +ret + + +hal_read_port_32: +mov rdx,rdi +xor rax,rax +in eax,dx +nop +nop +nop +ret \ No newline at end of file diff --git a/hal/mb_hdr.asm.in b/hal/mb_hdr.asm.in new file mode 100644 index 0000000..587572c --- /dev/null +++ b/hal/mb_hdr.asm.in @@ -0,0 +1,47 @@ +#define ASM_FILE +#include "multiboot2.h" +#include "mlayout.h" + +extern hmain +global hal_main_32 + +section .multiboot_header +bits 32 +align KERNEL_PAGE_SIZE +MULTIBOOT_ARCH equ 0 +MULTIBOOT_CHECK_SUM equ (0xFFFFFFFF - (MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_HEADER_SIZE + MULTIBOOT_ARCH) + 1) +MULTIBOOT_REQ_LOADERNAME equ 2 +MULTIBOOT_REQ_MMAP equ 6 +MULTIBOOT_REQ_ACPI_RSDP equ 15 +;==================== +;header tag +align MULTIBOOT_HEADER_ALIGN +multiboot_header_tag: + dd MULTIBOOT2_HEADER_MAGIC + dd MULTIBOOT_ARCH + dd MULTIBOOT_HEADER_SIZE + dd MULTIBOOT_CHECK_SUM +;==================== +;INFO_REQUEST_TAG +align MULTIBOOT_INFO_ALIGN +multiboot_info_tag: + dw 0x1 ; type=1 + dw 0x0 ; flag=0 + dd MULTIBOOT_INFO_TAG_SIZE + dd MULTIBOOT_REQ_LOADERNAME + dd MULTIBOOT_REQ_MMAP +MULTIBOOT_INFO_TAG_SIZE equ ($ - multiboot_info_tag) +;==================== +;MODULE ALIGNMENT TAG +align MULTIBOOT_INFO_ALIGN + dw 0x6; type=6 + dw 0x0; flag=0 + dd 0x8 +;==================== +align MULTIBOOT_INFO_ALIGN + ;End_tag + dw 0x0 + dw 0x0 + dd 0x8 +;==================== +MULTIBOOT_HEADER_SIZE equ ($ - multiboot_header_tag) \ No newline at end of file diff --git a/hal/mem.c b/hal/mem.c index f20180c..f6b1bc8 100644 --- a/hal/mem.c +++ b/hal/mem.c @@ -1,9 +1,9 @@ -#include "common.h" +#include "cdef.h" #include "cpu.h" #include "mem.h" #include "intr.h" -#include "hal_export.h" +#include "hal.h" static uint8 _gdts[HAL_CORE_COUNT][GDT_ENTRY_NUM * GDT_ENTRY_SIZE]; static struct hal_gdt_ptr _gdt_ptrs[HAL_CORE_COUNT]; @@ -12,7 +12,8 @@ static struct hal_gdt_ptr _gdt_ptrs[HAL_CORE_COUNT]; static uint32 hal_heap_used; static char hal_heap[HAL_HEAP_SIZE]; -uint32 hal_write_initial_page_table(void *multiboot_info) +uint32 +hal_write_initial_page_table(void *multiboot_info) { UNREFERENCED(multiboot_info); diff --git a/hal/print.c b/hal/print.c index 08392af..03a9eec 100644 --- a/hal/print.c +++ b/hal/print.c @@ -1,6 +1,7 @@ -#include "common.h" +#include "cdef.h" #include "cpu.h" #include "print.h" +#include "clib.h" // #define get_column(pos) ((pos) % 80) #define get_row(pos) ((pos) / 80) diff --git a/inc/cdef.h b/inc/cdef.h new file mode 100644 index 0000000..20ddc41 --- /dev/null +++ b/inc/cdef.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +typedef uint32_t uint32; +typedef int32_t int32; +typedef uint64_t uint64; +typedef int64_t int64; +typedef uintptr_t uintptr; +typedef uint16_t uint16; +typedef int16_t int16; +typedef uint8_t uint8; +typedef int8_t int8; + +typedef _Bool bool; +#define TRUE (1) +#define FALSE (0) + +#define STRUCT_PACKED __attribute__((packed)) + +#define UNREFERENCED(x) {(x) = (x);} + +#define KABI __attribute__((sysv_abi)) + + diff --git a/inc/clib.h b/inc/clib.h new file mode 100644 index 0000000..81bd1ca --- /dev/null +++ b/inc/clib.h @@ -0,0 +1,54 @@ +#pragma once + +#include "cdef.h" + +/** + * Common macros, etc + */ + +#define OBTAIN_STRUCT_ADDR(member_addr, struct_name, member_name) ((struct_name*)((uintptr)(member_addr) - (uintptr)(&(((struct_name*)0)->member_name)))) + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define SWAP(a, b, T) do { T temp = *(a); *(a) = *(b); *(b) = temp; } while(0); + +uint32 +lb_rand(void); + +void +lb_srand(uint32 _seed); + +void +lb_mrand(uint32 max); + +uint64 +str_len(char const *str); + + +uint64 +str_cmp(char const *str1, char const *str2); + + +void +mem_cpy(void *src, void *dst, uint64 size); + + +void +mem_mv(void *src, void *dst, uint64 size); + + +void +mem_set(void *src, uint8 val, uint64 size); + + +static inline uint64 +bit_mask(uint32 bit) +{ + return (uint64) 1 << bit; +} + +static inline uint64 +bit_field_mask(uint32 low, uint32 high) +{ + return ~(~(uint64) 0 << high << 1) << low; +} diff --git a/inc/hal.h b/inc/hal.h new file mode 100644 index 0000000..21992ca --- /dev/null +++ b/inc/hal.h @@ -0,0 +1,38 @@ +#pragma once + +#include "cdef.h" + +int32 KABI +hal_atomic_xchg_32(int32 *target, int32 val); + +int32 KABI +hal_atomic_inc_32(int32 *target, int32 increment); + +int32 KABI +hal_atomic_cmpxchg_32(int32 *target, int32 compare, int32 val); + +uint32 KABI +hal_set_irql(uint32 irql); + +uint32 KABI +hal_get_irql(void); + +void KABI +hal_halt(void); + +void KABI +hal_issue_intr(uint32 core, uint32 vector); + +typedef void (KABI *k_intr_dispatcher)(uint32 intr_vec, void *h_context); + +void KABI +hal_set_intr_dispatcher(k_intr_dispatcher handler); + +typedef void (KABI *k_exc_dispatcher)(uint32 exc_vec, uintptr exc_addr, uint32 err_code, void *h_context); + +void KABI +hal_set_exc_dispatcher(k_exc_dispatcher handler); + +uint32 KABI +hal_get_core_id(void); + diff --git a/inc/hal_export.h b/inc/hal_export.h deleted file mode 100644 index 842c1ba..0000000 --- a/inc/hal_export.h +++ /dev/null @@ -1,82 +0,0 @@ -#pragma once - -#include "common.h" - -/** - * HAL Structures - */ - -/** - * boot_info structure - * must NOT use kernel structures - */ -struct boot_info -{ - struct - { - char cpu_vendor[13]; - } cpu_info; - - struct - { - uintptr krnl_start_vaddr; - uintptr krnl_end_vaddr; - } mem_info; - - struct intr_info - { - uint32 timer_intr_vec; - uint32 dpc_intr_vec; - uint32 page_fault_vec; - - uint32 irql_low; - uint32 irql_dpc; - uint32 irql_high; - } intr_info; -}; - -/** - * HAL functions - */ -int32 KABI -hal_atomic_xchg_32(int32 *target, int32 val); - -int32 KABI -hal_atomic_inc_32(int32 *target, int32 increment); - -int32 KABI -hal_atomic_cmpxchg_32(int32 *target, int32 compare, int32 val); - -uint32 KABI -hal_set_irql(uint32 irql); - -uint32 KABI -hal_get_irql(void); - -void KABI -hal_halt(void); - -void KABI -hal_issue_intr(uint32 core, uint32 vector); - -typedef void (KABI *intr_handler_fp)(uintptr exc_addr, uintptr exc_stack, uint32 error_code); - -void KABI -hal_reg_intr(uint32 index, intr_handler_fp handler); - -void KABI -hal_dereg_intr(uint32 index); - -typedef void (KABI *exc_handler_fp)(uintptr exc_addr, uintptr exc_stack, uint32 error_code); - -void KABI -hal_reg_exc(uint32 exc, exc_handler_fp handler); - -void KABI -hal_dereg_exc(uint32 exc); - -uint32 KABI -hal_get_core_id(void); - -void KABI -ke_main(struct boot_info *boot_info); diff --git a/inc/kernel.h b/inc/kernel.h new file mode 100644 index 0000000..d7c70da --- /dev/null +++ b/inc/kernel.h @@ -0,0 +1,34 @@ +#pragma once + +#include "cdef.h" + + /** + * boot_info structure + */ +struct boot_info +{ + struct + { + char cpu_vendor[13]; + } cpu_info; + + struct + { + uintptr krnl_start_vaddr; + uintptr krnl_end_vaddr; + } mem_info; + + struct intr_info + { + uint32 timer_intr_vec; + uint32 dpc_intr_vec; + uint32 page_fault_vec; + + uint32 irql_low; + uint32 irql_dpc; + uint32 irql_high; + } intr_info; +}; + +void KABI +kmain(struct boot_info *boot_info); diff --git a/inc/kernel/ke.h b/inc/kernel/ke.h deleted file mode 100644 index b485033..0000000 --- a/inc/kernel/ke.h +++ /dev/null @@ -1,147 +0,0 @@ -#pragma once - -#include "common.h" -#include "kernel/status.h" -#include "kernel/lb.h" -#include "hal_export.h" - -/** - * memory - */ -void -ke_alloc_init(void); - -void * -ke_alloc(uint32 size); - -void -ke_free(void *ptr); - - -/** - * atomic - */ -int32 -ke_atomic_xchg_32(int32 *target, int32 val); - -int32 -ke_atomic_inc_32(int32 *target, int32 increment); - -int32 -ke_atmoic_cmpxchg_32(int32 *target, int32 compare, int32 val); - - -/** - * assert - */ -#define ke_assert(expr) ke_assert_ex(#expr, __FILE__, __LINE__, expr) - -void -ke_assert_ex(const char *expr_str, const char *file, int32 line, int32 expr); - - -/** - * bugcheck - */ -void -ke_panic(uint64 reason); - - -/** - * interrupt - */ -#define IRQL_LOW (0) -#define IRQL_DPC (1) -#define IRQL_HIGH (2) -#define IRQL_NUM (3) - -uint32 -ke_raise_irql(uint32 irql); - -uint32 -ke_lower_irql(uint32 irql); - -uint32 -ke_get_irql(void); - -void -ke_issue_intr(uint32 core, uint32 vector); - -void -ke_reg_intr(uint32 index, intr_handler_fp handler); - -void -ke_dereg_intr(uint32 index); - -#define EXC_UNRCVY (0) -#define EXC_DIV (1) -#define EXC_PROT (2) -#define EXC_OP (3) -#define EXC_PF (4) -#define EXC_UNSUP (5) -#define EXC_DEBUG (6) -void -ke_reg_exc(uint32 exc, exc_handler_fp handler); - -void -ke_dereg_exc(uint32 exc); - -uint32 -ke_get_core_id(void); - - -/** - * print - */ -void -ke_printf(const char *str, ...); - -void -ke_vprintf(const char *str, va_list args); - - -/** - * spinlock - */ -struct spin_lock -{ - int32 val; -}; - -void -ke_spin_init(struct spin_lock *lock); - -void -ke_spin_lock(struct spin_lock *lock); - -void -ke_spin_unlock(struct spin_lock *lock); - - -/** - * rwwlock - */ -struct rwwlock -{ - struct spin_lock w_mutex; - struct spin_lock r_mutex; - struct spin_lock res_lock; - struct spin_lock r_try; - uint32 reader_ct; - uint32 writer_ct; -}; - -void -ke_rww_init(struct rwwlock *lock); - -void -ke_rww_r_lock(struct rwwlock *lock); - -void -ke_rww_r_unlock(struct rwwlock *lock); - -void -ke_rww_w_lock(struct rwwlock *lock); - -void -ke_rww_w_unlock(struct rwwlock *lock); diff --git a/inc/kernel/lb.h b/inc/kernel/lb.h deleted file mode 100644 index e6de716..0000000 --- a/inc/kernel/lb.h +++ /dev/null @@ -1,218 +0,0 @@ -#pragma once - -#include "common.h" -#include "kernel/status.h" - -/* - //Not used for now - //BST interface - -struct bstree; - -struct bstree_node -{ - struct bstree_node *left; - struct bstree_node *treenode; -}; - -struct bstree_impl -{ - struct bstree_node *(KAPI *t_search)(struct bstree *tree, struct bstree_node *entry); - - k_status (KAPI *t_insert)(struct bstree *tree, struct bstree_node *entry); - - k_status (KAPI *t_delete)(struct bstree *tree, struct bstree_node *entry, struct bstree_node **out); - - int32 (KAPI *t_size)(struct bstree *tree); - - struct bstree_node *(KAPI *t_max)(struct bstree *tree); - - struct bstree_node *(KAPI *t_min)(struct bstree *tree); - - struct bstree_node *(KAPI *t_prev)(struct bstree_node *tree); - - struct bstree_node *(KAPI *t_next)(struct bstree_node *tree); - - k_status (KAPI *t_validate)(struct bstree *tree); -}; - -typedef int32 (KAPI *bstree_cmp_fp)(struct bstree_node *left, struct bstree_node *treenode); - -struct bstree -{ - struct bstree_impl *impl; - struct tree_node *root; - bstree_cmp_fp cmp; -}; - -*/ - -/** - * AVL tree - */ -struct atree_node -{ - struct atree_node *left; - struct atree_node *right; - int32 height; -}; - -/** -* A comparison function between self (yours) and treenode (tree's) -* Returns: -* < 0 if treenode < self -* = 0 if treenode = self -* > 0 if treenode > self -*/ -typedef int32 (*atree_cmp_fp)( - struct atree_node *tree_node, - struct atree_node *self); - -struct atree -{ - atree_cmp_fp cmpf; - struct atree_node *root; -}; - - -struct atree_node * -lb_atree_search( - struct atree *tree, - struct atree_node *entry); - - -struct atree_node * -lb_atree_insert( - struct atree *tree, - struct atree_node *entry); - - -struct atree_node * -lb_atree_delete( - struct atree *tree, - struct atree_node *entry); - - -void -lb_atree_init( - struct atree *tree, - atree_cmp_fp compare); - - -struct atree_node * -lb_atree_max( - struct atree *tree); - - -struct atree_node * -lb_atree_min( - struct atree *tree); - - -struct atree_node * -lb_atree_next( - struct atree *tree, - struct atree_node *entry); - - -struct atree_node * -lb_atree_prev( - struct atree *tree, - struct atree_node *entry); - -bool -lb_atree_validate( - struct atree *tree); - -uint32 -lb_atree_size( - struct atree *tree); - - -/** - * Linked list - */ - -struct llist_node -{ - struct llist_node *prev; - struct llist_node *next; -}; - -struct llist -{ - struct llist_node *head; - struct llist_node *tail; - uint32 size; -}; - -void -lb_llist_init(struct llist *list); - -uint32 -lb_llist_size(struct llist *list); - -void -lb_llist_push_front(struct llist *list, struct llist_node *node); - -void -lb_llist_push_back(struct llist *list, struct llist_node *node); - -struct llist_node * -lb_llist_pop_front(struct llist *list); - - -struct llist_node * -lb_llist_pop_back(struct llist *list); - -void -lb_llist_insert_by_idx(struct llist *list, uint32 index, struct llist_node *node); - -struct llist_node * -lb_llist_remove_by_idx(struct llist *list, uint32 index); - - -struct llist_node * -lb_llist_get(struct llist *list, uint32 index); - - -void -lb_llist_insert_by_ref(struct llist *list, struct llist_node *cur_node, struct llist_node *new_node); - - -struct llist_node * -lb_llist_remove_by_ref(struct llist *list, struct llist_node *node); - - -struct llist_node * -lb_llist_next(struct llist_node *node); - - -struct llist_node * -lb_llist_prev(struct llist_node *node); - - -struct llist_node * -lb_llist_first(struct llist *list); - - -struct llist_node * -lb_llist_last(struct llist *list); - - -/** - * SALLOC - */ - -void -lb_salloc_init(void *base, uint32 size); - -void * -lb_salloc(void *base, uint32 size); - -void -lb_sfree(void *base, void *ptr); - -bool -lb_salloc_assert(void *base, const uint32 *blk_size, const bool *blk_free, uint32 size); - diff --git a/inc/common.h b/inc/mlayout.h similarity index 50% rename from inc/common.h rename to inc/mlayout.h index 144c581..280d59d 100644 --- a/inc/common.h +++ b/inc/mlayout.h @@ -29,83 +29,3 @@ #define KERNEL_DYNAMIC_SIZE (0x0000007F80000000) #define KERNEL_IMAGE_VADDR (0xFFFFFFFF80000000) #define KERNEL_IMAGE_SIZE (0x0000000080000000) - -#ifndef ASM_FILE - -#include -#include -#include - -typedef uint32_t uint32; -typedef int32_t int32; -typedef uint64_t uint64; -typedef int64_t int64; -typedef uintptr_t uintptr; -typedef uint16_t uint16; -typedef int16_t int16; -typedef uint8_t uint8; -typedef int8_t int8; - -typedef _Bool bool; -#define TRUE (1) -#define FALSE (0) - -#define STRUCT_PACKED __attribute__((packed)) - -#define UNREFERENCED(x) {(x) = (x);} - -#define KABI __attribute__((sysv_abi)) - -/** - * Common macros, etc - */ - -#define OBTAIN_STRUCT_ADDR(member_addr, struct_name, member_name) ((struct_name*)((uintptr)(member_addr) - (uintptr)(&(((struct_name*)0)->member_name)))) - -#define MAX(a, b) ((a) > (b) ? (a) : (b)) - -#define SWAP(a, b, T) do { T temp = *(a); *(a) = *(b); *(b) = temp; } while(0); - -uint32 -lb_rand(void); - -void -lb_srand(uint32 _seed); - -void -lb_mrand(uint32 max); - -uint64 -str_len(char const *str); - - -uint64 -str_cmp(char const *str1, char const *str2); - - -void -mem_cpy(void *src, void *dst, uint64 size); - - -void -mem_mv(void *src, void *dst, uint64 size); - - -void -mem_set(void *src, uint8 val, uint64 size); - - -static inline uint64 -bit_mask(uint32 bit) -{ - return (uint64) 1 << bit; -} - -static inline uint64 -bit_field_mask(uint32 low, uint32 high) -{ - return ~(~(uint64) 0 << high << 1) << low; -} - -#endif - diff --git a/kernel/Rules.mk b/kernel/Rules.mk index ae39533..ccb5180 100644 --- a/kernel/Rules.mk +++ b/kernel/Rules.mk @@ -1,5 +1,9 @@ include $(MK)/prologue.mk +MOD:=KERNEL +C_FLAGS_$(MOD):=$(addprefix -I, $(d)/inc) +AS_FLAGS_$(MOD):=$(addprefix -I, $(d)/inc) + dir := $(d)/ke include $(dir)/Rules.mk dir := $(d)/mm diff --git a/kernel/inc/ke/alloc.h b/kernel/inc/ke/alloc.h new file mode 100644 index 0000000..520d355 --- /dev/null +++ b/kernel/inc/ke/alloc.h @@ -0,0 +1,12 @@ +#pragma once + +#include "cdef.h" + +void +ke_alloc_init(void); + +void * +ke_alloc(uint32 size); + +void +ke_free(void *ptr); diff --git a/kernel/inc/ke/assert.h b/kernel/inc/ke/assert.h new file mode 100644 index 0000000..7a32136 --- /dev/null +++ b/kernel/inc/ke/assert.h @@ -0,0 +1,8 @@ +#pragma once + +#include "cdef.h" + +#define KE_ASSERT(expr) ke_assert_ex(#expr, __FILE__, __LINE__, expr) + +void +ke_assert_ex(const char *expr_str, const char *file, int32 line, int32 expr); diff --git a/kernel/inc/ke/atomic.h b/kernel/inc/ke/atomic.h new file mode 100644 index 0000000..bd844f5 --- /dev/null +++ b/kernel/inc/ke/atomic.h @@ -0,0 +1,12 @@ +#pragma once + +#include "cdef.h" + +int32 +ke_atomic_xchg_32(int32 *target, int32 val); + +int32 +ke_atomic_inc_32(int32 *target, int32 increment); + +int32 +ke_atmoic_cmpxchg_32(int32 *target, int32 compare, int32 val); diff --git a/kernel/inc/ke/intr.h b/kernel/inc/ke/intr.h new file mode 100644 index 0000000..712cc2e --- /dev/null +++ b/kernel/inc/ke/intr.h @@ -0,0 +1,48 @@ +#pragma once + +#include "cdef.h" + +#define IRQL_LOW (0) +#define IRQL_DPC (1) +#define IRQL_HIGH (2) +#define IRQL_NUM (3) + +uint32 +ke_raise_irql(uint32 irql); + +uint32 +ke_lower_irql(uint32 irql); + +uint32 +ke_get_irql(void); + +void +ke_issue_intr(uint32 core, uint32 vector); + +typedef void (KABI *k_intr_handler)(void* k_context); + +void +ke_reg_intr(uint32 vec, k_intr_handler); + +void +ke_dereg_intr(uint32 vec); + +#define EXC_UNRCVY (0) +#define EXC_DIV (1) +#define EXC_PROT (2) +#define EXC_OP (3) +#define EXC_PF (4) +#define EXC_UNSUP (5) +#define EXC_DEBUG (6) + +typedef void (KABI *k_exc_handler)(uintptr exc_addr, uint64 err_code); + +void +ke_reg_exc(uint32 vec, k_exc_handler handler); + +void +ke_dereg_exc(uint32 vec); + +uint32 +ke_get_core_id(void); + diff --git a/kernel/inc/ke/panic.h b/kernel/inc/ke/panic.h new file mode 100644 index 0000000..8808167 --- /dev/null +++ b/kernel/inc/ke/panic.h @@ -0,0 +1,6 @@ +#pragma once + +#include "cdef.h" + +void +ke_panic(uint32 reason); diff --git a/kernel/inc/ke/print.h b/kernel/inc/ke/print.h new file mode 100644 index 0000000..bfa4a2a --- /dev/null +++ b/kernel/inc/ke/print.h @@ -0,0 +1,9 @@ +#pragma once + +#include "cdef.h" + +void +ke_printf(const char *str, ...); + +void +ke_vprintf(const char *str, va_list args); diff --git a/kernel/inc/ke/rww_lock.h b/kernel/inc/ke/rww_lock.h new file mode 100644 index 0000000..accfcd8 --- /dev/null +++ b/kernel/inc/ke/rww_lock.h @@ -0,0 +1,29 @@ +#pragma once + +#include "cdef.h" +#include "ke/spin_lock.h" + +struct rww_lock +{ + struct spin_lock w_mutex; + struct spin_lock r_mutex; + struct spin_lock res_lock; + struct spin_lock r_try; + uint32 reader_ct; + uint32 writer_ct; +}; + +void +ke_rww_init(struct rww_lock *lock); + +void +ke_rww_r_lock(struct rww_lock *lock); + +void +ke_rww_r_unlock(struct rww_lock *lock); + +void +ke_rww_w_lock(struct rww_lock *lock); + +void +ke_rww_w_unlock(struct rww_lock *lock); diff --git a/kernel/inc/ke/spin_lock.h b/kernel/inc/ke/spin_lock.h new file mode 100644 index 0000000..3e6125a --- /dev/null +++ b/kernel/inc/ke/spin_lock.h @@ -0,0 +1,17 @@ +#pragma once + +#include "cdef.h" + +struct spin_lock +{ + int32 val; +}; + +void +ke_spin_init(struct spin_lock *lock); + +void +ke_spin_lock(struct spin_lock *lock); + +void +ke_spin_unlock(struct spin_lock *lock); diff --git a/kernel/inc/lb/atree.h b/kernel/inc/lb/atree.h new file mode 100644 index 0000000..5bb0239 --- /dev/null +++ b/kernel/inc/lb/atree.h @@ -0,0 +1,65 @@ +#pragma once + +#include "cdef.h" + + +struct atree_node +{ + struct atree_node *left; + struct atree_node *right; + int32 height; +}; + +/** +* A comparison function between self (yours) and treenode (tree's) +* Returns: +* < 0 if treenode < self +* = 0 if treenode = self +* > 0 if treenode > self +*/ +typedef int32 (*atree_cmp_fp)(struct atree_node *tree_node, struct atree_node *self); + +struct atree +{ + atree_cmp_fp cmpf; + struct atree_node *root; +}; + + +struct atree_node * +lb_atree_search(struct atree *tree, struct atree_node *entry); + + +struct atree_node * +lb_atree_insert(struct atree *tree, struct atree_node *entry); + + +struct atree_node * +lb_atree_delete(struct atree *tree, struct atree_node *entry); + + +void +lb_atree_init(struct atree *tree, atree_cmp_fp compare); + + +struct atree_node * +lb_atree_max(struct atree *tree); + + +struct atree_node * +lb_atree_min(struct atree *tree); + + +struct atree_node * +lb_atree_next(struct atree *tree, struct atree_node *entry); + + +struct atree_node * +lb_atree_prev(struct atree *tree, struct atree_node *entry); + +bool +lb_atree_validate(struct atree *tree); + +uint32 +lb_atree_size(struct atree *tree); + diff --git a/kernel/inc/lb/llist.h b/kernel/inc/lb/llist.h new file mode 100644 index 0000000..28cde95 --- /dev/null +++ b/kernel/inc/lb/llist.h @@ -0,0 +1,69 @@ +#pragma once + +#include "cdef.h" + +struct llist_node +{ + struct llist_node *prev; + struct llist_node *next; +}; + +struct llist +{ + struct llist_node *head; + struct llist_node *tail; + uint32 size; +}; + +void +lb_llist_init(struct llist *list); + +uint32 +lb_llist_size(struct llist *list); + +void +lb_llist_push_front(struct llist *list, struct llist_node *node); + +void +lb_llist_push_back(struct llist *list, struct llist_node *node); + +struct llist_node * +lb_llist_pop_front(struct llist *list); + + +struct llist_node * +lb_llist_pop_back(struct llist *list); + +void +lb_llist_insert_by_idx(struct llist *list, uint32 index, struct llist_node *node); + +struct llist_node * +lb_llist_remove_by_idx(struct llist *list, uint32 index); + + +struct llist_node * +lb_llist_get(struct llist *list, uint32 index); + + +void +lb_llist_insert_by_ref(struct llist *list, struct llist_node *cur_node, struct llist_node *new_node); + + +struct llist_node * +lb_llist_remove_by_ref(struct llist *list, struct llist_node *node); + + +struct llist_node * +lb_llist_next(struct llist_node *node); + + +struct llist_node * +lb_llist_prev(struct llist_node *node); + + +struct llist_node * +lb_llist_first(struct llist *list); + + +struct llist_node * +lb_llist_last(struct llist *list); diff --git a/kernel/inc/lb/salloc.h b/kernel/inc/lb/salloc.h new file mode 100644 index 0000000..8434f09 --- /dev/null +++ b/kernel/inc/lb/salloc.h @@ -0,0 +1,16 @@ +#pragma once + +#include "cdef.h" + +void +lb_salloc_init(void *base, uint32 size); + +void * +lb_salloc(void *base, uint32 size); + +void +lb_sfree(void *base, void *ptr); + +bool +lb_salloc_assert(void *base, const uint32 *blk_size, const bool *blk_free, uint32 size); + diff --git a/inc/kernel/mm.h b/kernel/inc/mm/pmm.h similarity index 71% rename from inc/kernel/mm.h rename to kernel/inc/mm/pmm.h index 948b406..3ff7127 100644 --- a/inc/kernel/mm.h +++ b/kernel/inc/mm/pmm.h @@ -1,9 +1,9 @@ #pragma once -#include "common.h" -#include "kernel/status.h" -#include "kernel/lb.h" -#include "kernel/ke.h" +#include "cdef.h" +#include "status.h" +#include "kernel.h" +#include "mlayout.h" /** * physical page allocation diff --git a/inc/kernel/rf.h b/kernel/inc/rf/ref.h similarity index 91% rename from inc/kernel/rf.h rename to kernel/inc/rf/ref.h index d2277ac..e266413 100644 --- a/inc/kernel/rf.h +++ b/kernel/inc/rf/ref.h @@ -1,8 +1,8 @@ #pragma once -#include "common.h" -#include "kernel/status.h" -#include "kernel/lb.h" +#include "cdef.h" +#include "status.h" +#include "lb/atree.h" typedef uint32 k_ident; diff --git a/inc/kernel/status.h b/kernel/inc/status.h similarity index 93% rename from inc/kernel/status.h rename to kernel/inc/status.h index 4492f98..90fb14f 100644 --- a/inc/kernel/status.h +++ b/kernel/inc/status.h @@ -1,6 +1,6 @@ #pragma once -#include "common.h" +#include "cdef.h" typedef uint32 k_status; diff --git a/kernel/ke/Rules.mk b/kernel/ke/Rules.mk index f6c664f..f61901f 100644 --- a/kernel/ke/Rules.mk +++ b/kernel/ke/Rules.mk @@ -3,10 +3,10 @@ include $(MK)/prologue.mk SRC_$(d) := $(d)/alloc.c \ $(d)/assert.c \ $(d)/atomic.c \ - $(d)/bug_check.c \ + $(d)/panic.c \ $(d)/intr.c \ $(d)/print.c \ - $(d)/rwwlock.c \ + $(d)/rww_lock.c \ $(d)/spin_lock.c \ $(d)/main.c diff --git a/kernel/ke/alloc.c b/kernel/ke/alloc.c index 5081007..4d76e31 100644 --- a/kernel/ke/alloc.c +++ b/kernel/ke/alloc.c @@ -1,4 +1,5 @@ -#include "kp.h" +#include "ke/alloc.h" +#include "lb/salloc.h" #define K_KERNEL_HEAP_SIZE 8192 @@ -16,15 +17,13 @@ ke_alloc_init(void) } void * -ke_alloc( - uint32 size) +ke_alloc(uint32 size) { return alloc_initialized ? lb_salloc(alloc_heap, size) : NULL; } void -ke_free( - void *ptr) +ke_free(void *ptr) { if (alloc_initialized) { diff --git a/kernel/ke/assert.c b/kernel/ke/assert.c index ae8b152..d7cbabb 100644 --- a/kernel/ke/assert.c +++ b/kernel/ke/assert.c @@ -1,4 +1,5 @@ -#include "kp.h" +#include "ke/assert.h" +#include "ke/print.h" void ke_assert_ex(const char *expr_str, const char *file, int32 line, int32 expr) { @@ -6,4 +7,4 @@ void ke_assert_ex(const char *expr_str, const char *file, int32 line, int32 expr { ke_printf("Assertion \"%s\" failed at %s:%d.\n", expr_str, file, line); } -} \ No newline at end of file +} diff --git a/kernel/ke/atomic.c b/kernel/ke/atomic.c index 3845017..faaeea6 100644 --- a/kernel/ke/atomic.c +++ b/kernel/ke/atomic.c @@ -1,4 +1,5 @@ -#include "kp.h" +#include "ke/atomic.h" +#include "hal.h" int32 ke_atomic_xchg_32(int32 *target, int32 val) { diff --git a/kernel/ke/bug_check.c b/kernel/ke/bug_check.c deleted file mode 100644 index 0ce2f9f..0000000 --- a/kernel/ke/bug_check.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "kp.h" - -void ke_panic(uint64 reason) -{ - ke_printf("BugCheck: Reason - %ul\n", reason); - hal_halt(); -} diff --git a/kernel/ke/intr.c b/kernel/ke/intr.c index 4c693e1..5f19ef5 100644 --- a/kernel/ke/intr.c +++ b/kernel/ke/intr.c @@ -1,23 +1,12 @@ -#include "common.h" -#include "kernel/status.h" -#include "kp.h" - -static uint32 irql_arr[IRQL_NUM]; - -k_status -kp_intr_init(struct boot_info *info) -{ - irql_arr[IRQL_HIGH] = info->intr_info.irql_high; - irql_arr[IRQL_DPC] = info->intr_info.irql_dpc; - irql_arr[IRQL_LOW] = info->intr_info.irql_low; - return STATUS_SUCCESS; -} - +#include "cdef.h" +#include "status.h" +#include "intrp.h" +#include "ke/assert.h" uint32 ke_raise_irql(uint32 irql) { - ke_assert(ke_get_irql() <= irql); + KE_ASSERT(ke_get_irql() <= irql); return hal_set_irql(irql); } @@ -26,7 +15,7 @@ uint32 ke_lower_irql(uint32 irql) { uint32 old_irql = ke_get_irql(); - ke_assert(old_irql >= irql); + KE_ASSERT(old_irql >= irql); return hal_set_irql(irql); } @@ -46,30 +35,36 @@ ke_issue_intr(uint32 core, uint32 vector) void -ke_reg_intr(uint32 index, intr_handler_fp handler) +ke_reg_intr(uint32 vec, k_intr_handler handler) { - hal_reg_intr(index, handler); + // TODO: implement kernel dispatch table + UNREFERENCED(vec); + UNREFERENCED(handler); } void -ke_dereg_intr(uint32 index) +ke_dereg_intr(uint32 vec) { - hal_dereg_intr(index); + // TODO: implement kernel dispatch table + UNREFERENCED(vec); } void -ke_reg_exc(uint32 exc, exc_handler_fp handler) +ke_reg_exc(uint32 vec, k_exc_handler handler) { - hal_reg_exc(exc, handler); + // TODO: implement kernel dispatch table + UNREFERENCED(vec); + UNREFERENCED(handler); } void -ke_dereg_exc(uint32 exc) +ke_dereg_exc(uint32 vec) { - hal_dereg_exc(exc); + // TODO: implement kernel dispatch table + UNREFERENCED(vec); } @@ -79,3 +74,13 @@ ke_get_core_id(void) return hal_get_core_id(); } + +k_status +kp_intr_init(struct boot_info *info) +{ + // TODO: initialize kernel dispatch table + UNREFERENCED(info); + return STATUS_INVALID_ARGS; +} + + diff --git a/kernel/ke/intrp.h b/kernel/ke/intrp.h new file mode 100644 index 0000000..a118d51 --- /dev/null +++ b/kernel/ke/intrp.h @@ -0,0 +1,9 @@ +#pragma once + +#include "hal.h" +#include "ke/intr.h" +#include "status.h" +#include "kernel.h" + +k_status +kp_intr_init(struct boot_info *info); diff --git a/kernel/ke/kp.h b/kernel/ke/kp.h deleted file mode 100644 index e064c95..0000000 --- a/kernel/ke/kp.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include "kernel/ke.h" - -/** - * interrupts - */ -k_status -kp_intr_init(struct boot_info *boot_info); diff --git a/kernel/ke/main.c b/kernel/ke/main.c index 1e101e9..315de7f 100644 --- a/kernel/ke/main.c +++ b/kernel/ke/main.c @@ -1,14 +1,17 @@ -#include "kp.h" -#include "kernel/mm.h" +#include "kernel.h" +#include "cdef.h" +#include "intrp.h" +#include "mm/pmm.h" +#include "ke/panic.h" /** * Kernel entry point * @param boot_info passed by the bootloader */ void KABI -ke_main(struct boot_info *boot_info) +kmain(struct boot_info *boot_info) { - k_status status = STATUS_SUCCESS; + k_status status; // initialize interrupts status = kp_intr_init(boot_info); diff --git a/kernel/ke/panic.c b/kernel/ke/panic.c new file mode 100644 index 0000000..323595e --- /dev/null +++ b/kernel/ke/panic.c @@ -0,0 +1,9 @@ +#include "ke/panic.h" +#include "ke/print.h" +#include "hal.h" + +void ke_panic(uint32 reason) +{ + ke_printf("BugCheck: Reason - %ul\n", reason); + hal_halt(); +} diff --git a/kernel/ke/print.c b/kernel/ke/print.c index c0d19fa..acf9f61 100644 --- a/kernel/ke/print.c +++ b/kernel/ke/print.c @@ -1,4 +1,5 @@ -#include "kp.h" +#include "ke/print.h" +#include "ke/assert.h" void ke_printf(const char *str, ...) @@ -13,7 +14,7 @@ void ke_vprintf(const char *str, va_list args) { //TODO: implement - ke_assert(0); + KE_ASSERT(0); UNREFERENCED(str); UNREFERENCED(args); } diff --git a/kernel/ke/rwwlock.c b/kernel/ke/rww_lock.c similarity index 85% rename from kernel/ke/rwwlock.c rename to kernel/ke/rww_lock.c index 42b3d2d..2cd7b23 100644 --- a/kernel/ke/rwwlock.c +++ b/kernel/ke/rww_lock.c @@ -1,7 +1,7 @@ -#include "kp.h" +#include "ke/rww_lock.h" void -ke_rww_init(struct rwwlock *lock) +ke_rww_init(struct rww_lock *lock) { if (lock != NULL) { @@ -15,7 +15,7 @@ ke_rww_init(struct rwwlock *lock) } void -ke_rww_r_lock(struct rwwlock *lock) +ke_rww_r_lock(struct rww_lock *lock) { if (lock != NULL) { @@ -32,7 +32,7 @@ ke_rww_r_lock(struct rwwlock *lock) } void -ke_rww_r_unlock(struct rwwlock *lock) +ke_rww_r_unlock(struct rww_lock *lock) { if (lock != NULL) { @@ -47,7 +47,7 @@ ke_rww_r_unlock(struct rwwlock *lock) } void -ke_rww_w_lock(struct rwwlock *lock) +ke_rww_w_lock(struct rww_lock *lock) { ke_spin_lock(&lock->w_mutex); lock->writer_ct++; @@ -60,7 +60,7 @@ ke_rww_w_lock(struct rwwlock *lock) } void -ke_rww_w_unlock(struct rwwlock *lock) +ke_rww_w_unlock(struct rww_lock *lock) { ke_spin_unlock(&lock->res_lock); ke_spin_lock(&lock->w_mutex); diff --git a/kernel/ke/spin_lock.c b/kernel/ke/spin_lock.c index 26be0a4..0194d23 100644 --- a/kernel/ke/spin_lock.c +++ b/kernel/ke/spin_lock.c @@ -1,4 +1,5 @@ -#include "kp.h" +#include "ke/spin_lock.h" +#include "ke/atomic.h" void ke_spin_init(struct spin_lock *lock) diff --git a/kernel/lb/atree.c b/kernel/lb/atree.c index 84f14f0..315e410 100644 --- a/kernel/lb/atree.c +++ b/kernel/lb/atree.c @@ -1,9 +1,8 @@ -#include "lp.h" +#include "lb/atree.h" +#include "clib.h" -static -struct atree_node * -atree_node_max( - struct atree_node *node) +static struct atree_node * +atree_node_max(struct atree_node *node) { while ((node != NULL) && (node->right != NULL)) { @@ -13,10 +12,8 @@ atree_node_max( } -static -struct atree_node * -atree_node_min( - struct atree_node *node) +static struct atree_node * +atree_node_min(struct atree_node *node) { while ((node != NULL) && (node->left != NULL)) { @@ -26,10 +23,8 @@ atree_node_min( } -static -void -atree_node_init( - struct atree_node *it) +static void +atree_node_init(struct atree_node *it) { if (it != NULL) { @@ -40,19 +35,15 @@ atree_node_init( } -static -int32 -atree_node_get_height( - struct atree_node *node) +static int32 +atree_node_get_height(struct atree_node *node) { return node == NULL ? -1 : node->height; } -static -int32 -atree_node_get_balance_factor( - struct atree_node *node) +static int32 +atree_node_get_balance_factor(struct atree_node *node) { if (node == NULL) { @@ -62,10 +53,8 @@ atree_node_get_balance_factor( } -static -struct atree_node * -atree_node_right_rotate( - struct atree_node *node) +static struct atree_node * +atree_node_right_rotate(struct atree_node *node) { struct atree_node *lchild = node->left; node->left = lchild->right; @@ -77,10 +66,8 @@ atree_node_right_rotate( } -static -struct atree_node * -atree_node_left_rotate( - struct atree_node *node) +static struct atree_node * +atree_node_left_rotate(struct atree_node *node) { struct atree_node *rchild = node->right; node->right = rchild->left; @@ -349,25 +336,21 @@ atree_node_delete(struct atree_node *node, struct atree_node *entry, atree_cmp_f struct atree_node * -lb_atree_min( - struct atree *tree) +lb_atree_min(struct atree *tree) { return atree_node_min(tree->root); } struct atree_node * -lb_atree_max( - struct atree *tree) +lb_atree_max(struct atree *tree) { return atree_node_max(tree->root); } struct atree_node * -lb_atree_next( - struct atree *tree, - struct atree_node *entry) +lb_atree_next(struct atree *tree, struct atree_node *entry) { struct atree_node *succ; struct atree_node *node; @@ -407,9 +390,7 @@ lb_atree_next( struct atree_node * -lb_atree_prev( - struct atree *tree, - struct atree_node *entry) +lb_atree_prev(struct atree *tree, struct atree_node *entry) { struct atree_node *prev; struct atree_node *node; @@ -449,18 +430,14 @@ lb_atree_prev( struct atree_node * -lb_atree_search( - struct atree *tree, - struct atree_node *entry) +lb_atree_search(struct atree *tree, struct atree_node *entry) { return atree_node_search(tree->root, entry, tree->cmpf, NULL); } struct atree_node * -lb_atree_insert( - struct atree *tree, - struct atree_node *entry) +lb_atree_insert(struct atree *tree, struct atree_node *entry) { struct atree_node *old; @@ -471,9 +448,7 @@ lb_atree_insert( struct atree_node * -lb_atree_delete( - struct atree *tree, - struct atree_node *entry) +lb_atree_delete(struct atree *tree, struct atree_node *entry) { struct atree_node *node; @@ -484,8 +459,7 @@ lb_atree_delete( uint32 -lb_atree_size( - struct atree *tree) +lb_atree_size(struct atree *tree) { uint32 size; struct atree_node *node; @@ -505,9 +479,7 @@ lb_atree_size( void -lb_atree_init( - struct atree *tree, - atree_cmp_fp compare) +lb_atree_init(struct atree *tree, atree_cmp_fp compare) { tree->cmpf = compare; tree->root = NULL; diff --git a/kernel/lb/llist.c b/kernel/lb/llist.c index fba915b..5ca7e37 100644 --- a/kernel/lb/llist.c +++ b/kernel/lb/llist.c @@ -1,4 +1,4 @@ -#include "lp.h" +#include "lb/llist.h" static void llist_node_init(struct llist_node *node) diff --git a/kernel/lb/lp.h b/kernel/lb/lp.h deleted file mode 100644 index 7ee31cb..0000000 --- a/kernel/lb/lp.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#include "kernel/lb.h" diff --git a/kernel/lb/salloc.c b/kernel/lb/salloc.c index a19d141..d347c37 100644 --- a/kernel/lb/salloc.c +++ b/kernel/lb/salloc.c @@ -1,4 +1,5 @@ -#include "lp.h" +#include "lb/salloc.h" +#include "clib.h" struct salloc_header { diff --git a/kernel/mm/mp.h b/kernel/mm/mp.h deleted file mode 100644 index f762dcf..0000000 --- a/kernel/mm/mp.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include "kernel/mm.h" - -/** - * PMM init info - */ -struct pmm_node -{ - uintptr base; - uint64 size; - uint32 attr; -}; - -struct pmm_info -{ - uint32 num_of_nodes; - struct pmm_node nodes[]; -}; - diff --git a/kernel/mm/pmm.c b/kernel/mm/pmm.c index d58414b..386e7f3 100644 --- a/kernel/mm/pmm.c +++ b/kernel/mm/pmm.c @@ -1,4 +1,10 @@ -#include "mp.h" +#include "mm/pmm.h" + +#include "lb/atree.h" +#include "lb/llist.h" +#include "ke/rww_lock.h" +#include "clib.h" +#include "ke/intr.h" struct phys_page_desc { @@ -10,7 +16,7 @@ struct phys_page_desc static struct atree active_tree; static struct llist free_list; -static struct rwwlock lock; +static struct rww_lock lock; /* * A comparison function between tree_node and your_node @@ -65,7 +71,7 @@ k_status mm_pmm_init(struct boot_info *info) // { // pmm_node_t *each_node = &info->nodes[i]; // -// ke_assert (each_node->base % KERNEL_PAGE_SIZE != 0); +// KE_ASSERT (each_node->base % KERNEL_PAGE_SIZE != 0); // // for (uint64 j = 0; j <= each_node->size; j++) // { diff --git a/kernel/rf/Rules.mk b/kernel/rf/Rules.mk index f5f70b5..8462177 100644 --- a/kernel/rf/Rules.mk +++ b/kernel/rf/Rules.mk @@ -1,6 +1,6 @@ include $(MK)/prologue.mk -SRC_$(d) := $(d)/rf.c +SRC_$(d) := $(d)/ref.c include $(MK)/stdrules.mk diff --git a/kernel/rf/rf.c b/kernel/rf/ref.c similarity index 90% rename from kernel/rf/rf.c rename to kernel/rf/ref.c index 69aa5a5..38adc5d 100644 --- a/kernel/rf/rf.c +++ b/kernel/rf/ref.c @@ -1,6 +1,9 @@ -#include "rp.h" -#include "kernel/ke.h" -#include "kernel/lb.h" +#include "rf/ref.h" +#include "ke/assert.h" +#include "ke/spin_lock.h" +#include "ke/atomic.h" +#include "ke/intr.h" +#include "clib.h" #define K_IDENT_BASE (0x80000000ul) @@ -58,7 +61,7 @@ rf_ref_init(void) k_status rf_ref_node_init(struct ref_node *rf_node, ref_free_fp free_func) { - ke_assert(ke_get_irql() <= IRQL_DPC); + KE_ASSERT(ke_get_irql() <= IRQL_DPC); rf_node->f_free = free_func; rf_node->ref_count = 1; @@ -70,7 +73,7 @@ rf_ref_node_init(struct ref_node *rf_node, ref_free_fp free_func) k_status rf_ref_obj(struct ref_node *ref_node) { - ke_assert(ke_get_irql() <= IRQL_DPC); + KE_ASSERT(ke_get_irql() <= IRQL_DPC); if (ref_node == NULL) { @@ -79,7 +82,7 @@ rf_ref_obj(struct ref_node *ref_node) int32 old_ref_count = ke_atomic_inc_32(&ref_node->ref_count, 1); - ke_assert(old_ref_count >= 1); + KE_ASSERT(old_ref_count >= 1); return STATUS_SUCCESS; } @@ -88,7 +91,7 @@ rf_ref_obj(struct ref_node *ref_node) k_status rf_deref_obj(struct ref_node *rf_node) { - ke_assert(ke_get_irql() <= IRQL_DPC); + KE_ASSERT(ke_get_irql() <= IRQL_DPC); if (rf_node == NULL) { @@ -99,7 +102,7 @@ rf_deref_obj(struct ref_node *rf_node) int32 old_ref_count = ke_atomic_inc_32(&rf_node->ref_count, -1); - ke_assert(old_ref_count >= 1); + KE_ASSERT(old_ref_count >= 1); if (old_ref_count == 1) { @@ -113,7 +116,7 @@ rf_deref_obj(struct ref_node *rf_node) k_status rf_open_obj_by_ident(k_ident id, struct ref_node **out) { - ke_assert(ke_get_irql() <= IRQL_DPC); + KE_ASSERT(ke_get_irql() <= IRQL_DPC); uint32 irql; k_status status = STATUS_SUCCESS; @@ -153,7 +156,7 @@ rf_ident_create(struct ref_node *rf_node, struct ident_node *id_node, k_ident *o k_status result; uint32 irql; - ke_assert(ke_get_irql() <= IRQL_DPC); + KE_ASSERT(ke_get_irql() <= IRQL_DPC); result = STATUS_SUCCESS; @@ -198,7 +201,7 @@ rf_ident_close(k_ident id) k_status status; struct ref_node *ref; - ke_assert(ke_get_irql() <= IRQL_DPC); + KE_ASSERT(ke_get_irql() <= IRQL_DPC); status = STATUS_SUCCESS; ref = NULL; diff --git a/kernel/rf/rp.h b/kernel/rf/rp.h deleted file mode 100644 index 436b4f0..0000000 --- a/kernel/rf/rp.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#include "kernel/rf.h" diff --git a/mk/Rules.mk b/mk/Rules.mk index 5077e99..d274a91 100644 --- a/mk/Rules.mk +++ b/mk/Rules.mk @@ -1,7 +1,8 @@ include $(MK)/prologue.mk -SRCIN_$(d) := $(d)/linker.ld.in \ - $(d)/grub.cfg.in +MOD:=MK + +SRCIN_$(d) := $(d)/linker.ld.in include $(MK)/stdrules.mk diff --git a/mk/grub.cfg.in b/mk/grub.cfg similarity index 100% rename from mk/grub.cfg.in rename to mk/grub.cfg diff --git a/mk/linker.ld.in b/mk/linker.ld.in index 6a4ca6b..b780979 100644 --- a/mk/linker.ld.in +++ b/mk/linker.ld.in @@ -1,9 +1,9 @@ #define ASM_FILE -#include "common.h" +#include "mlayout.h" OUTPUT_FORMAT(elf64-x86-64) OUTPUT_ARCH(i386:x86-64) -ENTRY(hal_main_32) +ENTRY(sys_entry) SECTIONS { diff --git a/mk/stdrules.mk b/mk/stdrules.mk index 9a15773..f9968a1 100644 --- a/mk/stdrules.mk +++ b/mk/stdrules.mk @@ -11,22 +11,25 @@ OBJAS_$(d) := $(OBJAS_$(d)) $(addprefix $(OUT)/, $(SRCAS_$(d):.asm=.a)) OBJIN_$(d) := $(OBJIN_$(d)) $(addprefix $(OUT)/, $(SRCIN_$(d):.in=)) DEP_$(d) := $(DEP_$(d)) $(addsuffix .d, $(OBJ_$(d)) $(OBJIN_$(d))) -$(OBJ_$(d)): $(OUT)/%.o: %.c +$(OUT)/$(d)/%.o: MOD:=$(MOD) + +$(OBJ_$(d)): $(OUT)/$(d)/%.o: $(d)/%.c $(MKDIR) $(COMP) $(GDEP) -$(OBJAS_$(d)): $(OUT)/%.a: %.asm +$(OUT)/$(d)/%.a: MOD:=$(MOD) + +$(OBJAS_$(d)): $(OUT)/$(d)/%.a: $(d)/%.asm $(MKDIR) $(COMPAS) -$(OBJIN_$(d)): $(OUT)/%: %.in +$(OBJIN_$(d)): $(OUT)/$(d)/%: $(d)/%.in $(MKDIR) $(PREP) $(GDEP) # append all OBJECTS to OBJ OBJ := $(OBJ) $(OBJ_$(d)) $(OBJAS_$(d)) -CLEAN := $(CLEAN) $(OBJ_$(d)) $(OBJAS_$(d)) $(OBJIN_$(d)) $(DEP_$(d)) -include $(DEP_$(d)) diff --git a/qemu.bat b/qemu.bat new file mode 100644 index 0000000..040c28f --- /dev/null +++ b/qemu.bat @@ -0,0 +1 @@ +qemu-system-x86_64 -bios qemu_bios.bin -cdrom out/secxkrnl.iso -s -S \ No newline at end of file diff --git a/qemu_bios.bin b/qemu_bios.bin new file mode 100644 index 0000000..455e121 Binary files /dev/null and b/qemu_bios.bin differ diff --git a/qemugdb b/qemugdb new file mode 100644 index 0000000..0385adf --- /dev/null +++ b/qemugdb @@ -0,0 +1,3 @@ +target remote localhost:1234 +symbol-file out/secxkrnl.elf +break *0x1001000 diff --git a/test/atree_test.c b/test/atree_test.c index c277ca0..48d61df 100644 --- a/test/atree_test.c +++ b/test/atree_test.c @@ -1,6 +1,7 @@ #include "test_main.h" #include "test_case.h" -#include "lb.h" +#include "lb/atree.h" +#include "clib.h" #include struct test_node diff --git a/test/inc/test_case.h b/test/inc/test_case.h index fbb2bac..2e6cbd0 100644 --- a/test/inc/test_case.h +++ b/test/inc/test_case.h @@ -1,7 +1,7 @@ #ifndef TEST_TEST_H #define TEST_TEST_H -#include "common.h" +#include "cdef.h" void linked_list_test(void); diff --git a/test/inc/test_main.h b/test/inc/test_main.h index d87ba24..3f4fd13 100644 --- a/test/inc/test_main.h +++ b/test/inc/test_main.h @@ -1,7 +1,7 @@ #ifndef TEST_DRIVER_H #define TEST_DRIVER_H -#include "common.h" +#include "cdef.h" void test_begin(char *name); diff --git a/test/kern_stub.c b/test/kern_stub.c index 15822f3..7175e88 100644 --- a/test/kern_stub.c +++ b/test/kern_stub.c @@ -1,6 +1,5 @@ -#include "common.h" -#include "kernel/ke.h" -#include "hal_export.h" +#include "cdef.h" +#include "hal.h" /** * Bogus implementation of HAL @@ -42,7 +41,7 @@ hal_issue_intr(uint32 core, uint32 vector) } void KABI -hal_reg_intr(uint32 index, intr_handler_fp handler) +hal_reg_intr(uint32 index, k_intr_dispatcher handler) { } @@ -52,7 +51,7 @@ hal_dereg_intr(uint32 index) } void KABI -hal_reg_exc(uint32 exc, exc_handler_fp handler) +hal_reg_exc(uint32 exc, k_exc_dispatcher handler) { } @@ -65,4 +64,10 @@ uint32 KABI hal_get_core_id(void) { return 0; +} + +void KABI +hal_halt(void) +{ + } \ No newline at end of file diff --git a/test/llist_test.c b/test/llist_test.c index 00dad61..6fe7c23 100644 --- a/test/llist_test.c +++ b/test/llist_test.c @@ -1,6 +1,7 @@ #include "test_main.h" #include "test_case.h" -#include "lb.h" +#include "lb/llist.h" +#include "clib.h" #include struct test_list_node diff --git a/test/salloc_test.c b/test/salloc_test.c index 7e01022..d180526 100644 --- a/test/salloc_test.c +++ b/test/salloc_test.c @@ -1,5 +1,5 @@ #include "test_main.h" -#include "lb.h" +#include "lb/salloc.h" #include "test_case.h" typedef union