2020 release
This commit is contained in:
commit
61348e8b07
86 changed files with 5127 additions and 0 deletions
196
am/src/native/cte.c
Normal file
196
am/src/native/cte.c
Normal file
|
@ -0,0 +1,196 @@
|
|||
#include <sys/time.h>
|
||||
#include "platform.h"
|
||||
|
||||
#define TIMER_HZ 100
|
||||
|
||||
#define YIELD_INSTR "0xff,0x14,0x25,0x08,0x00,0x10,0x00" // callq *0x100008
|
||||
#define YIELD_INSTR_LEN ((sizeof(YIELD_INSTR)) / 5) // sizeof() counts the '\0' byte
|
||||
#define SYSCALL_INSTR_LEN YIELD_INSTR_LEN
|
||||
|
||||
static_assert(SYSCALL_INSTR_LEN == 7);
|
||||
|
||||
static Context* (*user_handler)(Event, Context*) = NULL;
|
||||
|
||||
void __am_kcontext_start();
|
||||
void __am_switch(Context *c);
|
||||
int __am_in_userspace(void *addr);
|
||||
void __am_pmem_protect();
|
||||
void __am_pmem_unprotect();
|
||||
|
||||
void __am_panic_on_return() { panic("should not reach here\n"); }
|
||||
|
||||
static void irq_handle(Context *c) {
|
||||
c->vm_head = thiscpu->vm_head;
|
||||
c->ksp = thiscpu->ksp;
|
||||
|
||||
c = user_handler(thiscpu->ev, c);
|
||||
assert(c != NULL);
|
||||
|
||||
__am_switch(c);
|
||||
|
||||
// magic call to restore context
|
||||
asm volatile("call *0x100010" : : "a" (c));
|
||||
__am_panic_on_return();
|
||||
}
|
||||
|
||||
static void setup_stack(uintptr_t event, ucontext_t *uc) {
|
||||
void *rip = (void *)uc->uc_mcontext.gregs[REG_RIP];
|
||||
extern uint8_t _start, _etext;
|
||||
int trap_from_user = __am_in_userspace(rip);
|
||||
int signal_safe = IN_RANGE(rip, RANGE(&_start, &_etext)) || trap_from_user ||
|
||||
// Hack here: "+13" points to the instruction after syscall. This is the
|
||||
// instruction which will trigger the pending signal if interrupt is enabled.
|
||||
(rip == (void *)&sigprocmask + 13);
|
||||
|
||||
if (((event == EVENT_IRQ_IODEV) || (event == EVENT_IRQ_TIMER)) && !signal_safe) {
|
||||
// Shared libraries contain code which are not reenterable.
|
||||
// If the signal comes when executing code in shared libraries,
|
||||
// the signal handler can not call any function which is not signal-safe,
|
||||
// else the behavior is undefined (may be dead lock).
|
||||
// To handle this, we just refuse to handle the signal and return directly
|
||||
// to pretend missing the interrupt.
|
||||
// See man 7 signal-safety for more information.
|
||||
return;
|
||||
}
|
||||
|
||||
if (trap_from_user) __am_pmem_unprotect();
|
||||
|
||||
// skip the instructions causing SIGSEGV for syscall and yield
|
||||
if (event == EVENT_SYSCALL) { rip += SYSCALL_INSTR_LEN; }
|
||||
else if (event == EVENT_YIELD) { rip += YIELD_INSTR_LEN; }
|
||||
uc->uc_mcontext.gregs[REG_RIP] = (uintptr_t)rip;
|
||||
|
||||
// switch to kernel stack if we were previously in user space
|
||||
uintptr_t rsp = trap_from_user ? thiscpu->ksp : uc->uc_mcontext.gregs[REG_RSP];
|
||||
rsp -= sizeof(Context);
|
||||
// keep (rsp + 8) % 16 == 0 to support SSE
|
||||
if ((rsp + 8) % 16 != 0) rsp -= 8;
|
||||
Context *c = (void *)rsp;
|
||||
|
||||
// save the context on the stack
|
||||
c->uc = *uc;
|
||||
|
||||
// disable interrupt
|
||||
__am_get_intr_sigmask(&uc->uc_sigmask);
|
||||
|
||||
// call irq_handle after returning from the signal handler
|
||||
uc->uc_mcontext.gregs[REG_RDI] = (uintptr_t)c;
|
||||
uc->uc_mcontext.gregs[REG_RIP] = (uintptr_t)irq_handle;
|
||||
uc->uc_mcontext.gregs[REG_RSP] = (uintptr_t)c;
|
||||
}
|
||||
|
||||
static void iret(ucontext_t *uc) {
|
||||
Context *c = (void *)uc->uc_mcontext.gregs[REG_RAX];
|
||||
// restore the context
|
||||
*uc = c->uc;
|
||||
thiscpu->ksp = c->ksp;
|
||||
if (__am_in_userspace((void *)uc->uc_mcontext.gregs[REG_RIP])) __am_pmem_protect();
|
||||
}
|
||||
|
||||
static void sig_handler(int sig, siginfo_t *info, void *ucontext) {
|
||||
thiscpu->ev = (Event) {0};
|
||||
thiscpu->ev.event = EVENT_ERROR;
|
||||
switch (sig) {
|
||||
case SIGUSR1: thiscpu->ev.event = EVENT_IRQ_IODEV; break;
|
||||
case SIGVTALRM: thiscpu->ev.event = EVENT_IRQ_TIMER; break;
|
||||
case SIGSEGV:
|
||||
if (info->si_code == SEGV_ACCERR) {
|
||||
switch ((uintptr_t)info->si_addr) {
|
||||
case 0x100000: thiscpu->ev.event = EVENT_SYSCALL; break;
|
||||
case 0x100008: thiscpu->ev.event = EVENT_YIELD; break;
|
||||
case 0x100010: iret(ucontext); return;
|
||||
}
|
||||
}
|
||||
if (__am_in_userspace(info->si_addr)) {
|
||||
assert(thiscpu->ev.event == EVENT_ERROR);
|
||||
thiscpu->ev.event = EVENT_PAGEFAULT;
|
||||
switch (info->si_code) {
|
||||
case SEGV_MAPERR: thiscpu->ev.cause = MMAP_READ; break;
|
||||
// we do not support mapped user pages with MMAP_NONE
|
||||
case SEGV_ACCERR: thiscpu->ev.cause = MMAP_WRITE; break;
|
||||
default: assert(0);
|
||||
}
|
||||
thiscpu->ev.ref = (uintptr_t)info->si_addr;
|
||||
}
|
||||
|
||||
if (thiscpu->ev.event == EVENT_ERROR) {
|
||||
uintptr_t rip = ((ucontext_t *)ucontext)->uc_mcontext.gregs[REG_RIP];
|
||||
printf("Unhandle SIGSEGV at rip = %p, badaddr = %p\n", rip, info->si_addr);
|
||||
}
|
||||
break;
|
||||
default: assert(0);
|
||||
}
|
||||
assert(thiscpu->ev.event != EVENT_ERROR);
|
||||
setup_stack(thiscpu->ev.event, ucontext);
|
||||
}
|
||||
|
||||
// signal handlers are inherited across fork()
|
||||
static void install_signal_handler() {
|
||||
struct sigaction s;
|
||||
memset(&s, 0, sizeof(s));
|
||||
s.sa_sigaction = sig_handler;
|
||||
s.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
|
||||
__am_get_intr_sigmask(&s.sa_mask);
|
||||
|
||||
int ret = sigaction(SIGVTALRM, &s, NULL);
|
||||
assert(ret == 0);
|
||||
ret = sigaction(SIGUSR1, &s, NULL);
|
||||
assert(ret == 0);
|
||||
ret = sigaction(SIGSEGV, &s, NULL);
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
// setitimer() are inherited across fork(), should be called again from children
|
||||
void __am_init_timer_irq() {
|
||||
iset(0);
|
||||
|
||||
struct itimerval it = {};
|
||||
it.it_value.tv_sec = 0;
|
||||
it.it_value.tv_usec = 1000000 / TIMER_HZ;
|
||||
it.it_interval = it.it_value;
|
||||
int ret = setitimer(ITIMER_VIRTUAL, &it, NULL);
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
bool cte_init(Context*(*handler)(Event, Context*)) {
|
||||
user_handler = handler;
|
||||
|
||||
install_signal_handler();
|
||||
__am_init_timer_irq();
|
||||
return true;
|
||||
}
|
||||
|
||||
Context* kcontext(Area kstack, void (*entry)(void *), void *arg) {
|
||||
Context *c = (Context*)kstack.end - 1;
|
||||
|
||||
__am_get_example_uc(c);
|
||||
c->uc.uc_mcontext.gregs[REG_RIP] = (uintptr_t)__am_kcontext_start;
|
||||
c->uc.uc_mcontext.gregs[REG_RSP] = (uintptr_t)kstack.end;
|
||||
|
||||
int ret = sigemptyset(&(c->uc.uc_sigmask)); // enable interrupt
|
||||
assert(ret == 0);
|
||||
|
||||
c->vm_head = NULL;
|
||||
|
||||
c->GPR1 = (uintptr_t)arg;
|
||||
c->GPR2 = (uintptr_t)entry;
|
||||
return c;
|
||||
}
|
||||
|
||||
void yield() {
|
||||
asm volatile (".byte " YIELD_INSTR);
|
||||
}
|
||||
|
||||
bool ienabled() {
|
||||
sigset_t set;
|
||||
int ret = sigprocmask(0, NULL, &set);
|
||||
assert(ret == 0);
|
||||
return __am_is_sigmask_sti(&set);
|
||||
}
|
||||
|
||||
void iset(bool enable) {
|
||||
extern sigset_t __am_intr_sigmask;
|
||||
// NOTE: sigprocmask does not supported in multithreading
|
||||
int ret = sigprocmask(enable ? SIG_UNBLOCK : SIG_BLOCK, &__am_intr_sigmask, NULL);
|
||||
assert(ret == 0);
|
||||
}
|
71
am/src/native/ioe.c
Normal file
71
am/src/native/ioe.c
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <am.h>
|
||||
#include <klib-macros.h>
|
||||
|
||||
bool __am_has_ioe = false;
|
||||
static bool ioe_init_done = false;
|
||||
|
||||
void __am_timer_init();
|
||||
void __am_gpu_init();
|
||||
void __am_input_init();
|
||||
void __am_input_config(AM_INPUT_CONFIG_T *);
|
||||
void __am_timer_config(AM_TIMER_CONFIG_T *);
|
||||
void __am_timer_rtc(AM_TIMER_RTC_T *);
|
||||
void __am_timer_uptime(AM_TIMER_UPTIME_T *);
|
||||
void __am_input_keybrd(AM_INPUT_KEYBRD_T *);
|
||||
void __am_gpu_config(AM_GPU_CONFIG_T *);
|
||||
void __am_gpu_status(AM_GPU_STATUS_T *);
|
||||
void __am_gpu_fbdraw(AM_GPU_FBDRAW_T *);
|
||||
void __am_audio_ctrl(AM_AUDIO_CTRL_T *);
|
||||
void __am_audio_status(AM_AUDIO_STATUS_T *);
|
||||
void __am_audio_play(AM_AUDIO_PLAY_T *);
|
||||
static void __am_uart_config(AM_UART_CONFIG_T *cfg) { cfg->present = false; }
|
||||
static void __am_audio_config(AM_AUDIO_CONFIG_T *cfg) { cfg->present = true; }
|
||||
static void __am_disk_config(AM_DISK_CONFIG_T *cfg) { cfg->present = false; }
|
||||
static void __am_net_config (AM_NET_CONFIG_T *cfg) { cfg->present = false; }
|
||||
|
||||
typedef void (*handler_t)(void *buf);
|
||||
static void *lut[128] = {
|
||||
[AM_TIMER_CONFIG] = __am_timer_config,
|
||||
[AM_TIMER_RTC ] = __am_timer_rtc,
|
||||
[AM_TIMER_UPTIME] = __am_timer_uptime,
|
||||
[AM_INPUT_CONFIG] = __am_input_config,
|
||||
[AM_INPUT_KEYBRD] = __am_input_keybrd,
|
||||
[AM_GPU_CONFIG ] = __am_gpu_config,
|
||||
[AM_GPU_FBDRAW ] = __am_gpu_fbdraw,
|
||||
[AM_GPU_STATUS ] = __am_gpu_status,
|
||||
[AM_UART_CONFIG ] = __am_uart_config,
|
||||
[AM_AUDIO_CONFIG] = __am_audio_config,
|
||||
[AM_AUDIO_CTRL ] = __am_audio_ctrl,
|
||||
[AM_AUDIO_STATUS] = __am_audio_status,
|
||||
[AM_AUDIO_PLAY ] = __am_audio_play,
|
||||
[AM_DISK_CONFIG ] = __am_disk_config,
|
||||
[AM_NET_CONFIG ] = __am_net_config,
|
||||
};
|
||||
|
||||
bool ioe_init() {
|
||||
panic_on(cpu_current() != 0, "call ioe_init() in other CPUs");
|
||||
panic_on(ioe_init_done, "double-initialization");
|
||||
__am_has_ioe = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void fail(void *buf) { panic("access nonexist register"); }
|
||||
|
||||
void __am_ioe_init() {
|
||||
for (int i = 0; i < LENGTH(lut); i++)
|
||||
if (!lut[i]) lut[i] = fail;
|
||||
__am_timer_init();
|
||||
__am_gpu_init();
|
||||
__am_input_init();
|
||||
ioe_init_done = true;
|
||||
}
|
||||
|
||||
static void do_io(int reg, void *buf) {
|
||||
if (!ioe_init_done) {
|
||||
__am_ioe_init();
|
||||
}
|
||||
((handler_t)lut[reg])(buf);
|
||||
}
|
||||
|
||||
void ioe_read (int reg, void *buf) { do_io(reg, buf); }
|
||||
void ioe_write(int reg, void *buf) { do_io(reg, buf); }
|
51
am/src/native/mpe.c
Normal file
51
am/src/native/mpe.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include <stdatomic.h>
|
||||
#include "platform.h"
|
||||
|
||||
int __am_mpe_init = 0;
|
||||
extern bool __am_has_ioe;
|
||||
void __am_ioe_init();
|
||||
|
||||
bool mpe_init(void (*entry)()) {
|
||||
__am_mpe_init = 1;
|
||||
|
||||
int sync_pipe[2];
|
||||
assert(0 == pipe(sync_pipe));
|
||||
|
||||
for (int i = 1; i < cpu_count(); i++) {
|
||||
if (fork() == 0) {
|
||||
char ch;
|
||||
assert(read(sync_pipe[0], &ch, 1) == 1);
|
||||
assert(ch == '+');
|
||||
close(sync_pipe[0]); close(sync_pipe[1]);
|
||||
|
||||
thiscpu->cpuid = i;
|
||||
__am_init_timer_irq();
|
||||
entry();
|
||||
}
|
||||
}
|
||||
|
||||
if (__am_has_ioe) {
|
||||
__am_ioe_init();
|
||||
}
|
||||
|
||||
for (int i = 1; i < cpu_count(); i++) {
|
||||
assert(write(sync_pipe[1], "+", 1) == 1);
|
||||
}
|
||||
close(sync_pipe[0]); close(sync_pipe[1]);
|
||||
|
||||
entry();
|
||||
panic("MP entry should not return\n");
|
||||
}
|
||||
|
||||
int cpu_count() {
|
||||
extern int __am_ncpu;
|
||||
return __am_ncpu;
|
||||
}
|
||||
|
||||
int cpu_current() {
|
||||
return thiscpu->cpuid;
|
||||
}
|
||||
|
||||
int atomic_xchg(int *addr, int newval) {
|
||||
return atomic_exchange((int *)addr, newval);
|
||||
}
|
74
am/src/native/native-audio.c
Normal file
74
am/src/native/native-audio.c
Normal file
|
@ -0,0 +1,74 @@
|
|||
#include <klib.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#define SBUF_SIZE_MAX 65536
|
||||
static uint8_t sbuf [SBUF_SIZE_MAX] = {};
|
||||
static int sbuf_size = 0;
|
||||
static int head = 0, tail = 0;
|
||||
static volatile int count = 0;
|
||||
|
||||
static void audio_play(void *userdata, uint8_t *stream, int len) {
|
||||
int nread = len;
|
||||
if (count < len) nread = count;
|
||||
|
||||
if (nread + tail < sbuf_size) {
|
||||
memcpy(stream, sbuf + tail, nread);
|
||||
tail += nread;
|
||||
} else {
|
||||
int first_cpy_len = sbuf_size - tail;
|
||||
memcpy(stream, sbuf + tail, first_cpy_len);
|
||||
memcpy(stream + first_cpy_len, sbuf, nread - first_cpy_len);
|
||||
tail = nread - first_cpy_len;
|
||||
}
|
||||
count -= nread;
|
||||
if (len > nread) memset(stream + nread, 0, len - nread);
|
||||
}
|
||||
|
||||
static int audio_write(uint8_t *buf, int len) {
|
||||
int free = sbuf_size - count;
|
||||
int nwrite = len;
|
||||
if (free < len) nwrite = free;
|
||||
|
||||
if (nwrite + head < sbuf_size) {
|
||||
memcpy(sbuf + head, buf, nwrite);
|
||||
head += nwrite;
|
||||
} else {
|
||||
int first_cpy_len = sbuf_size - head;
|
||||
memcpy(sbuf + head, buf, first_cpy_len);
|
||||
memcpy(sbuf, buf + first_cpy_len, nwrite - first_cpy_len);
|
||||
head = nwrite - first_cpy_len;
|
||||
}
|
||||
count += nwrite;
|
||||
return nwrite;
|
||||
}
|
||||
|
||||
void __am_audio_ctrl(AM_AUDIO_CTRL_T *ctrl) {
|
||||
SDL_AudioSpec s = {};
|
||||
s.freq = ctrl->freq;
|
||||
s.format = AUDIO_S16SYS;
|
||||
s.channels = ctrl->channels;
|
||||
s.samples = ctrl->samples;
|
||||
s.callback = audio_play;
|
||||
s.userdata = NULL;
|
||||
sbuf_size = ctrl->bufsize;
|
||||
assert(sbuf_size <= SBUF_SIZE_MAX);
|
||||
|
||||
head = tail = 0;
|
||||
count = 0;
|
||||
int ret = SDL_InitSubSystem(SDL_INIT_AUDIO);
|
||||
if (ret == 0) {
|
||||
SDL_OpenAudio(&s, NULL);
|
||||
SDL_PauseAudio(0);
|
||||
}
|
||||
}
|
||||
|
||||
void __am_audio_status(AM_AUDIO_STATUS_T *stat) {
|
||||
stat->count = count;
|
||||
}
|
||||
|
||||
void __am_audio_play(AM_AUDIO_PLAY_T *ctl) {
|
||||
int len = ctl->buf.end - ctl->buf.start;
|
||||
assert(len <= sbuf_size);
|
||||
while (sbuf_size - count < len);
|
||||
audio_write(ctl->buf.start, len);
|
||||
}
|
54
am/src/native/native-gpu.c
Normal file
54
am/src/native/native-gpu.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include <am.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#define W 400
|
||||
#define H 300
|
||||
#define FPS 60
|
||||
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
|
||||
static SDL_Texture *texture = NULL;
|
||||
static uint32_t fb[W * H] = {};
|
||||
|
||||
static inline int min(int x, int y) { return (x < y) ? x : y; }
|
||||
|
||||
static Uint32 texture_sync(Uint32 interval, void *param) {
|
||||
SDL_UpdateTexture(texture, NULL, fb, W * sizeof(Uint32));
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
return interval;
|
||||
}
|
||||
|
||||
void __am_gpu_init() {
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
|
||||
SDL_CreateWindowAndRenderer(W * 2, H * 2, 0, &window, &renderer);
|
||||
SDL_SetWindowTitle(window, "Native Application");
|
||||
texture = SDL_CreateTexture(renderer,
|
||||
SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, W, H);
|
||||
memset(fb, 0, W * H * sizeof(uint32_t));
|
||||
SDL_AddTimer(1000 / FPS, texture_sync, NULL);
|
||||
}
|
||||
|
||||
void __am_gpu_config(AM_GPU_CONFIG_T *cfg) {
|
||||
*cfg = (AM_GPU_CONFIG_T) {
|
||||
.present = true, .has_accel = false,
|
||||
.width = W, .height = H,
|
||||
.vmemsz = 0
|
||||
};
|
||||
}
|
||||
|
||||
void __am_gpu_status(AM_GPU_STATUS_T *stat) {
|
||||
stat->ready = true;
|
||||
}
|
||||
|
||||
void __am_gpu_fbdraw(AM_GPU_FBDRAW_T *ctl) {
|
||||
int x = ctl->x, y = ctl->y, w = ctl->w, h = ctl->h;
|
||||
uint32_t *pixels = ctl->pixels;
|
||||
int cp_bytes = sizeof(uint32_t) * min(w, W - x);
|
||||
for (int j = 0; j < h && y + j < H; j ++) {
|
||||
memcpy(&fb[(y + j) * W + x], pixels, cp_bytes);
|
||||
pixels += w;
|
||||
}
|
||||
}
|
63
am/src/native/native-input.c
Normal file
63
am/src/native/native-input.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <am.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include "platform.h"
|
||||
|
||||
#define KEYDOWN_MASK 0x8000
|
||||
|
||||
#define KEY_QUEUE_LEN 1024
|
||||
static int key_queue[KEY_QUEUE_LEN] = {};
|
||||
static int key_f = 0, key_r = 0;
|
||||
static SDL_mutex *key_queue_lock = NULL;
|
||||
|
||||
#define XX(k) [SDL_SCANCODE_##k] = AM_KEY_##k,
|
||||
static int keymap[256] = {
|
||||
AM_KEYS(XX)
|
||||
};
|
||||
|
||||
static int event_thread(void *args) {
|
||||
SDL_Event event;
|
||||
while (1) {
|
||||
SDL_WaitEvent(&event);
|
||||
switch (event.type) {
|
||||
case SDL_QUIT: halt(0);
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP: {
|
||||
SDL_Keysym k = event.key.keysym;
|
||||
int keydown = event.key.type == SDL_KEYDOWN;
|
||||
int scancode = k.scancode;
|
||||
if (keymap[scancode] != 0) {
|
||||
int am_code = keymap[scancode] | (keydown ? KEYDOWN_MASK : 0);
|
||||
SDL_LockMutex(key_queue_lock);
|
||||
key_queue[key_r] = am_code;
|
||||
key_r = (key_r + 1) % KEY_QUEUE_LEN;
|
||||
SDL_UnlockMutex(key_queue_lock);
|
||||
kill(getpid(), SIGUSR1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void __am_input_init() {
|
||||
key_queue_lock = SDL_CreateMutex();
|
||||
SDL_CreateThread(event_thread, "event thread", NULL);
|
||||
}
|
||||
|
||||
void __am_input_config(AM_INPUT_CONFIG_T *cfg) {
|
||||
cfg->present = true;
|
||||
}
|
||||
|
||||
void __am_input_keybrd(AM_INPUT_KEYBRD_T *kbd) {
|
||||
int k = AM_KEY_NONE;
|
||||
|
||||
SDL_LockMutex(key_queue_lock);
|
||||
if (key_f != key_r) {
|
||||
k = key_queue[key_f];
|
||||
key_f = (key_f + 1) % KEY_QUEUE_LEN;
|
||||
}
|
||||
SDL_UnlockMutex(key_queue_lock);
|
||||
|
||||
kbd->keydown = (k & KEYDOWN_MASK ? true : false);
|
||||
kbd->keycode = k & ~KEYDOWN_MASK;
|
||||
}
|
33
am/src/native/native-timer.c
Normal file
33
am/src/native/native-timer.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include <am.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static struct timeval boot_time = {};
|
||||
|
||||
void __am_timer_config(AM_TIMER_CONFIG_T *cfg) {
|
||||
cfg->present = cfg->has_rtc = true;
|
||||
}
|
||||
|
||||
void __am_timer_rtc(AM_TIMER_RTC_T *rtc) {
|
||||
time_t t = time(NULL);
|
||||
struct tm *tm = localtime(&t);
|
||||
rtc->second = tm->tm_sec;
|
||||
rtc->minute = tm->tm_min;
|
||||
rtc->hour = tm->tm_hour;
|
||||
rtc->day = tm->tm_mday;
|
||||
rtc->month = tm->tm_mon + 1;
|
||||
rtc->year = tm->tm_year + 1900;
|
||||
}
|
||||
|
||||
void __am_timer_uptime(AM_TIMER_UPTIME_T *uptime) {
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
long seconds = now.tv_sec - boot_time.tv_sec;
|
||||
long useconds = now.tv_usec - boot_time.tv_usec;
|
||||
uptime->us = seconds * 1000000 + (useconds + 500);
|
||||
}
|
||||
|
||||
void __am_timer_init() {
|
||||
gettimeofday(&boot_time, NULL);
|
||||
}
|
212
am/src/native/platform.c
Normal file
212
am/src/native/platform.c
Normal file
|
@ -0,0 +1,212 @@
|
|||
#define _GNU_SOURCE
|
||||
#include <sys/mman.h>
|
||||
#include <sys/auxv.h>
|
||||
#include <elf.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "platform.h"
|
||||
|
||||
#define MAX_CPU 16
|
||||
#define TRAP_PAGE_START (void *)0x100000
|
||||
#define PMEM_START (void *)0x3000000 // for nanos-lite with vme disabled
|
||||
#define PMEM_SIZE (128 * 1024 * 1024)
|
||||
static int pmem_fd = 0;
|
||||
static void *pmem = NULL;
|
||||
static ucontext_t uc_example = {};
|
||||
static int sys_pgsz;
|
||||
sigset_t __am_intr_sigmask = {};
|
||||
__am_cpu_t *__am_cpu_struct = NULL;
|
||||
int __am_ncpu = 0;
|
||||
int __am_pgsize;
|
||||
|
||||
static void save_context_handler(int sig, siginfo_t *info, void *ucontext) {
|
||||
memcpy(&uc_example, ucontext, sizeof(uc_example));
|
||||
}
|
||||
|
||||
static void save_example_context() {
|
||||
// getcontext() does not save segment registers. In the signal
|
||||
// handler, restoring a context previously saved by getcontext()
|
||||
// will trigger segmentation fault because of the invalid segment
|
||||
// registers. So we save the example context during signal handling
|
||||
// to get a context with everything valid.
|
||||
struct sigaction s;
|
||||
memset(&s, 0, sizeof(s));
|
||||
s.sa_sigaction = save_context_handler;
|
||||
s.sa_flags = SA_SIGINFO;
|
||||
int ret = sigaction(SIGUSR1, &s, NULL);
|
||||
assert(ret == 0);
|
||||
|
||||
raise(SIGUSR1);
|
||||
|
||||
s.sa_flags = 0;
|
||||
s.sa_handler = SIG_DFL;
|
||||
ret = sigaction(SIGUSR1, &s, NULL);
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
static void setup_sigaltstack() {
|
||||
stack_t ss;
|
||||
ss.ss_sp = thiscpu->sigstack;
|
||||
ss.ss_size = sizeof(thiscpu->sigstack);
|
||||
ss.ss_flags = 0;
|
||||
int ret = sigaltstack(&ss, NULL);
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
int main(const char *args);
|
||||
|
||||
static void init_platform() __attribute__((constructor));
|
||||
static void init_platform() {
|
||||
// create memory object and set up mapping to simulate the physical memory
|
||||
pmem_fd = memfd_create("pmem", 0);
|
||||
assert(pmem_fd != -1);
|
||||
assert(0 == ftruncate(pmem_fd, PMEM_SIZE));
|
||||
|
||||
pmem = mmap(PMEM_START, PMEM_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
|
||||
MAP_SHARED | MAP_FIXED, pmem_fd, 0);
|
||||
assert(pmem != (void *)-1);
|
||||
|
||||
// allocate private per-cpu structure
|
||||
thiscpu = mmap(NULL, sizeof(*thiscpu), PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
assert(thiscpu != (void *)-1);
|
||||
thiscpu->cpuid = 0;
|
||||
thiscpu->vm_head = NULL;
|
||||
|
||||
// create trap page to receive syscall and yield by SIGSEGV
|
||||
sys_pgsz = sysconf(_SC_PAGESIZE);
|
||||
void *ret = mmap(TRAP_PAGE_START, sys_pgsz, PROT_NONE,
|
||||
MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
|
||||
assert(ret != (void *)-1);
|
||||
|
||||
// remap writable sections as MAP_SHARED
|
||||
Elf64_Phdr *phdr = (void *)getauxval(AT_PHDR);
|
||||
int phnum = (int)getauxval(AT_PHNUM);
|
||||
int i;
|
||||
int ret2;
|
||||
for (i = 0; i < phnum; i ++) {
|
||||
if (phdr[i].p_type == PT_LOAD && (phdr[i].p_flags & PF_W)) {
|
||||
// allocate temporary memory
|
||||
extern char end;
|
||||
void *vaddr = (void *)&end - phdr[i].p_memsz;
|
||||
uintptr_t pad = (uintptr_t)vaddr & 0xfff;
|
||||
void *vaddr_align = vaddr - pad;
|
||||
uintptr_t size = phdr[i].p_memsz + pad;
|
||||
void *temp_mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
assert(temp_mem != (void *)-1);
|
||||
|
||||
// save data and bss sections
|
||||
memcpy(temp_mem, vaddr_align, size);
|
||||
|
||||
// save the addresses of library functions which will be used after munamp()
|
||||
// since calling the library functions requires accessing GOT, which will be unmapped
|
||||
void *(*volatile mmap_libc)(void *, size_t, int, int, int, off_t) = &mmap;
|
||||
void *(*volatile memcpy_libc)(void *, const void *, size_t) = &memcpy;
|
||||
|
||||
// unmap the data and bss sections
|
||||
ret2 = munmap(vaddr_align, size);
|
||||
assert(ret2 == 0);
|
||||
|
||||
// map the sections again with MAP_SHARED, which will be shared across fork()
|
||||
ret = mmap_libc(vaddr_align, size, PROT_READ | PROT_WRITE | PROT_EXEC,
|
||||
MAP_SHARED | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
|
||||
assert(ret == vaddr_align);
|
||||
|
||||
// restore the data in the sections
|
||||
memcpy_libc(vaddr_align, temp_mem, size);
|
||||
|
||||
// unmap the temporary memory
|
||||
ret2 = munmap(temp_mem, size);
|
||||
assert(ret2 == 0);
|
||||
}
|
||||
}
|
||||
|
||||
// set up the AM heap
|
||||
heap = RANGE(pmem, pmem + PMEM_SIZE);
|
||||
|
||||
// initialize sigmask for interrupts
|
||||
ret2 = sigemptyset(&__am_intr_sigmask);
|
||||
assert(ret2 == 0);
|
||||
ret2 = sigaddset(&__am_intr_sigmask, SIGVTALRM);
|
||||
assert(ret2 == 0);
|
||||
ret2 = sigaddset(&__am_intr_sigmask, SIGUSR1);
|
||||
assert(ret2 == 0);
|
||||
|
||||
// setup alternative signal stack
|
||||
setup_sigaltstack();
|
||||
|
||||
// save the context template
|
||||
save_example_context();
|
||||
__am_get_intr_sigmask(&uc_example.uc_sigmask);
|
||||
|
||||
// disable interrupts by default
|
||||
iset(0);
|
||||
|
||||
// set ncpu
|
||||
const char *smp = getenv("smp");
|
||||
__am_ncpu = smp ? atoi(smp) : 1;
|
||||
assert(0 < __am_ncpu && __am_ncpu <= MAX_CPU);
|
||||
|
||||
// set pgsize
|
||||
const char *pgsize = getenv("pgsize");
|
||||
__am_pgsize = pgsize ? atoi(pgsize) : sys_pgsz;
|
||||
assert(__am_pgsize > 0 && __am_pgsize % sys_pgsz == 0);
|
||||
|
||||
// set stdout unbuffered
|
||||
setbuf(stdout, NULL);
|
||||
|
||||
const char *args = getenv("mainargs");
|
||||
halt(main(args ? args : "")); // call main here!
|
||||
}
|
||||
|
||||
void __am_exit_platform(int code) {
|
||||
// let Linux clean up other resource
|
||||
extern int __am_mpe_init;
|
||||
if (__am_mpe_init && cpu_count() > 1) kill(0, SIGKILL);
|
||||
exit(code);
|
||||
}
|
||||
|
||||
void __am_pmem_map(void *va, void *pa, int prot) {
|
||||
// translate AM prot to mmap prot
|
||||
int mmap_prot = PROT_NONE;
|
||||
// we do not support executable bit, so mark
|
||||
// all readable pages executable as well
|
||||
if (prot & MMAP_READ) mmap_prot |= PROT_READ | PROT_EXEC;
|
||||
if (prot & MMAP_WRITE) mmap_prot |= PROT_WRITE;
|
||||
void *ret = mmap(va, __am_pgsize, mmap_prot,
|
||||
MAP_SHARED | MAP_FIXED, pmem_fd, (uintptr_t)(pa - pmem));
|
||||
assert(ret != (void *)-1);
|
||||
}
|
||||
|
||||
void __am_pmem_unmap(void *va) {
|
||||
int ret = munmap(va, __am_pgsize);
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
void __am_get_example_uc(Context *r) {
|
||||
memcpy(&r->uc, &uc_example, sizeof(uc_example));
|
||||
}
|
||||
|
||||
void __am_get_intr_sigmask(sigset_t *s) {
|
||||
memcpy(s, &__am_intr_sigmask, sizeof(__am_intr_sigmask));
|
||||
}
|
||||
|
||||
int __am_is_sigmask_sti(sigset_t *s) {
|
||||
return !sigismember(s, SIGVTALRM);
|
||||
}
|
||||
|
||||
void __am_pmem_protect() {
|
||||
int ret = mprotect(PMEM_START, PMEM_SIZE, PROT_NONE);
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
void __am_pmem_unprotect() {
|
||||
int ret = mprotect(PMEM_START, PMEM_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
// This dummy function will be called in trm.c.
|
||||
// The purpose of this dummy function is to let linker add this file to the object
|
||||
// file set. Without it, the constructor of @_init_platform will not be linked.
|
||||
void __am_platform_dummy() {
|
||||
}
|
28
am/src/native/platform.h
Normal file
28
am/src/native/platform.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef __PLATFORM_H__
|
||||
#define __PLATFORM_H__
|
||||
|
||||
#include <am.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <klib.h>
|
||||
#include <klib-macros.h>
|
||||
|
||||
void __am_get_example_uc(Context *r);
|
||||
void __am_get_intr_sigmask(sigset_t *s);
|
||||
int __am_is_sigmask_sti(sigset_t *s);
|
||||
void __am_init_timer_irq();
|
||||
void __am_pmem_map(void *va, void *pa, int prot);
|
||||
void __am_pmem_unmap(void *va);
|
||||
|
||||
// per-cpu structure
|
||||
typedef struct {
|
||||
void *vm_head;
|
||||
uintptr_t ksp;
|
||||
int cpuid;
|
||||
Event ev; // similar to cause register in mips/riscv
|
||||
uint8_t sigstack[SIGSTKSZ];
|
||||
} __am_cpu_t;
|
||||
extern __am_cpu_t *__am_cpu_struct;
|
||||
#define thiscpu __am_cpu_struct
|
||||
|
||||
#endif
|
10
am/src/native/trap.S
Normal file
10
am/src/native/trap.S
Normal file
|
@ -0,0 +1,10 @@
|
|||
.global __am_kcontext_start
|
||||
__am_kcontext_start:
|
||||
// rdi = arg, rsi = entry
|
||||
|
||||
// (rsp + 8) should be multiple of 16 when
|
||||
// control is transfered to the function entry point.
|
||||
// See amd64 ABI manual for more details
|
||||
andq $0xfffffffffffffff0, %rsp
|
||||
call *%rsi
|
||||
call __am_panic_on_return
|
22
am/src/native/trm.c
Normal file
22
am/src/native/trm.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <am.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void __am_platform_dummy();
|
||||
void __am_exit_platform(int code);
|
||||
|
||||
void trm_init() {
|
||||
__am_platform_dummy();
|
||||
}
|
||||
|
||||
void putch(char ch) {
|
||||
putchar(ch);
|
||||
}
|
||||
|
||||
void halt(int code) {
|
||||
printf("Exit (%d)\n", code);
|
||||
__am_exit_platform(code);
|
||||
printf("Should not reach here!\n");
|
||||
while (1);
|
||||
}
|
||||
|
||||
Area heap = {};
|
116
am/src/native/vme.c
Normal file
116
am/src/native/vme.c
Normal file
|
@ -0,0 +1,116 @@
|
|||
#include "platform.h"
|
||||
|
||||
#define USER_SPACE RANGE(0x40000000, 0xc0000000)
|
||||
|
||||
typedef struct PageMap {
|
||||
void *va;
|
||||
void *pa;
|
||||
struct PageMap *next;
|
||||
int prot;
|
||||
int is_mapped;
|
||||
} PageMap;
|
||||
|
||||
#define list_foreach(p, head) \
|
||||
for (p = ((PageMap *)(head))->next; p != NULL; p = p->next)
|
||||
|
||||
extern int __am_pgsize;
|
||||
static int vme_enable = 0;
|
||||
static void* (*pgalloc)(int) = NULL;
|
||||
static void (*pgfree)(void *) = NULL;
|
||||
|
||||
bool vme_init(void* (*pgalloc_f)(int), void (*pgfree_f)(void*)) {
|
||||
pgalloc = pgalloc_f;
|
||||
pgfree = pgfree_f;
|
||||
vme_enable = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
void protect(AddrSpace *as) {
|
||||
assert(as != NULL);
|
||||
as->ptr = pgalloc(__am_pgsize); // used as head of the list
|
||||
as->pgsize = __am_pgsize;
|
||||
as->area = USER_SPACE;
|
||||
}
|
||||
|
||||
void unprotect(AddrSpace *as) {
|
||||
}
|
||||
|
||||
void __am_switch(Context *c) {
|
||||
if (!vme_enable) return;
|
||||
|
||||
PageMap *head = c->vm_head;
|
||||
if (head == thiscpu->vm_head) return;
|
||||
|
||||
PageMap *pp;
|
||||
if (thiscpu->vm_head != NULL) {
|
||||
// munmap all mappings
|
||||
list_foreach(pp, thiscpu->vm_head) {
|
||||
if (pp->is_mapped) {
|
||||
__am_pmem_unmap(pp->va);
|
||||
pp->is_mapped = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (head != NULL) {
|
||||
// mmap all mappings
|
||||
list_foreach(pp, head) {
|
||||
assert(IN_RANGE(pp->va, USER_SPACE));
|
||||
__am_pmem_map(pp->va, pp->pa, pp->prot);
|
||||
pp->is_mapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
thiscpu->vm_head = head;
|
||||
}
|
||||
|
||||
void map(AddrSpace *as, void *va, void *pa, int prot) {
|
||||
assert(IN_RANGE(va, USER_SPACE));
|
||||
assert((uintptr_t)va % __am_pgsize == 0);
|
||||
assert((uintptr_t)pa % __am_pgsize == 0);
|
||||
assert(as != NULL);
|
||||
PageMap *pp = NULL;
|
||||
PageMap *vm_head = as->ptr;
|
||||
assert(vm_head != NULL);
|
||||
list_foreach(pp, vm_head) {
|
||||
if (pp->va == va) break;
|
||||
}
|
||||
|
||||
if (pp == NULL) {
|
||||
pp = pgalloc(__am_pgsize); // this will waste memory, any better idea?
|
||||
}
|
||||
pp->va = va;
|
||||
pp->pa = pa;
|
||||
pp->prot = prot;
|
||||
pp->is_mapped = false;
|
||||
// add after to vm_head to keep vm_head unchanged,
|
||||
// since vm_head is a key to describe an address space
|
||||
pp->next = vm_head->next;
|
||||
vm_head->next = pp;
|
||||
|
||||
if (vm_head == thiscpu->vm_head) {
|
||||
// enforce the map immediately
|
||||
__am_pmem_map(pp->va, pp->pa, pp->prot);
|
||||
pp->is_mapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
Context* ucontext(AddrSpace *as, Area kstack, void *entry) {
|
||||
Context *c = (Context*)kstack.end - 1;
|
||||
|
||||
__am_get_example_uc(c);
|
||||
c->uc.uc_mcontext.gregs[REG_RIP] = (uintptr_t)entry;
|
||||
c->uc.uc_mcontext.gregs[REG_RSP] = (uintptr_t)USER_SPACE.end;
|
||||
|
||||
int ret = sigemptyset(&(c->uc.uc_sigmask)); // enable interrupt
|
||||
assert(ret == 0);
|
||||
c->vm_head = as->ptr;
|
||||
|
||||
c->ksp = (uintptr_t)kstack.end;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
int __am_in_userspace(void *addr) {
|
||||
return vme_enable && thiscpu->vm_head != NULL && IN_RANGE(addr, USER_SPACE);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue