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>
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#if defined __linux__
29#include <asm/unistd.h>
30#define	SIG_SETMASK   2
31#define	SIGSET_BYTE_SIZE   (64/8)
32#elif defined __FreeBSD__
33#include <sys/syscall.h>
34#endif
35
36/*  int _Ux86_64_setcontext (const ucontext_t *ucp)
37
38  Restores the machine context provided.
39  Unlike the libc implementation, doesn't clobber %rax
40
41*/
42	.global _Ux86_64_setcontext
43	.type _Ux86_64_setcontext, @function
44
45_Ux86_64_setcontext:
46
47#if defined __linux__
48	/* restore signal mask
49           sigprocmask(SIG_SETMASK, ucp->uc_sigmask, NULL, sizeof(sigset_t)) */
50	push %rdi
51	mov $__NR_rt_sigprocmask, %rax
52	lea UC_SIGMASK(%rdi), %rsi
53	mov $SIG_SETMASK, %rdi
54	xor %rdx, %rdx
55	mov $SIGSET_BYTE_SIZE, %r10
56	syscall
57	pop %rdi
58
59        /* restore fp state */
60	mov    UC_MCONTEXT_FPREGS_PTR(%rdi),%r8
61	fldenv (%r8)
62	ldmxcsr FPREGS_OFFSET_MXCSR(%r8)
63#elif defined __FreeBSD__
64	/* restore signal mask */
65	pushq	%rdi
66	xorl	%edx,%edx
67	leaq	UC_SIGMASK(%rdi),%rsi
68	movl	$3,%edi/* SIG_SETMASK */
69	movl	$SYS_sigprocmask,%eax
70	movq	%rcx,%r10
71	syscall
72	popq	%rdi
73
74	/* restore fp state */
75	cmpq $UC_MCONTEXT_FPOWNED_FPU,UC_MCONTEXT_OWNEDFP(%rdi)
76	jne 1f
77	cmpq $UC_MCONTEXT_FPFMT_XMM,UC_MCONTEXT_FPFORMAT(%rdi)
78	jne 1f
79	fxrstor UC_MCONTEXT_FPSTATE(%rdi)
801:
81#else
82#error Port me
83#endif
84
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
99
100        /* push the return address on the stack */
101	mov    UC_MCONTEXT_GREGS_RIP(%rdi),%rcx
102	push   %rcx
103
104	mov    UC_MCONTEXT_GREGS_RCX(%rdi),%rcx
105	mov    UC_MCONTEXT_GREGS_RDI(%rdi),%rdi
106	retq
107
108	.size _Ux86_64_setcontext, . - _Ux86_64_setcontext
109
110      /* We do not need executable stack.  */
111      .section        .note.GNU-stack,"",@progbits
112