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