* better
[mascara-docs.git] / i386 / linux-2.3.21 / arch / m68k / kernel / ptrace.c
blob7db64849dd29ff9c6ee16b9b1ccf4425a563a25b
1 /*
2 * linux/arch/m68k/kernel/ptrace.c
4 * Copyright (C) 1994 by Hamish Macdonald
5 * Taken from linux/kernel/ptrace.c and modified for M680x0.
6 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License. See the file COPYING in the main directory of
10 * this archive for more details.
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/config.h>
23 #include <asm/uaccess.h>
24 #include <asm/page.h>
25 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #include <asm/processor.h>
30 * does not yet catch signals sent when the child dies.
31 * in exit.c or in signal.c.
34 /* determines which bits in the SR the user has access to. */
35 /* 1 = access 0 = no access */
36 #define SR_MASK 0x001f
38 /* sets the trace bits. */
39 #define TRACE_BITS 0x8000
41 /* Find the stack offset for a register, relative to thread.esp0. */
42 #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
43 #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
44 - sizeof(struct switch_stack))
45 /* Mapping from PT_xxx to the stack offset at which the register is
46 saved. Notice that usp has no stack-slot and needs to be treated
47 specially (see get_reg/put_reg below). */
48 static int regoff[] = {
49 PT_REG(d1), PT_REG(d2), PT_REG(d3), PT_REG(d4),
50 PT_REG(d5), SW_REG(d6), SW_REG(d7), PT_REG(a0),
51 PT_REG(a1), PT_REG(a2), SW_REG(a3), SW_REG(a4),
52 SW_REG(a5), SW_REG(a6), PT_REG(d0), -1,
53 PT_REG(orig_d0), PT_REG(sr), PT_REG(pc),
57 * Get contents of register REGNO in task TASK.
59 static inline long get_reg(struct task_struct *task, int regno)
61 unsigned long *addr;
63 if (regno == PT_USP)
64 addr = &task->thread.usp;
65 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
66 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
67 else
68 return 0;
69 return *addr;
73 * Write contents of register REGNO in task TASK.
75 static inline int put_reg(struct task_struct *task, int regno,
76 unsigned long data)
78 unsigned long *addr;
80 if (regno == PT_USP)
81 addr = &task->thread.usp;
82 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
83 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
84 else
85 return -1;
86 *addr = data;
87 return 0;
91 * This routine gets a long from any process space by following the page
92 * tables. NOTE! You should check that the long isn't on a page boundary,
93 * and that it is in the task area before calling this: this routine does
94 * no checking.
97 static unsigned long get_long(struct task_struct * tsk,
98 struct vm_area_struct * vma, unsigned long addr)
100 pgd_t * pgdir;
101 pmd_t * pgmiddle;
102 pte_t * pgtable;
103 unsigned long page;
105 repeat:
106 pgdir = pgd_offset(vma->vm_mm, addr);
107 if (pgd_none(*pgdir)) {
108 handle_mm_fault(tsk, vma, addr, 0);
109 goto repeat;
111 if (pgd_bad(*pgdir)) {
112 printk("ptrace: bad page directory %08lx\n", pgd_val(*pgdir));
113 pgd_clear(pgdir);
114 return 0;
116 pgmiddle = pmd_offset(pgdir,addr);
117 if (pmd_none(*pgmiddle)) {
118 handle_mm_fault(tsk, vma, addr, 0);
119 goto repeat;
121 if (pmd_bad(*pgmiddle)) {
122 printk("ptrace: bad page directory %08lx\n",
123 pmd_val(*pgmiddle));
124 pmd_clear(pgmiddle);
125 return 0;
127 pgtable = pte_offset(pgmiddle, addr);
128 if (!pte_present(*pgtable)) {
129 handle_mm_fault(tsk, vma, addr, 0);
130 goto repeat;
132 page = pte_page(*pgtable);
133 /* this is a hack for non-kernel-mapped video buffers and similar */
134 if (MAP_NR(page) >= max_mapnr)
135 return 0;
136 page += addr & ~PAGE_MASK;
137 return *(unsigned long *) page;
141 * This routine puts a long into any process space by following the page
142 * tables. NOTE! You should check that the long isn't on a page boundary,
143 * and that it is in the task area before calling this: this routine does
144 * no checking.
146 * Now keeps R/W state of page so that a text page stays readonly
147 * even if a debugger scribbles breakpoints into it. -M.U-
149 static void put_long(struct task_struct * tsk, struct vm_area_struct * vma, unsigned long addr,
150 unsigned long data)
152 pgd_t *pgdir;
153 pmd_t *pgmiddle;
154 pte_t *pgtable;
155 unsigned long page;
157 repeat:
158 pgdir = pgd_offset(vma->vm_mm, addr);
159 if (!pgd_present(*pgdir)) {
160 handle_mm_fault(tsk, vma, addr, 1);
161 goto repeat;
163 if (pgd_bad(*pgdir)) {
164 printk("ptrace: bad page directory %08lx\n", pgd_val(*pgdir));
165 pgd_clear(pgdir);
166 return;
168 pgmiddle = pmd_offset(pgdir,addr);
169 if (pmd_none(*pgmiddle)) {
170 handle_mm_fault(tsk, vma, addr, 1);
171 goto repeat;
173 if (pmd_bad(*pgmiddle)) {
174 printk("ptrace: bad page directory %08lx\n",
175 pmd_val(*pgmiddle));
176 pmd_clear(pgmiddle);
177 return;
179 pgtable = pte_offset(pgmiddle, addr);
180 if (!pte_present(*pgtable)) {
181 handle_mm_fault(tsk, vma, addr, 1);
182 goto repeat;
184 page = pte_page(*pgtable);
185 if (!pte_write(*pgtable)) {
186 handle_mm_fault(tsk, vma, addr, 1);
187 goto repeat;
189 /* this is a hack for non-kernel-mapped video buffers and similar */
190 if (MAP_NR(page) < max_mapnr) {
191 *(unsigned long *) (page + (addr & ~PAGE_MASK)) = data;
192 flush_page_to_ram (page);
194 /* we're bypassing pagetables, so we have to set the dirty bit ourselves */
195 /* this should also re-instate whatever read-only mode there was before */
196 *pgtable = pte_mkdirty(mk_pte(page, vma->vm_page_prot));
197 flush_tlb_all();
201 * This routine checks the page boundaries, and that the offset is
202 * within the task area. It then calls get_long() to read a long.
204 static int read_long(struct task_struct * tsk, unsigned long addr,
205 unsigned long * result)
207 struct vm_area_struct * vma = find_extend_vma(tsk, addr);
209 if (!vma)
210 return -EIO;
211 if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
212 unsigned long low,high;
213 struct vm_area_struct * vma_low = vma;
215 if (addr + sizeof(long) >= vma->vm_end) {
216 vma_low = vma->vm_next;
217 if (!vma_low || vma_low->vm_start != vma->vm_end)
218 return -EIO;
220 high = get_long(tsk, vma,addr & ~(sizeof(long)-1));
221 low = get_long(tsk, vma_low,(addr+sizeof(long)) & ~(sizeof(long)-1));
222 switch (addr & (sizeof(long)-1)) {
223 case 3:
224 low >>= 8;
225 low |= high << 24;
226 break;
227 case 2:
228 low >>= 16;
229 low |= high << 16;
230 break;
231 case 1:
232 low >>= 24;
233 low |= high << 8;
234 break;
236 *result = low;
237 } else
238 *result = get_long(tsk, vma,addr);
239 return 0;
243 * This routine checks the page boundaries, and that the offset is
244 * within the task area. It then calls put_long() to write a long.
246 static int write_long(struct task_struct * tsk, unsigned long addr,
247 unsigned long data)
249 struct vm_area_struct * vma = find_extend_vma(tsk, addr);
251 if (!vma)
252 return -EIO;
253 if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
254 unsigned long low,high;
255 struct vm_area_struct * vma_low = vma;
257 if (addr + sizeof(long) >= vma->vm_end) {
258 vma_low = vma->vm_next;
259 if (!vma_low || vma_low->vm_start != vma->vm_end)
260 return -EIO;
262 high = get_long(tsk, vma,addr & ~(sizeof(long)-1));
263 low = get_long(tsk, vma_low,(addr+sizeof(long)) & ~(sizeof(long)-1));
264 switch (addr & (sizeof(long)-1)) {
265 case 0: /* shouldn't happen, but safety first */
266 high = data;
267 break;
268 case 3:
269 low &= 0x000000ff;
270 low |= data << 8;
271 high &= ~0xff;
272 high |= data >> 24;
273 break;
274 case 2:
275 low &= 0x0000ffff;
276 low |= data << 16;
277 high &= ~0xffff;
278 high |= data >> 16;
279 break;
280 case 1:
281 low &= 0x00ffffff;
282 low |= data << 24;
283 high &= ~0xffffff;
284 high |= data >> 8;
285 break;
287 put_long(tsk, vma,addr & ~(sizeof(long)-1),high);
288 put_long(tsk, vma_low,(addr+sizeof(long)) & ~(sizeof(long)-1),low);
289 } else
290 put_long(tsk, vma,addr,data);
291 return 0;
294 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
296 struct task_struct *child;
297 unsigned long flags;
298 int ret;
300 lock_kernel();
301 ret = -EPERM;
302 if (request == PTRACE_TRACEME) {
303 /* are we already being traced? */
304 if (current->flags & PF_PTRACED)
305 goto out;
306 /* set the ptrace bit in the process flags. */
307 current->flags |= PF_PTRACED;
308 ret = 0;
309 goto out;
311 ret = -ESRCH;
312 read_lock(&tasklist_lock);
313 child = find_task_by_pid(pid);
314 read_unlock(&tasklist_lock); /* FIXME!!! */
315 if (!child)
316 goto out;
317 ret = -EPERM;
318 if (pid == 1) /* you may not mess with init */
319 goto out;
320 if (request == PTRACE_ATTACH) {
321 if (child == current)
322 goto out;
323 if ((!child->dumpable ||
324 (current->uid != child->euid) ||
325 (current->uid != child->suid) ||
326 (current->uid != child->uid) ||
327 (current->gid != child->egid) ||
328 (current->gid != child->sgid) ||
329 (!cap_issubset(child->cap_permitted, current->cap_permitted)) ||
330 (current->gid != child->gid)) && !capable(CAP_SYS_PTRACE))
331 goto out;
332 /* the same process cannot be attached many times */
333 if (child->flags & PF_PTRACED)
334 goto out;
335 child->flags |= PF_PTRACED;
337 write_lock_irqsave(&tasklist_lock, flags);
338 if (child->p_pptr != current) {
339 REMOVE_LINKS(child);
340 child->p_pptr = current;
341 SET_LINKS(child);
343 write_unlock_irqrestore(&tasklist_lock, flags);
345 send_sig(SIGSTOP, child, 1);
346 ret = 0;
347 goto out;
349 ret = -ESRCH;
350 if (!(child->flags & PF_PTRACED))
351 goto out;
352 if (child->state != TASK_STOPPED) {
353 if (request != PTRACE_KILL)
354 goto out;
356 if (child->p_pptr != current)
357 goto out;
359 switch (request) {
360 /* when I and D space are separate, these will need to be fixed. */
361 case PTRACE_PEEKTEXT: /* read word at location addr. */
362 case PTRACE_PEEKDATA: {
363 unsigned long tmp;
365 down(&child->mm->mmap_sem);
366 ret = read_long(child, addr, &tmp);
367 up(&child->mm->mmap_sem);
368 if (ret >= 0)
369 ret = put_user(tmp, (unsigned long *) data);
370 goto out;
373 /* read the word at location addr in the USER area. */
374 case PTRACE_PEEKUSR: {
375 unsigned long tmp;
377 ret = -EIO;
378 if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
379 goto out;
381 tmp = 0; /* Default return condition */
382 addr = addr >> 2; /* temporary hack. */
383 ret = -EIO;
384 if (addr < 19) {
385 tmp = get_reg(child, addr);
386 if (addr == PT_SR)
387 tmp >>= 16;
388 } else if (addr >= 21 && addr < 49) {
389 tmp = child->thread.fp[addr - 21];
390 #ifdef CONFIG_M68KFPU_EMU
391 /* Convert internal fpu reg representation
392 * into long double format
394 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
395 tmp = ((tmp & 0xffff0000) << 15) |
396 ((tmp & 0x0000ffff) << 16);
397 #endif
398 } else
399 goto out;
400 ret = put_user(tmp,(unsigned long *) data);
401 goto out;
404 /* when I and D space are separate, this will have to be fixed. */
405 case PTRACE_POKETEXT: /* write the word at location addr. */
406 case PTRACE_POKEDATA:
407 down(&child->mm->mmap_sem);
408 ret = write_long(child,addr,data);
409 up(&child->mm->mmap_sem);
410 goto out;
412 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
413 ret = -EIO;
414 if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
415 goto out;
417 addr = addr >> 2; /* temporary hack. */
419 if (addr == PT_ORIG_D0)
420 goto out;
421 if (addr == PT_SR) {
422 data &= SR_MASK;
423 data <<= 16;
424 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
426 if (addr < 19) {
427 if (put_reg(child, addr, data))
428 goto out;
429 ret = 0;
430 goto out;
432 if (addr >= 21 && addr < 48)
434 #ifdef CONFIG_M68KFPU_EMU
435 /* Convert long double format
436 * into internal fpu reg representation
438 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
439 data = (unsigned long)data << 15;
440 data = (data & 0xffff0000) |
441 ((data & 0x0000ffff) >> 1);
443 #endif
444 child->thread.fp[addr - 21] = data;
445 ret = 0;
447 goto out;
449 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
450 case PTRACE_CONT: { /* restart after signal. */
451 long tmp;
453 ret = -EIO;
454 if ((unsigned long) data > _NSIG)
455 goto out;
456 if (request == PTRACE_SYSCALL)
457 child->flags |= PF_TRACESYS;
458 else
459 child->flags &= ~PF_TRACESYS;
460 child->exit_code = data;
461 /* make sure the single step bit is not set. */
462 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
463 put_reg(child, PT_SR, tmp);
464 wake_up_process(child);
465 ret = 0;
466 goto out;
470 * make the child exit. Best I can do is send it a sigkill.
471 * perhaps it should be put in the status that it wants to
472 * exit.
474 case PTRACE_KILL: {
475 long tmp;
477 ret = 0;
478 if (child->state == TASK_ZOMBIE) /* already dead */
479 goto out;
480 child->exit_code = SIGKILL;
481 /* make sure the single step bit is not set. */
482 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
483 put_reg(child, PT_SR, tmp);
484 wake_up_process(child);
485 goto out;
488 case PTRACE_SINGLESTEP: { /* set the trap flag. */
489 long tmp;
491 ret = -EIO;
492 if ((unsigned long) data > _NSIG)
493 goto out;
494 child->flags &= ~PF_TRACESYS;
495 tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
496 put_reg(child, PT_SR, tmp);
498 child->exit_code = data;
499 /* give it a chance to run. */
500 wake_up_process(child);
501 ret = 0;
502 goto out;
505 case PTRACE_DETACH: { /* detach a process that was attached. */
506 long tmp;
508 ret = -EIO;
509 if ((unsigned long) data > _NSIG)
510 goto out;
511 child->flags &= ~(PF_PTRACED|PF_TRACESYS);
512 child->exit_code = data;
513 write_lock_irqsave(&tasklist_lock, flags);
514 REMOVE_LINKS(child);
515 child->p_pptr = child->p_opptr;
516 SET_LINKS(child);
517 write_unlock_irqrestore(&tasklist_lock, flags);
518 /* make sure the single step bit is not set. */
519 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
520 put_reg(child, PT_SR, tmp);
521 wake_up_process(child);
522 ret = 0;
523 goto out;
526 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
527 int i;
528 unsigned long tmp;
529 for (i = 0; i < 19; i++) {
530 tmp = get_reg(child, i);
531 if (i == PT_SR)
532 tmp >>= 16;
533 if (put_user(tmp, (unsigned long *) data)) {
534 ret = -EFAULT;
535 goto out;
537 data += sizeof(long);
539 ret = 0;
540 goto out;
543 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
544 int i;
545 unsigned long tmp;
546 for (i = 0; i < 19; i++) {
547 if (get_user(tmp, (unsigned long *) data)) {
548 ret = -EFAULT;
549 goto out;
551 if (i == PT_SR) {
552 tmp &= SR_MASK;
553 tmp <<= 16;
554 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
556 put_reg(child, i, tmp);
557 data += sizeof(long);
559 ret = 0;
560 goto out;
563 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
564 ret = 0;
565 if (copy_to_user((void *)data, &child->thread.fp,
566 sizeof(struct user_m68kfp_struct)))
567 ret = -EFAULT;
568 goto out;
571 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
572 ret = 0;
573 if (copy_from_user(&child->thread.fp, (void *)data,
574 sizeof(struct user_m68kfp_struct)))
575 ret = -EFAULT;
576 goto out;
579 default:
580 ret = -EIO;
581 goto out;
583 out:
584 unlock_kernel();
585 return ret;
588 asmlinkage void syscall_trace(void)
590 lock_kernel();
591 if ((current->flags & (PF_PTRACED|PF_TRACESYS))
592 != (PF_PTRACED|PF_TRACESYS))
593 goto out;
594 current->exit_code = SIGTRAP;
595 current->state = TASK_STOPPED;
596 notify_parent(current, SIGCHLD);
597 schedule();
599 * this isn't the same as continuing with a signal, but it will do
600 * for normal use. strace only continues with a signal if the
601 * stopping signal is not SIGTRAP. -brl
603 if (current->exit_code) {
604 send_sig(current->exit_code, current, 1);
605 current->exit_code = 0;
607 out:
608 unlock_kernel();