xref: /onnv-gate/usr/src/lib/libc/amd64/sys/getcontext.s (revision 7298:b69e27387f74)
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
56515Sraf * Common Development and Distribution License (the "License").
66515Sraf * 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 */
216515Sraf
220Sstevel@tonic-gate/*
236515Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*7298SMark.J.Nelson@Sun.COM	.file	"getcontext.s"
280Sstevel@tonic-gate
290Sstevel@tonic-gate#include <sys/asm_linkage.h>
300Sstevel@tonic-gate
310Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(getcontext,function)
320Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(swapcontext,function)
330Sstevel@tonic-gate
340Sstevel@tonic-gate#include "SYS.h"
350Sstevel@tonic-gate#include <../assym.h>
360Sstevel@tonic-gate
370Sstevel@tonic-gate/*
380Sstevel@tonic-gate * getcontext() is written in assembler since it has to capture the correct
390Sstevel@tonic-gate * machine state of the calle.
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * As swapcontext() is actually equivalent to getcontext() + setcontext(),
420Sstevel@tonic-gate * swapcontext() shares the most code with getcontext().
430Sstevel@tonic-gate */
440Sstevel@tonic-gate
450Sstevel@tonic-gate#define	GETCONTEXT_IMPL(offset)						\
460Sstevel@tonic-gate	pushq	%rdi;		/* preserve the ucontext_t pointer */	\
476515Sraf	call	__getcontext;						\
480Sstevel@tonic-gate				/* call getcontext: syscall */		\
490Sstevel@tonic-gate	popq	%rdx;							\
500Sstevel@tonic-gate	andl	%eax, %eax;	/* if (error_return_from_syscall) */	\
510Sstevel@tonic-gate	je	1f;							\
520Sstevel@tonic-gate	addq	$offset, %rsp;						\
530Sstevel@tonic-gate	ret;			/*	then just return */		\
540Sstevel@tonic-gate1:									\
550Sstevel@tonic-gate	/*								\
560Sstevel@tonic-gate	 * fix up %rsp and %rip						\
570Sstevel@tonic-gate	 */								\
580Sstevel@tonic-gate	addq	$UC_MCONTEXT_GREGS, %rdx;				\
590Sstevel@tonic-gate				/* &ucp->uc_mcontext.gregs */		\
600Sstevel@tonic-gate	movq	offset+0(%rsp), %rax;					\
610Sstevel@tonic-gate				/* read return PC from stack */		\
620Sstevel@tonic-gate	movq	%rax, RIP_OFF (%rdx);					\
630Sstevel@tonic-gate				/* store ret PC in EIP of env var */	\
640Sstevel@tonic-gate	leaq	offset+8(%rsp), %rax;					\
650Sstevel@tonic-gate				/* get caller's sp at time of call */	\
660Sstevel@tonic-gate	movq	%rax, RSP_OFF (%rdx);					\
670Sstevel@tonic-gate				/* store the sp into UESP of env var */	\
680Sstevel@tonic-gate	xorq	%rax, %rax;	/* return 0 */				\
690Sstevel@tonic-gate	movq	%rax, RAX_OFF (%rdx);					\
700Sstevel@tonic-gate				/* getcontext returns 0 after setcontext */
710Sstevel@tonic-gate
720Sstevel@tonic-gate/*
730Sstevel@tonic-gate * getcontext(ucontext_t *ucp)
740Sstevel@tonic-gate */
750Sstevel@tonic-gate
760Sstevel@tonic-gate	ENTRY(getcontext)
770Sstevel@tonic-gate	GETCONTEXT_IMPL(0)
780Sstevel@tonic-gate	ret
790Sstevel@tonic-gate	SET_SIZE(getcontext)
800Sstevel@tonic-gate
810Sstevel@tonic-gate/*
820Sstevel@tonic-gate * swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
830Sstevel@tonic-gate */
840Sstevel@tonic-gate
850Sstevel@tonic-gate	ENTRY(swapcontext)
860Sstevel@tonic-gate	pushq	%rsi			/* preserve the 2nd argument */
870Sstevel@tonic-gate
880Sstevel@tonic-gate	GETCONTEXT_IMPL(8)
890Sstevel@tonic-gate
900Sstevel@tonic-gate	/* call setcontext */
910Sstevel@tonic-gate	popq	%rdi
926515Sraf	call	setcontext
930Sstevel@tonic-gate	ret
940Sstevel@tonic-gate	SET_SIZE(swapcontext)
95