rc4-x86_64.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. #! /usr/bin/env perl
  2. # Copyright 2005-2016 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: or $len,$len
  127. jne .Lentry
  128. ret
  129. .Lentry:
  130. .cfi_startproc
  131. push %rbx
  132. .cfi_push %rbx
  133. push %r12
  134. .cfi_push %r12
  135. push %r13
  136. .cfi_push %r13
  137. .Lprologue:
  138. mov $len,%r11
  139. mov $inp,%r12
  140. mov $out,%r13
  141. ___
  142. my $len="%r11"; # reassign input arguments
  143. my $inp="%r12";
  144. my $out="%r13";
  145. my @XX=("%r10","%rsi");
  146. my @TX=("%rax","%rbx");
  147. my $YY="%rcx";
  148. my $TY="%rdx";
  149. $code.=<<___;
  150. xor $XX[0],$XX[0]
  151. xor $YY,$YY
  152. lea 8($dat),$dat
  153. mov -8($dat),$XX[0]#b
  154. mov -4($dat),$YY#b
  155. cmpl \$-1,256($dat)
  156. je .LRC4_CHAR
  157. mov OPENSSL_ia32cap_P(%rip),%r8d
  158. xor $TX[1],$TX[1]
  159. inc $XX[0]#b
  160. sub $XX[0],$TX[1]
  161. sub $inp,$out
  162. movl ($dat,$XX[0],4),$TX[0]#d
  163. test \$-16,$len
  164. jz .Lloop1
  165. bt \$30,%r8d # Intel CPU?
  166. jc .Lintel
  167. and \$7,$TX[1]
  168. lea 1($XX[0]),$XX[1]
  169. jz .Loop8
  170. sub $TX[1],$len
  171. .Loop8_warmup:
  172. add $TX[0]#b,$YY#b
  173. movl ($dat,$YY,4),$TY#d
  174. movl $TX[0]#d,($dat,$YY,4)
  175. movl $TY#d,($dat,$XX[0],4)
  176. add $TY#b,$TX[0]#b
  177. inc $XX[0]#b
  178. movl ($dat,$TX[0],4),$TY#d
  179. movl ($dat,$XX[0],4),$TX[0]#d
  180. xorb ($inp),$TY#b
  181. movb $TY#b,($out,$inp)
  182. lea 1($inp),$inp
  183. dec $TX[1]
  184. jnz .Loop8_warmup
  185. lea 1($XX[0]),$XX[1]
  186. jmp .Loop8
  187. .align 16
  188. .Loop8:
  189. ___
  190. for ($i=0;$i<8;$i++) {
  191. $code.=<<___ if ($i==7);
  192. add \$8,$XX[1]#b
  193. ___
  194. $code.=<<___;
  195. add $TX[0]#b,$YY#b
  196. movl ($dat,$YY,4),$TY#d
  197. movl $TX[0]#d,($dat,$YY,4)
  198. movl `4*($i==7?-1:$i)`($dat,$XX[1],4),$TX[1]#d
  199. ror \$8,%r8 # ror is redundant when $i=0
  200. movl $TY#d,4*$i($dat,$XX[0],4)
  201. add $TX[0]#b,$TY#b
  202. movb ($dat,$TY,4),%r8b
  203. ___
  204. push(@TX,shift(@TX)); #push(@XX,shift(@XX)); # "rotate" registers
  205. }
  206. $code.=<<___;
  207. add \$8,$XX[0]#b
  208. ror \$8,%r8
  209. sub \$8,$len
  210. xor ($inp),%r8
  211. mov %r8,($out,$inp)
  212. lea 8($inp),$inp
  213. test \$-8,$len
  214. jnz .Loop8
  215. cmp \$0,$len
  216. jne .Lloop1
  217. jmp .Lexit
  218. .align 16
  219. .Lintel:
  220. test \$-32,$len
  221. jz .Lloop1
  222. and \$15,$TX[1]
  223. jz .Loop16_is_hot
  224. sub $TX[1],$len
  225. .Loop16_warmup:
  226. add $TX[0]#b,$YY#b
  227. movl ($dat,$YY,4),$TY#d
  228. movl $TX[0]#d,($dat,$YY,4)
  229. movl $TY#d,($dat,$XX[0],4)
  230. add $TY#b,$TX[0]#b
  231. inc $XX[0]#b
  232. movl ($dat,$TX[0],4),$TY#d
  233. movl ($dat,$XX[0],4),$TX[0]#d
  234. xorb ($inp),$TY#b
  235. movb $TY#b,($out,$inp)
  236. lea 1($inp),$inp
  237. dec $TX[1]
  238. jnz .Loop16_warmup
  239. mov $YY,$TX[1]
  240. xor $YY,$YY
  241. mov $TX[1]#b,$YY#b
  242. .Loop16_is_hot:
  243. lea ($dat,$XX[0],4),$XX[1]
  244. ___
  245. sub RC4_loop {
  246. my $i=shift;
  247. my $j=$i<0?0:$i;
  248. my $xmm="%xmm".($j&1);
  249. $code.=" add \$16,$XX[0]#b\n" if ($i==15);
  250. $code.=" movdqu ($inp),%xmm2\n" if ($i==15);
  251. $code.=" add $TX[0]#b,$YY#b\n" if ($i<=0);
  252. $code.=" movl ($dat,$YY,4),$TY#d\n";
  253. $code.=" pxor %xmm0,%xmm2\n" if ($i==0);
  254. $code.=" psllq \$8,%xmm1\n" if ($i==0);
  255. $code.=" pxor $xmm,$xmm\n" if ($i<=1);
  256. $code.=" movl $TX[0]#d,($dat,$YY,4)\n";
  257. $code.=" add $TY#b,$TX[0]#b\n";
  258. $code.=" movl `4*($j+1)`($XX[1]),$TX[1]#d\n" if ($i<15);
  259. $code.=" movz $TX[0]#b,$TX[0]#d\n";
  260. $code.=" movl $TY#d,4*$j($XX[1])\n";
  261. $code.=" pxor %xmm1,%xmm2\n" if ($i==0);
  262. $code.=" lea ($dat,$XX[0],4),$XX[1]\n" if ($i==15);
  263. $code.=" add $TX[1]#b,$YY#b\n" if ($i<15);
  264. $code.=" pinsrw \$`($j>>1)&7`,($dat,$TX[0],4),$xmm\n";
  265. $code.=" movdqu %xmm2,($out,$inp)\n" if ($i==0);
  266. $code.=" lea 16($inp),$inp\n" if ($i==0);
  267. $code.=" movl ($XX[1]),$TX[1]#d\n" if ($i==15);
  268. }
  269. RC4_loop(-1);
  270. $code.=<<___;
  271. jmp .Loop16_enter
  272. .align 16
  273. .Loop16:
  274. ___
  275. for ($i=0;$i<16;$i++) {
  276. $code.=".Loop16_enter:\n" if ($i==1);
  277. RC4_loop($i);
  278. push(@TX,shift(@TX)); # "rotate" registers
  279. }
  280. $code.=<<___;
  281. mov $YY,$TX[1]
  282. xor $YY,$YY # keyword to partial register
  283. sub \$16,$len
  284. mov $TX[1]#b,$YY#b
  285. test \$-16,$len
  286. jnz .Loop16
  287. psllq \$8,%xmm1
  288. pxor %xmm0,%xmm2
  289. pxor %xmm1,%xmm2
  290. movdqu %xmm2,($out,$inp)
  291. lea 16($inp),$inp
  292. cmp \$0,$len
  293. jne .Lloop1
  294. jmp .Lexit
  295. .align 16
  296. .Lloop1:
  297. add $TX[0]#b,$YY#b
  298. movl ($dat,$YY,4),$TY#d
  299. movl $TX[0]#d,($dat,$YY,4)
  300. movl $TY#d,($dat,$XX[0],4)
  301. add $TY#b,$TX[0]#b
  302. inc $XX[0]#b
  303. movl ($dat,$TX[0],4),$TY#d
  304. movl ($dat,$XX[0],4),$TX[0]#d
  305. xorb ($inp),$TY#b
  306. movb $TY#b,($out,$inp)
  307. lea 1($inp),$inp
  308. dec $len
  309. jnz .Lloop1
  310. jmp .Lexit
  311. .align 16
  312. .LRC4_CHAR:
  313. add \$1,$XX[0]#b
  314. movzb ($dat,$XX[0]),$TX[0]#d
  315. test \$-8,$len
  316. jz .Lcloop1
  317. jmp .Lcloop8
  318. .align 16
  319. .Lcloop8:
  320. mov ($inp),%r8d
  321. mov 4($inp),%r9d
  322. ___
  323. # unroll 2x4-wise, because 64-bit rotates kill Intel P4...
  324. for ($i=0;$i<4;$i++) {
  325. $code.=<<___;
  326. add $TX[0]#b,$YY#b
  327. lea 1($XX[0]),$XX[1]
  328. movzb ($dat,$YY),$TY#d
  329. movzb $XX[1]#b,$XX[1]#d
  330. movzb ($dat,$XX[1]),$TX[1]#d
  331. movb $TX[0]#b,($dat,$YY)
  332. cmp $XX[1],$YY
  333. movb $TY#b,($dat,$XX[0])
  334. jne .Lcmov$i # Intel cmov is sloooow...
  335. mov $TX[0],$TX[1]
  336. .Lcmov$i:
  337. add $TX[0]#b,$TY#b
  338. xor ($dat,$TY),%r8b
  339. ror \$8,%r8d
  340. ___
  341. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  342. }
  343. for ($i=4;$i<8;$i++) {
  344. $code.=<<___;
  345. add $TX[0]#b,$YY#b
  346. lea 1($XX[0]),$XX[1]
  347. movzb ($dat,$YY),$TY#d
  348. movzb $XX[1]#b,$XX[1]#d
  349. movzb ($dat,$XX[1]),$TX[1]#d
  350. movb $TX[0]#b,($dat,$YY)
  351. cmp $XX[1],$YY
  352. movb $TY#b,($dat,$XX[0])
  353. jne .Lcmov$i # Intel cmov is sloooow...
  354. mov $TX[0],$TX[1]
  355. .Lcmov$i:
  356. add $TX[0]#b,$TY#b
  357. xor ($dat,$TY),%r9b
  358. ror \$8,%r9d
  359. ___
  360. push(@TX,shift(@TX)); push(@XX,shift(@XX)); # "rotate" registers
  361. }
  362. $code.=<<___;
  363. lea -8($len),$len
  364. mov %r8d,($out)
  365. lea 8($inp),$inp
  366. mov %r9d,4($out)
  367. lea 8($out),$out
  368. test \$-8,$len
  369. jnz .Lcloop8
  370. cmp \$0,$len
  371. jne .Lcloop1
  372. jmp .Lexit
  373. ___
  374. $code.=<<___;
  375. .align 16
  376. .Lcloop1:
  377. add $TX[0]#b,$YY#b
  378. movzb $YY#b,$YY#d
  379. movzb ($dat,$YY),$TY#d
  380. movb $TX[0]#b,($dat,$YY)
  381. movb $TY#b,($dat,$XX[0])
  382. add $TX[0]#b,$TY#b
  383. add \$1,$XX[0]#b
  384. movzb $TY#b,$TY#d
  385. movzb $XX[0]#b,$XX[0]#d
  386. movzb ($dat,$TY),$TY#d
  387. movzb ($dat,$XX[0]),$TX[0]#d
  388. xorb ($inp),$TY#b
  389. lea 1($inp),$inp
  390. movb $TY#b,($out)
  391. lea 1($out),$out
  392. sub \$1,$len
  393. jnz .Lcloop1
  394. jmp .Lexit
  395. .align 16
  396. .Lexit:
  397. sub \$1,$XX[0]#b
  398. movl $XX[0]#d,-8($dat)
  399. movl $YY#d,-4($dat)
  400. mov (%rsp),%r13
  401. .cfi_restore %r13
  402. mov 8(%rsp),%r12
  403. .cfi_restore %r12
  404. mov 16(%rsp),%rbx
  405. .cfi_restore %rbx
  406. add \$24,%rsp
  407. .cfi_adjust_cfa_offset -24
  408. .Lepilogue:
  409. ret
  410. .cfi_endproc
  411. .size RC4,.-RC4
  412. ___
  413. }
  414. $idx="%r8";
  415. $ido="%r9";
  416. $code.=<<___;
  417. .globl RC4_set_key
  418. .type RC4_set_key,\@function,3
  419. .align 16
  420. RC4_set_key:
  421. lea 8($dat),$dat
  422. lea ($inp,$len),$inp
  423. neg $len
  424. mov $len,%rcx
  425. xor %eax,%eax
  426. xor $ido,$ido
  427. xor %r10,%r10
  428. xor %r11,%r11
  429. mov OPENSSL_ia32cap_P(%rip),$idx#d
  430. bt \$20,$idx#d # RC4_CHAR?
  431. jc .Lc1stloop
  432. jmp .Lw1stloop
  433. .align 16
  434. .Lw1stloop:
  435. mov %eax,($dat,%rax,4)
  436. add \$1,%al
  437. jnc .Lw1stloop
  438. xor $ido,$ido
  439. xor $idx,$idx
  440. .align 16
  441. .Lw2ndloop:
  442. mov ($dat,$ido,4),%r10d
  443. add ($inp,$len,1),$idx#b
  444. add %r10b,$idx#b
  445. add \$1,$len
  446. mov ($dat,$idx,4),%r11d
  447. cmovz %rcx,$len
  448. mov %r10d,($dat,$idx,4)
  449. mov %r11d,($dat,$ido,4)
  450. add \$1,$ido#b
  451. jnc .Lw2ndloop
  452. jmp .Lexit_key
  453. .align 16
  454. .Lc1stloop:
  455. mov %al,($dat,%rax)
  456. add \$1,%al
  457. jnc .Lc1stloop
  458. xor $ido,$ido
  459. xor $idx,$idx
  460. .align 16
  461. .Lc2ndloop:
  462. mov ($dat,$ido),%r10b
  463. add ($inp,$len),$idx#b
  464. add %r10b,$idx#b
  465. add \$1,$len
  466. mov ($dat,$idx),%r11b
  467. jnz .Lcnowrap
  468. mov %rcx,$len
  469. .Lcnowrap:
  470. mov %r10b,($dat,$idx)
  471. mov %r11b,($dat,$ido)
  472. add \$1,$ido#b
  473. jnc .Lc2ndloop
  474. movl \$-1,256($dat)
  475. .align 16
  476. .Lexit_key:
  477. xor %eax,%eax
  478. mov %eax,-8($dat)
  479. mov %eax,-4($dat)
  480. ret
  481. .size RC4_set_key,.-RC4_set_key
  482. .globl RC4_options
  483. .type RC4_options,\@abi-omnipotent
  484. .align 16
  485. RC4_options:
  486. lea .Lopts(%rip),%rax
  487. mov OPENSSL_ia32cap_P(%rip),%edx
  488. bt \$20,%edx
  489. jc .L8xchar
  490. bt \$30,%edx
  491. jnc .Ldone
  492. add \$25,%rax
  493. ret
  494. .L8xchar:
  495. add \$12,%rax
  496. .Ldone:
  497. ret
  498. .align 64
  499. .Lopts:
  500. .asciz "rc4(8x,int)"
  501. .asciz "rc4(8x,char)"
  502. .asciz "rc4(16x,int)"
  503. .asciz "RC4 for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  504. .align 64
  505. .size RC4_options,.-RC4_options
  506. ___
  507. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  508. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  509. if ($win64) {
  510. $rec="%rcx";
  511. $frame="%rdx";
  512. $context="%r8";
  513. $disp="%r9";
  514. $code.=<<___;
  515. .extern __imp_RtlVirtualUnwind
  516. .type stream_se_handler,\@abi-omnipotent
  517. .align 16
  518. stream_se_handler:
  519. push %rsi
  520. push %rdi
  521. push %rbx
  522. push %rbp
  523. push %r12
  524. push %r13
  525. push %r14
  526. push %r15
  527. pushfq
  528. sub \$64,%rsp
  529. mov 120($context),%rax # pull context->Rax
  530. mov 248($context),%rbx # pull context->Rip
  531. lea .Lprologue(%rip),%r10
  532. cmp %r10,%rbx # context->Rip<prologue label
  533. jb .Lin_prologue
  534. mov 152($context),%rax # pull context->Rsp
  535. lea .Lepilogue(%rip),%r10
  536. cmp %r10,%rbx # context->Rip>=epilogue label
  537. jae .Lin_prologue
  538. lea 24(%rax),%rax
  539. mov -8(%rax),%rbx
  540. mov -16(%rax),%r12
  541. mov -24(%rax),%r13
  542. mov %rbx,144($context) # restore context->Rbx
  543. mov %r12,216($context) # restore context->R12
  544. mov %r13,224($context) # restore context->R13
  545. .Lin_prologue:
  546. mov 8(%rax),%rdi
  547. mov 16(%rax),%rsi
  548. mov %rax,152($context) # restore context->Rsp
  549. mov %rsi,168($context) # restore context->Rsi
  550. mov %rdi,176($context) # restore context->Rdi
  551. jmp .Lcommon_seh_exit
  552. .size stream_se_handler,.-stream_se_handler
  553. .type key_se_handler,\@abi-omnipotent
  554. .align 16
  555. key_se_handler:
  556. push %rsi
  557. push %rdi
  558. push %rbx
  559. push %rbp
  560. push %r12
  561. push %r13
  562. push %r14
  563. push %r15
  564. pushfq
  565. sub \$64,%rsp
  566. mov 152($context),%rax # pull context->Rsp
  567. mov 8(%rax),%rdi
  568. mov 16(%rax),%rsi
  569. mov %rsi,168($context) # restore context->Rsi
  570. mov %rdi,176($context) # restore context->Rdi
  571. .Lcommon_seh_exit:
  572. mov 40($disp),%rdi # disp->ContextRecord
  573. mov $context,%rsi # context
  574. mov \$154,%ecx # sizeof(CONTEXT)
  575. .long 0xa548f3fc # cld; rep movsq
  576. mov $disp,%rsi
  577. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  578. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  579. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  580. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  581. mov 40(%rsi),%r10 # disp->ContextRecord
  582. lea 56(%rsi),%r11 # &disp->HandlerData
  583. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  584. mov %r10,32(%rsp) # arg5
  585. mov %r11,40(%rsp) # arg6
  586. mov %r12,48(%rsp) # arg7
  587. mov %rcx,56(%rsp) # arg8, (NULL)
  588. call *__imp_RtlVirtualUnwind(%rip)
  589. mov \$1,%eax # ExceptionContinueSearch
  590. add \$64,%rsp
  591. popfq
  592. pop %r15
  593. pop %r14
  594. pop %r13
  595. pop %r12
  596. pop %rbp
  597. pop %rbx
  598. pop %rdi
  599. pop %rsi
  600. ret
  601. .size key_se_handler,.-key_se_handler
  602. .section .pdata
  603. .align 4
  604. .rva .LSEH_begin_RC4
  605. .rva .LSEH_end_RC4
  606. .rva .LSEH_info_RC4
  607. .rva .LSEH_begin_RC4_set_key
  608. .rva .LSEH_end_RC4_set_key
  609. .rva .LSEH_info_RC4_set_key
  610. .section .xdata
  611. .align 8
  612. .LSEH_info_RC4:
  613. .byte 9,0,0,0
  614. .rva stream_se_handler
  615. .LSEH_info_RC4_set_key:
  616. .byte 9,0,0,0
  617. .rva key_se_handler
  618. ___
  619. }
  620. sub reg_part {
  621. my ($reg,$conv)=@_;
  622. if ($reg =~ /%r[0-9]+/) { $reg .= $conv; }
  623. elsif ($conv eq "b") { $reg =~ s/%[er]([^x]+)x?/%$1l/; }
  624. elsif ($conv eq "w") { $reg =~ s/%[er](.+)/%$1/; }
  625. elsif ($conv eq "d") { $reg =~ s/%[er](.+)/%e$1/; }
  626. return $reg;
  627. }
  628. $code =~ s/(%[a-z0-9]+)#([bwd])/reg_part($1,$2)/gem;
  629. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  630. print $code;
  631. close STDOUT;