Add files using upload-large-folder tool
Browse files- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock00_rlb1_mmwp1_mml0_pmp_13.c +318 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock01_rlb1_mmwp0_mml1_pmp_02.c +318 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock01_rlb1_mmwp0_mml1_pmp_12.c +318 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock10_rlb0_mmwp0_mml0_sec_02.c +318 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock10_rlb0_mmwp1_mml1_pmp_17.c +318 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock10_rlb1_mmwp1_mml1_sec_02.c +318 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u0_rw00_x0_l0_match1_mmwp0_mml0.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u0_rw00_x1_l0_match0_mmwp0_mml1.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u0_rw10_x1_l0_match0_mmwp1_mml0.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u0_rw11_x1_l0_match0_mmwp0_mml0.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw00_x0_l0_match0_mmwp0_mml0.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw00_x0_l0_match1_mmwp0_mml1.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw00_x0_l1_match1_mmwp0_mml0.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw10_x0_l1_match0_mmwp1_mml1.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw11_x0_l1_match1_mmwp1_mml1.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw11_x1_l0_match1_mmwp1_mml1.c +346 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-tests/debug/targets/RISC-V/spike-1.cfg +33 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-tests/debug/targets/RISC-V/spike64-2-rtos.py +19 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-tests/debug/targets/RISC-V/spike64.lds +36 -0
- snapshots/lowRISC_ibex_pr2214/vendor/riscv-tests/debug/targets/RISC-V/spike64.py +25 -0
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock00_rlb1_mmwp1_mml0_pmp_13.c
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_csr_1_lock00_rlb1_mmwp1_mml0_pmp_13.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_csr_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is verify the pmp CSR access when seccfg introduced.
|
| 7 |
+
* It's expected to executed from M mode.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - CSR protection for non-M mode access is assumed and not coverred.
|
| 11 |
+
* - The access on invalid CSR index like pmpcfg1 for rv64 is not coverred.
|
| 12 |
+
* - Executed on RV64 only.
|
| 13 |
+
*/
|
| 14 |
+
|
| 15 |
+
/*
|
| 16 |
+
* Macros from encoding.h
|
| 17 |
+
*/
|
| 18 |
+
#define MSTATUS_MPP 0x00001800
|
| 19 |
+
|
| 20 |
+
#define PMP_R 0x01
|
| 21 |
+
#define PMP_W 0x02
|
| 22 |
+
#define PMP_X 0x04
|
| 23 |
+
#define PMP_A 0x18
|
| 24 |
+
#define PMP_L 0x80
|
| 25 |
+
#define PMP_SHIFT 2
|
| 26 |
+
|
| 27 |
+
#if ((PMP_R | PMP_W | PMP_X) != 0x7)
|
| 28 |
+
#error unexpected
|
| 29 |
+
#endif
|
| 30 |
+
|
| 31 |
+
#define PMP_OFF 0x0
|
| 32 |
+
#define PMP_TOR 0x08
|
| 33 |
+
#define PMP_NA4 0x10
|
| 34 |
+
#define PMP_NAPOT 0x18
|
| 35 |
+
|
| 36 |
+
#define MSECCFG_MML 0x1
|
| 37 |
+
#define MSECCFG_MMWP 0x2
|
| 38 |
+
#define MSECCFG_RLB 0x4
|
| 39 |
+
|
| 40 |
+
#define TEST_RW 1
|
| 41 |
+
#define TEST_FETCH 1
|
| 42 |
+
/*
|
| 43 |
+
* Whether rwx share single cfg for M mode
|
| 44 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 45 |
+
*/
|
| 46 |
+
#define M_MODE_RWX 0
|
| 47 |
+
|
| 48 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 49 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 50 |
+
|
| 51 |
+
typedef unsigned long reg_t;
|
| 52 |
+
typedef unsigned long uintptr_t;
|
| 53 |
+
|
| 54 |
+
/*
|
| 55 |
+
* functions from syscalls.c
|
| 56 |
+
*/
|
| 57 |
+
#if PRINTF_SUPPORTED
|
| 58 |
+
int printf(const char* fmt, ...);
|
| 59 |
+
#else
|
| 60 |
+
#define printf(...)
|
| 61 |
+
#endif
|
| 62 |
+
|
| 63 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 64 |
+
void exit(int code);
|
| 65 |
+
|
| 66 |
+
/*
|
| 67 |
+
* local status
|
| 68 |
+
*/
|
| 69 |
+
#define TEST_MEM_START 0x80200000
|
| 70 |
+
#define TEST_MEM_END 0x80240000
|
| 71 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 72 |
+
#define FAKE_ADDRESS 0x10000000
|
| 73 |
+
|
| 74 |
+
static const unsigned long expected_seccfg_fail = 0;
|
| 75 |
+
static unsigned actual_seccfg_fail = 0;
|
| 76 |
+
|
| 77 |
+
static const unsigned long expected_pmpaddr_fail = 0;
|
| 78 |
+
static unsigned actual_pmpaddr_fail = 0;
|
| 79 |
+
|
| 80 |
+
static const unsigned long expected_pmpcfg_fail = 0;
|
| 81 |
+
static unsigned actual_pmpcfg_fail = 0;
|
| 82 |
+
|
| 83 |
+
static void checkTestResult(void);
|
| 84 |
+
|
| 85 |
+
/*
|
| 86 |
+
* override syscalls.c.
|
| 87 |
+
* currently simply skip to nexp instruction
|
| 88 |
+
*/
|
| 89 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 90 |
+
{
|
| 91 |
+
tohost_exit(1337);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 96 |
+
void target_foo() {
|
| 97 |
+
asm volatile ("nop");
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 101 |
+
volatile unsigned char target_arr[100] = {
|
| 102 |
+
1,2,3,4,5,6,7,8,
|
| 103 |
+
};
|
| 104 |
+
|
| 105 |
+
__attribute ((section(".text_umode"), noinline))
|
| 106 |
+
void target_foo_U() {
|
| 107 |
+
asm volatile ("nop");
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
__attribute ((section(".data_umode"), aligned(8)))
|
| 111 |
+
volatile unsigned char target_arr_U[100] = {
|
| 112 |
+
1,2,3,4,5,6,7,8,
|
| 113 |
+
};
|
| 114 |
+
|
| 115 |
+
/*
|
| 116 |
+
* On processor_t::reset():
|
| 117 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 118 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 119 |
+
*/
|
| 120 |
+
static void set_cfg() {
|
| 121 |
+
#if 1 // @set_rlb_at_start:int@
|
| 122 |
+
/*
|
| 123 |
+
* set MSECCFG_RLB to avoid locked at start
|
| 124 |
+
*/
|
| 125 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(MSECCFG_RLB));
|
| 126 |
+
asm volatile ("nop");
|
| 127 |
+
#endif
|
| 128 |
+
|
| 129 |
+
//------------------------Set current status before the test target (CSR access)
|
| 130 |
+
/*
|
| 131 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 132 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 133 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 134 |
+
*/
|
| 135 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 136 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 137 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 138 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory");
|
| 139 |
+
|
| 140 |
+
#if M_MODE_RWX
|
| 141 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 142 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 143 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 144 |
+
#else
|
| 145 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 146 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 147 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 148 |
+
reg_t cfg0 = PMP_OFF;
|
| 149 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 150 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 151 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 152 |
+
#endif
|
| 153 |
+
|
| 154 |
+
if (0) { // need to set L bit for M mode code access
|
| 155 |
+
#if M_MODE_RWX
|
| 156 |
+
cfg0 |= PMP_L;
|
| 157 |
+
cfg1 |= (PMP_L << 24);
|
| 158 |
+
#else
|
| 159 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 160 |
+
#endif
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
reg_t sub_cfg = PMP_R | PMP_W | PMP_X | PMP_TOR | ((0 || 0) ? PMP_L : 0);
|
| 164 |
+
cfg0 |= sub_cfg << 24; // for U_MEM
|
| 165 |
+
cfg0 |= sub_cfg << 16; // for TEST_MEM
|
| 166 |
+
|
| 167 |
+
#if __riscv_xlen == 64
|
| 168 |
+
cfg0 |= (cfg1 << 32);
|
| 169 |
+
#else
|
| 170 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 171 |
+
:
|
| 172 |
+
: "r"(cfg1)
|
| 173 |
+
: "memory");
|
| 174 |
+
#endif // __riscv_xlen == 64
|
| 175 |
+
|
| 176 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 177 |
+
:
|
| 178 |
+
: "r"(cfg0)
|
| 179 |
+
: "memory");
|
| 180 |
+
|
| 181 |
+
if (0 != 0) {
|
| 182 |
+
reg_t lock_bits = (PMP_L << 16) | ((reg_t)PMP_L << 24); // avoid use (default) int type
|
| 183 |
+
if (0) {
|
| 184 |
+
asm volatile ("csrs pmpcfg0, %0 \n"
|
| 185 |
+
:
|
| 186 |
+
: "r"(lock_bits)
|
| 187 |
+
: "memory");
|
| 188 |
+
} else {
|
| 189 |
+
asm volatile ("csrc pmpcfg0, %0 \n"
|
| 190 |
+
:
|
| 191 |
+
: "r"(lock_bits)
|
| 192 |
+
: "memory");
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// set proc->state.mseccfg
|
| 197 |
+
const unsigned seccfg_bits = (1 ? MSECCFG_RLB : 0)
|
| 198 |
+
| (0 ? MSECCFG_MML : 0)
|
| 199 |
+
| (1 ? MSECCFG_MMWP : 0);
|
| 200 |
+
asm volatile ("csrw 0x747, %0 \n"::"r"(seccfg_bits));
|
| 201 |
+
|
| 202 |
+
//------------------------Test target
|
| 203 |
+
asm volatile ("nop");
|
| 204 |
+
/*
|
| 205 |
+
* Need to separate pmp and seccfg access since pmplock_recorded status may be
|
| 206 |
+
* updated again when accessing pmpcfg.
|
| 207 |
+
*/
|
| 208 |
+
reg_t wval = 0, rval;
|
| 209 |
+
reg_t prev_val = 0;
|
| 210 |
+
#if 1
|
| 211 |
+
asm volatile ("csrr %0, pmpaddr7 \n"
|
| 212 |
+
: "=r"(rval));
|
| 213 |
+
// give a valid value for both NAPOT and TOR
|
| 214 |
+
if (7 == 0) {
|
| 215 |
+
wval = ((rval + 1) << 1) - 1; // NAPOT mask
|
| 216 |
+
} else {
|
| 217 |
+
wval = (rval << 1) + 65536;
|
| 218 |
+
}
|
| 219 |
+
prev_val = rval;
|
| 220 |
+
asm volatile ("csrw pmpaddr7, %1 \n"
|
| 221 |
+
"\tcsrr %0, pmpaddr7 \n"
|
| 222 |
+
: "=r"(rval)
|
| 223 |
+
: "r"(wval)
|
| 224 |
+
: "memory");
|
| 225 |
+
if (wval != rval) {
|
| 226 |
+
actual_pmpaddr_fail = 1;
|
| 227 |
+
}
|
| 228 |
+
asm volatile ("csrw pmpaddr7, %0 \n"
|
| 229 |
+
:: "r"(prev_val));
|
| 230 |
+
// Update cfg0 to avoid changing idx other than 2
|
| 231 |
+
asm volatile ("csrr %0, pmpcfg2 \n"
|
| 232 |
+
: "=r"(cfg0)
|
| 233 |
+
:
|
| 234 |
+
: "memory");
|
| 235 |
+
|
| 236 |
+
// reuse lock_once here since it's for RLB and independent with pmp_lock
|
| 237 |
+
wval = cfg0 ^ ((reg_t)(3 | (0 ? PMP_L : 0)) << (2 * 8));
|
| 238 |
+
asm volatile ("csrw pmpcfg2, %1 \n"
|
| 239 |
+
"\tcsrr %0, pmpcfg2 \n"
|
| 240 |
+
: "=r"(rval)
|
| 241 |
+
: "r"(wval)
|
| 242 |
+
: "memory");
|
| 243 |
+
if (wval != rval) {
|
| 244 |
+
actual_pmpcfg_fail = 1;
|
| 245 |
+
}
|
| 246 |
+
#else
|
| 247 |
+
/*
|
| 248 |
+
* need to set PMP_L for cfg0 otherwise next PC will illegal
|
| 249 |
+
* This is a little coverage hole for non-PMP_L + mml, which should be
|
| 250 |
+
* a restricted use case and can be accepted anyway.
|
| 251 |
+
*/
|
| 252 |
+
if (1) {
|
| 253 |
+
#if M_MODE_RWX
|
| 254 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(PMP_L));
|
| 255 |
+
#else
|
| 256 |
+
#if __riscv_xlen == 64
|
| 257 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(((reg_t)PMP_L << 40) | ((reg_t)PMP_L << 48)));
|
| 258 |
+
#else
|
| 259 |
+
asm volatile ("csrs pmpcfg1, %0 \n"::"r"((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24)));
|
| 260 |
+
#endif // __riscv_xlen == 64
|
| 261 |
+
#endif // M_MODE_RWX
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
wval = (1 ? MSECCFG_RLB : 0)
|
| 265 |
+
| (1 ? MSECCFG_MML : 0)
|
| 266 |
+
| (1 ? MSECCFG_MMWP : 0);
|
| 267 |
+
asm volatile ("csrw 0x747, %1 \n"
|
| 268 |
+
"\tcsrr %0, 0x747 \n"
|
| 269 |
+
: "=r"(rval)
|
| 270 |
+
: "r"(wval)
|
| 271 |
+
: "memory");
|
| 272 |
+
reg_t expected_val = wval & (MSECCFG_RLB | MSECCFG_MML | MSECCFG_MMWP);
|
| 273 |
+
/*
|
| 274 |
+
* pre_sec_mml means cfg0 locked
|
| 275 |
+
* pmp_lock means cfg2/3 locked
|
| 276 |
+
* sec_mml is the test coverage hole just mentioned
|
| 277 |
+
*/
|
| 278 |
+
if ((0 || 0 || 1)
|
| 279 |
+
&& 1 == 0) {
|
| 280 |
+
expected_val &= ~MSECCFG_RLB;
|
| 281 |
+
}
|
| 282 |
+
if (0) {
|
| 283 |
+
expected_val |= MSECCFG_MML;
|
| 284 |
+
}
|
| 285 |
+
if (1) {
|
| 286 |
+
expected_val |= MSECCFG_MMWP;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
if (expected_val != rval) actual_seccfg_fail = 1;
|
| 290 |
+
#endif
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
static void checkTestResult() {
|
| 294 |
+
int ret = 0;
|
| 295 |
+
if (expected_seccfg_fail != actual_seccfg_fail) {
|
| 296 |
+
ret += 1;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
if (expected_pmpaddr_fail != actual_pmpaddr_fail) {
|
| 300 |
+
ret += 2;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
if (expected_pmpcfg_fail != actual_pmpcfg_fail) {
|
| 304 |
+
ret += 4;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
exit(ret);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
int main() {
|
| 312 |
+
// assert in M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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_csr_1_lock01_rlb1_mmwp0_mml1_pmp_02.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_csr_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is verify the pmp CSR access when seccfg introduced.
|
| 7 |
+
* It's expected to executed from M mode.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - CSR protection for non-M mode access is assumed and not coverred.
|
| 11 |
+
* - The access on invalid CSR index like pmpcfg1 for rv64 is not coverred.
|
| 12 |
+
* - Executed on RV64 only.
|
| 13 |
+
*/
|
| 14 |
+
|
| 15 |
+
/*
|
| 16 |
+
* Macros from encoding.h
|
| 17 |
+
*/
|
| 18 |
+
#define MSTATUS_MPP 0x00001800
|
| 19 |
+
|
| 20 |
+
#define PMP_R 0x01
|
| 21 |
+
#define PMP_W 0x02
|
| 22 |
+
#define PMP_X 0x04
|
| 23 |
+
#define PMP_A 0x18
|
| 24 |
+
#define PMP_L 0x80
|
| 25 |
+
#define PMP_SHIFT 2
|
| 26 |
+
|
| 27 |
+
#if ((PMP_R | PMP_W | PMP_X) != 0x7)
|
| 28 |
+
#error unexpected
|
| 29 |
+
#endif
|
| 30 |
+
|
| 31 |
+
#define PMP_OFF 0x0
|
| 32 |
+
#define PMP_TOR 0x08
|
| 33 |
+
#define PMP_NA4 0x10
|
| 34 |
+
#define PMP_NAPOT 0x18
|
| 35 |
+
|
| 36 |
+
#define MSECCFG_MML 0x1
|
| 37 |
+
#define MSECCFG_MMWP 0x2
|
| 38 |
+
#define MSECCFG_RLB 0x4
|
| 39 |
+
|
| 40 |
+
#define TEST_RW 1
|
| 41 |
+
#define TEST_FETCH 1
|
| 42 |
+
/*
|
| 43 |
+
* Whether rwx share single cfg for M mode
|
| 44 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 45 |
+
*/
|
| 46 |
+
#define M_MODE_RWX 0
|
| 47 |
+
|
| 48 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 49 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 50 |
+
|
| 51 |
+
typedef unsigned long reg_t;
|
| 52 |
+
typedef unsigned long uintptr_t;
|
| 53 |
+
|
| 54 |
+
/*
|
| 55 |
+
* functions from syscalls.c
|
| 56 |
+
*/
|
| 57 |
+
#if PRINTF_SUPPORTED
|
| 58 |
+
int printf(const char* fmt, ...);
|
| 59 |
+
#else
|
| 60 |
+
#define printf(...)
|
| 61 |
+
#endif
|
| 62 |
+
|
| 63 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 64 |
+
void exit(int code);
|
| 65 |
+
|
| 66 |
+
/*
|
| 67 |
+
* local status
|
| 68 |
+
*/
|
| 69 |
+
#define TEST_MEM_START 0x80200000
|
| 70 |
+
#define TEST_MEM_END 0x80240000
|
| 71 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 72 |
+
#define FAKE_ADDRESS 0x10000000
|
| 73 |
+
|
| 74 |
+
static const unsigned long expected_seccfg_fail = 0;
|
| 75 |
+
static unsigned actual_seccfg_fail = 0;
|
| 76 |
+
|
| 77 |
+
static const unsigned long expected_pmpaddr_fail = 0;
|
| 78 |
+
static unsigned actual_pmpaddr_fail = 0;
|
| 79 |
+
|
| 80 |
+
static const unsigned long expected_pmpcfg_fail = 0;
|
| 81 |
+
static unsigned actual_pmpcfg_fail = 0;
|
| 82 |
+
|
| 83 |
+
static void checkTestResult(void);
|
| 84 |
+
|
| 85 |
+
/*
|
| 86 |
+
* override syscalls.c.
|
| 87 |
+
* currently simply skip to nexp instruction
|
| 88 |
+
*/
|
| 89 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 90 |
+
{
|
| 91 |
+
tohost_exit(1337);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 96 |
+
void target_foo() {
|
| 97 |
+
asm volatile ("nop");
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 101 |
+
volatile unsigned char target_arr[100] = {
|
| 102 |
+
1,2,3,4,5,6,7,8,
|
| 103 |
+
};
|
| 104 |
+
|
| 105 |
+
__attribute ((section(".text_umode"), noinline))
|
| 106 |
+
void target_foo_U() {
|
| 107 |
+
asm volatile ("nop");
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
__attribute ((section(".data_umode"), aligned(8)))
|
| 111 |
+
volatile unsigned char target_arr_U[100] = {
|
| 112 |
+
1,2,3,4,5,6,7,8,
|
| 113 |
+
};
|
| 114 |
+
|
| 115 |
+
/*
|
| 116 |
+
* On processor_t::reset():
|
| 117 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 118 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 119 |
+
*/
|
| 120 |
+
static void set_cfg() {
|
| 121 |
+
#if 1 // @set_rlb_at_start:int@
|
| 122 |
+
/*
|
| 123 |
+
* set MSECCFG_RLB to avoid locked at start
|
| 124 |
+
*/
|
| 125 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(MSECCFG_RLB));
|
| 126 |
+
asm volatile ("nop");
|
| 127 |
+
#endif
|
| 128 |
+
|
| 129 |
+
//------------------------Set current status before the test target (CSR access)
|
| 130 |
+
/*
|
| 131 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 132 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 133 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 134 |
+
*/
|
| 135 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 136 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 137 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 138 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory");
|
| 139 |
+
|
| 140 |
+
#if M_MODE_RWX
|
| 141 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 142 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 143 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 144 |
+
#else
|
| 145 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 146 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 147 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 148 |
+
reg_t cfg0 = PMP_OFF;
|
| 149 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 150 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 151 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 152 |
+
#endif
|
| 153 |
+
|
| 154 |
+
if (1) { // need to set L bit for M mode code access
|
| 155 |
+
#if M_MODE_RWX
|
| 156 |
+
cfg0 |= PMP_L;
|
| 157 |
+
cfg1 |= (PMP_L << 24);
|
| 158 |
+
#else
|
| 159 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 160 |
+
#endif
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
reg_t sub_cfg = PMP_R | PMP_W | PMP_X | PMP_TOR | ((1 || 0) ? PMP_L : 0);
|
| 164 |
+
cfg0 |= sub_cfg << 24; // for U_MEM
|
| 165 |
+
cfg0 |= sub_cfg << 16; // for TEST_MEM
|
| 166 |
+
|
| 167 |
+
#if __riscv_xlen == 64
|
| 168 |
+
cfg0 |= (cfg1 << 32);
|
| 169 |
+
#else
|
| 170 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 171 |
+
:
|
| 172 |
+
: "r"(cfg1)
|
| 173 |
+
: "memory");
|
| 174 |
+
#endif // __riscv_xlen == 64
|
| 175 |
+
|
| 176 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 177 |
+
:
|
| 178 |
+
: "r"(cfg0)
|
| 179 |
+
: "memory");
|
| 180 |
+
|
| 181 |
+
if (1 != 0) {
|
| 182 |
+
reg_t lock_bits = (PMP_L << 16) | ((reg_t)PMP_L << 24); // avoid use (default) int type
|
| 183 |
+
if (0) {
|
| 184 |
+
asm volatile ("csrs pmpcfg0, %0 \n"
|
| 185 |
+
:
|
| 186 |
+
: "r"(lock_bits)
|
| 187 |
+
: "memory");
|
| 188 |
+
} else {
|
| 189 |
+
asm volatile ("csrc pmpcfg0, %0 \n"
|
| 190 |
+
:
|
| 191 |
+
: "r"(lock_bits)
|
| 192 |
+
: "memory");
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// set proc->state.mseccfg
|
| 197 |
+
const unsigned seccfg_bits = (1 ? MSECCFG_RLB : 0)
|
| 198 |
+
| (1 ? MSECCFG_MML : 0)
|
| 199 |
+
| (0 ? MSECCFG_MMWP : 0);
|
| 200 |
+
asm volatile ("csrw 0x747, %0 \n"::"r"(seccfg_bits));
|
| 201 |
+
|
| 202 |
+
//------------------------Test target
|
| 203 |
+
asm volatile ("nop");
|
| 204 |
+
/*
|
| 205 |
+
* Need to separate pmp and seccfg access since pmplock_recorded status may be
|
| 206 |
+
* updated again when accessing pmpcfg.
|
| 207 |
+
*/
|
| 208 |
+
reg_t wval = 0, rval;
|
| 209 |
+
reg_t prev_val = 0;
|
| 210 |
+
#if 1
|
| 211 |
+
asm volatile ("csrr %0, pmpaddr2 \n"
|
| 212 |
+
: "=r"(rval));
|
| 213 |
+
// give a valid value for both NAPOT and TOR
|
| 214 |
+
if (2 == 0) {
|
| 215 |
+
wval = ((rval + 1) << 1) - 1; // NAPOT mask
|
| 216 |
+
} else {
|
| 217 |
+
wval = (rval << 1) + 0;
|
| 218 |
+
}
|
| 219 |
+
prev_val = rval;
|
| 220 |
+
asm volatile ("csrw pmpaddr2, %1 \n"
|
| 221 |
+
"\tcsrr %0, pmpaddr2 \n"
|
| 222 |
+
: "=r"(rval)
|
| 223 |
+
: "r"(wval)
|
| 224 |
+
: "memory");
|
| 225 |
+
if (wval != rval) {
|
| 226 |
+
actual_pmpaddr_fail = 1;
|
| 227 |
+
}
|
| 228 |
+
asm volatile ("csrw pmpaddr2, %0 \n"
|
| 229 |
+
:: "r"(prev_val));
|
| 230 |
+
// Update cfg0 to avoid changing idx other than 2
|
| 231 |
+
asm volatile ("csrr %0, pmpcfg0 \n"
|
| 232 |
+
: "=r"(cfg0)
|
| 233 |
+
:
|
| 234 |
+
: "memory");
|
| 235 |
+
|
| 236 |
+
// reuse lock_once here since it's for RLB and independent with pmp_lock
|
| 237 |
+
wval = cfg0 ^ ((reg_t)(2 | (1 ? PMP_L : 0)) << (2 * 8));
|
| 238 |
+
asm volatile ("csrw pmpcfg0, %1 \n"
|
| 239 |
+
"\tcsrr %0, pmpcfg0 \n"
|
| 240 |
+
: "=r"(rval)
|
| 241 |
+
: "r"(wval)
|
| 242 |
+
: "memory");
|
| 243 |
+
if (wval != rval) {
|
| 244 |
+
actual_pmpcfg_fail = 1;
|
| 245 |
+
}
|
| 246 |
+
#else
|
| 247 |
+
/*
|
| 248 |
+
* need to set PMP_L for cfg0 otherwise next PC will illegal
|
| 249 |
+
* This is a little coverage hole for non-PMP_L + mml, which should be
|
| 250 |
+
* a restricted use case and can be accepted anyway.
|
| 251 |
+
*/
|
| 252 |
+
if (1) {
|
| 253 |
+
#if M_MODE_RWX
|
| 254 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(PMP_L));
|
| 255 |
+
#else
|
| 256 |
+
#if __riscv_xlen == 64
|
| 257 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(((reg_t)PMP_L << 40) | ((reg_t)PMP_L << 48)));
|
| 258 |
+
#else
|
| 259 |
+
asm volatile ("csrs pmpcfg1, %0 \n"::"r"((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24)));
|
| 260 |
+
#endif // __riscv_xlen == 64
|
| 261 |
+
#endif // M_MODE_RWX
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
wval = (1 ? MSECCFG_RLB : 0)
|
| 265 |
+
| (1 ? MSECCFG_MML : 0)
|
| 266 |
+
| (1 ? MSECCFG_MMWP : 0);
|
| 267 |
+
asm volatile ("csrw 0x747, %1 \n"
|
| 268 |
+
"\tcsrr %0, 0x747 \n"
|
| 269 |
+
: "=r"(rval)
|
| 270 |
+
: "r"(wval)
|
| 271 |
+
: "memory");
|
| 272 |
+
reg_t expected_val = wval & (MSECCFG_RLB | MSECCFG_MML | MSECCFG_MMWP);
|
| 273 |
+
/*
|
| 274 |
+
* pre_sec_mml means cfg0 locked
|
| 275 |
+
* pmp_lock means cfg2/3 locked
|
| 276 |
+
* sec_mml is the test coverage hole just mentioned
|
| 277 |
+
*/
|
| 278 |
+
if ((1 || 0 || 1)
|
| 279 |
+
&& 1 == 0) {
|
| 280 |
+
expected_val &= ~MSECCFG_RLB;
|
| 281 |
+
}
|
| 282 |
+
if (1) {
|
| 283 |
+
expected_val |= MSECCFG_MML;
|
| 284 |
+
}
|
| 285 |
+
if (0) {
|
| 286 |
+
expected_val |= MSECCFG_MMWP;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
if (expected_val != rval) actual_seccfg_fail = 1;
|
| 290 |
+
#endif
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
static void checkTestResult() {
|
| 294 |
+
int ret = 0;
|
| 295 |
+
if (expected_seccfg_fail != actual_seccfg_fail) {
|
| 296 |
+
ret += 1;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
if (expected_pmpaddr_fail != actual_pmpaddr_fail) {
|
| 300 |
+
ret += 2;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
if (expected_pmpcfg_fail != actual_pmpcfg_fail) {
|
| 304 |
+
ret += 4;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
exit(ret);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
int main() {
|
| 312 |
+
// assert in M mode
|
| 313 |
+
set_cfg();
|
| 314 |
+
|
| 315 |
+
checkTestResult();
|
| 316 |
+
return 0; // assert 0
|
| 317 |
+
}
|
| 318 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock01_rlb1_mmwp0_mml1_pmp_12.c
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_csr_1_lock01_rlb1_mmwp0_mml1_pmp_12.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_csr_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is verify the pmp CSR access when seccfg introduced.
|
| 7 |
+
* It's expected to executed from M mode.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - CSR protection for non-M mode access is assumed and not coverred.
|
| 11 |
+
* - The access on invalid CSR index like pmpcfg1 for rv64 is not coverred.
|
| 12 |
+
* - Executed on RV64 only.
|
| 13 |
+
*/
|
| 14 |
+
|
| 15 |
+
/*
|
| 16 |
+
* Macros from encoding.h
|
| 17 |
+
*/
|
| 18 |
+
#define MSTATUS_MPP 0x00001800
|
| 19 |
+
|
| 20 |
+
#define PMP_R 0x01
|
| 21 |
+
#define PMP_W 0x02
|
| 22 |
+
#define PMP_X 0x04
|
| 23 |
+
#define PMP_A 0x18
|
| 24 |
+
#define PMP_L 0x80
|
| 25 |
+
#define PMP_SHIFT 2
|
| 26 |
+
|
| 27 |
+
#if ((PMP_R | PMP_W | PMP_X) != 0x7)
|
| 28 |
+
#error unexpected
|
| 29 |
+
#endif
|
| 30 |
+
|
| 31 |
+
#define PMP_OFF 0x0
|
| 32 |
+
#define PMP_TOR 0x08
|
| 33 |
+
#define PMP_NA4 0x10
|
| 34 |
+
#define PMP_NAPOT 0x18
|
| 35 |
+
|
| 36 |
+
#define MSECCFG_MML 0x1
|
| 37 |
+
#define MSECCFG_MMWP 0x2
|
| 38 |
+
#define MSECCFG_RLB 0x4
|
| 39 |
+
|
| 40 |
+
#define TEST_RW 1
|
| 41 |
+
#define TEST_FETCH 1
|
| 42 |
+
/*
|
| 43 |
+
* Whether rwx share single cfg for M mode
|
| 44 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 45 |
+
*/
|
| 46 |
+
#define M_MODE_RWX 0
|
| 47 |
+
|
| 48 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 49 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 50 |
+
|
| 51 |
+
typedef unsigned long reg_t;
|
| 52 |
+
typedef unsigned long uintptr_t;
|
| 53 |
+
|
| 54 |
+
/*
|
| 55 |
+
* functions from syscalls.c
|
| 56 |
+
*/
|
| 57 |
+
#if PRINTF_SUPPORTED
|
| 58 |
+
int printf(const char* fmt, ...);
|
| 59 |
+
#else
|
| 60 |
+
#define printf(...)
|
| 61 |
+
#endif
|
| 62 |
+
|
| 63 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 64 |
+
void exit(int code);
|
| 65 |
+
|
| 66 |
+
/*
|
| 67 |
+
* local status
|
| 68 |
+
*/
|
| 69 |
+
#define TEST_MEM_START 0x80200000
|
| 70 |
+
#define TEST_MEM_END 0x80240000
|
| 71 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 72 |
+
#define FAKE_ADDRESS 0x10000000
|
| 73 |
+
|
| 74 |
+
static const unsigned long expected_seccfg_fail = 0;
|
| 75 |
+
static unsigned actual_seccfg_fail = 0;
|
| 76 |
+
|
| 77 |
+
static const unsigned long expected_pmpaddr_fail = 0;
|
| 78 |
+
static unsigned actual_pmpaddr_fail = 0;
|
| 79 |
+
|
| 80 |
+
static const unsigned long expected_pmpcfg_fail = 0;
|
| 81 |
+
static unsigned actual_pmpcfg_fail = 0;
|
| 82 |
+
|
| 83 |
+
static void checkTestResult(void);
|
| 84 |
+
|
| 85 |
+
/*
|
| 86 |
+
* override syscalls.c.
|
| 87 |
+
* currently simply skip to nexp instruction
|
| 88 |
+
*/
|
| 89 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 90 |
+
{
|
| 91 |
+
tohost_exit(1337);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 96 |
+
void target_foo() {
|
| 97 |
+
asm volatile ("nop");
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 101 |
+
volatile unsigned char target_arr[100] = {
|
| 102 |
+
1,2,3,4,5,6,7,8,
|
| 103 |
+
};
|
| 104 |
+
|
| 105 |
+
__attribute ((section(".text_umode"), noinline))
|
| 106 |
+
void target_foo_U() {
|
| 107 |
+
asm volatile ("nop");
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
__attribute ((section(".data_umode"), aligned(8)))
|
| 111 |
+
volatile unsigned char target_arr_U[100] = {
|
| 112 |
+
1,2,3,4,5,6,7,8,
|
| 113 |
+
};
|
| 114 |
+
|
| 115 |
+
/*
|
| 116 |
+
* On processor_t::reset():
|
| 117 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 118 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 119 |
+
*/
|
| 120 |
+
static void set_cfg() {
|
| 121 |
+
#if 1 // @set_rlb_at_start:int@
|
| 122 |
+
/*
|
| 123 |
+
* set MSECCFG_RLB to avoid locked at start
|
| 124 |
+
*/
|
| 125 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(MSECCFG_RLB));
|
| 126 |
+
asm volatile ("nop");
|
| 127 |
+
#endif
|
| 128 |
+
|
| 129 |
+
//------------------------Set current status before the test target (CSR access)
|
| 130 |
+
/*
|
| 131 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 132 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 133 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 134 |
+
*/
|
| 135 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 136 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 137 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 138 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory");
|
| 139 |
+
|
| 140 |
+
#if M_MODE_RWX
|
| 141 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 142 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 143 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 144 |
+
#else
|
| 145 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 146 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 147 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 148 |
+
reg_t cfg0 = PMP_OFF;
|
| 149 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 150 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 151 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 152 |
+
#endif
|
| 153 |
+
|
| 154 |
+
if (1) { // need to set L bit for M mode code access
|
| 155 |
+
#if M_MODE_RWX
|
| 156 |
+
cfg0 |= PMP_L;
|
| 157 |
+
cfg1 |= (PMP_L << 24);
|
| 158 |
+
#else
|
| 159 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 160 |
+
#endif
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
reg_t sub_cfg = PMP_R | PMP_W | PMP_X | PMP_TOR | ((1 || 0) ? PMP_L : 0);
|
| 164 |
+
cfg0 |= sub_cfg << 24; // for U_MEM
|
| 165 |
+
cfg0 |= sub_cfg << 16; // for TEST_MEM
|
| 166 |
+
|
| 167 |
+
#if __riscv_xlen == 64
|
| 168 |
+
cfg0 |= (cfg1 << 32);
|
| 169 |
+
#else
|
| 170 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 171 |
+
:
|
| 172 |
+
: "r"(cfg1)
|
| 173 |
+
: "memory");
|
| 174 |
+
#endif // __riscv_xlen == 64
|
| 175 |
+
|
| 176 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 177 |
+
:
|
| 178 |
+
: "r"(cfg0)
|
| 179 |
+
: "memory");
|
| 180 |
+
|
| 181 |
+
if (1 != 0) {
|
| 182 |
+
reg_t lock_bits = (PMP_L << 16) | ((reg_t)PMP_L << 24); // avoid use (default) int type
|
| 183 |
+
if (0) {
|
| 184 |
+
asm volatile ("csrs pmpcfg0, %0 \n"
|
| 185 |
+
:
|
| 186 |
+
: "r"(lock_bits)
|
| 187 |
+
: "memory");
|
| 188 |
+
} else {
|
| 189 |
+
asm volatile ("csrc pmpcfg0, %0 \n"
|
| 190 |
+
:
|
| 191 |
+
: "r"(lock_bits)
|
| 192 |
+
: "memory");
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// set proc->state.mseccfg
|
| 197 |
+
const unsigned seccfg_bits = (1 ? MSECCFG_RLB : 0)
|
| 198 |
+
| (1 ? MSECCFG_MML : 0)
|
| 199 |
+
| (0 ? MSECCFG_MMWP : 0);
|
| 200 |
+
asm volatile ("csrw 0x747, %0 \n"::"r"(seccfg_bits));
|
| 201 |
+
|
| 202 |
+
//------------------------Test target
|
| 203 |
+
asm volatile ("nop");
|
| 204 |
+
/*
|
| 205 |
+
* Need to separate pmp and seccfg access since pmplock_recorded status may be
|
| 206 |
+
* updated again when accessing pmpcfg.
|
| 207 |
+
*/
|
| 208 |
+
reg_t wval = 0, rval;
|
| 209 |
+
reg_t prev_val = 0;
|
| 210 |
+
#if 1
|
| 211 |
+
asm volatile ("csrr %0, pmpaddr7 \n"
|
| 212 |
+
: "=r"(rval));
|
| 213 |
+
// give a valid value for both NAPOT and TOR
|
| 214 |
+
if (7 == 0) {
|
| 215 |
+
wval = ((rval + 1) << 1) - 1; // NAPOT mask
|
| 216 |
+
} else {
|
| 217 |
+
wval = (rval << 1) + 65536;
|
| 218 |
+
}
|
| 219 |
+
prev_val = rval;
|
| 220 |
+
asm volatile ("csrw pmpaddr7, %1 \n"
|
| 221 |
+
"\tcsrr %0, pmpaddr7 \n"
|
| 222 |
+
: "=r"(rval)
|
| 223 |
+
: "r"(wval)
|
| 224 |
+
: "memory");
|
| 225 |
+
if (wval != rval) {
|
| 226 |
+
actual_pmpaddr_fail = 1;
|
| 227 |
+
}
|
| 228 |
+
asm volatile ("csrw pmpaddr7, %0 \n"
|
| 229 |
+
:: "r"(prev_val));
|
| 230 |
+
// Update cfg0 to avoid changing idx other than 3
|
| 231 |
+
asm volatile ("csrr %0, pmpcfg2 \n"
|
| 232 |
+
: "=r"(cfg0)
|
| 233 |
+
:
|
| 234 |
+
: "memory");
|
| 235 |
+
|
| 236 |
+
// reuse lock_once here since it's for RLB and independent with pmp_lock
|
| 237 |
+
wval = cfg0 ^ ((reg_t)(2 | (1 ? PMP_L : 0)) << (3 * 8));
|
| 238 |
+
asm volatile ("csrw pmpcfg2, %1 \n"
|
| 239 |
+
"\tcsrr %0, pmpcfg2 \n"
|
| 240 |
+
: "=r"(rval)
|
| 241 |
+
: "r"(wval)
|
| 242 |
+
: "memory");
|
| 243 |
+
if (wval != rval) {
|
| 244 |
+
actual_pmpcfg_fail = 1;
|
| 245 |
+
}
|
| 246 |
+
#else
|
| 247 |
+
/*
|
| 248 |
+
* need to set PMP_L for cfg0 otherwise next PC will illegal
|
| 249 |
+
* This is a little coverage hole for non-PMP_L + mml, which should be
|
| 250 |
+
* a restricted use case and can be accepted anyway.
|
| 251 |
+
*/
|
| 252 |
+
if (1) {
|
| 253 |
+
#if M_MODE_RWX
|
| 254 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(PMP_L));
|
| 255 |
+
#else
|
| 256 |
+
#if __riscv_xlen == 64
|
| 257 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(((reg_t)PMP_L << 40) | ((reg_t)PMP_L << 48)));
|
| 258 |
+
#else
|
| 259 |
+
asm volatile ("csrs pmpcfg1, %0 \n"::"r"((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24)));
|
| 260 |
+
#endif // __riscv_xlen == 64
|
| 261 |
+
#endif // M_MODE_RWX
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
wval = (1 ? MSECCFG_RLB : 0)
|
| 265 |
+
| (1 ? MSECCFG_MML : 0)
|
| 266 |
+
| (1 ? MSECCFG_MMWP : 0);
|
| 267 |
+
asm volatile ("csrw 0x747, %1 \n"
|
| 268 |
+
"\tcsrr %0, 0x747 \n"
|
| 269 |
+
: "=r"(rval)
|
| 270 |
+
: "r"(wval)
|
| 271 |
+
: "memory");
|
| 272 |
+
reg_t expected_val = wval & (MSECCFG_RLB | MSECCFG_MML | MSECCFG_MMWP);
|
| 273 |
+
/*
|
| 274 |
+
* pre_sec_mml means cfg0 locked
|
| 275 |
+
* pmp_lock means cfg2/3 locked
|
| 276 |
+
* sec_mml is the test coverage hole just mentioned
|
| 277 |
+
*/
|
| 278 |
+
if ((1 || 0 || 1)
|
| 279 |
+
&& 1 == 0) {
|
| 280 |
+
expected_val &= ~MSECCFG_RLB;
|
| 281 |
+
}
|
| 282 |
+
if (1) {
|
| 283 |
+
expected_val |= MSECCFG_MML;
|
| 284 |
+
}
|
| 285 |
+
if (0) {
|
| 286 |
+
expected_val |= MSECCFG_MMWP;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
if (expected_val != rval) actual_seccfg_fail = 1;
|
| 290 |
+
#endif
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
static void checkTestResult() {
|
| 294 |
+
int ret = 0;
|
| 295 |
+
if (expected_seccfg_fail != actual_seccfg_fail) {
|
| 296 |
+
ret += 1;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
if (expected_pmpaddr_fail != actual_pmpaddr_fail) {
|
| 300 |
+
ret += 2;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
if (expected_pmpcfg_fail != actual_pmpcfg_fail) {
|
| 304 |
+
ret += 4;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
exit(ret);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
int main() {
|
| 312 |
+
// assert in M mode
|
| 313 |
+
set_cfg();
|
| 314 |
+
|
| 315 |
+
checkTestResult();
|
| 316 |
+
return 0; // assert 0
|
| 317 |
+
}
|
| 318 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock10_rlb0_mmwp0_mml0_sec_02.c
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_csr_1_lock10_rlb0_mmwp0_mml0_sec_02.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_csr_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is verify the pmp CSR access when seccfg introduced.
|
| 7 |
+
* It's expected to executed from M mode.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - CSR protection for non-M mode access is assumed and not coverred.
|
| 11 |
+
* - The access on invalid CSR index like pmpcfg1 for rv64 is not coverred.
|
| 12 |
+
* - Executed on RV64 only.
|
| 13 |
+
*/
|
| 14 |
+
|
| 15 |
+
/*
|
| 16 |
+
* Macros from encoding.h
|
| 17 |
+
*/
|
| 18 |
+
#define MSTATUS_MPP 0x00001800
|
| 19 |
+
|
| 20 |
+
#define PMP_R 0x01
|
| 21 |
+
#define PMP_W 0x02
|
| 22 |
+
#define PMP_X 0x04
|
| 23 |
+
#define PMP_A 0x18
|
| 24 |
+
#define PMP_L 0x80
|
| 25 |
+
#define PMP_SHIFT 2
|
| 26 |
+
|
| 27 |
+
#if ((PMP_R | PMP_W | PMP_X) != 0x7)
|
| 28 |
+
#error unexpected
|
| 29 |
+
#endif
|
| 30 |
+
|
| 31 |
+
#define PMP_OFF 0x0
|
| 32 |
+
#define PMP_TOR 0x08
|
| 33 |
+
#define PMP_NA4 0x10
|
| 34 |
+
#define PMP_NAPOT 0x18
|
| 35 |
+
|
| 36 |
+
#define MSECCFG_MML 0x1
|
| 37 |
+
#define MSECCFG_MMWP 0x2
|
| 38 |
+
#define MSECCFG_RLB 0x4
|
| 39 |
+
|
| 40 |
+
#define TEST_RW 1
|
| 41 |
+
#define TEST_FETCH 1
|
| 42 |
+
/*
|
| 43 |
+
* Whether rwx share single cfg for M mode
|
| 44 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 45 |
+
*/
|
| 46 |
+
#define M_MODE_RWX 0
|
| 47 |
+
|
| 48 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 49 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 50 |
+
|
| 51 |
+
typedef unsigned long reg_t;
|
| 52 |
+
typedef unsigned long uintptr_t;
|
| 53 |
+
|
| 54 |
+
/*
|
| 55 |
+
* functions from syscalls.c
|
| 56 |
+
*/
|
| 57 |
+
#if PRINTF_SUPPORTED
|
| 58 |
+
int printf(const char* fmt, ...);
|
| 59 |
+
#else
|
| 60 |
+
#define printf(...)
|
| 61 |
+
#endif
|
| 62 |
+
|
| 63 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 64 |
+
void exit(int code);
|
| 65 |
+
|
| 66 |
+
/*
|
| 67 |
+
* local status
|
| 68 |
+
*/
|
| 69 |
+
#define TEST_MEM_START 0x80200000
|
| 70 |
+
#define TEST_MEM_END 0x80240000
|
| 71 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 72 |
+
#define FAKE_ADDRESS 0x10000000
|
| 73 |
+
|
| 74 |
+
static const unsigned long expected_seccfg_fail = 0;
|
| 75 |
+
static unsigned actual_seccfg_fail = 0;
|
| 76 |
+
|
| 77 |
+
static const unsigned long expected_pmpaddr_fail = 0;
|
| 78 |
+
static unsigned actual_pmpaddr_fail = 0;
|
| 79 |
+
|
| 80 |
+
static const unsigned long expected_pmpcfg_fail = 0;
|
| 81 |
+
static unsigned actual_pmpcfg_fail = 0;
|
| 82 |
+
|
| 83 |
+
static void checkTestResult(void);
|
| 84 |
+
|
| 85 |
+
/*
|
| 86 |
+
* override syscalls.c.
|
| 87 |
+
* currently simply skip to nexp instruction
|
| 88 |
+
*/
|
| 89 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 90 |
+
{
|
| 91 |
+
tohost_exit(1337);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 96 |
+
void target_foo() {
|
| 97 |
+
asm volatile ("nop");
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 101 |
+
volatile unsigned char target_arr[100] = {
|
| 102 |
+
1,2,3,4,5,6,7,8,
|
| 103 |
+
};
|
| 104 |
+
|
| 105 |
+
__attribute ((section(".text_umode"), noinline))
|
| 106 |
+
void target_foo_U() {
|
| 107 |
+
asm volatile ("nop");
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
__attribute ((section(".data_umode"), aligned(8)))
|
| 111 |
+
volatile unsigned char target_arr_U[100] = {
|
| 112 |
+
1,2,3,4,5,6,7,8,
|
| 113 |
+
};
|
| 114 |
+
|
| 115 |
+
/*
|
| 116 |
+
* On processor_t::reset():
|
| 117 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 118 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 119 |
+
*/
|
| 120 |
+
static void set_cfg() {
|
| 121 |
+
#if 1 // @set_rlb_at_start:int@
|
| 122 |
+
/*
|
| 123 |
+
* set MSECCFG_RLB to avoid locked at start
|
| 124 |
+
*/
|
| 125 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(MSECCFG_RLB));
|
| 126 |
+
asm volatile ("nop");
|
| 127 |
+
#endif
|
| 128 |
+
|
| 129 |
+
//------------------------Set current status before the test target (CSR access)
|
| 130 |
+
/*
|
| 131 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 132 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 133 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 134 |
+
*/
|
| 135 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 136 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 137 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 138 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory");
|
| 139 |
+
|
| 140 |
+
#if M_MODE_RWX
|
| 141 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 142 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 143 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 144 |
+
#else
|
| 145 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 146 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 147 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 148 |
+
reg_t cfg0 = PMP_OFF;
|
| 149 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 150 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 151 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 152 |
+
#endif
|
| 153 |
+
|
| 154 |
+
if (0) { // need to set L bit for M mode code access
|
| 155 |
+
#if M_MODE_RWX
|
| 156 |
+
cfg0 |= PMP_L;
|
| 157 |
+
cfg1 |= (PMP_L << 24);
|
| 158 |
+
#else
|
| 159 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 160 |
+
#endif
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
reg_t sub_cfg = PMP_R | PMP_W | PMP_X | PMP_TOR | ((0 || 1) ? PMP_L : 0);
|
| 164 |
+
cfg0 |= sub_cfg << 24; // for U_MEM
|
| 165 |
+
cfg0 |= sub_cfg << 16; // for TEST_MEM
|
| 166 |
+
|
| 167 |
+
#if __riscv_xlen == 64
|
| 168 |
+
cfg0 |= (cfg1 << 32);
|
| 169 |
+
#else
|
| 170 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 171 |
+
:
|
| 172 |
+
: "r"(cfg1)
|
| 173 |
+
: "memory");
|
| 174 |
+
#endif // __riscv_xlen == 64
|
| 175 |
+
|
| 176 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 177 |
+
:
|
| 178 |
+
: "r"(cfg0)
|
| 179 |
+
: "memory");
|
| 180 |
+
|
| 181 |
+
if (0 != 1) {
|
| 182 |
+
reg_t lock_bits = (PMP_L << 16) | ((reg_t)PMP_L << 24); // avoid use (default) int type
|
| 183 |
+
if (1) {
|
| 184 |
+
asm volatile ("csrs pmpcfg0, %0 \n"
|
| 185 |
+
:
|
| 186 |
+
: "r"(lock_bits)
|
| 187 |
+
: "memory");
|
| 188 |
+
} else {
|
| 189 |
+
asm volatile ("csrc pmpcfg0, %0 \n"
|
| 190 |
+
:
|
| 191 |
+
: "r"(lock_bits)
|
| 192 |
+
: "memory");
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// set proc->state.mseccfg
|
| 197 |
+
const unsigned seccfg_bits = (0 ? MSECCFG_RLB : 0)
|
| 198 |
+
| (0 ? MSECCFG_MML : 0)
|
| 199 |
+
| (0 ? MSECCFG_MMWP : 0);
|
| 200 |
+
asm volatile ("csrw 0x747, %0 \n"::"r"(seccfg_bits));
|
| 201 |
+
|
| 202 |
+
//------------------------Test target
|
| 203 |
+
asm volatile ("nop");
|
| 204 |
+
/*
|
| 205 |
+
* Need to separate pmp and seccfg access since pmplock_recorded status may be
|
| 206 |
+
* updated again when accessing pmpcfg.
|
| 207 |
+
*/
|
| 208 |
+
reg_t wval = 0, rval;
|
| 209 |
+
reg_t prev_val = 0;
|
| 210 |
+
#if 0
|
| 211 |
+
asm volatile ("csrr %0, pmpaddr11 \n"
|
| 212 |
+
: "=r"(rval));
|
| 213 |
+
// give a valid value for both NAPOT and TOR
|
| 214 |
+
if (11 == 0) {
|
| 215 |
+
wval = ((rval + 1) << 1) - 1; // NAPOT mask
|
| 216 |
+
} else {
|
| 217 |
+
wval = (rval << 1) + 65536;
|
| 218 |
+
}
|
| 219 |
+
prev_val = rval;
|
| 220 |
+
asm volatile ("csrw pmpaddr11, %1 \n"
|
| 221 |
+
"\tcsrr %0, pmpaddr11 \n"
|
| 222 |
+
: "=r"(rval)
|
| 223 |
+
: "r"(wval)
|
| 224 |
+
: "memory");
|
| 225 |
+
if (wval != rval) {
|
| 226 |
+
actual_pmpaddr_fail = 1;
|
| 227 |
+
}
|
| 228 |
+
asm volatile ("csrw pmpaddr11, %0 \n"
|
| 229 |
+
:: "r"(prev_val));
|
| 230 |
+
// Update cfg0 to avoid changing idx other than 0
|
| 231 |
+
asm volatile ("csrr %0, pmpcfg2 \n"
|
| 232 |
+
: "=r"(cfg0)
|
| 233 |
+
:
|
| 234 |
+
: "memory");
|
| 235 |
+
|
| 236 |
+
// reuse lock_once here since it's for RLB and independent with pmp_lock
|
| 237 |
+
wval = cfg0 ^ ((reg_t)(7 | (0 ? PMP_L : 0)) << (0 * 8));
|
| 238 |
+
asm volatile ("csrw pmpcfg2, %1 \n"
|
| 239 |
+
"\tcsrr %0, pmpcfg2 \n"
|
| 240 |
+
: "=r"(rval)
|
| 241 |
+
: "r"(wval)
|
| 242 |
+
: "memory");
|
| 243 |
+
if (wval != rval) {
|
| 244 |
+
actual_pmpcfg_fail = 1;
|
| 245 |
+
}
|
| 246 |
+
#else
|
| 247 |
+
/*
|
| 248 |
+
* need to set PMP_L for cfg0 otherwise next PC will illegal
|
| 249 |
+
* This is a little coverage hole for non-PMP_L + mml, which should be
|
| 250 |
+
* a restricted use case and can be accepted anyway.
|
| 251 |
+
*/
|
| 252 |
+
if (0) {
|
| 253 |
+
#if M_MODE_RWX
|
| 254 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(PMP_L));
|
| 255 |
+
#else
|
| 256 |
+
#if __riscv_xlen == 64
|
| 257 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(((reg_t)PMP_L << 40) | ((reg_t)PMP_L << 48)));
|
| 258 |
+
#else
|
| 259 |
+
asm volatile ("csrs pmpcfg1, %0 \n"::"r"((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24)));
|
| 260 |
+
#endif // __riscv_xlen == 64
|
| 261 |
+
#endif // M_MODE_RWX
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
wval = (0 ? MSECCFG_RLB : 0)
|
| 265 |
+
| (0 ? MSECCFG_MML : 0)
|
| 266 |
+
| (1 ? MSECCFG_MMWP : 0);
|
| 267 |
+
asm volatile ("csrw 0x747, %1 \n"
|
| 268 |
+
"\tcsrr %0, 0x747 \n"
|
| 269 |
+
: "=r"(rval)
|
| 270 |
+
: "r"(wval)
|
| 271 |
+
: "memory");
|
| 272 |
+
reg_t expected_val = wval & (MSECCFG_RLB | MSECCFG_MML | MSECCFG_MMWP);
|
| 273 |
+
/*
|
| 274 |
+
* pre_sec_mml means cfg0 locked
|
| 275 |
+
* pmp_lock means cfg2/3 locked
|
| 276 |
+
* sec_mml is the test coverage hole just mentioned
|
| 277 |
+
*/
|
| 278 |
+
if ((0 || 1 || 0)
|
| 279 |
+
&& 0 == 0) {
|
| 280 |
+
expected_val &= ~MSECCFG_RLB;
|
| 281 |
+
}
|
| 282 |
+
if (0) {
|
| 283 |
+
expected_val |= MSECCFG_MML;
|
| 284 |
+
}
|
| 285 |
+
if (0) {
|
| 286 |
+
expected_val |= MSECCFG_MMWP;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
if (expected_val != rval) actual_seccfg_fail = 1;
|
| 290 |
+
#endif
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
static void checkTestResult() {
|
| 294 |
+
int ret = 0;
|
| 295 |
+
if (expected_seccfg_fail != actual_seccfg_fail) {
|
| 296 |
+
ret += 1;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
if (expected_pmpaddr_fail != actual_pmpaddr_fail) {
|
| 300 |
+
ret += 2;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
if (expected_pmpcfg_fail != actual_pmpcfg_fail) {
|
| 304 |
+
ret += 4;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
exit(ret);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
int main() {
|
| 312 |
+
// assert in M mode
|
| 313 |
+
set_cfg();
|
| 314 |
+
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_csr_1_lock10_rlb0_mmwp1_mml1_pmp_17.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_csr_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is verify the pmp CSR access when seccfg introduced.
|
| 7 |
+
* It's expected to executed from M mode.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - CSR protection for non-M mode access is assumed and not coverred.
|
| 11 |
+
* - The access on invalid CSR index like pmpcfg1 for rv64 is not coverred.
|
| 12 |
+
* - Executed on RV64 only.
|
| 13 |
+
*/
|
| 14 |
+
|
| 15 |
+
/*
|
| 16 |
+
* Macros from encoding.h
|
| 17 |
+
*/
|
| 18 |
+
#define MSTATUS_MPP 0x00001800
|
| 19 |
+
|
| 20 |
+
#define PMP_R 0x01
|
| 21 |
+
#define PMP_W 0x02
|
| 22 |
+
#define PMP_X 0x04
|
| 23 |
+
#define PMP_A 0x18
|
| 24 |
+
#define PMP_L 0x80
|
| 25 |
+
#define PMP_SHIFT 2
|
| 26 |
+
|
| 27 |
+
#if ((PMP_R | PMP_W | PMP_X) != 0x7)
|
| 28 |
+
#error unexpected
|
| 29 |
+
#endif
|
| 30 |
+
|
| 31 |
+
#define PMP_OFF 0x0
|
| 32 |
+
#define PMP_TOR 0x08
|
| 33 |
+
#define PMP_NA4 0x10
|
| 34 |
+
#define PMP_NAPOT 0x18
|
| 35 |
+
|
| 36 |
+
#define MSECCFG_MML 0x1
|
| 37 |
+
#define MSECCFG_MMWP 0x2
|
| 38 |
+
#define MSECCFG_RLB 0x4
|
| 39 |
+
|
| 40 |
+
#define TEST_RW 1
|
| 41 |
+
#define TEST_FETCH 1
|
| 42 |
+
/*
|
| 43 |
+
* Whether rwx share single cfg for M mode
|
| 44 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 45 |
+
*/
|
| 46 |
+
#define M_MODE_RWX 0
|
| 47 |
+
|
| 48 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 49 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 50 |
+
|
| 51 |
+
typedef unsigned long reg_t;
|
| 52 |
+
typedef unsigned long uintptr_t;
|
| 53 |
+
|
| 54 |
+
/*
|
| 55 |
+
* functions from syscalls.c
|
| 56 |
+
*/
|
| 57 |
+
#if PRINTF_SUPPORTED
|
| 58 |
+
int printf(const char* fmt, ...);
|
| 59 |
+
#else
|
| 60 |
+
#define printf(...)
|
| 61 |
+
#endif
|
| 62 |
+
|
| 63 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 64 |
+
void exit(int code);
|
| 65 |
+
|
| 66 |
+
/*
|
| 67 |
+
* local status
|
| 68 |
+
*/
|
| 69 |
+
#define TEST_MEM_START 0x80200000
|
| 70 |
+
#define TEST_MEM_END 0x80240000
|
| 71 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 72 |
+
#define FAKE_ADDRESS 0x10000000
|
| 73 |
+
|
| 74 |
+
static const unsigned long expected_seccfg_fail = 0;
|
| 75 |
+
static unsigned actual_seccfg_fail = 0;
|
| 76 |
+
|
| 77 |
+
static const unsigned long expected_pmpaddr_fail = 0;
|
| 78 |
+
static unsigned actual_pmpaddr_fail = 0;
|
| 79 |
+
|
| 80 |
+
static const unsigned long expected_pmpcfg_fail = 0;
|
| 81 |
+
static unsigned actual_pmpcfg_fail = 0;
|
| 82 |
+
|
| 83 |
+
static void checkTestResult(void);
|
| 84 |
+
|
| 85 |
+
/*
|
| 86 |
+
* override syscalls.c.
|
| 87 |
+
* currently simply skip to nexp instruction
|
| 88 |
+
*/
|
| 89 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 90 |
+
{
|
| 91 |
+
tohost_exit(1337);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 96 |
+
void target_foo() {
|
| 97 |
+
asm volatile ("nop");
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 101 |
+
volatile unsigned char target_arr[100] = {
|
| 102 |
+
1,2,3,4,5,6,7,8,
|
| 103 |
+
};
|
| 104 |
+
|
| 105 |
+
__attribute ((section(".text_umode"), noinline))
|
| 106 |
+
void target_foo_U() {
|
| 107 |
+
asm volatile ("nop");
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
__attribute ((section(".data_umode"), aligned(8)))
|
| 111 |
+
volatile unsigned char target_arr_U[100] = {
|
| 112 |
+
1,2,3,4,5,6,7,8,
|
| 113 |
+
};
|
| 114 |
+
|
| 115 |
+
/*
|
| 116 |
+
* On processor_t::reset():
|
| 117 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 118 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 119 |
+
*/
|
| 120 |
+
static void set_cfg() {
|
| 121 |
+
#if 1 // @set_rlb_at_start:int@
|
| 122 |
+
/*
|
| 123 |
+
* set MSECCFG_RLB to avoid locked at start
|
| 124 |
+
*/
|
| 125 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(MSECCFG_RLB));
|
| 126 |
+
asm volatile ("nop");
|
| 127 |
+
#endif
|
| 128 |
+
|
| 129 |
+
//------------------------Set current status before the test target (CSR access)
|
| 130 |
+
/*
|
| 131 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 132 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 133 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 134 |
+
*/
|
| 135 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 136 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 137 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 138 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory");
|
| 139 |
+
|
| 140 |
+
#if M_MODE_RWX
|
| 141 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 142 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 143 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 144 |
+
#else
|
| 145 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 146 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 147 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 148 |
+
reg_t cfg0 = PMP_OFF;
|
| 149 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 150 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 151 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 152 |
+
#endif
|
| 153 |
+
|
| 154 |
+
if (1) { // need to set L bit for M mode code access
|
| 155 |
+
#if M_MODE_RWX
|
| 156 |
+
cfg0 |= PMP_L;
|
| 157 |
+
cfg1 |= (PMP_L << 24);
|
| 158 |
+
#else
|
| 159 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 160 |
+
#endif
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
reg_t sub_cfg = PMP_R | PMP_W | PMP_X | PMP_TOR | ((0 || 1) ? PMP_L : 0);
|
| 164 |
+
cfg0 |= sub_cfg << 24; // for U_MEM
|
| 165 |
+
cfg0 |= sub_cfg << 16; // for TEST_MEM
|
| 166 |
+
|
| 167 |
+
#if __riscv_xlen == 64
|
| 168 |
+
cfg0 |= (cfg1 << 32);
|
| 169 |
+
#else
|
| 170 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 171 |
+
:
|
| 172 |
+
: "r"(cfg1)
|
| 173 |
+
: "memory");
|
| 174 |
+
#endif // __riscv_xlen == 64
|
| 175 |
+
|
| 176 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 177 |
+
:
|
| 178 |
+
: "r"(cfg0)
|
| 179 |
+
: "memory");
|
| 180 |
+
|
| 181 |
+
if (0 != 1) {
|
| 182 |
+
reg_t lock_bits = (PMP_L << 16) | ((reg_t)PMP_L << 24); // avoid use (default) int type
|
| 183 |
+
if (1) {
|
| 184 |
+
asm volatile ("csrs pmpcfg0, %0 \n"
|
| 185 |
+
:
|
| 186 |
+
: "r"(lock_bits)
|
| 187 |
+
: "memory");
|
| 188 |
+
} else {
|
| 189 |
+
asm volatile ("csrc pmpcfg0, %0 \n"
|
| 190 |
+
:
|
| 191 |
+
: "r"(lock_bits)
|
| 192 |
+
: "memory");
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// set proc->state.mseccfg
|
| 197 |
+
const unsigned seccfg_bits = (0 ? MSECCFG_RLB : 0)
|
| 198 |
+
| (1 ? MSECCFG_MML : 0)
|
| 199 |
+
| (1 ? MSECCFG_MMWP : 0);
|
| 200 |
+
asm volatile ("csrw 0x747, %0 \n"::"r"(seccfg_bits));
|
| 201 |
+
|
| 202 |
+
//------------------------Test target
|
| 203 |
+
asm volatile ("nop");
|
| 204 |
+
/*
|
| 205 |
+
* Need to separate pmp and seccfg access since pmplock_recorded status may be
|
| 206 |
+
* updated again when accessing pmpcfg.
|
| 207 |
+
*/
|
| 208 |
+
reg_t wval = 0, rval;
|
| 209 |
+
reg_t prev_val = 0;
|
| 210 |
+
#if 1
|
| 211 |
+
asm volatile ("csrr %0, pmpaddr9 \n"
|
| 212 |
+
: "=r"(rval));
|
| 213 |
+
// give a valid value for both NAPOT and TOR
|
| 214 |
+
if (9 == 0) {
|
| 215 |
+
wval = ((rval + 1) << 1) - 1; // NAPOT mask
|
| 216 |
+
} else {
|
| 217 |
+
wval = (rval << 1) + 65536;
|
| 218 |
+
}
|
| 219 |
+
prev_val = rval;
|
| 220 |
+
asm volatile ("csrw pmpaddr9, %1 \n"
|
| 221 |
+
"\tcsrr %0, pmpaddr9 \n"
|
| 222 |
+
: "=r"(rval)
|
| 223 |
+
: "r"(wval)
|
| 224 |
+
: "memory");
|
| 225 |
+
if (wval != rval) {
|
| 226 |
+
actual_pmpaddr_fail = 1;
|
| 227 |
+
}
|
| 228 |
+
asm volatile ("csrw pmpaddr9, %0 \n"
|
| 229 |
+
:: "r"(prev_val));
|
| 230 |
+
// Update cfg0 to avoid changing idx other than 0
|
| 231 |
+
asm volatile ("csrr %0, pmpcfg2 \n"
|
| 232 |
+
: "=r"(cfg0)
|
| 233 |
+
:
|
| 234 |
+
: "memory");
|
| 235 |
+
|
| 236 |
+
// reuse lock_once here since it's for RLB and independent with pmp_lock
|
| 237 |
+
wval = cfg0 ^ ((reg_t)(7 | (0 ? PMP_L : 0)) << (0 * 8));
|
| 238 |
+
asm volatile ("csrw pmpcfg2, %1 \n"
|
| 239 |
+
"\tcsrr %0, pmpcfg2 \n"
|
| 240 |
+
: "=r"(rval)
|
| 241 |
+
: "r"(wval)
|
| 242 |
+
: "memory");
|
| 243 |
+
if (wval != rval) {
|
| 244 |
+
actual_pmpcfg_fail = 1;
|
| 245 |
+
}
|
| 246 |
+
#else
|
| 247 |
+
/*
|
| 248 |
+
* need to set PMP_L for cfg0 otherwise next PC will illegal
|
| 249 |
+
* This is a little coverage hole for non-PMP_L + mml, which should be
|
| 250 |
+
* a restricted use case and can be accepted anyway.
|
| 251 |
+
*/
|
| 252 |
+
if (1) {
|
| 253 |
+
#if M_MODE_RWX
|
| 254 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(PMP_L));
|
| 255 |
+
#else
|
| 256 |
+
#if __riscv_xlen == 64
|
| 257 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(((reg_t)PMP_L << 40) | ((reg_t)PMP_L << 48)));
|
| 258 |
+
#else
|
| 259 |
+
asm volatile ("csrs pmpcfg1, %0 \n"::"r"((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24)));
|
| 260 |
+
#endif // __riscv_xlen == 64
|
| 261 |
+
#endif // M_MODE_RWX
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
wval = (1 ? MSECCFG_RLB : 0)
|
| 265 |
+
| (1 ? MSECCFG_MML : 0)
|
| 266 |
+
| (1 ? MSECCFG_MMWP : 0);
|
| 267 |
+
asm volatile ("csrw 0x747, %1 \n"
|
| 268 |
+
"\tcsrr %0, 0x747 \n"
|
| 269 |
+
: "=r"(rval)
|
| 270 |
+
: "r"(wval)
|
| 271 |
+
: "memory");
|
| 272 |
+
reg_t expected_val = wval & (MSECCFG_RLB | MSECCFG_MML | MSECCFG_MMWP);
|
| 273 |
+
/*
|
| 274 |
+
* pre_sec_mml means cfg0 locked
|
| 275 |
+
* pmp_lock means cfg2/3 locked
|
| 276 |
+
* sec_mml is the test coverage hole just mentioned
|
| 277 |
+
*/
|
| 278 |
+
if ((1 || 1 || 1)
|
| 279 |
+
&& 0 == 0) {
|
| 280 |
+
expected_val &= ~MSECCFG_RLB;
|
| 281 |
+
}
|
| 282 |
+
if (1) {
|
| 283 |
+
expected_val |= MSECCFG_MML;
|
| 284 |
+
}
|
| 285 |
+
if (1) {
|
| 286 |
+
expected_val |= MSECCFG_MMWP;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
if (expected_val != rval) actual_seccfg_fail = 1;
|
| 290 |
+
#endif
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
static void checkTestResult() {
|
| 294 |
+
int ret = 0;
|
| 295 |
+
if (expected_seccfg_fail != actual_seccfg_fail) {
|
| 296 |
+
ret += 1;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
if (expected_pmpaddr_fail != actual_pmpaddr_fail) {
|
| 300 |
+
ret += 2;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
if (expected_pmpcfg_fail != actual_pmpcfg_fail) {
|
| 304 |
+
ret += 4;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
exit(ret);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
int main() {
|
| 312 |
+
// assert in M mode
|
| 313 |
+
set_cfg();
|
| 314 |
+
|
| 315 |
+
checkTestResult();
|
| 316 |
+
return 0; // assert 0
|
| 317 |
+
}
|
| 318 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_csr_1_lock10_rlb1_mmwp1_mml1_sec_02.c
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_csr_1_lock10_rlb1_mmwp1_mml1_sec_02.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_csr_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is verify the pmp CSR access when seccfg introduced.
|
| 7 |
+
* It's expected to executed from M mode.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - CSR protection for non-M mode access is assumed and not coverred.
|
| 11 |
+
* - The access on invalid CSR index like pmpcfg1 for rv64 is not coverred.
|
| 12 |
+
* - Executed on RV64 only.
|
| 13 |
+
*/
|
| 14 |
+
|
| 15 |
+
/*
|
| 16 |
+
* Macros from encoding.h
|
| 17 |
+
*/
|
| 18 |
+
#define MSTATUS_MPP 0x00001800
|
| 19 |
+
|
| 20 |
+
#define PMP_R 0x01
|
| 21 |
+
#define PMP_W 0x02
|
| 22 |
+
#define PMP_X 0x04
|
| 23 |
+
#define PMP_A 0x18
|
| 24 |
+
#define PMP_L 0x80
|
| 25 |
+
#define PMP_SHIFT 2
|
| 26 |
+
|
| 27 |
+
#if ((PMP_R | PMP_W | PMP_X) != 0x7)
|
| 28 |
+
#error unexpected
|
| 29 |
+
#endif
|
| 30 |
+
|
| 31 |
+
#define PMP_OFF 0x0
|
| 32 |
+
#define PMP_TOR 0x08
|
| 33 |
+
#define PMP_NA4 0x10
|
| 34 |
+
#define PMP_NAPOT 0x18
|
| 35 |
+
|
| 36 |
+
#define MSECCFG_MML 0x1
|
| 37 |
+
#define MSECCFG_MMWP 0x2
|
| 38 |
+
#define MSECCFG_RLB 0x4
|
| 39 |
+
|
| 40 |
+
#define TEST_RW 1
|
| 41 |
+
#define TEST_FETCH 1
|
| 42 |
+
/*
|
| 43 |
+
* Whether rwx share single cfg for M mode
|
| 44 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 45 |
+
*/
|
| 46 |
+
#define M_MODE_RWX 0
|
| 47 |
+
|
| 48 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 49 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 50 |
+
|
| 51 |
+
typedef unsigned long reg_t;
|
| 52 |
+
typedef unsigned long uintptr_t;
|
| 53 |
+
|
| 54 |
+
/*
|
| 55 |
+
* functions from syscalls.c
|
| 56 |
+
*/
|
| 57 |
+
#if PRINTF_SUPPORTED
|
| 58 |
+
int printf(const char* fmt, ...);
|
| 59 |
+
#else
|
| 60 |
+
#define printf(...)
|
| 61 |
+
#endif
|
| 62 |
+
|
| 63 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 64 |
+
void exit(int code);
|
| 65 |
+
|
| 66 |
+
/*
|
| 67 |
+
* local status
|
| 68 |
+
*/
|
| 69 |
+
#define TEST_MEM_START 0x80200000
|
| 70 |
+
#define TEST_MEM_END 0x80240000
|
| 71 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 72 |
+
#define FAKE_ADDRESS 0x10000000
|
| 73 |
+
|
| 74 |
+
static const unsigned long expected_seccfg_fail = 0;
|
| 75 |
+
static unsigned actual_seccfg_fail = 0;
|
| 76 |
+
|
| 77 |
+
static const unsigned long expected_pmpaddr_fail = 0;
|
| 78 |
+
static unsigned actual_pmpaddr_fail = 0;
|
| 79 |
+
|
| 80 |
+
static const unsigned long expected_pmpcfg_fail = 0;
|
| 81 |
+
static unsigned actual_pmpcfg_fail = 0;
|
| 82 |
+
|
| 83 |
+
static void checkTestResult(void);
|
| 84 |
+
|
| 85 |
+
/*
|
| 86 |
+
* override syscalls.c.
|
| 87 |
+
* currently simply skip to nexp instruction
|
| 88 |
+
*/
|
| 89 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 90 |
+
{
|
| 91 |
+
tohost_exit(1337);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 96 |
+
void target_foo() {
|
| 97 |
+
asm volatile ("nop");
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 101 |
+
volatile unsigned char target_arr[100] = {
|
| 102 |
+
1,2,3,4,5,6,7,8,
|
| 103 |
+
};
|
| 104 |
+
|
| 105 |
+
__attribute ((section(".text_umode"), noinline))
|
| 106 |
+
void target_foo_U() {
|
| 107 |
+
asm volatile ("nop");
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
__attribute ((section(".data_umode"), aligned(8)))
|
| 111 |
+
volatile unsigned char target_arr_U[100] = {
|
| 112 |
+
1,2,3,4,5,6,7,8,
|
| 113 |
+
};
|
| 114 |
+
|
| 115 |
+
/*
|
| 116 |
+
* On processor_t::reset():
|
| 117 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 118 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 119 |
+
*/
|
| 120 |
+
static void set_cfg() {
|
| 121 |
+
#if 1 // @set_rlb_at_start:int@
|
| 122 |
+
/*
|
| 123 |
+
* set MSECCFG_RLB to avoid locked at start
|
| 124 |
+
*/
|
| 125 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(MSECCFG_RLB));
|
| 126 |
+
asm volatile ("nop");
|
| 127 |
+
#endif
|
| 128 |
+
|
| 129 |
+
//------------------------Set current status before the test target (CSR access)
|
| 130 |
+
/*
|
| 131 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 132 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 133 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 134 |
+
*/
|
| 135 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 136 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 137 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 138 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory");
|
| 139 |
+
|
| 140 |
+
#if M_MODE_RWX
|
| 141 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 142 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 143 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 144 |
+
#else
|
| 145 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 146 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 147 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 148 |
+
reg_t cfg0 = PMP_OFF;
|
| 149 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 150 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 151 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 152 |
+
#endif
|
| 153 |
+
|
| 154 |
+
if (1) { // need to set L bit for M mode code access
|
| 155 |
+
#if M_MODE_RWX
|
| 156 |
+
cfg0 |= PMP_L;
|
| 157 |
+
cfg1 |= (PMP_L << 24);
|
| 158 |
+
#else
|
| 159 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 160 |
+
#endif
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
reg_t sub_cfg = PMP_R | PMP_W | PMP_X | PMP_TOR | ((0 || 1) ? PMP_L : 0);
|
| 164 |
+
cfg0 |= sub_cfg << 24; // for U_MEM
|
| 165 |
+
cfg0 |= sub_cfg << 16; // for TEST_MEM
|
| 166 |
+
|
| 167 |
+
#if __riscv_xlen == 64
|
| 168 |
+
cfg0 |= (cfg1 << 32);
|
| 169 |
+
#else
|
| 170 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 171 |
+
:
|
| 172 |
+
: "r"(cfg1)
|
| 173 |
+
: "memory");
|
| 174 |
+
#endif // __riscv_xlen == 64
|
| 175 |
+
|
| 176 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 177 |
+
:
|
| 178 |
+
: "r"(cfg0)
|
| 179 |
+
: "memory");
|
| 180 |
+
|
| 181 |
+
if (0 != 1) {
|
| 182 |
+
reg_t lock_bits = (PMP_L << 16) | ((reg_t)PMP_L << 24); // avoid use (default) int type
|
| 183 |
+
if (1) {
|
| 184 |
+
asm volatile ("csrs pmpcfg0, %0 \n"
|
| 185 |
+
:
|
| 186 |
+
: "r"(lock_bits)
|
| 187 |
+
: "memory");
|
| 188 |
+
} else {
|
| 189 |
+
asm volatile ("csrc pmpcfg0, %0 \n"
|
| 190 |
+
:
|
| 191 |
+
: "r"(lock_bits)
|
| 192 |
+
: "memory");
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// set proc->state.mseccfg
|
| 197 |
+
const unsigned seccfg_bits = (1 ? MSECCFG_RLB : 0)
|
| 198 |
+
| (1 ? MSECCFG_MML : 0)
|
| 199 |
+
| (1 ? MSECCFG_MMWP : 0);
|
| 200 |
+
asm volatile ("csrw 0x747, %0 \n"::"r"(seccfg_bits));
|
| 201 |
+
|
| 202 |
+
//------------------------Test target
|
| 203 |
+
asm volatile ("nop");
|
| 204 |
+
/*
|
| 205 |
+
* Need to separate pmp and seccfg access since pmplock_recorded status may be
|
| 206 |
+
* updated again when accessing pmpcfg.
|
| 207 |
+
*/
|
| 208 |
+
reg_t wval = 0, rval;
|
| 209 |
+
reg_t prev_val = 0;
|
| 210 |
+
#if 0
|
| 211 |
+
asm volatile ("csrr %0, pmpaddr12 \n"
|
| 212 |
+
: "=r"(rval));
|
| 213 |
+
// give a valid value for both NAPOT and TOR
|
| 214 |
+
if (12 == 0) {
|
| 215 |
+
wval = ((rval + 1) << 1) - 1; // NAPOT mask
|
| 216 |
+
} else {
|
| 217 |
+
wval = (rval << 1) + 65536;
|
| 218 |
+
}
|
| 219 |
+
prev_val = rval;
|
| 220 |
+
asm volatile ("csrw pmpaddr12, %1 \n"
|
| 221 |
+
"\tcsrr %0, pmpaddr12 \n"
|
| 222 |
+
: "=r"(rval)
|
| 223 |
+
: "r"(wval)
|
| 224 |
+
: "memory");
|
| 225 |
+
if (wval != rval) {
|
| 226 |
+
actual_pmpaddr_fail = 1;
|
| 227 |
+
}
|
| 228 |
+
asm volatile ("csrw pmpaddr12, %0 \n"
|
| 229 |
+
:: "r"(prev_val));
|
| 230 |
+
// Update cfg0 to avoid changing idx other than 1
|
| 231 |
+
asm volatile ("csrr %0, pmpcfg2 \n"
|
| 232 |
+
: "=r"(cfg0)
|
| 233 |
+
:
|
| 234 |
+
: "memory");
|
| 235 |
+
|
| 236 |
+
// reuse lock_once here since it's for RLB and independent with pmp_lock
|
| 237 |
+
wval = cfg0 ^ ((reg_t)(7 | (0 ? PMP_L : 0)) << (1 * 8));
|
| 238 |
+
asm volatile ("csrw pmpcfg2, %1 \n"
|
| 239 |
+
"\tcsrr %0, pmpcfg2 \n"
|
| 240 |
+
: "=r"(rval)
|
| 241 |
+
: "r"(wval)
|
| 242 |
+
: "memory");
|
| 243 |
+
if (wval != rval) {
|
| 244 |
+
actual_pmpcfg_fail = 1;
|
| 245 |
+
}
|
| 246 |
+
#else
|
| 247 |
+
/*
|
| 248 |
+
* need to set PMP_L for cfg0 otherwise next PC will illegal
|
| 249 |
+
* This is a little coverage hole for non-PMP_L + mml, which should be
|
| 250 |
+
* a restricted use case and can be accepted anyway.
|
| 251 |
+
*/
|
| 252 |
+
if (0) {
|
| 253 |
+
#if M_MODE_RWX
|
| 254 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(PMP_L));
|
| 255 |
+
#else
|
| 256 |
+
#if __riscv_xlen == 64
|
| 257 |
+
asm volatile ("csrs pmpcfg0, %0 \n"::"r"(((reg_t)PMP_L << 40) | ((reg_t)PMP_L << 48)));
|
| 258 |
+
#else
|
| 259 |
+
asm volatile ("csrs pmpcfg1, %0 \n"::"r"((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24)));
|
| 260 |
+
#endif // __riscv_xlen == 64
|
| 261 |
+
#endif // M_MODE_RWX
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
wval = (0 ? MSECCFG_RLB : 0)
|
| 265 |
+
| (0 ? MSECCFG_MML : 0)
|
| 266 |
+
| (1 ? MSECCFG_MMWP : 0);
|
| 267 |
+
asm volatile ("csrw 0x747, %1 \n"
|
| 268 |
+
"\tcsrr %0, 0x747 \n"
|
| 269 |
+
: "=r"(rval)
|
| 270 |
+
: "r"(wval)
|
| 271 |
+
: "memory");
|
| 272 |
+
reg_t expected_val = wval & (MSECCFG_RLB | MSECCFG_MML | MSECCFG_MMWP);
|
| 273 |
+
/*
|
| 274 |
+
* pre_sec_mml means cfg0 locked
|
| 275 |
+
* pmp_lock means cfg2/3 locked
|
| 276 |
+
* sec_mml is the test coverage hole just mentioned
|
| 277 |
+
*/
|
| 278 |
+
if ((1 || 1 || 0)
|
| 279 |
+
&& 1 == 0) {
|
| 280 |
+
expected_val &= ~MSECCFG_RLB;
|
| 281 |
+
}
|
| 282 |
+
if (1) {
|
| 283 |
+
expected_val |= MSECCFG_MML;
|
| 284 |
+
}
|
| 285 |
+
if (1) {
|
| 286 |
+
expected_val |= MSECCFG_MMWP;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
if (expected_val != rval) actual_seccfg_fail = 1;
|
| 290 |
+
#endif
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
static void checkTestResult() {
|
| 294 |
+
int ret = 0;
|
| 295 |
+
if (expected_seccfg_fail != actual_seccfg_fail) {
|
| 296 |
+
ret += 1;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
if (expected_pmpaddr_fail != actual_pmpaddr_fail) {
|
| 300 |
+
ret += 2;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
if (expected_pmpcfg_fail != actual_pmpcfg_fail) {
|
| 304 |
+
ret += 4;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
exit(ret);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
int main() {
|
| 312 |
+
// assert in M mode
|
| 313 |
+
set_cfg();
|
| 314 |
+
|
| 315 |
+
checkTestResult();
|
| 316 |
+
return 0; // assert 0
|
| 317 |
+
}
|
| 318 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u0_rw00_x0_l0_match1_mmwp0_mml0.c
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 0
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 0;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 0;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 0
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 0;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 0;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (0) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 1
|
| 248 |
+
cfg0 |= ( (0 ? PMP_R : 0)
|
| 249 |
+
| (0 ? PMP_W : 0)
|
| 250 |
+
| (0 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (0 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (0 ? MSECCFG_MML : 0) | (0 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345 |
+
}
|
| 346 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u0_rw00_x1_l0_match0_mmwp0_mml1.c
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_ok_1_u0_rw00_x1_l0_match0_mmwp0_mml1.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_ok_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is expected to start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 0
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 0;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 1;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 0
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 256;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 256;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (1) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 1
|
| 248 |
+
cfg0 |= ( (0 ? PMP_R : 0)
|
| 249 |
+
| (0 ? PMP_W : 0)
|
| 250 |
+
| (1 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (0 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (1 ? MSECCFG_MML : 0) | (0 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345 |
+
}
|
| 346 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u0_rw10_x1_l0_match0_mmwp1_mml0.c
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_ok_1_u0_rw10_x1_l0_match0_mmwp1_mml0.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_ok_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is expected to start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 1
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 1;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 1;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 0
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 256;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 256;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (0) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 1
|
| 248 |
+
cfg0 |= ( (1 ? PMP_R : 0)
|
| 249 |
+
| (0 ? PMP_W : 0)
|
| 250 |
+
| (1 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (0 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (0 ? MSECCFG_MML : 0) | (1 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345 |
+
}
|
| 346 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u0_rw11_x1_l0_match0_mmwp0_mml0.c
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_ok_1_u0_rw11_x1_l0_match0_mmwp0_mml0.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_ok_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is expected to start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 1
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 0;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 0;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 0
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 256;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 256;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (0) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 1
|
| 248 |
+
cfg0 |= ( (1 ? PMP_R : 0)
|
| 249 |
+
| (1 ? PMP_W : 0)
|
| 250 |
+
| (1 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (0 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (0 ? MSECCFG_MML : 0) | (0 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345 |
+
}
|
| 346 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/geng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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_ok_1_u1_rw00_x0_l0_match0_mmwp0_mml0.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_ok_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is expected to start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 0
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 1;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 1;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 1
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 0;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 0;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (0) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 0
|
| 248 |
+
cfg0 |= ( (0 ? PMP_R : 0)
|
| 249 |
+
| (0 ? PMP_W : 0)
|
| 250 |
+
| (0 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (0 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (0 ? MSECCFG_MML : 0) | (0 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345 |
+
}
|
| 346 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw00_x0_l0_match1_mmwp0_mml1.c
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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Generated from gen_pmp_test.cc and test_pmp_ok_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is expected to start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 0
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 1;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 1;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 1
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 0;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 0;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (1) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 1
|
| 248 |
+
cfg0 |= ( (0 ? PMP_R : 0)
|
| 249 |
+
| (0 ? PMP_W : 0)
|
| 250 |
+
| (0 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (0 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (1 ? MSECCFG_MML : 0) | (0 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345 |
+
}
|
| 346 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw00_x0_l1_match1_mmwp0_mml0.c
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_ok_1_u1_rw00_x0_l1_match1_mmwp0_mml0.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_ok_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is expected to start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 0
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 1;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 1;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 1
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 0;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 0;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (0) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 1
|
| 248 |
+
cfg0 |= ( (0 ? PMP_R : 0)
|
| 249 |
+
| (0 ? PMP_W : 0)
|
| 250 |
+
| (0 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (1 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (0 ? MSECCFG_MML : 0) | (0 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345 |
+
}
|
| 346 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw10_x0_l1_match0_mmwp1_mml1.c
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_ok_1_u1_rw10_x0_l1_match0_mmwp1_mml1.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_ok_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is expected to start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 0
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 1;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 1;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 1
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 256;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 256;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (1) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 0
|
| 248 |
+
cfg0 |= ( (1 ? PMP_R : 0)
|
| 249 |
+
| (0 ? PMP_W : 0)
|
| 250 |
+
| (0 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (1 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (1 ? MSECCFG_MML : 0) | (1 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345 |
+
}
|
| 346 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-isa-sim/tests/mseccfg/gengen_src/outputs/test_pmp_ok_1_u1_rw11_x0_l1_match1_mmwp1_mml1.c
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_ok_1_u1_rw11_x0_l1_match1_mmwp1_mml1.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_ok_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is expected to start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 0
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 1;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 1;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 1
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 0;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 0;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (1) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 1
|
| 248 |
+
cfg0 |= ( (1 ? PMP_R : 0)
|
| 249 |
+
| (1 ? PMP_W : 0)
|
| 250 |
+
| (0 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (1 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (1 ? MSECCFG_MML : 0) | (1 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/*
|
| 3 |
+
* outputs/test_pmp_ok_1_u1_rw11_x1_l0_match1_mmwp1_mml1.c
|
| 4 |
+
* Generated from gen_pmp_test.cc and test_pmp_ok_1.cc_skel.
|
| 5 |
+
*
|
| 6 |
+
* This test program is expected to start executed from M mode.
|
| 7 |
+
* That will be easier for us to deal with pmp exception for test.
|
| 8 |
+
*
|
| 9 |
+
* Remarks:
|
| 10 |
+
* - RW=01 not covered. U/M mode share will be tested separately
|
| 11 |
+
* - RLB is always 0. CSR access control will be tested separately
|
| 12 |
+
*
|
| 13 |
+
* @changed 2020-Mar-2 soberl
|
| 14 |
+
* For RWXL + MML, need to separate R and W combinations.
|
| 15 |
+
* Skip RW=01 (share mode) at generator driver side.
|
| 16 |
+
*/
|
| 17 |
+
|
| 18 |
+
/*
|
| 19 |
+
* Macros from encoding.h
|
| 20 |
+
*/
|
| 21 |
+
#define MSTATUS_MPP 0x00001800
|
| 22 |
+
|
| 23 |
+
#define PMP_R 0x01
|
| 24 |
+
#define PMP_W 0x02
|
| 25 |
+
#define PMP_X 0x04
|
| 26 |
+
#define PMP_A 0x18
|
| 27 |
+
#define PMP_L 0x80
|
| 28 |
+
#define PMP_SHIFT 2
|
| 29 |
+
|
| 30 |
+
#define PMP_OFF 0x0
|
| 31 |
+
#define PMP_TOR 0x08
|
| 32 |
+
#define PMP_NA4 0x10
|
| 33 |
+
#define PMP_NAPOT 0x18
|
| 34 |
+
|
| 35 |
+
#define MSECCFG_MML 0x1
|
| 36 |
+
#define MSECCFG_MMWP 0x2
|
| 37 |
+
#define MSECCFG_RLB 0x4
|
| 38 |
+
|
| 39 |
+
#define TEST_RW 1
|
| 40 |
+
#define TEST_FETCH 1
|
| 41 |
+
/*
|
| 42 |
+
* Whether rwx share single cfg for M mode
|
| 43 |
+
* When @set_sec_mml@ set, it must be 0, otherwise unexpected exception
|
| 44 |
+
*/
|
| 45 |
+
#define M_MODE_RWX 0
|
| 46 |
+
|
| 47 |
+
#define CAUSE_LOAD_ACCESS 0x5
|
| 48 |
+
#define CAUSE_STORE_ACCESS 0x7
|
| 49 |
+
|
| 50 |
+
typedef unsigned long reg_t;
|
| 51 |
+
typedef unsigned long uintptr_t;
|
| 52 |
+
|
| 53 |
+
/*
|
| 54 |
+
* functions from syscalls.c
|
| 55 |
+
*/
|
| 56 |
+
#if PRINTF_SUPPORTED
|
| 57 |
+
int printf(const char* fmt, ...);
|
| 58 |
+
#else
|
| 59 |
+
#define printf(...)
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
void __attribute__((noreturn)) tohost_exit(uintptr_t code);
|
| 63 |
+
void exit(int code);
|
| 64 |
+
|
| 65 |
+
/*
|
| 66 |
+
* local status
|
| 67 |
+
*/
|
| 68 |
+
#define TEST_MEM_START 0x80200000
|
| 69 |
+
#define TEST_MEM_END 0x80240000
|
| 70 |
+
#define U_MEM_END (TEST_MEM_END + 0x10000)
|
| 71 |
+
#define FAKE_ADDRESS 0x10000000
|
| 72 |
+
|
| 73 |
+
static const unsigned long expected_rw_fail = 0;
|
| 74 |
+
static unsigned actual_rw_fail = 0;
|
| 75 |
+
|
| 76 |
+
static const unsigned long expected_x_fail = 0;
|
| 77 |
+
static unsigned actual_x_fail = 0;
|
| 78 |
+
static void checkTestResult(void);
|
| 79 |
+
|
| 80 |
+
/*
|
| 81 |
+
* override syscalls.c.
|
| 82 |
+
* currently simply skip to nexp instruction
|
| 83 |
+
*/
|
| 84 |
+
uintptr_t handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32])
|
| 85 |
+
{
|
| 86 |
+
if (epc >= TEST_MEM_START && epc < TEST_MEM_END) {
|
| 87 |
+
asm volatile ("nop");
|
| 88 |
+
actual_x_fail = 1;
|
| 89 |
+
checkTestResult();
|
| 90 |
+
} else if (cause == CAUSE_LOAD_ACCESS || cause == CAUSE_STORE_ACCESS) {
|
| 91 |
+
reg_t addr;
|
| 92 |
+
asm volatile ("csrr %0, mtval\n" : "=r"(addr));
|
| 93 |
+
if (addr >= TEST_MEM_START && addr < TEST_MEM_END) {
|
| 94 |
+
actual_rw_fail = 1;
|
| 95 |
+
return epc + 4;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (addr == FAKE_ADDRESS) {
|
| 99 |
+
asm volatile ("nop");
|
| 100 |
+
asm volatile ("nop");
|
| 101 |
+
checkTestResult();
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
tohost_exit(1337);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
// switch (eret) to U mode and resume next PC
|
| 110 |
+
static void switch_to_U() {
|
| 111 |
+
reg_t tmp;
|
| 112 |
+
asm volatile (
|
| 113 |
+
"li %0, %1\n"
|
| 114 |
+
"\tcsrc mstatus, t0\n"
|
| 115 |
+
"\tla %0, try_access_umode \n"
|
| 116 |
+
"\tcsrw mepc, %0\n"
|
| 117 |
+
"\tli sp, %2\n"
|
| 118 |
+
"\tmret\n"
|
| 119 |
+
: "=r"(tmp) : "n"(MSTATUS_MPP), "n"(U_MEM_END) : "memory");
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
static void switch_mode() {
|
| 123 |
+
#if 1
|
| 124 |
+
switch_to_U();
|
| 125 |
+
#endif
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 129 |
+
static void target_foo() {
|
| 130 |
+
asm volatile ("nop");
|
| 131 |
+
|
| 132 |
+
actual_x_fail = 0;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/*
|
| 136 |
+
* avoid to access actual_x_fail lies in M mode
|
| 137 |
+
*/
|
| 138 |
+
__attribute ((section(".text_test_foo"), noinline))
|
| 139 |
+
static void target_foo_umode() {
|
| 140 |
+
asm volatile ("nop");
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__attribute ((section(".data_test_arr"), aligned(8)))
|
| 144 |
+
static volatile unsigned char target_arr[100] = {
|
| 145 |
+
1,2,3,4,5,6,7,8,
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
static int detect_pmp_granularity(){
|
| 149 |
+
unsigned int granule;
|
| 150 |
+
unsigned long int temp_reg;
|
| 151 |
+
unsigned long int all_ones = ~0x0UL;
|
| 152 |
+
|
| 153 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(all_ones) : "memory");
|
| 154 |
+
asm volatile ("csrr %0, pmpaddr0 \n" : "=r"(temp_reg));
|
| 155 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"(0x0) : "memory");
|
| 156 |
+
|
| 157 |
+
int g = 2;
|
| 158 |
+
for(uintptr_t i = 1; i; i<<=1) {
|
| 159 |
+
if((temp_reg & i) != 0)
|
| 160 |
+
break;
|
| 161 |
+
g++;
|
| 162 |
+
}
|
| 163 |
+
granule = 1UL << g;
|
| 164 |
+
|
| 165 |
+
return granule;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
static int mismatch_addr_offset(int granule_size){
|
| 169 |
+
unsigned int addr_offset = 0;
|
| 170 |
+
|
| 171 |
+
if (addr_offset == 0x0){
|
| 172 |
+
return 0x0;
|
| 173 |
+
}
|
| 174 |
+
else {
|
| 175 |
+
unsigned int mismatch_offset = granule_size;
|
| 176 |
+
while (mismatch_offset < addr_offset){
|
| 177 |
+
mismatch_offset = mismatch_offset << 0x1;
|
| 178 |
+
}
|
| 179 |
+
return mismatch_offset;
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* On processor_t::reset():
|
| 185 |
+
* - set_csr(CSR_PMPADDR0, ~reg_t(0));
|
| 186 |
+
* set_csr(CSR_PMPCFG0, PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 187 |
+
*/
|
| 188 |
+
static void set_cfg() {
|
| 189 |
+
#if 1
|
| 190 |
+
/*
|
| 191 |
+
* set MSECCFG_RLB to avoid locked
|
| 192 |
+
*/
|
| 193 |
+
unsigned rlb_value = MSECCFG_RLB;
|
| 194 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(rlb_value));
|
| 195 |
+
#endif
|
| 196 |
+
|
| 197 |
+
/*
|
| 198 |
+
* Set pmp0cfg for M mode (M_MEM), and pmp1cfg for base of TOR.
|
| 199 |
+
* Then use pmp2cfg for TEST_MEM. Both test code and data share PMP entrance.
|
| 200 |
+
* Also use pmp3cfg for fixed U mode (U_MEM).
|
| 201 |
+
*
|
| 202 |
+
* Here @pmp_addr_offset:int@ is to create an address mismatch
|
| 203 |
+
* And @create_pmp_cfg:int@ is to create cfg mismatch.
|
| 204 |
+
*/
|
| 205 |
+
|
| 206 |
+
unsigned int mismatch_offset = 0;
|
| 207 |
+
|
| 208 |
+
if (mismatch_offset != 0x0){
|
| 209 |
+
volatile int pmp_granularity = detect_pmp_granularity();
|
| 210 |
+
mismatch_offset = mismatch_addr_offset(pmp_granularity);
|
| 211 |
+
}
|
| 212 |
+
asm volatile ("csrw pmpaddr7, %0 \n" :: "r"(0x8ffffff8 >> 2) : "memory"); // for ibex signature addr
|
| 213 |
+
asm volatile ("csrw pmpaddr3, %0 \n" :: "r"(U_MEM_END >> 2) : "memory");
|
| 214 |
+
asm volatile ("csrw pmpaddr2, %0 \n" :: "r"(TEST_MEM_END >> 2) : "memory");
|
| 215 |
+
asm volatile ("csrw pmpaddr1, %0 \n" :: "r"((TEST_MEM_START + mismatch_offset) >> 2) : "memory");
|
| 216 |
+
|
| 217 |
+
#if M_MODE_RWX
|
| 218 |
+
asm volatile ("csrw pmpaddr0, %0 \n" :: "r"((0x80000000 >> 2) | 0xfffff) : "memory");
|
| 219 |
+
reg_t cfg0 = (PMP_R | PMP_W | PMP_X | PMP_NAPOT);
|
| 220 |
+
reg_t cfg1 = (PMP_R | PMP_W | PMP_NAPOT) << 24;
|
| 221 |
+
#else
|
| 222 |
+
asm volatile ("csrw pmpaddr6, %0 \n" :: "r"(TEST_MEM_START >> 2) : "memory"); // for data
|
| 223 |
+
asm volatile ("csrw pmpaddr5, %0 \n" :: "r"(0x80010000 >> 2) : "memory"); // for code
|
| 224 |
+
asm volatile ("csrw pmpaddr4, %0 \n" :: "r"(0x80000000 >> 2) : "memory"); // addr start
|
| 225 |
+
reg_t cfg0 = PMP_OFF;
|
| 226 |
+
reg_t cfg1 = PMP_OFF | ((PMP_R | PMP_W | PMP_NAPOT) << 24)
|
| 227 |
+
| ((PMP_R | PMP_W | PMP_TOR) << 16)
|
| 228 |
+
| ((PMP_X | PMP_TOR) << 8);
|
| 229 |
+
#endif
|
| 230 |
+
|
| 231 |
+
// Only true for Spike
|
| 232 |
+
// asm volatile ("csrr %0, pmpcfg0\n":"=r"(cfg0));
|
| 233 |
+
// if (cfg0 != (PMP_R | PMP_W | PMP_X | PMP_NAPOT)) {
|
| 234 |
+
// exit(cfg0);
|
| 235 |
+
// }
|
| 236 |
+
|
| 237 |
+
if (1) { // need to set L bit for M mode code like trap_handling
|
| 238 |
+
#if M_MODE_RWX
|
| 239 |
+
cfg0 |= PMP_L;
|
| 240 |
+
cfg1 |= (PMP_L << 24);
|
| 241 |
+
#else
|
| 242 |
+
cfg1 |= ((PMP_L << 8) | (PMP_L << 16) | (PMP_L << 24));
|
| 243 |
+
#endif
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
cfg0 |= (PMP_R | PMP_W | PMP_X | PMP_TOR) << 24; // for U_MEM
|
| 247 |
+
#if 1
|
| 248 |
+
cfg0 |= ( (1 ? PMP_R : 0)
|
| 249 |
+
| (1 ? PMP_W : 0)
|
| 250 |
+
| (1 ? PMP_X : 0)
|
| 251 |
+
| PMP_TOR | (0 ? PMP_L : 0)) << 16;
|
| 252 |
+
#endif
|
| 253 |
+
|
| 254 |
+
#if __riscv_xlen == 64
|
| 255 |
+
cfg0 |= (cfg1 << 32);
|
| 256 |
+
#else
|
| 257 |
+
asm volatile ("csrw pmpcfg1, %0 \n"
|
| 258 |
+
:
|
| 259 |
+
: "r"(cfg1)
|
| 260 |
+
: "memory");
|
| 261 |
+
#endif // __riscv_xlen == 64
|
| 262 |
+
|
| 263 |
+
asm volatile ("csrw pmpcfg0, %0 \n"
|
| 264 |
+
:
|
| 265 |
+
: "r"(cfg0)
|
| 266 |
+
: "memory");
|
| 267 |
+
|
| 268 |
+
// set proc->state.mseccfg, for MML/MMWP
|
| 269 |
+
const unsigned seccfg_bits = (1 ? MSECCFG_MML : 0) | (1 ? MSECCFG_MMWP : 0);
|
| 270 |
+
if (seccfg_bits) {
|
| 271 |
+
asm volatile ("csrs 0x747, %0 \n"::"r"(seccfg_bits));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// currently dummy since tlb flushed when set_csr on mseccfg
|
| 275 |
+
asm volatile ("fence.i \n");
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// from pmp_ok() side,W/R/X is similar
|
| 279 |
+
__attribute ((noinline))
|
| 280 |
+
static void try_access() {
|
| 281 |
+
#if TEST_RW
|
| 282 |
+
target_arr[0] += 1;
|
| 283 |
+
const unsigned long delta = (unsigned long)0x1020304005060708ULL;
|
| 284 |
+
*(long *)target_arr += delta;
|
| 285 |
+
|
| 286 |
+
if (*(long *)target_arr != (unsigned long)0x0807060504030201ULL + delta + 1) {
|
| 287 |
+
actual_rw_fail = 1;
|
| 288 |
+
}
|
| 289 |
+
#endif
|
| 290 |
+
|
| 291 |
+
#if TEST_FETCH
|
| 292 |
+
actual_x_fail = 1; // reset inside target_foo()
|
| 293 |
+
target_foo();
|
| 294 |
+
#endif
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// in case mml set, printf cannot be used in U mode
|
| 298 |
+
__attribute ((section(".text_umode")))
|
| 299 |
+
void try_access_umode() {
|
| 300 |
+
#if TEST_RW
|
| 301 |
+
target_arr[0] += 1;
|
| 302 |
+
// const unsigned long delta = 0x1020304005060708UL;
|
| 303 |
+
// *(long *)target_arr += delta;
|
| 304 |
+
|
| 305 |
+
// if (*(long *)target_arr != 0x0807060504030201UL + delta + 1) {
|
| 306 |
+
// actual_rw_fail = 1;
|
| 307 |
+
// }
|
| 308 |
+
#endif
|
| 309 |
+
|
| 310 |
+
#if TEST_FETCH
|
| 311 |
+
target_foo_umode();
|
| 312 |
+
#endif
|
| 313 |
+
|
| 314 |
+
/*
|
| 315 |
+
* switch to M mode by invoking a write access fault for special address.
|
| 316 |
+
*/
|
| 317 |
+
volatile unsigned char * p = (unsigned char *)(FAKE_ADDRESS);
|
| 318 |
+
*p = 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
static void checkTestResult() {
|
| 322 |
+
int ret = 0;
|
| 323 |
+
if (expected_rw_fail != actual_rw_fail) {
|
| 324 |
+
ret += 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
if (expected_x_fail != actual_x_fail) {
|
| 328 |
+
ret += 2;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
exit(ret);
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
int main() {
|
| 336 |
+
// assert in M mode
|
| 337 |
+
set_cfg();
|
| 338 |
+
|
| 339 |
+
switch_mode(); // in case swith to u mode, branch to try_access_umode directly
|
| 340 |
+
|
| 341 |
+
try_access();
|
| 342 |
+
|
| 343 |
+
checkTestResult();
|
| 344 |
+
return 0; // assert 0
|
| 345 |
+
}
|
| 346 |
+
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-tests/debug/targets/RISC-V/spike-1.cfg
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
adapter_khz 10000
|
| 2 |
+
|
| 3 |
+
interface remote_bitbang
|
| 4 |
+
remote_bitbang_host $::env(REMOTE_BITBANG_HOST)
|
| 5 |
+
remote_bitbang_port $::env(REMOTE_BITBANG_PORT)
|
| 6 |
+
|
| 7 |
+
set _CHIPNAME riscv
|
| 8 |
+
jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x10e31913
|
| 9 |
+
|
| 10 |
+
set _TARGETNAME $_CHIPNAME.cpu
|
| 11 |
+
if {$::env(USE_FREERTOS)} {
|
| 12 |
+
target create $_TARGETNAME riscv -chain-position $_TARGETNAME -rtos FreeRTOS
|
| 13 |
+
} else {
|
| 14 |
+
target create $_TARGETNAME riscv -chain-position $_TARGETNAME
|
| 15 |
+
}
|
| 16 |
+
$_TARGETNAME configure -work-area-phys $::env(WORK_AREA) -work-area-size 8096 -work-area-backup 1
|
| 17 |
+
|
| 18 |
+
gdb_report_data_abort enable
|
| 19 |
+
gdb_report_register_access_error enable
|
| 20 |
+
|
| 21 |
+
# Expose an unimplemented CSR so we can test non-existent register access
|
| 22 |
+
# behavior.
|
| 23 |
+
riscv expose_csrs 2288
|
| 24 |
+
riscv expose_custom 1,12345-12348
|
| 25 |
+
|
| 26 |
+
init
|
| 27 |
+
|
| 28 |
+
set challenge [riscv authdata_read]
|
| 29 |
+
riscv authdata_write [expr $challenge + 1]
|
| 30 |
+
|
| 31 |
+
halt
|
| 32 |
+
|
| 33 |
+
arm semihosting enable
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-tests/debug/targets/RISC-V/spike64-2-rtos.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spike64 # pylint: disable=import-error
|
| 2 |
+
|
| 3 |
+
import targets
|
| 4 |
+
import testlib
|
| 5 |
+
|
| 6 |
+
class spike64_2_rtos(targets.Target):
|
| 7 |
+
harts = [spike64.spike64_hart(misa=0x8000000000141129),
|
| 8 |
+
spike64.spike64_hart(misa=0x8000000000141129)]
|
| 9 |
+
openocd_config_path = "spike-rtos.cfg"
|
| 10 |
+
timeout_sec = 60
|
| 11 |
+
implements_custom_test = True
|
| 12 |
+
support_hasel = False
|
| 13 |
+
test_semihosting = False
|
| 14 |
+
support_manual_hwbp = False # not supported with `-rtos riscv`
|
| 15 |
+
support_memory_sampling = False # not supported with `-rtos riscv`
|
| 16 |
+
|
| 17 |
+
def create(self):
|
| 18 |
+
return testlib.Spike(self, abstract_rti=30, support_hasel=False,
|
| 19 |
+
support_abstract_csr=False)
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-tests/debug/targets/RISC-V/spike64.lds
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
OUTPUT_ARCH( "riscv" )
|
| 2 |
+
|
| 3 |
+
SECTIONS
|
| 4 |
+
{
|
| 5 |
+
. = 0x1212340000;
|
| 6 |
+
.text :
|
| 7 |
+
{
|
| 8 |
+
*(.text.entry)
|
| 9 |
+
*(.text)
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
/* data segment */
|
| 13 |
+
.data : { *(.data) }
|
| 14 |
+
|
| 15 |
+
.sdata : {
|
| 16 |
+
__global_pointer$ = . + 0x800;
|
| 17 |
+
*(.srodata.cst16) *(.srodata.cst8) *(.srodata.cst4) *(.srodata.cst2)
|
| 18 |
+
*(.srodata*)
|
| 19 |
+
*(.sdata .sdata.* .gnu.linkonce.s.*)
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
/* bss segment */
|
| 23 |
+
__bss_start = .;
|
| 24 |
+
.sbss : {
|
| 25 |
+
*(.sbss .sbss.* .gnu.linkonce.sb.*)
|
| 26 |
+
*(.scommon)
|
| 27 |
+
}
|
| 28 |
+
.bss : { *(.bss) }
|
| 29 |
+
__bss_end = .;
|
| 30 |
+
|
| 31 |
+
__malloc_start = .;
|
| 32 |
+
. = . + 512;
|
| 33 |
+
|
| 34 |
+
/* End of uninitalized data segement */
|
| 35 |
+
_end = .;
|
| 36 |
+
}
|
snapshots/lowRISC_ibex_pr2214/vendor/riscv-tests/debug/targets/RISC-V/spike64.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import targets
|
| 2 |
+
import testlib
|
| 3 |
+
|
| 4 |
+
class spike64_hart(targets.Hart):
|
| 5 |
+
xlen = 64
|
| 6 |
+
ram = 0x1212340000
|
| 7 |
+
ram_size = 0x10000000
|
| 8 |
+
bad_address = ram - 8
|
| 9 |
+
instruction_hardware_breakpoint_count = 4
|
| 10 |
+
reset_vectors = [0x1000]
|
| 11 |
+
link_script_path = "spike64.lds"
|
| 12 |
+
misa = 0x8000000000141125
|
| 13 |
+
|
| 14 |
+
class spike64(targets.Target):
|
| 15 |
+
harts = [spike64_hart()]
|
| 16 |
+
openocd_config_path = "spike-1.cfg"
|
| 17 |
+
timeout_sec = 30
|
| 18 |
+
implements_custom_test = True
|
| 19 |
+
freertos_binary = "bin/RTOSDemo64.axf"
|
| 20 |
+
|
| 21 |
+
def create(self):
|
| 22 |
+
# 32-bit FPRs only
|
| 23 |
+
return testlib.Spike(self, isa="RV64IMAFC", progbufsize=0,
|
| 24 |
+
abstract_rti=30, support_abstract_csr=True,
|
| 25 |
+
support_abstract_fpr=True)
|