xref: /titanic_52/usr/src/lib/libc/port/unwind/unwind.c (revision 6a3e8e8695d5c7d1d18c6800d676990d7f61a2a4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e8031f0aSraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277257d1b4Sraf #include "lint.h"
287c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
297c478bd9Sstevel@tonic-gate #include <dlfcn.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * This is common code for sparc, sparcv9, and i386.
337c478bd9Sstevel@tonic-gate  * The amd64 unwind code is vastly different from this.
347c478bd9Sstevel@tonic-gate  * Look under the amd64-specific directory structure for details.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * _ex_unwind() is provided by libC, but if libC is not loaded we
397c478bd9Sstevel@tonic-gate  * need to call a local version of _ex_unwind() which does exactly
407c478bd9Sstevel@tonic-gate  * the same thing except for calling C++ destructors.
41*6a3e8e86SRichard Lowe  *
42*6a3e8e86SRichard Lowe  * Note that neither of these literally "returns twice" as, for eg, setjmp
43*6a3e8e86SRichard Lowe  * does, but they induce unusual control flow which the compiler should treat
44*6a3e8e86SRichard Lowe  * in the same manner (make all registers dead, etc.).
457c478bd9Sstevel@tonic-gate  */
46*6a3e8e86SRichard Lowe extern	void	_ex_clnup_handler(void *, void (*)(void *)) __RETURNS_TWICE;
47*6a3e8e86SRichard Lowe extern	void	_ex_unwind_local(void) __RETURNS_TWICE;
487c478bd9Sstevel@tonic-gate #pragma unknown_control_flow(_ex_clnup_handler)
497c478bd9Sstevel@tonic-gate #pragma unknown_control_flow(_ex_unwind_local)
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * _t_cancel(fp):calls cleanup handlers if there are any in
537c478bd9Sstevel@tonic-gate  *		 frame (fp), and calls _ex_unwind() to call
547c478bd9Sstevel@tonic-gate  *		 destructors if libC has been linked.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Control comes here from _thrp_unwind.  Logically:
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  *	_thrp_unwind: first arg = current fp;
597c478bd9Sstevel@tonic-gate  *	    jump _t_cancel;
607c478bd9Sstevel@tonic-gate  *
617257d1b4Sraf  * We could have called _t_cancel(_getfp) from thr_exit()
627c478bd9Sstevel@tonic-gate  * but _ex_unwind() also calls _t_cancel() and it does after
637c478bd9Sstevel@tonic-gate  * poping out the two frames.  If _ex_unwind() passes the current
647c478bd9Sstevel@tonic-gate  * fp, then it will be invalid.  For a caller of _thrp_unwind()
657c478bd9Sstevel@tonic-gate  * it looks as if it is calling _t_cancel(fp).
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * _t_cancel will eventually call _thrp_exit().
687c478bd9Sstevel@tonic-gate  * It never returns from _t_cancel().
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate void
727c478bd9Sstevel@tonic-gate _t_cancel(void *fp)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
757c478bd9Sstevel@tonic-gate 	__cleanup_t *head;
767c478bd9Sstevel@tonic-gate 	void (*fptr)(void (*func)(void *), void *arg);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	/* Do this once per thread exit, not once per unwind frame */
797c478bd9Sstevel@tonic-gate 	if (self->ul_ex_unwind == NULL &&
807c478bd9Sstevel@tonic-gate 	    (self->ul_ex_unwind = dlsym(RTLD_PROBE, "_ex_unwind")) == NULL)
817c478bd9Sstevel@tonic-gate 		self->ul_ex_unwind = (void *)-1;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (self->ul_ex_unwind == (void *)-1)
847c478bd9Sstevel@tonic-gate 		fptr = NULL;
857c478bd9Sstevel@tonic-gate 	else
867c478bd9Sstevel@tonic-gate 		fptr = (void (*)())self->ul_ex_unwind;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
897c478bd9Sstevel@tonic-gate 		_thrp_exit();
907c478bd9Sstevel@tonic-gate 		thr_panic("_t_cancel(): _thrp_exit() returned");
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if ((head = self->ul_clnup_hdr) != NULL && fp == head->fp) {
947c478bd9Sstevel@tonic-gate 		self->ul_clnup_hdr = head->next;
957c478bd9Sstevel@tonic-gate 		/* execute the cleanup handler */
967c478bd9Sstevel@tonic-gate 		_ex_clnup_handler(head->arg, head->func);
977c478bd9Sstevel@tonic-gate 		thr_panic("_t_cancel(): _ex_clnup_handler() returned");
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (fptr != NULL && self->ul_unwind) {
1017c478bd9Sstevel@tonic-gate 		/* libC is loaded and thread is canceled, call libC version */
1027c478bd9Sstevel@tonic-gate 		(*fptr)(_thrp_unwind, NULL);
1037c478bd9Sstevel@tonic-gate 		thr_panic("_t_cancel(): _ex_unwind() returned");
1047c478bd9Sstevel@tonic-gate 	} else if (head != NULL) {
1057c478bd9Sstevel@tonic-gate 		/* libC not present, call local version */
1067c478bd9Sstevel@tonic-gate 		_ex_unwind_local();
1077c478bd9Sstevel@tonic-gate 		thr_panic("_t_cancel(): _ex_unwind_local() returned");
1087c478bd9Sstevel@tonic-gate 	} else {
1097c478bd9Sstevel@tonic-gate 		/* libC not present and no cleanup handlers, exit here */
1107c478bd9Sstevel@tonic-gate 		_thrp_exit();
1117c478bd9Sstevel@tonic-gate 		thr_panic("_t_cancel(): _thrp_exit() returned");
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	/* never returns here */
1147c478bd9Sstevel@tonic-gate }
115