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
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * 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 */
21*7257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
29*7257d1b4Sraf #include "lint.h"
307c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
317c478bd9Sstevel@tonic-gate #include "stack_unwind.h"
327c478bd9Sstevel@tonic-gate #include "reg_num.h"
337c478bd9Sstevel@tonic-gate #include <dlfcn.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate * Due to the subtle mysteries of the amd64 unwind interfaces, the
377c478bd9Sstevel@tonic-gate * "Canonical Frame Address" is 16 bytes higher in memory than the
387c478bd9Sstevel@tonic-gate * value of the frame pointer (%fp).
397c478bd9Sstevel@tonic-gate */
407c478bd9Sstevel@tonic-gate #define CFA_ADJUST 16
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /* ARGSUSED */
437c478bd9Sstevel@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)447c478bd9Sstevel@tonic-gate posix_stop_func(
457c478bd9Sstevel@tonic-gate int version,
467c478bd9Sstevel@tonic-gate _Unwind_Action _Unwind_actions,
477c478bd9Sstevel@tonic-gate uint64_t exceptionClass,
487c478bd9Sstevel@tonic-gate struct _Unwind_Exception *exceptionObject,
497c478bd9Sstevel@tonic-gate struct _Unwind_Context *context,
507c478bd9Sstevel@tonic-gate void *func_arg)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate __cleanup_t **headp = (__cleanup_t **)func_arg;
537c478bd9Sstevel@tonic-gate __cleanup_t *head;
547c478bd9Sstevel@tonic-gate uint64_t cfa;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate * If we have reached the origin of the stack, exit now.
587c478bd9Sstevel@tonic-gate */
597c478bd9Sstevel@tonic-gate cfa = _Unwind_GetCFA(context);
607c478bd9Sstevel@tonic-gate if (cfa == 0 || _Unwind_GetGR(context, RET_ADD) == 0) {
617c478bd9Sstevel@tonic-gate _Unwind_DeleteException(exceptionObject);
627c478bd9Sstevel@tonic-gate _thrp_exit();
637c478bd9Sstevel@tonic-gate thr_panic("posix_stop_func(): _thrp_exit() returned");
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * Call all Posix cleanup handlers for this frame.
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate while ((head = *headp) != NULL &&
707c478bd9Sstevel@tonic-gate (caddr_t)cfa == head->fp + CFA_ADJUST) {
717c478bd9Sstevel@tonic-gate *headp = head->next;
727c478bd9Sstevel@tonic-gate (*head->func)(head->arg);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate return (_URC_NO_REASON);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * _ex_unwind() is provided by libCrun to perform stack unwinding
807c478bd9Sstevel@tonic-gate * and calling C++ destructors as needed, interleaved with calling
817c478bd9Sstevel@tonic-gate * Posix cleanup handlers along the way. If libCrun is not present
827c478bd9Sstevel@tonic-gate * we just need to call the Posix cleanup handlers.
837c478bd9Sstevel@tonic-gate */
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /* ARGSUSED */
867c478bd9Sstevel@tonic-gate void
_thrp_unwind(void * dummy)877c478bd9Sstevel@tonic-gate _thrp_unwind(void *dummy)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
907c478bd9Sstevel@tonic-gate __cleanup_t **headp = &self->ul_clnup_hdr;
917c478bd9Sstevel@tonic-gate __cleanup_t *head;
927c478bd9Sstevel@tonic-gate void (*fptr)(_Unwind_Stop_Fn, void *);
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /* Do this once per thread exit, not once per unwind frame */
957c478bd9Sstevel@tonic-gate if (self->ul_ex_unwind == NULL &&
967c478bd9Sstevel@tonic-gate (self->ul_ex_unwind = dlsym(RTLD_PROBE, "_ex_unwind")) == NULL)
977c478bd9Sstevel@tonic-gate self->ul_ex_unwind = (void *)-1;
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate if (self->ul_ex_unwind == (void *)-1)
1007c478bd9Sstevel@tonic-gate fptr = NULL;
1017c478bd9Sstevel@tonic-gate else
1027c478bd9Sstevel@tonic-gate fptr = (void (*)())self->ul_ex_unwind;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * Call _ex_unwind() if it is present (C++ loaded),
1067c478bd9Sstevel@tonic-gate * else just call the Posix cleanup handlers.
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate if (fptr != NULL)
1097c478bd9Sstevel@tonic-gate (*fptr)(posix_stop_func, headp);
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate * Call all remaining Posix cleanup handlers.
1137c478bd9Sstevel@tonic-gate */
1147c478bd9Sstevel@tonic-gate while ((head = *headp) != NULL) {
1157c478bd9Sstevel@tonic-gate *headp = head->next;
1167c478bd9Sstevel@tonic-gate (*head->func)(head->arg);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate _thrp_exit();
1207c478bd9Sstevel@tonic-gate thr_panic("_thrp_unwind(): _thrp_exit() returned");
1217c478bd9Sstevel@tonic-gate }
122