diff options
| author | Jake Mannens <jake72360@gmail.com> | 2018-06-24 01:49:05 +1000 |
|---|---|---|
| committer | Jake Mannens <jake72360@gmail.com> | 2018-06-24 01:49:05 +1000 |
| commit | acba87c1e946118f0ba4308a7211199cf9b7cbb2 (patch) | |
| tree | 034276cdb5a855516317069b37075a768b46ce96 /kernel | |
| parent | 2c429f6e1ac51ea27f203005eeef20d2b05c759e (diff) | |
Modified makefiles to use the more appropriate variable $(MAKE) when
invoking the tool recursively.
Disabled GCC's position-independent-code generation in makefiles.
Modified makefile for kernel/usrbin so that it now compiles and links C
code into the userspace test.
Created errno.h and populated it with standard error definitions.
Replaced the va_list based system call handlers with a system call table
defined in the header kernel/sys.h. NOTE: This header is included in
kmain.c and should ONLY be included there! Do NOT include this header in
sys.c.
Rather than fetching the user's stack pointer and using it to initialize
a va_list, parameters are now passed to the call handlers via the
general purpose registers EAX, EBX, ECX and EDX where EAX contains the
requested call number and conveys the return value.
Setup macros in unistd.h to aid to making system calls from userspace.
Implemented two basic system calls; sys_print and sys_dummy. The former
takes a single char* argument and displays it on screen whilst the
latter is used to populate otherwise empty entries of the system call
table. sys_dummy returns the error ENOSYS whenever it is called.
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/Makefile | 11 | ||||
| -rw-r--r-- | kernel/kmain.c | 1 | ||||
| -rw-r--r-- | kernel/link.ld | 2 | ||||
| -rw-r--r-- | kernel/sys.c | 10 | ||||
| -rw-r--r-- | kernel/syscall.c | 12 | ||||
| -rw-r--r-- | kernel/traps.s | 17 | ||||
| -rw-r--r-- | kernel/usrbin/Makefile | 27 | ||||
| -rw-r--r-- | kernel/usrbin/lib.s | 10 | ||||
| -rw-r--r-- | kernel/usrbin/link.ld | 8 | ||||
| -rw-r--r-- | kernel/usrbin/main.c | 7 | ||||
| -rw-r--r-- | kernel/usrbin/print.c | 3 | ||||
| -rw-r--r-- | kernel/usrbin/usrbin.bin | bin | 59 -> 0 bytes | |||
| -rw-r--r-- | kernel/usrbin/usrbin.s | 12 |
13 files changed, 77 insertions, 43 deletions
diff --git a/kernel/Makefile b/kernel/Makefile index 3427449..6cf8ae4 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -2,10 +2,10 @@ TARGET = kernel SRCS = $(wildcard *.c) ASMS = $(wildcard *.s) -OBJS = $(SRCS:.c=.o) $(ASMS:.s=.o) usrbin/usrbin.o +OBJS = $(SRCS:.c=.o) $(ASMS:.s=.o) usrbin/usrbin_blob.o -CFLAGS = -m32 -I../include -ffreestanding -nostdinc -nostdlib -fno-stack-protector -gstabs+ -LDFLAGS = -m elf_i386 -Tlink.ld +CFLAGS = -m32 -I../include -ffreestanding -nostdinc -nostdlib -fno-stack-protector -fno-pie -gstabs+ +LDFLAGS = -m elf_i386 -T link.ld ASMFLAGS = -f elf32 CC = gcc $(CFLAGS) @@ -16,8 +16,8 @@ all: build build: $(TARGET) -usrbin/usrbin.o: usrbin/usrbin.s - make -C usrbin +usrbin/usrbin_blob.o: $(wildcard usrbin/*.s) $(wildcard usrbin/*.c) + $(MAKE) -C usrbin .s.o: $(ASM) -o $*.o $^ @@ -31,6 +31,7 @@ $(TARGET): $(OBJS) clean: rm -f $(OBJS) rm -f $(TARGET) + $(MAKE) -C usrbin clean run: $(TARGET) qemu-system-x86_64 -s -kernel $(TARGET) diff --git a/kernel/kmain.c b/kernel/kmain.c index bf6b9ae..4bfd129 100644 --- a/kernel/kmain.c +++ b/kernel/kmain.c @@ -1,4 +1,5 @@ #include <kernel/con.h> +#include <kernel/sys.h> #include <stdint.h> #include <sys/types.h> diff --git a/kernel/link.ld b/kernel/link.ld index a97b27f..21c9b7c 100644 --- a/kernel/link.ld +++ b/kernel/link.ld @@ -1,9 +1,7 @@ OUTPUT_FORMAT(elf32-i386) -/* OUTPUT_FORMAT(binary) */ ENTRY(kboot) SECTIONS { - /* . = 0x100000; */ . = 0; .mboothdr : { *(.mboothdr) } .text : { *(.text) } diff --git a/kernel/sys.c b/kernel/sys.c new file mode 100644 index 0000000..b648dd1 --- /dev/null +++ b/kernel/sys.c @@ -0,0 +1,10 @@ +#include <errno.h> +#include <kernel/con.h> + +int sys_print(char *s) { + printf(s); +} + +int sys_dummy(void) { + return -ENOSYS; +} diff --git a/kernel/syscall.c b/kernel/syscall.c deleted file mode 100644 index 8fa5da0..0000000 --- a/kernel/syscall.c +++ /dev/null @@ -1,12 +0,0 @@ -#include <kernel/con.h> -#include <stdarg.h> -#include <stdint.h> - -/* main syscall handler */ -void syscall(va_list ap) { - uint8_t call; - char *s; - - printf("Call Number: 0x%02x\n", va_arg(ap, uint8_t)); - printf(va_arg(ap, char*)); -} diff --git a/kernel/traps.s b/kernel/traps.s index 7b7446b..ba9814c 100644 --- a/kernel/traps.s +++ b/kernel/traps.s @@ -5,9 +5,9 @@ global syscall_init global traps_init +extern call_table extern idt extern register_isr -extern syscall %macro SAVE 0 pusha @@ -20,6 +20,8 @@ extern syscall mov es, ax mov fs, ax mov gs, ax + ; restore EAX (needed for syscalls) + mov eax, [esp+44] %endmacro %macro SAVE_ERR 0 @@ -206,11 +208,14 @@ exc_reserved: syscall_handler: SAVE - ; pass the saved ESP value to the REAL handler - mov eax, [esp+46] - push eax - call syscall - add esp, 4 + push edx + push ecx + push ebx + and eax, 0xFF + shl eax, 2 + add eax, call_table + call [eax] + add esp, 12 RESTORE diff --git a/kernel/usrbin/Makefile b/kernel/usrbin/Makefile index dfd1b84..dd42909 100644 --- a/kernel/usrbin/Makefile +++ b/kernel/usrbin/Makefile @@ -1,17 +1,32 @@ -TARGET = usrbin.o +TARGET = usrbin_blob.o -ASM = nasm +SRCS = $(wildcard *.c) +ASMS = $(wildcard *.s) +OBJS = $(SRCS:.c=.o) $(ASMS:.s=.o) + +CFLAGS = -m32 -I../../include -ffreestanding -nostdinc -nostdlib -fno-stack-protector -fno-pie +LDFLAGS = -m elf_i386 -T link.ld +ASMFLAGS = -f elf32 + +CC = gcc $(CFLAGS) +LD = ld $(LDFLAGS) +ASM = nasm $(ASMFLAGS) all: build build: $(TARGET) -usrbin.bin: usrbin.s - $(ASM) -f bin -o usrbin.bin usrbin.s +.s.o: + $(ASM) -o $*.o $^ + +.c.o: + $(CC) -c -o $*.o $^ -$(TARGET): usrbin.bin - objcopy -I binary -O elf32-i386 -B i386 usrbin.bin usrbin.o +$(TARGET): $(OBJS) + $(LD) -o usrbin.bin $(OBJS) + objcopy -I binary -O elf32-i386 -B i386 usrbin.bin $(TARGET) clean: rm -f usrbin.bin + rm -f $(OBJS) rm -f $(TARGET) diff --git a/kernel/usrbin/lib.s b/kernel/usrbin/lib.s new file mode 100644 index 0000000..ffe4f5f --- /dev/null +++ b/kernel/usrbin/lib.s @@ -0,0 +1,10 @@ +bits 32 + +extern main + +section .entry +init: + call main +.loop: + ; loop forever + jmp .loop diff --git a/kernel/usrbin/link.ld b/kernel/usrbin/link.ld new file mode 100644 index 0000000..c1a7a42 --- /dev/null +++ b/kernel/usrbin/link.ld @@ -0,0 +1,8 @@ +OUTPUT_FORMAT(binary) +SECTIONS +{ + . = 0x100000; + .text : { *(.entry); .*(.text) } + .data : { *(.data) } + .bss : { *(.bss) } +} diff --git a/kernel/usrbin/main.c b/kernel/usrbin/main.c new file mode 100644 index 0000000..5cc7567 --- /dev/null +++ b/kernel/usrbin/main.c @@ -0,0 +1,7 @@ +#include <stdint.h> + +extern int print(char*); + +void main(void) { + print("We did it ma!\n"); +} diff --git a/kernel/usrbin/print.c b/kernel/usrbin/print.c new file mode 100644 index 0000000..ea32cdf --- /dev/null +++ b/kernel/usrbin/print.c @@ -0,0 +1,3 @@ +#include <unistd.h> + +_syscall1(int, print, char*, s); diff --git a/kernel/usrbin/usrbin.bin b/kernel/usrbin/usrbin.bin Binary files differdeleted file mode 100644 index bdf8c29..0000000 --- a/kernel/usrbin/usrbin.bin +++ /dev/null diff --git a/kernel/usrbin/usrbin.s b/kernel/usrbin/usrbin.s deleted file mode 100644 index 248d823..0000000 --- a/kernel/usrbin/usrbin.s +++ /dev/null @@ -1,12 +0,0 @@ -bits 32 -org 0x100000 - -main: - push .msg - push byte 0 - int 0x80 - add esp, 4 -.loop: - ; loop forever - jmp .loop -.msg: db "Hello World from Userspace, using syscalls!", 10, 0 |
