Home | History | Annotate | Line # | Download | only in gen
_setjmp.S revision 1.4
      1 /*	$NetBSD: _setjmp.S,v 1.4 2008/04/28 20:22:56 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matthew Fredette.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <machine/asm.h>
     33 /* XXX fredette - this is awful */
     34 #define _LOCORE
     35 #include <machine/frame.h>
     36 #undef _LOCORE
     37 
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 	RCSID("$NetBSD: _setjmp.S,v 1.4 2008/04/28 20:22:56 martin Exp $")
     40 #endif /* LIBC_SCCS and not lint */
     41 
     42 /*
     43  * C library -- setjmp, longjmp
     44  *
     45  *	longjmp(a,v)
     46  * will generate a "return(v)" from
     47  * the last call to
     48  *	setjmp(a)
     49  * by restoring registers from the stack,
     50  * and a struct sigcontext, see <signal.h>
     51  */
     52 
     53 ENTRY(_setjmp,0)
     54 	/* A sigcontext is at the beginning of our jmp_buf. */
     55 	stw	%r0, 0(%arg0)		; no onstack
     56 	stw	%r0, 4(%arg0)		; unused word (old style signal mask)
     57 	stw	%sp, 8(%arg0)		; sc.sc_sp = %sp
     58 	stw	%r0, 16(%arg0)		; sc.sc_ap = NULL
     59 	mfsp	%sr0, %r1
     60 	stw	%r1, 20(%arg0)		; sc.sc_pcsqh = %sr0
     61 	stw	%rp, 24(%arg0)		; sc.sc_pcoqh = %rp
     62 	stw	%r1, 28(%arg0)		; sc.sc_pcsqh = %sr0
     63 	ldo	4(%rp), %r1
     64 	stw	%r1, 32(%arg0)		; sc.sc_pcoqt = %rp + 4
     65 	stw	%r0, 36(%arg0)		; set sc.sc_ps
     66 
     67 	/* We store all callee-saved registers after the sigcontext. */
     68 	ldo	56(%arg0), %r1
     69 	stwm	%r3, 4(%r1)
     70 	stwm	%r4, 4(%r1)
     71 	stwm	%r5, 4(%r1)
     72 	stwm	%r6, 4(%r1)
     73 	stwm	%r7, 4(%r1)
     74 	stwm	%r8, 4(%r1)
     75 	stwm	%r9, 4(%r1)
     76 	stwm	%r10, 4(%r1)
     77 	stwm	%r11, 4(%r1)
     78 	stwm	%r12, 4(%r1)
     79 	stwm	%r13, 4(%r1)
     80 	stwm	%r14, 4(%r1)
     81 	stwm	%r15, 4(%r1)
     82 	stwm	%r16, 4(%r1)
     83 	stwm	%r17, 4(%r1)
     84 	stwm	%r18, 4(%r1)
     85 
     86 	/* Return 0. */
     87 	bv	%r0(%rp)
     88 	copy	%r0, %ret0
     89 EXIT(_setjmp)
     90 
     91 ENTRY(_longjmp,0)
     92 	ldw	8(%arg0), %r1		; ensure non-zero SP
     93 	combt,=,n	%r0, %r1, botch	; oops!
     94 	add,<>	%r0, %arg1, %ret0	; ensure return value non-zero
     95 	ldi	1, %ret0
     96 
     97 	/* restore callee-saved registers */
     98 	ldo	56(%arg0), %r1
     99 	ldwm	4(%r1), %r3
    100 	ldwm	4(%r1), %r4
    101 	ldwm	4(%r1), %r5
    102 	ldwm	4(%r1), %r6
    103 	ldwm	4(%r1), %r7
    104 	ldwm	4(%r1), %r8
    105 	ldwm	4(%r1), %r9
    106 	ldwm	4(%r1), %r10
    107 	ldwm	4(%r1), %r11
    108 	ldwm	4(%r1), %r12
    109 	ldwm	4(%r1), %r13
    110 	ldwm	4(%r1), %r14
    111 	ldwm	4(%r1), %r15
    112 	ldwm	4(%r1), %r16
    113 	ldwm	4(%r1), %r17
    114 	ldwm	4(%r1), %r18
    115 
    116 	/* restore the rest */
    117 	ldw	8(%arg0), %sp
    118 	ldw	24(%arg0), %rp
    119 	bv	%r0(%rp)
    120 	nop
    121 botch:
    122 	bl	longjmperror, %rp
    123 	nop
    124 	bl	abort, %rp
    125 	nop
    126 EXIT(_longjmp)
    127 
    128 	.end
    129