Skip to content

Commit 137df3f

Browse files
committed
Implement environment call properly
Implement ecall instruction with control and status registers (CSRs).
1 parent 64e2f6b commit 137df3f

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ Current progress of this emulator in riscv-arch-test(RV32):
8484
- `M`: Standard Extension for Integer Multiplication and Division
8585
- `C`: Standard Extension for Compressed Instruction
8686
- `Zifencei`: Instruction-Fetch Fence
87-
* Failed Tests
8887
- `privilege`: RISCV Privileged Specification
89-
+ 1 system calls
90-
* `ecall`
9188
* Unsupported tests (runnable but incomplete)
9289
- `F` Standard Extension for Single-Precision Floating-Point
9390

src/emulate.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ extern struct target_ops gdbstub_ops;
4040
_(breakpoint) /* Breakpoint */ \
4141
_(load_misaligned) /* Load address misaligned */ \
4242
_(load_fault) /* Load access fault */ \
43-
_(store_misaligned) /* Store/AMO address misaligned */
43+
_(store_misaligned) /* Store/AMO address misaligned */ \
44+
_(store_fault) /* Store/AMO access fault */ \
45+
_(ecall_U) /* Environment call from U-mode */ \
46+
_(ecall_S) /* Environment call from S-mode */ \
47+
_(reserved) /* Reserved */ \
48+
_(ecall_M) /* Environment call from M-mode */
4449

4550
enum {
4651
#define _(type) GET_EXCEPTION_CODE(type),
@@ -775,7 +780,7 @@ static inline bool op_system(struct riscv_t *rv, uint32_t insn)
775780
switch (funct12) { /* dispatch from imm field */
776781
case 0: /* ECALL: Environment Call */
777782
rv->io.on_ecall(rv);
778-
break;
783+
return true;
779784
case 1: /* EBREAK: Environment Break */
780785
rv->io.on_ebreak(rv);
781786
return true;
@@ -2141,3 +2146,10 @@ void ebreak_handler(struct riscv_t *rv)
21412146
assert(rv);
21422147
rv_except_breakpoint(rv, rv->PC);
21432148
}
2149+
2150+
void ecall_handler(struct riscv_t *rv)
2151+
{
2152+
assert(rv);
2153+
rv_except_ecall_M(rv, 0);
2154+
syscall_handler(rv);
2155+
}

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ int main(int argc, char **args)
199199
.mem_write_b = MEMIO(write_b),
200200

201201
/* system */
202-
.on_ecall = syscall_handler,
202+
.on_ecall = ecall_handler,
203203
.on_ebreak = ebreak_handler,
204204
};
205205

src/riscv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ riscv_word_t rv_get_reg(struct riscv_t *, uint32_t reg);
130130
/* system call handler */
131131
void syscall_handler(struct riscv_t *rv);
132132

133+
/* environment call handler */
134+
void ecall_handler(struct riscv_t *rv);
135+
133136
/* breakpoint exception handler */
134137
void ebreak_handler(struct riscv_t *rv);
135138

src/syscall.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ void syscall_handler(struct riscv_t *rv)
336336
#undef _
337337
default:
338338
fprintf(stderr, "unknown syscall %d\n", (int) syscall);
339-
rv_halt(rv);
340339
break;
341340
}
342341
}

0 commit comments

Comments
 (0)