/*-1* Copyright (c) 2015-2016 Ruslan Bukin <[email protected]>2* All rights reserved.3*4* Portions of this software were developed by SRI International and the5* University of Cambridge Computer Laboratory under DARPA/AFRL contract6* FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.7*8* Portions of this software were developed by the University of Cambridge9* Computer Laboratory as part of the CTSRD Project, with support from the10* UK Higher Education Innovation Fund (HEIF).11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20*21* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <machine/asm.h>35#include <machine/setjmp.h>3637ENTRY(setjmp)38addi sp, sp, -(2 * 8)39sd a0, 0(sp)40sd ra, 8(sp)4142/* Store the signal mask */43addi a2, a0, (_JB_SIGMASK * 8) /* oset */44li a1, 0 /* set */45li a0, 1 /* SIG_BLOCK */46call _C_LABEL(sigprocmask)4748ld a0, 0(sp)49ld ra, 8(sp)50addi sp, sp, (2 * 8)5152/* Store the magic value and stack pointer */53ld t0, .Lmagic54sd t0, (0 * 8)(a0)55sd sp, (1 * 8)(a0)56addi a0, a0, (2 * 8)5758/* Store the general purpose registers and ra */59sd s0, (0 * 8)(a0)60sd s1, (1 * 8)(a0)61sd s2, (2 * 8)(a0)62sd s3, (3 * 8)(a0)63sd s4, (4 * 8)(a0)64sd s5, (5 * 8)(a0)65sd s6, (6 * 8)(a0)66sd s7, (7 * 8)(a0)67sd s8, (8 * 8)(a0)68sd s9, (9 * 8)(a0)69sd s10, (10 * 8)(a0)70sd s11, (11 * 8)(a0)71sd ra, (12 * 8)(a0)72addi a0, a0, (13 * 8)7374#ifdef __riscv_float_abi_double75/* Store the fpe registers */76fsd fs0, (0 * 8)(a0)77fsd fs1, (1 * 8)(a0)78fsd fs2, (2 * 8)(a0)79fsd fs3, (3 * 8)(a0)80fsd fs4, (4 * 8)(a0)81fsd fs5, (5 * 8)(a0)82fsd fs6, (6 * 8)(a0)83fsd fs7, (7 * 8)(a0)84fsd fs8, (8 * 8)(a0)85fsd fs9, (9 * 8)(a0)86fsd fs10, (10 * 8)(a0)87fsd fs11, (11 * 8)(a0)88addi a0, a0, (12 * 8)89#endif9091/* Return value */92li a0, 093ret94.align 395.Lmagic:96.quad _JB_MAGIC_SETJMP97END(setjmp)9899ENTRY(longjmp)100/* Check the magic value */101ld t0, 0(a0)102ld t1, .Lmagic103bne t0, t1, botch104105addi sp, sp, -(4 * 8)106sd a0, (0 * 8)(sp)107sd ra, (1 * 8)(sp)108sd a1, (2 * 8)(sp)109110/* Restore the signal mask */111li a2, 0 /* oset */112addi a1, a0, (_JB_SIGMASK * 8) /* set */113li a0, 3 /* SIG_BLOCK */114call _C_LABEL(sigprocmask)115116ld a1, (2 * 8)(sp)117ld ra, (1 * 8)(sp)118ld a0, (0 * 8)(sp)119addi sp, sp, (4 * 8)120121/* Restore the stack pointer */122ld t0, 8(a0)123mv sp, t0124addi a0, a0, (2 * 8)125126/* Restore the general purpose registers and ra */127ld s0, (0 * 8)(a0)128ld s1, (1 * 8)(a0)129ld s2, (2 * 8)(a0)130ld s3, (3 * 8)(a0)131ld s4, (4 * 8)(a0)132ld s5, (5 * 8)(a0)133ld s6, (6 * 8)(a0)134ld s7, (7 * 8)(a0)135ld s8, (8 * 8)(a0)136ld s9, (9 * 8)(a0)137ld s10, (10 * 8)(a0)138ld s11, (11 * 8)(a0)139ld ra, (12 * 8)(a0)140addi a0, a0, (13 * 8)141142#ifdef __riscv_float_abi_double143/* Restore the fpe registers */144fld fs0, (0 * 8)(a0)145fld fs1, (1 * 8)(a0)146fld fs2, (2 * 8)(a0)147fld fs3, (3 * 8)(a0)148fld fs4, (4 * 8)(a0)149fld fs5, (5 * 8)(a0)150fld fs6, (6 * 8)(a0)151fld fs7, (7 * 8)(a0)152fld fs8, (8 * 8)(a0)153fld fs9, (9 * 8)(a0)154fld fs10, (10 * 8)(a0)155fld fs11, (11 * 8)(a0)156addi a0, a0, (12 * 8)157#endif158159/* Load the return value */160mv a0, a1161bnez a1, 1f162li a0, 11631:164ret165166botch:167call _C_LABEL(longjmperror)168call _C_LABEL(abort)169END(longjmp)170171172