xref: /onnv-gate/usr/src/lib/libproc/sparcv9/Pisadep.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #define	__sparcv9cpu
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <sys/stack.h>
32*0Sstevel@tonic-gate #include <sys/regset.h>
33*0Sstevel@tonic-gate #include <sys/frame.h>
34*0Sstevel@tonic-gate #include <sys/sysmacros.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <unistd.h>
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <errno.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include "Pcontrol.h"
43*0Sstevel@tonic-gate #include "Pstack.h"
44*0Sstevel@tonic-gate #include "Pisadep.h"
45*0Sstevel@tonic-gate #include "P32ton.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #define	M_PLT32_NRSV		4	/* reserved PLT entries */
48*0Sstevel@tonic-gate #define	M_PLT32_ENTSIZE		12	/* size of each PLT entry */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	M_PLT64_NRSV		4	/* reserved bit PLT entries */
51*0Sstevel@tonic-gate #define	M_PLT64_ENTSIZE		32	/* size of each PLT entry */
52*0Sstevel@tonic-gate #define	M_PLT64_FENTSIZE	24	/* size of far PLT entry */
53*0Sstevel@tonic-gate #define	M_PLT64_NEARPLTS	0x8000	/* # of NEAR PLTS we can have */
54*0Sstevel@tonic-gate #define	M_PLT64_FBLKCNTS	160	/* # of plts in far PLT blocks */
55*0Sstevel@tonic-gate #define	M_PLT64_FBLOCKSZ	(M_PLT64_FBLKCNTS * \
56*0Sstevel@tonic-gate 				(M_PLT64_FENTSIZE + sizeof (uint64_t)))
57*0Sstevel@tonic-gate 					/* size of far PLT block */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #define	SYSCALL32 0x91d02008	/* 32-bit syscall (ta 8) instruction */
60*0Sstevel@tonic-gate #define	SYSCALL64 0x91d02040	/* 64-bit syscall (ta 64) instruction */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate const char *
63*0Sstevel@tonic-gate Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr)
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate 	map_info_t *mp = Paddr2mptr(P, pltaddr);
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	uintptr_t r_addr;
68*0Sstevel@tonic-gate 	file_info_t *fp;
69*0Sstevel@tonic-gate 	size_t i;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	if (mp == NULL || (fp = mp->map_file) == NULL ||
72*0Sstevel@tonic-gate 	    fp->file_plt_base == 0 || pltaddr < fp->file_plt_base ||
73*0Sstevel@tonic-gate 	    pltaddr >= fp->file_plt_base + fp->file_plt_size) {
74*0Sstevel@tonic-gate 		errno = EINVAL;
75*0Sstevel@tonic-gate 		return (NULL);
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
79*0Sstevel@tonic-gate 		Elf64_Rela	r;
80*0Sstevel@tonic-gate 		uintptr_t	pltoff;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 		pltoff = pltaddr - fp->file_plt_base;
83*0Sstevel@tonic-gate 		if (pltoff < (M_PLT64_NEARPLTS * M_PLT64_ENTSIZE)) {
84*0Sstevel@tonic-gate 			i = (pltaddr - fp->file_plt_base -
85*0Sstevel@tonic-gate 			    M_PLT64_NRSV * M_PLT64_ENTSIZE) / M_PLT64_ENTSIZE;
86*0Sstevel@tonic-gate 		} else {
87*0Sstevel@tonic-gate 			uintptr_t	pltblockoff;
88*0Sstevel@tonic-gate 			pltblockoff = pltoff - (M_PLT64_NEARPLTS *
89*0Sstevel@tonic-gate 				M_PLT64_ENTSIZE);
90*0Sstevel@tonic-gate 			i = M_PLT64_NEARPLTS +
91*0Sstevel@tonic-gate 				((pltblockoff / M_PLT64_FBLOCKSZ) *
92*0Sstevel@tonic-gate 				M_PLT64_FBLKCNTS) + ((pltblockoff %
93*0Sstevel@tonic-gate 				M_PLT64_FBLOCKSZ) / M_PLT64_FENTSIZE) -
94*0Sstevel@tonic-gate 				M_PLT64_NRSV;
95*0Sstevel@tonic-gate 		}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 		r_addr = fp->file_jmp_rel + i * sizeof (Elf64_Rela);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
100*0Sstevel@tonic-gate 		    (i = ELF64_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 			Elf_Data *data = fp->file_dynsym.sym_data;
103*0Sstevel@tonic-gate 			Elf64_Sym *symp = &(((Elf64_Sym *)data->d_buf)[i]);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 			return (fp->file_dynsym.sym_strs + symp->st_name);
106*0Sstevel@tonic-gate 		}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	} else /* PR_MODEL_ILP32 */ {
109*0Sstevel@tonic-gate 		Elf32_Rela r;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 		i = (pltaddr - fp->file_plt_base -
112*0Sstevel@tonic-gate 		    M_PLT32_NRSV * M_PLT32_ENTSIZE) / M_PLT32_ENTSIZE;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		r_addr = fp->file_jmp_rel + i * sizeof (Elf32_Rela);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
117*0Sstevel@tonic-gate 		    (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 			Elf_Data *data = fp->file_dynsym.sym_data;
120*0Sstevel@tonic-gate 			Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 			return (fp->file_dynsym.sym_strs + symp->st_name);
123*0Sstevel@tonic-gate 		}
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	return (NULL);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate int
130*0Sstevel@tonic-gate Pissyscall(struct ps_prochandle *P, uintptr_t addr)
131*0Sstevel@tonic-gate {
132*0Sstevel@tonic-gate 	instr_t sysinstr;
133*0Sstevel@tonic-gate 	instr_t instr;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64)
136*0Sstevel@tonic-gate 		sysinstr = SYSCALL64;
137*0Sstevel@tonic-gate 	else
138*0Sstevel@tonic-gate 		sysinstr = SYSCALL32;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (Pread(P, &instr, sizeof (instr), addr) != sizeof (instr) ||
141*0Sstevel@tonic-gate 	    instr != sysinstr)
142*0Sstevel@tonic-gate 		return (0);
143*0Sstevel@tonic-gate 	else
144*0Sstevel@tonic-gate 		return (1);
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate int
148*0Sstevel@tonic-gate Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate 	uintptr_t prevaddr = addr - sizeof (instr_t);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	if (Pissyscall(P, prevaddr)) {
153*0Sstevel@tonic-gate 		if (dst)
154*0Sstevel@tonic-gate 			*dst = prevaddr;
155*0Sstevel@tonic-gate 		return (1);
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	return (0);
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate /* ARGSUSED */
162*0Sstevel@tonic-gate int
163*0Sstevel@tonic-gate Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	instr_t sysinstr;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64)
168*0Sstevel@tonic-gate 		sysinstr = SYSCALL64;
169*0Sstevel@tonic-gate 	else
170*0Sstevel@tonic-gate 		sysinstr = SYSCALL32;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	if (buflen >= sizeof (instr_t) &&
173*0Sstevel@tonic-gate 	    memcmp(buf, &sysinstr, sizeof (instr_t)) == 0)
174*0Sstevel@tonic-gate 		return (1);
175*0Sstevel@tonic-gate 	else
176*0Sstevel@tonic-gate 		return (0);
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate  * For gwindows_t support, we define a structure to pass arguments to
181*0Sstevel@tonic-gate  * a Plwp_iter() callback routine.
182*0Sstevel@tonic-gate  */
183*0Sstevel@tonic-gate typedef struct {
184*0Sstevel@tonic-gate 	struct ps_prochandle *gq_proc;	/* libproc handle */
185*0Sstevel@tonic-gate 	struct rwindow *gq_rwin;	/* rwindow destination buffer */
186*0Sstevel@tonic-gate 	uintptr_t gq_addr;		/* stack address to match */
187*0Sstevel@tonic-gate } gwin_query_t;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate static int
190*0Sstevel@tonic-gate find_gwin(gwin_query_t *gqp, const lwpstatus_t *psp)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	gwindows_t gwin;
193*0Sstevel@tonic-gate 	struct stat64 st;
194*0Sstevel@tonic-gate 	char path[64];
195*0Sstevel@tonic-gate 	ssize_t n;
196*0Sstevel@tonic-gate 	int fd, i;
197*0Sstevel@tonic-gate 	int rv = 0; /* Return value for skip to next lwp */
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "/proc/%d/lwp/%d/gwindows",
200*0Sstevel@tonic-gate 	    (int)gqp->gq_proc->pid, (int)psp->pr_lwpid);
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	if (stat64(path, &st) == -1 || st.st_size == 0)
203*0Sstevel@tonic-gate 		return (0); /* Nothing doing; skip to next lwp */
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	if ((fd = open64(path, O_RDONLY)) >= 0) {
206*0Sstevel@tonic-gate 		/*
207*0Sstevel@tonic-gate 		 * Zero out the gwindows_t because the gwindows file only has
208*0Sstevel@tonic-gate 		 * as much data as needed to represent the saved windows.
209*0Sstevel@tonic-gate 		 */
210*0Sstevel@tonic-gate 		if (gqp->gq_proc->status.pr_dmodel == PR_MODEL_ILP32) {
211*0Sstevel@tonic-gate 			gwindows32_t g32;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 			(void) memset(&g32, 0, sizeof (g32));
214*0Sstevel@tonic-gate 			if ((n = read(fd, &g32, sizeof (g32))) > 0)
215*0Sstevel@tonic-gate 				gwindows_32_to_n(&g32, &gwin);
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 		} else {
218*0Sstevel@tonic-gate 			(void) memset(&gwin, 0, sizeof (gwin));
219*0Sstevel@tonic-gate 			n = read(fd, &gwin, sizeof (gwin));
220*0Sstevel@tonic-gate 		}
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 		if (n > 0) {
223*0Sstevel@tonic-gate 			/*
224*0Sstevel@tonic-gate 			 * If we actually found a non-zero gwindows file and
225*0Sstevel@tonic-gate 			 * were able to read it, iterate through the buffers
226*0Sstevel@tonic-gate 			 * looking for a stack pointer match; if one is found,
227*0Sstevel@tonic-gate 			 * copy out the corresponding register window.
228*0Sstevel@tonic-gate 			 */
229*0Sstevel@tonic-gate 			for (i = 0; i < gwin.wbcnt; i++) {
230*0Sstevel@tonic-gate 				if (gwin.spbuf[i] == (greg_t *)gqp->gq_addr) {
231*0Sstevel@tonic-gate 					(void) memcpy(gqp->gq_rwin,
232*0Sstevel@tonic-gate 					    &gwin.wbuf[i],
233*0Sstevel@tonic-gate 					    sizeof (struct rwindow));
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 					rv = 1; /* We're done */
236*0Sstevel@tonic-gate 					break;
237*0Sstevel@tonic-gate 				}
238*0Sstevel@tonic-gate 			}
239*0Sstevel@tonic-gate 		}
240*0Sstevel@tonic-gate 		(void) close(fd);
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	return (rv);
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate static int
247*0Sstevel@tonic-gate read_gwin(struct ps_prochandle *P, struct rwindow *rwp, uintptr_t sp)
248*0Sstevel@tonic-gate {
249*0Sstevel@tonic-gate 	gwin_query_t gq;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
252*0Sstevel@tonic-gate 		lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
253*0Sstevel@tonic-gate 		uint_t n;
254*0Sstevel@tonic-gate 		int i;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 		for (n = 0; n < P->core->core_nlwp; n++, lwp = list_next(lwp)) {
257*0Sstevel@tonic-gate 			gwindows_t *gwin = lwp->lwp_gwins;
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 			if (gwin == NULL)
260*0Sstevel@tonic-gate 				continue; /* No gwindows for this lwp */
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 			/*
263*0Sstevel@tonic-gate 			 * If this lwp has gwindows associated with it, iterate
264*0Sstevel@tonic-gate 			 * through the buffers looking for a stack pointer
265*0Sstevel@tonic-gate 			 * match; if one is found, copy out the register window.
266*0Sstevel@tonic-gate 			 */
267*0Sstevel@tonic-gate 			for (i = 0; i < gwin->wbcnt; i++) {
268*0Sstevel@tonic-gate 				if (gwin->spbuf[i] == (greg_t *)sp) {
269*0Sstevel@tonic-gate 					(void) memcpy(rwp, &gwin->wbuf[i],
270*0Sstevel@tonic-gate 					    sizeof (struct rwindow));
271*0Sstevel@tonic-gate 					return (0); /* We're done */
272*0Sstevel@tonic-gate 				}
273*0Sstevel@tonic-gate 			}
274*0Sstevel@tonic-gate 		}
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 		return (-1); /* No gwindows match found */
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	gq.gq_proc = P;
281*0Sstevel@tonic-gate 	gq.gq_rwin = rwp;
282*0Sstevel@tonic-gate 	gq.gq_addr = sp;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	return (Plwp_iter(P, (proc_lwp_f *)find_gwin, &gq) ? 0 : -1);
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate static void
288*0Sstevel@tonic-gate ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
289*0Sstevel@tonic-gate {
290*0Sstevel@tonic-gate 	const greg_t *gregs = &src->uc_mcontext.gregs[0];
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	dst[R_CCR] = gregs[REG_CCR];
293*0Sstevel@tonic-gate 	dst[R_ASI] = gregs[REG_ASI];
294*0Sstevel@tonic-gate 	dst[R_FPRS] = gregs[REG_FPRS];
295*0Sstevel@tonic-gate 	dst[R_PC] = gregs[REG_PC];
296*0Sstevel@tonic-gate 	dst[R_nPC] = gregs[REG_nPC];
297*0Sstevel@tonic-gate 	dst[R_Y] = gregs[REG_Y];
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	dst[R_G1] = gregs[REG_G1];
300*0Sstevel@tonic-gate 	dst[R_G2] = gregs[REG_G2];
301*0Sstevel@tonic-gate 	dst[R_G3] = gregs[REG_G3];
302*0Sstevel@tonic-gate 	dst[R_G4] = gregs[REG_G4];
303*0Sstevel@tonic-gate 	dst[R_G5] = gregs[REG_G5];
304*0Sstevel@tonic-gate 	dst[R_G6] = gregs[REG_G6];
305*0Sstevel@tonic-gate 	dst[R_G7] = gregs[REG_G7];
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	dst[R_O0] = gregs[REG_O0];
308*0Sstevel@tonic-gate 	dst[R_O1] = gregs[REG_O1];
309*0Sstevel@tonic-gate 	dst[R_O2] = gregs[REG_O2];
310*0Sstevel@tonic-gate 	dst[R_O3] = gregs[REG_O3];
311*0Sstevel@tonic-gate 	dst[R_O4] = gregs[REG_O4];
312*0Sstevel@tonic-gate 	dst[R_O5] = gregs[REG_O5];
313*0Sstevel@tonic-gate 	dst[R_O6] = gregs[REG_O6];
314*0Sstevel@tonic-gate 	dst[R_O7] = gregs[REG_O7];
315*0Sstevel@tonic-gate }
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate static void
318*0Sstevel@tonic-gate ucontext_32_to_prgregs(const ucontext32_t *src, prgregset_t dst)
319*0Sstevel@tonic-gate {
320*0Sstevel@tonic-gate 	/*
321*0Sstevel@tonic-gate 	 * We need to be very careful here to cast the greg32_t's (signed) to
322*0Sstevel@tonic-gate 	 * unsigned and then explicitly promote them as unsigned values.
323*0Sstevel@tonic-gate 	 */
324*0Sstevel@tonic-gate 	const greg32_t *gregs = &src->uc_mcontext.gregs[0];
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	dst[R_PSR] = (uint64_t)(uint32_t)gregs[REG_PSR];
327*0Sstevel@tonic-gate 	dst[R_PC] = (uint64_t)(uint32_t)gregs[REG_PC];
328*0Sstevel@tonic-gate 	dst[R_nPC] = (uint64_t)(uint32_t)gregs[REG_nPC];
329*0Sstevel@tonic-gate 	dst[R_Y] = (uint64_t)(uint32_t)gregs[REG_Y];
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	dst[R_G1] = (uint64_t)(uint32_t)gregs[REG_G1];
332*0Sstevel@tonic-gate 	dst[R_G2] = (uint64_t)(uint32_t)gregs[REG_G2];
333*0Sstevel@tonic-gate 	dst[R_G3] = (uint64_t)(uint32_t)gregs[REG_G3];
334*0Sstevel@tonic-gate 	dst[R_G4] = (uint64_t)(uint32_t)gregs[REG_G4];
335*0Sstevel@tonic-gate 	dst[R_G5] = (uint64_t)(uint32_t)gregs[REG_G5];
336*0Sstevel@tonic-gate 	dst[R_G6] = (uint64_t)(uint32_t)gregs[REG_G6];
337*0Sstevel@tonic-gate 	dst[R_G7] = (uint64_t)(uint32_t)gregs[REG_G7];
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	dst[R_O0] = (uint64_t)(uint32_t)gregs[REG_O0];
340*0Sstevel@tonic-gate 	dst[R_O1] = (uint64_t)(uint32_t)gregs[REG_O1];
341*0Sstevel@tonic-gate 	dst[R_O2] = (uint64_t)(uint32_t)gregs[REG_O2];
342*0Sstevel@tonic-gate 	dst[R_O3] = (uint64_t)(uint32_t)gregs[REG_O3];
343*0Sstevel@tonic-gate 	dst[R_O4] = (uint64_t)(uint32_t)gregs[REG_O4];
344*0Sstevel@tonic-gate 	dst[R_O5] = (uint64_t)(uint32_t)gregs[REG_O5];
345*0Sstevel@tonic-gate 	dst[R_O6] = (uint64_t)(uint32_t)gregs[REG_O6];
346*0Sstevel@tonic-gate 	dst[R_O7] = (uint64_t)(uint32_t)gregs[REG_O7];
347*0Sstevel@tonic-gate }
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate int
350*0Sstevel@tonic-gate Pstack_iter(struct ps_prochandle *P, const prgregset_t regs,
351*0Sstevel@tonic-gate 	proc_stack_f *func, void *arg)
352*0Sstevel@tonic-gate {
353*0Sstevel@tonic-gate 	prgreg_t *prevfp = NULL;
354*0Sstevel@tonic-gate 	uint_t pfpsize = 0;
355*0Sstevel@tonic-gate 	int nfp = 0;
356*0Sstevel@tonic-gate 	prgregset_t gregs;
357*0Sstevel@tonic-gate 	long args[6];
358*0Sstevel@tonic-gate 	prgreg_t fp;
359*0Sstevel@tonic-gate 	int i;
360*0Sstevel@tonic-gate 	int rv;
361*0Sstevel@tonic-gate 	uintptr_t sp;
362*0Sstevel@tonic-gate 	ssize_t n;
363*0Sstevel@tonic-gate 	uclist_t ucl;
364*0Sstevel@tonic-gate 	ucontext_t uc;
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	init_uclist(&ucl, P);
367*0Sstevel@tonic-gate 	(void) memcpy(gregs, regs, sizeof (gregs));
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	for (;;) {
370*0Sstevel@tonic-gate 		fp = gregs[R_FP];
371*0Sstevel@tonic-gate 		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
372*0Sstevel@tonic-gate 			break;
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 		for (i = 0; i < 6; i++)
375*0Sstevel@tonic-gate 			args[i] = gregs[R_I0 + i];
376*0Sstevel@tonic-gate 		if ((rv = func(arg, gregs, 6, args)) != 0)
377*0Sstevel@tonic-gate 			break;
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 		gregs[R_PC] = gregs[R_I7];
380*0Sstevel@tonic-gate 		gregs[R_nPC] = gregs[R_PC] + 4;
381*0Sstevel@tonic-gate 		(void) memcpy(&gregs[R_O0], &gregs[R_I0], 8*sizeof (prgreg_t));
382*0Sstevel@tonic-gate 		if ((sp = gregs[R_FP]) == 0)
383*0Sstevel@tonic-gate 			break;
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_ILP32) {
386*0Sstevel@tonic-gate 			struct rwindow32 rw32;
387*0Sstevel@tonic-gate 			ucontext32_t uc32;
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 			if (find_uclink(&ucl, sp +
390*0Sstevel@tonic-gate 			    SA32(sizeof (struct frame32))) &&
391*0Sstevel@tonic-gate 			    Pread(P, &uc32, sizeof (uc32), sp +
392*0Sstevel@tonic-gate 			    SA32(sizeof (struct frame32))) == sizeof (uc32)) {
393*0Sstevel@tonic-gate 				ucontext_32_to_prgregs(&uc32, gregs);
394*0Sstevel@tonic-gate 				sp = gregs[R_SP];
395*0Sstevel@tonic-gate 			}
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 			n = Pread(P, &rw32, sizeof (struct rwindow32), sp);
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 			if (n == sizeof (struct rwindow32)) {
400*0Sstevel@tonic-gate 				rwindow_32_to_n(&rw32,
401*0Sstevel@tonic-gate 				    (struct rwindow *)&gregs[R_L0]);
402*0Sstevel@tonic-gate 				continue;
403*0Sstevel@tonic-gate 			}
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 		} else {
406*0Sstevel@tonic-gate 			sp += STACK_BIAS;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 			if (find_uclink(&ucl, sp + SA(sizeof (struct frame))) &&
409*0Sstevel@tonic-gate 			    Pread(P, &uc, sizeof (uc), sp +
410*0Sstevel@tonic-gate 			    SA(sizeof (struct frame))) == sizeof (uc)) {
411*0Sstevel@tonic-gate 				ucontext_n_to_prgregs(&uc, gregs);
412*0Sstevel@tonic-gate 				sp = gregs[R_SP] + STACK_BIAS;
413*0Sstevel@tonic-gate 			}
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 			n = Pread(P, &gregs[R_L0], sizeof (struct rwindow), sp);
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 			if (n == sizeof (struct rwindow))
418*0Sstevel@tonic-gate 				continue;
419*0Sstevel@tonic-gate 		}
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 		/*
422*0Sstevel@tonic-gate 		 * If we get here, then our Pread of the register window
423*0Sstevel@tonic-gate 		 * failed.  If this is because the address was not mapped,
424*0Sstevel@tonic-gate 		 * then we attempt to read this window via any gwindows
425*0Sstevel@tonic-gate 		 * information we have.  If that too fails, abort our loop.
426*0Sstevel@tonic-gate 		 */
427*0Sstevel@tonic-gate 		if (n > 0)
428*0Sstevel@tonic-gate 			break;	/* Failed for reason other than not mapped */
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 		if (read_gwin(P, (struct rwindow *)&gregs[R_L0], sp) == -1)
431*0Sstevel@tonic-gate 			break;	/* No gwindows match either */
432*0Sstevel@tonic-gate 	}
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	if (prevfp)
435*0Sstevel@tonic-gate 		free(prevfp);
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	free_uclist(&ucl);
438*0Sstevel@tonic-gate 	return (rv);
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate uintptr_t
442*0Sstevel@tonic-gate Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
443*0Sstevel@tonic-gate {
444*0Sstevel@tonic-gate 	uintptr_t ret;
445*0Sstevel@tonic-gate 	int model = P->status.pr_dmodel;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	if (model == PR_MODEL_LP64) {
448*0Sstevel@tonic-gate 		sp -= (nargs > 6)?
449*0Sstevel@tonic-gate 			WINDOWSIZE64 + sizeof (int64_t) * nargs :
450*0Sstevel@tonic-gate 			WINDOWSIZE64 + sizeof (int64_t) * 6;
451*0Sstevel@tonic-gate 		sp = PSTACK_ALIGN64(sp);
452*0Sstevel@tonic-gate 		ret = sp + WINDOWSIZE32 + sizeof (int32_t);
453*0Sstevel@tonic-gate 	} else {
454*0Sstevel@tonic-gate 		sp -= (nargs > 6)?
455*0Sstevel@tonic-gate 			WINDOWSIZE32 + sizeof (int32_t) * (1 + nargs) :
456*0Sstevel@tonic-gate 			WINDOWSIZE32 + sizeof (int32_t) * (1 + 6);
457*0Sstevel@tonic-gate 		sp = PSTACK_ALIGN32(sp);
458*0Sstevel@tonic-gate 		ret = sp + WINDOWSIZE64 + sizeof (int32_t);
459*0Sstevel@tonic-gate 	}
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	P->status.pr_lwp.pr_reg[R_G1] = sysindex;
462*0Sstevel@tonic-gate 	if (model == PR_MODEL_LP64)
463*0Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[R_SP] = sp - STACK_BIAS;
464*0Sstevel@tonic-gate 	else
465*0Sstevel@tonic-gate 		P->status.pr_lwp.pr_reg[R_SP] = sp;
466*0Sstevel@tonic-gate 	P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr;
467*0Sstevel@tonic-gate 	P->status.pr_lwp.pr_reg[R_nPC] = P->sysaddr + sizeof (instr_t);
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	return (ret);
470*0Sstevel@tonic-gate }
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate int
473*0Sstevel@tonic-gate Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
474*0Sstevel@tonic-gate     uintptr_t ap)
475*0Sstevel@tonic-gate {
476*0Sstevel@tonic-gate 	uint32_t arglist32[MAXARGS+2];
477*0Sstevel@tonic-gate 	uint64_t arglist64[MAXARGS+2];
478*0Sstevel@tonic-gate 	int i;
479*0Sstevel@tonic-gate 	argdes_t *adp;
480*0Sstevel@tonic-gate 	int model = P->status.pr_dmodel;
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	for (i = 0, adp = argp; i < nargs; i++, adp++) {
483*0Sstevel@tonic-gate 		arglist32[i] = (uint32_t)adp->arg_value;
484*0Sstevel@tonic-gate 		arglist64[i] = (uint64_t)adp->arg_value;
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 		if (i < 6)
487*0Sstevel@tonic-gate 			(void) Pputareg(P, R_O0+i, adp->arg_value);
488*0Sstevel@tonic-gate 	}
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	if (model == PR_MODEL_LP64) {
491*0Sstevel@tonic-gate 		if (nargs > 6 &&
492*0Sstevel@tonic-gate 		    Pwrite(P, &arglist64[0], sizeof (int64_t) * nargs,
493*0Sstevel@tonic-gate 		    (uintptr_t)ap) != sizeof (int64_t) * nargs)
494*0Sstevel@tonic-gate 			return (-1);
495*0Sstevel@tonic-gate 	} else {
496*0Sstevel@tonic-gate 		if (nargs > 6 &&
497*0Sstevel@tonic-gate 		    Pwrite(P, &arglist32[0], sizeof (int32_t) * nargs,
498*0Sstevel@tonic-gate 		    (uintptr_t)ap) != sizeof (int32_t) * nargs)
499*0Sstevel@tonic-gate 			return (-1);
500*0Sstevel@tonic-gate 	}
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	return (0);
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate /* ARGSUSED */
506*0Sstevel@tonic-gate int
507*0Sstevel@tonic-gate Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
508*0Sstevel@tonic-gate     uintptr_t ap)
509*0Sstevel@tonic-gate {
510*0Sstevel@tonic-gate 	/* Do nothing */
511*0Sstevel@tonic-gate 	return (0);
512*0Sstevel@tonic-gate }
513