1*eb9395f8Suwe/*	$NetBSD: compat_setjmp.S,v 1.1 2005/10/15 22:11:22 uwe Exp $	*/
2*eb9395f8Suwe
3*eb9395f8Suwe/*
4*eb9395f8Suwe * Copyright (c) 1992, 1993
5*eb9395f8Suwe *	The Regents of the University of California.  All rights reserved.
6*eb9395f8Suwe *
7*eb9395f8Suwe * This software was developed by the Computer Systems Engineering group
8*eb9395f8Suwe * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9*eb9395f8Suwe * contributed to Berkeley.
10*eb9395f8Suwe *
11*eb9395f8Suwe * Redistribution and use in source and binary forms, with or without
12*eb9395f8Suwe * modification, are permitted provided that the following conditions
13*eb9395f8Suwe * are met:
14*eb9395f8Suwe * 1. Redistributions of source code must retain the above copyright
15*eb9395f8Suwe *    notice, this list of conditions and the following disclaimer.
16*eb9395f8Suwe * 2. Redistributions in binary form must reproduce the above copyright
17*eb9395f8Suwe *    notice, this list of conditions and the following disclaimer in the
18*eb9395f8Suwe *    documentation and/or other materials provided with the distribution.
19*eb9395f8Suwe * 3. Neither the name of the University nor the names of its contributors
20*eb9395f8Suwe *    may be used to endorse or promote products derived from this software
21*eb9395f8Suwe *    without specific prior written permission.
22*eb9395f8Suwe *
23*eb9395f8Suwe * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*eb9395f8Suwe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*eb9395f8Suwe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*eb9395f8Suwe * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*eb9395f8Suwe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*eb9395f8Suwe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*eb9395f8Suwe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*eb9395f8Suwe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*eb9395f8Suwe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*eb9395f8Suwe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*eb9395f8Suwe * SUCH DAMAGE.
34*eb9395f8Suwe *
35*eb9395f8Suwe * from: Header: setjmp.s,v 1.2 92/06/25 03:18:43 torek Exp
36*eb9395f8Suwe */
37*eb9395f8Suwe
38*eb9395f8Suwe#include <machine/asm.h>
39*eb9395f8Suwe#if defined(LIBC_SCCS) && !defined(lint)
40*eb9395f8Suwe#if 0
41*eb9395f8Suwe	.asciz "@(#)setjmp.s	8.1 (Berkeley) 6/4/93"
42*eb9395f8Suwe#else
43*eb9395f8Suwe	RCSID("$NetBSD: compat_setjmp.S,v 1.1 2005/10/15 22:11:22 uwe Exp $")
44*eb9395f8Suwe#endif
45*eb9395f8Suwe#endif /* LIBC_SCCS and not lint */
46*eb9395f8Suwe
47*eb9395f8Suwe/*
48*eb9395f8Suwe * C library -- setjmp, longjmp
49*eb9395f8Suwe *
50*eb9395f8Suwe *	longjmp(a,v)
51*eb9395f8Suwe * will generate a "return(v)" from
52*eb9395f8Suwe * the last call to
53*eb9395f8Suwe *	setjmp(a)
54*eb9395f8Suwe * by restoring registers from the stack,
55*eb9395f8Suwe * and a struct sigcontext, see <signal.h>
56*eb9395f8Suwe */
57*eb9395f8Suwe
58*eb9395f8Suwe#include "SYS.h"
59*eb9395f8Suwe
60*eb9395f8SuweENTRY(setjmp)
61*eb9395f8Suwe	/*
62*eb9395f8Suwe	 * We use the caller's `arg dump' area (%sp+0x44; there are 6 ints
63*eb9395f8Suwe	 * reserved there for us) to avoid having to allocate stack space
64*eb9395f8Suwe	 * here.
65*eb9395f8Suwe	 */
66*eb9395f8Suwe	mov	%o0, %o2	/* build sigcontext in [%o2] */
67*eb9395f8Suwe	mov	1, %o0		/* SIG_BLOCK */
68*eb9395f8Suwe	mov	SYS_compat_13_sigprocmask13, %g1
69*eb9395f8Suwe	clr	%o1		/* sigprocmask(SIG_BLOCK, (sigset_t *)NULL) */
70*eb9395f8Suwe	t	ST_SYSCALL
71*eb9395f8Suwe	st	%o0, [%o2 + 4]	/* sc.sc_mask = current mask; */
72*eb9395f8Suwe	mov	SYS___sigaltstack14, %g1
73*eb9395f8Suwe	clr	%o0		/* sigaltstack(NULL, &foo) */
74*eb9395f8Suwe	add	%sp, 0x48, %o1	/* (foo being in arg dump area) */
75*eb9395f8Suwe	t	ST_SYSCALL
76*eb9395f8Suwe	ld	[%sp + 0x50], %o0	/* foo.ss_flags */
77*eb9395f8Suwe	and	%o0, 1, %o1	/* onstack = foo.ss_flags & 1; */
78*eb9395f8Suwe	st	%o0, [%o2 + 0]	/* sc.sc_onstack = current onstack; */
79*eb9395f8Suwe	st	%sp, [%o2 + 8]	/* sc.sc_sp = sp (both ours and caller's) */
80*eb9395f8Suwe	add	%o7, 8, %o0
81*eb9395f8Suwe	st	%o0, [%o2 + 12]	/* sc.sc_pc = return_pc */
82*eb9395f8Suwe	add	%o7, 12, %o0
83*eb9395f8Suwe	st	%o0, [%o2 + 16]	/* sc.sc_npc = return_pc + 4 */
84*eb9395f8Suwe	st	%g0, [%o2 + 20]	/* sc.sc_psr = (clean psr) */
85*eb9395f8Suwe	st	%fp, [%o2 + 24]	/* sc.sc_g1 = %fp (misuse, but what the heck) */
86*eb9395f8Suwe				/* sc.sc_o0 = random(), set in longjmp */
87*eb9395f8Suwe	retl			/* return 0 */
88*eb9395f8Suwe	 clr	%o0
89*eb9395f8Suwe
90*eb9395f8Suwe/*
91*eb9395f8Suwe * All we need to do here is force sigreturn to load a new stack pointer,
92*eb9395f8Suwe * new <pc,npc>, and appropriate %o0 return value from the sigcontext built
93*eb9395f8Suwe * in setjmp.  The %i and %l registers will be reloaded from the place to
94*eb9395f8Suwe * which %sp points, due to sigreturn() semantics (sigreturn does not modify
95*eb9395f8Suwe * the window pointer in the psr, hence it must force all windows to reload).
96*eb9395f8Suwe */
97*eb9395f8SuweENTRY(longjmp)
98*eb9395f8Suwe	save	%sp, -96, %sp
99*eb9395f8Suwe	ld	[%i0 + 8], %o2	/* make sure sc->sc_sp, sc->sc_fp nonzero */
100*eb9395f8Suwe	ld	[%i0 + 24], %o3
101*eb9395f8Suwe	orcc	%o2, %o3, %g0
102*eb9395f8Suwe	bz	Lbotch
103*eb9395f8Suwe	 tst	%i1		/* if (v == 0) v = 1; */
104*eb9395f8Suwe	bz,a	1f
105*eb9395f8Suwe	 mov	1, %i1
106*eb9395f8Suwe1:
107*eb9395f8Suwe	st	%i1, [%i0 + 28]	/* sc.sc_o0 = v; */
108*eb9395f8Suwe	mov	SYS_compat_13_sigreturn13, %g1
109*eb9395f8Suwe	mov	%i0, %o0
110*eb9395f8Suwe	t	ST_SYSCALL	/* sigreturn(scp); */
111*eb9395f8Suwe
112*eb9395f8SuweLbotch:
113*eb9395f8Suwe	/* oops, caller botched it */
114*eb9395f8Suwe	call	_C_LABEL(longjmperror)
115*eb9395f8Suwe	 nop
116*eb9395f8Suwe	unimp	0
117