Files
linux-kernel-module-cheat/baremetal/lib/syscalls.c
Ciro Santilli 六四事件 法轮功 f28191a735 baremetal aarch64: create C version of multicore.S as well
Attempted to do the same for arm, but it failed.
2019-08-21 00:00:00 +00:00

101 lines
2.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <lkmc.h>
enum {
UART_FR_RXFE = 0x10,
};
#define UART_DR(baseaddr) (*(unsigned int *)(baseaddr))
#define UART_FR(baseaddr) (*(((unsigned int *)(baseaddr))+6))
void lkmc_baremetal_on_exit_callback(int status, void *arg) {
(void)arg;
if (status != 0) {
printf("lkmc_exit_status_%d\n", status);
}
}
int _close(int file) {
LKMC_UNUSED(file);
return -1;
}
int _fstat(int file, struct stat *st) {
LKMC_UNUSED(file);
st->st_mode = S_IFCHR;
return 0;
}
/* Required by assert. */
int _getpid(void) { return 0; }
/* Required by assert. */
int _kill(pid_t pid, int sig) {
LKMC_UNUSED(pid);
exit(128 + sig);
}
int _isatty(int file) {
LKMC_UNUSED(file);
return 1;
}
int _lseek(int file, int ptr, int dir) {
LKMC_UNUSED(file);
LKMC_UNUSED(ptr);
LKMC_UNUSED(dir);
return 0;
}
int _open(const char *name, int flags, int mode) {
LKMC_UNUSED(name);
LKMC_UNUSED(flags);
LKMC_UNUSED(mode);
return -1;
}
int _read(int file, char *ptr, int len) {
int todo;
LKMC_UNUSED(file);
if (len == 0)
return 0;
while (UART_FR(LKMC_UART0_ADDR) & UART_FR_RXFE);
*ptr++ = UART_DR(LKMC_UART0_ADDR);
for (todo = 1; todo < len; todo++) {
if (UART_FR(LKMC_UART0_ADDR) & UART_FR_RXFE) {
break;
}
*ptr++ = UART_DR(LKMC_UART0_ADDR);
}
return todo;
}
char *heap_end = 0;
caddr_t _sbrk(int incr) {
extern char heap_low;
extern char heap_top;
char *prev_heap_end;
if (heap_end == 0) {
heap_end = &heap_low;
}
prev_heap_end = heap_end;
if (heap_end + incr > &heap_top) {
/* Heap and stack collision */
return (caddr_t)0;
}
heap_end += incr;
return (caddr_t)prev_heap_end;
}
int _write(int file, char *ptr, int len) {
int todo;
LKMC_UNUSED(file);
for (todo = 0; todo < len; todo++) {
UART_DR(LKMC_UART0_ADDR) = *ptr++;
}
return len;
}