xref: /titanic_41/usr/src/cmd/mdb/sparc/v9/libstand/setjmp.s (revision ea394cb00fd96864e34d2841b4a22357b621c78f)
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/*
23 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29#if defined(__lint)
30#include <setjmp.h>
31#endif
32
33#include <sys/asm_linkage.h>
34
35/*
36 * This is a copy of the setjmp (and longjmp) code used in libc.  Note that
37 * we use sigsetjmp as an alias for setjmp, with a corresponding alias between
38 * siglongjmp and longjmp.  We can do this because there aren't any signals
39 * in kmdb (with the possible exception of the smoke signals the machine will
40 * emit when we break something).  We can also use a sigjmp_buf as a jmp_buf,
41 * since the latter is smaller than the former.
42 */
43
44#if !defined(__lint)
45JB_FLAGS	= (0*8)	! offsets in jmpbuf (see sigsetjmp.c)
46JB_SP		= (1*8)	! words 5 through 11 are unused!
47JB_PC		= (2*8)
48JB_FP		= (3*8)
49JB_I7		= (4*8)
50#endif
51
52/*
53 * setjmp(buf_ptr)
54 * buf_ptr points to a twelve word array (jmp_buf)
55 */
56
57#if defined(__lint)
58/* ARGSUSED */
59int
60setjmp(jmp_buf env)
61{
62	return (0);
63}
64
65/* ARGSUSED */
66int
67sigsetjmp(sigjmp_buf env, int savemask)
68{
69	return (0);
70}
71#else	/* __lint */
72
73	ENTRY(setjmp)
74	ALTENTRY(sigsetjmp)
75	clr	[%o0 + JB_FLAGS]	! clear flags (used by sigsetjmp)
76	stx	%sp, [%o0 + JB_SP]	! save caller's sp
77	add	%o7, 8, %o1		! compute return pc
78	stx	%o1, [%o0 + JB_PC]	! save pc
79	stx	%fp, [%o0 + JB_FP]	! save fp
80	stx	%i7, [%o0 + JB_I7]	! save %i7
81	flushw
82	retl
83	clr	%o0			! return (0)
84
85	SET_SIZE(setjmp)
86#endif	/* __lint */
87
88/*
89 * longjmp(buf_ptr, val)
90 * buf_ptr points to a jmpbuf which has been initialized by setjmp.
91 * val is the value we wish to return to setjmp's caller
92 *
93 * We flush the register file to the stack by doing a kernel call.
94 * This is necessary to ensure that the registers we want to
95 * pick up are stored on the stack, and that subsequent restores
96 * will function correctly.
97 *
98 * sp, fp, and %i7, the caller's return address, are all restored
99 * to the values they had at the time of the call to setjmp().  All
100 * other locals, ins and outs are set to potentially random values
101 * (as per the man page).  This is sufficient to permit the correct
102 * operation of normal code.
103 *
104 * Actually, the above description is not quite correct.  If the routine
105 * that called setjmp() has not altered the sp value of their frame we
106 * will restore the remaining locals and ins to the values these
107 * registers had in the this frame at the time of the call to longjmp()
108 * (not setjmp()!).  This is intended to help compilers, typically not
109 * C compilers, that have some registers assigned to fixed purposes,
110 * and that only alter the values of these registers on function entry
111 * and exit.
112 *
113 * Since a C routine could call setjmp() followed by alloca() and thus
114 * alter the sp this feature will typically not be helpful for a C
115 * compiler.
116 *
117 * Note also that because the caller of a routine compiled "flat" (without
118 * register windows) assumes that their ins and locals are preserved,
119 * routines that call setjmp() must not be flat.
120 */
121
122#if defined(__lint)
123/* ARGSUSED */
124void
125longjmp(jmp_buf env, int val)
126{
127}
128
129/* ARGSUSED */
130void
131siglongjmp(sigjmp_buf env, int val)
132{
133}
134#else	/* __lint */
135
136	ENTRY(longjmp)
137	ALTENTRY(siglongjmp)
138
139	/* flush all reg windows to the stack. */
140	save
141	flushw
142	restore
143	nop
144
145	ldx	[%o0 + JB_SP], %o2	! sp in %o2 until safe to puke there
146	ldx	[%o2 + STACK_BIAS], %l0	! restore locals and ins if we can
147	ldx	[%o2 + (1*8) + STACK_BIAS], %l1
148	ldx	[%o2 + (2*8) + STACK_BIAS], %l2
149	ldx	[%o2 + (3*8) + STACK_BIAS], %l3
150	ldx	[%o2 + (4*8) + STACK_BIAS], %l4
151	ldx	[%o2 + (5*8) + STACK_BIAS], %l5
152	ldx	[%o2 + (6*8) + STACK_BIAS], %l6
153	ldx	[%o2 + (7*8) + STACK_BIAS], %l7
154	ldx	[%o2 + (8*8) + STACK_BIAS], %i0
155	ldx	[%o2 + (9*8) + STACK_BIAS], %i1
156	ldx	[%o2 + (10*8) + STACK_BIAS], %i2
157	ldx	[%o2 + (11*8) + STACK_BIAS], %i3
158	ldx	[%o2 + (12*8) + STACK_BIAS], %i4
159	ldx	[%o2 + (13*8) + STACK_BIAS], %i5
160	ldx	[%o0 + JB_FP], %fp	! restore fp
161	mov	%o2, %sp		! restore sp
162	ldx	[%o0 + JB_I7], %i7	! restore %i7
163	ldx	[%o0 + JB_PC], %o3	! get new return pc
164	tst	%o1			! is return value 0?
165	bnz	1f			! no - leave it alone
166	sub	%o3, 8, %o7		! normalize return (for adb) (dly slot)
167	mov	1, %o1			! yes - set it to one
1681:
169	retl
170	mov	%o1, %o0		! return (val)
171
172	SET_SIZE(longjmp)
173#endif	/* __lint */
174