arm_arch.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* ====================================================================
  2. * Copyright (c) 1998-2011 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #ifndef OPENSSL_HEADER_ARM_ARCH_H
  53. #define OPENSSL_HEADER_ARM_ARCH_H
  54. // arm_arch.h contains symbols used by ARM assembly, and the C code that calls
  55. // it. It is included as a public header to simplify the build, but is not
  56. // intended for external use.
  57. #if defined(__ARMEL__) || defined(_M_ARM) || defined(__AARCH64EL__) || \
  58. defined(_M_ARM64)
  59. // ARMV7_NEON is true when a NEON unit is present in the current CPU.
  60. #define ARMV7_NEON (1 << 0)
  61. // ARMV8_AES indicates support for hardware AES instructions.
  62. #define ARMV8_AES (1 << 2)
  63. // ARMV8_SHA1 indicates support for hardware SHA-1 instructions.
  64. #define ARMV8_SHA1 (1 << 3)
  65. // ARMV8_SHA256 indicates support for hardware SHA-256 instructions.
  66. #define ARMV8_SHA256 (1 << 4)
  67. // ARMV8_PMULL indicates support for carryless multiplication.
  68. #define ARMV8_PMULL (1 << 5)
  69. // ARMV8_SHA512 indicates support for hardware SHA-512 instructions.
  70. #define ARMV8_SHA512 (1 << 6)
  71. #if defined(__ASSEMBLER__)
  72. // We require the ARM assembler provide |__ARM_ARCH| from Arm C Language
  73. // Extensions (ACLE). This is supported in GCC 4.8+ and Clang 3.2+. MSVC does
  74. // not implement ACLE, but we require Clang's assembler on Windows.
  75. #if !defined(__ARM_ARCH)
  76. #error "ARM assembler must define __ARM_ARCH"
  77. #endif
  78. // __ARM_ARCH__ is used by OpenSSL assembly to determine the minimum target ARM
  79. // version.
  80. //
  81. // TODO(davidben): Switch the assembly to use |__ARM_ARCH| directly.
  82. #define __ARM_ARCH__ __ARM_ARCH
  83. // Even when building for 32-bit ARM, support for aarch64 crypto instructions
  84. // will be included.
  85. #define __ARM_MAX_ARCH__ 8
  86. // Support macros for
  87. // - Armv8.3-A Pointer Authentication and
  88. // - Armv8.5-A Branch Target Identification
  89. // features which require emitting a .note.gnu.property section with the
  90. // appropriate architecture-dependent feature bits set.
  91. //
  92. // |AARCH64_SIGN_LINK_REGISTER| and |AARCH64_VALIDATE_LINK_REGISTER| expand to
  93. // PACIxSP and AUTIxSP, respectively. |AARCH64_SIGN_LINK_REGISTER| should be
  94. // used immediately before saving the LR register (x30) to the stack.
  95. // |AARCH64_VALIDATE_LINK_REGISTER| should be used immediately after restoring
  96. // it. Note |AARCH64_SIGN_LINK_REGISTER|'s modifications to LR must be undone
  97. // with |AARCH64_VALIDATE_LINK_REGISTER| before RET. The SP register must also
  98. // have the same value at the two points. For example:
  99. //
  100. // .global f
  101. // f:
  102. // AARCH64_SIGN_LINK_REGISTER
  103. // stp x29, x30, [sp, #-96]!
  104. // mov x29, sp
  105. // ...
  106. // ldp x29, x30, [sp], #96
  107. // AARCH64_VALIDATE_LINK_REGISTER
  108. // ret
  109. //
  110. // |AARCH64_VALID_CALL_TARGET| expands to BTI 'c'. Either it, or
  111. // |AARCH64_SIGN_LINK_REGISTER|, must be used at every point that may be an
  112. // indirect call target. In particular, all symbols exported from a file must
  113. // begin with one of these macros. For example, a leaf function that does not
  114. // save LR can instead use |AARCH64_VALID_CALL_TARGET|:
  115. //
  116. // .globl return_zero
  117. // return_zero:
  118. // AARCH64_VALID_CALL_TARGET
  119. // mov x0, #0
  120. // ret
  121. //
  122. // A non-leaf function which does not immediately save LR may need both macros
  123. // because |AARCH64_SIGN_LINK_REGISTER| appears late. For example, the function
  124. // may jump to an alternate implementation before setting up the stack:
  125. //
  126. // .globl with_early_jump
  127. // with_early_jump:
  128. // AARCH64_VALID_CALL_TARGET
  129. // cmp x0, #128
  130. // b.lt .Lwith_early_jump_128
  131. // AARCH64_SIGN_LINK_REGISTER
  132. // stp x29, x30, [sp, #-96]!
  133. // mov x29, sp
  134. // ...
  135. // ldp x29, x30, [sp], #96
  136. // AARCH64_VALIDATE_LINK_REGISTER
  137. // ret
  138. //
  139. // .Lwith_early_jump_128:
  140. // ...
  141. // ret
  142. //
  143. // These annotations are only required with indirect calls. Private symbols that
  144. // are only the target of direct calls do not require annotations. Also note
  145. // that |AARCH64_VALID_CALL_TARGET| is only valid for indirect calls (BLR), not
  146. // indirect jumps (BR). Indirect jumps in assembly are currently not supported
  147. // and would require a macro for BTI 'j'.
  148. //
  149. // Although not necessary, it is safe to use these macros in 32-bit ARM
  150. // assembly. This may be used to simplify dual 32-bit and 64-bit files.
  151. //
  152. // References:
  153. // - "ELF for the Arm® 64-bit Architecture"
  154. // https://github.com/ARM-software/abi-aa/blob/master/aaelf64/aaelf64.rst
  155. // - "Providing protection for complex software"
  156. // https://developer.arm.com/architectures/learn-the-architecture/providing-protection-for-complex-software
  157. #if defined(__ARM_FEATURE_BTI_DEFAULT) && __ARM_FEATURE_BTI_DEFAULT == 1
  158. #define GNU_PROPERTY_AARCH64_BTI (1 << 0) // Has Branch Target Identification
  159. #define AARCH64_VALID_CALL_TARGET hint #34 // BTI 'c'
  160. #else
  161. #define GNU_PROPERTY_AARCH64_BTI 0 // No Branch Target Identification
  162. #define AARCH64_VALID_CALL_TARGET
  163. #endif
  164. #if defined(__ARM_FEATURE_PAC_DEFAULT) && \
  165. (__ARM_FEATURE_PAC_DEFAULT & 1) == 1 // Signed with A-key
  166. #define GNU_PROPERTY_AARCH64_POINTER_AUTH \
  167. (1 << 1) // Has Pointer Authentication
  168. #define AARCH64_SIGN_LINK_REGISTER hint #25 // PACIASP
  169. #define AARCH64_VALIDATE_LINK_REGISTER hint #29 // AUTIASP
  170. #elif defined(__ARM_FEATURE_PAC_DEFAULT) && \
  171. (__ARM_FEATURE_PAC_DEFAULT & 2) == 2 // Signed with B-key
  172. #define GNU_PROPERTY_AARCH64_POINTER_AUTH \
  173. (1 << 1) // Has Pointer Authentication
  174. #define AARCH64_SIGN_LINK_REGISTER hint #27 // PACIBSP
  175. #define AARCH64_VALIDATE_LINK_REGISTER hint #31 // AUTIBSP
  176. #else
  177. #define GNU_PROPERTY_AARCH64_POINTER_AUTH 0 // No Pointer Authentication
  178. #if GNU_PROPERTY_AARCH64_BTI != 0
  179. #define AARCH64_SIGN_LINK_REGISTER AARCH64_VALID_CALL_TARGET
  180. #else
  181. #define AARCH64_SIGN_LINK_REGISTER
  182. #endif
  183. #define AARCH64_VALIDATE_LINK_REGISTER
  184. #endif
  185. #if GNU_PROPERTY_AARCH64_POINTER_AUTH != 0 || GNU_PROPERTY_AARCH64_BTI != 0
  186. .pushsection .note.gnu.property, "a";
  187. .balign 8;
  188. .long 4;
  189. .long 0x10;
  190. .long 0x5;
  191. .asciz "GNU";
  192. .long 0xc0000000; /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */
  193. .long 4;
  194. .long (GNU_PROPERTY_AARCH64_POINTER_AUTH | GNU_PROPERTY_AARCH64_BTI);
  195. .long 0;
  196. .popsection;
  197. #endif
  198. #endif // __ASSEMBLER__
  199. #endif // __ARMEL__ || _M_ARM || __AARCH64EL__ || _M_ARM64
  200. #endif // OPENSSL_HEADER_ARM_ARCH_H