8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libproc / sparc / Pisadep.c
blobc3bbd997881a765c5052a2cc81d839fbbd83cc1b
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <errno.h>
36 #include <string.h>
38 #include "Pcontrol.h"
39 #include "Pstack.h"
40 #include "Pisadep.h"
42 #define SYSCALL32 0x91d02008 /* 32-bit syscall (ta 8) instruction */
44 #ifndef WINDOWSIZE32
45 #define WINDOWSIZE32 (16 * sizeof (int32_t))
46 #endif
48 const char *
49 Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr)
51 map_info_t *mp = Paddr2mptr(P, pltaddr);
53 uintptr_t r_addr;
54 file_info_t *fp;
55 Elf32_Rela r;
56 size_t i;
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);
65 i = (pltaddr - fp->file_plt_base -
66 M_PLT_XNumber * M32_PLT_ENTSIZE) / M32_PLT_ENTSIZE;
68 r_addr = fp->file_jmp_rel + i * sizeof (Elf32_Rela);
70 if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
71 (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
73 Elf_Data *data = fp->file_dynsym.sym_data_pri;
74 Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
76 return (fp->file_dynsym.sym_strs + symp->st_name);
79 return (NULL);
82 int
83 Pissyscall(struct ps_prochandle *P, uintptr_t addr)
85 instr_t sysinstr;
86 instr_t instr;
88 sysinstr = SYSCALL32;
90 if (Pread(P, &instr, sizeof (instr), addr) != sizeof (instr) ||
91 instr != sysinstr)
92 return (0);
93 else
94 return (1);
97 int
98 Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
100 uintptr_t prevaddr = addr - sizeof (instr_t);
102 if (Pissyscall(P, prevaddr)) {
103 if (dst)
104 *dst = prevaddr;
105 return (1);
108 return (0);
111 /* ARGSUSED */
113 Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
115 instr_t sysinstr;
117 sysinstr = SYSCALL32;
119 if (buflen >= sizeof (instr_t) &&
120 memcmp(buf, &sysinstr, sizeof (instr_t)) == 0)
121 return (1);
122 else
123 return (0);
127 * For gwindows_t support, we define a structure to pass arguments to
128 * a Plwp_iter() callback routine.
130 typedef 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;
136 static int
137 find_gwin(gwin_query_t *gqp, const lwpstatus_t *psp)
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 */
146 (void) snprintf(path, sizeof (path), "/proc/%d/lwp/%d/gwindows",
147 (int)gqp->gq_proc->pid, (int)psp->pr_lwpid);
149 if (stat64(path, &st) == -1 || st.st_size == 0)
150 return (0); /* Nothing doing; skip to next lwp */
152 if ((fd = open64(path, O_RDONLY)) >= 0) {
154 * Zero out the gwindows_t because the gwindows file only has
155 * as much data as needed to represent the saved windows.
157 (void) memset(&gwin, 0, sizeof (gwin));
158 n = read(fd, &gwin, sizeof (gwin));
160 if (n > 0) {
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.
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));
173 rv = 1; /* We're done */
174 break;
178 (void) close(fd);
181 return (rv);
184 static int
185 read_gwin(struct ps_prochandle *P, struct rwindow *rwp, uintptr_t sp)
187 gwin_query_t gq;
189 if (P->state == PS_DEAD) {
190 core_info_t *core = P->data;
191 lwp_info_t *lwp = list_next(&core->core_lwp_head);
192 uint_t n;
193 int i;
195 for (n = 0; n < core->core_nlwp; n++, lwp = list_next(lwp)) {
196 gwindows_t *gwin = lwp->lwp_gwins;
198 if (gwin == NULL)
199 continue; /* No gwindows for this lwp */
202 * If this lwp has gwindows associated with it, iterate
203 * through the buffers looking for a stack pointer
204 * match; if one is found, copy out the register window.
206 for (i = 0; i < gwin->wbcnt; i++) {
207 if (gwin->spbuf[i] == (greg_t *)sp) {
208 (void) memcpy(rwp, &gwin->wbuf[i],
209 sizeof (struct rwindow));
210 return (0); /* We're done */
215 return (-1); /* No gwindows match found */
219 gq.gq_proc = P;
220 gq.gq_rwin = rwp;
221 gq.gq_addr = sp;
223 return (Plwp_iter(P, (proc_lwp_f *)find_gwin, &gq) ? 0 : -1);
226 static void
227 ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
229 const greg_t *gregs = &src->uc_mcontext.gregs[0];
231 dst[R_PSR] = gregs[REG_PSR];
232 dst[R_PC] = gregs[REG_PC];
233 dst[R_nPC] = gregs[REG_nPC];
234 dst[R_Y] = gregs[REG_Y];
236 dst[R_G1] = gregs[REG_G1];
237 dst[R_G2] = gregs[REG_G2];
238 dst[R_G3] = gregs[REG_G3];
239 dst[R_G4] = gregs[REG_G4];
240 dst[R_G5] = gregs[REG_G5];
241 dst[R_G6] = gregs[REG_G6];
242 dst[R_G7] = gregs[REG_G7];
244 dst[R_O0] = gregs[REG_O0];
245 dst[R_O1] = gregs[REG_O1];
246 dst[R_O2] = gregs[REG_O2];
247 dst[R_O3] = gregs[REG_O3];
248 dst[R_O4] = gregs[REG_O4];
249 dst[R_O5] = gregs[REG_O5];
250 dst[R_O6] = gregs[REG_O6];
251 dst[R_O7] = gregs[REG_O7];
255 Pstack_iter(struct ps_prochandle *P, const prgregset_t regs,
256 proc_stack_f *func, void *arg)
258 prgreg_t *prevfp = NULL;
259 uint_t pfpsize = 0;
260 int nfp = 0;
261 prgregset_t gregs;
262 long args[6];
263 prgreg_t fp;
264 int i;
265 int rv;
266 uintptr_t sp;
267 ssize_t n;
268 uclist_t ucl;
269 ucontext_t uc;
271 init_uclist(&ucl, P);
272 (void) memcpy(gregs, regs, sizeof (gregs));
274 for (;;) {
275 fp = gregs[R_FP];
276 if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
277 break;
279 for (i = 0; i < 6; i++)
280 args[i] = gregs[R_I0 + i];
281 if ((rv = func(arg, gregs, 6, args)) != 0)
282 break;
284 gregs[R_PC] = gregs[R_I7];
285 gregs[R_nPC] = gregs[R_PC] + 4;
286 (void) memcpy(&gregs[R_O0], &gregs[R_I0], 8*sizeof (prgreg_t));
287 if ((sp = gregs[R_FP]) == 0)
288 break;
290 sp += STACK_BIAS;
292 if (find_uclink(&ucl, sp + SA(sizeof (struct frame))) &&
293 Pread(P, &uc, sizeof (uc), sp +
294 SA(sizeof (struct frame))) == sizeof (uc)) {
295 ucontext_n_to_prgregs(&uc, gregs);
296 sp = gregs[R_SP] + STACK_BIAS;
299 n = Pread(P, &gregs[R_L0], sizeof (struct rwindow), sp);
301 if (n == sizeof (struct rwindow))
302 continue;
305 * If we get here, then our Pread of the register window
306 * failed. If this is because the address was not mapped,
307 * then we attempt to read this window via any gwindows
308 * information we have. If that too fails, abort our loop.
310 if (n > 0)
311 break; /* Failed for reason other than not mapped */
313 if (read_gwin(P, (struct rwindow *)&gregs[R_L0], sp) == -1)
314 break; /* No gwindows match either */
317 if (prevfp)
318 free(prevfp);
320 free_uclist(&ucl);
321 return (rv);
324 uintptr_t
325 Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
327 sp -= (nargs > 6)?
328 WINDOWSIZE32 + sizeof (int32_t) * (1 + nargs) :
329 WINDOWSIZE32 + sizeof (int32_t) * (1 + 6);
330 sp = PSTACK_ALIGN32(sp);
332 P->status.pr_lwp.pr_reg[R_G1] = sysindex;
333 P->status.pr_lwp.pr_reg[R_SP] = sp;
334 P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr;
335 P->status.pr_lwp.pr_reg[R_nPC] = P->sysaddr + sizeof (instr_t);
337 return (sp + WINDOWSIZE32 + sizeof (int32_t));
341 Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
342 uintptr_t ap)
344 uint32_t arglist[MAXARGS+2];
345 int i;
346 argdes_t *adp;
348 for (i = 0, adp = argp; i < nargs; i++, adp++) {
349 arglist[i] = adp->arg_value;
351 if (i < 6)
352 (void) Pputareg(P, R_O0+i, adp->arg_value);
355 if (nargs > 6 &&
356 Pwrite(P, &arglist[0], sizeof (int32_t) * nargs,
357 (uintptr_t)ap) != sizeof (int32_t) * nargs)
358 return (-1);
360 return (0);
363 /* ARGSUSED */
365 Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
366 uintptr_t ap)
368 /* Do nothing */
369 return (0);