xref: /titanic_50/usr/src/lib/libc/amd64/sys/getcontext.s (revision 2449e17f82f6097fd2c665b64723e31ceecbeca6)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31#include <sys/asm_linkage.h>
32
33	ANSI_PRAGMA_WEAK(getcontext,function)
34	ANSI_PRAGMA_WEAK(swapcontext,function)
35
36#include "SYS.h"
37#include <../assym.h>
38
39/*
40 * getcontext() is written in assembler since it has to capture the correct
41 * machine state of the calle.
42 *
43 * As swapcontext() is actually equivalent to getcontext() + setcontext(),
44 * swapcontext() shares the most code with getcontext().
45 */
46
47#define	GETCONTEXT_IMPL(offset)						\
48	pushq	%rdi;		/* preserve the ucontext_t pointer */	\
49	call	__getcontext_syscall;					\
50				/* call getcontext: syscall */		\
51	popq	%rdx;							\
52	andl	%eax, %eax;	/* if (error_return_from_syscall) */	\
53	je	1f;							\
54	addq	$offset, %rsp;						\
55	ret;			/*	then just return */		\
561:									\
57	/*								\
58	 * fix up %rsp and %rip						\
59	 */								\
60	addq	$UC_MCONTEXT_GREGS, %rdx;				\
61				/* &ucp->uc_mcontext.gregs */		\
62	movq	offset+0(%rsp), %rax;					\
63				/* read return PC from stack */		\
64	movq	%rax, RIP_OFF (%rdx);					\
65				/* store ret PC in EIP of env var */	\
66	leaq	offset+8(%rsp), %rax;					\
67				/* get caller's sp at time of call */	\
68	movq	%rax, RSP_OFF (%rdx);					\
69				/* store the sp into UESP of env var */	\
70	xorq	%rax, %rax;	/* return 0 */				\
71	movq	%rax, RAX_OFF (%rdx);					\
72				/* getcontext returns 0 after setcontext */
73
74/*
75 * getcontext(ucontext_t *ucp)
76 */
77
78	ANSI_PRAGMA_WEAK2(_private_getcontext,getcontext,function)
79
80	ENTRY(getcontext)
81	GETCONTEXT_IMPL(0)
82	ret
83	SET_SIZE(getcontext)
84
85/*
86 * swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
87 */
88
89	ENTRY(swapcontext)
90	pushq	%rsi			/* preserve the 2nd argument */
91
92	GETCONTEXT_IMPL(8)
93
94	/* call setcontext */
95	popq	%rdi
96	call	_private_setcontext	/* call setcontext */
97	ret
98	SET_SIZE(swapcontext)
99