xref: /titanic_44/usr/src/lib/libc/i386/sys/getcontext.s (revision 24db46411fd54f70c35b94bb952eb7ba040e43b4)
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() and swapcontext() are written in assembler since it has to
41 * capture the correct machine state of the caller, including
42 * the registers: %edi, %esi and %ebx.
43 *
44 * As swapcontext() is actually equivalent to getcontext() + setcontext(),
45 * swapcontext() shares the most code with getcontext().
46 */
47
48
49#define	GETCONTEXT_IMPL							\
50	movl	4(%esp), %eax;		/* %eax <-- first arg: ucp */	\
51	pushl	%eax;			/* push ucp for system call */	\
52	call	__getcontext_syscall;	/* call getcontext: syscall */	\
53	addl	$4, %esp;		/* pop arg */			\
54	andl	%eax, %eax;		/* if (err_ret_from_syscall) */	\
55	je	1f;							\
56	ret;				/*	then return */		\
571:									\
58	movl	4(%esp), %eax;		/* recompute first arg */	\
59	/*								\
60	 * fix up %esp and %eip						\
61	 */								\
62	leal	UC_MCONTEXT_GREGS (%eax), %edx;				\
63				/* %edx <-- &ucp->uc_mcontext.gregs */	\
64	movl	0(%esp), %eax;	/* read return PC from stack */		\
65	movl	%eax, EIP_OFF (%edx);					\
66				/* store ret PC in EIP of env var */	\
67	leal	4(%esp), %eax;	/* get caller's sp at time of call */	\
68	movl	%eax, UESP_OFF (%edx);					\
69				/* store the sp into UESP of env var */	\
70	xorl	%eax, %eax;	/* return 0 */				\
71	movl	%eax, EAX_OFF (%edx);					\
72				/* getcontext returns 0 after a setcontext */
73
74/*
75 * getcontext(ucontext_t *ucp)
76 */
77	ANSI_PRAGMA_WEAK2(_private_getcontext,getcontext,function)
78
79	ENTRY(getcontext)
80	GETCONTEXT_IMPL
81	ret
82	SET_SIZE(getcontext)
83
84
85/*
86 * swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
87 */
88	ENTRY(swapcontext)
89	GETCONTEXT_IMPL
90	/ call setcontext
91	movl	8(%esp), %eax		/* %eax <-- second arg: ucp */
92	pushl	%eax			/* push ucp for setcontext */
93	call	_private_setcontext	/* call setcontext */
94	addl	$4, %esp		/* pop arg: just in case */
95	ret
96	SET_SIZE(swapcontext)
97