2f4d96dea9dfcbd321d8e2fa7687e21084655119
[riscv-tests.git] / isa / rv64mi / ma_addr.S
1 # See LICENSE for license details.
2
3 #*****************************************************************************
4 # ma_addr.S
5 #-----------------------------------------------------------------------------
6 #
7 # Test misaligned ld/st trap.
8 #
9
10 #include "riscv_test.h"
11 #include "test_macros.h"
12
13 RVTEST_RV64M
14 RVTEST_CODE_BEGIN
15
16 .option norvc
17
18 la s0, data
19
20 # indicate it's a load test
21 li s1, CAUSE_MISALIGNED_LOAD
22
23 #define SEXT(x, n) ((-((x) >> ((n)-1)) << (n)) | ((x) & ((1 << (n))-1)))
24
25 /* Check that a misaligned load either writes the correct value, or
26 takes an exception and performs no writeback. */
27 #define MISALIGNED_LOAD_TEST(testnum, insn, base, offset, res) \
28 li TESTNUM, testnum; \
29 la t2, 1f; \
30 addi t1, base, offset; \
31 insn t1, offset(base); \
32 li t2, res; \
33 bne t1, t2, fail; \
34 1:
35
36 MISALIGNED_LOAD_TEST(2, lh, s0, 1, SEXT(0xbbcc, 16))
37 MISALIGNED_LOAD_TEST(3, lhu, s0, 1, 0xbbcc)
38 MISALIGNED_LOAD_TEST(4, lw, s0, 1, SEXT(0x99aabbcc, 32))
39 MISALIGNED_LOAD_TEST(5, lw, s0, 2, SEXT(0x8899aabb, 32))
40 MISALIGNED_LOAD_TEST(6, lw, s0, 3, SEXT(0x778899aa, 32))
41
42 #if __riscv_xlen == 64
43 MISALIGNED_LOAD_TEST(7, lwu, s0, 1, 0x99aabbcc)
44 MISALIGNED_LOAD_TEST(8, lwu, s0, 2, 0x8899aabb)
45 MISALIGNED_LOAD_TEST(9, lwu, s0, 3, 0x778899aa)
46
47 MISALIGNED_LOAD_TEST(10, ld, s0, 1, 0x5566778899aabbcc)
48 MISALIGNED_LOAD_TEST(11, ld, s0, 2, 0x445566778899aabb)
49 MISALIGNED_LOAD_TEST(12, ld, s0, 3, 0x33445566778899aa)
50 MISALIGNED_LOAD_TEST(13, ld, s0, 4, 0x2233445566778899)
51 MISALIGNED_LOAD_TEST(14, ld, s0, 5, 0x1122334455667788)
52 MISALIGNED_LOAD_TEST(15, ld, s0, 6, 0xee11223344556677)
53 MISALIGNED_LOAD_TEST(16, ld, s0, 7, 0xffee112233445566)
54 #endif
55
56 # indicate it's a store test
57 li s1, CAUSE_MISALIGNED_STORE
58
59 /* Check that a misaligned store has some effect and takes no exception,
60 or takes no effect and generates an exception. This is not very
61 thorough. */
62 #define MISALIGNED_STORE_TEST(testnum, insn, base, offset, size) \
63 li TESTNUM, testnum; \
64 la t2, 1f; \
65 addi t1, base, offset; \
66 insn x0, offset(base); \
67 lb t1, (offset - 1)(base); \
68 beqz t1, fail; \
69 lb t1, (offset + size)(base); \
70 beqz t1, fail; \
71 lb t1, (offset + 0)(base); \
72 bnez t1, fail; \
73 lb t1, (offset + size - 1)(base); \
74 bnez t1, fail; \
75 1:
76
77 MISALIGNED_STORE_TEST(22, sh, s0, 1, 2)
78 MISALIGNED_STORE_TEST(23, sw, s0, 5, 4)
79 MISALIGNED_STORE_TEST(24, sw, s0, 10, 4)
80 MISALIGNED_STORE_TEST(25, sw, s0, 15, 4)
81
82 #if __riscv_xlen == 64
83 MISALIGNED_STORE_TEST(26, sd, s0, 25, 8)
84 MISALIGNED_STORE_TEST(27, sd, s0, 34, 8)
85 MISALIGNED_STORE_TEST(28, sd, s0, 43, 8)
86 MISALIGNED_STORE_TEST(29, sd, s0, 52, 8)
87 MISALIGNED_STORE_TEST(30, sd, s0, 61, 8)
88 MISALIGNED_STORE_TEST(31, sd, s0, 70, 8)
89 MISALIGNED_STORE_TEST(32, sd, s0, 79, 8)
90 #endif
91
92 TEST_PASSFAIL
93
94 .align 3
95 .global mtvec_handler
96 mtvec_handler:
97 csrr t0, mcause
98 bne t0, s1, fail
99
100 csrr t0, mbadaddr
101 bne t0, t1, fail
102
103 lb t0, (t0)
104 beqz t0, fail
105
106 csrw mepc, t2
107 mret
108
109 RVTEST_CODE_END
110
111 .data
112 RVTEST_DATA_BEGIN
113
114 data:
115 .align 3
116 .word 0xaabbccdd
117 .word 0x66778899
118 .word 0x22334455
119 .word 0xeeffee11
120 .fill 0xff, 1, 80
121
122
123 TEST_DATA
124
125 RVTEST_DATA_END