Finally compiles after refactoring..

This commit is contained in:
secXsQuared 2018-10-02 02:43:30 -04:00
parent 48388bbf01
commit 73458d7f60
86 changed files with 1094 additions and 1182 deletions

View File

@ -3,15 +3,19 @@ project(secX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11") 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 hal_src ./inc/*.h ./hal/*.c ./hal/*.h ./common/*.c)
file(GLOB_RECURSE kernel_src ./kernel/*.h ./kernel/*.c ./common/*.h ./common/*.c) file(GLOB_RECURSE kernel_src ./inc/*.h ./kernel/*.c ./kernel/*.h ./common/*.c)
file(GLOB_RECURSE test_src ./test/*.h ./test/*.c ./common/*.h ./common/*.c) file(GLOB_RECURSE test_src ./inc/*.h ./test/*.c ./test/*.h ./common/*.c)
include_directories(inc) include_directories(inc)
#KERNEL + HAL # HAL
#include_directories(hal/inc)
#add_executable(hal ${hal_src})
# KERNEL
include_directories(kernel/inc) include_directories(kernel/inc)
include_directories(hal/inc) add_executable(kernel ${kernel_src})
add_executable(kernel ${kernel_src} ${hal_src})
# KERNEL + TESTS # KERNEL + TESTS
#include_directories(test/inc) #include_directories(test/inc)

View File

@ -10,6 +10,8 @@ OUT := out
C_IGNORED_WARNINGS = -Wno-cast-align \ C_IGNORED_WARNINGS = -Wno-cast-align \
-Wno-padded -Wno-padded
# generic freestanding cflags used for target
# each submodule can append to this flag
C_FLAGS = -xc\ C_FLAGS = -xc\
-g \ -g \
-c \ -c \
@ -31,32 +33,28 @@ C_FLAGS = -xc\
-mno-sse3 \ -mno-sse3 \
-mno-3dnow \ -mno-3dnow \
-target x86_64-pc-none-elf \ -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 \ AS_FLAGS = -w+all \
-w+error \ -w+error \
-f elf64 \ -f elf64 \
-F dwarf \ -F dwarf \
-g \ -g \
-I$(INC_COMMON) \ -I$(INC_COMMON) \
$(INC_$(d)) $(AS_FLAGS_$(MOD))
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
# generic pre-processing flags used for target
PREP_FLAGS = -E \ PREP_FLAGS = -E \
-xc\ -xc\
-P \ -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) \ GDEP_FLAGS = $(PREP_FLAGS) \
-MMD \ -MMD \
-MT $@ -MT $@
@ -64,8 +62,6 @@ GDEP_FLAGS = $(PREP_FLAGS) \
MKDIR = mkdir -p $(dir $@) MKDIR = mkdir -p $(dir $@)
COMP = $(CC) $(C_FLAGS) -o $@ $< COMP = $(CC) $(C_FLAGS) -o $@ $<
COMPAS = $(AS) $(AS_FLAGS) -o $@ $< COMPAS = $(AS) $(AS_FLAGS) -o $@ $<
LINK = $(LD) $(LD_FLAGS) -o $@ $^
DUMP = $(DAS) $(DUMP_FLAGS) $< > $@
PREP = $(CC) $(PREP_FLAGS) $< > $@ PREP = $(CC) $(PREP_FLAGS) $< > $@
GDEP = $(CC) $(GDEP_FLAGS) -MF $(addsuffix .d, $@) $< > /dev/null GDEP = $(CC) $(GDEP_FLAGS) -MF $(addsuffix .d, $@) $< > /dev/null

View File

@ -3,16 +3,24 @@
All platforms where the required packages are available. All platforms where the required packages are available.
### Required packages ### 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 ### 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. 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.

View File

@ -2,7 +2,7 @@ include $(MK)/prologue.mk
.DEFAULT_GOAL := all .DEFAULT_GOAL := all
#OBJ var holds all object files # OBJ var holds all OBJS required to link the kernel
dir := hal dir := hal
include $(dir)/Rules.mk include $(dir)/Rules.mk
@ -13,20 +13,43 @@ include $(dir)/Rules.mk
dir := common dir := common
include $(dir)/Rules.mk include $(dir)/Rules.mk
LD_SCRIPT := $(OUT)/$(MK)/linker.ld LD_SCRIPT := $(OUT)/$(MK)/linker.ld
GRUB_CFG = $(MK)/grub.cfg
TGT := $(OUT)/secxkrnl.elf TGT := $(OUT)/secxkrnl.elf
DMP := $(OUT)/secxkrnl.dmp 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) $(TGT): $(OBJ) $(LD_SCRIPT)
$(LINK) $(LD) $(LD_FLAGS) -o $@ $^
$(DMP): $(TGT) $(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 .PHONY: clean
clean: clean:
rm -f $(CLEAN) $(TGT) $(DMP) $(ISO) rm -rf $(OUT)
find $(OUT) -empty -type d -delete
.PHONY: compile .PHONY: compile
compile: $(TGT) compile: $(TGT)
@ -35,6 +58,6 @@ compile: $(TGT)
dump: $(DMP) dump: $(DMP)
.PHONY: all .PHONY: all
all: compile dump all: compile dump iso
include $(MK)/epilogue.mk include $(MK)/epilogue.mk

View File

@ -1,6 +1,8 @@
include $(MK)/prologue.mk include $(MK)/prologue.mk
SRC_$(d) := $(d)/common.c MOD:=COMMON
SRC_$(d) := $(d)/clib.c
include $(MK)/stdrules.mk include $(MK)/stdrules.mk

View File

@ -1,5 +1,6 @@
#include "common.h" #include "cdef.h"
#include "clib.h"
void void
mem_cpy(void *src, void *dst, uint64 size) mem_cpy(void *src, void *dst, uint64 size)

View File

@ -1,14 +1,34 @@
include $(MK)/prologue.mk 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 \ SRC_$(d) := $(d)/boot.c \
$(d)/intr.c \ $(d)/intr.c \
$(d)/mem.c \ $(d)/mem.c \
$(d)/print.c $(d)/print.c \
$(d)/hal.c
SRCAS_$(d) := $(d)/cpu.asm \ 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 this at last
include $(MK)/stdrules.mk include $(MK)/stdrules.mk

28
hal/atomic.asm Normal file
View File

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

View File

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

View File

@ -1,57 +1,16 @@
#define ASM_FILE #define ASM_FILE
#include "mlayout.h"
#include "multiboot2.h" #include "multiboot2.h"
%define GET_PADDR(x) ((x) - KERNEL_IMAGE_VADDR + KERNEL_IMAGE_PADDR) %define GET_PADDR(x) ((x) - KERNEL_IMAGE_VADDR + KERNEL_IMAGE_PADDR)
%define BOCHS_BREAK xchg bx,bx %define BOCHS_BREAK xchg bx,bx
extern hal_main global sys_entry
global hal_main_32 extern hmain
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)
section .text section .text
bits 32 bits 32
align KERNEL_PAGE_SIZE sys_entry:
hal_main_32:
cli cli
cld cld
cmp eax,MULTIBOOT2_BOOTLOADER_MAGIC cmp eax,MULTIBOOT2_BOOTLOADER_MAGIC
@ -119,7 +78,7 @@ hal_main_32:
; enter long mode ; enter long mode
lgdt [GET_PADDR(_gdt.ptr)] lgdt [GET_PADDR(_gdt.ptr)]
jmp _gdt.code:GET_PADDR(halp_entry_64) jmp _gdt.code:GET_PADDR(hmain_stub)
hlt hlt
halp_check_long_mode: halp_check_long_mode:
@ -144,7 +103,7 @@ halp_check_long_mode:
mov eax, 0x80000001 ; Set the A-register to 0x80000001. mov eax, 0x80000001 ; Set the A-register to 0x80000001.
cpuid ; CPU identification. cpuid ; CPU identification.
test edx, 1 << 29 ; Test if the LM-bit, which is bit 29, is set in the D-register. 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 mov eax,1
jmp .end jmp .end
.not_supported: .not_supported:
@ -156,7 +115,7 @@ halp_check_long_mode:
section .text section .text
bits 64 bits 64
halp_entry_64: hmain_stub:
; note that we are still at the identity mapping ; note that we are still at the identity mapping
mov ax,_gdt.data mov ax,_gdt.data
mov ds,ax mov ds,ax
@ -167,10 +126,7 @@ halp_entry_64:
mov rsp, GET_PADDR(_stack) mov rsp, GET_PADDR(_stack)
mov rdi, rsi ; multiboot_info* mov rdi, rsi ; multiboot_info*
call hal_write_initial_page_table call hmain
test rax,rax
jne .end
call hal_main
.end: .end:
hlt hlt
@ -211,4 +167,4 @@ _gdt: ; Global Descriptor Table (long mode).
.ptr: .ptr:
; GDT PTR ; GDT PTR
dw $ - _gdt - 1 ; Limit. dw $ - _gdt - 1 ; Limit.
dq GET_PADDR(_gdt) ; Base. dq GET_PADDR(_gdt) ; Base.

View File

@ -1,9 +1,9 @@
#include "print.h"
#include "mem.h" #include "mem.h"
#include "print.h"
#include "intr.h" #include "intr.h"
#include "cpu.h" #include "cpu.h"
#include "call.h" #include "kernel.h"
#include "hal_export.h" #include "hal.h"
//static void //static void
//halp_obtain_cpu_info(struct boot_info *hal_info) //halp_obtain_cpu_info(struct boot_info *hal_info)
@ -21,10 +21,10 @@
//} //}
void HABI void HABI
hal_main(void *m_info); hmain(void *m_info);
void HABI void HABI
hal_main(void *m_info) hmain(void *m_info)
{ {
if (m_info == NULL || (uint64) m_info & bit_field_mask(0, 2)) if (m_info == NULL || (uint64) m_info & bit_field_mask(0, 2))
{ {
@ -47,5 +47,5 @@ hal_main(void *m_info)
hal_halt_cpu(); hal_halt_cpu();
} }
ke_main(boot_info); kmain(boot_info);
} }

View File

@ -2,10 +2,23 @@
;rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11 are scratch registers. ;rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11 are scratch registers.
;function parameter: rdi,rsi,rdx,rcx,r8,r9 ;function parameter: rdi,rsi,rdx,rcx,r8,r9
[SECTION .text]
[BITS 64]
;======================
global hal_flush_gdt 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: hal_flush_gdt:
push rbp push rbp
mov rbp,rsp mov rbp,rsp
@ -31,16 +44,13 @@ mov ds,dx
pop rbp pop rbp
ret ret
;======================
global hal_flush_tlb
;void flush_tlb(void)
hal_flush_tlb: hal_flush_tlb:
mov rax,cr3 mov rax,cr3
mov cr3,rax mov cr3,rax
ret ret
;======================
global hal_flush_idt
hal_flush_idt: hal_flush_idt:
lidt [rdi] lidt [rdi]
ret ret
@ -75,32 +85,6 @@ hal_write_cr8:
mov cr8,rdi mov cr8,rdi
ret 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); ; extern void KAPI hal_cpuid(uint32* eax, uint32* ebx, uint32* ecx, uint32* edx);
global hal_cpuid global hal_cpuid
@ -127,99 +111,6 @@ mov rsp,rbp
pop rbp pop rbp
ret 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 global hal_halt_cpu
hal_halt_cpu: hal_halt_cpu:

71
hal/hal.c Normal file
View File

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

View File

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

13
hal/inc/atomic.h Normal file
View File

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

View File

@ -1,10 +1,9 @@
#pragma once #pragma once
#include "common.h" #include "hdef.h"
#define HAL_CORE_COUNT 1 #define HAL_CORE_COUNT 1
struct STRUCT_PACKED hal_gdt_ptr struct STRUCT_PACKED hal_gdt_ptr
{ {
uint16 limit; uint16 limit;
@ -17,40 +16,14 @@ struct STRUCT_PACKED hal_idt_ptr
uint64 base; uint64 base;
}; };
/** /**
* CPU Instructions * ASM declaration
*/ */
void HABI hal_cpuid(uint32 *eax, uint32 *ebx, uint32 *ecx, uint32 *edx); void HABI hal_cpuid(uint32 *eax, uint32 *ebx, uint32 *ecx, uint32 *edx);
void HABI hal_halt_cpu(void); 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_gdt(struct hal_gdt_ptr *gdt_ptr, uint64 code_slct, uint64 data_slct);
void HABI hal_flush_tlb(void); 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); void HABI hal_read_idt(struct hal_idt_ptr **idt_ptr);
/**
* Control Register Operations
*/
#define MSR_IA32_APIC_BASE 0x1B #define MSR_IA32_APIC_BASE 0x1B
void HABI hal_read_msr(uint32 *ecx, uint32 *edx, uint32 *eax); void HABI hal_read_msr(uint32 *ecx, uint32 *edx, uint32 *eax);

5
hal/inc/hdef.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include "cdef.h"
#define HABI KABI

View File

@ -1,20 +1,22 @@
#pragma once #pragma once
#include "common.h" #include "hdef.h"
#include "intr.h" #include "intr.h"
#include "call.h" #include "kernel.h"
#include "clib.h"
#include "hal.h"
/** /**
* Interrupt context structure * Interrupt context structure
*/ */
typedef struct struct interrupt_context
{ {
const uint64 rip; const uint64 rip;
const uint64 cs; const uint64 cs;
const uint64 rflags; const uint64 rflags;
const uint64 rsp; const uint64 rsp;
const uint64 ss; const uint64 ss;
} hal_interrupt_context_t; };
/** /**
* IDT Defns * IDT Defns
@ -32,10 +34,8 @@ typedef struct
#define IDT_ENTRY_SIZE 16 #define IDT_ENTRY_SIZE 16
/** /**
* intr.h * C declaration
*/ */
int32 int32
hal_interrupt_init(void); hal_interrupt_init(void);
@ -45,16 +45,40 @@ hal_write_gate(void * gate, uint64 offset, uint32 selector, uint32 attr);
void void
hal_set_interrupt_handler(uint64 index, void (*handler)(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 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 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 * System exception Handlers

19
hal/inc/io.h Normal file
View File

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

View File

@ -1,8 +1,7 @@
#pragma once #pragma once
#include "common.h" #include "cdef.h"
#include "mem.h" #include "mem.h"
#include "kernel/status.h"
/** /**
Global Descriptors Table Definitions Global Descriptors Table Definitions
@ -93,7 +92,7 @@ hal_write_pd(void *base, uintptr pt_addr, uint64 attr);
void void
hal_write_pt(void *base, uintptr p_addr, uint64 attr); hal_write_pt(void *base, uintptr p_addr, uint64 attr);
k_status uint32
hal_write_initial_page_table(void *multiboot_info); hal_write_initial_page_table(void *multiboot_info);
/** /**

View File

@ -21,22 +21,6 @@
#pragma once #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. */ /* How many bytes from the start of the file we search for the header. */
#define MULTIBOOT_SEARCH 32768 #define MULTIBOOT_SEARCH 32768
#define MULTIBOOT_HEADER_ALIGN 8 #define MULTIBOOT_HEADER_ALIGN 8

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "common.h" #include "cdef.h"
#include "print.h" #include "print.h"
void void

View File

@ -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 %macro PUSHAQ 0
push rax ;save current rax push rax ;save current rax
push rbx ;save current rbx push rbx ;save current rbx

View File

@ -1,30 +1,30 @@
#include "common.h"
#include "intr.h"
#include "cpu.h" #include "cpu.h"
#include "hdef.h"
#include "hal.h"
#include "intr.h" #include "intr.h"
#include "print.h"
#include "mem.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 uint8 cpu_idts[HAL_CORE_COUNT][IDT_ENTRY_NUM * IDT_ENTRY_SIZE];
static struct hal_idt_ptr _idt_ptrs[HAL_CORE_COUNT]; static struct hal_idt_ptr cpu_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 k_exc_dispatcher k_exc_disps[HAL_CORE_COUNT];
static exc_handler_fp _exc_handler_table[HAL_CORE_COUNT][IDT_ENTRY_NUM]; static k_intr_dispatcher k_intr_disps[HAL_CORE_COUNT];
uint32 uint32
hal_set_irql(uint32 irql) impl_hal_set_irql(uint32 irql)
{ {
UNREFERENCED(irql) UNREFERENCED(irql)
hal_assert(FALSE, "Unimplemented function called."); hal_halt_cpu();
return 0; return 0;
} }
uint32 uint32
hal_get_irql(void) impl_hal_get_irql(void)
{ {
hal_assert(FALSE, "Unimplemented function called."); hal_halt_cpu();
return 0; return 0;
} }
@ -55,92 +55,57 @@ hal_set_interrupt_handler(uint64 index, void (*handler)(void))
{ {
if (index < IDT_ENTRY_NUM) 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); GATE_DPL_0 | GATE_PRESENT | GATE_TYPE_INTERRUPT);
} }
} }
void KABI void
hal_issue_intr(uint32 target_core, uint32 vector) impl_hal_issue_intr(uint32 target_core, uint32 vector)
{ {
UNREFERENCED(target_core); UNREFERENCED(target_core);
UNREFERENCED(vector); 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(); 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 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(); 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); hal_printf("Unhandled interrupt %d at 0x%X.\n", int_vec, context->rip);
} }
else else
{ {
_intr_handler_table[coreid][int_vec](context->rip, context->rsp, 0); k_intr_disps[coreid]((uint32) int_vec, context);
} }
} }
void HABI 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(); 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); hal_printf("Unhandled exception %d at 0x%X.\n", exc_vec, context->rip);
} }
else 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 uint32
hal_get_core_id(void) impl_hal_get_core_id(void)
{ {
// TODO // TODO
return 0; return 0;
@ -426,21 +391,17 @@ hal_interrupt_init(void)
} }
// get idt ptr ready // get idt ptr ready
_idt_ptrs[coreid].base = (uint64) &_idts[coreid]; cpu_idt_ptrs[coreid].base = (uint64) &cpu_idts[coreid];
_idt_ptrs[coreid].limit = IDT_ENTRY_NUM * IDT_ENTRY_SIZE - 1; cpu_idt_ptrs[coreid].limit = IDT_ENTRY_NUM * IDT_ENTRY_SIZE - 1;
// clear dispatch table // clear dispatch table
for (uint64 i = 0; i < IDT_ENTRY_NUM; i++) k_exc_disps[coreid] = NULL;
{ k_intr_disps[coreid] = NULL;
_intr_handler_table[coreid][i] = NULL;
_exc_handler_table[coreid][i] = NULL;
_intr_handler_context_table[coreid][i] = NULL;
}
// hook asm interrupt handlers // hook asm interrupt handlers
halp_populate_idt(); halp_populate_idt();
hal_flush_idt(&_idt_ptrs[coreid]); hal_flush_idt(&cpu_idt_ptrs[coreid]);
// disable PIC // disable PIC
hal_write_port_8(0xa1, 0xff); hal_write_port_8(0xa1, 0xff);

67
hal/io.asm Normal file
View File

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

47
hal/mb_hdr.asm.in Normal file
View File

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

View File

@ -1,9 +1,9 @@
#include "common.h" #include "cdef.h"
#include "cpu.h" #include "cpu.h"
#include "mem.h" #include "mem.h"
#include "intr.h" #include "intr.h"
#include "hal_export.h" #include "hal.h"
static uint8 _gdts[HAL_CORE_COUNT][GDT_ENTRY_NUM * GDT_ENTRY_SIZE]; static uint8 _gdts[HAL_CORE_COUNT][GDT_ENTRY_NUM * GDT_ENTRY_SIZE];
static struct hal_gdt_ptr _gdt_ptrs[HAL_CORE_COUNT]; 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 uint32 hal_heap_used;
static char hal_heap[HAL_HEAP_SIZE]; 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); UNREFERENCED(multiboot_info);

View File

@ -1,6 +1,7 @@
#include "common.h" #include "cdef.h"
#include "cpu.h" #include "cpu.h"
#include "print.h" #include "print.h"
#include "clib.h"
// #define get_column(pos) ((pos) % 80) // #define get_column(pos) ((pos) % 80)
#define get_row(pos) ((pos) / 80) #define get_row(pos) ((pos) / 80)

27
inc/cdef.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
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))

54
inc/clib.h Normal file
View File

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

38
inc/hal.h Normal file
View File

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

View File

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

34
inc/kernel.h Normal file
View File

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

View File

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

View File

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

View File

@ -29,83 +29,3 @@
#define KERNEL_DYNAMIC_SIZE (0x0000007F80000000) #define KERNEL_DYNAMIC_SIZE (0x0000007F80000000)
#define KERNEL_IMAGE_VADDR (0xFFFFFFFF80000000) #define KERNEL_IMAGE_VADDR (0xFFFFFFFF80000000)
#define KERNEL_IMAGE_SIZE (0x0000000080000000) #define KERNEL_IMAGE_SIZE (0x0000000080000000)
#ifndef ASM_FILE
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
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

View File

@ -1,5 +1,9 @@
include $(MK)/prologue.mk include $(MK)/prologue.mk
MOD:=KERNEL
C_FLAGS_$(MOD):=$(addprefix -I, $(d)/inc)
AS_FLAGS_$(MOD):=$(addprefix -I, $(d)/inc)
dir := $(d)/ke dir := $(d)/ke
include $(dir)/Rules.mk include $(dir)/Rules.mk
dir := $(d)/mm dir := $(d)/mm

12
kernel/inc/ke/alloc.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include "cdef.h"
void
ke_alloc_init(void);
void *
ke_alloc(uint32 size);
void
ke_free(void *ptr);

8
kernel/inc/ke/assert.h Normal file
View File

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

12
kernel/inc/ke/atomic.h Normal file
View File

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

48
kernel/inc/ke/intr.h Normal file
View File

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

6
kernel/inc/ke/panic.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include "cdef.h"
void
ke_panic(uint32 reason);

9
kernel/inc/ke/print.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "cdef.h"
void
ke_printf(const char *str, ...);
void
ke_vprintf(const char *str, va_list args);

29
kernel/inc/ke/rww_lock.h Normal file
View File

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

17
kernel/inc/ke/spin_lock.h Normal file
View File

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

65
kernel/inc/lb/atree.h Normal file
View File

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

69
kernel/inc/lb/llist.h Normal file
View File

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

16
kernel/inc/lb/salloc.h Normal file
View File

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

View File

@ -1,9 +1,9 @@
#pragma once #pragma once
#include "common.h" #include "cdef.h"
#include "kernel/status.h" #include "status.h"
#include "kernel/lb.h" #include "kernel.h"
#include "kernel/ke.h" #include "mlayout.h"
/** /**
* physical page allocation * physical page allocation

View File

@ -1,8 +1,8 @@
#pragma once #pragma once
#include "common.h" #include "cdef.h"
#include "kernel/status.h" #include "status.h"
#include "kernel/lb.h" #include "lb/atree.h"
typedef uint32 k_ident; typedef uint32 k_ident;

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "common.h" #include "cdef.h"
typedef uint32 k_status; typedef uint32 k_status;

View File

@ -3,10 +3,10 @@ include $(MK)/prologue.mk
SRC_$(d) := $(d)/alloc.c \ SRC_$(d) := $(d)/alloc.c \
$(d)/assert.c \ $(d)/assert.c \
$(d)/atomic.c \ $(d)/atomic.c \
$(d)/bug_check.c \ $(d)/panic.c \
$(d)/intr.c \ $(d)/intr.c \
$(d)/print.c \ $(d)/print.c \
$(d)/rwwlock.c \ $(d)/rww_lock.c \
$(d)/spin_lock.c \ $(d)/spin_lock.c \
$(d)/main.c $(d)/main.c

View File

@ -1,4 +1,5 @@
#include "kp.h" #include "ke/alloc.h"
#include "lb/salloc.h"
#define K_KERNEL_HEAP_SIZE 8192 #define K_KERNEL_HEAP_SIZE 8192
@ -16,15 +17,13 @@ ke_alloc_init(void)
} }
void * void *
ke_alloc( ke_alloc(uint32 size)
uint32 size)
{ {
return alloc_initialized ? lb_salloc(alloc_heap, size) : NULL; return alloc_initialized ? lb_salloc(alloc_heap, size) : NULL;
} }
void void
ke_free( ke_free(void *ptr)
void *ptr)
{ {
if (alloc_initialized) if (alloc_initialized)
{ {

View File

@ -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) 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); ke_printf("Assertion \"%s\" failed at %s:%d.\n", expr_str, file, line);
} }
} }

View File

@ -1,4 +1,5 @@
#include "kp.h" #include "ke/atomic.h"
#include "hal.h"
int32 ke_atomic_xchg_32(int32 *target, int32 val) int32 ke_atomic_xchg_32(int32 *target, int32 val)
{ {

View File

@ -1,7 +0,0 @@
#include "kp.h"
void ke_panic(uint64 reason)
{
ke_printf("BugCheck: Reason - %ul\n", reason);
hal_halt();
}

View File

@ -1,23 +1,12 @@
#include "common.h" #include "cdef.h"
#include "kernel/status.h" #include "status.h"
#include "kp.h" #include "intrp.h"
#include "ke/assert.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;
}
uint32 uint32
ke_raise_irql(uint32 irql) ke_raise_irql(uint32 irql)
{ {
ke_assert(ke_get_irql() <= irql); KE_ASSERT(ke_get_irql() <= irql);
return hal_set_irql(irql); return hal_set_irql(irql);
} }
@ -26,7 +15,7 @@ uint32
ke_lower_irql(uint32 irql) ke_lower_irql(uint32 irql)
{ {
uint32 old_irql = ke_get_irql(); uint32 old_irql = ke_get_irql();
ke_assert(old_irql >= irql); KE_ASSERT(old_irql >= irql);
return hal_set_irql(irql); return hal_set_irql(irql);
} }
@ -46,30 +35,36 @@ ke_issue_intr(uint32 core, uint32 vector)
void 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 void
ke_dereg_intr(uint32 index) ke_dereg_intr(uint32 vec)
{ {
hal_dereg_intr(index); // TODO: implement kernel dispatch table
UNREFERENCED(vec);
} }
void 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 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(); 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;
}

9
kernel/ke/intrp.h Normal file
View File

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

View File

@ -1,9 +0,0 @@
#pragma once
#include "kernel/ke.h"
/**
* interrupts
*/
k_status
kp_intr_init(struct boot_info *boot_info);

View File

@ -1,14 +1,17 @@
#include "kp.h" #include "kernel.h"
#include "kernel/mm.h" #include "cdef.h"
#include "intrp.h"
#include "mm/pmm.h"
#include "ke/panic.h"
/** /**
* Kernel entry point * Kernel entry point
* @param boot_info passed by the bootloader * @param boot_info passed by the bootloader
*/ */
void KABI 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 // initialize interrupts
status = kp_intr_init(boot_info); status = kp_intr_init(boot_info);

9
kernel/ke/panic.c Normal file
View File

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

View File

@ -1,4 +1,5 @@
#include "kp.h" #include "ke/print.h"
#include "ke/assert.h"
void void
ke_printf(const char *str, ...) ke_printf(const char *str, ...)
@ -13,7 +14,7 @@ void
ke_vprintf(const char *str, va_list args) ke_vprintf(const char *str, va_list args)
{ {
//TODO: implement //TODO: implement
ke_assert(0); KE_ASSERT(0);
UNREFERENCED(str); UNREFERENCED(str);
UNREFERENCED(args); UNREFERENCED(args);
} }

View File

@ -1,7 +1,7 @@
#include "kp.h" #include "ke/rww_lock.h"
void void
ke_rww_init(struct rwwlock *lock) ke_rww_init(struct rww_lock *lock)
{ {
if (lock != NULL) if (lock != NULL)
{ {
@ -15,7 +15,7 @@ ke_rww_init(struct rwwlock *lock)
} }
void void
ke_rww_r_lock(struct rwwlock *lock) ke_rww_r_lock(struct rww_lock *lock)
{ {
if (lock != NULL) if (lock != NULL)
{ {
@ -32,7 +32,7 @@ ke_rww_r_lock(struct rwwlock *lock)
} }
void void
ke_rww_r_unlock(struct rwwlock *lock) ke_rww_r_unlock(struct rww_lock *lock)
{ {
if (lock != NULL) if (lock != NULL)
{ {
@ -47,7 +47,7 @@ ke_rww_r_unlock(struct rwwlock *lock)
} }
void void
ke_rww_w_lock(struct rwwlock *lock) ke_rww_w_lock(struct rww_lock *lock)
{ {
ke_spin_lock(&lock->w_mutex); ke_spin_lock(&lock->w_mutex);
lock->writer_ct++; lock->writer_ct++;
@ -60,7 +60,7 @@ ke_rww_w_lock(struct rwwlock *lock)
} }
void void
ke_rww_w_unlock(struct rwwlock *lock) ke_rww_w_unlock(struct rww_lock *lock)
{ {
ke_spin_unlock(&lock->res_lock); ke_spin_unlock(&lock->res_lock);
ke_spin_lock(&lock->w_mutex); ke_spin_lock(&lock->w_mutex);

View File

@ -1,4 +1,5 @@
#include "kp.h" #include "ke/spin_lock.h"
#include "ke/atomic.h"
void void
ke_spin_init(struct spin_lock *lock) ke_spin_init(struct spin_lock *lock)

View File

@ -1,9 +1,8 @@
#include "lp.h" #include "lb/atree.h"
#include "clib.h"
static static struct atree_node *
struct atree_node * atree_node_max(struct atree_node *node)
atree_node_max(
struct atree_node *node)
{ {
while ((node != NULL) && (node->right != NULL)) while ((node != NULL) && (node->right != NULL))
{ {
@ -13,10 +12,8 @@ atree_node_max(
} }
static static struct atree_node *
struct atree_node * atree_node_min(struct atree_node *node)
atree_node_min(
struct atree_node *node)
{ {
while ((node != NULL) && (node->left != NULL)) while ((node != NULL) && (node->left != NULL))
{ {
@ -26,10 +23,8 @@ atree_node_min(
} }
static static void
void atree_node_init(struct atree_node *it)
atree_node_init(
struct atree_node *it)
{ {
if (it != NULL) if (it != NULL)
{ {
@ -40,19 +35,15 @@ atree_node_init(
} }
static static int32
int32 atree_node_get_height(struct atree_node *node)
atree_node_get_height(
struct atree_node *node)
{ {
return node == NULL ? -1 : node->height; return node == NULL ? -1 : node->height;
} }
static static int32
int32 atree_node_get_balance_factor(struct atree_node *node)
atree_node_get_balance_factor(
struct atree_node *node)
{ {
if (node == NULL) if (node == NULL)
{ {
@ -62,10 +53,8 @@ atree_node_get_balance_factor(
} }
static static struct atree_node *
struct atree_node * atree_node_right_rotate(struct atree_node *node)
atree_node_right_rotate(
struct atree_node *node)
{ {
struct atree_node *lchild = node->left; struct atree_node *lchild = node->left;
node->left = lchild->right; node->left = lchild->right;
@ -77,10 +66,8 @@ atree_node_right_rotate(
} }
static static struct atree_node *
struct atree_node * atree_node_left_rotate(struct atree_node *node)
atree_node_left_rotate(
struct atree_node *node)
{ {
struct atree_node *rchild = node->right; struct atree_node *rchild = node->right;
node->right = rchild->left; 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 * struct atree_node *
lb_atree_min( lb_atree_min(struct atree *tree)
struct atree *tree)
{ {
return atree_node_min(tree->root); return atree_node_min(tree->root);
} }
struct atree_node * struct atree_node *
lb_atree_max( lb_atree_max(struct atree *tree)
struct atree *tree)
{ {
return atree_node_max(tree->root); return atree_node_max(tree->root);
} }
struct atree_node * struct atree_node *
lb_atree_next( lb_atree_next(struct atree *tree, struct atree_node *entry)
struct atree *tree,
struct atree_node *entry)
{ {
struct atree_node *succ; struct atree_node *succ;
struct atree_node *node; struct atree_node *node;
@ -407,9 +390,7 @@ lb_atree_next(
struct atree_node * struct atree_node *
lb_atree_prev( lb_atree_prev(struct atree *tree, struct atree_node *entry)
struct atree *tree,
struct atree_node *entry)
{ {
struct atree_node *prev; struct atree_node *prev;
struct atree_node *node; struct atree_node *node;
@ -449,18 +430,14 @@ lb_atree_prev(
struct atree_node * struct atree_node *
lb_atree_search( lb_atree_search(struct atree *tree, struct atree_node *entry)
struct atree *tree,
struct atree_node *entry)
{ {
return atree_node_search(tree->root, entry, tree->cmpf, NULL); return atree_node_search(tree->root, entry, tree->cmpf, NULL);
} }
struct atree_node * struct atree_node *
lb_atree_insert( lb_atree_insert(struct atree *tree, struct atree_node *entry)
struct atree *tree,
struct atree_node *entry)
{ {
struct atree_node *old; struct atree_node *old;
@ -471,9 +448,7 @@ lb_atree_insert(
struct atree_node * struct atree_node *
lb_atree_delete( lb_atree_delete(struct atree *tree, struct atree_node *entry)
struct atree *tree,
struct atree_node *entry)
{ {
struct atree_node *node; struct atree_node *node;
@ -484,8 +459,7 @@ lb_atree_delete(
uint32 uint32
lb_atree_size( lb_atree_size(struct atree *tree)
struct atree *tree)
{ {
uint32 size; uint32 size;
struct atree_node *node; struct atree_node *node;
@ -505,9 +479,7 @@ lb_atree_size(
void void
lb_atree_init( lb_atree_init(struct atree *tree, atree_cmp_fp compare)
struct atree *tree,
atree_cmp_fp compare)
{ {
tree->cmpf = compare; tree->cmpf = compare;
tree->root = NULL; tree->root = NULL;

View File

@ -1,4 +1,4 @@
#include "lp.h" #include "lb/llist.h"
static void static void
llist_node_init(struct llist_node *node) llist_node_init(struct llist_node *node)

View File

@ -1,3 +0,0 @@
#pragma once
#include "kernel/lb.h"

View File

@ -1,4 +1,5 @@
#include "lp.h" #include "lb/salloc.h"
#include "clib.h"
struct salloc_header struct salloc_header
{ {

View File

@ -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[];
};

View File

@ -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 struct phys_page_desc
{ {
@ -10,7 +16,7 @@ struct phys_page_desc
static struct atree active_tree; static struct atree active_tree;
static struct llist free_list; static struct llist free_list;
static struct rwwlock lock; static struct rww_lock lock;
/* /*
* A comparison function between tree_node and your_node * 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]; // 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++) // for (uint64 j = 0; j <= each_node->size; j++)
// { // {

View File

@ -1,6 +1,6 @@
include $(MK)/prologue.mk include $(MK)/prologue.mk
SRC_$(d) := $(d)/rf.c SRC_$(d) := $(d)/ref.c
include $(MK)/stdrules.mk include $(MK)/stdrules.mk

View File

@ -1,6 +1,9 @@
#include "rp.h" #include "rf/ref.h"
#include "kernel/ke.h" #include "ke/assert.h"
#include "kernel/lb.h" #include "ke/spin_lock.h"
#include "ke/atomic.h"
#include "ke/intr.h"
#include "clib.h"
#define K_IDENT_BASE (0x80000000ul) #define K_IDENT_BASE (0x80000000ul)
@ -58,7 +61,7 @@ rf_ref_init(void)
k_status k_status
rf_ref_node_init(struct ref_node *rf_node, ref_free_fp free_func) 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->f_free = free_func;
rf_node->ref_count = 1; 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 k_status
rf_ref_obj(struct ref_node *ref_node) 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) 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); 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; return STATUS_SUCCESS;
} }
@ -88,7 +91,7 @@ rf_ref_obj(struct ref_node *ref_node)
k_status k_status
rf_deref_obj(struct ref_node *rf_node) 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) 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); 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) if (old_ref_count == 1)
{ {
@ -113,7 +116,7 @@ rf_deref_obj(struct ref_node *rf_node)
k_status k_status
rf_open_obj_by_ident(k_ident id, struct ref_node **out) 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; uint32 irql;
k_status status = STATUS_SUCCESS; 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; k_status result;
uint32 irql; uint32 irql;
ke_assert(ke_get_irql() <= IRQL_DPC); KE_ASSERT(ke_get_irql() <= IRQL_DPC);
result = STATUS_SUCCESS; result = STATUS_SUCCESS;
@ -198,7 +201,7 @@ rf_ident_close(k_ident id)
k_status status; k_status status;
struct ref_node *ref; struct ref_node *ref;
ke_assert(ke_get_irql() <= IRQL_DPC); KE_ASSERT(ke_get_irql() <= IRQL_DPC);
status = STATUS_SUCCESS; status = STATUS_SUCCESS;
ref = NULL; ref = NULL;

View File

@ -1,3 +0,0 @@
#pragma once
#include "kernel/rf.h"

View File

@ -1,7 +1,8 @@
include $(MK)/prologue.mk include $(MK)/prologue.mk
SRCIN_$(d) := $(d)/linker.ld.in \ MOD:=MK
$(d)/grub.cfg.in
SRCIN_$(d) := $(d)/linker.ld.in
include $(MK)/stdrules.mk include $(MK)/stdrules.mk

View File

@ -1,9 +1,9 @@
#define ASM_FILE #define ASM_FILE
#include "common.h" #include "mlayout.h"
OUTPUT_FORMAT(elf64-x86-64) OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64) OUTPUT_ARCH(i386:x86-64)
ENTRY(hal_main_32) ENTRY(sys_entry)
SECTIONS SECTIONS
{ {

View File

@ -11,22 +11,25 @@ OBJAS_$(d) := $(OBJAS_$(d)) $(addprefix $(OUT)/, $(SRCAS_$(d):.asm=.a))
OBJIN_$(d) := $(OBJIN_$(d)) $(addprefix $(OUT)/, $(SRCIN_$(d):.in=)) OBJIN_$(d) := $(OBJIN_$(d)) $(addprefix $(OUT)/, $(SRCIN_$(d):.in=))
DEP_$(d) := $(DEP_$(d)) $(addsuffix .d, $(OBJ_$(d)) $(OBJIN_$(d))) 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) $(MKDIR)
$(COMP) $(COMP)
$(GDEP) $(GDEP)
$(OBJAS_$(d)): $(OUT)/%.a: %.asm $(OUT)/$(d)/%.a: MOD:=$(MOD)
$(OBJAS_$(d)): $(OUT)/$(d)/%.a: $(d)/%.asm
$(MKDIR) $(MKDIR)
$(COMPAS) $(COMPAS)
$(OBJIN_$(d)): $(OUT)/%: %.in $(OBJIN_$(d)): $(OUT)/$(d)/%: $(d)/%.in
$(MKDIR) $(MKDIR)
$(PREP) $(PREP)
$(GDEP) $(GDEP)
# append all OBJECTS to OBJ # append all OBJECTS to OBJ
OBJ := $(OBJ) $(OBJ_$(d)) $(OBJAS_$(d)) OBJ := $(OBJ) $(OBJ_$(d)) $(OBJAS_$(d))
CLEAN := $(CLEAN) $(OBJ_$(d)) $(OBJAS_$(d)) $(OBJIN_$(d)) $(DEP_$(d))
-include $(DEP_$(d)) -include $(DEP_$(d))

1
qemu.bat Normal file
View File

@ -0,0 +1 @@
qemu-system-x86_64 -bios qemu_bios.bin -cdrom out/secxkrnl.iso -s -S

BIN
qemu_bios.bin Normal file

Binary file not shown.

3
qemugdb Normal file
View File

@ -0,0 +1,3 @@
target remote localhost:1234
symbol-file out/secxkrnl.elf
break *0x1001000

View File

@ -1,6 +1,7 @@
#include "test_main.h" #include "test_main.h"
#include "test_case.h" #include "test_case.h"
#include "lb.h" #include "lb/atree.h"
#include "clib.h"
#include <stdio.h> #include <stdio.h>
struct test_node struct test_node

View File

@ -1,7 +1,7 @@
#ifndef TEST_TEST_H #ifndef TEST_TEST_H
#define TEST_TEST_H #define TEST_TEST_H
#include "common.h" #include "cdef.h"
void void
linked_list_test(void); linked_list_test(void);

View File

@ -1,7 +1,7 @@
#ifndef TEST_DRIVER_H #ifndef TEST_DRIVER_H
#define TEST_DRIVER_H #define TEST_DRIVER_H
#include "common.h" #include "cdef.h"
void void
test_begin(char *name); test_begin(char *name);

View File

@ -1,6 +1,5 @@
#include "common.h" #include "cdef.h"
#include "kernel/ke.h" #include "hal.h"
#include "hal_export.h"
/** /**
* Bogus implementation of HAL * Bogus implementation of HAL
@ -42,7 +41,7 @@ hal_issue_intr(uint32 core, uint32 vector)
} }
void KABI 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 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) hal_get_core_id(void)
{ {
return 0; return 0;
}
void KABI
hal_halt(void)
{
} }

View File

@ -1,6 +1,7 @@
#include "test_main.h" #include "test_main.h"
#include "test_case.h" #include "test_case.h"
#include "lb.h" #include "lb/llist.h"
#include "clib.h"
#include <stdio.h> #include <stdio.h>
struct test_list_node struct test_list_node

View File

@ -1,5 +1,5 @@
#include "test_main.h" #include "test_main.h"
#include "lb.h" #include "lb/salloc.h"
#include "test_case.h" #include "test_case.h"
typedef union typedef union