OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [ppc/] [kernel/] [ptrace.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1624 jcastillo
/*
2
 *  linux/arch/ppc/kernel/ptrace.c
3
 *
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
7
 *
8
 * Adapted from 'linux/arch/m68k/kernel/ptrace.c'
9
 * PowerPC version by Gary Thomas (gdt@linuxppc.org)
10
 *
11
 * This file is subject to the terms and conditions of the GNU General
12
 * Public License.  See the file README.legal in the main directory of
13
 * this archive for more details.
14
 */
15
 
16
#include <stddef.h>
17
#include <linux/kernel.h>
18
#include <linux/sched.h>
19
#include <linux/mm.h>
20
#include <linux/errno.h>
21
#include <linux/ptrace.h>
22
#include <linux/user.h>
23
 
24
#include <asm/segment.h>
25
#include <asm/page.h>
26
#include <asm/pgtable.h>
27
#include <asm/system.h>
28
 
29
/*
30
 * does not yet catch signals sent when the child dies.
31
 * in exit.c or in signal.c.
32
 */
33
 
34
/* Find the stack offset for a register, relative to tss.ksp. */
35
#define PT_REG(reg)     ((long)&((struct pt_regs *)0)->reg)
36
/* Mapping from PT_xxx to the stack offset at which the register is
37
   saved.  Notice that usp has no stack-slot and needs to be treated
38
   specially (see get_reg/put_reg below). */
39
static int regoff[] = {
40
};
41
 
42
/* change a pid into a task struct. */
43
static inline struct task_struct * get_task(int pid)
44
{
45
        int i;
46
 
47
        for (i = 1; i < NR_TASKS; i++) {
48
                if (task[i] != NULL && (task[i]->pid == pid))
49
                        return task[i];
50
        }
51
        return NULL;
52
}
53
 
54
/*
55
 * Get contents of register REGNO in task TASK.
56
 */
57
static inline long get_reg(struct task_struct *task, int regno)
58
{
59
        struct pt_regs *regs = task->tss.regs;
60
        if (regno <= PT_R31)
61
        {
62
                return (regs->gpr[regno]);
63
        } else
64
        if (regno == PT_NIP)
65
        {
66
                return (regs->nip);
67
        } else
68
        if (regno == PT_MSR)
69
        {
70
                return (regs->msr);
71
        } else
72
        if (regno == PT_ORIG_R3)
73
        {
74
                return (regs->orig_gpr3);
75
        } else
76
        if (regno == PT_CTR)
77
        {
78
                return (regs->ctr);
79
        } else
80
        if (regno == PT_LNK)
81
        {
82
                return (regs->link);
83
        } else
84
        if (regno == PT_XER)
85
        {
86
                return (regs->xer);
87
        } else
88
        if (regno == PT_CCR)
89
        {
90
                return (regs->ccr);
91
        }
92
        return (0);
93
}
94
 
95
/*
96
 * Write contents of register REGNO in task TASK.
97
 */
98
static inline int put_reg(struct task_struct *task, int regno,
99
                          unsigned long data)
100
{
101
        struct pt_regs *regs = task->tss.regs;
102
        if (regno <= PT_R31)
103
        {
104
                regs->gpr[regno] = data;
105
        } else
106
        if (regno == PT_NIP)
107
        {
108
                regs->nip = data;
109
        } else
110
        if (regno == PT_MSR)
111
        {
112
                regs->msr = data;
113
        } else
114
        if (regno == PT_CTR)
115
        {
116
                regs->ctr = data;
117
        } else
118
        if (regno == PT_LNK)
119
        {
120
                regs->link = data;
121
        } else
122
        if (regno == PT_XER)
123
        {
124
                regs->xer = data;
125
        } else
126
        if (regno == PT_CCR)
127
        {
128
                regs->ccr = data;
129
        } else
130
        { /* Invalid register */
131
                return (-1);
132
        }
133
        return (0);
134
}
135
 
136
static inline
137
set_single_step(struct task_struct *task)
138
{
139
        struct pt_regs *regs = task->tss.regs;
140
printk("Set single step - Task: %x, Regs: %x", task, regs);
141
printk(", MSR: %x/", regs->msr);
142
        regs->msr |= MSR_SE;
143
printk("%x\n", regs->msr);
144
}
145
 
146
static inline
147
clear_single_step(struct task_struct *task)
148
{
149
        struct pt_regs *regs = task->tss.regs;
150
        regs->msr &= ~MSR_SE;
151
}
152
 
153
/*
154
 * This routine gets a long from any process space by following the page
155
 * tables. NOTE! You should check that the long isn't on a page boundary,
156
 * and that it is in the task area before calling this: this routine does
157
 * no checking.
158
 *
159
 */
160
static unsigned long get_long(struct task_struct * tsk,
161
        struct vm_area_struct * vma, unsigned long addr)
162
{
163
        pgd_t * pgdir;
164
        pmd_t * pgmiddle;
165
        pte_t * pgtable;
166
        unsigned long page;
167
 
168
repeat:
169
        pgdir = pgd_offset(vma->vm_mm, addr);
170
        if (pgd_none(*pgdir)) {
171
                do_no_page(tsk, vma, addr, 0);
172
                goto repeat;
173
        }
174
        if (pgd_bad(*pgdir)) {
175
                printk("ptrace: bad page directory %08lx\n", pgd_val(*pgdir));
176
                pgd_clear(pgdir);
177
                return 0;
178
        }
179
        pgmiddle = pmd_offset(pgdir,addr);
180
        if (pmd_none(*pgmiddle)) {
181
                do_no_page(tsk, vma, addr, 0);
182
                goto repeat;
183
        }
184
        if (pmd_bad(*pgmiddle)) {
185
                printk("ptrace: bad page directory %08lx\n",
186
                       pmd_val(*pgmiddle));
187
                pmd_clear(pgmiddle);
188
                return 0;
189
        }
190
        pgtable = pte_offset(pgmiddle, addr);
191
        if (!pte_present(*pgtable)) {
192
                do_no_page(tsk, vma, addr, 0);
193
                goto repeat;
194
        }
195
        page = pte_page(*pgtable);
196
/* this is a hack for non-kernel-mapped video buffers and similar */
197
        if (page >= high_memory)
198
                return 0;
199
        page += addr & ~PAGE_MASK;
200
        return *(unsigned long *) page;
201
}
202
 
203
/*
204
 * This routine puts a long into any process space by following the page
205
 * tables. NOTE! You should check that the long isn't on a page boundary,
206
 * and that it is in the task area before calling this: this routine does
207
 * no checking.
208
 *
209
 * Now keeps R/W state of page so that a text page stays readonly
210
 * even if a debugger scribbles breakpoints into it.  -M.U-
211
 */
212
static void put_long(struct task_struct * tsk, struct vm_area_struct * vma, unsigned long addr,
213
        unsigned long data)
214
{
215
        pgd_t *pgdir;
216
        pmd_t *pgmiddle;
217
        pte_t *pgtable;
218
        unsigned long page;
219
 
220
repeat:
221
        pgdir = pgd_offset(vma->vm_mm, addr);
222
        if (!pgd_present(*pgdir)) {
223
                do_no_page(tsk, vma, addr, 1);
224
                goto repeat;
225
        }
226
        if (pgd_bad(*pgdir)) {
227
                printk("ptrace: bad page directory %08lx\n", pgd_val(*pgdir));
228
                pgd_clear(pgdir);
229
                return;
230
        }
231
        pgmiddle = pmd_offset(pgdir,addr);
232
        if (pmd_none(*pgmiddle)) {
233
                do_no_page(tsk, vma, addr, 1);
234
                goto repeat;
235
        }
236
        if (pmd_bad(*pgmiddle)) {
237
                printk("ptrace: bad page directory %08lx\n",
238
                       pmd_val(*pgmiddle));
239
                pmd_clear(pgmiddle);
240
                return;
241
        }
242
        pgtable = pte_offset(pgmiddle, addr);
243
        if (!pte_present(*pgtable)) {
244
                do_no_page(tsk, vma, addr, 1);
245
                goto repeat;
246
        }
247
        page = pte_page(*pgtable);
248
        if (!pte_write(*pgtable)) {
249
                do_wp_page(tsk, vma, addr, 2);
250
                goto repeat;
251
        }
252
/* this is a hack for non-kernel-mapped video buffers and similar */
253
        if (page < high_memory) {
254
                *(unsigned long *) (page + (addr & ~PAGE_MASK)) = data;
255
        }
256
/* we're bypassing pagetables, so we have to set the dirty bit ourselves */
257
/* this should also re-instate whatever read-only mode there was before */
258
        *pgtable = pte_mkdirty(mk_pte(page, vma->vm_page_prot));
259
        flush_tlb_all();
260
}
261
 
262
static struct vm_area_struct * find_extend_vma(struct task_struct * tsk, unsigned long addr)
263
{
264
        struct vm_area_struct * vma;
265
 
266
        addr &= PAGE_MASK;
267
        vma = find_vma(tsk,addr);
268
        if (!vma)
269
                return NULL;
270
        if (vma->vm_start <= addr)
271
                return vma;
272
        if (!(vma->vm_flags & VM_GROWSDOWN))
273
                return NULL;
274
        if (vma->vm_end - addr > tsk->rlim[RLIMIT_STACK].rlim_cur)
275
                return NULL;
276
        vma->vm_offset -= vma->vm_start - addr;
277
        vma->vm_start = addr;
278
        return vma;
279
}
280
 
281
/*
282
 * This routine checks the page boundaries, and that the offset is
283
 * within the task area. It then calls get_long() to read a long.
284
 */
285
static int read_long(struct task_struct * tsk, unsigned long addr,
286
        unsigned long * result)
287
{
288
        struct vm_area_struct * vma = find_extend_vma(tsk, addr);
289
 
290
        if (!vma)
291
                return -EIO;
292
        if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
293
                unsigned long low,high;
294
                struct vm_area_struct * vma_low = vma;
295
 
296
                if (addr + sizeof(long) >= vma->vm_end) {
297
                        vma_low = vma->vm_next;
298
                        if (!vma_low || vma_low->vm_start != vma->vm_end)
299
                                return -EIO;
300
                }
301
                high = get_long(tsk, vma,addr & ~(sizeof(long)-1));
302
                low = get_long(tsk, vma_low,(addr+sizeof(long)) & ~(sizeof(long)-1));
303
                switch (addr & (sizeof(long)-1)) {
304
                        case 3:
305
                                low >>= 8;
306
                                low |= high << 24;
307
                                break;
308
                        case 2:
309
                                low >>= 16;
310
                                low |= high << 16;
311
                                break;
312
                        case 1:
313
                                low >>= 24;
314
                                low |= high << 8;
315
                                break;
316
                }
317
                *result = low;
318
        } else
319
                *result = get_long(tsk, vma,addr);
320
        return 0;
321
}
322
 
323
/*
324
 * This routine checks the page boundaries, and that the offset is
325
 * within the task area. It then calls put_long() to write a long.
326
 */
327
static int write_long(struct task_struct * tsk, unsigned long addr,
328
        unsigned long data)
329
{
330
        struct vm_area_struct * vma = find_extend_vma(tsk, addr);
331
 
332
        if (!vma)
333
                return -EIO;
334
        if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
335
                unsigned long low,high;
336
                struct vm_area_struct * vma_low = vma;
337
 
338
                if (addr + sizeof(long) >= vma->vm_end) {
339
                        vma_low = vma->vm_next;
340
                        if (!vma_low || vma_low->vm_start != vma->vm_end)
341
                                return -EIO;
342
                }
343
                high = get_long(tsk, vma,addr & ~(sizeof(long)-1));
344
                low = get_long(tsk, vma_low,(addr+sizeof(long)) & ~(sizeof(long)-1));
345
                switch (addr & (sizeof(long)-1)) {
346
                        case 0: /* shouldn't happen, but safety first */
347
                                high = data;
348
                                break;
349
                        case 3:
350
                                low &= 0x000000ff;
351
                                low |= data << 8;
352
                                high &= ~0xff;
353
                                high |= data >> 24;
354
                                break;
355
                        case 2:
356
                                low &= 0x0000ffff;
357
                                low |= data << 16;
358
                                high &= ~0xffff;
359
                                high |= data >> 16;
360
                                break;
361
                        case 1:
362
                                low &= 0x00ffffff;
363
                                low |= data << 24;
364
                                high &= ~0xffffff;
365
                                high |= data >> 8;
366
                                break;
367
                }
368
                put_long(tsk, vma,addr & ~(sizeof(long)-1),high);
369
                put_long(tsk, vma_low,(addr+sizeof(long)) & ~(sizeof(long)-1),low);
370
        } else
371
                put_long(tsk, vma,addr,data);
372
        return 0;
373
}
374
 
375
asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
376
{
377
        struct task_struct *child;
378
        struct user * dummy;
379
 
380
        dummy = NULL;
381
 
382
        if (request == PTRACE_TRACEME) {
383
                /* are we already being traced? */
384
                if (current->flags & PF_PTRACED)
385
                        return -EPERM;
386
                /* set the ptrace bit in the process flags. */
387
                current->flags |= PF_PTRACED;
388
                return 0;
389
        }
390
        if (pid == 1)           /* you may not mess with init */
391
                return -EPERM;
392
        if (!(child = get_task(pid)))
393
                return -ESRCH;
394
        if (request == PTRACE_ATTACH) {
395
                if (child == current)
396
                        return -EPERM;
397
                if ((!child->dumpable ||
398
                    (current->uid != child->euid) ||
399
                    (current->uid != child->uid) ||
400
                    (current->gid != child->egid) ||
401
                    (current->gid != child->gid)) && !suser())
402
                        return -EPERM;
403
                /* the same process cannot be attached many times */
404
                if (child->flags & PF_PTRACED)
405
                        return -EPERM;
406
                child->flags |= PF_PTRACED;
407
                if (child->p_pptr != current) {
408
                        REMOVE_LINKS(child);
409
                        child->p_pptr = current;
410
                        SET_LINKS(child);
411
                }
412
                send_sig(SIGSTOP, child, 1);
413
                return 0;
414
        }
415
        if (!(child->flags & PF_PTRACED))
416
                return -ESRCH;
417
        if (child->state != TASK_STOPPED) {
418
                if (request != PTRACE_KILL)
419
                        return -ESRCH;
420
        }
421
        if (child->p_pptr != current)
422
                return -ESRCH;
423
 
424
        switch (request) {
425
        /* If I and D space are separate, these will need to be fixed. */
426
                case PTRACE_PEEKTEXT: /* read word at location addr. */
427
                case PTRACE_PEEKDATA: {
428
                        unsigned long tmp;
429
                        int res;
430
 
431
                        res = read_long(child, addr, &tmp);
432
                        if (res < 0)
433
                                return res;
434
                        res = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
435
                        if (!res)
436
                                put_user(tmp, (unsigned long *) data);
437
                        return res;
438
                }
439
 
440
        /* read the word at location addr in the USER area. */
441
                case PTRACE_PEEKUSR: {
442
                        unsigned long tmp;
443
                        int res;
444
 
445
                        if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
446
                                return -EIO;
447
 
448
                        res = verify_area(VERIFY_WRITE, (void *) data,
449
                                          sizeof(long));
450
                        if (res)
451
                                return res;
452
                        tmp = 0;  /* Default return condition */
453
                        addr = addr >> 2; /* temporary hack. */
454
                        if (addr < PT_FPR0) {
455
                                tmp = get_reg(child, addr);
456
                        }
457
#if 0                   
458
                        else if (addr >= PT_FPR0 && addr < PT_FPR31)
459
                                tmp = child->tss.fpr[addr - PT_FPR0];
460
#endif                          
461
                        else
462
                                return -EIO;
463
                        put_user(tmp,(unsigned long *) data);
464
                        return 0;
465
                }
466
 
467
      /* If I and D space are separate, this will have to be fixed. */
468
                case PTRACE_POKETEXT: /* write the word at location addr. */
469
                case PTRACE_POKEDATA:
470
                        return write_long(child,addr,data);
471
 
472
                case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
473
                        if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
474
                                return -EIO;
475
 
476
                        addr = addr >> 2; /* temporary hack. */
477
 
478
                        if (addr == PT_ORIG_R3)
479
                                return -EIO;
480
#if 0 /* Let this check be in 'put_reg' */                              
481
                        if (addr == PT_SR) {
482
                                data &= SR_MASK;
483
                                data <<= 16;
484
                                data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
485
                        }
486
#endif                  
487
                        if (addr < PT_FPR0) {
488
                                if (put_reg(child, addr, data))
489
                                        return -EIO;
490
                                return 0;
491
                        }
492
#if 0                   
493
                        if (addr >= 21 && addr < 48)
494
                        {
495
                                child->tss.fp[addr - 21] = data;
496
                                return 0;
497
                        }
498
#endif                  
499
                        return -EIO;
500
 
501
                case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
502
                case PTRACE_CONT: { /* restart after signal. */
503
                        if ((unsigned long) data >= NSIG)
504
                                return -EIO;
505
                        if (request == PTRACE_SYSCALL)
506
                                child->flags |= PF_TRACESYS;
507
                        else
508
                                child->flags &= ~PF_TRACESYS;
509
                        child->exit_code = data;
510
                        wake_up_process(child);
511
                        /* make sure the single step bit is not set. */
512
                        clear_single_step(child);
513
                        return 0;
514
                }
515
 
516
/*
517
 * make the child exit.  Best I can do is send it a sigkill.
518
 * perhaps it should be put in the status that it wants to
519
 * exit.
520
 */
521
                case PTRACE_KILL: {
522
                        if (child->state == TASK_ZOMBIE) /* already dead */
523
                                return 0;
524
                        wake_up_process(child);
525
                        child->exit_code = SIGKILL;
526
                        /* make sure the single step bit is not set. */
527
                        clear_single_step(child);
528
                        return 0;
529
                }
530
 
531
                case PTRACE_SINGLESTEP: {  /* set the trap flag. */
532
                        if ((unsigned long) data >= NSIG)
533
                                return -EIO;
534
                        child->flags &= ~PF_TRACESYS;
535
                        set_single_step(child);
536
                        wake_up_process(child);
537
                        child->exit_code = data;
538
                        /* give it a chance to run. */
539
                        return 0;
540
                }
541
 
542
                case PTRACE_DETACH: { /* detach a process that was attached. */
543
                        if ((unsigned long) data >= NSIG)
544
                                return -EIO;
545
                        child->flags &= ~(PF_PTRACED|PF_TRACESYS);
546
                        wake_up_process(child);
547
                        child->exit_code = data;
548
                        REMOVE_LINKS(child);
549
                        child->p_pptr = child->p_opptr;
550
                        SET_LINKS(child);
551
                        /* make sure the single step bit is not set. */
552
                        clear_single_step(child);
553
                        return 0;
554
                }
555
 
556
                default:
557
                        return -EIO;
558
        }
559
}
560
 
561
asmlinkage void syscall_trace(void)
562
{
563
        if ((current->flags & (PF_PTRACED|PF_TRACESYS))
564
                        != (PF_PTRACED|PF_TRACESYS))
565
                return;
566
        current->exit_code = SIGTRAP;
567
        current->state = TASK_STOPPED;
568
        notify_parent(current);
569
        schedule();
570
        /*
571
         * this isn't the same as continuing with a signal, but it will do
572
         * for normal use.  strace only continues with a signal if the
573
         * stopping signal is not SIGTRAP.  -brl
574
         */
575
        if (current->exit_code)
576
                current->signal |= (1 << (current->exit_code - 1));
577
        current->exit_code = 0;
578
        return;
579
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.