1*8e33e1ffSuwe/* $NetBSD: swapcontext.S,v 1.8 2018/02/28 20:51:58 uwe Exp $ */ 23fdac2b8Sthorpej 33fdac2b8Sthorpej/*- 43fdac2b8Sthorpej * Copyright (c) 2001 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 "SYS.h" 33cf88c389Smatt#include "assym.h" 343fdac2b8Sthorpej 35*8e33e1ffSuwe__RCSID("$NetBSD: swapcontext.S,v 1.8 2018/02/28 20:51:58 uwe Exp $") 36cf88c389Smatt 37b79441d4Smatt#define XCALLFRAMELEN (((2+3)*SZREG + CALLFRAMELEN - 1) & -CALLFRAMELEN) 38b79441d4Smatt#define XCALLFRAME_R30 (XCALLFRAMELEN-1*SZREG) 39b79441d4Smatt#define XCALLFRAME_UCP (XCALLFRAMELEN-2*SZREG) 40b79441d4Smatt#define XCALLFRAME_OUCP (XCALLFRAMELEN-3*SZREG) 413fdac2b8Sthorpej 423fdac2b8SthorpejENTRY(swapcontext) 43b79441d4Smatt stwu %r1,-XCALLFRAMELEN(%r1) # set up new stack frame 4409c5c31fSkleink mflr %r0 45b79441d4Smatt stw %r0,XCALLFRAMELEN+CALLFRAME_LR(%r1) # save link register 46b79441d4Smatt stw %r3,XCALLFRAME_OUCP(%r1) # must save oucp 47b79441d4Smatt stw %r4,XCALLFRAME_UCP(%r1) # must save ucp 484d12bfcdSjoerg#ifdef __PIC__ 49b79441d4Smatt stw %r30,XCALLFRAME_R30(%r1) # must save r30 50b79441d4Smatt PIC_TOCSETUP(swapcontext,%r30) # setup toc pointer 51b79441d4Smatt#endif 523fdac2b8Sthorpej bl PIC_PLT(_C_LABEL(_getcontext)) # getcontext(oucp) 5309c5c31fSkleink cmpwi %r3,0 543fdac2b8Sthorpej bne 1f 55b79441d4Smatt lwz %r11,XCALLFRAME_OUCP(%r1) # load oucp for adjustment 56b79441d4Smatt lwz %r0,XCALLFRAMELEN+CALLFRAME_LR(%r1) 57cf88c389Smatt stw %r0,UC_GREGS_PC(%r11) # pc <- lr 58b79441d4Smatt addi %r0,%r1,XCALLFRAMELEN 59b79441d4Smatt stw %r0,UC_GREGS_R1(%r11) # adjust sp 60*8e33e1ffSuwe#ifdef __PIC__ 61*8e33e1ffSuwe lwz %r0,XCALLFRAME_R30(%r1) 62*8e33e1ffSuwe stw %r0,UC_GREGS_R30(%r11) # caller's r30 63*8e33e1ffSuwe#endif 64b79441d4Smatt lwz %r3,XCALLFRAME_UCP(%r1) # load ucp 653fdac2b8Sthorpej bl PIC_PLT(_C_LABEL(setcontext)) # setcontext(ucp) 663fdac2b8Sthorpej1: 67b79441d4Smatt lwz %r0,XCALLFRAMELEN+CALLFRAME_LR(%r1) 684d12bfcdSjoerg#ifdef __PIC__ 69b79441d4Smatt lwz %r30,XCALLFRAME_R30(%r1) 70b79441d4Smatt#endif 7109c5c31fSkleink mtlr %r0 72b79441d4Smatt addi %r1,%r1,XCALLFRAMELEN 733fdac2b8Sthorpej blr 74cf88c389SmattEND(swapcontext) 75