xref: /titanic_51/usr/src/lib/libproc/amd64/Pisadep.c (revision 2d4be7aab5a43b4b2bf4e1b527dcddb9bff39ee6)
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 /*
22d9452f23SEdward 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>
31d9452f23SEdward 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 
39*2d4be7aaSRichard Lowe #include <saveargs.h>
407c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
417c478bd9Sstevel@tonic-gate #include "Pstack.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static uchar_t int_syscall_instr[] = { 0xCD, T_SYSCALLINT };
447c478bd9Sstevel@tonic-gate static uchar_t syscall_instr[] = { 0x0f, 0x05 };
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate const char *
477c478bd9Sstevel@tonic-gate Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	map_info_t *mp = Paddr2mptr(P, pltaddr);
507c478bd9Sstevel@tonic-gate 	file_info_t *fp;
517c478bd9Sstevel@tonic-gate 	size_t i;
527c478bd9Sstevel@tonic-gate 	uintptr_t r_addr;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if (mp == NULL || (fp = mp->map_file) == NULL ||
557c478bd9Sstevel@tonic-gate 	    fp->file_plt_base == 0 ||
567c478bd9Sstevel@tonic-gate 	    pltaddr - fp->file_plt_base >= fp->file_plt_size) {
577c478bd9Sstevel@tonic-gate 		errno = EINVAL;
587c478bd9Sstevel@tonic-gate 		return (NULL);
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 
61d9452f23SEdward Pilatowicz 	i = (pltaddr - fp->file_plt_base) / M_PLT_ENTSIZE - M_PLT_XNumber;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
647c478bd9Sstevel@tonic-gate 		Elf64_Rela r;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 		r_addr = fp->file_jmp_rel + i * sizeof (r);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
697c478bd9Sstevel@tonic-gate 		    (i = ELF64_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
70d51e9074Sab196087 			Elf_Data *data = fp->file_dynsym.sym_data_pri;
717c478bd9Sstevel@tonic-gate 			Elf64_Sym *symp = &(((Elf64_Sym *)data->d_buf)[i]);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 			return (fp->file_dynsym.sym_strs + symp->st_name);
747c478bd9Sstevel@tonic-gate 		}
757c478bd9Sstevel@tonic-gate 	} else {
767c478bd9Sstevel@tonic-gate 		Elf32_Rel r;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 		r_addr = fp->file_jmp_rel + i * sizeof (r);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
817c478bd9Sstevel@tonic-gate 		    (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
82d51e9074Sab196087 			Elf_Data *data = fp->file_dynsym.sym_data_pri;
837c478bd9Sstevel@tonic-gate 			Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 			return (fp->file_dynsym.sym_strs + symp->st_name);
867c478bd9Sstevel@tonic-gate 		}
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	return (NULL);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate int
937c478bd9Sstevel@tonic-gate Pissyscall(struct ps_prochandle *P, uintptr_t addr)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	uchar_t instr[16];
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
987c478bd9Sstevel@tonic-gate 		if (Pread(P, instr, sizeof (syscall_instr), addr) !=
997c478bd9Sstevel@tonic-gate 		    sizeof (syscall_instr) ||
1007c478bd9Sstevel@tonic-gate 		    memcmp(instr, syscall_instr, sizeof (syscall_instr)) != 0)
1017c478bd9Sstevel@tonic-gate 			return (0);
1027c478bd9Sstevel@tonic-gate 		else
1037c478bd9Sstevel@tonic-gate 			return (1);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (Pread(P, instr, sizeof (int_syscall_instr), addr) !=
1077c478bd9Sstevel@tonic-gate 	    sizeof (int_syscall_instr))
1087c478bd9Sstevel@tonic-gate 		return (0);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (memcmp(instr, int_syscall_instr, sizeof (int_syscall_instr)) == 0)
1117c478bd9Sstevel@tonic-gate 		return (1);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	return (0);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate int
1177c478bd9Sstevel@tonic-gate Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	int ret;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
1227c478bd9Sstevel@tonic-gate 		if (Pissyscall(P, addr - sizeof (syscall_instr))) {
1237c478bd9Sstevel@tonic-gate 			if (dst)
1247c478bd9Sstevel@tonic-gate 				*dst = addr - sizeof (syscall_instr);
1257c478bd9Sstevel@tonic-gate 			return (1);
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 		return (0);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if ((ret = Pissyscall(P, addr - sizeof (int_syscall_instr))) != 0) {
1317c478bd9Sstevel@tonic-gate 		if (dst)
1327c478bd9Sstevel@tonic-gate 			*dst = addr - sizeof (int_syscall_instr);
1337c478bd9Sstevel@tonic-gate 		return (ret);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return (0);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate int
1407c478bd9Sstevel@tonic-gate Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
1437c478bd9Sstevel@tonic-gate 		if (buflen >= sizeof (syscall_instr) &&
1447c478bd9Sstevel@tonic-gate 		    memcmp(buf, syscall_instr, sizeof (syscall_instr)) == 0)
1457c478bd9Sstevel@tonic-gate 			return (1);
1467c478bd9Sstevel@tonic-gate 		else
1477c478bd9Sstevel@tonic-gate 			return (0);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (buflen < sizeof (int_syscall_instr))
1517c478bd9Sstevel@tonic-gate 		return (0);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (memcmp(buf, int_syscall_instr, sizeof (int_syscall_instr)) == 0)
1547c478bd9Sstevel@tonic-gate 		return (1);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	return (0);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #define	TR_ARG_MAX 6	/* Max args to print, same as SPARC */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * Given a return address, determine the likely number of arguments
1637c478bd9Sstevel@tonic-gate  * that were pushed on the stack prior to its execution.  We do this by
1647c478bd9Sstevel@tonic-gate  * expecting that a typical call sequence consists of pushing arguments on
1657c478bd9Sstevel@tonic-gate  * the stack, executing a call instruction, and then performing an add
1667c478bd9Sstevel@tonic-gate  * on %esp to restore it to the value prior to pushing the arguments for
1677c478bd9Sstevel@tonic-gate  * the call.  We attempt to detect such an add, and divide the addend
1687c478bd9Sstevel@tonic-gate  * by the size of a word to determine the number of pushed arguments.
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  * If we do not find such an add, this does not necessarily imply that the
1717c478bd9Sstevel@tonic-gate  * function took no arguments. It is not possible to reliably detect such a
1727c478bd9Sstevel@tonic-gate  * void function because hand-coded assembler does not always perform an add
1737c478bd9Sstevel@tonic-gate  * to %esp immediately after the "call" instruction (eg. _sys_call()).
1747c478bd9Sstevel@tonic-gate  * Because of this, we default to returning MIN(sz, TR_ARG_MAX) instead of 0
1757c478bd9Sstevel@tonic-gate  * in the absence of an add to %esp.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate static ulong_t
1787c478bd9Sstevel@tonic-gate argcount(struct ps_prochandle *P, uint32_t pc, ssize_t sz)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	uchar_t instr[6];
1817c478bd9Sstevel@tonic-gate 	ulong_t count, max;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	max = MIN(sz / sizeof (uint32_t), TR_ARG_MAX);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/*
1867c478bd9Sstevel@tonic-gate 	 * Read the instruction at the return location.
1877c478bd9Sstevel@tonic-gate 	 */
1887c478bd9Sstevel@tonic-gate 	if (Pread(P, instr, sizeof (instr), (uintptr_t)pc) != sizeof (instr))
1897c478bd9Sstevel@tonic-gate 		return (max);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (instr[1] != 0xc4)
1927c478bd9Sstevel@tonic-gate 		return (max);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	switch (instr[0]) {
1957c478bd9Sstevel@tonic-gate 	case 0x81:	/* count is a longword */
1967c478bd9Sstevel@tonic-gate 		count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24);
1977c478bd9Sstevel@tonic-gate 		break;
1987c478bd9Sstevel@tonic-gate 	case 0x83:	/* count is a byte */
1997c478bd9Sstevel@tonic-gate 		count = instr[2];
2007c478bd9Sstevel@tonic-gate 		break;
2017c478bd9Sstevel@tonic-gate 	default:
2027c478bd9Sstevel@tonic-gate 		return (max);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	count /= sizeof (uint32_t);
2067c478bd9Sstevel@tonic-gate 	return (MIN(count, max));
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate static void
2107c478bd9Sstevel@tonic-gate ucontext_32_to_prgregs(const ucontext32_t *uc, prgregset_t dst)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	const greg32_t *src = &uc->uc_mcontext.gregs[0];
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	dst[REG_DS] = (uint16_t)src[DS];
2157c478bd9Sstevel@tonic-gate 	dst[REG_ES] = (uint16_t)src[ES];
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	dst[REG_GS] = (uint16_t)src[GS];
2187c478bd9Sstevel@tonic-gate 	dst[REG_FS] = (uint16_t)src[FS];
2197c478bd9Sstevel@tonic-gate 	dst[REG_SS] = (uint16_t)src[SS];
2207c478bd9Sstevel@tonic-gate 	dst[REG_RSP] = (uint32_t)src[UESP];
2217c478bd9Sstevel@tonic-gate 	dst[REG_RFL] = src[EFL];
2227c478bd9Sstevel@tonic-gate 	dst[REG_CS] = (uint16_t)src[CS];
2237c478bd9Sstevel@tonic-gate 	dst[REG_RIP] = (uint32_t)src[EIP];
2247c478bd9Sstevel@tonic-gate 	dst[REG_ERR] = (uint32_t)src[ERR];
2257c478bd9Sstevel@tonic-gate 	dst[REG_TRAPNO] = (uint32_t)src[TRAPNO];
2267c478bd9Sstevel@tonic-gate 	dst[REG_RAX] = (uint32_t)src[EAX];
2277c478bd9Sstevel@tonic-gate 	dst[REG_RCX] = (uint32_t)src[ECX];
2287c478bd9Sstevel@tonic-gate 	dst[REG_RDX] = (uint32_t)src[EDX];
2297c478bd9Sstevel@tonic-gate 	dst[REG_RBX] = (uint32_t)src[EBX];
2307c478bd9Sstevel@tonic-gate 	dst[REG_RBP] = (uint32_t)src[EBP];
2317c478bd9Sstevel@tonic-gate 	dst[REG_RSI] = (uint32_t)src[ESI];
2327c478bd9Sstevel@tonic-gate 	dst[REG_RDI] = (uint32_t)src[EDI];
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static int
2367c478bd9Sstevel@tonic-gate Pstack_iter32(struct ps_prochandle *P, const prgregset_t regs,
2377c478bd9Sstevel@tonic-gate     proc_stack_f *func, void *arg)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	prgreg_t *prevfp = NULL;
2407c478bd9Sstevel@tonic-gate 	uint_t pfpsize = 0;
2417c478bd9Sstevel@tonic-gate 	int nfp = 0;
2427c478bd9Sstevel@tonic-gate 	struct {
2437c478bd9Sstevel@tonic-gate 		prgreg32_t fp;
2447c478bd9Sstevel@tonic-gate 		prgreg32_t pc;
2457c478bd9Sstevel@tonic-gate 		prgreg32_t args[32];
2467c478bd9Sstevel@tonic-gate 	} frame;
2477c478bd9Sstevel@tonic-gate 	uint_t argc;
2487c478bd9Sstevel@tonic-gate 	ssize_t sz;
2497c478bd9Sstevel@tonic-gate 	prgregset_t gregs;
2507c478bd9Sstevel@tonic-gate 	uint32_t fp, pfp, pc;
2517c478bd9Sstevel@tonic-gate 	long args[32];
2527c478bd9Sstevel@tonic-gate 	int rv;
2537c478bd9Sstevel@tonic-gate 	int i;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Type definition for a structure corresponding to an IA32
2577c478bd9Sstevel@tonic-gate 	 * signal frame.  Refer to the comments in Pstack.c for more info
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	typedef struct {
2607c478bd9Sstevel@tonic-gate 		prgreg32_t fp;
2617c478bd9Sstevel@tonic-gate 		prgreg32_t pc;
2627c478bd9Sstevel@tonic-gate 		int signo;
2637c478bd9Sstevel@tonic-gate 		caddr32_t ucp;
2647c478bd9Sstevel@tonic-gate 		caddr32_t sip;
2657c478bd9Sstevel@tonic-gate 	} sf_t;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	uclist_t ucl;
2687c478bd9Sstevel@tonic-gate 	ucontext32_t uc;
2697c478bd9Sstevel@tonic-gate 	uintptr_t uc_addr;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	init_uclist(&ucl, P);
2727c478bd9Sstevel@tonic-gate 	(void) memcpy(gregs, regs, sizeof (gregs));
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	fp = regs[R_FP];
2757c478bd9Sstevel@tonic-gate 	pc = regs[R_PC];
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	while (fp != 0 || pc != 0) {
2787c478bd9Sstevel@tonic-gate 		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
2797c478bd9Sstevel@tonic-gate 			break;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
2827c478bd9Sstevel@tonic-gate 		    (sz = Pread(P, &frame, sizeof (frame), (uintptr_t)fp)
2837c478bd9Sstevel@tonic-gate 		    >= (ssize_t)(2* sizeof (uint32_t)))) {
2847c478bd9Sstevel@tonic-gate 			/*
2857c478bd9Sstevel@tonic-gate 			 * One more trick for signal frames: the kernel sets
2867c478bd9Sstevel@tonic-gate 			 * the return pc of the signal frame to 0xffffffff on
2877c478bd9Sstevel@tonic-gate 			 * Intel IA32, so argcount won't work.
2887c478bd9Sstevel@tonic-gate 			 */
2897c478bd9Sstevel@tonic-gate 			if (frame.pc != -1L) {
2907c478bd9Sstevel@tonic-gate 				sz -= 2* sizeof (uint32_t);
2917c478bd9Sstevel@tonic-gate 				argc = argcount(P, (uint32_t)frame.pc, sz);
2927c478bd9Sstevel@tonic-gate 			} else
2937c478bd9Sstevel@tonic-gate 				argc = 3; /* sighandler(signo, sip, ucp) */
2947c478bd9Sstevel@tonic-gate 		} else {
2957c478bd9Sstevel@tonic-gate 			(void) memset(&frame, 0, sizeof (frame));
2967c478bd9Sstevel@tonic-gate 			argc = 0;
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		gregs[R_FP] = fp;
3007c478bd9Sstevel@tonic-gate 		gregs[R_PC] = pc;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++)
3037c478bd9Sstevel@tonic-gate 			args[i] = (uint32_t)frame.args[i];
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		if ((rv = func(arg, gregs, argc, args)) != 0)
3067c478bd9Sstevel@tonic-gate 			break;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		/*
3097c478bd9Sstevel@tonic-gate 		 * In order to allow iteration over java frames (which can have
3107c478bd9Sstevel@tonic-gate 		 * their own frame pointers), we allow the iterator to change
3117c478bd9Sstevel@tonic-gate 		 * the contents of gregs.  If we detect a change, then we assume
3127c478bd9Sstevel@tonic-gate 		 * that the new values point to the next frame.
3137c478bd9Sstevel@tonic-gate 		 */
3147c478bd9Sstevel@tonic-gate 		if (gregs[R_FP] != fp || gregs[R_PC] != pc) {
3157c478bd9Sstevel@tonic-gate 			fp = gregs[R_FP];
3167c478bd9Sstevel@tonic-gate 			pc = gregs[R_PC];
3177c478bd9Sstevel@tonic-gate 			continue;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		pfp = fp;
3217c478bd9Sstevel@tonic-gate 		fp = frame.fp;
3227c478bd9Sstevel@tonic-gate 		pc = frame.pc;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		if (find_uclink(&ucl, pfp + sizeof (sf_t)))
3257c478bd9Sstevel@tonic-gate 			uc_addr = pfp + sizeof (sf_t);
3267c478bd9Sstevel@tonic-gate 		else
3277c478bd9Sstevel@tonic-gate 			uc_addr = NULL;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		if (uc_addr != NULL &&
3307c478bd9Sstevel@tonic-gate 		    Pread(P, &uc, sizeof (uc), uc_addr) == sizeof (uc)) {
3317c478bd9Sstevel@tonic-gate 			ucontext_32_to_prgregs(&uc, gregs);
3327c478bd9Sstevel@tonic-gate 			fp = gregs[R_FP];
3337c478bd9Sstevel@tonic-gate 			pc = gregs[R_PC];
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (prevfp)
3387c478bd9Sstevel@tonic-gate 		free(prevfp);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	free_uclist(&ucl);
3417c478bd9Sstevel@tonic-gate 	return (rv);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate static void
3457c478bd9Sstevel@tonic-gate ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	(void) memcpy(dst, src->uc_mcontext.gregs, sizeof (gregset_t));
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
350*2d4be7aaSRichard Lowe /*
351*2d4be7aaSRichard Lowe  * Read arguments from the frame indicated by regs into args, return the
352*2d4be7aaSRichard Lowe  * number of arguments successfully read
353*2d4be7aaSRichard Lowe  */
354*2d4be7aaSRichard Lowe static int
355*2d4be7aaSRichard Lowe read_args(struct ps_prochandle *P, uintptr_t fp, uintptr_t pc, prgreg_t *args,
356*2d4be7aaSRichard Lowe     size_t argsize)
357*2d4be7aaSRichard Lowe {
358*2d4be7aaSRichard Lowe 	GElf_Sym sym;
359*2d4be7aaSRichard Lowe 	ctf_file_t *ctfp = NULL;
360*2d4be7aaSRichard Lowe 	ctf_funcinfo_t finfo;
361*2d4be7aaSRichard Lowe 	prsyminfo_t si = {0};
362*2d4be7aaSRichard Lowe 	uint8_t ins[SAVEARGS_INSN_SEQ_LEN];
363*2d4be7aaSRichard Lowe 	size_t insnsize;
364*2d4be7aaSRichard Lowe 	int argc = 0;
365*2d4be7aaSRichard Lowe 	int rettype = 0;
366*2d4be7aaSRichard Lowe 	int start_index = 0;
367*2d4be7aaSRichard Lowe 	int args_style = 0;
368*2d4be7aaSRichard Lowe 	int i;
369*2d4be7aaSRichard Lowe 	ctf_id_t args_types[5];
370*2d4be7aaSRichard Lowe 
371*2d4be7aaSRichard Lowe 	if (Pxlookup_by_addr(P, pc, NULL, 0, &sym, &si) != 0)
372*2d4be7aaSRichard Lowe 		return (0);
373*2d4be7aaSRichard Lowe 
374*2d4be7aaSRichard Lowe 	if ((ctfp = Paddr_to_ctf(P, pc)) == NULL)
375*2d4be7aaSRichard Lowe 		return (0);
376*2d4be7aaSRichard Lowe 
377*2d4be7aaSRichard Lowe 	if (ctf_func_info(ctfp, si.prs_id, &finfo) == CTF_ERR)
378*2d4be7aaSRichard Lowe 		return (0);
379*2d4be7aaSRichard Lowe 
380*2d4be7aaSRichard Lowe 	argc = finfo.ctc_argc;
381*2d4be7aaSRichard Lowe 
382*2d4be7aaSRichard Lowe 	if (argc == 0)
383*2d4be7aaSRichard Lowe 		return (0);
384*2d4be7aaSRichard Lowe 
385*2d4be7aaSRichard Lowe 	rettype = ctf_type_kind(ctfp, finfo.ctc_return);
386*2d4be7aaSRichard Lowe 
387*2d4be7aaSRichard Lowe 	/*
388*2d4be7aaSRichard Lowe 	 * If the function returns a structure or union greater than 16 bytes
389*2d4be7aaSRichard Lowe 	 * in size %rdi contains the address in which to store the return
390*2d4be7aaSRichard Lowe 	 * value rather than for an argument.
391*2d4be7aaSRichard Lowe 	 */
392*2d4be7aaSRichard Lowe 	if (((rettype == CTF_K_STRUCT) || (rettype == CTF_K_UNION)) &&
393*2d4be7aaSRichard Lowe 	    ctf_type_size(ctfp, finfo.ctc_return) > 16)
394*2d4be7aaSRichard Lowe 		start_index = 1;
395*2d4be7aaSRichard Lowe 	else
396*2d4be7aaSRichard Lowe 		start_index = 0;
397*2d4be7aaSRichard Lowe 
398*2d4be7aaSRichard Lowe 	/*
399*2d4be7aaSRichard Lowe 	 * If any of the first 5 arguments are a structure less than 16 bytes
400*2d4be7aaSRichard Lowe 	 * in size, it will be passed spread across two argument registers,
401*2d4be7aaSRichard Lowe 	 * and we will not cope.
402*2d4be7aaSRichard Lowe 	 */
403*2d4be7aaSRichard Lowe 	if (ctf_func_args(ctfp, si.prs_id, 5, args_types) == CTF_ERR)
404*2d4be7aaSRichard Lowe 		return (0);
405*2d4be7aaSRichard Lowe 
406*2d4be7aaSRichard Lowe 	for (i = 0; i < MIN(5, finfo.ctc_argc); i++) {
407*2d4be7aaSRichard Lowe 		int t = ctf_type_kind(ctfp, args_types[i]);
408*2d4be7aaSRichard Lowe 
409*2d4be7aaSRichard Lowe 		if (((t == CTF_K_STRUCT) || (t == CTF_K_UNION)) &&
410*2d4be7aaSRichard Lowe 		    ctf_type_size(ctfp, args_types[i]) <= 16)
411*2d4be7aaSRichard Lowe 			return (0);
412*2d4be7aaSRichard Lowe 	}
413*2d4be7aaSRichard Lowe 
414*2d4be7aaSRichard Lowe 	/*
415*2d4be7aaSRichard Lowe 	 * The number of instructions to search for argument saving is limited
416*2d4be7aaSRichard Lowe 	 * such that only instructions prior to %pc are considered and we
417*2d4be7aaSRichard Lowe 	 * never read arguments from a function where the saving code has not
418*2d4be7aaSRichard Lowe 	 * in fact yet executed.
419*2d4be7aaSRichard Lowe 	 */
420*2d4be7aaSRichard Lowe 	insnsize = MIN(MIN(sym.st_size, SAVEARGS_INSN_SEQ_LEN),
421*2d4be7aaSRichard Lowe 	    pc - sym.st_value);
422*2d4be7aaSRichard Lowe 
423*2d4be7aaSRichard Lowe 	if (Pread(P, ins, insnsize, sym.st_value) != insnsize)
424*2d4be7aaSRichard Lowe 		return (0);
425*2d4be7aaSRichard Lowe 
426*2d4be7aaSRichard Lowe 	if ((argc != 0) &&
427*2d4be7aaSRichard Lowe 	    ((args_style = saveargs_has_args(ins, insnsize, argc,
428*2d4be7aaSRichard Lowe 	    start_index)) != SAVEARGS_NO_ARGS)) {
429*2d4be7aaSRichard Lowe 		int regargs = MIN((6 - start_index), argc);
430*2d4be7aaSRichard Lowe 		size_t size = regargs * sizeof (long);
431*2d4be7aaSRichard Lowe 		int i;
432*2d4be7aaSRichard Lowe 
433*2d4be7aaSRichard Lowe 		/*
434*2d4be7aaSRichard Lowe 		 * If Studio pushed a structure return address as an argument,
435*2d4be7aaSRichard Lowe 		 * we need to read one more argument than actually exists (the
436*2d4be7aaSRichard Lowe 		 * addr) to make everything line up.
437*2d4be7aaSRichard Lowe 		 */
438*2d4be7aaSRichard Lowe 		if (args_style == SAVEARGS_STRUCT_ARGS)
439*2d4be7aaSRichard Lowe 			size += sizeof (long);
440*2d4be7aaSRichard Lowe 
441*2d4be7aaSRichard Lowe 		if (Pread(P, args, size, (fp - size)) != size)
442*2d4be7aaSRichard Lowe 			return (0);
443*2d4be7aaSRichard Lowe 
444*2d4be7aaSRichard Lowe 		for (i = 0; i < (regargs / 2); i++) {
445*2d4be7aaSRichard Lowe 			prgreg_t t = args[i];
446*2d4be7aaSRichard Lowe 
447*2d4be7aaSRichard Lowe 			args[i] = args[regargs - i - 1];
448*2d4be7aaSRichard Lowe 			args[regargs - i - 1] = t;
449*2d4be7aaSRichard Lowe 		}
450*2d4be7aaSRichard Lowe 
451*2d4be7aaSRichard Lowe 		if (argc > regargs) {
452*2d4be7aaSRichard Lowe 			size = MIN((argc - regargs) * sizeof (long),
453*2d4be7aaSRichard Lowe 			    argsize - (regargs * sizeof (long)));
454*2d4be7aaSRichard Lowe 
455*2d4be7aaSRichard Lowe 			if (Pread(P, &args[regargs], size, fp +
456*2d4be7aaSRichard Lowe 			    (sizeof (uintptr_t) * 2)) != size)
457*2d4be7aaSRichard Lowe 				return (6);
458*2d4be7aaSRichard Lowe 		}
459*2d4be7aaSRichard Lowe 
460*2d4be7aaSRichard Lowe 		return (argc);
461*2d4be7aaSRichard Lowe 	} else {
462*2d4be7aaSRichard Lowe 		return (0);
463*2d4be7aaSRichard Lowe 	}
464*2d4be7aaSRichard Lowe }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate int
4677c478bd9Sstevel@tonic-gate Pstack_iter(struct ps_prochandle *P, const prgregset_t regs,
4687c478bd9Sstevel@tonic-gate 	proc_stack_f *func, void *arg)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	struct {
4717c478bd9Sstevel@tonic-gate 		uintptr_t fp;
4727c478bd9Sstevel@tonic-gate 		uintptr_t pc;
4737c478bd9Sstevel@tonic-gate 	} frame;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	uint_t pfpsize = 0;
4767c478bd9Sstevel@tonic-gate 	prgreg_t *prevfp = NULL;
4777c478bd9Sstevel@tonic-gate 	prgreg_t fp, pfp;
4787c478bd9Sstevel@tonic-gate 	prgreg_t pc;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	prgregset_t gregs;
4817c478bd9Sstevel@tonic-gate 	int nfp = 0;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	uclist_t ucl;
4847c478bd9Sstevel@tonic-gate 	int rv = 0;
4857c478bd9Sstevel@tonic-gate 	int argc;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	uintptr_t uc_addr;
4887c478bd9Sstevel@tonic-gate 	ucontext_t uc;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	/*
4917c478bd9Sstevel@tonic-gate 	 * Type definition for a structure corresponding to an IA32
4927c478bd9Sstevel@tonic-gate 	 * signal frame.  Refer to the comments in Pstack.c for more info
4937c478bd9Sstevel@tonic-gate 	 */
4947c478bd9Sstevel@tonic-gate 	typedef struct {
4957c478bd9Sstevel@tonic-gate 		prgreg_t fp;
4967c478bd9Sstevel@tonic-gate 		prgreg_t pc;
4977c478bd9Sstevel@tonic-gate 		prgreg_t signo;
4987c478bd9Sstevel@tonic-gate 		siginfo_t *sip;
4997c478bd9Sstevel@tonic-gate 	} sigframe_t;
500*2d4be7aaSRichard Lowe 	prgreg_t args[32] = {0};
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel != PR_MODEL_LP64)
5037c478bd9Sstevel@tonic-gate 		return (Pstack_iter32(P, regs, func, arg));
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	init_uclist(&ucl, P);
5067c478bd9Sstevel@tonic-gate 	(void) memcpy(gregs, regs, sizeof (gregs));
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	fp = gregs[R_FP];
5097c478bd9Sstevel@tonic-gate 	pc = gregs[R_PC];
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	while (fp != 0 || pc != 0) {
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
5147c478bd9Sstevel@tonic-gate 			break;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
5177c478bd9Sstevel@tonic-gate 		    Pread(P, &frame, sizeof (frame), (uintptr_t)fp) ==
5187c478bd9Sstevel@tonic-gate 		    sizeof (frame)) {
519*2d4be7aaSRichard Lowe 			if (frame.pc == -1) {
5207c478bd9Sstevel@tonic-gate 				argc = 3;
5217c478bd9Sstevel@tonic-gate 				args[2] = fp + sizeof (sigframe_t);
5227c478bd9Sstevel@tonic-gate 				if (Pread(P, &args, 2 * sizeof (prgreg_t),
5237c478bd9Sstevel@tonic-gate 				    fp + 2 * sizeof (prgreg_t)) !=
5247c478bd9Sstevel@tonic-gate 				    2 * sizeof (prgreg_t))
5257c478bd9Sstevel@tonic-gate 					argc = 0;
526*2d4be7aaSRichard Lowe 			} else {
527*2d4be7aaSRichard Lowe 				argc = read_args(P, fp, pc, args,
528*2d4be7aaSRichard Lowe 				    sizeof (args));
5297c478bd9Sstevel@tonic-gate 			}
5307c478bd9Sstevel@tonic-gate 		} else {
5317c478bd9Sstevel@tonic-gate 			(void) memset(&frame, 0, sizeof (frame));
5327c478bd9Sstevel@tonic-gate 			argc = 0;
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		gregs[R_FP] = fp;
5367c478bd9Sstevel@tonic-gate 		gregs[R_PC] = pc;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		if ((rv = func(arg, gregs, argc, args)) != 0)
5397c478bd9Sstevel@tonic-gate 			break;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		pfp = fp;
5427c478bd9Sstevel@tonic-gate 		fp = frame.fp;
5437c478bd9Sstevel@tonic-gate 		pc = frame.pc;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 		if (pc == -1 && find_uclink(&ucl, pfp + sizeof (sigframe_t))) {
5467c478bd9Sstevel@tonic-gate 			uc_addr = pfp + sizeof (sigframe_t);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 			if (Pread(P, &uc, sizeof (uc), uc_addr)
5497c478bd9Sstevel@tonic-gate 			    == sizeof (uc)) {
5507c478bd9Sstevel@tonic-gate 				ucontext_n_to_prgregs(&uc, gregs);
5517c478bd9Sstevel@tonic-gate 				fp = gregs[R_FP];
5527c478bd9Sstevel@tonic-gate 				pc = gregs[R_PC];
5537c478bd9Sstevel@tonic-gate 			}
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (prevfp)
5587c478bd9Sstevel@tonic-gate 		free(prevfp);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	free_uclist(&ucl);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	return (rv);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate uintptr_t
5667c478bd9Sstevel@tonic-gate Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
5697c478bd9Sstevel@tonic-gate 		sp -= sizeof (int) * (nargs+2);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RAX] = sysindex;
5727c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RSP] = sp;
5737c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RIP] = P->sysaddr;
5747c478bd9Sstevel@tonic-gate 	} else {
5757c478bd9Sstevel@tonic-gate 		int pusharg = (nargs > 6) ? nargs - 6: 0;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		sp -= sizeof (int64_t) * (pusharg+2);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RAX] = sysindex;
5807c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RSP] = sp;
5817c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RIP] = P->sysaddr;
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	return (sp);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate int
5887c478bd9Sstevel@tonic-gate Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
5897c478bd9Sstevel@tonic-gate     uintptr_t ap)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
5927c478bd9Sstevel@tonic-gate 		int32_t arglist[MAXARGS+2];
5937c478bd9Sstevel@tonic-gate 		int i;
5947c478bd9Sstevel@tonic-gate 		argdes_t *adp;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++)
5977c478bd9Sstevel@tonic-gate 			arglist[1 + i] = (int32_t)adp->arg_value;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		arglist[0] = P->status.pr_lwp.pr_reg[REG_RIP];
6007c478bd9Sstevel@tonic-gate 		if (Pwrite(P, &arglist[0], sizeof (int) * (nargs+1),
6017c478bd9Sstevel@tonic-gate 		    (uintptr_t)ap) != sizeof (int) * (nargs+1))
6027c478bd9Sstevel@tonic-gate 			return (-1);
6037c478bd9Sstevel@tonic-gate 	} else {
6047c478bd9Sstevel@tonic-gate 		int64_t arglist[MAXARGS+2];
6057c478bd9Sstevel@tonic-gate 		int i;
6067c478bd9Sstevel@tonic-gate 		argdes_t *adp;
6077c478bd9Sstevel@tonic-gate 		int pusharg = (nargs > 6) ? nargs - 6: 0;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++) {
6107c478bd9Sstevel@tonic-gate 			switch (i) {
6117c478bd9Sstevel@tonic-gate 			case 0:
6127c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RDI, adp->arg_value);
6137c478bd9Sstevel@tonic-gate 				break;
6147c478bd9Sstevel@tonic-gate 			case 1:
6157c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RSI, adp->arg_value);
6167c478bd9Sstevel@tonic-gate 				break;
6177c478bd9Sstevel@tonic-gate 			case 2:
6187c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RDX, adp->arg_value);
6197c478bd9Sstevel@tonic-gate 				break;
6207c478bd9Sstevel@tonic-gate 			case 3:
6217c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RCX, adp->arg_value);
6227c478bd9Sstevel@tonic-gate 				break;
6237c478bd9Sstevel@tonic-gate 			case 4:
6247c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_R8, adp->arg_value);
6257c478bd9Sstevel@tonic-gate 				break;
6267c478bd9Sstevel@tonic-gate 			case 5:
6277c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_R9, adp->arg_value);
6287c478bd9Sstevel@tonic-gate 				break;
6297c478bd9Sstevel@tonic-gate 			default:
6307c478bd9Sstevel@tonic-gate 				arglist[i - 5] = (uint64_t)adp->arg_value;
6317c478bd9Sstevel@tonic-gate 				break;
6327c478bd9Sstevel@tonic-gate 			}
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 		arglist[0] = P->status.pr_lwp.pr_reg[REG_RIP];
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		if (Pwrite(P, &arglist[0],
6387c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) * (pusharg + 1), ap) !=
6397c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) * (pusharg + 1))
6407c478bd9Sstevel@tonic-gate 			return (-1);
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	return (0);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate int
6477c478bd9Sstevel@tonic-gate Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
6487c478bd9Sstevel@tonic-gate     uintptr_t ap)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
6517c478bd9Sstevel@tonic-gate 		uint32_t arglist[MAXARGS + 2];
6527c478bd9Sstevel@tonic-gate 		int i;
6537c478bd9Sstevel@tonic-gate 		argdes_t *adp;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 		if (Pread(P, &arglist[0], sizeof (int) * (nargs+1),
6567c478bd9Sstevel@tonic-gate 		    (uintptr_t)ap) != sizeof (int) * (nargs+1))
6577c478bd9Sstevel@tonic-gate 			return (-1);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++)
6607c478bd9Sstevel@tonic-gate 			adp->arg_value = arglist[i];
6617c478bd9Sstevel@tonic-gate 	} else {
6627c478bd9Sstevel@tonic-gate 		int pusharg = (nargs > 6) ? nargs - 6: 0;
6637c478bd9Sstevel@tonic-gate 		int64_t arglist[MAXARGS+2];
6647c478bd9Sstevel@tonic-gate 		int i;
6657c478bd9Sstevel@tonic-gate 		argdes_t *adp;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		if (pusharg  > 0 &&
6687c478bd9Sstevel@tonic-gate 		    Pread(P, &arglist[0], sizeof (int64_t) * (pusharg + 1),
6697c478bd9Sstevel@tonic-gate 		    ap) != sizeof (int64_t) * (pusharg + 1))
6707c478bd9Sstevel@tonic-gate 			return (-1);
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++) {
6737c478bd9Sstevel@tonic-gate 			switch (i) {
6747c478bd9Sstevel@tonic-gate 			case 0:
6757c478bd9Sstevel@tonic-gate 				adp->arg_value =
6767c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RDI];
6777c478bd9Sstevel@tonic-gate 				break;
6787c478bd9Sstevel@tonic-gate 			case 1:
6797c478bd9Sstevel@tonic-gate 				adp->arg_value =
6807c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RSI];
6817c478bd9Sstevel@tonic-gate 				break;
6827c478bd9Sstevel@tonic-gate 			case 2:
6837c478bd9Sstevel@tonic-gate 				adp->arg_value =
6847c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RDX];
6857c478bd9Sstevel@tonic-gate 				break;
6867c478bd9Sstevel@tonic-gate 			case 3:
6877c478bd9Sstevel@tonic-gate 				adp->arg_value =
6887c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RCX];
6897c478bd9Sstevel@tonic-gate 				break;
6907c478bd9Sstevel@tonic-gate 			case 4:
6917c478bd9Sstevel@tonic-gate 				adp->arg_value =
6927c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_R8];
6937c478bd9Sstevel@tonic-gate 				break;
6947c478bd9Sstevel@tonic-gate 			case 5:
6957c478bd9Sstevel@tonic-gate 				adp->arg_value =
6967c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_R9];
6977c478bd9Sstevel@tonic-gate 				break;
6987c478bd9Sstevel@tonic-gate 			default:
6997c478bd9Sstevel@tonic-gate 				adp->arg_value = arglist[i - 6];
7007c478bd9Sstevel@tonic-gate 				break;
7017c478bd9Sstevel@tonic-gate 			}
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		return (0);
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	return (0);
7087c478bd9Sstevel@tonic-gate }
709