thunks.S 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * thunks.S - assembly helpers for mixed-bitness code
  3. * Copyright (c) 2015 Andrew Lutomirski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * These are little helpers that make it easier to switch bitness on
  15. * the fly.
  16. */
  17. .text
  18. .global call32_from_64
  19. .type call32_from_64, @function
  20. call32_from_64:
  21. // rdi: stack to use
  22. // esi: function to call
  23. // Save registers
  24. pushq %rbx
  25. pushq %rbp
  26. pushq %r12
  27. pushq %r13
  28. pushq %r14
  29. pushq %r15
  30. pushfq
  31. // Switch stacks
  32. mov %rsp,(%rdi)
  33. mov %rdi,%rsp
  34. // Switch to compatibility mode
  35. pushq $0x23 /* USER32_CS */
  36. pushq $1f
  37. lretq
  38. 1:
  39. .code32
  40. // Call the function
  41. call *%esi
  42. // Switch back to long mode
  43. jmp $0x33,$1f
  44. .code64
  45. 1:
  46. // Restore the stack
  47. mov (%rsp),%rsp
  48. // Restore registers
  49. popfq
  50. popq %r15
  51. popq %r14
  52. popq %r13
  53. popq %r12
  54. popq %rbp
  55. popq %rbx
  56. ret
  57. .size call32_from_64, .-call32_from_64