1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2007 Google, Inc
3 Contributed by Arun Sharma <arun.sharma@google.com>
4 Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
6 This file is part of libunwind.
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
27 #include "ucontext_i.h"
29 #include <asm/unistd.h>
32 #define SYS_sigprocmask 95
34 #elif defined __FreeBSD__
35 #include <sys/syscall.h>
38 #define SIGSET_BYTE_SIZE (64/8)
41 /* int _Ux86_64_setcontext (const ucontext_t *ucp)
43 Restores the machine context provided.
44 Unlike the libc implementation, doesn't clobber %rax
47 .global _Ux86_64_setcontext
48 .type _Ux86_64_setcontext, @function
52 #if defined(__linux__) || defined(__sun__)
53 /* restore signal mask
54 sigprocmask(SIG_SETMASK, ucp->uc_sigmask, NULL, sizeof(sigset_t)) */
57 mov $SYS_sigprocmask, %rax
59 mov $__NR_rt_sigprocmask, %rax
61 lea UC_SIGMASK(%rdi), %rsi
62 mov $SIG_SETMASK, %rdi
64 mov $SIGSET_BYTE_SIZE, %r10
68 /* restore fp state */
69 #ifdef UC_MCONTEXT_FPREGS_PTR
70 mov UC_MCONTEXT_FPREGS_PTR(%rdi),%r8
71 #else // UC_MCONTEXT_FPREGS_PTR
72 leaq UC_MCONTEXT_FPREGS_MEM(%rdi),%r8
73 #endif // UC_MCONTEXT_FPREGS_PTR
75 ldmxcsr FPREGS_OFFSET_MXCSR(%r8)
76 #elif defined __FreeBSD__
77 /* restore signal mask */
80 leaq UC_SIGMASK(%rdi),%rsi
81 movl $3,%edi/* SIG_SETMASK */
82 movl $SYS_sigprocmask,%eax
87 /* restore fp state */
88 cmpq $UC_MCONTEXT_FPOWNED_FPU,UC_MCONTEXT_OWNEDFP(%rdi)
90 cmpq $UC_MCONTEXT_FPFMT_XMM,UC_MCONTEXT_FPFORMAT(%rdi)
92 fxrstor UC_MCONTEXT_FPSTATE(%rdi)
98 /* restore the rest of the state */
99 mov UC_MCONTEXT_GREGS_R8(%rdi),%r8
100 mov UC_MCONTEXT_GREGS_R9(%rdi),%r9
101 mov UC_MCONTEXT_GREGS_RBX(%rdi),%rbx
102 mov UC_MCONTEXT_GREGS_RBP(%rdi),%rbp
103 mov UC_MCONTEXT_GREGS_R12(%rdi),%r12
104 mov UC_MCONTEXT_GREGS_R13(%rdi),%r13
105 mov UC_MCONTEXT_GREGS_R14(%rdi),%r14
106 mov UC_MCONTEXT_GREGS_R15(%rdi),%r15
107 mov UC_MCONTEXT_GREGS_RSI(%rdi),%rsi
108 mov UC_MCONTEXT_GREGS_RDX(%rdi),%rdx
109 mov UC_MCONTEXT_GREGS_RAX(%rdi),%rax
110 mov UC_MCONTEXT_GREGS_RCX(%rdi),%rcx
111 mov UC_MCONTEXT_GREGS_RSP(%rdi),%rsp
113 /* push the return address on the stack */
114 mov UC_MCONTEXT_GREGS_RIP(%rdi),%rcx
117 mov UC_MCONTEXT_GREGS_RCX(%rdi),%rcx
118 mov UC_MCONTEXT_GREGS_RDI(%rdi),%rdi
121 .size _Ux86_64_setcontext, . - _Ux86_64_setcontext
123 /* We do not need executable stack. */
124 .section .note.GNU-stack,"",@progbits