ptrace.c 4.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7
/*
 * Copyright (C) 2000-2003, Axis Communications AB.
 */

#include <linux/kernel.h>
#include <linux/sched.h>
8
#include <linux/sched/task_stack.h>
L
Linus Torvalds 已提交
9 10 11 12 13
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/errno.h>
#include <linux/ptrace.h>
#include <linux/user.h>
14
#include <linux/signal.h>
15
#include <linux/security.h>
L
Linus Torvalds 已提交
16

17
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/processor.h>

/* 
 * Determines which bits in DCCR the user has access to.
 * 1 = access, 0 = no access.
 */
#define DCCR_MASK 0x0000001f     /* XNZVC */

/*
 * Get contents of register REGNO in task TASK.
 */
inline long get_reg(struct task_struct *task, unsigned int regno)
{
	/* USP is a special case, it's not in the pt_regs struct but
	 * in the tasks thread struct
	 */

	if (regno == PT_USP)
		return task->thread.usp;
	else if (regno < PT_MAX)
A
Al Viro 已提交
40
		return ((unsigned long *)task_pt_regs(task))[regno];
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
	else
		return 0;
}

/*
 * Write contents of register REGNO in task TASK.
 */
inline int put_reg(struct task_struct *task, unsigned int regno,
			  unsigned long data)
{
	if (regno == PT_USP)
		task->thread.usp = data;
	else if (regno < PT_MAX)
A
Al Viro 已提交
54
		((unsigned long *)task_pt_regs(task))[regno] = data;
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
	else
		return -1;
	return 0;
}

/*
 * Called by kernel/ptrace.c when detaching.
 *
 * Make sure the single step bit is not set.
 */
void 
ptrace_disable(struct task_struct *child)
{
       /* Todo - pending singlesteps? */
69
       clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79
}

/* 
 * Note that this implementation of ptrace behaves differently from vanilla
 * ptrace.  Contrary to what the man page says, in the PTRACE_PEEKTEXT,
 * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
 * ignored.  Instead, the data variable is expected to point at a location
 * (in user space) where the result of the ptrace call is written (instead of
 * being returned).
 */
80 81
long arch_ptrace(struct task_struct *child, long request,
		 unsigned long addr, unsigned long data)
L
Linus Torvalds 已提交
82 83
{
	int ret;
84
	unsigned int regno = addr >> 2;
L
Linus Torvalds 已提交
85 86 87 88 89
	unsigned long __user *datap = (unsigned long __user *)data;

	switch (request) {
		/* Read word at location address. */ 
		case PTRACE_PEEKTEXT:
A
Alexey Dobriyan 已提交
90 91
		case PTRACE_PEEKDATA:
			ret = generic_ptrace_peekdata(child, addr, data);
L
Linus Torvalds 已提交
92 93 94 95 96 97 98
			break;

		/* Read the word at location address in the USER area. */
		case PTRACE_PEEKUSR: {
			unsigned long tmp;

			ret = -EIO;
99
			if ((addr & 3) || regno > PT_MAX)
L
Linus Torvalds 已提交
100 101
				break;

102
			tmp = get_reg(child, regno);
L
Linus Torvalds 已提交
103 104 105 106 107 108 109
			ret = put_user(tmp, datap);
			break;
		}
		
		/* Write the word at location address. */
		case PTRACE_POKETEXT:
		case PTRACE_POKEDATA:
A
Alexey Dobriyan 已提交
110
			ret = generic_ptrace_pokedata(child, addr, data);
L
Linus Torvalds 已提交
111 112 113 114 115
			break;
 
 		/* Write the word at location address in the USER area. */
		case PTRACE_POKEUSR:
			ret = -EIO;
116
			if ((addr & 3) || regno > PT_MAX)
L
Linus Torvalds 已提交
117 118
				break;

119
			if (regno == PT_DCCR) {
L
Linus Torvalds 已提交
120 121 122 123 124 125
				/* don't allow the tracing process to change stuff like
				 * interrupt enable, kernel/user bit, dma enables etc.
				 */
				data &= DCCR_MASK;
				data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
			}
126
			if (put_reg(child, regno, data))
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135
				break;
			ret = 0;
			break;

		/* Get all GP registers from the child. */
		case PTRACE_GETREGS: {
		  	int i;
			unsigned long tmp;
			
136
			ret = 0;
L
Linus Torvalds 已提交
137 138 139 140 141
			for (i = 0; i <= PT_MAX; i++) {
				tmp = get_reg(child, i);
				
				if (put_user(tmp, datap)) {
					ret = -EFAULT;
142
					break;
L
Linus Torvalds 已提交
143 144
				}
				
145
				datap++;
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155
			}

			break;
		}

		/* Set all GP registers in the child. */
		case PTRACE_SETREGS: {
			int i;
			unsigned long tmp;
			
156
			ret = 0;
L
Linus Torvalds 已提交
157 158 159
			for (i = 0; i <= PT_MAX; i++) {
				if (get_user(tmp, datap)) {
					ret = -EFAULT;
160
					break;
L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168
				}
				
				if (i == PT_DCCR) {
					tmp &= DCCR_MASK;
					tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
				}
				
				put_reg(child, i, tmp);
169
				datap++;
L
Linus Torvalds 已提交
170 171 172 173 174 175 176 177 178
			}
			
			break;
		}

		default:
			ret = ptrace_request(child, request, addr, data);
			break;
	}
179

L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	return ret;
}

void do_syscall_trace(void)
{
	if (!test_thread_flag(TIF_SYSCALL_TRACE))
		return;
	
	if (!(current->ptrace & PT_PTRACED))
		return;
	
	/* the 0x80 provides a way for the tracing parent to distinguish
	   between a syscall stop and SIGTRAP delivery */
	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
				 ? 0x80 : 0));
	
	/*
	 * This isn't the same as continuing with a signal, but it will do for
	 * normal use.
	 */
	if (current->exit_code) {
		send_sig(current->exit_code, current, 1);
		current->exit_code = 0;
	}
}