OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

893 lines
28 KiB

  1. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  2. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. #
  4. # Filename: package/.../qemu/qemu-0.9.0-gcc4.patch
  5. # Copyright (C) 2007 The OpenSDE Project
  6. #
  7. # More information can be found in the files COPYING and README.
  8. #
  9. # This patch file is dual-licensed. It is available under the license the
  10. # patched project is licensed under, as long as it is an OpenSource license
  11. # as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms
  12. # of the GNU General Public License as published by the Free Software
  13. # Foundation; either version 2 of the License, or (at your option) any later
  14. # version.
  15. # --- SDE-COPYRIGHT-NOTE-END ---
  16. 2007-04-12 rediffed for qemu 0.9.0
  17. 2005-06-02 Gwenole Beauchesne <gbeauchesne@mandriva.com>
  18. * dyngen.c (trace_i386_insn): Fix push/imul case with 8-bit
  19. immediate.
  20. 2005-05-11 Paul Brook <paul@codesourcery.com>
  21. * gcc4 host support.
  22. diff -ruN qemu-0.9.0/dyngen-exec.h qemu-0.9.0-gcc4/dyngen-exec.h
  23. --- qemu-0.9.0/dyngen-exec.h 2007-02-06 00:01:54.000000000 +0100
  24. +++ qemu-0.9.0-gcc4/dyngen-exec.h 2007-04-12 17:24:57.000000000 +0200
  25. @@ -191,7 +191,12 @@
  26. #endif
  27. /* force GCC to generate only one epilog at the end of the function */
  28. +#if defined(__i386__) || defined(__x86_64__)
  29. +/* Also add 4 bytes of padding so that we can replace the ret with a jmp. */
  30. +#define FORCE_RET() __asm__ __volatile__("nop;nop;nop;nop" : : : "memory");
  31. +#else
  32. #define FORCE_RET() __asm__ __volatile__("" : : : "memory");
  33. +#endif
  34. #ifndef OPPROTO
  35. #define OPPROTO
  36. @@ -241,12 +246,19 @@
  37. #endif
  38. #ifdef __i386__
  39. -#define EXIT_TB() asm volatile ("ret")
  40. -#define GOTO_LABEL_PARAM(n) asm volatile ("jmp " ASM_NAME(__op_gen_label) #n)
  41. +/* Dyngen will replace hlt instructions with a ret instruction. Inserting a
  42. + ret directly would confuse dyngen. */
  43. +#define EXIT_TB() asm volatile ("hlt")
  44. +/* Dyngen will replace cli with 0x9e (jmp).
  45. + We generate the offset manually. */
  46. +#define GOTO_LABEL_PARAM(n) \
  47. + asm volatile ("cli;.long " ASM_NAME(__op_gen_label) #n " - 1f;1:")
  48. #endif
  49. #ifdef __x86_64__
  50. -#define EXIT_TB() asm volatile ("ret")
  51. -#define GOTO_LABEL_PARAM(n) asm volatile ("jmp " ASM_NAME(__op_gen_label) #n)
  52. +/* The same as i386. */
  53. +#define EXIT_TB() asm volatile ("hlt")
  54. +#define GOTO_LABEL_PARAM(n) \
  55. + asm volatile ("cli;.long " ASM_NAME(__op_gen_label) #n " - 1f;1:")
  56. #endif
  57. #ifdef __powerpc__
  58. #define EXIT_TB() asm volatile ("blr")
  59. diff -ruN qemu-0.9.0/dyngen.c qemu-0.9.0-gcc4/dyngen.c
  60. --- qemu-0.9.0/dyngen.c 2007-02-06 00:01:54.000000000 +0100
  61. +++ qemu-0.9.0-gcc4/dyngen.c 2007-04-12 17:24:57.000000000 +0200
  62. @@ -32,6 +32,8 @@
  63. #include "config-host.h"
  64. +//#define DEBUG_OP
  65. +
  66. /* NOTE: we test CONFIG_WIN32 instead of _WIN32 to enabled cross
  67. compilation */
  68. #if defined(CONFIG_WIN32)
  69. @@ -1414,6 +1416,644 @@
  70. #endif
  71. +#if defined(HOST_I386) || defined(HOST_X86_64)
  72. +
  73. +/* This byte is the first byte of an instruction. */
  74. +#define FLAG_INSN (1 << 0)
  75. +/* This byte has been processed as part of an instruction. */
  76. +#define FLAG_SCANNED (1 << 1)
  77. +/* This instruction is a return instruction. Gcc cometimes generates prefix
  78. + bytes, so may be more than one byte long. */
  79. +#define FLAG_RET (1 << 2)
  80. +/* This is either the target of a jump, or the preceeding instruction uses
  81. + a pc-relative offset. */
  82. +#define FLAG_TARGET (1 << 3)
  83. +/* This is a magic instruction that needs fixing up. */
  84. +#define FLAG_EXIT (1 << 4)
  85. +#define MAX_EXITS 5
  86. +
  87. +static void
  88. +bad_opcode(const char *name, uint32_t op)
  89. +{
  90. + error("Unsupported opcode %0*x in %s", (op > 0xff) ? 4 : 2, op, name);
  91. +}
  92. +
  93. +/* Mark len bytes as scanned, Returns insn_size + len. Reports an error
  94. + if these bytes have already been scanned. */
  95. +static int
  96. +eat_bytes(const char *name, char *flags, int insn, int insn_size, int len)
  97. +{
  98. + while (len > 0) {
  99. + /* This should never occur in sane code. */
  100. + if (flags[insn + insn_size] & FLAG_SCANNED)
  101. + error ("Overlapping instructions in %s", name);
  102. + flags[insn + insn_size] |= FLAG_SCANNED;
  103. + insn_size++;
  104. + len--;
  105. + }
  106. + return insn_size;
  107. +}
  108. +
  109. +static void
  110. +trace_i386_insn (const char *name, uint8_t *start_p, char *flags, int insn,
  111. + int len)
  112. +{
  113. + uint8_t *ptr;
  114. + uint8_t op;
  115. + int modrm;
  116. + int is_prefix;
  117. + int op_size;
  118. + int addr_size;
  119. + int insn_size;
  120. + int is_ret;
  121. + int is_condjmp;
  122. + int is_jmp;
  123. + int is_exit;
  124. + int is_pcrel;
  125. + int immed;
  126. + int seen_rexw;
  127. + int32_t disp;
  128. +
  129. + ptr = start_p + insn;
  130. + /* nonzero if this insn has a ModR/M byte. */
  131. + modrm = 1;
  132. + /* The size of the immediate value in this instruction. */
  133. + immed = 0;
  134. + /* The operand size. */
  135. + op_size = 4;
  136. + /* The address size */
  137. + addr_size = 4;
  138. + /* The total length of this instruction. */
  139. + insn_size = 0;
  140. + is_prefix = 1;
  141. + is_ret = 0;
  142. + is_condjmp = 0;
  143. + is_jmp = 0;
  144. + is_exit = 0;
  145. + seen_rexw = 0;
  146. + is_pcrel = 0;
  147. +
  148. + while (is_prefix) {
  149. + op = ptr[insn_size];
  150. + insn_size = eat_bytes(name, flags, insn, insn_size, 1);
  151. + is_prefix = 0;
  152. + switch (op >> 4) {
  153. + case 0:
  154. + case 1:
  155. + case 2:
  156. + case 3:
  157. + if (op == 0x0f) {
  158. + /* two-byte opcode. */
  159. + op = ptr[insn_size];
  160. + insn_size = eat_bytes(name, flags, insn, insn_size, 1);
  161. + switch (op >> 4) {
  162. + case 0:
  163. + if ((op & 0xf) > 3)
  164. + modrm = 0;
  165. + break;
  166. + case 1: /* vector move or prefetch */
  167. + case 2: /* various moves and vector compares. */
  168. + case 4: /* cmov */
  169. + case 5: /* vector instructions */
  170. + case 6:
  171. + case 13:
  172. + case 14:
  173. + case 15:
  174. + break;
  175. + case 7: /* mmx */
  176. + if (op & 0x77) /* emms */
  177. + modrm = 0;
  178. + break;
  179. + case 3: /* wrmsr, rdtsc, rdmsr, rdpmc, sysenter, sysexit */
  180. + modrm = 0;
  181. + break;
  182. + case 8: /* long conditional jump */
  183. + is_condjmp = 1;
  184. + immed = op_size;
  185. + modrm = 0;
  186. + break;
  187. + case 9: /* setcc */
  188. + break;
  189. + case 10:
  190. + switch (op & 0x7) {
  191. + case 0: /* push fs/gs */
  192. + case 1: /* pop fs/gs */
  193. + case 2: /* cpuid/rsm */
  194. + modrm = 0;
  195. + break;
  196. + case 4: /* shld/shrd immediate */
  197. + immed = 1;
  198. + break;
  199. + default: /* Normal instructions with a ModR/M byte. */
  200. + break;
  201. + }
  202. + break;
  203. + case 11:
  204. + switch (op & 0xf) {
  205. + case 10: /* bt, bts, btr, btc */
  206. + immed = 1;
  207. + break;
  208. + default:
  209. + /* cmpxchg, lss, btr, lfs, lgs, movzx, btc, bsf, bsr
  210. + undefined, and movsx */
  211. + break;
  212. + }
  213. + break;
  214. + case 12:
  215. + if (op & 8) {
  216. + /* bswap */
  217. + modrm = 0;
  218. + } else {
  219. + switch (op & 0x7) {
  220. + case 2:
  221. + case 4:
  222. + case 5:
  223. + case 6:
  224. + immed = 1;
  225. + break;
  226. + default:
  227. + break;
  228. + }
  229. + }
  230. + break;
  231. + }
  232. + } else if ((op & 0x07) <= 0x3) {
  233. + /* General arithmentic ax. */
  234. + } else if ((op & 0x07) <= 0x5) {
  235. + /* General arithmetic ax, immediate. */
  236. + if (op & 0x01)
  237. + immed = op_size;
  238. + else
  239. + immed = 1;
  240. + modrm = 0;
  241. + } else if ((op & 0x23) == 0x22) {
  242. + /* Segment prefix. */
  243. + is_prefix = 1;
  244. + } else {
  245. + /* Segment register push/pop or DAA/AAA/DAS/AAS. */
  246. + modrm = 0;
  247. + }
  248. + break;
  249. +
  250. +#if defined(HOST_X86_64)
  251. + case 4: /* rex prefix. */
  252. + is_prefix = 1;
  253. + /* The address/operand size is actually 64-bit, but the immediate
  254. + values in the instruction are still 32-bit. */
  255. + op_size = 4;
  256. + addr_size = 4;
  257. + if (op & 8)
  258. + seen_rexw = 1;
  259. + break;
  260. +#else
  261. + case 4: /* inc/dec register. */
  262. +#endif
  263. + case 5: /* push/pop general register. */
  264. + modrm = 0;
  265. + break;
  266. +
  267. + case 6:
  268. + switch (op & 0x0f) {
  269. + case 0: /* pusha */
  270. + case 1: /* popa */
  271. + modrm = 0;
  272. + break;
  273. + case 2: /* bound */
  274. + case 3: /* arpl */
  275. + break;
  276. + case 4: /* FS */
  277. + case 5: /* GS */
  278. + is_prefix = 1;
  279. + break;
  280. + case 6: /* opcode size prefix. */
  281. + op_size = 2;
  282. + is_prefix = 1;
  283. + break;
  284. + case 7: /* Address size prefix. */
  285. + addr_size = 2;
  286. + is_prefix = 1;
  287. + break;
  288. + case 8: /* push immediate */
  289. + immed = op_size;
  290. + modrm = 0;
  291. + break;
  292. + case 10: /* push 8-bit immediate */
  293. + immed = 1;
  294. + modrm = 0;
  295. + break;
  296. + case 9: /* imul immediate */
  297. + immed = op_size;
  298. + break;
  299. + case 11: /* imul 8-bit immediate */
  300. + immed = 1;
  301. + break;
  302. + case 12: /* insb */
  303. + case 13: /* insw */
  304. + case 14: /* outsb */
  305. + case 15: /* outsw */
  306. + modrm = 0;
  307. + break;
  308. + }
  309. + break;
  310. +
  311. + case 7: /* Short conditional jump. */
  312. + is_condjmp = 1;
  313. + immed = 1;
  314. + modrm = 0;
  315. + break;
  316. +
  317. + case 8:
  318. + if ((op & 0xf) <= 3) {
  319. + /* arithmetic immediate. */
  320. + if ((op & 3) == 1)
  321. + immed = op_size;
  322. + else
  323. + immed = 1;
  324. + }
  325. + /* else test, xchg, mov, lea or pop general. */
  326. + break;
  327. +
  328. + case 9:
  329. + /* Various single-byte opcodes with no modrm byte. */
  330. + modrm = 0;
  331. + if (op == 10) {
  332. + /* Call */
  333. + immed = 4;
  334. + }
  335. + break;
  336. +
  337. + case 10:
  338. + switch ((op & 0xe) >> 1) {
  339. + case 0: /* mov absoliute immediate. */
  340. + case 1:
  341. + if (seen_rexw)
  342. + immed = 8;
  343. + else
  344. + immed = addr_size;
  345. + break;
  346. + case 4: /* test immediate. */
  347. + if (op & 1)
  348. + immed = op_size;
  349. + else
  350. + immed = 1;
  351. + break;
  352. + default: /* Various string ops. */
  353. + break;
  354. + }
  355. + modrm = 0;
  356. + break;
  357. +
  358. + case 11: /* move immediate to register */
  359. + if (op & 8) {
  360. + if (seen_rexw)
  361. + immed = 8;
  362. + else
  363. + immed = op_size;
  364. + } else {
  365. + immed = 1;
  366. + }
  367. + modrm = 0;
  368. + break;
  369. +
  370. + case 12:
  371. + switch (op & 0xf) {
  372. + case 0: /* shift immediate */
  373. + case 1:
  374. + immed = 1;
  375. + break;
  376. + case 2: /* ret immediate */
  377. + immed = 2;
  378. + modrm = 0;
  379. + bad_opcode(name, op);
  380. + break;
  381. + case 3: /* ret */
  382. + modrm = 0;
  383. + is_ret = 1;
  384. + case 4: /* les */
  385. + case 5: /* lds */
  386. + break;
  387. + case 6: /* mov immediate byte */
  388. + immed = 1;
  389. + break;
  390. + case 7: /* mov immediate */
  391. + immed = op_size;
  392. + break;
  393. + case 8: /* enter */
  394. + /* TODO: Is this right? */
  395. + immed = 3;
  396. + modrm = 0;
  397. + break;
  398. + case 10: /* retf immediate */
  399. + immed = 2;
  400. + modrm = 0;
  401. + bad_opcode(name, op);
  402. + break;
  403. + case 13: /* int */
  404. + immed = 1;
  405. + modrm = 0;
  406. + break;
  407. + case 11: /* retf */
  408. + case 15: /* iret */
  409. + modrm = 0;
  410. + bad_opcode(name, op);
  411. + break;
  412. + default: /* leave, int3 or into */
  413. + modrm = 0;
  414. + break;
  415. + }
  416. + break;
  417. +
  418. + case 13:
  419. + if ((op & 0xf) >= 8) {
  420. + /* Coprocessor escape. For our purposes this is just a normal
  421. + instruction with a ModR/M byte. */
  422. + } else if ((op & 0xf) >= 4) {
  423. + /* AAM, AAD or XLAT */
  424. + modrm = 0;
  425. + }
  426. + /* else shift instruction */
  427. + break;
  428. +
  429. + case 14:
  430. + switch ((op & 0xc) >> 2) {
  431. + case 0: /* loop or jcxz */
  432. + is_condjmp = 1;
  433. + immed = 1;
  434. + break;
  435. + case 1: /* in/out immed */
  436. + immed = 1;
  437. + break;
  438. + case 2: /* call or jmp */
  439. + switch (op & 3) {
  440. + case 0: /* call */
  441. + immed = op_size;
  442. + break;
  443. + case 1: /* long jump */
  444. + immed = 4;
  445. + is_jmp = 1;
  446. + break;
  447. + case 2: /* far jmp */
  448. + bad_opcode(name, op);
  449. + break;
  450. + case 3: /* short jmp */
  451. + immed = 1;
  452. + is_jmp = 1;
  453. + break;
  454. + }
  455. + break;
  456. + case 3: /* in/out register */
  457. + break;
  458. + }
  459. + modrm = 0;
  460. + break;
  461. +
  462. + case 15:
  463. + switch ((op & 0xe) >> 1) {
  464. + case 0:
  465. + case 1:
  466. + is_prefix = 1;
  467. + break;
  468. + case 2:
  469. + case 4:
  470. + case 5:
  471. + case 6:
  472. + modrm = 0;
  473. + /* Some privileged insns are used as markers. */
  474. + switch (op) {
  475. + case 0xf4: /* hlt: Exit translation block. */
  476. + is_exit = 1;
  477. + break;
  478. + case 0xfa: /* cli: Jump to label. */
  479. + is_exit = 1;
  480. + immed = 4;
  481. + break;
  482. + case 0xfb: /* sti: TB patch jump. */
  483. + /* Mark the insn for patching, but continue sscanning. */
  484. + flags[insn] |= FLAG_EXIT;
  485. + immed = 4;
  486. + break;
  487. + }
  488. + break;
  489. + case 3: /* unary grp3 */
  490. + if ((ptr[insn_size] & 0x38) == 0) {
  491. + if (op == 0xf7)
  492. + immed = op_size;
  493. + else
  494. + immed = 1; /* test immediate */
  495. + }
  496. + break;
  497. + case 7: /* inc/dec grp4/5 */
  498. + /* TODO: This includes indirect jumps. We should fail if we
  499. + encounter one of these. */
  500. + break;
  501. + }
  502. + break;
  503. + }
  504. + }
  505. +
  506. + if (modrm) {
  507. + if (addr_size != 4)
  508. + error("16-bit addressing mode used in %s", name);
  509. +
  510. + disp = 0;
  511. + modrm = ptr[insn_size];
  512. + insn_size = eat_bytes(name, flags, insn, insn_size, 1);
  513. + modrm &= 0xc7;
  514. + switch ((modrm & 0xc0) >> 6) {
  515. + case 0:
  516. + if (modrm == 5)
  517. + disp = 4;
  518. + break;
  519. + case 1:
  520. + disp = 1;
  521. + break;
  522. + case 2:
  523. + disp = 4;
  524. + break;
  525. + }
  526. + if ((modrm & 0xc0) != 0xc0 && (modrm & 0x7) == 4) {
  527. + /* SIB byte */
  528. + if (modrm == 4 && (ptr[insn_size] & 0x7) == 5) {
  529. + disp = 4;
  530. + is_pcrel = 1;
  531. + }
  532. + insn_size = eat_bytes(name, flags, insn, insn_size, 1);
  533. + }
  534. + insn_size = eat_bytes(name, flags, insn, insn_size, disp);
  535. + }
  536. + insn_size = eat_bytes(name, flags, insn, insn_size, immed);
  537. + if (is_condjmp || is_jmp) {
  538. + if (immed == 1) {
  539. + disp = (int8_t)*(ptr + insn_size - 1);
  540. + } else {
  541. + disp = (((int32_t)*(ptr + insn_size - 1)) << 24)
  542. + | (((int32_t)*(ptr + insn_size - 2)) << 16)
  543. + | (((int32_t)*(ptr + insn_size - 3)) << 8)
  544. + | *(ptr + insn_size - 4);
  545. + }
  546. + disp += insn_size;
  547. + /* Jumps to external symbols point to the address of the offset
  548. + before relocation. */
  549. + /* ??? These are probably a tailcall. We could fix them up by
  550. + replacing them with jmp to EOB + call, but it's easier to just
  551. + prevent the compiler generating them. */
  552. + if (disp == 1)
  553. + error("Unconditional jump (sibcall?) in %s", name);
  554. + disp += insn;
  555. + if (disp < 0 || disp > len)
  556. + error("Jump outside instruction in %s", name);
  557. +
  558. + if ((flags[disp] & (FLAG_INSN | FLAG_SCANNED)) == FLAG_SCANNED)
  559. + error("Overlapping instructions in %s", name);
  560. +
  561. + flags[disp] |= (FLAG_INSN | FLAG_TARGET);
  562. + is_pcrel = 1;
  563. + }
  564. + if (is_pcrel) {
  565. + /* Mark the following insn as a jump target. This will stop
  566. + this instruction being moved. */
  567. + flags[insn + insn_size] |= FLAG_TARGET;
  568. + }
  569. + if (is_ret)
  570. + flags[insn] |= FLAG_RET;
  571. +
  572. + if (is_exit)
  573. + flags[insn] |= FLAG_EXIT;
  574. +
  575. + if (!(is_jmp || is_ret || is_exit))
  576. + flags[insn + insn_size] |= FLAG_INSN;
  577. +}
  578. +
  579. +/* Scan a function body. Returns the position of the return sequence.
  580. + Sets *patch_bytes to the number of bytes that need to be copied from that
  581. + location. If no patching is required (ie. the return is the last insn)
  582. + *patch_bytes will be set to -1. *plen is the number of code bytes to copy.
  583. + */
  584. +static int trace_i386_op(const char * name, uint8_t *start_p, int *plen,
  585. + int *patch_bytes, int *exit_addrs)
  586. +{
  587. + char *flags;
  588. + int more;
  589. + int insn;
  590. + int retpos;
  591. + int bytes;
  592. + int num_exits;
  593. + int len;
  594. + int last_insn;
  595. +
  596. + len = *plen;
  597. + flags = malloc(len + 1);
  598. + memset(flags, 0, len + 1);
  599. + flags[0] |= FLAG_INSN;
  600. + more = 1;
  601. + while (more) {
  602. + more = 0;
  603. + for (insn = 0; insn < len; insn++) {
  604. + if ((flags[insn] & (FLAG_INSN | FLAG_SCANNED)) == FLAG_INSN) {
  605. + trace_i386_insn(name, start_p, flags, insn, len);
  606. + more = 1;
  607. + }
  608. + }
  609. + }
  610. +
  611. + /* Strip any unused code at the end of the function. */
  612. + while (len > 0 && flags[len - 1] == 0)
  613. + len--;
  614. +
  615. + retpos = -1;
  616. + num_exits = 0;
  617. + last_insn = 0;
  618. + for (insn = 0; insn < len; insn++) {
  619. + if (flags[insn] & FLAG_RET) {
  620. + /* ??? In theory it should be possible to handle multiple return
  621. + points. In practice it's not worth the effort. */
  622. + if (retpos != -1)
  623. + error("Multiple return instructions in %s", name);
  624. + retpos = insn;
  625. + }
  626. + if (flags[insn] & FLAG_EXIT) {
  627. + if (num_exits == MAX_EXITS)
  628. + error("Too many block exits in %s", name);
  629. + exit_addrs[num_exits] = insn;
  630. + num_exits++;
  631. + }
  632. + if (flags[insn] & FLAG_INSN)
  633. + last_insn = insn;
  634. + }
  635. +
  636. + exit_addrs[num_exits] = -1;
  637. + if (retpos == -1) {
  638. + if (num_exits == 0) {
  639. + error ("No return instruction found in %s", name);
  640. + } else {
  641. + retpos = len;
  642. + last_insn = len;
  643. + }
  644. + }
  645. +
  646. + /* If the return instruction is the last instruction we can just
  647. + remove it. */
  648. + if (retpos == last_insn)
  649. + *patch_bytes = -1;
  650. + else
  651. + *patch_bytes = 0;
  652. +
  653. + /* Back up over any nop instructions. */
  654. + while (retpos > 0
  655. + && (flags[retpos] & FLAG_TARGET) == 0
  656. + && (flags[retpos - 1] & FLAG_INSN) != 0
  657. + && start_p[retpos - 1] == 0x90) {
  658. + retpos--;
  659. + }
  660. +
  661. + if (*patch_bytes == -1) {
  662. + *plen = retpos;
  663. + free (flags);
  664. + return retpos;
  665. + }
  666. + *plen = len;
  667. +
  668. + /* The ret is in the middle of the function. Find four more bytes that
  669. + so the ret can be replaced by a jmp. */
  670. + /* ??? Use a short jump where possible. */
  671. + bytes = 4;
  672. + insn = retpos + 1;
  673. + /* We can clobber everything up to the next jump target. */
  674. + while (insn < len && bytes > 0 && (flags[insn] & FLAG_TARGET) == 0) {
  675. + insn++;
  676. + bytes--;
  677. + }
  678. + if (bytes > 0) {
  679. + /* ???: Strip out nop blocks. */
  680. + /* We can't do the replacement without clobbering anything important.
  681. + Copy preceeding instructions(s) to give us some space. */
  682. + while (retpos > 0) {
  683. + /* If this byte is the target of a jmp we can't move it. */
  684. + if (flags[retpos] & FLAG_TARGET)
  685. + break;
  686. +
  687. + (*patch_bytes)++;
  688. + bytes--;
  689. + retpos--;
  690. +
  691. + /* Break out of the loop if we have enough space and this is either
  692. + the first byte of an instruction or a pad byte. */
  693. + if ((flags[retpos] & (FLAG_INSN | FLAG_SCANNED)) != FLAG_SCANNED
  694. + && bytes <= 0) {
  695. + break;
  696. + }
  697. + }
  698. + }
  699. +
  700. + if (bytes > 0)
  701. + error("Unable to replace ret with jmp in %s\n", name);
  702. +
  703. + free(flags);
  704. + return retpos;
  705. +}
  706. +
  707. +#endif
  708. +
  709. #define MAX_ARGS 3
  710. /* generate op code */
  711. @@ -1427,6 +2067,11 @@
  712. uint8_t args_present[MAX_ARGS];
  713. const char *sym_name, *p;
  714. EXE_RELOC *rel;
  715. +#if defined(HOST_I386) || defined(HOST_X86_64)
  716. + int patch_bytes;
  717. + int retpos;
  718. + int exit_addrs[MAX_EXITS];
  719. +#endif
  720. /* Compute exact size excluding prologue and epilogue instructions.
  721. * Increment start_offset to skip epilogue instructions, then compute
  722. @@ -1437,33 +2082,12 @@
  723. p_end = p_start + size;
  724. start_offset = offset;
  725. #if defined(HOST_I386) || defined(HOST_X86_64)
  726. -#ifdef CONFIG_FORMAT_COFF
  727. - {
  728. - uint8_t *p;
  729. - p = p_end - 1;
  730. - if (p == p_start)
  731. - error("empty code for %s", name);
  732. - while (*p != 0xc3) {
  733. - p--;
  734. - if (p <= p_start)
  735. - error("ret or jmp expected at the end of %s", name);
  736. - }
  737. - copy_size = p - p_start;
  738. - }
  739. -#else
  740. {
  741. int len;
  742. len = p_end - p_start;
  743. - if (len == 0)
  744. - error("empty code for %s", name);
  745. - if (p_end[-1] == 0xc3) {
  746. - len--;
  747. - } else {
  748. - error("ret or jmp expected at the end of %s", name);
  749. - }
  750. + retpos = trace_i386_op(name, p_start, &len, &patch_bytes, exit_addrs);
  751. copy_size = len;
  752. }
  753. -#endif
  754. #elif defined(HOST_PPC)
  755. {
  756. uint8_t *p;
  757. @@ -1675,6 +2299,13 @@
  758. }
  759. if (gen_switch == 2) {
  760. +#if defined(HOST_I386) || defined(HOST_X86_64)
  761. + if (patch_bytes != -1)
  762. + copy_size += patch_bytes;
  763. +#ifdef DEBUG_OP
  764. + copy_size += 2;
  765. +#endif
  766. +#endif
  767. fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
  768. } else if (gen_switch == 1) {
  769. @@ -1879,7 +2510,43 @@
  770. #error unsupport object format
  771. #endif
  772. }
  773. + }
  774. + /* Replace the marker instructions with the actual opcodes. */
  775. + for (i = 0; exit_addrs[i] != -1; i++) {
  776. + int op;
  777. + switch (p_start[exit_addrs[i]])
  778. + {
  779. + case 0xf4: op = 0xc3; break; /* hlt -> ret */
  780. + case 0xfa: op = 0xe9; break; /* cli -> jmp */
  781. + case 0xfb: op = 0xe9; break; /* sti -> jmp */
  782. + default: error("Internal error");
  783. + }
  784. + fprintf(outfile,
  785. + " *(uint8_t *)(gen_code_ptr + %d) = 0x%x;\n",
  786. + exit_addrs[i], op);
  787. }
  788. + /* Fix up the return instruction. */
  789. + if (patch_bytes != -1) {
  790. + if (patch_bytes) {
  791. + fprintf(outfile, " memcpy(gen_code_ptr + %d,"
  792. + "gen_code_ptr + %d, %d);\n",
  793. + copy_size, retpos, patch_bytes);
  794. + }
  795. + fprintf(outfile,
  796. + " *(uint8_t *)(gen_code_ptr + %d) = 0xe9;\n",
  797. + retpos);
  798. + fprintf(outfile,
  799. + " *(uint32_t *)(gen_code_ptr + %d) = 0x%x;\n",
  800. + retpos + 1, copy_size - (retpos + 5));
  801. +
  802. + copy_size += patch_bytes;
  803. + }
  804. +#ifdef DEBUG_OP
  805. + fprintf(outfile,
  806. + " *(uint16_t *)(gen_code_ptr + %d) = 0x9090;\n",
  807. + copy_size);
  808. + copy_size += 2;
  809. +#endif
  810. }
  811. #elif defined(HOST_X86_64)
  812. {
  813. @@ -1913,6 +2580,42 @@
  814. }
  815. }
  816. }
  817. + /* Replace the marker instructions with the actual opcodes. */
  818. + for (i = 0; exit_addrs[i] != -1; i++) {
  819. + int op;
  820. + switch (p_start[exit_addrs[i]])
  821. + {
  822. + case 0xf4: op = 0xc3; break; /* hlt -> ret */
  823. + case 0xfa: op = 0xe9; break; /* cli -> jmp */
  824. + case 0xfb: op = 0xe9; break; /* sti -> jmp */
  825. + default: error("Internal error");
  826. + }
  827. + fprintf(outfile,
  828. + " *(uint8_t *)(gen_code_ptr + %d) = 0x%x;\n",
  829. + exit_addrs[i], op);
  830. + }
  831. + /* Fix up the return instruction. */
  832. + if (patch_bytes != -1) {
  833. + if (patch_bytes) {
  834. + fprintf(outfile, " memcpy(gen_code_ptr + %d,"
  835. + "gen_code_ptr + %d, %d);\n",
  836. + copy_size, retpos, patch_bytes);
  837. + }
  838. + fprintf(outfile,
  839. + " *(uint8_t *)(gen_code_ptr + %d) = 0xe9;\n",
  840. + retpos);
  841. + fprintf(outfile,
  842. + " *(uint32_t *)(gen_code_ptr + %d) = 0x%x;\n",
  843. + retpos + 1, copy_size - (retpos + 5));
  844. +
  845. + copy_size += patch_bytes;
  846. + }
  847. +#ifdef DEBUG_OP
  848. + fprintf(outfile,
  849. + " *(uint16_t *)(gen_code_ptr + %d) = 0x9090;\n",
  850. + copy_size);
  851. + copy_size += 2;
  852. +#endif
  853. }
  854. #elif defined(HOST_PPC)
  855. {
  856. diff -ruN qemu-0.9.0/exec-all.h qemu-0.9.0-gcc4/exec-all.h
  857. --- qemu-0.9.0/exec-all.h 2007-02-06 00:01:54.000000000 +0100
  858. +++ qemu-0.9.0-gcc4/exec-all.h 2007-04-12 17:24:57.000000000 +0200
  859. @@ -326,14 +326,15 @@
  860. #elif defined(__i386__) && defined(USE_DIRECT_JUMP)
  861. -/* we patch the jump instruction directly */
  862. +/* we patch the jump instruction directly. Use sti in place of the actual
  863. + jmp instruction so that dyngen can patch in the correct result. */
  864. #define GOTO_TB(opname, tbparam, n)\
  865. do {\
  866. asm volatile (".section .data\n"\
  867. ASM_OP_LABEL_NAME(n, opname) ":\n"\
  868. ".long 1f\n"\
  869. ASM_PREVIOUS_SECTION \
  870. - "jmp " ASM_NAME(__op_jmp) #n "\n"\
  871. + "sti;.long " ASM_NAME(__op_jmp) #n " - 1f\n"\
  872. "1:\n");\
  873. } while (0)