Working on libc

This commit is contained in:
Ali Mashtizadeh 2014-09-04 18:22:04 -07:00
parent 3ada1a4fcc
commit 173e699a8e
4 changed files with 61 additions and 3 deletions

View File

@ -12,7 +12,8 @@ src_common = [
]
src_amd64 = [
"amd64/syscall.S"
"amd64/entry.S",
"amd64/syscall.S",
]
if (env["ARCH"] == "amd64"):

14
lib/libc/amd64/entry.S Normal file
View File

@ -0,0 +1,14 @@
#
# Program entry point
#
.text
.globl _start
_start:
call main
movq %rax, %rdi
call SystemExit
int $3

View File

@ -4,15 +4,41 @@
#include <syscall.h>
struct atexit_cb {
struct atexit_cb *next;
void (*cb)(void);
};
static uint64_t _atexit_count = 0;
static struct atexit_cb _atexits[32]; // POSIX requires at least 32 atexit functions
static struct atexit_cb *_atexit_last = NULL;
int
atexit(void (*function)(void))
{
if (_atexit_count < 32) {
struct atexit_cb *prev = _atexit_last;
_atexits[_atexit_count].cb = function;
_atexits[_atexit_count].next = prev;
_atexit_last = &_atexits[_atexit_count];
_atexit_count++;
} else {
// XXX: Support malloc
return -1;
}
return 0;
}
void
exit(int status)
{
//
while (_atexit_last != NULL) {
(_atexit_last->cb)();
_atexit_last = _atexit_last->next;
}
SystemExit(status);

View File

@ -37,10 +37,27 @@ SystemMemUnmap(void *addr, uint64_t len)
return syscall(SYSCALL_MUNMAP, addr, len);
}
int
SystemMemProtect(void *addr, uint64_t len, int flags)
{
return syscall(SYSCALL_MPROTECT, addr, len, flags);
}
int
SystemRead(uint64_t fd, void *addr, uint64_t off, uint64_t length)
{
return syscall(SYSCALL_READ, fd, addr, off, length);
}
int
SystemWrite(uint64_t fd, void *addr, uint64_t off, uint64_t length)
{
return syscall(SYSCALL_WRITE, fd, addr, off, length);
}
int
SystemFlush(uint64_t fd)
{
return syscall(SYSCALL_FLUSH, fd);
}