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)
61 /* Save fp state (not needed, except for setcontext not
62 restoring garbage). */
63 leaq UC_MCONTEXT_FPREGS_MEM(%rdi),%r8
64 movq %r8, UC_MCONTEXT_FPREGS_PTR(%rdi)
66 stmxcsr FPREGS_OFFSET_MXCSR(%r8)
67 #elif defined __FreeBSD__
68 fxsave UC_MCONTEXT_FPSTATE(%rdi)
69 movq $UC_MCONTEXT_FPOWNED_FPU,UC_MCONTEXT_OWNEDFP(%rdi)
70 movq $UC_MCONTEXT_FPFMT_XMM,UC_MCONTEXT_FPFORMAT(%rdi)
71 /* Save rflags and segment registers, so that sigreturn(2)
74 .cfi_adjust_cfa_offset 8
75 popq UC_MCONTEXT_RFLAGS(%rdi)
76 .cfi_adjust_cfa_offset -8
77 movl $0, UC_MCONTEXT_FLAGS(%rdi)
78 movw %cs, UC_MCONTEXT_CS(%rdi)
79 movw %ss, UC_MCONTEXT_SS(%rdi)
81 /* Setting the flags to 0 above disables restore of segment
82 registers from the context */
83 movw %ds, UC_MCONTEXT_DS(%rdi)
84 movw %es, UC_MCONTEXT_ES(%rdi)
85 movw %fs, UC_MCONTEXT_FS(%rdi)
86 movw %gs, UC_MCONTEXT_GS(%rdi)
88 movq $UC_MCONTEXT_MC_LEN_VAL, UC_MCONTEXT_MC_LEN(%rdi)
93 leaq 8(%rsp), %rax /* exclude this call. */
94 movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi)
97 movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi)
102 .size _Ux86_64_getcontext, . - _Ux86_64_getcontext
104 /* int _Ux86_64_getcontext_trace (ucontext_t *ucp)
106 Saves limited machine context in UCP necessary for libunwind.
107 Unlike _Ux86_64_getcontext, saves only the parts needed for
108 fast trace. If fast trace fails, caller will have to get the
112 .global _Ux86_64_getcontext_trace
113 .hidden _Ux86_64_getcontext_trace
114 .type _Ux86_64_getcontext_trace, @function
115 _Ux86_64_getcontext_trace:
118 /* Save only RBP, RBX, RSP, RIP - exclude this call. */
119 movq %rbp, UC_MCONTEXT_GREGS_RBP(%rdi)
120 movq %rbx, UC_MCONTEXT_GREGS_RBX(%rdi)
123 movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi)
126 movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi)
131 .size _Ux86_64_getcontext_trace, . - _Ux86_64_getcontext_trace
133 /* We do not need executable stack. */
134 .section .note.GNU-stack,"",@progbits