Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Tilera Corporation. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation, version 2. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 11 | * NON INFRINGEMENT. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * Copied from i386: Ross Biro 1/23/92 |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/ptrace.h> |
| 19 | #include <linux/kprobes.h> |
| 20 | #include <linux/compat.h> |
| 21 | #include <linux/uaccess.h> |
Simon Marchi | 7be6828 | 2012-12-17 20:08:09 -0500 | [diff] [blame] | 22 | #include <linux/regset.h> |
| 23 | #include <linux/elf.h> |
Simon Marchi | ef18272 | 2012-12-22 00:21:10 -0500 | [diff] [blame] | 24 | #include <linux/tracehook.h> |
Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 25 | #include <asm/traps.h> |
Simon Marchi | 7be6828 | 2012-12-17 20:08:09 -0500 | [diff] [blame] | 26 | #include <arch/chip.h> |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 27 | |
Simon Marchi | ef567f2 | 2013-01-21 19:54:57 -0500 | [diff] [blame] | 28 | #define CREATE_TRACE_POINTS |
| 29 | #include <trace/events/syscalls.h> |
| 30 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 31 | void user_enable_single_step(struct task_struct *child) |
| 32 | { |
| 33 | set_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 34 | } |
| 35 | |
| 36 | void user_disable_single_step(struct task_struct *child) |
| 37 | { |
| 38 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 39 | } |
| 40 | |
| 41 | /* |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 42 | * Called by kernel/ptrace.c when detaching.. |
| 43 | */ |
| 44 | void ptrace_disable(struct task_struct *child) |
| 45 | { |
| 46 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 47 | |
| 48 | /* |
| 49 | * These two are currently unused, but will be set by arch_ptrace() |
| 50 | * and used in the syscall assembly when we do support them. |
| 51 | */ |
| 52 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 53 | } |
| 54 | |
Chris Metcalf | cb67e16 | 2012-12-12 17:24:39 -0500 | [diff] [blame] | 55 | /* |
| 56 | * Get registers from task and ready the result for userspace. |
| 57 | * Note that we localize the API issues to getregs() and putregs() at |
| 58 | * some cost in performance, e.g. we need a full pt_regs copy for |
| 59 | * PEEKUSR, and two copies for POKEUSR. But in general we expect |
| 60 | * GETREGS/PUTREGS to be the API of choice anyway. |
| 61 | */ |
| 62 | static char *getregs(struct task_struct *child, struct pt_regs *uregs) |
| 63 | { |
| 64 | *uregs = *task_pt_regs(child); |
| 65 | |
| 66 | /* Set up flags ABI bits. */ |
| 67 | uregs->flags = 0; |
| 68 | #ifdef CONFIG_COMPAT |
| 69 | if (task_thread_info(child)->status & TS_COMPAT) |
| 70 | uregs->flags |= PT_FLAGS_COMPAT; |
| 71 | #endif |
| 72 | |
| 73 | return (char *)uregs; |
| 74 | } |
| 75 | |
| 76 | /* Put registers back to task. */ |
| 77 | static void putregs(struct task_struct *child, struct pt_regs *uregs) |
| 78 | { |
| 79 | struct pt_regs *regs = task_pt_regs(child); |
| 80 | |
| 81 | /* Don't allow overwriting the kernel-internal flags word. */ |
| 82 | uregs->flags = regs->flags; |
| 83 | |
| 84 | /* Only allow setting the ICS bit in the ex1 word. */ |
| 85 | uregs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(uregs->ex1)); |
| 86 | |
| 87 | *regs = *uregs; |
| 88 | } |
| 89 | |
Simon Marchi | 7be6828 | 2012-12-17 20:08:09 -0500 | [diff] [blame] | 90 | enum tile_regset { |
| 91 | REGSET_GPR, |
| 92 | }; |
| 93 | |
| 94 | static int tile_gpr_get(struct task_struct *target, |
| 95 | const struct user_regset *regset, |
| 96 | unsigned int pos, unsigned int count, |
| 97 | void *kbuf, void __user *ubuf) |
| 98 | { |
| 99 | struct pt_regs regs; |
| 100 | |
| 101 | getregs(target, ®s); |
| 102 | |
| 103 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, ®s, 0, |
| 104 | sizeof(regs)); |
| 105 | } |
| 106 | |
| 107 | static int tile_gpr_set(struct task_struct *target, |
| 108 | const struct user_regset *regset, |
| 109 | unsigned int pos, unsigned int count, |
| 110 | const void *kbuf, const void __user *ubuf) |
| 111 | { |
| 112 | int ret; |
| 113 | struct pt_regs regs; |
| 114 | |
| 115 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, ®s, 0, |
| 116 | sizeof(regs)); |
| 117 | if (ret) |
| 118 | return ret; |
| 119 | |
| 120 | putregs(target, ®s); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static const struct user_regset tile_user_regset[] = { |
| 126 | [REGSET_GPR] = { |
| 127 | .core_note_type = NT_PRSTATUS, |
| 128 | .n = ELF_NGREG, |
| 129 | .size = sizeof(elf_greg_t), |
| 130 | .align = sizeof(elf_greg_t), |
| 131 | .get = tile_gpr_get, |
| 132 | .set = tile_gpr_set, |
| 133 | }, |
| 134 | }; |
| 135 | |
| 136 | static const struct user_regset_view tile_user_regset_view = { |
| 137 | .name = CHIP_ARCH_NAME, |
| 138 | .e_machine = ELF_ARCH, |
| 139 | .ei_osabi = ELF_OSABI, |
| 140 | .regsets = tile_user_regset, |
| 141 | .n = ARRAY_SIZE(tile_user_regset), |
| 142 | }; |
| 143 | |
| 144 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) |
| 145 | { |
| 146 | return &tile_user_regset_view; |
| 147 | } |
| 148 | |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 149 | long arch_ptrace(struct task_struct *child, long request, |
| 150 | unsigned long addr, unsigned long data) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 151 | { |
Chris Metcalf | ce7f2a3 | 2010-10-14 16:48:00 -0400 | [diff] [blame] | 152 | unsigned long __user *datap = (long __user __force *)data; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 153 | unsigned long tmp; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 154 | long ret = -EIO; |
Chris Metcalf | ce7f2a3 | 2010-10-14 16:48:00 -0400 | [diff] [blame] | 155 | char *childreg; |
Chris Metcalf | 1deb9c5 | 2010-10-28 15:47:06 -0400 | [diff] [blame] | 156 | struct pt_regs copyregs; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 157 | |
| 158 | switch (request) { |
| 159 | |
| 160 | case PTRACE_PEEKUSR: /* Read register from pt_regs. */ |
Namhyung Kim | 8c0acac | 2010-10-27 15:34:04 -0700 | [diff] [blame] | 161 | if (addr >= PTREGS_SIZE) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 162 | break; |
Chris Metcalf | cb67e16 | 2012-12-12 17:24:39 -0500 | [diff] [blame] | 163 | childreg = getregs(child, ©regs) + addr; |
Chris Metcalf | ce7f2a3 | 2010-10-14 16:48:00 -0400 | [diff] [blame] | 164 | #ifdef CONFIG_COMPAT |
| 165 | if (is_compat_task()) { |
| 166 | if (addr & (sizeof(compat_long_t)-1)) |
| 167 | break; |
| 168 | ret = put_user(*(compat_long_t *)childreg, |
| 169 | (compat_long_t __user *)datap); |
| 170 | } else |
| 171 | #endif |
| 172 | { |
| 173 | if (addr & (sizeof(long)-1)) |
| 174 | break; |
| 175 | ret = put_user(*(long *)childreg, datap); |
| 176 | } |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 177 | break; |
| 178 | |
| 179 | case PTRACE_POKEUSR: /* Write register in pt_regs. */ |
Namhyung Kim | 8c0acac | 2010-10-27 15:34:04 -0700 | [diff] [blame] | 180 | if (addr >= PTREGS_SIZE) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 181 | break; |
Chris Metcalf | cb67e16 | 2012-12-12 17:24:39 -0500 | [diff] [blame] | 182 | childreg = getregs(child, ©regs) + addr; |
Chris Metcalf | ce7f2a3 | 2010-10-14 16:48:00 -0400 | [diff] [blame] | 183 | #ifdef CONFIG_COMPAT |
| 184 | if (is_compat_task()) { |
| 185 | if (addr & (sizeof(compat_long_t)-1)) |
| 186 | break; |
| 187 | *(compat_long_t *)childreg = data; |
| 188 | } else |
| 189 | #endif |
| 190 | { |
| 191 | if (addr & (sizeof(long)-1)) |
| 192 | break; |
| 193 | *(long *)childreg = data; |
| 194 | } |
Chris Metcalf | cb67e16 | 2012-12-12 17:24:39 -0500 | [diff] [blame] | 195 | putregs(child, ©regs); |
Chris Metcalf | bcd97c3 | 2010-07-02 14:17:52 -0400 | [diff] [blame] | 196 | ret = 0; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 197 | break; |
| 198 | |
| 199 | case PTRACE_GETREGS: /* Get all registers from the child. */ |
Simon Marchi | 9af6254 | 2012-12-17 20:08:10 -0500 | [diff] [blame] | 200 | ret = copy_regset_to_user(child, &tile_user_regset_view, |
| 201 | REGSET_GPR, 0, |
| 202 | sizeof(struct pt_regs), datap); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 203 | break; |
| 204 | |
| 205 | case PTRACE_SETREGS: /* Set all registers in the child. */ |
Simon Marchi | 9af6254 | 2012-12-17 20:08:10 -0500 | [diff] [blame] | 206 | ret = copy_regset_from_user(child, &tile_user_regset_view, |
| 207 | REGSET_GPR, 0, |
| 208 | sizeof(struct pt_regs), datap); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 209 | break; |
| 210 | |
| 211 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ |
| 212 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ |
| 213 | break; |
| 214 | |
| 215 | case PTRACE_SETOPTIONS: |
| 216 | /* Support TILE-specific ptrace options. */ |
Chris Metcalf | 395e095 | 2012-12-13 11:34:45 -0500 | [diff] [blame] | 217 | BUILD_BUG_ON(PTRACE_O_MASK_TILE & PTRACE_O_MASK); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 218 | tmp = data & PTRACE_O_MASK_TILE; |
| 219 | data &= ~PTRACE_O_MASK_TILE; |
| 220 | ret = ptrace_request(child, request, addr, data); |
Chris Metcalf | 395e095 | 2012-12-13 11:34:45 -0500 | [diff] [blame] | 221 | if (ret == 0) { |
| 222 | unsigned int flags = child->ptrace; |
| 223 | flags &= ~(PTRACE_O_MASK_TILE << PT_OPT_FLAG_SHIFT); |
| 224 | flags |= (tmp << PT_OPT_FLAG_SHIFT); |
| 225 | child->ptrace = flags; |
| 226 | } |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 227 | break; |
| 228 | |
| 229 | default: |
| 230 | #ifdef CONFIG_COMPAT |
| 231 | if (task_thread_info(current)->status & TS_COMPAT) { |
| 232 | ret = compat_ptrace_request(child, request, |
| 233 | addr, data); |
| 234 | break; |
| 235 | } |
| 236 | #endif |
| 237 | ret = ptrace_request(child, request, addr, data); |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | #ifdef CONFIG_COMPAT |
| 245 | /* Not used; we handle compat issues in arch_ptrace() directly. */ |
| 246 | long compat_arch_ptrace(struct task_struct *child, compat_long_t request, |
| 247 | compat_ulong_t addr, compat_ulong_t data) |
| 248 | { |
| 249 | BUG(); |
| 250 | } |
| 251 | #endif |
| 252 | |
Simon Marchi | ef18272 | 2012-12-22 00:21:10 -0500 | [diff] [blame] | 253 | int do_syscall_trace_enter(struct pt_regs *regs) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 254 | { |
Simon Marchi | ef567f2 | 2013-01-21 19:54:57 -0500 | [diff] [blame] | 255 | if (test_thread_flag(TIF_SYSCALL_TRACE)) { |
| 256 | if (tracehook_report_syscall_entry(regs)) |
| 257 | regs->regs[TREG_SYSCALL_NR] = -1; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 258 | } |
Simon Marchi | ef18272 | 2012-12-22 00:21:10 -0500 | [diff] [blame] | 259 | |
Simon Marchi | ef567f2 | 2013-01-21 19:54:57 -0500 | [diff] [blame] | 260 | if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) |
| 261 | trace_sys_enter(regs, regs->regs[TREG_SYSCALL_NR]); |
| 262 | |
Simon Marchi | ef18272 | 2012-12-22 00:21:10 -0500 | [diff] [blame] | 263 | return regs->regs[TREG_SYSCALL_NR]; |
| 264 | } |
| 265 | |
| 266 | void do_syscall_trace_exit(struct pt_regs *regs) |
| 267 | { |
Simon Marchi | ef567f2 | 2013-01-21 19:54:57 -0500 | [diff] [blame] | 268 | if (test_thread_flag(TIF_SYSCALL_TRACE)) |
| 269 | tracehook_report_syscall_exit(regs, 0); |
| 270 | |
| 271 | if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) |
Simon Marchi | 9fc1894 | 2013-04-17 11:01:22 -0400 | [diff] [blame] | 272 | trace_sys_exit(regs, regs->regs[0]); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code) |
| 276 | { |
| 277 | struct siginfo info; |
| 278 | |
| 279 | memset(&info, 0, sizeof(info)); |
| 280 | info.si_signo = SIGTRAP; |
| 281 | info.si_code = TRAP_BRKPT; |
| 282 | info.si_addr = (void __user *) regs->pc; |
| 283 | |
| 284 | /* Send us the fakey SIGTRAP */ |
| 285 | force_sig_info(SIGTRAP, &info, tsk); |
| 286 | } |
| 287 | |
| 288 | /* Handle synthetic interrupt delivered only by the simulator. */ |
| 289 | void __kprobes do_breakpoint(struct pt_regs* regs, int fault_num) |
| 290 | { |
| 291 | send_sigtrap(current, regs, fault_num); |
| 292 | } |