diff options
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | .vimrc | 14 | ||||
-rw-r--r-- | kernel/Makefile | 38 | ||||
-rwxr-xr-x | make_cdrom.sh | 2 | ||||
-rwxr-xr-x | run_bochs_debug.sh | 3 | ||||
-rwxr-xr-x | run_qemu.sh | 5 | ||||
-rwxr-xr-x | run_qemu_debug.sh | 7 | ||||
-rw-r--r-- | src/common/Makefile | 11 | ||||
-rw-r--r-- | src/common/README | 18 | ||||
-rw-r--r-- | src/common/buffer.c (renamed from kernel/lib/buffer.c) | 18 | ||||
-rw-r--r-- | src/common/common.lib | bin | 0 -> 41450 bytes | |||
-rw-r--r-- | src/common/hashtbl.c (renamed from kernel/lib/hashtbl.c) | 24 | ||||
-rw-r--r-- | src/common/include/buffer.h (renamed from kernel/include/buffer.h) | 1 | ||||
-rw-r--r-- | src/common/include/debug.h | 14 | ||||
-rw-r--r-- | src/common/include/hashtbl.h (renamed from kernel/include/hashtbl.h) | 0 | ||||
-rw-r--r-- | src/common/include/malloc.h | 11 | ||||
-rw-r--r-- | src/common/include/mutex.h (renamed from kernel/include/mutex.h) | 0 | ||||
-rw-r--r-- | src/common/include/printf.h (renamed from kernel/include/printf.h) | 0 | ||||
-rw-r--r-- | src/common/include/slab_alloc.h (renamed from kernel/include/slab_alloc.h) | 11 | ||||
-rw-r--r-- | src/common/include/string.h (renamed from kernel/include/string.h) | 0 | ||||
-rw-r--r-- | src/common/mutex.c (renamed from kernel/lib/mutex.c) | 0 | ||||
-rw-r--r-- | src/common/printf.c (renamed from kernel/lib/printf.c) | 0 | ||||
-rw-r--r-- | src/common/slab_alloc.c (renamed from kernel/lib/slab_alloc.c) | 0 | ||||
-rw-r--r-- | src/common/string.c (renamed from kernel/lib/string.c) | 0 | ||||
-rw-r--r-- | src/kernel/Makefile | 14 | ||||
-rw-r--r-- | src/kernel/config.h (renamed from kernel/config.h) | 0 | ||||
-rw-r--r-- | src/kernel/core/context_switch.s (renamed from kernel/l0/context_switch.s) | 0 | ||||
-rw-r--r-- | src/kernel/core/dbglog.c (renamed from kernel/l0/dbglog.c) | 0 | ||||
-rw-r--r-- | src/kernel/core/frame.c (renamed from kernel/l0/frame.c) | 0 | ||||
-rw-r--r-- | src/kernel/core/gdt.c (renamed from kernel/l0/gdt.c) | 0 | ||||
-rw-r--r-- | src/kernel/core/idt.c (renamed from kernel/l0/idt.c) | 0 | ||||
-rw-r--r-- | src/kernel/core/interrupt.s (renamed from kernel/l0/interrupt.s) | 0 | ||||
-rw-r--r-- | src/kernel/core/kmain.c (renamed from kernel/l0/kmain.c) | 9 | ||||
-rw-r--r-- | src/kernel/core/kmalloc.c (renamed from kernel/l0/kmalloc.c) | 14 | ||||
-rw-r--r-- | src/kernel/core/loader.s (renamed from kernel/l0/loader.s) | 0 | ||||
-rw-r--r-- | src/kernel/core/paging.c (renamed from kernel/l0/paging.c) | 8 | ||||
-rw-r--r-- | src/kernel/core/region.c (renamed from kernel/l0/region.c) | 0 | ||||
-rw-r--r-- | src/kernel/core/sys.c (renamed from kernel/l0/sys.c) | 0 | ||||
-rw-r--r-- | src/kernel/core/thread.c (renamed from kernel/l0/thread.c) | 8 | ||||
l--------- | src/kernel/include/config.h | 1 | ||||
-rw-r--r-- | src/kernel/include/dbglog.h (renamed from kernel/include/dbglog.h) | 3 | ||||
-rw-r--r-- | src/kernel/include/frame.h (renamed from kernel/include/frame.h) | 0 | ||||
-rw-r--r-- | src/kernel/include/gdt.h (renamed from kernel/include/gdt.h) | 0 | ||||
-rw-r--r-- | src/kernel/include/idt.h (renamed from kernel/include/idt.h) | 0 | ||||
-rw-r--r-- | src/kernel/include/kmalloc.h (renamed from kernel/include/kmalloc.h) | 3 | ||||
-rw-r--r-- | src/kernel/include/multiboot.h (renamed from kernel/include/multiboot.h) | 0 | ||||
-rw-r--r-- | src/kernel/include/paging.h (renamed from kernel/include/paging.h) | 0 | ||||
-rw-r--r-- | src/kernel/include/process.h | 43 | ||||
-rw-r--r-- | src/kernel/include/region.h (renamed from kernel/include/region.h) | 0 | ||||
-rw-r--r-- | src/kernel/include/sys.h (renamed from kernel/include/sys.h) | 6 | ||||
-rw-r--r-- | src/kernel/include/thread.h (renamed from kernel/include/thread.h) | 0 | ||||
-rw-r--r-- | src/kernel/linker.ld (renamed from kernel/linker.ld) | 0 | ||||
-rw-r--r-- | src/kernel/user/process.c | 30 | ||||
-rw-r--r-- | src/rules.make | 34 | ||||
-rw-r--r-- | src/tests/Makefile (renamed from kernel/lib_tests/Makefile) | 0 | ||||
-rw-r--r-- | src/tests/slab_test.c (renamed from kernel/lib_tests/slab_test.c) | 0 |
56 files changed, 254 insertions, 100 deletions
@@ -1,8 +1,11 @@ *~ *.swp +tags + *.o *.bin +*.lib *.map *.iso @@ -18,3 +21,4 @@ doc/html # bochs debugger bx_enh_dbg.ini + @@ -0,0 +1,14 @@ +autocmd VimEnter * NERDTree +autocmd VimEnter * wincmd p + +map <C-F12> :!ctags -R --sort=yes --c-kinds=+pl --fields=+iaS --extra=+q kernel<CR> + + +let g:syntastic_always_populate_loc_list = 1 +let g:syntastic_auto_loc_list = 1 +let g:syntastic_check_on_open = 1 +let g:syntastic_check_on_wq = 0 + +let g:syntastic_c_gcc_exec = 'i586-elf-gcc' +let g:syntastic_c_include_dirs = [ 'src/kernel/include', 'src/kernel', 'src/common/include' ] +let g:syntastic_c_compiler_options = '-ffreestanding -Wall -Wextra -std=gnu99 -Wno-unused-parameter' diff --git a/kernel/Makefile b/kernel/Makefile deleted file mode 100644 index 1764764..0000000 --- a/kernel/Makefile +++ /dev/null @@ -1,38 +0,0 @@ - -AS = nasm -ASFLAGS = -felf -g - -CC = i586-elf-gcc -CFLAGS = -ffreestanding -O2 -std=gnu99 -Wall -Wextra -I . -I ./include -g -Wno-unused-parameter -# CXX = i586-elf-g++ -# CXFLAGS = -ffreestanding -O3 -Wall -Wextra -I . -I ./include -fno-exceptions -fno-rtti -LD = i586-elf-gcc -LDFLAGS = -T linker.ld -ffreestanding -O2 -nostdlib -lgcc -Xlinker -Map=kernel.map - -OBJ = lib/string.o lib/printf.o lib/slab_alloc.o lib/mutex.o \ - lib/hashtbl.o lib/buffer.o\ - l0/loader.o l0/kmain.o l0/dbglog.o l0/sys.o \ - l0/gdt.o l0/idt.o l0/interrupt.o l0/context_switch.o l0/thread.o \ - l0/frame.o l0/paging.o l0/region.o l0/kmalloc.o -OUT = kernel.bin - -all: $(OUT) - -$(OUT): $(OBJ) - $(LD) $(LDFLAGS) -o $@ $^ - -%.o: %.s - $(AS) $(ASFLAGS) -o $@ $< - -%.o: %.c - $(CC) -c $< -o $@ $(CFLAGS) - -# %.o: %.cpp -# $(CXX) -c $< -o $@ $(CXFLAGS) - -clean: - rm */*.o || true -mrproper: clean - rm $(OUT) || true - -rebuild: mrproper all diff --git a/make_cdrom.sh b/make_cdrom.sh index ef9e5f7..82a3bd2 100755 --- a/make_cdrom.sh +++ b/make_cdrom.sh @@ -8,7 +8,7 @@ if [ ! -e cdrom/boot/grub/stage2_eltorito ]; then fi cp menu_cdrom.lst cdrom/boot/grub/menu.lst -cp kernel/kernel.bin cdrom; strip cdrom/kernel.bin +cp src/kernel/kernel.bin cdrom; strip cdrom/kernel.bin genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot \ -boot-load-size 4 -boot-info-table -input-charset ascii \ diff --git a/run_bochs_debug.sh b/run_bochs_debug.sh index df6c0d4..335b810 100755 --- a/run_bochs_debug.sh +++ b/run_bochs_debug.sh @@ -2,6 +2,7 @@ cd `dirname $0` -make -C kernel || exit 1 +make -C src/common || exit 1 +make -C src/kernel || exit 1 ./make_cdrom.sh bochs -f bochs_debug.cfg -q diff --git a/run_qemu.sh b/run_qemu.sh index 6c76909..59f1f19 100755 --- a/run_qemu.sh +++ b/run_qemu.sh @@ -2,5 +2,6 @@ cd `dirname $0` -make -C kernel || exit 1 -qemu-system-i386 -kernel kernel/kernel.bin -serial stdio -m 16 +make -C src/common || exit 1 +make -C src/kernel || exit 1 +qemu-system-i386 -kernel src/kernel/kernel.bin -serial stdio -m 16 diff --git a/run_qemu_debug.sh b/run_qemu_debug.sh index 71ee6d3..9e84613 100755 --- a/run_qemu_debug.sh +++ b/run_qemu_debug.sh @@ -2,6 +2,7 @@ cd `dirname $0` -make -C kernel || exit 1 -qemu-system-i386 -kernel kernel/kernel.bin -serial stdio -s -S & -(sleep 0.1; gdb kernel/kernel.bin -x gdb_cmd) +make -C src/common || exit 1 +make -C src/kernel || exit 1 +qemu-system-i386 -kernel src/kernel/kernel.bin -serial stdio -s -S & +(sleep 0.1; gdb src/kernel/kernel.bin -x gdb_cmd) diff --git a/src/common/Makefile b/src/common/Makefile new file mode 100644 index 0000000..d27c6d5 --- /dev/null +++ b/src/common/Makefile @@ -0,0 +1,11 @@ +OBJ = string.o printf.o slab_alloc.o mutex.o hashtbl.o buffer.o + +LIB = + +CFLAGS = -I ./include + +LDFLAGS = + +OUT = common.lib + +include ../rules.make diff --git a/src/common/README b/src/common/README new file mode 100644 index 0000000..deae76f --- /dev/null +++ b/src/common/README @@ -0,0 +1,18 @@ +This directory contains the library functions common to userland +and kernel code. + +It relies on a few functions being exported : + +- panic(char* msg, char* file, int line) +- panic_assert(char* assert, char* file, int line) +- dbg_print(const char* str) +- void* malloc(size_t size) +- free(void* ptr) + +These function are supposed to be defined in the code that calls +the common functions. The headers for these functions are to be +found in `assert.h` and `malloc.h`. + +Panic and panic_assert end the execution of the current program +(or of the kernel when in kernel-mode). + diff --git a/kernel/lib/buffer.c b/src/common/buffer.c index d2f9644..21f1ec4 100644 --- a/kernel/lib/buffer.c +++ b/src/common/buffer.c @@ -1,9 +1,9 @@ -#include <kmalloc.h> +#include <malloc.h> #include <buffer.h> #include <string.h> -#include <sys.h> +#include <debug.h> // three types of buffers #define T_BYTES 1 @@ -37,7 +37,7 @@ void buffer_unref(buffer_t *b) { if (b->rc == 0) { switch (b->type) { case T_BYTES: - if (b->bytes.owned) kfree((void*)b->bytes.data); + if (b->bytes.owned) free((void*)b->bytes.data); break; case T_SLICE: buffer_unref(b->slice.buf); @@ -49,7 +49,7 @@ void buffer_unref(buffer_t *b) { default: ASSERT(false); } - kfree(b); + free(b); } } @@ -92,7 +92,7 @@ size_t read_buffer(buffer_t *b, char* dest, size_t begin, size_t n) { // ========================= // buffer_t *buffer_from_bytes_nocopy(const char* data, size_t n, bool own_bytes) { - buffer_t *b = (buffer_t*)kmalloc(sizeof(buffer_t)); + buffer_t *b = (buffer_t*)malloc(sizeof(buffer_t)); if (b == 0) return 0; b->rc = 1; @@ -104,14 +104,14 @@ buffer_t *buffer_from_bytes_nocopy(const char* data, size_t n, bool own_bytes) { return b; } buffer_t *buffer_from_bytes(const char* data, size_t n) { - char* data2 = (char*)kmalloc(n); + char* data2 = (char*)malloc(n); if (data2 == 0) return 0; memcpy(data2, data, n); buffer_t *b = buffer_from_bytes_nocopy(data2, n, true); if (b == 0) { - kfree(data2); + free(data2); return 0; } @@ -122,7 +122,7 @@ buffer_t *buffer_from_bytes(const char* data, size_t n) { buffer_t* buffer_slice(buffer_t* src, size_t begin, size_t n) { if (begin + n > src->len) return 0; // invalid request - buffer_t *b = (buffer_t*)kmalloc(sizeof(buffer_t)); + buffer_t *b = (buffer_t*)malloc(sizeof(buffer_t)); if (b == 0) return 0; b->rc = 1; @@ -135,7 +135,7 @@ buffer_t* buffer_slice(buffer_t* src, size_t begin, size_t n) { } buffer_t* buffer_concat(buffer_t* a, buffer_t* b) { - buffer_t *r = (buffer_t*)kmalloc(sizeof(buffer_t)); + buffer_t *r = (buffer_t*)malloc(sizeof(buffer_t)); if (r == 0) return r; r->rc = 1; diff --git a/src/common/common.lib b/src/common/common.lib Binary files differnew file mode 100644 index 0000000..16259e6 --- /dev/null +++ b/src/common/common.lib diff --git a/kernel/lib/hashtbl.c b/src/common/hashtbl.c index 91fb3cb..47072b4 100644 --- a/kernel/lib/hashtbl.c +++ b/src/common/hashtbl.c @@ -1,5 +1,5 @@ #include <hashtbl.h> -#include <kmalloc.h> +#include <malloc.h> #include <string.h> #define DEFAULT_INIT_SIZE 16 @@ -21,7 +21,7 @@ struct hashtbl { }; hashtbl_t *create_hashtbl(key_eq_fun_t ef, hash_fun_t hf, size_t initial_size) { - hashtbl_t *ht = (hashtbl_t*)kmalloc(sizeof(hashtbl_t)); + hashtbl_t *ht = (hashtbl_t*)malloc(sizeof(hashtbl_t)); if (ht == 0) return 0; ht->ef = ef; @@ -30,9 +30,9 @@ hashtbl_t *create_hashtbl(key_eq_fun_t ef, hash_fun_t hf, size_t initial_size) { ht->size = (initial_size == 0 ? DEFAULT_INIT_SIZE : initial_size); ht->nitems = 0; - ht->items = (hashtbl_item_t**)kmalloc(ht->size * sizeof(hashtbl_item_t*)); + ht->items = (hashtbl_item_t**)malloc(ht->size * sizeof(hashtbl_item_t*)); if (ht->items == 0) { - kfree(ht); + free(ht); return 0; } @@ -46,14 +46,14 @@ void delete_hashtbl(hashtbl_t *ht) { for (size_t i = 0; i < ht->size; i++) { while (ht->items[i] != 0) { hashtbl_item_t *n = ht->items[i]->next; - kfree(ht->items[i]); + free(ht->items[i]); ht->items[i] = n; } } // Free table - kfree(ht->items); - kfree(ht); + free(ht->items); + free(ht); } static void hashtbl_check_size(hashtbl_t *ht) { @@ -62,7 +62,7 @@ static void hashtbl_check_size(hashtbl_t *ht) { if (4 * ht->nitems > 3 * ht->size) nsize = ht->size * 2; if (nsize != 0) { - hashtbl_item_t **nitems = (hashtbl_item_t**)kmalloc(nsize * sizeof(hashtbl_item_t*)); + hashtbl_item_t **nitems = (hashtbl_item_t**)malloc(nsize * sizeof(hashtbl_item_t*)); if (nitems == 0) return; // if we can't realloc, too bad, we just lose space for (size_t i = 0; i < nsize; i++) nitems[i] = 0; @@ -78,7 +78,7 @@ static void hashtbl_check_size(hashtbl_t *ht) { nitems[slot] = x; } } - kfree(ht->items); + free(ht->items); ht->size = nsize; ht->items = nitems; } @@ -87,7 +87,7 @@ static void hashtbl_check_size(hashtbl_t *ht) { int hashtbl_add(hashtbl_t *ht, void* key, void* v) { size_t slot = SLOT_OF_HASH(ht->hf(key), ht->size); - hashtbl_item_t *i = (hashtbl_item_t*)kmalloc(sizeof(hashtbl_item_t)); + hashtbl_item_t *i = (hashtbl_item_t*)malloc(sizeof(hashtbl_item_t)); if (i == 0) return 1; // OOM // make sure item is not already present @@ -122,14 +122,14 @@ void hashtbl_remove(hashtbl_t* ht, void* key) { if (ht->ef(ht->items[slot]->key, key)) { hashtbl_item_t *x = ht->items[slot]; ht->items[slot] = x->next; - kfree(x); + free(x); ht->nitems--; } else { for (hashtbl_item_t *i = ht->items[slot]; i->next != 0; i = i->next) { if (ht->ef(i->next->key, key)) { hashtbl_item_t *x = i->next; i->next = x->next; - kfree(x); + free(x); ht->nitems--; break; } diff --git a/kernel/include/buffer.h b/src/common/include/buffer.h index c47e2b4..0d6cfbf 100644 --- a/kernel/include/buffer.h +++ b/src/common/include/buffer.h @@ -2,6 +2,7 @@ #include <stdint.h> #include <stddef.h> +#include <stdbool.h> // The buffer_t type is a simple reference-counted buffer type // enabling the creation, sharing, slicing and concatenation of buffers diff --git a/src/common/include/debug.h b/src/common/include/debug.h new file mode 100644 index 0000000..a4b8b6f --- /dev/null +++ b/src/common/include/debug.h @@ -0,0 +1,14 @@ +#pragma once + +#include <stddef.h> +#include <stdint.h> + +void panic(const char* message, const char* file, int line); +void panic_assert(const char* assertion, const char* file, int line); +#define PANIC(s) panic(s, __FILE__, __LINE__); +#define ASSERT(s) { if (!(s)) panic_assert(#s, __FILE__, __LINE__); } + +void dbg_print(const char* str); +void dbg_printf(const char* format, ...); + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/kernel/include/hashtbl.h b/src/common/include/hashtbl.h index 16dfefb..16dfefb 100644 --- a/kernel/include/hashtbl.h +++ b/src/common/include/hashtbl.h diff --git a/src/common/include/malloc.h b/src/common/include/malloc.h new file mode 100644 index 0000000..0ba5572 --- /dev/null +++ b/src/common/include/malloc.h @@ -0,0 +1,11 @@ +#pragma once + +#include <stdint.h> +#include <stddef.h> + +// Header is in common/, but implementation is not. + +void* malloc(size_t sz); +void free(void* ptr); + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/kernel/include/mutex.h b/src/common/include/mutex.h index 6814adf..6814adf 100644 --- a/kernel/include/mutex.h +++ b/src/common/include/mutex.h diff --git a/kernel/include/printf.h b/src/common/include/printf.h index b4e1c1b..b4e1c1b 100644 --- a/kernel/include/printf.h +++ b/src/common/include/printf.h diff --git a/kernel/include/slab_alloc.h b/src/common/include/slab_alloc.h index eb9588d..c8d5d6c 100644 --- a/kernel/include/slab_alloc.h +++ b/src/common/include/slab_alloc.h @@ -8,18 +8,19 @@ #include <stddef.h> #include <stdbool.h> + #if defined(__linux__) //redefine necessary stuff -#include <assert.h> +#include <assert.h> // standard linux assert.h #define ASSERT assert -#define PAGE_SIZE 0x1000 -#include <stdio.h> +#include <stdio.h> #define dbg_printf printf #else -#include <sys.h> // this is macroscope -#include <dbglog.h> +#include <debug.h> #endif +#define PAGE_SIZE 0x1000 + // expected format for the array of slab_type_t given to slab_create : // an array of slab_type descriptors, with last descriptor full of zeroes // and with obj_size increasing (strictly) in the array diff --git a/kernel/include/string.h b/src/common/include/string.h index 682b25a..682b25a 100644 --- a/kernel/include/string.h +++ b/src/common/include/string.h diff --git a/kernel/lib/mutex.c b/src/common/mutex.c index cda8049..cda8049 100644 --- a/kernel/lib/mutex.c +++ b/src/common/mutex.c diff --git a/kernel/lib/printf.c b/src/common/printf.c index 68e08d8..68e08d8 100644 --- a/kernel/lib/printf.c +++ b/src/common/printf.c diff --git a/kernel/lib/slab_alloc.c b/src/common/slab_alloc.c index 714c49f..714c49f 100644 --- a/kernel/lib/slab_alloc.c +++ b/src/common/slab_alloc.c diff --git a/kernel/lib/string.c b/src/common/string.c index 9dce27b..9dce27b 100644 --- a/kernel/lib/string.c +++ b/src/common/string.c diff --git a/src/kernel/Makefile b/src/kernel/Makefile new file mode 100644 index 0000000..3fc7e9c --- /dev/null +++ b/src/kernel/Makefile @@ -0,0 +1,14 @@ + +OBJ = core/loader.o core/kmain.o core/dbglog.o core/sys.o \ + core/gdt.o core/idt.o core/interrupt.o core/context_switch.o core/thread.o \ + core/frame.o core/paging.o core/region.o core/kmalloc.o + +LIB = ../common/common.lib + +CFLAGS = -I ./include -I ../common/include + +LDFLAGS = -T linker.ld -Xlinker -Map=kernel.map + +OUT = kernel.bin + +include ../rules.make diff --git a/kernel/config.h b/src/kernel/config.h index a0e59d3..a0e59d3 100644 --- a/kernel/config.h +++ b/src/kernel/config.h diff --git a/kernel/l0/context_switch.s b/src/kernel/core/context_switch.s index 6738a03..6738a03 100644 --- a/kernel/l0/context_switch.s +++ b/src/kernel/core/context_switch.s diff --git a/kernel/l0/dbglog.c b/src/kernel/core/dbglog.c index e042625..e042625 100644 --- a/kernel/l0/dbglog.c +++ b/src/kernel/core/dbglog.c diff --git a/kernel/l0/frame.c b/src/kernel/core/frame.c index 489d010..489d010 100644 --- a/kernel/l0/frame.c +++ b/src/kernel/core/frame.c diff --git a/kernel/l0/gdt.c b/src/kernel/core/gdt.c index eadde5f..eadde5f 100644 --- a/kernel/l0/gdt.c +++ b/src/kernel/core/gdt.c diff --git a/kernel/l0/idt.c b/src/kernel/core/idt.c index 2f244e3..2f244e3 100644 --- a/kernel/l0/idt.c +++ b/src/kernel/core/idt.c diff --git a/kernel/l0/interrupt.s b/src/kernel/core/interrupt.s index d40fff0..d40fff0 100644 --- a/kernel/l0/interrupt.s +++ b/src/kernel/core/interrupt.s diff --git a/kernel/l0/kmain.c b/src/kernel/core/kmain.c index 826fa14..2169b7d 100644 --- a/kernel/l0/kmain.c +++ b/src/kernel/core/kmain.c @@ -2,6 +2,7 @@ #include <config.h> #include <dbglog.h> #include <sys.h> +#include <malloc.h> #include <gdt.h> #include <idt.h> @@ -81,10 +82,10 @@ void kmalloc_test(void* kernel_data_end) { dbg_print_region_info(); dbg_printf("Begin kmalloc test...\n"); const int m = 200; - uint16_t** ptr = kmalloc(m * sizeof(uint32_t)); + uint16_t** ptr = malloc(m * sizeof(uint32_t)); for (int i = 0; i < m; i++) { size_t s = 1 << ((i * 7) % 11 + 2); - ptr[i] = (uint16_t*)kmalloc(s); + ptr[i] = (uint16_t*)malloc(s); ASSERT((void*)ptr[i] >= kernel_data_end && (size_t)ptr[i] < 0xFFC00000); *ptr[i] = ((i * 211) % 1024); } @@ -94,9 +95,9 @@ void kmalloc_test(void* kernel_data_end) { for (int j = i; j < m; j++) { ASSERT(*ptr[j] == (j * 211) % 1024); } - kfree(ptr[i]); + free(ptr[i]); } - kfree(ptr); + free(ptr); dbg_printf("Kmalloc test OK.\n"); dbg_print_region_info(); } diff --git a/kernel/l0/kmalloc.c b/src/kernel/core/kmalloc.c index e356ada..e15572a 100644 --- a/kernel/l0/kmalloc.c +++ b/src/kernel/core/kmalloc.c @@ -27,7 +27,7 @@ static slab_type_t slab_sizes[] = { }; static mem_allocator_t *kernel_allocator = 0; -STATIC_MUTEX(kmalloc_mutex); +STATIC_MUTEX(malloc_mutex); void kmalloc_setup() { kernel_allocator = @@ -35,18 +35,18 @@ void kmalloc_setup() { region_free_unmap_free); } -void* kmalloc(size_t sz) { +void* malloc(size_t sz) { void* res = 0; - mutex_lock(&kmalloc_mutex); + mutex_lock(&malloc_mutex); res = slab_alloc(kernel_allocator, sz); - mutex_unlock(&kmalloc_mutex); + mutex_unlock(&malloc_mutex); return res; } -void kfree(void* ptr) { - mutex_lock(&kmalloc_mutex); +void free(void* ptr) { + mutex_lock(&malloc_mutex); slab_free(kernel_allocator, ptr); - mutex_unlock(&kmalloc_mutex); + mutex_unlock(&malloc_mutex); } diff --git a/kernel/l0/loader.s b/src/kernel/core/loader.s index 447d82d..447d82d 100644 --- a/kernel/l0/loader.s +++ b/src/kernel/core/loader.s diff --git a/kernel/l0/paging.c b/src/kernel/core/paging.c index 683abe6..e60ca53 100644 --- a/kernel/l0/paging.c +++ b/src/kernel/core/paging.c @@ -5,7 +5,7 @@ #include <region.h> #include <mutex.h> #include <thread.h> -#include <kmalloc.h> +#include <malloc.h> #define PAGE_OF_ADDR(x) (((size_t)x >> PAGE_SHIFT) % N_PAGES_IN_PT) #define PT_OF_ADDR(x) ((size_t)x >> (PAGE_SHIFT + PT_SHIFT)) @@ -233,7 +233,7 @@ pagedir_t *create_pagedir() { pd_phys = frame_alloc(1); if (pd_phys == 0) goto error; - pd = (pagedir_t*)kmalloc(sizeof(pagedir_t)); + pd = (pagedir_t*)malloc(sizeof(pagedir_t)); if (pd == 0) goto error; temp = region_alloc(PAGE_SIZE, 0, 0); @@ -263,7 +263,7 @@ pagedir_t *create_pagedir() { error: if (pd_phys != 0) frame_free(pd_phys, 1); - if (pd != 0) kfree(pd); + if (pd != 0) free(pd); if (temp != 0) region_free(temp); return 0; } @@ -290,7 +290,7 @@ void delete_pagedir(pagedir_t *pd) { ASSERT(pd_phys == (backup.page[N_PAGES_IN_PT-1] >> PTE_FRAME_SHIFT)); frame_free(pd_phys, 1); // free the pagedir_t structure - kfree(pd); + free(pd); return; } diff --git a/kernel/l0/region.c b/src/kernel/core/region.c index 3127048..3127048 100644 --- a/kernel/l0/region.c +++ b/src/kernel/core/region.c diff --git a/kernel/l0/sys.c b/src/kernel/core/sys.c index 2b77463..2b77463 100644 --- a/kernel/l0/sys.c +++ b/src/kernel/core/sys.c diff --git a/kernel/l0/thread.c b/src/kernel/core/thread.c index 0f5c209..7f0bb5b 100644 --- a/kernel/l0/thread.c +++ b/src/kernel/core/thread.c @@ -1,5 +1,5 @@ #include <thread.h> -#include <kmalloc.h> +#include <malloc.h> #include <dbglog.h> #include <idt.h> @@ -113,12 +113,12 @@ static void run_thread(void (*entry)(void*), void* data) { ASSERT(false); } thread_t *new_thread(entry_t entry, void* data) { - thread_t *t = (thread_t*)kmalloc(sizeof(thread_t)); + thread_t *t = (thread_t*)malloc(sizeof(thread_t)); if (t == 0) return 0; void* stack = region_alloc(KPROC_STACK_SIZE, "Stack", 0); if (stack == 0) { - kfree(t); + free(t); return 0; } @@ -126,7 +126,7 @@ thread_t *new_thread(entry_t entry, void* data) { uint32_t f = frame_alloc(1); if (f == 0) { region_free_unmap_free(stack); - kfree(t); + free(t); return 0; } pd_map_page(i, f, true); diff --git a/src/kernel/include/config.h b/src/kernel/include/config.h new file mode 120000 index 0000000..93307d9 --- /dev/null +++ b/src/kernel/include/config.h @@ -0,0 +1 @@ +../config.h
\ No newline at end of file diff --git a/kernel/include/dbglog.h b/src/kernel/include/dbglog.h index 2f670e8..8bf6962 100644 --- a/kernel/include/dbglog.h +++ b/src/kernel/include/dbglog.h @@ -1,9 +1,8 @@ #pragma once #include <config.h> +#include <debug.h> void dbglog_setup(); -void dbg_print(const char* str); -void dbg_printf(const char* format, ...); /* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/kernel/include/frame.h b/src/kernel/include/frame.h index 9ffafb3..9ffafb3 100644 --- a/kernel/include/frame.h +++ b/src/kernel/include/frame.h diff --git a/kernel/include/gdt.h b/src/kernel/include/gdt.h index a62d0db..a62d0db 100644 --- a/kernel/include/gdt.h +++ b/src/kernel/include/gdt.h diff --git a/kernel/include/idt.h b/src/kernel/include/idt.h index 8e84cea..8e84cea 100644 --- a/kernel/include/idt.h +++ b/src/kernel/include/idt.h diff --git a/kernel/include/kmalloc.h b/src/kernel/include/kmalloc.h index a409865..d4a9272 100644 --- a/kernel/include/kmalloc.h +++ b/src/kernel/include/kmalloc.h @@ -8,7 +8,4 @@ void kmalloc_setup(); -void* kmalloc(size_t sz); -void kfree(void* ptr); - /* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/kernel/include/multiboot.h b/src/kernel/include/multiboot.h index 581337a..581337a 100644 --- a/kernel/include/multiboot.h +++ b/src/kernel/include/multiboot.h diff --git a/kernel/include/paging.h b/src/kernel/include/paging.h index 44014a2..44014a2 100644 --- a/kernel/include/paging.h +++ b/src/kernel/include/paging.h diff --git a/src/kernel/include/process.h b/src/kernel/include/process.h new file mode 100644 index 0000000..00ed1d7 --- /dev/null +++ b/src/kernel/include/process.h @@ -0,0 +1,43 @@ +#pragma once + +// Things described in this file are essentially a public interface +// All implementation details are hidden in process.c + +#include <thread.h> + +#include <hashtbl.h> +#include <buffer.h> + +#define PW_NOT_WAITING 0 +#define PW_WAIT_ANY_MSG 1 +#define PW_WAIT_MSG_ON_CHAN 2 + +#define PROCESS_MAILBOX_SIZE 42 + +typedef int chan_id_t; + +typedef struct chan_pair { + chan_id_t fst, snd; +} chan_pair_t; + +typedef struct message { + buffer_t *data; + chan_id_t chan_id; +} message_t; + +struct process; +typedef struct process process_t; + +process_t *new_process(entry_t entry, void* data, chan_pair_t *give_chans); + +chan_pair_t new_chan(); // not used very often, but still usefull +chan_id_t unbox_chan(chan_id_t chan, chan_id_t subchan); +void detach_chan(chan_id_t chan); // chan ID is freed + +int send_message(chan_id_t chan, buffer_t *msg); // nonnull on error (recipient queue is full) + +size_t await_message(); // returns the size of the first message to come +size_t await_message_on_chan(chan_id_t chan); // returns the size of the first message to come +message_t get_message(); // gets the first message in the queue (or nothing when queue is empty) + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/kernel/include/region.h b/src/kernel/include/region.h index 1fef582..1fef582 100644 --- a/kernel/include/region.h +++ b/src/kernel/include/region.h diff --git a/kernel/include/sys.h b/src/kernel/include/sys.h index c8721ec..29735fa 100644 --- a/kernel/include/sys.h +++ b/src/kernel/include/sys.h @@ -1,5 +1,6 @@ #pragma once +#include <debug.h> // common header #include <config.h> static inline void outb(uint16_t port, uint8_t value) { @@ -26,11 +27,6 @@ static inline void invlpg(void* addr) { asm volatile("invlpg (%0)" : : "r"(addr) : "memory"); } -void panic(const char* message, const char* file, int line); -void panic_assert(const char* assertion, const char* file, int line); -#define PANIC(s) panic(s, __FILE__, __LINE__); -#define ASSERT(s) { if (!(s)) panic_assert(#s, __FILE__, __LINE__); } - #define BOCHS_BREAKPOINT asm volatile("xchg %bx, %bx") diff --git a/kernel/include/thread.h b/src/kernel/include/thread.h index 757ba00..757ba00 100644 --- a/kernel/include/thread.h +++ b/src/kernel/include/thread.h diff --git a/kernel/linker.ld b/src/kernel/linker.ld index 1203950..1203950 100644 --- a/kernel/linker.ld +++ b/src/kernel/linker.ld diff --git a/src/kernel/user/process.c b/src/kernel/user/process.c new file mode 100644 index 0000000..7519dae --- /dev/null +++ b/src/kernel/user/process.c @@ -0,0 +1,30 @@ +#include <mutex.h> +#include <process.h> + +typedef struct process { + thread_t *thread; + int pid; + + mutex_t com_mutex; + + hashtbl_t *chans; + chan_id_t next_chan_id; + + message_t mbox[PROCESS_MAILBOX_SIZE]; + int mbox_size; // number of messages in queue + int mbox_first; // first message in queue (circular buffer) + + // a process can be in several waiting states : + // - wait for any message + // - wait for a message on a particular channel + // in this case, if a message is pushed on this particular channel, + // then it is put in front of the queue, so that it is the next message read + // (it is guaranteed that doing await_message_on_chan() immediately followed by get_message() + // gets the message whose size was returned by await_...) + int wait_state; + chan_id_t wait_chan_id; // when waiting for a message on a particular channel +} process_t; + +int push_message(process_t *proc, message_t msg); // nonnull on error (process queue is full) + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/rules.make b/src/rules.make new file mode 100644 index 0000000..e3840dd --- /dev/null +++ b/src/rules.make @@ -0,0 +1,34 @@ + +AS = nasm +ASFLAGS = -felf -g + +CC = i586-elf-gcc +CFLAGS += -ffreestanding -O2 -std=gnu99 -Wall -Wextra -I . -I ./include -g -Wno-unused-parameter +# CXX = i586-elf-g++ +# CXFLAGS = -ffreestanding -O3 -Wall -Wextra -I . -I ./include -fno-exceptions -fno-rtti +LD = i586-elf-gcc +LDFLAGS += -ffreestanding -O2 -nostdlib -lgcc + +all: $(OUT) + +%.bin: $(OBJ) + $(LD) $(LDFLAGS) -o $@ $^ $(LIB) + +%.lib: $(OBJ) + $(LD) $(LDFLAGS) -r -o $@ $^ $(LIB) + +%.o: %.s + $(AS) $(ASFLAGS) -o $@ $< + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAGS) + +# %.o: %.cpp +# $(CXX) -c $< -o $@ $(CXFLAGS) + +clean: + rm */*.o || true +mrproper: clean + rm $(OUT) || true + +rebuild: mrproper all diff --git a/kernel/lib_tests/Makefile b/src/tests/Makefile index 4a21edf..4a21edf 100644 --- a/kernel/lib_tests/Makefile +++ b/src/tests/Makefile diff --git a/kernel/lib_tests/slab_test.c b/src/tests/slab_test.c index 747c785..747c785 100644 --- a/kernel/lib_tests/slab_test.c +++ b/src/tests/slab_test.c |