Rework host/target interface to use ecall and proper syscalls instead of CSRs

This commit is contained in:
Toby Jaffey 2025-12-08 02:44:38 +00:00
parent a305fce466
commit 751f068486
26 changed files with 360 additions and 364 deletions

View file

@ -3,25 +3,28 @@
#include "common/uvm32_common_custom.h"
// Precompiled binary program to print integers
// This code expects to print via CSR 0x13C (IOREQ_PRINTD in common/uvm32_common_custom.h)
// This code expects to print via syscall 0x13C (IOREQ_PRINTD in common/uvm32_common_custom.h)
uint8_t rom[] = {
0x23, 0x26, 0x11, 0x00, 0xef, 0x00, 0x80, 0x00, 0x73, 0x50, 0x80, 0x13,
0x37, 0xf6, 0xff, 0xff, 0xb7, 0x17, 0x00, 0x00, 0x37, 0xe7, 0xff, 0xff,
0x13, 0x05, 0xf0, 0x01, 0xb7, 0x45, 0x00, 0x00, 0x13, 0x06, 0xd6, 0xcc,
0x93, 0x86, 0x37, 0x33, 0x13, 0x07, 0x77, 0xe6, 0x93, 0x87, 0x37, 0xb3,
0x13, 0x08, 0xa0, 0x00, 0x63, 0xcc, 0xc6, 0x06, 0x93, 0x08, 0x07, 0x00,
0x63, 0xc2, 0xe7, 0x06, 0x93, 0x03, 0x00, 0x00, 0x13, 0x03, 0x00, 0x00,
0x13, 0x0e, 0x00, 0x00, 0x93, 0x0e, 0x00, 0x00, 0x93, 0x02, 0x00, 0x02,
0x13, 0x8f, 0x02, 0xfe, 0x63, 0x6e, 0xe5, 0x03, 0x33, 0x0f, 0x73, 0x00,
0x63, 0xea, 0xe5, 0x03, 0x33, 0x8e, 0xce, 0x03, 0xb3, 0x0e, 0x73, 0x40,
0x73, 0x50, 0x90, 0x13, 0x13, 0x5e, 0xbe, 0x40, 0xb3, 0x8e, 0x1e, 0x01,
0x33, 0x0e, 0xce, 0x00, 0x33, 0x83, 0xde, 0x03, 0x13, 0x53, 0xc3, 0x00,
0xb3, 0x03, 0xce, 0x03, 0x93, 0xd3, 0xc3, 0x00, 0x93, 0x82, 0x12, 0x00,
0x6f, 0xf0, 0x5f, 0xfc, 0x73, 0x90, 0x02, 0x14, 0x93, 0x88, 0x18, 0x09,
0xe3, 0xd2, 0x17, 0xfb, 0x73, 0x10, 0x08, 0x14, 0x13, 0x06, 0x96, 0x19,
0xe3, 0xd8, 0xc6, 0xf8, 0x37, 0x05, 0x00, 0x80, 0x13, 0x05, 0x05, 0x0c,
0x73, 0x10, 0xb5, 0x13, 0x67, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x00
0x23, 0x26, 0x11, 0x00, 0xef, 0x00, 0xc0, 0x00, 0x93, 0x08, 0x80, 0x13,
0x73, 0x00, 0x00, 0x00, 0x37, 0xf5, 0xff, 0xff, 0xb7, 0x15, 0x00, 0x00,
0x37, 0xe6, 0xff, 0xff, 0x93, 0x06, 0xf0, 0x01, 0x13, 0x07, 0xd5, 0xcc,
0x93, 0x87, 0x35, 0x33, 0x13, 0x08, 0x76, 0xe6, 0x93, 0x82, 0x35, 0xb3,
0x37, 0x43, 0x00, 0x00, 0x63, 0xc8, 0xe7, 0x08, 0x93, 0x03, 0x08, 0x00,
0x63, 0xca, 0x02, 0x07, 0x93, 0x08, 0x00, 0x00, 0x93, 0x05, 0x00, 0x00,
0x13, 0x0e, 0x00, 0x00, 0x93, 0x0e, 0x00, 0x00, 0x13, 0x06, 0x00, 0x02,
0x13, 0x05, 0x06, 0xfe, 0x63, 0xe2, 0xa6, 0x04, 0x33, 0x85, 0x15, 0x01,
0x63, 0x6e, 0xa3, 0x02, 0x13, 0x05, 0x00, 0x00, 0x33, 0x8e, 0xce, 0x03,
0xb3, 0x8e, 0x15, 0x41, 0x93, 0x08, 0x90, 0x13, 0x73, 0x00, 0x00, 0x00,
0x13, 0x5e, 0xbe, 0x40, 0xb3, 0x8e, 0x7e, 0x00, 0x33, 0x0e, 0xee, 0x00,
0xb3, 0x85, 0xde, 0x03, 0x93, 0xd5, 0xc5, 0x00, 0x33, 0x05, 0xce, 0x03,
0x93, 0x58, 0xc5, 0x00, 0x13, 0x06, 0x16, 0x00, 0x6f, 0xf0, 0xdf, 0xfb,
0x93, 0x08, 0x00, 0x14, 0x13, 0x05, 0x06, 0x00, 0x73, 0x00, 0x00, 0x00,
0x93, 0x83, 0x13, 0x09, 0xe3, 0xda, 0x72, 0xf8, 0x13, 0x05, 0xa0, 0x00,
0x93, 0x08, 0x00, 0x14, 0x73, 0x00, 0x00, 0x00, 0x13, 0x07, 0x97, 0x19,
0xe3, 0xdc, 0xe7, 0xf6, 0x37, 0x05, 0x00, 0x80, 0x13, 0x05, 0x05, 0x0e,
0x93, 0x08, 0xb0, 0x13, 0x73, 0x00, 0x00, 0x00, 0x67, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x65, 0x6c, 0x6c,
0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x00
};
// Create an identifier for our host handler
@ -98,5 +101,5 @@ void loop(void) {
delay(2000);
}
return 0;
return;
}

View file

@ -24,7 +24,7 @@ static void setup_err_evt(uvm32_state_t *vmst, uvm32_evt_t *evt) {
static void setStatus(uvm32_state_t *vmst, uvm32_status_t newStatus) {
if (vmst->status == UVM32_STATUS_ERROR) {
// always stay in error state until a uvm32_reset()
// always stay in error state until a uvm32_init()
return;
} else {
vmst->status = newStatus;
@ -47,7 +47,7 @@ void uvm32_init(uvm32_state_t *vmst, const uvm32_mapping_t *mappings, uint32_t n
// setup stack pointer
// la sp, _sstack
// addi sp,sp,-16
vmst->core->regs[2] = (MINIRV32_RAM_IMAGE_OFFSET + UVM32_MEMORY_SIZE) - 16;
vmst->core->regs[2] = (MINIRV32_RAM_IMAGE_OFFSET + UVM32_MEMORY_SIZE - sizeof(struct MiniRV32IMAState)) - 16;
vmst->core->regs[10] = 0x00; //hart ID
vmst->core->regs[11] = 0;
vmst->core->extraflags |= 3; // Machine-mode.
@ -67,8 +67,45 @@ bool uvm32_load(uvm32_state_t *vmst, uint8_t *rom, int len) {
return true;
}
// Read C-string up to terminator and return len,ptr
static void get_safeptr_terminated(uvm32_state_t *vmst, uint32_t addr, uint8_t terminator, uvm32_evt_ioreq_buf_t *buf) {
uint32_t ptrstart = addr - MINIRV32_RAM_IMAGE_OFFSET;
uint32_t p = ptrstart;
if (p >= UVM32_MEMORY_SIZE) {
setStatusErr(vmst, UVM32_ERR_MEM_RD);
buf->ptr = NULL;
buf->len = 0;
return;
}
while(vmst->memory[p] != terminator) {
p++;
if (p >= UVM32_MEMORY_SIZE) {
setStatusErr(vmst, UVM32_ERR_MEM_RD);
buf->ptr = NULL;
buf->len = 0;
return;
}
}
buf->ptr = &vmst->memory[ptrstart];
buf->len = p - ptrstart;
}
#if 0
static void get_safeptr(uvm32_state_t *vmst, uint32_t addr, uint32_t len, uvm32_evt_ioreq_buf_t *buf) {
uint32_t ptrstart = addr - MINIRV32_RAM_IMAGE_OFFSET;
if (ptrstart + len >= UVM32_MEMORY_SIZE) {
setStatusErr(vmst, UVM32_ERR_MEM_RD);
}
buf->ptr = &vmst->memory[ptrstart];
buf->len = len;
}
#endif
uint32_t uvm32_run(uvm32_state_t *vmst, uvm32_evt_t *evt, uint32_t instr_meter) {
uint32_t num_instr = 0;
// uvm32_evt_ioreq_buf_t b;
if (vmst->status != UVM32_STATUS_PAUSED) {
setStatusErr(vmst, UVM32_ERR_NOTREADY);
setup_err_evt(vmst, evt);
@ -80,7 +117,65 @@ uint32_t uvm32_run(uvm32_state_t *vmst, uvm32_evt_t *evt, uint32_t instr_meter)
// run CPU until no longer in running state
while(vmst->status == UVM32_STATUS_RUNNING) {
uint64_t elapsedUs = 1;
if (0 != MiniRV32IMAStep(vmst, vmst->core, vmst->memory, 0, elapsedUs, 1)) {
uint32_t ret;
ret = MiniRV32IMAStep(vmst, vmst->core, vmst->memory, 0, elapsedUs, 1);
if (3 == ret) {
const uint32_t syscall = vmst->core->regs[17]; // a7
uint32_t value = vmst->core->regs[10]; // a0
bool syscall_valid = false;
// on exception we should jump to mtvec, but we handle directly
// and skip over the ecall instruction
vmst->core->pc += 4;
switch(syscall) {
// inbuilt syscalls
case IOREQ_HALT:
setStatus(vmst, UVM32_STATUS_ENDED);
syscall_valid = true;
break;
case IOREQ_YIELD:
vmst->ioevt.typ = UVM32_EVT_YIELD;
setStatus(vmst, UVM32_STATUS_PAUSED);
syscall_valid = true;
break;
// user defined syscalls
default:
// search in mappings
for (int i=0;i<vmst->numMappings;i++) {
if (syscall == vmst->mappings[i].syscall) {
// setup ioevt.data according to mapping typ
switch(vmst->mappings[i].typ) {
case IOREQ_TYP_VOID:
break;
case IOREQ_TYP_U32_WR:
vmst->ioevt.data.ioreq.val.u32 = value;
break;
case IOREQ_TYP_BUF_TERMINATED_WR:
get_safeptr_terminated(vmst, value, 0x00, &vmst->ioevt.data.ioreq.val.buf);
break;
case IOREQ_TYP_U32_RD:
// get_safeptr(vmst, value, 4, &b);
vmst->ioevt.data.ioreq.val.u32p = &vmst->core->regs[11]; // r1, //(uint32_t *)b.ptr;
break;
}
vmst->ioevt.typ = UVM32_EVT_IOREQ;
vmst->ioevt.data.ioreq.code = vmst->mappings[i].code;
vmst->ioevt.data.ioreq.typ = vmst->mappings[i].typ;
//#warning FIXME, retval
// vmst->core->regs[11] = 456; // r1
setStatus(vmst, UVM32_STATUS_PAUSED);
syscall_valid = true;
break; // stop searching
}
}
// no mapping found
if (!syscall_valid) {
setStatusErr(vmst, UVM32_ERR_BAD_SYSCALL);
}
break;
}
} else if (ret != 0) {
// unhandled exception
setStatusErr(vmst, UVM32_ERR_INTERNAL_CORE);
setup_err_evt(vmst, evt);
}
@ -116,83 +211,6 @@ uint32_t uvm32_run(uvm32_state_t *vmst, uvm32_evt_t *evt, uint32_t instr_meter)
}
}
// Read C-string up to terminator and return len,ptr
static void get_safeptr_terminated(uvm32_state_t *vmst, uint32_t addr, uint8_t terminator, uvm32_evt_ioreq_buf_t *buf) {
uint32_t ptrstart = addr - MINIRV32_RAM_IMAGE_OFFSET;
uint32_t p = ptrstart;
if (p >= UVM32_MEMORY_SIZE) {
setStatusErr(vmst, UVM32_ERR_MEM_RD);
buf->ptr = NULL;
buf->len = 0;
return;
}
while(vmst->memory[p] != terminator) {
p++;
if (p >= UVM32_MEMORY_SIZE) {
setStatusErr(vmst, UVM32_ERR_MEM_RD);
buf->ptr = NULL;
buf->len = 0;
return;
}
}
buf->ptr = &vmst->memory[ptrstart];
buf->len = p - ptrstart;
}
static void get_safeptr(uvm32_state_t *vmst, uint32_t addr, uint32_t len, uvm32_evt_ioreq_buf_t *buf) {
uint32_t ptrstart = addr - MINIRV32_RAM_IMAGE_OFFSET;
if (ptrstart + len >= UVM32_MEMORY_SIZE) {
setStatusErr(vmst, UVM32_ERR_MEM_RD);
}
buf->ptr = &vmst->memory[ptrstart];
buf->len = len;
}
void uvm32_HandleOtherCSRWrite(void *userdata, uint16_t csrno, uint32_t value) {
uvm32_evt_ioreq_buf_t b;
uvm32_state_t *vmst = (uvm32_state_t *)userdata;
switch(csrno) {
case IOREQ_HALT:
setStatus(vmst, UVM32_STATUS_ENDED);
break;
case IOREQ_YIELD:
vmst->ioevt.typ = UVM32_EVT_YIELD;
setStatus(vmst, UVM32_STATUS_PAUSED);
break;
default:
// search in mappings
for (int i=0;i<vmst->numMappings;i++) {
if (csrno == vmst->mappings[i].csr) {
// setup ioevt.data according to mapping typ
switch(vmst->mappings[i].typ) {
case IOREQ_TYP_VOID:
break;
case IOREQ_TYP_U32_WR:
vmst->ioevt.data.ioreq.val.u32 = value;
break;
case IOREQ_TYP_BUF_TERMINATED_WR:
get_safeptr_terminated(vmst, value, 0x00, &vmst->ioevt.data.ioreq.val.buf);
break;
case IOREQ_TYP_U32_RD:
get_safeptr(vmst, value, 4, &b);
vmst->ioevt.data.ioreq.val.u32p = (uint32_t *)b.ptr;
break;
}
vmst->ioevt.typ = UVM32_EVT_IOREQ;
vmst->ioevt.data.ioreq.code = vmst->mappings[i].code;
vmst->ioevt.data.ioreq.typ = vmst->mappings[i].typ;
setStatus(vmst, UVM32_STATUS_PAUSED);
return;
}
}
// no mapping found
setStatusErr(vmst, UVM32_ERR_BAD_CSR);
break;
}
}
bool uvm32_hasEnded(const uvm32_state_t *vmst) {
return vmst->status == UVM32_STATUS_ENDED;
}

View file

@ -13,7 +13,7 @@
X(UVM32_ERR_NOTREADY) \
X(UVM32_ERR_MEM_RD) \
X(UVM32_ERR_MEM_WR) \
X(UVM32_ERR_BAD_CSR) \
X(UVM32_ERR_BAD_SYSCALL) \
X(UVM32_ERR_HUNG) \
X(UVM32_ERR_INTERNAL_CORE) \
X(UVM32_ERR_INTERNAL_STATE) \
@ -40,9 +40,9 @@ typedef enum {
typedef uint32_t uvm32_user_ioreq_code_t;
// user supplied mapping from csr to typed ioreq
// user supplied mapping from syscall to typed ioreq
typedef struct {
uint32_t csr;
uint32_t syscall;
uvm32_user_ioreq_code_t code;
uvm32_ioreq_typ_t typ;
} uvm32_mapping_t;
@ -75,11 +75,9 @@ typedef struct {
} data;
} uvm32_evt_t;
void uvm32_HandleOtherCSRWrite(void *userdata, uint16_t csrno, uint32_t value);
#define MINIRV32_DECORATE static
#define MINI_RV32_RAM_SIZE UVM32_MEMORY_SIZE
#define MINIRV32_POSTEXEC(pc, ir, retval) {if (retval > 0) return 3;}
#define MINIRV32_OTHERCSR_WRITE(csrno, value) uvm32_HandleOtherCSRWrite(userdata, csrno, value);
#ifndef MINIRV32_IMPLEMENTATION
#define MINIRV32_STEPPROTO
#endif