arm64cpuid.pl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2022 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. # $output is the last argument if it looks like a file (it has an extension)
  9. # $flavour is the first argument if it doesn't look like a file
  10. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  11. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  12. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  13. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  14. ( $xlate="${dir}perlasm/arm-xlate.pl" and -f $xlate) or
  15. die "can't locate arm-xlate.pl";
  16. open OUT,"| \"$^X\" $xlate $flavour \"$output\""
  17. or die "can't call $xlate: $!";
  18. *STDOUT=*OUT;
  19. $code.=<<___;
  20. #include "arm_arch.h"
  21. .text
  22. .arch armv8-a+crypto
  23. .align 5
  24. .globl _armv7_neon_probe
  25. .type _armv7_neon_probe,%function
  26. _armv7_neon_probe:
  27. AARCH64_VALID_CALL_TARGET
  28. orr v15.16b, v15.16b, v15.16b
  29. ret
  30. .size _armv7_neon_probe,.-_armv7_neon_probe
  31. .globl _armv7_tick
  32. .type _armv7_tick,%function
  33. _armv7_tick:
  34. AARCH64_VALID_CALL_TARGET
  35. #ifdef __APPLE__
  36. mrs x0, CNTPCT_EL0
  37. #else
  38. mrs x0, CNTVCT_EL0
  39. #endif
  40. ret
  41. .size _armv7_tick,.-_armv7_tick
  42. .globl _armv8_aes_probe
  43. .type _armv8_aes_probe,%function
  44. _armv8_aes_probe:
  45. AARCH64_VALID_CALL_TARGET
  46. aese v0.16b, v0.16b
  47. ret
  48. .size _armv8_aes_probe,.-_armv8_aes_probe
  49. .globl _armv8_sha1_probe
  50. .type _armv8_sha1_probe,%function
  51. _armv8_sha1_probe:
  52. AARCH64_VALID_CALL_TARGET
  53. sha1h s0, s0
  54. ret
  55. .size _armv8_sha1_probe,.-_armv8_sha1_probe
  56. .globl _armv8_sha256_probe
  57. .type _armv8_sha256_probe,%function
  58. _armv8_sha256_probe:
  59. AARCH64_VALID_CALL_TARGET
  60. sha256su0 v0.4s, v0.4s
  61. ret
  62. .size _armv8_sha256_probe,.-_armv8_sha256_probe
  63. .globl _armv8_pmull_probe
  64. .type _armv8_pmull_probe,%function
  65. _armv8_pmull_probe:
  66. AARCH64_VALID_CALL_TARGET
  67. pmull v0.1q, v0.1d, v0.1d
  68. ret
  69. .size _armv8_pmull_probe,.-_armv8_pmull_probe
  70. .globl _armv8_sm4_probe
  71. .type _armv8_sm4_probe,%function
  72. _armv8_sm4_probe:
  73. AARCH64_VALID_CALL_TARGET
  74. .inst 0xcec08400 // sm4e v0.4s, v0.4s
  75. ret
  76. .size _armv8_sm4_probe,.-_armv8_sm4_probe
  77. .globl _armv8_sha512_probe
  78. .type _armv8_sha512_probe,%function
  79. _armv8_sha512_probe:
  80. AARCH64_VALID_CALL_TARGET
  81. .inst 0xcec08000 // sha512su0 v0.2d,v0.2d
  82. ret
  83. .size _armv8_sha512_probe,.-_armv8_sha512_probe
  84. .globl _armv8_eor3_probe
  85. .type _armv8_eor3_probe,%function
  86. _armv8_eor3_probe:
  87. AARCH64_VALID_CALL_TARGET
  88. .inst 0xce010800 // eor3 v0.16b, v0.16b, v1.16b, v2.16b
  89. ret
  90. .size _armv8_eor3_probe,.-_armv8_eor3_probe
  91. .globl _armv8_sve_probe
  92. .type _armv8_sve_probe,%function
  93. _armv8_sve_probe:
  94. AARCH64_VALID_CALL_TARGET
  95. .inst 0x04a03000 // eor z0.d,z0.d,z0.d
  96. ret
  97. .size _armv8_sve_probe,.-_armv8_sve_probe
  98. .globl _armv8_sve2_probe
  99. .type _armv8_sve2_probe,%function
  100. _armv8_sve2_probe:
  101. AARCH64_VALID_CALL_TARGET
  102. .inst 0x04e03400 // xar z0.d,z0.d,z0.d
  103. ret
  104. .size _armv8_sve2_probe,.-_armv8_sve2_probe
  105. .globl _armv8_cpuid_probe
  106. .type _armv8_cpuid_probe,%function
  107. _armv8_cpuid_probe:
  108. AARCH64_VALID_CALL_TARGET
  109. mrs x0, midr_el1
  110. ret
  111. .size _armv8_cpuid_probe,.-_armv8_cpuid_probe
  112. .globl _armv8_sm3_probe
  113. .type _armv8_sm3_probe,%function
  114. _armv8_sm3_probe:
  115. AARCH64_VALID_CALL_TARGET
  116. .inst 0xce63c004 // sm3partw1 v4.4s, v0.4s, v3.4s
  117. ret
  118. .size _armv8_sm3_probe,.-_armv8_sm3_probe
  119. .globl OPENSSL_cleanse
  120. .type OPENSSL_cleanse,%function
  121. .align 5
  122. OPENSSL_cleanse:
  123. AARCH64_VALID_CALL_TARGET
  124. cbz x1,.Lret // len==0?
  125. cmp x1,#15
  126. b.hi .Lot // len>15
  127. nop
  128. .Little:
  129. strb wzr,[x0],#1 // store byte-by-byte
  130. subs x1,x1,#1
  131. b.ne .Little
  132. .Lret: ret
  133. .align 4
  134. .Lot: tst x0,#7
  135. b.eq .Laligned // inp is aligned
  136. strb wzr,[x0],#1 // store byte-by-byte
  137. sub x1,x1,#1
  138. b .Lot
  139. .align 4
  140. .Laligned:
  141. str xzr,[x0],#8 // store word-by-word
  142. sub x1,x1,#8
  143. tst x1,#-8
  144. b.ne .Laligned // len>=8
  145. cbnz x1,.Little // len!=0?
  146. ret
  147. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  148. .globl CRYPTO_memcmp
  149. .type CRYPTO_memcmp,%function
  150. .align 4
  151. CRYPTO_memcmp:
  152. AARCH64_VALID_CALL_TARGET
  153. eor w3,w3,w3
  154. cbz x2,.Lno_data // len==0?
  155. cmp x2,#16
  156. b.ne .Loop_cmp
  157. ldp x8,x9,[x0]
  158. ldp x10,x11,[x1]
  159. eor x8,x8,x10
  160. eor x9,x9,x11
  161. orr x8,x8,x9
  162. mov x0,#1
  163. cmp x8,#0
  164. csel x0,xzr,x0,eq
  165. ret
  166. .align 4
  167. .Loop_cmp:
  168. ldrb w4,[x0],#1
  169. ldrb w5,[x1],#1
  170. eor w4,w4,w5
  171. orr w3,w3,w4
  172. subs x2,x2,#1
  173. b.ne .Loop_cmp
  174. .Lno_data:
  175. neg w0,w3
  176. lsr w0,w0,#31
  177. ret
  178. .size CRYPTO_memcmp,.-CRYPTO_memcmp
  179. .globl _armv8_rng_probe
  180. .type _armv8_rng_probe,%function
  181. _armv8_rng_probe:
  182. AARCH64_VALID_CALL_TARGET
  183. mrs x0, s3_3_c2_c4_0 // rndr
  184. mrs x0, s3_3_c2_c4_1 // rndrrs
  185. ret
  186. .size _armv8_rng_probe,.-_armv8_rng_probe
  187. ___
  188. sub gen_random {
  189. my $rdop = shift;
  190. my $rand_reg = $rdop eq "rndr" ? "s3_3_c2_c4_0" : "s3_3_c2_c4_1";
  191. return <<___;
  192. // Fill buffer with Randomly Generated Bytes
  193. // inputs: char * in x0 - Pointer to buffer
  194. // size_t in x1 - Number of bytes to write to buffer
  195. // outputs: size_t in x0 - Number of bytes successfully written to buffer
  196. .globl OPENSSL_${rdop}_asm
  197. .type OPENSSL_${rdop}_asm,%function
  198. .align 4
  199. OPENSSL_${rdop}_asm:
  200. AARCH64_VALID_CALL_TARGET
  201. mov x2,xzr
  202. mov x3,xzr
  203. .align 4
  204. .Loop_${rdop}:
  205. cmp x1,#0
  206. b.eq .${rdop}_done
  207. mov x3,xzr
  208. mrs x3,$rand_reg
  209. b.eq .${rdop}_done
  210. cmp x1,#8
  211. b.lt .Loop_single_byte_${rdop}
  212. str x3,[x0]
  213. add x0,x0,#8
  214. add x2,x2,#8
  215. subs x1,x1,#8
  216. b.ge .Loop_${rdop}
  217. .align 4
  218. .Loop_single_byte_${rdop}:
  219. strb w3,[x0]
  220. lsr x3,x3,#8
  221. add x2,x2,#1
  222. add x0,x0,#1
  223. subs x1,x1,#1
  224. b.gt .Loop_single_byte_${rdop}
  225. .align 4
  226. .${rdop}_done:
  227. mov x0,x2
  228. ret
  229. .size OPENSSL_${rdop}_asm,.-OPENSSL_${rdop}_asm
  230. ___
  231. }
  232. $code .= gen_random("rndr");
  233. $code .= gen_random("rndrrs");
  234. print $code;
  235. close STDOUT or die "error closing STDOUT: $!";