Skip to content

Commit 36dec9e

Browse files
cuiyunhuiAlexandre Ghiti
authored andcommitted
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 <[email protected]> Reviewed-by: Samuel Holland <[email protected]> Signed-off-by: Yunhui Cui <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexandre Ghiti <[email protected]>
1 parent eb10039 commit 36dec9e

File tree

1 file changed

+55
-11
lines changed
  • tools/testing/selftests/riscv/hwprobe

1 file changed

+55
-11
lines changed

tools/testing/selftests/riscv/hwprobe/cbo.c

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ static void cbo_clean(char *base) { cbo_insn(base, 1); }
5050
static void cbo_flush(char *base) { cbo_insn(base, 2); }
5151
static void cbo_zero(char *base) { cbo_insn(base, 4); }
5252

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+
5361
static void test_no_zicbom(void *arg)
5462
{
5563
ksft_print_msg("Testing Zicbom instructions remain privileged\n");
@@ -61,10 +69,6 @@ static void test_no_zicbom(void *arg)
6169
illegal_insn = false;
6270
cbo_flush(&mem[0]);
6371
ksft_test_result(illegal_insn, "No cbo.flush\n");
64-
65-
illegal_insn = false;
66-
cbo_inval(&mem[0]);
67-
ksft_test_result(illegal_insn, "No cbo.inval\n");
6872
}
6973

7074
static void test_no_zicboz(void *arg)
@@ -81,6 +85,30 @@ static bool is_power_of_2(__u64 n)
8185
return n != 0 && (n & (n - 1)) == 0;
8286
}
8387

88+
static void test_zicbom(void *arg)
89+
{
90+
struct riscv_hwprobe pair = {
91+
.key = RISCV_HWPROBE_KEY_ZICBOM_BLOCK_SIZE,
92+
};
93+
cpu_set_t *cpus = (cpu_set_t *)arg;
94+
__u64 block_size;
95+
long rc;
96+
97+
rc = riscv_hwprobe(&pair, 1, sizeof(cpu_set_t), (unsigned long *)cpus, 0);
98+
block_size = pair.value;
99+
ksft_test_result(rc == 0 && pair.key == RISCV_HWPROBE_KEY_ZICBOM_BLOCK_SIZE &&
100+
is_power_of_2(block_size), "Zicbom block size\n");
101+
ksft_print_msg("Zicbom block size: %llu\n", block_size);
102+
103+
illegal_insn = false;
104+
cbo_clean(&mem[block_size]);
105+
ksft_test_result(!illegal_insn, "cbo.clean\n");
106+
107+
illegal_insn = false;
108+
cbo_flush(&mem[block_size]);
109+
ksft_test_result(!illegal_insn, "cbo.flush\n");
110+
}
111+
84112
static void test_zicboz(void *arg)
85113
{
86114
struct riscv_hwprobe pair = {
@@ -129,14 +157,15 @@ static void test_zicboz(void *arg)
129157
ksft_test_result_pass("cbo.zero check\n");
130158
}
131159

132-
static void check_no_zicboz_cpus(cpu_set_t *cpus)
160+
static void check_no_zicbo_cpus(cpu_set_t *cpus, __u64 cbo)
133161
{
134162
struct riscv_hwprobe pair = {
135163
.key = RISCV_HWPROBE_KEY_IMA_EXT_0,
136164
};
137165
cpu_set_t one_cpu;
138166
int i = 0, c = 0;
139167
long rc;
168+
char *cbostr;
140169

141170
while (i++ < CPU_COUNT(cpus)) {
142171
while (!CPU_ISSET(c, cpus))
@@ -148,18 +177,23 @@ static void check_no_zicboz_cpus(cpu_set_t *cpus)
148177
rc = riscv_hwprobe(&pair, 1, sizeof(cpu_set_t), (unsigned long *)&one_cpu, 0);
149178
assert(rc == 0 && pair.key == RISCV_HWPROBE_KEY_IMA_EXT_0);
150179

151-
if (pair.value & RISCV_HWPROBE_EXT_ZICBOZ)
152-
ksft_exit_fail_msg("Zicboz is only present on a subset of harts.\n"
153-
"Use taskset to select a set of harts where Zicboz\n"
154-
"presence (present or not) is consistent for each hart\n");
180+
cbostr = cbo == RISCV_HWPROBE_EXT_ZICBOZ ? "Zicboz" : "Zicbom";
181+
182+
if (pair.value & cbo)
183+
ksft_exit_fail_msg("%s is only present on a subset of harts.\n"
184+
"Use taskset to select a set of harts where %s\n"
185+
"presence (present or not) is consistent for each hart\n",
186+
cbostr, cbostr);
155187
++c;
156188
}
157189
}
158190

159191
enum {
160192
TEST_ZICBOZ,
161193
TEST_NO_ZICBOZ,
194+
TEST_ZICBOM,
162195
TEST_NO_ZICBOM,
196+
TEST_NO_CBO_INVAL,
163197
};
164198

165199
static struct test_info {
@@ -169,7 +203,9 @@ static struct test_info {
169203
} tests[] = {
170204
[TEST_ZICBOZ] = { .nr_tests = 3, test_zicboz },
171205
[TEST_NO_ZICBOZ] = { .nr_tests = 1, test_no_zicboz },
172-
[TEST_NO_ZICBOM] = { .nr_tests = 3, test_no_zicbom },
206+
[TEST_ZICBOM] = { .nr_tests = 3, test_zicbom },
207+
[TEST_NO_ZICBOM] = { .nr_tests = 2, test_no_zicbom },
208+
[TEST_NO_CBO_INVAL] = { .nr_tests = 1, test_no_cbo_inval },
173209
};
174210

175211
int main(int argc, char **argv)
@@ -189,6 +225,7 @@ int main(int argc, char **argv)
189225
assert(rc == 0);
190226
tests[TEST_NO_ZICBOZ].enabled = true;
191227
tests[TEST_NO_ZICBOM].enabled = true;
228+
tests[TEST_NO_CBO_INVAL].enabled = true;
192229
}
193230

194231
rc = sched_getaffinity(0, sizeof(cpu_set_t), &cpus);
@@ -206,7 +243,14 @@ int main(int argc, char **argv)
206243
tests[TEST_ZICBOZ].enabled = true;
207244
tests[TEST_NO_ZICBOZ].enabled = false;
208245
} else {
209-
check_no_zicboz_cpus(&cpus);
246+
check_no_zicbo_cpus(&cpus, RISCV_HWPROBE_EXT_ZICBOZ);
247+
}
248+
249+
if (pair.value & RISCV_HWPROBE_EXT_ZICBOM) {
250+
tests[TEST_ZICBOM].enabled = true;
251+
tests[TEST_NO_ZICBOM].enabled = false;
252+
} else {
253+
check_no_zicbo_cpus(&cpus, RISCV_HWPROBE_EXT_ZICBOM);
210254
}
211255

212256
for (i = 0; i < ARRAY_SIZE(tests); ++i)

0 commit comments

Comments
 (0)