summaryrefslogtreecommitdiff
path: root/src/hwf-arm.c
blob: 3dc050e7a7b725acb5beaf881a923bc66c92569f (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
/* hwf-arm.c - Detect hardware features - ARM part
 * Copyright (C) 2013  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 "g10lib.h"
#include "hwf-common.h"

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

#undef HAS_SYS_AT_HWCAP
#undef HAS_PROC_CPUINFO
#ifdef __linux__

#define HAS_SYS_AT_HWCAP 1

#define AT_HWCAP 16
#define HWCAP_NEON 4096

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

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

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

  while (fread(&auxv, sizeof(auxv), 1, f) > 0)
    {
      if (auxv.a_type != AT_HWCAP)
        continue;

      stored_hwcap = auxv.a_val;
      hwcap_initialized = 1;
      err = 0;
      break;
    }

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

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

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

#ifdef ENABLE_NEON_SUPPORT
  if (hwcap & HWCAP_NEON)
    features |= HWF_ARM_NEON;
#endif

  return features;
}

#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_neon;
  int cpu_implementer, cpu_arch, cpu_variant, cpu_part, cpu_revision;
  FILE *f;
  int readlen, i;
  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;
      *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 for NEON. */
  str_neon = strstr(str_features, " neon");
  if (str_neon && (str_neon[5] == ' ' || str_neon[5] == '\0'))
    stored_cpuinfo_features |= HWF_ARM_NEON;

  /* 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__ */

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(__ARM_NEON__) && defined(ENABLE_NEON_SUPPORT)
  ret |= HWF_ARM_NEON;
#endif

  ret &= ~broken_hwfs;

  return ret;
}