| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "internal.h" |
| 16 |
| 17 | // While Arm system registers are normally not available to userspace, FreeBSD |
| 18 | // expects userspace to simply read them. It traps the reads and fills in CPU |
| 19 | // capabilities. |
| 20 | #if defined(OPENSSL_AARCH64) && !defined(OPENSSL_STATIC_ARMCAP) && \ |
| 21 | (defined(ANDROID_BAREMETAL) || defined(OPENSSL_FREEBSD)) && \ |
| 22 | !defined(OPENSSL_NO_ASM) |
| 23 |
| 24 | #define ID_AA64PFR0_EL1_ADVSIMD 5 |
| 25 |
| 26 | #define ID_AA64ISAR0_EL1_AES 1 |
| 27 | #define ID_AA64ISAR0_EL1_SHA1 2 |
| 28 | #define ID_AA64ISAR0_EL1_SHA2 3 |
| 29 |
| 30 | #define NBITS_ID_FIELD 4 |
| 31 |
| 32 | #define READ_SYSREG(name) \ |
| 33 | ({ \ |
| 34 | uint64_t _r; \ |
| 35 | __asm__("mrs %0, " name : "=r"(_r)); \ |
| 36 | _r; \ |
| 37 | }) |
| 38 |
| 39 | static unsigned get_id_field(uint64_t reg, unsigned field) { |
| 40 | return (reg >> (field * NBITS_ID_FIELD)) & ((1 << NBITS_ID_FIELD) - 1); |
| 41 | } |
| 42 |
| 43 | static int get_signed_id_field(uint64_t reg, unsigned field) { |
| 44 | unsigned value = get_id_field(reg, field); |
| 45 | if (value & (1 << (NBITS_ID_FIELD - 1))) { |
| 46 | return (int)(value | (UINT64_MAX << NBITS_ID_FIELD)); |
| 47 | } else { |
| 48 | return (int)value; |
| 49 | } |
| 50 | } |
| 51 |
| 52 | static uint32_t read_armcap(void) { |
| 53 | uint32_t armcap = ARMV7_NEON; |
| 54 |
| 55 | uint64_t id_aa64pfr0_el1 = READ_SYSREG("id_aa64pfr0_el1"); |
| 56 |
| 57 | if (get_signed_id_field(id_aa64pfr0_el1, ID_AA64PFR0_EL1_ADVSIMD) < 0) { |
| 58 | // If AdvSIMD ("NEON") is missing, don't report other features either. |
| 59 | // This matches OpenSSL. |
| 60 | return 0; |
| 61 | } |
| 62 |
| 63 | uint64_t id_aa64isar0_el1 = READ_SYSREG("id_aa64isar0_el1"); |
| 64 |
| 65 | unsigned aes = get_id_field(id_aa64isar0_el1, ID_AA64ISAR0_EL1_AES); |
| 66 | if (aes > 0) { |
| 67 | armcap |= ARMV8_AES; |
| 68 | } |
| 69 | if (aes > 1) { |
| 70 | armcap |= ARMV8_PMULL; |
| 71 | } |
| 72 |
| 73 | unsigned sha1 = get_id_field(id_aa64isar0_el1, ID_AA64ISAR0_EL1_SHA1); |
| 74 | if (sha1 > 0) { |
| 75 | armcap |= ARMV8_SHA1; |
| 76 | } |
| 77 |
| 78 | unsigned sha2 = get_id_field(id_aa64isar0_el1, ID_AA64ISAR0_EL1_SHA2); |
| 79 | if (sha2 > 0) { |
| 80 | armcap |= ARMV8_SHA256; |
| 81 | } |
| 82 | if (sha2 > 1) { |
| 83 | armcap |= ARMV8_SHA512; |
| 84 | } |
| 85 |
| 86 | return armcap; |
| 87 | } |
| 88 |
| 89 | void OPENSSL_cpuid_setup(void) { OPENSSL_armcap_P |= read_armcap(); } |
| 90 |
| 91 | #endif // OPENSSL_AARCH64 && !OPENSSL_STATIC_ARMCAP && |
| 92 | // (ANDROID_BAREMETAL || OPENSSL_FREEBSD) |
| 93 |