arm64cpuid.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2020 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. .long 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. .long 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. .long 0xce010800 // eor3 v0.16b, v0.16b, v1.16b, v2.16b
  89. ret
  90. .size _armv8_eor3_probe,.-_armv8_eor3_probe
  91. .globl _armv8_cpuid_probe
  92. .type _armv8_cpuid_probe,%function
  93. _armv8_cpuid_probe:
  94. AARCH64_VALID_CALL_TARGET
  95. mrs x0, midr_el1
  96. ret
  97. .size _armv8_cpuid_probe,.-_armv8_cpuid_probe
  98. .globl _armv8_sm3_probe
  99. .type _armv8_sm3_probe,%function
  100. _armv8_sm3_probe:
  101. AARCH64_VALID_CALL_TARGET
  102. .long 0xce63c004 // sm3partw1 v4.4s, v0.4s, v3.4s
  103. ret
  104. .size _armv8_sm3_probe,.-_armv8_sm3_probe
  105. .globl OPENSSL_cleanse
  106. .type OPENSSL_cleanse,%function
  107. .align 5
  108. OPENSSL_cleanse:
  109. AARCH64_VALID_CALL_TARGET
  110. cbz x1,.Lret // len==0?
  111. cmp x1,#15
  112. b.hi .Lot // len>15
  113. nop
  114. .Little:
  115. strb wzr,[x0],#1 // store byte-by-byte
  116. subs x1,x1,#1
  117. b.ne .Little
  118. .Lret: ret
  119. .align 4
  120. .Lot: tst x0,#7
  121. b.eq .Laligned // inp is aligned
  122. strb wzr,[x0],#1 // store byte-by-byte
  123. sub x1,x1,#1
  124. b .Lot
  125. .align 4
  126. .Laligned:
  127. str xzr,[x0],#8 // store word-by-word
  128. sub x1,x1,#8
  129. tst x1,#-8
  130. b.ne .Laligned // len>=8
  131. cbnz x1,.Little // len!=0?
  132. ret
  133. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  134. .globl CRYPTO_memcmp
  135. .type CRYPTO_memcmp,%function
  136. .align 4
  137. CRYPTO_memcmp:
  138. AARCH64_VALID_CALL_TARGET
  139. eor w3,w3,w3
  140. cbz x2,.Lno_data // len==0?
  141. cmp x2,#16
  142. b.ne .Loop_cmp
  143. ldp x8,x9,[x0]
  144. ldp x10,x11,[x1]
  145. eor x8,x8,x10
  146. eor x9,x9,x11
  147. orr x8,x8,x9
  148. mov x0,#1
  149. cmp x8,#0
  150. csel x0,xzr,x0,eq
  151. ret
  152. .align 4
  153. .Loop_cmp:
  154. ldrb w4,[x0],#1
  155. ldrb w5,[x1],#1
  156. eor w4,w4,w5
  157. orr w3,w3,w4
  158. subs x2,x2,#1
  159. b.ne .Loop_cmp
  160. .Lno_data:
  161. neg w0,w3
  162. lsr w0,w0,#31
  163. ret
  164. .size CRYPTO_memcmp,.-CRYPTO_memcmp
  165. .globl _armv8_rng_probe
  166. .type _armv8_rng_probe,%function
  167. _armv8_rng_probe:
  168. mrs x0, s3_3_c2_c4_0 // rndr
  169. mrs x0, s3_3_c2_c4_1 // rndrrs
  170. ret
  171. .size _armv8_rng_probe,.-_armv8_rng_probe
  172. ___
  173. sub gen_random {
  174. my $rdop = shift;
  175. my $rand_reg = $rdop eq "rndr" ? "s3_3_c2_c4_0" : "s3_3_c2_c4_1";
  176. print<<___;
  177. // Fill buffer with Randomly Generated Bytes
  178. // inputs: char * in x0 - Pointer to buffer
  179. // size_t in x1 - Number of bytes to write to buffer
  180. // outputs: size_t in x0 - Number of bytes successfully written to buffer
  181. .globl OPENSSL_${rdop}_asm
  182. .type OPENSSL_${rdop}_asm,%function
  183. .align 4
  184. OPENSSL_${rdop}_asm:
  185. mov x2,xzr
  186. mov x3,xzr
  187. .align 4
  188. .Loop_${rdop}:
  189. cmp x1,#0
  190. b.eq .${rdop}_done
  191. mov x3,xzr
  192. mrs x3,$rand_reg
  193. b.eq .${rdop}_done
  194. cmp x1,#8
  195. b.lt .Loop_single_byte_${rdop}
  196. str x3,[x0]
  197. add x0,x0,#8
  198. add x2,x2,#8
  199. subs x1,x1,#8
  200. b.ge .Loop_${rdop}
  201. .align 4
  202. .Loop_single_byte_${rdop}:
  203. strb w3,[x0]
  204. lsr x3,x3,#8
  205. add x2,x2,#1
  206. add x0,x0,#1
  207. subs x1,x1,#1
  208. b.gt .Loop_single_byte_${rdop}
  209. .align 4
  210. .${rdop}_done:
  211. mov x0,x2
  212. ret
  213. .size OPENSSL_${rdop}_asm,.-OPENSSL_${rdop}_asm
  214. ___
  215. }
  216. gen_random("rndr");
  217. gen_random("rndrrs");
  218. print $code;
  219. close STDOUT or die "error closing STDOUT: $!";