thunks.S 916 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * thunks.S - assembly helpers for mixed-bitness code
  4. * Copyright (c) 2015 Andrew Lutomirski
  5. *
  6. * These are little helpers that make it easier to switch bitness on
  7. * the fly.
  8. */
  9. .text
  10. .global call32_from_64
  11. .type call32_from_64, @function
  12. call32_from_64:
  13. // rdi: stack to use
  14. // esi: function to call
  15. // Save registers
  16. pushq %rbx
  17. pushq %rbp
  18. pushq %r12
  19. pushq %r13
  20. pushq %r14
  21. pushq %r15
  22. pushfq
  23. // Switch stacks
  24. mov %rsp,(%rdi)
  25. mov %rdi,%rsp
  26. // Switch to compatibility mode
  27. pushq $0x23 /* USER32_CS */
  28. pushq $1f
  29. lretq
  30. 1:
  31. .code32
  32. // Call the function
  33. call *%esi
  34. // Switch back to long mode
  35. jmp $0x33,$1f
  36. .code64
  37. 1:
  38. // Restore the stack
  39. mov (%rsp),%rsp
  40. // Restore registers
  41. popfq
  42. popq %r15
  43. popq %r14
  44. popq %r13
  45. popq %r12
  46. popq %rbp
  47. popq %rbx
  48. ret
  49. .size call32_from_64, .-call32_from_64
  50. .section .note.GNU-stack,"",%progbits