diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/kernel/syscall/actions/sys_write.c | 9 | ||||
-rw-r--r-- | src/kernel/syscall/syscall.c | 15 | ||||
-rw-r--r-- | src/userspace/main.c | 12 | ||||
-rw-r--r-- | src/userspace/syscall.h | 72 |
5 files changed, 91 insertions, 21 deletions
@@ -36,8 +36,8 @@ build: clean # Userspace nasm -f elf ./src/userspace/start.asm -o ./build/user_start.o || exit; \ - i686-elf-gcc -c ./src/userspace/main.c -o ./build/user_main.o -I ./src/mlibc -std=gnu99 -ffreestanding -O3 -Wall -Wextra -Wno-unused-parameter || exit; \ - i686-elf-gcc -I ./src/mlibc -o ./build/user.o -std=gnu99 -ffreestanding -O2 -nostdlib ./build/user_start.o ./build/user_main.o || exit; \ + i686-elf-gcc -c ./src/userspace/main.c -o ./build/user_main.o -I ./src/mlibc -I ./src/userspace -std=gnu99 -ffreestanding -O3 -Wall -Wextra -Wno-unused-parameter || exit; \ + i686-elf-gcc -I ./src/mlibc -I ./src/userspace -o ./build/user.o -std=gnu99 -ffreestanding -O2 -nostdlib ./build/user_start.o ./build/user_main.o || exit; \ i686-elf-objcopy -O binary ./build/user.o ./build/user.bin; \ # Create ISO diff --git a/src/kernel/syscall/actions/sys_write.c b/src/kernel/syscall/actions/sys_write.c index 19c35fa..882ff19 100644 --- a/src/kernel/syscall/actions/sys_write.c +++ b/src/kernel/syscall/actions/sys_write.c @@ -1,12 +1,9 @@ #include <stdint-gcc.h> #include <mlibc/stdio.h> -#include <kernel/io/io.h> -uint32_t sys_write(unsigned int buf, unsigned int count) +uint32_t sys_write(unsigned int fd, unsigned int buf, unsigned int count) { - serial_write("\n"); - serial_write_dec(count); - serial_write("WRITE: \n"); - serial_write((const char *) buf); + for (uint32_t i = 0; i < count; i++) + writec(*((char *) buf++)); return count; }
\ No newline at end of file diff --git a/src/kernel/syscall/syscall.c b/src/kernel/syscall/syscall.c index 08b4d49..0eac619 100644 --- a/src/kernel/syscall/syscall.c +++ b/src/kernel/syscall/syscall.c @@ -5,13 +5,20 @@ typedef uint32_t (*syscall_func)(unsigned int, ...); +uint32_t (*syscalls[])() = { + [1] = sys_write +}; + void syscall_handler(struct regs *r) { + if (r->eax >= sizeof(syscalls) / sizeof(*syscalls)) + return; + + syscall_func location = (syscall_func) syscalls[r->eax]; + if (!location) + return; + serial_write("Received syscall!\n"); - serial_write_dec(r->eax); - serial_write("\n"); - serial_write_dec(r->ecx); - syscall_func location = (syscall_func) sys_write; location(r->ebx, r->ecx, r->edx, r->esi, r->edi); } diff --git a/src/userspace/main.c b/src/userspace/main.c index 5df961f..f7cd3ba 100644 --- a/src/userspace/main.c +++ b/src/userspace/main.c @@ -1,15 +1,9 @@ -void write(const char *str, int len) -{ - int ret; - asm volatile ("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ - : "=a" (ret) \ - : "0" (1), "b" ((int) (str)), "c"((int) (len))); -} +#include <syscall.h> void user_main() { - const char hello[] = "Switched to usermode!\n"; - write(hello, sizeof(hello)); + const char hello[] = "> Successfully to usermode!\n"; + syscall_write(0, hello, sizeof(hello)); while (1) {}; }
\ No newline at end of file diff --git a/src/userspace/syscall.h b/src/userspace/syscall.h new file mode 100644 index 0000000..162b219 --- /dev/null +++ b/src/userspace/syscall.h @@ -0,0 +1,72 @@ +#ifndef MELVIX_SYSCALL_H +#define MELVIX_SYSCALL_H + +#define DECL_SYSCALL0(fn) int syscall_##fn() +#define DECL_SYSCALL1(fn, p1) int syscall_##fn(p1) +#define DECL_SYSCALL2(fn, p1, p2) int syscall_##fn(p1,p2) +#define DECL_SYSCALL3(fn, p1, p2, p3) int syscall_##fn(p1,p2,p3) +#define DECL_SYSCALL4(fn, p1, p2, p3, p4) int syscall_##fn(p1,p2,p3,p4) +#define DECL_SYSCALL5(fn, p1, p2, p3, p4, p5) int syscall_##fn(p1,p2,p3,p4,p5) + +#define DEFN_SYSCALL0(fn, num) \ + int syscall_##fn() { \ + int a; __asm__ __volatile__("int $0x80" : "=a" (a) : "0" (num)); \ + return a; \ + } + +#define DEFN_SYSCALL1(fn, num, P1) \ + int syscall_##fn(P1 p1) { \ + int __res; __asm__ __volatile__("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ + : "=a" (__res) \ + : "0" (num), "b" ((int)(p1)) \ + : "memory"); \ + return __res; \ + } + +#define DEFN_SYSCALL2(fn, num, P1, P2) \ + int syscall_##fn(P1 p1, P2 p2) { \ + int __res; __asm__ __volatile__("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ + : "=a" (__res) \ + : "0" (num), "b" ((int)(p1)), "c"((int)(p2)) \ + : "memory"); \ + return __res; \ + } + +#define DEFN_SYSCALL3(fn, num, P1, P2, P3) \ + int syscall_##fn(P1 p1, P2 p2, P3 p3) { \ + int __res; __asm__ __volatile__("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ + : "=a" (__res) \ + : "0" (num), "b" ((int)(p1)), "c"((p2)), "d"((int)(p3)) \ + : "memory"); \ + return __res; \ + } + +#define DEFN_SYSCALL4(fn, num, P1, P2, P3, P4) \ + int syscall_##fn(P1 p1, P2 p2, P3 p3, P4 p4) { \ + int __res; __asm__ __volatile__("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ + : "=a" (__res) \ + : "0" (num), "b" ((int)(p1)), "c"((int)(p2)), "d"((int)(p3)), "S"((int)(p4)) \ + : "memory"); \ + return __res; \ + } + +#define DEFN_SYSCALL5(fn, num, P1, P2, P3, P4, P5) \ + int syscall_##fn(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { \ + int __res; __asm__ __volatile__("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ + : "=a" (__res) \ + : "0" (num), "b" ((int)(p1)), "c"((int)(p2)), "d"((int)(p3)), "S"((int)(p4)), "D"((int)(p5)) \ + : "memory"); \ + return __res; \ + } + +/** + * DECLARATIONS + */ +DECL_SYSCALL3(write, int, const char *, int); + +/** + * DEFINITIONS + */ +DEFN_SYSCALL3(write, 1, int, const char *, int); + +#endif |