vme: use mkstemp to surpress warning; update test code
This commit is contained in:
parent
526da7b029
commit
bfd47e199c
|
@ -55,7 +55,7 @@ int main(const char *args);
|
|||
static void init_platform() __attribute__((constructor));
|
||||
static void init_platform() {
|
||||
// create shared memory object and set up mapping to simulate the physical memory
|
||||
mktemp(pmem_shm_file);
|
||||
assert(mkstemp(pmem_shm_file) < 0);
|
||||
pmem_fd = shm_open(pmem_shm_file, O_RDWR | O_CREAT | O_EXCL, 0700);
|
||||
assert(pmem_fd != -1);
|
||||
assert(0 == ftruncate(pmem_fd, PMEM_SIZE));
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#include <amtest.h>
|
||||
|
||||
static _Context uctx;
|
||||
static _Context *uctx;
|
||||
static _AddressSpace prot;
|
||||
static uintptr_t st = 0;
|
||||
static _Context *current = NULL;
|
||||
static int first_trap = 1;
|
||||
|
||||
void *simple_pgalloc(size_t size) {
|
||||
if (st == 0) { st = (uintptr_t)_heap.start; }
|
||||
|
@ -17,17 +17,12 @@ void simple_pgfree(void *ptr) {
|
|||
}
|
||||
|
||||
_Context* vm_handler(_Event ev, _Context *ctx) {
|
||||
if (current) {
|
||||
*current = *ctx;
|
||||
} else {
|
||||
current = &uctx;
|
||||
}
|
||||
switch (ev.event) {
|
||||
case _EVENT_YIELD:
|
||||
break;
|
||||
case _EVENT_IRQ_TIMER:
|
||||
case _EVENT_IRQ_IODEV:
|
||||
printf("==== interrupt (%s) ===\n", ev.msg);
|
||||
printf("==== interrupt (%s) ====\n", ev.msg);
|
||||
break;
|
||||
case _EVENT_PAGEFAULT:
|
||||
printf("PF: %x %s%s%s\n",
|
||||
|
@ -37,12 +32,18 @@ _Context* vm_handler(_Event ev, _Context *ctx) {
|
|||
(ev.cause & _PROT_WRITE) ? "[write fail]" : "");
|
||||
break;
|
||||
case _EVENT_SYSCALL:
|
||||
_intr_write(1);
|
||||
printf("%d ", ctx->GPRx);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
return current;
|
||||
if (first_trap) {
|
||||
first_trap = 0;
|
||||
return uctx;
|
||||
} else {
|
||||
return ctx;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t code[] = {
|
||||
|
@ -55,25 +56,20 @@ uint8_t code[] = {
|
|||
};
|
||||
|
||||
void vm_test() {
|
||||
if (strncmp(__ISA__, "x86", 3) != 0) {
|
||||
printf("VM test: only runs on x86.\n");
|
||||
return;
|
||||
}
|
||||
_protect(&prot);
|
||||
printf("Protected address space: [%p, %p)\n", prot.area.start, prot.area.end);
|
||||
|
||||
uint8_t *ptr = (void*)((uintptr_t)(prot.area.start) +
|
||||
((uintptr_t)(prot.area.end) - (uintptr_t)(prot.area.start)) / 2);
|
||||
|
||||
int pgsz = 4096;
|
||||
void *pg = simple_pgalloc(prot.pgsize);
|
||||
memcpy(pg, code, sizeof(code));
|
||||
|
||||
void *up1 = simple_pgalloc(pgsz);
|
||||
_map(&prot, ptr, up1, _PROT_WRITE);
|
||||
_map(&prot, ptr, pg, _PROT_WRITE | _PROT_READ | _PROT_EXEC);
|
||||
printf("Code copied to %p (physical %p) execute\n", ptr, pg);
|
||||
|
||||
memcpy(up1, code, sizeof(code));
|
||||
printf("Code copied to %p (physical %p) execute\n", ptr, up1);
|
||||
|
||||
static uint8_t kstk[4096];
|
||||
_ucontext(&uctx, &prot, RANGE(kstk, kstk + 4096), ptr);
|
||||
static uint8_t stack[4096];
|
||||
uctx = _ucontext(&prot, RANGE(stack, stack + sizeof(stack)), ptr);
|
||||
|
||||
_intr_write(1);
|
||||
while (1) ;
|
||||
|
|
Loading…
Reference in New Issue