linux/arch/avr32/kernel/ptrace.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2004-2006 Atmel Corporation
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License version 2 as
   6 * published by the Free Software Foundation.
   7 */
   8#undef DEBUG
   9#include <linux/kernel.h>
  10#include <linux/sched.h>
  11#include <linux/mm.h>
  12#include <linux/ptrace.h>
  13#include <linux/errno.h>
  14#include <linux/user.h>
  15#include <linux/security.h>
  16#include <linux/unistd.h>
  17#include <linux/notifier.h>
  18
  19#include <asm/traps.h>
  20#include <asm/uaccess.h>
  21#include <asm/ocd.h>
  22#include <asm/mmu_context.h>
  23#include <linux/kdebug.h>
  24
  25static struct pt_regs *get_user_regs(struct task_struct *tsk)
  26{
  27        return (struct pt_regs *)((unsigned long)task_stack_page(tsk) +
  28                                  THREAD_SIZE - sizeof(struct pt_regs));
  29}
  30
  31static void ptrace_single_step(struct task_struct *tsk)
  32{
  33        pr_debug("ptrace_single_step: pid=%u, PC=0x%08lx, SR=0x%08lx\n",
  34                 tsk->pid, task_pt_regs(tsk)->pc, task_pt_regs(tsk)->sr);
  35
  36        /*
  37         * We can't schedule in Debug mode, so when TIF_BREAKPOINT is
  38         * set, the system call or exception handler will do a
  39         * breakpoint to enter monitor mode before returning to
  40         * userspace.
  41         *
  42         * The monitor code will then notice that TIF_SINGLE_STEP is
  43         * set and return to userspace with single stepping enabled.
  44         * The CPU will then enter monitor mode again after exactly
  45         * one instruction has been executed, and the monitor code
  46         * will then send a SIGTRAP to the process.
  47         */
  48        set_tsk_thread_flag(tsk, TIF_BREAKPOINT);
  49        set_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
  50}
  51
  52/*
  53 * Called by kernel/ptrace.c when detaching
  54 *
  55 * Make sure any single step bits, etc. are not set
  56 */
  57void ptrace_disable(struct task_struct *child)
  58{
  59        clear_tsk_thread_flag(child, TIF_SINGLE_STEP);
  60        clear_tsk_thread_flag(child, TIF_BREAKPOINT);
  61        ocd_disable(child);
  62}
  63
  64/*
  65 * Read the word at offset "offset" into the task's "struct user". We
  66 * actually access the pt_regs struct stored on the kernel stack.
  67 */
  68static int ptrace_read_user(struct task_struct *tsk, unsigned long offset,
  69                            unsigned long __user *data)
  70{
  71        unsigned long *regs;
  72        unsigned long value;
  73
  74        if (offset & 3 || offset >= sizeof(struct user)) {
  75                printk("ptrace_read_user: invalid offset 0x%08lx\n", offset);
  76                return -EIO;
  77        }
  78
  79        regs = (unsigned long *)get_user_regs(tsk);
  80
  81        value = 0;
  82        if (offset < sizeof(struct pt_regs))
  83                value = regs[offset / sizeof(regs[0])];
  84
  85        pr_debug("ptrace_read_user(%s[%u], %#lx, %p) -> %#lx\n",
  86                 tsk->comm, tsk->pid, offset, data, value);
  87
  88        return put_user(value, data);
  89}
  90
  91/*
  92 * Write the word "value" to offset "offset" into the task's "struct
  93 * user". We actually access the pt_regs struct stored on the kernel
  94 * stack.
  95 */
  96static int ptrace_write_user(struct task_struct *tsk, unsigned long offset,
  97                             unsigned long value)
  98{
  99        unsigned long *regs;
 100
 101        pr_debug("ptrace_write_user(%s[%u], %#lx, %#lx)\n",
 102                        tsk->comm, tsk->pid, offset, value);
 103
 104        if (offset & 3 || offset >= sizeof(struct user)) {
 105                pr_debug("  invalid offset 0x%08lx\n", offset);
 106                return -EIO;
 107        }
 108
 109        if (offset >= sizeof(struct pt_regs))
 110                return 0;
 111
 112        regs = (unsigned long *)get_user_regs(tsk);
 113        regs[offset / sizeof(regs[0])] = value;
 114
 115        return 0;
 116}
 117
 118static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
 119{
 120        struct pt_regs *regs = get_user_regs(tsk);
 121
 122        return copy_to_user(uregs, regs, sizeof(*regs)) ? -EFAULT : 0;
 123}
 124
 125static int ptrace_setregs(struct task_struct *tsk, const void __user *uregs)
 126{
 127        struct pt_regs newregs;
 128        int ret;
 129
 130        ret = -EFAULT;
 131        if (copy_from_user(&newregs, uregs, sizeof(newregs)) == 0) {
 132                struct pt_regs *regs = get_user_regs(tsk);
 133
 134                ret = -EINVAL;
 135                if (valid_user_regs(&newregs)) {
 136                        *regs = newregs;
 137                        ret = 0;
 138                }
 139        }
 140
 141        return ret;
 142}
 143
 144long arch_ptrace(struct task_struct *child, long request, long addr, long data)
 145{
 146        int ret;
 147
 148        switch (request) {
 149        /* Read the word at location addr in the child process */
 150        case PTRACE_PEEKTEXT:
 151        case PTRACE_PEEKDATA:
 152                ret = generic_ptrace_peekdata(child, addr, data);
 153                break;
 154
 155        case PTRACE_PEEKUSR:
 156                ret = ptrace_read_user(child, addr,
 157                                       (unsigned long __user *)data);
 158                break;
 159
 160        /* Write the word in data at location addr */
 161        case PTRACE_POKETEXT:
 162        case PTRACE_POKEDATA:
 163                ret = generic_ptrace_pokedata(child, addr, data);
 164                break;
 165
 166        case PTRACE_POKEUSR:
 167                ret = ptrace_write_user(child, addr, data);
 168                break;
 169
 170        /* continue and stop at next (return from) syscall */
 171        case PTRACE_SYSCALL:
 172        /* restart after signal */
 173        case PTRACE_CONT:
 174                ret = -EIO;
 175                if (!valid_signal(data))
 176                        break;
 177                if (request == PTRACE_SYSCALL)
 178                        set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
 179                else
 180                        clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
 181                child->exit_code = data;
 182                /* XXX: Are we sure no breakpoints are active here? */
 183                wake_up_process(child);
 184                ret = 0;
 185                break;
 186
 187        /*
 188         * Make the child exit. Best I can do is send it a
 189         * SIGKILL. Perhaps it should be put in the status that it
 190         * wants to exit.
 191         */
 192        case PTRACE_KILL:
 193                ret = 0;
 194                if (child->exit_state == EXIT_ZOMBIE)
 195                        break;
 196                child->exit_code = SIGKILL;
 197                wake_up_process(child);
 198                break;
 199
 200        /*
 201         * execute single instruction.
 202         */
 203        case PTRACE_SINGLESTEP:
 204                ret = -EIO;
 205                if (!valid_signal(data))
 206                        break;
 207                clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
 208                ptrace_single_step(child);
 209                child->exit_code = data;
 210                wake_up_process(child);
 211                ret = 0;
 212                break;
 213
 214        case PTRACE_GETREGS:
 215                ret = ptrace_getregs(child, (void __user *)data);
 216                break;
 217
 218        case PTRACE_SETREGS:
 219                ret = ptrace_setregs(child, (const void __user *)data);
 220                break;
 221
 222        default:
 223                ret = ptrace_request(child, request, addr, data);
 224                break;
 225        }
 226
 227        return ret;
 228}
 229
 230asmlinkage void syscall_trace(void)
 231{
 232        if (!test_thread_flag(TIF_SYSCALL_TRACE))
 233                return;
 234        if (!(current->ptrace & PT_PTRACED))
 235                return;
 236
 237        /* The 0x80 provides a way for the tracing parent to
 238         * distinguish between a syscall stop and SIGTRAP delivery */
 239        ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
 240                                 ? 0x80 : 0));
 241
 242        /*
 243         * this isn't the same as continuing with a signal, but it
 244         * will do for normal use.  strace only continues with a
 245         * signal if the stopping signal is not SIGTRAP.  -brl
 246         */
 247        if (current->exit_code) {
 248                pr_debug("syscall_trace: sending signal %d to PID %u\n",
 249                         current->exit_code, current->pid);
 250                send_sig(current->exit_code, current, 1);
 251                current->exit_code = 0;
 252        }
 253}
 254
 255/*
 256 * debug_trampoline() is an assembly stub which will store all user
 257 * registers on the stack and execute a breakpoint instruction.
 258 *
 259 * If we single-step into an exception handler which runs with
 260 * interrupts disabled the whole time so it doesn't have to check for
 261 * pending work, its return address will be modified so that it ends
 262 * up returning to debug_trampoline.
 263 *
 264 * If the exception handler decides to store the user context and
 265 * enable interrupts after all, it will restore the original return
 266 * address and status register value. Before it returns, it will
 267 * notice that TIF_BREAKPOINT is set and execute a breakpoint
 268 * instruction.
 269 */
 270extern void debug_trampoline(void);
 271
 272asmlinkage struct pt_regs *do_debug(struct pt_regs *regs)
 273{
 274        struct thread_info      *ti;
 275        unsigned long           trampoline_addr;
 276        u32                     status;
 277        u32                     ctrl;
 278        int                     code;
 279
 280        status = ocd_read(DS);
 281        ti = current_thread_info();
 282        code = TRAP_BRKPT;
 283
 284        pr_debug("do_debug: status=0x%08x PC=0x%08lx SR=0x%08lx tif=0x%08lx\n",
 285                        status, regs->pc, regs->sr, ti->flags);
 286
 287        if (!user_mode(regs)) {
 288                unsigned long   die_val = DIE_BREAKPOINT;
 289
 290                if (status & (1 << OCD_DS_SSS_BIT))
 291                        die_val = DIE_SSTEP;
 292
 293                if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP)
 294                                == NOTIFY_STOP)
 295                        return regs;
 296
 297                if ((status & (1 << OCD_DS_SWB_BIT))
 298                                && test_and_clear_ti_thread_flag(
 299                                        ti, TIF_BREAKPOINT)) {
 300                        /*
 301                         * Explicit breakpoint from trampoline or
 302                         * exception/syscall/interrupt handler.
 303                         *
 304                         * The real saved regs are on the stack right
 305                         * after the ones we saved on entry.
 306                         */
 307                        regs++;
 308                        pr_debug("  -> TIF_BREAKPOINT done, adjusted regs:"
 309                                        "PC=0x%08lx SR=0x%08lx\n",
 310                                        regs->pc, regs->sr);
 311                        BUG_ON(!user_mode(regs));
 312
 313                        if (test_thread_flag(TIF_SINGLE_STEP)) {
 314                                pr_debug("Going to do single step...\n");
 315                                return regs;
 316                        }
 317
 318                        /*
 319                         * No TIF_SINGLE_STEP means we're done
 320                         * stepping over a syscall. Do the trap now.
 321                         */
 322                        code = TRAP_TRACE;
 323                } else if ((status & (1 << OCD_DS_SSS_BIT))
 324                                && test_ti_thread_flag(ti, TIF_SINGLE_STEP)) {
 325
 326                        pr_debug("Stepped into something, "
 327                                        "setting TIF_BREAKPOINT...\n");
 328                        set_ti_thread_flag(ti, TIF_BREAKPOINT);
 329
 330                        /*
 331                         * We stepped into an exception, interrupt or
 332                         * syscall handler. Some exception handlers
 333                         * don't check for pending work, so we need to
 334                         * set up a trampoline just in case.
 335                         *
 336                         * The exception entry code will undo the
 337                         * trampoline stuff if it does a full context
 338                         * save (which also means that it'll check for
 339                         * pending work later.)
 340                         */
 341                        if ((regs->sr & MODE_MASK) == MODE_EXCEPTION) {
 342                                trampoline_addr
 343                                        = (unsigned long)&debug_trampoline;
 344
 345                                pr_debug("Setting up trampoline...\n");
 346                                ti->rar_saved = sysreg_read(RAR_EX);
 347                                ti->rsr_saved = sysreg_read(RSR_EX);
 348                                sysreg_write(RAR_EX, trampoline_addr);
 349                                sysreg_write(RSR_EX, (MODE_EXCEPTION
 350                                                        | SR_EM | SR_GM));
 351                                BUG_ON(ti->rsr_saved & MODE_MASK);
 352                        }
 353
 354                        /*
 355                         * If we stepped into a system call, we
 356                         * shouldn't do a single step after we return
 357                         * since the return address is right after the
 358                         * "scall" instruction we were told to step
 359                         * over.
 360                         */
 361                        if ((regs->sr & MODE_MASK) == MODE_SUPERVISOR) {
 362                                pr_debug("Supervisor; no single step\n");
 363                                clear_ti_thread_flag(ti, TIF_SINGLE_STEP);
 364                        }
 365
 366                        ctrl = ocd_read(DC);
 367                        ctrl &= ~(1 << OCD_DC_SS_BIT);
 368                        ocd_write(DC, ctrl);
 369
 370                        return regs;
 371                } else {
 372                        printk(KERN_ERR "Unexpected OCD_DS value: 0x%08x\n",
 373                                        status);
 374                        printk(KERN_ERR "Thread flags: 0x%08lx\n", ti->flags);
 375                        die("Unhandled debug trap in kernel mode",
 376                                        regs, SIGTRAP);
 377                }
 378        } else if (status & (1 << OCD_DS_SSS_BIT)) {
 379                /* Single step in user mode */
 380                code = TRAP_TRACE;
 381
 382                ctrl = ocd_read(DC);
 383                ctrl &= ~(1 << OCD_DC_SS_BIT);
 384                ocd_write(DC, ctrl);
 385        }
 386
 387        pr_debug("Sending SIGTRAP: code=%d PC=0x%08lx SR=0x%08lx\n",
 388                        code, regs->pc, regs->sr);
 389
 390        clear_thread_flag(TIF_SINGLE_STEP);
 391        _exception(SIGTRAP, regs, code, instruction_pointer(regs));
 392
 393        return regs;
 394}
 395