2
0

s390xcpuid.pl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #! /usr/bin/env perl
  2. # Copyright 2009-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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. $flavour = shift;
  9. if ($flavour =~ /3[12]/) {
  10. $SIZE_T=4;
  11. $g="";
  12. } else {
  13. $SIZE_T=8;
  14. $g="g";
  15. }
  16. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  17. open STDOUT,">$output";
  18. $ra="%r14";
  19. $sp="%r15";
  20. $stdframe=16*$SIZE_T+4*8;
  21. $code=<<___;
  22. #include "s390x_arch.h"
  23. .text
  24. .globl OPENSSL_s390x_facilities
  25. .type OPENSSL_s390x_facilities,\@function
  26. .align 16
  27. OPENSSL_s390x_facilities:
  28. lghi %r0,0
  29. larl %r4,OPENSSL_s390xcap_P
  30. stg %r0,S390X_STFLE+8(%r4) # wipe capability vectors
  31. stg %r0,S390X_STFLE+16(%r4)
  32. stg %r0,S390X_STFLE+24(%r4)
  33. stg %r0,S390X_KIMD(%r4)
  34. stg %r0,S390X_KIMD+8(%r4)
  35. stg %r0,S390X_KLMD(%r4)
  36. stg %r0,S390X_KLMD+8(%r4)
  37. stg %r0,S390X_KM(%r4)
  38. stg %r0,S390X_KM+8(%r4)
  39. stg %r0,S390X_KMC(%r4)
  40. stg %r0,S390X_KMC+8(%r4)
  41. stg %r0,S390X_KMAC(%r4)
  42. stg %r0,S390X_KMAC+8(%r4)
  43. stg %r0,S390X_KMCTR(%r4)
  44. stg %r0,S390X_KMCTR+8(%r4)
  45. stg %r0,S390X_KMO(%r4)
  46. stg %r0,S390X_KMO+8(%r4)
  47. stg %r0,S390X_KMF(%r4)
  48. stg %r0,S390X_KMF+8(%r4)
  49. stg %r0,S390X_PRNO(%r4)
  50. stg %r0,S390X_PRNO+8(%r4)
  51. stg %r0,S390X_KMA(%r4)
  52. stg %r0,S390X_KMA+8(%r4)
  53. .long 0xb2b04000 # stfle 0(%r4)
  54. brc 8,.Ldone
  55. lghi %r0,1
  56. .long 0xb2b04000 # stfle 0(%r4)
  57. brc 8,.Ldone
  58. lghi %r0,2
  59. .long 0xb2b04000 # stfle 0(%r4)
  60. .Ldone:
  61. lmg %r2,%r3,S390X_STFLE(%r4)
  62. tmhl %r2,0x4000 # check for message-security-assist
  63. jz .Lret
  64. lghi %r0,S390X_QUERY # query kimd capabilities
  65. la %r1,S390X_KIMD(%r4)
  66. .long 0xb93e0002 # kimd %r0,%r2
  67. lghi %r0,S390X_QUERY # query klmd capabilities
  68. la %r1,S390X_KLMD(%r4)
  69. .long 0xb93f0002 # klmd %r0,%r2
  70. lghi %r0,S390X_QUERY # query km capability vector
  71. la %r1,S390X_KM(%r4)
  72. .long 0xb92e0042 # km %r4,%r2
  73. lghi %r0,S390X_QUERY # query kmc capability vector
  74. la %r1,S390X_KMC(%r4)
  75. .long 0xb92f0042 # kmc %r4,%r2
  76. lghi %r0,S390X_QUERY # query kmac capability vector
  77. la %r1,S390X_KMAC(%r4)
  78. .long 0xb91e0042 # kmac %r4,%r2
  79. tmhh %r3,0x0004 # check for message-security-assist-4
  80. jz .Lret
  81. lghi %r0,S390X_QUERY # query kmctr capability vector
  82. la %r1,S390X_KMCTR(%r4)
  83. .long 0xb92d2042 # kmctr %r4,%r2,%r2
  84. lghi %r0,S390X_QUERY # query kmo capability vector
  85. la %r1,S390X_KMO(%r4)
  86. .long 0xb92b0042 # kmo %r4,%r2
  87. lghi %r0,S390X_QUERY # query kmf capability vector
  88. la %r1,S390X_KMF(%r4)
  89. .long 0xb92a0042 # kmf %r4,%r2
  90. tml %r2,0x40 # check for message-security-assist-5
  91. jz .Lret
  92. lghi %r0,S390X_QUERY # query prno capability vector
  93. la %r1,S390X_PRNO(%r4)
  94. .long 0xb93c0042 # prno %r4,%r2
  95. lg %r2,S390X_STFLE+16(%r4)
  96. tmhl %r2,0x2000 # check for message-security-assist-8
  97. jz .Lret
  98. lghi %r0,S390X_QUERY # query kma capability vector
  99. la %r1,S390X_KMA(%r4)
  100. .long 0xb9294022 # kma %r2,%r4,%r2
  101. .Lret:
  102. br $ra
  103. .size OPENSSL_s390x_facilities,.-OPENSSL_s390x_facilities
  104. .globl OPENSSL_rdtsc
  105. .type OPENSSL_rdtsc,\@function
  106. .align 16
  107. OPENSSL_rdtsc:
  108. larl %r4,OPENSSL_s390xcap_P
  109. tm S390X_STFLE+3(%r4),0x40 # check for store-clock-fast facility
  110. jz .Lstck
  111. .long 0xb27cf010 # stckf 16($sp)
  112. lg %r2,16($sp)
  113. br $ra
  114. .Lstck:
  115. stck 16($sp)
  116. lg %r2,16($sp)
  117. br $ra
  118. .size OPENSSL_rdtsc,.-OPENSSL_rdtsc
  119. .globl OPENSSL_atomic_add
  120. .type OPENSSL_atomic_add,\@function
  121. .align 16
  122. OPENSSL_atomic_add:
  123. l %r1,0(%r2)
  124. .Lspin: lr %r0,%r1
  125. ar %r0,%r3
  126. cs %r1,%r0,0(%r2)
  127. brc 4,.Lspin
  128. lgfr %r2,%r0 # OpenSSL expects the new value
  129. br $ra
  130. .size OPENSSL_atomic_add,.-OPENSSL_atomic_add
  131. .globl OPENSSL_wipe_cpu
  132. .type OPENSSL_wipe_cpu,\@function
  133. .align 16
  134. OPENSSL_wipe_cpu:
  135. xgr %r0,%r0
  136. xgr %r1,%r1
  137. lgr %r2,$sp
  138. xgr %r3,%r3
  139. xgr %r4,%r4
  140. lzdr %f0
  141. lzdr %f1
  142. lzdr %f2
  143. lzdr %f3
  144. lzdr %f4
  145. lzdr %f5
  146. lzdr %f6
  147. lzdr %f7
  148. br $ra
  149. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  150. .globl OPENSSL_cleanse
  151. .type OPENSSL_cleanse,\@function
  152. .align 16
  153. OPENSSL_cleanse:
  154. #if !defined(__s390x__) && !defined(__s390x)
  155. llgfr %r3,%r3
  156. #endif
  157. lghi %r4,15
  158. lghi %r0,0
  159. clgr %r3,%r4
  160. jh .Lot
  161. clgr %r3,%r0
  162. bcr 8,%r14
  163. .Little:
  164. stc %r0,0(%r2)
  165. la %r2,1(%r2)
  166. brctg %r3,.Little
  167. br %r14
  168. .align 4
  169. .Lot: tmll %r2,7
  170. jz .Laligned
  171. stc %r0,0(%r2)
  172. la %r2,1(%r2)
  173. brctg %r3,.Lot
  174. .Laligned:
  175. srlg %r4,%r3,3
  176. .Loop: stg %r0,0(%r2)
  177. la %r2,8(%r2)
  178. brctg %r4,.Loop
  179. lghi %r4,7
  180. ngr %r3,%r4
  181. jnz .Little
  182. br $ra
  183. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  184. .globl CRYPTO_memcmp
  185. .type CRYPTO_memcmp,\@function
  186. .align 16
  187. CRYPTO_memcmp:
  188. #if !defined(__s390x__) && !defined(__s390x)
  189. llgfr %r4,%r4
  190. #endif
  191. lghi %r5,0
  192. clgr %r4,%r5
  193. je .Lno_data
  194. .Loop_cmp:
  195. llgc %r0,0(%r2)
  196. la %r2,1(%r2)
  197. llgc %r1,0(%r3)
  198. la %r3,1(%r3)
  199. xr %r1,%r0
  200. or %r5,%r1
  201. brctg %r4,.Loop_cmp
  202. lnr %r5,%r5
  203. srl %r5,31
  204. .Lno_data:
  205. lgr %r2,%r5
  206. br $ra
  207. .size CRYPTO_memcmp,.-CRYPTO_memcmp
  208. .globl OPENSSL_instrument_bus
  209. .type OPENSSL_instrument_bus,\@function
  210. .align 16
  211. OPENSSL_instrument_bus:
  212. lghi %r2,0
  213. br %r14
  214. .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
  215. .globl OPENSSL_instrument_bus2
  216. .type OPENSSL_instrument_bus2,\@function
  217. .align 16
  218. OPENSSL_instrument_bus2:
  219. lghi %r2,0
  220. br $ra
  221. .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
  222. .globl OPENSSL_vx_probe
  223. .type OPENSSL_vx_probe,\@function
  224. .align 16
  225. OPENSSL_vx_probe:
  226. .word 0xe700,0x0000,0x0044 # vzero %v0
  227. br $ra
  228. .size OPENSSL_vx_probe,.-OPENSSL_vx_probe
  229. ___
  230. {
  231. ################
  232. # void s390x_kimd(const unsigned char *in, size_t len, unsigned int fc,
  233. # void *param)
  234. my ($in,$len,$fc,$param) = map("%r$_",(2..5));
  235. $code.=<<___;
  236. .globl s390x_kimd
  237. .type s390x_kimd,\@function
  238. .align 16
  239. s390x_kimd:
  240. llgfr %r0,$fc
  241. lgr %r1,$param
  242. .long 0xb93e0002 # kimd %r0,%r2
  243. brc 1,.-4 # pay attention to "partial completion"
  244. br $ra
  245. .size s390x_kimd,.-s390x_kimd
  246. ___
  247. }
  248. {
  249. ################
  250. # void s390x_klmd(const unsigned char *in, size_t inlen, unsigned char *out,
  251. # size_t outlen, unsigned int fc, void *param)
  252. my ($in,$inlen,$out,$outlen,$fc) = map("%r$_",(2..6));
  253. $code.=<<___;
  254. .globl s390x_klmd
  255. .type s390x_klmd,\@function
  256. .align 32
  257. s390x_klmd:
  258. llgfr %r0,$fc
  259. l${g} %r1,$stdframe($sp)
  260. .long 0xb93f0042 # klmd %r4,%r2
  261. brc 1,.-4 # pay attention to "partial completion"
  262. br $ra
  263. .size s390x_klmd,.-s390x_klmd
  264. ___
  265. }
  266. ################
  267. # void s390x_km(const unsigned char *in, size_t len, unsigned char *out,
  268. # unsigned int fc, void *param)
  269. {
  270. my ($in,$len,$out,$fc,$param) = map("%r$_",(2..6));
  271. $code.=<<___;
  272. .globl s390x_km
  273. .type s390x_km,\@function
  274. .align 16
  275. s390x_km:
  276. lr %r0,$fc
  277. l${g}r %r1,$param
  278. .long 0xb92e0042 # km $out,$in
  279. brc 1,.-4 # pay attention to "partial completion"
  280. br $ra
  281. .size s390x_km,.-s390x_km
  282. ___
  283. }
  284. ################
  285. # void s390x_kmac(const unsigned char *in, size_t len, unsigned int fc,
  286. # void *param)
  287. {
  288. my ($in,$len,$fc,$param) = map("%r$_",(2..5));
  289. $code.=<<___;
  290. .globl s390x_kmac
  291. .type s390x_kmac,\@function
  292. .align 16
  293. s390x_kmac:
  294. lr %r0,$fc
  295. l${g}r %r1,$param
  296. .long 0xb91e0002 # kmac %r0,$in
  297. brc 1,.-4 # pay attention to "partial completion"
  298. br $ra
  299. .size s390x_kmac,.-s390x_kmac
  300. ___
  301. }
  302. ################
  303. # void s390x_kmo(const unsigned char *in, size_t len, unsigned char *out,
  304. # unsigned int fc, void *param)
  305. {
  306. my ($in,$len,$out,$fc,$param) = map("%r$_",(2..6));
  307. $code.=<<___;
  308. .globl s390x_kmo
  309. .type s390x_kmo,\@function
  310. .align 16
  311. s390x_kmo:
  312. lr %r0,$fc
  313. l${g}r %r1,$param
  314. .long 0xb92b0042 # kmo $out,$in
  315. brc 1,.-4 # pay attention to "partial completion"
  316. br $ra
  317. .size s390x_kmo,.-s390x_kmo
  318. ___
  319. }
  320. ################
  321. # void s390x_kmf(const unsigned char *in, size_t len, unsigned char *out,
  322. # unsigned int fc, void *param)
  323. {
  324. my ($in,$len,$out,$fc,$param) = map("%r$_",(2..6));
  325. $code.=<<___;
  326. .globl s390x_kmf
  327. .type s390x_kmf,\@function
  328. .align 16
  329. s390x_kmf:
  330. lr %r0,$fc
  331. l${g}r %r1,$param
  332. .long 0xb92a0042 # kmf $out,$in
  333. brc 1,.-4 # pay attention to "partial completion"
  334. br $ra
  335. .size s390x_kmf,.-s390x_kmf
  336. ___
  337. }
  338. ################
  339. # void s390x_kma(const unsigned char *aad, size_t alen,
  340. # const unsigned char *in, size_t len,
  341. # unsigned char *out, unsigned int fc, void *param)
  342. {
  343. my ($aad,$alen,$in,$len,$out) = map("%r$_",(2..6));
  344. $code.=<<___;
  345. .globl s390x_kma
  346. .type s390x_kma,\@function
  347. .align 16
  348. s390x_kma:
  349. st${g} $out,6*$SIZE_T($sp)
  350. lm${g} %r0,%r1,$stdframe($sp)
  351. .long 0xb9292064 # kma $out,$aad,$in
  352. brc 1,.-4 # pay attention to "partial completion"
  353. l${g} $out,6*$SIZE_T($sp)
  354. br $ra
  355. .size s390x_kma,.-s390x_kma
  356. ___
  357. }
  358. $code.=<<___;
  359. .section .init
  360. brasl $ra,OPENSSL_cpuid_setup
  361. ___
  362. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  363. print $code;
  364. close STDOUT or die "error closing STDOUT: $!"; # force flush