xref: /onnv-gate/usr/src/lib/libc/amd64/unwind/thrp_unwind.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "synonyms.h"
30*0Sstevel@tonic-gate #include "thr_uberdata.h"
31*0Sstevel@tonic-gate #include "stack_unwind.h"
32*0Sstevel@tonic-gate #include "reg_num.h"
33*0Sstevel@tonic-gate #include <dlfcn.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate  * Due to the subtle mysteries of the amd64 unwind interfaces, the
37*0Sstevel@tonic-gate  * "Canonical Frame Address" is 16 bytes higher in memory than the
38*0Sstevel@tonic-gate  * value of the frame pointer (%fp).
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate #define	CFA_ADJUST	16
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /* ARGSUSED */
43*0Sstevel@tonic-gate static _Unwind_Reason_Code
44*0Sstevel@tonic-gate posix_stop_func(
45*0Sstevel@tonic-gate 	int version,
46*0Sstevel@tonic-gate 	_Unwind_Action _Unwind_actions,
47*0Sstevel@tonic-gate 	uint64_t exceptionClass,
48*0Sstevel@tonic-gate 	struct _Unwind_Exception *exceptionObject,
49*0Sstevel@tonic-gate 	struct _Unwind_Context *context,
50*0Sstevel@tonic-gate 	void *func_arg)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate 	__cleanup_t **headp = (__cleanup_t **)func_arg;
53*0Sstevel@tonic-gate 	__cleanup_t *head;
54*0Sstevel@tonic-gate 	uint64_t cfa;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	/*
57*0Sstevel@tonic-gate 	 * If we have reached the origin of the stack, exit now.
58*0Sstevel@tonic-gate 	 */
59*0Sstevel@tonic-gate 	cfa = _Unwind_GetCFA(context);
60*0Sstevel@tonic-gate 	if (cfa == 0 || _Unwind_GetGR(context, RET_ADD) == 0) {
61*0Sstevel@tonic-gate 		_Unwind_DeleteException(exceptionObject);
62*0Sstevel@tonic-gate 		_thrp_exit();
63*0Sstevel@tonic-gate 		thr_panic("posix_stop_func(): _thrp_exit() returned");
64*0Sstevel@tonic-gate 	}
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	/*
67*0Sstevel@tonic-gate 	 * Call all Posix cleanup handlers for this frame.
68*0Sstevel@tonic-gate 	 */
69*0Sstevel@tonic-gate 	while ((head = *headp) != NULL &&
70*0Sstevel@tonic-gate 	    (caddr_t)cfa == head->fp + CFA_ADJUST) {
71*0Sstevel@tonic-gate 		*headp = head->next;
72*0Sstevel@tonic-gate 		(*head->func)(head->arg);
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	return (_URC_NO_REASON);
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  * _ex_unwind() is provided by libCrun to perform stack unwinding
80*0Sstevel@tonic-gate  * and calling C++ destructors as needed, interleaved with calling
81*0Sstevel@tonic-gate  * Posix cleanup handlers along the way.  If libCrun is not present
82*0Sstevel@tonic-gate  * we just need to call the Posix cleanup handlers.
83*0Sstevel@tonic-gate  */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate /* ARGSUSED */
86*0Sstevel@tonic-gate void
87*0Sstevel@tonic-gate _thrp_unwind(void *dummy)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	ulwp_t *self = curthread;
90*0Sstevel@tonic-gate 	__cleanup_t **headp = &self->ul_clnup_hdr;
91*0Sstevel@tonic-gate 	__cleanup_t *head;
92*0Sstevel@tonic-gate 	void (*fptr)(_Unwind_Stop_Fn, void *);
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	/* Do this once per thread exit, not once per unwind frame */
95*0Sstevel@tonic-gate 	if (self->ul_ex_unwind == NULL &&
96*0Sstevel@tonic-gate 	    (self->ul_ex_unwind = dlsym(RTLD_PROBE, "_ex_unwind")) == NULL)
97*0Sstevel@tonic-gate 		self->ul_ex_unwind = (void *)-1;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	if (self->ul_ex_unwind == (void *)-1)
100*0Sstevel@tonic-gate 		fptr = NULL;
101*0Sstevel@tonic-gate 	else
102*0Sstevel@tonic-gate 		fptr = (void (*)())self->ul_ex_unwind;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	/*
105*0Sstevel@tonic-gate 	 * Call _ex_unwind() if it is present (C++ loaded),
106*0Sstevel@tonic-gate 	 * else just call the Posix cleanup handlers.
107*0Sstevel@tonic-gate 	 */
108*0Sstevel@tonic-gate 	if (fptr != NULL)
109*0Sstevel@tonic-gate 		(*fptr)(posix_stop_func, headp);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	/*
112*0Sstevel@tonic-gate 	 * Call all remaining Posix cleanup handlers.
113*0Sstevel@tonic-gate 	 */
114*0Sstevel@tonic-gate 	while ((head = *headp) != NULL) {
115*0Sstevel@tonic-gate 		*headp = head->next;
116*0Sstevel@tonic-gate 		(*head->func)(head->arg);
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	_thrp_exit();
120*0Sstevel@tonic-gate 	thr_panic("_thrp_unwind(): _thrp_exit() returned");
121*0Sstevel@tonic-gate }
122