xref: /onnv-gate/usr/src/lib/libc/sparc/gen/setjmp.s (revision 6812:febeba71273d)
10Sstevel@tonic-gate/*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*6812Sraf
22*6812Sraf/*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf * Use is subject to license terms.
25*6812Sraf */
26*6812Sraf
270Sstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate/*	  All Rights Reserved	*/
290Sstevel@tonic-gate
30*6812Sraf#pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate
32*6812Sraf	.file	"%M%"
330Sstevel@tonic-gate
340Sstevel@tonic-gate#include <sys/asm_linkage.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(setjmp,function)
370Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(longjmp,function)
380Sstevel@tonic-gate
390Sstevel@tonic-gate#include <sys/trap.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gateJB_FLAGS	= (0*4)	! offsets in jmpbuf (see siglonglmp.c)
420Sstevel@tonic-gateJB_SP		= (1*4)	! words 5 through 11 are unused!
430Sstevel@tonic-gateJB_PC		= (2*4)
440Sstevel@tonic-gateJB_FP		= (3*4)
450Sstevel@tonic-gateJB_I7		= (4*4)
460Sstevel@tonic-gate
470Sstevel@tonic-gate/*
480Sstevel@tonic-gate * setjmp(buf_ptr)
490Sstevel@tonic-gate * buf_ptr points to a twelve word array (jmp_buf)
500Sstevel@tonic-gate */
510Sstevel@tonic-gate	ENTRY(setjmp)
520Sstevel@tonic-gate	clr	[%o0 + JB_FLAGS]	! clear flags (used by sigsetjmp)
530Sstevel@tonic-gate	st	%sp, [%o0 + JB_SP]	! save caller's sp
540Sstevel@tonic-gate	add	%o7, 8, %o1		! comupte return pc
550Sstevel@tonic-gate	st	%o1, [%o0 + JB_PC]	! save pc
560Sstevel@tonic-gate	st	%fp, [%o0 + JB_FP]	! save fp
570Sstevel@tonic-gate	st	%i7, [%o0 + JB_I7]	! save %i7
580Sstevel@tonic-gate	retl
590Sstevel@tonic-gate	clr	%o0			! return (0)
600Sstevel@tonic-gate
610Sstevel@tonic-gate	SET_SIZE(setjmp)
620Sstevel@tonic-gate
630Sstevel@tonic-gate/*
640Sstevel@tonic-gate * longjmp(buf_ptr, val)
650Sstevel@tonic-gate * buf_ptr points to a jmpbuf which has been initialized by setjmp.
660Sstevel@tonic-gate * val is the value we wish to return to setjmp's caller
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * We flush the register file to the stack by doing a kernel call.
690Sstevel@tonic-gate * This is necessary to ensure that the registers we want to
700Sstevel@tonic-gate * pick up are stored on the stack, and that subsequent restores
710Sstevel@tonic-gate * will function correctly.
720Sstevel@tonic-gate *
730Sstevel@tonic-gate * sp, fp, and %i7, the caller's return address, are all restored
740Sstevel@tonic-gate * to the values they had at the time of the call to setjmp().  All
750Sstevel@tonic-gate * other locals, ins and outs are set to potentially random values
760Sstevel@tonic-gate * (as per the man page).  This is sufficient to permit the correct
770Sstevel@tonic-gate * operation of normal code.
780Sstevel@tonic-gate *
790Sstevel@tonic-gate * Actually, the above description is not quite correct.  If the routine
800Sstevel@tonic-gate * that called setjmp() has not altered the sp value of their frame we
810Sstevel@tonic-gate * will restore the remaining locals and ins to the values these
820Sstevel@tonic-gate * registers had in the this frame at the time of the call to longjmp()
830Sstevel@tonic-gate * (not setjmp()!).  This is intended to help compilers, typically not
840Sstevel@tonic-gate * C compilers, that have some registers assigned to fixed purposes,
850Sstevel@tonic-gate * and that only alter the values of these registers on function entry
860Sstevel@tonic-gate * and exit.
870Sstevel@tonic-gate *
880Sstevel@tonic-gate * Since a C routine could call setjmp() followed by alloca() and thus
890Sstevel@tonic-gate * alter the sp this feature will typically not be helpful for a C
900Sstevel@tonic-gate * compiler.
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * Note also that because the caller of a routine compiled "flat" (without
930Sstevel@tonic-gate * register windows) assumes that their ins and locals are preserved,
940Sstevel@tonic-gate * routines that call setjmp() must not be flat.
950Sstevel@tonic-gate */
960Sstevel@tonic-gate	ENTRY(longjmp)
970Sstevel@tonic-gate	ta	ST_FLUSH_WINDOWS	! flush all reg windows to the stack.
980Sstevel@tonic-gate	ld	[%o0 + JB_SP], %o2	! sp in %o2 until safe to puke there
990Sstevel@tonic-gate	ldd	[%o2 + (0*8)], %l0	! restore locals and ins if we can
1000Sstevel@tonic-gate	ldd	[%o2 + (1*8)], %l2
1010Sstevel@tonic-gate	ldd	[%o2 + (2*8)], %l4
1020Sstevel@tonic-gate	ldd	[%o2 + (3*8)], %l6
1030Sstevel@tonic-gate	ldd	[%o2 + (4*8)], %i0
1040Sstevel@tonic-gate	ldd	[%o2 + (5*8)], %i2
1050Sstevel@tonic-gate	ldd	[%o2 + (6*8)], %i4
1060Sstevel@tonic-gate	ld	[%o0 + JB_FP], %fp	! restore fp
1070Sstevel@tonic-gate	mov	%o2, %sp		! restore sp
1080Sstevel@tonic-gate	ld	[%o0 + JB_I7], %i7	! restore %i7
1090Sstevel@tonic-gate	ld	[%o0 + JB_PC], %o3	! get new return pc
1100Sstevel@tonic-gate	tst	%o1			! is return value 0?
1110Sstevel@tonic-gate	bnz	1f			! no - leave it alone
1120Sstevel@tonic-gate	sub	%o3, 8, %o7		! normalize return (for adb) (dly slot)
1130Sstevel@tonic-gate	mov	1, %o1			! yes - set it to one
1140Sstevel@tonic-gate1:
1150Sstevel@tonic-gate	retl
1160Sstevel@tonic-gate	mov	%o1, %o0		! return (val)
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate	SET_SIZE(longjmp)
119