ptrace.c 8.2 KB
Newer Older
1 2 3 4 5 6
// TODO some minor issues
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
7
 * Copyright (C) 2001 - 2007  Tensilica Inc.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
 * Chris Zankel <chris@zankel.net>
 * Scott Foehner<sfoehner@yahoo.com>,
 * Kevin Chea
 * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
 */

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/ptrace.h>
#include <linux/smp.h>
#include <linux/security.h>
J
Jesper Juhl 已提交
23
#include <linux/signal.h>
24 25 26 27 28 29

#include <asm/pgtable.h>
#include <asm/page.h>
#include <asm/uaccess.h>
#include <asm/ptrace.h>
#include <asm/elf.h>
30
#include <asm/coprocessor.h>
31

32 33 34 35 36 37 38 39 40 41 42

void user_enable_single_step(struct task_struct *child)
{
	child->ptrace |= PT_SINGLESTEP;
}

void user_disable_single_step(struct task_struct *child)
{
	child->ptrace &= ~PT_SINGLESTEP;
}

43
/*
44
 * Called by kernel/ptrace.c when detaching to disable single stepping.
45 46 47 48 49 50 51
 */

void ptrace_disable(struct task_struct *child)
{
	/* Nothing to do.. */
}

52
int ptrace_getregs(struct task_struct *child, void __user *uregs)
53
{
54 55
	struct pt_regs *regs = task_pt_regs(child);
	xtensa_gregset_t __user *gregset = uregs;
56
	unsigned long wb = regs->windowbase;
57
	int i;
58 59 60 61

	if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
		return -EIO;

62 63 64 65 66 67 68
	__put_user(regs->pc, &gregset->pc);
	__put_user(regs->ps & ~(1 << PS_EXCM_BIT), &gregset->ps);
	__put_user(regs->lbeg, &gregset->lbeg);
	__put_user(regs->lend, &gregset->lend);
	__put_user(regs->lcount, &gregset->lcount);
	__put_user(regs->windowstart, &gregset->windowstart);
	__put_user(regs->windowbase, &gregset->windowbase);
C
Chris Zankel 已提交
69
	__put_user(regs->threadptr, &gregset->threadptr);
70

71 72 73
	for (i = 0; i < XCHAL_NUM_AREGS; i++)
		__put_user(regs->areg[i],
				gregset->a + ((wb * 4 + i) % XCHAL_NUM_AREGS));
74 75

	return 0;
76
}
77

78 79 80 81 82 83
int ptrace_setregs(struct task_struct *child, void __user *uregs)
{
	struct pt_regs *regs = task_pt_regs(child);
	xtensa_gregset_t *gregset = uregs;
	const unsigned long ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
	unsigned long ps;
84
	unsigned long wb, ws;
85 86 87 88

	if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
		return -EIO;

89 90 91 92 93
	__get_user(regs->pc, &gregset->pc);
	__get_user(ps, &gregset->ps);
	__get_user(regs->lbeg, &gregset->lbeg);
	__get_user(regs->lend, &gregset->lend);
	__get_user(regs->lcount, &gregset->lcount);
94
	__get_user(ws, &gregset->windowstart);
95
	__get_user(wb, &gregset->windowbase);
C
Chris Zankel 已提交
96
	__get_user(regs->threadptr, &gregset->threadptr);
97 98 99

	regs->ps = (regs->ps & ~ps_mask) | (ps & ps_mask) | (1 << PS_EXCM_BIT);

100 101
	if (wb >= XCHAL_NUM_AREGS / 4)
		return -EFAULT;
102

103 104 105 106 107 108 109 110 111 112 113
	if (wb != regs->windowbase || ws != regs->windowstart) {
		unsigned long rotws, wmask;

		rotws = (((ws | (ws << WSBITS)) >> wb) &
				((1 << WSBITS) - 1)) & ~1;
		wmask = ((rotws ? WSBITS + 1 - ffs(rotws) : 0) << 4) |
			(rotws & 0xF) | 1;
		regs->windowbase = wb;
		regs->windowstart = ws;
		regs->wmask = wmask;
	}
114 115

	if (wb != 0 &&  __copy_from_user(regs->areg + XCHAL_NUM_AREGS - wb * 4,
116
				gregset->a, wb * 16))
117 118
		return -EFAULT;

119 120
	if (__copy_from_user(regs->areg, gregset->a + wb * 4,
				(WSBITS - wb) * 16))
121 122 123
		return -EFAULT;

	return 0;
124
}
125 126


127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
int ptrace_getxregs(struct task_struct *child, void __user *uregs)
{
	struct pt_regs *regs = task_pt_regs(child);
	struct thread_info *ti = task_thread_info(child);
	elf_xtregs_t __user *xtregs = uregs;
	int ret = 0;

	if (!access_ok(VERIFY_WRITE, uregs, sizeof(elf_xtregs_t)))
		return -EIO;

#if XTENSA_HAVE_COPROCESSORS
	/* Flush all coprocessor registers to memory. */
	coprocessor_flush_all(ti);
	ret |= __copy_to_user(&xtregs->cp0, &ti->xtregs_cp,
			      sizeof(xtregs_coprocessor_t));
#endif
	ret |= __copy_to_user(&xtregs->opt, &regs->xtregs_opt,
			      sizeof(xtregs->opt));
	ret |= __copy_to_user(&xtregs->user,&ti->xtregs_user,
			      sizeof(xtregs->user));
147

148 149 150 151 152 153 154 155 156 157
	return ret ? -EFAULT : 0;
}

int ptrace_setxregs(struct task_struct *child, void __user *uregs)
{
	struct thread_info *ti = task_thread_info(child);
	struct pt_regs *regs = task_pt_regs(child);
	elf_xtregs_t *xtregs = uregs;
	int ret = 0;

158 159 160
	if (!access_ok(VERIFY_READ, uregs, sizeof(elf_xtregs_t)))
		return -EFAULT;

161 162 163 164 165
#if XTENSA_HAVE_COPROCESSORS
	/* Flush all coprocessors before we overwrite them. */
	coprocessor_flush_all(ti);
	coprocessor_release_all(ti);

166
	ret |= __copy_from_user(&ti->xtregs_cp, &xtregs->cp0,
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
				sizeof(xtregs_coprocessor_t));
#endif
	ret |= __copy_from_user(&regs->xtregs_opt, &xtregs->opt,
				sizeof(xtregs->opt));
	ret |= __copy_from_user(&ti->xtregs_user, &xtregs->user,
				sizeof(xtregs->user));

	return ret ? -EFAULT : 0;
}

int ptrace_peekusr(struct task_struct *child, long regno, long __user *ret)
{
	struct pt_regs *regs;
	unsigned long tmp;

	regs = task_pt_regs(child);
	tmp = 0;  /* Default return value. */
184

185
	switch(regno) {
186 187

		case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
188
			tmp = regs->areg[regno - REG_AR_BASE];
189
			break;
190

191
		case REG_A_BASE ... REG_A_BASE + 15:
192
			tmp = regs->areg[regno - REG_A_BASE];
193
			break;
194

195 196 197
		case REG_PC:
			tmp = regs->pc;
			break;
198

199 200 201 202
		case REG_PS:
			/* Note:  PS.EXCM is not set while user task is running;
			 * its being set in regs is for exception handling
			 * convenience.  */
203
			tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
204
			break;
205

206
		case REG_WB:
207 208
			break;		/* tmp = 0 */

209
		case REG_WS:
210 211 212 213
		{
			unsigned long wb = regs->windowbase;
			unsigned long ws = regs->windowstart;
			tmp = ((ws>>wb) | (ws<<(WSBITS-wb))) & ((1<<WSBITS)-1);
214
			break;
215
		}
216 217 218
		case REG_LBEG:
			tmp = regs->lbeg;
			break;
219

220 221 222
		case REG_LEND:
			tmp = regs->lend;
			break;
223

224 225 226
		case REG_LCOUNT:
			tmp = regs->lcount;
			break;
227

228 229 230
		case REG_SAR:
			tmp = regs->sar;
			break;
231

232 233 234 235
		case SYSCALL_NR:
			tmp = regs->syscall;
			break;

236 237 238 239 240
		default:
			return -EIO;
	}
	return put_user(tmp, ret);
}
241

242 243 244 245
int ptrace_pokeusr(struct task_struct *child, long regno, long val)
{
	struct pt_regs *regs;
	regs = task_pt_regs(child);
246

247
	switch (regno) {
248
		case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
249
			regs->areg[regno - REG_AR_BASE] = val;
250
			break;
251

252
		case REG_A_BASE ... REG_A_BASE + 15:
253
			regs->areg[regno - REG_A_BASE] = val;
254
			break;
255

256
		case REG_PC:
257
			regs->pc = val;
258
			break;
259

260
		case SYSCALL_NR:
261
			regs->syscall = val;
262 263 264
			break;

		default:
265 266 267 268 269
			return -EIO;
	}
	return 0;
}

270 271
long arch_ptrace(struct task_struct *child, long request,
		 unsigned long addr, unsigned long data)
272 273
{
	int ret = -EPERM;
274
	void __user *datap = (void __user *) data;
275 276 277 278 279 280 281 282

	switch (request) {
	case PTRACE_PEEKTEXT:	/* read word at location addr. */
	case PTRACE_PEEKDATA:
		ret = generic_ptrace_peekdata(child, addr, data);
		break;

	case PTRACE_PEEKUSR:	/* read register specified by addr. */
283
		ret = ptrace_peekusr(child, addr, datap);
284 285 286 287 288 289 290 291 292
		break;

	case PTRACE_POKETEXT:	/* write the word at location addr. */
	case PTRACE_POKEDATA:
		ret = generic_ptrace_pokedata(child, addr, data);
		break;

	case PTRACE_POKEUSR:	/* write register specified by addr. */
		ret = ptrace_pokeusr(child, addr, data);
293 294 295
		break;

	case PTRACE_GETREGS:
296
		ret = ptrace_getregs(child, datap);
297 298 299
		break;

	case PTRACE_SETREGS:
300
		ret = ptrace_setregs(child, datap);
301 302
		break;

303
	case PTRACE_GETXTREGS:
304
		ret = ptrace_getxregs(child, datap);
305 306
		break;

307
	case PTRACE_SETXTREGS:
308
		ret = ptrace_setxregs(child, datap);
309 310 311 312
		break;

	default:
		ret = ptrace_request(child, request, addr, data);
313
		break;
314
	}
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	return ret;
}

void do_syscall_trace(void)
{
	/*
	 * 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.  strace only continues with a signal if the
	 * stopping signal is not SIGTRAP.  -brl
	 */
	if (current->exit_code) {
		send_sig(current->exit_code, current, 1);
		current->exit_code = 0;
	}
}
337 338 339 340 341 342 343 344

void do_syscall_trace_enter(struct pt_regs *regs)
{
	if (test_thread_flag(TIF_SYSCALL_TRACE)
			&& (current->ptrace & PT_PTRACED))
		do_syscall_trace();

#if 0
345
	audit_syscall_entry(current, AUDIT_ARCH_XTENSA..);
346 347 348 349 350 351 352 353 354
#endif
}

void do_syscall_trace_leave(struct pt_regs *regs)
{
	if ((test_thread_flag(TIF_SYSCALL_TRACE))
			&& (current->ptrace & PT_PTRACED))
		do_syscall_trace();
}