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>
31 #define SIGSET_BYTE_SIZE (64/8)
32 #elif defined __FreeBSD__
33 #include <sys/syscall.h>
36 /* int _Ux86_64_setcontext (const ucontext_t *ucp)
38 Restores the machine context provided.
39 Unlike the libc implementation, doesn't clobber %rax
42 .global _Ux86_64_setcontext
43 .type _Ux86_64_setcontext, @function
48 /* restore signal mask
49 sigprocmask(SIG_SETMASK, ucp->uc_sigmask, NULL, sizeof(sigset_t)) */
51 mov $__NR_rt_sigprocmask, %rax
52 lea UC_SIGMASK(%rdi), %rsi
53 mov $SIG_SETMASK, %rdi
55 mov $SIGSET_BYTE_SIZE, %r10
59 /* restore fp state */
60 mov UC_MCONTEXT_FPREGS_PTR(%rdi),%r8
62 ldmxcsr FPREGS_OFFSET_MXCSR(%r8)
63 #elif defined __FreeBSD__
64 /* restore signal mask */
67 leaq UC_SIGMASK(%rdi),%rsi
68 movl $3,%edi/* SIG_SETMASK */
69 movl $SYS_sigprocmask,%eax
74 /* restore fp state */
75 cmpq $UC_MCONTEXT_FPOWNED_FPU,UC_MCONTEXT_OWNEDFP(%rdi)
77 cmpq $UC_MCONTEXT_FPFMT_XMM,UC_MCONTEXT_FPFORMAT(%rdi)
79 fxrstor UC_MCONTEXT_FPSTATE(%rdi)
85 /* restore the rest of the state */
86 mov UC_MCONTEXT_GREGS_R8(%rdi),%r8
87 mov UC_MCONTEXT_GREGS_R9(%rdi),%r9
88 mov UC_MCONTEXT_GREGS_RBX(%rdi),%rbx
89 mov UC_MCONTEXT_GREGS_RBP(%rdi),%rbp
90 mov UC_MCONTEXT_GREGS_R12(%rdi),%r12
91 mov UC_MCONTEXT_GREGS_R13(%rdi),%r13
92 mov UC_MCONTEXT_GREGS_R14(%rdi),%r14
93 mov UC_MCONTEXT_GREGS_R15(%rdi),%r15
94 mov UC_MCONTEXT_GREGS_RSI(%rdi),%rsi
95 mov UC_MCONTEXT_GREGS_RDX(%rdi),%rdx
96 mov UC_MCONTEXT_GREGS_RAX(%rdi),%rax
97 mov UC_MCONTEXT_GREGS_RCX(%rdi),%rcx
98 mov UC_MCONTEXT_GREGS_RSP(%rdi),%rsp
100 /* push the return address on the stack */
101 mov UC_MCONTEXT_GREGS_RIP(%rdi),%rcx
104 mov UC_MCONTEXT_GREGS_RCX(%rdi),%rcx
105 mov UC_MCONTEXT_GREGS_RDI(%rdi),%rdi
108 .size _Ux86_64_setcontext, . - _Ux86_64_setcontext
110 /* We do not need executable stack. */
111 .section .note.GNU-stack,"",@progbits