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 */
217257d1b4Sraf
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 "stack_unwind.h"
307c478bd9Sstevel@tonic-gate #include "reg_num.h"
317c478bd9Sstevel@tonic-gate #include <dlfcn.h>
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate * Due to the subtle mysteries of the amd64 unwind interfaces, the
357c478bd9Sstevel@tonic-gate * "Canonical Frame Address" is 16 bytes higher in memory than the
367c478bd9Sstevel@tonic-gate * value of the frame pointer (%fp).
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate #define CFA_ADJUST 16
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate static _Unwind_Reason_Code
posix_stop_func(int version __unused,_Unwind_Action _Unwind_actions __unused,uint64_t exceptionClass __unused,struct _Unwind_Exception * exceptionObject,struct _Unwind_Context * context,void * func_arg)41*4a38094cSToomas Soome posix_stop_func(int version __unused,
42*4a38094cSToomas Soome _Unwind_Action _Unwind_actions __unused,
43*4a38094cSToomas Soome uint64_t exceptionClass __unused,
447c478bd9Sstevel@tonic-gate struct _Unwind_Exception *exceptionObject,
457c478bd9Sstevel@tonic-gate struct _Unwind_Context *context,
467c478bd9Sstevel@tonic-gate void *func_arg)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate __cleanup_t **headp = (__cleanup_t **)func_arg;
497c478bd9Sstevel@tonic-gate __cleanup_t *head;
507c478bd9Sstevel@tonic-gate uint64_t cfa;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate * If we have reached the origin of the stack, exit now.
547c478bd9Sstevel@tonic-gate */
557c478bd9Sstevel@tonic-gate cfa = _Unwind_GetCFA(context);
567c478bd9Sstevel@tonic-gate if (cfa == 0 || _Unwind_GetGR(context, RET_ADD) == 0) {
577c478bd9Sstevel@tonic-gate _Unwind_DeleteException(exceptionObject);
587c478bd9Sstevel@tonic-gate _thrp_exit();
597c478bd9Sstevel@tonic-gate thr_panic("posix_stop_func(): _thrp_exit() returned");
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate * Call all Posix cleanup handlers for this frame.
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate while ((head = *headp) != NULL &&
667c478bd9Sstevel@tonic-gate (caddr_t)cfa == head->fp + CFA_ADJUST) {
677c478bd9Sstevel@tonic-gate *headp = head->next;
687c478bd9Sstevel@tonic-gate (*head->func)(head->arg);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate return (_URC_NO_REASON);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate * _ex_unwind() is provided by libCrun to perform stack unwinding
767c478bd9Sstevel@tonic-gate * and calling C++ destructors as needed, interleaved with calling
777c478bd9Sstevel@tonic-gate * Posix cleanup handlers along the way. If libCrun is not present
787c478bd9Sstevel@tonic-gate * we just need to call the Posix cleanup handlers.
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate void
_thrp_unwind(void * dummy __unused)82*4a38094cSToomas Soome _thrp_unwind(void *dummy __unused)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
857c478bd9Sstevel@tonic-gate __cleanup_t **headp = &self->ul_clnup_hdr;
867c478bd9Sstevel@tonic-gate __cleanup_t *head;
877c478bd9Sstevel@tonic-gate void (*fptr)(_Unwind_Stop_Fn, void *);
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /* Do this once per thread exit, not once per unwind frame */
907c478bd9Sstevel@tonic-gate if (self->ul_ex_unwind == NULL &&
917c478bd9Sstevel@tonic-gate (self->ul_ex_unwind = dlsym(RTLD_PROBE, "_ex_unwind")) == NULL)
927c478bd9Sstevel@tonic-gate self->ul_ex_unwind = (void *)-1;
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate if (self->ul_ex_unwind == (void *)-1)
957c478bd9Sstevel@tonic-gate fptr = NULL;
967c478bd9Sstevel@tonic-gate else
977c478bd9Sstevel@tonic-gate fptr = (void (*)())self->ul_ex_unwind;
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate * Call _ex_unwind() if it is present (C++ loaded),
1017c478bd9Sstevel@tonic-gate * else just call the Posix cleanup handlers.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate if (fptr != NULL)
1047c478bd9Sstevel@tonic-gate (*fptr)(posix_stop_func, headp);
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate * Call all remaining Posix cleanup handlers.
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate while ((head = *headp) != NULL) {
1107c478bd9Sstevel@tonic-gate *headp = head->next;
1117c478bd9Sstevel@tonic-gate (*head->func)(head->arg);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate _thrp_exit();
1157c478bd9Sstevel@tonic-gate thr_panic("_thrp_unwind(): _thrp_exit() returned");
1167c478bd9Sstevel@tonic-gate }
117