Skip to content

Commit 1de60d7

Browse files
committed
Improve compliance for privileged instructions
Pass serveral privilege instruction and exceptions tests like ebreak, misaligned load word and misaligned store word. Moreover, the privilege instruction ecall doesn't pass the test because it only dispatches the syscall number to corresponding handler. Therefore, it needs to implement the exception handler with control and status registers (CSRs).
1 parent ad74134 commit 1de60d7

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

README.md

+1-12
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,8 @@ Current progress of this emulator in riscv-arch-test(RV32):
8686
- `Zifencei`: Instruction-Fetch Fence
8787
* Failed Tests
8888
- `privilege`: RISCV Privileged Specification
89-
+ 2 system calls
90-
* `ebreak`
89+
+ 1 system calls
9190
* `ecall`
92-
+ 5 misaligned CB-type instructions
93-
* `misalign-beq`
94-
* `misalign-bge`
95-
* `misalign-bgeu`
96-
* `misalign-blt`
97-
* `misalign-bltu`
98-
+ 1 misaligned CL-type instructions
99-
* `misalign-lw`
100-
+ 1 misaligned CS-type instructions
101-
* `misalign-sw`
10291
* Unsupported tests (runnable but incomplete)
10392
- `F` Standard Extension for Single-Precision Floating-Point
10493

src/emulate.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ static inline bool op_system(struct riscv_t *rv, uint32_t insn)
761761
* 31 20 19 15 14 12 11 7 6 0
762762
* | csr | rs1 | funct3 | rd | opcode |
763763
*/
764-
const int32_t imm = dec_itype_imm(insn);
764+
const int32_t funct12 = dec_funct12(insn);
765765
const int32_t csr = dec_csr(insn);
766766
const uint32_t funct3 = dec_funct3(insn);
767767
const uint32_t rs1 = dec_rs1(insn);
@@ -770,13 +770,13 @@ static inline bool op_system(struct riscv_t *rv, uint32_t insn)
770770
/* dispatch by func3 field */
771771
switch (funct3) {
772772
case 0:
773-
switch (imm) { /* dispatch from imm field */
774-
case 0: /* ECALL: Environment Call */
773+
switch (funct12) { /* dispatch from imm field */
774+
case 0: /* ECALL: Environment Call */
775775
rv->io.on_ecall(rv);
776776
break;
777777
case 1: /* EBREAK: Environment Break */
778778
rv->io.on_ebreak(rv);
779-
break;
779+
return true;
780780
case 0x002: /* URET: Return from handling an interrupt or exception */
781781
case 0x102: /* SRET */
782782
case 0x202: /* HRET */

src/riscv_private.h

+6
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ static inline uint32_t dec_r4type_rs3(const uint32_t insn)
261261
return (insn & FR4_RS3) >> 27;
262262
}
263263

264+
/* decode system instruction immediate (same as itype) */
265+
static inline uint32_t dec_funct12(const uint32_t insn)
266+
{
267+
return ((uint32_t)(insn & FI_IMM_11_0)) >> 20;
268+
}
269+
264270
/* decode csr instruction immediate (same as itype, zero extend) */
265271
static inline uint32_t dec_csr(const uint32_t insn)
266272
{

tests/arch-test-target/device/rv32i_m/privilege/Makefile.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RUN_TARGET= $(TARGETDIR)/build/rv32emu $(<) \
55

66
RISCV_GCC ?= $(RISCV_PREFIX)gcc
77
RISCV_GCC_OPTS ?= \
8-
-march=rv32gc \
8+
-march=rv32g \
99
-mabi=ilp32 \
1010
-static \
1111
-mcmodel=medany \

0 commit comments

Comments
 (0)