2a326a6cc807a0de79b11190651859328f25c02c
[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 # Set up stvec in case we trap.
17 la t0, stvec
18 csrw stvec, t0
19 csrwi scycle, 0
20
21 csrwi sscratch, 3
22 TEST_CASE( 2, a0, 3, csrr a0, sscratch);
23 TEST_CASE( 3, a1, 3, csrrci a1, sscratch, 1);
24 TEST_CASE( 4, a2, 2, csrrsi a2, sscratch, 4);
25 TEST_CASE( 5, a3, 6, csrrwi a3, sscratch, 2);
26 TEST_CASE( 6, a1, 2, li a0, 0xbad1dea; csrrw a1, sscratch, a0);
27 TEST_CASE( 7, a0, 0xbad1dea, li a0, 0x0001dea; csrrc a0, sscratch, a0);
28 TEST_CASE( 8, a0, 0xbad0000, li a0, 0x000beef; csrrs a0, sscratch, a0);
29 TEST_CASE( 9, a0, 0xbadbeef, csrr a0, sscratch);
30
31 # Make sure writing the cycle counter causes an exception.
32 TEST_CASE(10, a0, 255, li a0, 255; csrrw a0, cycle, x0);
33
34 # Enter user mode
35 li t0, SSTATUS_PS
36 csrc sstatus, t0
37 la t0, 1f
38 csrw sepc, t0
39 sret
40 1:
41
42 # Make sure reading status in user mode causes an exception.
43 TEST_CASE(11, a0, 255, li a0, 255; csrr a0, sstatus);
44
45 # Make sure rdcycle is legal in user mode.
46 TEST_CASE(12, x0, 0, rdcycle a0)
47
48 # Exit by doing a syscall.
49 TEST_CASE(13, x0, 1, scall)
50
51 # We should only fall through to this if scall failed.
52 TEST_PASSFAIL
53
54 stvec:
55 # Trapping on tests 10, 11, and 13 is usually good news.
56 # Note that since the test didn't complete, TESTNUM is smaller by 1.
57 li t0, 9
58 beq TESTNUM, t0, privileged
59 li t0, 10
60 beq TESTNUM, t0, privileged
61 li t0, 12
62 beq TESTNUM, t0, syscall
63
64 # Trapping on other tests is bad news.
65 j fail
66
67 privileged:
68 # Make sure CAUSE indicates a lack of privilege.
69 csrr t0, scause
70 li t1, CAUSE_ILLEGAL_INSTRUCTION
71 bne t0, t1, fail
72 # Return to user mode, but skip the trapping instruction.
73 csrr t0, sepc
74 addi t0, t0, 4
75 csrw sepc, t0
76 sret
77
78 syscall:
79 # Make sure CAUSE indicates a syscall.
80 csrr t0, scause
81 li t1, CAUSE_SCALL
82 bne t0, t1, fail
83
84 # We're done.
85 j pass
86
87 RVTEST_CODE_END
88
89 .data
90 RVTEST_DATA_BEGIN
91
92 TEST_DATA
93
94 RVTEST_DATA_END