FFmpeg
Loading...
Searching...
No Matches
cpu.c
Go to the documentation of this file.
1/*
2 * Copyright © 2022-2025 Rémi Denis-Courmont.
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 are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "checkasm_config.h"
29
30#if ARCH_RISCV
31
32 #ifndef _GNU_SOURCE
33 #define _GNU_SOURCE
34 #endif
35
36 #include "cpu.h"
37 #include "internal.h"
38
39 #include <inttypes.h>
40 #include <limits.h>
41 #include <stdatomic.h>
42 #include <stdbool.h>
43 #if HAVE_SYS_HWPROBE_H
44 #include <sys/hwprobe.h>
45 #elif HAVE_ASM_HWPROBE_H
46 #include <asm/hwprobe.h>
47 #include <asm/unistd.h>
48 #include <sys/syscall.h>
49 #include <unistd.h>
50
51static int __riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
52 size_t cpu_count, unsigned long *cpus,
53 unsigned int flags)
54{
55 return syscall(__NR_riscv_hwprobe, pairs, pair_count, cpu_count, cpus,
56 flags);
57}
58 #endif
59 #if HAVE_GETAUXVAL || HAVE_ELF_AUX_INFO
60 #include <sys/auxv.h>
61 #define HWCAP_RV(letter) (1ul << ((letter) - 'A'))
62 #endif
63
64int checkasm_get_cpuids(uint32_t *vendor, uintptr_t *arch, uintptr_t *imp)
65{
66#if HAVE_SYS_HWPROBE_H || HAVE_ASM_HWPROBE_H
67 struct riscv_hwprobe pairs[] = {
68 { RISCV_HWPROBE_KEY_MVENDORID, 0 },
69 { RISCV_HWPROBE_KEY_MARCHID, 0 },
70 { RISCV_HWPROBE_KEY_MIMPID, 0 },
71 };
72
73 if (__riscv_hwprobe(pairs, ARRAY_SIZE(pairs), 0, NULL, 0) == 0) {
74 *vendor = (uint32_t)pairs[0].value;
75 *arch = (uintptr_t)pairs[1].value;
76 *imp = (uintptr_t)pairs[2].value;
77 return 0;
78 }
79#endif
80 return -1;
81}
82
83const char *checkasm_get_riscv_vendor_name(uint32_t vendorid)
84{
85 if (vendorid > 0) {
86 unsigned bank = vendorid >> 7, offset = vendorid & 0x7f;
87
89 } else {
90 return "Unspecified";
91 }
92}
93
94const char *checkasm_get_riscv_arch_name(char *buf, size_t buflen,
95 uint32_t vendor, uintptr_t arch)
96{
97 if (arch & INTPTR_MIN) { // Proprietary vendor-specific architecture
98 arch &= INTPTR_MAX;
99 snprintf(buf, buflen, "vendor arch 0x%"PRIXPTR, arch);
100 return buf;
101
102 } else {
103 switch (arch) { // Open core listed in ISA manual.
104 case 0: return "Unspecified";
105 case 5: return "Spike";
106 case 42: return "QEMU";
107 default:
108 snprintf(buf, buflen, "open arch %"PRIuPTR, arch);
109 return buf;
110 }
111 }
112}
113
114/* CPU capabilities relevant to the checked call harness. */
115#define RISCV_FLOAT (1 << 0)
116#define RISCV_VECTOR (1 << 1)
117
118static int checkasm_hwprobe(void)
119{
120 int flags = 0;
121#if HAVE_SYS_HWPROBE_H || HAVE_ASM_HWPROBE_H
122 struct riscv_hwprobe pairs[] = {
123 { RISCV_HWPROBE_KEY_IMA_EXT_0, 0 },
124 };
125
126 if (__riscv_hwprobe(pairs, ARRAY_SIZE(pairs), 0, NULL, 0) == 0) {
127 if (pairs[0].value & RISCV_HWPROBE_IMA_FD)
128 flags |= RISCV_FLOAT;
129#ifdef RISCV_HWPROBE_EXT_ZVE32X
130 if (pairs[0].value & RISCV_HWPROBE_EXT_ZVE32X)
131 flags |= RISCV_VECTOR;
132#endif
133#ifdef RISCV_HWPROBE_IMA_V
134 // A few Linux kernel (6.6+) versions recognise V but not Zve32x.
135 if (pairs[0].value & RISCV_HWPROBE_IMA_V)
136 flags |= RISCV_VECTOR;
137#endif
138 }
139 /*
140 * We purposely do not fallback to HWCAP on Linux. Kernel versions without
141 * `hwprobe()` have hard-coded float support (on or off) and do not support
142 * other register extensions such as vectors.
143 */
144#elif HAVE_GETAUXVAL || HAVE_ELF_AUX_INFO
145 {
146 const unsigned long hwcap = checkasm_getauxval(AT_HWCAP);
147
148 if (hwcap & HWCAP_RV('F'))
149 flags |= RISCV_FLOAT;
150 if (hwcap & HWCAP_RV('V'))
151 flags |= RISCV_VECTOR;
152 }
153#endif
154#ifdef __riscv_f
155 flags |= RISCV_FLOAT;
156#endif
157#ifdef __riscv_vector
158 flags |= RISCV_VECTOR;
159#endif
160 return flags;
161}
162
163static int checkasm_cpu_flags(void)
164{
165 static atomic_int cpu_flags = INT_MIN;
166 int flags = atomic_load_explicit(&cpu_flags, memory_order_relaxed);
167
168 if (flags < 0) {
169 flags = checkasm_hwprobe();
170 atomic_store_explicit(&cpu_flags, flags, memory_order_relaxed);
171 }
172
173 return flags;
174}
175
176int checkasm_has_float(void)
177{
178 return (checkasm_cpu_flags() & RISCV_FLOAT) != 0;
179}
180
181int checkasm_has_vector(void)
182{
183 return (checkasm_cpu_flags() & RISCV_VECTOR) != 0;
184}
185
187{
188 void *checked_call = NULL;
189 int flags = checkasm_cpu_flags();
190
191 switch (flags) {
192#ifdef __riscv_float_abi_soft
193 case 0:
194 checked_call = checkasm_checked_call_i;
195 break;
196 case RISCV_VECTOR:
197 checked_call = checkasm_checked_call_iv;
198 break;
199#endif
200 case RISCV_FLOAT:
201 checked_call = checkasm_checked_call_if;
202 break;
203 case RISCV_FLOAT | RISCV_VECTOR:
204 checked_call = checkasm_checked_call_ifv;
205 break;
206 }
207 assert(checked_call != NULL);
208 return checked_call;
209}
210
211#endif /* ARCH_RISCV */
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define NULL
Definition coverity.c:32
intptr_t atomic_int
Definition stdatomic.h:55
#define atomic_load_explicit(object, order)
Definition stdatomic.h:96
#define atomic_store_explicit(object, desired, order)
Definition stdatomic.h:90
double value
Definition eval.c:102
unsigned offset
Definition libaomenc.c:763
static atomic_int cpu_flags
Definition cpu.c:56
static atomic_int cpu_count
Definition cpu.c:57
#define AT_HWCAP
Definition cpu.c:50
CHECKASM_API checkasm_checked_call_func checkasm_checked_call_ptr(void)
#define snprintf
Definition snprintf.h:34
COLD unsigned long checkasm_getauxval(unsigned long type)
Definition cpu.c:51
COLD const char * checkasm_get_jedec_vendor_name(unsigned bank, unsigned offset)
Definition cpu.c:159
#define ARRAY_SIZE(a)
Definition internal.h:81