diff options
Diffstat (limited to 'src/kernel')
-rw-r--r-- | src/kernel/Makefile | 6 | ||||
-rw-r--r-- | src/kernel/lib/sched.c | 26 | ||||
-rw-r--r-- | src/kernel/lib/sched.h | 24 | ||||
-rw-r--r-- | src/kernel/lib/std.cpp | 87 | ||||
-rw-r--r-- | src/kernel/lib/std.h | 21 | ||||
-rw-r--r-- | src/kernel/linker/elf.cpp | 2 | ||||
-rw-r--r-- | src/kernel/mem/_dlmalloc.h | 5 | ||||
-rw-r--r-- | src/kernel/mem/gdt.cpp | 2 | ||||
-rw-r--r-- | src/kernel/mem/mem.h | 4 | ||||
-rw-r--r-- | src/kernel/mem/paging.cpp | 2 | ||||
-rw-r--r-- | src/kernel/task/idt.cpp | 1 | ||||
-rw-r--r-- | src/kernel/ui/vt.cpp | 4 | ||||
-rw-r--r-- | src/kernel/vfs/vdir.h | 2 |
13 files changed, 165 insertions, 21 deletions
diff --git a/src/kernel/Makefile b/src/kernel/Makefile index bfa5908..5c09e80 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -1,18 +1,16 @@ Out = kernel.elf Obj = core/loader_.o core/kmain.o core/sys.o \ task/timer.o task/idt.o task/idt_.o task/task.o task/task_.o task/syscall.o task/sched.o \ - lib/bitset.o lib/std.o lib/cpp.o \ + lib/bitset.o lib/std.o lib/cpp.o lib/sched.o \ mem/mem.o mem/paging.o mem/gdt.o mem/_dlmalloc.o mem/seg.o \ linker/elf.o \ vfs/node.o vfs/vdir.o \ dev/vgatxt.o dev/keyboard.o dev/ps2keyboard.o \ ui/vt.o -ExtObj = $(SrcPath)/common/_common.o - include $(SrcPath)/common.make -CFLAGS += -I $(SrcPath)/common/include -I . -I ./lib +CFLAGS += -I $(SrcPath)/include -I . -I ./lib LDFLAGS += -T link.ld -Map kernel.map diff --git a/src/kernel/lib/sched.c b/src/kernel/lib/sched.c new file mode 100644 index 0000000..29ea8a8 --- /dev/null +++ b/src/kernel/lib/sched.c @@ -0,0 +1,26 @@ +#include <sched.h> + +/* Internal use only. This function is atomic, meaning it cannot be interrupted by a system task switch. */ +static uint32_t atomic_exchange(uint32_t* ptr, uint32_t newval) { + uint32_t r; + asm volatile("xchg (%%ecx), %%eax" : "=a"(r) : "c"(ptr), "a"(newval)); + return r; +} + +int mutex_lock(uint32_t* mutex) { + while (atomic_exchange(mutex, MUTEX_LOCKED) == MUTEX_LOCKED) { + schedule(); + } + return 0; +} + +int mutex_lockE(uint32_t* mutex) { + if (atomic_exchange(mutex, MUTEX_LOCKED) == MUTEX_LOCKED) { + return 0; + } + return 1; +} + +void mutex_unlock(uint32_t* mutex) { + *mutex = MUTEX_UNLOCKED; +} diff --git a/src/kernel/lib/sched.h b/src/kernel/lib/sched.h new file mode 100644 index 0000000..1fe148d --- /dev/null +++ b/src/kernel/lib/sched.h @@ -0,0 +1,24 @@ +#ifndef DEF_MUTEX_H +#define DEF_MUTEX_H + +#include <types.h> + +#define MUTEX_LOCKED 1 +#define MUTEX_UNLOCKED 0 + +//A mutex is just an uint32_t +typedef uint32_t mutex_t; + +#ifdef __cplusplus +extern "C" { +#endif + +int mutex_lock(mutex_t* mutex); //wait for mutex to be free +int mutex_lockE(mutex_t* mutex); //lock mutex only if free, returns !0 if locked, 0 if was busy +void mutex_unlock(mutex_t* mutex); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/kernel/lib/std.cpp b/src/kernel/lib/std.cpp index aa5f606..4407f55 100644 --- a/src/kernel/lib/std.cpp +++ b/src/kernel/lib/std.cpp @@ -5,8 +5,89 @@ int errno = 0; void abort() { - ke_vt->writeStr("\n\n ABORT - errno: "); - ke_vt->writeDec(errno); - ke_vt->writeStr("\n"); + *ke_vt << "\n\n ABORT - errno: "; + *ke_vt << errno; + *ke_vt <<"\n"; PANIC("abort() called, probably a memory manager failure."); } + + + +int strlen(const char *str) { + int i = 0; + while (str[i++]); + return i; +} + +char *strchr(const char *str, char c) { + while (*str) { + if (*str == c) return (char*)str; + str++; + } + return NULL; +} + +char *strcpy(char *dest, const char *src) { + memcpy(dest, src, strlen(src) + 1); + return (char*)src; +} + +char *strdup(const char *src) { + char* ret = (char*)kmalloc(strlen(src) + 1); + if (ret == NULL) return ret; + strcpy(ret, src); + return ret; +} + +char *strcat(char *dest, const char *src) { + char *dest2 = dest; + dest2 += strlen(dest) - 1; + while (*src) { + *dest2 = *src; + src++; + dest2++; + } + *dest2 = 0; + return dest; +} + +int strcmp(const char *s1, const char *s2) { + while ((*s1) && (*s1 == *s2)) { + s1++; + s2++; + } + return (* (unsigned char*)s1 - *(unsigned char*)s2); +} + +void *memcpy(void *vd, const void *vs, int count) { + uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; + int 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; +} + +void *memset(void *dest, int val, int count) { + uint8_t *dest_c = (uint8_t*)dest; + int i; + for (i = 0; i < count; i++) { + dest_c[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; +} diff --git a/src/kernel/lib/std.h b/src/kernel/lib/std.h index ced49b5..fc1b65d 100644 --- a/src/kernel/lib/std.h +++ b/src/kernel/lib/std.h @@ -5,10 +5,27 @@ #include <types.h> /* For size_t */ +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + #ifdef __cplusplus -extern "C" void abort(); -#else +extern "C" { +#endif + void abort(); +void *memcpy(void *dest, const void *src, int count); +void *memset(void *dest, int val, int count); +uint16_t *memsetw(uint16_t *dest, uint16_t val, int count); + +int strlen(const char *str); +char *strcpy(char *dest, const char *src); +char *strdup(const char *src); +char *strchr(const char *str, char c); +char *strcat(char *dest, const char *src); +int strcmp(const char *s1, const char *s2); + +#ifdef __cplusplus +} #endif #define sbrk ksbrk diff --git a/src/kernel/linker/elf.cpp b/src/kernel/linker/elf.cpp index 29689a2..c6aebaa 100644 --- a/src/kernel/linker/elf.cpp +++ b/src/kernel/linker/elf.cpp @@ -1,7 +1,7 @@ #include "elf.h" #include <mem/paging.h> #include <mem/seg.h> -#include <stdlib_common.h> +#include <lib/std.h> #include <core/sys.h> int elf_check(uint8_t *data) { diff --git a/src/kernel/mem/_dlmalloc.h b/src/kernel/mem/_dlmalloc.h index 23682be..b171ef1 100644 --- a/src/kernel/mem/_dlmalloc.h +++ b/src/kernel/mem/_dlmalloc.h @@ -3,16 +3,15 @@ #include <types.h> #include "lib/std.h" -#include <stdlib_common.h> -#include <string.h> #define NO_MALLOC_STATS 1 #define LACKS_TIME_H #define LACKS_UNISTD_H #define LACKS_SYS_PARAM_H #define LACKS_STDLIB_H +#define LACKS_STRING_H -// #define USE_DL_PREFIX +#define USE_DL_PREFIX #define USE_LOCKS 2 diff --git a/src/kernel/mem/gdt.cpp b/src/kernel/mem/gdt.cpp index cca1767..1cac335 100644 --- a/src/kernel/mem/gdt.cpp +++ b/src/kernel/mem/gdt.cpp @@ -1,5 +1,5 @@ #include "gdt.h" -#include <stdlib_common.h> +#include <lib/std.h> extern "C" void gdt_flush(uint32_t); //ASM (imported from idt_.asm) extern "C" void tss_flush(); diff --git a/src/kernel/mem/mem.h b/src/kernel/mem/mem.h index 4201983..085d9ce 100644 --- a/src/kernel/mem/mem.h +++ b/src/kernel/mem/mem.h @@ -14,8 +14,8 @@ void kfree_page(void* page); extern "C" void* ksbrk(size_t size); extern "C" void kbrk(void* ptr); -#define kmalloc malloc -#define kfree free +#define kmalloc dlmalloc +#define kfree dlfree extern bool _no_more_ksbrk; extern size_t mem_placementAddr; diff --git a/src/kernel/mem/paging.cpp b/src/kernel/mem/paging.cpp index a6daf68..03c6e2d 100644 --- a/src/kernel/mem/paging.cpp +++ b/src/kernel/mem/paging.cpp @@ -1,11 +1,11 @@ #include "paging.h" #include <bitset.h> -#include <stdlib_common.h> #include "mem.h" #include "seg.h" #include <core/sys.h> #include <task/task.h> #include <ui/vt.h> +#include <lib/std.h> static bitset frames; diff --git a/src/kernel/task/idt.cpp b/src/kernel/task/idt.cpp index 3d6d3cb..e56a51b 100644 --- a/src/kernel/task/idt.cpp +++ b/src/kernel/task/idt.cpp @@ -7,7 +7,6 @@ #include <ui/vt.h> -#include <stdlib_common.h> extern "C" { diff --git a/src/kernel/ui/vt.cpp b/src/kernel/ui/vt.cpp index 683d484..cbb29f7 100644 --- a/src/kernel/ui/vt.cpp +++ b/src/kernel/ui/vt.cpp @@ -129,7 +129,7 @@ int vt::outputTo(display *display) { w = output->text_w(); h = output->text_h(); - text = (vt_char*)malloc(w * h * sizeof(vt_char)); + text = (vt_char*)kmalloc(w * h * sizeof(vt_char)); for (int c = 0; c < w; c++) { for (int l = 0; l < h; l++) { if (c < ow && l < oh) { @@ -139,7 +139,7 @@ int vt::outputTo(display *display) { } } } - if (old_text != 0) free(old_text); + if (old_text != 0) kfree(old_text); if (csr_c >= w) csr_c = w; if (csr_l >= h) csr_l = h; } else { diff --git a/src/kernel/vfs/vdir.h b/src/kernel/vfs/vdir.h index 609a504..7ac1b2a 100644 --- a/src/kernel/vfs/vdir.h +++ b/src/kernel/vfs/vdir.h @@ -3,7 +3,7 @@ #include "node.h" #include <lib/earray.h> -#include <string.h> +#include <lib/std.h> struct vdir_child { char* name; |