xref: /titanic_44/usr/src/lib/libc/amd64/sys/getcontext.s (revision 20e6d5c536ad5b300e7fafb6a92e13040f492977)
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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 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;						\
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	ENTRY(getcontext)
79	GETCONTEXT_IMPL(0)
80	ret
81	SET_SIZE(getcontext)
82
83/*
84 * swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
85 */
86
87	ENTRY(swapcontext)
88	pushq	%rsi			/* preserve the 2nd argument */
89
90	GETCONTEXT_IMPL(8)
91
92	/* call setcontext */
93	popq	%rdi
94	call	setcontext
95	ret
96	SET_SIZE(swapcontext)
97