getcontext_light.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <ucontext.h>
  2. #include <stddef.h>
  3. extern "C" void getcontext_light(ucontext_t *ctx);
  4. // clang's built-in asm cannot handle .set directives
  5. #if defined(__GNUC__) && !defined(__llvm__) && defined(__x86_64) && defined(_LP64)
  6. #define R(r) offsetof(ucontext_t, uc_mcontext.gregs[r])
  7. static __attribute__((used))
  8. void getcontext_tramp(ucontext_t *ctx) {
  9. __asm__ __volatile__(".set oRBX, %c0\n"
  10. ".set oRBP, %c1\n"
  11. ".set oR12, %c2\n"
  12. ".set oR13, %c3\n"
  13. ".set oR14, %c4\n"
  14. ".set oR15, %c5\n"
  15. ".set oRIP, %c6\n"
  16. ".set oRSP, %c7\n"
  17. : :
  18. "p" (R(REG_RBX)),
  19. "p" (R(REG_RBP)),
  20. "p" (R(REG_R12)),
  21. "p" (R(REG_R13)),
  22. "p" (R(REG_R14)),
  23. "p" (R(REG_R15)),
  24. "p" (R(REG_RIP)),
  25. "p" (R(REG_RSP)));
  26. getcontext_light(ctx);
  27. }
  28. __asm__(".pushsection .text; .globl getcontext_light\n"
  29. ".type getcontext_light, @function\n"
  30. "getcontext_light:\n"
  31. "\t.cfi_startproc\n"
  32. "\tmovq %rbx, oRBX(%rdi)\n"
  33. "\tmovq %rbp, oRBP(%rdi)\n"
  34. "\tmovq %r12, oR12(%rdi)\n"
  35. "\tmovq %r13, oR13(%rdi)\n"
  36. "\tmovq %r14, oR14(%rdi)\n"
  37. "\tmovq %r15, oR14(%rdi)\n"
  38. "\tmovq (%rsp), %rcx\n"
  39. "\tmovq %rcx, oRIP(%rdi)\n"
  40. "\tleaq 8(%rsp), %rcx\n" /* Exclude the return address. */
  41. "\tmovq %rcx, oRSP(%rdi)\n"
  42. "\tret\n"
  43. ".cfi_endproc\n"
  44. ".size getcontext_light, .-getcontext_light\n"
  45. ".popsection\n"
  46. );
  47. #else
  48. extern "C" void getcontext_light(ucontext_t *ctx) {
  49. getcontext(ctx);
  50. }
  51. #endif