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 7cab458..0000000 Binary files a/src/myfile.o and /dev/null differ diff --git a/src/syscall.c b/src/syscall.c index 670f283..78f1150 100644 --- a/src/syscall.c +++ b/src/syscall.c @@ -6,9 +6,9 @@ * Syscall handler for write, currently only serial write is supported. * Use the fd 1, to make a serial write. */ -int write(int fd, void *buf) +int write(int fd, void *buf, int size) { - if (fd != 1) + if (fd != 1 || size < 0) return -1; if (write_serial(buf)) diff --git a/src/syscall.h b/src/syscall.h index 36c9358..d69a768 100644 --- a/src/syscall.h +++ b/src/syscall.h @@ -1,7 +1,7 @@ #ifndef SYSCALL_H #define SYSCALL_H -int write(int fd, void *buf); +int write(int fd, void *buf, int size); int keyboard(void); #endif /* !SYSCALL_H */ \ No newline at end of file diff --git a/src/userland.c b/src/userland.c index edf2ed8..e154f90 100644 --- a/src/userland.c +++ b/src/userland.c @@ -20,6 +20,8 @@ int create_process(int uid, char *data_start) // Allocate stack allocate_new_page(uid, 0x7FFFF000); + allocate_new_page(uid, 0x7FFFE000); + allocate_new_page(uid, 0x7FFFD000); // TODO : create data seg by process // load elf