xref: /minix3/lib/libc/arch/powerpc/gen/swapcontext.S (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc/*	$NetBSD: swapcontext.S,v 1.7 2013/09/12 15:36:15 joerg Exp $	*/
22fe8fb19SBen Gras
32fe8fb19SBen Gras/*-
42fe8fb19SBen Gras * Copyright (c) 2001 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Klaus Klein.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras#include "SYS.h"
332fe8fb19SBen Gras#include "assym.h"
342fe8fb19SBen Gras
35*84d9c625SLionel Sambuc__RCSID("$NetBSD: swapcontext.S,v 1.7 2013/09/12 15:36:15 joerg Exp $")
362fe8fb19SBen Gras
372fe8fb19SBen Gras#define	XCALLFRAMELEN	(((2+3)*SZREG + CALLFRAMELEN - 1) & -CALLFRAMELEN)
382fe8fb19SBen Gras#define	XCALLFRAME_R30	(XCALLFRAMELEN-1*SZREG)
392fe8fb19SBen Gras#define	XCALLFRAME_UCP	(XCALLFRAMELEN-2*SZREG)
402fe8fb19SBen Gras#define	XCALLFRAME_OUCP	(XCALLFRAMELEN-3*SZREG)
412fe8fb19SBen Gras
422fe8fb19SBen GrasENTRY(swapcontext)
432fe8fb19SBen Gras	stwu	%r1,-XCALLFRAMELEN(%r1)		# set up new stack frame
442fe8fb19SBen Gras	mflr	%r0
452fe8fb19SBen Gras	stw	%r0,XCALLFRAMELEN+CALLFRAME_LR(%r1)	# save link register
462fe8fb19SBen Gras	stw	%r3,XCALLFRAME_OUCP(%r1)	# must save oucp
472fe8fb19SBen Gras	stw	%r4,XCALLFRAME_UCP(%r1)		# must save ucp
48*84d9c625SLionel Sambuc#ifdef __PIC__
492fe8fb19SBen Gras	stw	%r30,XCALLFRAME_R30(%r1)	# must save r30
502fe8fb19SBen Gras	PIC_TOCSETUP(swapcontext,%r30)		# setup toc pointer
512fe8fb19SBen Gras#endif
522fe8fb19SBen Gras	bl	PIC_PLT(_C_LABEL(_getcontext))	# getcontext(oucp)
532fe8fb19SBen Gras	cmpwi	%r3,0
542fe8fb19SBen Gras	bne	1f
552fe8fb19SBen Gras	lwz	%r11,XCALLFRAME_OUCP(%r1)	# load oucp for adjustment
562fe8fb19SBen Gras	lwz	%r0,XCALLFRAMELEN+CALLFRAME_LR(%r1)
572fe8fb19SBen Gras	stw	%r0,UC_GREGS_PC(%r11)		# pc <- lr
582fe8fb19SBen Gras	addi	%r0,%r1,XCALLFRAMELEN
592fe8fb19SBen Gras	stw	%r0,UC_GREGS_R1(%r11)		# adjust sp
602fe8fb19SBen Gras	lwz	%r3,XCALLFRAME_UCP(%r1)		# load ucp
612fe8fb19SBen Gras	bl	PIC_PLT(_C_LABEL(setcontext))	# setcontext(ucp)
622fe8fb19SBen Gras1:
632fe8fb19SBen Gras	lwz	%r0,XCALLFRAMELEN+CALLFRAME_LR(%r1)
64*84d9c625SLionel Sambuc#ifdef __PIC__
652fe8fb19SBen Gras	lwz	%r30,XCALLFRAME_R30(%r1)
662fe8fb19SBen Gras#endif
672fe8fb19SBen Gras	mtlr	%r0
682fe8fb19SBen Gras	addi	%r1,%r1,XCALLFRAMELEN
692fe8fb19SBen Gras	blr
702fe8fb19SBen GrasEND(swapcontext)
71