swapcontext.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Save current context and install the given one.
  2. Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Andreas Jaeger <aj@suse.de>, 2002.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <sysdep.h>
  17. #include "ucontext_i.h"
  18. /* int __swapcontext (ucontext_t *oucp, const ucontext_t *ucp);
  19. Saves the machine context in oucp such that when it is activated,
  20. it appears as if __swapcontextt() returned again, restores the
  21. machine context in ucp and thereby resumes execution in that
  22. context.
  23. This implementation is intended to be used for *synchronous* context
  24. switches only. Therefore, it does not have to save anything
  25. other than the PRESERVED state. */
  26. ENTRY(__swapcontext)
  27. /* Save the preserved registers, the registers used for passing args,
  28. and the return address. */
  29. movq %rbx, oRBX(%rdi)
  30. movq %rbp, oRBP(%rdi)
  31. movq %r12, oR12(%rdi)
  32. movq %r13, oR13(%rdi)
  33. movq %r14, oR14(%rdi)
  34. movq %r15, oR15(%rdi)
  35. movq %rdi, oRDI(%rdi)
  36. movq %rsi, oRSI(%rdi)
  37. movq %rdx, oRDX(%rdi)
  38. movq %rcx, oRCX(%rdi)
  39. movq %r8, oR8(%rdi)
  40. movq %r9, oR9(%rdi)
  41. movq (%rsp), %rcx
  42. movq %rcx, oRIP(%rdi)
  43. leaq 8(%rsp), %rcx /* Exclude the return address. */
  44. movq %rcx, oRSP(%rdi)
  45. /* We have separate floating-point register content memory on the
  46. stack. We use the __fpregs_mem block in the context. Set the
  47. links up correctly. */
  48. leaq oFPREGSMEM(%rdi), %rcx
  49. movq %rcx, oFPREGS(%rdi)
  50. /* Save the floating-point environment. */
  51. fnstenv (%rcx)
  52. stmxcsr oMXCSR(%rdi)
  53. /* The syscall destroys some registers, save them. */
  54. movq %rsi, %r12
  55. /* Save the current signal mask and install the new one with
  56. rt_sigprocmask (SIG_BLOCK, newset, oldset,_NSIG/8). */
  57. leaq oSIGMASK(%rdi), %rdx
  58. leaq oSIGMASK(%rsi), %rsi
  59. movl $SIG_SETMASK, %edi
  60. movl $_NSIG8,%r10d
  61. movl $__NR_rt_sigprocmask, %eax
  62. syscall
  63. cmpq $-4095, %rax /* Check %rax for error. */
  64. jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
  65. /* Restore destroyed registers. */
  66. movq %r12, %rsi
  67. /* Restore the floating-point context. Not the registers, only the
  68. rest. */
  69. movq oFPREGS(%rsi), %rcx
  70. fldenv (%rcx)
  71. ldmxcsr oMXCSR(%rsi)
  72. /* Load the new stack pointer and the preserved registers. */
  73. movq oRSP(%rsi), %rsp
  74. movq oRBX(%rsi), %rbx
  75. movq oRBP(%rsi), %rbp
  76. movq oR12(%rsi), %r12
  77. movq oR13(%rsi), %r13
  78. movq oR14(%rsi), %r14
  79. movq oR15(%rsi), %r15
  80. /* The following ret should return to the address set with
  81. getcontext. Therefore push the address on the stack. */
  82. movq oRIP(%rsi), %rcx
  83. pushq %rcx
  84. /* Setup registers used for passing args. */
  85. movq oRDI(%rsi), %rdi
  86. movq oRDX(%rsi), %rdx
  87. movq oRCX(%rsi), %rcx
  88. movq oR8(%rsi), %r8
  89. movq oR9(%rsi), %r9
  90. /* Setup finally %rsi. */
  91. movq oRSI(%rsi), %rsi
  92. /* Clear rax to indicate success. */
  93. xorl %eax, %eax
  94. L(pseudo_end):
  95. ret
  96. PSEUDO_END(__swapcontext)
  97. weak_alias (__swapcontext, swapcontext)