On arm64, check whether the CPU has the BT feature to determine the expected
authorkettenis <kettenis@openbsd.org>
Tue, 30 Jul 2024 09:07:00 +0000 (09:07 +0000)
committerkettenis <kettenis@openbsd.org>
Tue, 30 Jul 2024 09:07:00 +0000 (09:07 +0000)
outcome of the test.

regress/sys/btcfi/foobar.c

index 8ed60c1..5efbfec 100644 (file)
@@ -22,14 +22,43 @@ handler(int sig, siginfo_t *si, void *context)
 }
 
 #if defined(__amd64__)
+
 static int
-has_cet_ibt(void)
+has_btcfi(void)
 {
        uint32_t d;
 
        asm("cpuid" : "=d" (d) : "a" (7), "c" (0));
        return (d & (1U << 20)) ? 1 : 0;
 }
+
+#elif defined(__aarch64__)
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+
+#include <machine/armreg.h>
+#include <machine/cpu.h>
+
+static int
+has_btcfi(void)
+{
+       int mib[] = { CTL_MACHDEP, CPU_ID_AA64PFR1 };
+       uint64_t id_aa64pfr1 = 0;
+       size_t size = sizeof(id_aa64pfr1);
+
+       sysctl(mib, 2, &id_aa64pfr1, &size, NULL, 0);
+       return ID_AA64PFR1_BT(id_aa64pfr1) >= ID_AA64PFR1_BT_IMPL;
+}
+
+#else
+
+static int
+has_btcfi(void)
+{
+       return 0;
+}
+
 #endif
 
 int
@@ -37,13 +66,11 @@ main(void)
 {
        struct sigaction sa;
 
-#if defined(__amd64__)
-       if (!has_cet_ibt()) {
+       if (!has_btcfi()) {
                printf("Unsupported CPU\n");
                printf("SKIPPED\n");
                exit(0);
        }
-#endif
 
        sa.sa_sigaction = handler;
        sa.sa_mask = 0;