17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5d51e907ab * Common Development and Distribution License (the "License").
6d51e907ab * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22d9452f2Edward Pilatowicz * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#define	__sparcv9cpu
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate#include <sys/stack.h>
297c478bdstevel@tonic-gate#include <sys/regset.h>
307c478bdstevel@tonic-gate#include <sys/frame.h>
317c478bdstevel@tonic-gate#include <sys/sysmacros.h>
32d9452f2Edward Pilatowicz#include <sys/machelf.h>
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gate#include <stdlib.h>
357c478bdstevel@tonic-gate#include <unistd.h>
367c478bdstevel@tonic-gate#include <sys/types.h>
377c478bdstevel@tonic-gate#include <errno.h>
387c478bdstevel@tonic-gate#include <string.h>
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate#include "Pcontrol.h"
417c478bdstevel@tonic-gate#include "Pstack.h"
427c478bdstevel@tonic-gate#include "Pisadep.h"
437c478bdstevel@tonic-gate#include "P32ton.h"
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate#define	SYSCALL32 0x91d02008	/* 32-bit syscall (ta 8) instruction */
467c478bdstevel@tonic-gate#define	SYSCALL64 0x91d02040	/* 64-bit syscall (ta 64) instruction */
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gateconst char *
497c478bdstevel@tonic-gatePpltdest(struct ps_prochandle *P, uintptr_t pltaddr)
507c478bdstevel@tonic-gate{
517c478bdstevel@tonic-gate	map_info_t *mp = Paddr2mptr(P, pltaddr);
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gate	uintptr_t r_addr;
547c478bdstevel@tonic-gate	file_info_t *fp;
557c478bdstevel@tonic-gate	size_t i;
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate	if (mp == NULL || (fp = mp->map_file) == NULL ||
587c478bdstevel@tonic-gate	    fp->file_plt_base == 0 || pltaddr < fp->file_plt_base ||
597c478bdstevel@tonic-gate	    pltaddr >= fp->file_plt_base + fp->file_plt_size) {
607c478bdstevel@tonic-gate		errno = EINVAL;
617c478bdstevel@tonic-gate		return (NULL);
627c478bdstevel@tonic-gate	}
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate	if (P->status.pr_dmodel == PR_MODEL_LP64) {
657c478bdstevel@tonic-gate		Elf64_Rela	r;
667c478bdstevel@tonic-gate		uintptr_t	pltoff;
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate		pltoff = pltaddr - fp->file_plt_base;
69d9452f2Edward Pilatowicz		if (pltoff < (M64_PLT_NEARPLTS * M64_PLT_ENTSIZE)) {
707c478bdstevel@tonic-gate			i = (pltaddr - fp->file_plt_base -
71d9452f2Edward Pilatowicz			    M_PLT_XNumber * M64_PLT_ENTSIZE) / M64_PLT_ENTSIZE;
727c478bdstevel@tonic-gate		} else {
737c478bdstevel@tonic-gate			uintptr_t	pltblockoff;
74d9452f2Edward Pilatowicz			pltblockoff = pltoff - (M64_PLT_NEARPLTS *
75d9452f2Edward Pilatowicz			    M64_PLT_ENTSIZE);
76d9452f2Edward Pilatowicz			i = M64_PLT_NEARPLTS +
77d9452f2Edward Pilatowicz			    ((pltblockoff / M64_PLT_FBLOCKSZ) *
78d9452f2Edward Pilatowicz			    M64_PLT_FBLKCNTS) + ((pltblockoff %
79d9452f2Edward Pilatowicz			    M64_PLT_FBLOCKSZ) / M64_PLT_FENTSIZE) -
80d9452f2Edward Pilatowicz			    M_PLT_XNumber;
817c478bdstevel@tonic-gate		}
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gate		r_addr = fp->file_jmp_rel + i * sizeof (Elf64_Rela);
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
867c478bdstevel@tonic-gate		    (i = ELF64_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
877c478bdstevel@tonic-gate
88d51e907ab			Elf_Data *data = fp->file_dynsym.sym_data_pri;
897c478bdstevel@tonic-gate			Elf64_Sym *symp = &(((Elf64_Sym *)data->d_buf)[i]);
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate			return (fp->file_dynsym.sym_strs + symp->st_name);
927c478bdstevel@tonic-gate		}
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate	} else /* PR_MODEL_ILP32 */ {
957c478bdstevel@tonic-gate		Elf32_Rela r;
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate		i = (pltaddr - fp->file_plt_base -
98d9452f2Edward Pilatowicz		    M_PLT_XNumber * M32_PLT_ENTSIZE) / M32_PLT_ENTSIZE;
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gate		r_addr = fp->file_jmp_rel + i * sizeof (Elf32_Rela);
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
1037c478bdstevel@tonic-gate		    (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
1047c478bdstevel@tonic-gate
105d51e907ab			Elf_Data *data = fp->file_dynsym.sym_data_pri;
1067c478bdstevel@tonic-gate			Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate			return (fp->file_dynsym.sym_strs + symp->st_name);
1097c478bdstevel@tonic-gate		}
1107c478bdstevel@tonic-gate	}
1117c478bdstevel@tonic-gate
1127c478bdstevel@tonic-gate	return (NULL);
1137c478bdstevel@tonic-gate}
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gateint
1167c478bdstevel@tonic-gatePissyscall(struct ps_prochandle *P, uintptr_t addr)
1177c478bdstevel@tonic-gate{
1187c478bdstevel@tonic-gate	instr_t sysinstr;
1197c478bdstevel@tonic-gate	instr_t instr;
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate	if (P->status.pr_dmodel == PR_MODEL_LP64)
1227c478bdstevel@tonic-gate		sysinstr = SYSCALL64;
1237c478bdstevel@tonic-gate	else
1247c478bdstevel@tonic-gate		sysinstr = SYSCALL32;
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gate	if (Pread(P, &instr, sizeof (instr), addr) != sizeof (instr) ||
1277c478bdstevel@tonic-gate	    instr != sysinstr)
1287c478bdstevel@tonic-gate		return (0);
1297c478bdstevel@tonic-gate	else
1307c478bdstevel@tonic-gate		return (1);
1317c478bdstevel@tonic-gate}
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gateint
1347c478bdstevel@tonic-gatePissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
1357c478bdstevel@tonic-gate{
1367c478bdstevel@tonic-gate	uintptr_t prevaddr = addr - sizeof (instr_t);
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gate	if (Pissyscall(P, prevaddr)) {
1397c478bdstevel@tonic-gate		if (dst)
1407c478bdstevel@tonic-gate			*dst = prevaddr;
1417c478bdstevel@tonic-gate		return (1);
1427c478bdstevel@tonic-gate	}
1437c478bdstevel@tonic-gate
1447c478bdstevel@tonic-gate	return (0);
1457c478bdstevel@tonic-gate}
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate/* ARGSUSED */
1487c478bdstevel@tonic-gateint
1497c478bdstevel@tonic-gatePissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
1507c478bdstevel@tonic-gate{
1517c478bdstevel@tonic-gate	instr_t sysinstr;
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate	if (P->status.pr_dmodel == PR_MODEL_LP64)
1547c478bdstevel@tonic-gate		sysinstr = SYSCALL64;
1557c478bdstevel@tonic-gate	else
1567c478bdstevel@tonic-gate		sysinstr = SYSCALL32;
1577c478bdstevel@tonic-gate
1587c478bdstevel@tonic-gate	if (buflen >= sizeof (instr_t) &&
1597c478bdstevel@tonic-gate	    memcmp(buf, &sysinstr, sizeof (instr_t)) == 0)
1607c478bdstevel@tonic-gate		return (1);
1617c478bdstevel@tonic-gate	else
1627c478bdstevel@tonic-gate		return (0);
1637c478bdstevel@tonic-gate}
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate/*
1667c478bdstevel@tonic-gate * For gwindows_t support, we define a structure to pass arguments to
1677c478bdstevel@tonic-gate * a Plwp_iter() callback routine.
1687c478bdstevel@tonic-gate */
1697c478bdstevel@tonic-gatetypedef struct {
1707c478bdstevel@tonic-gate	struct ps_prochandle *gq_proc;	/* libproc handle */
1717c478bdstevel@tonic-gate	struct rwindow *gq_rwin;	/* rwindow destination buffer */
1727c478bdstevel@tonic-gate	uintptr_t gq_addr;		/* stack address to match */
1737c478bdstevel@tonic-gate} gwin_query_t;
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gatestatic int
1767c478bdstevel@tonic-gatefind_gwin(gwin_query_t *gqp, const lwpstatus_t *psp)
1777c478bdstevel@tonic-gate{
1787c478bdstevel@tonic-gate	gwindows_t gwin;
1797c478bdstevel@tonic-gate	struct stat64 st;
1807c478bdstevel@tonic-gate	char path[64];
1817c478bdstevel@tonic-gate	ssize_t n;
1827c478bdstevel@tonic-gate	int fd, i;
1837c478bdstevel@tonic-gate	int rv = 0; /* Return value for skip to next lwp */
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate	(void) snprintf(path, sizeof (path), "/proc/%d/lwp/%d/gwindows",
1867c478bdstevel@tonic-gate	    (int)gqp->gq_proc->pid, (int)psp->pr_lwpid);
1877c478bdstevel@tonic-gate
1887c478bdstevel@tonic-gate	if (stat64(path, &st) == -1 || st.st_size == 0)
1897c478bdstevel@tonic-gate		return (0); /* Nothing doing; skip to next lwp */
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gate	if ((fd = open64(path, O_RDONLY)) >= 0) {
1927c478bdstevel@tonic-gate		/*
1937c478bdstevel@tonic-gate		 * Zero out the gwindows_t because the gwindows file only has
1947c478bdstevel@tonic-gate		 * as much data as needed to represent the saved windows.
1957c478bdstevel@tonic-gate		 */
1967c478bdstevel@tonic-gate		if (gqp->gq_proc->status.pr_dmodel == PR_MODEL_ILP32) {
1977c478bdstevel@tonic-gate			gwindows32_t g32;
1987c478bdstevel@tonic-gate
1997c478bdstevel@tonic-gate			(void) memset(&g32, 0, sizeof (g32));
2007c478bdstevel@tonic-gate			if ((n = read(fd, &g32, sizeof (g32))) > 0)
2017c478bdstevel@tonic-gate				gwindows_32_to_n(&g32, &gwin);
2027c478bdstevel@tonic-gate
2037c478bdstevel@tonic-gate		} else {
2047c478bdstevel@tonic-gate			(void) memset(&gwin, 0, sizeof (gwin));
2057c478bdstevel@tonic-gate			n = read(fd, &gwin, sizeof (gwin));
2067c478bdstevel@tonic-gate		}
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate		if (n > 0) {
2097c478bdstevel@tonic-gate			/*
2107c478bdstevel@tonic-gate			 * If we actually found a non-zero gwindows file and
2117c478bdstevel@tonic-gate			 * were able to read it, iterate through the buffers
2127c478bdstevel@tonic-gate			 * looking for a stack pointer match; if one is found,
2137c478bdstevel@tonic-gate			 * copy out the corresponding register window.
2147c478bdstevel@tonic-gate			 */
2157c478bdstevel@tonic-gate			for (i = 0; i < gwin.wbcnt; i++) {
2167c478bdstevel@tonic-gate				if (gwin.spbuf[i] == (greg_t *)gqp->gq_addr) {
2177c478bdstevel@tonic-gate					(void) memcpy(gqp->gq_rwin,
2187c478bdstevel@tonic-gate					    &gwin.wbuf[i],
2197c478bdstevel@tonic-gate					    sizeof (struct rwindow));
2207c478bdstevel@tonic-gate
2217c478bdstevel@tonic-gate					rv = 1; /* We're done */
2227c478bdstevel@tonic-gate					break;
2237c478bdstevel@tonic-gate				}
2247c478bdstevel@tonic-gate			}
2257c478bdstevel@tonic-gate		}
2267c478bdstevel@tonic-gate		(void) close(fd);
2277c478bdstevel@tonic-gate	}
2287c478bdstevel@tonic-gate
2297c478bdstevel@tonic-gate	return (rv);
2307c478bdstevel@tonic-gate}
2317c478bdstevel@tonic-gate
2327c478bdstevel@tonic-gatestatic int
2337c478bdstevel@tonic-gateread_gwin(struct ps_prochandle *P, struct rwindow *rwp, uintptr_t sp)
2347c478bdstevel@tonic-gate{
2357c478bdstevel@tonic-gate	gwin_query_t gq;
2367c478bdstevel@tonic-gate
2377c478bdstevel@tonic-gate	if (P->state == PS_DEAD) {
23869a119cChristopher Siden		core_info_t *core = P->data;
23969a119cChristopher Siden		lwp_info_t *lwp = list_next(&core->core_lwp_head);
2407c478bdstevel@tonic-gate		uint_t n;
2417c478bdstevel@tonic-gate		int i;
2427c478bdstevel@tonic-gate
24369a119cChristopher Siden		for (n = 0; n < core->core_nlwp; n++, lwp = list_next(lwp)) {
2447c478bdstevel@tonic-gate			gwindows_t *gwin = lwp->lwp_gwins;
2457c478bdstevel@tonic-gate
2467c478bdstevel@tonic-gate			if (gwin == NULL)
2477c478bdstevel@tonic-gate				continue; /* No gwindows for this lwp */
2487c478bdstevel@tonic-gate
2497c478bdstevel@tonic-gate			/*
2507c478bdstevel@tonic-gate			 * If this lwp has gwindows associated with it, iterate
2517c478bdstevel@tonic-gate			 * through the buffers looking for a stack pointer
2527c478bdstevel@tonic-gate			 * match; if one is found, copy out the register window.
2537c478bdstevel@tonic-gate			 */
2547c478bdstevel@tonic-gate			for (i = 0; i < gwin->wbcnt; i++) {
2557c478bdstevel@tonic-gate				if (gwin->spbuf[i] == (greg_t *)sp) {
2567c478bdstevel@tonic-gate					(void) memcpy(rwp, &gwin->wbuf[i],
2577c478bdstevel@tonic-gate					    sizeof (struct rwindow));
2587c478bdstevel@tonic-gate					return (0); /* We're done */
2597c478bdstevel@tonic-gate				}
2607c478bdstevel@tonic-gate			}
2617c478bdstevel@tonic-gate		}
2627c478bdstevel@tonic-gate
2637c478bdstevel@tonic-gate		return (-1); /* No gwindows match found */
2647c478bdstevel@tonic-gate
2657c478bdstevel@tonic-gate	}
2667c478bdstevel@tonic-gate
2677c478bdstevel@tonic-gate	gq.gq_proc = P;
2687c478bdstevel@tonic-gate	gq.gq_rwin = rwp;
2697c478bdstevel@tonic-gate	gq.gq_addr = sp;
2707c478bdstevel@tonic-gate
2717c478bdstevel@tonic-gate	return (Plwp_iter(P, (proc_lwp_f *)find_gwin, &gq) ? 0 : -1);
2727c478bdstevel@tonic-gate}
2737c478bdstevel@tonic-gate
2747c478bdstevel@tonic-gatestatic void
2757c478bdstevel@tonic-gateucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
2767c478bdstevel@tonic-gate{
2777c478bdstevel@tonic-gate	const greg_t *gregs = &src->uc_mcontext.gregs[0];
2787c478bdstevel@tonic-gate
2797c478bdstevel@tonic-gate	dst[R_CCR] = gregs[REG_CCR];
2807c478bdstevel@tonic-gate	dst[R_ASI] = gregs[REG_ASI];
2817c478bdstevel@tonic-gate	dst[R_FPRS] = gregs[REG_FPRS];
2827c478bdstevel@tonic-gate	dst[R_PC] = gregs[REG_PC];
2837c478bdstevel@tonic-gate	dst[R_nPC] = gregs[REG_nPC];
2847c478bdstevel@tonic-gate	dst[R_Y] = gregs[REG_Y];
2857c478bdstevel@tonic-gate
2867c478bdstevel@tonic-gate	dst[R_G1] = gregs[REG_G1];
2877c478bdstevel@tonic-gate	dst[R_G2] = gregs[REG_G2];
2887c478bdstevel@tonic-gate	dst[R_G3] = gregs[REG_G3];
2897c478bdstevel@tonic-gate	dst[R_G4] = gregs[REG_G4];
2907c478bdstevel@tonic-gate	dst[R_G5] = gregs[REG_G5];
2917c478bdstevel@tonic-gate	dst[R_G6] = gregs[REG_G6];
2927c478bdstevel@tonic-gate	dst[R_G7] = gregs[REG_G7];
2937c478bdstevel@tonic-gate
2947c478bdstevel@tonic-gate	dst[R_O0] = gregs[REG_O0];
2957c478bdstevel@tonic-gate	dst[R_O1] = gregs[REG_O1];
2967c478bdstevel@tonic-gate	dst[R_O2] = gregs[REG_O2];
2977c478bdstevel@tonic-gate	dst[R_O3] = gregs[REG_O3];
2987c478bdstevel@tonic-gate	dst[R_O4] = gregs[REG_O4];
2997c478bdstevel@tonic-gate	dst[R_O5] = gregs[REG_O5];
3007c478bdstevel@tonic-gate	dst[R_O6] = gregs[REG_O6];
3017c478bdstevel@tonic-gate	dst[R_O7] = gregs[REG_O7];
3027c478bdstevel@tonic-gate}
3037c478bdstevel@tonic-gate
3047c478bdstevel@tonic-gatestatic void
3057c478bdstevel@tonic-gateucontext_32_to_prgregs(const ucontext32_t *src, prgregset_t dst)
3067c478bdstevel@tonic-gate{
3077c478bdstevel@tonic-gate	/*
3087c478bdstevel@tonic-gate	 * We need to be very careful here to cast the greg32_t's (signed) to
3097c478bdstevel@tonic-gate	 * unsigned and then explicitly promote them as unsigned values.
3107c478bdstevel@tonic-gate	 */
3117c478bdstevel@tonic-gate	const greg32_t *gregs = &src->uc_mcontext.gregs[0];
3127c478bdstevel@tonic-gate
3137c478bdstevel@tonic-gate	dst[R_PSR] = (uint64_t)(uint32_t)gregs[REG_PSR];
3147c478bdstevel@tonic-gate	dst[R_PC] = (uint64_t)(uint32_t)gregs[REG_PC];
3157c478bdstevel@tonic-gate	dst[R_nPC] = (uint64_t)(uint32_t)gregs[REG_nPC];
3167c478bdstevel@tonic-gate	dst[R_Y] = (uint64_t)(uint32_t)gregs[REG_Y];
3177c478bdstevel@tonic-gate
3187c478bdstevel@tonic-gate	dst[R_G1] = (uint64_t)(uint32_t)gregs[REG_G1];
3197c478bdstevel@tonic-gate	dst[R_G2] = (uint64_t)(uint32_t)gregs[REG_G2];
3207c478bdstevel@tonic-gate	dst[R_G3] = (uint64_t)(uint32_t)gregs[REG_G3];
3217c478bdstevel@tonic-gate	dst[R_G4] = (uint64_t)(uint32_t)gregs[REG_G4];
3227c478bdstevel@tonic-gate	dst[R_G5] = (uint64_t)(uint32_t)gregs[REG_G5];
3237c478bdstevel@tonic-gate	dst[R_G6] = (uint64_t)(uint32_t)gregs[REG_G6];
3247c478bdstevel@tonic-gate	dst[R_G7] = (uint64_t)(uint32_t)gregs[REG_G7];
3257c478bdstevel@tonic-gate
3267c478bdstevel@tonic-gate	dst[R_O0] = (uint64_t)(uint32_t)gregs[REG_O0];
3277c478bdstevel@tonic-gate	dst[R_O1] = (uint64_t)(uint32_t)gregs[REG_O1];
3287c478bdstevel@tonic-gate	dst[R_O2] = (uint64_t)(uint32_t)gregs[REG_O2];
3297c478bdstevel@tonic-gate	dst[R_O3] = (uint64_t)(uint32_t)gregs[REG_O3];
3307c478bdstevel@tonic-gate	dst[R_O4] = (uint64_t)(uint32_t)gregs[REG_O4];
3317c478bdstevel@tonic-gate	dst[R_O5] = (uint64_t)(uint32_t)gregs[REG_O5];
3327c478bdstevel@tonic-gate	dst[R_O6] = (uint64_t)(uint32_t)gregs[REG_O6];
3337c478bdstevel@tonic-gate	dst[R_O7] = (uint64_t)(uint32_t)gregs[REG_O7];
3347c478bdstevel@tonic-gate}
3357c478bdstevel@tonic-gate
3367c478bdstevel@tonic-gateint
3377c478bdstevel@tonic-gatePstack_iter(struct ps_prochandle *P, const prgregset_t regs,
3387c478bdstevel@tonic-gate	proc_stack_f *func, void *arg)
3397c478bdstevel@tonic-gate{
3407c478bdstevel@tonic-gate	prgreg_t *prevfp = NULL;
3417c478bdstevel@tonic-gate	uint_t pfpsize = 0;
3427c478bdstevel@tonic-gate	int nfp = 0;
3437c478bdstevel@tonic-gate	prgregset_t gregs;
3447c478bdstevel@tonic-gate	long args[6];
3457c478bdstevel@tonic-gate	prgreg_t fp;
3467c478bdstevel@tonic-gate	int i;
3477c478bdstevel@tonic-gate	int rv;
3487c478bdstevel@tonic-gate	uintptr_t sp;
3497c478bdstevel@tonic-gate	ssize_t n;
3507c478bdstevel@tonic-gate	uclist_t ucl;
3517c478bdstevel@tonic-gate	ucontext_t uc;
3527c478bdstevel@tonic-gate
3537c478bdstevel@tonic-gate	init_uclist(&ucl, P);
3547c478bdstevel@tonic-gate	(void) memcpy(gregs, regs, sizeof (gregs));
3557c478bdstevel@tonic-gate
3567c478bdstevel@tonic-gate	for (;;) {
3577c478bdstevel@tonic-gate		fp = gregs[R_FP];
3587c478bdstevel@tonic-gate		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
3597c478bdstevel@tonic-gate			break;
3607c478bdstevel@tonic-gate
3617c478bdstevel@tonic-gate		for (i = 0; i < 6; i++)
3627c478bdstevel@tonic-gate			args[i] = gregs[R_I0 + i];
3637c478bdstevel@tonic-gate		if ((rv = func(arg, gregs, 6, args)) != 0)
3647c478bdstevel@tonic-gate			break;
3657c478bdstevel@tonic-gate
3667c478bdstevel@tonic-gate		gregs[R_PC] = gregs[R_I7];
3677c478bdstevel@tonic-gate		gregs[R_nPC] = gregs[R_PC] + 4;
3687c478bdstevel@tonic-gate		(void) memcpy(&gregs[R_O0], &gregs[R_I0], 8*sizeof (prgreg_t));
3697c478bdstevel@tonic-gate		if ((sp = gregs[R_FP]) == 0)
3707c478bdstevel@tonic-gate			break;
3717c478bdstevel@tonic-gate
3727c478bdstevel@tonic-gate		if (P->status.pr_dmodel == PR_MODEL_ILP32) {
3737c478bdstevel@tonic-gate			struct rwindow32 rw32;
3747c478bdstevel@tonic-gate			ucontext32_t uc32;
3757c478bdstevel@tonic-gate
3767c478bdstevel@tonic-gate			if (find_uclink(&ucl, sp +
3777c478bdstevel@tonic-gate			    SA32(sizeof (struct frame32))) &&
3787c478bdstevel@tonic-gate			    Pread(P, &uc32, sizeof (uc32), sp +
3797c478bdstevel@tonic-gate			    SA32(sizeof (struct frame32))) == sizeof (uc32)) {
3807c478bdstevel@tonic-gate				ucontext_32_to_prgregs(&uc32, gregs);
3817c478bdstevel@tonic-gate				sp = gregs[R_SP];
3827c478bdstevel@tonic-gate			}
3837c478bdstevel@tonic-gate
3847c478bdstevel@tonic-gate			n = Pread(P, &rw32, sizeof (struct rwindow32), sp);
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gate			if (n == sizeof (struct rwindow32)) {
3877c478bdstevel@tonic-gate				rwindow_32_to_n(&rw32,
3887c478bdstevel@tonic-gate				    (struct rwindow *)&gregs[R_L0]);
3897c478bdstevel@tonic-gate				continue;
3907c478bdstevel@tonic-gate			}
3917c478bdstevel@tonic-gate
3927c478bdstevel@tonic-gate		} else {
3937c478bdstevel@tonic-gate			sp += STACK_BIAS;
3947c478bdstevel@tonic-gate
3957c478bdstevel@tonic-gate			if (find_uclink(&ucl, sp + SA(sizeof (struct frame))) &&
3967c478bdstevel@tonic-gate			    Pread(P, &uc, sizeof (uc), sp +
3977c478bdstevel@tonic-gate			    SA(sizeof (struct frame))) == sizeof (uc)) {
3987c478bdstevel@tonic-gate				ucontext_n_to_prgregs(&uc, gregs);
3997c478bdstevel@tonic-gate				sp = gregs[R_SP] + STACK_BIAS;
4007c478bdstevel@tonic-gate			}
4017c478bdstevel@tonic-gate
4027c478bdstevel@tonic-gate			n = Pread(P, &gregs[R_L0], sizeof (struct rwindow), sp);
4037c478bdstevel@tonic-gate
4047c478bdstevel@tonic-gate			if (n == sizeof (struct rwindow))
4057c478bdstevel@tonic-gate				continue;
4067c478bdstevel@tonic-gate		}
4077c478bdstevel@tonic-gate
4087c478bdstevel@tonic-gate		/*
4097c478bdstevel@tonic-gate		 * If we get here, then our Pread of the register window
4107c478bdstevel@tonic-gate		 * failed.  If this is because the address was not mapped,
4117c478bdstevel@tonic-gate		 * then we attempt to read this window via any gwindows
4127c478bdstevel@tonic-gate		 * information we have.  If that too fails, abort our loop.
4137c478bdstevel@tonic-gate		 */
4147c478bdstevel@tonic-gate		if (n > 0)
4157c478bdstevel@tonic-gate			break;	/* Failed for reason other than not mapped */
4167c478bdstevel@tonic-gate
4177c478bdstevel@tonic-gate		if (read_gwin(P, (struct rwindow *)&gregs[R_L0], sp) == -1)
4187c478bdstevel@tonic-gate			break;	/* No gwindows match either */
4197c478bdstevel@tonic-gate	}
4207c478bdstevel@tonic-gate
4217c478bdstevel@tonic-gate	if (prevfp)
4227c478bdstevel@tonic-gate		free(prevfp);
4237c478bdstevel@tonic-gate
4247c478bdstevel@tonic-gate	free_uclist(&ucl);
4257c478bdstevel@tonic-gate	return (rv);
4267c478bdstevel@tonic-gate}
4277c478bdstevel@tonic-gate
4287c478bdstevel@tonic-gateuintptr_t
4297c478bdstevel@tonic-gatePsyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
4307c478bdstevel@tonic-gate{
4317c478bdstevel@tonic-gate	uintptr_t ret;
4327c478bdstevel@tonic-gate	int model = P->status.pr_dmodel;
4337c478bdstevel@tonic-gate
4347c478bdstevel@tonic-gate	if (model == PR_MODEL_LP64) {
4357c478bdstevel@tonic-gate		sp -= (nargs > 6)?
436d9452f2Edward Pilatowicz		    WINDOWSIZE64 + sizeof (int64_t) * nargs :
437d9452f2Edward Pilatowicz		    WINDOWSIZE64 + sizeof (int64_t) * 6;
4387c478bdstevel@tonic-gate		sp = PSTACK_ALIGN64(sp);
4397c478bdstevel@tonic-gate		ret = sp + WINDOWSIZE32 + sizeof (int32_t);
4407c478bdstevel@tonic-gate	} else {
4417c478bdstevel@tonic-gate		sp -= (nargs > 6)?
442d9452f2Edward Pilatowicz		    WINDOWSIZE32 + sizeof (int32_t) * (1 + nargs) :
443d9452f2Edward Pilatowicz		    WINDOWSIZE32 + sizeof (int32_t) * (1 + 6);
4447c478bdstevel@tonic-gate		sp = PSTACK_ALIGN32(sp);
4457c478bdstevel@tonic-gate		ret = sp + WINDOWSIZE64 + sizeof (int32_t);
4467c478bdstevel@tonic-gate	}
4477c478bdstevel@tonic-gate
4487c478bdstevel@tonic-gate	P->status.pr_lwp.pr_reg[R_G1] = sysindex;
4497c478bdstevel@tonic-gate	if (model == PR_MODEL_LP64)
4507c478bdstevel@tonic-gate		P->status.pr_lwp.pr_reg[R_SP] = sp - STACK_BIAS;
4517c478bdstevel@tonic-gate	else
4527c478bdstevel@tonic-gate		P->status.pr_lwp.pr_reg[R_SP] = sp;
4537c478bdstevel@tonic-gate	P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr;
4547c478bdstevel@tonic-gate	P->status.pr_lwp.pr_reg[R_nPC] = P->sysaddr + sizeof (instr_t);
4557c478bdstevel@tonic-gate
4567c478bdstevel@tonic-gate	return (ret);
4577c478bdstevel@tonic-gate}
4587c478bdstevel@tonic-gate
4597c478bdstevel@tonic-gateint
4607c478bdstevel@tonic-gatePsyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
4617c478bdstevel@tonic-gate    uintptr_t ap)
4627c478bdstevel@tonic-gate{
4637c478bdstevel@tonic-gate	uint32_t arglist32[MAXARGS+2];
4647c478bdstevel@tonic-gate	uint64_t arglist64[MAXARGS+2];
4657c478bdstevel@tonic-gate	int i;
4667c478bdstevel@tonic-gate	argdes_t *adp;
4677c478bdstevel@tonic-gate	int model = P->status.pr_dmodel;
4687c478bdstevel@tonic-gate
4697c478bdstevel@tonic-gate	for (i = 0, adp = argp; i < nargs; i++, adp++) {
4707c478bdstevel@tonic-gate		arglist32[i] = (uint32_t)adp->arg_value;
4717c478bdstevel@tonic-gate		arglist64[i] = (uint64_t)adp->arg_value;
4727c478bdstevel@tonic-gate
4737c478bdstevel@tonic-gate		if (i < 6)
4747c478bdstevel@tonic-gate			(void) Pputareg(P, R_O0+i, adp->arg_value);
4757c478bdstevel@tonic-gate	}
4767c478bdstevel@tonic-gate
4777c478bdstevel@tonic-gate	if (model == PR_MODEL_LP64) {
4787c478bdstevel@tonic-gate		if (nargs > 6 &&
4797c478bdstevel@tonic-gate		    Pwrite(P, &arglist64[0], sizeof (int64_t) * nargs,
4807c478bdstevel@tonic-gate		    (uintptr_t)ap) != sizeof (int64_t) * nargs)
4817c478bdstevel@tonic-gate			return (-1);
4827c478bdstevel@tonic-gate	} else {
4837c478bdstevel@tonic-gate		if (nargs > 6 &&
4847c478bdstevel@tonic-gate		    Pwrite(P, &arglist32[0], sizeof (int32_t) * nargs,
4857c478bdstevel@tonic-gate		    (uintptr_t)ap) != sizeof (int32_t) * nargs)
4867c478bdstevel@tonic-gate			return (-1);
4877c478bdstevel@tonic-gate	}
4887c478bdstevel@tonic-gate
4897c478bdstevel@tonic-gate	return (0);
4907c478bdstevel@tonic-gate}
4917c478bdstevel@tonic-gate
4927c478bdstevel@tonic-gate/* ARGSUSED */
4937c478bdstevel@tonic-gateint
4947c478bdstevel@tonic-gatePsyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
4957c478bdstevel@tonic-gate    uintptr_t ap)
4967c478bdstevel@tonic-gate{
4977c478bdstevel@tonic-gate	/* Do nothing */
4987c478bdstevel@tonic-gate	return (0);
4997c478bdstevel@tonic-gate}
500