diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common.make | 2 | ||||
-rw-r--r-- | src/common/Makefile | 8 | ||||
-rw-r--r-- | src/common/include/stdlib.h (renamed from src/kernel/lib/stdlib.h) | 11 | ||||
-rw-r--r-- | src/common/include/string.h (renamed from src/include/string.h) | 10 | ||||
-rw-r--r-- | src/common/include/types.h (renamed from src/kernel/lib/types.h) | 2 | ||||
-rw-r--r-- | src/common/string.c (renamed from src/library/std/string.c) | 5 | ||||
-rw-r--r-- | src/include/gc/shm.h | 16 | ||||
-rw-r--r-- | src/include/gc/syscall.h | 43 | ||||
-rw-r--r-- | src/include/stdlib.h | 12 | ||||
-rw-r--r-- | src/kernel/Makefile | 9 | ||||
-rw-r--r-- | src/kernel/config.h | 8 | ||||
-rw-r--r-- | src/kernel/core/kmain.c | 10 | ||||
-rw-r--r-- | src/kernel/core/loader_.asm | 8 | ||||
-rw-r--r-- | src/kernel/core/monitor.c | 4 | ||||
-rw-r--r-- | src/kernel/core/sys.c | 4 | ||||
-rw-r--r-- | src/kernel/ipc/shm.c | 100 | ||||
-rw-r--r-- | src/kernel/ipc/shm.h | 22 | ||||
-rw-r--r-- | src/kernel/lib/stdlib.c | 39 | ||||
-rw-r--r-- | src/kernel/link.ld | 12 | ||||
-rw-r--r-- | src/kernel/mem/heap.basic.h | 2 | ||||
-rw-r--r-- | src/kernel/mem/heap.std.c | 6 | ||||
-rw-r--r-- | src/kernel/mem/heap.std.h | 2 | ||||
-rw-r--r-- | src/kernel/mem/mem.c | 4 | ||||
-rw-r--r-- | src/kernel/mem/mem.h | 4 | ||||
-rw-r--r-- | src/kernel/mem/paging.c | 24 | ||||
-rw-r--r-- | src/kernel/task/syscall.c | 4 | ||||
-rw-r--r-- | src/kernel/task/task.c | 6 | ||||
-rw-r--r-- | src/library/Makefile | 12 | ||||
-rw-r--r-- | src/library/gc/shm.c | 100 | ||||
-rw-r--r-- | src/modules/test/Makefile | 8 | ||||
-rw-r--r-- | src/user/app_common.make | 6 | ||||
-rw-r--r-- | src/user/lib/Makefile | 12 | ||||
-rw-r--r-- | src/user/lib/include/mutex.h (renamed from src/include/mutex.h) | 2 | ||||
-rw-r--r-- | src/user/lib/include/tce/mem.h (renamed from src/include/gc/mem.h) | 2 | ||||
-rw-r--r-- | src/user/lib/include/tce/syscall.h | 21 | ||||
-rw-r--r-- | src/user/lib/start.c (renamed from src/library/start.c) | 2 | ||||
-rw-r--r-- | src/user/lib/std/mutex.c (renamed from src/library/std/mutex.c) | 0 | ||||
-rw-r--r-- | src/user/lib/std/stdio.c (renamed from src/library/std/stdio.c) | 0 | ||||
-rw-r--r-- | src/user/lib/tce/mem.c (renamed from src/library/gc/mem.c) | 2 | ||||
-rw-r--r-- | src/user/lib/tce/syscall.c (renamed from src/library/gc/syscall.c) | 4 | ||||
-rw-r--r-- | src/user/link.ld (renamed from src/library/link.ld) | 1 | ||||
-rw-r--r-- | src/user/test/Makefile | 6 | ||||
-rw-r--r-- | src/user/test/main.c (renamed from src/modules/test/main.c) | 8 |
43 files changed, 138 insertions, 425 deletions
diff --git a/src/common.make b/src/common.make index 6aa6e4b..438ff07 100644 --- a/src/common.make +++ b/src/common.make @@ -17,7 +17,7 @@ all: $(Out) $(Out): $(Obj) echo ""; echo "- Linking $@..." - $(LD) $(LDFLAGS) $^ -o $@ + $(LD) $(LDFLAGS) $^ -o $@ $(ExtObj) # ============== GENERAL CLEAINING PROCEDURES diff --git a/src/common/Makefile b/src/common/Makefile new file mode 100644 index 0000000..ef0f6b1 --- /dev/null +++ b/src/common/Makefile @@ -0,0 +1,8 @@ +Out = _common.o +Obj = string.o +ExtObj = + +include $(SrcPath)/common.make + +LDFLAGS += -r +CFLAGS += -I $(SrcPath)/common/include diff --git a/src/kernel/lib/stdlib.h b/src/common/include/stdlib.h index 319cf26..2c5e6dd 100644 --- a/src/kernel/lib/stdlib.h +++ b/src/common/include/stdlib.h @@ -1,15 +1,14 @@ -#ifndef DEF_STDLIB_H -#define DEF_STDLIB_H +#ifndef _DEF_STDLIB_H +#define _DEF_STDLIB_H #include <types.h> +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + void *memcpy(void *dest, const void *src, int count); uint8_t *memset(uint8_t *dest, uint8_t val, int count); uint16_t *memsetw(uint16_t *dest, uint16_t val, int count); -int strlen(const char *str); - -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) #endif diff --git a/src/include/string.h b/src/common/include/string.h index 24ac216..a41459e 100644 --- a/src/include/string.h +++ b/src/common/include/string.h @@ -1,16 +1,12 @@ #ifndef _DEF_STRING_H #define _DEF_STRING_H -#include <gc/syscall.h> -#include <stdlib.h> - -void *memcpy(void *dest, const void *src, int count); -uint8_t *memset(uint8_t *dest, uint8_t val, int count); -uint16_t *memsetw(uint16_t *dest, uint16_t val, int count); +// #include <stdlib.h> +#include <types.h> int strlen(const char *str); char *strcpy(char *dest, const char *src); -char *strdup(const char *src); +// char *strdup(const char *src); // uses malloc, that's bad char *strchr(const char *str, char c); char *strcat(char *dest, const char *src); int strcmp(const char *s1, const char *s2); diff --git a/src/kernel/lib/types.h b/src/common/include/types.h index b7b19ab..ec24ce5 100644 --- a/src/kernel/lib/types.h +++ b/src/common/include/types.h @@ -12,4 +12,6 @@ typedef char int8_t; typedef unsigned int size_t; +#define NULL 0 + #endif diff --git a/src/library/std/string.c b/src/common/string.c index 4c374ad..b2ec309 100644 --- a/src/library/std/string.c +++ b/src/common/string.c @@ -1,4 +1,5 @@ #include <string.h> +#include <stdlib.h> int strlen(const char *str) { int i = 0; @@ -19,12 +20,12 @@ char *strcpy(char *dest, const char *src) { return (char*)src; } -char *strdup(const char *src) { +/*char *strdup(const char *src) { char* ret = malloc(strlen(src) + 1); if (ret == NULL) return ret; strcpy(ret, src); return ret; -} +}*/ char *strcat(char *dest, const char *src) { char *dest2 = dest; diff --git a/src/include/gc/shm.h b/src/include/gc/shm.h deleted file mode 100644 index 1a3f0a1..0000000 --- a/src/include/gc/shm.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef DEF_SHM_H -#define DEF_SHM_H - -#include <gc/syscall.h> - -/* - * This file contains headers for the shared segment mapping manager. - */ - -void* shm_alloc(size_t size); //allocates a spot in shared memory space -void shm_free(void* p); //frees a spot - -void* shm_allocNew(size_t size); //calls shm_alloc, and maps a new segment there -void shm_freeDel(void* p); //unmaps segment and calls shm_free - -#endif diff --git a/src/include/gc/syscall.h b/src/include/gc/syscall.h deleted file mode 100644 index a8928e3..0000000 --- a/src/include/gc/syscall.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef DEF_SYSCALL_H -#define DEF_SYSCALL_H - -typedef unsigned long long uint64_t; -typedef unsigned int uint32_t; -typedef unsigned short uint16_t; -typedef unsigned char uint8_t; -typedef long long int64_t; -typedef int int32_t; -typedef short int16_t; -typedef char int8_t; - -typedef unsigned size_t; - -struct user_request { - uint32_t func, params[3], shmsize[3]; - int isBlocking; // 1 : blocking request, 0 : nonblocking request (message) - int pid; //pid of caller process -}; - -struct user_sendrequest { - uint32_t func, a, b, c; - uint32_t answeri; - int64_t answerll; - int errcode; -}; - -#define NEW_STACK_SIZE 0x8000 - -void thread_exit(); -void schedule(); -void thread_sleep(int time); -void process_exit(int retval); -void printk(char* str); -void thread_new(void (*entry)(void*), void *data); -void irq_wait(int number); -int proc_priv(); - -int proc_setheap(size_t start, size_t end); -int shm_create(size_t offset, size_t length); -int shm_delete(size_t offset); - -#endif diff --git a/src/include/stdlib.h b/src/include/stdlib.h deleted file mode 100644 index 3a2b42c..0000000 --- a/src/include/stdlib.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef _DEF_STDLIB_H -#define _DEF_STDLIB_H - -#define NULL 0 - -//includes malloc/free -#include <gc/mem.h> - -void printk_int(int number); -void printk_hex(unsigned number); - -#endif diff --git a/src/kernel/Makefile b/src/kernel/Makefile index 5d46325..53cfc31 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -2,14 +2,15 @@ Out = kernel.elf Obj = core/loader_.o core/kmain.o core/sys.o core/test.o \ core/monitor.o task/timer.o \ task/idt.o task/idt_.o task/task.o task/task_.o task/syscall.o task/sched.o \ - lib/stdlib.o lib/bitset.o lib/mutex.o \ + lib/bitset.o lib/mutex.o \ mem/mem.o mem/paging.o mem/gdt.o mem/heap.o mem/seg.o \ - ipc/shm.o \ linker/elf.o -include ../common.make +ExtObj = $(SrcPath)/common/_common.o -CFLAGS += -I . -I ./lib -g +include $(SrcPath)/common.make + +CFLAGS += -g -I $(SrcPath)/common/include -I . -I ./lib LDFLAGS += -T link.ld -Map kernel.map diff --git a/src/kernel/config.h b/src/kernel/config.h index 38487dc..f021d9c 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -1,9 +1,9 @@ #ifndef DEF_CONFIG_H #define DEF_CONFIG_H -#define K_OS_NAME "Grapes" -#define K_OS_VER "0.0.4" -#define K_OS_CODENAME "Cat in my heart" +#define K_OS_NAME "T/CE" +#define K_OS_VER "0.0.1" +#define K_OS_CODENAME "Nyanyanya" /* HEAP CODE TO USE : Two kernel heap implementations are available : @@ -12,4 +12,6 @@ Comment the following define to use the second version */ // #define K_USE_BASIC_HEAP + + #endif diff --git a/src/kernel/core/kmain.c b/src/kernel/core/kmain.c index 8f14843..86db4ff 100644 --- a/src/kernel/core/kmain.c +++ b/src/kernel/core/kmain.c @@ -27,12 +27,12 @@ void kmain(struct multiboot_info_t* mbd, int32_t magic) { uint32_t i; mem_placementAddr = ((size_t)&end & 0xFFFFF000) + 0x1000; - mbd->cmdline += 0xE0000000; mbd->mods_addr += 0xE0000000; + mbd->cmdline += K_HIGHHALF_ADDR; mbd->mods_addr += K_HIGHHALF_ADDR; struct module_t *mods = (struct module_t*)mbd->mods_addr; for (i = 0; i < mbd->mods_count; i++) { - mods[i].mod_start += 0xE0000000; - mods[i].mod_end += 0xE0000000; - mods[i].string += 0xE0000000; + mods[i].mod_start += K_HIGHHALF_ADDR; + mods[i].mod_end += K_HIGHHALF_ADDR; + mods[i].string += K_HIGHHALF_ADDR; if (mods[i].mod_end > mem_placementAddr) mem_placementAddr = (mods[i].mod_end & 0xFFFFF000) + 0x1000; } @@ -68,7 +68,7 @@ void kmain(struct multiboot_info_t* mbd, int32_t magic) { if (pr == 0) { monitor_write(" : Error loading\n"); } else { - monitor_write(" : OK pid:"); monitor_writeDec(pr->pid); monitor_write("\n"); + monitor_write(" : OK, pid="); monitor_writeDec(pr->pid); monitor_write("\n"); } } } diff --git a/src/kernel/core/loader_.asm b/src/kernel/core/loader_.asm index 5ee998a..07de52c 100644 --- a/src/kernel/core/loader_.asm +++ b/src/kernel/core/loader_.asm @@ -1,3 +1,4 @@ +[EXTERN k_highhalf_addr] [GLOBAL loader] ; making entry point visible to linker [EXTERN kmain] ; kmain is defined in kmain.c [EXTERN tasking_tmpStack] ; a temporary 4k stack used by tasking, and used when setting up kernel stuff @@ -30,10 +31,9 @@ loader: ;here, we load our false GDT, used for having the kernel in higher half section .text higherhalf: ; now we're running in higher half - mov esp, tasking_tmpStack+0x8000 ; set up the stack push eax ; pass Multiboot magic number - add ebx, 0xE0000000 ; update the MB info structure so that it is in the new seg + add ebx, k_highhalf_addr ; update the MB info structure so that it is in the new seg push ebx ; pass Multiboot info structure call kmain ; call kernel proper @@ -51,7 +51,7 @@ trickgdt: ; our false GDT gdt: dd 0, 0 ; null GDT entry - db 0xFF, 0xFF, 0, 0, 0, 10011010b, 11001111b, 0x20 ; kernel code segment - db 0xFF, 0xFF, 0, 0, 0, 10010010b, 11001111b, 0x20 ; kernel data segment + db 0xFF, 0xFF, 0, 0, 0, 10011010b, 11001111b, 0x40 ; kernel code segment + db 0xFF, 0xFF, 0, 0, 0, 10010010b, 11001111b, 0x40 ; kernel data segment gdt_end: diff --git a/src/kernel/core/monitor.c b/src/kernel/core/monitor.c index 0d0f5eb..ba2a6df 100644 --- a/src/kernel/core/monitor.c +++ b/src/kernel/core/monitor.c @@ -1,8 +1,10 @@ #include "monitor.h" #include "sys.h" +#include "mem/mem.h" + static int cursor_x = 0, cursor_y = 0; -static uint16_t *video_memory = (uint16_t*)0xE00B8000; +static uint16_t *video_memory = (uint16_t*)((size_t)K_HIGHHALF_ADDR + 0xB8000); static uint8_t attribute = 0x07; // 0 = background = black, 7 = foreground = white diff --git a/src/kernel/core/sys.c b/src/kernel/core/sys.c index 05a7bc5..4c53d58 100644 --- a/src/kernel/core/sys.c +++ b/src/kernel/core/sys.c @@ -1,6 +1,8 @@ #include "sys.h" #include "monitor.h" +#include "mem/mem.h" + /* These four functions are wrappers around ASM operations. These functions are used when comunicating with the system hardware. */ @@ -27,7 +29,7 @@ uint16_t inw(uint16_t port) { ////// void stack_trace(size_t bp) { uint32_t *stack = (uint32_t*)bp, i; - for (i = 0; i < 5 && stack > 0xE0000000 && stack < (bp + 0x8000); i++) { + for (i = 0; i < 5 && stack > K_HIGHHALF_ADDR && stack < (bp + 0x8000); i++) { monitor_write("| "); monitor_writeHex(stack); monitor_write("\tnext:"); monitor_writeHex(stack[0]); monitor_write("\t\tret:"); monitor_writeHex(stack[1]); monitor_write("\n"); diff --git a/src/kernel/ipc/shm.c b/src/kernel/ipc/shm.c deleted file mode 100644 index 6e48613..0000000 --- a/src/kernel/ipc/shm.c +++ /dev/null @@ -1,100 +0,0 @@ -#include "shm.h" -#include <mem/mem.h> -#include <mem/seg.h> -#include <task/task.h> -#include <core/sys.h> - -static struct segment_map* shmseg_map(struct segment* seg, struct page_directory *pagedir, size_t offset); -static void shmseg_unmap(struct segment_map*); -static void shmseg_delete(struct segment *seg); -static int shmseg_handleFault(struct segment_map *map, size_t addr, int write); - -/* Call this function when creating a new shared memory segment. - Creates the shmseg struct and segment struct, fills them in. */ -struct segment* shmseg_make(size_t len, struct process* owner) { - struct shmseg *ss = kmalloc(sizeof(struct shmseg)); - struct segment *se = kmalloc(sizeof(struct segment)); - unsigned i; - se->seg_data = ss; - se->mappings = 0; - se->map = shmseg_map; - se->unmap = shmseg_unmap; - se->delete = shmseg_delete; - se->handle_fault = shmseg_handleFault; - ss->len = len; - ss->owner = owner; - ss->frames = kmalloc((len / 0x1000) * sizeof(uint32_t)); - for (i = 0; i < (len / 0x1000); i++) ss->frames[i] = 0; - return se; -} - -/* For internal use only. Called when the shared memory is mapped somewhere. - Creates the segment_map struct and fills it up. */ -struct segment_map *shmseg_map(struct segment *seg, struct page_directory *pagedir, size_t offset) { - struct segment_map *sm = kmalloc(sizeof(struct segment_map)); - sm->start = offset; - sm->len = ((struct shmseg*)(seg->seg_data))->len; - return sm; -} - -/* For internal use only. Called when the shared memory is unmapped from somewhere. - Unmaps all the pages from that place. */ -void shmseg_unmap(struct segment_map *sm) { - size_t i; - for (i = 0; i < sm->len; i += 0x1000) { - struct page *page = pagedir_getPage(sm->pagedir, sm->start + i, 0); - if (page != 0) page_unmap(page); - } -} - -/* For internal use only. Called when a page fault occurs. - Maps a page at that place, possibly requesting a free frame first. */ -int shmseg_handleFault(struct segment_map *sm, size_t addr, int write) { - struct shmseg *ss = sm->seg->seg_data; - addr &= 0xFFFFF000; - struct page *p = pagedir_getPage(sm->pagedir, addr, 1); - if (p->frame != 0) return 1; - int frame_idx = (addr - sm->start) / 0x1000; - if (ss->frames[frame_idx] == 0) { - ss->frames[frame_idx] = frame_alloc(); - } - page_map(p, ss->frames[frame_idx], 1, 1); - return 0; -} - -/* For internal use only. Called when the shared memory is deleted. - Frees all the frames. */ -void shmseg_delete(struct segment *seg) { - struct shmseg *ss = seg->seg_data; - unsigned i; - for (i = 0; i < (ss->len / 0x1000); i++) { - if (ss->frames[i] != 0) frame_free(ss->frames[i]); - } - kfree(ss->frames); -} - -/* Looks through the mapped segment for a process, looking for a shared memory segment at given offset. */ -struct segment_map* shmseg_getByOff(struct process* pr, size_t offset) { - struct segment_map* m = pr->pagedir->mappedSegs; - while (m != 0) { - if (m->start == offset && m->seg->delete == shmseg_delete) return m; - m = m->next; - } - return 0; -} - -// **** **** SHM syscalls **** **** -int shm_create(size_t offset, size_t len) { - if (offset >= 0xE0000000) return -1; - if (len >= 0x10000000) return -1; - if (offset+len >= 0xE0000000) return -1; - seg_map(shmseg_make(len, current_thread->process), current_thread->process->pagedir, offset); - return 0; -} - -int shm_delete(size_t offset) { - struct segment_map *s = shmseg_getByOff(current_thread->process, offset); - if (s == 0) return -1; - seg_unmap(s); - return 0; -} diff --git a/src/kernel/ipc/shm.h b/src/kernel/ipc/shm.h deleted file mode 100644 index 5459dbb..0000000 --- a/src/kernel/ipc/shm.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef DEF_SHM_H -#define DEF_SHM_H - -#include <task/task.h> - -struct shmseg { - size_t len; - uint32_t *frames; - struct process* owner; -}; - -//Shared memory segment stuff -struct segment* shmseg_make(size_t len, struct process* owner); - //find a shared memory segment in current address space by its offset -struct segment_map* shmseg_getByOff(struct process* pr, size_t offset); - -//Shared memory syscalls -int shm_create(size_t offset, size_t len); -int shm_delete(size_t offset); - -#endif - diff --git a/src/kernel/lib/stdlib.c b/src/kernel/lib/stdlib.c deleted file mode 100644 index c5245e7..0000000 --- a/src/kernel/lib/stdlib.c +++ /dev/null @@ -1,39 +0,0 @@ -#include "stdlib.h" - -void *memcpy(void *vd, const void *vs, int count) { - uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; - uint32_t f = count % 4, n = count / 4, i; - const uint32_t* s = (uint32_t*)src; - uint32_t* d = (uint32_t*)dest; - for (i = 0; i < n; i++) { - d[i] = s[i]; - } - if (f != 0) { - for (i = count - f; i < count; i++) { - dest[i] = src[i]; - } - } - return vd; -} - -uint8_t *memset(uint8_t *dest, uint8_t val, int count) { - int i; - for (i = 0; i < count; i++) { - dest[i] = val; - } - return dest; -} - -uint16_t *memsetw(uint16_t *dest, uint16_t val, int count) { - int i; - for (i = 0; i < count; i++) { - dest[i] = val; - } - return dest; -} - -int strlen(const char *str) { - int i = 0; - while (str[i++]); - return i; -} diff --git a/src/kernel/link.ld b/src/kernel/link.ld index 1ce17cf..fcdb0bb 100644 --- a/src/kernel/link.ld +++ b/src/kernel/link.ld @@ -1,27 +1,29 @@ ENTRY (loader) SECTIONS{ + k_highhalf_addr = 0xC0000000; + . = 0x00100000; .setup : { *(.setup) } - . += 0xE0000000; + . += k_highhalf_addr; - .text : AT(ADDR(.text) - 0xE0000000) { + .text : AT(ADDR(.text) - k_highhalf_addr) { *(.text) } - .rodata ALIGN (0x1000) : AT(ADDR(.rodata) - 0xE0000000) { + .rodata ALIGN (0x1000) : AT(ADDR(.rodata) - k_highhalf_addr) { *(.rodata) } - .data ALIGN (0x1000) : AT(ADDR(.data) - 0xE0000000) { + .data ALIGN (0x1000) : AT(ADDR(.data) - k_highhalf_addr) { *(.data) } - .bss : AT(ADDR(.bss) - 0xE0000000) { + .bss : AT(ADDR(.bss) - k_highhalf_addr) { sbss = .; *(COMMON) *(.bss) diff --git a/src/kernel/mem/heap.basic.h b/src/kernel/mem/heap.basic.h index c752750..b52be4e 100644 --- a/src/kernel/mem/heap.basic.h +++ b/src/kernel/mem/heap.basic.h @@ -3,7 +3,7 @@ /* This heap implementation is extra-simple and extra-slow. */ -#include "types.h" +#include <types.h> #define HH_FREE_MAGIC 0xFEE0FEE0 #define HH_ALLOC_MAGIC 0xA110A110 diff --git a/src/kernel/mem/heap.std.c b/src/kernel/mem/heap.std.c index 5b2d025..84c11e2 100644 --- a/src/kernel/mem/heap.std.c +++ b/src/kernel/mem/heap.std.c @@ -2,7 +2,9 @@ #include "paging.h" #include <core/sys.h> -#include <lib/stdlib.h> +#include <mem/mem.h> + +#include <stdlib.h> #define HEAP_MAGIC 0xBAD0BEEF #define HEAP_MIN_SIZE 0x4000 @@ -156,7 +158,7 @@ static void heap_contract(struct heap *heap) { /* Alocate some bytes on the heap. */ void* heap_alloc(struct heap *heap, size_t sz) { - ASSERT(heap > 0xE0000000); + ASSERT(heap > K_HIGHHALF_ADDR); size_t newsize = sz + sizeof(struct heap_header) + sizeof(struct heap_footer); uint32_t iterator = 0; diff --git a/src/kernel/mem/heap.std.h b/src/kernel/mem/heap.std.h index fa146ae..2018508 100644 --- a/src/kernel/mem/heap.std.h +++ b/src/kernel/mem/heap.std.h @@ -6,7 +6,7 @@ The heap algorithm used is based on the one described here : http://www.jamesmolloy.co.uk/tutorial_html/7.-The%20Heap.html */ -#include "types.h" +#include <types.h> struct heap_header { uint32_t magic; diff --git a/src/kernel/mem/mem.c b/src/kernel/mem/mem.c index c6494b7..8d814e4 100644 --- a/src/kernel/mem/mem.c +++ b/src/kernel/mem/mem.c @@ -11,6 +11,8 @@ #include "heap.std.h" #endif +#include "mem.h" + #define FREEPAGESTOKEEP 5 #define KHEAP_IDXSIZE 0x4000 // only used with heap.std.h @@ -50,7 +52,7 @@ static void get_free_pages() { mem_placementAddr += 0x1000; } freepages[freepagecount].virt = (size_t)kmalloc(0x1000); - freepages[freepagecount].phys = freepages[freepagecount].virt - 0xE0000000; + freepages[freepagecount].phys = freepages[freepagecount].virt - K_HIGHHALF_ADDR; freepagecount++; } } diff --git a/src/kernel/mem/mem.h b/src/kernel/mem/mem.h index b372fae..093545e 100644 --- a/src/kernel/mem/mem.h +++ b/src/kernel/mem/mem.h @@ -16,5 +16,9 @@ void kheap_init(); extern size_t mem_placementAddr; extern void end; //Symbol defined by linker : end of kernel code +extern void k_highhalf_addr; // Symbol defined by linker : high half position of the kerne + // Should be at 0xC0000000 +#define K_HIGHHALF_ADDR ((size_t)(&k_highhalf_addr)) + #endif diff --git a/src/kernel/mem/paging.c b/src/kernel/mem/paging.c index 4e5f33a..3e2bffa 100644 --- a/src/kernel/mem/paging.c +++ b/src/kernel/mem/paging.c @@ -7,6 +7,8 @@ #include <core/sys.h> #include <task/task.h> +#define FIRST_KERNEL_PAGETABLE (K_HIGHHALF_ADDR >> 22) + static struct bitset frames; struct page_directory *kernel_pagedir, *current_pagedir; @@ -27,7 +29,7 @@ void frame_free(uint32_t id) { /************************* PAGING INITIALIZATION *****************************/ /* This function creates the kernel page directory. It must be called before the GDT is loaded. - It maps 0xE0000000+ to the corresponding physical kernel code, but it also maps + It maps 0xC0000000+ (k_highhalf_addr) to the corresponding physical kernel code, but it also maps 0x00000000+ to that code because with the false GDT we set up in loader_.asm, the code will be looked for at the beginning of the memory. Only when the real GDT is loaded we can de-allocate pages at 0x00000000 ; this is done by paging_cleanup. */ @@ -45,12 +47,12 @@ void paging_init(size_t totalRam) { kernel_pagedir->tablesPhysical[i] = 0; } - for (i = 0xE0000000; i < mem_placementAddr; i += 0x1000) { + for (i = K_HIGHHALF_ADDR; i < mem_placementAddr; i += 0x1000) { page_map(pagedir_getPage(kernel_pagedir, i, 1), frame_alloc(), 0, 0); } - for (i = 0; i < (mem_placementAddr - 0xE0000000) / 0x100000; i++) { - kernel_pagedir->tablesPhysical[i] = kernel_pagedir->tablesPhysical[i + 896]; - kernel_pagedir->tables[i] = kernel_pagedir->tables[i + 896]; + for (i = 0; i < (mem_placementAddr - K_HIGHHALF_ADDR) / 0x100000; i++) { + kernel_pagedir->tablesPhysical[i] = kernel_pagedir->tablesPhysical[i + FIRST_KERNEL_PAGETABLE]; + kernel_pagedir->tables[i] = kernel_pagedir->tables[i + FIRST_KERNEL_PAGETABLE]; } monitor_write("{PD: "); @@ -61,8 +63,8 @@ void paging_init(size_t totalRam) { /* De-allocates pages at 0x00000000 where kernel code was read from with the GDT from loader_.asm. */ void paging_cleanup() { - uint32_t i; - for (i = 0; i < (mem_placementAddr - 0xE0000000) / 0x100000; i++) { + size_t i; + for (i = 0; i < (mem_placementAddr - K_HIGHHALF_ADDR) / 0x100000; i++) { kernel_pagedir->tablesPhysical[i] = 0; kernel_pagedir->tables[i] = 0; } @@ -94,7 +96,7 @@ struct page_directory *pagedir_new() { pd->tables[i] = 0; pd->tablesPhysical[i] = 0; } - for (i = 896; i < 1024; i++) { + for (i = FIRST_KERNEL_PAGETABLE; i < 1024; i++) { pd->tables[i] = kernel_pagedir->tables[i]; pd->tablesPhysical[i] = kernel_pagedir->tablesPhysical[i]; } @@ -108,7 +110,7 @@ void pagedir_delete(struct page_directory *pd) { //Unmap segments while (pd->mappedSegs != 0) seg_unmap(pd->mappedSegs); //Cleanup page tables - for (i = 0; i < 896; i++) { + for (i = 0; i < FIRST_KERNEL_PAGETABLE; i++) { kfree_page(pd->tables[i]); } kfree_page(pd->tablesPhysical); @@ -130,7 +132,7 @@ uint32_t paging_fault(struct registers *regs) { } if (seg != 0) { - if (seg->seg->handle_fault(seg, addr, (regs->err_code & 0x2) && (regs->eip < 0xE0000000)) != 0) seg = 0; + if (seg->seg->handle_fault(seg, addr, (regs->err_code & 0x2) && (regs->eip < K_HIGHHALF_ADDR)) != 0) seg = 0; } if (seg == 0) { @@ -158,7 +160,7 @@ struct page *pagedir_getPage(struct page_directory *pd, uint32_t address, int ma return &pd->tables[table_idx]->pages[address % 1024]; } else if (make) { pd->tables[table_idx] = kmalloc_page(pd->tablesPhysical + table_idx); - if (table_idx >= 896) + if (table_idx >= FIRST_KERNEL_PAGETABLE) tasking_updateKernelPagetable(table_idx, pd->tables[table_idx], pd->tablesPhysical[table_idx]); memset((uint8_t*)pd->tables[table_idx], 0, 0x1000); pd->tablesPhysical[table_idx] |= 0x07; diff --git a/src/kernel/task/syscall.c b/src/kernel/task/syscall.c index 3ae546f..5d40596 100644 --- a/src/kernel/task/syscall.c +++ b/src/kernel/task/syscall.c @@ -23,8 +23,6 @@ CALL1(monitor_write, printk_sc); CALL1V(idt_waitIrq, irq_wait_sc); CALL0(proc_priv, proc_priv_sc); CALL2(process_setheapseg, proc_setheap_sc); -CALL2(shm_create, shm_create_sc); -CALL1(shm_delete, shm_delete_sc); static void thread_new_sc(struct registers* r) { cli(); @@ -42,6 +40,4 @@ int_callback syscalls[NUMBER_OF_SYSCALLS] = { irq_wait_sc, proc_priv_sc, proc_setheap_sc, - shm_create_sc, - shm_delete_sc, //10 0 }; diff --git a/src/kernel/task/task.c b/src/kernel/task/task.c index 3d322c1..a6becdb 100644 --- a/src/kernel/task/task.c +++ b/src/kernel/task/task.c @@ -42,7 +42,7 @@ void tasking_init() { monitor_write("[Tasking] "); } -/* Called by the paging functions when a page table is allocated in the kernel space (>0xE0000000). +/* Called by the paging functions when a page table is allocated in the kernel space (>K_HIGHHALF_ADDR). Updates the page directories of all the processes. */ void tasking_updateKernelPagetable(uint32_t idx, struct page_table *table, uint32_t tablephysical) { if (idx < 896) return; @@ -103,7 +103,7 @@ uint32_t tasking_handleException(struct registers *regs) { "Page Fault","Unknown Interrupt","Coprocessor Fault","Alignment Check","Machine Check"}; monitor_write(exception_messages[regs->int_no]); monitor_write("'\teip:"); monitor_writeHex(regs->eip); - if (regs->eip >= 0xE0000000) { + if (regs->eip >= K_HIGHHALF_ADDR) { monitor_write("\n Exception stack trace :\n"); stack_trace(regs->ebp); PANIC("Kernel error'd."); @@ -342,7 +342,7 @@ static void process_delete(struct process *pr) { /* System call. Called by the app to define the place for the heap. */ int process_setheapseg(size_t start, size_t end) { //syscall struct process *p = current_thread->process; - if (start >= 0xE0000000 || end >= 0xE0000000) return -1; + if (start >= K_HIGHHALF_ADDR || end >= K_HIGHHALF_ADDR) return -1; if (p->heapseg == 0) { struct segment *s = simpleseg_make(start, end - start, 1); if (s == 0) return -5; diff --git a/src/library/Makefile b/src/library/Makefile deleted file mode 100644 index 6e51efd..0000000 --- a/src/library/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -Out = grapes.o -Obj = gc/syscall.o \ - gc/mem.o gc/shm.o \ - std/mutex.o std/string.o std/stdio.o \ - start.o - -include ../common.make - -CFLAGS = -I../include - -LDFLAGS += -r - diff --git a/src/library/gc/shm.c b/src/library/gc/shm.c deleted file mode 100644 index 7f8609a..0000000 --- a/src/library/gc/shm.c +++ /dev/null @@ -1,100 +0,0 @@ -#include <gc/shm.h> -#include <gc/mem.h> -#include <mutex.h> - -struct shm_block { - size_t start, size; - int is_hole; //1 : hole, 0 : used block - struct shm_block *prev, *next; -}; - -struct shm_block *blocks = 0; - -static uint32_t tMutex = MUTEX_UNLOCKED; - -static void shm_init() { - struct shm_block *b = malloc(sizeof(struct shm_block)); - b->start = 0x80000000; - b->size = 0xD0000000 - 0x80000000; - b->is_hole = 1; - b->prev = b->next = 0; - blocks = b; -} - -void* shm_alloc(size_t size) { - mutex_lock(&tMutex); - if (blocks == 0) shm_init(); - if (size & 0xFFF) size = (size & 0xFFFFF000) + 0x1000; - //go through all blocks, get the one with the closest size - struct shm_block *i = blocks, *block = 0; - while (i) { - if (i->size >= size && i->is_hole == 1 && (block == 0 || i->size < block->size)) block = i; - i = i->next; - } - if (block == 0) { - mutex_unlock(&tMutex); - return 0; - } - //if the block's size is bigger, reduce it and create a new one after - if (block->size > size) { - struct shm_block *newb = malloc(sizeof(struct shm_block)); - newb->start = block->start + size; - newb->size = block->size - size; - newb->is_hole = 1; - newb->prev = block; newb->next = block->next; - block->size = size; - if (block->next != 0) block->next->prev = newb; - block->next = newb; - } - //mark block as used - block->is_hole = 0; - //return block's address - mutex_unlock(&tMutex); - return (void*)block->start; -} - -static void unify (struct shm_block *b) { - if (b->next == 0 || b->is_hole == 0 || b->next->is_hole == 0) return; - struct shm_block *n = b->next; - b->size += n->size; - if (n->next != 0) n->next->prev = b; - b->next = n->next; - free(n); -} - -void shm_free(void* p) { - mutex_lock(&tMutex); - //find block - struct shm_block *bl = blocks; - while (bl) { - if (bl->start == (size_t)p) break; - bl = bl->next; - } - if (bl == 0) { - mutex_unlock(&tMutex); - return; - } - //mark it as a hole - bl->is_hole = 1; - //unify after if possible - if (bl->next != 0 && bl->next->is_hole == 1) unify(bl); - //unify before if possible - if (bl->prev != 0 && bl->prev->is_hole == 1) unify(bl->prev); - mutex_unlock(&tMutex); -} - -void* shm_allocNew(size_t size) { - if (size & 0xFFF) size = (size & 0xFFFFF000) + 0x1000; - - void* p = shm_alloc(size); - if (shm_create((size_t)p, size) != 0) { - shm_free(p); - return 0; - } - return p; -} - -void shm_freeDel(void *p) { - shm_delete((size_t)p); - shm_free(p); -} diff --git a/src/modules/test/Makefile b/src/modules/test/Makefile deleted file mode 100644 index 3b9ac14..0000000 --- a/src/modules/test/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -Obj = main.o -Out = test.elf - -include ../../common.make - -CFLAGS += -I ../../include - -LDFLAGS += -T ../../library/link.ld -L ../../library -Map test.map diff --git a/src/user/app_common.make b/src/user/app_common.make new file mode 100644 index 0000000..8d7c8a8 --- /dev/null +++ b/src/user/app_common.make @@ -0,0 +1,6 @@ +ExtObj = $(SrcPath)/user/lib/_user.o + +include $(SrcPath)/common.make + +CFLAGS += -I $(SrcPath)/common/include -I $(SrcPath)/user/lib/include +LDFLAGS += -T $(SrcPath)/user/link.ld diff --git a/src/user/lib/Makefile b/src/user/lib/Makefile new file mode 100644 index 0000000..a4d68e7 --- /dev/null +++ b/src/user/lib/Makefile @@ -0,0 +1,12 @@ +Out = _user.o +Obj = tce/syscall.o tce/mem.o \ + std/mutex.o std/stdio.o \ + start.o +ExtObj = $(SrcPath)/common/_common.o + +include $(SrcPath)/common.make + +CFLAGS = -I$(SrcPath)/common/include -I$(SrcPath)/user/lib/include + +LDFLAGS += -r + diff --git a/src/include/mutex.h b/src/user/lib/include/mutex.h index 50b5606..a6b949b 100644 --- a/src/include/mutex.h +++ b/src/user/lib/include/mutex.h @@ -1,7 +1,7 @@ #ifndef DEF_MUTEX_H #define DEF_MUTEX_H -#include <gc/syscall.h> +#include <tce/syscall.h> #define MUTEX_LOCKED 1 #define MUTEX_UNLOCKED 0 diff --git a/src/include/gc/mem.h b/src/user/lib/include/tce/mem.h index 2557289..ab17bde 100644 --- a/src/include/gc/mem.h +++ b/src/user/lib/include/tce/mem.h @@ -1,7 +1,7 @@ #ifndef DEF_HEAP_H #define DEF_HEAP_H -#include "gc/syscall.h" +#include <tce/syscall.h> void* malloc(size_t size); void free(void* p); diff --git a/src/user/lib/include/tce/syscall.h b/src/user/lib/include/tce/syscall.h new file mode 100644 index 0000000..5257a02 --- /dev/null +++ b/src/user/lib/include/tce/syscall.h @@ -0,0 +1,21 @@ +#ifndef DEF_SYSCALL_H +#define DEF_SYSCALL_H + +#include <types.h> + +#define NEW_STACK_SIZE 0x8000 + +void thread_exit(); +void schedule(); +void thread_sleep(int time); +void process_exit(int retval); +void printk(char* str); +void thread_new(void (*entry)(void*), void *data); +void irq_wait(int number); +int proc_priv(); + +int proc_setheap(size_t start, size_t end); +int shm_create(size_t offset, size_t length); +int shm_delete(size_t offset); + +#endif diff --git a/src/library/start.c b/src/user/lib/start.c index b3c5541..3dbb994 100644 --- a/src/library/start.c +++ b/src/user/lib/start.c @@ -1,4 +1,4 @@ -#include <gc/syscall.h> +#include <tce/syscall.h> extern int main(); diff --git a/src/library/std/mutex.c b/src/user/lib/std/mutex.c index ac0ee8f..ac0ee8f 100644 --- a/src/library/std/mutex.c +++ b/src/user/lib/std/mutex.c diff --git a/src/library/std/stdio.c b/src/user/lib/std/stdio.c index 3b24da1..3b24da1 100644 --- a/src/library/std/stdio.c +++ b/src/user/lib/std/stdio.c diff --git a/src/library/gc/mem.c b/src/user/lib/tce/mem.c index fe243ba..e30e36b 100644 --- a/src/library/gc/mem.c +++ b/src/user/lib/tce/mem.c @@ -1,4 +1,4 @@ -#include "gc/mem.h" +#include <tce/mem.h> struct heap_header { uint32_t magic; diff --git a/src/library/gc/syscall.c b/src/user/lib/tce/syscall.c index 3100dd7..ae58e73 100644 --- a/src/library/gc/syscall.c +++ b/src/user/lib/tce/syscall.c @@ -1,5 +1,5 @@ -#include <gc/syscall.h> -#include <gc/mem.h> +#include <tce/syscall.h> +#include <tce/mem.h> static int call(unsigned a, unsigned b, unsigned c, unsigned d, unsigned e, unsigned f) { unsigned ret; diff --git a/src/library/link.ld b/src/user/link.ld index be5e028..95c2fc1 100644 --- a/src/library/link.ld +++ b/src/user/link.ld @@ -1,5 +1,4 @@ ENTRY (start) -INPUT (grapes.o) SECTIONS{ . = 0x100000; diff --git a/src/user/test/Makefile b/src/user/test/Makefile new file mode 100644 index 0000000..5c8464a --- /dev/null +++ b/src/user/test/Makefile @@ -0,0 +1,6 @@ +Obj = main.o +Out = test.elf + +include $(SrcPath)/user/app_common.make + +LDFLAGS += -Map test.map diff --git a/src/modules/test/main.c b/src/user/test/main.c index 6c13e5a..14f6363 100644 --- a/src/modules/test/main.c +++ b/src/user/test/main.c @@ -1,5 +1,5 @@ -#include <gc/syscall.h> -#include <gc/mem.h> +#include <tce/syscall.h> +#include <tce/mem.h> #include <stdlib.h> void thread_cascade(void* d) { @@ -31,8 +31,8 @@ void thread_cascade(void* d) { int main() { printk("Hi world from test module !\n"); - printk("{1} Creating thread cascade len:5\n"); - thread_new(thread_cascade, (void*)5); + printk("{1} Creating thread cascade len:4\n"); + thread_new(thread_cascade, (void*)4); printk("{1} Thread now sleeping...\n"); while (1) thread_sleep(1000); |