xref: /titanic_44/usr/src/lib/libproc/sparcv9/Pisadep.c (revision fcdc8680ec9ba9c5b19abb1bfa18a8eaa0d06416)
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 #define	__sparcv9cpu
27 
28 #include <sys/stack.h>
29 #include <sys/regset.h>
30 #include <sys/frame.h>
31 #include <sys/sysmacros.h>
32 #include <sys/machelf.h>
33 
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <errno.h>
38 #include <string.h>
39 
40 #include "Pcontrol.h"
41 #include "Pstack.h"
42 #include "Pisadep.h"
43 #include "P32ton.h"
44 
45 #define	SYSCALL32 0x91d02008	/* 32-bit syscall (ta 8) instruction */
46 #define	SYSCALL64 0x91d02040	/* 64-bit syscall (ta 64) instruction */
47 
48 const char *
49 Ppltdest(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 	size_t i;
56 
57 	if (mp == NULL || (fp = mp->map_file) == NULL ||
58 	    fp->file_plt_base == 0 || pltaddr < fp->file_plt_base ||
59 	    pltaddr >= fp->file_plt_base + fp->file_plt_size) {
60 		errno = EINVAL;
61 		return (NULL);
62 	}
63 
64 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
65 		Elf64_Rela	r;
66 		uintptr_t	pltoff;
67 
68 		pltoff = pltaddr - fp->file_plt_base;
69 		if (pltoff < (M64_PLT_NEARPLTS * M64_PLT_ENTSIZE)) {
70 			i = (pltaddr - fp->file_plt_base -
71 			    M_PLT_XNumber * M64_PLT_ENTSIZE) / M64_PLT_ENTSIZE;
72 		} else {
73 			uintptr_t	pltblockoff;
74 			pltblockoff = pltoff - (M64_PLT_NEARPLTS *
75 			    M64_PLT_ENTSIZE);
76 			i = M64_PLT_NEARPLTS +
77 			    ((pltblockoff / M64_PLT_FBLOCKSZ) *
78 			    M64_PLT_FBLKCNTS) + ((pltblockoff %
79 			    M64_PLT_FBLOCKSZ) / M64_PLT_FENTSIZE) -
80 			    M_PLT_XNumber;
81 		}
82 
83 		r_addr = fp->file_jmp_rel + i * sizeof (Elf64_Rela);
84 
85 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
86 		    (i = ELF64_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
87 
88 			Elf_Data *data = fp->file_dynsym.sym_data_pri;
89 			Elf64_Sym *symp = &(((Elf64_Sym *)data->d_buf)[i]);
90 
91 			return (fp->file_dynsym.sym_strs + symp->st_name);
92 		}
93 
94 	} else /* PR_MODEL_ILP32 */ {
95 		Elf32_Rela r;
96 
97 		i = (pltaddr - fp->file_plt_base -
98 		    M_PLT_XNumber * M32_PLT_ENTSIZE) / M32_PLT_ENTSIZE;
99 
100 		r_addr = fp->file_jmp_rel + i * sizeof (Elf32_Rela);
101 
102 		if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
103 		    (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
104 
105 			Elf_Data *data = fp->file_dynsym.sym_data_pri;
106 			Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
107 
108 			return (fp->file_dynsym.sym_strs + symp->st_name);
109 		}
110 	}
111 
112 	return (NULL);
113 }
114 
115 int
116 Pissyscall(struct ps_prochandle *P, uintptr_t addr)
117 {
118 	instr_t sysinstr;
119 	instr_t instr;
120 
121 	if (P->status.pr_dmodel == PR_MODEL_LP64)
122 		sysinstr = SYSCALL64;
123 	else
124 		sysinstr = SYSCALL32;
125 
126 	if (Pread(P, &instr, sizeof (instr), addr) != sizeof (instr) ||
127 	    instr != sysinstr)
128 		return (0);
129 	else
130 		return (1);
131 }
132 
133 int
134 Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
135 {
136 	uintptr_t prevaddr = addr - sizeof (instr_t);
137 
138 	if (Pissyscall(P, prevaddr)) {
139 		if (dst)
140 			*dst = prevaddr;
141 		return (1);
142 	}
143 
144 	return (0);
145 }
146 
147 /* ARGSUSED */
148 int
149 Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
150 {
151 	instr_t sysinstr;
152 
153 	if (P->status.pr_dmodel == PR_MODEL_LP64)
154 		sysinstr = SYSCALL64;
155 	else
156 		sysinstr = SYSCALL32;
157 
158 	if (buflen >= sizeof (instr_t) &&
159 	    memcmp(buf, &sysinstr, sizeof (instr_t)) == 0)
160 		return (1);
161 	else
162 		return (0);
163 }
164 
165 /*
166  * For gwindows_t support, we define a structure to pass arguments to
167  * a Plwp_iter() callback routine.
168  */
169 typedef struct {
170 	struct ps_prochandle *gq_proc;	/* libproc handle */
171 	struct rwindow *gq_rwin;	/* rwindow destination buffer */
172 	uintptr_t gq_addr;		/* stack address to match */
173 } gwin_query_t;
174 
175 static int
176 find_gwin(gwin_query_t *gqp, const lwpstatus_t *psp)
177 {
178 	gwindows_t gwin;
179 	struct stat64 st;
180 	char path[64];
181 	ssize_t n;
182 	int fd, i;
183 	int rv = 0; /* Return value for skip to next lwp */
184 
185 	(void) snprintf(path, sizeof (path), "/proc/%d/lwp/%d/gwindows",
186 	    (int)gqp->gq_proc->pid, (int)psp->pr_lwpid);
187 
188 	if (stat64(path, &st) == -1 || st.st_size == 0)
189 		return (0); /* Nothing doing; skip to next lwp */
190 
191 	if ((fd = open64(path, O_RDONLY)) >= 0) {
192 		/*
193 		 * Zero out the gwindows_t because the gwindows file only has
194 		 * as much data as needed to represent the saved windows.
195 		 */
196 		if (gqp->gq_proc->status.pr_dmodel == PR_MODEL_ILP32) {
197 			gwindows32_t g32;
198 
199 			(void) memset(&g32, 0, sizeof (g32));
200 			if ((n = read(fd, &g32, sizeof (g32))) > 0)
201 				gwindows_32_to_n(&g32, &gwin);
202 
203 		} else {
204 			(void) memset(&gwin, 0, sizeof (gwin));
205 			n = read(fd, &gwin, sizeof (gwin));
206 		}
207 
208 		if (n > 0) {
209 			/*
210 			 * If we actually found a non-zero gwindows file and
211 			 * were able to read it, iterate through the buffers
212 			 * looking for a stack pointer match; if one is found,
213 			 * copy out the corresponding register window.
214 			 */
215 			for (i = 0; i < gwin.wbcnt; i++) {
216 				if (gwin.spbuf[i] == (greg_t *)gqp->gq_addr) {
217 					(void) memcpy(gqp->gq_rwin,
218 					    &gwin.wbuf[i],
219 					    sizeof (struct rwindow));
220 
221 					rv = 1; /* We're done */
222 					break;
223 				}
224 			}
225 		}
226 		(void) close(fd);
227 	}
228 
229 	return (rv);
230 }
231 
232 static int
233 read_gwin(struct ps_prochandle *P, struct rwindow *rwp, uintptr_t sp)
234 {
235 	gwin_query_t gq;
236 
237 	if (P->state == PS_DEAD) {
238 		lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
239 		uint_t n;
240 		int i;
241 
242 		for (n = 0; n < P->core->core_nlwp; n++, lwp = list_next(lwp)) {
243 			gwindows_t *gwin = lwp->lwp_gwins;
244 
245 			if (gwin == NULL)
246 				continue; /* No gwindows for this lwp */
247 
248 			/*
249 			 * If this lwp has gwindows associated with it, iterate
250 			 * through the buffers looking for a stack pointer
251 			 * match; if one is found, copy out the register window.
252 			 */
253 			for (i = 0; i < gwin->wbcnt; i++) {
254 				if (gwin->spbuf[i] == (greg_t *)sp) {
255 					(void) memcpy(rwp, &gwin->wbuf[i],
256 					    sizeof (struct rwindow));
257 					return (0); /* We're done */
258 				}
259 			}
260 		}
261 
262 		return (-1); /* No gwindows match found */
263 
264 	}
265 
266 	gq.gq_proc = P;
267 	gq.gq_rwin = rwp;
268 	gq.gq_addr = sp;
269 
270 	return (Plwp_iter(P, (proc_lwp_f *)find_gwin, &gq) ? 0 : -1);
271 }
272 
273 static void
274 ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
275 {
276 	const greg_t *gregs = &src->uc_mcontext.gregs[0];
277 
278 	dst[R_CCR] = gregs[REG_CCR];
279 	dst[R_ASI] = gregs[REG_ASI];
280 	dst[R_FPRS] = gregs[REG_FPRS];
281 	dst[R_PC] = gregs[REG_PC];
282 	dst[R_nPC] = gregs[REG_nPC];
283 	dst[R_Y] = gregs[REG_Y];
284 
285 	dst[R_G1] = gregs[REG_G1];
286 	dst[R_G2] = gregs[REG_G2];
287 	dst[R_G3] = gregs[REG_G3];
288 	dst[R_G4] = gregs[REG_G4];
289 	dst[R_G5] = gregs[REG_G5];
290 	dst[R_G6] = gregs[REG_G6];
291 	dst[R_G7] = gregs[REG_G7];
292 
293 	dst[R_O0] = gregs[REG_O0];
294 	dst[R_O1] = gregs[REG_O1];
295 	dst[R_O2] = gregs[REG_O2];
296 	dst[R_O3] = gregs[REG_O3];
297 	dst[R_O4] = gregs[REG_O4];
298 	dst[R_O5] = gregs[REG_O5];
299 	dst[R_O6] = gregs[REG_O6];
300 	dst[R_O7] = gregs[REG_O7];
301 }
302 
303 static void
304 ucontext_32_to_prgregs(const ucontext32_t *src, prgregset_t dst)
305 {
306 	/*
307 	 * We need to be very careful here to cast the greg32_t's (signed) to
308 	 * unsigned and then explicitly promote them as unsigned values.
309 	 */
310 	const greg32_t *gregs = &src->uc_mcontext.gregs[0];
311 
312 	dst[R_PSR] = (uint64_t)(uint32_t)gregs[REG_PSR];
313 	dst[R_PC] = (uint64_t)(uint32_t)gregs[REG_PC];
314 	dst[R_nPC] = (uint64_t)(uint32_t)gregs[REG_nPC];
315 	dst[R_Y] = (uint64_t)(uint32_t)gregs[REG_Y];
316 
317 	dst[R_G1] = (uint64_t)(uint32_t)gregs[REG_G1];
318 	dst[R_G2] = (uint64_t)(uint32_t)gregs[REG_G2];
319 	dst[R_G3] = (uint64_t)(uint32_t)gregs[REG_G3];
320 	dst[R_G4] = (uint64_t)(uint32_t)gregs[REG_G4];
321 	dst[R_G5] = (uint64_t)(uint32_t)gregs[REG_G5];
322 	dst[R_G6] = (uint64_t)(uint32_t)gregs[REG_G6];
323 	dst[R_G7] = (uint64_t)(uint32_t)gregs[REG_G7];
324 
325 	dst[R_O0] = (uint64_t)(uint32_t)gregs[REG_O0];
326 	dst[R_O1] = (uint64_t)(uint32_t)gregs[REG_O1];
327 	dst[R_O2] = (uint64_t)(uint32_t)gregs[REG_O2];
328 	dst[R_O3] = (uint64_t)(uint32_t)gregs[REG_O3];
329 	dst[R_O4] = (uint64_t)(uint32_t)gregs[REG_O4];
330 	dst[R_O5] = (uint64_t)(uint32_t)gregs[REG_O5];
331 	dst[R_O6] = (uint64_t)(uint32_t)gregs[REG_O6];
332 	dst[R_O7] = (uint64_t)(uint32_t)gregs[REG_O7];
333 }
334 
335 int
336 Pstack_iter(struct ps_prochandle *P, const prgregset_t regs,
337 	proc_stack_f *func, void *arg)
338 {
339 	prgreg_t *prevfp = NULL;
340 	uint_t pfpsize = 0;
341 	int nfp = 0;
342 	prgregset_t gregs;
343 	long args[6];
344 	prgreg_t fp;
345 	int i;
346 	int rv;
347 	uintptr_t sp;
348 	ssize_t n;
349 	uclist_t ucl;
350 	ucontext_t uc;
351 
352 	init_uclist(&ucl, P);
353 	(void) memcpy(gregs, regs, sizeof (gregs));
354 
355 	for (;;) {
356 		fp = gregs[R_FP];
357 		if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
358 			break;
359 
360 		for (i = 0; i < 6; i++)
361 			args[i] = gregs[R_I0 + i];
362 		if ((rv = func(arg, gregs, 6, args)) != 0)
363 			break;
364 
365 		gregs[R_PC] = gregs[R_I7];
366 		gregs[R_nPC] = gregs[R_PC] + 4;
367 		(void) memcpy(&gregs[R_O0], &gregs[R_I0], 8*sizeof (prgreg_t));
368 		if ((sp = gregs[R_FP]) == 0)
369 			break;
370 
371 		if (P->status.pr_dmodel == PR_MODEL_ILP32) {
372 			struct rwindow32 rw32;
373 			ucontext32_t uc32;
374 
375 			if (find_uclink(&ucl, sp +
376 			    SA32(sizeof (struct frame32))) &&
377 			    Pread(P, &uc32, sizeof (uc32), sp +
378 			    SA32(sizeof (struct frame32))) == sizeof (uc32)) {
379 				ucontext_32_to_prgregs(&uc32, gregs);
380 				sp = gregs[R_SP];
381 			}
382 
383 			n = Pread(P, &rw32, sizeof (struct rwindow32), sp);
384 
385 			if (n == sizeof (struct rwindow32)) {
386 				rwindow_32_to_n(&rw32,
387 				    (struct rwindow *)&gregs[R_L0]);
388 				continue;
389 			}
390 
391 		} else {
392 			sp += STACK_BIAS;
393 
394 			if (find_uclink(&ucl, sp + SA(sizeof (struct frame))) &&
395 			    Pread(P, &uc, sizeof (uc), sp +
396 			    SA(sizeof (struct frame))) == sizeof (uc)) {
397 				ucontext_n_to_prgregs(&uc, gregs);
398 				sp = gregs[R_SP] + STACK_BIAS;
399 			}
400 
401 			n = Pread(P, &gregs[R_L0], sizeof (struct rwindow), sp);
402 
403 			if (n == sizeof (struct rwindow))
404 				continue;
405 		}
406 
407 		/*
408 		 * If we get here, then our Pread of the register window
409 		 * failed.  If this is because the address was not mapped,
410 		 * then we attempt to read this window via any gwindows
411 		 * information we have.  If that too fails, abort our loop.
412 		 */
413 		if (n > 0)
414 			break;	/* Failed for reason other than not mapped */
415 
416 		if (read_gwin(P, (struct rwindow *)&gregs[R_L0], sp) == -1)
417 			break;	/* No gwindows match either */
418 	}
419 
420 	if (prevfp)
421 		free(prevfp);
422 
423 	free_uclist(&ucl);
424 	return (rv);
425 }
426 
427 uintptr_t
428 Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
429 {
430 	uintptr_t ret;
431 	int model = P->status.pr_dmodel;
432 
433 	if (model == PR_MODEL_LP64) {
434 		sp -= (nargs > 6)?
435 		    WINDOWSIZE64 + sizeof (int64_t) * nargs :
436 		    WINDOWSIZE64 + sizeof (int64_t) * 6;
437 		sp = PSTACK_ALIGN64(sp);
438 		ret = sp + WINDOWSIZE32 + sizeof (int32_t);
439 	} else {
440 		sp -= (nargs > 6)?
441 		    WINDOWSIZE32 + sizeof (int32_t) * (1 + nargs) :
442 		    WINDOWSIZE32 + sizeof (int32_t) * (1 + 6);
443 		sp = PSTACK_ALIGN32(sp);
444 		ret = sp + WINDOWSIZE64 + sizeof (int32_t);
445 	}
446 
447 	P->status.pr_lwp.pr_reg[R_G1] = sysindex;
448 	if (model == PR_MODEL_LP64)
449 		P->status.pr_lwp.pr_reg[R_SP] = sp - STACK_BIAS;
450 	else
451 		P->status.pr_lwp.pr_reg[R_SP] = sp;
452 	P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr;
453 	P->status.pr_lwp.pr_reg[R_nPC] = P->sysaddr + sizeof (instr_t);
454 
455 	return (ret);
456 }
457 
458 int
459 Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
460     uintptr_t ap)
461 {
462 	uint32_t arglist32[MAXARGS+2];
463 	uint64_t arglist64[MAXARGS+2];
464 	int i;
465 	argdes_t *adp;
466 	int model = P->status.pr_dmodel;
467 
468 	for (i = 0, adp = argp; i < nargs; i++, adp++) {
469 		arglist32[i] = (uint32_t)adp->arg_value;
470 		arglist64[i] = (uint64_t)adp->arg_value;
471 
472 		if (i < 6)
473 			(void) Pputareg(P, R_O0+i, adp->arg_value);
474 	}
475 
476 	if (model == PR_MODEL_LP64) {
477 		if (nargs > 6 &&
478 		    Pwrite(P, &arglist64[0], sizeof (int64_t) * nargs,
479 		    (uintptr_t)ap) != sizeof (int64_t) * nargs)
480 			return (-1);
481 	} else {
482 		if (nargs > 6 &&
483 		    Pwrite(P, &arglist32[0], sizeof (int32_t) * nargs,
484 		    (uintptr_t)ap) != sizeof (int32_t) * nargs)
485 			return (-1);
486 	}
487 
488 	return (0);
489 }
490 
491 /* ARGSUSED */
492 int
493 Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
494     uintptr_t ap)
495 {
496 	/* Do nothing */
497 	return (0);
498 }
499