• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
5
6This file is part of libunwind.
7
8Permission is hereby granted, free of charge, to any person obtaining
9a copy of this software and associated documentation files (the
10"Software"), to deal in the Software without restriction, including
11without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense, and/or sell copies of the Software, and to
13permit persons to whom the Software is furnished to do so, subject to
14the following conditions:
15
16The above copyright notice and this permission notice shall be
17included in all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26
27#include "ucontext_i.h"
28
29/*  int _Ux86_64_getcontext (ucontext_t *ucp)
30
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.
34
35*/
36
37	.global _Ux86_64_getcontext
38	.type _Ux86_64_getcontext, @function
39_Ux86_64_getcontext:
40	.cfi_startproc
41
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)
49
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)
59
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
67	fnstenv (%r8)
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)
74	does not complain. */
75	pushfq
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)
82#if 0
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)
89#endif
90	movq $UC_MCONTEXT_MC_LEN_VAL, UC_MCONTEXT_MC_LEN(%rdi)
91#else
92#error Port me
93#endif
94
95	leaq 8(%rsp), %rax /* exclude this call.  */
96	movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi)
97
98	movq 0(%rsp), %rax
99	movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi)
100
101	xorq	%rax, %rax
102	retq
103	.cfi_endproc
104	.size _Ux86_64_getcontext, . - _Ux86_64_getcontext
105
106/*  int _Ux86_64_getcontext_trace (ucontext_t *ucp)
107
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
111  full context.
112*/
113
114	.global _Ux86_64_getcontext_trace
115	.hidden _Ux86_64_getcontext_trace
116	.type _Ux86_64_getcontext_trace, @function
117_Ux86_64_getcontext_trace:
118	.cfi_startproc
119
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)
123
124	leaq 8(%rsp), %rax
125	movq %rax, UC_MCONTEXT_GREGS_RSP(%rdi)
126
127	movq 0(%rsp), %rax
128	movq %rax, UC_MCONTEXT_GREGS_RIP(%rdi)
129
130	xorq	%rax, %rax
131	retq
132	.cfi_endproc
133	.size _Ux86_64_getcontext_trace, . - _Ux86_64_getcontext_trace
134
135      /* We do not need executable stack.  */
136      .section        .note.GNU-stack,"",@progbits
137