xref: /onnv-gate/usr/src/lib/libc/amd64/unwind/thrp_unwind.c (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 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29*6812Sraf #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
310Sstevel@tonic-gate #include "stack_unwind.h"
320Sstevel@tonic-gate #include "reg_num.h"
330Sstevel@tonic-gate #include <dlfcn.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * Due to the subtle mysteries of the amd64 unwind interfaces, the
370Sstevel@tonic-gate  * "Canonical Frame Address" is 16 bytes higher in memory than the
380Sstevel@tonic-gate  * value of the frame pointer (%fp).
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate #define	CFA_ADJUST	16
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /* ARGSUSED */
430Sstevel@tonic-gate static _Unwind_Reason_Code
posix_stop_func(int version,_Unwind_Action _Unwind_actions,uint64_t exceptionClass,struct _Unwind_Exception * exceptionObject,struct _Unwind_Context * context,void * func_arg)440Sstevel@tonic-gate posix_stop_func(
450Sstevel@tonic-gate 	int version,
460Sstevel@tonic-gate 	_Unwind_Action _Unwind_actions,
470Sstevel@tonic-gate 	uint64_t exceptionClass,
480Sstevel@tonic-gate 	struct _Unwind_Exception *exceptionObject,
490Sstevel@tonic-gate 	struct _Unwind_Context *context,
500Sstevel@tonic-gate 	void *func_arg)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	__cleanup_t **headp = (__cleanup_t **)func_arg;
530Sstevel@tonic-gate 	__cleanup_t *head;
540Sstevel@tonic-gate 	uint64_t cfa;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	/*
570Sstevel@tonic-gate 	 * If we have reached the origin of the stack, exit now.
580Sstevel@tonic-gate 	 */
590Sstevel@tonic-gate 	cfa = _Unwind_GetCFA(context);
600Sstevel@tonic-gate 	if (cfa == 0 || _Unwind_GetGR(context, RET_ADD) == 0) {
610Sstevel@tonic-gate 		_Unwind_DeleteException(exceptionObject);
620Sstevel@tonic-gate 		_thrp_exit();
630Sstevel@tonic-gate 		thr_panic("posix_stop_func(): _thrp_exit() returned");
640Sstevel@tonic-gate 	}
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * Call all Posix cleanup handlers for this frame.
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 	while ((head = *headp) != NULL &&
700Sstevel@tonic-gate 	    (caddr_t)cfa == head->fp + CFA_ADJUST) {
710Sstevel@tonic-gate 		*headp = head->next;
720Sstevel@tonic-gate 		(*head->func)(head->arg);
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	return (_URC_NO_REASON);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * _ex_unwind() is provided by libCrun to perform stack unwinding
800Sstevel@tonic-gate  * and calling C++ destructors as needed, interleaved with calling
810Sstevel@tonic-gate  * Posix cleanup handlers along the way.  If libCrun is not present
820Sstevel@tonic-gate  * we just need to call the Posix cleanup handlers.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /* ARGSUSED */
860Sstevel@tonic-gate void
_thrp_unwind(void * dummy)870Sstevel@tonic-gate _thrp_unwind(void *dummy)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	ulwp_t *self = curthread;
900Sstevel@tonic-gate 	__cleanup_t **headp = &self->ul_clnup_hdr;
910Sstevel@tonic-gate 	__cleanup_t *head;
920Sstevel@tonic-gate 	void (*fptr)(_Unwind_Stop_Fn, void *);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/* Do this once per thread exit, not once per unwind frame */
950Sstevel@tonic-gate 	if (self->ul_ex_unwind == NULL &&
960Sstevel@tonic-gate 	    (self->ul_ex_unwind = dlsym(RTLD_PROBE, "_ex_unwind")) == NULL)
970Sstevel@tonic-gate 		self->ul_ex_unwind = (void *)-1;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if (self->ul_ex_unwind == (void *)-1)
1000Sstevel@tonic-gate 		fptr = NULL;
1010Sstevel@tonic-gate 	else
1020Sstevel@tonic-gate 		fptr = (void (*)())self->ul_ex_unwind;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/*
1050Sstevel@tonic-gate 	 * Call _ex_unwind() if it is present (C++ loaded),
1060Sstevel@tonic-gate 	 * else just call the Posix cleanup handlers.
1070Sstevel@tonic-gate 	 */
1080Sstevel@tonic-gate 	if (fptr != NULL)
1090Sstevel@tonic-gate 		(*fptr)(posix_stop_func, headp);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/*
1120Sstevel@tonic-gate 	 * Call all remaining Posix cleanup handlers.
1130Sstevel@tonic-gate 	 */
1140Sstevel@tonic-gate 	while ((head = *headp) != NULL) {
1150Sstevel@tonic-gate 		*headp = head->next;
1160Sstevel@tonic-gate 		(*head->func)(head->arg);
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	_thrp_exit();
1200Sstevel@tonic-gate 	thr_panic("_thrp_unwind(): _thrp_exit() returned");
1210Sstevel@tonic-gate }
122