Initial import from FreeBSD RELENG_4:
[games.git] / lib / libcr / mips / gen / setjmp.S
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ralph Campbell.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #include <sys/syscall.h>
38 #include <machine/asm.h>
39 #include <machine/regnum.h>
40
41 #if defined(LIBC_SCCS) 
42         .text
43         .asciz "$OpenBSD: setjmp.S,v 1.6 1997/07/23 20:55:25 kstailey Exp $"
44 #endif /* LIBC_SCCS */
45
46 /*
47  * C library -- setjmp, longjmp
48  *
49  *      longjmp(a,v)
50  * will generate a "return(v)" from
51  * the last call to
52  *      setjmp(a)
53  * by restoring registers from the stack,
54  * and a struct sigcontext, see <signal.h>
55  */
56
57 #define SETJMP_FRAME_SIZE       (STAND_FRAME_SIZE + 12)
58
59 NON_LEAF(setjmp, SETJMP_FRAME_SIZE, ra)
60         .mask   0x80000000, (STAND_RA_OFFSET - STAND_FRAME_SIZE)
61         subu    sp, sp, SETJMP_FRAME_SIZE       # allocate stack frame
62 #ifdef ABICALLS
63         .cprestore 16
64 #endif
65         .set    reorder
66         sw      ra, STAND_RA_OFFSET(sp)         # save state
67         sw      a0, SETJMP_FRAME_SIZE(sp)
68         move    a0, zero                        # get current signal mask
69         jal     _C_LABEL(sigblock)
70         lw      v1, SETJMP_FRAME_SIZE(sp)       # v1 = jmpbuf
71         sw      v0, (1 * 4)(v1)                 # save sc_mask = sigblock(0)
72         move    a0, zero
73         addu    a1, sp, STAND_FRAME_SIZE        # pointer to struct sigaltstack
74         jal     _C_LABEL(sigaltstack)
75         lw      a0, SETJMP_FRAME_SIZE(sp)       # restore jmpbuf
76         lw      v1, STAND_FRAME_SIZE+8(sp)      # get old ss_onstack
77         and     v1, v1, 1                       # extract onstack flag
78         sw      v1, 0(a0)                       # save it in sc_onstack
79         lw      ra, STAND_RA_OFFSET(sp)
80         addu    sp, sp, SETJMP_FRAME_SIZE
81         blt     v0, zero, botch                 # check for sigstack() error
82         sw      ra, (2 * 4)(a0)                 # sc_pc = return address
83         li      v0, 0xACEDBADE                  # sigcontext magic number
84         sw      v0, ((ZERO + 3) * 4)(a0)        #   saved in sc_regs[0]
85         sw      s0, ((S0 + 3) * 4)(a0)
86         sw      s1, ((S1 + 3) * 4)(a0)
87         sw      s2, ((S2 + 3) * 4)(a0)
88         sw      s3, ((S3 + 3) * 4)(a0)
89         sw      s4, ((S4 + 3) * 4)(a0)
90         sw      s5, ((S5 + 3) * 4)(a0)
91         sw      s6, ((S6 + 3) * 4)(a0)
92         sw      s7, ((S7 + 3) * 4)(a0)
93         sw      gp, ((GP + 3) * 4)(a0)
94         sw      sp, ((SP + 3) * 4)(a0)
95         sw      s8, ((S8 + 3) * 4)(a0)
96         li      v0, 1                           # be nice if we could tell
97         sw      v0, (37 * 4)(a0)                # sc_fpused = 1
98         cfc1    v0, $31
99         swc1    $f20, ((20 + 38) * 4)(a0)
100         swc1    $f21, ((21 + 38) * 4)(a0)
101         swc1    $f22, ((22 + 38) * 4)(a0)
102         swc1    $f23, ((23 + 38) * 4)(a0)
103         swc1    $f24, ((24 + 38) * 4)(a0)
104         swc1    $f25, ((25 + 38) * 4)(a0)
105         swc1    $f26, ((26 + 38) * 4)(a0)
106         swc1    $f27, ((27 + 38) * 4)(a0)
107         swc1    $f28, ((28 + 38) * 4)(a0)
108         swc1    $f29, ((29 + 38) * 4)(a0)
109         swc1    $f30, ((30 + 38) * 4)(a0)
110         swc1    $f31, ((31 + 38) * 4)(a0)
111         sw      v0, ((32 + 38) * 4)(a0)
112         move    v0, zero
113         j       ra
114 END(setjmp)
115
116 LEAF(longjmp)
117 #ifdef ABICALLS
118         subu    sp, sp, 32
119         .cprestore 16
120 #endif
121         .set    reorder
122         sw      a1, ((V0 + 3) * 4)(a0)  # save return value in sc_regs[V0]
123         li      v0, SYS_sigreturn
124         syscall
125 botch:
126         jal     _C_LABEL(longjmperror)
127         jal     _C_LABEL(abort)
128 END(longjmp)