Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000-2003, Axis Communications AB. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/sched.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 7 | #include <linux/sched/task_stack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/mm.h> |
| 9 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/errno.h> |
| 11 | #include <linux/ptrace.h> |
| 12 | #include <linux/user.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 13 | #include <linux/signal.h> |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 14 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/page.h> |
| 18 | #include <asm/pgtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/processor.h> |
| 20 | |
| 21 | /* |
| 22 | * Determines which bits in DCCR the user has access to. |
| 23 | * 1 = access, 0 = no access. |
| 24 | */ |
| 25 | #define DCCR_MASK 0x0000001f /* XNZVC */ |
| 26 | |
| 27 | /* |
| 28 | * Get contents of register REGNO in task TASK. |
| 29 | */ |
| 30 | inline long get_reg(struct task_struct *task, unsigned int regno) |
| 31 | { |
| 32 | /* USP is a special case, it's not in the pt_regs struct but |
| 33 | * in the tasks thread struct |
| 34 | */ |
| 35 | |
| 36 | if (regno == PT_USP) |
| 37 | return task->thread.usp; |
| 38 | else if (regno < PT_MAX) |
Al Viro | 95ca0dc | 2006-01-12 01:06:03 -0800 | [diff] [blame] | 39 | return ((unsigned long *)task_pt_regs(task))[regno]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | else |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Write contents of register REGNO in task TASK. |
| 46 | */ |
| 47 | inline int put_reg(struct task_struct *task, unsigned int regno, |
| 48 | unsigned long data) |
| 49 | { |
| 50 | if (regno == PT_USP) |
| 51 | task->thread.usp = data; |
| 52 | else if (regno < PT_MAX) |
Al Viro | 95ca0dc | 2006-01-12 01:06:03 -0800 | [diff] [blame] | 53 | ((unsigned long *)task_pt_regs(task))[regno] = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | else |
| 55 | return -1; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Called by kernel/ptrace.c when detaching. |
| 61 | * |
| 62 | * Make sure the single step bit is not set. |
| 63 | */ |
| 64 | void |
| 65 | ptrace_disable(struct task_struct *child) |
| 66 | { |
| 67 | /* Todo - pending singlesteps? */ |
Jesper Nilsson | 2afab72 | 2008-01-21 14:06:32 +0100 | [diff] [blame] | 68 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Note that this implementation of ptrace behaves differently from vanilla |
| 73 | * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT, |
| 74 | * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not |
| 75 | * ignored. Instead, the data variable is expected to point at a location |
| 76 | * (in user space) where the result of the ptrace call is written (instead of |
| 77 | * being returned). |
| 78 | */ |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 79 | long arch_ptrace(struct task_struct *child, long request, |
| 80 | unsigned long addr, unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | int ret; |
Namhyung Kim | 475a4b8 | 2010-10-27 15:33:51 -0700 | [diff] [blame] | 83 | unsigned int regno = addr >> 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | unsigned long __user *datap = (unsigned long __user *)data; |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | switch (request) { |
| 87 | /* Read word at location address. */ |
| 88 | case PTRACE_PEEKTEXT: |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame] | 89 | case PTRACE_PEEKDATA: |
| 90 | ret = generic_ptrace_peekdata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | /* Read the word at location address in the USER area. */ |
| 94 | case PTRACE_PEEKUSR: { |
| 95 | unsigned long tmp; |
| 96 | |
| 97 | ret = -EIO; |
Namhyung Kim | 475a4b8 | 2010-10-27 15:33:51 -0700 | [diff] [blame] | 98 | if ((addr & 3) || regno > PT_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | break; |
| 100 | |
Namhyung Kim | 475a4b8 | 2010-10-27 15:33:51 -0700 | [diff] [blame] | 101 | tmp = get_reg(child, regno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | ret = put_user(tmp, datap); |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | /* Write the word at location address. */ |
| 107 | case PTRACE_POKETEXT: |
| 108 | case PTRACE_POKEDATA: |
Alexey Dobriyan | f284ce7 | 2007-07-17 04:03:44 -0700 | [diff] [blame] | 109 | ret = generic_ptrace_pokedata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | break; |
| 111 | |
| 112 | /* Write the word at location address in the USER area. */ |
| 113 | case PTRACE_POKEUSR: |
| 114 | ret = -EIO; |
Namhyung Kim | 475a4b8 | 2010-10-27 15:33:51 -0700 | [diff] [blame] | 115 | if ((addr & 3) || regno > PT_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | break; |
| 117 | |
Namhyung Kim | 475a4b8 | 2010-10-27 15:33:51 -0700 | [diff] [blame] | 118 | if (regno == PT_DCCR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /* don't allow the tracing process to change stuff like |
| 120 | * interrupt enable, kernel/user bit, dma enables etc. |
| 121 | */ |
| 122 | data &= DCCR_MASK; |
| 123 | data |= get_reg(child, PT_DCCR) & ~DCCR_MASK; |
| 124 | } |
Namhyung Kim | 475a4b8 | 2010-10-27 15:33:51 -0700 | [diff] [blame] | 125 | if (put_reg(child, regno, data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | break; |
| 127 | ret = 0; |
| 128 | break; |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* Get all GP registers from the child. */ |
| 131 | case PTRACE_GETREGS: { |
| 132 | int i; |
| 133 | unsigned long tmp; |
| 134 | |
Al Viro | c350885 | 2006-01-28 22:17:11 -0500 | [diff] [blame] | 135 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | for (i = 0; i <= PT_MAX; i++) { |
| 137 | tmp = get_reg(child, i); |
| 138 | |
| 139 | if (put_user(tmp, datap)) { |
| 140 | ret = -EFAULT; |
Al Viro | c350885 | 2006-01-28 22:17:11 -0500 | [diff] [blame] | 141 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Namhyung Kim | 475a4b8 | 2010-10-27 15:33:51 -0700 | [diff] [blame] | 144 | datap++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | |
| 150 | /* Set all GP registers in the child. */ |
| 151 | case PTRACE_SETREGS: { |
| 152 | int i; |
| 153 | unsigned long tmp; |
| 154 | |
Al Viro | c350885 | 2006-01-28 22:17:11 -0500 | [diff] [blame] | 155 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | for (i = 0; i <= PT_MAX; i++) { |
| 157 | if (get_user(tmp, datap)) { |
| 158 | ret = -EFAULT; |
Al Viro | c350885 | 2006-01-28 22:17:11 -0500 | [diff] [blame] | 159 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | if (i == PT_DCCR) { |
| 163 | tmp &= DCCR_MASK; |
| 164 | tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK; |
| 165 | } |
| 166 | |
| 167 | put_reg(child, i, tmp); |
Namhyung Kim | 475a4b8 | 2010-10-27 15:33:51 -0700 | [diff] [blame] | 168 | datap++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | |
| 174 | default: |
| 175 | ret = ptrace_request(child, request, addr, data); |
| 176 | break; |
| 177 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | void do_syscall_trace(void) |
| 183 | { |
| 184 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
| 185 | return; |
| 186 | |
| 187 | if (!(current->ptrace & PT_PTRACED)) |
| 188 | return; |
| 189 | |
| 190 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 191 | between a syscall stop and SIGTRAP delivery */ |
| 192 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
| 193 | ? 0x80 : 0)); |
| 194 | |
| 195 | /* |
| 196 | * This isn't the same as continuing with a signal, but it will do for |
| 197 | * normal use. |
| 198 | */ |
| 199 | if (current->exit_code) { |
| 200 | send_sig(current->exit_code, current, 1); |
| 201 | current->exit_code = 0; |
| 202 | } |
| 203 | } |