diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | apps/Makefile | 2 | ||||
-rw-r--r-- | kernel/Makefile | 2 | ||||
-rw-r--r-- | kernel/drivers/interrupts.c | 9 | ||||
-rw-r--r-- | kernel/features/load.c | 1 | ||||
-rw-r--r-- | kernel/features/proc.c | 2 | ||||
-rw-r--r-- | kernel/inc/proc.h | 1 | ||||
-rw-r--r-- | kernel/main.c | 8 | ||||
-rw-r--r-- | libc/mem.c | 11 | ||||
-rwxr-xr-x | run | 42 |
10 files changed, 50 insertions, 30 deletions
@@ -6,7 +6,7 @@ CFLAGS_DEFAULT = $(CFLAGS_WARNINGS) $(CFLAGS_OPTIMIZATION) -std=c99 -m32 -nostdl all: compile -debug: DEBUG = -ggdb3 -s +debug: CFLAGS_DEFAULT += -ggdb3 -s debug: compile export diff --git a/apps/Makefile b/apps/Makefile index 6b54d17..8426b9c 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -14,7 +14,7 @@ all: $(COBJS) @$(CC) -c $(CFLAGS) $< -o $@ @$(LD) -o $(@:.o=.elf) -Tlink.ld -L../build/ $@ -lgui -ltxt -lnet -lc @$(OC) -O binary $(@:.o=.elf) ../build/apps/$(@:.o=) -# @cp $(@:.o=.elf) ../build/apps/$(@:.o=.dbg) + @cp $(@:.o=.elf) ../build/apps/$(@:.o=.elf) # %.o: %.c # @mkdir -p ../build/apps/ diff --git a/kernel/Makefile b/kernel/Makefile index f629b3d..5e5b752 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -20,7 +20,7 @@ LD = ccache ../cross/opt/bin/i686-elf-ld OC = ccache ../cross/opt/bin/i686-elf-objcopy AS = ccache nasm -CFLAGS = $(CFLAGS_DEFAULT) -Wno-address-of-packed-member -ffreestanding -Wl,-ekernel_main -I../libc/inc/ -Iinc/ -Dkernel $(DEBUG) +CFLAGS = $(CFLAGS_DEFAULT) -Wno-address-of-packed-member -ffreestanding -Wl,-ekernel_main -I../libc/inc/ -Iinc/ -Dkernel ASFLAGS = -f elf32 all: compile diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c index 268bff5..fa455d7 100644 --- a/kernel/drivers/interrupts.c +++ b/kernel/drivers/interrupts.c @@ -173,12 +173,15 @@ void isr_handler(struct regs *r); void isr_handler(struct regs *r) { if (r->int_no <= 32) { - printf("%s Exception, exiting!\n", isr_exceptions[r->int_no]); struct proc *proc = proc_current(); - if (proc) + printf("%s Exception at 0x%x, exiting!\n", isr_exceptions[r->int_no], r->eip); + if (proc) { + printf("\t-> Exception occurred in %s at addr 0x%x\n", proc->name, + r->eip - proc->entry); proc_exit(proc, 1); - else + } else { __asm__ volatile("cli\nhlt"); + } proc_yield(r); } else { // Execute fault handler if exists diff --git a/kernel/features/load.c b/kernel/features/load.c index f79e6b4..9024c82 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -12,6 +12,7 @@ void proc_load(struct proc *proc, void *data) proc->regs.ebp = (u32)stack; proc->regs.useresp = (u32)stack; proc->regs.eip = (u32)data; + proc->entry = (u32)data; } int bin_load(const char *path, struct proc *proc) diff --git a/kernel/features/proc.c b/kernel/features/proc.c index 48bde72..35c0fa7 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -162,6 +162,8 @@ void proc_yield(struct regs *r) void proc_enable_waiting(u32 id, enum proc_wait_type type) { struct proc *proc_bak = proc_current(); + if (!proc_bak) + return; struct node *iterator = proc_list->head; while (iterator) { diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h index a232f46..272a3ac 100644 --- a/kernel/inc/proc.h +++ b/kernel/inc/proc.h @@ -46,6 +46,7 @@ struct stream { struct proc { u32 pid; + u32 entry; u8 super; char name[32]; struct stream streams[4]; diff --git a/kernel/main.c b/kernel/main.c index 5e236a7..2112541 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -21,15 +21,15 @@ struct vid_info *boot_passed; void kernel_main(struct vid_info *vid_info); // Decl void kernel_main(struct vid_info *vid_info) { - heap_init(0x00f00000 + rand()); - - boot_passed = vid_info; - // Serial connection serial_install(); serial_print("\nKernel was compiled at " __TIME__ " on " __DATE__ "\n"); serial_print("Serial connected.\n"); + heap_init(0x00f00000 + rand()); + + boot_passed = vid_info; + cpu_enable_features(); cpu_print(); @@ -121,14 +121,15 @@ int mememp(const u8 *buf, u32 n) /** * Heap allocator + * Inspired by SHMALL (MIT License) + * Copyright (c) 2017 Chris Careaga + * Copyright (c) 2021 Marvin Borner */ #ifdef kernel -int malloc_allocated = 0; - -#define HEAP_MAGIC 0x42424242 -#define HEAP_INIT_SIZE 0xff000000 +#define HEAP_MAGIC 0x424242 +#define HEAP_INIT_SIZE 0xf000000 #define HEAP_MIN_SIZE HEAP_INIT_SIZE #define MIN_ALLOC_SZ 4 #define BIN_COUNT 9 @@ -292,7 +293,6 @@ void heap_init(u32 start) static void *_malloc(u32 size) { - malloc_allocated += size; u32 index = bin_index(size); struct h_bin *temp = (struct h_bin *)&heap.bins[index]; struct h_node *found = node_best_fit(temp, size); @@ -348,7 +348,6 @@ static void _free(void *p) struct h_node *head = (struct h_node *)((char *)p - 12); assert(head->magic == HEAP_MAGIC && head->hole == 0); - malloc_allocated -= head->size; if (head == (struct h_node *)(u32 *)heap.start) { head->hole = 1; node_add(&heap.bins[bin_index(head->size)], head); @@ -22,10 +22,11 @@ fi mode="${1}" no_ask="${2}" -# TODO: Support q35 chipset ('-machine q35') - loops in ide_wait +# TODO: Support q35 chipset ('-machine q35') +# TODO: Support -enable-kvm: GPF due to some malloc bug?! qemu_with_flags() { network="rtl8139" - qemu-system-i386 -cpu max -no-reboot -vga std -rtc base=localtime -m 256M -smp 4 -netdev user,id=net0,hostfwd=tcp:127.0.0.1:8000-10.0.2.15:8000 -device $network,netdev=net0 -object filter-dump,id=dump,netdev=net0,file=dump.pcap "$@" + qemu-system-i386 -d guest_errors -cpu max -no-reboot -vga std -rtc base=localtime -m 256M -netdev user,id=net0,hostfwd=tcp:127.0.0.1:8000-10.0.2.15:8000 -device $network,netdev=net0 -object filter-dump,id=dump,netdev=net0,file=dump.pcap "$@" } make_cross() { @@ -110,7 +111,7 @@ make_build() { rm -rf build/* printf "\nBuilding...\n" - if [ "$mode" = "debug" ]; then + if [ "$mode" = "debug" ] || [ "$MELVIX_DEBUG" = "1" ]; then $MAKE -j $($NPROC) debug else $MAKE -j $($NPROC) @@ -171,10 +172,23 @@ make_debug() { } make_disasm() { - objdump -drwC -Mintel build/kernel.elf | less -R + if [ -z "$1" ]; then + echo "Usage: './run disasm {kernel, apps/wm, ...} [-S]'" + exit 1 + fi + objdump -drwC "$2" -Mintel build/"$1".elf | less -R #hexdump -C build/kernel.bin | less -R } +make_addr() { + printf "Info: Make sure that you've turned the debug build on (e.g. with MELVIX_DEBUG=1)\n\n" + if [ -z "$2" ]; then + echo "Usage: './run addr kernel 0x50042'" + exit 1 + fi + addr2line -e build/"$1".elf "$2" +} + make_append_commands() { s="" while read -r data; do @@ -207,7 +221,7 @@ make_sync() { } make_clean() { - rm -rf build/ + #rm -rf build/ $MAKE clean } @@ -222,11 +236,9 @@ elif [ "${mode}" = "clean" ]; then elif [ "${mode}" = "again" ]; then make_test elif [ "${mode}" = "disasm" ]; then - make_cross - make_clean - make_build - make_disasm - make_clean + make_disasm "$2" "$3" +elif [ "${mode}" = "addr" ]; then + make_addr "$2" "$3" elif [ "${mode}" = "sync" ]; then make_sync elif [ "${mode}" = "disk" ]; then @@ -245,7 +257,7 @@ elif [ "${mode}" = "test" ] || [ "${mode}" = "" ]; then make_test make_clean else - echo "Usage: ./run {cross | clean | build | test | debug | again | disasm | sync | disk} [-y]" + echo "Usage: ./run {cross | clean | build | test | debug | again | disasm | addr | sync | disk} [-y]" printf "\nDescription of options:\n" printf "cross\t\tBuilds the cross compiler\n" printf "clean\t\tRemoves the compiled files\n" @@ -253,9 +265,11 @@ else printf "test\t\tRuns the Melvix unit tests with QEMU (cross+clean+build)\n" printf "debug\t\tEmulates Melvix with QEMU and debug options (cross+clean+build)\n" printf "again\t\tOpens QEMU again using the previous build\n" - printf "disasm\t\tDisassembles the main kernel binary\n" + printf "disasm\t\tDisassembles a given part of Melvix\n" + printf "addr\t\tResolves an address to a line of code\n" printf "sync\t\tSyncs the 'tags' and 'compile_commands.json' file\n" printf "disk\t\tPrepares the userspace disk (e.g. fonts)\n" - printf "nothing\t\tWhen no option is set, Melvix gets built and emulated using QEMU (cross+clean+build)\n" - printf "*\t\tAnything else prints this help\n\n" + printf "*\t\tAnything else prints this help\n" + printf "\t\tWhen no option is set, Melvix gets built and emulated using QEMU (cross+clean+build)\n\n" + echo "Set the environment variable 'MELVIX_DEBUG=1' for persistent debug builds" fi |