xref: /illumos-gate/usr/src/lib/libproc/amd64/Pisadep.c (revision c75682cd)
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
5d51e9074Sab  * Common Development and Distribution License (the "License").
6d51e9074Sab  * 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.
248f697f9aSRobert Mustacchi  * Copyright 2018, Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/stack.h>
287c478bd9Sstevel@tonic-gate #include <sys/regset.h>
297c478bd9Sstevel@tonic-gate #include <sys/frame.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/trap.h>
32d9452f23SEdward Pilatowicz #include <sys/machelf.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate 
402d4be7aaSRichard Lowe #include <saveargs.h>
417c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
427c478bd9Sstevel@tonic-gate #include "Pstack.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static uchar_t int_syscall_instr[] = { 0xCD, T_SYSCALLINT };
457c478bd9Sstevel@tonic-gate static uchar_t syscall_instr[] = { 0x0f, 0x05 };
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate const char *
Ppltdest(struct ps_prochandle * P,uintptr_t pltaddr)487c478bd9Sstevel@tonic-gate Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	map_info_t *mp = Paddr2mptr(P, pltaddr);
517c478bd9Sstevel@tonic-gate 	file_info_t *fp;
527c478bd9Sstevel@tonic-gate 	size_t i;
537c478bd9Sstevel@tonic-gate 	uintptr_t r_addr;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if (mp == NULL || (fp = mp->map_file) == NULL ||
567c478bd9Sstevel@tonic-gate 	    fp->file_plt_base == 0 ||
577c478bd9Sstevel@tonic-gate 	    pltaddr - fp->file_plt_base >= fp->file_plt_size) {
587c478bd9Sstevel@tonic-gate 		errno = EINVAL;
597c478bd9Sstevel@tonic-gate 		return (NULL);
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 
62d9452f23SEdward Pilatowicz 	i = (pltaddr - fp->file_plt_base) / M_PLT_ENTSIZE - M_PLT_XNumber;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
657c478bd9Sstevel@tonic-gate 		Elf64_Rela r;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 		r_addr = fp->file_jmp_rel + i * sizeof (r);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
707c478bd9Sstevel@tonic-gate 		    (i = ELF64_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
71d51e9074Sab 			Elf_Data *data = fp->file_dynsym.sym_data_pri;
727c478bd9Sstevel@tonic-gate 			Elf64_Sym *symp = &(((Elf64_Sym *)data->d_buf)[i]);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 			return (fp->file_dynsym.sym_strs + symp->st_name);
757c478bd9Sstevel@tonic-gate 		}
767c478bd9Sstevel@tonic-gate 	} else {
777c478bd9Sstevel@tonic-gate 		Elf32_Rel r;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		r_addr = fp->file_jmp_rel + i * sizeof (r);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
827c478bd9Sstevel@tonic-gate 		    (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
83d51e9074Sab 			Elf_Data *data = fp->file_dynsym.sym_data_pri;
847c478bd9Sstevel@tonic-gate 			Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 			return (fp->file_dynsym.sym_strs + symp->st_name);
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	return (NULL);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate int
Pissyscall(struct ps_prochandle * P,uintptr_t addr)947c478bd9Sstevel@tonic-gate Pissyscall(struct ps_prochandle *P, uintptr_t addr)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	uchar_t instr[16];
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
997c478bd9Sstevel@tonic-gate 		if (Pread(P, instr, sizeof (syscall_instr), addr) !=
1007c478bd9Sstevel@tonic-gate 		    sizeof (syscall_instr) ||
1017c478bd9Sstevel@tonic-gate 		    memcmp(instr, syscall_instr, sizeof (syscall_instr)) != 0)
1027c478bd9Sstevel@tonic-gate 			return (0);
1037c478bd9Sstevel@tonic-gate 		else
1047c478bd9Sstevel@tonic-gate 			return (1);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (Pread(P, instr, sizeof (int_syscall_instr), addr) !=
1087c478bd9Sstevel@tonic-gate 	    sizeof (int_syscall_instr))
1097c478bd9Sstevel@tonic-gate 		return (0);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (memcmp(instr, int_syscall_instr, sizeof (int_syscall_instr)) == 0)
1127c478bd9Sstevel@tonic-gate 		return (1);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	return (0);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate int
Pissyscall_prev(struct ps_prochandle * P,uintptr_t addr,uintptr_t * dst)1187c478bd9Sstevel@tonic-gate Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int ret;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
1237c478bd9Sstevel@tonic-gate 		if (Pissyscall(P, addr - sizeof (syscall_instr))) {
1247c478bd9Sstevel@tonic-gate 			if (dst)
1257c478bd9Sstevel@tonic-gate 				*dst = addr - sizeof (syscall_instr);
1267c478bd9Sstevel@tonic-gate 			return (1);
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 		return (0);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if ((ret = Pissyscall(P, addr - sizeof (int_syscall_instr))) != 0) {
1327c478bd9Sstevel@tonic-gate 		if (dst)
1337c478bd9Sstevel@tonic-gate 			*dst = addr - sizeof (int_syscall_instr);
1347c478bd9Sstevel@tonic-gate 		return (ret);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	return (0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate int
Pissyscall_text(struct ps_prochandle * P,const void * buf,size_t buflen)1417c478bd9Sstevel@tonic-gate Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
1447c478bd9Sstevel@tonic-gate 		if (buflen >= sizeof (syscall_instr) &&
1457c478bd9Sstevel@tonic-gate 		    memcmp(buf, syscall_instr, sizeof (syscall_instr)) == 0)
1467c478bd9Sstevel@tonic-gate 			return (1);
1477c478bd9Sstevel@tonic-gate 		else
1487c478bd9Sstevel@tonic-gate 			return (0);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (buflen < sizeof (int_syscall_instr))
1527c478bd9Sstevel@tonic-gate 		return (0);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (memcmp(buf, int_syscall_instr, sizeof (int_syscall_instr)) == 0)
1557c478bd9Sstevel@tonic-gate 		return (1);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	return (0);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #define	TR_ARG_MAX 6	/* Max args to print, same as SPARC */
1617c478bd9Sstevel@tonic-gate 
1628f697f9aSRobert Mustacchi static boolean_t
argcount_ctf(struct ps_prochandle * P,uint32_t pc,uint_t * countp)1638f697f9aSRobert Mustacchi argcount_ctf(struct ps_prochandle *P, uint32_t pc, uint_t *countp)
1648f697f9aSRobert Mustacchi {
1658f697f9aSRobert Mustacchi 	GElf_Sym sym;
1668f697f9aSRobert Mustacchi 	ctf_file_t *ctfp;
1678f697f9aSRobert Mustacchi 	ctf_funcinfo_t finfo;
1688f697f9aSRobert Mustacchi 	prsyminfo_t si = { 0 };
1698f697f9aSRobert Mustacchi 
1708f697f9aSRobert Mustacchi 	if (Pxlookup_by_addr(P, pc, NULL, 0, &sym, &si) != 0)
1718f697f9aSRobert Mustacchi 		return (B_FALSE);
1728f697f9aSRobert Mustacchi 
1738f697f9aSRobert Mustacchi 	if ((ctfp = Paddr_to_ctf(P, pc)) == NULL)
1748f697f9aSRobert Mustacchi 		return (B_FALSE);
1758f697f9aSRobert Mustacchi 
1768f697f9aSRobert Mustacchi 	if (ctf_func_info(ctfp, si.prs_id, &finfo) == CTF_ERR)
1778f697f9aSRobert Mustacchi 		return (B_FALSE);
1788f697f9aSRobert Mustacchi 
1798f697f9aSRobert Mustacchi 	*countp = finfo.ctc_argc;
1808f697f9aSRobert Mustacchi 
1818f697f9aSRobert Mustacchi 	return (B_TRUE);
1828f697f9aSRobert Mustacchi }
1838f697f9aSRobert Mustacchi 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * Given a return address, determine the likely number of arguments
1867c478bd9Sstevel@tonic-gate  * that were pushed on the stack prior to its execution.  We do this by
1877c478bd9Sstevel@tonic-gate  * expecting that a typical call sequence consists of pushing arguments on
1887c478bd9Sstevel@tonic-gate  * the stack, executing a call instruction, and then performing an add
1897c478bd9Sstevel@tonic-gate  * on %esp to restore it to the value prior to pushing the arguments for
1907c478bd9Sstevel@tonic-gate  * the call.  We attempt to detect such an add, and divide the addend
1917c478bd9Sstevel@tonic-gate  * by the size of a word to determine the number of pushed arguments.
1927c478bd9Sstevel@tonic-gate  *
1937c478bd9Sstevel@tonic-gate  * If we do not find such an add, this does not necessarily imply that the
1947c478bd9Sstevel@tonic-gate  * function took no arguments. It is not possible to reliably detect such a
1957c478bd9Sstevel@tonic-gate  * void function because hand-coded assembler does not always perform an add
1967c478bd9Sstevel@tonic-gate  * to %esp immediately after the "call" instruction (eg. _sys_call()).
1977c478bd9Sstevel@tonic-gate  * Because of this, we default to returning MIN(sz, TR_ARG_MAX) instead of 0
1987c478bd9Sstevel@tonic-gate  * in the absence of an add to %esp.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate static ulong_t
argcount(struct ps_prochandle * P,uint32_t pc,ssize_t sz)2017c478bd9Sstevel@tonic-gate argcount(struct ps_prochandle *P, uint32_t pc, ssize_t sz)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	uchar_t instr[6];
2047c478bd9Sstevel@tonic-gate 	ulong_t count, max;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	max = MIN(sz / sizeof (uint32_t), TR_ARG_MAX);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Read the instruction at the return location.
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	if (Pread(P, instr, sizeof (instr), (uintptr_t)pc) != sizeof (instr))
2127c478bd9Sstevel@tonic-gate 		return (max);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (instr[1] != 0xc4)
2157c478bd9Sstevel@tonic-gate 		return (max);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	switch (instr[0]) {
2187c478bd9Sstevel@tonic-gate 	case 0x81:	/* count is a longword */
2197c478bd9Sstevel@tonic-gate 		count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24);
2207c478bd9Sstevel@tonic-gate 		break;
2217c478bd9Sstevel@tonic-gate 	case 0x83:	/* count is a byte */
2227c478bd9Sstevel@tonic-gate 		count = instr[2];
2237c478bd9Sstevel@tonic-gate 		break;
2247c478bd9Sstevel@tonic-gate 	default:
2257c478bd9Sstevel@tonic-gate 		return (max);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	count /= sizeof (uint32_t);
2297c478bd9Sstevel@tonic-gate 	return (MIN(count, max));
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static void
ucontext_32_to_prgregs(const ucontext32_t * uc,prgregset_t dst)2337c478bd9Sstevel@tonic-gate ucontext_32_to_prgregs(const ucontext32_t *uc, prgregset_t dst)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	const greg32_t *src = &uc->uc_mcontext.gregs[0];
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	dst[REG_DS] = (uint16_t)src[DS];
2387c478bd9Sstevel@tonic-gate 	dst[REG_ES] = (uint16_t)src[ES];
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	dst[REG_GS] = (uint16_t)src[GS];
2417c478bd9Sstevel@tonic-gate 	dst[REG_FS] = (uint16_t)src[FS];
2427c478bd9Sstevel@tonic-gate 	dst[REG_SS] = (uint16_t)src[SS];
2437c478bd9Sstevel@tonic-gate 	dst[REG_RSP] = (uint32_t)src[UESP];
2447c478bd9Sstevel@tonic-gate 	dst[REG_RFL] = src[EFL];
2457c478bd9Sstevel@tonic-gate 	dst[REG_CS] = (uint16_t)src[CS];
2467c478bd9Sstevel@tonic-gate 	dst[REG_RIP] = (uint32_t)src[EIP];
2477c478bd9Sstevel@tonic-gate 	dst[REG_ERR] = (uint32_t)src[ERR];
2487c478bd9Sstevel@tonic-gate 	dst[REG_TRAPNO] = (uint32_t)src[TRAPNO];
2497c478bd9Sstevel@tonic-gate 	dst[REG_RAX] = (uint32_t)src[EAX];
2507c478bd9Sstevel@tonic-gate 	dst[REG_RCX] = (uint32_t)src[ECX];
2517c478bd9Sstevel@tonic-gate 	dst[REG_RDX] = (uint32_t)src[EDX];
2527c478bd9Sstevel@tonic-gate 	dst[REG_RBX] = (uint32_t)src[EBX];
2537c478bd9Sstevel@tonic-gate 	dst[REG_RBP] = (uint32_t)src[EBP];
2547c478bd9Sstevel@tonic-gate 	dst[REG_RSI] = (uint32_t)src[ESI];
2557c478bd9Sstevel@tonic-gate 	dst[REG_RDI] = (uint32_t)src[EDI];
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static int
Pstack_iter32(struct ps_prochandle * P,const prgregset_t regs,proc_stack_f * func,void * arg)2597c478bd9Sstevel@tonic-gate Pstack_iter32(struct ps_prochandle *P, const prgregset_t regs,
2607c478bd9Sstevel@tonic-gate     proc_stack_f *func, void *arg)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	prgreg_t *prevfp = NULL;
2637c478bd9Sstevel@tonic-gate 	uint_t pfpsize = 0;
2647c478bd9Sstevel@tonic-gate 	int nfp = 0;
2657c478bd9Sstevel@tonic-gate 	struct {
2667c478bd9Sstevel@tonic-gate 		prgreg32_t fp;
2677c478bd9Sstevel@tonic-gate 		prgreg32_t pc;
2687c478bd9Sstevel@tonic-gate 		prgreg32_t args[32];
2697c478bd9Sstevel@tonic-gate 	} frame;
2707c478bd9Sstevel@tonic-gate 	uint_t argc;
2717c478bd9Sstevel@tonic-gate 	ssize_t sz;
2727c478bd9Sstevel@tonic-gate 	prgregset_t gregs;
2738f697f9aSRobert Mustacchi 	uint32_t fp, pfp, pc, ctf_pc;
2747c478bd9Sstevel@tonic-gate 	long args[32];
2757c478bd9Sstevel@tonic-gate 	int rv;
2767c478bd9Sstevel@tonic-gate 	int i;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * Type definition for a structure corresponding to an IA32
2807c478bd9Sstevel@tonic-gate 	 * signal frame.  Refer to the comments in Pstack.c for more info
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	typedef struct {
2837c478bd9Sstevel@tonic-gate 		prgreg32_t fp;
2847c478bd9Sstevel@tonic-gate 		prgreg32_t pc;
2857c478bd9Sstevel@tonic-gate 		int signo;
2867c478bd9Sstevel@tonic-gate 		caddr32_t ucp;
2877c478bd9Sstevel@tonic-gate 		caddr32_t sip;
2887c478bd9Sstevel@tonic-gate 	} sf_t;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	uclist_t ucl;
2917c478bd9Sstevel@tonic-gate 	ucontext32_t uc;
2927c478bd9Sstevel@tonic-gate 	uintptr_t uc_addr;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	init_uclist(&ucl, P);
2957c478bd9Sstevel@tonic-gate 	(void) memcpy(gregs, regs, sizeof (gregs));
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	fp = regs[R_FP];
2988f697f9aSRobert Mustacchi 	ctf_pc = pc = regs[R_PC];
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	while (fp != 0 || pc != 0) {
3017c478bd9Sstevel@tonic-gate 		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
3027c478bd9Sstevel@tonic-gate 			break;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
3057c478bd9Sstevel@tonic-gate 		    (sz = Pread(P, &frame, sizeof (frame), (uintptr_t)fp)
3067c478bd9Sstevel@tonic-gate 		    >= (ssize_t)(2* sizeof (uint32_t)))) {
3077c478bd9Sstevel@tonic-gate 			/*
3087c478bd9Sstevel@tonic-gate 			 * One more trick for signal frames: the kernel sets
3097c478bd9Sstevel@tonic-gate 			 * the return pc of the signal frame to 0xffffffff on
3107c478bd9Sstevel@tonic-gate 			 * Intel IA32, so argcount won't work.
3117c478bd9Sstevel@tonic-gate 			 */
3127c478bd9Sstevel@tonic-gate 			if (frame.pc != -1L) {
3137c478bd9Sstevel@tonic-gate 				sz -= 2* sizeof (uint32_t);
3148f697f9aSRobert Mustacchi 				if (argcount_ctf(P, ctf_pc, &argc)) {
3158f697f9aSRobert Mustacchi 					argc = MIN(argc, 32);
3168f697f9aSRobert Mustacchi 				} else {
3178f697f9aSRobert Mustacchi 					argc = argcount(P, (uint32_t)frame.pc,
3188f697f9aSRobert Mustacchi 					    sz);
3198f697f9aSRobert Mustacchi 				}
3207c478bd9Sstevel@tonic-gate 			} else
3217c478bd9Sstevel@tonic-gate 				argc = 3; /* sighandler(signo, sip, ucp) */
3227c478bd9Sstevel@tonic-gate 		} else {
3237c478bd9Sstevel@tonic-gate 			(void) memset(&frame, 0, sizeof (frame));
3247c478bd9Sstevel@tonic-gate 			argc = 0;
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 
3278f697f9aSRobert Mustacchi 		ctf_pc = frame.pc;
3287c478bd9Sstevel@tonic-gate 		gregs[R_FP] = fp;
3297c478bd9Sstevel@tonic-gate 		gregs[R_PC] = pc;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++)
3327c478bd9Sstevel@tonic-gate 			args[i] = (uint32_t)frame.args[i];
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		if ((rv = func(arg, gregs, argc, args)) != 0)
3357c478bd9Sstevel@tonic-gate 			break;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		/*
3387c478bd9Sstevel@tonic-gate 		 * In order to allow iteration over java frames (which can have
3397c478bd9Sstevel@tonic-gate 		 * their own frame pointers), we allow the iterator to change
3407c478bd9Sstevel@tonic-gate 		 * the contents of gregs.  If we detect a change, then we assume
3417c478bd9Sstevel@tonic-gate 		 * that the new values point to the next frame.
3427c478bd9Sstevel@tonic-gate 		 */
3437c478bd9Sstevel@tonic-gate 		if (gregs[R_FP] != fp || gregs[R_PC] != pc) {
3447c478bd9Sstevel@tonic-gate 			fp = gregs[R_FP];
3457c478bd9Sstevel@tonic-gate 			pc = gregs[R_PC];
3467c478bd9Sstevel@tonic-gate 			continue;
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		pfp = fp;
3507c478bd9Sstevel@tonic-gate 		fp = frame.fp;
3517c478bd9Sstevel@tonic-gate 		pc = frame.pc;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		if (find_uclink(&ucl, pfp + sizeof (sf_t)))
3547c478bd9Sstevel@tonic-gate 			uc_addr = pfp + sizeof (sf_t);
3557c478bd9Sstevel@tonic-gate 		else
356*c75682cdSToomas Soome 			uc_addr = (uintptr_t)NULL;
3577c478bd9Sstevel@tonic-gate 
358*c75682cdSToomas Soome 		if (uc_addr != (uintptr_t)NULL &&
3597c478bd9Sstevel@tonic-gate 		    Pread(P, &uc, sizeof (uc), uc_addr) == sizeof (uc)) {
3607c478bd9Sstevel@tonic-gate 			ucontext_32_to_prgregs(&uc, gregs);
3617c478bd9Sstevel@tonic-gate 			fp = gregs[R_FP];
3627c478bd9Sstevel@tonic-gate 			pc = gregs[R_PC];
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (prevfp)
3677c478bd9Sstevel@tonic-gate 		free(prevfp);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	free_uclist(&ucl);
3707c478bd9Sstevel@tonic-gate 	return (rv);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate static void
ucontext_n_to_prgregs(const ucontext_t * src,prgregset_t dst)3747c478bd9Sstevel@tonic-gate ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	(void) memcpy(dst, src->uc_mcontext.gregs, sizeof (gregset_t));
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3792d4be7aaSRichard Lowe /*
3802d4be7aaSRichard Lowe  * Read arguments from the frame indicated by regs into args, return the
3812d4be7aaSRichard Lowe  * number of arguments successfully read
3822d4be7aaSRichard Lowe  */
3832d4be7aaSRichard Lowe static int
read_args(struct ps_prochandle * P,uintptr_t fp,uintptr_t pc,prgreg_t * args,size_t argsize)3842d4be7aaSRichard Lowe read_args(struct ps_prochandle *P, uintptr_t fp, uintptr_t pc, prgreg_t *args,
3852d4be7aaSRichard Lowe     size_t argsize)
3862d4be7aaSRichard Lowe {
3872d4be7aaSRichard Lowe 	GElf_Sym sym;
3882d4be7aaSRichard Lowe 	ctf_file_t *ctfp = NULL;
3892d4be7aaSRichard Lowe 	ctf_funcinfo_t finfo;
3902d4be7aaSRichard Lowe 	prsyminfo_t si = {0};
3912d4be7aaSRichard Lowe 	uint8_t ins[SAVEARGS_INSN_SEQ_LEN];
3922d4be7aaSRichard Lowe 	size_t insnsize;
3932d4be7aaSRichard Lowe 	int argc = 0;
3942d4be7aaSRichard Lowe 	int rettype = 0;
3952d4be7aaSRichard Lowe 	int start_index = 0;
3962d4be7aaSRichard Lowe 	int args_style = 0;
3972d4be7aaSRichard Lowe 	int i;
3982d4be7aaSRichard Lowe 	ctf_id_t args_types[5];
3992d4be7aaSRichard Lowe 
4002d4be7aaSRichard Lowe 	if (Pxlookup_by_addr(P, pc, NULL, 0, &sym, &si) != 0)
4012d4be7aaSRichard Lowe 		return (0);
4022d4be7aaSRichard Lowe 
4032d4be7aaSRichard Lowe 	if ((ctfp = Paddr_to_ctf(P, pc)) == NULL)
4042d4be7aaSRichard Lowe 		return (0);
4052d4be7aaSRichard Lowe 
4062d4be7aaSRichard Lowe 	if (ctf_func_info(ctfp, si.prs_id, &finfo) == CTF_ERR)
4072d4be7aaSRichard Lowe 		return (0);
4082d4be7aaSRichard Lowe 
4092d4be7aaSRichard Lowe 	argc = finfo.ctc_argc;
4102d4be7aaSRichard Lowe 
4112d4be7aaSRichard Lowe 	if (argc == 0)
4122d4be7aaSRichard Lowe 		return (0);
4132d4be7aaSRichard Lowe 
4142d4be7aaSRichard Lowe 	rettype = ctf_type_kind(ctfp, finfo.ctc_return);
4152d4be7aaSRichard Lowe 
4162d4be7aaSRichard Lowe 	/*
4172d4be7aaSRichard Lowe 	 * If the function returns a structure or union greater than 16 bytes
4182d4be7aaSRichard Lowe 	 * in size %rdi contains the address in which to store the return
4192d4be7aaSRichard Lowe 	 * value rather than for an argument.
4202d4be7aaSRichard Lowe 	 */
4212d4be7aaSRichard Lowe 	if (((rettype == CTF_K_STRUCT) || (rettype == CTF_K_UNION)) &&
4222d4be7aaSRichard Lowe 	    ctf_type_size(ctfp, finfo.ctc_return) > 16)
4232d4be7aaSRichard Lowe 		start_index = 1;
4242d4be7aaSRichard Lowe 	else
4252d4be7aaSRichard Lowe 		start_index = 0;
4262d4be7aaSRichard Lowe 
4272d4be7aaSRichard Lowe 	/*
4282d4be7aaSRichard Lowe 	 * If any of the first 5 arguments are a structure less than 16 bytes
4292d4be7aaSRichard Lowe 	 * in size, it will be passed spread across two argument registers,
4302d4be7aaSRichard Lowe 	 * and we will not cope.
4312d4be7aaSRichard Lowe 	 */
4322d4be7aaSRichard Lowe 	if (ctf_func_args(ctfp, si.prs_id, 5, args_types) == CTF_ERR)
4332d4be7aaSRichard Lowe 		return (0);
4342d4be7aaSRichard Lowe 
4352d4be7aaSRichard Lowe 	for (i = 0; i < MIN(5, finfo.ctc_argc); i++) {
4362d4be7aaSRichard Lowe 		int t = ctf_type_kind(ctfp, args_types[i]);
4372d4be7aaSRichard Lowe 
4382d4be7aaSRichard Lowe 		if (((t == CTF_K_STRUCT) || (t == CTF_K_UNION)) &&
4392d4be7aaSRichard Lowe 		    ctf_type_size(ctfp, args_types[i]) <= 16)
4402d4be7aaSRichard Lowe 			return (0);
4412d4be7aaSRichard Lowe 	}
4422d4be7aaSRichard Lowe 
4432d4be7aaSRichard Lowe 	/*
4442d4be7aaSRichard Lowe 	 * The number of instructions to search for argument saving is limited
4452d4be7aaSRichard Lowe 	 * such that only instructions prior to %pc are considered and we
4462d4be7aaSRichard Lowe 	 * never read arguments from a function where the saving code has not
4472d4be7aaSRichard Lowe 	 * in fact yet executed.
4482d4be7aaSRichard Lowe 	 */
4492d4be7aaSRichard Lowe 	insnsize = MIN(MIN(sym.st_size, SAVEARGS_INSN_SEQ_LEN),
4502d4be7aaSRichard Lowe 	    pc - sym.st_value);
4512d4be7aaSRichard Lowe 
4522d4be7aaSRichard Lowe 	if (Pread(P, ins, insnsize, sym.st_value) != insnsize)
4532d4be7aaSRichard Lowe 		return (0);
4542d4be7aaSRichard Lowe 
4552d4be7aaSRichard Lowe 	if ((argc != 0) &&
4562d4be7aaSRichard Lowe 	    ((args_style = saveargs_has_args(ins, insnsize, argc,
4572d4be7aaSRichard Lowe 	    start_index)) != SAVEARGS_NO_ARGS)) {
4582d4be7aaSRichard Lowe 		int regargs = MIN((6 - start_index), argc);
4592d4be7aaSRichard Lowe 		size_t size = regargs * sizeof (long);
4602d4be7aaSRichard Lowe 		int i;
4612d4be7aaSRichard Lowe 
4622d4be7aaSRichard Lowe 		/*
4632d4be7aaSRichard Lowe 		 * If Studio pushed a structure return address as an argument,
4642d4be7aaSRichard Lowe 		 * we need to read one more argument than actually exists (the
4652d4be7aaSRichard Lowe 		 * addr) to make everything line up.
4662d4be7aaSRichard Lowe 		 */
4672d4be7aaSRichard Lowe 		if (args_style == SAVEARGS_STRUCT_ARGS)
4682d4be7aaSRichard Lowe 			size += sizeof (long);
4692d4be7aaSRichard Lowe 
4702d4be7aaSRichard Lowe 		if (Pread(P, args, size, (fp - size)) != size)
4712d4be7aaSRichard Lowe 			return (0);
4722d4be7aaSRichard Lowe 
4732d4be7aaSRichard Lowe 		for (i = 0; i < (regargs / 2); i++) {
4742d4be7aaSRichard Lowe 			prgreg_t t = args[i];
4752d4be7aaSRichard Lowe 
4762d4be7aaSRichard Lowe 			args[i] = args[regargs - i - 1];
4772d4be7aaSRichard Lowe 			args[regargs - i - 1] = t;
4782d4be7aaSRichard Lowe 		}
4792d4be7aaSRichard Lowe 
4802d4be7aaSRichard Lowe 		if (argc > regargs) {
4812d4be7aaSRichard Lowe 			size = MIN((argc - regargs) * sizeof (long),
4822d4be7aaSRichard Lowe 			    argsize - (regargs * sizeof (long)));
4832d4be7aaSRichard Lowe 
4842d4be7aaSRichard Lowe 			if (Pread(P, &args[regargs], size, fp +
4852d4be7aaSRichard Lowe 			    (sizeof (uintptr_t) * 2)) != size)
4862d4be7aaSRichard Lowe 				return (6);
4872d4be7aaSRichard Lowe 		}
4882d4be7aaSRichard Lowe 
4892d4be7aaSRichard Lowe 		return (argc);
4902d4be7aaSRichard Lowe 	} else {
4912d4be7aaSRichard Lowe 		return (0);
4922d4be7aaSRichard Lowe 	}
4932d4be7aaSRichard Lowe }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate int
Pstack_iter(struct ps_prochandle * P,const prgregset_t regs,proc_stack_f * func,void * arg)4967c478bd9Sstevel@tonic-gate Pstack_iter(struct ps_prochandle *P, const prgregset_t regs,
4978f697f9aSRobert Mustacchi     proc_stack_f *func, void *arg)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	struct {
5007c478bd9Sstevel@tonic-gate 		uintptr_t fp;
5017c478bd9Sstevel@tonic-gate 		uintptr_t pc;
5027c478bd9Sstevel@tonic-gate 	} frame;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	uint_t pfpsize = 0;
5057c478bd9Sstevel@tonic-gate 	prgreg_t *prevfp = NULL;
5067c478bd9Sstevel@tonic-gate 	prgreg_t fp, pfp;
5077c478bd9Sstevel@tonic-gate 	prgreg_t pc;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	prgregset_t gregs;
5107c478bd9Sstevel@tonic-gate 	int nfp = 0;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	uclist_t ucl;
5137c478bd9Sstevel@tonic-gate 	int rv = 0;
5147c478bd9Sstevel@tonic-gate 	int argc;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	uintptr_t uc_addr;
5177c478bd9Sstevel@tonic-gate 	ucontext_t uc;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * Type definition for a structure corresponding to an IA32
5217c478bd9Sstevel@tonic-gate 	 * signal frame.  Refer to the comments in Pstack.c for more info
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	typedef struct {
5247c478bd9Sstevel@tonic-gate 		prgreg_t fp;
5257c478bd9Sstevel@tonic-gate 		prgreg_t pc;
5267c478bd9Sstevel@tonic-gate 		prgreg_t signo;
5277c478bd9Sstevel@tonic-gate 		siginfo_t *sip;
5287c478bd9Sstevel@tonic-gate 	} sigframe_t;
5292d4be7aaSRichard Lowe 	prgreg_t args[32] = {0};
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel != PR_MODEL_LP64)
5327c478bd9Sstevel@tonic-gate 		return (Pstack_iter32(P, regs, func, arg));
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	init_uclist(&ucl, P);
5357c478bd9Sstevel@tonic-gate 	(void) memcpy(gregs, regs, sizeof (gregs));
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	fp = gregs[R_FP];
5387c478bd9Sstevel@tonic-gate 	pc = gregs[R_PC];
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	while (fp != 0 || pc != 0) {
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
5437c478bd9Sstevel@tonic-gate 			break;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 		if (fp != 0 &&
5467c478bd9Sstevel@tonic-gate 		    Pread(P, &frame, sizeof (frame), (uintptr_t)fp) ==
5477c478bd9Sstevel@tonic-gate 		    sizeof (frame)) {
5482d4be7aaSRichard Lowe 			if (frame.pc == -1) {
5497c478bd9Sstevel@tonic-gate 				argc = 3;
5507c478bd9Sstevel@tonic-gate 				args[2] = fp + sizeof (sigframe_t);
5517c478bd9Sstevel@tonic-gate 				if (Pread(P, &args, 2 * sizeof (prgreg_t),
5527c478bd9Sstevel@tonic-gate 				    fp + 2 * sizeof (prgreg_t)) !=
5537c478bd9Sstevel@tonic-gate 				    2 * sizeof (prgreg_t))
5547c478bd9Sstevel@tonic-gate 					argc = 0;
5552d4be7aaSRichard Lowe 			} else {
5562d4be7aaSRichard Lowe 				argc = read_args(P, fp, pc, args,
5572d4be7aaSRichard Lowe 				    sizeof (args));
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 		} else {
5607c478bd9Sstevel@tonic-gate 			(void) memset(&frame, 0, sizeof (frame));
5617c478bd9Sstevel@tonic-gate 			argc = 0;
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		gregs[R_FP] = fp;
5657c478bd9Sstevel@tonic-gate 		gregs[R_PC] = pc;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		if ((rv = func(arg, gregs, argc, args)) != 0)
5687c478bd9Sstevel@tonic-gate 			break;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 		pfp = fp;
5717c478bd9Sstevel@tonic-gate 		fp = frame.fp;
5727c478bd9Sstevel@tonic-gate 		pc = frame.pc;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		if (pc == -1 && find_uclink(&ucl, pfp + sizeof (sigframe_t))) {
5757c478bd9Sstevel@tonic-gate 			uc_addr = pfp + sizeof (sigframe_t);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 			if (Pread(P, &uc, sizeof (uc), uc_addr)
5787c478bd9Sstevel@tonic-gate 			    == sizeof (uc)) {
5797c478bd9Sstevel@tonic-gate 				ucontext_n_to_prgregs(&uc, gregs);
5807c478bd9Sstevel@tonic-gate 				fp = gregs[R_FP];
5817c478bd9Sstevel@tonic-gate 				pc = gregs[R_PC];
5827c478bd9Sstevel@tonic-gate 			}
5837c478bd9Sstevel@tonic-gate 		}
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (prevfp)
5877c478bd9Sstevel@tonic-gate 		free(prevfp);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	free_uclist(&ucl);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	return (rv);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate uintptr_t
Psyscall_setup(struct ps_prochandle * P,int nargs,int sysindex,uintptr_t sp)5957c478bd9Sstevel@tonic-gate Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
5987c478bd9Sstevel@tonic-gate 		sp -= sizeof (int) * (nargs+2);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RAX] = sysindex;
6017c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RSP] = sp;
6027c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RIP] = P->sysaddr;
6037c478bd9Sstevel@tonic-gate 	} else {
6047c478bd9Sstevel@tonic-gate 		int pusharg = (nargs > 6) ? nargs - 6: 0;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		sp -= sizeof (int64_t) * (pusharg+2);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RAX] = sysindex;
6097c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RSP] = sp;
6107c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[REG_RIP] = P->sysaddr;
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	return (sp);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate int
Psyscall_copyinargs(struct ps_prochandle * P,int nargs,argdes_t * argp,uintptr_t ap)6177c478bd9Sstevel@tonic-gate Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
6187c478bd9Sstevel@tonic-gate     uintptr_t ap)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
6217c478bd9Sstevel@tonic-gate 		int32_t arglist[MAXARGS+2];
6227c478bd9Sstevel@tonic-gate 		int i;
6237c478bd9Sstevel@tonic-gate 		argdes_t *adp;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++)
6267c478bd9Sstevel@tonic-gate 			arglist[1 + i] = (int32_t)adp->arg_value;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		arglist[0] = P->status.pr_lwp.pr_reg[REG_RIP];
6297c478bd9Sstevel@tonic-gate 		if (Pwrite(P, &arglist[0], sizeof (int) * (nargs+1),
6307c478bd9Sstevel@tonic-gate 		    (uintptr_t)ap) != sizeof (int) * (nargs+1))
6317c478bd9Sstevel@tonic-gate 			return (-1);
6327c478bd9Sstevel@tonic-gate 	} else {
6337c478bd9Sstevel@tonic-gate 		int64_t arglist[MAXARGS+2];
6347c478bd9Sstevel@tonic-gate 		int i;
6357c478bd9Sstevel@tonic-gate 		argdes_t *adp;
6367c478bd9Sstevel@tonic-gate 		int pusharg = (nargs > 6) ? nargs - 6: 0;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++) {
6397c478bd9Sstevel@tonic-gate 			switch (i) {
6407c478bd9Sstevel@tonic-gate 			case 0:
6417c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RDI, adp->arg_value);
6427c478bd9Sstevel@tonic-gate 				break;
6437c478bd9Sstevel@tonic-gate 			case 1:
6447c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RSI, adp->arg_value);
6457c478bd9Sstevel@tonic-gate 				break;
6467c478bd9Sstevel@tonic-gate 			case 2:
6477c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RDX, adp->arg_value);
6487c478bd9Sstevel@tonic-gate 				break;
6497c478bd9Sstevel@tonic-gate 			case 3:
6507c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_RCX, adp->arg_value);
6517c478bd9Sstevel@tonic-gate 				break;
6527c478bd9Sstevel@tonic-gate 			case 4:
6537c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_R8, adp->arg_value);
6547c478bd9Sstevel@tonic-gate 				break;
6557c478bd9Sstevel@tonic-gate 			case 5:
6567c478bd9Sstevel@tonic-gate 				(void) Pputareg(P, REG_R9, adp->arg_value);
6577c478bd9Sstevel@tonic-gate 				break;
6587c478bd9Sstevel@tonic-gate 			default:
6597c478bd9Sstevel@tonic-gate 				arglist[i - 5] = (uint64_t)adp->arg_value;
6607c478bd9Sstevel@tonic-gate 				break;
6617c478bd9Sstevel@tonic-gate 			}
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		arglist[0] = P->status.pr_lwp.pr_reg[REG_RIP];
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		if (Pwrite(P, &arglist[0],
6677c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) * (pusharg + 1), ap) !=
6687c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) * (pusharg + 1))
6697c478bd9Sstevel@tonic-gate 			return (-1);
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	return (0);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate int
Psyscall_copyoutargs(struct ps_prochandle * P,int nargs,argdes_t * argp,uintptr_t ap)6767c478bd9Sstevel@tonic-gate Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
6777c478bd9Sstevel@tonic-gate     uintptr_t ap)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
6807c478bd9Sstevel@tonic-gate 		uint32_t arglist[MAXARGS + 2];
6817c478bd9Sstevel@tonic-gate 		int i;
6827c478bd9Sstevel@tonic-gate 		argdes_t *adp;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		if (Pread(P, &arglist[0], sizeof (int) * (nargs+1),
6857c478bd9Sstevel@tonic-gate 		    (uintptr_t)ap) != sizeof (int) * (nargs+1))
6867c478bd9Sstevel@tonic-gate 			return (-1);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++)
6897c478bd9Sstevel@tonic-gate 			adp->arg_value = arglist[i];
6907c478bd9Sstevel@tonic-gate 	} else {
6917c478bd9Sstevel@tonic-gate 		int pusharg = (nargs > 6) ? nargs - 6: 0;
6927c478bd9Sstevel@tonic-gate 		int64_t arglist[MAXARGS+2];
6937c478bd9Sstevel@tonic-gate 		int i;
6947c478bd9Sstevel@tonic-gate 		argdes_t *adp;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 		if (pusharg  > 0 &&
6977c478bd9Sstevel@tonic-gate 		    Pread(P, &arglist[0], sizeof (int64_t) * (pusharg + 1),
6987c478bd9Sstevel@tonic-gate 		    ap) != sizeof (int64_t) * (pusharg + 1))
6997c478bd9Sstevel@tonic-gate 			return (-1);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		for (i = 0, adp = argp; i < nargs; i++, adp++) {
7027c478bd9Sstevel@tonic-gate 			switch (i) {
7037c478bd9Sstevel@tonic-gate 			case 0:
7047c478bd9Sstevel@tonic-gate 				adp->arg_value =
7057c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RDI];
7067c478bd9Sstevel@tonic-gate 				break;
7077c478bd9Sstevel@tonic-gate 			case 1:
7087c478bd9Sstevel@tonic-gate 				adp->arg_value =
7097c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RSI];
7107c478bd9Sstevel@tonic-gate 				break;
7117c478bd9Sstevel@tonic-gate 			case 2:
7127c478bd9Sstevel@tonic-gate 				adp->arg_value =
7137c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RDX];
7147c478bd9Sstevel@tonic-gate 				break;
7157c478bd9Sstevel@tonic-gate 			case 3:
7167c478bd9Sstevel@tonic-gate 				adp->arg_value =
7177c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_RCX];
7187c478bd9Sstevel@tonic-gate 				break;
7197c478bd9Sstevel@tonic-gate 			case 4:
7207c478bd9Sstevel@tonic-gate 				adp->arg_value =
7217c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_R8];
7227c478bd9Sstevel@tonic-gate 				break;
7237c478bd9Sstevel@tonic-gate 			case 5:
7247c478bd9Sstevel@tonic-gate 				adp->arg_value =
7257c478bd9Sstevel@tonic-gate 				    P->status.pr_lwp.pr_reg[REG_R9];
7267c478bd9Sstevel@tonic-gate 				break;
7277c478bd9Sstevel@tonic-gate 			default:
7287c478bd9Sstevel@tonic-gate 				adp->arg_value = arglist[i - 6];
7297c478bd9Sstevel@tonic-gate 				break;
7307c478bd9Sstevel@tonic-gate 			}
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		return (0);
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	return (0);
7377c478bd9Sstevel@tonic-gate }
738