^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 2014 Altera Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This file is subject to the terms and conditions of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Public License. See the file COPYING in the main directory of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * archive for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/regset.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/tracehook.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int genregs_get(struct task_struct *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) const struct user_regset *regset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct membuf to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) const struct pt_regs *regs = task_pt_regs(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) const struct switch_stack *sw = (struct switch_stack *)regs - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) membuf_zero(&to, 4); // R0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) membuf_write(&to, ®s->r1, 7 * 4); // R1..R7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) membuf_write(&to, ®s->r8, 8 * 4); // R8..R15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) membuf_write(&to, sw, 8 * 4); // R16..R23
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) membuf_zero(&to, 2 * 4); /* et and bt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) membuf_store(&to, regs->gp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) membuf_store(&to, regs->sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) membuf_store(&to, regs->fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) membuf_store(&to, regs->ea);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) membuf_zero(&to, 4); // PTR_BA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) membuf_store(&to, regs->ra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) membuf_store(&to, regs->ea); /* use ea for PC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return membuf_zero(&to, (NUM_PTRACE_REG - PTR_PC) * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Set the thread state from a regset passed in via ptrace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int genregs_set(struct task_struct *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) const struct user_regset *regset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int pos, unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) const void *kbuf, const void __user *ubuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct pt_regs *regs = task_pt_regs(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) const struct switch_stack *sw = (struct switch_stack *)regs - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define REG_IGNORE_RANGE(START, END) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!ret) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) START * 4, (END * 4) + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define REG_IN_ONE(PTR, LOC) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (!ret) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) (void *)(PTR), LOC * 4, (LOC * 4) + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define REG_IN_RANGE(PTR, START, END) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (!ret) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) (void *)(PTR), START * 4, (END * 4) + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) REG_IGNORE_RANGE(PTR_R0, PTR_R0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) REG_IN_RANGE(®s->r1, PTR_R1, PTR_R7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) REG_IN_RANGE(®s->r8, PTR_R8, PTR_R15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) REG_IN_RANGE(sw, PTR_R16, PTR_R23);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) REG_IGNORE_RANGE(PTR_R24, PTR_R25); /* et and bt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) REG_IN_ONE(®s->gp, PTR_GP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) REG_IN_ONE(®s->sp, PTR_SP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) REG_IN_ONE(®s->fp, PTR_FP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) REG_IN_ONE(®s->ea, PTR_EA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) REG_IGNORE_RANGE(PTR_BA, PTR_BA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) REG_IN_ONE(®s->ra, PTR_RA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) REG_IN_ONE(®s->ea, PTR_PC); /* use ea for PC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) PTR_STATUS * 4, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Define the register sets available on Nios2 under Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) enum nios2_regset {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) REGSET_GENERAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static const struct user_regset nios2_regsets[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) [REGSET_GENERAL] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .core_note_type = NT_PRSTATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .n = NUM_PTRACE_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .size = sizeof(unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .align = sizeof(unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .regset_get = genregs_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .set = genregs_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static const struct user_regset_view nios2_user_view = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .name = "nios2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .e_machine = ELF_ARCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .ei_osabi = ELF_OSABI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .regsets = nios2_regsets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .n = ARRAY_SIZE(nios2_regsets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) const struct user_regset_view *task_user_regset_view(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return &nios2_user_view;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) void ptrace_disable(struct task_struct *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned long data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return ptrace_request(child, request, addr, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) asmlinkage int do_syscall_trace_enter(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (test_thread_flag(TIF_SYSCALL_TRACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ret = tracehook_report_syscall_entry(task_pt_regs(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) asmlinkage void do_syscall_trace_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (test_thread_flag(TIF_SYSCALL_TRACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) tracehook_report_syscall_exit(task_pt_regs(current), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }