1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#include <sys/stack.h>
27#include <sys/regset.h>
28#include <sys/frame.h>
29#include <sys/sysmacros.h>
30#include <sys/machelf.h>
31
32#include <stdlib.h>
33#include <unistd.h>
34#include <sys/types.h>
35#include <errno.h>
36#include <string.h>
37
38#include "Pcontrol.h"
39#include "Pstack.h"
40#include "Pisadep.h"
41
42#define	SYSCALL32 0x91d02008	/* 32-bit syscall (ta 8) instruction */
43
44#ifndef	WINDOWSIZE32
45#define	WINDOWSIZE32	(16 * sizeof (int32_t))
46#endif
47
48const char *
49Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr)
50{
51	map_info_t *mp = Paddr2mptr(P, pltaddr);
52
53	uintptr_t r_addr;
54	file_info_t *fp;
55	Elf32_Rela r;
56	size_t i;
57
58	if (mp == NULL || (fp = mp->map_file) == NULL ||
59	    fp->file_plt_base == 0 || pltaddr < fp->file_plt_base ||
60	    pltaddr >= fp->file_plt_base + fp->file_plt_size) {
61		errno = EINVAL;
62		return (NULL);
63	}
64
65	i = (pltaddr - fp->file_plt_base -
66	    M_PLT_XNumber * M32_PLT_ENTSIZE) / M32_PLT_ENTSIZE;
67
68	r_addr = fp->file_jmp_rel + i * sizeof (Elf32_Rela);
69
70	if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
71	    (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
72
73		Elf_Data *data = fp->file_dynsym.sym_data_pri;
74		Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
75
76		return (fp->file_dynsym.sym_strs + symp->st_name);
77	}
78
79	return (NULL);
80}
81
82int
83Pissyscall(struct ps_prochandle *P, uintptr_t addr)
84{
85	instr_t sysinstr;
86	instr_t instr;
87
88	sysinstr = SYSCALL32;
89
90	if (Pread(P, &instr, sizeof (instr), addr) != sizeof (instr) ||
91	    instr != sysinstr)
92		return (0);
93	else
94		return (1);
95}
96
97int
98Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
99{
100	uintptr_t prevaddr = addr - sizeof (instr_t);
101
102	if (Pissyscall(P, prevaddr)) {
103		if (dst)
104			*dst = prevaddr;
105		return (1);
106	}
107
108	return (0);
109}
110
111/* ARGSUSED */
112int
113Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
114{
115	instr_t sysinstr;
116
117	sysinstr = SYSCALL32;
118
119	if (buflen >= sizeof (instr_t) &&
120	    memcmp(buf, &sysinstr, sizeof (instr_t)) == 0)
121		return (1);
122	else
123		return (0);
124}
125
126/*
127 * For gwindows_t support, we define a structure to pass arguments to
128 * a Plwp_iter() callback routine.
129 */
130typedef struct {
131	struct ps_prochandle *gq_proc;	/* libproc handle */
132	struct rwindow *gq_rwin;	/* rwindow destination buffer */
133	uintptr_t gq_addr;		/* stack address to match */
134} gwin_query_t;
135
136static int
137find_gwin(gwin_query_t *gqp, const lwpstatus_t *psp)
138{
139	gwindows_t gwin;
140	struct stat64 st;
141	char path[64];
142	ssize_t n;
143	int fd, i;
144	int rv = 0; /* Return value for skip to next lwp */
145
146	(void) snprintf(path, sizeof (path), "/proc/%d/lwp/%d/gwindows",
147	    (int)gqp->gq_proc->pid, (int)psp->pr_lwpid);
148
149	if (stat64(path, &st) == -1 || st.st_size == 0)
150		return (0); /* Nothing doing; skip to next lwp */
151
152	if ((fd = open64(path, O_RDONLY)) >= 0) {
153		/*
154		 * Zero out the gwindows_t because the gwindows file only has
155		 * as much data as needed to represent the saved windows.
156		 */
157		(void) memset(&gwin, 0, sizeof (gwin));
158		n = read(fd, &gwin, sizeof (gwin));
159
160		if (n > 0) {
161			/*
162			 * If we actually found a non-zero gwindows file and
163			 * were able to read it, iterate through the buffers
164			 * looking for a stack pointer match; if one is found,
165			 * copy out the corresponding register window.
166			 */
167			for (i = 0; i < gwin.wbcnt; i++) {
168				if (gwin.spbuf[i] == (greg_t *)gqp->gq_addr) {
169					(void) memcpy(gqp->gq_rwin,
170					    &gwin.wbuf[i],
171					    sizeof (struct rwindow));
172
173					rv = 1; /* We're done */
174					break;
175				}
176			}
177		}
178		(void) close(fd);
179	}
180
181	return (rv);
182}
183
184static int
185read_gwin(struct ps_prochandle *P, struct rwindow *rwp, uintptr_t sp)
186{
187	gwin_query_t gq;
188
189	if (P->state == PS_DEAD) {
190		lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
191		uint_t n;
192		int i;
193
194		for (n = 0; n < P->core->core_nlwp; n++, lwp = list_next(lwp)) {
195			gwindows_t *gwin = lwp->lwp_gwins;
196
197			if (gwin == NULL)
198				continue; /* No gwindows for this lwp */
199
200			/*
201			 * If this lwp has gwindows associated with it, iterate
202			 * through the buffers looking for a stack pointer
203			 * match; if one is found, copy out the register window.
204			 */
205			for (i = 0; i < gwin->wbcnt; i++) {
206				if (gwin->spbuf[i] == (greg_t *)sp) {
207					(void) memcpy(rwp, &gwin->wbuf[i],
208					    sizeof (struct rwindow));
209					return (0); /* We're done */
210				}
211			}
212		}
213
214		return (-1); /* No gwindows match found */
215
216	}
217
218	gq.gq_proc = P;
219	gq.gq_rwin = rwp;
220	gq.gq_addr = sp;
221
222	return (Plwp_iter(P, (proc_lwp_f *)find_gwin, &gq) ? 0 : -1);
223}
224
225static void
226ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
227{
228	const greg_t *gregs = &src->uc_mcontext.gregs[0];
229
230	dst[R_PSR] = gregs[REG_PSR];
231	dst[R_PC] = gregs[REG_PC];
232	dst[R_nPC] = gregs[REG_nPC];
233	dst[R_Y] = gregs[REG_Y];
234
235	dst[R_G1] = gregs[REG_G1];
236	dst[R_G2] = gregs[REG_G2];
237	dst[R_G3] = gregs[REG_G3];
238	dst[R_G4] = gregs[REG_G4];
239	dst[R_G5] = gregs[REG_G5];
240	dst[R_G6] = gregs[REG_G6];
241	dst[R_G7] = gregs[REG_G7];
242
243	dst[R_O0] = gregs[REG_O0];
244	dst[R_O1] = gregs[REG_O1];
245	dst[R_O2] = gregs[REG_O2];
246	dst[R_O3] = gregs[REG_O3];
247	dst[R_O4] = gregs[REG_O4];
248	dst[R_O5] = gregs[REG_O5];
249	dst[R_O6] = gregs[REG_O6];
250	dst[R_O7] = gregs[REG_O7];
251}
252
253int
254Pstack_iter(struct ps_prochandle *P, const prgregset_t regs,
255	proc_stack_f *func, void *arg)
256{
257	prgreg_t *prevfp = NULL;
258	uint_t pfpsize = 0;
259	int nfp = 0;
260	prgregset_t gregs;
261	long args[6];
262	prgreg_t fp;
263	int i;
264	int rv;
265	uintptr_t sp;
266	ssize_t n;
267	uclist_t ucl;
268	ucontext_t uc;
269
270	init_uclist(&ucl, P);
271	(void) memcpy(gregs, regs, sizeof (gregs));
272
273	for (;;) {
274		fp = gregs[R_FP];
275		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
276			break;
277
278		for (i = 0; i < 6; i++)
279			args[i] = gregs[R_I0 + i];
280		if ((rv = func(arg, gregs, 6, args)) != 0)
281			break;
282
283		gregs[R_PC] = gregs[R_I7];
284		gregs[R_nPC] = gregs[R_PC] + 4;
285		(void) memcpy(&gregs[R_O0], &gregs[R_I0], 8*sizeof (prgreg_t));
286		if ((sp = gregs[R_FP]) == 0)
287			break;
288
289		sp += STACK_BIAS;
290
291		if (find_uclink(&ucl, sp + SA(sizeof (struct frame))) &&
292		    Pread(P, &uc, sizeof (uc), sp +
293		    SA(sizeof (struct frame))) == sizeof (uc)) {
294			ucontext_n_to_prgregs(&uc, gregs);
295			sp = gregs[R_SP] + STACK_BIAS;
296		}
297
298		n = Pread(P, &gregs[R_L0], sizeof (struct rwindow), sp);
299
300		if (n == sizeof (struct rwindow))
301			continue;
302
303		/*
304		 * If we get here, then our Pread of the register window
305		 * failed.  If this is because the address was not mapped,
306		 * then we attempt to read this window via any gwindows
307		 * information we have.  If that too fails, abort our loop.
308		 */
309		if (n > 0)
310			break;	/* Failed for reason other than not mapped */
311
312		if (read_gwin(P, (struct rwindow *)&gregs[R_L0], sp) == -1)
313			break;	/* No gwindows match either */
314	}
315
316	if (prevfp)
317		free(prevfp);
318
319	free_uclist(&ucl);
320	return (rv);
321}
322
323uintptr_t
324Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
325{
326	sp -= (nargs > 6)?
327	    WINDOWSIZE32 + sizeof (int32_t) * (1 + nargs) :
328	    WINDOWSIZE32 + sizeof (int32_t) * (1 + 6);
329	sp = PSTACK_ALIGN32(sp);
330
331	P->status.pr_lwp.pr_reg[R_G1] = sysindex;
332	P->status.pr_lwp.pr_reg[R_SP] = sp;
333	P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr;
334	P->status.pr_lwp.pr_reg[R_nPC] = P->sysaddr + sizeof (instr_t);
335
336	return (sp + WINDOWSIZE32 + sizeof (int32_t));
337}
338
339int
340Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
341    uintptr_t ap)
342{
343	uint32_t arglist[MAXARGS+2];
344	int i;
345	argdes_t *adp;
346
347	for (i = 0, adp = argp; i < nargs; i++, adp++) {
348		arglist[i] = adp->arg_value;
349
350		if (i < 6)
351			(void) Pputareg(P, R_O0+i, adp->arg_value);
352	}
353
354	if (nargs > 6 &&
355	    Pwrite(P, &arglist[0], sizeof (int32_t) * nargs,
356	    (uintptr_t)ap) != sizeof (int32_t) * nargs)
357		return (-1);
358
359	return (0);
360}
361
362/* ARGSUSED */
363int
364Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
365    uintptr_t ap)
366{
367	/* Do nothing */
368	return (0);
369}
370