diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-18 11:25:25 +0200 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-18 11:25:25 +0200 |
commit | f3e03796652b792bb3fd5d3d25b687b9a7f14633 (patch) | |
tree | feef62986de699702136aaf0d56295fde9297115 /src | |
parent | c19415d2ffb6f063f91983ff505e2530ab500908 (diff) | |
download | TCE-f3e03796652b792bb3fd5d3d25b687b9a7f14633.tar.gz TCE-f3e03796652b792bb3fd5d3d25b687b9a7f14633.zip |
A bit of a change in the directory layout.
Diffstat (limited to 'src')
-rw-r--r-- | src/common/Makefile | 8 | ||||
-rw-r--r-- | src/common/include/stdlib_common.h | 22 | ||||
-rw-r--r-- | src/include/errno.h (renamed from src/common/include/errno.h) | 0 | ||||
-rw-r--r-- | src/include/tce/syscalls.h (renamed from src/common/include/tce/syscalls.h) | 0 | ||||
-rw-r--r-- | src/include/tce/vfs.h (renamed from src/common/include/tce/vfs.h) | 0 | ||||
-rw-r--r-- | src/include/types.h (renamed from src/common/include/types.h) | 0 | ||||
-rw-r--r-- | src/kernel/Makefile | 6 | ||||
-rw-r--r-- | src/kernel/lib/sched.c (renamed from src/common/sched.c) | 0 | ||||
-rw-r--r-- | src/kernel/lib/sched.h (renamed from src/common/include/sched.h) | 0 | ||||
-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 | ||||
-rw-r--r-- | src/user/app_common.make | 2 | ||||
-rw-r--r-- | src/user/lib/Makefile | 5 | ||||
-rw-r--r-- | src/user/lib/include/sched.h | 24 | ||||
-rw-r--r-- | src/user/lib/include/stdlib.h | 1 | ||||
-rw-r--r-- | src/user/lib/include/string.h (renamed from src/common/include/string.h) | 13 | ||||
-rw-r--r-- | src/user/lib/std/sched.c | 26 | ||||
-rw-r--r-- | src/user/lib/std/string.c (renamed from src/common/string.c) | 5 |
26 files changed, 179 insertions, 63 deletions
diff --git a/src/common/Makefile b/src/common/Makefile deleted file mode 100644 index 8698100..0000000 --- a/src/common/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -Out = _common.o -Obj = string.o sched.o -ExtObj = - -include $(SrcPath)/common.make - -LDFLAGS += -r -CFLAGS += -I $(SrcPath)/common/include diff --git a/src/common/include/stdlib_common.h b/src/common/include/stdlib_common.h deleted file mode 100644 index dc40b96..0000000 --- a/src/common/include/stdlib_common.h +++ /dev/null @@ -1,22 +0,0 @@ -#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)) - -#ifdef __cplusplus -extern "C" { -#endif - -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); - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/src/common/include/errno.h b/src/include/errno.h index fe60d81..fe60d81 100644 --- a/src/common/include/errno.h +++ b/src/include/errno.h diff --git a/src/common/include/tce/syscalls.h b/src/include/tce/syscalls.h index 6c67523..6c67523 100644 --- a/src/common/include/tce/syscalls.h +++ b/src/include/tce/syscalls.h diff --git a/src/common/include/tce/vfs.h b/src/include/tce/vfs.h index 20ea03b..20ea03b 100644 --- a/src/common/include/tce/vfs.h +++ b/src/include/tce/vfs.h diff --git a/src/common/include/types.h b/src/include/types.h index e7c1a35..e7c1a35 100644 --- a/src/common/include/types.h +++ b/src/include/types.h 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/common/sched.c b/src/kernel/lib/sched.c index 29ea8a8..29ea8a8 100644 --- a/src/common/sched.c +++ b/src/kernel/lib/sched.c diff --git a/src/common/include/sched.h b/src/kernel/lib/sched.h index 1fe148d..1fe148d 100644 --- a/src/common/include/sched.h +++ b/src/kernel/lib/sched.h 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; diff --git a/src/user/app_common.make b/src/user/app_common.make index 8d7c8a8..f2f0c46 100644 --- a/src/user/app_common.make +++ b/src/user/app_common.make @@ -2,5 +2,5 @@ ExtObj = $(SrcPath)/user/lib/_user.o include $(SrcPath)/common.make -CFLAGS += -I $(SrcPath)/common/include -I $(SrcPath)/user/lib/include +CFLAGS += -I $(SrcPath)/include -I $(SrcPath)/user/lib/include LDFLAGS += -T $(SrcPath)/user/link.ld diff --git a/src/user/lib/Makefile b/src/user/lib/Makefile index ff916bf..9111ff5 100644 --- a/src/user/lib/Makefile +++ b/src/user/lib/Makefile @@ -1,12 +1,11 @@ Out = _user.o Obj = tce/syscall.o std/_dlmalloc.o \ - std/stdio.o std/stdlib.o \ + std/stdio.o std/stdlib.o std/string.o std/sched.o \ start.o -ExtObj = $(SrcPath)/common/_common.o include $(SrcPath)/common.make -CFLAGS += -I$(SrcPath)/common/include -I$(SrcPath)/user/lib/include +CFLAGS += -I$(SrcPath)/include -I$(SrcPath)/user/lib/include LDFLAGS += -r diff --git a/src/user/lib/include/sched.h b/src/user/lib/include/sched.h new file mode 100644 index 0000000..1fe148d --- /dev/null +++ b/src/user/lib/include/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/user/lib/include/stdlib.h b/src/user/lib/include/stdlib.h index 289c0fa..594f5c5 100644 --- a/src/user/lib/include/stdlib.h +++ b/src/user/lib/include/stdlib.h @@ -2,7 +2,6 @@ #define DEF_STDLIB_H #include <types.h> -#include <stdlib_common.h> #include <tce/syscall.h> void abort(); diff --git a/src/common/include/string.h b/src/user/lib/include/string.h index 19e87c1..f14af35 100644 --- a/src/common/include/string.h +++ b/src/user/lib/include/string.h @@ -1,16 +1,19 @@ -#ifndef _DEF_STRING_H -#define _DEF_STRING_H +#ifndef DEF_LIB_STRING_H +#define DEF_LIB_STRING_H -// #include <stdlib.h> #include <types.h> #ifdef __cplusplus extern "C" { #endif +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); // uses malloc, that's bad +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); @@ -19,4 +22,6 @@ int strcmp(const char *s1, const char *s2); } #endif + #endif + diff --git a/src/user/lib/std/sched.c b/src/user/lib/std/sched.c new file mode 100644 index 0000000..29ea8a8 --- /dev/null +++ b/src/user/lib/std/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/common/string.c b/src/user/lib/std/string.c index 3825e47..20fd9e8 100644 --- a/src/common/string.c +++ b/src/user/lib/std/string.c @@ -1,5 +1,4 @@ #include <string.h> -#include <stdlib_common.h> int strlen(const char *str) { int i = 0; @@ -21,7 +20,7 @@ char *strcpy(char *dest, const char *src) { } char *strdup(const char *src) { - char* ret = malloc(strlen(src) + 1); + char* ret = (char*)malloc(strlen(src) + 1); if (ret == NULL) return ret; strcpy(ret, src); return ret; @@ -49,7 +48,7 @@ int strcmp(const char *s1, const char *s2) { 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; + 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++) { |