Skip to content

Commit 95cc6a3

Browse files
committed
feat(sim): basic syscall framework
1 parent 514fe21 commit 95cc6a3

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

sim/include/cpu.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33

44
#include <stdint.h>
55

6+
#define CSR_MEPC 0x341
7+
#define CSR_MCAUSE 0x342
8+
#define CSR_MSTATUS 0x300
9+
#define CSR_MTVEC 0x305
10+
611
typedef struct {
712
uint64_t reg[32];
813
uint64_t pc;
14+
uint64_t csr[4096];
915
} CPU_state;
1016

1117
void init_cpu();

sim/include/trap.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef SYSCALL_H
2+
#define SYSCALL_H
3+
4+
#include <decode.h>
5+
6+
#define SYSCALL_EXIT 93
7+
#define SYSCALL_WRITE 64
8+
9+
void handle_syscall(Decode *s);
10+
11+
#endif

sim/src/cpu.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ static int running = 1;
88
void init_cpu(){
99
cpu.pc = MEM_BASE;
1010
memset(cpu.reg, 0, sizeof(cpu.reg));
11+
memset(cpu.csr, 0, sizeof(cpu.csr));
1112
}
1213

1314

sim/src/decode.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cpu.h>
55
#include <memory.h>
66
#include <decode.h>
7+
#include <trap.h>
78

89
extern CPU_state cpu;
910

@@ -45,6 +46,13 @@ static void decode_operand(Decode *s, int *rd, uint64_t *src1, uint64_t *src2, u
4546
case TYPE_J: immJ(); break;
4647
}
4748
}
49+
50+
static void ecall_handler(Decode *s) {
51+
cpu.csr[CSR_MEPC] = s->snpc;
52+
cpu.csr[CSR_MCAUSE] = 8;
53+
handle_syscall(s);
54+
}
55+
4856
void decode_exec(Decode *s){
4957
int rd = 0;
5058
uint64_t src1 = 0, src2 = 0, imm = 0;
@@ -121,6 +129,7 @@ void decode_exec(Decode *s){
121129

122130
// Related to System Calls
123131
// ECALL
132+
INSTPAT("0000000 00000 00000 000 00000 11100 11", ecall , N, ecall_handler(s));
124133
// CSRRW, CSRRS, CSRRC, CSRRWI, CSRRSI, CSRRCI
125134

126135
// RV64M

sim/src/syscall.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <cpu.h>
2+
#include <trap.h>
3+
#include <decode.h>
4+
5+
extern CPU_state cpu;
6+
7+
void handle_syscall(Decode *s) {
8+
uint64_t syscall_no = cpu.reg[17]; // a7 register
9+
switch (syscall_no) {
10+
case SYSCALL_EXIT:
11+
Log("Syscall: exit with code %lu", cpu.reg[10]); // a0 register
12+
halt_trap(s->pc, cpu.reg[10]);
13+
break;
14+
15+
case SYSCALL_WRITE:
16+
{
17+
uint64_t fd = cpu.reg[10]; // a0
18+
uint64_t buf = cpu.reg[11]; // a1
19+
uint64_t count = cpu.reg[12]; // a2
20+
if (fd == 1) { // stdout
21+
for (uint64_t i = 0; i < count; i++) {
22+
putchar(*(uint8_t *)(buf + i));
23+
}
24+
cpu.reg[10] = count; // return value in a0
25+
} else {
26+
Log("Syscall: write to unsupported fd %lu", fd);
27+
cpu.reg[10] = -1; // error
28+
}
29+
}
30+
break;
31+
32+
default:
33+
Log("Unknown syscall: %lu", syscall_no);
34+
halt_trap(s->pc, -1);
35+
break;
36+
}
37+
}

0 commit comments

Comments
 (0)