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