17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1braf * Common Development and Distribution License (the "License").
67257d1braf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21e8031f0raf
227c478bdstevel@tonic-gate/*
233cd8a1aRoger A. Faulkner * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
27dd2ed56Toomas Soome/*	  All Rights Reserved	*/
287c478bdstevel@tonic-gate
297257d1braf#include "lint.h"
307c478bdstevel@tonic-gate#include <sys/types.h>
317c478bdstevel@tonic-gate#include <sys/stack.h>
327c478bdstevel@tonic-gate#include <sys/frame.h>
337c478bdstevel@tonic-gate#include <memory.h>
347c478bdstevel@tonic-gate#include <ucontext.h>
357c478bdstevel@tonic-gate#include <setjmp.h>
367c478bdstevel@tonic-gate#include "sigjmp_struct.h"
377c478bdstevel@tonic-gate#include "libc.h"
387c478bdstevel@tonic-gate
39dd2ed56Toomas Soome#pragma weak _siglongjmp = siglongjmp
40dd2ed56Toomas Soome
417c478bdstevel@tonic-gatevoid
42e8031f0rafsiglongjmp(sigjmp_buf env, int val)
437c478bdstevel@tonic-gate{
447c478bdstevel@tonic-gate	extern void _fetch_globals(greg_t *);
457c478bdstevel@tonic-gate	ucontext_t uc;
467c478bdstevel@tonic-gate	greg_t *reg = uc.uc_mcontext.gregs;
477c478bdstevel@tonic-gate	volatile sigjmp_struct_t *bp = (sigjmp_struct_t *)env;
487c478bdstevel@tonic-gate	greg_t fp = bp->sjs_fp;
497c478bdstevel@tonic-gate	greg_t i7 = bp->sjs_i7;
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gate	/*
527c478bdstevel@tonic-gate	 * Create a ucontext_t structure from scratch.
537c478bdstevel@tonic-gate	 * We only need to fetch the globals.
547c478bdstevel@tonic-gate	 * The outs are assumed to be trashed on return from sigsetjmp().
557c478bdstevel@tonic-gate	 * The ins and locals are restored from the resumed register window.
567c478bdstevel@tonic-gate	 * The floating point state is unmodified.
577c478bdstevel@tonic-gate	 * Everything else is in the sigjmp_struct_t buffer.
587c478bdstevel@tonic-gate	 */
597c478bdstevel@tonic-gate	(void) memset(&uc, 0, sizeof (uc));
607c478bdstevel@tonic-gate	uc.uc_flags = UC_STACK | UC_CPU;
617c478bdstevel@tonic-gate	_fetch_globals(&reg[REG_G1]);
627c478bdstevel@tonic-gate	uc.uc_stack = bp->sjs_stack;
637c478bdstevel@tonic-gate	uc.uc_link = bp->sjs_uclink;
647c478bdstevel@tonic-gate	reg[REG_PC] = bp->sjs_pc;
657c478bdstevel@tonic-gate	reg[REG_nPC] = reg[REG_PC] + 0x4;
667c478bdstevel@tonic-gate	reg[REG_SP] = bp->sjs_sp;
673cd8a1aRoger A. Faulkner	reg[REG_ASI] = bp->sjs_asi;
683cd8a1aRoger A. Faulkner	reg[REG_FPRS] = bp->sjs_fprs;
697c478bdstevel@tonic-gate
707c478bdstevel@tonic-gate	if (bp->sjs_flags & JB_SAVEMASK) {
717c478bdstevel@tonic-gate		uc.uc_flags |= UC_SIGMASK;
727c478bdstevel@tonic-gate		uc.uc_sigmask = bp->sjs_sigmask;
737c478bdstevel@tonic-gate	}
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate	if (val)
767c478bdstevel@tonic-gate		reg[REG_O0] = (greg_t)val;
777c478bdstevel@tonic-gate	else
787c478bdstevel@tonic-gate		reg[REG_O0] = (greg_t)1;
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gate	/*
817c478bdstevel@tonic-gate	 * Copy the fp and i7 values into the register window save area.
827c478bdstevel@tonic-gate	 * These may have been clobbered between calls to sigsetjmp
837c478bdstevel@tonic-gate	 * and siglongjmp.  For example, the save area could have been
847c478bdstevel@tonic-gate	 * relocated to lower addresses and the original save area
857c478bdstevel@tonic-gate	 * given to an alloca() call.  Notice that all reads from
867c478bdstevel@tonic-gate	 * the sigjmp_struct_t buffer should take place before the
877c478bdstevel@tonic-gate	 * following two writes.  It is possible that user code may
887c478bdstevel@tonic-gate	 * move/copy the sigjmpbuf around, and overlap the original
897c478bdstevel@tonic-gate	 * register window save area.
907c478bdstevel@tonic-gate	 */
917c478bdstevel@tonic-gate	if (bp->sjs_sp != 0 && (bp->sjs_flags & JB_FRAMEPTR)) {
927c478bdstevel@tonic-gate		struct frame *sp = (struct frame *)(bp->sjs_sp + STACK_BIAS);
937c478bdstevel@tonic-gate		sp->fr_savfp = (struct frame *)fp;
947c478bdstevel@tonic-gate		sp->fr_savpc = i7;
957c478bdstevel@tonic-gate	}
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate	(void) setcontext(&uc);
987c478bdstevel@tonic-gate}
99