s390xcpuid.pl 8.7 KB

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