1*eda14cbcSMatt Macy/* 2*eda14cbcSMatt Macy * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009 3*eda14cbcSMatt Macy * The President and Fellows of Harvard College. 4*eda14cbcSMatt Macy * Copyright (c) 2017 MIPS Technologies, Inc. 5*eda14cbcSMatt Macy * 6*eda14cbcSMatt Macy * Redistribution and use in source and binary forms, with or without 7*eda14cbcSMatt Macy * modification, are permitted provided that the following conditions 8*eda14cbcSMatt Macy * are met: 9*eda14cbcSMatt Macy * 1. Redistributions of source code must retain the above copyright 10*eda14cbcSMatt Macy * notice, this list of conditions and the following disclaimer. 11*eda14cbcSMatt Macy * 2. Redistributions in binary form must reproduce the above copyright 12*eda14cbcSMatt Macy * notice, this list of conditions and the following disclaimer in the 13*eda14cbcSMatt Macy * documentation and/or other materials provided with the distribution. 14*eda14cbcSMatt Macy * 3. Neither the name of the University nor the names of its contributors 15*eda14cbcSMatt Macy * may be used to endorse or promote products derived from this software 16*eda14cbcSMatt Macy * without specific prior written permission. 17*eda14cbcSMatt Macy * 18*eda14cbcSMatt Macy * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND 19*eda14cbcSMatt Macy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*eda14cbcSMatt Macy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*eda14cbcSMatt Macy * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE 22*eda14cbcSMatt Macy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23*eda14cbcSMatt Macy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24*eda14cbcSMatt Macy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25*eda14cbcSMatt Macy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26*eda14cbcSMatt Macy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27*eda14cbcSMatt Macy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28*eda14cbcSMatt Macy * SUCH DAMAGE. 29*eda14cbcSMatt Macy */ 30*eda14cbcSMatt Macy 31*eda14cbcSMatt Macy#include <asm/asm.h> 32*eda14cbcSMatt Macy#include <asm/regdef.h> 33*eda14cbcSMatt Macy 34*eda14cbcSMatt Macy/* 35*eda14cbcSMatt Macy * setjmp and longjmp for MIPS. 36*eda14cbcSMatt Macy */ 37*eda14cbcSMatt Macy 38*eda14cbcSMatt Macy .text 39*eda14cbcSMatt Macy .set noreorder 40*eda14cbcSMatt Macy 41*eda14cbcSMatt Macy /* 42*eda14cbcSMatt Macy * int setjmp(jmp_buf jb); 43*eda14cbcSMatt Macy * 44*eda14cbcSMatt Macy * Save the current state so we can return again from the call later 45*eda14cbcSMatt Macy * if/when longjmp is called. (If the function that called setjmp 46*eda14cbcSMatt Macy * returns before longjmp is called, the results are undefined. We 47*eda14cbcSMatt Macy * only need to save registers, not the whole contents of the stack.) 48*eda14cbcSMatt Macy */ 49*eda14cbcSMatt MacyLEAF(setjmp) 50*eda14cbcSMatt Macy /* 51*eda14cbcSMatt Macy * jmp_buf is in a0. We need to save s0-s8, sp, gp, and ra in it. 52*eda14cbcSMatt Macy * Don't store more registers without adjusting machine/setjmp.h. 53*eda14cbcSMatt Macy */ 54*eda14cbcSMatt Macy 55*eda14cbcSMatt Macy REG_S sp, 0(a0) /* save registers */ 56*eda14cbcSMatt Macy REG_S ra, 1*SZREG(a0) 57*eda14cbcSMatt Macy REG_S gp, 2*SZREG(a0) 58*eda14cbcSMatt Macy REG_S s0, 3*SZREG(a0) 59*eda14cbcSMatt Macy REG_S s1, 4*SZREG(a0) 60*eda14cbcSMatt Macy REG_S s2, 5*SZREG(a0) 61*eda14cbcSMatt Macy REG_S s3, 6*SZREG(a0) 62*eda14cbcSMatt Macy REG_S s4, 7*SZREG(a0) 63*eda14cbcSMatt Macy REG_S s5, 8*SZREG(a0) 64*eda14cbcSMatt Macy REG_S s6, 9*SZREG(a0) 65*eda14cbcSMatt Macy REG_S s7, 10*SZREG(a0) 66*eda14cbcSMatt Macy REG_S s8, 11*SZREG(a0) 67*eda14cbcSMatt Macy 68*eda14cbcSMatt Macy jr ra /* done */ 69*eda14cbcSMatt Macy move v0, zero /* return 0 (in delay slot) */ 70*eda14cbcSMatt MacyEND(setjmp) 71*eda14cbcSMatt Macy 72*eda14cbcSMatt Macy 73*eda14cbcSMatt Macy /* 74*eda14cbcSMatt Macy * void longjmp(jmp_buf jb, int code); 75*eda14cbcSMatt Macy */ 76*eda14cbcSMatt MacyLEAF(longjmp) 77*eda14cbcSMatt Macy /* 78*eda14cbcSMatt Macy * jmp_buf is in a0. Return code is in a1. 79*eda14cbcSMatt Macy * We need to restore s0-s8, sp, gp, and ra from the jmp_buf. 80*eda14cbcSMatt Macy * The return code is forced to 1 if 0 is passed in. 81*eda14cbcSMatt Macy */ 82*eda14cbcSMatt Macy 83*eda14cbcSMatt Macy sltiu t0, a1, 1 /* set t0 to 1 if return code is 0... otherwise 0 */ 84*eda14cbcSMatt Macy addu a1, a1, t0 /* update the return code */ 85*eda14cbcSMatt Macy 86*eda14cbcSMatt Macy REG_L sp, 0(a0) /* restore registers */ 87*eda14cbcSMatt Macy REG_L ra, 1*SZREG(a0) 88*eda14cbcSMatt Macy REG_L gp, 2*SZREG(a0) 89*eda14cbcSMatt Macy REG_L s0, 3*SZREG(a0) 90*eda14cbcSMatt Macy REG_L s1, 4*SZREG(a0) 91*eda14cbcSMatt Macy REG_L s2, 5*SZREG(a0) 92*eda14cbcSMatt Macy REG_L s3, 6*SZREG(a0) 93*eda14cbcSMatt Macy REG_L s4, 7*SZREG(a0) 94*eda14cbcSMatt Macy REG_L s5, 8*SZREG(a0) 95*eda14cbcSMatt Macy REG_L s6, 9*SZREG(a0) 96*eda14cbcSMatt Macy REG_L s7, 10*SZREG(a0) 97*eda14cbcSMatt Macy REG_L s8, 11*SZREG(a0) 98*eda14cbcSMatt Macy 99*eda14cbcSMatt Macy jr ra /* return, to where setjmp was called from */ 100*eda14cbcSMatt Macy move v0, a1 /* set return value */ 101*eda14cbcSMatt MacyEND(longjmp) 102*eda14cbcSMatt Macy 103*eda14cbcSMatt Macy#ifdef __ELF__ 104*eda14cbcSMatt Macy.section .note.GNU-stack,"",%progbits 105*eda14cbcSMatt Macy#endif 106