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