xref: /titanic_44/usr/src/lib/libc/sparcv9/gen/setjmp.s (revision 24db46411fd54f70c35b94bb952eb7ba040e43b4)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*	Copyright (c) 1988 AT&T	*/
23/*	  All Rights Reserved	*/
24
25
26/*
27 * Copyright 1987-2003 Sun Microsystems, Inc.  All rights reserved.
28 * Use is subject to license terms.
29 */
30
31.ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.4	*/
32
33	.file	"setjmp.s"
34
35#include <sys/asm_linkage.h>
36
37	ANSI_PRAGMA_WEAK(setjmp,function)
38	ANSI_PRAGMA_WEAK(longjmp,function)
39
40#include "synonyms.h"
41
42#include <sys/trap.h>
43
44JB_FLAGS	= (0*8)	! offsets in jmpbuf (see siglongjmp.c)
45JB_SP		= (1*8)	! words 5 through 11 are unused!
46JB_PC		= (2*8)
47JB_FP		= (3*8)
48JB_I7		= (4*8)
49
50/*
51 * setjmp(buf_ptr)
52 * buf_ptr points to a twelve word array (jmp_buf)
53 */
54	ENTRY(setjmp)
55	clr	[%o0 + JB_FLAGS]	! clear flags (used by sigsetjmp)
56	stx	%sp, [%o0 + JB_SP]	! save caller's sp
57	add	%o7, 8, %o1		! compute return pc
58	stx	%o1, [%o0 + JB_PC]	! save pc
59	stx	%fp, [%o0 + JB_FP]	! save fp
60	stx	%i7, [%o0 + JB_I7]	! save %i7
61	retl
62	clr	%o0			! return (0)
63
64	SET_SIZE(setjmp)
65
66/*
67 * longjmp(buf_ptr, val)
68 * buf_ptr points to a jmpbuf which has been initialized by setjmp.
69 * val is the value we wish to return to setjmp's caller
70 *
71 * We flush the register file to the stack by doing a kernel call.
72 * This is necessary to ensure that the registers we want to
73 * pick up are stored on the stack, and that subsequent restores
74 * will function correctly.
75 *
76 * sp, fp, and %i7, the caller's return address, are all restored
77 * to the values they had at the time of the call to setjmp().  All
78 * other locals, ins and outs are set to potentially random values
79 * (as per the man page).  This is sufficient to permit the correct
80 * operation of normal code.
81 *
82 * Actually, the above description is not quite correct.  If the routine
83 * that called setjmp() has not altered the sp value of their frame we
84 * will restore the remaining locals and ins to the values these
85 * registers had in the this frame at the time of the call to longjmp()
86 * (not setjmp()!).  This is intended to help compilers, typically not
87 * C compilers, that have some registers assigned to fixed purposes,
88 * and that only alter the values of these registers on function entry
89 * and exit.
90 *
91 * Since a C routine could call setjmp() followed by alloca() and thus
92 * alter the sp this feature will typically not be helpful for a C
93 * compiler.
94 *
95 * Note also that because the caller of a routine compiled "flat" (without
96 * register windows) assumes that their ins and locals are preserved,
97 * routines that call setjmp() must not be flat.
98 */
99	ENTRY(longjmp)
100	ta	ST_FLUSH_WINDOWS	! flush all reg windows to the stack.
101	ldx	[%o0 + JB_SP], %o2	! sp in %o2 until safe to puke there
102	ldx	[%o2 + STACK_BIAS], %l0	! restore locals and ins if we can
103	ldx	[%o2 + (1*8) + STACK_BIAS], %l1
104	ldx	[%o2 + (2*8) + STACK_BIAS], %l2
105	ldx	[%o2 + (3*8) + STACK_BIAS], %l3
106	ldx	[%o2 + (4*8) + STACK_BIAS], %l4
107	ldx	[%o2 + (5*8) + STACK_BIAS], %l5
108	ldx	[%o2 + (6*8) + STACK_BIAS], %l6
109	ldx	[%o2 + (7*8) + STACK_BIAS], %l7
110	ldx	[%o2 + (8*8) + STACK_BIAS], %i0
111	ldx	[%o2 + (9*8) + STACK_BIAS], %i1
112	ldx	[%o2 + (10*8) + STACK_BIAS], %i2
113	ldx	[%o2 + (11*8) + STACK_BIAS], %i3
114	ldx	[%o2 + (12*8) + STACK_BIAS], %i4
115	ldx	[%o2 + (13*8) + STACK_BIAS], %i5
116	ldx	[%o0 + JB_FP], %fp	! restore fp
117	mov	%o2, %sp		! restore sp
118	ldx	[%o0 + JB_I7], %i7	! restore %i7
119	ldx	[%o0 + JB_PC], %o3	! get new return pc
120	tst	%o1			! is return value 0?
121	bnz	1f			! no - leave it alone
122	sub	%o3, 8, %o7		! normalize return (for adb) (dly slot)
123	mov	1, %o1			! yes - set it to one
1241:
125	retl
126	mov	%o1, %o0		! return (val)
127
128	SET_SIZE(longjmp)
129