diff --git a/Makefile b/Makefile index 96815f4..06a5fe9 100644 --- a/Makefile +++ b/Makefile @@ -46,16 +46,32 @@ AS_FLAGS = -w+all \ -g \ $(addprefix -I, $(INCLUDE_DIR)/) -LD_FLAGS = -lgcc \ +LD_FLAGS = -lgcc \ -nodefaultlibs \ -nostartfiles \ -nostdlib \ -Wl,-n \ -Wl,--build-id=none -COMP = $(CC) $(C_FLAGS) $^ -o $@ -COMPAS = $(AS) $(AS_FLAGS) $^ -o $@ +DUMP_FLAGS = -M intel \ + -D + +PREP_FLAGS = -E \ + -x c \ + -P \ + -traditional-cpp \ + $(C_FLAGS) + +GDEP_FLAGS = $(PREP_FLAGS) \ + -MMD \ + -MT $@ + + +COMP = $(CC) $(C_FLAGS) $< -o $@ +COMPAS = $(AS) $(AS_FLAGS) $< -o $@ LINK = $(LD) $(LD_FLAGS) $^ -o $@ -DUMP = $(DAS) -M intel -D $^ > $@ +DUMP = $(DAS) $(DUMP_FLAGS) $< > $@ +PREP = $(CC) $(PREP_FLAGS) $< > $@ +GDEP = $(CC) $(GDEP_FLAGS) -MF $*.d $< > /dev/null include Rules.top \ No newline at end of file diff --git a/Rules.top b/Rules.top index 8ab0e54..f5fdcb0 100644 --- a/Rules.top +++ b/Rules.top @@ -12,12 +12,14 @@ dir := lib include $(dir)/Rules.mk dir := test include $(dir)/Rules.mk +dir := mk +include $(dir)/Rules.mk TGT := secxkrnl.elf DMP := secxkrnl.dmp ISO := secxkrnl.iso -$(TGT): $(OBJ) +$(TGT): $(OBJ) $(LD_SCRIPT) $(LINK) -T $(LD_SCRIPT) $(DMP): $(TGT) @@ -25,7 +27,7 @@ $(DMP): $(TGT) .PHONY: clean clean: - rm $(OBJ) $(TGT) $(DMP) $(ISO) + rm -f $(CLEAN) $(TGT) $(DMP) $(ISO) .PHONY: compile compile: $(TGT) diff --git a/hal/Rules.mk b/hal/Rules.mk index 16ab394..bb07e2e 100644 --- a/hal/Rules.mk +++ b/hal/Rules.mk @@ -1,29 +1,16 @@ include $(MK)/prologue.mk -SRC_$(d) := boot.c \ - intr.c \ - mem.c \ - print.c +SRC_$(d) := $(d)/boot.c \ + $(d)/intr.c \ + $(d)/mem.c \ + $(d)/print.c -SRC_$(d) := $(addprefix $(d)/, $(SRC_$(d))) +SRCAS_$(d) := $(d)/boot.asm \ + $(d)/cpu.asm \ + $(d)/intr.asm -OBJ_$(d) := $(SRC_$(d):.c=.o) +SRCIN_$(d) := $(d)/boot.asm.in -SRCAS_$(d) := boot.asm \ - cpu.asm \ - intr.asm - -SRCAS_$(d) := $(addprefix $(d)/, $(SRCAS_$(d))) - -OBJAS_$(d) := $(SRCAS_$(d):.asm=.a) - -$(OBJ_$(d)): %.o: %.c - $(COMP) - -$(OBJAS_$(d)): %.a: %.asm - $(COMPAS) - -# append all OBJECTS to clean -OBJ := $(OBJ) $(OBJ_$(d)) $(OBJAS_$(d)) +include $(MK)/stdrules.mk include $(MK)/epilogue.mk \ No newline at end of file diff --git a/hal/boot.asm b/hal/boot.asm deleted file mode 100644 index 0a7eb05..0000000 --- a/hal/boot.asm +++ /dev/null @@ -1,332 +0,0 @@ -; Copyright 2016 secXsQuared -; Distributed under GPL license -; See COPYING under root for details - -%include "hal/addr.inc" - -MULTIBOOT_TAG_ALIGNMENT equ 8 -MULTIBOOT_HEADER_ALIGNMENT equ 8 -MULTIBOOT_LOADED_MAGIC equ 0x36d76289 -MULTIBOOT_MAGIC_NUMBER equ 0xE85250D6 -MULTIBOOT_ARCH equ 0 -; NASM does not like -MULTIBOOT_CHECK_SUM equ (0xFFFFFFFF - (MULTIBOOT_MAGIC_NUMBER + MULTIBOOT_HEADER_SIZE + MULTIBOOT_ARCH) + 1) -MULTIBOOT_REQ_LOADERNAME equ 2 -MULTIBOOT_REQ_MMAP equ 6 -MULTIBOOT_REQ_ACPI_RSDP equ 15 - -[SECTION .multiboot_header] -[BITS 32] -;==================== -;header tag -align MULTIBOOT_HEADER_ALIGNMENT -multiboot_header_tag: -dd MULTIBOOT_MAGIC_NUMBER -dd MULTIBOOT_ARCH -dd MULTIBOOT_HEADER_SIZE -dd MULTIBOOT_CHECK_SUM -;==================== -;INFO_REQUEST_TAG -align MULTIBOOT_TAG_ALIGNMENT -multiboot_info_tag: -dw 0x1 ; type=1 -dw 0x0 ; flag=0 -dd MULTIBOOT_INFO_TAG_SIZE -dd MULTIBOOT_REQ_LOADERNAME -dd MULTIBOOT_REQ_MMAP -dd MULTIBOOT_REQ_ACPI_RSDP -MULTIBOOT_INFO_TAG_SIZE equ ($ - multiboot_info_tag) -;==================== -;MODULE ALIGNMENT TAG -align MULTIBOOT_TAG_ALIGNMENT -dw 0x6; type=6 -dw 0x0; flag=0 -dd 0x8 -;==================== -;End_tag -align MULTIBOOT_TAG_ALIGNMENT -dw 0x0 -dw 0x0 -dd 0x8 -;==================== -MULTIBOOT_HEADER_SIZE equ ($ - multiboot_header_tag) - -[SECTION .text] -[BITS 32] -align 4096 -global hal_entry_32 -hal_entry_32: -; close interrupt -cli -cld - -; check loaded by grub -cmp eax,MULTIBOOT_LOADED_MAGIC -je .loaded_by_grub -hlt - -.loaded_by_grub: -; set stack pointer -mov esp, GET_PADDR(kernel_stack_end) - -; save multiboot_info* -mov esi,ebx - -; check x64 support -call halp_ensure_support_x64 -cmp eax,1 -je .init_x64 -hlt - -.init_x64: -; disable paging first -mov eax, cr0 ; Set the A-register to control register 0. -and eax, 0x7FFFFFFF ; Clear the PG-bit, which is bit 31. -mov cr0, eax ; Set control register 0 to the A-register. - -; for pages mapped here, attributes are always: -; PRESENT - 1 << 0 -; RW - 1 << 1 -; RING0 - 0 << 2 -; Page Level Write Back - 0 << 3 -; Page Cache Enabled - 0 << 4 -; Not Accessed - 0 << 5 -; Not dirty - 0 << 6 -; No PAT - 0 << 7 (must be 0 for recursive) -; Not Global - 0 << 8 -; Execution Enabled - 0 << 63 -; To summary - 3 + ADDR -; since 4k aligned, we can just add the attribute -PAGE_TABLE_ATTRIBUTE equ 0011b - -; need to map the PDE (1GB region) of the identity mapping + 2 * PTE (identity + vaddr) -; also need to map the kernel itself to the VIRT_ADDR + PHYS_ADDR -; calculate the Nth 2MB within the first 1GB -mov eax, HAL_KERNEL_BASE_PADDR -mov ecx, 1*1024*1024*1024; -xor edx, edx -div ecx ; we need the remainder here (EDX) -mov eax, edx -mov ecx, 2*1024*1024 ; 2MB -xor edx, edx -div ecx ; divide ecx:eax by ecx EAX = quotient, EDX = remainder, 0 in this case -mov edx, eax - -; map the identity PDE -mov eax, GET_PADDR(pde_base_kernel_static_identity) -mov dword [eax + edx * 8], GET_PADDR(pte_base_kernel_static) + PAGE_TABLE_ATTRIBUTE - -; map the kernel static PDE -mov eax, GET_PADDR(pde_base_kernel_static) -; map the first 2MB of the last 1GB to kernel static pte -mov dword [eax + edx * 8], GET_PADDR(pte_base_kernel_static) + PAGE_TABLE_ATTRIBUTE - -; map everything from the start of the kernel (0MB) -mov eax, GET_PADDR(pte_base_kernel_static) -mov edx, 0 ;512 times -mov ecx, HAL_KERNEL_BASE_PADDR + PAGE_TABLE_ATTRIBUTE -.pte_static_loop: -mov dword [eax + edx * 8], ecx -add ecx, 4096 ;4k per entry -inc edx -cmp edx, 512 -jne .pte_static_loop - -; enable PAE -mov eax, cr4 ; Set the A-register to control register 4. -or eax, 1 << 5 ; Set the PAE-bit, which is the 6th bit (bit 5). -mov cr4, eax ; Set control register 4 to the A-register. - -; enable x86_64 -mov ecx, 0xC0000080 ; Set the C-register to 0xC0000080, which is the EFER MSR. -rdmsr ; Read from the model-specific register. -or eax, 1 << 8 ; Set the LM-bit which is the 9th bit (bit 8). -wrmsr ; Write to the model-specific register. - -; let cr3 point at page table -mov eax, GET_PADDR(pml4_base) -mov cr3, eax - -; enable paging, enter compatibility mode -mov eax, cr0 ; Set the A-register to control register 0. -or eax, 1 << 31 ; Set the PG-bit, which is bit 31. -mov cr0, eax ; Set control register 0 to the A-register. - -; enter x64 -lgdt [GET_PADDR(GDT64.GDT64_PTR)] -jmp SLCT_CODE:GET_PADDR(halp_entry_64) -hlt - -halp_ensure_support_x64: -push ebp -mov ebp,esp -pushfd -pop eax -mov ecx, eax -xor eax, 1 << 21 -push eax -popfd -pushfd -pop eax -push ecx -popfd -xor eax, ecx -jz .not_supported -mov eax, 0x80000000 ; Set the A-register to 0x80000000. -cpuid ; CPU identification. -cmp eax, 0x80000001 ; Compare the A-register with 0x80000001. -jb .not_supported ; It is less, there is no 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. -mov eax,1 -jmp .end -.not_supported: -xor eax,eax -.end: -mov esp,ebp -pop ebp -ret - -[SECTION .text] -[BITS 64] -extern hal_main -halp_entry_64: -; note that we are still at the identity mapping -mov rax, .entry_64 -jmp rax -.entry_64: -mov ax,SLCT_DATA -mov ds,ax -mov es,ax -mov fs,ax -mov gs,ax -mov ss,ax - -; set correct rsp to the new kernel stack vaddr, the top of which is the start of the kernel -mov rsp, HAL_KERNEL_BASE_VADDR -mov rdi, rsi ; multiboot_info* -add rdi, HAL_KERNEL_BASE_VADDR -call hal_main -hlt - -[SECTION .data] -[BITS 64] -; we only map two sections: the last 512 GB for kernel and the second last 512 GB for -; recursive page table. YA BOI GUZMA -; 0xFFFF 8000 0000 0000 unmapped kernel -; 0xFFFF FF00 0000 0000 512 GB Recursive page table (MAP THIS) -; 0xFFFF FF80 0000 0000 510 GB Kernel Dynamic -; inside dynamic, the lowest addr is for heap and the highest is for the stack -; 0xFFFF FFFF 8000 0000 to 0xFFFF FFFF FFFF FFFF 2 GB Kernel static (MAP THIS) - -align 4096 -pml4_base: -; 512 GB per entry, we need 4 ENTRIES, BUT ONE IS MAPPED TO ITSELF -; within the first 512G (Definitely since grub does not load to more than 4G), identity map the kernel -dq GET_PADDR(pdpt_base_identity) + PAGE_TABLE_ATTRIBUTE -times 509 dq 0 -; 511th entry for 2nd last 512G -dq GET_PADDR(pml4_base) + PAGE_TABLE_ATTRIBUTE -; 512th entry for last 512G (kernel space) -dq GET_PADDR(pdpt_base) + PAGE_TABLE_ATTRIBUTE - -align 4096 -; 1GB per entry -pdpt_base: -; this pdpt is for the pml4 last 512 GB entry -; we need 3 ENTRIES, 1 for kernel and 2 for kernel dynamics (first 1GB heap + last 1GB stack) -; map the 1st GB to kernel dynamic heap -dq GET_PADDR(pde_base_kernel_dynamic_heap) + PAGE_TABLE_ATTRIBUTE -times 508 dq 0 -; map the 510th GB to kernel dynamic stack -dq GET_PADDR(pde_base_kernel_dynamic_stack) + PAGE_TABLE_ATTRIBUTE -; map the 511th GB to kernel static space -dq GET_PADDR(pde_base_kernel_static) + PAGE_TABLE_ATTRIBUTE -; the 512th GB is unmapped -dq 0 - -pdpt_base_identity: -; this pdpt is for the kernel identity mapping -; the kernel is definitely loaded to the first 1GB -dq GET_PADDR(pde_base_kernel_static_identity) + PAGE_TABLE_ATTRIBUTE -times 511 dq 0 - -align 4096 -; 2MB per entry -pde_base_kernel_dynamic_heap: -; this pde is for the kernel dynamic pdpt entry (kernel heap), we need 1 entry to map the initial 2M -; map the first 2MB of the 510 GB to kernel heap -dq GET_PADDR(pte_base_kernel_dynamic_heap) + PAGE_TABLE_ATTRIBUTE -times 511 dq 0 - -pde_base_kernel_dynamic_stack: -; this pde is for the kernel dynamic pdpt entry (kernel stack), we need 1 entry to map the last 2M -times 511 dq 0 -; map the last 2MB of the 510 GB to kernel stack -dq GET_PADDR(pte_base_kernel_dynamic_stack) + PAGE_TABLE_ATTRIBUTE - -pde_base_kernel_static: -; this pde is for the kernel static pdpt entry (kernel core), we need 1 entry to map the initial 2M -; map the first 2MB of the last 1GB to kernel static pte -times 512 dq 0 - -pde_base_kernel_static_identity: -; this pde is for the kernel static identity pdpt entry (kernel core), we need 1 entry to map the initial 2M -times 512 dq 0 - -align 4096 -; 4KB per entry -pte_base_kernel_dynamic_heap: -; this pte is for the kernel dynamic heap pde entry, we need 1 entry to map the kernel heap (4k) -; map the heap segment (first 4k) -dq GET_PADDR(kernel_heap) + PAGE_TABLE_ATTRIBUTE -times 511 dq 0 - -pte_base_kernel_dynamic_stack: -; this pte is for the kernel dynamic stack pde entry, we need 1 entry to map the kernel stack (4k) -times 511 dq 0 -; map the stack segment (last 4k) -dq GET_PADDR(kernel_stack) + PAGE_TABLE_ATTRIBUTE - -pte_base_kernel_static: -; this pte is for the kernel static pde entry, we need 512 entries, map the complete 1st 2MB (TODO: calculate kernel size) -times 512 dq 0 - -align 4096 -; long mode gdt -GDT64: ; Global Descriptor Table (64-bit). - ; NULL - dw 0 ; Limit (low). - dw 0 ; Base (low). - db 0 ; Base (middle) - db 0 ; Access. - db 0 ; Granularity. - db 0 ; Base (high). - SLCT_CODE equ $ - GDT64 ; The code descriptor. - dw 0 ; Limit (low). - dw 0 ; Base (low). - db 0 ; Base (middle) - db 10011010b ; Access. - db 00100000b ; Granularity. - db 0 ; Base (high). - SLCT_DATA equ $ - GDT64 ; The data descriptor. - dw 0 ; Limit (low). - dw 0 ; Base (low). - db 0 ; Base (middle) - db 10010010b ; Access. - db 00100000b ; Granularity. - db 0 ; Base (high). - .GDT64_PTR: ; The GDT-pointer. - dw $ - GDT64 - 1 ; Limit. - dq GET_PADDR(GDT64) ; Base. - -align 4096 -kernel_stack: -times 4096 db 0 -kernel_stack_end: - -align 4096 -kernel_heap: -times 4096 db 0 \ No newline at end of file diff --git a/hal/boot.asm.in b/hal/boot.asm.in new file mode 100644 index 0000000..caf3f2d --- /dev/null +++ b/hal/boot.asm.in @@ -0,0 +1,219 @@ +#include "kernel/hal/memdef.h" + +%define GET_PADDR(x) ((x) - KERNEL_IMAGE_VADDR) +%define BOCHS_BREAK xchg bx,bx + +extern hal_main +extern hal_write_initial_page_table +global hal_entry_32 + +section .multiboot_header +bits 32 +MULTIBOOT_TAG_ALIGNMENT equ 8 +MULTIBOOT_HEADER_ALIGNMENT equ 8 +MULTIBOOT_LOADED_MAGIC equ 0x36d76289 +MULTIBOOT_MAGIC_NUMBER equ 0xE85250D6 +MULTIBOOT_ARCH equ 0 +MULTIBOOT_CHECK_SUM equ (0xFFFFFFFF - (MULTIBOOT_MAGIC_NUMBER + 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_ALIGNMENT +multiboot_header_tag: + dd MULTIBOOT_MAGIC_NUMBER + dd MULTIBOOT_ARCH + dd MULTIBOOT_HEADER_SIZE + dd MULTIBOOT_CHECK_SUM +;==================== +;INFO_REQUEST_TAG +align MULTIBOOT_TAG_ALIGNMENT +multiboot_info_tag: + dw 0x1 ; type=1 + dw 0x0 ; flag=0 + dd MULTIBOOT_INFO_TAG_SIZE + dd MULTIBOOT_REQ_LOADERNAME + dd MULTIBOOT_REQ_MMAP + dd MULTIBOOT_REQ_ACPI_RSDP +MULTIBOOT_INFO_TAG_SIZE equ ($ - multiboot_info_tag) +;==================== +;MODULE ALIGNMENT TAG +align MULTIBOOT_TAG_ALIGNMENT + dw 0x6; type=6 + dw 0x0; flag=0 + dd 0x8 +;==================== +align MULTIBOOT_TAG_ALIGNMENT + ;End_tag + dw 0x0 + dw 0x0 + dd 0x8 +;==================== +MULTIBOOT_HEADER_SIZE equ ($ - multiboot_header_tag) + + +section .text +bits 32 +align 4096 +hal_entry_32: + cli + cld + cmp eax,MULTIBOOT_LOADED_MAGIC + je .loaded_by_grub + hlt + +.loaded_by_grub: + ; save multiboot_info* + mov esi,ebx + call halp_check_long_mode + cmp eax,1 + je .init_long_mode + hlt + +.init_long_mode: + ; disable paging first + mov eax, cr0 ; Set the A-register to control register 0. + and eax, ~(1 << 31) ; Clear the PG-bit, which is bit 31. + mov cr0, eax ; Set control register 0 to the A-register. + + ; identity map the first 4GB + ; ATTRIBUTE = READ/WRITE + SU + mov eax, GET_PADDR(_pml4) + mov dword [eax], GET_PADDR(_pdpt) + 11b + + ; write values for pdpt + mov ecx, 10000011b + + mov eax, GET_PADDR(_pdpt) + mov dword [eax], ecx + + add eax,8 + add ecx,0x40000000 ;1G + mov dword [eax], ecx + + add eax,8 + add ecx,0x40000000 ;1G + mov dword [eax], ecx + + add eax,8 + add ecx,0x40000000 ;1G + mov dword [eax], ecx + + BOCHS_BREAK + + ; enable PAE + mov eax, cr4 ; Set the A-register to control register 4. + or eax, 1 << 5 ; Set the PAE-bit, which is the 6th bit (bit 5). + mov cr4, eax ; Set control register 4 to the A-register. + + ; enable long mode + mov ecx, 0xC0000080 ; Set the C-register to 0xC0000080, which is the EFER MSR. + rdmsr ; Read from the model-specific register. + or eax, 1 << 8 ; Set the LM-bit which is the 9th bit (bit 8). + wrmsr ; Write to the model-specific register. + + ; let cr3 point at page table + mov eax, GET_PADDR(_pml4) + mov cr3, eax + + ; enable paging, enter compatibility mode + mov eax, cr0 ; Set the A-register to control register 0. + or eax, 1 << 31 ; Set the PG-bit, which is bit 31. + mov cr0, eax ; Set control register 0 to the A-register. + + ; enter long mode + lgdt [GET_PADDR(_gdt.ptr)] + jmp _gdt.code:GET_PADDR(halp_entry_64) + hlt + +halp_check_long_mode: + push ebp + mov ebp,esp + pushfd + pop eax + mov ecx, eax + xor eax, 1 << 21 + push eax + popfd + pushfd + pop eax + push ecx + popfd + xor eax, ecx + jz .not_supported + mov eax, 0x80000000 ; Set the A-register to 0x80000000. + cpuid ; CPU identification. + cmp eax, 0x80000001 ; Compare the A-register with 0x80000001. + jb .not_supported ; It is less, there is no 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. + mov eax,1 + jmp .end +.not_supported: + xor eax,eax +.end: + mov esp,ebp + pop ebp + ret + +section .text +bits 64 +halp_entry_64: + ; note that we are still at the identity mapping + mov ax,_gdt.data + mov ds,ax + mov es,ax + mov fs,ax + mov gs,ax + mov ss,ax + + mov rsp, GET_PADDR(_stack) + mov rdi, rsi ; multiboot_info* + call hal_write_initial_page_table + test rax,rax + jne .end + call hal_main +.end: + hlt + +section .data +bits 64 +align 4096 + times 4096 db 0 +_stack: + +_pml4: +align 4096 + times 4096 db 0 +_pdpt: +align 4096 + times 4096 db 0 +_gdt: ; Global Descriptor Table (long mode). +.null: equ $ - _gdt ; The null descriptor. + dw 0 ; Limit (low). + dw 0 ; Base (low). + db 0 ; Base (middle) + db 0 ; Access. + db 0 ; Granularity. + db 0 ; Base (high). +.code: equ $ - _gdt ; The code descriptor. + dw 0 ; Limit (low). + dw 0 ; Base (low). + db 0 ; Base (middle) + db 10011010b ; Access (exec/read). + db 00100000b ; Granularity. + db 0 ; Base (high). +.data: equ $ - _gdt ; The data descriptor. + dw 0 ; Limit (low). + dw 0 ; Base (low). + db 0 ; Base (middle) + db 10010010b ; Access (read/write). + db 00000000b ; Granularity. + db 0 ; Base (high). +.ptr: + ; GDT PTR + dw $ - _gdt - 1 ; Limit. + dq GET_PADDR(_gdt) ; Base. \ No newline at end of file diff --git a/hal/boot.c b/hal/boot.c index 6a49935..6ea7171 100644 --- a/hal/boot.c +++ b/hal/boot.c @@ -10,9 +10,6 @@ #include "lib/sxtdlib.h" #include "hal/boot.h" -extern char HAL_KERNEL_START_VADDR[]; -extern char HAL_KERNEL_END_VADDR[]; - static void KABI halp_obtain_cpu_info(boot_info_t *hal_info) { if(hal_info == NULL) @@ -37,9 +34,7 @@ void KABI hal_main(void *m_info) boot_info_t* boot_info = halloc(sizeof(boot_info_t)); - // set up HAL def - boot_info->krnl_start = (virtual_addr_t)HAL_KERNEL_START_VADDR; - boot_info->krnl_end = (virtual_addr_t)HAL_KERNEL_END_VADDR; + boot_info->krnl_end = KERNEL_IMAGE_END_VADDR; // obtain cpu info halp_obtain_cpu_info(boot_info); diff --git a/hal/mem.c b/hal/mem.c index c49a323..91036b0 100644 --- a/hal/mem.c +++ b/hal/mem.c @@ -4,6 +4,7 @@ #include "hal/cpu.h" #include "lib/salloc.h" #include "hal/intr.h" +#include "status.h" static uint8_t _gdts[HAL_CORE_COUNT][GDT_ENTRY_NUM * GDT_ENTRY_SIZE]; static hal_gdt_ptr_t _gdt_ptrs[HAL_CORE_COUNT]; @@ -20,22 +21,46 @@ char kernel_heap[KERNEL_HEAP_SIZE]; * @param pt_base page table base paddr * @param pt_end page table entry paddr */ -void KABI hal_write_initial_page_table(void* k_start, void* k_end, void* multiboot_info, void* pt_base, void* pt_end) +status_t KABI hal_write_initial_page_table(void* multiboot_info) { + UNREFERENCED(multiboot_info); + + /* + // still identity mapping uint32_t pt_num = 0; uint32_t pd_num = 0; uint32_t pdpt_num = 0; uint32_t pml4_num = 0; // calculate the number of page tables required: - uint64_t k_size = (uintptr_t)k_start - (uintptr_t)k_end; - // + uint64_t k_size = (uintptr_t)KERNEL_IMAGE_END_VADDR - (uintptr_t)KERNEL_IMAGE_VADDR; + // see multiboot boot info header uint32_t m_size = *(uint32_t *)multiboot_info; + // how many pages do we need to hold the entries + // 512 page table entries per 4k page + pt_num = (1 + (uint32_t)((k_size + m_size - 1) / KERNEL_PAGE_SIZE)) / 512; + pd_num = 1 + (pt_num - 1) / 512; + pdpt_num = 1 + (pd_num - 1) / 512; + pml4_num = 1 + (pdpt_num - 1) / 512; - // construct recursive mapping with the first page table + // calculate the # of page tables + if ((((uintptr_t)(pt_end) - (uintptr_t)(pt_base)) / KERNEL_PAGE_SIZE) < (pt_num + pd_num + pdpt_num + pml4_num)) + { + return STATUS_FAIL; + } + // map kernel first + KERNEL_IMAGE_VADDR = ; + // map kernel dynamic + KERNEL_DYNAMIC_SIZE = ; + + // map recursive page tables + hal_write_pml4(pt_base, (uintptr_t)pt_base, PML4_PRESENT | PML4_WRITE); + */ + + return STATUS_SUCCESS; } diff --git a/include/hal/addr.inc b/include/hal/addr.inc deleted file mode 100644 index 41697bb..0000000 --- a/include/hal/addr.inc +++ /dev/null @@ -1,7 +0,0 @@ -HAL_KERNEL_BASE_PADDR equ 0x1000000 ; #16 MB. The current setup requires the kernel to be loaded to the first 1GB -global HAL_KERNEL_BASE_PADDR - -HAL_KERNEL_BASE_VADDR equ 0xFFFFFFFF80000000 -global HAL_KERNEL_BASE_VADDR - -%define GET_PADDR(x) ((x) - HAL_KERNEL_BASE_VADDR) \ No newline at end of file diff --git a/include/hal/mem.h b/include/hal/mem.h index f3d72fe..9080c29 100644 --- a/include/hal/mem.h +++ b/include/hal/mem.h @@ -74,12 +74,10 @@ void KABI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uin #define PT_GLOBAL (1ull << 8) #define PT_EXECUTION_DISABLED (1ull << 63) -#define PML4_ENTRY_NUM(mem) ((mem) / (4096ull * 512ull * 512ull * 512ull)) -#define PDPT_ENTRY_NUM(mem) ((mem) / (4096ull * 512ull * 512ull)) -#define PD_ENTRY_NUM(mem) ((mem) / (4096ull*512ull)) -#define PT_ENTRY_NUM(mem) ((mem) / 4096ull) - -#define PAGE_ENTRY_BASE(PAGE_ENTRY) ((PAGE_ENTRY) & 0xFFFFFFFFFF000) +#define PML4_ENTRY_NUM(vaddr) (((vaddr) >> 39) & 0x1FF) +#define PDPT_ENTRY_NUM(vaddr) (((vaddr) >> 30) & 0x1FF) +#define PD_ENTRY_NUM(vaddr) (((vaddr) >> 21) & 0x1FF) +#define PT_ENTRY_NUM(vaddr) (((vaddr) >> 12) & 0x1FF) void KABI hal_write_pml4(void *const base, uintptr_t const pdpt_addr, uint64_t const attr); diff --git a/include/kernel/hal/boot.h b/include/kernel/hal/boot.h index d64346d..20ae282 100644 --- a/include/kernel/hal/boot.h +++ b/include/kernel/hal/boot.h @@ -11,8 +11,7 @@ typedef struct { - virtual_addr_t krnl_start; - virtual_addr_t krnl_end; + void* krnl_end; intr_info_t intr_info; char cpu_vd_str[13]; } boot_info_t; diff --git a/include/kernel/hal/mem.h b/include/kernel/hal/mem.h index 9df3da9..0ddfdfb 100644 --- a/include/kernel/hal/mem.h +++ b/include/kernel/hal/mem.h @@ -3,35 +3,12 @@ #include "type.h" #include "lib/linked_list.h" +#include "kernel/hal/memdef.h" /** - Kernel Memory Layout -**/ -#define KERNEL_PAGE_SIZE (0x1000ull) - -#define KERNEL_AREA_START_VADDR (0xFFFF800000000000ull) -#define KERNEL_AREA_SIZE (0xFFFFFFFFFFFFFFFF - KERNEL_AREA_START_VADDR + 1) - -#define KERNEL_PAGE_TABLE_VADDR (0xFFFFFF0000000000ull) -#define KERNEL_PAGE_TABLE_SIZE (0x8000000000ull) - -// 510 GB -#define KERNEL_DYN_VADDR (KERNEL_PAGE_TABLE_VADDR + KERNEL_PAGE_TABLE_SIZE) -#define KERNEL_DYN_SIZE (0x‬‭7F80000000ull) - -#define KERNEL_HEAP_VADDR KERNEL_DYN_VADDR -#define KERNEL_INITIAL_HEAP_SIZE (0x1000ull) - -#define KERNEL_INITIAL_STACK_SIZE (0x1000ull) -#define KERNEL_STACK_VADDR (KERNEL_DYN_VADDR + KERNEL_DYN_SIZE - KERNEL_INITIAL_STACK_SIZE) - -// address space that is reserved for HAL to map its own stuff -#define KERNEL_HAL_VADDR (KERNEL_DYN_VADDR + KERNEL_DYN_SIZE) -// 16MB Virtual Address Space -#define KERNEL_HAL_VADDR_LIMIT (0x1000000ull) - -#define KERNEL_LOAD_VADDR (KERNEL_HAL_VADDR + KERNEL_HAL_VADDR_LIMIT) -#define KERNEL_LOAD_SIZE (0xFFFFFFFFFFFFFFFF - KERNEL_LOAD_VADDR + 1) + * From linker.inc + */ +extern char KERNEL_IMAGE_END_VADDR[]; /** * PMM init info @@ -49,5 +26,4 @@ typedef struct pmm_node_t nodes[]; } pmm_info_t; - #endif diff --git a/include/kernel/hal/memdef.h b/include/kernel/hal/memdef.h new file mode 100644 index 0000000..57b5d5d --- /dev/null +++ b/include/kernel/hal/memdef.h @@ -0,0 +1,32 @@ +#ifndef _KERNEL_HAL_MEMDEF_H_ +#define _KERNEL_HAL_MEMDEF_H_ +/** + * Kernel Memory Layout + * ----------------------- 0x0000,0000,0000,0000 - User Space + * Application SIZE: 0x0000,8000,0000,0000 (256x PML4) + * ----------------------- 0x0000,8000,0000,0000 + * Non-canonical + * ----------------------- 0xFFFF,8000,0000,0000 - Kernel Space + * Reserved SIZE: 0x0000,7F00,0000,0000 (254x PML4) + * ----------------------- 0xFFFF,FF00,0000,0000 + * Page Table SIZE: 0x0000,0080,0000,0000 (1x PML4) + * ----------------------- 0xFFFF,FF80,0000,0000 + * Kernel Dynamic SIZE: 0x0000,007F,8000,0000 (Kernel Dynamic + Kernel Image = 1x PML4) + * ----------------------- 0xFFFF,FFFF,8000,0000 + * Kernel Image SIZE: 0x0000,0000,8000,0000 + * ----------------------- 0xFFFF,FFFF,FFFF,FFFF +**/ + +#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) +#endif \ No newline at end of file diff --git a/include/kernel/hal/status.h b/include/kernel/hal/status.h deleted file mode 100644 index e17181f..0000000 --- a/include/kernel/hal/status.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef _KERNEL_HAL_STATUS_H_ -#define _KERNEL_HAL_STATUS_H_ - -#endif \ No newline at end of file diff --git a/include/kernel/ke/status.h b/include/kernel/ke/status.h deleted file mode 100644 index d19e6ca..0000000 --- a/include/kernel/ke/status.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef _KERNEL_KE_STATUS_H_ -#define _KERNEL_KE_STATUS_H_ - -#include "type.h" -#include "lib/sxtdlib.h" -#include "kernel/hal/status.h" - -typedef uint32_t status_t; - -// -// 32 bit ints -// -// bits 30 - 31 - Error/Success -// 00 = Success -// 01 = Error -// -// bits 0-14 - Return Code - 32768 in total -// bits 15-29 - Facility 32768 in total -// - -#define SX_SUCCESS(status) (((status) >> 30) == 0) -#define SX_RETURN(status) (((status) & ke_bit_field_mask(15,29)) >> 15) -#define SX_FACILITY(status) ((status) & ke_bit_field_mask(0,14)) - -#define SX_MAKE_STATUS(Severity, Facility, Return) (((Severity) << 30) | ((Facility) << 15) | (Return)) - -#define SEVERITY_ERROR 0x1 -#define SEVERITY_SUCCESS 0x0 - -#define FACILITY_GENERIC 0 -#define FACILITY_REF 1 -#define FACILITY_PMM 2 - -enum _status_t -{ - STATUS_SUCCESS = SX_MAKE_STATUS(SEVERITY_SUCCESS, FACILITY_GENERIC, 0), - - REF_STATUS_UNINITIALIZED = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_REF, 1), - - REF_STATUS_ALLOCATION_FAILED = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_REF, 2), - - REF_STATUS_INVALID_ARGUMENTS = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_REF, 1), - - REF_STATUS_INVALID_HANDLE = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_REF, 1), - - REF_STATUS_DUPLICATED_HANDLE = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_REF, 1), - - PMM_STATUS_INVALID_ARGUMENTS = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_PMM, 1), - - PMM_STATUS_ALLOCATION_FAILED = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_PMM, 3), - - PMM_STATUS_UNINITIALIZED = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_PMM, 4), - - PMM_STATUS_NOT_ENOUGH_PAGE = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_PMM, 5), -}; - -#endif \ No newline at end of file diff --git a/include/kernel/mm/pmm.h b/include/kernel/mm/pmm.h index c657550..294e112 100644 --- a/include/kernel/mm/pmm.h +++ b/include/kernel/mm/pmm.h @@ -1,12 +1,12 @@ -#ifndef _PMM_H_ -#define _PMM_H_ +#ifndef _KERNEL_MM_PMM_H_ +#define _KERNEL_MM_PMM_H_ #include "type.h" #include "lib/avl_tree.h" #include "lib/linked_list.h" #include "kernel/mm/mem.h" #include "kernel/ke/atomic.h" -#include "kernel/ke/status.h" +#include "status.h" //#define PMM_PAGE_ATTR_FREE_BIT 0 //#define PMM_PAGE_ATTR_PAGED_BIT 1 @@ -16,20 +16,21 @@ // uint32_t attr; //} k_physical_page_attr_t; + status_t KABI sx_pmm_init(pmm_info_t *info); -status_t KABI mm_alloc_page(physical_addr_t *out); +status_t KABI mm_alloc_page(uintptr_t *out); -status_t KABI mm_free_page(physical_addr_t base); +status_t KABI mm_free_page(uintptr_t base); -status_t KABI mm_query_page_attr(physical_addr_t base, +status_t KABI mm_query_page_attr(uintptr_t base, int32_t *out); // TODO: implement these somehow, i might just reserve the first 16MB for these int32_t KABI mm_alloc_contiguous_pages(uint64_t num_of_page, - physical_addr_t highest_p_addr, - physical_addr_t *out); + uintptr_t highest_p_addr, + uintptr_t *out); -int32_t KABI mm_free_contiguous_pages(physical_addr_t base); +int32_t KABI mm_free_contiguous_pages(uintptr_t base); #endif \ No newline at end of file diff --git a/include/kernel/rf/ref.h b/include/kernel/rf/ref.h index 0290e65..8dc7d57 100644 --- a/include/kernel/rf/ref.h +++ b/include/kernel/rf/ref.h @@ -1,8 +1,8 @@ -#ifndef _K_REF_H_ -#define _K_REF_H_ +#ifndef _KERNEL_RF_REF_H_ +#define _KERNEL_RF_REF_H_ #include "type.h" -#include "kernel/ke/status.h" +#include "status.h" typedef uint32_t handle_t; diff --git a/include/status.h b/include/status.h new file mode 100644 index 0000000..1772454 --- /dev/null +++ b/include/status.h @@ -0,0 +1,53 @@ +#ifndef _STATUS_H_ +#define _STATUS_H_ + +#include "type.h" +#include "lib/sxtdlib.h" + +typedef uint32_t status_t; + +// +// 32 bit ints +// +// bits 30 - 31 - Error/Success +// 00 = Success +// 01 = Error +// +// bits 0-14 - Return Code - 32768 in total +// bits 15-29 - Facility 32768 in total +// + +#define SX_MAKE_STATUS(Severity, Facility, Return) (((Severity) << 30) | ((Facility) << 16) | (Return)) + +#define SEVERITY_ERROR 0x3 +#define SEVERITY_SUCCESS 0x0 +#define SEVERITY_INFO 0x1 + +#define FACILITY_GENERIC 0 +#define FACILITY_RF 1 +#define FACILITY_MM 2 + +static inline bool sx_success(status_t status) +{ + uint32_t severity = status >> 30; + return (severity == SEVERITY_INFO) || (severity == SEVERITY_SUCCESS); +} + +enum _status_t +{ + STATUS_SUCCESS = SX_MAKE_STATUS(SEVERITY_SUCCESS, FACILITY_GENERIC, 0), + STATUS_FAIL = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_GENERIC, 0), + + RF_UNINITIALIZED = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_RF, 1), + RF_ALLOCATION_FAILED = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_RF, 2), + RF_INVALID_ARGUMENTS = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_RF, 3), + RF_INVALID_HANDLE = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_RF, 4), + RF_DUPLICATED_HANDLE = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_RF, 5), + + MM_INVALID_ARGUMENTS = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_MM, 1), + MM_ALLOCATION_FAILED = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_MM, 2), + MM_UNINITIALIZED = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_MM, 3), + MM_NOT_ENOUGH_PAGE = SX_MAKE_STATUS(SEVERITY_ERROR, FACILITY_MM, 4), +}; + +#endif \ No newline at end of file diff --git a/kernel/ke/Rules.mk b/kernel/ke/Rules.mk index c4fa70a..deaf32e 100644 --- a/kernel/ke/Rules.mk +++ b/kernel/ke/Rules.mk @@ -1,23 +1,15 @@ include $(MK)/prologue.mk -SRC_$(d) := alloc.c \ - assert.c \ - atomic.c \ - boot.c \ - bug_check.c \ - intr.c \ - print.c \ - rwwlock.c \ - spin_lock.c - -SRC_$(d) := $(addprefix $(d)/, $(SRC_$(d))) - -OBJ_$(d) := $(SRC_$(d):.c=.o) - -$(OBJ_$(d)): %.o: %.c - $(COMP) - -# append all OBJECTS to clean -OBJ := $(OBJ) $(OBJ_$(d)) +SRC_$(d) := $(d)/alloc.c \ + $(d)/assert.c \ + $(d)/atomic.c \ + $(d)/boot.c \ + $(d)/bug_check.c \ + $(d)/intr.c \ + $(d)/print.c \ + $(d)/rwwlock.c \ + $(d)/spin_lock.c + +include $(MK)/stdrules.mk include $(MK)/epilogue.mk \ No newline at end of file diff --git a/kernel/ke/boot.c b/kernel/ke/boot.c index cb4a331..0214ee4 100644 --- a/kernel/ke/boot.c +++ b/kernel/ke/boot.c @@ -23,10 +23,10 @@ void KABI ke_main(boot_info_t *boot_info) ke_alloc_init(); - ke_printf("KERNEL: Base Addr is 0x%X. Size is %uB, %uKB.\n", - boot_info->krnl_start, - (boot_info->krnl_end - boot_info->krnl_start), - (boot_info->krnl_end - boot_info->krnl_start) / 1024); +// ke_printf("KERNEL: Base Addr is 0x%X. Size is %uB, %uKB.\n", +// boot_info->krnl_start, +// (boot_info->krnl_end - boot_info->krnl_start), +// (boot_info->krnl_end - boot_info->krnl_start) / 1024); ke_printf("KERNEL: CPU Vendor is \"%s\".\n", boot_info->cpu_vd_str); diff --git a/kernel/mm/Rules.mk b/kernel/mm/Rules.mk index fc660da..e9cedb3 100644 --- a/kernel/mm/Rules.mk +++ b/kernel/mm/Rules.mk @@ -1,15 +1,7 @@ include $(MK)/prologue.mk -SRC_$(d) := pmm.c +SRC_$(d) := $(d)/pmm.c -SRC_$(d) := $(addprefix $(d)/, $(SRC_$(d))) - -OBJ_$(d) := $(SRC_$(d):.c=.o) - -$(OBJ_$(d)): %.o: %.c - $(COMP) - -# append all OBJECTS to clean -OBJ := $(OBJ) $(OBJ_$(d)) +include $(MK)/stdrules.mk include $(MK)/epilogue.mk \ No newline at end of file diff --git a/kernel/mm/pmm.c b/kernel/mm/pmm.c index de285aa..9f7a6f3 100644 --- a/kernel/mm/pmm.c +++ b/kernel/mm/pmm.c @@ -1,6 +1,6 @@ #include "kernel/ke/assert.h" #include "kernel/ke/rwwlock.h" -#include "kernel/ke/status.h" +#include "status.h" #include "kernel/ke/alloc.h" #include "kernel/mm/pmm.h" @@ -8,7 +8,7 @@ typedef struct { linked_list_node_t free_list_node; avl_tree_node_t avl_tree_node; - physical_addr_t base; + uintptr_t base; int32_t attr; } physical_page_descriptor_t; @@ -26,10 +26,10 @@ static _Bool initialized; */ static int32_t mmp_base_paddr_compare(void *tree_node, void *my_node) { - physical_addr_t tree_base = OBTAIN_STRUCT_ADDR(tree_node, + uintptr_t tree_base = OBTAIN_STRUCT_ADDR(tree_node, physical_page_descriptor_t, avl_tree_node)->base; - physical_addr_t my_base = OBTAIN_STRUCT_ADDR(my_node, + uintptr_t my_base = OBTAIN_STRUCT_ADDR(my_node, physical_page_descriptor_t, avl_tree_node)->base; if (tree_base > my_base) @@ -44,7 +44,7 @@ status_t KABI sx_pmm_init(pmm_info_t *info) { if (info == NULL) { - return PMM_STATUS_INVALID_ARGUMENTS; + return MM_INVALID_ARGUMENTS; } if (initialized) @@ -72,7 +72,7 @@ status_t KABI sx_pmm_init(pmm_info_t *info) if (page_info == NULL) { - return PMM_STATUS_ALLOCATION_FAILED; + return MM_ALLOCATION_FAILED; } page_info->base = each_node->base; @@ -88,16 +88,16 @@ status_t KABI sx_pmm_init(pmm_info_t *info) // potential callers of these, since timer/interrupts queue DPC, which might trigger // page fault (kernel heap), therefore, it must set IRQL to DISABLED -status_t KABI mm_alloc_page(physical_addr_t *out) +status_t KABI mm_alloc_page(uintptr_t *out) { if (!initialized) { - return PMM_STATUS_UNINITIALIZED; + return MM_UNINITIALIZED; } if (out == NULL) { - return PMM_STATUS_INVALID_ARGUMENTS; + return MM_INVALID_ARGUMENTS; } irql_t irql = ke_rwwlock_writer_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL); @@ -114,24 +114,24 @@ status_t KABI mm_alloc_page(physical_addr_t *out) *out = page_info->base; } else { - result = PMM_STATUS_NOT_ENOUGH_PAGE; + result = MM_NOT_ENOUGH_PAGE; } ke_rwwlock_writer_unlock_lower_irql(&lock, irql); return result; } -status_t KABI mm_query_page_attr(physical_addr_t base, - int32_t *out) +status_t KABI mm_query_page_attr(uintptr_t base, + int32_t *out) { if (!initialized) { - return PMM_STATUS_UNINITIALIZED; + return MM_UNINITIALIZED; } if (out == NULL) { - return PMM_STATUS_INVALID_ARGUMENTS; + return MM_INVALID_ARGUMENTS; } irql_t irql = ke_rwwlock_reader_lock_raise_irql(&lock, IRQL_DISABLED_LEVEL); @@ -149,7 +149,7 @@ status_t KABI mm_query_page_attr(physical_addr_t base, *out = page_info->attr; } else { - result = PMM_STATUS_INVALID_ARGUMENTS; + result = MM_INVALID_ARGUMENTS; } ke_rwwlock_reader_unlock_lower_irql(&lock, irql); @@ -157,11 +157,11 @@ status_t KABI mm_query_page_attr(physical_addr_t base, return result; } -status_t KABI mm_free_page(physical_addr_t base) +status_t KABI mm_free_page(uintptr_t base) { if (!initialized) { - return PMM_STATUS_UNINITIALIZED; + return MM_UNINITIALIZED; } // just lock since not sharing with anyone @@ -179,7 +179,7 @@ status_t KABI mm_free_page(physical_addr_t base) lb_linked_list_push_back(&free_list, &page_info->free_list_node); } else { - result = PMM_STATUS_INVALID_ARGUMENTS; + result = MM_INVALID_ARGUMENTS; } ke_rwwlock_writer_unlock_lower_irql(&lock, irql); diff --git a/kernel/rf/Rules.mk b/kernel/rf/Rules.mk index 8280ad1..8462177 100644 --- a/kernel/rf/Rules.mk +++ b/kernel/rf/Rules.mk @@ -1,15 +1,7 @@ include $(MK)/prologue.mk -SRC_$(d) := ref.c +SRC_$(d) := $(d)/ref.c -SRC_$(d) := $(addprefix $(d)/, $(SRC_$(d))) - -OBJ_$(d) := $(SRC_$(d):.c=.o) - -$(OBJ_$(d)): %.o: %.c - $(COMP) - -# append all OBJECTS to clean -OBJ := $(OBJ) $(OBJ_$(d)) +include $(MK)/stdrules.mk include $(MK)/epilogue.mk \ No newline at end of file diff --git a/kernel/rf/ref.c b/kernel/rf/ref.c index 6e14b65..309e590 100644 --- a/kernel/rf/ref.c +++ b/kernel/rf/ref.c @@ -4,6 +4,7 @@ #include "kernel/ke/assert.h" #include "kernel/ke/atomic.h" #include "lib/avl_tree.h" +#include "status.h" typedef struct { @@ -69,7 +70,7 @@ status_t KABI rf_reference_create(ref_node_t *ref, ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL); if (ref == NULL || free_func == NULL) - return REF_STATUS_INVALID_ARGUMENTS; + return RF_INVALID_ARGUMENTS; ref->free_routine = free_func; ref->ref_count = 1; @@ -82,7 +83,7 @@ status_t KABI rf_reference_obj(ref_node_t *ref_node) ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL); if (ref_node == NULL) - return REF_STATUS_INVALID_ARGUMENTS; + return RF_INVALID_ARGUMENTS; int32_t old_ref_count = ke_interlocked_increment_32(&ref_node->ref_count, 1); @@ -96,7 +97,7 @@ status_t KABI rf_dereference_obj(ref_node_t *ref_node) ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL); if (ref_node == NULL) - return REF_STATUS_INVALID_ARGUMENTS; + return RF_INVALID_ARGUMENTS; status_t result = STATUS_SUCCESS; @@ -119,12 +120,12 @@ static status_t KABI rf_open_obj_by_handle(handle_t handle, ref_node_t **out) if (!initialized) { - return REF_STATUS_UNINITIALIZED; + return RF_UNINITIALIZED; } if (out == NULL) { - return REF_STATUS_INVALID_ARGUMENTS; + return RF_INVALID_ARGUMENTS; } irql_t irql; @@ -136,7 +137,7 @@ static status_t KABI rf_open_obj_by_handle(handle_t handle, ref_node_t **out) handle_node_t *handle_node = rfp_search_handle_node(handle); if (handle_node == NULL) { - status = REF_STATUS_INVALID_HANDLE; + status = RF_INVALID_HANDLE; } else { ref = handle_node->ref; @@ -144,7 +145,7 @@ static status_t KABI rf_open_obj_by_handle(handle_t handle, ref_node_t **out) // PREREQUISITE: Having a handle -> having a reference // MUST GUARANTEE that handle exists while we reference - if (SX_SUCCESS(status)) + if (sx_success(status)) { // reference the object then return the reference rf_reference_obj(ref); @@ -163,16 +164,16 @@ static status_t KABI rf_create_handle(ref_node_t *ref, ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL); if (!initialized) - return REF_STATUS_UNINITIALIZED; + return RF_UNINITIALIZED; if (ref == NULL || node == NULL || out == NULL) - return REF_STATUS_INVALID_ARGUMENTS; + return RF_INVALID_ARGUMENTS; status_t result = STATUS_SUCCESS; irql_t irql; - if (SX_SUCCESS(result)) + if (sx_success(result)) { // TODO: CHECK OVERFLOW node->handle = (handle_t) ke_interlocked_increment_32(&handle_base, 1); @@ -184,14 +185,14 @@ static status_t KABI rf_create_handle(ref_node_t *ref, lb_avl_tree_insert(&handle_tree, &node->tree_node); } else { - result = REF_STATUS_DUPLICATED_HANDLE; + result = RF_DUPLICATED_HANDLE; } ke_spin_unlock_lower_irql(&handle_tree_lock, irql); } - if (SX_SUCCESS(result)) + if (sx_success(result)) { rf_reference_obj(ref); *out = node->handle; @@ -208,7 +209,7 @@ static status_t KABI rf_close_handle(handle_t handle) ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL); if (!initialized) - return REF_STATUS_UNINITIALIZED; + return RF_UNINITIALIZED; irql_t irql; status_t status = STATUS_SUCCESS; @@ -219,7 +220,7 @@ static status_t KABI rf_close_handle(handle_t handle) handle_node_t *handle_node = rfp_search_handle_node(handle); if (handle_node == NULL) { - status = REF_STATUS_INVALID_HANDLE; + status = RF_INVALID_HANDLE; } else { ref = handle_node->ref; @@ -233,7 +234,7 @@ static status_t KABI rf_close_handle(handle_t handle) handle_node->free_routine(handle_node, NULL); } - if (SX_SUCCESS(status)) + if (sx_success(status)) { // dereference the object rf_dereference_obj(ref); @@ -252,13 +253,13 @@ status_t KABI sx_create_handle(ref_node_t *ref, handle_t *out) ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL); if (!initialized) - return REF_STATUS_UNINITIALIZED; + return RF_UNINITIALIZED; handle_node_t *node; node = (handle_node_t *) ke_alloc(sizeof(handle_node_t)); if (node == NULL) { - return REF_STATUS_ALLOCATION_FAILED; + return RF_ALLOCATION_FAILED; } node->free_routine = rfp_handle_node_free; @@ -271,7 +272,7 @@ status_t KABI sx_close_handle(handle_t handle) ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL); if (!initialized) - return REF_STATUS_UNINITIALIZED; + return RF_UNINITIALIZED; // need to keep sx version since need to do handle check here @@ -283,10 +284,10 @@ status_t KABI sx_open_obj_by_handle(handle_t handle, ref_node_t **out) ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL); if (!initialized) - return REF_STATUS_UNINITIALIZED; + return RF_UNINITIALIZED; if (out == NULL) - return REF_STATUS_INVALID_ARGUMENTS; + return RF_INVALID_ARGUMENTS; // check special handles first // if (handle == K_HANDLE_CURRENT_THREAD) diff --git a/lib/Rules.mk b/lib/Rules.mk index 8ec59d6..e7f7864 100644 --- a/lib/Rules.mk +++ b/lib/Rules.mk @@ -1,18 +1,10 @@ include $(MK)/prologue.mk -SRC_$(d) := avl_tree.c \ - linked_list.c \ - salloc.c \ - sxtdlib.c +SRC_$(d) := $(d)/avl_tree.c \ + $(d)/linked_list.c \ + $(d)/salloc.c \ + $(d)/sxtdlib.c -SRC_$(d) := $(addprefix $(d)/, $(SRC_$(d))) - -OBJ_$(d) := $(SRC_$(d):.c=.o) - -$(OBJ_$(d)): %.o: %.c - $(COMP) - -# append all OBJECTS to clean -OBJ := $(OBJ) $(OBJ_$(d)) +include $(MK)/stdrules.mk include $(MK)/epilogue.mk \ No newline at end of file diff --git a/mk/Rules.mk b/mk/Rules.mk new file mode 100644 index 0000000..4faa14a --- /dev/null +++ b/mk/Rules.mk @@ -0,0 +1,7 @@ +include $(MK)/prologue.mk + +SRCIN_$(d) := $(d)/linker.ld.in + +include $(MK)/stdrules.mk + +include $(MK)/epilogue.mk \ No newline at end of file diff --git a/mk/linker.ld b/mk/linker.ld deleted file mode 100644 index 6525682..0000000 --- a/mk/linker.ld +++ /dev/null @@ -1,44 +0,0 @@ -OUTPUT_FORMAT(elf64-x86-64) -OUTPUT_ARCH(i386:x86-64) -ENTRY(hal_entry_32) - -SECTIONS -{ - . = HAL_KERNEL_BASE_VADDR + HAL_KERNEL_BASE_PADDR; - - HAL_KERNEL_START_VADDR = .; - - .multiboot_header : AT(ADDR(.multiboot_header) - HAL_KERNEL_BASE_VADDR) - { - *(.multiboot_header) - } - - .text ALIGN(0x1000) : AT(ADDR(.text) - HAL_KERNEL_BASE_VADDR) - { - *(.text) - } - - .data ALIGN(0x1000) : AT(ADDR(.data) - HAL_KERNEL_BASE_VADDR) - { - *(.data) - *(.rodata*) - } - - .bss ALIGN(0x1000) : AT(ADDR(.bss) - HAL_KERNEL_BASE_VADDR) - { - *(.bss) - *(COMMON) - } - - HAL_KERNEL_END_VADDR = .; - - /DISCARD/ : - { - *(.gcc_except_table) - *(.eh_frame) - *(.note) - *(.comment) - *(.rel.*) - *(.rela.*) - } -} diff --git a/mk/linker.ld.in b/mk/linker.ld.in new file mode 100644 index 0000000..02fe9db --- /dev/null +++ b/mk/linker.ld.in @@ -0,0 +1,44 @@ +#include "kernel/hal/memdef.h" + +OUTPUT_FORMAT(elf64-x86-64) +OUTPUT_ARCH(i386:x86-64) +ENTRY(hal_entry_32) + +SECTIONS +{ + . = KERNEL_IMAGE_VADDR; + + .multiboot_header : AT(ADDR(.multiboot_header) + KERNEL_IMAGE_PADDR - KERNEL_IMAGE_VADDR) + { + *(.multiboot_header) + } + + .text ALIGN(0x1000) : AT(ADDR(.text) + KERNEL_IMAGE_PADDR - KERNEL_IMAGE_VADDR) + { + *(.text) + } + + .data ALIGN(0x1000) : AT(ADDR(.data) + KERNEL_IMAGE_PADDR - KERNEL_IMAGE_VADDR) + { + *(.data) + *(.rodata*) + } + + .bss ALIGN(0x1000) : AT(ADDR(.bss) + KERNEL_IMAGE_PADDR - KERNEL_IMAGE_VADDR) + { + *(.bss) + *(COMMON) + } + + KERNEL_IMAGE_END_VADDR = .; + + /DISCARD/ : + { + *(.gcc_except_table) + *(.eh_frame) + *(.note) + *(.comment) + *(.rel.*) + *(.rela.*) + } +} diff --git a/mk/stdrules.mk b/mk/stdrules.mk new file mode 100644 index 0000000..7ccea1f --- /dev/null +++ b/mk/stdrules.mk @@ -0,0 +1,29 @@ +# The source rules.mk defines: +# SRC_$(d) for all c source files +# SRCAS_$(d) for all asm source files +# SRCIN_$(d) for all in(preprocessor) source files +# Compiles all c and in source files and generate dependencies +# Adds c and asm object files to $OBJ variable +# Adds all generated files to $CLEAN variable + +OBJ_$(d) := $(SRC_$(d):.c=.o) +OBJAS_$(d) := $(SRCAS_$(d):.asm=.a) +OBJIN_$(d) := $(SRCIN_$(d):.in=) +DEP_$(d) := $(SRC_$(d):.c=.d) $(SRCIN_$(d):.in=.d) + +$(OBJ_$(d)): %.o: %.c + $(COMP) + $(GDEP) + +$(OBJAS_$(d)): %.a: %.asm + $(COMPAS) + +$(OBJIN_$(d)): %: %.in + $(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)) \ No newline at end of file diff --git a/test/Rules.mk b/test/Rules.mk index b9fd3fc..d2dac8a 100644 --- a/test/Rules.mk +++ b/test/Rules.mk @@ -1,18 +1,10 @@ include $(MK)/prologue.mk -SRC_$(d) := avl_tree_test.c \ - driver.c \ - linked_list_test.c \ - salloc_test.c +SRC_$(d) := $(d)/avl_tree_test.c \ + $(d)/driver.c \ + $(d)/linked_list_test.c \ + $(d)/salloc_test.c -SRC_$(d) := $(addprefix $(d)/, $(SRC_$(d))) - -OBJ_$(d) := $(SRC_$(d):.c=.o) - -$(OBJ_$(d)): %.o: %.c - $(COMP) - -# append all OBJECTS to clean -OBJ := $(OBJ) $(OBJ_$(d)) +include $(MK)/stdrules.mk include $(MK)/epilogue.mk \ No newline at end of file