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