xref: /original-bsd/lib/libc/sparc/gen/setjmp.s (revision 7921151c)
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 * %sccs.include.redist.c%
10 *
11 * from: $Header: setjmp.s,v 1.2 92/06/25 03:18:43 torek Exp $
12 */
13
14#if defined(LIBC_SCCS) && !defined(lint)
15	.asciz "@(#)setjmp.s	8.1 (Berkeley) 06/04/93"
16#endif /* LIBC_SCCS and not lint */
17
18/*
19 * C library -- setjmp, longjmp
20 *
21 *	longjmp(a,v)
22 * will generate a "return(v)" from
23 * the last call to
24 *	setjmp(a)
25 * by restoring registers from the stack,
26 * and a struct sigcontext, see <signal.h>
27 */
28
29#include "SYS.h"
30
31ENTRY(setjmp)
32	/*
33	 * We use the caller's `arg dump' area (%sp+0x44; there are 6 ints
34	 * reserved there for us) to avoid having to allocate stack space
35	 * here.
36	 */
37	mov	%o0, %o2	/* build sigcontext in [%o2] */
38	mov	1, %o0		/* SIG_BLOCK */
39	mov	SYS_sigprocmask, %g1
40	clr	%o1		/* sigprocmask(SIG_BLOCK, (sigset_t *)NULL) */
41	t	ST_SYSCALL
42	st	%o0, [%o2 + 4]	/* sc.sc_mask = current mask; */
43	mov	SYS_sigaltstack, %g1
44	clr	%o0		/* sigstack(NULL, &foo) */
45	add	%sp, 0x48, %o1	/* (foo being in arg dump area) */
46	t	ST_SYSCALL
47	ld	[%sp + 0x50], %o0	/* foo.ss_flags */
48	and	%o0, 1, %o1	/* onstack = foo.ss_flags & 1; */
49	st	%o0, [%o2 + 0]	/* sc.sc_onstack = current onstack; */
50	st	%sp, [%o2 + 8]	/* sc.sc_sp = sp (both ours and caller's) */
51	add	%o7, 8, %o0
52	st	%o0, [%o2 + 12]	/* sc.sc_pc = return_pc */
53	add	%o7, 12, %o0
54	st	%o0, [%o2 + 16]	/* sc.sc_npc = return_pc + 4 */
55	st	%g0, [%o2 + 20]	/* sc.sc_psr = (clean psr) */
56	st	%fp, [%o2 + 24]	/* sc.sc_g1 = %fp (misuse, but what the heck) */
57				/* sc.sc_o0 = random(), set in longjmp */
58	retl			/* return 0 */
59	 clr	%o0
60
61/*
62 * All we need to do here is force sigreturn to load a new stack pointer,
63 * new <pc,npc>, and appropriate %o0 return value from the sigcontext built
64 * in setjmp.  The %i and %l registers will be reloaded from the place to
65 * which %sp points, due to sigreturn() semantics (sigreturn does not modify
66 * the window pointer in the psr, hence it must force all windows to reload).
67 */
68ENTRY(longjmp)
69	save	%sp, -96, %sp
70	ld	[%i0 + 8], %o2	/* make sure sc->sc_sp, sc->sc_fp nonzero */
71	ld	[%i0 + 24], %o3
72	orcc	%o2, %o3, %g0
73	bz	Lbotch
74	 tst	%i1		/* if (v == 0) v = 1; */
75	bz,a	1f
76	 mov	1, %i1
771:
78	st	%i1, [%i0 + 28]	/* sc.sc_o0 = v; */
79	mov	SYS_sigreturn, %g1
80	mov	%i0, %o0
81	t	ST_SYSCALL	/* sigreturn(scp); */
82
83Lbotch:
84	/* oops, caller botched it */
85	call	_longjmperror
86	 nop
87	unimp	0
88