Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

RISC-V: selftests: Add TEST_ZICBOM into CBO tests

Add test for Zicbom and its block size into CBO tests, when
Zicbom is present, test that cbo.clean/flush may be issued and works.
As the software can't verify the clean/flush functions, we just judged
that cbo.clean/flush isn't executed illegally.

Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
Link: https://lore.kernel.org/r/20250226063206.71216-4-cuiyunhui@bytedance.com
Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>

authored by

Yunhui Cui and committed by
Alexandre Ghiti
36dec9e4 eb100397

+55 -11
+55 -11
tools/testing/selftests/riscv/hwprobe/cbo.c
··· 50 50 static void cbo_flush(char *base) { cbo_insn(base, 2); } 51 51 static void cbo_zero(char *base) { cbo_insn(base, 4); } 52 52 53 + static void test_no_cbo_inval(void *arg) 54 + { 55 + ksft_print_msg("Testing cbo.inval instruction remain privileged\n"); 56 + illegal_insn = false; 57 + cbo_inval(&mem[0]); 58 + ksft_test_result(illegal_insn, "No cbo.inval\n"); 59 + } 60 + 53 61 static void test_no_zicbom(void *arg) 54 62 { 55 63 ksft_print_msg("Testing Zicbom instructions remain privileged\n"); ··· 69 61 illegal_insn = false; 70 62 cbo_flush(&mem[0]); 71 63 ksft_test_result(illegal_insn, "No cbo.flush\n"); 72 - 73 - illegal_insn = false; 74 - cbo_inval(&mem[0]); 75 - ksft_test_result(illegal_insn, "No cbo.inval\n"); 76 64 } 77 65 78 66 static void test_no_zicboz(void *arg) ··· 83 79 static bool is_power_of_2(__u64 n) 84 80 { 85 81 return n != 0 && (n & (n - 1)) == 0; 82 + } 83 + 84 + static void test_zicbom(void *arg) 85 + { 86 + struct riscv_hwprobe pair = { 87 + .key = RISCV_HWPROBE_KEY_ZICBOM_BLOCK_SIZE, 88 + }; 89 + cpu_set_t *cpus = (cpu_set_t *)arg; 90 + __u64 block_size; 91 + long rc; 92 + 93 + rc = riscv_hwprobe(&pair, 1, sizeof(cpu_set_t), (unsigned long *)cpus, 0); 94 + block_size = pair.value; 95 + ksft_test_result(rc == 0 && pair.key == RISCV_HWPROBE_KEY_ZICBOM_BLOCK_SIZE && 96 + is_power_of_2(block_size), "Zicbom block size\n"); 97 + ksft_print_msg("Zicbom block size: %llu\n", block_size); 98 + 99 + illegal_insn = false; 100 + cbo_clean(&mem[block_size]); 101 + ksft_test_result(!illegal_insn, "cbo.clean\n"); 102 + 103 + illegal_insn = false; 104 + cbo_flush(&mem[block_size]); 105 + ksft_test_result(!illegal_insn, "cbo.flush\n"); 86 106 } 87 107 88 108 static void test_zicboz(void *arg) ··· 157 129 ksft_test_result_pass("cbo.zero check\n"); 158 130 } 159 131 160 - static void check_no_zicboz_cpus(cpu_set_t *cpus) 132 + static void check_no_zicbo_cpus(cpu_set_t *cpus, __u64 cbo) 161 133 { 162 134 struct riscv_hwprobe pair = { 163 135 .key = RISCV_HWPROBE_KEY_IMA_EXT_0, ··· 165 137 cpu_set_t one_cpu; 166 138 int i = 0, c = 0; 167 139 long rc; 140 + char *cbostr; 168 141 169 142 while (i++ < CPU_COUNT(cpus)) { 170 143 while (!CPU_ISSET(c, cpus)) ··· 177 148 rc = riscv_hwprobe(&pair, 1, sizeof(cpu_set_t), (unsigned long *)&one_cpu, 0); 178 149 assert(rc == 0 && pair.key == RISCV_HWPROBE_KEY_IMA_EXT_0); 179 150 180 - if (pair.value & RISCV_HWPROBE_EXT_ZICBOZ) 181 - ksft_exit_fail_msg("Zicboz is only present on a subset of harts.\n" 182 - "Use taskset to select a set of harts where Zicboz\n" 183 - "presence (present or not) is consistent for each hart\n"); 151 + cbostr = cbo == RISCV_HWPROBE_EXT_ZICBOZ ? "Zicboz" : "Zicbom"; 152 + 153 + if (pair.value & cbo) 154 + ksft_exit_fail_msg("%s is only present on a subset of harts.\n" 155 + "Use taskset to select a set of harts where %s\n" 156 + "presence (present or not) is consistent for each hart\n", 157 + cbostr, cbostr); 184 158 ++c; 185 159 } 186 160 } ··· 191 159 enum { 192 160 TEST_ZICBOZ, 193 161 TEST_NO_ZICBOZ, 162 + TEST_ZICBOM, 194 163 TEST_NO_ZICBOM, 164 + TEST_NO_CBO_INVAL, 195 165 }; 196 166 197 167 static struct test_info { ··· 203 169 } tests[] = { 204 170 [TEST_ZICBOZ] = { .nr_tests = 3, test_zicboz }, 205 171 [TEST_NO_ZICBOZ] = { .nr_tests = 1, test_no_zicboz }, 206 - [TEST_NO_ZICBOM] = { .nr_tests = 3, test_no_zicbom }, 172 + [TEST_ZICBOM] = { .nr_tests = 3, test_zicbom }, 173 + [TEST_NO_ZICBOM] = { .nr_tests = 2, test_no_zicbom }, 174 + [TEST_NO_CBO_INVAL] = { .nr_tests = 1, test_no_cbo_inval }, 207 175 }; 208 176 209 177 int main(int argc, char **argv) ··· 225 189 assert(rc == 0); 226 190 tests[TEST_NO_ZICBOZ].enabled = true; 227 191 tests[TEST_NO_ZICBOM].enabled = true; 192 + tests[TEST_NO_CBO_INVAL].enabled = true; 228 193 } 229 194 230 195 rc = sched_getaffinity(0, sizeof(cpu_set_t), &cpus); ··· 243 206 tests[TEST_ZICBOZ].enabled = true; 244 207 tests[TEST_NO_ZICBOZ].enabled = false; 245 208 } else { 246 - check_no_zicboz_cpus(&cpus); 209 + check_no_zicbo_cpus(&cpus, RISCV_HWPROBE_EXT_ZICBOZ); 210 + } 211 + 212 + if (pair.value & RISCV_HWPROBE_EXT_ZICBOM) { 213 + tests[TEST_ZICBOM].enabled = true; 214 + tests[TEST_NO_ZICBOM].enabled = false; 215 + } else { 216 + check_no_zicbo_cpus(&cpus, RISCV_HWPROBE_EXT_ZICBOM); 247 217 } 248 218 249 219 for (i = 0; i < ARRAY_SIZE(tests); ++i)