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