From 633decc0afd474bcee55764a493e28d1560a9da0 Mon Sep 17 00:00:00 2001 From: "brice.boisson" Date: Wed, 27 Sep 2023 10:35:00 +0900 Subject: [PATCH] Add: libc --- Makefile | 10 +++++++++- Makefile.common | 1 + libk/Makefile | 20 ++++++++++++++++++++ libk/syscall.c | 27 +++++++++++++++++++++++++++ libk/syscall.h | 7 +++++++ src/int.S | 10 +++++++++- src/isr.c | 11 ++++++----- src/launch_process.c | 2 +- src/myfile.o | Bin 13840 -> 0 bytes src/syscall.c | 4 ++-- src/syscall.h | 2 +- src/userland.c | 2 ++ 12 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 libk/Makefile create mode 100644 libk/syscall.c create mode 100644 libk/syscall.h delete mode 100644 src/myfile.o diff --git a/Makefile b/Makefile index 9c577e0..b87473f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ include Makefile.common -all: k.iso +all: bin k.iso k.iso: install ./tools/create_iso.sh @@ -9,8 +9,16 @@ install: mkdir -p $(GRUBDIR) $(MAKE) -C $(SOURCEDIR) $@ +bin: + $(MAKE) -C $(LIB) + gcc -c test.c -Ilibk -Llibk -lk -m32 + ld -m elf_i386 -Ttext=0x6000000 --entry=main test.o -L./libk/ -lk + objcopy --input binary --output elf32-i386 --binary-architecture i386 --rename-section .data=.rodata,CONTENTS,ALLOC,LOAD,READONLY,DATA a.out myfile.o + cp myfile.o $(SOURCEDIR)/myfile.o + clean: $(MAKE) -C $(SOURCEDIR) $@ + $(MAKE) -C $(LIB) $@ $(RM) kernel.iso $(RM) -r iso diff --git a/Makefile.common b/Makefile.common index 095aa11..92ed0b0 100644 --- a/Makefile.common +++ b/Makefile.common @@ -4,3 +4,4 @@ ASFLAGS = -m32 GRUBDIR = iso SOURCEDIR = src TARGET = kernel +LIB=libk diff --git a/libk/Makefile b/libk/Makefile new file mode 100644 index 0000000..15e0207 --- /dev/null +++ b/libk/Makefile @@ -0,0 +1,20 @@ +include ../Makefile.common + + +OBJS = \ + syscall.o \ + +DEPS = $(OBJS:.o=.d) + +CPPFLAGS += -MMD -Iinclude +CFLAGS += $(K_EXTRA_CFLAGS) -g -nostdlib +LDFLAGS += -Wl,-Tkernel.lds +LDLIBS = + +all: $(TARGET) + ar -rcs $(LIB).a *.o + +$(TARGET): $(OBJS) + +clean: + $(RM) $(OBJS) $(DEPS) $(TARGET) $(LIB).a \ No newline at end of file diff --git a/libk/syscall.c b/libk/syscall.c new file mode 100644 index 0000000..aca3fae --- /dev/null +++ b/libk/syscall.c @@ -0,0 +1,27 @@ +#include "syscall.h" + +int __syscall(int num, void *a0, void *a1, void *a2, void *a3, void *a4) +{ + int ret; + asm volatile(" \n \ + mov %1, %%eax; \n \ + mov %2, %%ebx; \n \ + mov %3, %%ecx; \n \ + mov %4, %%edx; \n \ + mov %5, %%edi; \n \ + mov %6, %%esi; \n \ + int $48; \n \ + mov %%eax, %0" : "=m" (ret), + "+m" (num), + "+m" (a0), + "+m" (a1), + "+m" (a2), + "+m" (a3), + "+m" (a4)); + return ret; +} + +int write(int fd, char *buf, int count) +{ + return __syscall(1, (void *) fd, (void *) buf, (void *) count, (void *) 0, (void *) 0); +} diff --git a/libk/syscall.h b/libk/syscall.h new file mode 100644 index 0000000..8c0bafc --- /dev/null +++ b/libk/syscall.h @@ -0,0 +1,7 @@ +#ifndef SYSCALL_H +#define SYSCALL_H + +int __syscall(int num, void *a0, void *a1, void *a2, void *a3, void *a4); +int write(int fd, char *buf, int count); + +#endif /* !SYSCALL_H */ \ No newline at end of file diff --git a/src/int.S b/src/int.S index fbadc20..ef6f802 100644 --- a/src/int.S +++ b/src/int.S @@ -82,16 +82,24 @@ _asm_sycall_handler: push %ds push %es push %fs - push %gs + push %gs push %ebx mov $0x10,%bx mov %bx,%ds pop %ebx + push %esi + push %edi + push %edx + push %ecx push %ebx push %eax call syscall_handler pop %ebx pop %ebx + pop %ecx + pop %edx + pop %edi + pop %esi pop %gs pop %fs pop %es diff --git a/src/isr.c b/src/isr.c index 213950d..e3ea96c 100644 --- a/src/isr.c +++ b/src/isr.c @@ -28,16 +28,17 @@ void isr_kbd_int(void) DEBUG_INFO("Keyboard input: %d", x); } -void syscall_handler(int eax, int ebx) +int syscall_handler(int eax, void *ebx, void *ecx, void *edx, void *edi, void *esi) { - DEBUG_INFO("Syscall %d has been called from the userland with parameter %d", eax, ebx); + DEBUG_INFO("Syscall %d has been called from the userland with parameters: %d, %d, %d, %d, %d", eax, ebx, ecx, edx, edi, esi); switch (eax) { case 1: - char t = *((char *) ebx); - DEBUG_INFO("Syscall write : %d", (int) t); - return write(1, (u32) ebx); + int fd = (int) ebx; + char *buf = (char *) ecx; + int size = (int) edx; + return write(fd, buf, size); break; case 2: DEBUG_INFO("Syscall keyboard"); diff --git a/src/launch_process.c b/src/launch_process.c index 2735335..18ca823 100644 --- a/src/launch_process.c +++ b/src/launch_process.c @@ -24,7 +24,7 @@ void launch_process(u16 tss, int memory_start, int userland_stack, int userland_ movw %%ax, %%gs \n \ mov %%esp, %%eax \n \ push $0x23 \n \ - push $0x7FFFFFFF \n \ + push $0x7FFFEFFF \n \ pushfl \n \ push $0x1B \n \ push $0x6000000 \n \ diff --git a/src/myfile.o b/src/myfile.o deleted file mode 100644 index 7cab4588283ec1115d243bfe938b4b0d0bde422a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 13840 zcmeI3&ubGw6vt=NB-T<(Eec8%78R_QHBGgIB4S(m<4{{{Q?JXW-6p{%Te4GVN}-4d zO8)_m9{n=}5B?Q`2MZMiEqD~;_q)lYX|<)`!5rSBZ{N(kH?uRJnM-E-wkKvLEz43y zEY+b7YD!Jtt$Xf7W0_(2YCv`2*&OqGEuo-UG*IIV?NxO@ zEF?+2iU&0TZTjt~oJ2i2jUu(R|Bu^BL6g*b@KHIK>7O0MJD*c@xTYP&j@ITCscOtw$;a$np>>_dkthXjNAl4{zT2tfWBlZ9yZrOuFPTCo? z)2Z}ODw9HP!%=C?D~PN8S|X{YIF7$Vy#@q=uTF6YWVR4TW)%#*GHH>-+ z{b6vQlTc$Uz|Xs$J{40_u&w^tBXycV>YiQ{dcH5Z0%sJ)6-C9hakuD|SQ4~9p}39BXTqA42DSqe%ezpOFsQ%gT0&p>0)D7OUr zz`PP8+(U{3<$gr)XmBCM{1zi-ub4l>GRH$qlAAEiazo5`_fcnD2egs%66KRL!lJwq z#&SW! z7EA?}V^PjSxdtOUhdS~cN8SicE-P~Wd9H&-lsm8$Ex!Mbi7 zr^9VLPlsE=;--J%5vv5z!HM2_x(AtZ?uH?!Z05-k%(_}**!~8A4`9iPzT-IWA#MbI TBWAp#oUb9T>$(YoHF3WIR;