xref: /netbsd-src/lib/libc/arch/i386/gen/swapcontext.S (revision 0d34bfa26d596b4f9bcb49175a92020d91572850)
1*0d34bfa2Suebayasi/*	$NetBSD: swapcontext.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>
333fdac2b8Sthorpej#if defined(LIBC_SCCS) && !defined(lint)
34*0d34bfa2Suebayasi	RCSID("$NetBSD: swapcontext.S,v 1.8 2014/05/23 02:34:19 uebayasi Exp $")
353fdac2b8Sthorpej#endif /* LIBC_SCCS && !lint */
363fdac2b8Sthorpej
373fdac2b8SthorpejENTRY(swapcontext)
383fdac2b8Sthorpej	movl	4(%esp),%ecx
393fdac2b8Sthorpej	PIC_PROLOGUE
403fdac2b8Sthorpej	pushl	%ecx			/* push oucp */
414d12bfcdSjoerg#ifdef __PIC__
423b364b9bSkleink	call	PIC_PLT(_C_LABEL(_getcontext))
433fdac2b8Sthorpej#else
443b364b9bSkleink	call	_C_LABEL(_getcontext)
453fdac2b8Sthorpej#endif
463fdac2b8Sthorpej	addl	$4,%esp			/* pop oucp */
473fdac2b8Sthorpej	PIC_EPILOGUE
483fdac2b8Sthorpej	testl	%eax,%eax
493fdac2b8Sthorpej	jnz	2f
503fdac2b8Sthorpej	movl	4(%esp),%ecx
513fdac2b8Sthorpej	movl	0(%esp),%edx
523fdac2b8Sthorpej	movl	%edx,(36 + 14 * 4)(%ecx)	/*  Adjust saved %eip ... */
534d12bfcdSjoerg#ifdef __PIC__
543fdac2b8Sthorpej	movl	%ebx,(36 +  8 * 4)(%ecx)	/* ... clobbered PIC reg ... */
553fdac2b8Sthorpej#endif
56e77f46b3Skleink	leal	4(%esp),%edx
573a798554Sfvdl	movl	%edx,(36 + 17 * 4)(%ecx)	/* ... and %esp. */
583fdac2b8Sthorpej	/* Note: oucp->%eax has been cleared by getcontext(). */
593fdac2b8Sthorpej	movl	8(%esp),%ecx
603fdac2b8Sthorpej	PIC_PROLOGUE
613fdac2b8Sthorpej	pushl	%ecx			/* push ucp */
624d12bfcdSjoerg#ifdef __PIC__
633fdac2b8Sthorpej	call	PIC_PLT(_C_LABEL(setcontext))
643fdac2b8Sthorpej#else
653fdac2b8Sthorpej	call	_C_LABEL(setcontext)
663fdac2b8Sthorpej#endif
673fdac2b8Sthorpej	addl	$4,%esp			/* pop ucp */
683fdac2b8Sthorpej	PIC_EPILOGUE
693fdac2b8Sthorpej	/* No need to error-check: setcontext() must have failed. */
703fdac2b8Sthorpej2:	ret
71*0d34bfa2SuebayasiEND(swapcontext)
72