10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53347Sab196087 * Common Development and Distribution License (the "License").
63347Sab196087 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*10201SEdward.Pilatowicz@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #define __sparcv9cpu
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/stack.h>
290Sstevel@tonic-gate #include <sys/regset.h>
300Sstevel@tonic-gate #include <sys/frame.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
32*10201SEdward.Pilatowicz@Sun.COM #include <sys/machelf.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate #include "Pcontrol.h"
410Sstevel@tonic-gate #include "Pstack.h"
420Sstevel@tonic-gate #include "Pisadep.h"
430Sstevel@tonic-gate #include "P32ton.h"
440Sstevel@tonic-gate
450Sstevel@tonic-gate #define SYSCALL32 0x91d02008 /* 32-bit syscall (ta 8) instruction */
460Sstevel@tonic-gate #define SYSCALL64 0x91d02040 /* 64-bit syscall (ta 64) instruction */
470Sstevel@tonic-gate
480Sstevel@tonic-gate const char *
Ppltdest(struct ps_prochandle * P,uintptr_t pltaddr)490Sstevel@tonic-gate Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate map_info_t *mp = Paddr2mptr(P, pltaddr);
520Sstevel@tonic-gate
530Sstevel@tonic-gate uintptr_t r_addr;
540Sstevel@tonic-gate file_info_t *fp;
550Sstevel@tonic-gate size_t i;
560Sstevel@tonic-gate
570Sstevel@tonic-gate if (mp == NULL || (fp = mp->map_file) == NULL ||
580Sstevel@tonic-gate fp->file_plt_base == 0 || pltaddr < fp->file_plt_base ||
590Sstevel@tonic-gate pltaddr >= fp->file_plt_base + fp->file_plt_size) {
600Sstevel@tonic-gate errno = EINVAL;
610Sstevel@tonic-gate return (NULL);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_LP64) {
650Sstevel@tonic-gate Elf64_Rela r;
660Sstevel@tonic-gate uintptr_t pltoff;
670Sstevel@tonic-gate
680Sstevel@tonic-gate pltoff = pltaddr - fp->file_plt_base;
69*10201SEdward.Pilatowicz@Sun.COM if (pltoff < (M64_PLT_NEARPLTS * M64_PLT_ENTSIZE)) {
700Sstevel@tonic-gate i = (pltaddr - fp->file_plt_base -
71*10201SEdward.Pilatowicz@Sun.COM M_PLT_XNumber * M64_PLT_ENTSIZE) / M64_PLT_ENTSIZE;
720Sstevel@tonic-gate } else {
730Sstevel@tonic-gate uintptr_t pltblockoff;
74*10201SEdward.Pilatowicz@Sun.COM pltblockoff = pltoff - (M64_PLT_NEARPLTS *
75*10201SEdward.Pilatowicz@Sun.COM M64_PLT_ENTSIZE);
76*10201SEdward.Pilatowicz@Sun.COM i = M64_PLT_NEARPLTS +
77*10201SEdward.Pilatowicz@Sun.COM ((pltblockoff / M64_PLT_FBLOCKSZ) *
78*10201SEdward.Pilatowicz@Sun.COM M64_PLT_FBLKCNTS) + ((pltblockoff %
79*10201SEdward.Pilatowicz@Sun.COM M64_PLT_FBLOCKSZ) / M64_PLT_FENTSIZE) -
80*10201SEdward.Pilatowicz@Sun.COM M_PLT_XNumber;
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate r_addr = fp->file_jmp_rel + i * sizeof (Elf64_Rela);
840Sstevel@tonic-gate
850Sstevel@tonic-gate if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
860Sstevel@tonic-gate (i = ELF64_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
870Sstevel@tonic-gate
883347Sab196087 Elf_Data *data = fp->file_dynsym.sym_data_pri;
890Sstevel@tonic-gate Elf64_Sym *symp = &(((Elf64_Sym *)data->d_buf)[i]);
900Sstevel@tonic-gate
910Sstevel@tonic-gate return (fp->file_dynsym.sym_strs + symp->st_name);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate } else /* PR_MODEL_ILP32 */ {
950Sstevel@tonic-gate Elf32_Rela r;
960Sstevel@tonic-gate
970Sstevel@tonic-gate i = (pltaddr - fp->file_plt_base -
98*10201SEdward.Pilatowicz@Sun.COM M_PLT_XNumber * M32_PLT_ENTSIZE) / M32_PLT_ENTSIZE;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate r_addr = fp->file_jmp_rel + i * sizeof (Elf32_Rela);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
1030Sstevel@tonic-gate (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
1040Sstevel@tonic-gate
1053347Sab196087 Elf_Data *data = fp->file_dynsym.sym_data_pri;
1060Sstevel@tonic-gate Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate return (fp->file_dynsym.sym_strs + symp->st_name);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate return (NULL);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate int
Pissyscall(struct ps_prochandle * P,uintptr_t addr)1160Sstevel@tonic-gate Pissyscall(struct ps_prochandle *P, uintptr_t addr)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate instr_t sysinstr;
1190Sstevel@tonic-gate instr_t instr;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_LP64)
1220Sstevel@tonic-gate sysinstr = SYSCALL64;
1230Sstevel@tonic-gate else
1240Sstevel@tonic-gate sysinstr = SYSCALL32;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if (Pread(P, &instr, sizeof (instr), addr) != sizeof (instr) ||
1270Sstevel@tonic-gate instr != sysinstr)
1280Sstevel@tonic-gate return (0);
1290Sstevel@tonic-gate else
1300Sstevel@tonic-gate return (1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate int
Pissyscall_prev(struct ps_prochandle * P,uintptr_t addr,uintptr_t * dst)1340Sstevel@tonic-gate Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate uintptr_t prevaddr = addr - sizeof (instr_t);
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate if (Pissyscall(P, prevaddr)) {
1390Sstevel@tonic-gate if (dst)
1400Sstevel@tonic-gate *dst = prevaddr;
1410Sstevel@tonic-gate return (1);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate return (0);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* ARGSUSED */
1480Sstevel@tonic-gate int
Pissyscall_text(struct ps_prochandle * P,const void * buf,size_t buflen)1490Sstevel@tonic-gate Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate instr_t sysinstr;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_LP64)
1540Sstevel@tonic-gate sysinstr = SYSCALL64;
1550Sstevel@tonic-gate else
1560Sstevel@tonic-gate sysinstr = SYSCALL32;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate if (buflen >= sizeof (instr_t) &&
1590Sstevel@tonic-gate memcmp(buf, &sysinstr, sizeof (instr_t)) == 0)
1600Sstevel@tonic-gate return (1);
1610Sstevel@tonic-gate else
1620Sstevel@tonic-gate return (0);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * For gwindows_t support, we define a structure to pass arguments to
1670Sstevel@tonic-gate * a Plwp_iter() callback routine.
1680Sstevel@tonic-gate */
1690Sstevel@tonic-gate typedef struct {
1700Sstevel@tonic-gate struct ps_prochandle *gq_proc; /* libproc handle */
1710Sstevel@tonic-gate struct rwindow *gq_rwin; /* rwindow destination buffer */
1720Sstevel@tonic-gate uintptr_t gq_addr; /* stack address to match */
1730Sstevel@tonic-gate } gwin_query_t;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate static int
find_gwin(gwin_query_t * gqp,const lwpstatus_t * psp)1760Sstevel@tonic-gate find_gwin(gwin_query_t *gqp, const lwpstatus_t *psp)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate gwindows_t gwin;
1790Sstevel@tonic-gate struct stat64 st;
1800Sstevel@tonic-gate char path[64];
1810Sstevel@tonic-gate ssize_t n;
1820Sstevel@tonic-gate int fd, i;
1830Sstevel@tonic-gate int rv = 0; /* Return value for skip to next lwp */
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "/proc/%d/lwp/%d/gwindows",
1860Sstevel@tonic-gate (int)gqp->gq_proc->pid, (int)psp->pr_lwpid);
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate if (stat64(path, &st) == -1 || st.st_size == 0)
1890Sstevel@tonic-gate return (0); /* Nothing doing; skip to next lwp */
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate if ((fd = open64(path, O_RDONLY)) >= 0) {
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate * Zero out the gwindows_t because the gwindows file only has
1940Sstevel@tonic-gate * as much data as needed to represent the saved windows.
1950Sstevel@tonic-gate */
1960Sstevel@tonic-gate if (gqp->gq_proc->status.pr_dmodel == PR_MODEL_ILP32) {
1970Sstevel@tonic-gate gwindows32_t g32;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate (void) memset(&g32, 0, sizeof (g32));
2000Sstevel@tonic-gate if ((n = read(fd, &g32, sizeof (g32))) > 0)
2010Sstevel@tonic-gate gwindows_32_to_n(&g32, &gwin);
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate } else {
2040Sstevel@tonic-gate (void) memset(&gwin, 0, sizeof (gwin));
2050Sstevel@tonic-gate n = read(fd, &gwin, sizeof (gwin));
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if (n > 0) {
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate * If we actually found a non-zero gwindows file and
2110Sstevel@tonic-gate * were able to read it, iterate through the buffers
2120Sstevel@tonic-gate * looking for a stack pointer match; if one is found,
2130Sstevel@tonic-gate * copy out the corresponding register window.
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate for (i = 0; i < gwin.wbcnt; i++) {
2160Sstevel@tonic-gate if (gwin.spbuf[i] == (greg_t *)gqp->gq_addr) {
2170Sstevel@tonic-gate (void) memcpy(gqp->gq_rwin,
2180Sstevel@tonic-gate &gwin.wbuf[i],
2190Sstevel@tonic-gate sizeof (struct rwindow));
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate rv = 1; /* We're done */
2220Sstevel@tonic-gate break;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate (void) close(fd);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate return (rv);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate static int
read_gwin(struct ps_prochandle * P,struct rwindow * rwp,uintptr_t sp)2330Sstevel@tonic-gate read_gwin(struct ps_prochandle *P, struct rwindow *rwp, uintptr_t sp)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate gwin_query_t gq;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate if (P->state == PS_DEAD) {
2380Sstevel@tonic-gate lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
2390Sstevel@tonic-gate uint_t n;
2400Sstevel@tonic-gate int i;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate for (n = 0; n < P->core->core_nlwp; n++, lwp = list_next(lwp)) {
2430Sstevel@tonic-gate gwindows_t *gwin = lwp->lwp_gwins;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (gwin == NULL)
2460Sstevel@tonic-gate continue; /* No gwindows for this lwp */
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * If this lwp has gwindows associated with it, iterate
2500Sstevel@tonic-gate * through the buffers looking for a stack pointer
2510Sstevel@tonic-gate * match; if one is found, copy out the register window.
2520Sstevel@tonic-gate */
2530Sstevel@tonic-gate for (i = 0; i < gwin->wbcnt; i++) {
2540Sstevel@tonic-gate if (gwin->spbuf[i] == (greg_t *)sp) {
2550Sstevel@tonic-gate (void) memcpy(rwp, &gwin->wbuf[i],
2560Sstevel@tonic-gate sizeof (struct rwindow));
2570Sstevel@tonic-gate return (0); /* We're done */
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate return (-1); /* No gwindows match found */
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate gq.gq_proc = P;
2670Sstevel@tonic-gate gq.gq_rwin = rwp;
2680Sstevel@tonic-gate gq.gq_addr = sp;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate return (Plwp_iter(P, (proc_lwp_f *)find_gwin, &gq) ? 0 : -1);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate static void
ucontext_n_to_prgregs(const ucontext_t * src,prgregset_t dst)2740Sstevel@tonic-gate ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate const greg_t *gregs = &src->uc_mcontext.gregs[0];
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate dst[R_CCR] = gregs[REG_CCR];
2790Sstevel@tonic-gate dst[R_ASI] = gregs[REG_ASI];
2800Sstevel@tonic-gate dst[R_FPRS] = gregs[REG_FPRS];
2810Sstevel@tonic-gate dst[R_PC] = gregs[REG_PC];
2820Sstevel@tonic-gate dst[R_nPC] = gregs[REG_nPC];
2830Sstevel@tonic-gate dst[R_Y] = gregs[REG_Y];
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate dst[R_G1] = gregs[REG_G1];
2860Sstevel@tonic-gate dst[R_G2] = gregs[REG_G2];
2870Sstevel@tonic-gate dst[R_G3] = gregs[REG_G3];
2880Sstevel@tonic-gate dst[R_G4] = gregs[REG_G4];
2890Sstevel@tonic-gate dst[R_G5] = gregs[REG_G5];
2900Sstevel@tonic-gate dst[R_G6] = gregs[REG_G6];
2910Sstevel@tonic-gate dst[R_G7] = gregs[REG_G7];
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate dst[R_O0] = gregs[REG_O0];
2940Sstevel@tonic-gate dst[R_O1] = gregs[REG_O1];
2950Sstevel@tonic-gate dst[R_O2] = gregs[REG_O2];
2960Sstevel@tonic-gate dst[R_O3] = gregs[REG_O3];
2970Sstevel@tonic-gate dst[R_O4] = gregs[REG_O4];
2980Sstevel@tonic-gate dst[R_O5] = gregs[REG_O5];
2990Sstevel@tonic-gate dst[R_O6] = gregs[REG_O6];
3000Sstevel@tonic-gate dst[R_O7] = gregs[REG_O7];
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate static void
ucontext_32_to_prgregs(const ucontext32_t * src,prgregset_t dst)3040Sstevel@tonic-gate ucontext_32_to_prgregs(const ucontext32_t *src, prgregset_t dst)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate * We need to be very careful here to cast the greg32_t's (signed) to
3080Sstevel@tonic-gate * unsigned and then explicitly promote them as unsigned values.
3090Sstevel@tonic-gate */
3100Sstevel@tonic-gate const greg32_t *gregs = &src->uc_mcontext.gregs[0];
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate dst[R_PSR] = (uint64_t)(uint32_t)gregs[REG_PSR];
3130Sstevel@tonic-gate dst[R_PC] = (uint64_t)(uint32_t)gregs[REG_PC];
3140Sstevel@tonic-gate dst[R_nPC] = (uint64_t)(uint32_t)gregs[REG_nPC];
3150Sstevel@tonic-gate dst[R_Y] = (uint64_t)(uint32_t)gregs[REG_Y];
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate dst[R_G1] = (uint64_t)(uint32_t)gregs[REG_G1];
3180Sstevel@tonic-gate dst[R_G2] = (uint64_t)(uint32_t)gregs[REG_G2];
3190Sstevel@tonic-gate dst[R_G3] = (uint64_t)(uint32_t)gregs[REG_G3];
3200Sstevel@tonic-gate dst[R_G4] = (uint64_t)(uint32_t)gregs[REG_G4];
3210Sstevel@tonic-gate dst[R_G5] = (uint64_t)(uint32_t)gregs[REG_G5];
3220Sstevel@tonic-gate dst[R_G6] = (uint64_t)(uint32_t)gregs[REG_G6];
3230Sstevel@tonic-gate dst[R_G7] = (uint64_t)(uint32_t)gregs[REG_G7];
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate dst[R_O0] = (uint64_t)(uint32_t)gregs[REG_O0];
3260Sstevel@tonic-gate dst[R_O1] = (uint64_t)(uint32_t)gregs[REG_O1];
3270Sstevel@tonic-gate dst[R_O2] = (uint64_t)(uint32_t)gregs[REG_O2];
3280Sstevel@tonic-gate dst[R_O3] = (uint64_t)(uint32_t)gregs[REG_O3];
3290Sstevel@tonic-gate dst[R_O4] = (uint64_t)(uint32_t)gregs[REG_O4];
3300Sstevel@tonic-gate dst[R_O5] = (uint64_t)(uint32_t)gregs[REG_O5];
3310Sstevel@tonic-gate dst[R_O6] = (uint64_t)(uint32_t)gregs[REG_O6];
3320Sstevel@tonic-gate dst[R_O7] = (uint64_t)(uint32_t)gregs[REG_O7];
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate int
Pstack_iter(struct ps_prochandle * P,const prgregset_t regs,proc_stack_f * func,void * arg)3360Sstevel@tonic-gate Pstack_iter(struct ps_prochandle *P, const prgregset_t regs,
3370Sstevel@tonic-gate proc_stack_f *func, void *arg)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate prgreg_t *prevfp = NULL;
3400Sstevel@tonic-gate uint_t pfpsize = 0;
3410Sstevel@tonic-gate int nfp = 0;
3420Sstevel@tonic-gate prgregset_t gregs;
3430Sstevel@tonic-gate long args[6];
3440Sstevel@tonic-gate prgreg_t fp;
3450Sstevel@tonic-gate int i;
3460Sstevel@tonic-gate int rv;
3470Sstevel@tonic-gate uintptr_t sp;
3480Sstevel@tonic-gate ssize_t n;
3490Sstevel@tonic-gate uclist_t ucl;
3500Sstevel@tonic-gate ucontext_t uc;
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate init_uclist(&ucl, P);
3530Sstevel@tonic-gate (void) memcpy(gregs, regs, sizeof (gregs));
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate for (;;) {
3560Sstevel@tonic-gate fp = gregs[R_FP];
3570Sstevel@tonic-gate if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
3580Sstevel@tonic-gate break;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate for (i = 0; i < 6; i++)
3610Sstevel@tonic-gate args[i] = gregs[R_I0 + i];
3620Sstevel@tonic-gate if ((rv = func(arg, gregs, 6, args)) != 0)
3630Sstevel@tonic-gate break;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate gregs[R_PC] = gregs[R_I7];
3660Sstevel@tonic-gate gregs[R_nPC] = gregs[R_PC] + 4;
3670Sstevel@tonic-gate (void) memcpy(&gregs[R_O0], &gregs[R_I0], 8*sizeof (prgreg_t));
3680Sstevel@tonic-gate if ((sp = gregs[R_FP]) == 0)
3690Sstevel@tonic-gate break;
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) {
3720Sstevel@tonic-gate struct rwindow32 rw32;
3730Sstevel@tonic-gate ucontext32_t uc32;
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate if (find_uclink(&ucl, sp +
3760Sstevel@tonic-gate SA32(sizeof (struct frame32))) &&
3770Sstevel@tonic-gate Pread(P, &uc32, sizeof (uc32), sp +
3780Sstevel@tonic-gate SA32(sizeof (struct frame32))) == sizeof (uc32)) {
3790Sstevel@tonic-gate ucontext_32_to_prgregs(&uc32, gregs);
3800Sstevel@tonic-gate sp = gregs[R_SP];
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate n = Pread(P, &rw32, sizeof (struct rwindow32), sp);
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate if (n == sizeof (struct rwindow32)) {
3860Sstevel@tonic-gate rwindow_32_to_n(&rw32,
3870Sstevel@tonic-gate (struct rwindow *)&gregs[R_L0]);
3880Sstevel@tonic-gate continue;
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate } else {
3920Sstevel@tonic-gate sp += STACK_BIAS;
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if (find_uclink(&ucl, sp + SA(sizeof (struct frame))) &&
3950Sstevel@tonic-gate Pread(P, &uc, sizeof (uc), sp +
3960Sstevel@tonic-gate SA(sizeof (struct frame))) == sizeof (uc)) {
3970Sstevel@tonic-gate ucontext_n_to_prgregs(&uc, gregs);
3980Sstevel@tonic-gate sp = gregs[R_SP] + STACK_BIAS;
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate n = Pread(P, &gregs[R_L0], sizeof (struct rwindow), sp);
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate if (n == sizeof (struct rwindow))
4040Sstevel@tonic-gate continue;
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate * If we get here, then our Pread of the register window
4090Sstevel@tonic-gate * failed. If this is because the address was not mapped,
4100Sstevel@tonic-gate * then we attempt to read this window via any gwindows
4110Sstevel@tonic-gate * information we have. If that too fails, abort our loop.
4120Sstevel@tonic-gate */
4130Sstevel@tonic-gate if (n > 0)
4140Sstevel@tonic-gate break; /* Failed for reason other than not mapped */
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate if (read_gwin(P, (struct rwindow *)&gregs[R_L0], sp) == -1)
4170Sstevel@tonic-gate break; /* No gwindows match either */
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate if (prevfp)
4210Sstevel@tonic-gate free(prevfp);
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate free_uclist(&ucl);
4240Sstevel@tonic-gate return (rv);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate uintptr_t
Psyscall_setup(struct ps_prochandle * P,int nargs,int sysindex,uintptr_t sp)4280Sstevel@tonic-gate Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate uintptr_t ret;
4310Sstevel@tonic-gate int model = P->status.pr_dmodel;
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate if (model == PR_MODEL_LP64) {
4340Sstevel@tonic-gate sp -= (nargs > 6)?
435*10201SEdward.Pilatowicz@Sun.COM WINDOWSIZE64 + sizeof (int64_t) * nargs :
436*10201SEdward.Pilatowicz@Sun.COM WINDOWSIZE64 + sizeof (int64_t) * 6;
4370Sstevel@tonic-gate sp = PSTACK_ALIGN64(sp);
4380Sstevel@tonic-gate ret = sp + WINDOWSIZE32 + sizeof (int32_t);
4390Sstevel@tonic-gate } else {
4400Sstevel@tonic-gate sp -= (nargs > 6)?
441*10201SEdward.Pilatowicz@Sun.COM WINDOWSIZE32 + sizeof (int32_t) * (1 + nargs) :
442*10201SEdward.Pilatowicz@Sun.COM WINDOWSIZE32 + sizeof (int32_t) * (1 + 6);
4430Sstevel@tonic-gate sp = PSTACK_ALIGN32(sp);
4440Sstevel@tonic-gate ret = sp + WINDOWSIZE64 + sizeof (int32_t);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate P->status.pr_lwp.pr_reg[R_G1] = sysindex;
4480Sstevel@tonic-gate if (model == PR_MODEL_LP64)
4490Sstevel@tonic-gate P->status.pr_lwp.pr_reg[R_SP] = sp - STACK_BIAS;
4500Sstevel@tonic-gate else
4510Sstevel@tonic-gate P->status.pr_lwp.pr_reg[R_SP] = sp;
4520Sstevel@tonic-gate P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr;
4530Sstevel@tonic-gate P->status.pr_lwp.pr_reg[R_nPC] = P->sysaddr + sizeof (instr_t);
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate return (ret);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate int
Psyscall_copyinargs(struct ps_prochandle * P,int nargs,argdes_t * argp,uintptr_t ap)4590Sstevel@tonic-gate Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
4600Sstevel@tonic-gate uintptr_t ap)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate uint32_t arglist32[MAXARGS+2];
4630Sstevel@tonic-gate uint64_t arglist64[MAXARGS+2];
4640Sstevel@tonic-gate int i;
4650Sstevel@tonic-gate argdes_t *adp;
4660Sstevel@tonic-gate int model = P->status.pr_dmodel;
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate for (i = 0, adp = argp; i < nargs; i++, adp++) {
4690Sstevel@tonic-gate arglist32[i] = (uint32_t)adp->arg_value;
4700Sstevel@tonic-gate arglist64[i] = (uint64_t)adp->arg_value;
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate if (i < 6)
4730Sstevel@tonic-gate (void) Pputareg(P, R_O0+i, adp->arg_value);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate if (model == PR_MODEL_LP64) {
4770Sstevel@tonic-gate if (nargs > 6 &&
4780Sstevel@tonic-gate Pwrite(P, &arglist64[0], sizeof (int64_t) * nargs,
4790Sstevel@tonic-gate (uintptr_t)ap) != sizeof (int64_t) * nargs)
4800Sstevel@tonic-gate return (-1);
4810Sstevel@tonic-gate } else {
4820Sstevel@tonic-gate if (nargs > 6 &&
4830Sstevel@tonic-gate Pwrite(P, &arglist32[0], sizeof (int32_t) * nargs,
4840Sstevel@tonic-gate (uintptr_t)ap) != sizeof (int32_t) * nargs)
4850Sstevel@tonic-gate return (-1);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate return (0);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate /* ARGSUSED */
4920Sstevel@tonic-gate int
Psyscall_copyoutargs(struct ps_prochandle * P,int nargs,argdes_t * argp,uintptr_t ap)4930Sstevel@tonic-gate Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
4940Sstevel@tonic-gate uintptr_t ap)
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate /* Do nothing */
4970Sstevel@tonic-gate return (0);
4980Sstevel@tonic-gate }
499