summaryrefslogtreecommitdiff
path: root/src/user/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/lib')
-rw-r--r--src/user/lib/Makefile5
-rw-r--r--src/user/lib/include/sched.h24
-rw-r--r--src/user/lib/include/stdlib.h1
-rw-r--r--src/user/lib/include/string.h27
-rw-r--r--src/user/lib/std/sched.c26
-rw-r--r--src/user/lib/std/string.c80
6 files changed, 159 insertions, 4 deletions
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/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 <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);
+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 <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/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 <string.h>
+
+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;
+}