Make yield a regular syscall taking an argument.

Allows "wait for interrupt"/"wait for event" type operation where VM code blocks until the host has something for it.
This commit is contained in:
Toby Jaffey 2025-12-10 10:35:18 +00:00
parent d52baca7b2
commit e07eeab043
14 changed files with 88 additions and 24 deletions

View file

@ -57,6 +57,8 @@ void loop(void) {
break;
case UVM32_EVT_SYSCALL: // vm has paused to handle UVM32_SYSCALL
switch(evt.data.syscall.code) {
case UVM32_SYSCALL_YIELD:
break;
case UVM32_SYSCALL_PUTC:
Serial.print((char)uvm32_getval(&vmst, &evt, ARG0));
break;

View file

@ -1,4 +1,5 @@
#include "config.h"
#define MINIRV32_IMPLEMENTATION
#include "uvm32.h"
@ -11,6 +12,9 @@
// On an invalid operation, an error is set in uvm32_state_t, but a valid pointer still needs to be temporarily used
static uint32_t garbage;
// magic value for stack canary
#define STACK_CANARY_VALUE 0x42
#ifndef UVM32_MEMCPY
#define UVM32_MEMCPY uvm32_memcpy
void uvm32_memcpy(void *dst, const void *src, int len) {
@ -85,6 +89,8 @@ bool uvm32_load(uvm32_state_t *vmst, const uint8_t *rom, int len) {
}
UVM32_MEMCPY(vmst->memory, rom, len);
vmst->stack_canary = (uint8_t *)UVM32_NULL;
return true;
}
@ -136,6 +142,12 @@ void uvm32_clearError(uvm32_state_t *vmst) {
uint32_t uvm32_run(uvm32_state_t *vmst, uvm32_evt_t *evt, uint32_t instr_meter) {
uint32_t num_instr = 0;
if (vmst->stack_canary != UVM32_NULL && *vmst->stack_canary != STACK_CANARY_VALUE) {
setStatusErr(vmst, UVM32_ERR_STACKOVERFLOW);
setup_err_evt(vmst, evt);
return num_instr;
}
if (vmst->status != UVM32_STATUS_PAUSED) {
setStatusErr(vmst, UVM32_ERR_NOTREADY);
setup_err_evt(vmst, evt);
@ -160,10 +172,25 @@ uint32_t uvm32_run(uvm32_state_t *vmst, uvm32_evt_t *evt, uint32_t instr_meter)
case UVM32_SYSCALL_HALT:
setStatus(vmst, UVM32_STATUS_ENDED);
break;
case UVM32_SYSCALL_YIELD:
vmst->ioevt.typ = UVM32_EVT_YIELD;
setStatus(vmst, UVM32_STATUS_PAUSED);
break;
case UVM32_SYSCALL_STACKPROTECT: {
// don't allow errant code to change it once set
if (vmst->stack_canary == (uint8_t *)UVM32_NULL) {
uint32_t param0 = vmst->core.regs[10]; // a0
uint32_t mem_offset = param0 - MINIRV32_RAM_IMAGE_OFFSET;
// check data fits in ram
if (mem_offset > UVM32_MEMORY_SIZE) {
setStatusErr(vmst, UVM32_ERR_STACKOVERFLOW);
setup_err_evt(vmst, evt);
}
// check canary is inside valid memory
if (mem_offset < UVM32_MEMORY_SIZE) {
// set canary
vmst->stack_canary = &vmst->memory[mem_offset];
*vmst->stack_canary = STACK_CANARY_VALUE;
}
}
} break;
default:
// user defined syscalls
vmst->ioevt.typ = UVM32_EVT_SYSCALL;