1 // SPDX-License-Identifier: GPL-2.0
3 /* By Ross Biro 1/23/92 */
4 /* edited by Linus Torvalds */
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/sched/task_stack.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/security.h>
17 #include <linux/signal.h>
18 #include <linux/audit.h>
20 #include <linux/uaccess.h>
34 #define DBG(fac,args) {if ((fac) & DEBUG) printk args;}
39 #define BREAKINST 0x00000080 /* call_pal bpt */
42 * does not yet catch signals sent when the child dies.
43 * in exit.c or in signal.c.
47 * Processes always block with the following stack-layout:
49 * +================================+ <---- task + 2*PAGE_SIZE
50 * | PALcode saved frame (ps, pc, | ^
51 * | gp, a0, a1, a2) | |
52 * +================================+ | struct pt_regs
54 * | frame generated by SAVE_ALL | |
56 * +================================+
58 * | frame saved by do_switch_stack | | struct switch_stack
60 * +================================+
64 * The following table maps a register index into the stack offset at
65 * which the register is saved. Register indices are 0-31 for integer
66 * regs, 32-63 for fp regs, and 64 for the pc. Notice that sp and
67 * zero have no stack-slot and need to be treated specially (see
68 * get_reg/put_reg below).
71 REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
75 (PAGE_SIZE*2 - sizeof(struct pt_regs) + offsetof(struct pt_regs, reg))
78 (PAGE_SIZE*2 - sizeof(struct pt_regs) - sizeof(struct switch_stack) \
79 + offsetof(struct switch_stack, reg))
81 #define FP_REG(reg) (offsetof(struct thread_info, reg))
83 static int regoff[] = {
84 PT_REG( r0), PT_REG( r1), PT_REG( r2), PT_REG( r3),
85 PT_REG( r4), PT_REG( r5), PT_REG( r6), PT_REG( r7),
86 PT_REG( r8), SW_REG( r9), SW_REG( r10), SW_REG( r11),
87 SW_REG( r12), SW_REG( r13), SW_REG( r14), SW_REG( r15),
88 PT_REG( r16), PT_REG( r17), PT_REG( r18), PT_REG( r19),
89 PT_REG( r20), PT_REG( r21), PT_REG( r22), PT_REG( r23),
90 PT_REG( r24), PT_REG( r25), PT_REG( r26), PT_REG( r27),
91 PT_REG( r28), PT_REG( gp), -1, -1,
92 FP_REG(fp[ 0]), FP_REG(fp[ 1]), FP_REG(fp[ 2]), FP_REG(fp[ 3]),
93 FP_REG(fp[ 4]), FP_REG(fp[ 5]), FP_REG(fp[ 6]), FP_REG(fp[ 7]),
94 FP_REG(fp[ 8]), FP_REG(fp[ 9]), FP_REG(fp[10]), FP_REG(fp[11]),
95 FP_REG(fp[12]), FP_REG(fp[13]), FP_REG(fp[14]), FP_REG(fp[15]),
96 FP_REG(fp[16]), FP_REG(fp[17]), FP_REG(fp[18]), FP_REG(fp[19]),
97 FP_REG(fp[20]), FP_REG(fp[21]), FP_REG(fp[22]), FP_REG(fp[23]),
98 FP_REG(fp[24]), FP_REG(fp[25]), FP_REG(fp[26]), FP_REG(fp[27]),
99 FP_REG(fp[28]), FP_REG(fp[29]), FP_REG(fp[30]), FP_REG(fp[31]),
103 static unsigned long zero;
106 * Get address of register REGNO in task TASK.
108 static unsigned long *
109 get_reg_addr(struct task_struct * task, unsigned long regno)
114 addr = &task_thread_info(task)->pcb.usp;
115 } else if (regno == 65) {
116 addr = &task_thread_info(task)->pcb.unique;
117 } else if (regno == 31 || regno > 65) {
121 addr = task_stack_page(task) + regoff[regno];
127 * Get contents of register REGNO in task TASK.
130 get_reg(struct task_struct * task, unsigned long regno)
132 /* Special hack for fpcr -- combine hardware and software bits. */
134 unsigned long fpcr = *get_reg_addr(task, regno);
136 = task_thread_info(task)->ieee_state & IEEE_SW_MASK;
137 swcr = swcr_update_status(swcr, fpcr);
140 return *get_reg_addr(task, regno);
144 * Write contents of register REGNO in task TASK.
147 put_reg(struct task_struct *task, unsigned long regno, unsigned long data)
150 task_thread_info(task)->ieee_state
151 = ((task_thread_info(task)->ieee_state & ~IEEE_SW_MASK)
152 | (data & IEEE_SW_MASK));
153 data = (data & FPCR_DYN_MASK) | ieee_swcr_to_fpcr(data);
155 *get_reg_addr(task, regno) = data;
160 read_int(struct task_struct *task, unsigned long addr, int * data)
162 int copied = access_process_vm(task, addr, data, sizeof(int),
164 return (copied == sizeof(int)) ? 0 : -EIO;
168 write_int(struct task_struct *task, unsigned long addr, int data)
170 int copied = access_process_vm(task, addr, &data, sizeof(int),
171 FOLL_FORCE | FOLL_WRITE);
172 return (copied == sizeof(int)) ? 0 : -EIO;
179 ptrace_set_bpt(struct task_struct * child)
181 int displ, i, res, reg_b, nsaved = 0;
182 unsigned int insn, op_code;
185 pc = get_reg(child, REG_PC);
186 res = read_int(child, pc, (int *) &insn);
190 op_code = insn >> 26;
191 if (op_code >= 0x30) {
193 * It's a branch: instead of trying to figure out
194 * whether the branch will be taken or not, we'll put
195 * a breakpoint at either location. This is simpler,
196 * more reliable, and probably not a whole lot slower
197 * than the alternative approach of emulating the
198 * branch (emulation can be tricky for fp branches).
200 displ = ((s32)(insn << 11)) >> 9;
201 task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
202 if (displ) /* guard against unoptimized code */
203 task_thread_info(child)->bpt_addr[nsaved++]
205 DBG(DBG_BPT, ("execing branch\n"));
206 } else if (op_code == 0x1a) {
207 reg_b = (insn >> 16) & 0x1f;
208 task_thread_info(child)->bpt_addr[nsaved++] = get_reg(child, reg_b);
209 DBG(DBG_BPT, ("execing jump\n"));
211 task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
212 DBG(DBG_BPT, ("execing normal insn\n"));
215 /* install breakpoints: */
216 for (i = 0; i < nsaved; ++i) {
217 res = read_int(child, task_thread_info(child)->bpt_addr[i],
221 task_thread_info(child)->bpt_insn[i] = insn;
222 DBG(DBG_BPT, (" -> next_pc=%lx\n",
223 task_thread_info(child)->bpt_addr[i]));
224 res = write_int(child, task_thread_info(child)->bpt_addr[i],
229 task_thread_info(child)->bpt_nsaved = nsaved;
234 * Ensure no single-step breakpoint is pending. Returns non-zero
235 * value if child was being single-stepped.
238 ptrace_cancel_bpt(struct task_struct * child)
240 int i, nsaved = task_thread_info(child)->bpt_nsaved;
242 task_thread_info(child)->bpt_nsaved = 0;
245 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
249 for (i = 0; i < nsaved; ++i) {
250 write_int(child, task_thread_info(child)->bpt_addr[i],
251 task_thread_info(child)->bpt_insn[i]);
253 return (nsaved != 0);
256 void user_enable_single_step(struct task_struct *child)
258 /* Mark single stepping. */
259 task_thread_info(child)->bpt_nsaved = -1;
262 void user_disable_single_step(struct task_struct *child)
264 ptrace_cancel_bpt(child);
268 * Called by kernel/ptrace.c when detaching..
270 * Make sure the single step bit is not set.
272 void ptrace_disable(struct task_struct *child)
274 user_disable_single_step(child);
277 long arch_ptrace(struct task_struct *child, long request,
278 unsigned long addr, unsigned long data)
285 /* When I and D space are separate, these will need to be fixed. */
286 case PTRACE_PEEKTEXT: /* read word at location addr. */
287 case PTRACE_PEEKDATA:
288 copied = ptrace_access_vm(child, addr, &tmp, sizeof(tmp),
291 if (copied != sizeof(tmp))
294 force_successful_syscall_return();
298 /* Read register number ADDR. */
300 force_successful_syscall_return();
301 ret = get_reg(child, addr);
302 DBG(DBG_MEM, ("peek $%lu->%#lx\n", addr, ret));
305 /* When I and D space are separate, this will have to be fixed. */
306 case PTRACE_POKETEXT: /* write the word at location addr. */
307 case PTRACE_POKEDATA:
308 ret = generic_ptrace_pokedata(child, addr, data);
311 case PTRACE_POKEUSR: /* write the specified register */
312 DBG(DBG_MEM, ("poke $%lu<-%#lx\n", addr, data));
313 ret = put_reg(child, addr, data);
316 ret = ptrace_request(child, request, addr, data);
322 asmlinkage unsigned long syscall_trace_enter(void)
324 unsigned long ret = 0;
325 struct pt_regs *regs = current_pt_regs();
326 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
327 ptrace_report_syscall_entry(current_pt_regs()))
329 audit_syscall_entry(regs->r0, regs->r16, regs->r17, regs->r18, regs->r19);
330 return ret ?: current_pt_regs()->r0;
334 syscall_trace_leave(void)
336 audit_syscall_exit(current_pt_regs());
337 if (test_thread_flag(TIF_SYSCALL_TRACE))
338 ptrace_report_syscall_exit(current_pt_regs(), 0);