diff options
author | Anton Kling <anton@kling.gg> | 2024-02-22 17:28:08 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-02-22 17:28:55 +0100 |
commit | a18da25e7355979d0f26cfd39dc0032172e8b135 (patch) | |
tree | 2af66bedd0dff4bbccffc8d82cd06836d7f68e0d | |
parent | 9b475d3db3275d4c34f02161ae70ced5595a0fdb (diff) |
Kernel: Fix undefined behavior and cleanup functions.
The int_syscall caused crashes for higher level of optimizations. To fix
this I rewrote the function in assembly as I deemed it to be simpler.
-rw-r--r-- | kernel/Makefile | 5 | ||||
-rw-r--r-- | kernel/cpu/idt.c | 1 | ||||
-rw-r--r-- | kernel/cpu/int_syscall.s | 35 | ||||
-rw-r--r-- | kernel/cpu/isr.s | 16 | ||||
-rw-r--r-- | kernel/cpu/syscall.c | 7 | ||||
-rw-r--r-- | kernel/drivers/mouse.c | 1 | ||||
-rw-r--r-- | kernel/drivers/pit.c | 4 | ||||
-rw-r--r-- | kernel/ipc.c | 2 | ||||
-rw-r--r-- | kernel/sched/scheduler.c | 2 | ||||
-rwxr-xr-x | meta/run.sh | 2 | ||||
-rw-r--r-- | userland/libc/Makefile | 3 | ||||
-rw-r--r-- | userland/windowserver/Makefile | 5 | ||||
-rw-r--r-- | userland/windowserver/ws.c | 4 |
13 files changed, 60 insertions, 27 deletions
diff --git a/kernel/Makefile b/kernel/Makefile index 7bdb91b..8f2cd3d 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,7 +1,8 @@ CC="i686-sb-gcc" AS="i686-sb-as" -OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o ipc.o syscalls/ipc.o syscalls/port.o syscalls/map_frames.o syscalls/virtual_to_physical.o syscalls/install_irq.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o cpu/arch_inst.o -CFLAGS = -std=c99 -Ofast -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL +OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o ipc.o syscalls/ipc.o syscalls/port.o syscalls/map_frames.o syscalls/virtual_to_physical.o syscalls/install_irq.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o cpu/arch_inst.o cpu/int_syscall.o +#CFLAGS = -std=c99 -Ofast -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL +CFLAGS = -std=c99 -Ofast -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL #CFLAGS = -Ofast -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL #CFLAGS = -O0 -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL LDFLAGS=-flto -Ofast diff --git a/kernel/cpu/idt.c b/kernel/cpu/idt.c index ec7ca63..805eb20 100644 --- a/kernel/cpu/idt.c +++ b/kernel/cpu/idt.c @@ -249,7 +249,6 @@ void install_handler(void (*handler_function)(), u16 type_attribute, u8 entry) { } void idt_init(void) { - // list_of_handlers = kcalloc(sizeof(void *), 128); memset(list_of_handlers, 0, sizeof(void *) * 256); install_handler(page_fault, INT_32_INTERRUPT_GATE(0x0), 0xE); diff --git a/kernel/cpu/int_syscall.s b/kernel/cpu/int_syscall.s new file mode 100644 index 0000000..b2c0de9 --- /dev/null +++ b/kernel/cpu/int_syscall.s @@ -0,0 +1,35 @@ +.intel_syntax noprefix +.global int_syscall +.extern syscall_functions +int_syscall: +push ebp +mov ebp,esp +push edi +push esi +push ebx + +mov edx,DWORD PTR [ebp+0x8] # reg_t* +mov eax,DWORD PTR [edx+0x20] # syscall number +mov eax,DWORD PTR [eax*4+syscall_functions] # function pointer + +mov edi,DWORD PTR [edx+0x4] +push edi +mov edi,DWORD PTR [edx+0x8] +push edi +mov edi,DWORD PTR [edx+0x18] +push edi +mov edi,DWORD PTR [edx+0x1c] +push edi +mov edi,DWORD PTR [edx+0x14] +push edi +call eax +add esp,0x20 +mov edx,DWORD PTR [ebp+0x8] # reg_t* +mov DWORD PTR [edx+0x20],eax +lea esp,[ebp-0xc] + +pop ebx +pop esi +pop edi +pop ebp +ret diff --git a/kernel/cpu/isr.s b/kernel/cpu/isr.s index 1bb2fff..8032485 100644 --- a/kernel/cpu/isr.s +++ b/kernel/cpu/isr.s @@ -270,12 +270,12 @@ ISR_NOERRCODE 254 ISR_NOERRCODE 255 isr_common_stub: - pusha # Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax + pusha # Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax - mov ax, ds # Lower 16-bits of eax = ds. - push eax # save the data segment descriptor + mov ax, ds # Lower 16-bits of eax = ds. + push eax # save the data segment descriptor - mov ax, 0x10 # load the kernel data segment descriptor + mov ax, 0x10 # load the kernel data segment descriptor mov ds, ax mov es, ax mov fs, ax @@ -285,13 +285,13 @@ isr_common_stub: call int_handler add esp, 4 - pop ebx # reload the original data segment descriptor + pop ebx # reload the original data segment descriptor mov ds, bx mov es, bx mov fs, bx mov gs, bx - popa # Pops edi,esi,ebp... - add esp, 8 # Cleans up the pushed error code and pushed ISR number + popa # Pops edi,esi,ebp... + add esp, 8 # Cleans up the pushed error code and pushed ISR number sti - iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP + iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c index e2ea7e3..193367e 100644 --- a/kernel/cpu/syscall.c +++ b/kernel/cpu/syscall.c @@ -182,18 +182,13 @@ int (*syscall_functions[])() = { (void(*))syscall_tmp_handle_packet, }; +void int_syscall(reg_t *r); void syscall_function_handler(u32 eax, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 arg5, u32 ebp, u32 esp) { assert(eax < sizeof(syscall_functions) / sizeof(syscall_functions[0])); syscall_functions[eax](arg1, arg2, arg3, arg4, arg5); } -void int_syscall(reg_t *r) { - u32 syscall = r->eax; - assert(syscall < sizeof(syscall_functions) / sizeof(syscall_functions[0])); - r->eax = syscall_functions[syscall](r->ebx, r->ecx, r->edx, r->esi, r->edi); -} - void syscalls_init(void) { install_handler(int_syscall, INT_32_INTERRUPT_GATE(0x3), 0x80); } diff --git a/kernel/drivers/mouse.c b/kernel/drivers/mouse.c index ec7ce8c..af4f22e 100644 --- a/kernel/drivers/mouse.c +++ b/kernel/drivers/mouse.c @@ -142,4 +142,5 @@ void install_mouse(void) { install_handler(int_mouse, INT_32_INTERRUPT_GATE(0x3), 12 + 0x20); install_handler(what, INT_32_INTERRUPT_GATE(0x3), 0xe + 0x20); + install_handler(what, INT_32_INTERRUPT_GATE(0x3), 0xf + 0x20); } diff --git a/kernel/drivers/pit.c b/kernel/drivers/pit.c index 060fc54..30fd3ed 100644 --- a/kernel/drivers/pit.c +++ b/kernel/drivers/pit.c @@ -43,8 +43,8 @@ void set_pit_count(u16 _hertz) { outb(PIT_IO_CHANNEL_0, (divisor & 0xFF00) >> 8); } -void int_clock(reg_t regs) { - outb(0x20, 0x20); +void int_clock(reg_t *regs) { + EOI(0x20); pit_counter++; if (pit_counter * 1000 >= hertz) { pit_counter = 0; diff --git a/kernel/ipc.c b/kernel/ipc.c index 56ca79a..bce346a 100644 --- a/kernel/ipc.c +++ b/kernel/ipc.c @@ -58,7 +58,7 @@ int ipc_read(u8 *buffer, u32 length, u32 *sender_pid) { return 0; } get_current_task()->is_halted = 1; - enable_interrupts(); + switch_task(); continue; } break; diff --git a/kernel/sched/scheduler.c b/kernel/sched/scheduler.c index 614f621..e34e7a7 100644 --- a/kernel/sched/scheduler.c +++ b/kernel/sched/scheduler.c @@ -325,7 +325,7 @@ int fork(void) { } int isset_fdhalt(process_t *p) { - if(NULL == p) { + if (NULL == p) { p = get_current_task(); } int blocked = 0; diff --git a/meta/run.sh b/meta/run.sh index 9231f14..01ec924 100755 --- a/meta/run.sh +++ b/meta/run.sh @@ -3,7 +3,7 @@ scriptdir="$(dirname "$0")" cd "$scriptdir" cd .. #qemu-system-i386 -netdev user,id=n0,hostfwd=tcp:127.0.0.1:6001-:6000 -device rtl8139,netdev=n0 -object filter-dump,id=id,netdev=n0,file=./logs/netout -d int -no-reboot -no-shutdown -chardev stdio,id=char0,logfile=./logs/serial.log,signal=off -serial chardev:char0 -drive id=disk,file=./meta/ext2.img,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -m 512M -cdrom ./kernel/myos.iso -s -qemu-system-i386 -netdev user,id=n0,hostfwd=tcp:127.0.0.1:6001-:6000 -device rtl8139,netdev=n0 -object filter-dump,id=id,netdev=n0,file=./logs/netout -no-reboot -no-shutdown -chardev stdio,id=char0,logfile=./logs/serial.log,signal=off -serial chardev:char0 -drive id=disk,file=./meta/ext2.img,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -m 512M -cdrom ./kernel/myos.iso -s +qemu-system-i386 -d int -netdev user,id=n0,hostfwd=tcp:127.0.0.1:6001-:6000 -device rtl8139,netdev=n0 -object filter-dump,id=id,netdev=n0,file=./logs/netout -no-reboot -no-shutdown -chardev stdio,id=char0,logfile=./logs/serial.log,signal=off -serial chardev:char0 -drive id=disk,file=./meta/ext2.img,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -m 512M -cdrom ./kernel/myos.iso -s # Sync the sysroot cd ./meta/ mkdir ./mount diff --git a/userland/libc/Makefile b/userland/libc/Makefile index 0c426b4..62d4973 100644 --- a/userland/libc/Makefile +++ b/userland/libc/Makefile @@ -1,7 +1,8 @@ CC="i686-sb-gcc" AR="i686-sb-ar" AS="i686-sb-as" -CFLAGS = -ggdb -ffreestanding -O2 -Wall -Wextra -pedantic -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter -Wno-return-type +#CFLAGS = -ggdb -ffreestanding -O2 -Wall -Wextra -pedantic -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter -Wno-return-type +CFLAGS = -ggdb -ffreestanding -Ofast -Wall -Wextra -pedantic -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter -Wno-return-type OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o systemcall.o sys/random/randomfill.o fcntl/open.o unistd/write.o unistd/pwrite.o all: libc.a diff --git a/userland/windowserver/Makefile b/userland/windowserver/Makefile index 7f80f2e..0adbdaa 100644 --- a/userland/windowserver/Makefile +++ b/userland/windowserver/Makefile @@ -1,5 +1,6 @@ CC="i686-sb-gcc" -CFLAGS = -ggdb -ffreestanding -O2 -Wall -Wextra -pedantic -mgeneral-regs-only -Wimplicit-fallthrough +#CFLAGS = -ggdb -ffreestanding -O2 -Wall -Wextra -pedantic -mgeneral-regs-only -Wimplicit-fallthrough +CFLAGS = -ggdb -ffreestanding -Ofast -Wall -Wextra -pedantic -mgeneral-regs-only -Wimplicit-fallthrough BIN=ws LIB=-lgcc INC=-I../libgui/ @@ -13,4 +14,4 @@ clean: rm $(OBJ) ws $(BIN): $(OBJ) - $(CC) -o $(BIN) $(CFLAGS) $(OBJ) $(LIB) + $(CC) -flto -o $(BIN) $(CFLAGS) $(OBJ) $(LIB) diff --git a/userland/windowserver/ws.c b/userland/windowserver/ws.c index a438566..a4f5a59 100644 --- a/userland/windowserver/ws.c +++ b/userland/windowserver/ws.c @@ -333,8 +333,8 @@ void parse_mouse_event(int fd) { x |= 0xFF00; if (ys) y |= 0xFF00; - xc += *(int16_t *)&x; - yc += *(int16_t *)&y; + xc += x; + yc += y; } } mouse_x += xc; |