Files
beaglefw/syscall.c

119 lines
2.3 KiB
C

#include <sys/stat.h>
#include <stdlib.h>
#include "cortexa8.hh"
#include "mm.hh"
#include <errno.h>
#undef errno
extern int errno;
#include "uart.hh"
static ICharacterDevice* console = nullptr;
void setConsole(ICharacterDevice* newConsole) {
console = newConsole;
}
#ifdef __cplusplus
extern "C" {
#endif
int _close(int file) __attribute__((used));
int _close(int file) {
return -1;
}
int _lseek(int file, int ptr, int dir) __attribute__((used));
int _lseek(int file, int ptr, int dir) {
return 0;
}
int _fstat(int file, struct stat *st) __attribute__((used));
int _fstat(int file, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
int _isatty(int file) __attribute__((used));
int _isatty(int file) {
return 1;
}
int _read(int file, char *ptr, int len) __attribute__((used));
int _read(int file, char *ptr, int len) {
/* if(file != 0) */
/* return 0; */
if(console)
return console->read(ptr, len);
else {
errno = EIO;
return -1;
}
}
int _write(int file, const char *ptr, int len) __attribute__((used));
int _write(int file, const char *ptr, int len) {
// if(file != 1 && file != 2) // Only stdout supported
//return 0;
if(console) {
console->write(ptr, len);
return len;
} else {
errno = EIO;
return -1;
}
}
extern uint32_t __heap_start;
caddr_t _sbrk(int incr) __attribute__((used));
caddr_t _sbrk(int incr) {
static uintptr_t heap_end = 0;
static uintptr_t brk;
if(heap_end == 0) {
heap_end = mm::get_heap_end();
brk = (uintptr_t)&__heap_start;
}
if(brk + incr >= heap_end) {
// Allocate additional RAM for heap
try {
unsigned pages = (brk+incr-heap_end+1)/4096;
if((brk+incr-heap_end+1)%4096 != 0)
++pages;
heap_end = mm::grow_heap(pages);
} catch (ex::bad_alloc &ex) {
_write(1, "Heap allocation failure\n", 24);
abort();
}
}
caddr_t prev_brk = (caddr_t)brk;
brk += incr;
return prev_brk;
}
int _kill(int pid, int sig) __attribute__((used));
int _kill(int pid, int sig) {
errno = EINVAL;
return -1;
}
int _getpid(void) __attribute__((used));
int _getpid(void) {
return 1;
}
int _open(const char *name, int flags, int mode) __attribute__((used));
int _open(const char *name, int flags, int mode) {
return -1;
}
#ifdef __cplusplus
}
#endif