xref: /onnv-gate/usr/src/lib/libc/i386/sys/getcontext.s (revision 6515:10dab2b883e0)
10Sstevel@tonic-gate/*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6515Sraf * Common Development and Distribution License (the "License").
6*6515Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*6515Sraf
220Sstevel@tonic-gate/*
23*6515Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate	.file	"%M%"
300Sstevel@tonic-gate
310Sstevel@tonic-gate#include <sys/asm_linkage.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(getcontext,function)
340Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(swapcontext,function)
350Sstevel@tonic-gate
360Sstevel@tonic-gate#include "SYS.h"
370Sstevel@tonic-gate#include <assym.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate/*
400Sstevel@tonic-gate * getcontext() and swapcontext() are written in assembler since it has to
410Sstevel@tonic-gate * capture the correct machine state of the caller, including
420Sstevel@tonic-gate * the registers: %edi, %esi and %ebx.
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * As swapcontext() is actually equivalent to getcontext() + setcontext(),
450Sstevel@tonic-gate * swapcontext() shares the most code with getcontext().
460Sstevel@tonic-gate */
470Sstevel@tonic-gate
480Sstevel@tonic-gate
490Sstevel@tonic-gate#define	GETCONTEXT_IMPL							\
500Sstevel@tonic-gate	movl	4(%esp), %eax;		/* %eax <-- first arg: ucp */	\
510Sstevel@tonic-gate	pushl	%eax;			/* push ucp for system call */	\
52*6515Sraf	call	__getcontext;		/* call getcontext: syscall */	\
530Sstevel@tonic-gate	addl	$4, %esp;		/* pop arg */			\
540Sstevel@tonic-gate	andl	%eax, %eax;		/* if (err_ret_from_syscall) */	\
550Sstevel@tonic-gate	je	1f;							\
560Sstevel@tonic-gate	ret;				/*	then return */		\
570Sstevel@tonic-gate1:									\
580Sstevel@tonic-gate	movl	4(%esp), %eax;		/* recompute first arg */	\
590Sstevel@tonic-gate	/*								\
600Sstevel@tonic-gate	 * fix up %esp and %eip						\
610Sstevel@tonic-gate	 */								\
620Sstevel@tonic-gate	leal	UC_MCONTEXT_GREGS (%eax), %edx;				\
630Sstevel@tonic-gate				/* %edx <-- &ucp->uc_mcontext.gregs */	\
640Sstevel@tonic-gate	movl	0(%esp), %eax;	/* read return PC from stack */		\
650Sstevel@tonic-gate	movl	%eax, EIP_OFF (%edx);					\
660Sstevel@tonic-gate				/* store ret PC in EIP of env var */	\
670Sstevel@tonic-gate	leal	4(%esp), %eax;	/* get caller's sp at time of call */	\
680Sstevel@tonic-gate	movl	%eax, UESP_OFF (%edx);					\
690Sstevel@tonic-gate				/* store the sp into UESP of env var */	\
700Sstevel@tonic-gate	xorl	%eax, %eax;	/* return 0 */				\
710Sstevel@tonic-gate	movl	%eax, EAX_OFF (%edx);					\
720Sstevel@tonic-gate				/* getcontext returns 0 after a setcontext */
730Sstevel@tonic-gate
740Sstevel@tonic-gate/*
750Sstevel@tonic-gate * getcontext(ucontext_t *ucp)
760Sstevel@tonic-gate */
770Sstevel@tonic-gate	ENTRY(getcontext)
780Sstevel@tonic-gate	GETCONTEXT_IMPL
790Sstevel@tonic-gate	ret
800Sstevel@tonic-gate	SET_SIZE(getcontext)
810Sstevel@tonic-gate
820Sstevel@tonic-gate
830Sstevel@tonic-gate/*
840Sstevel@tonic-gate * swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
850Sstevel@tonic-gate */
860Sstevel@tonic-gate	ENTRY(swapcontext)
870Sstevel@tonic-gate	GETCONTEXT_IMPL
880Sstevel@tonic-gate	/ call setcontext
890Sstevel@tonic-gate	movl	8(%esp), %eax		/* %eax <-- second arg: ucp */
900Sstevel@tonic-gate	pushl	%eax			/* push ucp for setcontext */
91*6515Sraf	call	setcontext
920Sstevel@tonic-gate	addl	$4, %esp		/* pop arg: just in case */
930Sstevel@tonic-gate	ret
940Sstevel@tonic-gate	SET_SIZE(swapcontext)
95