linux/arch/sh/kernel/ptrace_64.c
<<
>>
Prefs
   1/*
   2 * arch/sh/kernel/ptrace_64.c
   3 *
   4 * Copyright (C) 2000, 2001  Paolo Alberelli
   5 * Copyright (C) 2003 - 2008  Paul Mundt
   6 *
   7 * Started from SH3/4 version:
   8 *   SuperH version:   Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
   9 *
  10 *   Original x86 implementation:
  11 *      By Ross Biro 1/23/92
  12 *      edited by Linus Torvalds
  13 *
  14 * This file is subject to the terms and conditions of the GNU General Public
  15 * License.  See the file "COPYING" in the main directory of this archive
  16 * for more details.
  17 */
  18#include <linux/kernel.h>
  19#include <linux/rwsem.h>
  20#include <linux/sched.h>
  21#include <linux/sched/task_stack.h>
  22#include <linux/mm.h>
  23#include <linux/smp.h>
  24#include <linux/bitops.h>
  25#include <linux/errno.h>
  26#include <linux/ptrace.h>
  27#include <linux/user.h>
  28#include <linux/signal.h>
  29#include <linux/syscalls.h>
  30#include <linux/audit.h>
  31#include <linux/seccomp.h>
  32#include <linux/tracehook.h>
  33#include <linux/elf.h>
  34#include <linux/regset.h>
  35#include <asm/io.h>
  36#include <linux/uaccess.h>
  37#include <asm/pgtable.h>
  38#include <asm/processor.h>
  39#include <asm/mmu_context.h>
  40#include <asm/syscalls.h>
  41#include <asm/fpu.h>
  42#include <asm/traps.h>
  43
  44#define CREATE_TRACE_POINTS
  45#include <trace/events/syscalls.h>
  46
  47/* This mask defines the bits of the SR which the user is not allowed to
  48   change, which are everything except S, Q, M, PR, SZ, FR. */
  49#define SR_MASK      (0xffff8cfd)
  50
  51/*
  52 * does not yet catch signals sent when the child dies.
  53 * in exit.c or in signal.c.
  54 */
  55
  56/*
  57 * This routine will get a word from the user area in the process kernel stack.
  58 */
  59static inline int get_stack_long(struct task_struct *task, int offset)
  60{
  61        unsigned char *stack;
  62
  63        stack = (unsigned char *)(task->thread.uregs);
  64        stack += offset;
  65        return (*((int *)stack));
  66}
  67
  68static inline unsigned long
  69get_fpu_long(struct task_struct *task, unsigned long addr)
  70{
  71        unsigned long tmp;
  72        struct pt_regs *regs;
  73        regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  74
  75        if (!tsk_used_math(task)) {
  76                if (addr == offsetof(struct user_fpu_struct, fpscr)) {
  77                        tmp = FPSCR_INIT;
  78                } else {
  79                        tmp = 0xffffffffUL; /* matches initial value in fpu.c */
  80                }
  81                return tmp;
  82        }
  83
  84        if (last_task_used_math == task) {
  85                enable_fpu();
  86                save_fpu(task);
  87                disable_fpu();
  88                last_task_used_math = 0;
  89                regs->sr |= SR_FD;
  90        }
  91
  92        tmp = ((long *)task->thread.xstate)[addr / sizeof(unsigned long)];
  93        return tmp;
  94}
  95
  96/*
  97 * This routine will put a word into the user area in the process kernel stack.
  98 */
  99static inline int put_stack_long(struct task_struct *task, int offset,
 100                                 unsigned long data)
 101{
 102        unsigned char *stack;
 103
 104        stack = (unsigned char *)(task->thread.uregs);
 105        stack += offset;
 106        *(unsigned long *) stack = data;
 107        return 0;
 108}
 109
 110static inline int
 111put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
 112{
 113        struct pt_regs *regs;
 114
 115        regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
 116
 117        if (!tsk_used_math(task)) {
 118                init_fpu(task);
 119        } else if (last_task_used_math == task) {
 120                enable_fpu();
 121                save_fpu(task);
 122                disable_fpu();
 123                last_task_used_math = 0;
 124                regs->sr |= SR_FD;
 125        }
 126
 127        ((long *)task->thread.xstate)[addr / sizeof(unsigned long)] = data;
 128        return 0;
 129}
 130
 131void user_enable_single_step(struct task_struct *child)
 132{
 133        struct pt_regs *regs = child->thread.uregs;
 134
 135        regs->sr |= SR_SSTEP;   /* auto-resetting upon exception */
 136
 137        set_tsk_thread_flag(child, TIF_SINGLESTEP);
 138}
 139
 140void user_disable_single_step(struct task_struct *child)
 141{
 142        struct pt_regs *regs = child->thread.uregs;
 143
 144        regs->sr &= ~SR_SSTEP;
 145
 146        clear_tsk_thread_flag(child, TIF_SINGLESTEP);
 147}
 148
 149static int genregs_get(struct task_struct *target,
 150                       const struct user_regset *regset,
 151                       unsigned int pos, unsigned int count,
 152                       void *kbuf, void __user *ubuf)
 153{
 154        const struct pt_regs *regs = task_pt_regs(target);
 155        int ret;
 156
 157        /* PC, SR, SYSCALL */
 158        ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
 159                                  &regs->pc,
 160                                  0, 3 * sizeof(unsigned long long));
 161
 162        /* R1 -> R63 */
 163        if (!ret)
 164                ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
 165                                          regs->regs,
 166                                          offsetof(struct pt_regs, regs[0]),
 167                                          63 * sizeof(unsigned long long));
 168        /* TR0 -> TR7 */
 169        if (!ret)
 170                ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
 171                                          regs->tregs,
 172                                          offsetof(struct pt_regs, tregs[0]),
 173                                          8 * sizeof(unsigned long long));
 174
 175        if (!ret)
 176                ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
 177                                               sizeof(struct pt_regs), -1);
 178
 179        return ret;
 180}
 181
 182static int genregs_set(struct task_struct *target,
 183                       const struct user_regset *regset,
 184                       unsigned int pos, unsigned int count,
 185                       const void *kbuf, const void __user *ubuf)
 186{
 187        struct pt_regs *regs = task_pt_regs(target);
 188        int ret;
 189
 190        /* PC, SR, SYSCALL */
 191        ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
 192                                 &regs->pc,
 193                                 0, 3 * sizeof(unsigned long long));
 194
 195        /* R1 -> R63 */
 196        if (!ret && count > 0)
 197                ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
 198                                         regs->regs,
 199                                         offsetof(struct pt_regs, regs[0]),
 200                                         63 * sizeof(unsigned long long));
 201
 202        /* TR0 -> TR7 */
 203        if (!ret && count > 0)
 204                ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
 205                                         regs->tregs,
 206                                         offsetof(struct pt_regs, tregs[0]),
 207                                         8 * sizeof(unsigned long long));
 208
 209        if (!ret)
 210                ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
 211                                                sizeof(struct pt_regs), -1);
 212
 213        return ret;
 214}
 215
 216#ifdef CONFIG_SH_FPU
 217int fpregs_get(struct task_struct *target,
 218               const struct user_regset *regset,
 219               unsigned int pos, unsigned int count,
 220               void *kbuf, void __user *ubuf)
 221{
 222        int ret;
 223
 224        ret = init_fpu(target);
 225        if (ret)
 226                return ret;
 227
 228        return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
 229                                   &target->thread.xstate->hardfpu, 0, -1);
 230}
 231
 232static int fpregs_set(struct task_struct *target,
 233                       const struct user_regset *regset,
 234                       unsigned int pos, unsigned int count,
 235                       const void *kbuf, const void __user *ubuf)
 236{
 237        int ret;
 238
 239        ret = init_fpu(target);
 240        if (ret)
 241                return ret;
 242
 243        set_stopped_child_used_math(target);
 244
 245        return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
 246                                  &target->thread.xstate->hardfpu, 0, -1);
 247}
 248
 249static int fpregs_active(struct task_struct *target,
 250                         const struct user_regset *regset)
 251{
 252        return tsk_used_math(target) ? regset->n : 0;
 253}
 254#endif
 255
 256const struct pt_regs_offset regoffset_table[] = {
 257        REG_OFFSET_NAME(pc),
 258        REG_OFFSET_NAME(sr),
 259        REG_OFFSET_NAME(syscall_nr),
 260        REGS_OFFSET_NAME(0),
 261        REGS_OFFSET_NAME(1),
 262        REGS_OFFSET_NAME(2),
 263        REGS_OFFSET_NAME(3),
 264        REGS_OFFSET_NAME(4),
 265        REGS_OFFSET_NAME(5),
 266        REGS_OFFSET_NAME(6),
 267        REGS_OFFSET_NAME(7),
 268        REGS_OFFSET_NAME(8),
 269        REGS_OFFSET_NAME(9),
 270        REGS_OFFSET_NAME(10),
 271        REGS_OFFSET_NAME(11),
 272        REGS_OFFSET_NAME(12),
 273        REGS_OFFSET_NAME(13),
 274        REGS_OFFSET_NAME(14),
 275        REGS_OFFSET_NAME(15),
 276        REGS_OFFSET_NAME(16),
 277        REGS_OFFSET_NAME(17),
 278        REGS_OFFSET_NAME(18),
 279        REGS_OFFSET_NAME(19),
 280        REGS_OFFSET_NAME(20),
 281        REGS_OFFSET_NAME(21),
 282        REGS_OFFSET_NAME(22),
 283        REGS_OFFSET_NAME(23),
 284        REGS_OFFSET_NAME(24),
 285        REGS_OFFSET_NAME(25),
 286        REGS_OFFSET_NAME(26),
 287        REGS_OFFSET_NAME(27),
 288        REGS_OFFSET_NAME(28),
 289        REGS_OFFSET_NAME(29),
 290        REGS_OFFSET_NAME(30),
 291        REGS_OFFSET_NAME(31),
 292        REGS_OFFSET_NAME(32),
 293        REGS_OFFSET_NAME(33),
 294        REGS_OFFSET_NAME(34),
 295        REGS_OFFSET_NAME(35),
 296        REGS_OFFSET_NAME(36),
 297        REGS_OFFSET_NAME(37),
 298        REGS_OFFSET_NAME(38),
 299        REGS_OFFSET_NAME(39),
 300        REGS_OFFSET_NAME(40),
 301        REGS_OFFSET_NAME(41),
 302        REGS_OFFSET_NAME(42),
 303        REGS_OFFSET_NAME(43),
 304        REGS_OFFSET_NAME(44),
 305        REGS_OFFSET_NAME(45),
 306        REGS_OFFSET_NAME(46),
 307        REGS_OFFSET_NAME(47),
 308        REGS_OFFSET_NAME(48),
 309        REGS_OFFSET_NAME(49),
 310        REGS_OFFSET_NAME(50),
 311        REGS_OFFSET_NAME(51),
 312        REGS_OFFSET_NAME(52),
 313        REGS_OFFSET_NAME(53),
 314        REGS_OFFSET_NAME(54),
 315        REGS_OFFSET_NAME(55),
 316        REGS_OFFSET_NAME(56),
 317        REGS_OFFSET_NAME(57),
 318        REGS_OFFSET_NAME(58),
 319        REGS_OFFSET_NAME(59),
 320        REGS_OFFSET_NAME(60),
 321        REGS_OFFSET_NAME(61),
 322        REGS_OFFSET_NAME(62),
 323        REGS_OFFSET_NAME(63),
 324        TREGS_OFFSET_NAME(0),
 325        TREGS_OFFSET_NAME(1),
 326        TREGS_OFFSET_NAME(2),
 327        TREGS_OFFSET_NAME(3),
 328        TREGS_OFFSET_NAME(4),
 329        TREGS_OFFSET_NAME(5),
 330        TREGS_OFFSET_NAME(6),
 331        TREGS_OFFSET_NAME(7),
 332        REG_OFFSET_END,
 333};
 334
 335/*
 336 * These are our native regset flavours.
 337 */
 338enum sh_regset {
 339        REGSET_GENERAL,
 340#ifdef CONFIG_SH_FPU
 341        REGSET_FPU,
 342#endif
 343};
 344
 345static const struct user_regset sh_regsets[] = {
 346        /*
 347         * Format is:
 348         *      PC, SR, SYSCALL,
 349         *      R1 --> R63,
 350         *      TR0 --> TR7,
 351         */
 352        [REGSET_GENERAL] = {
 353                .core_note_type = NT_PRSTATUS,
 354                .n              = ELF_NGREG,
 355                .size           = sizeof(long long),
 356                .align          = sizeof(long long),
 357                .get            = genregs_get,
 358                .set            = genregs_set,
 359        },
 360
 361#ifdef CONFIG_SH_FPU
 362        [REGSET_FPU] = {
 363                .core_note_type = NT_PRFPREG,
 364                .n              = sizeof(struct user_fpu_struct) /
 365                                  sizeof(long long),
 366                .size           = sizeof(long long),
 367                .align          = sizeof(long long),
 368                .get            = fpregs_get,
 369                .set            = fpregs_set,
 370                .active         = fpregs_active,
 371        },
 372#endif
 373};
 374
 375static const struct user_regset_view user_sh64_native_view = {
 376        .name           = "sh64",
 377        .e_machine      = EM_SH,
 378        .regsets        = sh_regsets,
 379        .n              = ARRAY_SIZE(sh_regsets),
 380};
 381
 382const struct user_regset_view *task_user_regset_view(struct task_struct *task)
 383{
 384        return &user_sh64_native_view;
 385}
 386
 387long arch_ptrace(struct task_struct *child, long request,
 388                 unsigned long addr, unsigned long data)
 389{
 390        int ret;
 391        unsigned long __user *datap = (unsigned long __user *) data;
 392
 393        switch (request) {
 394        /* read the word at location addr in the USER area. */
 395        case PTRACE_PEEKUSR: {
 396                unsigned long tmp;
 397
 398                ret = -EIO;
 399                if ((addr & 3) || addr < 0)
 400                        break;
 401
 402                if (addr < sizeof(struct pt_regs))
 403                        tmp = get_stack_long(child, addr);
 404                else if ((addr >= offsetof(struct user, fpu)) &&
 405                         (addr <  offsetof(struct user, u_fpvalid))) {
 406                        unsigned long index;
 407                        ret = init_fpu(child);
 408                        if (ret)
 409                                break;
 410                        index = addr - offsetof(struct user, fpu);
 411                        tmp = get_fpu_long(child, index);
 412                } else if (addr == offsetof(struct user, u_fpvalid)) {
 413                        tmp = !!tsk_used_math(child);
 414                } else {
 415                        break;
 416                }
 417                ret = put_user(tmp, datap);
 418                break;
 419        }
 420
 421        case PTRACE_POKEUSR:
 422                /* write the word at location addr in the USER area. We must
 423                   disallow any changes to certain SR bits or u_fpvalid, since
 424                   this could crash the kernel or result in a security
 425                   loophole. */
 426                ret = -EIO;
 427                if ((addr & 3) || addr < 0)
 428                        break;
 429
 430                if (addr < sizeof(struct pt_regs)) {
 431                        /* Ignore change of top 32 bits of SR */
 432                        if (addr == offsetof (struct pt_regs, sr)+4)
 433                        {
 434                                ret = 0;
 435                                break;
 436                        }
 437                        /* If lower 32 bits of SR, ignore non-user bits */
 438                        if (addr == offsetof (struct pt_regs, sr))
 439                        {
 440                                long cursr = get_stack_long(child, addr);
 441                                data &= ~(SR_MASK);
 442                                data |= (cursr & SR_MASK);
 443                        }
 444                        ret = put_stack_long(child, addr, data);
 445                }
 446                else if ((addr >= offsetof(struct user, fpu)) &&
 447                         (addr <  offsetof(struct user, u_fpvalid))) {
 448                        unsigned long index;
 449                        ret = init_fpu(child);
 450                        if (ret)
 451                                break;
 452                        index = addr - offsetof(struct user, fpu);
 453                        ret = put_fpu_long(child, index, data);
 454                }
 455                break;
 456
 457        case PTRACE_GETREGS:
 458                return copy_regset_to_user(child, &user_sh64_native_view,
 459                                           REGSET_GENERAL,
 460                                           0, sizeof(struct pt_regs),
 461                                           datap);
 462        case PTRACE_SETREGS:
 463                return copy_regset_from_user(child, &user_sh64_native_view,
 464                                             REGSET_GENERAL,
 465                                             0, sizeof(struct pt_regs),
 466                                             datap);
 467#ifdef CONFIG_SH_FPU
 468        case PTRACE_GETFPREGS:
 469                return copy_regset_to_user(child, &user_sh64_native_view,
 470                                           REGSET_FPU,
 471                                           0, sizeof(struct user_fpu_struct),
 472                                           datap);
 473        case PTRACE_SETFPREGS:
 474                return copy_regset_from_user(child, &user_sh64_native_view,
 475                                             REGSET_FPU,
 476                                             0, sizeof(struct user_fpu_struct),
 477                                             datap);
 478#endif
 479        default:
 480                ret = ptrace_request(child, request, addr, data);
 481                break;
 482        }
 483
 484        return ret;
 485}
 486
 487asmlinkage int sh64_ptrace(long request, long pid,
 488                           unsigned long addr, unsigned long data)
 489{
 490#define WPC_DBRMODE 0x0d104008
 491        static unsigned long first_call;
 492
 493        if (!test_and_set_bit(0, &first_call)) {
 494                /* Set WPC.DBRMODE to 0.  This makes all debug events get
 495                 * delivered through RESVEC, i.e. into the handlers in entry.S.
 496                 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
 497                 * would normally be left set to 1, which makes debug events get
 498                 * delivered through DBRVEC, i.e. into the remote gdb's
 499                 * handlers.  This prevents ptrace getting them, and confuses
 500                 * the remote gdb.) */
 501                printk("DBRMODE set to 0 to permit native debugging\n");
 502                poke_real_address_q(WPC_DBRMODE, 0);
 503        }
 504
 505        return sys_ptrace(request, pid, addr, data);
 506}
 507
 508asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
 509{
 510        long long ret = 0;
 511
 512        secure_computing_strict(regs->regs[9]);
 513
 514        if (test_thread_flag(TIF_SYSCALL_TRACE) &&
 515            tracehook_report_syscall_entry(regs))
 516                /*
 517                 * Tracing decided this syscall should not happen.
 518                 * We'll return a bogus call number to get an ENOSYS
 519                 * error, but leave the original number in regs->regs[0].
 520                 */
 521                ret = -1LL;
 522
 523        if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
 524                trace_sys_enter(regs, regs->regs[9]);
 525
 526        audit_syscall_entry(regs->regs[1], regs->regs[2], regs->regs[3],
 527                            regs->regs[4], regs->regs[5]);
 528
 529        return ret ?: regs->regs[9];
 530}
 531
 532asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
 533{
 534        int step;
 535
 536        audit_syscall_exit(regs);
 537
 538        if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
 539                trace_sys_exit(regs, regs->regs[9]);
 540
 541        step = test_thread_flag(TIF_SINGLESTEP);
 542        if (step || test_thread_flag(TIF_SYSCALL_TRACE))
 543                tracehook_report_syscall_exit(regs, step);
 544}
 545
 546/* Called with interrupts disabled */
 547asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
 548{
 549        /* This is called after a single step exception (DEBUGSS).
 550           There is no need to change the PC, as it is a post-execution
 551           exception, as entry.S does not do anything to the PC for DEBUGSS.
 552           We need to clear the Single Step setting in SR to avoid
 553           continually stepping. */
 554        local_irq_enable();
 555        regs->sr &= ~SR_SSTEP;
 556        force_sig(SIGTRAP, current);
 557}
 558
 559/* Called with interrupts disabled */
 560BUILD_TRAP_HANDLER(breakpoint)
 561{
 562        TRAP_HANDLER_DECL;
 563
 564        /* We need to forward step the PC, to counteract the backstep done
 565           in signal.c. */
 566        local_irq_enable();
 567        force_sig(SIGTRAP, current);
 568        regs->pc += 4;
 569}
 570
 571/*
 572 * Called by kernel/ptrace.c when detaching..
 573 *
 574 * Make sure single step bits etc are not set.
 575 */
 576void ptrace_disable(struct task_struct *child)
 577{
 578        user_disable_single_step(child);
 579}
 580