From f3e03796652b792bb3fd5d3d25b687b9a7f14633 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Fri, 18 May 2012 11:25:25 +0200 Subject: A bit of a change in the directory layout. --- Makefile | 2 +- doc/directories.txt | 3 +- src/common/Makefile | 8 ---- src/common/include/errno.h | 42 ------------------ src/common/include/sched.h | 24 ----------- src/common/include/stdlib_common.h | 22 ---------- src/common/include/string.h | 22 ---------- src/common/include/tce/syscalls.h | 38 ----------------- src/common/include/tce/vfs.h | 42 ------------------ src/common/include/types.h | 17 -------- src/common/sched.c | 26 ------------ src/common/string.c | 81 ----------------------------------- src/include/errno.h | 42 ++++++++++++++++++ src/include/tce/syscalls.h | 38 +++++++++++++++++ src/include/tce/vfs.h | 42 ++++++++++++++++++ src/include/types.h | 17 ++++++++ src/kernel/Makefile | 6 +-- src/kernel/lib/sched.c | 26 ++++++++++++ src/kernel/lib/sched.h | 24 +++++++++++ src/kernel/lib/std.cpp | 87 ++++++++++++++++++++++++++++++++++++-- src/kernel/lib/std.h | 21 ++++++++- src/kernel/linker/elf.cpp | 2 +- src/kernel/mem/_dlmalloc.h | 5 +-- src/kernel/mem/gdt.cpp | 2 +- src/kernel/mem/mem.h | 4 +- src/kernel/mem/paging.cpp | 2 +- src/kernel/task/idt.cpp | 1 - src/kernel/ui/vt.cpp | 4 +- src/kernel/vfs/vdir.h | 2 +- src/user/app_common.make | 2 +- src/user/lib/Makefile | 5 +-- src/user/lib/include/sched.h | 24 +++++++++++ src/user/lib/include/stdlib.h | 1 - src/user/lib/include/string.h | 27 ++++++++++++ src/user/lib/std/sched.c | 26 ++++++++++++ src/user/lib/std/string.c | 80 +++++++++++++++++++++++++++++++++++ 36 files changed, 466 insertions(+), 351 deletions(-) delete mode 100644 src/common/Makefile delete mode 100644 src/common/include/errno.h delete mode 100644 src/common/include/sched.h delete mode 100644 src/common/include/stdlib_common.h delete mode 100644 src/common/include/string.h delete mode 100644 src/common/include/tce/syscalls.h delete mode 100644 src/common/include/tce/vfs.h delete mode 100644 src/common/include/types.h delete mode 100644 src/common/sched.c delete mode 100644 src/common/string.c create mode 100644 src/include/errno.h create mode 100644 src/include/tce/syscalls.h create mode 100644 src/include/tce/vfs.h create mode 100644 src/include/types.h create mode 100644 src/kernel/lib/sched.c create mode 100644 src/kernel/lib/sched.h create mode 100644 src/user/lib/include/sched.h create mode 100644 src/user/lib/include/string.h create mode 100644 src/user/lib/std/sched.c create mode 100644 src/user/lib/std/string.c diff --git a/Makefile b/Makefile index bb759b8..29ae6c3 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: clean, mrproper, Init.rfs, floppy, commit -Projects = common kernel user/lib user/test +Projects = kernel user/lib user/test QemuCmd = qemu-system-i386 BasePath = $(shell pwd) diff --git a/doc/directories.txt b/doc/directories.txt index c5d3208..2a70729 100644 --- a/doc/directories.txt +++ b/doc/directories.txt @@ -4,8 +4,7 @@ doc documentation about the system mnt temporary folder for mounting disk images src source code src/kernel source code for everything running in kernel-land -src/common source code for stuff (libraries) that can run in kernel or user land -src/common/inc user&kernel-shared headers +src/include headers that the kernel and the userland must share src/user source code for stuff that can run only in user land src/user/lib userland libraries src/user/lib/inc header files for these libraries 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/errno.h b/src/common/include/errno.h deleted file mode 100644 index fe60d81..0000000 --- a/src/common/include/errno.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef _DEF_ERRNO_H -#define _DEF_ERRNO_H - -// Copied from /usr/include/asm-generic/errno-base.h on a standard Linux system -// Most of these will NOT be used here. - -#define EPERM 1 /* Operation not permitted */ -#define ENOENT 2 /* No such file or directory */ -#define ESRCH 3 /* No such process */ -#define EINTR 4 /* Interrupted system call */ -#define EIO 5 /* I/O error */ -#define ENXIO 6 /* No such device or address */ -#define E2BIG 7 /* Argument list too long */ -#define ENOEXEC 8 /* Exec format error */ -#define EBADF 9 /* Bad file number */ -#define ECHILD 10 /* No child processes */ -#define EAGAIN 11 /* Try again */ -#define ENOMEM 12 /* Out of memory */ -#define EACCES 13 /* Permission denied */ -#define EFAULT 14 /* Bad address */ -#define ENOTBLK 15 /* Block device required */ -#define EBUSY 16 /* Device or resource busy */ -#define EEXIST 17 /* File exists */ -#define EXDEV 18 /* Cross-device link */ -#define ENODEV 19 /* No such device */ -#define ENOTDIR 20 /* Not a directory */ -#define EISDIR 21 /* Is a directory */ -#define EINVAL 22 /* Invalid argument */ -#define ENFILE 23 /* File table overflow */ -#define EMFILE 24 /* Too many open files */ -#define ENOTTY 25 /* Not a typewriter */ -#define ETXTBSY 26 /* Text file busy */ -#define EFBIG 27 /* File too large */ -#define ENOSPC 28 /* No space left on device */ -#define ESPIPE 29 /* Illegal seek */ -#define EROFS 30 /* Read-only file system */ -#define EMLINK 31 /* Too many links */ -#define EPIPE 32 /* Broken pipe */ -#define EDOM 33 /* Math argument out of domain of func */ -#define ERANGE 34 /* Math result not representable */ - -#endif diff --git a/src/common/include/sched.h b/src/common/include/sched.h deleted file mode 100644 index 1fe148d..0000000 --- a/src/common/include/sched.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef DEF_MUTEX_H -#define DEF_MUTEX_H - -#include - -#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/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 - -#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/string.h b/src/common/include/string.h deleted file mode 100644 index 19e87c1..0000000 --- a/src/common/include/string.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef _DEF_STRING_H -#define _DEF_STRING_H - -// #include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -int strlen(const char *str); -char *strcpy(char *dest, 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); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/common/include/tce/syscalls.h b/src/common/include/tce/syscalls.h deleted file mode 100644 index 6c67523..0000000 --- a/src/common/include/tce/syscalls.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef DEF_TCE_SYSCALLS_H -#define DEF_TCE_SYSCALLS_H - -#define SC_THREAD_EXIT 1 -#define SC_SCHEDULE 2 -#define SC_THREAD_SLEEP 3 -#define SC_PROCESS_EXIT 4 -#define SC_PRINTK 5 -#define SC_THREAD_NEW 6 -#define SC_IRQ_WAIT 7 -#define SC_PROC_PRIV 8 - -#define SC_SBRK 10 -#define SC_BRK 11 -// NOT YET IMPLEMENTED -#define SC_MMAP 12 -#define SC_MUNMAP 13 - -#define SC_OPEN 20 -#define SC_OPEN_RELATIVE 21 -#define SC_STAT 22 -#define SC_STAT_RELATIVE 23 -#define SC_STATF 24 -#define SC_CLOSE 25 -#define SC_READ 26 -#define SC_WRITE 27 -#define SC_LINK 28 - - -// ERRORS -#define E_NOT_IMPLEMENTED -1 -#define E_NOT_FOUND -2 -#define E_INVALID_FD -3 -#define E_TOO_SHORT -4 // not enough space for data to be copied to -#define E_INVALID_RANGE -5 -#define E_INVALID -6 // anything went wrong - invalid parameter, usually - -#endif diff --git a/src/common/include/tce/vfs.h b/src/common/include/tce/vfs.h deleted file mode 100644 index 20ea03b..0000000 --- a/src/common/include/tce/vfs.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef DEF_TCE_VFS_H -#define DEF_TCE_VFS_H - -#include - -typedef size_t FILE; - -typedef struct _file_info { - uint32_t type; - uint32_t dev_type; - uint32_t mode; - uint32_t uid, gid; - size_t size; -} file_info; - -// file open flags -#define FM_READ 0x00000001 -#define FM_WRITE 0x00000002 -#define FM_APPEND 0x00000004 -#define FM_TRUNC 0x00000008 -#define FM_CREATE 0x00000010 -#define FM_DELETE 0x00000020 - -// link modes -#define LM_SYMLINK 1 -#define LM_HARDLINK 2 -#define LM_MOUNT 3 -#define LM_OUTPUT_TO 4 - -// file type flags -#define FT_FILE 0x00000001 -#define FT_DIR 0x00000002 -#define FT_SYMLINK 0x00000004 -#define FT_DEV 0x00000008 -#define FT_TERMINAL 0x00000010 - -// device types -#define DT_BLOCK 1 -#define DT_KEYBOARD 2 -#define DT_DISPLAY 3 - -#endif diff --git a/src/common/include/types.h b/src/common/include/types.h deleted file mode 100644 index e7c1a35..0000000 --- a/src/common/include/types.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef DEF_TYPES_H -#define DEF_TYPES_H - -typedef unsigned long long uint64_t; -typedef unsigned long 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 long unsigned int size_t; - -#define NULL 0 - -#endif diff --git a/src/common/sched.c b/src/common/sched.c deleted file mode 100644 index 29ea8a8..0000000 --- a/src/common/sched.c +++ /dev/null @@ -1,26 +0,0 @@ -#include - -/* 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/common/string.c deleted file mode 100644 index 3825e47..0000000 --- a/src/common/string.c +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include - -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 = malloc(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; - 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; -} - -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/include/errno.h b/src/include/errno.h new file mode 100644 index 0000000..fe60d81 --- /dev/null +++ b/src/include/errno.h @@ -0,0 +1,42 @@ +#ifndef _DEF_ERRNO_H +#define _DEF_ERRNO_H + +// Copied from /usr/include/asm-generic/errno-base.h on a standard Linux system +// Most of these will NOT be used here. + +#define EPERM 1 /* Operation not permitted */ +#define ENOENT 2 /* No such file or directory */ +#define ESRCH 3 /* No such process */ +#define EINTR 4 /* Interrupted system call */ +#define EIO 5 /* I/O error */ +#define ENXIO 6 /* No such device or address */ +#define E2BIG 7 /* Argument list too long */ +#define ENOEXEC 8 /* Exec format error */ +#define EBADF 9 /* Bad file number */ +#define ECHILD 10 /* No child processes */ +#define EAGAIN 11 /* Try again */ +#define ENOMEM 12 /* Out of memory */ +#define EACCES 13 /* Permission denied */ +#define EFAULT 14 /* Bad address */ +#define ENOTBLK 15 /* Block device required */ +#define EBUSY 16 /* Device or resource busy */ +#define EEXIST 17 /* File exists */ +#define EXDEV 18 /* Cross-device link */ +#define ENODEV 19 /* No such device */ +#define ENOTDIR 20 /* Not a directory */ +#define EISDIR 21 /* Is a directory */ +#define EINVAL 22 /* Invalid argument */ +#define ENFILE 23 /* File table overflow */ +#define EMFILE 24 /* Too many open files */ +#define ENOTTY 25 /* Not a typewriter */ +#define ETXTBSY 26 /* Text file busy */ +#define EFBIG 27 /* File too large */ +#define ENOSPC 28 /* No space left on device */ +#define ESPIPE 29 /* Illegal seek */ +#define EROFS 30 /* Read-only file system */ +#define EMLINK 31 /* Too many links */ +#define EPIPE 32 /* Broken pipe */ +#define EDOM 33 /* Math argument out of domain of func */ +#define ERANGE 34 /* Math result not representable */ + +#endif diff --git a/src/include/tce/syscalls.h b/src/include/tce/syscalls.h new file mode 100644 index 0000000..6c67523 --- /dev/null +++ b/src/include/tce/syscalls.h @@ -0,0 +1,38 @@ +#ifndef DEF_TCE_SYSCALLS_H +#define DEF_TCE_SYSCALLS_H + +#define SC_THREAD_EXIT 1 +#define SC_SCHEDULE 2 +#define SC_THREAD_SLEEP 3 +#define SC_PROCESS_EXIT 4 +#define SC_PRINTK 5 +#define SC_THREAD_NEW 6 +#define SC_IRQ_WAIT 7 +#define SC_PROC_PRIV 8 + +#define SC_SBRK 10 +#define SC_BRK 11 +// NOT YET IMPLEMENTED +#define SC_MMAP 12 +#define SC_MUNMAP 13 + +#define SC_OPEN 20 +#define SC_OPEN_RELATIVE 21 +#define SC_STAT 22 +#define SC_STAT_RELATIVE 23 +#define SC_STATF 24 +#define SC_CLOSE 25 +#define SC_READ 26 +#define SC_WRITE 27 +#define SC_LINK 28 + + +// ERRORS +#define E_NOT_IMPLEMENTED -1 +#define E_NOT_FOUND -2 +#define E_INVALID_FD -3 +#define E_TOO_SHORT -4 // not enough space for data to be copied to +#define E_INVALID_RANGE -5 +#define E_INVALID -6 // anything went wrong - invalid parameter, usually + +#endif diff --git a/src/include/tce/vfs.h b/src/include/tce/vfs.h new file mode 100644 index 0000000..20ea03b --- /dev/null +++ b/src/include/tce/vfs.h @@ -0,0 +1,42 @@ +#ifndef DEF_TCE_VFS_H +#define DEF_TCE_VFS_H + +#include + +typedef size_t FILE; + +typedef struct _file_info { + uint32_t type; + uint32_t dev_type; + uint32_t mode; + uint32_t uid, gid; + size_t size; +} file_info; + +// file open flags +#define FM_READ 0x00000001 +#define FM_WRITE 0x00000002 +#define FM_APPEND 0x00000004 +#define FM_TRUNC 0x00000008 +#define FM_CREATE 0x00000010 +#define FM_DELETE 0x00000020 + +// link modes +#define LM_SYMLINK 1 +#define LM_HARDLINK 2 +#define LM_MOUNT 3 +#define LM_OUTPUT_TO 4 + +// file type flags +#define FT_FILE 0x00000001 +#define FT_DIR 0x00000002 +#define FT_SYMLINK 0x00000004 +#define FT_DEV 0x00000008 +#define FT_TERMINAL 0x00000010 + +// device types +#define DT_BLOCK 1 +#define DT_KEYBOARD 2 +#define DT_DISPLAY 3 + +#endif diff --git a/src/include/types.h b/src/include/types.h new file mode 100644 index 0000000..e7c1a35 --- /dev/null +++ b/src/include/types.h @@ -0,0 +1,17 @@ +#ifndef DEF_TYPES_H +#define DEF_TYPES_H + +typedef unsigned long long uint64_t; +typedef unsigned long 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 long unsigned int size_t; + +#define NULL 0 + +#endif 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 + +/* 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 + +#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 /* 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 #include -#include +#include #include 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 #include "lib/std.h" -#include -#include #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 +#include 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 -#include #include "mem.h" #include "seg.h" #include #include #include +#include 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 -#include 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 -#include +#include 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 + +#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 -#include #include void abort(); diff --git a/src/user/lib/include/string.h b/src/user/lib/include/string.h new file mode 100644 index 0000000..f14af35 --- /dev/null +++ b/src/user/lib/include/string.h @@ -0,0 +1,27 @@ +#ifndef DEF_LIB_STRING_H +#define DEF_LIB_STRING_H + +#include + +#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); +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 + + +#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 + +/* 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/user/lib/std/string.c b/src/user/lib/std/string.c new file mode 100644 index 0000000..20fd9e8 --- /dev/null +++ b/src/user/lib/std/string.c @@ -0,0 +1,80 @@ +#include + +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*)malloc(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; +} -- cgit v1.2.3