summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/google/boringssl/crypto/cpu_aarch64_sysreg.cc
blob: 67a2c972aaa92f2445dc6f5566255dc6c9768d8d (plain) (blame)
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
// Copyright 2023 The BoringSSL Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <contrib/restricted/google/boringssl/include/openssl/cpu.h>

#include "internal.h"

// While Arm system registers are normally not available to userspace, FreeBSD
// expects userspace to simply read them. It traps the reads and fills in CPU
// capabilities.
#if defined(OPENSSL_AARCH64) && !defined(OPENSSL_STATIC_ARMCAP) && \
    (defined(ANDROID_BAREMETAL) || defined(OPENSSL_FREEBSD)) &&    \
    !defined(OPENSSL_NO_ASM)

#include "./armv8_feature_parsing.h"

BSSL_NAMESPACE_BEGIN

#define ID_AA64PFR0_EL1_ADVSIMD 5

#define READ_SYSREG(name)                \
  ({                                     \
    uint64_t _r;                         \
    __asm__("mrs %0, " name : "=r"(_r)); \
    _r;                                  \
  })

// We use the common GetIDField helper now, but need a signed variant
// for the NEON check using ID_AA64PFR0_EL1.
static int GetSignedIDField(uint64_t reg, unsigned field) {
  unsigned value = armcap::GetIDField(reg, field);
  if (value & (1 << (NBITS_ID_FIELD - 1))) {
    return (int)(value | (UINT64_MAX << NBITS_ID_FIELD));
  } else {
    return (int)value;
  }
}

void OPENSSL_cpuid_setup() {
  uint64_t id_aa64pfr0_el1 = READ_SYSREG("id_aa64pfr0_el1");
  if (GetSignedIDField(id_aa64pfr0_el1, ID_AA64PFR0_EL1_ADVSIMD) < 0) {
    // If AdvSIMD ("NEON") is missing, don't report other features either.
    // This matches OpenSSL.
    return;
  }

  // Use the common parsing function to check all cryptographic features.
  uint64_t id_aa64isar0_el1 = READ_SYSREG("id_aa64isar0_el1");
  BSSL_armcap_P |= ARMV7_NEON | armcap::ParseISAR0Flags(id_aa64isar0_el1);
}

BSSL_NAMESPACE_END

#endif  // OPENSSL_AARCH64 && !OPENSSL_STATIC_ARMCAP &&
        // (ANDROID_BAREMETAL || OPENSSL_FREEBSD)