Pisadep.c revision 2
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#define __sparcv9cpu
2N/A
2N/A#include <sys/stack.h>
2N/A#include <sys/regset.h>
2N/A#include <sys/frame.h>
2N/A#include <sys/sysmacros.h>
2N/A#include <sys/machelf.h>
2N/A
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A#include <sys/types.h>
2N/A#include <errno.h>
2N/A#include <string.h>
2N/A
2N/A#include "Pcontrol.h"
2N/A#include "Pstack.h"
2N/A#include "Pisadep.h"
2N/A#include "P32ton.h"
2N/A
2N/A#define SYSCALL32 0x91d02008 /* 32-bit syscall (ta 8) instruction */
2N/A#define SYSCALL64 0x91d02040 /* 64-bit syscall (ta 64) instruction */
2N/A
2N/Aconst char *
2N/APpltdest(struct ps_prochandle *P, uintptr_t pltaddr)
2N/A{
2N/A map_info_t *mp = Paddr2mptr(P, pltaddr);
2N/A
2N/A uintptr_t r_addr;
2N/A file_info_t *fp;
2N/A size_t i;
2N/A
2N/A if (mp == NULL || (fp = mp->map_file) == NULL ||
2N/A fp->file_plt_base == 0 || pltaddr < fp->file_plt_base ||
2N/A pltaddr >= fp->file_plt_base + fp->file_plt_size) {
2N/A errno = EINVAL;
2N/A return (NULL);
2N/A }
2N/A
2N/A if (P->status.pr_dmodel == PR_MODEL_LP64) {
2N/A Elf64_Rela r;
2N/A uintptr_t pltoff;
2N/A
2N/A pltoff = pltaddr - fp->file_plt_base;
2N/A if (pltoff < (M64_PLT_NEARPLTS * M64_PLT_ENTSIZE)) {
2N/A i = (pltaddr - fp->file_plt_base -
2N/A M_PLT_XNumber * M64_PLT_ENTSIZE) / M64_PLT_ENTSIZE;
2N/A } else {
2N/A uintptr_t pltblockoff;
2N/A pltblockoff = pltoff - (M64_PLT_NEARPLTS *
2N/A M64_PLT_ENTSIZE);
2N/A i = M64_PLT_NEARPLTS +
2N/A ((pltblockoff / M64_PLT_FBLOCKSZ) *
2N/A M64_PLT_FBLKCNTS) + ((pltblockoff %
2N/A M64_PLT_FBLOCKSZ) / M64_PLT_FENTSIZE) -
2N/A M_PLT_XNumber;
2N/A }
2N/A
2N/A r_addr = fp->file_jmp_rel + i * sizeof (Elf64_Rela);
2N/A
2N/A if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
2N/A (i = ELF64_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
2N/A
2N/A Elf_Data *data = fp->file_dynsym.sym_data_pri;
2N/A Elf64_Sym *symp = &(((Elf64_Sym *)data->d_buf)[i]);
2N/A
2N/A return (fp->file_dynsym.sym_strs + symp->st_name);
2N/A }
2N/A
2N/A } else /* PR_MODEL_ILP32 */ {
2N/A Elf32_Rela r;
2N/A
2N/A i = (pltaddr - fp->file_plt_base -
2N/A M_PLT_XNumber * M32_PLT_ENTSIZE) / M32_PLT_ENTSIZE;
2N/A
2N/A r_addr = fp->file_jmp_rel + i * sizeof (Elf32_Rela);
2N/A
2N/A if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) &&
2N/A (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) {
2N/A
2N/A Elf_Data *data = fp->file_dynsym.sym_data_pri;
2N/A Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]);
2N/A
2N/A return (fp->file_dynsym.sym_strs + symp->st_name);
2N/A }
2N/A }
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/Aint
2N/APissyscall(struct ps_prochandle *P, uintptr_t addr)
2N/A{
2N/A instr_t sysinstr;
2N/A instr_t instr;
2N/A
2N/A if (P->status.pr_dmodel == PR_MODEL_LP64)
2N/A sysinstr = SYSCALL64;
2N/A else
2N/A sysinstr = SYSCALL32;
2N/A
2N/A if (Pread(P, &instr, sizeof (instr), addr) != sizeof (instr) ||
2N/A instr != sysinstr)
2N/A return (0);
2N/A else
2N/A return (1);
2N/A}
2N/A
2N/Aint
2N/APissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst)
2N/A{
2N/A uintptr_t prevaddr = addr - sizeof (instr_t);
2N/A
2N/A if (Pissyscall(P, prevaddr)) {
2N/A if (dst)
2N/A *dst = prevaddr;
2N/A return (1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Aint
2N/APissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen)
2N/A{
2N/A instr_t sysinstr;
2N/A
2N/A if (P->status.pr_dmodel == PR_MODEL_LP64)
2N/A sysinstr = SYSCALL64;
2N/A else
2N/A sysinstr = SYSCALL32;
2N/A
2N/A if (buflen >= sizeof (instr_t) &&
2N/A memcmp(buf, &sysinstr, sizeof (instr_t)) == 0)
2N/A return (1);
2N/A else
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * For gwindows_t support, we define a structure to pass arguments to
2N/A * a Plwp_iter() callback routine.
2N/A */
2N/Atypedef struct {
2N/A struct ps_prochandle *gq_proc; /* libproc handle */
2N/A struct rwindow *gq_rwin; /* rwindow destination buffer */
2N/A uintptr_t gq_addr; /* stack address to match */
2N/A} gwin_query_t;
2N/A
2N/Astatic int
2N/Afind_gwin(gwin_query_t *gqp, const lwpstatus_t *psp)
2N/A{
2N/A gwindows_t gwin;
2N/A struct stat64 st;
2N/A char path[64];
2N/A ssize_t n;
2N/A int fd, i;
2N/A int rv = 0; /* Return value for skip to next lwp */
2N/A
2N/A (void) snprintf(path, sizeof (path), "/proc/%d/lwp/%d/gwindows",
2N/A (int)gqp->gq_proc->pid, (int)psp->pr_lwpid);
2N/A
2N/A if (stat64(path, &st) == -1 || st.st_size == 0)
2N/A return (0); /* Nothing doing; skip to next lwp */
2N/A
2N/A if ((fd = open64(path, O_RDONLY)) >= 0) {
2N/A /*
2N/A * Zero out the gwindows_t because the gwindows file only has
2N/A * as much data as needed to represent the saved windows.
2N/A */
2N/A if (gqp->gq_proc->status.pr_dmodel == PR_MODEL_ILP32) {
2N/A gwindows32_t g32;
2N/A
2N/A (void) memset(&g32, 0, sizeof (g32));
2N/A if ((n = read(fd, &g32, sizeof (g32))) > 0)
2N/A gwindows_32_to_n(&g32, &gwin);
2N/A
2N/A } else {
2N/A (void) memset(&gwin, 0, sizeof (gwin));
2N/A n = read(fd, &gwin, sizeof (gwin));
2N/A }
2N/A
2N/A if (n > 0) {
2N/A /*
2N/A * If we actually found a non-zero gwindows file and
2N/A * were able to read it, iterate through the buffers
2N/A * looking for a stack pointer match; if one is found,
2N/A * copy out the corresponding register window.
2N/A */
2N/A for (i = 0; i < gwin.wbcnt; i++) {
2N/A if (gwin.spbuf[i] == (greg_t *)gqp->gq_addr) {
2N/A (void) memcpy(gqp->gq_rwin,
2N/A &gwin.wbuf[i],
2N/A sizeof (struct rwindow));
2N/A
2N/A rv = 1; /* We're done */
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A (void) close(fd);
2N/A }
2N/A
2N/A return (rv);
2N/A}
2N/A
2N/Astatic int
2N/Aread_gwin(struct ps_prochandle *P, struct rwindow *rwp, uintptr_t sp)
2N/A{
2N/A gwin_query_t gq;
2N/A
2N/A if (P->state == PS_DEAD) {
2N/A lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
2N/A uint_t n;
2N/A int i;
2N/A
2N/A for (n = 0; n < P->core->core_nlwp; n++, lwp = list_next(lwp)) {
2N/A gwindows_t *gwin = lwp->lwp_gwins;
2N/A
2N/A if (gwin == NULL)
2N/A continue; /* No gwindows for this lwp */
2N/A
2N/A /*
2N/A * If this lwp has gwindows associated with it, iterate
2N/A * through the buffers looking for a stack pointer
2N/A * match; if one is found, copy out the register window.
2N/A */
2N/A for (i = 0; i < gwin->wbcnt; i++) {
2N/A if (gwin->spbuf[i] == (greg_t *)sp) {
2N/A (void) memcpy(rwp, &gwin->wbuf[i],
2N/A sizeof (struct rwindow));
2N/A return (0); /* We're done */
2N/A }
2N/A }
2N/A }
2N/A
2N/A return (-1); /* No gwindows match found */
2N/A
2N/A }
2N/A
2N/A gq.gq_proc = P;
2N/A gq.gq_rwin = rwp;
2N/A gq.gq_addr = sp;
2N/A
2N/A return (Plwp_iter(P, (proc_lwp_f *)find_gwin, &gq) ? 0 : -1);
2N/A}
2N/A
2N/Astatic void
2N/Aucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst)
2N/A{
2N/A const greg_t *gregs = &src->uc_mcontext.gregs[0];
2N/A
2N/A dst[R_CCR] = gregs[REG_CCR];
2N/A dst[R_ASI] = gregs[REG_ASI];
2N/A dst[R_FPRS] = gregs[REG_FPRS];
2N/A dst[R_PC] = gregs[REG_PC];
2N/A dst[R_nPC] = gregs[REG_nPC];
2N/A dst[R_Y] = gregs[REG_Y];
2N/A
2N/A dst[R_G1] = gregs[REG_G1];
2N/A dst[R_G2] = gregs[REG_G2];
2N/A dst[R_G3] = gregs[REG_G3];
2N/A dst[R_G4] = gregs[REG_G4];
2N/A dst[R_G5] = gregs[REG_G5];
2N/A dst[R_G6] = gregs[REG_G6];
2N/A dst[R_G7] = gregs[REG_G7];
2N/A
2N/A dst[R_O0] = gregs[REG_O0];
2N/A dst[R_O1] = gregs[REG_O1];
2N/A dst[R_O2] = gregs[REG_O2];
2N/A dst[R_O3] = gregs[REG_O3];
2N/A dst[R_O4] = gregs[REG_O4];
2N/A dst[R_O5] = gregs[REG_O5];
2N/A dst[R_O6] = gregs[REG_O6];
2N/A dst[R_O7] = gregs[REG_O7];
2N/A}
2N/A
2N/Astatic void
2N/Aucontext_32_to_prgregs(const ucontext32_t *src, prgregset_t dst)
2N/A{
2N/A /*
2N/A * We need to be very careful here to cast the greg32_t's (signed) to
2N/A * unsigned and then explicitly promote them as unsigned values.
2N/A */
2N/A const greg32_t *gregs = &src->uc_mcontext.gregs[0];
2N/A
2N/A dst[R_PSR] = (uint64_t)(uint32_t)gregs[REG_PSR];
2N/A dst[R_PC] = (uint64_t)(uint32_t)gregs[REG_PC];
2N/A dst[R_nPC] = (uint64_t)(uint32_t)gregs[REG_nPC];
2N/A dst[R_Y] = (uint64_t)(uint32_t)gregs[REG_Y];
2N/A
2N/A dst[R_G1] = (uint64_t)(uint32_t)gregs[REG_G1];
2N/A dst[R_G2] = (uint64_t)(uint32_t)gregs[REG_G2];
2N/A dst[R_G3] = (uint64_t)(uint32_t)gregs[REG_G3];
2N/A dst[R_G4] = (uint64_t)(uint32_t)gregs[REG_G4];
2N/A dst[R_G5] = (uint64_t)(uint32_t)gregs[REG_G5];
2N/A dst[R_G6] = (uint64_t)(uint32_t)gregs[REG_G6];
2N/A dst[R_G7] = (uint64_t)(uint32_t)gregs[REG_G7];
2N/A
2N/A dst[R_O0] = (uint64_t)(uint32_t)gregs[REG_O0];
2N/A dst[R_O1] = (uint64_t)(uint32_t)gregs[REG_O1];
2N/A dst[R_O2] = (uint64_t)(uint32_t)gregs[REG_O2];
2N/A dst[R_O3] = (uint64_t)(uint32_t)gregs[REG_O3];
2N/A dst[R_O4] = (uint64_t)(uint32_t)gregs[REG_O4];
2N/A dst[R_O5] = (uint64_t)(uint32_t)gregs[REG_O5];
2N/A dst[R_O6] = (uint64_t)(uint32_t)gregs[REG_O6];
2N/A dst[R_O7] = (uint64_t)(uint32_t)gregs[REG_O7];
2N/A}
2N/A
2N/Aint
2N/APstack_iter(struct ps_prochandle *P, const prgregset_t regs,
2N/A proc_stack_f *func, void *arg)
2N/A{
2N/A prgreg_t *prevfp = NULL;
2N/A uint_t pfpsize = 0;
2N/A int nfp = 0;
2N/A prgregset_t gregs;
2N/A long args[6];
2N/A prgreg_t fp;
2N/A int i;
2N/A int rv;
2N/A uintptr_t sp;
2N/A ssize_t n;
2N/A uclist_t ucl;
2N/A ucontext_t uc;
2N/A uintptr_t uc_addr;
2N/A GElf_Sym sigh;
2N/A int frame_flags = 0;
2N/A int sig; /* ignored unless (frame_flags & PR_FOUND_SIGNAL) */
2N/A
2N/A init_uclist(&ucl, P);
2N/A (void) memcpy(gregs, regs, sizeof (gregs));
2N/A (void) memset(&sigh, 0, sizeof (GElf_Sym));
2N/A (void) Plookup_by_name(P, "libc.so", "__sighndlr", &sigh);
2N/A
2N/A for (;;) {
2N/A fp = gregs[R_FP];
2N/A if (stack_loop(fp, &prevfp, &nfp, &pfpsize))
2N/A break;
2N/A
2N/A if (gregs[R_PC] - sigh.st_value < sigh.st_size) {
2N/A frame_flags = PR_FOUND_SIGNAL;
2N/A sig = gregs[R_I0];
2N/A }
2N/A
2N/A uc_addr = (P->status.pr_dmodel == PR_MODEL_ILP32) ?
2N/A fp + SA32(sizeof (struct frame32)) :
2N/A fp + STACK_BIAS + SA(sizeof (struct frame));
2N/A if (find_uclink(&ucl, uc_addr))
2N/A frame_flags |= PR_SIGNAL_FRAME;
2N/A else
2N/A uc_addr = NULL;
2N/A
2N/A for (i = 0; i < 6; i++)
2N/A args[i] = gregs[R_I0 + i];
2N/A if ((rv = func(arg, gregs, 6, args, frame_flags, sig)) != 0)
2N/A break;
2N/A
2N/A if (frame_flags & PR_SIGNAL_FRAME)
2N/A frame_flags = 0;
2N/A
2N/A gregs[R_PC] = gregs[R_I7];
2N/A gregs[R_nPC] = gregs[R_PC] + 4;
2N/A (void) memcpy(&gregs[R_O0], &gregs[R_I0], 8*sizeof (prgreg_t));
2N/A if ((sp = gregs[R_FP]) == 0)
2N/A break;
2N/A
2N/A if (P->status.pr_dmodel == PR_MODEL_ILP32) {
2N/A struct rwindow32 rw32;
2N/A ucontext32_t uc32;
2N/A
2N/A if (uc_addr != NULL &&
2N/A Pread(P, &uc32, sizeof (uc32), uc_addr) ==
2N/A sizeof (uc32)) {
2N/A ucontext_32_to_prgregs(&uc32, gregs);
2N/A sp = gregs[R_SP];
2N/A }
2N/A
2N/A n = Pread(P, &rw32, sizeof (struct rwindow32), sp);
2N/A
2N/A if (n == sizeof (struct rwindow32)) {
2N/A rwindow_32_to_n(&rw32,
2N/A (struct rwindow *)&gregs[R_L0]);
2N/A continue;
2N/A }
2N/A
2N/A } else {
2N/A sp += STACK_BIAS;
2N/A
2N/A if (uc_addr != NULL &&
2N/A Pread(P, &uc, sizeof (uc), uc_addr) ==
2N/A sizeof (uc)) {
2N/A ucontext_n_to_prgregs(&uc, gregs);
2N/A sp = gregs[R_SP] + STACK_BIAS;
2N/A }
2N/A
2N/A n = Pread(P, &gregs[R_L0], sizeof (struct rwindow), sp);
2N/A
2N/A if (n == sizeof (struct rwindow))
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * If we get here, then our Pread of the register window
2N/A * failed. If this is because the address was not mapped,
2N/A * then we attempt to read this window via any gwindows
2N/A * information we have. If that too fails, abort our loop.
2N/A */
2N/A if (n > 0)
2N/A break; /* Failed for reason other than not mapped */
2N/A
2N/A if (read_gwin(P, (struct rwindow *)&gregs[R_L0], sp) == -1)
2N/A break; /* No gwindows match either */
2N/A }
2N/A
2N/A if (prevfp)
2N/A free(prevfp);
2N/A
2N/A free_uclist(&ucl);
2N/A return (rv);
2N/A}
2N/A
2N/Auintptr_t
2N/APsyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp)
2N/A{
2N/A uintptr_t ret;
2N/A int model = P->status.pr_dmodel;
2N/A
2N/A if (model == PR_MODEL_LP64) {
2N/A sp -= (nargs > 6)?
2N/A WINDOWSIZE64 + sizeof (int64_t) * nargs :
2N/A WINDOWSIZE64 + sizeof (int64_t) * 6;
2N/A sp = PSTACK_ALIGN64(sp);
2N/A ret = sp + WINDOWSIZE32 + sizeof (int32_t);
2N/A } else {
2N/A sp -= (nargs > 6)?
2N/A WINDOWSIZE32 + sizeof (int32_t) * (1 + nargs) :
2N/A WINDOWSIZE32 + sizeof (int32_t) * (1 + 6);
2N/A sp = PSTACK_ALIGN32(sp);
2N/A ret = sp + WINDOWSIZE64 + sizeof (int32_t);
2N/A }
2N/A
2N/A P->status.pr_lwp.pr_reg[R_G1] = sysindex;
2N/A if (model == PR_MODEL_LP64)
2N/A P->status.pr_lwp.pr_reg[R_SP] = sp - STACK_BIAS;
2N/A else
2N/A P->status.pr_lwp.pr_reg[R_SP] = sp;
2N/A P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr;
2N/A P->status.pr_lwp.pr_reg[R_nPC] = P->sysaddr + sizeof (instr_t);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Aint
2N/APsyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
2N/A uintptr_t ap)
2N/A{
2N/A uint32_t arglist32[MAXARGS+2];
2N/A uint64_t arglist64[MAXARGS+2];
2N/A int i;
2N/A argdes_t *adp;
2N/A int model = P->status.pr_dmodel;
2N/A
2N/A for (i = 0, adp = argp; i < nargs; i++, adp++) {
2N/A arglist32[i] = (uint32_t)adp->arg_value;
2N/A arglist64[i] = (uint64_t)adp->arg_value;
2N/A
2N/A if (i < 6)
2N/A (void) Pputareg(P, R_O0+i, adp->arg_value);
2N/A }
2N/A
2N/A if (model == PR_MODEL_LP64) {
2N/A if (nargs > 6 &&
2N/A Pwrite(P, &arglist64[0], sizeof (int64_t) * nargs,
2N/A (uintptr_t)ap) != sizeof (int64_t) * nargs)
2N/A return (-1);
2N/A } else {
2N/A if (nargs > 6 &&
2N/A Pwrite(P, &arglist32[0], sizeof (int32_t) * nargs,
2N/A (uintptr_t)ap) != sizeof (int32_t) * nargs)
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Aint
2N/APsyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp,
2N/A uintptr_t ap)
2N/A{
2N/A /* Do nothing */
2N/A return (0);
2N/A}