Update to privileged architecture version 1.7
[riscv-tests.git] / isa / rv64si / csr.S
1 # See LICENSE for license details.
2
3 #*****************************************************************************
4 # csr.S
5 #-----------------------------------------------------------------------------
6 #
7 # Test CSRRx and CSRRxI instructions.
8 #
9
10 #include "riscv_test.h"
11 #include "test_macros.h"
12
13 RVTEST_RV64S
14 RVTEST_CODE_BEGIN
15
16 #ifdef __MACHINE_MODE
17 #define sscratch mscratch
18 #define sstatus mstatus
19 #define scause mcause
20 #define sepc mepc
21 #define stvec_handler mtvec_handler
22 #undef SSTATUS_PS
23 #define SSTATUS_PS MSTATUS_PRV1
24 #endif
25
26 csrwi cyclew, 0
27
28 csrwi sscratch, 3
29 TEST_CASE( 2, a0, 3, csrr a0, sscratch);
30 TEST_CASE( 3, a1, 3, csrrci a1, sscratch, 1);
31 TEST_CASE( 4, a2, 2, csrrsi a2, sscratch, 4);
32 TEST_CASE( 5, a3, 6, csrrwi a3, sscratch, 2);
33 TEST_CASE( 6, a1, 2, li a0, 0xbad1dea; csrrw a1, sscratch, a0);
34 TEST_CASE( 7, a0, 0xbad1dea, li a0, 0x0001dea; csrrc a0, sscratch, a0);
35 TEST_CASE( 8, a0, 0xbad0000, li a0, 0x000beef; csrrs a0, sscratch, a0);
36 TEST_CASE( 9, a0, 0xbadbeef, csrr a0, sscratch);
37
38 # Make sure writing the cycle counter causes an exception.
39 TEST_CASE(10, a0, 255, li a0, 255; csrrw a0, cycle, x0);
40
41 # jump to user land
42 li t0, SSTATUS_PS
43 csrc sstatus, t0
44 la t0, 1f
45 csrw sepc, t0
46 sret
47 1:
48
49 # Make sure reading status in user mode causes an exception.
50 TEST_CASE(11, a0, 255, li a0, 255; csrr a0, sstatus);
51
52 # Make sure rdcycle is legal in user mode.
53 TEST_CASE(12, x0, 0, rdcycle a0)
54
55 # Exit by doing a syscall.
56 TEST_CASE(13, x0, 1, scall)
57
58 # We should only fall through to this if scall failed.
59 TEST_PASSFAIL
60
61 stvec_handler:
62 # Trapping on tests 10, 11, and 13 is usually good news.
63 # Note that since the test didn't complete, TESTNUM is smaller by 1.
64 li t0, 9
65 beq TESTNUM, t0, privileged
66 li t0, 10
67 beq TESTNUM, t0, privileged
68 li t0, 12
69 beq TESTNUM, t0, syscall
70
71 # Trapping on other tests is bad news.
72 j fail
73
74 privileged:
75 # Make sure scause indicates a lack of privilege.
76 csrr t0, scause
77 li t1, CAUSE_ILLEGAL_INSTRUCTION
78 bne t0, t1, fail
79 # Return to user mode, but skip the trapping instruction.
80 csrr t0, sepc
81 addi t0, t0, 4
82 csrw sepc, t0
83 sret
84
85 syscall:
86 # Make sure scause indicates a syscall.
87 csrr t0, scause
88 li t1, CAUSE_USER_ECALL
89 bne t0, t1, fail
90
91 # We're done.
92 j pass
93
94 RVTEST_CODE_END
95
96 .data
97 RVTEST_DATA_BEGIN
98
99 TEST_DATA
100
101 RVTEST_DATA_END