1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 Google, Inc
3 Contributed by Paul Pluzhnikov <ppluzhnikov@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 /* int _Ux86_64_getcontext (ucontext_t *ucp)
31 Saves the machine context in UCP necessary for libunwind.
32 Unlike the libc implementation, we don't save the signal mask
33 and hence avoid the cost of a system call per unwind.
37 .global _Ux86_64_getcontext
38 .type _Ux86_64_getcontext, @function
42 /* Callee saved: RBX, RBP, R12-R15 */
43 movq %r12, UC_MCONTEXT_GREGS_R12(%rdi)
44 movq %r13, UC_MCONTEXT_GREGS_R13(%rdi)
45 movq %r14, UC_MCONTEXT_GREGS_R14(%rdi)
46 movq %r15, UC_MCONTEXT_GREGS_R15(%rdi)
47 movq %rbp, UC_MCONTEXT_GREGS_RBP(%rdi)
48 movq %rbx, UC_MCONTEXT_GREGS_RBX(%rdi)
50 /* Save argument registers (not strictly needed, but setcontext
51 restores them, so don't restore garbage). */
52 movq %r8, UC_MCONTEXT_GREGS_R8(%rdi)
53 movq %r9, UC_MCONTEXT_GREGS_R9(%rdi)
54 movq %rdi, UC_MCONTEXT_GREGS_RDI(%rdi)
55 movq %rsi, UC_MCONTEXT_GREGS_RSI(%rdi)
56 movq %rdx, UC_MCONTEXT_GREGS_RDX(%rdi)
57 movq %rax, UC_MCONTEXT_GREGS_RAX(%rdi)
58 movq %rcx, UC_MCONTEXT_GREGS_RCX(%rdi)
60 #if defined(__linux__) || defined(__sun__)
61 /* Save fp state (not needed, except for setcontext not
62 restoring garbage). */
63 leaq UC_MCONTEXT_FPREGS_MEM(%rdi),%r8
64 #ifdef UC_MCONTEXT_FPREGS_PTR
65 movq %r8, UC_MCONTEXT_FPREGS_PTR(%rdi)
66 #endif // UC_MCONTEXT_FPREGS_PTR
68 stmxcsr FPREGS_OFFSET_MXCSR(%r8)
69 #elif defined __FreeBSD__
70 fxsave UC_MCONTEXT_FPSTATE(%rdi)
71 movq $UC_MCONTEXT_FPOWNED_FPU,UC_MCONTEXT_OWNEDFP(%rdi)
72 movq $UC_MCONTEXT_FPFMT_XMM,UC_MCONTEXT_FPFORMAT(%rdi)
73 /* Save rflags and segment registers, so that sigreturn(2)
76 .cfi_adjust_cfa_offset 8
77 popq UC_MCONTEXT_RFLAGS(%rdi)
78 .cfi_adjust_cfa_offset -8
79 movl $0, UC_MCONTEXT_FLAGS(%rdi)
80 movw %cs, UC_MCONTEXT_CS(%rdi)
81 movw %ss, UC_MCONTEXT_SS(%rdi)
83 /* Setting the flags to 0 above disables restore of segment
84 registers from the context */
85 movw %ds, UC_MCONTEXT_DS(%rdi)
86 movw %es, UC_MCONTEXT_ES(%rdi)
87 movw %fs, UC_MCONTEXT_FS(%rdi)
88 movw %gs, UC_MCONTEXT_GS(%rdi)
90 movq $UC_MCONTEXT_MC_LEN_VAL, UC_MCONTEXT_MC_LEN(%rdi)
95 leaq 8(%rsp), %rax /* exclude this call. */
96 movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi)
99 movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi)
104 .size _Ux86_64_getcontext, . - _Ux86_64_getcontext
106 /* int _Ux86_64_getcontext_trace (ucontext_t *ucp)
108 Saves limited machine context in UCP necessary for libunwind.
109 Unlike _Ux86_64_getcontext, saves only the parts needed for
110 fast trace. If fast trace fails, caller will have to get the
114 .global _Ux86_64_getcontext_trace
115 .hidden _Ux86_64_getcontext_trace
116 .type _Ux86_64_getcontext_trace, @function
117 _Ux86_64_getcontext_trace:
120 /* Save only RBP, RBX, RSP, RIP - exclude this call. */
121 movq %rbp, UC_MCONTEXT_GREGS_RBP(%rdi)
122 movq %rbx, UC_MCONTEXT_GREGS_RBX(%rdi)
125 movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi)
128 movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi)
133 .size _Ux86_64_getcontext_trace, . - _Ux86_64_getcontext_trace
135 /* We do not need executable stack. */
136 .section .note.GNU-stack,"",@progbits