at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright 2010 Tilera Corporation. All Rights Reserved. |
| 4 | * Copyright 2015 Regents of the University of California |
| 5 | * Copyright 2017 SiFive |
| 6 | * |
| 7 | * Copied from arch/tile/kernel/ptrace.c |
| 8 | */ |
| 9 | |
| 10 | #include <asm/vector.h> |
| 11 | #include <asm/ptrace.h> |
| 12 | #include <asm/syscall.h> |
| 13 | #include <asm/thread_info.h> |
| 14 | #include <asm/switch_to.h> |
| 15 | #include <linux/audit.h> |
| 16 | #include <linux/compat.h> |
| 17 | #include <linux/ptrace.h> |
| 18 | #include <linux/elf.h> |
| 19 | #include <linux/regset.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/sched/task_stack.h> |
| 22 | #include <asm/usercfi.h> |
| 23 | |
| 24 | enum riscv_regset { |
| 25 | REGSET_X, |
| 26 | #ifdef CONFIG_FPU |
| 27 | REGSET_F, |
| 28 | #endif |
| 29 | #ifdef CONFIG_RISCV_ISA_V |
| 30 | REGSET_V, |
| 31 | #endif |
| 32 | #ifdef CONFIG_RISCV_ISA_SUPM |
| 33 | REGSET_TAGGED_ADDR_CTRL, |
| 34 | #endif |
| 35 | #ifdef CONFIG_RISCV_USER_CFI |
| 36 | REGSET_CFI, |
| 37 | #endif |
| 38 | }; |
| 39 | |
| 40 | static int riscv_gpr_get(struct task_struct *target, |
| 41 | const struct user_regset *regset, |
| 42 | struct membuf to) |
| 43 | { |
| 44 | return membuf_write(&to, task_pt_regs(target), |
| 45 | sizeof(struct user_regs_struct)); |
| 46 | } |
| 47 | |
| 48 | static int riscv_gpr_set(struct task_struct *target, |
| 49 | const struct user_regset *regset, |
| 50 | unsigned int pos, unsigned int count, |
| 51 | const void *kbuf, const void __user *ubuf) |
| 52 | { |
| 53 | struct pt_regs *regs; |
| 54 | |
| 55 | regs = task_pt_regs(target); |
| 56 | return user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1); |
| 57 | } |
| 58 | |
| 59 | #ifdef CONFIG_FPU |
| 60 | static int riscv_fpr_get(struct task_struct *target, |
| 61 | const struct user_regset *regset, |
| 62 | struct membuf to) |
| 63 | { |
| 64 | struct __riscv_d_ext_state *fstate = &target->thread.fstate; |
| 65 | |
| 66 | if (target == current) |
| 67 | fstate_save(current, task_pt_regs(current)); |
| 68 | |
| 69 | membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr)); |
| 70 | membuf_store(&to, fstate->fcsr); |
| 71 | return membuf_zero(&to, 4); // explicitly pad |
| 72 | } |
| 73 | |
| 74 | static int riscv_fpr_set(struct task_struct *target, |
| 75 | const struct user_regset *regset, |
| 76 | unsigned int pos, unsigned int count, |
| 77 | const void *kbuf, const void __user *ubuf) |
| 78 | { |
| 79 | int ret; |
| 80 | struct __riscv_d_ext_state *fstate = &target->thread.fstate; |
| 81 | |
| 82 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 83 | offsetof(struct __riscv_d_ext_state, fcsr)); |
| 84 | if (!ret) { |
| 85 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 86 | offsetof(struct __riscv_d_ext_state, fcsr) + |
| 87 | sizeof(fstate->fcsr)); |
| 88 | } |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | #ifdef CONFIG_RISCV_ISA_V |
| 95 | static int riscv_vr_get(struct task_struct *target, |
| 96 | const struct user_regset *regset, |
| 97 | struct membuf to) |
| 98 | { |
| 99 | struct __riscv_v_ext_state *vstate = &target->thread.vstate; |
| 100 | struct __riscv_v_regset_state ptrace_vstate; |
| 101 | |
| 102 | if (!(has_vector() || has_xtheadvector())) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | if (!riscv_v_vstate_query(task_pt_regs(target))) |
| 106 | return -ENODATA; |
| 107 | |
| 108 | /* |
| 109 | * Ensure the vector registers have been saved to the memory before |
| 110 | * copying them to membuf. |
| 111 | */ |
| 112 | if (target == current) { |
| 113 | get_cpu_vector_context(); |
| 114 | riscv_v_vstate_save(¤t->thread.vstate, task_pt_regs(current)); |
| 115 | put_cpu_vector_context(); |
| 116 | } |
| 117 | |
| 118 | ptrace_vstate.vstart = vstate->vstart; |
| 119 | ptrace_vstate.vl = vstate->vl; |
| 120 | ptrace_vstate.vtype = vstate->vtype; |
| 121 | ptrace_vstate.vcsr = vstate->vcsr; |
| 122 | ptrace_vstate.vlenb = vstate->vlenb; |
| 123 | |
| 124 | /* Copy vector header from vstate. */ |
| 125 | membuf_write(&to, &ptrace_vstate, sizeof(struct __riscv_v_regset_state)); |
| 126 | |
| 127 | /* Copy all the vector registers from vstate. */ |
| 128 | return membuf_write(&to, vstate->datap, riscv_v_vsize); |
| 129 | } |
| 130 | |
| 131 | static int invalid_ptrace_v_csr(struct __riscv_v_ext_state *vstate, |
| 132 | struct __riscv_v_regset_state *ptrace) |
| 133 | { |
| 134 | unsigned long vsew, vlmul, vfrac, vl; |
| 135 | unsigned long elen, vlen; |
| 136 | unsigned long sew, lmul; |
| 137 | unsigned long reserved; |
| 138 | |
| 139 | vlen = vstate->vlenb * 8; |
| 140 | if (vstate->vlenb != ptrace->vlenb) |
| 141 | return 1; |
| 142 | |
| 143 | /* do not allow to set vcsr/vxrm/vxsat reserved bits */ |
| 144 | reserved = ~(CSR_VXSAT_MASK | (CSR_VXRM_MASK << CSR_VXRM_SHIFT)); |
| 145 | if (ptrace->vcsr & reserved) |
| 146 | return 1; |
| 147 | |
| 148 | if (has_vector()) { |
| 149 | /* do not allow to set vtype reserved bits and vill bit */ |
| 150 | reserved = ~(VTYPE_VSEW | VTYPE_VLMUL | VTYPE_VMA | VTYPE_VTA); |
| 151 | if (ptrace->vtype & reserved) |
| 152 | return 1; |
| 153 | |
| 154 | elen = riscv_has_extension_unlikely(RISCV_ISA_EXT_ZVE64X) ? 64 : 32; |
| 155 | vsew = (ptrace->vtype & VTYPE_VSEW) >> VTYPE_VSEW_SHIFT; |
| 156 | sew = 8 << vsew; |
| 157 | |
| 158 | if (sew > elen) |
| 159 | return 1; |
| 160 | |
| 161 | vfrac = (ptrace->vtype & VTYPE_VLMUL_FRAC); |
| 162 | vlmul = (ptrace->vtype & VTYPE_VLMUL); |
| 163 | |
| 164 | /* RVV 1.0 spec 3.4.2: VLMUL(0x4) reserved */ |
| 165 | if (vlmul == 4) |
| 166 | return 1; |
| 167 | |
| 168 | /* RVV 1.0 spec 3.4.2: (LMUL < SEW_min / ELEN) reserved */ |
| 169 | if (vlmul == 5 && elen == 32) |
| 170 | return 1; |
| 171 | |
| 172 | /* for zero vl verify that at least one element is possible */ |
| 173 | vl = ptrace->vl ? ptrace->vl : 1; |
| 174 | |
| 175 | if (vfrac) { |
| 176 | /* integer 1/LMUL: VL =< VLMAX = VLEN / SEW / LMUL */ |
| 177 | lmul = 2 << (3 - (vlmul - vfrac)); |
| 178 | if (vlen < vl * sew * lmul) |
| 179 | return 1; |
| 180 | } else { |
| 181 | /* integer LMUL: VL =< VLMAX = LMUL * VLEN / SEW */ |
| 182 | lmul = 1 << vlmul; |
| 183 | if (vl * sew > lmul * vlen) |
| 184 | return 1; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (has_xtheadvector()) { |
| 189 | /* do not allow to set vtype reserved bits and vill bit */ |
| 190 | reserved = ~(VTYPE_VSEW_THEAD | VTYPE_VLMUL_THEAD | VTYPE_VEDIV_THEAD); |
| 191 | if (ptrace->vtype & reserved) |
| 192 | return 1; |
| 193 | |
| 194 | /* |
| 195 | * THead ISA Extension spec chapter 16: |
| 196 | * divided element extension ('Zvediv') is not part of XTheadVector |
| 197 | */ |
| 198 | if (ptrace->vtype & VTYPE_VEDIV_THEAD) |
| 199 | return 1; |
| 200 | |
| 201 | vsew = (ptrace->vtype & VTYPE_VSEW_THEAD) >> VTYPE_VSEW_THEAD_SHIFT; |
| 202 | sew = 8 << vsew; |
| 203 | |
| 204 | vlmul = (ptrace->vtype & VTYPE_VLMUL_THEAD); |
| 205 | lmul = 1 << vlmul; |
| 206 | |
| 207 | /* for zero vl verify that at least one element is possible */ |
| 208 | vl = ptrace->vl ? ptrace->vl : 1; |
| 209 | |
| 210 | if (vl * sew > lmul * vlen) |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int riscv_vr_set(struct task_struct *target, |
| 218 | const struct user_regset *regset, |
| 219 | unsigned int pos, unsigned int count, |
| 220 | const void *kbuf, const void __user *ubuf) |
| 221 | { |
| 222 | int ret; |
| 223 | struct __riscv_v_ext_state *vstate = &target->thread.vstate; |
| 224 | struct __riscv_v_regset_state ptrace_vstate; |
| 225 | |
| 226 | if (!(has_vector() || has_xtheadvector())) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | if (!riscv_v_vstate_query(task_pt_regs(target))) |
| 230 | return -ENODATA; |
| 231 | |
| 232 | /* Copy rest of the vstate except datap */ |
| 233 | memset(&ptrace_vstate, 0, sizeof(ptrace_vstate)); |
| 234 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ptrace_vstate, 0, |
| 235 | sizeof(struct __riscv_v_regset_state)); |
| 236 | if (unlikely(ret)) |
| 237 | return ret; |
| 238 | |
| 239 | if (invalid_ptrace_v_csr(vstate, &ptrace_vstate)) |
| 240 | return -EINVAL; |
| 241 | |
| 242 | vstate->vstart = ptrace_vstate.vstart; |
| 243 | vstate->vl = ptrace_vstate.vl; |
| 244 | vstate->vtype = ptrace_vstate.vtype; |
| 245 | vstate->vcsr = ptrace_vstate.vcsr; |
| 246 | |
| 247 | /* Copy all the vector registers. */ |
| 248 | pos = 0; |
| 249 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate->datap, |
| 250 | 0, riscv_v_vsize); |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static int riscv_vr_active(struct task_struct *target, const struct user_regset *regset) |
| 255 | { |
| 256 | if (!(has_vector() || has_xtheadvector())) |
| 257 | return -ENODEV; |
| 258 | |
| 259 | if (!riscv_v_vstate_query(task_pt_regs(target))) |
| 260 | return 0; |
| 261 | |
| 262 | return regset->n; |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | #ifdef CONFIG_RISCV_ISA_SUPM |
| 267 | static int tagged_addr_ctrl_get(struct task_struct *target, |
| 268 | const struct user_regset *regset, |
| 269 | struct membuf to) |
| 270 | { |
| 271 | long ctrl = get_tagged_addr_ctrl(target); |
| 272 | |
| 273 | if (IS_ERR_VALUE(ctrl)) |
| 274 | return ctrl; |
| 275 | |
| 276 | return membuf_write(&to, &ctrl, sizeof(ctrl)); |
| 277 | } |
| 278 | |
| 279 | static int tagged_addr_ctrl_set(struct task_struct *target, |
| 280 | const struct user_regset *regset, |
| 281 | unsigned int pos, unsigned int count, |
| 282 | const void *kbuf, const void __user *ubuf) |
| 283 | { |
| 284 | int ret; |
| 285 | long ctrl; |
| 286 | |
| 287 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl, 0, -1); |
| 288 | if (ret) |
| 289 | return ret; |
| 290 | |
| 291 | return set_tagged_addr_ctrl(target, ctrl); |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | #ifdef CONFIG_RISCV_USER_CFI |
| 296 | static int riscv_cfi_get(struct task_struct *target, |
| 297 | const struct user_regset *regset, |
| 298 | struct membuf to) |
| 299 | { |
| 300 | struct user_cfi_state user_cfi; |
| 301 | struct pt_regs *regs; |
| 302 | |
| 303 | if (!is_user_lpad_enabled() && !is_user_shstk_enabled()) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | memset(&user_cfi, 0, sizeof(user_cfi)); |
| 307 | regs = task_pt_regs(target); |
| 308 | |
| 309 | if (is_indir_lp_enabled(target)) { |
| 310 | user_cfi.cfi_status.cfi_state |= PTRACE_CFI_BRANCH_LANDING_PAD_EN_STATE; |
| 311 | user_cfi.cfi_status.cfi_state |= is_indir_lp_locked(target) ? |
| 312 | PTRACE_CFI_BRANCH_LANDING_PAD_LOCK_STATE : 0; |
| 313 | user_cfi.cfi_status.cfi_state |= (regs->status & SR_ELP) ? |
| 314 | PTRACE_CFI_BRANCH_EXPECTED_LANDING_PAD_STATE : 0; |
| 315 | } |
| 316 | |
| 317 | if (is_shstk_enabled(target)) { |
| 318 | user_cfi.cfi_status.cfi_state |= (PTRACE_CFI_SHADOW_STACK_EN_STATE | |
| 319 | PTRACE_CFI_SHADOW_STACK_PTR_STATE); |
| 320 | user_cfi.cfi_status.cfi_state |= is_shstk_locked(target) ? |
| 321 | PTRACE_CFI_SHADOW_STACK_LOCK_STATE : 0; |
| 322 | user_cfi.shstk_ptr = get_active_shstk(target); |
| 323 | } |
| 324 | |
| 325 | return membuf_write(&to, &user_cfi, sizeof(user_cfi)); |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * Does it make sense to allow enable / disable of cfi via ptrace? |
| 330 | * We don't allow enable / disable / locking control via ptrace for now. |
| 331 | * Setting the shadow stack pointer is allowed. GDB might use it to unwind or |
| 332 | * some other fixup. Similarly gdb might want to suppress elp and may want |
| 333 | * to reset elp state. |
| 334 | */ |
| 335 | static int riscv_cfi_set(struct task_struct *target, |
| 336 | const struct user_regset *regset, |
| 337 | unsigned int pos, unsigned int count, |
| 338 | const void *kbuf, const void __user *ubuf) |
| 339 | { |
| 340 | int ret; |
| 341 | struct user_cfi_state user_cfi; |
| 342 | struct pt_regs *regs; |
| 343 | |
| 344 | if (!is_user_lpad_enabled() && !is_user_shstk_enabled()) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | regs = task_pt_regs(target); |
| 348 | |
| 349 | memset(&user_cfi, 0, sizeof(user_cfi)); |
| 350 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_cfi, 0, -1); |
| 351 | if (ret) |
| 352 | return ret; |
| 353 | |
| 354 | /* |
| 355 | * Not allowing enabling or locking shadow stack or landing pad |
| 356 | * There is no disabling of shadow stack or landing pad via ptrace |
| 357 | * rsvd field should be set to zero so that if those fields are needed in future |
| 358 | */ |
| 359 | if ((user_cfi.cfi_status.cfi_state & |
| 360 | (PTRACE_CFI_BRANCH_LANDING_PAD_EN_STATE | PTRACE_CFI_BRANCH_LANDING_PAD_LOCK_STATE | |
| 361 | PTRACE_CFI_SHADOW_STACK_EN_STATE | PTRACE_CFI_SHADOW_STACK_LOCK_STATE)) || |
| 362 | (user_cfi.cfi_status.cfi_state & PTRACE_CFI_STATE_INVALID_MASK)) |
| 363 | return -EINVAL; |
| 364 | |
| 365 | /* If lpad is enabled on target and ptrace requests to set / clear elp, do that */ |
| 366 | if (is_indir_lp_enabled(target)) { |
| 367 | if (user_cfi.cfi_status.cfi_state & |
| 368 | PTRACE_CFI_BRANCH_EXPECTED_LANDING_PAD_STATE) /* set elp state */ |
| 369 | regs->status |= SR_ELP; |
| 370 | else |
| 371 | regs->status &= ~SR_ELP; /* clear elp state */ |
| 372 | } |
| 373 | |
| 374 | /* If shadow stack enabled on target, set new shadow stack pointer */ |
| 375 | if (is_shstk_enabled(target) && |
| 376 | (user_cfi.cfi_status.cfi_state & PTRACE_CFI_SHADOW_STACK_PTR_STATE)) |
| 377 | set_active_shstk(target, user_cfi.shstk_ptr); |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | #endif |
| 382 | |
| 383 | static struct user_regset riscv_user_regset[] __ro_after_init = { |
| 384 | [REGSET_X] = { |
| 385 | USER_REGSET_NOTE_TYPE(PRSTATUS), |
| 386 | .n = ELF_NGREG, |
| 387 | .size = sizeof(elf_greg_t), |
| 388 | .align = sizeof(elf_greg_t), |
| 389 | .regset_get = riscv_gpr_get, |
| 390 | .set = riscv_gpr_set, |
| 391 | }, |
| 392 | #ifdef CONFIG_FPU |
| 393 | [REGSET_F] = { |
| 394 | USER_REGSET_NOTE_TYPE(PRFPREG), |
| 395 | .n = ELF_NFPREG, |
| 396 | .size = sizeof(elf_fpreg_t), |
| 397 | .align = sizeof(elf_fpreg_t), |
| 398 | .regset_get = riscv_fpr_get, |
| 399 | .set = riscv_fpr_set, |
| 400 | }, |
| 401 | #endif |
| 402 | #ifdef CONFIG_RISCV_ISA_V |
| 403 | [REGSET_V] = { |
| 404 | USER_REGSET_NOTE_TYPE(RISCV_VECTOR), |
| 405 | .align = 16, |
| 406 | .size = sizeof(__u32), |
| 407 | .regset_get = riscv_vr_get, |
| 408 | .set = riscv_vr_set, |
| 409 | .active = riscv_vr_active, |
| 410 | }, |
| 411 | #endif |
| 412 | #ifdef CONFIG_RISCV_ISA_SUPM |
| 413 | [REGSET_TAGGED_ADDR_CTRL] = { |
| 414 | USER_REGSET_NOTE_TYPE(RISCV_TAGGED_ADDR_CTRL), |
| 415 | .n = 1, |
| 416 | .size = sizeof(long), |
| 417 | .align = sizeof(long), |
| 418 | .regset_get = tagged_addr_ctrl_get, |
| 419 | .set = tagged_addr_ctrl_set, |
| 420 | }, |
| 421 | #endif |
| 422 | #ifdef CONFIG_RISCV_USER_CFI |
| 423 | [REGSET_CFI] = { |
| 424 | USER_REGSET_NOTE_TYPE(RISCV_USER_CFI), |
| 425 | .align = sizeof(__u64), |
| 426 | .n = sizeof(struct user_cfi_state) / sizeof(__u64), |
| 427 | .size = sizeof(__u64), |
| 428 | .regset_get = riscv_cfi_get, |
| 429 | .set = riscv_cfi_set, |
| 430 | }, |
| 431 | #endif |
| 432 | }; |
| 433 | |
| 434 | static const struct user_regset_view riscv_user_native_view = { |
| 435 | .name = "riscv", |
| 436 | .e_machine = EM_RISCV, |
| 437 | .regsets = riscv_user_regset, |
| 438 | .n = ARRAY_SIZE(riscv_user_regset), |
| 439 | }; |
| 440 | |
| 441 | #ifdef CONFIG_RISCV_ISA_V |
| 442 | void __init update_regset_vector_info(unsigned long size) |
| 443 | { |
| 444 | riscv_user_regset[REGSET_V].n = (size + sizeof(struct __riscv_v_regset_state)) / |
| 445 | sizeof(__u32); |
| 446 | } |
| 447 | #endif |
| 448 | |
| 449 | struct pt_regs_offset { |
| 450 | const char *name; |
| 451 | int offset; |
| 452 | }; |
| 453 | |
| 454 | #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)} |
| 455 | #define REG_OFFSET_END {.name = NULL, .offset = 0} |
| 456 | |
| 457 | static const struct pt_regs_offset regoffset_table[] = { |
| 458 | REG_OFFSET_NAME(epc), |
| 459 | REG_OFFSET_NAME(ra), |
| 460 | REG_OFFSET_NAME(sp), |
| 461 | REG_OFFSET_NAME(gp), |
| 462 | REG_OFFSET_NAME(tp), |
| 463 | REG_OFFSET_NAME(t0), |
| 464 | REG_OFFSET_NAME(t1), |
| 465 | REG_OFFSET_NAME(t2), |
| 466 | REG_OFFSET_NAME(s0), |
| 467 | REG_OFFSET_NAME(s1), |
| 468 | REG_OFFSET_NAME(a0), |
| 469 | REG_OFFSET_NAME(a1), |
| 470 | REG_OFFSET_NAME(a2), |
| 471 | REG_OFFSET_NAME(a3), |
| 472 | REG_OFFSET_NAME(a4), |
| 473 | REG_OFFSET_NAME(a5), |
| 474 | REG_OFFSET_NAME(a6), |
| 475 | REG_OFFSET_NAME(a7), |
| 476 | REG_OFFSET_NAME(s2), |
| 477 | REG_OFFSET_NAME(s3), |
| 478 | REG_OFFSET_NAME(s4), |
| 479 | REG_OFFSET_NAME(s5), |
| 480 | REG_OFFSET_NAME(s6), |
| 481 | REG_OFFSET_NAME(s7), |
| 482 | REG_OFFSET_NAME(s8), |
| 483 | REG_OFFSET_NAME(s9), |
| 484 | REG_OFFSET_NAME(s10), |
| 485 | REG_OFFSET_NAME(s11), |
| 486 | REG_OFFSET_NAME(t3), |
| 487 | REG_OFFSET_NAME(t4), |
| 488 | REG_OFFSET_NAME(t5), |
| 489 | REG_OFFSET_NAME(t6), |
| 490 | REG_OFFSET_NAME(status), |
| 491 | REG_OFFSET_NAME(badaddr), |
| 492 | REG_OFFSET_NAME(cause), |
| 493 | REG_OFFSET_NAME(orig_a0), |
| 494 | REG_OFFSET_END, |
| 495 | }; |
| 496 | |
| 497 | /** |
| 498 | * regs_query_register_offset() - query register offset from its name |
| 499 | * @name: the name of a register |
| 500 | * |
| 501 | * regs_query_register_offset() returns the offset of a register in struct |
| 502 | * pt_regs from its name. If the name is invalid, this returns -EINVAL; |
| 503 | */ |
| 504 | int regs_query_register_offset(const char *name) |
| 505 | { |
| 506 | const struct pt_regs_offset *roff; |
| 507 | |
| 508 | for (roff = regoffset_table; roff->name != NULL; roff++) |
| 509 | if (!strcmp(roff->name, name)) |
| 510 | return roff->offset; |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * regs_within_kernel_stack() - check the address in the stack |
| 516 | * @regs: pt_regs which contains kernel stack pointer. |
| 517 | * @addr: address which is checked. |
| 518 | * |
| 519 | * regs_within_kernel_stack() checks @addr is within the kernel stack page(s). |
| 520 | * If @addr is within the kernel stack, it returns true. If not, returns false. |
| 521 | */ |
| 522 | static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr) |
| 523 | { |
| 524 | return (addr & ~(THREAD_SIZE - 1)) == |
| 525 | (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * regs_get_kernel_stack_nth() - get Nth entry of the stack |
| 530 | * @regs: pt_regs which contains kernel stack pointer. |
| 531 | * @n: stack entry number. |
| 532 | * |
| 533 | * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which |
| 534 | * is specified by @regs. If the @n th entry is NOT in the kernel stack, |
| 535 | * this returns 0. |
| 536 | */ |
| 537 | unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n) |
| 538 | { |
| 539 | unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs); |
| 540 | |
| 541 | addr += n; |
| 542 | if (regs_within_kernel_stack(regs, (unsigned long)addr)) |
| 543 | return *addr; |
| 544 | else |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | void ptrace_disable(struct task_struct *child) |
| 549 | { |
| 550 | } |
| 551 | |
| 552 | long arch_ptrace(struct task_struct *child, long request, |
| 553 | unsigned long addr, unsigned long data) |
| 554 | { |
| 555 | long ret = -EIO; |
| 556 | |
| 557 | switch (request) { |
| 558 | default: |
| 559 | ret = ptrace_request(child, request, addr, data); |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | #ifdef CONFIG_COMPAT |
| 567 | static int compat_riscv_gpr_get(struct task_struct *target, |
| 568 | const struct user_regset *regset, |
| 569 | struct membuf to) |
| 570 | { |
| 571 | struct compat_user_regs_struct cregs; |
| 572 | |
| 573 | regs_to_cregs(&cregs, task_pt_regs(target)); |
| 574 | |
| 575 | return membuf_write(&to, &cregs, |
| 576 | sizeof(struct compat_user_regs_struct)); |
| 577 | } |
| 578 | |
| 579 | static int compat_riscv_gpr_set(struct task_struct *target, |
| 580 | const struct user_regset *regset, |
| 581 | unsigned int pos, unsigned int count, |
| 582 | const void *kbuf, const void __user *ubuf) |
| 583 | { |
| 584 | int ret; |
| 585 | struct compat_user_regs_struct cregs; |
| 586 | |
| 587 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1); |
| 588 | if (!ret) |
| 589 | cregs_to_regs(&cregs, task_pt_regs(target)); |
| 590 | |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | static const struct user_regset compat_riscv_user_regset[] = { |
| 595 | [REGSET_X] = { |
| 596 | USER_REGSET_NOTE_TYPE(PRSTATUS), |
| 597 | .n = ELF_NGREG, |
| 598 | .size = sizeof(compat_elf_greg_t), |
| 599 | .align = sizeof(compat_elf_greg_t), |
| 600 | .regset_get = compat_riscv_gpr_get, |
| 601 | .set = compat_riscv_gpr_set, |
| 602 | }, |
| 603 | #ifdef CONFIG_FPU |
| 604 | [REGSET_F] = { |
| 605 | USER_REGSET_NOTE_TYPE(PRFPREG), |
| 606 | .n = ELF_NFPREG, |
| 607 | .size = sizeof(elf_fpreg_t), |
| 608 | .align = sizeof(elf_fpreg_t), |
| 609 | .regset_get = riscv_fpr_get, |
| 610 | .set = riscv_fpr_set, |
| 611 | }, |
| 612 | #endif |
| 613 | }; |
| 614 | |
| 615 | static const struct user_regset_view compat_riscv_user_native_view = { |
| 616 | .name = "riscv", |
| 617 | .e_machine = EM_RISCV, |
| 618 | .regsets = compat_riscv_user_regset, |
| 619 | .n = ARRAY_SIZE(compat_riscv_user_regset), |
| 620 | }; |
| 621 | |
| 622 | long compat_arch_ptrace(struct task_struct *child, compat_long_t request, |
| 623 | compat_ulong_t caddr, compat_ulong_t cdata) |
| 624 | { |
| 625 | long ret = -EIO; |
| 626 | |
| 627 | switch (request) { |
| 628 | default: |
| 629 | ret = compat_ptrace_request(child, request, caddr, cdata); |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | return ret; |
| 634 | } |
| 635 | #else |
| 636 | static const struct user_regset_view compat_riscv_user_native_view = {}; |
| 637 | #endif /* CONFIG_COMPAT */ |
| 638 | |
| 639 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) |
| 640 | { |
| 641 | if (is_compat_thread(&task->thread_info)) |
| 642 | return &compat_riscv_user_native_view; |
| 643 | else |
| 644 | return &riscv_user_native_view; |
| 645 | } |