arm_arch.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2011-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_CRYPTO_ARM_ARCH_H
  10. # define OSSL_CRYPTO_ARM_ARCH_H
  11. # if !defined(__ARM_ARCH__)
  12. # if defined(__CC_ARM)
  13. # define __ARM_ARCH__ __TARGET_ARCH_ARM
  14. # if defined(__BIG_ENDIAN)
  15. # define __ARMEB__
  16. # else
  17. # define __ARMEL__
  18. # endif
  19. # elif defined(__GNUC__)
  20. # if defined(__aarch64__)
  21. # define __ARM_ARCH__ 8
  22. /*
  23. * Why doesn't gcc define __ARM_ARCH__? Instead it defines
  24. * bunch of below macros. See all_architectures[] table in
  25. * gcc/config/arm/arm.c. On a side note it defines
  26. * __ARMEL__/__ARMEB__ for little-/big-endian.
  27. */
  28. # elif defined(__ARM_ARCH)
  29. # define __ARM_ARCH__ __ARM_ARCH
  30. # elif defined(__ARM_ARCH_8A__)
  31. # define __ARM_ARCH__ 8
  32. # elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || \
  33. defined(__ARM_ARCH_7R__)|| defined(__ARM_ARCH_7M__) || \
  34. defined(__ARM_ARCH_7EM__)
  35. # define __ARM_ARCH__ 7
  36. # elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || \
  37. defined(__ARM_ARCH_6K__)|| defined(__ARM_ARCH_6M__) || \
  38. defined(__ARM_ARCH_6Z__)|| defined(__ARM_ARCH_6ZK__) || \
  39. defined(__ARM_ARCH_6T2__)
  40. # define __ARM_ARCH__ 6
  41. # elif defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) || \
  42. defined(__ARM_ARCH_5E__)|| defined(__ARM_ARCH_5TE__) || \
  43. defined(__ARM_ARCH_5TEJ__)
  44. # define __ARM_ARCH__ 5
  45. # elif defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__)
  46. # define __ARM_ARCH__ 4
  47. # else
  48. # error "unsupported ARM architecture"
  49. # endif
  50. # elif defined(__ARM_ARCH)
  51. # define __ARM_ARCH__ __ARM_ARCH
  52. # endif
  53. # endif
  54. # if !defined(__ARM_MAX_ARCH__)
  55. # define __ARM_MAX_ARCH__ __ARM_ARCH__
  56. # endif
  57. # if __ARM_MAX_ARCH__<__ARM_ARCH__
  58. # error "__ARM_MAX_ARCH__ can't be less than __ARM_ARCH__"
  59. # elif __ARM_MAX_ARCH__!=__ARM_ARCH__
  60. # if __ARM_ARCH__<7 && __ARM_MAX_ARCH__>=7 && defined(__ARMEB__)
  61. # error "can't build universal big-endian binary"
  62. # endif
  63. # endif
  64. # ifndef __ASSEMBLER__
  65. extern unsigned int OPENSSL_armcap_P;
  66. extern unsigned int OPENSSL_arm_midr;
  67. extern unsigned int OPENSSL_armv8_rsa_neonized;
  68. # endif
  69. # define ARMV7_NEON (1<<0)
  70. # define ARMV7_TICK (1<<1)
  71. # define ARMV8_AES (1<<2)
  72. # define ARMV8_SHA1 (1<<3)
  73. # define ARMV8_SHA256 (1<<4)
  74. # define ARMV8_PMULL (1<<5)
  75. # define ARMV8_SHA512 (1<<6)
  76. # define ARMV8_CPUID (1<<7)
  77. # define ARMV8_RNG (1<<8)
  78. # define ARMV8_SM3 (1<<9)
  79. # define ARMV8_SM4 (1<<10)
  80. # define ARMV8_SHA3 (1<<11)
  81. # define ARMV8_UNROLL8_EOR3 (1<<12)
  82. # define ARMV8_SVE (1<<13)
  83. # define ARMV8_SVE2 (1<<14)
  84. # define ARMV8_HAVE_SHA3_AND_WORTH_USING (1<<15)
  85. # define ARMV8_UNROLL12_EOR3 (1<<16)
  86. /*
  87. * MIDR_EL1 system register
  88. *
  89. * 63___ _ ___32_31___ _ ___24_23_____20_19_____16_15__ _ __4_3_______0
  90. * | | | | | | |
  91. * |RES0 | Implementer | Variant | Arch | PartNum |Revision|
  92. * |____ _ _____|_____ _ _____|_________|_______ _|____ _ ___|________|
  93. *
  94. */
  95. # define ARM_CPU_IMP_ARM 0x41
  96. # define HISI_CPU_IMP 0x48
  97. # define ARM_CPU_IMP_APPLE 0x61
  98. # define ARM_CPU_IMP_MICROSOFT 0x6D
  99. # define ARM_CPU_IMP_AMPERE 0xC0
  100. # define ARM_CPU_PART_CORTEX_A72 0xD08
  101. # define ARM_CPU_PART_N1 0xD0C
  102. # define ARM_CPU_PART_V1 0xD40
  103. # define ARM_CPU_PART_N2 0xD49
  104. # define HISI_CPU_PART_KP920 0xD01
  105. # define ARM_CPU_PART_V2 0xD4F
  106. # define APPLE_CPU_PART_M1_ICESTORM 0x022
  107. # define APPLE_CPU_PART_M1_FIRESTORM 0x023
  108. # define APPLE_CPU_PART_M1_ICESTORM_PRO 0x024
  109. # define APPLE_CPU_PART_M1_FIRESTORM_PRO 0x025
  110. # define APPLE_CPU_PART_M1_ICESTORM_MAX 0x028
  111. # define APPLE_CPU_PART_M1_FIRESTORM_MAX 0x029
  112. # define APPLE_CPU_PART_M2_BLIZZARD 0x032
  113. # define APPLE_CPU_PART_M2_AVALANCHE 0x033
  114. # define APPLE_CPU_PART_M2_BLIZZARD_PRO 0x034
  115. # define APPLE_CPU_PART_M2_AVALANCHE_PRO 0x035
  116. # define APPLE_CPU_PART_M2_BLIZZARD_MAX 0x038
  117. # define APPLE_CPU_PART_M2_AVALANCHE_MAX 0x039
  118. # define MICROSOFT_CPU_PART_COBALT_100 0xD49
  119. # define MIDR_PARTNUM_SHIFT 4
  120. # define MIDR_PARTNUM_MASK (0xfffU << MIDR_PARTNUM_SHIFT)
  121. # define MIDR_PARTNUM(midr) \
  122. (((midr) & MIDR_PARTNUM_MASK) >> MIDR_PARTNUM_SHIFT)
  123. # define MIDR_IMPLEMENTER_SHIFT 24
  124. # define MIDR_IMPLEMENTER_MASK (0xffU << MIDR_IMPLEMENTER_SHIFT)
  125. # define MIDR_IMPLEMENTER(midr) \
  126. (((midr) & MIDR_IMPLEMENTER_MASK) >> MIDR_IMPLEMENTER_SHIFT)
  127. # define MIDR_ARCHITECTURE_SHIFT 16
  128. # define MIDR_ARCHITECTURE_MASK (0xfU << MIDR_ARCHITECTURE_SHIFT)
  129. # define MIDR_ARCHITECTURE(midr) \
  130. (((midr) & MIDR_ARCHITECTURE_MASK) >> MIDR_ARCHITECTURE_SHIFT)
  131. # define MIDR_CPU_MODEL_MASK \
  132. (MIDR_IMPLEMENTER_MASK | \
  133. MIDR_PARTNUM_MASK | \
  134. MIDR_ARCHITECTURE_MASK)
  135. # define MIDR_CPU_MODEL(imp, partnum) \
  136. (((imp) << MIDR_IMPLEMENTER_SHIFT) | \
  137. (0xfU << MIDR_ARCHITECTURE_SHIFT) | \
  138. ((partnum) << MIDR_PARTNUM_SHIFT))
  139. # define MIDR_IS_CPU_MODEL(midr, imp, partnum) \
  140. (((midr) & MIDR_CPU_MODEL_MASK) == MIDR_CPU_MODEL(imp, partnum))
  141. #if defined(__ASSEMBLER__)
  142. /*
  143. * Support macros for
  144. * - Armv8.3-A Pointer Authentication and
  145. * - Armv8.5-A Branch Target Identification
  146. * features which require emitting a .note.gnu.property section with the
  147. * appropriate architecture-dependent feature bits set.
  148. * Read more: "ELF for the Arm® 64-bit Architecture"
  149. */
  150. # if defined(__ARM_FEATURE_BTI_DEFAULT) && __ARM_FEATURE_BTI_DEFAULT == 1
  151. # define GNU_PROPERTY_AARCH64_BTI (1 << 0) /* Has Branch Target Identification */
  152. # define AARCH64_VALID_CALL_TARGET hint #34 /* BTI 'c' */
  153. # else
  154. # define GNU_PROPERTY_AARCH64_BTI 0 /* No Branch Target Identification */
  155. # define AARCH64_VALID_CALL_TARGET
  156. # endif
  157. # if defined(__ARM_FEATURE_PAC_DEFAULT) && \
  158. (__ARM_FEATURE_PAC_DEFAULT & 1) == 1 /* Signed with A-key */
  159. # define GNU_PROPERTY_AARCH64_POINTER_AUTH \
  160. (1 << 1) /* Has Pointer Authentication */
  161. # define AARCH64_SIGN_LINK_REGISTER hint #25 /* PACIASP */
  162. # define AARCH64_VALIDATE_LINK_REGISTER hint #29 /* AUTIASP */
  163. # elif defined(__ARM_FEATURE_PAC_DEFAULT) && \
  164. (__ARM_FEATURE_PAC_DEFAULT & 2) == 2 /* Signed with B-key */
  165. # define GNU_PROPERTY_AARCH64_POINTER_AUTH \
  166. (1 << 1) /* Has Pointer Authentication */
  167. # define AARCH64_SIGN_LINK_REGISTER hint #27 /* PACIBSP */
  168. # define AARCH64_VALIDATE_LINK_REGISTER hint #31 /* AUTIBSP */
  169. # else
  170. # define GNU_PROPERTY_AARCH64_POINTER_AUTH 0 /* No Pointer Authentication */
  171. # if GNU_PROPERTY_AARCH64_BTI != 0
  172. # define AARCH64_SIGN_LINK_REGISTER AARCH64_VALID_CALL_TARGET
  173. # else
  174. # define AARCH64_SIGN_LINK_REGISTER
  175. # endif
  176. # define AARCH64_VALIDATE_LINK_REGISTER
  177. # endif
  178. # if GNU_PROPERTY_AARCH64_POINTER_AUTH != 0 || GNU_PROPERTY_AARCH64_BTI != 0
  179. .pushsection .note.gnu.property, "a";
  180. .balign 8;
  181. .long 4;
  182. .long 0x10;
  183. .long 0x5;
  184. .asciz "GNU";
  185. .long 0xc0000000; /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */
  186. .long 4;
  187. .long (GNU_PROPERTY_AARCH64_POINTER_AUTH | GNU_PROPERTY_AARCH64_BTI);
  188. .long 0;
  189. .popsection;
  190. # endif
  191. # endif /* defined __ASSEMBLER__ */
  192. # define IS_CPU_SUPPORT_UNROLL8_EOR3() \
  193. (OPENSSL_armcap_P & ARMV8_UNROLL8_EOR3)
  194. #endif