xref: /illumos-gate/usr/src/lib/libc/amd64/unwind/unwind.c (revision 0293487c)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*0293487cSraf 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * UNWIND - Unwind library
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * ===================== stack walk ====================
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Stack walk-back starts with the user code at the top of the stack
387c478bd9Sstevel@tonic-gate  * calling a language specific support routine which calls the generic
397c478bd9Sstevel@tonic-gate  * unwind code. The unwind code captures
407c478bd9Sstevel@tonic-gate  * information which can be used to partially build an _Unwind_Context
417c478bd9Sstevel@tonic-gate  * for the user code containing:
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  *    callee saves registers <current values>
447c478bd9Sstevel@tonic-gate  *    PC
457c478bd9Sstevel@tonic-gate  *    %rbp
467c478bd9Sstevel@tonic-gate  *    %rsp
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Using that pc location the unwind info for the function is found.
497c478bd9Sstevel@tonic-gate  * Then the CFA operations encoded in the unwind info are interepreted to get
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *    callee saves registers <values on entry>
527c478bd9Sstevel@tonic-gate  *    the return address
537c478bd9Sstevel@tonic-gate  *    cannonical frame address
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * completing the context for the user function (See
567c478bd9Sstevel@tonic-gate  * _Unw_Rollback_Registers()) .
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * The values computed above are equivalent to the info which would have been
597c478bd9Sstevel@tonic-gate  * captured from the caller and are used to initialize the callers context
607c478bd9Sstevel@tonic-gate  * (see _Unw_Propagate_Registers()) which can be completed.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * Using the same two-step procedure
637c478bd9Sstevel@tonic-gate  * context records for each frame down the stack may be constructed
647c478bd9Sstevel@tonic-gate  * in turn.  The ABI defined interface to _Unwind_Context provides
657c478bd9Sstevel@tonic-gate  * access to
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  *    callee saves registers <current values>
687c478bd9Sstevel@tonic-gate  *    current PC
697c478bd9Sstevel@tonic-gate  *    frame pointer
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * and allows changing
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  *    PC
747c478bd9Sstevel@tonic-gate  *    values of integer argument registers
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * (changed values take effect if context is "installed" - think
777c478bd9Sstevel@tonic-gate  * setcontext(2))
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * |                              |
847c478bd9Sstevel@tonic-gate  * | local storage for start()    | <FP == 0>
857c478bd9Sstevel@tonic-gate  * |                              |
867c478bd9Sstevel@tonic-gate  * --------------------------------.
877c478bd9Sstevel@tonic-gate  * |                              |
887c478bd9Sstevel@tonic-gate  * |     ..........               |
897c478bd9Sstevel@tonic-gate  * |                              | <-  CFA for bar()
907c478bd9Sstevel@tonic-gate  * --------------------------------.
917c478bd9Sstevel@tonic-gate  * |                              |
927c478bd9Sstevel@tonic-gate  * | local storage for bar()      |
937c478bd9Sstevel@tonic-gate  * |                              | <-  SP for bar(), CFA for foo()
947c478bd9Sstevel@tonic-gate  * ................................
957c478bd9Sstevel@tonic-gate  * |  pc for bar()                |
967c478bd9Sstevel@tonic-gate  * --------------------------------
977c478bd9Sstevel@tonic-gate  * |                              |
987c478bd9Sstevel@tonic-gate  * | local storage for foo()      |
997c478bd9Sstevel@tonic-gate  * |                              | <-  SP for foo(), CFA for ex_throw()
1007c478bd9Sstevel@tonic-gate  * ................................
1017c478bd9Sstevel@tonic-gate  * | pc for foo() - PC3           |
1027c478bd9Sstevel@tonic-gate  * ................................
1037c478bd9Sstevel@tonic-gate  * | saved RBP from foo() - BP3   | <-  FP for ex_throw() == FP2
1047c478bd9Sstevel@tonic-gate  * --------------------------------
1057c478bd9Sstevel@tonic-gate  * |                              |
1067c478bd9Sstevel@tonic-gate  * | local storage for ex_throw() |
1077c478bd9Sstevel@tonic-gate  * |                              | <- SP for ex_throw(), CFA for Unw()
1087c478bd9Sstevel@tonic-gate  * ................................
1097c478bd9Sstevel@tonic-gate  * | pc for ex_throw() - PC2      |
1107c478bd9Sstevel@tonic-gate  * ................................
1117c478bd9Sstevel@tonic-gate  * | saved RBP from ex_throw()    | <- FP for Unw() == FP1
1127c478bd9Sstevel@tonic-gate  * --------------------------------
1137c478bd9Sstevel@tonic-gate  * |                              |
1147c478bd9Sstevel@tonic-gate  * | local storage for Unw()      |
1157c478bd9Sstevel@tonic-gate  * |                              | <- SP for Unw() == SP1
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * We know that Unw() and ex_throw save and have an FP
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #ifdef _LIBCRUN_
1227c478bd9Sstevel@tonic-gate #define	_Unwind_DeleteException  _SUNW_Unwind_DeleteException
1237c478bd9Sstevel@tonic-gate #define	_Unwind_ForcedUnwind  _SUNW_Unwind_ForcedUnwind
1247c478bd9Sstevel@tonic-gate #define	_Unwind_GetCFA  _SUNW_Unwind_GetCFA
1257c478bd9Sstevel@tonic-gate #define	_Unwind_GetGR  _SUNW_Unwind_GetGR
1267c478bd9Sstevel@tonic-gate #define	_Unwind_GetIP  _SUNW_Unwind_GetIP
1277c478bd9Sstevel@tonic-gate #define	_Unwind_GetLanguageSpecificData _SUNW_Unwind_GetLanguageSpecificData
1287c478bd9Sstevel@tonic-gate #define	_Unwind_GetRegionStart  _SUNW_Unwind_GetRegionStart
1297c478bd9Sstevel@tonic-gate #define	_Unwind_RaiseException  _SUNW_Unwind_RaiseException
1307c478bd9Sstevel@tonic-gate #define	_Unwind_Resume  _SUNW_Unwind_Resume
1317c478bd9Sstevel@tonic-gate #define	_Unwind_SetGR  _SUNW_Unwind_SetGR
1327c478bd9Sstevel@tonic-gate #define	_Unwind_SetIP  _SUNW_Unwind_SetIP
1337c478bd9Sstevel@tonic-gate #else
1347c478bd9Sstevel@tonic-gate #pragma weak _Unwind_DeleteException = _SUNW_Unwind_DeleteException
1357c478bd9Sstevel@tonic-gate #pragma weak _Unwind_ForcedUnwind = _SUNW_Unwind_ForcedUnwind
1367c478bd9Sstevel@tonic-gate #pragma weak _Unwind_GetCFA = _SUNW_Unwind_GetCFA
1377c478bd9Sstevel@tonic-gate #pragma weak _Unwind_GetGR = _SUNW_Unwind_GetGR
1387c478bd9Sstevel@tonic-gate #pragma weak _Unwind_GetIP = _SUNW_Unwind_GetIP
1397c478bd9Sstevel@tonic-gate #pragma weak _Unwind_GetLanguageSpecificData = \
1407c478bd9Sstevel@tonic-gate 			_SUNW_Unwind_GetLanguageSpecificData
1417c478bd9Sstevel@tonic-gate #pragma weak _Unwind_GetRegionStart = _SUNW_Unwind_GetRegionStart
1427c478bd9Sstevel@tonic-gate #pragma weak _Unwind_RaiseException = _SUNW_Unwind_RaiseException
1437c478bd9Sstevel@tonic-gate #pragma weak _Unwind_Resume = _SUNW_Unwind_Resume
1447c478bd9Sstevel@tonic-gate #pragma weak _Unwind_SetGR = _SUNW_Unwind_SetGR
1457c478bd9Sstevel@tonic-gate #pragma weak _Unwind_SetIP = _SUNW_Unwind_SetIP
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #include "synonyms.h"
1487c478bd9Sstevel@tonic-gate #endif
1497c478bd9Sstevel@tonic-gate 
150*0293487cSraf #include <string.h>
1517c478bd9Sstevel@tonic-gate #include "stack_unwind.h"
1527c478bd9Sstevel@tonic-gate #include "reg_num.h"
1537c478bd9Sstevel@tonic-gate #include "unwind_context.h"
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate const _Unwind_Action _UA_SEARCH_PHASE = 1;
1567c478bd9Sstevel@tonic-gate const _Unwind_Action _UA_CLEANUP_PHASE = 2;
1577c478bd9Sstevel@tonic-gate const _Unwind_Action _UA_HANDLER_FRAME = 4;
1587c478bd9Sstevel@tonic-gate const _Unwind_Action _UA_FORCE_UNWIND = 8;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate void _Unw_capture_regs(uint64_t *regs);
1617c478bd9Sstevel@tonic-gate void _Unw_jmp(uint64_t pc, uint64_t *regs);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static void
1647c478bd9Sstevel@tonic-gate copy_ctx(struct _Unwind_Context *ctx1, struct _Unwind_Context *ctx2)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	if (ctx1 != ctx2) {
167*0293487cSraf 		(void) memcpy(ctx2, ctx1, sizeof (*ctx2));
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static _Unwind_Personality_Fn
1727c478bd9Sstevel@tonic-gate ctx_who(struct _Unwind_Context *ctx)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	return (ctx->pfn);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /* ARGSUSED */
1787c478bd9Sstevel@tonic-gate _Unwind_Reason_Code
1797c478bd9Sstevel@tonic-gate _Unw_very_boring_personality(int version, int actions, uint64_t exclass,
1807c478bd9Sstevel@tonic-gate 	struct _Unwind_Exception *exception_object,
1817c478bd9Sstevel@tonic-gate 	struct _Unwind_Context *ctx)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	_Unwind_Reason_Code res = _URC_CONTINUE_UNWIND;
1847c478bd9Sstevel@tonic-gate 	uint64_t fp;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	fp =  _Unwind_GetCFA(ctx);
1877c478bd9Sstevel@tonic-gate 	if (fp == 0 || _Unwind_GetIP(ctx) == 0) {
1887c478bd9Sstevel@tonic-gate 		return (_URC_END_OF_STACK);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	return (res);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * The only static variables in this code - changed by debugging hook below
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate static int using_ehf = 1;
1977c478bd9Sstevel@tonic-gate static uintptr_t def_per_fcn = (uintptr_t)&_Unw_very_boring_personality;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate void
2007c478bd9Sstevel@tonic-gate _SUNW_Unw_set_defaults(int use, uintptr_t def_per)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	using_ehf = use;
2037c478bd9Sstevel@tonic-gate 	def_per_fcn = def_per;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static void
2077c478bd9Sstevel@tonic-gate complete_context(struct _Unwind_Context *ctx)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	struct eh_frame_fields sf;
2107c478bd9Sstevel@tonic-gate 	struct eh_frame_fields *sfp = 0;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	ctx->pfn = (_Unwind_Personality_Fn)def_per_fcn;
2137c478bd9Sstevel@tonic-gate 	ctx->lsda = 0;
2147c478bd9Sstevel@tonic-gate 	ctx->func = 0;
2157c478bd9Sstevel@tonic-gate 	ctx->range = 0;
2167c478bd9Sstevel@tonic-gate 	ctx->fde = 0;
2177c478bd9Sstevel@tonic-gate 	if (using_ehf && (0 != _Unw_EhfhLookup(ctx))) {
2187c478bd9Sstevel@tonic-gate 		sfp = _Unw_Decode_FDE(&sf, ctx);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 	(void) _Unw_Rollback_Registers(sfp, ctx);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * input: FP1 (or FP2 if from _Unwind_Resume (from_landing_pad))
2257c478bd9Sstevel@tonic-gate  *
2267c478bd9Sstevel@tonic-gate  * FP2 = FP1[0];
2277c478bd9Sstevel@tonic-gate  * BP3 = FP2[0];
2287c478bd9Sstevel@tonic-gate  * PC3 = FP2[1];
2297c478bd9Sstevel@tonic-gate  * SP3 = FP2 + 16;
2307c478bd9Sstevel@tonic-gate  *
2317c478bd9Sstevel@tonic-gate  * output: PC3, SP3, and BP3
2327c478bd9Sstevel@tonic-gate  *
2337c478bd9Sstevel@tonic-gate  * remaining callee saves registers are also captured in context
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate static void
2367c478bd9Sstevel@tonic-gate finish_capture(struct _Unwind_Context *ctx, int from_landing_pad)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	uint64_t fp1 = ctx->current_regs[FP_RBP];
2397c478bd9Sstevel@tonic-gate 	uint64_t fp2 = from_landing_pad ? fp1 : ((uint64_t *)fp1)[0];
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	ctx->pc = ((uint64_t *)fp2)[1];
2427c478bd9Sstevel@tonic-gate 	ctx->current_regs[SP_RSP] = fp2 + 16;
2437c478bd9Sstevel@tonic-gate 	ctx->current_regs[FP_RBP] = ((uint64_t *)fp2)[0];
2447c478bd9Sstevel@tonic-gate 	complete_context(ctx);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static int
2487c478bd9Sstevel@tonic-gate down_one(struct _Unwind_Context *old_ctx, struct _Unwind_Context *new_ctx)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	uint64_t old_cfa = old_ctx->cfa;
2517c478bd9Sstevel@tonic-gate 	uint64_t old_pc = old_ctx->pc;
2527c478bd9Sstevel@tonic-gate 	uint64_t new_cfa;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (old_cfa == 0 || old_pc == 0) {
2557c478bd9Sstevel@tonic-gate 		new_ctx->pc = 0;
2567c478bd9Sstevel@tonic-gate 		new_ctx->cfa = 0;
2577c478bd9Sstevel@tonic-gate 		new_ctx->ra = 0;
2587c478bd9Sstevel@tonic-gate 		return (1);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	if (old_ctx->ra == 0) {
2617c478bd9Sstevel@tonic-gate 		new_ctx->pc = 0;
2627c478bd9Sstevel@tonic-gate 		new_ctx->cfa = 0;
2637c478bd9Sstevel@tonic-gate 		new_ctx->ra = 0;
2647c478bd9Sstevel@tonic-gate 		return (0);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	/* now shift ----------------------------- */
2677c478bd9Sstevel@tonic-gate 	_Unw_Propagate_Registers(old_ctx, new_ctx);
2687c478bd9Sstevel@tonic-gate 	complete_context(new_ctx);
2697c478bd9Sstevel@tonic-gate 	new_cfa = new_ctx->cfa;
2707c478bd9Sstevel@tonic-gate 	if ((new_cfa < old_cfa) || (new_cfa & 7)) {
2717c478bd9Sstevel@tonic-gate 		new_ctx->pc = 0;
2727c478bd9Sstevel@tonic-gate 		new_ctx->cfa = 0;
2737c478bd9Sstevel@tonic-gate 		new_ctx->ra = 0;
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	return (0);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate static void
2797c478bd9Sstevel@tonic-gate jmp_ctx(struct _Unwind_Context *ctx)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	_Unw_jmp(ctx->pc, ctx->current_regs);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * Here starts the real work - the entry points from either a language
2867c478bd9Sstevel@tonic-gate  * runtime or directly from user code.
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * The two ..._Body functions are intended as private interfaces for
2897c478bd9Sstevel@tonic-gate  * Sun code as well so should remain accessible.
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate _Unwind_Reason_Code
2927c478bd9Sstevel@tonic-gate _Unwind_RaiseException_Body(struct _Unwind_Exception *exception_object,
2937c478bd9Sstevel@tonic-gate 	struct _Unwind_Context *entry_ctx, int phase)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	struct _Unwind_Context context;
2967c478bd9Sstevel@tonic-gate 	struct _Unwind_Context *ctx = &context;
2977c478bd9Sstevel@tonic-gate 	_Unwind_Reason_Code res;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (phase & _UA_SEARCH_PHASE) {
3007c478bd9Sstevel@tonic-gate 		finish_capture(entry_ctx, 0);
3017c478bd9Sstevel@tonic-gate 		copy_ctx(entry_ctx, ctx);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		for (;;) {
3047c478bd9Sstevel@tonic-gate 			res = (*ctx_who(ctx))(1, phase,
3057c478bd9Sstevel@tonic-gate 			    exception_object->exception_class,
3067c478bd9Sstevel@tonic-gate 			    exception_object, ctx);
3077c478bd9Sstevel@tonic-gate 			if (res != _URC_CONTINUE_UNWIND)
3087c478bd9Sstevel@tonic-gate 				break;
3097c478bd9Sstevel@tonic-gate 			if (down_one(ctx, ctx))
3107c478bd9Sstevel@tonic-gate 				return (_URC_FATAL_PHASE1_ERROR);
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 		switch (res) {
3137c478bd9Sstevel@tonic-gate 		case _URC_HANDLER_FOUND:
3147c478bd9Sstevel@tonic-gate 			exception_object->private_2 = _Unwind_GetCFA(ctx);
3157c478bd9Sstevel@tonic-gate 			break;
3167c478bd9Sstevel@tonic-gate 		default:
3177c478bd9Sstevel@tonic-gate 			return (res);
3187c478bd9Sstevel@tonic-gate 			break;
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 	} else {
3217c478bd9Sstevel@tonic-gate 		finish_capture(entry_ctx, 1);
3227c478bd9Sstevel@tonic-gate 		if (down_one(entry_ctx, entry_ctx))
3237c478bd9Sstevel@tonic-gate 			return (_URC_FATAL_PHASE2_ERROR);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	phase = _UA_CLEANUP_PHASE;
3277c478bd9Sstevel@tonic-gate 	copy_ctx(entry_ctx, ctx);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	for (;;) {
3307c478bd9Sstevel@tonic-gate 		if (exception_object->private_2 == _Unwind_GetCFA(ctx)) {
3317c478bd9Sstevel@tonic-gate 			phase |= _UA_HANDLER_FRAME;
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		res = (*ctx_who(ctx))(1, phase,
3347c478bd9Sstevel@tonic-gate 		    exception_object->exception_class,
3357c478bd9Sstevel@tonic-gate 		    exception_object, ctx);
3367c478bd9Sstevel@tonic-gate 		if ((phase & _UA_HANDLER_FRAME) && res != _URC_INSTALL_CONTEXT)
3377c478bd9Sstevel@tonic-gate 			return (_URC_FATAL_PHASE2_ERROR);
3387c478bd9Sstevel@tonic-gate 		if (res != _URC_CONTINUE_UNWIND)
3397c478bd9Sstevel@tonic-gate 			break;
3407c478bd9Sstevel@tonic-gate 		if (down_one(ctx, ctx))
3417c478bd9Sstevel@tonic-gate 			return (_URC_FATAL_PHASE2_ERROR);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	switch (res) {
3447c478bd9Sstevel@tonic-gate 	case _URC_INSTALL_CONTEXT:
3457c478bd9Sstevel@tonic-gate 		exception_object->private_1 = 0;
3467c478bd9Sstevel@tonic-gate 		jmp_ctx(ctx); /* does not return */
3477c478bd9Sstevel@tonic-gate 		break;
3487c478bd9Sstevel@tonic-gate 	default:
3497c478bd9Sstevel@tonic-gate 		break;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 	return (res);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate _Unwind_Reason_Code
3557c478bd9Sstevel@tonic-gate _Unwind_RaiseException(struct _Unwind_Exception *exception_object)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	struct _Unwind_Context entry_context;
3587c478bd9Sstevel@tonic-gate 	struct _Unwind_Context *entry_ctx = &entry_context;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	_Unw_capture_regs(entry_ctx->current_regs);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	return (_Unwind_RaiseException_Body(exception_object, entry_ctx,
3637c478bd9Sstevel@tonic-gate 	    _UA_SEARCH_PHASE));
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate _Unwind_Reason_Code
3677c478bd9Sstevel@tonic-gate _Unwind_ForcedUnwind_Body(struct _Unwind_Exception *exception_object,
3687c478bd9Sstevel@tonic-gate 	_Unwind_Stop_Fn stop, void *stop_parameter,
3697c478bd9Sstevel@tonic-gate 	struct _Unwind_Context *ctx, int resume)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	_Unwind_Reason_Code res;
3727c478bd9Sstevel@tonic-gate 	int phase = _UA_CLEANUP_PHASE | _UA_FORCE_UNWIND;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	int again;
3757c478bd9Sstevel@tonic-gate 	int doper;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	finish_capture(ctx, resume);
3787c478bd9Sstevel@tonic-gate 	if (resume && down_one(ctx, ctx))
3797c478bd9Sstevel@tonic-gate 	    return (_URC_FATAL_PHASE2_ERROR);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	do {
3827c478bd9Sstevel@tonic-gate 		again = 0;
3837c478bd9Sstevel@tonic-gate 		doper = 0;
3847c478bd9Sstevel@tonic-gate 		res = (*stop)(1, phase,
3857c478bd9Sstevel@tonic-gate 		    exception_object->exception_class,
3867c478bd9Sstevel@tonic-gate 		    exception_object, ctx, stop_parameter);
3877c478bd9Sstevel@tonic-gate 		switch (res) {
3887c478bd9Sstevel@tonic-gate 		case _URC_CONTINUE_UNWIND:
3897c478bd9Sstevel@tonic-gate 			/* keep going - don't call personality */
3907c478bd9Sstevel@tonic-gate 			again = 1;
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 		case _URC_NO_REASON:
3937c478bd9Sstevel@tonic-gate 			/* keep going - do call personality */
3947c478bd9Sstevel@tonic-gate 			again = 1;
3957c478bd9Sstevel@tonic-gate 			doper = 1;
3967c478bd9Sstevel@tonic-gate 			break;
3977c478bd9Sstevel@tonic-gate 		case _URC_NORMAL_STOP:  /* done */
3987c478bd9Sstevel@tonic-gate 			break;
3997c478bd9Sstevel@tonic-gate 		case _URC_INSTALL_CONTEXT:  /* resume execution */
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate 		default:		/* failure */
4027c478bd9Sstevel@tonic-gate 			break;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 		if (doper) {
4057c478bd9Sstevel@tonic-gate 			res = (*ctx_who(ctx))(1, phase,
4067c478bd9Sstevel@tonic-gate 			    exception_object->exception_class,
4077c478bd9Sstevel@tonic-gate 			    exception_object, ctx);
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 		switch (res) {
4107c478bd9Sstevel@tonic-gate 		case _URC_INSTALL_CONTEXT:
4117c478bd9Sstevel@tonic-gate 			exception_object->private_1 = (uint64_t)stop;
4127c478bd9Sstevel@tonic-gate 			exception_object->private_2 = (uint64_t)stop_parameter;
4137c478bd9Sstevel@tonic-gate 			jmp_ctx(ctx); /* does not return */
4147c478bd9Sstevel@tonic-gate 			break;
4157c478bd9Sstevel@tonic-gate 		case _URC_CONTINUE_UNWIND:
4167c478bd9Sstevel@tonic-gate 		case _URC_NO_REASON:
4177c478bd9Sstevel@tonic-gate 			break;
4187c478bd9Sstevel@tonic-gate 		case _URC_END_OF_STACK:
4197c478bd9Sstevel@tonic-gate 			ctx->cfa = ctx->ra = ctx->pc = 0;
4207c478bd9Sstevel@tonic-gate 			res = (*stop)(1, phase,
4217c478bd9Sstevel@tonic-gate 					exception_object->exception_class,
4227c478bd9Sstevel@tonic-gate 					exception_object, ctx, stop_parameter);
4237c478bd9Sstevel@tonic-gate 			return (_URC_END_OF_STACK);
4247c478bd9Sstevel@tonic-gate 			break;
4257c478bd9Sstevel@tonic-gate 		default:
4267c478bd9Sstevel@tonic-gate 			again = 0;
4277c478bd9Sstevel@tonic-gate 			break;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		if (again) {
4307c478bd9Sstevel@tonic-gate 			if (down_one(ctx, ctx)) {
4317c478bd9Sstevel@tonic-gate 				return (_URC_FATAL_PHASE2_ERROR);
4327c478bd9Sstevel@tonic-gate 			}
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 	} while (again);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	return (res);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate _Unwind_Reason_Code
4407c478bd9Sstevel@tonic-gate _Unwind_ForcedUnwind(struct _Unwind_Exception *exception_object,
4417c478bd9Sstevel@tonic-gate 	_Unwind_Stop_Fn stop, void *stop_parameter)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	struct _Unwind_Context context;
4447c478bd9Sstevel@tonic-gate 	struct _Unwind_Context *ctx = &context;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	_Unw_capture_regs(ctx->current_regs);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	return (_Unwind_ForcedUnwind_Body(exception_object, stop,
4497c478bd9Sstevel@tonic-gate 	    stop_parameter, ctx, 0));
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate void
4537c478bd9Sstevel@tonic-gate _Unwind_Resume(struct _Unwind_Exception *exception_object)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	struct _Unwind_Context context;
4577c478bd9Sstevel@tonic-gate 	struct _Unwind_Context *ctx = &context;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	_Unw_capture_regs(ctx->current_regs);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if (exception_object->private_1)
4627c478bd9Sstevel@tonic-gate 		(void) _Unwind_ForcedUnwind_Body(exception_object,
4637c478bd9Sstevel@tonic-gate 		    (_Unwind_Stop_Fn)exception_object->private_1,
4647c478bd9Sstevel@tonic-gate 		    (void *)exception_object->private_2,
4657c478bd9Sstevel@tonic-gate 		    ctx, 1);
4667c478bd9Sstevel@tonic-gate 	else
4677c478bd9Sstevel@tonic-gate 		(void) _Unwind_RaiseException_Body(exception_object, ctx,
4687c478bd9Sstevel@tonic-gate 		    _UA_CLEANUP_PHASE);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /* Calls destructor function for exception object */
4727c478bd9Sstevel@tonic-gate void
4737c478bd9Sstevel@tonic-gate _Unwind_DeleteException(struct _Unwind_Exception *exception_object)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	if (exception_object->exception_cleanup != 0)
4767c478bd9Sstevel@tonic-gate 		(*(exception_object->exception_cleanup))(_URC_NO_REASON,
4777c478bd9Sstevel@tonic-gate 		    exception_object);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate /*
4827c478bd9Sstevel@tonic-gate  * stack frame context accessors defined in ABI
4837c478bd9Sstevel@tonic-gate  * (despite all the dire text in the ABI these are reliable Get/Set routines)
4847c478bd9Sstevel@tonic-gate  * Note: RA is handled as GR value
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate uint64_t
4877c478bd9Sstevel@tonic-gate _Unwind_GetGR(struct _Unwind_Context *context, int index)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	uint64_t res = 0;
4907c478bd9Sstevel@tonic-gate 	if (index <= EIR_R15) {
4917c478bd9Sstevel@tonic-gate 		res = context->current_regs[index];
4927c478bd9Sstevel@tonic-gate 	} else if (index == RET_ADD) {
4937c478bd9Sstevel@tonic-gate 		res = context->ra;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	return (res);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate void
5007c478bd9Sstevel@tonic-gate _Unwind_SetGR(struct _Unwind_Context *context, int index,
5017c478bd9Sstevel@tonic-gate uint64_t new_value)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	if (index <= EIR_R15) {
5047c478bd9Sstevel@tonic-gate 		context->current_regs[index] = new_value;
5057c478bd9Sstevel@tonic-gate 	} else if (index == RET_ADD) {
5067c478bd9Sstevel@tonic-gate 		context->ra = new_value;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate uint64_t
5127c478bd9Sstevel@tonic-gate _Unwind_GetIP(struct _Unwind_Context *context)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	return (context->pc);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate void
5187c478bd9Sstevel@tonic-gate _Unwind_SetIP(struct _Unwind_Context *context, uint64_t new_value)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	context->pc = new_value;
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate void *
5257c478bd9Sstevel@tonic-gate _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	return (context->lsda);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate uint64_t
5327c478bd9Sstevel@tonic-gate _Unwind_GetRegionStart(struct _Unwind_Context *context)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	return (context->func);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate uint64_t
5387c478bd9Sstevel@tonic-gate _Unwind_GetCFA(struct _Unwind_Context *context)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	return (context->cfa);
5417c478bd9Sstevel@tonic-gate }
542