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