xref: /netbsd-src/lib/libc/arch/i386/gen/resumecontext.S (revision 0d34bfa26d596b4f9bcb49175a92020d91572850)
1*0d34bfa2Suebayasi/*	$NetBSD: resumecontext.S,v 1.8 2014/05/23 02:34:19 uebayasi Exp $	*/
23fdac2b8Sthorpej
33fdac2b8Sthorpej/*-
43fdac2b8Sthorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
53fdac2b8Sthorpej * All rights reserved.
63fdac2b8Sthorpej *
73fdac2b8Sthorpej * This code is derived from software contributed to The NetBSD Foundation
83fdac2b8Sthorpej * by Klaus Klein.
93fdac2b8Sthorpej *
103fdac2b8Sthorpej * Redistribution and use in source and binary forms, with or without
113fdac2b8Sthorpej * modification, are permitted provided that the following conditions
123fdac2b8Sthorpej * are met:
133fdac2b8Sthorpej * 1. Redistributions of source code must retain the above copyright
143fdac2b8Sthorpej *    notice, this list of conditions and the following disclaimer.
153fdac2b8Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
163fdac2b8Sthorpej *    notice, this list of conditions and the following disclaimer in the
173fdac2b8Sthorpej *    documentation and/or other materials provided with the distribution.
183fdac2b8Sthorpej *
193fdac2b8Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
203fdac2b8Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
213fdac2b8Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223fdac2b8Sthorpej * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
233fdac2b8Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
243fdac2b8Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253fdac2b8Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263fdac2b8Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
273fdac2b8Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283fdac2b8Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293fdac2b8Sthorpej * POSSIBILITY OF SUCH DAMAGE.
303fdac2b8Sthorpej */
313fdac2b8Sthorpej
323fdac2b8Sthorpej#include <machine/asm.h>
33ac9d7f2fSdrochner#include "SYS.h"
343fdac2b8Sthorpej
353fdac2b8Sthorpej#if defined(LIBC_SCCS) && !defined(lint)
36*0d34bfa2Suebayasi	RCSID("$NetBSD: resumecontext.S,v 1.8 2014/05/23 02:34:19 uebayasi Exp $")
373fdac2b8Sthorpej#endif /* LIBC_SCCS && !lint */
383fdac2b8Sthorpej
393fdac2b8Sthorpej/*
403fdac2b8Sthorpej * This assembly-language implementation differs from the (obvious)
413fdac2b8Sthorpej * C-language implementation only in not clobbering the previous
423fdac2b8Sthorpej * function's return address (us), which is the point of the exercise.
433fdac2b8Sthorpej */
443fdac2b8Sthorpej
453fdac2b8SthorpejNENTRY(_resumecontext)	/* profiling prologue would clobber TOS */
46326194c6Skleink	leal	-(4 + 776)(%esp),%ecx	/* retaddr + sizeof (ucontext_t) */
47326194c6Skleink	andl	$~(0xf),%ecx		/* align on _UC_UCONTEXT_ALIGN */
48326194c6Skleink	movl	%ecx,%esp
493fdac2b8Sthorpej	PIC_PROLOGUE
503fdac2b8Sthorpej	pushl	%ecx
514d12bfcdSjoerg#ifdef __PIC__
523fdac2b8Sthorpej	call	PIC_PLT(_C_LABEL(_getcontext))
533fdac2b8Sthorpej#else
543fdac2b8Sthorpej	call	_C_LABEL(_getcontext)
553fdac2b8Sthorpej#endif
563fdac2b8Sthorpej	addl	$4,%esp
573fdac2b8Sthorpej	PIC_EPILOGUE
58326194c6Skleink	movl	4(%esp),%ecx		/* uc_link */
593fdac2b8Sthorpej	PIC_PROLOGUE
60ac9d7f2fSdrochner	pushl	%ecx	/* argument for exit() or setcontext() */
61ac9d7f2fSdrochner	testl	%ecx,%ecx		/* link end? */
62ac9d7f2fSdrochner	jnz	9f	/* watch out for local label use in PIC_* */
63ac9d7f2fSdrochner
64ac9d7f2fSdrochner	/* normal exit */
654d12bfcdSjoerg#ifdef __PIC__
66ac9d7f2fSdrochner	call	PIC_PLT(_C_LABEL(exit))
67ac9d7f2fSdrochner#else
68ac9d7f2fSdrochner	call	_C_LABEL(exit)
69ac9d7f2fSdrochner#endif
70ac9d7f2fSdrochner	/* NOTREACHED */
71ac9d7f2fSdrochner
72ac9d7f2fSdrochner9:
734d12bfcdSjoerg#ifdef __PIC__
743fdac2b8Sthorpej	call	PIC_PLT(_C_LABEL(setcontext))
753fdac2b8Sthorpej#else
763fdac2b8Sthorpej	call	_C_LABEL(setcontext)
773fdac2b8Sthorpej#endif
783fdac2b8Sthorpej	/* NOTREACHED */
79ac9d7f2fSdrochner
80ac9d7f2fSdrochner	/* something is wrong, pull the brake */
81ac9d7f2fSdrochner	pushl	$-1
82ac9d7f2fSdrochner	pushl	%eax	/* unused return address */
83ac9d7f2fSdrochner	SYSTRAP(exit)
84*0d34bfa2SuebayasiEND(_resumecontext)
85