linux/arch/x86/kernel/ptrace.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/* By Ross Biro 1/23/92 */
   3/*
   4 * Pentium III FXSR, SSE support
   5 *      Gareth Hughes <gareth@valinux.com>, May 2000
   6 */
   7
   8#include <linux/kernel.h>
   9#include <linux/sched.h>
  10#include <linux/sched/task_stack.h>
  11#include <linux/mm.h>
  12#include <linux/smp.h>
  13#include <linux/errno.h>
  14#include <linux/slab.h>
  15#include <linux/ptrace.h>
  16#include <linux/tracehook.h>
  17#include <linux/user.h>
  18#include <linux/elf.h>
  19#include <linux/security.h>
  20#include <linux/audit.h>
  21#include <linux/seccomp.h>
  22#include <linux/signal.h>
  23#include <linux/perf_event.h>
  24#include <linux/hw_breakpoint.h>
  25#include <linux/rcupdate.h>
  26#include <linux/export.h>
  27#include <linux/context_tracking.h>
  28#include <linux/nospec.h>
  29
  30#include <linux/uaccess.h>
  31#include <asm/pgtable.h>
  32#include <asm/processor.h>
  33#include <asm/fpu/internal.h>
  34#include <asm/fpu/signal.h>
  35#include <asm/fpu/regset.h>
  36#include <asm/debugreg.h>
  37#include <asm/ldt.h>
  38#include <asm/desc.h>
  39#include <asm/prctl.h>
  40#include <asm/proto.h>
  41#include <asm/hw_breakpoint.h>
  42#include <asm/traps.h>
  43#include <asm/syscall.h>
  44#include <asm/fsgsbase.h>
  45#include <asm/io_bitmap.h>
  46
  47#include "tls.h"
  48
  49enum x86_regset {
  50        REGSET_GENERAL,
  51        REGSET_FP,
  52        REGSET_XFP,
  53        REGSET_IOPERM64 = REGSET_XFP,
  54        REGSET_XSTATE,
  55        REGSET_TLS,
  56        REGSET_IOPERM32,
  57};
  58
  59struct pt_regs_offset {
  60        const char *name;
  61        int offset;
  62};
  63
  64#define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  65#define REG_OFFSET_END {.name = NULL, .offset = 0}
  66
  67static const struct pt_regs_offset regoffset_table[] = {
  68#ifdef CONFIG_X86_64
  69        REG_OFFSET_NAME(r15),
  70        REG_OFFSET_NAME(r14),
  71        REG_OFFSET_NAME(r13),
  72        REG_OFFSET_NAME(r12),
  73        REG_OFFSET_NAME(r11),
  74        REG_OFFSET_NAME(r10),
  75        REG_OFFSET_NAME(r9),
  76        REG_OFFSET_NAME(r8),
  77#endif
  78        REG_OFFSET_NAME(bx),
  79        REG_OFFSET_NAME(cx),
  80        REG_OFFSET_NAME(dx),
  81        REG_OFFSET_NAME(si),
  82        REG_OFFSET_NAME(di),
  83        REG_OFFSET_NAME(bp),
  84        REG_OFFSET_NAME(ax),
  85#ifdef CONFIG_X86_32
  86        REG_OFFSET_NAME(ds),
  87        REG_OFFSET_NAME(es),
  88        REG_OFFSET_NAME(fs),
  89        REG_OFFSET_NAME(gs),
  90#endif
  91        REG_OFFSET_NAME(orig_ax),
  92        REG_OFFSET_NAME(ip),
  93        REG_OFFSET_NAME(cs),
  94        REG_OFFSET_NAME(flags),
  95        REG_OFFSET_NAME(sp),
  96        REG_OFFSET_NAME(ss),
  97        REG_OFFSET_END,
  98};
  99
 100/**
 101 * regs_query_register_offset() - query register offset from its name
 102 * @name:       the name of a register
 103 *
 104 * regs_query_register_offset() returns the offset of a register in struct
 105 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
 106 */
 107int regs_query_register_offset(const char *name)
 108{
 109        const struct pt_regs_offset *roff;
 110        for (roff = regoffset_table; roff->name != NULL; roff++)
 111                if (!strcmp(roff->name, name))
 112                        return roff->offset;
 113        return -EINVAL;
 114}
 115
 116/**
 117 * regs_query_register_name() - query register name from its offset
 118 * @offset:     the offset of a register in struct pt_regs.
 119 *
 120 * regs_query_register_name() returns the name of a register from its
 121 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
 122 */
 123const char *regs_query_register_name(unsigned int offset)
 124{
 125        const struct pt_regs_offset *roff;
 126        for (roff = regoffset_table; roff->name != NULL; roff++)
 127                if (roff->offset == offset)
 128                        return roff->name;
 129        return NULL;
 130}
 131
 132/*
 133 * does not yet catch signals sent when the child dies.
 134 * in exit.c or in signal.c.
 135 */
 136
 137/*
 138 * Determines which flags the user has access to [1 = access, 0 = no access].
 139 */
 140#define FLAG_MASK_32            ((unsigned long)                        \
 141                                 (X86_EFLAGS_CF | X86_EFLAGS_PF |       \
 142                                  X86_EFLAGS_AF | X86_EFLAGS_ZF |       \
 143                                  X86_EFLAGS_SF | X86_EFLAGS_TF |       \
 144                                  X86_EFLAGS_DF | X86_EFLAGS_OF |       \
 145                                  X86_EFLAGS_RF | X86_EFLAGS_AC))
 146
 147/*
 148 * Determines whether a value may be installed in a segment register.
 149 */
 150static inline bool invalid_selector(u16 value)
 151{
 152        return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
 153}
 154
 155#ifdef CONFIG_X86_32
 156
 157#define FLAG_MASK               FLAG_MASK_32
 158
 159static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
 160{
 161        BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
 162        return &regs->bx + (regno >> 2);
 163}
 164
 165static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
 166{
 167        /*
 168         * Returning the value truncates it to 16 bits.
 169         */
 170        unsigned int retval;
 171        if (offset != offsetof(struct user_regs_struct, gs))
 172                retval = *pt_regs_access(task_pt_regs(task), offset);
 173        else {
 174                if (task == current)
 175                        retval = get_user_gs(task_pt_regs(task));
 176                else
 177                        retval = task_user_gs(task);
 178        }
 179        return retval;
 180}
 181
 182static int set_segment_reg(struct task_struct *task,
 183                           unsigned long offset, u16 value)
 184{
 185        if (WARN_ON_ONCE(task == current))
 186                return -EIO;
 187
 188        /*
 189         * The value argument was already truncated to 16 bits.
 190         */
 191        if (invalid_selector(value))
 192                return -EIO;
 193
 194        /*
 195         * For %cs and %ss we cannot permit a null selector.
 196         * We can permit a bogus selector as long as it has USER_RPL.
 197         * Null selectors are fine for other segment registers, but
 198         * we will never get back to user mode with invalid %cs or %ss
 199         * and will take the trap in iret instead.  Much code relies
 200         * on user_mode() to distinguish a user trap frame (which can
 201         * safely use invalid selectors) from a kernel trap frame.
 202         */
 203        switch (offset) {
 204        case offsetof(struct user_regs_struct, cs):
 205        case offsetof(struct user_regs_struct, ss):
 206                if (unlikely(value == 0))
 207                        return -EIO;
 208                /* Else, fall through */
 209
 210        default:
 211                *pt_regs_access(task_pt_regs(task), offset) = value;
 212                break;
 213
 214        case offsetof(struct user_regs_struct, gs):
 215                task_user_gs(task) = value;
 216        }
 217
 218        return 0;
 219}
 220
 221#else  /* CONFIG_X86_64 */
 222
 223#define FLAG_MASK               (FLAG_MASK_32 | X86_EFLAGS_NT)
 224
 225static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
 226{
 227        BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
 228        return &regs->r15 + (offset / sizeof(regs->r15));
 229}
 230
 231static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
 232{
 233        /*
 234         * Returning the value truncates it to 16 bits.
 235         */
 236        unsigned int seg;
 237
 238        switch (offset) {
 239        case offsetof(struct user_regs_struct, fs):
 240                if (task == current) {
 241                        /* Older gas can't assemble movq %?s,%r?? */
 242                        asm("movl %%fs,%0" : "=r" (seg));
 243                        return seg;
 244                }
 245                return task->thread.fsindex;
 246        case offsetof(struct user_regs_struct, gs):
 247                if (task == current) {
 248                        asm("movl %%gs,%0" : "=r" (seg));
 249                        return seg;
 250                }
 251                return task->thread.gsindex;
 252        case offsetof(struct user_regs_struct, ds):
 253                if (task == current) {
 254                        asm("movl %%ds,%0" : "=r" (seg));
 255                        return seg;
 256                }
 257                return task->thread.ds;
 258        case offsetof(struct user_regs_struct, es):
 259                if (task == current) {
 260                        asm("movl %%es,%0" : "=r" (seg));
 261                        return seg;
 262                }
 263                return task->thread.es;
 264
 265        case offsetof(struct user_regs_struct, cs):
 266        case offsetof(struct user_regs_struct, ss):
 267                break;
 268        }
 269        return *pt_regs_access(task_pt_regs(task), offset);
 270}
 271
 272static int set_segment_reg(struct task_struct *task,
 273                           unsigned long offset, u16 value)
 274{
 275        if (WARN_ON_ONCE(task == current))
 276                return -EIO;
 277
 278        /*
 279         * The value argument was already truncated to 16 bits.
 280         */
 281        if (invalid_selector(value))
 282                return -EIO;
 283
 284        /*
 285         * This function has some ABI oddities.
 286         *
 287         * A 32-bit ptracer probably expects that writing FS or GS will change
 288         * FSBASE or GSBASE respectively.  In the absence of FSGSBASE support,
 289         * this code indeed has that effect.  When FSGSBASE is added, this
 290         * will require a special case.
 291         *
 292         * For existing 64-bit ptracers, writing FS or GS *also* currently
 293         * changes the base if the selector is nonzero the next time the task
 294         * is run.  This behavior may not be needed, and trying to preserve it
 295         * when FSGSBASE is added would be complicated at best.
 296         */
 297
 298        switch (offset) {
 299        case offsetof(struct user_regs_struct,fs):
 300                task->thread.fsindex = value;
 301                break;
 302        case offsetof(struct user_regs_struct,gs):
 303                task->thread.gsindex = value;
 304                break;
 305        case offsetof(struct user_regs_struct,ds):
 306                task->thread.ds = value;
 307                break;
 308        case offsetof(struct user_regs_struct,es):
 309                task->thread.es = value;
 310                break;
 311
 312                /*
 313                 * Can't actually change these in 64-bit mode.
 314                 */
 315        case offsetof(struct user_regs_struct,cs):
 316                if (unlikely(value == 0))
 317                        return -EIO;
 318                task_pt_regs(task)->cs = value;
 319                break;
 320        case offsetof(struct user_regs_struct,ss):
 321                if (unlikely(value == 0))
 322                        return -EIO;
 323                task_pt_regs(task)->ss = value;
 324                break;
 325        }
 326
 327        return 0;
 328}
 329
 330#endif  /* CONFIG_X86_32 */
 331
 332static unsigned long get_flags(struct task_struct *task)
 333{
 334        unsigned long retval = task_pt_regs(task)->flags;
 335
 336        /*
 337         * If the debugger set TF, hide it from the readout.
 338         */
 339        if (test_tsk_thread_flag(task, TIF_FORCED_TF))
 340                retval &= ~X86_EFLAGS_TF;
 341
 342        return retval;
 343}
 344
 345static int set_flags(struct task_struct *task, unsigned long value)
 346{
 347        struct pt_regs *regs = task_pt_regs(task);
 348
 349        /*
 350         * If the user value contains TF, mark that
 351         * it was not "us" (the debugger) that set it.
 352         * If not, make sure it stays set if we had.
 353         */
 354        if (value & X86_EFLAGS_TF)
 355                clear_tsk_thread_flag(task, TIF_FORCED_TF);
 356        else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
 357                value |= X86_EFLAGS_TF;
 358
 359        regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
 360
 361        return 0;
 362}
 363
 364static int putreg(struct task_struct *child,
 365                  unsigned long offset, unsigned long value)
 366{
 367        switch (offset) {
 368        case offsetof(struct user_regs_struct, cs):
 369        case offsetof(struct user_regs_struct, ds):
 370        case offsetof(struct user_regs_struct, es):
 371        case offsetof(struct user_regs_struct, fs):
 372        case offsetof(struct user_regs_struct, gs):
 373        case offsetof(struct user_regs_struct, ss):
 374                return set_segment_reg(child, offset, value);
 375
 376        case offsetof(struct user_regs_struct, flags):
 377                return set_flags(child, value);
 378
 379#ifdef CONFIG_X86_64
 380        case offsetof(struct user_regs_struct,fs_base):
 381                if (value >= TASK_SIZE_MAX)
 382                        return -EIO;
 383                /*
 384                 * When changing the FS base, use do_arch_prctl_64()
 385                 * to set the index to zero and to set the base
 386                 * as requested.
 387                 *
 388                 * NB: This behavior is nonsensical and likely needs to
 389                 * change when FSGSBASE support is added.
 390                 */
 391                if (child->thread.fsbase != value)
 392                        return do_arch_prctl_64(child, ARCH_SET_FS, value);
 393                return 0;
 394        case offsetof(struct user_regs_struct,gs_base):
 395                /*
 396                 * Exactly the same here as the %fs handling above.
 397                 */
 398                if (value >= TASK_SIZE_MAX)
 399                        return -EIO;
 400                if (child->thread.gsbase != value)
 401                        return do_arch_prctl_64(child, ARCH_SET_GS, value);
 402                return 0;
 403#endif
 404        }
 405
 406        *pt_regs_access(task_pt_regs(child), offset) = value;
 407        return 0;
 408}
 409
 410static unsigned long getreg(struct task_struct *task, unsigned long offset)
 411{
 412        switch (offset) {
 413        case offsetof(struct user_regs_struct, cs):
 414        case offsetof(struct user_regs_struct, ds):
 415        case offsetof(struct user_regs_struct, es):
 416        case offsetof(struct user_regs_struct, fs):
 417        case offsetof(struct user_regs_struct, gs):
 418        case offsetof(struct user_regs_struct, ss):
 419                return get_segment_reg(task, offset);
 420
 421        case offsetof(struct user_regs_struct, flags):
 422                return get_flags(task);
 423
 424#ifdef CONFIG_X86_64
 425        case offsetof(struct user_regs_struct, fs_base):
 426                return x86_fsbase_read_task(task);
 427        case offsetof(struct user_regs_struct, gs_base):
 428                return x86_gsbase_read_task(task);
 429#endif
 430        }
 431
 432        return *pt_regs_access(task_pt_regs(task), offset);
 433}
 434
 435static int genregs_get(struct task_struct *target,
 436                       const struct user_regset *regset,
 437                       unsigned int pos, unsigned int count,
 438                       void *kbuf, void __user *ubuf)
 439{
 440        if (kbuf) {
 441                unsigned long *k = kbuf;
 442                while (count >= sizeof(*k)) {
 443                        *k++ = getreg(target, pos);
 444                        count -= sizeof(*k);
 445                        pos += sizeof(*k);
 446                }
 447        } else {
 448                unsigned long __user *u = ubuf;
 449                while (count >= sizeof(*u)) {
 450                        if (__put_user(getreg(target, pos), u++))
 451                                return -EFAULT;
 452                        count -= sizeof(*u);
 453                        pos += sizeof(*u);
 454                }
 455        }
 456
 457        return 0;
 458}
 459
 460static int genregs_set(struct task_struct *target,
 461                       const struct user_regset *regset,
 462                       unsigned int pos, unsigned int count,
 463                       const void *kbuf, const void __user *ubuf)
 464{
 465        int ret = 0;
 466        if (kbuf) {
 467                const unsigned long *k = kbuf;
 468                while (count >= sizeof(*k) && !ret) {
 469                        ret = putreg(target, pos, *k++);
 470                        count -= sizeof(*k);
 471                        pos += sizeof(*k);
 472                }
 473        } else {
 474                const unsigned long  __user *u = ubuf;
 475                while (count >= sizeof(*u) && !ret) {
 476                        unsigned long word;
 477                        ret = __get_user(word, u++);
 478                        if (ret)
 479                                break;
 480                        ret = putreg(target, pos, word);
 481                        count -= sizeof(*u);
 482                        pos += sizeof(*u);
 483                }
 484        }
 485        return ret;
 486}
 487
 488static void ptrace_triggered(struct perf_event *bp,
 489                             struct perf_sample_data *data,
 490                             struct pt_regs *regs)
 491{
 492        int i;
 493        struct thread_struct *thread = &(current->thread);
 494
 495        /*
 496         * Store in the virtual DR6 register the fact that the breakpoint
 497         * was hit so the thread's debugger will see it.
 498         */
 499        for (i = 0; i < HBP_NUM; i++) {
 500                if (thread->ptrace_bps[i] == bp)
 501                        break;
 502        }
 503
 504        thread->debugreg6 |= (DR_TRAP0 << i);
 505}
 506
 507/*
 508 * Walk through every ptrace breakpoints for this thread and
 509 * build the dr7 value on top of their attributes.
 510 *
 511 */
 512static unsigned long ptrace_get_dr7(struct perf_event *bp[])
 513{
 514        int i;
 515        int dr7 = 0;
 516        struct arch_hw_breakpoint *info;
 517
 518        for (i = 0; i < HBP_NUM; i++) {
 519                if (bp[i] && !bp[i]->attr.disabled) {
 520                        info = counter_arch_bp(bp[i]);
 521                        dr7 |= encode_dr7(i, info->len, info->type);
 522                }
 523        }
 524
 525        return dr7;
 526}
 527
 528static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
 529                                        int len, int type, bool disabled)
 530{
 531        int err, bp_len, bp_type;
 532
 533        err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
 534        if (!err) {
 535                attr->bp_len = bp_len;
 536                attr->bp_type = bp_type;
 537                attr->disabled = disabled;
 538        }
 539
 540        return err;
 541}
 542
 543static struct perf_event *
 544ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
 545                                unsigned long addr, bool disabled)
 546{
 547        struct perf_event_attr attr;
 548        int err;
 549
 550        ptrace_breakpoint_init(&attr);
 551        attr.bp_addr = addr;
 552
 553        err = ptrace_fill_bp_fields(&attr, len, type, disabled);
 554        if (err)
 555                return ERR_PTR(err);
 556
 557        return register_user_hw_breakpoint(&attr, ptrace_triggered,
 558                                                 NULL, tsk);
 559}
 560
 561static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
 562                                        int disabled)
 563{
 564        struct perf_event_attr attr = bp->attr;
 565        int err;
 566
 567        err = ptrace_fill_bp_fields(&attr, len, type, disabled);
 568        if (err)
 569                return err;
 570
 571        return modify_user_hw_breakpoint(bp, &attr);
 572}
 573
 574/*
 575 * Handle ptrace writes to debug register 7.
 576 */
 577static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
 578{
 579        struct thread_struct *thread = &tsk->thread;
 580        unsigned long old_dr7;
 581        bool second_pass = false;
 582        int i, rc, ret = 0;
 583
 584        data &= ~DR_CONTROL_RESERVED;
 585        old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
 586
 587restore:
 588        rc = 0;
 589        for (i = 0; i < HBP_NUM; i++) {
 590                unsigned len, type;
 591                bool disabled = !decode_dr7(data, i, &len, &type);
 592                struct perf_event *bp = thread->ptrace_bps[i];
 593
 594                if (!bp) {
 595                        if (disabled)
 596                                continue;
 597
 598                        bp = ptrace_register_breakpoint(tsk,
 599                                        len, type, 0, disabled);
 600                        if (IS_ERR(bp)) {
 601                                rc = PTR_ERR(bp);
 602                                break;
 603                        }
 604
 605                        thread->ptrace_bps[i] = bp;
 606                        continue;
 607                }
 608
 609                rc = ptrace_modify_breakpoint(bp, len, type, disabled);
 610                if (rc)
 611                        break;
 612        }
 613
 614        /* Restore if the first pass failed, second_pass shouldn't fail. */
 615        if (rc && !WARN_ON(second_pass)) {
 616                ret = rc;
 617                data = old_dr7;
 618                second_pass = true;
 619                goto restore;
 620        }
 621
 622        return ret;
 623}
 624
 625/*
 626 * Handle PTRACE_PEEKUSR calls for the debug register area.
 627 */
 628static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
 629{
 630        struct thread_struct *thread = &tsk->thread;
 631        unsigned long val = 0;
 632
 633        if (n < HBP_NUM) {
 634                int index = array_index_nospec(n, HBP_NUM);
 635                struct perf_event *bp = thread->ptrace_bps[index];
 636
 637                if (bp)
 638                        val = bp->hw.info.address;
 639        } else if (n == 6) {
 640                val = thread->debugreg6;
 641        } else if (n == 7) {
 642                val = thread->ptrace_dr7;
 643        }
 644        return val;
 645}
 646
 647static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
 648                                      unsigned long addr)
 649{
 650        struct thread_struct *t = &tsk->thread;
 651        struct perf_event *bp = t->ptrace_bps[nr];
 652        int err = 0;
 653
 654        if (!bp) {
 655                /*
 656                 * Put stub len and type to create an inactive but correct bp.
 657                 *
 658                 * CHECKME: the previous code returned -EIO if the addr wasn't
 659                 * a valid task virtual addr. The new one will return -EINVAL in
 660                 *  this case.
 661                 * -EINVAL may be what we want for in-kernel breakpoints users,
 662                 * but -EIO looks better for ptrace, since we refuse a register
 663                 * writing for the user. And anyway this is the previous
 664                 * behaviour.
 665                 */
 666                bp = ptrace_register_breakpoint(tsk,
 667                                X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
 668                                addr, true);
 669                if (IS_ERR(bp))
 670                        err = PTR_ERR(bp);
 671                else
 672                        t->ptrace_bps[nr] = bp;
 673        } else {
 674                struct perf_event_attr attr = bp->attr;
 675
 676                attr.bp_addr = addr;
 677                err = modify_user_hw_breakpoint(bp, &attr);
 678        }
 679
 680        return err;
 681}
 682
 683/*
 684 * Handle PTRACE_POKEUSR calls for the debug register area.
 685 */
 686static int ptrace_set_debugreg(struct task_struct *tsk, int n,
 687                               unsigned long val)
 688{
 689        struct thread_struct *thread = &tsk->thread;
 690        /* There are no DR4 or DR5 registers */
 691        int rc = -EIO;
 692
 693        if (n < HBP_NUM) {
 694                rc = ptrace_set_breakpoint_addr(tsk, n, val);
 695        } else if (n == 6) {
 696                thread->debugreg6 = val;
 697                rc = 0;
 698        } else if (n == 7) {
 699                rc = ptrace_write_dr7(tsk, val);
 700                if (!rc)
 701                        thread->ptrace_dr7 = val;
 702        }
 703        return rc;
 704}
 705
 706/*
 707 * These access the current or another (stopped) task's io permission
 708 * bitmap for debugging or core dump.
 709 */
 710static int ioperm_active(struct task_struct *target,
 711                         const struct user_regset *regset)
 712{
 713        struct io_bitmap *iobm = target->thread.io_bitmap;
 714
 715        return iobm ? DIV_ROUND_UP(iobm->max, regset->size) : 0;
 716}
 717
 718static int ioperm_get(struct task_struct *target,
 719                      const struct user_regset *regset,
 720                      unsigned int pos, unsigned int count,
 721                      void *kbuf, void __user *ubuf)
 722{
 723        struct io_bitmap *iobm = target->thread.io_bitmap;
 724
 725        if (!iobm)
 726                return -ENXIO;
 727
 728        return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
 729                                   iobm->bitmap, 0, IO_BITMAP_BYTES);
 730}
 731
 732/*
 733 * Called by kernel/ptrace.c when detaching..
 734 *
 735 * Make sure the single step bit is not set.
 736 */
 737void ptrace_disable(struct task_struct *child)
 738{
 739        user_disable_single_step(child);
 740}
 741
 742#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
 743static const struct user_regset_view user_x86_32_view; /* Initialized below. */
 744#endif
 745
 746long arch_ptrace(struct task_struct *child, long request,
 747                 unsigned long addr, unsigned long data)
 748{
 749        int ret;
 750        unsigned long __user *datap = (unsigned long __user *)data;
 751
 752        switch (request) {
 753        /* read the word at location addr in the USER area. */
 754        case PTRACE_PEEKUSR: {
 755                unsigned long tmp;
 756
 757                ret = -EIO;
 758                if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
 759                        break;
 760
 761                tmp = 0;  /* Default return condition */
 762                if (addr < sizeof(struct user_regs_struct))
 763                        tmp = getreg(child, addr);
 764                else if (addr >= offsetof(struct user, u_debugreg[0]) &&
 765                         addr <= offsetof(struct user, u_debugreg[7])) {
 766                        addr -= offsetof(struct user, u_debugreg[0]);
 767                        tmp = ptrace_get_debugreg(child, addr / sizeof(data));
 768                }
 769                ret = put_user(tmp, datap);
 770                break;
 771        }
 772
 773        case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
 774                ret = -EIO;
 775                if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
 776                        break;
 777
 778                if (addr < sizeof(struct user_regs_struct))
 779                        ret = putreg(child, addr, data);
 780                else if (addr >= offsetof(struct user, u_debugreg[0]) &&
 781                         addr <= offsetof(struct user, u_debugreg[7])) {
 782                        addr -= offsetof(struct user, u_debugreg[0]);
 783                        ret = ptrace_set_debugreg(child,
 784                                                  addr / sizeof(data), data);
 785                }
 786                break;
 787
 788        case PTRACE_GETREGS:    /* Get all gp regs from the child. */
 789                return copy_regset_to_user(child,
 790                                           task_user_regset_view(current),
 791                                           REGSET_GENERAL,
 792                                           0, sizeof(struct user_regs_struct),
 793                                           datap);
 794
 795        case PTRACE_SETREGS:    /* Set all gp regs in the child. */
 796                return copy_regset_from_user(child,
 797                                             task_user_regset_view(current),
 798                                             REGSET_GENERAL,
 799                                             0, sizeof(struct user_regs_struct),
 800                                             datap);
 801
 802        case PTRACE_GETFPREGS:  /* Get the child FPU state. */
 803                return copy_regset_to_user(child,
 804                                           task_user_regset_view(current),
 805                                           REGSET_FP,
 806                                           0, sizeof(struct user_i387_struct),
 807                                           datap);
 808
 809        case PTRACE_SETFPREGS:  /* Set the child FPU state. */
 810                return copy_regset_from_user(child,
 811                                             task_user_regset_view(current),
 812                                             REGSET_FP,
 813                                             0, sizeof(struct user_i387_struct),
 814                                             datap);
 815
 816#ifdef CONFIG_X86_32
 817        case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
 818                return copy_regset_to_user(child, &user_x86_32_view,
 819                                           REGSET_XFP,
 820                                           0, sizeof(struct user_fxsr_struct),
 821                                           datap) ? -EIO : 0;
 822
 823        case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
 824                return copy_regset_from_user(child, &user_x86_32_view,
 825                                             REGSET_XFP,
 826                                             0, sizeof(struct user_fxsr_struct),
 827                                             datap) ? -EIO : 0;
 828#endif
 829
 830#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
 831        case PTRACE_GET_THREAD_AREA:
 832                if ((int) addr < 0)
 833                        return -EIO;
 834                ret = do_get_thread_area(child, addr,
 835                                        (struct user_desc __user *)data);
 836                break;
 837
 838        case PTRACE_SET_THREAD_AREA:
 839                if ((int) addr < 0)
 840                        return -EIO;
 841                ret = do_set_thread_area(child, addr,
 842                                        (struct user_desc __user *)data, 0);
 843                break;
 844#endif
 845
 846#ifdef CONFIG_X86_64
 847                /* normal 64bit interface to access TLS data.
 848                   Works just like arch_prctl, except that the arguments
 849                   are reversed. */
 850        case PTRACE_ARCH_PRCTL:
 851                ret = do_arch_prctl_64(child, data, addr);
 852                break;
 853#endif
 854
 855        default:
 856                ret = ptrace_request(child, request, addr, data);
 857                break;
 858        }
 859
 860        return ret;
 861}
 862
 863#ifdef CONFIG_IA32_EMULATION
 864
 865#include <linux/compat.h>
 866#include <linux/syscalls.h>
 867#include <asm/ia32.h>
 868#include <asm/user32.h>
 869
 870#define R32(l,q)                                                        \
 871        case offsetof(struct user32, regs.l):                           \
 872                regs->q = value; break
 873
 874#define SEG32(rs)                                                       \
 875        case offsetof(struct user32, regs.rs):                          \
 876                return set_segment_reg(child,                           \
 877                                       offsetof(struct user_regs_struct, rs), \
 878                                       value);                          \
 879                break
 880
 881static int putreg32(struct task_struct *child, unsigned regno, u32 value)
 882{
 883        struct pt_regs *regs = task_pt_regs(child);
 884
 885        switch (regno) {
 886
 887        SEG32(cs);
 888        SEG32(ds);
 889        SEG32(es);
 890        SEG32(fs);
 891        SEG32(gs);
 892        SEG32(ss);
 893
 894        R32(ebx, bx);
 895        R32(ecx, cx);
 896        R32(edx, dx);
 897        R32(edi, di);
 898        R32(esi, si);
 899        R32(ebp, bp);
 900        R32(eax, ax);
 901        R32(eip, ip);
 902        R32(esp, sp);
 903
 904        case offsetof(struct user32, regs.orig_eax):
 905                /*
 906                 * Warning: bizarre corner case fixup here.  A 32-bit
 907                 * debugger setting orig_eax to -1 wants to disable
 908                 * syscall restart.  Make sure that the syscall
 909                 * restart code sign-extends orig_ax.  Also make sure
 910                 * we interpret the -ERESTART* codes correctly if
 911                 * loaded into regs->ax in case the task is not
 912                 * actually still sitting at the exit from a 32-bit
 913                 * syscall with TS_COMPAT still set.
 914                 */
 915                regs->orig_ax = value;
 916                if (syscall_get_nr(child, regs) >= 0)
 917                        child->thread_info.status |= TS_I386_REGS_POKED;
 918                break;
 919
 920        case offsetof(struct user32, regs.eflags):
 921                return set_flags(child, value);
 922
 923        case offsetof(struct user32, u_debugreg[0]) ...
 924                offsetof(struct user32, u_debugreg[7]):
 925                regno -= offsetof(struct user32, u_debugreg[0]);
 926                return ptrace_set_debugreg(child, regno / 4, value);
 927
 928        default:
 929                if (regno > sizeof(struct user32) || (regno & 3))
 930                        return -EIO;
 931
 932                /*
 933                 * Other dummy fields in the virtual user structure
 934                 * are ignored
 935                 */
 936                break;
 937        }
 938        return 0;
 939}
 940
 941#undef R32
 942#undef SEG32
 943
 944#define R32(l,q)                                                        \
 945        case offsetof(struct user32, regs.l):                           \
 946                *val = regs->q; break
 947
 948#define SEG32(rs)                                                       \
 949        case offsetof(struct user32, regs.rs):                          \
 950                *val = get_segment_reg(child,                           \
 951                                       offsetof(struct user_regs_struct, rs)); \
 952                break
 953
 954static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
 955{
 956        struct pt_regs *regs = task_pt_regs(child);
 957
 958        switch (regno) {
 959
 960        SEG32(ds);
 961        SEG32(es);
 962        SEG32(fs);
 963        SEG32(gs);
 964
 965        R32(cs, cs);
 966        R32(ss, ss);
 967        R32(ebx, bx);
 968        R32(ecx, cx);
 969        R32(edx, dx);
 970        R32(edi, di);
 971        R32(esi, si);
 972        R32(ebp, bp);
 973        R32(eax, ax);
 974        R32(orig_eax, orig_ax);
 975        R32(eip, ip);
 976        R32(esp, sp);
 977
 978        case offsetof(struct user32, regs.eflags):
 979                *val = get_flags(child);
 980                break;
 981
 982        case offsetof(struct user32, u_debugreg[0]) ...
 983                offsetof(struct user32, u_debugreg[7]):
 984                regno -= offsetof(struct user32, u_debugreg[0]);
 985                *val = ptrace_get_debugreg(child, regno / 4);
 986                break;
 987
 988        default:
 989                if (regno > sizeof(struct user32) || (regno & 3))
 990                        return -EIO;
 991
 992                /*
 993                 * Other dummy fields in the virtual user structure
 994                 * are ignored
 995                 */
 996                *val = 0;
 997                break;
 998        }
 999        return 0;
1000}
1001
1002#undef R32
1003#undef SEG32
1004
1005static int genregs32_get(struct task_struct *target,
1006                         const struct user_regset *regset,
1007                         unsigned int pos, unsigned int count,
1008                         void *kbuf, void __user *ubuf)
1009{
1010        if (kbuf) {
1011                compat_ulong_t *k = kbuf;
1012                while (count >= sizeof(*k)) {
1013                        getreg32(target, pos, k++);
1014                        count -= sizeof(*k);
1015                        pos += sizeof(*k);
1016                }
1017        } else {
1018                compat_ulong_t __user *u = ubuf;
1019                while (count >= sizeof(*u)) {
1020                        compat_ulong_t word;
1021                        getreg32(target, pos, &word);
1022                        if (__put_user(word, u++))
1023                                return -EFAULT;
1024                        count -= sizeof(*u);
1025                        pos += sizeof(*u);
1026                }
1027        }
1028
1029        return 0;
1030}
1031
1032static int genregs32_set(struct task_struct *target,
1033                         const struct user_regset *regset,
1034                         unsigned int pos, unsigned int count,
1035                         const void *kbuf, const void __user *ubuf)
1036{
1037        int ret = 0;
1038        if (kbuf) {
1039                const compat_ulong_t *k = kbuf;
1040                while (count >= sizeof(*k) && !ret) {
1041                        ret = putreg32(target, pos, *k++);
1042                        count -= sizeof(*k);
1043                        pos += sizeof(*k);
1044                }
1045        } else {
1046                const compat_ulong_t __user *u = ubuf;
1047                while (count >= sizeof(*u) && !ret) {
1048                        compat_ulong_t word;
1049                        ret = __get_user(word, u++);
1050                        if (ret)
1051                                break;
1052                        ret = putreg32(target, pos, word);
1053                        count -= sizeof(*u);
1054                        pos += sizeof(*u);
1055                }
1056        }
1057        return ret;
1058}
1059
1060static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1061                             compat_ulong_t caddr, compat_ulong_t cdata)
1062{
1063        unsigned long addr = caddr;
1064        unsigned long data = cdata;
1065        void __user *datap = compat_ptr(data);
1066        int ret;
1067        __u32 val;
1068
1069        switch (request) {
1070        case PTRACE_PEEKUSR:
1071                ret = getreg32(child, addr, &val);
1072                if (ret == 0)
1073                        ret = put_user(val, (__u32 __user *)datap);
1074                break;
1075
1076        case PTRACE_POKEUSR:
1077                ret = putreg32(child, addr, data);
1078                break;
1079
1080        case PTRACE_GETREGS:    /* Get all gp regs from the child. */
1081                return copy_regset_to_user(child, &user_x86_32_view,
1082                                           REGSET_GENERAL,
1083                                           0, sizeof(struct user_regs_struct32),
1084                                           datap);
1085
1086        case PTRACE_SETREGS:    /* Set all gp regs in the child. */
1087                return copy_regset_from_user(child, &user_x86_32_view,
1088                                             REGSET_GENERAL, 0,
1089                                             sizeof(struct user_regs_struct32),
1090                                             datap);
1091
1092        case PTRACE_GETFPREGS:  /* Get the child FPU state. */
1093                return copy_regset_to_user(child, &user_x86_32_view,
1094                                           REGSET_FP, 0,
1095                                           sizeof(struct user_i387_ia32_struct),
1096                                           datap);
1097
1098        case PTRACE_SETFPREGS:  /* Set the child FPU state. */
1099                return copy_regset_from_user(
1100                        child, &user_x86_32_view, REGSET_FP,
1101                        0, sizeof(struct user_i387_ia32_struct), datap);
1102
1103        case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1104                return copy_regset_to_user(child, &user_x86_32_view,
1105                                           REGSET_XFP, 0,
1106                                           sizeof(struct user32_fxsr_struct),
1107                                           datap);
1108
1109        case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1110                return copy_regset_from_user(child, &user_x86_32_view,
1111                                             REGSET_XFP, 0,
1112                                             sizeof(struct user32_fxsr_struct),
1113                                             datap);
1114
1115        case PTRACE_GET_THREAD_AREA:
1116        case PTRACE_SET_THREAD_AREA:
1117                return arch_ptrace(child, request, addr, data);
1118
1119        default:
1120                return compat_ptrace_request(child, request, addr, data);
1121        }
1122
1123        return ret;
1124}
1125#endif /* CONFIG_IA32_EMULATION */
1126
1127#ifdef CONFIG_X86_X32_ABI
1128static long x32_arch_ptrace(struct task_struct *child,
1129                            compat_long_t request, compat_ulong_t caddr,
1130                            compat_ulong_t cdata)
1131{
1132        unsigned long addr = caddr;
1133        unsigned long data = cdata;
1134        void __user *datap = compat_ptr(data);
1135        int ret;
1136
1137        switch (request) {
1138        /* Read 32bits at location addr in the USER area.  Only allow
1139           to return the lower 32bits of segment and debug registers.  */
1140        case PTRACE_PEEKUSR: {
1141                u32 tmp;
1142
1143                ret = -EIO;
1144                if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1145                    addr < offsetof(struct user_regs_struct, cs))
1146                        break;
1147
1148                tmp = 0;  /* Default return condition */
1149                if (addr < sizeof(struct user_regs_struct))
1150                        tmp = getreg(child, addr);
1151                else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1152                         addr <= offsetof(struct user, u_debugreg[7])) {
1153                        addr -= offsetof(struct user, u_debugreg[0]);
1154                        tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1155                }
1156                ret = put_user(tmp, (__u32 __user *)datap);
1157                break;
1158        }
1159
1160        /* Write the word at location addr in the USER area.  Only allow
1161           to update segment and debug registers with the upper 32bits
1162           zero-extended. */
1163        case PTRACE_POKEUSR:
1164                ret = -EIO;
1165                if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1166                    addr < offsetof(struct user_regs_struct, cs))
1167                        break;
1168
1169                if (addr < sizeof(struct user_regs_struct))
1170                        ret = putreg(child, addr, data);
1171                else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1172                         addr <= offsetof(struct user, u_debugreg[7])) {
1173                        addr -= offsetof(struct user, u_debugreg[0]);
1174                        ret = ptrace_set_debugreg(child,
1175                                                  addr / sizeof(data), data);
1176                }
1177                break;
1178
1179        case PTRACE_GETREGS:    /* Get all gp regs from the child. */
1180                return copy_regset_to_user(child,
1181                                           task_user_regset_view(current),
1182                                           REGSET_GENERAL,
1183                                           0, sizeof(struct user_regs_struct),
1184                                           datap);
1185
1186        case PTRACE_SETREGS:    /* Set all gp regs in the child. */
1187                return copy_regset_from_user(child,
1188                                             task_user_regset_view(current),
1189                                             REGSET_GENERAL,
1190                                             0, sizeof(struct user_regs_struct),
1191                                             datap);
1192
1193        case PTRACE_GETFPREGS:  /* Get the child FPU state. */
1194                return copy_regset_to_user(child,
1195                                           task_user_regset_view(current),
1196                                           REGSET_FP,
1197                                           0, sizeof(struct user_i387_struct),
1198                                           datap);
1199
1200        case PTRACE_SETFPREGS:  /* Set the child FPU state. */
1201                return copy_regset_from_user(child,
1202                                             task_user_regset_view(current),
1203                                             REGSET_FP,
1204                                             0, sizeof(struct user_i387_struct),
1205                                             datap);
1206
1207        default:
1208                return compat_ptrace_request(child, request, addr, data);
1209        }
1210
1211        return ret;
1212}
1213#endif
1214
1215#ifdef CONFIG_COMPAT
1216long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1217                        compat_ulong_t caddr, compat_ulong_t cdata)
1218{
1219#ifdef CONFIG_X86_X32_ABI
1220        if (!in_ia32_syscall())
1221                return x32_arch_ptrace(child, request, caddr, cdata);
1222#endif
1223#ifdef CONFIG_IA32_EMULATION
1224        return ia32_arch_ptrace(child, request, caddr, cdata);
1225#else
1226        return 0;
1227#endif
1228}
1229#endif  /* CONFIG_COMPAT */
1230
1231#ifdef CONFIG_X86_64
1232
1233static struct user_regset x86_64_regsets[] __ro_after_init = {
1234        [REGSET_GENERAL] = {
1235                .core_note_type = NT_PRSTATUS,
1236                .n = sizeof(struct user_regs_struct) / sizeof(long),
1237                .size = sizeof(long), .align = sizeof(long),
1238                .get = genregs_get, .set = genregs_set
1239        },
1240        [REGSET_FP] = {
1241                .core_note_type = NT_PRFPREG,
1242                .n = sizeof(struct user_i387_struct) / sizeof(long),
1243                .size = sizeof(long), .align = sizeof(long),
1244                .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1245        },
1246        [REGSET_XSTATE] = {
1247                .core_note_type = NT_X86_XSTATE,
1248                .size = sizeof(u64), .align = sizeof(u64),
1249                .active = xstateregs_active, .get = xstateregs_get,
1250                .set = xstateregs_set
1251        },
1252        [REGSET_IOPERM64] = {
1253                .core_note_type = NT_386_IOPERM,
1254                .n = IO_BITMAP_LONGS,
1255                .size = sizeof(long), .align = sizeof(long),
1256                .active = ioperm_active, .get = ioperm_get
1257        },
1258};
1259
1260static const struct user_regset_view user_x86_64_view = {
1261        .name = "x86_64", .e_machine = EM_X86_64,
1262        .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1263};
1264
1265#else  /* CONFIG_X86_32 */
1266
1267#define user_regs_struct32      user_regs_struct
1268#define genregs32_get           genregs_get
1269#define genregs32_set           genregs_set
1270
1271#endif  /* CONFIG_X86_64 */
1272
1273#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1274static struct user_regset x86_32_regsets[] __ro_after_init = {
1275        [REGSET_GENERAL] = {
1276                .core_note_type = NT_PRSTATUS,
1277                .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1278                .size = sizeof(u32), .align = sizeof(u32),
1279                .get = genregs32_get, .set = genregs32_set
1280        },
1281        [REGSET_FP] = {
1282                .core_note_type = NT_PRFPREG,
1283                .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1284                .size = sizeof(u32), .align = sizeof(u32),
1285                .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1286        },
1287        [REGSET_XFP] = {
1288                .core_note_type = NT_PRXFPREG,
1289                .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1290                .size = sizeof(u32), .align = sizeof(u32),
1291                .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1292        },
1293        [REGSET_XSTATE] = {
1294                .core_note_type = NT_X86_XSTATE,
1295                .size = sizeof(u64), .align = sizeof(u64),
1296                .active = xstateregs_active, .get = xstateregs_get,
1297                .set = xstateregs_set
1298        },
1299        [REGSET_TLS] = {
1300                .core_note_type = NT_386_TLS,
1301                .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1302                .size = sizeof(struct user_desc),
1303                .align = sizeof(struct user_desc),
1304                .active = regset_tls_active,
1305                .get = regset_tls_get, .set = regset_tls_set
1306        },
1307        [REGSET_IOPERM32] = {
1308                .core_note_type = NT_386_IOPERM,
1309                .n = IO_BITMAP_BYTES / sizeof(u32),
1310                .size = sizeof(u32), .align = sizeof(u32),
1311                .active = ioperm_active, .get = ioperm_get
1312        },
1313};
1314
1315static const struct user_regset_view user_x86_32_view = {
1316        .name = "i386", .e_machine = EM_386,
1317        .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1318};
1319#endif
1320
1321/*
1322 * This represents bytes 464..511 in the memory layout exported through
1323 * the REGSET_XSTATE interface.
1324 */
1325u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1326
1327void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1328{
1329#ifdef CONFIG_X86_64
1330        x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1331#endif
1332#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1333        x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1334#endif
1335        xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1336}
1337
1338const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1339{
1340#ifdef CONFIG_IA32_EMULATION
1341        if (!user_64bit_mode(task_pt_regs(task)))
1342#endif
1343#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1344                return &user_x86_32_view;
1345#endif
1346#ifdef CONFIG_X86_64
1347        return &user_x86_64_view;
1348#endif
1349}
1350
1351void send_sigtrap(struct pt_regs *regs, int error_code, int si_code)
1352{
1353        struct task_struct *tsk = current;
1354
1355        tsk->thread.trap_nr = X86_TRAP_DB;
1356        tsk->thread.error_code = error_code;
1357
1358        /* Send us the fake SIGTRAP */
1359        force_sig_fault(SIGTRAP, si_code,
1360                        user_mode(regs) ? (void __user *)regs->ip : NULL);
1361}
1362
1363void user_single_step_report(struct pt_regs *regs)
1364{
1365        send_sigtrap(regs, 0, TRAP_BRKPT);
1366}
1367