38
38
#include < errno.h>
39
39
#include < stdlib.h>
40
40
41
+ // Platform-specific includes
42
+ #if defined(PLATFORM_FREEBSD)
43
+ #include < sys/sysctl.h>
44
+ #elif defined(PLATFORM_MACOSX)
45
+ #include < sys/sysctl.h>
46
+ #endif
47
+
41
48
// Include common architectural definitions
42
49
#define PRIVATE_DSP_ARCH_AARCH64_IMPL
43
50
#include < private/dsp/arch/aarch64/features.h>
@@ -151,46 +158,36 @@ namespace lsp
151
158
else
152
159
return cpu_parts[mid].name ;
153
160
}
154
- return " Generic ARM processor" ;
161
+ return " Generic AArch64 processor" ;
155
162
}
156
163
157
- void detect_cpu_features (cpu_features_t *f) // must be at least 13 bytes
164
+ #ifdef PLATFORM_LINUX
165
+ static void read_cpu_info (cpu_features_t *f)
158
166
{
159
- f->implementer = 0 ;
160
- f->architecture = 0 ;
161
- IF_ARCH_ARM6 (f->architecture = 6 );
162
- IF_ARCH_ARM7 (f->architecture = 7 );
163
- IF_ARCH_ARM8 (f->architecture = 8 );
164
- f->variant = 0 ;
165
- f->part = 0 ;
166
- f->revision = 0 ;
167
-
168
- #if defined(PLATFORM_LINUX)
169
- f->hwcap = getauxval (AT_HWCAP);
170
- #elif defined(PLATFORM_BSD)
171
- unsigned long __hwcap = 0 ;
172
- if (elf_aux_info (AT_HWCAP, &__hwcap, sizeof (__hwcap)) == 0 )
173
- f->hwcap = __hwcap;
174
- #else
175
- f->hwcap = 0 ;
176
- #endif
177
-
178
- // processor : 0
179
- // BogoMIPS : 38.40
180
- // Features : fp asimd evtstrm crc32 cpuid
181
- // CPU implementer : 0x41
182
- // CPU architecture: 8
183
- // CPU variant : 0x0
184
- // CPU part : 0xd03
185
- // CPU revision : 4
167
+ // Example contents:
168
+ // processor : 0
169
+ // BogoMIPS : 38.40
170
+ // Features : fp asimd evtstrm crc32 cpuid
171
+ // CPU implementer : 0x41
172
+ // CPU architecture: 8
173
+ // CPU variant : 0x0
174
+ // CPU part : 0xd03
175
+ // CPU revision : 4
186
176
187
177
// Read /proc/cpuinfo
188
178
FILE *cpuinfo = fopen (" /proc/cpuinfo" , " r" );
189
179
if (cpuinfo == NULL )
190
180
return ; // handle error
181
+ lsp_finally {
182
+ fclose (cpuinfo);
183
+ };
191
184
192
185
size_t size = 0 ;
193
186
char *line = NULL ;
187
+ lsp_finally {
188
+ if (line != NULL )
189
+ free (line);
190
+ };
194
191
195
192
while (getline (&line, &size, cpuinfo) >= 0 )
196
193
{
@@ -238,11 +235,69 @@ namespace lsp
238
235
// Store parsed value
239
236
*field = value;
240
237
}
238
+ }
239
+ #endif /* PLATFORM_LINUX */
240
+
241
+ #ifdef PLATFORM_MACOSX
242
+ static void read_hwcap (cpu_features_t *f)
243
+ {
244
+ int val = 0 ;
245
+ size_t len = sizeof (val);
246
+ if (sysctlbyname (" hw.optional.AdvSIMD" , &val, &len, NULL , 0 ) == 0 )
247
+ {
248
+ if (val != 0 )
249
+ f->hwcap |= HWCAP_AARCH64_ASIMD;
250
+ }
251
+ }
252
+
253
+ static void read_cpu_name (cpu_features_t *f)
254
+ {
255
+ size_t len = sizeof (f->cpu_name );
256
+ sysctlbyname (" machdep.cpu.brand_string" , f->cpu_name , &len, nullptr , 0 );
257
+ }
258
+ #endif /* PLATFORM_MACOSX */
259
+
260
+ #ifdef PLATFORM_FREEBSD
261
+ static void read_cpu_name (cpu_features_t *f)
262
+ {
263
+ size_t len = sizeof (f->cpu_name );
264
+ sysctlbyname (" hw.model" , f->cpu_name , &len, nullptr , 0 );
265
+ }
266
+ #endif /* PLATFORM_FREEBSD */
267
+
268
+ void detect_cpu_features (cpu_features_t *f) // must be at least 13 bytes
269
+ {
270
+ f->implementer = 0 ;
271
+ f->architecture = 0 ;
272
+ IF_ARCH_ARM6 (f->architecture = 6 );
273
+ IF_ARCH_ARM7 (f->architecture = 7 );
274
+ IF_ARCH_ARM8 (f->architecture = 8 );
275
+ f->variant = 0 ;
276
+ f->part = 0 ;
277
+ f->revision = 0 ;
278
+ f->hwcap = 0 ;
279
+ strncpy (f->cpu_name , " Generic AArch64 processor" , sizeof (f->cpu_name ) - 1 );
280
+
281
+ #if defined(PLATFORM_LINUX)
282
+ f->hwcap = getauxval (AT_HWCAP);
283
+ read_cpu_info (f);
284
+ const char *cpu_name = find_cpu_name (f->part );
285
+ if (cpu_name != NULL )
286
+ strncpy (f->cpu_name , cpu_name, sizeof (f->cpu_name ));
287
+
288
+ #elif defined(PLATFORM_BSD)
289
+ unsigned long __hwcap = 0 ;
290
+ if (elf_aux_info (AT_HWCAP, &__hwcap, sizeof (__hwcap)) == 0 )
291
+ f->hwcap = __hwcap;
292
+ read_cpu_name (f);
293
+
294
+ #elif defined(PLATFORM_MACOSX)
295
+ read_hwcap (f);
296
+ read_cpu_name (f);
297
+
298
+ #endif
241
299
242
- // if we got here, handle error
243
- if (line != NULL )
244
- free (line);
245
- fclose (cpuinfo);
300
+ f->cpu_name [sizeof (f->cpu_name ) - 1 ] = ' \0 ' ;
246
301
}
247
302
248
303
static size_t estimate_features_size (const cpu_features_t *f)
@@ -284,7 +339,6 @@ namespace lsp
284
339
cpu_features_t f;
285
340
detect_cpu_features (&f);
286
341
287
- const char *cpu = find_cpu_name (f.part );
288
342
char *model = NULL ;
289
343
int n = asprintf (&model, " vendor=0x%x, architecture=%d, variant=%d, part=0x%x, revision=%d" ,
290
344
int (f.implementer ), int (f.architecture ), int (f.variant ), int (f.part ), int (f.revision ));
@@ -293,7 +347,7 @@ namespace lsp
293
347
294
348
size_t size = sizeof (dsp::info_t );
295
349
size += strlen (ARCH_STRING) + 1 ;
296
- size += strlen (cpu ) + 1 ;
350
+ size += strlen (f. cpu_name ) + 1 ;
297
351
size += strlen (model) + 1 ;
298
352
size += estimate_features_size (&f);
299
353
@@ -308,7 +362,7 @@ namespace lsp
308
362
res->arch = text;
309
363
text = stpcpy (text, ARCH_STRING) + 1 ;
310
364
res->cpu = text;
311
- text = stpcpy (text, cpu ) + 1 ;
365
+ text = stpcpy (text, f. cpu_name ) + 1 ;
312
366
res->model = text;
313
367
text = stpcpy (text, model) + 1 ;
314
368
res->features = text;
0 commit comments