Browse Source

Don't rely on the C library

pull/4/head
Andrew Waterman 12 years ago
parent
commit
fda0d85ae4
  1. 2
      Makefile.in
  2. 2
      configure
  3. 1
      configure.ac
  4. 1
      pk/file.h
  5. 4
      pk/handlers.c
  6. 1
      pk/pk.mk.in
  7. 53
      pk/string.c
  8. 12
      pk/syscall.c
  9. 3
      pk/syscall.h

2
Makefile.in

@ -91,7 +91,7 @@ COMPILE := $(CC) -MMD -MP $(CFLAGS) \
# - LIBS : Library flags (eg. -l)
LD := $(CC)
LDFLAGS := @LDFLAGS@ -nostartfiles -nostdlib $(LDFLAGS)
LDFLAGS := @LDFLAGS@ -nostartfiles -nostdlib -static $(LDFLAGS)
LIBS := @LIBS@
LINK := $(LD) $(LDFLAGS) -T $(src_dir)/pk/pk.ld

2
configure

@ -3953,8 +3953,6 @@ fi
CFLAGS="-Wall -Os -std=gnu99 -Wno-unused -Wno-attributes -fno-delete-null-pointer-checks"
LIBS="-lc -lgcc"
{ $as_echo "$as_me:${as_lineno-$LINENO}: adding default configure arg: --host=riscv" >&5

1
configure.ac

@ -72,7 +72,6 @@ AC_HEADER_STDC
#-------------------------------------------------------------------------
AC_SUBST([CFLAGS], ["-Wall -Os -std=gnu99 -Wno-unused -Wno-attributes -fno-delete-null-pointer-checks"])
AC_SUBST([LIBS], ["-lc -lgcc"])
AX_DEFAULT_CONFIGURE_ARG([--host=riscv])

1
pk/file.h

@ -4,6 +4,7 @@
#define _FILE_H
#include <sys/stat.h>
#include <unistd.h>
#include "atomic.h"
typedef struct file

4
pk/handlers.c

@ -112,8 +112,8 @@ void handle_fault_store(trapframe_t* tf)
static void handle_syscall(trapframe_t* tf)
{
tf->gpr[16] = syscall(tf->gpr[18], tf->gpr[19], tf->gpr[20], tf->gpr[21],
tf->gpr[22], tf->gpr[23], tf->gpr[16]);
tf->gpr[16] = do_syscall(tf->gpr[18], tf->gpr[19], tf->gpr[20], tf->gpr[21],
tf->gpr[22], tf->gpr[23], tf->gpr[16]);
tf->epc += 4;
}

1
pk/pk.mk.in

@ -24,6 +24,7 @@ pk_c_srcs = \
elf.c \
console.c \
vm.c \
string.c \
pk_asm_srcs = \
entry.S \

53
pk/string.c

@ -0,0 +1,53 @@
#include <string.h>
#include <stdint.h>
void* memcpy(void* dest, const void* src, size_t len)
{
if ((((uintptr_t)dest | (uintptr_t)src | len) & (sizeof(uintptr_t)-1)) == 0) {
const uintptr_t* s = src;
uintptr_t *d = dest;
while (d < (uintptr_t*)(dest + len))
*d++ = *s++;
} else {
const char* s = src;
char *d = dest;
while (d < (char*)(dest + len))
*d++ = *s++;
}
return dest;
}
void* memset(void* dest, int byte, size_t len)
{
if ((((uintptr_t)dest | len) & (sizeof(uintptr_t)-1)) == 0) {
uintptr_t word = byte & 0xFF;
word |= word << 8;
word |= word << 16;
word |= word << 16 << 16;
uintptr_t *d = dest;
while (d < (uintptr_t*)(dest + len))
*d++ = word;
} else {
char *d = dest;
while (d < (char*)(dest + len))
*d++ = byte;
}
return dest;
}
size_t strlen(const char *s)
{
const char *p = s;
while (*p)
p++;
return p - s;
}
char* strcpy(char* dest, const char* src)
{
char* d = dest;
while ((*d++ = *src++))
;
return dest;
}

12
pk/syscall.c

@ -384,12 +384,17 @@ int sys_getdents(int fd, void* dirbuf, int count)
return 0; //stub
}
int sys_nosys()
static int sys_stub_success()
{
return 0;
}
static int sys_stub_nosys()
{
return -ENOSYS;
}
long syscall(long a0, long a1, long a2, long a3, long a4, long a5, long n)
long do_syscall(long a0, long a1, long a2, long a3, long a4, long a5, long n)
{
const static void* syscall_table[] = {
[SYS_exit] = sys_exit,
@ -430,7 +435,8 @@ long syscall(long a0, long a1, long a2, long a3, long a4, long a5, long n)
[SYS_fcntl] = sys_fcntl,
[SYS_getdents] = sys_getdents,
[SYS_dup] = sys_dup,
[SYS_readlinkat] = sys_nosys,
[SYS_readlinkat] = sys_stub_nosys,
[SYS_rt_sigprocmask] = sys_stub_success,
};
if(n >= ARRAY_SIZE(syscall_table) || !syscall_table[n])

3
pk/syscall.h

@ -46,12 +46,13 @@
#define SYS_getdents 61
#define SYS_dup 23
#define SYS_readlinkat 78
#define SYS_rt_sigprocmask 135
#define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-4096)
#define ERR_PTR(x) ((void*)(long)(x))
#define PTR_ERR(x) ((long)(x))
void sys_exit(int code) __attribute__((noreturn));
long syscall(long a0, long a1, long a2, long a3, long a4, long a5, long n);
long do_syscall(long a0, long a1, long a2, long a3, long a4, long a5, long n);
#endif

Loading…
Cancel
Save