ADDED arch/x86.cdecl.32.s Index: arch/x86.cdecl.32.s ================================================================== --- arch/x86.cdecl.32.s +++ arch/x86.cdecl.32.s @@ -0,0 +1,19 @@ +;; x86.cdecl.32.s: x86 cdecl impl +; vim: ft=nasm + +%macro ccall 1-* + %assign i 0 + ; arguments must be pushed to the stack backwards + %assign ct (%0-ct)-1 + %rotate ct + %rep ct + %rotate -1 + push %1 + %endrep + %rotate ct + push esp ; it's our responsibility to preserve the stack + call %1 + ; the arguments are still on the stack; time to + ; dump them back into the Garbage Zone + pop esp +%endmacro ADDED arch/x86.cdecl.64.s Index: arch/x86.cdecl.64.s ================================================================== --- arch/x86.cdecl.64.s +++ arch/x86.cdecl.64.s @@ -0,0 +1,49 @@ +;; x86.cdecl.64.s: x86-64 cdecl impl +; vim: ft=nasm + +%macro ccall 1-* + %if %0 > ccall.reg.ct + %assign ct ccall.reg.ct + %else + %assign ct %0-1 + %endif + %assign i 0 + %rotate 1 + %rep ct + ; if the function is well-behaved, all its arguments fit + ; in registers. if not, things get ugly. see below. + mov ccall.reg. %+ i, %1 + %assign i i+1 + %rotate 1 + %endrep + %if %0 > ccall.reg.ct + ; if there are more parameters to a C function than the + ; number of permitted registers, they must be pushed in + ; reverse order to the stack. + ; keep your function signatures under control, people. + %assign ct (%0-ct)-1 + %rotate ct + %rep ct + %rotate -1 + push %1 + %endrep + %rotate ct + push rsp ; it's our responsibility to preserve the stack + %endif + call %1 + %if %0 > ccall.reg.ct + ; the extra arguments are still on the stack; time to + ; dump them back into the Garbage Zone + pop rsp + %endif +%endmacro + +; register order for ccall convention +%define ccall.reg.ct 6 +%define ccall.reg.ret rdi +%define ccall.reg.0 rdi +%define ccall.reg.1 rsi +%define ccall.reg.2 rdx +%define ccall.reg.3 rcx +%define ccall.reg.4 r8 +%define ccall.reg.5 r9 ADDED arch/x86.fbsd.32.s Index: arch/x86.fbsd.32.s ================================================================== --- arch/x86.fbsd.32.s +++ arch/x86.fbsd.32.s @@ -0,0 +1,23 @@ +;; abi definition file for x86 linux 64-bit +; vim: ft=nasm + +; syscall numbers - syscall table must be created first! +%include "calls.x86.fbsd.32.s" + +; extremely stupid freebsd-ism: expects the syscall to +; come from a function +_syscall: int 0x80 + ret + +%define sys.call call _syscall + +; parameters are passed on the stack +%macro syscall 1-* + mov eax, %1 + %rep %0-1 + %rotate 1 + push %1 + %endrep + sys.call + add esp, 4*(%0-1) +%endmacro ADDED arch/x86.fbsd.64.s Index: arch/x86.fbsd.64.s ================================================================== --- arch/x86.fbsd.64.s +++ arch/x86.fbsd.64.s @@ -0,0 +1,8 @@ +;; abi definition file for x86 linux 64-bit +; vim: ft=nasm + +; syscall numbers - syscall table must be created first! +%include "calls.x86.fbsd.64.s" + +; freebsd uses the common x86-64 ABI +%include "x86.syscall.64.s" ADDED arch/x86.syscall.64.s Index: arch/x86.syscall.64.s ================================================================== --- arch/x86.syscall.64.s +++ arch/x86.syscall.64.s @@ -0,0 +1,39 @@ +;; x86.64.s: common x86-64 ABI impl +; vim: ft=nasm + +%macro sys 1-8 +; syscall64 wrapper, ex. `sys sys.write, 1, msg, msg.len` + %assign i 0 + %rep %0 + mov sys.reg. %+ i, %1 ; i'm actually shocked this worked + %rotate 1 + %assign i i+1 + %endrep + syscall +%endmacro + +%macro sys.prep 1-8 + ; for when we need to modify parameters before we + ; make the actual call. + %assign i 0 + %rep %0 + mov sys.reg. %+ i, %1 + %rotate 1 + %assign i i+1 + %endrep +%endmacro + +; syscall ops +%define sys.call syscall + +; register order for syscall convention +%define sys.reg.n 7 +%define sys.reg.ret rax +%define sys.reg.0 rax +%define sys.reg.1 rdi +%define sys.reg.2 rsi +%define sys.reg.3 rdx +%define sys.reg.4 r10 +%define sys.reg.5 r8 +%define sys.reg.6 r9 + ADDED kcli/makefile Index: kcli/makefile ================================================================== --- kcli/makefile +++ kcli/makefile @@ -0,0 +1,1 @@ +include ../modmake ADDED kcore/boot.x86.lin.64.s Index: kcore/boot.x86.lin.64.s ================================================================== --- kcore/boot.x86.lin.64.s +++ kcore/boot.x86.lin.64.s @@ -0,0 +1,17 @@ +; vim: ft=nasm +bits 64 +%include "../arch/x86.lin.64.s" +global _start:function +extern _boot +extern entry; + +_start: + mov rbp, rsp + mov rdi, [rbp + 0] ; argc + lea rsi, [rbp + 8] ; argv + + call _boot + + mov sys.reg.1, sys.reg.ret + mov sys.reg.0, sys.exit + sys.call ADDED kcore/def.fbsd.i Index: kcore/def.fbsd.i ================================================================== --- kcore/def.fbsd.i +++ kcore/def.fbsd.i @@ -0,0 +1,2 @@ +#define KVos fbsd +typedef unsigned char stat; ADDED kcore/def.lin.i Index: kcore/def.lin.i ================================================================== --- kcore/def.lin.i +++ kcore/def.lin.i @@ -0,0 +1,2 @@ +#define KVos lin +typedef unsigned char stat; DELETED kcore/start.x86.lin.64.s Index: kcore/start.x86.lin.64.s ================================================================== --- kcore/start.x86.lin.64.s +++ kcore/start.x86.lin.64.s @@ -1,17 +0,0 @@ -; vim: ft=nasm -bits 64 -%include "../arch/x86.lin.64.s" -global _start -extern _boot -extern entry; - -_start: - mov rbp, rsp - mov rdi, [rbp + 0] ; argc - lea rsi, [rbp + 8] ; argv - - call _boot; - - mov sys.reg.1, sys.reg.ret - mov sys.reg.0, sys.exit - sys.call ADDED kcore/type.x86.32.i Index: kcore/type.x86.32.i ================================================================== --- kcore/type.x86.32.i +++ kcore/type.x86.32.i @@ -0,0 +1,14 @@ +#define KVarch x86 +#define KVbits 32 + +typedef unsigned long sz; + +typedef unsigned char u8; +typedef signed char s8; +typedef unsigned short u16; +typedef signed short s16; +typedef unsigned long u32; +typedef signed long s32; + +typedef u32 word; +typedef s32 sword; ADDED kcore/type.x86.64.i Index: kcore/type.x86.64.i ================================================================== --- kcore/type.x86.64.i +++ kcore/type.x86.64.i @@ -0,0 +1,18 @@ +#define KVarch x86 +#define KVbits 64 + +typedef unsigned long long sz; + +typedef unsigned char u8; +typedef signed char s8; +typedef unsigned short u16; +typedef signed short s16; +typedef unsigned long u32; +typedef signed long s32; +typedef unsigned long long u64; +typedef signed long long s64; +typedef __uint128_t u128; +typedef __int128_t s128; + +typedef u64 word; +typedef s64 sword; ADDED kio/kiostream.posix.i Index: kio/kiostream.posix.i ================================================================== --- kio/kiostream.posix.i +++ kio/kiostream.posix.i @@ -0,0 +1,1 @@ +int platform_fd; // using posix syscalls for low-level IO Index: makefile ================================================================== --- makefile +++ makefile @@ -22,11 +22,13 @@ # include libgcc.a in gcc builds, just in case ifeq ($(CC),gcc) export COMPLIB = -lgcc endif -all: defs obj tool +all: defs obj tool lib.static lib.shared +lib.static: defs obj $(OUT)/libk.a +lib.shared: defs obj $(OUT)/libk.so obj: $(moddirs:%=%.obj) defs: $(moddirs:%=%.def) tool: $(OUT)/libk.a $(binmods:%=%.tool) clean: rm -rf $(TMP) $(OUT) @@ -48,16 +50,20 @@ cd $* && $(MAKE) def %.calls: arch/makefile cd arch && $(MAKE) $(TMP)/calls.$*.s -$(OUT)/libk.so: obj $(OUT) - $(CC) -shared -nostdlib $(COMPLIB) -o $@ $(OUT)/*.o +$(OUT)/libk.so: obj $(OUT) $(OUT)/boot.o + ld -shared $(COMPLIB) -o $@ $(filter-out kcore.boot.%, $(wildcard *.o)) + # $(CC) -shared -fPIC -nostdlib $(COMPLIB) -o $@ $(OUT)/*.o + +$(OUT)/boot.o: $(OUT)/kcore.boot.o $(OUT)/kcore.boot.$(TARGET).o + ld -r $^ -o $(OUT)/boot.o $(OUT)/libk.a: obj $(OUT) # using `ar rc` and ranlib here instead of # `ar rcs` in case `ar` isn't the GNU version ar rc $@ $(OUT)/*.o ranlib $@ $(OUT) $(OUT)/k: mkdir -p $@ Index: modmake ================================================================== --- modmake +++ modmake @@ -11,11 +11,11 @@ tools = $(filter exe.%.c, $(src)) nontools = $(filter-out exe.%.c, $(src)) cobjects = $(filter %.c, $(nontools)) sobjects = $(filter %.${TARGET}.s, $(nontools)) -cflags = -isystem ${OUT} -nostdlib ${COMPLIB} -L${OUT} -lk +cflags = -isystem ${OUT} -fPIC -nostdlib ${COMPLIB} -L${OUT} -lk obj: $(cobjects:%.c=${OUT}/$(mod).%.o) \ $(sobjects:%.s=${OUT}/$(mod).%.o) tool: $(tools:exe.%.c=${OUT}/$(mod).%) \ ${OUT}/libk.a