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