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]
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>
34 #include <sys/types.h>
42 #define SYSCALL32 0x91d02008 /* 32-bit syscall (ta 8) instruction */
45 #define WINDOWSIZE32 (16 * sizeof (int32_t))
49 Ppltdest(struct ps_prochandle
*P
, uintptr_t pltaddr
)
51 map_info_t
*mp
= Paddr2mptr(P
, pltaddr
);
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
) {
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
);
83 Pissyscall(struct ps_prochandle
*P
, uintptr_t addr
)
90 if (Pread(P
, &instr
, sizeof (instr
), addr
) != sizeof (instr
) ||
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
)) {
113 Pissyscall_text(struct ps_prochandle
*P
, const void *buf
, size_t buflen
)
117 sysinstr
= SYSCALL32
;
119 if (buflen
>= sizeof (instr_t
) &&
120 memcmp(buf
, &sysinstr
, sizeof (instr_t
)) == 0)
127 * For gwindows_t support, we define a structure to pass arguments to
128 * a Plwp_iter() callback routine.
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 */
137 find_gwin(gwin_query_t
*gqp
, const lwpstatus_t
*psp
)
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
));
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
,
171 sizeof (struct rwindow
));
173 rv
= 1; /* We're done */
185 read_gwin(struct ps_prochandle
*P
, struct rwindow
*rwp
, uintptr_t sp
)
189 if (P
->state
== PS_DEAD
) {
190 core_info_t
*core
= P
->data
;
191 lwp_info_t
*lwp
= list_next(&core
->core_lwp_head
);
195 for (n
= 0; n
< core
->core_nlwp
; n
++, lwp
= list_next(lwp
)) {
196 gwindows_t
*gwin
= lwp
->lwp_gwins
;
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 */
223 return (Plwp_iter(P
, (proc_lwp_f
*)find_gwin
, &gq
) ? 0 : -1);
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
;
271 init_uclist(&ucl
, P
);
272 (void) memcpy(gregs
, regs
, sizeof (gregs
));
276 if (stack_loop(fp
, &prevfp
, &nfp
, &pfpsize
))
279 for (i
= 0; i
< 6; i
++)
280 args
[i
] = gregs
[R_I0
+ i
];
281 if ((rv
= func(arg
, gregs
, 6, args
)) != 0)
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)
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
))
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.
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 */
325 Psyscall_setup(struct ps_prochandle
*P
, int nargs
, int sysindex
, uintptr_t sp
)
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
,
344 uint32_t arglist
[MAXARGS
+2];
348 for (i
= 0, adp
= argp
; i
< nargs
; i
++, adp
++) {
349 arglist
[i
] = adp
->arg_value
;
352 (void) Pputareg(P
, R_O0
+i
, adp
->arg_value
);
356 Pwrite(P
, &arglist
[0], sizeof (int32_t) * nargs
,
357 (uintptr_t)ap
) != sizeof (int32_t) * nargs
)
365 Psyscall_copyoutargs(struct ps_prochandle
*P
, int nargs
, argdes_t
*argp
,