2
0

rc4-x86_64.pl 16 KB

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