^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * `ptrace' system call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007-2009 PetaLogix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2004-2007 John Williams <john.williams@petalogix.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * derived from arch/v850/kernel/ptrace.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2002,03 NEC Electronics Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (C) 2002,03 Miles Bader <miles@gnu.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Derived from arch/mips/kernel/ptrace.c:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Copyright (C) 1992 Ross Biro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Copyright (C) Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Copyright (C) 1996 David S. Miller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Copyright (C) 1999 MIPS Technologies, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * This file is subject to the terms and conditions of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Public License. See the file COPYING in the main directory of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * archive for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/audit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/seccomp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/tracehook.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <asm/asm-offsets.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <asm/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* Returns the address where the register at REG_OFFS in P is stashed away. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static microblaze_reg_t *reg_save_addr(unsigned reg_offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct task_struct *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct pt_regs *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Three basic cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * (1) A register normally saved before calling the scheduler, is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * available in the kernel entry pt_regs structure at the top
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * of the kernel stack. The kernel trap/irq exit path takes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * care to save/restore almost all registers for ptrace'd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * (2) A call-clobbered register, where the process P entered the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * kernel via [syscall] trap, is not stored anywhere; that's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * OK, because such registers are not expected to be preserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * when the trap returns anyway (so we don't actually bother to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * test for this case).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * (3) A few registers not used at all by the kernel, and so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * normally never saved except by context-switches, are in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * context switch state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* Register saved during kernel entry (or not available). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) regs = task_pt_regs(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return (microblaze_reg_t *)((char *)regs + reg_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) long arch_ptrace(struct task_struct *child, long request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned long addr, unsigned long data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned long val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) switch (request) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* Read/write the word at location ADDR in the registers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) case PTRACE_PEEKUSR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) case PTRACE_POKEUSR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) pr_debug("PEEKUSR/POKEUSR : 0x%08lx\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) rval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * Special requests that don't actually correspond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * to offsets in struct pt_regs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (addr == PT_TEXT_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) val = child->mm->start_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) } else if (addr == PT_DATA_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) val = child->mm->start_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) } else if (addr == PT_TEXT_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) val = child->mm->end_code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) - child->mm->start_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else if (addr < PT_SIZE && (addr & 0x3) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) microblaze_reg_t *reg_addr = reg_save_addr(addr, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (request == PTRACE_PEEKUSR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) val = *reg_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #if 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *reg_addr = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* MS potential problem on WB system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * Be aware that reg_addr is virtual address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * virt_to_phys conversion is necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * This could be sensible solution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) u32 paddr = virt_to_phys((u32)reg_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) invalidate_icache_range(paddr, paddr + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) *reg_addr = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) flush_dcache_range(paddr, paddr + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) rval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (rval == 0 && request == PTRACE_PEEKUSR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) rval = put_user(val, (unsigned long __user *)data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) rval = ptrace_request(child, request, addr, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) asmlinkage unsigned long do_syscall_trace_enter(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) unsigned long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) secure_computing_strict(regs->r12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (test_thread_flag(TIF_SYSCALL_TRACE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) tracehook_report_syscall_entry(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * Tracing decided this syscall should not happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * We'll return a bogus call number to get an ENOSYS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * error, but leave the original number in regs->regs[0].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ret = -1L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) audit_syscall_entry(regs->r12, regs->r5, regs->r6, regs->r7, regs->r8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return ret ?: regs->r12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) audit_syscall_exit(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) step = test_thread_flag(TIF_SINGLESTEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (step || test_thread_flag(TIF_SYSCALL_TRACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) tracehook_report_syscall_exit(regs, step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) void ptrace_disable(struct task_struct *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }