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 #include <sys/stack.h>
30*0Sstevel@tonic-gate #include <sys/regset.h>
31*0Sstevel@tonic-gate #include <sys/frame.h>
32*0Sstevel@tonic-gate #include <sys/sysmacros.h>
33*0Sstevel@tonic-gate #include <sys/trap.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <errno.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include "Pcontrol.h"
42*0Sstevel@tonic-gate #include "Pstack.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #define	M_PLT_NRSV		1	/* reserved PLT entries */
45*0Sstevel@tonic-gate #define	M_PLT_ENTSIZE		16	/* size of each PLT entry */
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static uchar_t int_syscall_instr[] = { 0xCD, T_SYSCALLINT };
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate const char *
50*0Sstevel@tonic-gate Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate 	map_info_t *mp = Paddr2mptr(P, pltaddr);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	uintptr_t r_addr;
55*0Sstevel@tonic-gate 	file_info_t *fp;
56*0Sstevel@tonic-gate 	Elf32_Rel r;
57*0Sstevel@tonic-gate 	size_t i;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	if (mp == NULL || (fp = mp->map_file) == NULL ||
60*0Sstevel@tonic-gate 	    fp->file_plt_base == 0 ||
61*0Sstevel@tonic-gate 	    pltaddr - fp->file_plt_base >= fp->file_plt_size) {
62*0Sstevel@tonic-gate 		errno = EINVAL;
63*0Sstevel@tonic-gate 		return (NULL);
64*0Sstevel@tonic-gate 	}
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	i = (pltaddr - fp->file_plt_base) / M_PLT_ENTSIZE - M_PLT_NRSV;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	r_addr = fp->file_jmp_rel + i * sizeof (r);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
71*0Sstevel@tonic-gate 	    (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 		Elf_Data *data = fp->file_dynsym.sym_data;
74*0Sstevel@tonic-gate 		Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 		return (fp->file_dynsym.sym_strs + symp->st_name);
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	return (NULL);
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate int
83*0Sstevel@tonic-gate Pissyscall(struct ps_prochandle *P, uintptr_t addr)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	uchar_t instr[16];
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	if (Pread(P, instr, sizeof (int_syscall_instr), addr) !=
88*0Sstevel@tonic-gate 	    sizeof (int_syscall_instr))
89*0Sstevel@tonic-gate 		return (0);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if (memcmp(instr, int_syscall_instr, sizeof (int_syscall_instr)) == 0)
92*0Sstevel@tonic-gate 		return (1);
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	return (0);
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate int
98*0Sstevel@tonic-gate Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	int ret;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if (ret = Pissyscall(P, addr - sizeof (int_syscall_instr))) {
103*0Sstevel@tonic-gate 		if (dst)
104*0Sstevel@tonic-gate 			*dst = addr - sizeof (int_syscall_instr);
105*0Sstevel@tonic-gate 		return (ret);
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	return (0);
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /* ARGSUSED */
112*0Sstevel@tonic-gate int
113*0Sstevel@tonic-gate Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate 	if (buflen < sizeof (int_syscall_instr))
116*0Sstevel@tonic-gate 		return (0);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (memcmp(buf, int_syscall_instr, sizeof (int_syscall_instr)) == 0)
119*0Sstevel@tonic-gate 		return (1);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	return (0);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate #define	TR_ARG_MAX 6	/* Max args to print, same as SPARC */
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate /*
127*0Sstevel@tonic-gate  * Given a return address, determine the likely number of arguments
128*0Sstevel@tonic-gate  * that were pushed on the stack prior to its execution.  We do this by
129*0Sstevel@tonic-gate  * expecting that a typical call sequence consists of pushing arguments on
130*0Sstevel@tonic-gate  * the stack, executing a call instruction, and then performing an add
131*0Sstevel@tonic-gate  * on %esp to restore it to the value prior to pushing the arguments for
132*0Sstevel@tonic-gate  * the call.  We attempt to detect such an add, and divide the addend
133*0Sstevel@tonic-gate  * by the size of a word to determine the number of pushed arguments.
134*0Sstevel@tonic-gate  *
135*0Sstevel@tonic-gate  * If we do not find such an add, this does not necessarily imply that the
136*0Sstevel@tonic-gate  * function took no arguments. It is not possible to reliably detect such a
137*0Sstevel@tonic-gate  * void function because hand-coded assembler does not always perform an add
138*0Sstevel@tonic-gate  * to %esp immediately after the "call" instruction (eg. _sys_call()).
139*0Sstevel@tonic-gate  * Because of this, we default to returning MIN(sz, TR_ARG_MAX) instead of 0
140*0Sstevel@tonic-gate  * in the absence of an add to %esp.
141*0Sstevel@tonic-gate  */
142*0Sstevel@tonic-gate static ulong_t
143*0Sstevel@tonic-gate argcount(struct ps_prochandle *P, long pc, ssize_t sz)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	uchar_t instr[6];
146*0Sstevel@tonic-gate 	ulong_t count, max;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	max = MIN(sz / sizeof (long), TR_ARG_MAX);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	/*
151*0Sstevel@tonic-gate 	 * Read the instruction at the return location.
152*0Sstevel@tonic-gate 	 */
153*0Sstevel@tonic-gate 	if (Pread(P, instr, sizeof (instr), pc) != sizeof (instr) ||
154*0Sstevel@tonic-gate 	    instr[1] != 0xc4)
155*0Sstevel@tonic-gate 		return (max);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	switch (instr[0]) {
158*0Sstevel@tonic-gate 	case 0x81:	/* count is a longword */
159*0Sstevel@tonic-gate 		count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24);
160*0Sstevel@tonic-gate 		break;
161*0Sstevel@tonic-gate 	case 0x83:	/* count is a byte */
162*0Sstevel@tonic-gate 		count = instr[2];
163*0Sstevel@tonic-gate 		break;
164*0Sstevel@tonic-gate 	default:
165*0Sstevel@tonic-gate 		return (max);
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	count /= sizeof (long);
169*0Sstevel@tonic-gate 	return (MIN(count, max));
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate static void
173*0Sstevel@tonic-gate ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	(void) memcpy(dst, src->uc_mcontext.gregs, sizeof (gregset_t));
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate int
179*0Sstevel@tonic-gate Pstack_iter(struct ps_prochandle *P, const prgregset_t regs,
180*0Sstevel@tonic-gate 	proc_stack_f *func, void *arg)
181*0Sstevel@tonic-gate {
182*0Sstevel@tonic-gate 	prgreg_t *prevfp = NULL;
183*0Sstevel@tonic-gate 	uint_t pfpsize = 0;
184*0Sstevel@tonic-gate 	int nfp = 0;
185*0Sstevel@tonic-gate 	struct {
186*0Sstevel@tonic-gate 		long	fp;
187*0Sstevel@tonic-gate 		long	pc;
188*0Sstevel@tonic-gate 		long	args[32];
189*0Sstevel@tonic-gate 	} frame;
190*0Sstevel@tonic-gate 	uint_t argc;
191*0Sstevel@tonic-gate 	ssize_t sz;
192*0Sstevel@tonic-gate 	prgregset_t gregs;
193*0Sstevel@tonic-gate 	prgreg_t fp, pfp;
194*0Sstevel@tonic-gate 	prgreg_t pc;
195*0Sstevel@tonic-gate 	int rv;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	/*
198*0Sstevel@tonic-gate 	 * Type definition for a structure corresponding to an IA32
199*0Sstevel@tonic-gate 	 * signal frame.  Refer to the comments in Pstack.c for more info
200*0Sstevel@tonic-gate 	 */
201*0Sstevel@tonic-gate 	typedef struct {
202*0Sstevel@tonic-gate 		long fp;
203*0Sstevel@tonic-gate 		long pc;
204*0Sstevel@tonic-gate 		int signo;
205*0Sstevel@tonic-gate 		ucontext_t *ucp;
206*0Sstevel@tonic-gate 		siginfo_t *sip;
207*0Sstevel@tonic-gate 	} sf_t;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	uclist_t ucl;
210*0Sstevel@tonic-gate 	ucontext_t uc;
211*0Sstevel@tonic-gate 	uintptr_t uc_addr;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	init_uclist(&ucl, P);
214*0Sstevel@tonic-gate 	(void) memcpy(gregs, regs, sizeof (gregs));
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	fp = regs[R_FP];
217*0Sstevel@tonic-gate 	pc = regs[R_PC];
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	while (fp != 0 || pc != 0) {
220*0Sstevel@tonic-gate 		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
221*0Sstevel@tonic-gate 			break;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 		if (fp != 0 &&
224*0Sstevel@tonic-gate 		    (sz = Pread(P, &frame, sizeof (frame), (uintptr_t)fp)
225*0Sstevel@tonic-gate 		    >= (ssize_t)(2* sizeof (long)))) {
226*0Sstevel@tonic-gate 			/*
227*0Sstevel@tonic-gate 			 * One more trick for signal frames: the kernel sets
228*0Sstevel@tonic-gate 			 * the return pc of the signal frame to 0xffffffff on
229*0Sstevel@tonic-gate 			 * Intel IA32, so argcount won't work.
230*0Sstevel@tonic-gate 			 */
231*0Sstevel@tonic-gate 			if (frame.pc != -1L) {
232*0Sstevel@tonic-gate 				sz -= 2* sizeof (long);
233*0Sstevel@tonic-gate 				argc = argcount(P, (long)frame.pc, sz);
234*0Sstevel@tonic-gate 			} else
235*0Sstevel@tonic-gate 				argc = 3; /* sighandler(signo, sip, ucp) */
236*0Sstevel@tonic-gate 		} else {
237*0Sstevel@tonic-gate 			(void) memset(&frame, 0, sizeof (frame));
238*0Sstevel@tonic-gate 			argc = 0;
239*0Sstevel@tonic-gate 		}
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 		gregs[R_FP] = fp;
242*0Sstevel@tonic-gate 		gregs[R_PC] = pc;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 		if ((rv = func(arg, gregs, argc, frame.args)) != 0)
245*0Sstevel@tonic-gate 			break;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 		/*
248*0Sstevel@tonic-gate 		 * In order to allow iteration over java frames (which can have
249*0Sstevel@tonic-gate 		 * their own frame pointers), we allow the iterator to change
250*0Sstevel@tonic-gate 		 * the contents of gregs.  If we detect a change, then we assume
251*0Sstevel@tonic-gate 		 * that the new values point to the next frame.
252*0Sstevel@tonic-gate 		 */
253*0Sstevel@tonic-gate 		if (gregs[R_FP] != fp || gregs[R_PC] != pc) {
254*0Sstevel@tonic-gate 			fp = gregs[R_FP];
255*0Sstevel@tonic-gate 			pc = gregs[R_PC];
256*0Sstevel@tonic-gate 			continue;
257*0Sstevel@tonic-gate 		}
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 		pfp = fp;
260*0Sstevel@tonic-gate 		fp = frame.fp;
261*0Sstevel@tonic-gate 		pc = frame.pc;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 		if (find_uclink(&ucl, pfp + sizeof (sf_t)))
264*0Sstevel@tonic-gate 			uc_addr = pfp + sizeof (sf_t);
265*0Sstevel@tonic-gate 		else
266*0Sstevel@tonic-gate 			uc_addr = NULL;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 		if (uc_addr != NULL &&
269*0Sstevel@tonic-gate 		    Pread(P, &uc, sizeof (uc), uc_addr) == sizeof (uc)) {
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 			ucontext_n_to_prgregs(&uc, gregs);
272*0Sstevel@tonic-gate 			fp = gregs[R_FP];
273*0Sstevel@tonic-gate 			pc = gregs[R_PC];
274*0Sstevel@tonic-gate 		}
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	if (prevfp)
278*0Sstevel@tonic-gate 		free(prevfp);
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	free_uclist(&ucl);
281*0Sstevel@tonic-gate 	return (rv);
282*0Sstevel@tonic-gate }
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate uintptr_t
285*0Sstevel@tonic-gate Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate 	sp -= sizeof (int) * (nargs+2);	/* space for arg list + CALL parms */
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	P->status.pr_lwp.pr_reg[EAX] = sysindex;
290*0Sstevel@tonic-gate 	P->status.pr_lwp.pr_reg[R_SP] = sp;
291*0Sstevel@tonic-gate 	P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr;
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	return (sp);
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate int
297*0Sstevel@tonic-gate Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
298*0Sstevel@tonic-gate     uintptr_t ap)
299*0Sstevel@tonic-gate {
300*0Sstevel@tonic-gate 	int32_t arglist[MAXARGS+2];
301*0Sstevel@tonic-gate 	int i;
302*0Sstevel@tonic-gate 	argdes_t *adp;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	for (i = 0, adp = argp; i < nargs; i++, adp++)
305*0Sstevel@tonic-gate 		arglist[1 + i] = (int32_t)adp->arg_value;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	arglist[0] = P->status.pr_lwp.pr_reg[R_PC];
308*0Sstevel@tonic-gate 	if (Pwrite(P, &arglist[0], sizeof (int) * (nargs+1),
309*0Sstevel@tonic-gate 	    (uintptr_t)ap) != sizeof (int) * (nargs+1))
310*0Sstevel@tonic-gate 		return (-1);
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	return (0);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate int
316*0Sstevel@tonic-gate Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
317*0Sstevel@tonic-gate     uintptr_t ap)
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate 	uint32_t arglist[MAXARGS + 2];
320*0Sstevel@tonic-gate 	int i;
321*0Sstevel@tonic-gate 	argdes_t *adp;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	if (Pread(P, &arglist[0], sizeof (int) * (nargs+1), (uintptr_t)ap)
324*0Sstevel@tonic-gate 	    != sizeof (int) * (nargs+1))
325*0Sstevel@tonic-gate 		return (-1);
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	for (i = 0, adp = argp; i < nargs; i++, adp++)
328*0Sstevel@tonic-gate 		adp->arg_value = arglist[i];
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	return (0);
331*0Sstevel@tonic-gate }
332