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

Source file repositories/reference/linux-study-clean/tools/testing/selftests/riscv/hwprobe/cbo.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/riscv/hwprobe/cbo.c
Extension
.c
Size
9550 bytes
Lines
378
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

static void cbo_inval(char *base) { cbo_insn(base, 0); }
static void cbo_clean(char *base) { cbo_insn(base, 1); }
static void cbo_flush(char *base) { cbo_insn(base, 2); }
static void cbo_zero(char *base)  { cbo_insn(base, 4); }
static void prefetch_i(char *base) { prefetch_insn(base, 0); }
static void prefetch_r(char *base) { prefetch_insn(base, 1); }
static void prefetch_w(char *base) { prefetch_insn(base, 3); }

static void test_no_cbo_inval(void *arg)
{
	ksft_print_msg("Testing cbo.inval instruction remain privileged\n");
	got_fault = false;
	cbo_inval(&mem[0]);
	ksft_test_result(got_fault, "No cbo.inval\n");
}

static void test_no_zicbom(void *arg)
{
	ksft_print_msg("Testing Zicbom instructions remain privileged\n");

	got_fault = false;
	cbo_clean(&mem[0]);
	ksft_test_result(got_fault, "No cbo.clean\n");

	got_fault = false;
	cbo_flush(&mem[0]);
	ksft_test_result(got_fault, "No cbo.flush\n");
}

static void test_no_zicboz(void *arg)
{
	ksft_print_msg("No Zicboz, testing cbo.zero remains privileged\n");

	got_fault = false;
	cbo_zero(&mem[0]);
	ksft_test_result(got_fault, "No cbo.zero\n");
}

static bool is_power_of_2(__u64 n)
{
	return n != 0 && (n & (n - 1)) == 0;
}

static void test_zicbop(void *arg)
{
	struct riscv_hwprobe pair = {
		.key = RISCV_HWPROBE_KEY_ZICBOP_BLOCK_SIZE,
	};
	struct sigaction act = {
		.sa_sigaction = &fault_handler,
		.sa_flags = SA_SIGINFO
	};
	struct sigaction dfl = {
		.sa_handler = SIG_DFL
	};
	cpu_set_t *cpus = (cpu_set_t *)arg;
	__u64 block_size;
	long rc;

	rc = sigaction(SIGSEGV, &act, NULL);
	assert(rc == 0);
	rc = sigaction(SIGBUS, &act, NULL);
	assert(rc == 0);

	rc = riscv_hwprobe(&pair, 1, sizeof(cpu_set_t), (unsigned long *)cpus, 0);
	block_size = pair.value;
	ksft_test_result(rc == 0 && pair.key == RISCV_HWPROBE_KEY_ZICBOP_BLOCK_SIZE &&
			 is_power_of_2(block_size), "Zicbop block size\n");
	ksft_print_msg("Zicbop block size: %llu\n", block_size);

	got_fault = false;
	prefetch_i(&mem[0]);
	prefetch_r(&mem[0]);
	prefetch_w(&mem[0]);
	ksft_test_result(!got_fault, "Zicbop prefetch.* on valid address\n");

	got_fault = false;
	prefetch_i(NULL);
	prefetch_r(NULL);
	prefetch_w(NULL);
	ksft_test_result(!got_fault, "Zicbop prefetch.* on NULL\n");

	rc = sigaction(SIGBUS, &dfl, NULL);
	assert(rc == 0);
	rc = sigaction(SIGSEGV, &dfl, NULL);
	assert(rc == 0);
}

static void test_zicbom(void *arg)
{

Annotation

Implementation Notes