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