rc4-x86_64.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. #! /usr/bin/env perl
  2. # Copyright 2005-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. #
  9. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # July 2004
  17. #
  18. # 2.22x RC4 tune-up:-) It should be noted though that my hand [as in
  19. # "hand-coded assembler"] doesn't stand for the whole improvement
  20. # coefficient. It turned out that eliminating RC4_CHAR from config
  21. # line results in ~40% improvement (yes, even for C implementation).
  22. # Presumably it has everything to do with AMD cache architecture and
  23. # RAW or whatever penalties. Once again! The module *requires* config
  24. # line *without* RC4_CHAR! As for coding "secret," I bet on partial
  25. # register arithmetics. For example instead of 'inc %r8; and $255,%r8'
  26. # I simply 'inc %r8b'. Even though optimization manual discourages
  27. # to operate on partial registers, it turned out to be the best bet.
  28. # At least for AMD... How IA32E would perform remains to be seen...
  29. # November 2004
  30. #
  31. # As was shown by Marc Bevand reordering of couple of load operations
  32. # results in even higher performance gain of 3.3x:-) At least on
  33. # Opteron... For reference, 1x in this case is RC4_CHAR C-code
  34. # compiled with gcc 3.3.2, which performs at ~54MBps per 1GHz clock.
  35. # Latter means that if you want to *estimate* what to expect from
  36. # *your* Opteron, then multiply 54 by 3.3 and clock frequency in GHz.
  37. # November 2004
  38. #
  39. # Intel P4 EM64T core was found to run the AMD64 code really slow...
  40. # The only way to achieve comparable performance on P4 was to keep
  41. # RC4_CHAR. Kind of ironic, huh? As it's apparently impossible to
  42. # compose blended code, which would perform even within 30% marginal
  43. # on either AMD and Intel platforms, I implement both cases. See
  44. # rc4_skey.c for further details...
  45. # April 2005
  46. #
  47. # P4 EM64T core appears to be "allergic" to 64-bit inc/dec. Replacing
  48. # those with add/sub results in 50% performance improvement of folded
  49. # loop...
  50. # May 2005
  51. #
  52. # As was shown by Zou Nanhai loop unrolling can improve Intel EM64T
  53. # performance by >30% [unlike P4 32-bit case that is]. But this is
  54. # provided that loads are reordered even more aggressively! Both code
  55. # paths, AMD64 and EM64T, reorder loads in essentially same manner
  56. # as my IA-64 implementation. On Opteron this resulted in modest 5%
  57. # improvement [I had to test it], while final Intel P4 performance
  58. # achieves respectful 432MBps on 2.8GHz processor now. For reference.
  59. # If executed on Xeon, current RC4_CHAR code-path is 2.7x faster than
  60. # RC4_INT code-path. While if executed on Opteron, it's only 25%
  61. # slower than the RC4_INT one [meaning that if CPU µ-arch detection
  62. # is not implemented, then this final RC4_CHAR code-path should be
  63. # preferred, as it provides better *all-round* performance].
  64. # March 2007
  65. #
  66. # Intel Core2 was observed to perform poorly on both code paths:-( It
  67. # apparently suffers from some kind of partial register stall, which
  68. # occurs in 64-bit mode only [as virtually identical 32-bit loop was
  69. # observed to outperform 64-bit one by almost 50%]. Adding two movzb to
  70. # cloop1 boosts its performance by 80%! This loop appears to be optimal
  71. # fit for Core2 and therefore the code was modified to skip cloop8 on
  72. # this CPU.
  73. # May 2010
  74. #
  75. # Intel Westmere was observed to perform suboptimally. Adding yet
  76. # another movzb to cloop1 improved performance by almost 50%! Core2
  77. # performance is improved too, but nominally...
  78. # May 2011
  79. #
  80. # The only code path that was not modified is P4-specific one. Non-P4
  81. # Intel code path optimization is heavily based on submission by Maxim
  82. # Perminov, Maxim Locktyukhin and Jim Guilford of Intel. I've used
  83. # some of the ideas even in attempt to optimize the original RC4_INT
  84. # code path... Current performance in cycles per processed byte (less
  85. # is better) and improvement coefficients relative to previous
  86. # version of this module are:
  87. #
  88. # Opteron 5.3/+0%(*)
  89. # P4 6.5
  90. # Core2 6.2/+15%(**)
  91. # Westmere 4.2/+60%
  92. # Sandy Bridge 4.2/+120%
  93. # Atom 9.3/+80%
  94. # VIA Nano 6.4/+4%
  95. # Ivy Bridge 4.1/+30%
  96. # Bulldozer 4.5/+30%(*)
  97. #
  98. # (*) But corresponding loop has less instructions, which should have
  99. # positive effect on upcoming Bulldozer, which has one less ALU.
  100. # For reference, Intel code runs at 6.8 cpb rate on Opteron.
  101. # (**) Note that Core2 result is ~15% lower than corresponding result
  102. # for 32-bit code, meaning that it's possible to improve it,
  103. # but more than likely at the cost of the others (see rc4-586.pl
  104. # to get the idea)...
  105. $flavour = shift;
  106. $output = shift;
  107. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  108. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  109. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  110. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  111. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  112. die "can't locate x86_64-xlate.pl";
  113. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
  114. *STDOUT=*OUT;
  115. $dat="%rdi"; # arg1
  116. $len="%rsi"; # arg2
  117. $inp="%rdx"; # arg3
  118. $out="%rcx"; # arg4
  119. {
  120. $code=<<___;
  121. .text
  122. .extern OPENSSL_ia32cap_P
  123. .globl RC4
  124. .type RC4,\@function,4
  125. .align 16
  126. RC4:
  127. .cfi_startproc
  128. or $len,$len
  129. jne .Lentry
  130. ret
  131. .Lentry:
  132. push %rbx
  133. .cfi_push %rbx
  134. push %r12
  135. .cfi_push %r12
  136. push %r13
  137. .cfi_push %r13
  138. .Lprologue:
  139. mov $len,%r11
  140. mov $inp,%r12
  141. mov $out,%r13
  142. ___
  143. my $len="%r11"; # reassign input arguments
  144. my $inp="%r12";
  145. my $out="%r13";
  146. my @XX=("%r10","%rsi");
  147. my @TX=("%rax","%rbx");
  148. my $YY="%rcx";
  149. my $TY="%rdx";
  150. $code.=<<___;
  151. xor $XX[0],$XX[0]
  152. xor $YY,$YY
  153. lea 8($dat),$dat
  154. mov -8($dat),$XX[0]#b
  155. mov -4($dat),$YY#b
  156. cmpl \$-1,256($dat)
  157. je .LRC4_CHAR
  158. mov OPENSSL_ia32cap_P(%rip),%r8d
  159. xor $TX[1],$TX[1]
  160. inc $XX[0]#b
  161. sub $XX[0],$TX[1]
  162. sub $inp,$out
  163. movl ($dat,$XX[0],4),$TX[0]#d
  164. test \$-16,$len
  165. jz .Lloop1
  166. bt \$30,%r8d # Intel CPU?
  167. jc .Lintel
  168. and \$7,$TX[1]
  169. lea 1($XX[0]),$XX[1]
  170. jz .Loop8
  171. sub $TX[1],$len
  172. .Loop8_warmup:
  173. add $TX[0]#b,$YY#b
  174. movl ($dat,$YY,4),$TY#d
  175. movl $TX[0]#d,($dat,$YY,4)
  176. movl $TY#d,($dat,$XX[0],4)
  177. add $TY#b,$TX[0]#b
  178. inc $XX[0]#b
  179. movl ($dat,$TX[0],4),$TY#d
  180. movl ($dat,$XX[0],4),$TX[0]#d
  181. xorb ($inp),$TY#b
  182. movb $TY#b,($out,$inp)
  183. lea 1($inp),$inp
  184. dec $TX[1]
  185. jnz .Loop8_warmup
  186. lea 1($XX[0]),$XX[1]
  187. jmp .Loop8
  188. .align 16
  189. .Loop8:
  190. ___
  191. for ($i=0;$i<8;$i++) {
  192. $code.=<<___ if ($i==7);
  193. add \$8,$XX[1]#b
  194. ___
  195. $code.=<<___;
  196. add $TX[0]#b,$YY#b
  197. movl ($dat,$YY,4),$TY#d
  198. movl $TX[0]#d,($dat,$YY,4)
  199. movl `4*($i==7?-1:$i)`($dat,$XX[1],4),$TX[1]#d
  200. ror \$8,%r8 # ror is redundant when $i=0
  201. movl $TY#d,4*$i($dat,$XX[0],4)
  202. add $TX[0]#b,$TY#b
  203. movb ($dat,$TY,4),%r8b
  204. ___
  205. push(@TX,shift(@TX)); #push(@XX,shift(@XX)); # "rotate" registers
  206. }
  207. $code.=<<___;
  208. add \$8,$XX[0]#b
  209. ror \$8,%r8
  210. sub \$8,$len
  211. xor ($inp),%r8
  212. mov %r8,($out,$inp)
  213. lea 8($inp),$inp
  214. test \$-8,$len
  215. jnz .Loop8
  216. cmp \$0,$len
  217. jne .Lloop1
  218. jmp .Lexit
  219. .align 16
  220. .Lintel:
  221. test \$-32,$len
  222. jz .Lloop1
  223. and \$15,$TX[1]
  224. jz .Loop16_is_hot
  225. sub $TX[1],$len
  226. .Loop16_warmup:
  227. add $TX[0]#b,$YY#b
  228. movl ($dat,$YY,4),$TY#d
  229. movl $TX[0]#d,($dat,$YY,4)
  230. movl $TY#d,($dat,$XX[0],4)
  231. add $TY#b,$TX[0]#b
  232. inc $XX[0]#b
  233. movl ($dat,$TX[0],4),$TY#d
  234. movl ($dat,$XX[0],4),$TX[0]#d
  235. xorb ($inp),$TY#b
  236. movb $TY#b,($out,$inp)
  237. lea 1($inp),$inp
  238. dec $TX[1]
  239. jnz .Loop16_warmup
  240. mov $YY,$TX[1]
  241. xor $YY,$YY
  242. mov $TX[1]#b,$YY#b
  243. .Loop16_is_hot:
  244. lea ($dat,$XX[0],4),$XX[1]
  245. ___
  246. sub RC4_loop {
  247. my $i=shift;
  248. my $j=$i<0?0:$i;
  249. my $xmm="%xmm".($j&1);
  250. $code.=" add \$16,$XX[0]#b\n" if ($i==15);
  251. $code.=" movdqu ($inp),%xmm2\n" if ($i==15);
  252. $code.=" add $TX[0]#b,$YY#b\n" if ($i<=0);
  253. $code.=" movl ($dat,$YY,4),$TY#d\n";
  254. $code.=" pxor %xmm0,%xmm2\n" if ($i==0);
  255. $code.=" psllq \$8,%xmm1\n" if ($i==0);
  256. $code.=" pxor $xmm,$xmm\n" if ($i<=1);
  257. $code.=" movl $TX[0]#d,($dat,$YY,4)\n";
  258. $code.=" add $TY#b,$TX[0]#b\n";
  259. $code.=" movl `4*($j+1)`($XX[1]),$TX[1]#d\n" if ($i<15);
  260. $code.=" movz $TX[0]#b,$TX[0]#d\n";
  261. $code.=" movl $TY#d,4*$j($XX[1])\n";
  262. $code.=" pxor %xmm1,%xmm2\n" if ($i==0);
  263. $code.=" lea ($dat,$XX[0],4),$XX[1]\n" if ($i==15);
  264. $code.=" add $TX[1]#b,$YY#b\n" if ($i<15);
  265. $code.=" pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n";
  266. $code.=" movdqu %xmm2,($out,$inp)\n" if ($i==0);
  267. $code.=" lea 16($inp),$inp\n" if ($i==0);
  268. $code.=" movl ($XX[1]),$TX[1]#d\n" if ($i==15);
  269. }
  270. RC4_loop(-1);
  271. $code.=<<___;
  272. jmp .Loop16_enter
  273. .align 16
  274. .Loop16:
  275. ___
  276. for ($i=0;$i<16;$i++) {
  277. $code.=".Loop16_enter:\n" if ($i==1);
  278. RC4_loop($i);
  279. push(@TX,shift(@TX)); # "rotate" registers
  280. }
  281. $code.=<<___;
  282. mov $YY,$TX[1]
  283. xor $YY,$YY # keyword to partial register
  284. sub \$16,$len
  285. mov $TX[1]#b,$YY#b
  286. test \$-16,$len
  287. jnz .Loop16
  288. psllq \$8,%xmm1
  289. pxor %xmm0,%xmm2
  290. pxor %xmm1,%xmm2
  291. movdqu %xmm2,($out,$inp)
  292. lea 16($inp),$inp
  293. cmp \$0,$len
  294. jne .Lloop1
  295. jmp .Lexit
  296. .align 16
  297. .Lloop1:
  298. add $TX[0]#b,$YY#b
  299. movl ($dat,$YY,4),$TY#d
  300. movl $TX[0]#d,($dat,$YY,4)
  301. movl $TY#d,($dat,$XX[0],4)
  302. add $TY#b,$TX[0]#b
  303. inc $XX[0]#b
  304. movl ($dat,$TX[0],4),$TY#d
  305. movl ($dat,$XX[0],4),$TX[0]#d
  306. xorb ($inp),$TY#b
  307. movb $TY#b,($out,$inp)
  308. lea 1($inp),$inp
  309. dec $len
  310. jnz .Lloop1
  311. jmp .Lexit
  312. .align 16
  313. .LRC4_CHAR:
  314. add \$1,$XX[0]#b
  315. movzb ($dat,$XX[0]),$TX[0]#d
  316. test \$-8,$len
  317. jz .Lcloop1
  318. jmp .Lcloop8
  319. .align 16
  320. .Lcloop8:
  321. mov ($inp),%r8d
  322. mov 4($inp),%r9d
  323. ___
  324. # unroll 2x4-wise, because 64-bit rotates kill Intel P4...
  325. for ($i=0;$i<4;$i++) {
  326. $code.=<<___;
  327. add $TX[0]#b,$YY#b
  328. lea 1($XX[0]),$XX[1]
  329. movzb ($dat,$YY),$TY#d
  330. movzb $XX[1]#b,$XX[1]#d
  331. movzb ($dat,$XX[1]),$TX[1]#d
  332. movb $TX[0]#b,($dat,$YY)
  333. cmp $XX[1],$YY
  334. movb $TY#b,($dat,$XX[0])
  335. jne .Lcmov$i # Intel cmov is sloooow...
  336. mov $TX[0],$TX[1]
  337. .Lcmov$i:
  338. add $TX[0]#b,$TY#b
  339. xor ($dat,$TY),%r8b
  340. ror \$8,%r8d
  341. ___
  342. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  343. }
  344. for ($i=4;$i<8;$i++) {
  345. $code.=<<___;
  346. add $TX[0]#b,$YY#b
  347. lea 1($XX[0]),$XX[1]
  348. movzb ($dat,$YY),$TY#d
  349. movzb $XX[1]#b,$XX[1]#d
  350. movzb ($dat,$XX[1]),$TX[1]#d
  351. movb $TX[0]#b,($dat,$YY)
  352. cmp $XX[1],$YY
  353. movb $TY#b,($dat,$XX[0])
  354. jne .Lcmov$i # Intel cmov is sloooow...
  355. mov $TX[0],$TX[1]
  356. .Lcmov$i:
  357. add $TX[0]#b,$TY#b
  358. xor ($dat,$TY),%r9b
  359. ror \$8,%r9d
  360. ___
  361. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  362. }
  363. $code.=<<___;
  364. lea -8($len),$len
  365. mov %r8d,($out)
  366. lea 8($inp),$inp
  367. mov %r9d,4($out)
  368. lea 8($out),$out
  369. test \$-8,$len
  370. jnz .Lcloop8
  371. cmp \$0,$len
  372. jne .Lcloop1
  373. jmp .Lexit
  374. ___
  375. $code.=<<___;
  376. .align 16
  377. .Lcloop1:
  378. add $TX[0]#b,$YY#b
  379. movzb $YY#b,$YY#d
  380. movzb ($dat,$YY),$TY#d
  381. movb $TX[0]#b,($dat,$YY)
  382. movb $TY#b,($dat,$XX[0])
  383. add $TX[0]#b,$TY#b
  384. add \$1,$XX[0]#b
  385. movzb $TY#b,$TY#d
  386. movzb $XX[0]#b,$XX[0]#d
  387. movzb ($dat,$TY),$TY#d
  388. movzb ($dat,$XX[0]),$TX[0]#d
  389. xorb ($inp),$TY#b
  390. lea 1($inp),$inp
  391. movb $TY#b,($out)
  392. lea 1($out),$out
  393. sub \$1,$len
  394. jnz .Lcloop1
  395. jmp .Lexit
  396. .align 16
  397. .Lexit:
  398. sub \$1,$XX[0]#b
  399. movl $XX[0]#d,-8($dat)
  400. movl $YY#d,-4($dat)
  401. mov (%rsp),%r13
  402. .cfi_restore %r13
  403. mov 8(%rsp),%r12
  404. .cfi_restore %r12
  405. mov 16(%rsp),%rbx
  406. .cfi_restore %rbx
  407. add \$24,%rsp
  408. .cfi_adjust_cfa_offset -24
  409. .Lepilogue:
  410. ret
  411. .cfi_endproc
  412. .size RC4,.-RC4
  413. ___
  414. }
  415. $idx="%r8";
  416. $ido="%r9";
  417. $code.=<<___;
  418. .globl RC4_set_key
  419. .type RC4_set_key,\@function,3
  420. .align 16
  421. RC4_set_key:
  422. .cfi_startproc
  423. lea 8($dat),$dat
  424. lea ($inp,$len),$inp
  425. neg $len
  426. mov $len,%rcx
  427. xor %eax,%eax
  428. xor $ido,$ido
  429. xor %r10,%r10
  430. xor %r11,%r11
  431. mov OPENSSL_ia32cap_P(%rip),$idx#d
  432. bt \$20,$idx#d # RC4_CHAR?
  433. jc .Lc1stloop
  434. jmp .Lw1stloop
  435. .align 16
  436. .Lw1stloop:
  437. mov %eax,($dat,%rax,4)
  438. add \$1,%al
  439. jnc .Lw1stloop
  440. xor $ido,$ido
  441. xor $idx,$idx
  442. .align 16
  443. .Lw2ndloop:
  444. mov ($dat,$ido,4),%r10d
  445. add ($inp,$len,1),$idx#b
  446. add %r10b,$idx#b
  447. add \$1,$len
  448. mov ($dat,$idx,4),%r11d
  449. cmovz %rcx,$len
  450. mov %r10d,($dat,$idx,4)
  451. mov %r11d,($dat,$ido,4)
  452. add \$1,$ido#b
  453. jnc .Lw2ndloop
  454. jmp .Lexit_key
  455. .align 16
  456. .Lc1stloop:
  457. mov %al,($dat,%rax)
  458. add \$1,%al
  459. jnc .Lc1stloop
  460. xor $ido,$ido
  461. xor $idx,$idx
  462. .align 16
  463. .Lc2ndloop:
  464. mov ($dat,$ido),%r10b
  465. add ($inp,$len),$idx#b
  466. add %r10b,$idx#b
  467. add \$1,$len
  468. mov ($dat,$idx),%r11b
  469. jnz .Lcnowrap
  470. mov %rcx,$len
  471. .Lcnowrap:
  472. mov %r10b,($dat,$idx)
  473. mov %r11b,($dat,$ido)
  474. add \$1,$ido#b
  475. jnc .Lc2ndloop
  476. movl \$-1,256($dat)
  477. .align 16
  478. .Lexit_key:
  479. xor %eax,%eax
  480. mov %eax,-8($dat)
  481. mov %eax,-4($dat)
  482. ret
  483. .cfi_endproc
  484. .size RC4_set_key,.-RC4_set_key
  485. .globl RC4_options
  486. .type RC4_options,\@abi-omnipotent
  487. .align 16
  488. RC4_options:
  489. .cfi_startproc
  490. lea .Lopts(%rip),%rax
  491. mov OPENSSL_ia32cap_P(%rip),%edx
  492. bt \$20,%edx
  493. jc .L8xchar
  494. bt \$30,%edx
  495. jnc .Ldone
  496. add \$25,%rax
  497. ret
  498. .L8xchar:
  499. add \$12,%rax
  500. .Ldone:
  501. ret
  502. .cfi_endproc
  503. .align 64
  504. .Lopts:
  505. .asciz "rc4(8x,int)"
  506. .asciz "rc4(8x,char)"
  507. .asciz "rc4(16x,int)"
  508. .asciz "RC4 for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  509. .align 64
  510. .size RC4_options,.-RC4_options
  511. ___
  512. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  513. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  514. if ($win64) {
  515. $rec="%rcx";
  516. $frame="%rdx";
  517. $context="%r8";
  518. $disp="%r9";
  519. $code.=<<___;
  520. .extern __imp_RtlVirtualUnwind
  521. .type stream_se_handler,\@abi-omnipotent
  522. .align 16
  523. stream_se_handler:
  524. push %rsi
  525. push %rdi
  526. push %rbx
  527. push %rbp
  528. push %r12
  529. push %r13
  530. push %r14
  531. push %r15
  532. pushfq
  533. sub \$64,%rsp
  534. mov 120($context),%rax # pull context->Rax
  535. mov 248($context),%rbx # pull context->Rip
  536. lea .Lprologue(%rip),%r10
  537. cmp %r10,%rbx # context->Rip<prologue label
  538. jb .Lin_prologue
  539. mov 152($context),%rax # pull context->Rsp
  540. lea .Lepilogue(%rip),%r10
  541. cmp %r10,%rbx # context->Rip>=epilogue label
  542. jae .Lin_prologue
  543. lea 24(%rax),%rax
  544. mov -8(%rax),%rbx
  545. mov -16(%rax),%r12
  546. mov -24(%rax),%r13
  547. mov %rbx,144($context) # restore context->Rbx
  548. mov %r12,216($context) # restore context->R12
  549. mov %r13,224($context) # restore context->R13
  550. .Lin_prologue:
  551. mov 8(%rax),%rdi
  552. mov 16(%rax),%rsi
  553. mov %rax,152($context) # restore context->Rsp
  554. mov %rsi,168($context) # restore context->Rsi
  555. mov %rdi,176($context) # restore context->Rdi
  556. jmp .Lcommon_seh_exit
  557. .size stream_se_handler,.-stream_se_handler
  558. .type key_se_handler,\@abi-omnipotent
  559. .align 16
  560. key_se_handler:
  561. push %rsi
  562. push %rdi
  563. push %rbx
  564. push %rbp
  565. push %r12
  566. push %r13
  567. push %r14
  568. push %r15
  569. pushfq
  570. sub \$64,%rsp
  571. mov 152($context),%rax # pull context->Rsp
  572. mov 8(%rax),%rdi
  573. mov 16(%rax),%rsi
  574. mov %rsi,168($context) # restore context->Rsi
  575. mov %rdi,176($context) # restore context->Rdi
  576. .Lcommon_seh_exit:
  577. mov 40($disp),%rdi # disp->ContextRecord
  578. mov $context,%rsi # context
  579. mov \$154,%ecx # sizeof(CONTEXT)
  580. .long 0xa548f3fc # cld; rep movsq
  581. mov $disp,%rsi
  582. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  583. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  584. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  585. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  586. mov 40(%rsi),%r10 # disp->ContextRecord
  587. lea 56(%rsi),%r11 # &disp->HandlerData
  588. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  589. mov %r10,32(%rsp) # arg5
  590. mov %r11,40(%rsp) # arg6
  591. mov %r12,48(%rsp) # arg7
  592. mov %rcx,56(%rsp) # arg8, (NULL)
  593. call *__imp_RtlVirtualUnwind(%rip)
  594. mov \$1,%eax # ExceptionContinueSearch
  595. add \$64,%rsp
  596. popfq
  597. pop %r15
  598. pop %r14
  599. pop %r13
  600. pop %r12
  601. pop %rbp
  602. pop %rbx
  603. pop %rdi
  604. pop %rsi
  605. ret
  606. .size key_se_handler,.-key_se_handler
  607. .section .pdata
  608. .align 4
  609. .rva .LSEH_begin_RC4
  610. .rva .LSEH_end_RC4
  611. .rva .LSEH_info_RC4
  612. .rva .LSEH_begin_RC4_set_key
  613. .rva .LSEH_end_RC4_set_key
  614. .rva .LSEH_info_RC4_set_key
  615. .section .xdata
  616. .align 8
  617. .LSEH_info_RC4:
  618. .byte 9,0,0,0
  619. .rva stream_se_handler
  620. .LSEH_info_RC4_set_key:
  621. .byte 9,0,0,0
  622. .rva key_se_handler
  623. ___
  624. }
  625. sub reg_part {
  626. my ($reg,$conv)=@_;
  627. if ($reg =~ /%r[0-9]+/) { $reg .= $conv; }
  628. elsif ($conv eq "b") { $reg =~ s/%[er]([^x]+)x?/%$1l/; }
  629. elsif ($conv eq "w") { $reg =~ s/%[er](.+)/%$1/; }
  630. elsif ($conv eq "d") { $reg =~ s/%[er](.+)/%e$1/; }
  631. return $reg;
  632. }
  633. $code =~ s/(%[a-z0-9]+)#([bwd])/reg_part($1,$2)/gem;
  634. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  635. print $code;
  636. close STDOUT or die "error closing STDOUT: $!";