summaryrefslogtreecommitdiff
path: root/src/hwf-arm.c
blob: 500cd97a7179d38a103776159fb3983d76ee21da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/* hwf-arm.c - Detect hardware features - ARM part
 * Copyright (C) 2013,2019,2022 Jussi Kivilinna <jussi.kivilinna@iki.fi>
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#if defined(HAVE_SYS_AUXV_H) && (defined(HAVE_GETAUXVAL) || \
    defined(HAVE_ELF_AUX_INFO))
#include <sys/auxv.h>
#endif
#if defined(__APPLE__) && defined(HAVE_SYS_SYSCTL_H) && \
    defined(HAVE_SYSCTLBYNAME)
#include <sys/sysctl.h>
#endif

#include "g10lib.h"
#include "hwf-common.h"

#if !defined (__arm__) && !defined (__aarch64__)
# error Module build for wrong CPU.
#endif


#if defined(HAVE_SYS_AUXV_H) && defined(HAVE_ELF_AUX_INFO) && \
    !defined(HAVE_GETAUXVAL) && defined(AT_HWCAP)
#define HAVE_GETAUXVAL
static unsigned long getauxval(unsigned long type)
{
  unsigned long auxval = 0;
  int err;

  /* FreeBSD provides 'elf_aux_info' function that does the same as
   * 'getauxval' on Linux. */

  err = elf_aux_info (type, &auxval, sizeof(auxval));
  if (err)
    {
      errno = err;
      auxval = 0;
    }

  return auxval;
}
#endif


#undef HAS_SYS_AT_HWCAP
#if defined(__linux__) || \
    (defined(HAVE_SYS_AUXV_H) && defined(HAVE_GETAUXVAL))
#define HAS_SYS_AT_HWCAP 1

struct feature_map_s {
  unsigned int hwcap_flag;
  unsigned int hwcap2_flag;
  const char *feature_match;
  unsigned int hwf_flag;
};

#ifdef __arm__

/* Note: These macros have same values on Linux and FreeBSD. */
#ifndef AT_HWCAP
# define AT_HWCAP      16
#endif
#ifndef AT_HWCAP2
# define AT_HWCAP2     26
#endif

#ifndef HWCAP_NEON
# define HWCAP_NEON    4096
#endif

#ifndef HWCAP2_AES
# define HWCAP2_AES    1
#endif
#ifndef HWCAP2_PMULL
# define HWCAP2_PMULL  2
#endif
#ifndef HWCAP2_SHA1
# define HWCAP2_SHA1   4
#endif
#ifndef HWCAP2_SHA2
# define HWCAP2_SHA2   8
#endif

static const struct feature_map_s arm_features[] =
  {
#ifdef ENABLE_NEON_SUPPORT
    { HWCAP_NEON, 0, " neon", HWF_ARM_NEON },
#endif
#ifdef ENABLE_ARM_CRYPTO_SUPPORT
    { 0, HWCAP2_AES, " aes", HWF_ARM_AES },
    { 0, HWCAP2_SHA1," sha1", HWF_ARM_SHA1 },
    { 0, HWCAP2_SHA2, " sha2", HWF_ARM_SHA2 },
    { 0, HWCAP2_PMULL, " pmull", HWF_ARM_PMULL },
#endif
  };

#elif defined(__aarch64__)

/* Note: These macros have same values on Linux and FreeBSD. */
#ifndef AT_HWCAP
# define AT_HWCAP    16
#endif
#ifndef AT_HWCAP2
# define AT_HWCAP2   -1
#endif

#ifndef HWCAP_ASIMD
# define HWCAP_ASIMD 2
#endif
#ifndef HWCAP_AES
# define HWCAP_AES   8
#endif
#ifndef HWCAP_PMULL
# define HWCAP_PMULL 16
#endif
#ifndef HWCAP_SHA1
# define HWCAP_SHA1  32
#endif
#ifndef HWCAP_SHA2
# define HWCAP_SHA2  64
#endif
#ifndef HWCAP_SHA3
# define HWCAP_SHA3  (1 << 17)
#endif
#ifndef HWCAP_SM3
# define HWCAP_SM3   (1 << 18)
#endif
#ifndef HWCAP_SM4
# define HWCAP_SM4   (1 << 19)
#endif
#ifndef HWCAP_SHA512
# define HWCAP_SHA512 (1 << 21)
#endif
#ifndef HWCAP_SVE
# define HWCAP_SVE    (1 << 22)
#endif

#ifndef HWCAP2_SVE2
# define HWCAP2_SVE2        (1 << 1)
#endif
#ifndef HWCAP2_SVEAES
# define HWCAP2_SVEAES      (1 << 2)
#endif
#ifndef HWCAP2_SVEPMULL
# define HWCAP2_SVEPMULL    (1 << 3)
#endif
#ifndef HWCAP2_SVESHA3
# define HWCAP2_SVESHA3     (1 << 5)
#endif
#ifndef HWCAP2_SVESM4
# define HWCAP2_SVESM4      (1 << 6)
#endif

static const struct feature_map_s arm_features[] =
  {
#ifdef ENABLE_NEON_SUPPORT
    { HWCAP_ASIMD, 0, " asimd", HWF_ARM_NEON },
#endif
#ifdef ENABLE_ARM_CRYPTO_SUPPORT
    { HWCAP_AES, 0, " aes", HWF_ARM_AES },
    { HWCAP_SHA1, 0, " sha1", HWF_ARM_SHA1 },
    { HWCAP_SHA2, 0, " sha2", HWF_ARM_SHA2 },
    { HWCAP_PMULL, 0, " pmull", HWF_ARM_PMULL },
    { HWCAP_SHA3, 0, " sha3",  HWF_ARM_SHA3 },
    { HWCAP_SM3, 0, " sm3",  HWF_ARM_SM3 },
    { HWCAP_SM4, 0, " sm4",  HWF_ARM_SM4 },
    { HWCAP_SHA512, 0, " sha512",  HWF_ARM_SHA512 },
#endif
#ifdef ENABLE_SVE_SUPPORT
    { HWCAP_SVE, 0, " sve",  HWF_ARM_SVE },
    { 0, HWCAP2_SVE2, " sve2",  HWF_ARM_SVE2 },
    { 0, HWCAP2_SVEAES, " sveaes",  HWF_ARM_SVEAES },
    { 0, HWCAP2_SVEPMULL, " svepmull",  HWF_ARM_SVEPMULL },
    { 0, HWCAP2_SVESHA3, " svesha3",  HWF_ARM_SVESHA3 },
    { 0, HWCAP2_SVESM4, " svesm4",  HWF_ARM_SVESM4 },
#endif
  };

#endif

static int
get_hwcap(unsigned int *hwcap, unsigned int *hwcap2)
{
  struct { unsigned long a_type; unsigned long a_val; } auxv;
  FILE *f;
  int err = -1;
  static int hwcap_initialized = 0;
  static unsigned int stored_hwcap = 0;
  static unsigned int stored_hwcap2 = 0;

  if (hwcap_initialized)
    {
      *hwcap = stored_hwcap;
      *hwcap2 = stored_hwcap2;
      return 0;
    }

#if defined(HAVE_SYS_AUXV_H) && defined(HAVE_GETAUXVAL)
  errno = 0;
  auxv.a_val = getauxval (AT_HWCAP);
  if (errno == 0)
    {
      stored_hwcap |= auxv.a_val;
      hwcap_initialized = 1;
    }

  if (AT_HWCAP2 >= 0)
    {
      errno = 0;
      auxv.a_val = getauxval (AT_HWCAP2);
      if (errno == 0)
	{
	  stored_hwcap2 |= auxv.a_val;
	  hwcap_initialized = 1;
	}
    }

  if (hwcap_initialized && (stored_hwcap || stored_hwcap2))
    {
      *hwcap = stored_hwcap;
      *hwcap2 = stored_hwcap2;
      return 0;
    }
#endif

  f = fopen("/proc/self/auxv", "r");
  if (!f)
    {
      *hwcap = stored_hwcap;
      *hwcap2 = stored_hwcap2;
      return -1;
    }

  while (fread(&auxv, sizeof(auxv), 1, f) > 0)
    {
      if (auxv.a_type == AT_HWCAP)
        {
          stored_hwcap |= auxv.a_val;
          hwcap_initialized = 1;
        }

      if (auxv.a_type == AT_HWCAP2)
        {
          stored_hwcap2 |= auxv.a_val;
          hwcap_initialized = 1;
        }
    }

  if (hwcap_initialized)
    err = 0;

  fclose(f);
  *hwcap = stored_hwcap;
  *hwcap2 = stored_hwcap2;
  return err;
}

static unsigned int
detect_arm_at_hwcap(void)
{
  unsigned int hwcap;
  unsigned int hwcap2;
  unsigned int features = 0;
  unsigned int i;

  if (get_hwcap(&hwcap, &hwcap2) < 0)
    return features;

  for (i = 0; i < DIM(arm_features); i++)
    {
      if (hwcap & arm_features[i].hwcap_flag)
        features |= arm_features[i].hwf_flag;

      if (hwcap2 & arm_features[i].hwcap2_flag)
        features |= arm_features[i].hwf_flag;
    }

  return features;
}

#endif

#undef HAS_PROC_CPUINFO
#ifdef __linux__
#define HAS_PROC_CPUINFO 1

static unsigned int
detect_arm_proc_cpuinfo(unsigned int *broken_hwfs)
{
  char buf[1024]; /* large enough */
  char *str_features, *str_feat;
  int cpu_implementer, cpu_arch, cpu_variant, cpu_part, cpu_revision;
  FILE *f;
  int readlen, i;
  size_t mlen;
  static int cpuinfo_initialized = 0;
  static unsigned int stored_cpuinfo_features;
  static unsigned int stored_broken_hwfs;
  struct {
    const char *name;
    int *value;
  } cpu_entries[5] = {
    { "CPU implementer", &cpu_implementer },
    { "CPU architecture", &cpu_arch },
    { "CPU variant", &cpu_variant },
    { "CPU part", &cpu_part },
    { "CPU revision", &cpu_revision },
  };

  if (cpuinfo_initialized)
    {
      *broken_hwfs |= stored_broken_hwfs;
      return stored_cpuinfo_features;
    }

  f = fopen("/proc/cpuinfo", "r");
  if (!f)
    return 0;

  memset (buf, 0, sizeof(buf));
  readlen = fread (buf, 1, sizeof(buf), f);
  fclose (f);
  if (readlen <= 0 || readlen > sizeof(buf))
    return 0;

  buf[sizeof(buf) - 1] = '\0';

  cpuinfo_initialized = 1;
  stored_cpuinfo_features = 0;
  stored_broken_hwfs = 0;

  /* Find features line. */
  str_features = strstr(buf, "Features");
  if (!str_features)
    return stored_cpuinfo_features;

  /* Find CPU version information. */
  for (i = 0; i < DIM(cpu_entries); i++)
    {
      char *str;

      *cpu_entries[i].value = -1;

      str = strstr(buf, cpu_entries[i].name);
      if (!str)
        continue;

      str = strstr(str, ": ");
      if (!str)
        continue;

      str += 2;
      if (strcmp(cpu_entries[i].name, "CPU architecture") == 0
          && strcmp(str, "AArch64") == 0)
        *cpu_entries[i].value = 8;
      else
        *cpu_entries[i].value = strtoul(str, NULL, 0);
    }

  /* Lines to strings. */
  for (i = 0; i < sizeof(buf); i++)
    if (buf[i] == '\n')
      buf[i] = '\0';

  /* Check features. */
  for (i = 0; i < DIM(arm_features); i++)
    {
      str_feat = strstr(str_features, arm_features[i].feature_match);
      if (str_feat)
        {
          mlen = strlen(arm_features[i].feature_match);
          if (str_feat[mlen] == ' ' || str_feat[mlen] == '\0')
            {
              stored_cpuinfo_features |= arm_features[i].hwf_flag;
            }
        }
    }

  /* Check for CPUs with broken NEON implementation. See
   * https://code.google.com/p/chromium/issues/detail?id=341598
   */
  if (cpu_implementer == 0x51
      && cpu_arch == 7
      && cpu_variant == 1
      && cpu_part == 0x4d
      && cpu_revision == 0)
    {
      stored_broken_hwfs = HWF_ARM_NEON;
    }

  *broken_hwfs |= stored_broken_hwfs;
  return stored_cpuinfo_features;
}

#endif /* __linux__ */


#undef HAS_APPLE_SYSCTLBYNAME
#if defined(__APPLE__) && defined(HAVE_SYS_SYSCTL_H) && \
    defined(HAVE_SYSCTLBYNAME)
#define HAS_APPLE_SYSCTLBYNAME 1

static unsigned int
detect_arm_apple_sysctlbyname (void)
{
  static const struct
  {
    const char *feat_name;
    unsigned int hwf_flag;
  } hw_optional_arm_features[] =
    {
#ifdef ENABLE_NEON_SUPPORT
      { "hw.optional.neon",            HWF_ARM_NEON },
      { "hw.optional.AdvSIMD",         HWF_ARM_NEON },
#endif
#ifdef ENABLE_ARM_CRYPTO_SUPPORT
      { "hw.optional.arm.FEAT_AES",    HWF_ARM_AES },
      { "hw.optional.arm.FEAT_SHA1",   HWF_ARM_SHA1 },
      { "hw.optional.arm.FEAT_SHA256", HWF_ARM_SHA2 },
      { "hw.optional.arm.FEAT_PMULL",  HWF_ARM_PMULL },
      { "hw.optional.arm.FEAT_SHA3",   HWF_ARM_SHA3 },
      { "hw.optional.armv8_2_sha3",    HWF_ARM_SHA3 },
      { "hw.optional.arm.FEAT_SHA512", HWF_ARM_SHA512 },
      { "hw.optional.armv8_2_sha512",  HWF_ARM_SHA512 },
#endif
    };
  unsigned int i;
  unsigned int hwf = 0;

  for (i = 0; i < DIM(hw_optional_arm_features); i++)
    {
      const char *name = hw_optional_arm_features[i].feat_name;
      int sysctl_value = 0;
      size_t value_size = sizeof(sysctl_value);

      if (sysctlbyname (name, &sysctl_value, &value_size, NULL, 0) != 0)
        continue;

      if (value_size != sizeof(sysctl_value))
        continue;

      if (sysctl_value == 1)
        {
          hwf |= hw_optional_arm_features[i].hwf_flag;
        }
    }

  return hwf;
}

#endif /* __APPLE__ */


static unsigned int
detect_arm_hwf_by_toolchain (void)
{
  unsigned int ret = 0;

  /* Detect CPU features required by toolchain.
   * This allows detection of ARMv8 crypto extension support,
   * for example, on macOS/aarch64.
   */

#if __GNUC__ >= 4

#if defined(__ARM_NEON) && defined(ENABLE_NEON_SUPPORT)
  ret |= HWF_ARM_NEON;

#ifdef HAVE_GCC_INLINE_ASM_NEON
  /* Early test for NEON instruction to detect faulty toolchain
   * configuration. */
  asm volatile ("veor q15, q15, q15":::"q15");
#endif

#ifdef HAVE_GCC_INLINE_ASM_AARCH64_NEON
  /* Early test for NEON instruction to detect faulty toolchain
   * configuration. */
  asm volatile ("eor v31.16b, v31.16b, v31.16b":::"v31");
#endif

#endif /* __ARM_NEON */

#if defined(__ARM_FEATURE_CRYPTO) && defined(ENABLE_ARM_CRYPTO_SUPPORT)
  /* ARMv8 crypto extensions include support for PMULL, AES, SHA1 and SHA2
   * instructions. */
  ret |= HWF_ARM_PMULL;
  ret |= HWF_ARM_AES;
  ret |= HWF_ARM_SHA1;
  ret |= HWF_ARM_SHA2;

#ifdef HAVE_GCC_INLINE_ASM_AARCH32_CRYPTO
  /* Early test for CE instructions to detect faulty toolchain
   * configuration. */
  asm volatile ("vmull.p64 q0, d0, d0;\n\t"
		"aesimc.8 q7, q0;\n\t"
		"sha1su1.32 q0, q0;\n\t"
		"sha256su1.32 q0, q7, q15;\n\t"
		:::
		"q0", "q7", "q15");
#endif

#ifdef HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO
  /* Early test for CE instructions to detect faulty toolchain
   * configuration. */
  asm volatile ("pmull2 v0.1q, v0.2d, v31.2d;\n\t"
		"aesimc v15.16b, v0.16b;\n\t"
		"sha1su1 v0.4s, v0.4s;\n\t"
		"sha256su1 v0.4s, v15.4s, v31.4s;\n\t"
		:::
		"v0", "v15", "v31");
#endif
#endif

#endif

  return ret;
}

unsigned int
_gcry_hwf_detect_arm (void)
{
  unsigned int ret = 0;
  unsigned int broken_hwfs = 0;

#if defined (HAS_SYS_AT_HWCAP)
  ret |= detect_arm_at_hwcap ();
#endif

#if defined (HAS_PROC_CPUINFO)
  ret |= detect_arm_proc_cpuinfo (&broken_hwfs);
#endif

#if defined (HAS_APPLE_SYSCTLBYNAME)
  ret |= detect_arm_apple_sysctlbyname ();
#endif

  ret |= detect_arm_hwf_by_toolchain ();

  ret &= ~broken_hwfs;

  return ret;
}