Commit 2f248e0f authored by Andrew Jones's avatar Andrew Jones Committed by Palmer Dabbelt

RISC-V: selftests: Convert hwprobe test to kselftest API

Returning (exiting with) negative exit codes isn't user friendly,
because the user must output the exit code with the shell, convert it
from its unsigned 8-bit value back to the negative value, and then
look up where that comes from in the code (which may be multiple
places). Use the kselftests TAP interface, instead.
Signed-off-by: default avatarAndrew Jones <ajones@ventanamicro.com>
Link: https://lore.kernel.org/r/20230918131518.56803-13-ajones@ventanamicro.comSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
parent fc9fdf2c
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#include <stddef.h> #include <stddef.h>
#include <asm/hwprobe.h> #include <asm/hwprobe.h>
#include "../../kselftest.h"
/* /*
* Rather than relying on having a new enough libc to define this, just do it * Rather than relying on having a new enough libc to define this, just do it
* ourselves. This way we don't need to be coupled to a new-enough libc to * ourselves. This way we don't need to be coupled to a new-enough libc to
...@@ -16,6 +18,9 @@ int main(int argc, char **argv) ...@@ -16,6 +18,9 @@ int main(int argc, char **argv)
unsigned long cpus; unsigned long cpus;
long out; long out;
ksft_print_header();
ksft_set_plan(5);
/* Fake the CPU_SET ops. */ /* Fake the CPU_SET ops. */
cpus = -1; cpus = -1;
...@@ -25,13 +30,16 @@ int main(int argc, char **argv) ...@@ -25,13 +30,16 @@ int main(int argc, char **argv)
*/ */
for (long i = 0; i < 8; i++) for (long i = 0; i < 8; i++)
pairs[i].key = i; pairs[i].key = i;
out = riscv_hwprobe(pairs, 8, 1, &cpus, 0); out = riscv_hwprobe(pairs, 8, 1, &cpus, 0);
if (out != 0) if (out != 0)
return -1; ksft_exit_fail_msg("hwprobe() failed with %ld\n", out);
for (long i = 0; i < 4; ++i) { for (long i = 0; i < 4; ++i) {
/* Fail if the kernel claims not to recognize a base key. */ /* Fail if the kernel claims not to recognize a base key. */
if ((i < 4) && (pairs[i].key != i)) if ((i < 4) && (pairs[i].key != i))
return -2; ksft_exit_fail_msg("Failed to recognize base key: key != i, "
"key=%ld, i=%ld\n", pairs[i].key, i);
if (pairs[i].key != RISCV_HWPROBE_KEY_BASE_BEHAVIOR) if (pairs[i].key != RISCV_HWPROBE_KEY_BASE_BEHAVIOR)
continue; continue;
...@@ -39,52 +47,30 @@ int main(int argc, char **argv) ...@@ -39,52 +47,30 @@ int main(int argc, char **argv)
if (pairs[i].value & RISCV_HWPROBE_BASE_BEHAVIOR_IMA) if (pairs[i].value & RISCV_HWPROBE_BASE_BEHAVIOR_IMA)
continue; continue;
return -3; ksft_exit_fail_msg("Unexpected pair: (%ld, %ld)\n", pairs[i].key, pairs[i].value);
} }
/*
* This should also work with a NULL CPU set, but should not work
* with an improperly supplied CPU set.
*/
out = riscv_hwprobe(pairs, 8, 0, 0, 0); out = riscv_hwprobe(pairs, 8, 0, 0, 0);
if (out != 0) ksft_test_result(out == 0, "NULL CPU set\n");
return -4;
out = riscv_hwprobe(pairs, 8, 0, &cpus, 0); out = riscv_hwprobe(pairs, 8, 0, &cpus, 0);
if (out == 0) ksft_test_result(out != 0, "Bad CPU set\n");
return -5;
out = riscv_hwprobe(pairs, 8, 1, 0, 0); out = riscv_hwprobe(pairs, 8, 1, 0, 0);
if (out == 0) ksft_test_result(out != 0, "NULL CPU set with non-zero count\n");
return -6;
/*
* Check that keys work by providing one that we know exists, and
* checking to make sure the resultig pair is what we asked for.
*/
pairs[0].key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR; pairs[0].key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR;
out = riscv_hwprobe(pairs, 1, 1, &cpus, 0); out = riscv_hwprobe(pairs, 1, 1, &cpus, 0);
if (out != 0) ksft_test_result(out == 0 && pairs[0].key == RISCV_HWPROBE_KEY_BASE_BEHAVIOR,
return -7; "Existing key is maintained\n");
if (pairs[0].key != RISCV_HWPROBE_KEY_BASE_BEHAVIOR)
return -8;
/*
* Check that an unknown key gets overwritten with -1,
* but doesn't block elements after it.
*/
pairs[0].key = 0x5555; pairs[0].key = 0x5555;
pairs[1].key = 1; pairs[1].key = 1;
pairs[1].value = 0xAAAA; pairs[1].value = 0xAAAA;
out = riscv_hwprobe(pairs, 2, 0, 0, 0); out = riscv_hwprobe(pairs, 2, 0, 0, 0);
if (out != 0) ksft_test_result(out == 0 && pairs[0].key == -1 &&
return -9; pairs[1].key == 1 && pairs[1].value != 0xAAAA,
"Unknown key overwritten with -1 and doesn't block other elements\n");
if (pairs[0].key != -1)
return -10;
if ((pairs[1].key != 1) || (pairs[1].value == 0xAAAA))
return -11;
return 0; ksft_finished();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment