s390xcpuid.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. #! /usr/bin/env perl
  2. # Copyright 2009-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. if ($flavour =~ /3[12]/) {
  13. $SIZE_T=4;
  14. $g="";
  15. } else {
  16. $SIZE_T=8;
  17. $g="g";
  18. }
  19. $output and open STDOUT,">$output";
  20. $ra="%r14";
  21. $sp="%r15";
  22. $stdframe=16*$SIZE_T+4*8;
  23. $code=<<___;
  24. #include "s390x_arch.h"
  25. .text
  26. .globl OPENSSL_s390x_facilities
  27. .type OPENSSL_s390x_facilities,\@function
  28. .align 16
  29. OPENSSL_s390x_facilities:
  30. lghi %r0,0
  31. larl %r4,OPENSSL_s390xcap_P
  32. stg %r0,S390X_STFLE+8(%r4) # wipe capability vectors
  33. stg %r0,S390X_STFLE+16(%r4)
  34. stg %r0,S390X_STFLE+24(%r4)
  35. .long 0xb2b04000 # stfle 0(%r4)
  36. brc 8,.Ldone
  37. lghi %r0,1
  38. .long 0xb2b04000 # stfle 0(%r4)
  39. brc 8,.Ldone
  40. lghi %r0,2
  41. .long 0xb2b04000 # stfle 0(%r4)
  42. .Ldone:
  43. br $ra
  44. .size OPENSSL_s390x_facilities,.-OPENSSL_s390x_facilities
  45. .globl OPENSSL_s390x_functions
  46. .type OPENSSL_s390x_functions,\@function
  47. .align 16
  48. OPENSSL_s390x_functions:
  49. lghi %r0,0
  50. larl %r4,OPENSSL_s390xcap_P
  51. stg %r0,S390X_KIMD(%r4) # wipe capability vectors
  52. stg %r0,S390X_KIMD+8(%r4)
  53. stg %r0,S390X_KLMD(%r4)
  54. stg %r0,S390X_KLMD+8(%r4)
  55. stg %r0,S390X_KM(%r4)
  56. stg %r0,S390X_KM+8(%r4)
  57. stg %r0,S390X_KMC(%r4)
  58. stg %r0,S390X_KMC+8(%r4)
  59. stg %r0,S390X_KMAC(%r4)
  60. stg %r0,S390X_KMAC+8(%r4)
  61. stg %r0,S390X_KMCTR(%r4)
  62. stg %r0,S390X_KMCTR+8(%r4)
  63. stg %r0,S390X_KMO(%r4)
  64. stg %r0,S390X_KMO+8(%r4)
  65. stg %r0,S390X_KMF(%r4)
  66. stg %r0,S390X_KMF+8(%r4)
  67. stg %r0,S390X_PRNO(%r4)
  68. stg %r0,S390X_PRNO+8(%r4)
  69. stg %r0,S390X_KMA(%r4)
  70. stg %r0,S390X_KMA+8(%r4)
  71. stg %r0,S390X_PCC(%r4)
  72. stg %r0,S390X_PCC+8(%r4)
  73. stg %r0,S390X_KDSA(%r4)
  74. stg %r0,S390X_KDSA+8(%r4)
  75. lmg %r2,%r3,S390X_STFLE(%r4)
  76. tmhl %r2,0x4000 # check for message-security-assist
  77. jz .Lret
  78. lghi %r0,S390X_QUERY # query kimd capabilities
  79. la %r1,S390X_KIMD(%r4)
  80. .long 0xb93e0002 # kimd %r0,%r2
  81. lghi %r0,S390X_QUERY # query klmd capabilities
  82. la %r1,S390X_KLMD(%r4)
  83. .long 0xb93f0002 # klmd %r0,%r2
  84. lghi %r0,S390X_QUERY # query km capability vector
  85. la %r1,S390X_KM(%r4)
  86. .long 0xb92e0042 # km %r4,%r2
  87. lghi %r0,S390X_QUERY # query kmc capability vector
  88. la %r1,S390X_KMC(%r4)
  89. .long 0xb92f0042 # kmc %r4,%r2
  90. lghi %r0,S390X_QUERY # query kmac capability vector
  91. la %r1,S390X_KMAC(%r4)
  92. .long 0xb91e0042 # kmac %r4,%r2
  93. tmhh %r3,0x0008 # check for message-security-assist-3
  94. jz .Lret
  95. lghi %r0,S390X_QUERY # query pcc capability vector
  96. la %r1,S390X_PCC(%r4)
  97. .long 0xb92c0000 # pcc
  98. tmhh %r3,0x0004 # check for message-security-assist-4
  99. jz .Lret
  100. lghi %r0,S390X_QUERY # query kmctr capability vector
  101. la %r1,S390X_KMCTR(%r4)
  102. .long 0xb92d2042 # kmctr %r4,%r2,%r2
  103. lghi %r0,S390X_QUERY # query kmo capability vector
  104. la %r1,S390X_KMO(%r4)
  105. .long 0xb92b0042 # kmo %r4,%r2
  106. lghi %r0,S390X_QUERY # query kmf capability vector
  107. la %r1,S390X_KMF(%r4)
  108. .long 0xb92a0042 # kmf %r4,%r2
  109. tml %r2,0x40 # check for message-security-assist-5
  110. jz .Lret
  111. lghi %r0,S390X_QUERY # query prno capability vector
  112. la %r1,S390X_PRNO(%r4)
  113. .long 0xb93c0042 # prno %r4,%r2
  114. lg %r2,S390X_STFLE+16(%r4)
  115. tmhl %r2,0x2000 # check for message-security-assist-8
  116. jz .Lret
  117. lghi %r0,S390X_QUERY # query kma capability vector
  118. la %r1,S390X_KMA(%r4)
  119. .long 0xb9294022 # kma %r2,%r4,%r2
  120. tmhl %r2,0x0010 # check for message-security-assist-9
  121. jz .Lret
  122. lghi %r0,S390X_QUERY # query kdsa capability vector
  123. la %r1,S390X_KDSA(%r4)
  124. .long 0xb93a0002 # kdsa %r0,%r2
  125. .Lret:
  126. br $ra
  127. .size OPENSSL_s390x_functions,.-OPENSSL_s390x_functions
  128. .globl OPENSSL_rdtsc
  129. .type OPENSSL_rdtsc,\@function
  130. .align 16
  131. OPENSSL_rdtsc:
  132. larl %r4,OPENSSL_s390xcap_P
  133. tm S390X_STFLE+3(%r4),0x40 # check for store-clock-fast facility
  134. jz .Lstck
  135. .long 0xb27cf010 # stckf 16($sp)
  136. lg %r2,16($sp)
  137. br $ra
  138. .Lstck:
  139. stck 16($sp)
  140. lg %r2,16($sp)
  141. br $ra
  142. .size OPENSSL_rdtsc,.-OPENSSL_rdtsc
  143. .globl OPENSSL_atomic_add
  144. .type OPENSSL_atomic_add,\@function
  145. .align 16
  146. OPENSSL_atomic_add:
  147. l %r1,0(%r2)
  148. .Lspin: lr %r0,%r1
  149. ar %r0,%r3
  150. cs %r1,%r0,0(%r2)
  151. brc 4,.Lspin
  152. lgfr %r2,%r0 # OpenSSL expects the new value
  153. br $ra
  154. .size OPENSSL_atomic_add,.-OPENSSL_atomic_add
  155. .globl OPENSSL_wipe_cpu
  156. .type OPENSSL_wipe_cpu,\@function
  157. .align 16
  158. OPENSSL_wipe_cpu:
  159. xgr %r0,%r0
  160. xgr %r1,%r1
  161. lgr %r2,$sp
  162. xgr %r3,%r3
  163. xgr %r4,%r4
  164. lzdr %f0
  165. lzdr %f1
  166. lzdr %f2
  167. lzdr %f3
  168. lzdr %f4
  169. lzdr %f5
  170. lzdr %f6
  171. lzdr %f7
  172. br $ra
  173. .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
  174. .globl OPENSSL_cleanse
  175. .type OPENSSL_cleanse,\@function
  176. .align 16
  177. OPENSSL_cleanse:
  178. #if !defined(__s390x__) && !defined(__s390x)
  179. llgfr %r3,%r3
  180. #endif
  181. lghi %r4,15
  182. lghi %r0,0
  183. clgr %r3,%r4
  184. jh .Lot
  185. clgr %r3,%r0
  186. bcr 8,%r14
  187. .Little:
  188. stc %r0,0(%r2)
  189. la %r2,1(%r2)
  190. brctg %r3,.Little
  191. br %r14
  192. .align 4
  193. .Lot: tmll %r2,7
  194. jz .Laligned
  195. stc %r0,0(%r2)
  196. la %r2,1(%r2)
  197. brctg %r3,.Lot
  198. .Laligned:
  199. srlg %r4,%r3,3
  200. .Loop: stg %r0,0(%r2)
  201. la %r2,8(%r2)
  202. brctg %r4,.Loop
  203. lghi %r4,7
  204. ngr %r3,%r4
  205. jnz .Little
  206. br $ra
  207. .size OPENSSL_cleanse,.-OPENSSL_cleanse
  208. .globl CRYPTO_memcmp
  209. .type CRYPTO_memcmp,\@function
  210. .align 16
  211. CRYPTO_memcmp:
  212. #if !defined(__s390x__) && !defined(__s390x)
  213. llgfr %r4,%r4
  214. #endif
  215. lghi %r5,0
  216. clgr %r4,%r5
  217. je .Lno_data
  218. .Loop_cmp:
  219. llgc %r0,0(%r2)
  220. la %r2,1(%r2)
  221. llgc %r1,0(%r3)
  222. la %r3,1(%r3)
  223. xr %r1,%r0
  224. or %r5,%r1
  225. brctg %r4,.Loop_cmp
  226. lnr %r5,%r5
  227. srl %r5,31
  228. .Lno_data:
  229. lgr %r2,%r5
  230. br $ra
  231. .size CRYPTO_memcmp,.-CRYPTO_memcmp
  232. .globl OPENSSL_instrument_bus
  233. .type OPENSSL_instrument_bus,\@function
  234. .align 16
  235. OPENSSL_instrument_bus:
  236. lghi %r2,0
  237. br %r14
  238. .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
  239. .globl OPENSSL_instrument_bus2
  240. .type OPENSSL_instrument_bus2,\@function
  241. .align 16
  242. OPENSSL_instrument_bus2:
  243. lghi %r2,0
  244. br $ra
  245. .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
  246. .globl OPENSSL_vx_probe
  247. .type OPENSSL_vx_probe,\@function
  248. .align 16
  249. OPENSSL_vx_probe:
  250. .word 0xe700,0x0000,0x0044 # vzero %v0
  251. br $ra
  252. .size OPENSSL_vx_probe,.-OPENSSL_vx_probe
  253. ___
  254. {
  255. ################
  256. # void s390x_kimd(const unsigned char *in, size_t len, unsigned int fc,
  257. # void *param)
  258. my ($in,$len,$fc,$param) = map("%r$_",(2..5));
  259. $code.=<<___;
  260. .globl s390x_kimd
  261. .type s390x_kimd,\@function
  262. .align 16
  263. s390x_kimd:
  264. llgfr %r0,$fc
  265. lgr %r1,$param
  266. .long 0xb93e0002 # kimd %r0,%r2
  267. brc 1,.-4 # pay attention to "partial completion"
  268. br $ra
  269. .size s390x_kimd,.-s390x_kimd
  270. ___
  271. }
  272. {
  273. ################
  274. # void s390x_klmd(const unsigned char *in, size_t inlen, unsigned char *out,
  275. # size_t outlen, unsigned int fc, void *param)
  276. my ($in,$inlen,$out,$outlen,$fc) = map("%r$_",(2..6));
  277. $code.=<<___;
  278. .globl s390x_klmd
  279. .type s390x_klmd,\@function
  280. .align 32
  281. s390x_klmd:
  282. llgfr %r0,$fc
  283. l${g} %r1,$stdframe($sp)
  284. .long 0xb93f0042 # klmd %r4,%r2
  285. brc 1,.-4 # pay attention to "partial completion"
  286. br $ra
  287. .size s390x_klmd,.-s390x_klmd
  288. ___
  289. }
  290. ################
  291. # void s390x_km(const unsigned char *in, size_t len, unsigned char *out,
  292. # unsigned int fc, void *param)
  293. {
  294. my ($in,$len,$out,$fc,$param) = map("%r$_",(2..6));
  295. $code.=<<___;
  296. .globl s390x_km
  297. .type s390x_km,\@function
  298. .align 16
  299. s390x_km:
  300. lr %r0,$fc
  301. l${g}r %r1,$param
  302. .long 0xb92e0042 # km $out,$in
  303. brc 1,.-4 # pay attention to "partial completion"
  304. br $ra
  305. .size s390x_km,.-s390x_km
  306. ___
  307. }
  308. ################
  309. # void s390x_kmac(const unsigned char *in, size_t len, unsigned int fc,
  310. # void *param)
  311. {
  312. my ($in,$len,$fc,$param) = map("%r$_",(2..5));
  313. $code.=<<___;
  314. .globl s390x_kmac
  315. .type s390x_kmac,\@function
  316. .align 16
  317. s390x_kmac:
  318. lr %r0,$fc
  319. l${g}r %r1,$param
  320. .long 0xb91e0002 # kmac %r0,$in
  321. brc 1,.-4 # pay attention to "partial completion"
  322. br $ra
  323. .size s390x_kmac,.-s390x_kmac
  324. ___
  325. }
  326. ################
  327. # void s390x_kmo(const unsigned char *in, size_t len, unsigned char *out,
  328. # unsigned int fc, void *param)
  329. {
  330. my ($in,$len,$out,$fc,$param) = map("%r$_",(2..6));
  331. $code.=<<___;
  332. .globl s390x_kmo
  333. .type s390x_kmo,\@function
  334. .align 16
  335. s390x_kmo:
  336. lr %r0,$fc
  337. l${g}r %r1,$param
  338. .long 0xb92b0042 # kmo $out,$in
  339. brc 1,.-4 # pay attention to "partial completion"
  340. br $ra
  341. .size s390x_kmo,.-s390x_kmo
  342. ___
  343. }
  344. ################
  345. # void s390x_kmf(const unsigned char *in, size_t len, unsigned char *out,
  346. # unsigned int fc, void *param)
  347. {
  348. my ($in,$len,$out,$fc,$param) = map("%r$_",(2..6));
  349. $code.=<<___;
  350. .globl s390x_kmf
  351. .type s390x_kmf,\@function
  352. .align 16
  353. s390x_kmf:
  354. lr %r0,$fc
  355. l${g}r %r1,$param
  356. .long 0xb92a0042 # kmf $out,$in
  357. brc 1,.-4 # pay attention to "partial completion"
  358. br $ra
  359. .size s390x_kmf,.-s390x_kmf
  360. ___
  361. }
  362. ################
  363. # void s390x_kma(const unsigned char *aad, size_t alen,
  364. # const unsigned char *in, size_t len,
  365. # unsigned char *out, unsigned int fc, void *param)
  366. {
  367. my ($aad,$alen,$in,$len,$out) = map("%r$_",(2..6));
  368. $code.=<<___;
  369. .globl s390x_kma
  370. .type s390x_kma,\@function
  371. .align 16
  372. s390x_kma:
  373. st${g} $out,6*$SIZE_T($sp)
  374. lm${g} %r0,%r1,$stdframe($sp)
  375. .long 0xb9292064 # kma $out,$aad,$in
  376. brc 1,.-4 # pay attention to "partial completion"
  377. l${g} $out,6*$SIZE_T($sp)
  378. br $ra
  379. .size s390x_kma,.-s390x_kma
  380. ___
  381. }
  382. ################
  383. # int s390x_pcc(unsigned int fc, void *param)
  384. {
  385. my ($fc,$param) = map("%r$_",(2..3));
  386. $code.=<<___;
  387. .globl s390x_pcc
  388. .type s390x_pcc,\@function
  389. .align 16
  390. s390x_pcc:
  391. lr %r0,$fc
  392. l${g}r %r1,$param
  393. lhi %r2,0
  394. .long 0xb92c0000 # pcc
  395. brc 1,.-4 # pay attention to "partial completion"
  396. brc 7,.Lpcc_err # if CC==0 return 0, else return 1
  397. .Lpcc_out:
  398. br $ra
  399. .Lpcc_err:
  400. lhi %r2,1
  401. j .Lpcc_out
  402. .size s390x_pcc,.-s390x_pcc
  403. ___
  404. }
  405. ################
  406. # int s390x_kdsa(unsigned int fc, void *param,
  407. # const unsigned char *in, size_t len)
  408. {
  409. my ($fc,$param,$in,$len) = map("%r$_",(2..5));
  410. $code.=<<___;
  411. .globl s390x_kdsa
  412. .type s390x_kdsa,\@function
  413. .align 16
  414. s390x_kdsa:
  415. lr %r0,$fc
  416. l${g}r %r1,$param
  417. lhi %r2,0
  418. .long 0xb93a0004 # kdsa %r0,$in
  419. brc 1,.-4 # pay attention to "partial completion"
  420. brc 7,.Lkdsa_err # if CC==0 return 0, else return 1
  421. .Lkdsa_out:
  422. br $ra
  423. .Lkdsa_err:
  424. lhi %r2,1
  425. j .Lkdsa_out
  426. .size s390x_kdsa,.-s390x_kdsa
  427. ___
  428. }
  429. ################
  430. # void s390x_flip_endian32(unsigned char dst[32], const unsigned char src[32])
  431. {
  432. my ($dst,$src) = map("%r$_",(2..3));
  433. $code.=<<___;
  434. .globl s390x_flip_endian32
  435. .type s390x_flip_endian32,\@function
  436. .align 16
  437. s390x_flip_endian32:
  438. lrvg %r0,0($src)
  439. lrvg %r1,8($src)
  440. lrvg %r4,16($src)
  441. lrvg %r5,24($src)
  442. stg %r0,24($dst)
  443. stg %r1,16($dst)
  444. stg %r4,8($dst)
  445. stg %r5,0($dst)
  446. br $ra
  447. .size s390x_flip_endian32,.-s390x_flip_endian32
  448. ___
  449. }
  450. ################
  451. # void s390x_flip_endian64(unsigned char dst[64], const unsigned char src[64])
  452. {
  453. my ($dst,$src) = map("%r$_",(2..3));
  454. $code.=<<___;
  455. .globl s390x_flip_endian64
  456. .type s390x_flip_endian64,\@function
  457. .align 16
  458. s390x_flip_endian64:
  459. stmg %r6,%r9,6*$SIZE_T($sp)
  460. lrvg %r0,0($src)
  461. lrvg %r1,8($src)
  462. lrvg %r4,16($src)
  463. lrvg %r5,24($src)
  464. lrvg %r6,32($src)
  465. lrvg %r7,40($src)
  466. lrvg %r8,48($src)
  467. lrvg %r9,56($src)
  468. stg %r0,56($dst)
  469. stg %r1,48($dst)
  470. stg %r4,40($dst)
  471. stg %r5,32($dst)
  472. stg %r6,24($dst)
  473. stg %r7,16($dst)
  474. stg %r8,8($dst)
  475. stg %r9,0($dst)
  476. lmg %r6,%r9,6*$SIZE_T($sp)
  477. br $ra
  478. .size s390x_flip_endian64,.-s390x_flip_endian64
  479. ___
  480. }
  481. $code.=<<___;
  482. .section .init
  483. brasl $ra,OPENSSL_cpuid_setup
  484. ___
  485. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  486. print $code;
  487. close STDOUT or die "error closing STDOUT: $!"; # force flush