xref: /titanic_50/usr/src/lib/libproc/amd64/Pisadep.c (revision d9452f237f843c1321abb5810d2f9ee6cbeae43c)
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
5d51e9074Sab196087  * Common Development and Distribution License (the "License").
6d51e9074Sab196087  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*d9452f23SEdward Pilatowicz  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/stack.h>
277c478bd9Sstevel@tonic-gate #include <sys/regset.h>
287c478bd9Sstevel@tonic-gate #include <sys/frame.h>
297c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
307c478bd9Sstevel@tonic-gate #include <sys/trap.h>
31*d9452f23SEdward Pilatowicz #include <sys/machelf.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
407c478bd9Sstevel@tonic-gate #include "Pstack.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static uchar_t int_syscall_instr[] = { 0xCD, T_SYSCALLINT };
437c478bd9Sstevel@tonic-gate static uchar_t syscall_instr[] = { 0x0f, 0x05 };
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate const char *
467c478bd9Sstevel@tonic-gate Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	map_info_t *mp = Paddr2mptr(P, pltaddr);
497c478bd9Sstevel@tonic-gate 	file_info_t *fp;
507c478bd9Sstevel@tonic-gate 	size_t i;
517c478bd9Sstevel@tonic-gate 	uintptr_t r_addr;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if (mp == NULL || (fp = mp->map_file) == NULL ||
547c478bd9Sstevel@tonic-gate 	    fp->file_plt_base == 0 ||
557c478bd9Sstevel@tonic-gate 	    pltaddr - fp->file_plt_base >= fp->file_plt_size) {
567c478bd9Sstevel@tonic-gate 		errno = EINVAL;
577c478bd9Sstevel@tonic-gate 		return (NULL);
587c478bd9Sstevel@tonic-gate 	}
597c478bd9Sstevel@tonic-gate 
60*d9452f23SEdward Pilatowicz 	i = (pltaddr - fp->file_plt_base) / M_PLT_ENTSIZE - M_PLT_XNumber;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
637c478bd9Sstevel@tonic-gate 		Elf64_Rela r;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 		r_addr = fp->file_jmp_rel + i * sizeof (r);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
687c478bd9Sstevel@tonic-gate 		    (i = ELF64_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
69d51e9074Sab196087 			Elf_Data *data = fp->file_dynsym.sym_data_pri;
707c478bd9Sstevel@tonic-gate 			Elf64_Sym *symp = &(((Elf64_Sym *)data->d_buf)[i]);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 			return (fp->file_dynsym.sym_strs + symp->st_name);
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 	} else {
757c478bd9Sstevel@tonic-gate 		Elf32_Rel r;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 		r_addr = fp->file_jmp_rel + i * sizeof (r);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
807c478bd9Sstevel@tonic-gate 		    (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
81d51e9074Sab196087 			Elf_Data *data = fp->file_dynsym.sym_data_pri;
827c478bd9Sstevel@tonic-gate 			Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 			return (fp->file_dynsym.sym_strs + symp->st_name);
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	return (NULL);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate int
927c478bd9Sstevel@tonic-gate Pissyscall(struct ps_prochandle *P, uintptr_t addr)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	uchar_t instr[16];
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
977c478bd9Sstevel@tonic-gate 		if (Pread(P, instr, sizeof (syscall_instr), addr) !=
987c478bd9Sstevel@tonic-gate 		    sizeof (syscall_instr) ||
997c478bd9Sstevel@tonic-gate 		    memcmp(instr, syscall_instr, sizeof (syscall_instr)) != 0)
1007c478bd9Sstevel@tonic-gate 			return (0);
1017c478bd9Sstevel@tonic-gate 		else
1027c478bd9Sstevel@tonic-gate 			return (1);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (Pread(P, instr, sizeof (int_syscall_instr), addr) !=
1067c478bd9Sstevel@tonic-gate 	    sizeof (int_syscall_instr))
1077c478bd9Sstevel@tonic-gate 		return (0);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (memcmp(instr, int_syscall_instr, sizeof (int_syscall_instr)) == 0)
1107c478bd9Sstevel@tonic-gate 		return (1);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	return (0);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate int
1167c478bd9Sstevel@tonic-gate Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	int ret;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
1217c478bd9Sstevel@tonic-gate 		if (Pissyscall(P, addr - sizeof (syscall_instr))) {
1227c478bd9Sstevel@tonic-gate 			if (dst)
1237c478bd9Sstevel@tonic-gate 				*dst = addr - sizeof (syscall_instr);
1247c478bd9Sstevel@tonic-gate 			return (1);
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 		return (0);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if ((ret = Pissyscall(P, addr - sizeof (int_syscall_instr))) != 0) {
1307c478bd9Sstevel@tonic-gate 		if (dst)
1317c478bd9Sstevel@tonic-gate 			*dst = addr - sizeof (int_syscall_instr);
1327c478bd9Sstevel@tonic-gate 		return (ret);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	return (0);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate int
1397c478bd9Sstevel@tonic-gate Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
1427c478bd9Sstevel@tonic-gate 		if (buflen >= sizeof (syscall_instr) &&
1437c478bd9Sstevel@tonic-gate 		    memcmp(buf, syscall_instr, sizeof (syscall_instr)) == 0)
1447c478bd9Sstevel@tonic-gate 			return (1);
1457c478bd9Sstevel@tonic-gate 		else
1467c478bd9Sstevel@tonic-gate 			return (0);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (buflen < sizeof (int_syscall_instr))
1507c478bd9Sstevel@tonic-gate 		return (0);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (memcmp(buf, int_syscall_instr, sizeof (int_syscall_instr)) == 0)
1537c478bd9Sstevel@tonic-gate 		return (1);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	return (0);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate #define	TR_ARG_MAX 6	/* Max args to print, same as SPARC */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Given a return address, determine the likely number of arguments
1627c478bd9Sstevel@tonic-gate  * that were pushed on the stack prior to its execution.  We do this by
1637c478bd9Sstevel@tonic-gate  * expecting that a typical call sequence consists of pushing arguments on
1647c478bd9Sstevel@tonic-gate  * the stack, executing a call instruction, and then performing an add
1657c478bd9Sstevel@tonic-gate  * on %esp to restore it to the value prior to pushing the arguments for
1667c478bd9Sstevel@tonic-gate  * the call.  We attempt to detect such an add, and divide the addend
1677c478bd9Sstevel@tonic-gate  * by the size of a word to determine the number of pushed arguments.
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  * If we do not find such an add, this does not necessarily imply that the
1707c478bd9Sstevel@tonic-gate  * function took no arguments. It is not possible to reliably detect such a
1717c478bd9Sstevel@tonic-gate  * void function because hand-coded assembler does not always perform an add
1727c478bd9Sstevel@tonic-gate  * to %esp immediately after the "call" instruction (eg. _sys_call()).
1737c478bd9Sstevel@tonic-gate  * Because of this, we default to returning MIN(sz, TR_ARG_MAX) instead of 0
1747c478bd9Sstevel@tonic-gate  * in the absence of an add to %esp.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate static ulong_t
1777c478bd9Sstevel@tonic-gate argcount(struct ps_prochandle *P, uint32_t pc, ssize_t sz)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	uchar_t instr[6];
1807c478bd9Sstevel@tonic-gate 	ulong_t count, max;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	max = MIN(sz / sizeof (uint32_t), TR_ARG_MAX);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * Read the instruction at the return location.
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 	if (Pread(P, instr, sizeof (instr), (uintptr_t)pc) != sizeof (instr))
1887c478bd9Sstevel@tonic-gate 		return (max);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (instr[1] != 0xc4)
1917c478bd9Sstevel@tonic-gate 		return (max);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	switch (instr[0]) {
1947c478bd9Sstevel@tonic-gate 	case 0x81:	/* count is a longword */
1957c478bd9Sstevel@tonic-gate 		count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24);
1967c478bd9Sstevel@tonic-gate 		break;
1977c478bd9Sstevel@tonic-gate 	case 0x83:	/* count is a byte */
1987c478bd9Sstevel@tonic-gate 		count = instr[2];
1997c478bd9Sstevel@tonic-gate 		break;
2007c478bd9Sstevel@tonic-gate 	default:
2017c478bd9Sstevel@tonic-gate 		return (max);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	count /= sizeof (uint32_t);
2057c478bd9Sstevel@tonic-gate 	return (MIN(count, max));
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static void
2097c478bd9Sstevel@tonic-gate ucontext_32_to_prgregs(const ucontext32_t *uc, prgregset_t dst)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	const greg32_t *src = &uc->uc_mcontext.gregs[0];
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	dst[REG_DS] = (uint16_t)src[DS];
2147c478bd9Sstevel@tonic-gate 	dst[REG_ES] = (uint16_t)src[ES];
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	dst[REG_GS] = (uint16_t)src[GS];
2177c478bd9Sstevel@tonic-gate 	dst[REG_FS] = (uint16_t)src[FS];
2187c478bd9Sstevel@tonic-gate 	dst[REG_SS] = (uint16_t)src[SS];
2197c478bd9Sstevel@tonic-gate 	dst[REG_RSP] = (uint32_t)src[UESP];
2207c478bd9Sstevel@tonic-gate 	dst[REG_RFL] = src[EFL];
2217c478bd9Sstevel@tonic-gate 	dst[REG_CS] = (uint16_t)src[CS];
2227c478bd9Sstevel@tonic-gate 	dst[REG_RIP] = (uint32_t)src[EIP];
2237c478bd9Sstevel@tonic-gate 	dst[REG_ERR] = (uint32_t)src[ERR];
2247c478bd9Sstevel@tonic-gate 	dst[REG_TRAPNO] = (uint32_t)src[TRAPNO];
2257c478bd9Sstevel@tonic-gate 	dst[REG_RAX] = (uint32_t)src[EAX];
2267c478bd9Sstevel@tonic-gate 	dst[REG_RCX] = (uint32_t)src[ECX];
2277c478bd9Sstevel@tonic-gate 	dst[REG_RDX] = (uint32_t)src[EDX];
2287c478bd9Sstevel@tonic-gate 	dst[REG_RBX] = (uint32_t)src[EBX];
2297c478bd9Sstevel@tonic-gate 	dst[REG_RBP] = (uint32_t)src[EBP];
2307c478bd9Sstevel@tonic-gate 	dst[REG_RSI] = (uint32_t)src[ESI];
2317c478bd9Sstevel@tonic-gate 	dst[REG_RDI] = (uint32_t)src[EDI];
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static int
2357c478bd9Sstevel@tonic-gate Pstack_iter32(struct ps_prochandle *P, const prgregset_t regs,
2367c478bd9Sstevel@tonic-gate     proc_stack_f *func, void *arg)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	prgreg_t *prevfp = NULL;
2397c478bd9Sstevel@tonic-gate 	uint_t pfpsize = 0;
2407c478bd9Sstevel@tonic-gate 	int nfp = 0;
2417c478bd9Sstevel@tonic-gate 	struct {
2427c478bd9Sstevel@tonic-gate 		prgreg32_t fp;
2437c478bd9Sstevel@tonic-gate 		prgreg32_t pc;
2447c478bd9Sstevel@tonic-gate 		prgreg32_t args[32];
2457c478bd9Sstevel@tonic-gate 	} frame;
2467c478bd9Sstevel@tonic-gate 	uint_t argc;
2477c478bd9Sstevel@tonic-gate 	ssize_t sz;
2487c478bd9Sstevel@tonic-gate 	prgregset_t gregs;
2497c478bd9Sstevel@tonic-gate 	uint32_t fp, pfp, pc;
2507c478bd9Sstevel@tonic-gate 	long args[32];
2517c478bd9Sstevel@tonic-gate 	int rv;
2527c478bd9Sstevel@tonic-gate 	int i;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * Type definition for a structure corresponding to an IA32
2567c478bd9Sstevel@tonic-gate 	 * signal frame.  Refer to the comments in Pstack.c for more info
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	typedef struct {
2597c478bd9Sstevel@tonic-gate 		prgreg32_t fp;
2607c478bd9Sstevel@tonic-gate 		prgreg32_t pc;
2617c478bd9Sstevel@tonic-gate 		int signo;
2627c478bd9Sstevel@tonic-gate 		caddr32_t ucp;
2637c478bd9Sstevel@tonic-gate 		caddr32_t sip;
2647c478bd9Sstevel@tonic-gate 	} sf_t;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	uclist_t ucl;
2677c478bd9Sstevel@tonic-gate 	ucontext32_t uc;
2687c478bd9Sstevel@tonic-gate 	uintptr_t uc_addr;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	init_uclist(&ucl, P);
2717c478bd9Sstevel@tonic-gate 	(void) memcpy(gregs, regs, sizeof (gregs));
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	fp = regs[R_FP];
2747c478bd9Sstevel@tonic-gate 	pc = regs[R_PC];
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	while (fp != 0 || pc != 0) {
2777c478bd9Sstevel@tonic-gate 		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
2817c478bd9Sstevel@tonic-gate 		    (sz = Pread(P, &frame, sizeof (frame), (uintptr_t)fp)
2827c478bd9Sstevel@tonic-gate 		    >= (ssize_t)(2* sizeof (uint32_t)))) {
2837c478bd9Sstevel@tonic-gate 			/*
2847c478bd9Sstevel@tonic-gate 			 * One more trick for signal frames: the kernel sets
2857c478bd9Sstevel@tonic-gate 			 * the return pc of the signal frame to 0xffffffff on
2867c478bd9Sstevel@tonic-gate 			 * Intel IA32, so argcount won't work.
2877c478bd9Sstevel@tonic-gate 			 */
2887c478bd9Sstevel@tonic-gate 			if (frame.pc != -1L) {
2897c478bd9Sstevel@tonic-gate 				sz -= 2* sizeof (uint32_t);
2907c478bd9Sstevel@tonic-gate 				argc = argcount(P, (uint32_t)frame.pc, sz);
2917c478bd9Sstevel@tonic-gate 			} else
2927c478bd9Sstevel@tonic-gate 				argc = 3; /* sighandler(signo, sip, ucp) */
2937c478bd9Sstevel@tonic-gate 		} else {
2947c478bd9Sstevel@tonic-gate 			(void) memset(&frame, 0, sizeof (frame));
2957c478bd9Sstevel@tonic-gate 			argc = 0;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		gregs[R_FP] = fp;
2997c478bd9Sstevel@tonic-gate 		gregs[R_PC] = pc;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++)
3027c478bd9Sstevel@tonic-gate 			args[i] = (uint32_t)frame.args[i];
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		if ((rv = func(arg, gregs, argc, args)) != 0)
3057c478bd9Sstevel@tonic-gate 			break;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * In order to allow iteration over java frames (which can have
3097c478bd9Sstevel@tonic-gate 		 * their own frame pointers), we allow the iterator to change
3107c478bd9Sstevel@tonic-gate 		 * the contents of gregs.  If we detect a change, then we assume
3117c478bd9Sstevel@tonic-gate 		 * that the new values point to the next frame.
3127c478bd9Sstevel@tonic-gate 		 */
3137c478bd9Sstevel@tonic-gate 		if (gregs[R_FP] != fp || gregs[R_PC] != pc) {
3147c478bd9Sstevel@tonic-gate 			fp = gregs[R_FP];
3157c478bd9Sstevel@tonic-gate 			pc = gregs[R_PC];
3167c478bd9Sstevel@tonic-gate 			continue;
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		pfp = fp;
3207c478bd9Sstevel@tonic-gate 		fp = frame.fp;
3217c478bd9Sstevel@tonic-gate 		pc = frame.pc;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		if (find_uclink(&ucl, pfp + sizeof (sf_t)))
3247c478bd9Sstevel@tonic-gate 			uc_addr = pfp + sizeof (sf_t);
3257c478bd9Sstevel@tonic-gate 		else
3267c478bd9Sstevel@tonic-gate 			uc_addr = NULL;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		if (uc_addr != NULL &&
3297c478bd9Sstevel@tonic-gate 		    Pread(P, &uc, sizeof (uc), uc_addr) == sizeof (uc)) {
3307c478bd9Sstevel@tonic-gate 			ucontext_32_to_prgregs(&uc, gregs);
3317c478bd9Sstevel@tonic-gate 			fp = gregs[R_FP];
3327c478bd9Sstevel@tonic-gate 			pc = gregs[R_PC];
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (prevfp)
3377c478bd9Sstevel@tonic-gate 		free(prevfp);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	free_uclist(&ucl);
3407c478bd9Sstevel@tonic-gate 	return (rv);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate static void
3447c478bd9Sstevel@tonic-gate ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	(void) memcpy(dst, src->uc_mcontext.gregs, sizeof (gregset_t));
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate int
3517c478bd9Sstevel@tonic-gate Pstack_iter(struct ps_prochandle *P, const prgregset_t regs,
3527c478bd9Sstevel@tonic-gate 	proc_stack_f *func, void *arg)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	struct {
3557c478bd9Sstevel@tonic-gate 		uintptr_t fp;
3567c478bd9Sstevel@tonic-gate 		uintptr_t pc;
3577c478bd9Sstevel@tonic-gate 	} frame;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	uint_t pfpsize = 0;
3607c478bd9Sstevel@tonic-gate 	prgreg_t *prevfp = NULL;
3617c478bd9Sstevel@tonic-gate 	prgreg_t fp, pfp;
3627c478bd9Sstevel@tonic-gate 	prgreg_t pc;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	prgregset_t gregs;
3657c478bd9Sstevel@tonic-gate 	int nfp = 0;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	uclist_t ucl;
3687c478bd9Sstevel@tonic-gate 	int rv = 0;
3697c478bd9Sstevel@tonic-gate 	int argc;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	uintptr_t uc_addr;
3727c478bd9Sstevel@tonic-gate 	ucontext_t uc;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * Type definition for a structure corresponding to an IA32
3767c478bd9Sstevel@tonic-gate 	 * signal frame.  Refer to the comments in Pstack.c for more info
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 	typedef struct {
3797c478bd9Sstevel@tonic-gate 		prgreg_t fp;
3807c478bd9Sstevel@tonic-gate 		prgreg_t pc;
3817c478bd9Sstevel@tonic-gate 		prgreg_t signo;
3827c478bd9Sstevel@tonic-gate 		siginfo_t *sip;
3837c478bd9Sstevel@tonic-gate 	} sigframe_t;
3847c478bd9Sstevel@tonic-gate 	prgreg_t args[32];
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel != PR_MODEL_LP64)
3877c478bd9Sstevel@tonic-gate 		return (Pstack_iter32(P, regs, func, arg));
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	init_uclist(&ucl, P);
3907c478bd9Sstevel@tonic-gate 	(void) memcpy(gregs, regs, sizeof (gregs));
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	fp = gregs[R_FP];
3937c478bd9Sstevel@tonic-gate 	pc = gregs[R_PC];
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	while (fp != 0 || pc != 0) {
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
3987c478bd9Sstevel@tonic-gate 			break;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
4017c478bd9Sstevel@tonic-gate 		    Pread(P, &frame, sizeof (frame), (uintptr_t)fp) ==
4027c478bd9Sstevel@tonic-gate 		    sizeof (frame)) {
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 			if (frame.pc != -1) {
4057c478bd9Sstevel@tonic-gate 				/*
4067c478bd9Sstevel@tonic-gate 				 * Function arguments are not available on
4077c478bd9Sstevel@tonic-gate 				 * amd64 without extensive DWARF processing.
4087c478bd9Sstevel@tonic-gate 				 */
4097c478bd9Sstevel@tonic-gate 				argc = 0;
4107c478bd9Sstevel@tonic-gate 			} else {
4117c478bd9Sstevel@tonic-gate 				argc = 3;
4127c478bd9Sstevel@tonic-gate 				args[2] = fp + sizeof (sigframe_t);
4137c478bd9Sstevel@tonic-gate 				if (Pread(P, &args, 2 * sizeof (prgreg_t),
4147c478bd9Sstevel@tonic-gate 				    fp + 2 * sizeof (prgreg_t)) !=
4157c478bd9Sstevel@tonic-gate 				    2 * sizeof (prgreg_t))
4167c478bd9Sstevel@tonic-gate 					argc = 0;
4177c478bd9Sstevel@tonic-gate 			}
4187c478bd9Sstevel@tonic-gate 		} else {
4197c478bd9Sstevel@tonic-gate 			(void) memset(&frame, 0, sizeof (frame));
4207c478bd9Sstevel@tonic-gate 			argc = 0;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		gregs[R_FP] = fp;
4247c478bd9Sstevel@tonic-gate 		gregs[R_PC] = pc;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		if ((rv = func(arg, gregs, argc, args)) != 0)
4277c478bd9Sstevel@tonic-gate 			break;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		pfp = fp;
4307c478bd9Sstevel@tonic-gate 		fp = frame.fp;
4317c478bd9Sstevel@tonic-gate 		pc = frame.pc;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		if (pc == -1 && find_uclink(&ucl, pfp + sizeof (sigframe_t))) {
4347c478bd9Sstevel@tonic-gate 			uc_addr = pfp + sizeof (sigframe_t);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 			if (Pread(P, &uc, sizeof (uc), uc_addr)
4377c478bd9Sstevel@tonic-gate 			    == sizeof (uc)) {
4387c478bd9Sstevel@tonic-gate 				ucontext_n_to_prgregs(&uc, gregs);
4397c478bd9Sstevel@tonic-gate 				fp = gregs[R_FP];
4407c478bd9Sstevel@tonic-gate 				pc = gregs[R_PC];
4417c478bd9Sstevel@tonic-gate 			}
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (prevfp)
4467c478bd9Sstevel@tonic-gate 		free(prevfp);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	free_uclist(&ucl);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	return (rv);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate uintptr_t
4547c478bd9Sstevel@tonic-gate Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
4577c478bd9Sstevel@tonic-gate 		sp -= sizeof (int) * (nargs+2);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RAX] = sysindex;
4607c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RSP] = sp;
4617c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RIP] = P->sysaddr;
4627c478bd9Sstevel@tonic-gate 	} else {
4637c478bd9Sstevel@tonic-gate 		int pusharg = (nargs > 6) ? nargs - 6: 0;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		sp -= sizeof (int64_t) * (pusharg+2);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RAX] = sysindex;
4687c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RSP] = sp;
4697c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RIP] = P->sysaddr;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	return (sp);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate int
4767c478bd9Sstevel@tonic-gate Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
4777c478bd9Sstevel@tonic-gate     uintptr_t ap)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
4807c478bd9Sstevel@tonic-gate 		int32_t arglist[MAXARGS+2];
4817c478bd9Sstevel@tonic-gate 		int i;
4827c478bd9Sstevel@tonic-gate 		argdes_t *adp;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++)
4857c478bd9Sstevel@tonic-gate 			arglist[1 + i] = (int32_t)adp->arg_value;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		arglist[0] = P->status.pr_lwp.pr_reg[REG_RIP];
4887c478bd9Sstevel@tonic-gate 		if (Pwrite(P, &arglist[0], sizeof (int) * (nargs+1),
4897c478bd9Sstevel@tonic-gate 		    (uintptr_t)ap) != sizeof (int) * (nargs+1))
4907c478bd9Sstevel@tonic-gate 			return (-1);
4917c478bd9Sstevel@tonic-gate 	} else {
4927c478bd9Sstevel@tonic-gate 		int64_t arglist[MAXARGS+2];
4937c478bd9Sstevel@tonic-gate 		int i;
4947c478bd9Sstevel@tonic-gate 		argdes_t *adp;
4957c478bd9Sstevel@tonic-gate 		int pusharg = (nargs > 6) ? nargs - 6: 0;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++) {
4987c478bd9Sstevel@tonic-gate 			switch (i) {
4997c478bd9Sstevel@tonic-gate 			case 0:
5007c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RDI, adp->arg_value);
5017c478bd9Sstevel@tonic-gate 				break;
5027c478bd9Sstevel@tonic-gate 			case 1:
5037c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RSI, adp->arg_value);
5047c478bd9Sstevel@tonic-gate 				break;
5057c478bd9Sstevel@tonic-gate 			case 2:
5067c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RDX, adp->arg_value);
5077c478bd9Sstevel@tonic-gate 				break;
5087c478bd9Sstevel@tonic-gate 			case 3:
5097c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RCX, adp->arg_value);
5107c478bd9Sstevel@tonic-gate 				break;
5117c478bd9Sstevel@tonic-gate 			case 4:
5127c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_R8, adp->arg_value);
5137c478bd9Sstevel@tonic-gate 				break;
5147c478bd9Sstevel@tonic-gate 			case 5:
5157c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_R9, adp->arg_value);
5167c478bd9Sstevel@tonic-gate 				break;
5177c478bd9Sstevel@tonic-gate 			default:
5187c478bd9Sstevel@tonic-gate 				arglist[i - 5] = (uint64_t)adp->arg_value;
5197c478bd9Sstevel@tonic-gate 				break;
5207c478bd9Sstevel@tonic-gate 			}
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		arglist[0] = P->status.pr_lwp.pr_reg[REG_RIP];
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		if (Pwrite(P, &arglist[0],
5267c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) * (pusharg + 1), ap) !=
5277c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) * (pusharg + 1))
5287c478bd9Sstevel@tonic-gate 			return (-1);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	return (0);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate int
5357c478bd9Sstevel@tonic-gate Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
5367c478bd9Sstevel@tonic-gate     uintptr_t ap)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
5397c478bd9Sstevel@tonic-gate 		uint32_t arglist[MAXARGS + 2];
5407c478bd9Sstevel@tonic-gate 		int i;
5417c478bd9Sstevel@tonic-gate 		argdes_t *adp;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 		if (Pread(P, &arglist[0], sizeof (int) * (nargs+1),
5447c478bd9Sstevel@tonic-gate 		    (uintptr_t)ap) != sizeof (int) * (nargs+1))
5457c478bd9Sstevel@tonic-gate 			return (-1);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++)
5487c478bd9Sstevel@tonic-gate 			adp->arg_value = arglist[i];
5497c478bd9Sstevel@tonic-gate 	} else {
5507c478bd9Sstevel@tonic-gate 		int pusharg = (nargs > 6) ? nargs - 6: 0;
5517c478bd9Sstevel@tonic-gate 		int64_t arglist[MAXARGS+2];
5527c478bd9Sstevel@tonic-gate 		int i;
5537c478bd9Sstevel@tonic-gate 		argdes_t *adp;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		if (pusharg  > 0 &&
5567c478bd9Sstevel@tonic-gate 		    Pread(P, &arglist[0], sizeof (int64_t) * (pusharg + 1),
5577c478bd9Sstevel@tonic-gate 		    ap) != sizeof (int64_t) * (pusharg + 1))
5587c478bd9Sstevel@tonic-gate 			return (-1);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++) {
5617c478bd9Sstevel@tonic-gate 			switch (i) {
5627c478bd9Sstevel@tonic-gate 			case 0:
5637c478bd9Sstevel@tonic-gate 				adp->arg_value =
5647c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RDI];
5657c478bd9Sstevel@tonic-gate 				break;
5667c478bd9Sstevel@tonic-gate 			case 1:
5677c478bd9Sstevel@tonic-gate 				adp->arg_value =
5687c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RSI];
5697c478bd9Sstevel@tonic-gate 				break;
5707c478bd9Sstevel@tonic-gate 			case 2:
5717c478bd9Sstevel@tonic-gate 				adp->arg_value =
5727c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RDX];
5737c478bd9Sstevel@tonic-gate 				break;
5747c478bd9Sstevel@tonic-gate 			case 3:
5757c478bd9Sstevel@tonic-gate 				adp->arg_value =
5767c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RCX];
5777c478bd9Sstevel@tonic-gate 				break;
5787c478bd9Sstevel@tonic-gate 			case 4:
5797c478bd9Sstevel@tonic-gate 				adp->arg_value =
5807c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_R8];
5817c478bd9Sstevel@tonic-gate 				break;
5827c478bd9Sstevel@tonic-gate 			case 5:
5837c478bd9Sstevel@tonic-gate 				adp->arg_value =
5847c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_R9];
5857c478bd9Sstevel@tonic-gate 				break;
5867c478bd9Sstevel@tonic-gate 			default:
5877c478bd9Sstevel@tonic-gate 				adp->arg_value = arglist[i - 6];
5887c478bd9Sstevel@tonic-gate 				break;
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		return (0);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	return (0);
5967c478bd9Sstevel@tonic-gate }
597