James Hogan | bc3966b | 2012-10-09 10:54:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005-2012 Imagination Technologies Ltd. |
| 3 | * |
| 4 | * This file is subject to the terms and conditions of the GNU General |
| 5 | * Public License. See the file COPYING in the main directory of |
| 6 | * this archive for more details. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/ptrace.h> |
| 13 | #include <linux/user.h> |
| 14 | #include <linux/regset.h> |
| 15 | #include <linux/tracehook.h> |
| 16 | #include <linux/elf.h> |
| 17 | #include <linux/uaccess.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 18 | #include <linux/sched/task_stack.h> |
| 19 | |
James Hogan | bc3966b | 2012-10-09 10:54:36 +0100 | [diff] [blame] | 20 | #include <trace/syscall.h> |
| 21 | |
| 22 | #define CREATE_TRACE_POINTS |
| 23 | #include <trace/events/syscalls.h> |
| 24 | |
| 25 | /* |
| 26 | * user_regset definitions. |
| 27 | */ |
| 28 | |
Dave Martin | 5fe81fe | 2017-03-27 15:10:56 +0100 | [diff] [blame] | 29 | static unsigned long user_txstatus(const struct pt_regs *regs) |
| 30 | { |
| 31 | unsigned long data = (unsigned long)regs->ctx.Flags; |
| 32 | |
| 33 | if (regs->ctx.SaveMask & TBICTX_CBUF_BIT) |
| 34 | data |= USER_GP_REGS_STATUS_CATCH_BIT; |
| 35 | |
| 36 | return data; |
| 37 | } |
| 38 | |
James Hogan | bc3966b | 2012-10-09 10:54:36 +0100 | [diff] [blame] | 39 | int metag_gp_regs_copyout(const struct pt_regs *regs, |
| 40 | unsigned int pos, unsigned int count, |
| 41 | void *kbuf, void __user *ubuf) |
| 42 | { |
| 43 | const void *ptr; |
| 44 | unsigned long data; |
| 45 | int ret; |
| 46 | |
| 47 | /* D{0-1}.{0-7} */ |
| 48 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 49 | regs->ctx.DX, 0, 4*16); |
| 50 | if (ret) |
| 51 | goto out; |
| 52 | /* A{0-1}.{0-1} */ |
| 53 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 54 | regs->ctx.AX, 4*16, 4*20); |
| 55 | if (ret) |
| 56 | goto out; |
| 57 | /* A{0-1}.2 */ |
| 58 | if (regs->ctx.SaveMask & TBICTX_XEXT_BIT) |
| 59 | ptr = regs->ctx.Ext.Ctx.pExt; |
| 60 | else |
| 61 | ptr = ®s->ctx.Ext.AX2; |
| 62 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 63 | ptr, 4*20, 4*22); |
| 64 | if (ret) |
| 65 | goto out; |
| 66 | /* A{0-1}.3 */ |
| 67 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 68 | ®s->ctx.AX3, 4*22, 4*24); |
| 69 | if (ret) |
| 70 | goto out; |
| 71 | /* PC */ |
| 72 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 73 | ®s->ctx.CurrPC, 4*24, 4*25); |
| 74 | if (ret) |
| 75 | goto out; |
| 76 | /* TXSTATUS */ |
Dave Martin | 5fe81fe | 2017-03-27 15:10:56 +0100 | [diff] [blame] | 77 | data = user_txstatus(regs); |
James Hogan | bc3966b | 2012-10-09 10:54:36 +0100 | [diff] [blame] | 78 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 79 | &data, 4*25, 4*26); |
| 80 | if (ret) |
| 81 | goto out; |
| 82 | /* TXRPT, TXBPOBITS, TXMODE */ |
| 83 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 84 | ®s->ctx.CurrRPT, 4*26, 4*29); |
| 85 | if (ret) |
| 86 | goto out; |
| 87 | /* Padding */ |
| 88 | ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, |
| 89 | 4*29, 4*30); |
| 90 | out: |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | int metag_gp_regs_copyin(struct pt_regs *regs, |
| 95 | unsigned int pos, unsigned int count, |
| 96 | const void *kbuf, const void __user *ubuf) |
| 97 | { |
| 98 | void *ptr; |
| 99 | unsigned long data; |
| 100 | int ret; |
| 101 | |
| 102 | /* D{0-1}.{0-7} */ |
| 103 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
| 104 | regs->ctx.DX, 0, 4*16); |
| 105 | if (ret) |
| 106 | goto out; |
| 107 | /* A{0-1}.{0-1} */ |
| 108 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
| 109 | regs->ctx.AX, 4*16, 4*20); |
| 110 | if (ret) |
| 111 | goto out; |
| 112 | /* A{0-1}.2 */ |
| 113 | if (regs->ctx.SaveMask & TBICTX_XEXT_BIT) |
| 114 | ptr = regs->ctx.Ext.Ctx.pExt; |
| 115 | else |
| 116 | ptr = ®s->ctx.Ext.AX2; |
| 117 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
| 118 | ptr, 4*20, 4*22); |
| 119 | if (ret) |
| 120 | goto out; |
| 121 | /* A{0-1}.3 */ |
| 122 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
| 123 | ®s->ctx.AX3, 4*22, 4*24); |
| 124 | if (ret) |
| 125 | goto out; |
| 126 | /* PC */ |
| 127 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
| 128 | ®s->ctx.CurrPC, 4*24, 4*25); |
| 129 | if (ret) |
| 130 | goto out; |
| 131 | /* TXSTATUS */ |
Dave Martin | 5fe81fe | 2017-03-27 15:10:56 +0100 | [diff] [blame] | 132 | data = user_txstatus(regs); |
James Hogan | bc3966b | 2012-10-09 10:54:36 +0100 | [diff] [blame] | 133 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
| 134 | &data, 4*25, 4*26); |
| 135 | if (ret) |
| 136 | goto out; |
| 137 | regs->ctx.Flags = data & 0xffff; |
| 138 | if (data & USER_GP_REGS_STATUS_CATCH_BIT) |
| 139 | regs->ctx.SaveMask |= TBICTX_XCBF_BIT | TBICTX_CBUF_BIT; |
| 140 | else |
| 141 | regs->ctx.SaveMask &= ~TBICTX_CBUF_BIT; |
| 142 | /* TXRPT, TXBPOBITS, TXMODE */ |
| 143 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
| 144 | ®s->ctx.CurrRPT, 4*26, 4*29); |
| 145 | out: |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | static int metag_gp_regs_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 | return metag_gp_regs_copyout(regs, pos, count, kbuf, ubuf); |
| 156 | } |
| 157 | |
| 158 | static int metag_gp_regs_set(struct task_struct *target, |
| 159 | const struct user_regset *regset, |
| 160 | unsigned int pos, unsigned int count, |
| 161 | const void *kbuf, const void __user *ubuf) |
| 162 | { |
| 163 | struct pt_regs *regs = task_pt_regs(target); |
| 164 | return metag_gp_regs_copyin(regs, pos, count, kbuf, ubuf); |
| 165 | } |
| 166 | |
| 167 | int metag_cb_regs_copyout(const struct pt_regs *regs, |
| 168 | unsigned int pos, unsigned int count, |
| 169 | void *kbuf, void __user *ubuf) |
| 170 | { |
| 171 | int ret; |
| 172 | |
| 173 | /* TXCATCH{0-3} */ |
| 174 | if (regs->ctx.SaveMask & TBICTX_XCBF_BIT) |
| 175 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 176 | regs->extcb0, 0, 4*4); |
| 177 | else |
| 178 | ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, |
| 179 | 0, 4*4); |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | int metag_cb_regs_copyin(struct pt_regs *regs, |
| 184 | unsigned int pos, unsigned int count, |
| 185 | const void *kbuf, const void __user *ubuf) |
| 186 | { |
| 187 | int ret; |
| 188 | |
| 189 | /* TXCATCH{0-3} */ |
| 190 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
| 191 | regs->extcb0, 0, 4*4); |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | static int metag_cb_regs_get(struct task_struct *target, |
| 196 | const struct user_regset *regset, |
| 197 | unsigned int pos, unsigned int count, |
| 198 | void *kbuf, void __user *ubuf) |
| 199 | { |
| 200 | const struct pt_regs *regs = task_pt_regs(target); |
| 201 | return metag_cb_regs_copyout(regs, pos, count, kbuf, ubuf); |
| 202 | } |
| 203 | |
| 204 | static int metag_cb_regs_set(struct task_struct *target, |
| 205 | const struct user_regset *regset, |
| 206 | unsigned int pos, unsigned int count, |
| 207 | const void *kbuf, const void __user *ubuf) |
| 208 | { |
| 209 | struct pt_regs *regs = task_pt_regs(target); |
| 210 | return metag_cb_regs_copyin(regs, pos, count, kbuf, ubuf); |
| 211 | } |
| 212 | |
| 213 | int metag_rp_state_copyout(const struct pt_regs *regs, |
| 214 | unsigned int pos, unsigned int count, |
| 215 | void *kbuf, void __user *ubuf) |
| 216 | { |
| 217 | unsigned long mask; |
| 218 | u64 *ptr; |
| 219 | int ret, i; |
| 220 | |
| 221 | /* Empty read pipeline */ |
| 222 | if (!(regs->ctx.SaveMask & TBICTX_CBRP_BIT)) { |
| 223 | ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, |
| 224 | 0, 4*13); |
| 225 | goto out; |
| 226 | } |
| 227 | |
| 228 | mask = (regs->ctx.CurrDIVTIME & TXDIVTIME_RPMASK_BITS) >> |
| 229 | TXDIVTIME_RPMASK_S; |
| 230 | |
| 231 | /* Read pipeline entries */ |
| 232 | ptr = (void *)®s->extcb0[1]; |
| 233 | for (i = 0; i < 6; ++i, ++ptr) { |
| 234 | if (mask & (1 << i)) |
| 235 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 236 | ptr, 8*i, 8*(i + 1)); |
| 237 | else |
| 238 | ret = user_regset_copyout_zero(&pos, &count, &kbuf, |
| 239 | &ubuf, 8*i, 8*(i + 1)); |
| 240 | if (ret) |
| 241 | goto out; |
| 242 | } |
| 243 | /* Mask of entries */ |
| 244 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, |
| 245 | &mask, 4*12, 4*13); |
| 246 | out: |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | int metag_rp_state_copyin(struct pt_regs *regs, |
| 251 | unsigned int pos, unsigned int count, |
| 252 | const void *kbuf, const void __user *ubuf) |
| 253 | { |
| 254 | struct user_rp_state rp; |
| 255 | unsigned long long *ptr; |
| 256 | int ret, i; |
| 257 | |
Dave Martin | 7195ee3 | 2017-03-27 15:10:57 +0100 | [diff] [blame] | 258 | if (count < 4*13) |
| 259 | return -EINVAL; |
James Hogan | bc3966b | 2012-10-09 10:54:36 +0100 | [diff] [blame] | 260 | /* Read the entire pipeline before making any changes */ |
| 261 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, |
| 262 | &rp, 0, 4*13); |
| 263 | if (ret) |
| 264 | goto out; |
| 265 | |
| 266 | /* Write pipeline entries */ |
| 267 | ptr = (void *)®s->extcb0[1]; |
| 268 | for (i = 0; i < 6; ++i, ++ptr) |
| 269 | if (rp.mask & (1 << i)) |
| 270 | *ptr = rp.entries[i]; |
| 271 | |
| 272 | /* Update RPMask in TXDIVTIME */ |
| 273 | regs->ctx.CurrDIVTIME &= ~TXDIVTIME_RPMASK_BITS; |
| 274 | regs->ctx.CurrDIVTIME |= (rp.mask << TXDIVTIME_RPMASK_S) |
| 275 | & TXDIVTIME_RPMASK_BITS; |
| 276 | |
| 277 | /* Set/clear flags to indicate catch/read pipeline state */ |
| 278 | if (rp.mask) |
| 279 | regs->ctx.SaveMask |= TBICTX_XCBF_BIT | TBICTX_CBRP_BIT; |
| 280 | else |
| 281 | regs->ctx.SaveMask &= ~TBICTX_CBRP_BIT; |
| 282 | out: |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static int metag_rp_state_get(struct task_struct *target, |
| 287 | const struct user_regset *regset, |
| 288 | unsigned int pos, unsigned int count, |
| 289 | void *kbuf, void __user *ubuf) |
| 290 | { |
| 291 | const struct pt_regs *regs = task_pt_regs(target); |
| 292 | return metag_rp_state_copyout(regs, pos, count, kbuf, ubuf); |
| 293 | } |
| 294 | |
| 295 | static int metag_rp_state_set(struct task_struct *target, |
| 296 | const struct user_regset *regset, |
| 297 | unsigned int pos, unsigned int count, |
| 298 | const void *kbuf, const void __user *ubuf) |
| 299 | { |
| 300 | struct pt_regs *regs = task_pt_regs(target); |
| 301 | return metag_rp_state_copyin(regs, pos, count, kbuf, ubuf); |
| 302 | } |
| 303 | |
Paul Clothier | 876d6dc | 2013-03-19 12:04:43 +0000 | [diff] [blame] | 304 | static int metag_tls_get(struct task_struct *target, |
| 305 | const struct user_regset *regset, |
| 306 | unsigned int pos, unsigned int count, |
| 307 | void *kbuf, void __user *ubuf) |
| 308 | { |
| 309 | void __user *tls = target->thread.tls_ptr; |
| 310 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1); |
| 311 | } |
| 312 | |
| 313 | static int metag_tls_set(struct task_struct *target, |
| 314 | const struct user_regset *regset, |
| 315 | unsigned int pos, unsigned int count, |
| 316 | const void *kbuf, const void __user *ubuf) |
| 317 | { |
| 318 | int ret; |
Dave Martin | a78ce80 | 2017-03-27 15:10:55 +0100 | [diff] [blame] | 319 | void __user *tls = target->thread.tls_ptr; |
Paul Clothier | 876d6dc | 2013-03-19 12:04:43 +0000 | [diff] [blame] | 320 | |
| 321 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | |
| 325 | target->thread.tls_ptr = tls; |
| 326 | return ret; |
| 327 | } |
| 328 | |
James Hogan | bc3966b | 2012-10-09 10:54:36 +0100 | [diff] [blame] | 329 | enum metag_regset { |
| 330 | REGSET_GENERAL, |
| 331 | REGSET_CBUF, |
| 332 | REGSET_READPIPE, |
Paul Clothier | 876d6dc | 2013-03-19 12:04:43 +0000 | [diff] [blame] | 333 | REGSET_TLS, |
James Hogan | bc3966b | 2012-10-09 10:54:36 +0100 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | static const struct user_regset metag_regsets[] = { |
| 337 | [REGSET_GENERAL] = { |
| 338 | .core_note_type = NT_PRSTATUS, |
| 339 | .n = ELF_NGREG, |
| 340 | .size = sizeof(long), |
| 341 | .align = sizeof(long long), |
| 342 | .get = metag_gp_regs_get, |
| 343 | .set = metag_gp_regs_set, |
| 344 | }, |
| 345 | [REGSET_CBUF] = { |
| 346 | .core_note_type = NT_METAG_CBUF, |
| 347 | .n = sizeof(struct user_cb_regs) / sizeof(long), |
| 348 | .size = sizeof(long), |
| 349 | .align = sizeof(long long), |
| 350 | .get = metag_cb_regs_get, |
| 351 | .set = metag_cb_regs_set, |
| 352 | }, |
| 353 | [REGSET_READPIPE] = { |
| 354 | .core_note_type = NT_METAG_RPIPE, |
| 355 | .n = sizeof(struct user_rp_state) / sizeof(long), |
| 356 | .size = sizeof(long), |
| 357 | .align = sizeof(long long), |
| 358 | .get = metag_rp_state_get, |
| 359 | .set = metag_rp_state_set, |
| 360 | }, |
Paul Clothier | 876d6dc | 2013-03-19 12:04:43 +0000 | [diff] [blame] | 361 | [REGSET_TLS] = { |
| 362 | .core_note_type = NT_METAG_TLS, |
| 363 | .n = 1, |
| 364 | .size = sizeof(void *), |
| 365 | .align = sizeof(void *), |
| 366 | .get = metag_tls_get, |
| 367 | .set = metag_tls_set, |
| 368 | }, |
James Hogan | bc3966b | 2012-10-09 10:54:36 +0100 | [diff] [blame] | 369 | }; |
| 370 | |
| 371 | static const struct user_regset_view user_metag_view = { |
| 372 | .name = "metag", |
| 373 | .e_machine = EM_METAG, |
| 374 | .regsets = metag_regsets, |
| 375 | .n = ARRAY_SIZE(metag_regsets) |
| 376 | }; |
| 377 | |
| 378 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) |
| 379 | { |
| 380 | return &user_metag_view; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Called by kernel/ptrace.c when detaching.. |
| 385 | * |
| 386 | * Make sure single step bits etc are not set. |
| 387 | */ |
| 388 | void ptrace_disable(struct task_struct *child) |
| 389 | { |
| 390 | /* nothing to do.. */ |
| 391 | } |
| 392 | |
| 393 | long arch_ptrace(struct task_struct *child, long request, unsigned long addr, |
| 394 | unsigned long data) |
| 395 | { |
| 396 | int ret; |
| 397 | |
| 398 | switch (request) { |
| 399 | default: |
| 400 | ret = ptrace_request(child, request, addr, data); |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | int syscall_trace_enter(struct pt_regs *regs) |
| 408 | { |
| 409 | int ret = 0; |
| 410 | |
| 411 | if (test_thread_flag(TIF_SYSCALL_TRACE)) |
| 412 | ret = tracehook_report_syscall_entry(regs); |
| 413 | |
| 414 | if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT))) |
| 415 | trace_sys_enter(regs, regs->ctx.DX[0].U1); |
| 416 | |
| 417 | return ret ? -1 : regs->ctx.DX[0].U1; |
| 418 | } |
| 419 | |
| 420 | void syscall_trace_leave(struct pt_regs *regs) |
| 421 | { |
| 422 | if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT))) |
| 423 | trace_sys_exit(regs, regs->ctx.DX[0].U1); |
| 424 | |
| 425 | if (test_thread_flag(TIF_SYSCALL_TRACE)) |
| 426 | tracehook_report_syscall_exit(regs, 0); |
| 427 | } |