| Elliott Hughes | f3d6b44 | 2023-07-27 16:53:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | #if __has_include(<sys/hwprobe.h>) |
| 32 | #include <sys/hwprobe.h> |
| Elliott Hughes | 728aaf6 | 2023-08-14 14:58:33 -0700 | [diff] [blame] | 33 | #include <sys/syscall.h> |
| Elliott Hughes | f3d6b44 | 2023-07-27 16:53:30 -0700 | [diff] [blame] | 34 | #endif |
| 35 | |
| AdityaK | 24705db | 2024-04-19 20:05:33 -0700 | [diff] [blame] | 36 | |
| 37 | #if defined(__riscv) |
| 38 | #include <riscv_vector.h> |
| 39 | |
| 40 | __attribute__((noinline)) |
| 41 | uint64_t scalar_cast(uint8_t const* p) { |
| 42 | return *(uint64_t const*)p; |
| 43 | } |
| 44 | |
| 45 | __attribute__((noinline)) |
| 46 | uint64_t scalar_memcpy(uint8_t const* p) { |
| 47 | uint64_t r; |
| 48 | __builtin_memcpy(&r, p, sizeof(r)); |
| 49 | return r; |
| 50 | } |
| 51 | |
| 52 | __attribute__((noinline)) |
| 53 | uint64_t vector_memcpy(uint8_t* d, uint8_t const* p) { |
| 54 | __builtin_memcpy(d, p, 16); |
| 55 | return *(uint64_t const*)d; |
| 56 | } |
| 57 | |
| 58 | __attribute__((noinline)) |
| 59 | uint64_t vector_ldst(uint8_t* d, uint8_t const* p) { |
| 60 | __riscv_vse8(d, __riscv_vle8_v_u8m1(p, 16), 16); |
| 61 | return *(uint64_t const*)d; |
| 62 | } |
| 63 | |
| 64 | __attribute__((noinline)) |
| 65 | uint64_t vector_ldst64(uint8_t* d, uint8_t const* p) { |
| 66 | __riscv_vse64((unsigned long *)d, __riscv_vle64_v_u64m1((const unsigned long *)p, 16), 16); |
| 67 | return *(uint64_t const*)d; |
| 68 | } |
| 69 | |
| 70 | // For testing scalar and vector unaligned accesses. |
| 71 | uint64_t tmp[3] = {1,1,1}; |
| 72 | uint64_t dst[3] = {1,1,1}; |
| 73 | #endif |
| 74 | |
| 75 | TEST(sys_hwprobe, __riscv_hwprobe_misaligned_scalar) { |
| 76 | #if defined(__riscv) |
| 77 | uint8_t* p = (uint8_t*)tmp + 1; |
| 78 | ASSERT_NE(0U, scalar_cast(p)); |
| 79 | ASSERT_NE(0U, scalar_memcpy(p)); |
| 80 | #else |
| 81 | GTEST_SKIP() << "__riscv_hwprobe requires riscv64"; |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | TEST(sys_hwprobe, __riscv_hwprobe_misaligned_vector) { |
| 86 | #if defined(__riscv) |
| 87 | uint8_t* p = (uint8_t*)tmp + 1; |
| 88 | uint8_t* d = (uint8_t*)dst + 1; |
| 89 | |
| 90 | ASSERT_NE(0U, vector_ldst(d, p)); |
| 91 | ASSERT_NE(0U, vector_memcpy(d, p)); |
| 92 | ASSERT_NE(0U, vector_ldst64(d, p)); |
| 93 | #else |
| 94 | GTEST_SKIP() << "__riscv_hwprobe requires riscv64"; |
| 95 | #endif |
| 96 | } |
| 97 | |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 98 | #define key_count(probes) (sizeof(probes)/sizeof(probes[0])) |
| 99 | |
| 100 | TEST(sys_hwprobe, __riscv_hwprobe_extensions) { |
| Elliott Hughes | 03f1f44 | 2025-04-04 10:26:47 -0700 | [diff] [blame] | 101 | #if defined(__riscv) |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 102 | riscv_hwprobe probes[] = {{.key = RISCV_HWPROBE_KEY_IMA_EXT_0}}; |
| 103 | ASSERT_EQ(0, __riscv_hwprobe(probes, key_count(probes), 0, nullptr, 0)); |
| 104 | |
| Elliott Hughes | f3d6b44 | 2023-07-27 16:53:30 -0700 | [diff] [blame] | 105 | EXPECT_EQ(RISCV_HWPROBE_KEY_IMA_EXT_0, probes[0].key); |
| 106 | EXPECT_TRUE((probes[0].value & RISCV_HWPROBE_IMA_FD) != 0); |
| 107 | EXPECT_TRUE((probes[0].value & RISCV_HWPROBE_IMA_C) != 0); |
| Elliott Hughes | f3d6b44 | 2023-07-27 16:53:30 -0700 | [diff] [blame] | 108 | EXPECT_TRUE((probes[0].value & RISCV_HWPROBE_IMA_V) != 0); |
| Elliott Hughes | f3d6b44 | 2023-07-27 16:53:30 -0700 | [diff] [blame] | 109 | EXPECT_TRUE((probes[0].value & RISCV_HWPROBE_EXT_ZBA) != 0); |
| Elliott Hughes | f3d6b44 | 2023-07-27 16:53:30 -0700 | [diff] [blame] | 110 | EXPECT_TRUE((probes[0].value & RISCV_HWPROBE_EXT_ZBB) != 0); |
| Elliott Hughes | f3d6b44 | 2023-07-27 16:53:30 -0700 | [diff] [blame] | 111 | EXPECT_TRUE((probes[0].value & RISCV_HWPROBE_EXT_ZBS) != 0); |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 112 | #else |
| 113 | GTEST_SKIP() << "__riscv_hwprobe requires riscv64"; |
| 114 | #endif |
| 115 | } |
| Elliott Hughes | f3d6b44 | 2023-07-27 16:53:30 -0700 | [diff] [blame] | 116 | |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 117 | TEST(sys_hwprobe, __riscv_hwprobe_cpu_perf) { |
| Elliott Hughes | 03f1f44 | 2025-04-04 10:26:47 -0700 | [diff] [blame] | 118 | #if defined(__riscv) |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 119 | riscv_hwprobe probes[] = {{.key = RISCV_HWPROBE_KEY_CPUPERF_0}}; |
| 120 | ASSERT_EQ(0, __riscv_hwprobe(probes, key_count(probes), 0, nullptr, 0)); |
| 121 | |
| 122 | EXPECT_EQ(RISCV_HWPROBE_KEY_CPUPERF_0, probes[0].key); |
| 123 | EXPECT_EQ(RISCV_HWPROBE_MISALIGNED_FAST, |
| 124 | static_cast<int>(probes[0].value & RISCV_HWPROBE_MISALIGNED_MASK)); |
| 125 | #else |
| 126 | GTEST_SKIP() << "__riscv_hwprobe requires riscv64"; |
| 127 | #endif |
| 128 | } |
| 129 | |
| 130 | TEST(sys_hwprobe, __riscv_hwprobe_scalar_perf) { |
| Elliott Hughes | 03f1f44 | 2025-04-04 10:26:47 -0700 | [diff] [blame] | 131 | #if defined(__riscv) |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 132 | riscv_hwprobe probes[] = {{.key = RISCV_HWPROBE_KEY_MISALIGNED_SCALAR_PERF}}; |
| 133 | ASSERT_EQ(0, __riscv_hwprobe(probes, key_count(probes), 0, nullptr, 0)); |
| 134 | |
| 135 | EXPECT_EQ(RISCV_HWPROBE_KEY_MISALIGNED_SCALAR_PERF, probes[0].key); |
| 136 | EXPECT_EQ(RISCV_HWPROBE_MISALIGNED_SCALAR_FAST, static_cast<int>(probes[0].value)); |
| 137 | #else |
| 138 | GTEST_SKIP() << "__riscv_hwprobe requires riscv64"; |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | TEST(sys_hwprobe, __riscv_hwprobe_vector_perf) { |
| Elliott Hughes | 03f1f44 | 2025-04-04 10:26:47 -0700 | [diff] [blame] | 143 | #if defined(__riscv) |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 144 | riscv_hwprobe probes[] = {{.key = RISCV_HWPROBE_KEY_MISALIGNED_SCALAR_PERF}}; |
| 145 | ASSERT_EQ(0, __riscv_hwprobe(probes, key_count(probes), 0, nullptr, 0)); |
| 146 | |
| 147 | EXPECT_EQ(RISCV_HWPROBE_KEY_MISALIGNED_VECTOR_PERF, probes[0].key); |
| 148 | EXPECT_EQ(RISCV_HWPROBE_MISALIGNED_VECTOR_FAST, static_cast<int>(probes[0].value)); |
| Elliott Hughes | f3d6b44 | 2023-07-27 16:53:30 -0700 | [diff] [blame] | 149 | #else |
| 150 | GTEST_SKIP() << "__riscv_hwprobe requires riscv64"; |
| 151 | #endif |
| 152 | } |
| Elliott Hughes | 728aaf6 | 2023-08-14 14:58:33 -0700 | [diff] [blame] | 153 | |
| 154 | TEST(sys_hwprobe, __riscv_hwprobe_syscall_vdso) { |
| Elliott Hughes | 03f1f44 | 2025-04-04 10:26:47 -0700 | [diff] [blame] | 155 | #if defined(__riscv) |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 156 | riscv_hwprobe probes_vdso[] = { |
| 157 | {.key = RISCV_HWPROBE_KEY_MVENDORID}, |
| 158 | {.key = RISCV_HWPROBE_KEY_MARCHID}, |
| 159 | {.key = RISCV_HWPROBE_KEY_MIMPID}, |
| 160 | {.key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR}, |
| 161 | {.key = RISCV_HWPROBE_KEY_IMA_EXT_0}, |
| 162 | {.key = RISCV_HWPROBE_KEY_CPUPERF_0}, |
| 163 | {.key = RISCV_HWPROBE_KEY_MISALIGNED_SCALAR_PERF}, |
| 164 | {.key = RISCV_HWPROBE_KEY_MISALIGNED_VECTOR_PERF}, |
| 165 | }; |
| 166 | ASSERT_EQ(0, __riscv_hwprobe(probes_vdso, key_count(probes_vdso), 0, nullptr, 0)); |
| Elliott Hughes | 728aaf6 | 2023-08-14 14:58:33 -0700 | [diff] [blame] | 167 | |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 168 | riscv_hwprobe probes_syscall[] = { |
| 169 | {.key = RISCV_HWPROBE_KEY_MVENDORID}, |
| 170 | {.key = RISCV_HWPROBE_KEY_MARCHID}, |
| 171 | {.key = RISCV_HWPROBE_KEY_MIMPID}, |
| 172 | {.key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR}, |
| 173 | {.key = RISCV_HWPROBE_KEY_IMA_EXT_0}, |
| 174 | {.key = RISCV_HWPROBE_KEY_CPUPERF_0}, |
| 175 | {.key = RISCV_HWPROBE_KEY_MISALIGNED_SCALAR_PERF}, |
| 176 | {.key = RISCV_HWPROBE_KEY_MISALIGNED_VECTOR_PERF}, |
| 177 | }; |
| 178 | ASSERT_EQ(0, syscall(SYS_riscv_hwprobe, key_count(probes_syscall), 0, nullptr, 0)); |
| Elliott Hughes | 728aaf6 | 2023-08-14 14:58:33 -0700 | [diff] [blame] | 179 | |
| 180 | // Check we got the same answers from the vdso and the syscall. |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 181 | for (size_t i = 0; i < key_count(probes_vdso); ++i) { |
| 182 | EXPECT_EQ(probes_vdso[i].key, probes_syscall[i].key) << i; |
| 183 | EXPECT_EQ(probes_vdso[i].value, probes_syscall[i].value) << i; |
| 184 | } |
| Elliott Hughes | 728aaf6 | 2023-08-14 14:58:33 -0700 | [diff] [blame] | 185 | #else |
| 186 | GTEST_SKIP() << "__riscv_hwprobe requires riscv64"; |
| 187 | #endif |
| 188 | } |
| Elliott Hughes | fce8a15 | 2023-08-29 15:38:33 -0700 | [diff] [blame] | 189 | |
| 190 | TEST(sys_hwprobe, __riscv_hwprobe_fail) { |
| Elliott Hughes | 03f1f44 | 2025-04-04 10:26:47 -0700 | [diff] [blame] | 191 | #if defined(__riscv) |
| Elliott Hughes | fce8a15 | 2023-08-29 15:38:33 -0700 | [diff] [blame] | 192 | riscv_hwprobe probes[] = {}; |
| 193 | ASSERT_EQ(EINVAL, __riscv_hwprobe(probes, 0, 0, nullptr, ~0)); |
| 194 | #else |
| 195 | GTEST_SKIP() << "__riscv_hwprobe requires riscv64"; |
| 196 | #endif |
| Elliott Hughes | 6334e8b | 2025-04-07 08:33:09 -0700 | [diff] [blame] | 197 | } |