From 5cac9acd3aedc8043d4272d93c56805c46ff6214 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Tue, 1 May 2012 12:20:45 +0200 Subject: Some cleanup ; relocated the kernel at 0xC0000000 --- src/user/lib/std/mutex.c | 22 +++++++++++++++++++++ src/user/lib/std/stdio.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/user/lib/std/mutex.c create mode 100644 src/user/lib/std/stdio.c (limited to 'src/user/lib/std') diff --git a/src/user/lib/std/mutex.c b/src/user/lib/std/mutex.c new file mode 100644 index 0000000..ac0ee8f --- /dev/null +++ b/src/user/lib/std/mutex.c @@ -0,0 +1,22 @@ +#include + +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; +} + +void mutex_lock(uint32_t* mutex) { + while (atomic_exchange(mutex, MUTEX_LOCKED) == MUTEX_LOCKED) { + thread_sleep(1); + } +} + +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/stdio.c b/src/user/lib/std/stdio.c new file mode 100644 index 0000000..3b24da1 --- /dev/null +++ b/src/user/lib/std/stdio.c @@ -0,0 +1,51 @@ +#include + +void printk_int(int number) { + if (number == 0) { + printk("0"); + return; + } + int negative = 0; + if (number < 0) { + negative = 1; + number = 0 - number; + } + int order = 0, temp = number, i; + char numbers[] = "0123456789"; + while (temp > 0) { + order++; + temp /= 10; + } + + char *s, *r; + s = malloc(order + (negative ? 2 : 1)); + if (negative) { + s[0] = '-'; + r = s + 1; + } else { + r = s; + } + + for (i = order; i > 0; i--) { + r[i - 1] = numbers[number % 10]; + number /= 10; + } + r[order] = 0; + printk(s); + free(s); +} + +void printk_hex(unsigned v) { + char s[11] = {'0', 'x', 0}; + + int i; + + char hexdigits[] = "0123456789ABCDEF"; + + for (i = 0; i < 8; i++) { + s[i + 2] = (hexdigits[v >> 28]); + v = v << 4; + } + s[11] = 0; + printk(s); +} -- cgit v1.2.3