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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [arch/] [ppc64/] [kernel/] [ptrace32.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *  linux/arch/ppc64/kernel/ptrace32.c
3
 *
4
 *  PowerPC version
5
 *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6
 *
7
 *  Derived from "arch/m68k/kernel/ptrace.c"
8
 *  Copyright (C) 1994 by Hamish Macdonald
9
 *  Taken from linux/kernel/ptrace.c and modified for M680x0.
10
 *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
11
 *
12
 * Modified by Cort Dougan (cort@hq.fsmlabs.com)
13
 * and Paul Mackerras (paulus@linuxcare.com.au).
14
 *
15
 * This file is subject to the terms and conditions of the GNU General
16
 * Public License.  See the file README.legal in the main directory of
17
 * this archive for more details.
18
 */
19
 
20
#include <linux/kernel.h>
21
#include <linux/sched.h>
22
#include <linux/mm.h>
23
#include <linux/smp.h>
24
#include <linux/smp_lock.h>
25
#include <linux/errno.h>
26
#include <linux/ptrace.h>
27
#include <linux/user.h>
28
 
29
#include <asm/uaccess.h>
30
#include <asm/page.h>
31
#include <asm/pgtable.h>
32
#include <asm/system.h>
33
 
34
#ifdef CONFIG_ALTIVEC
35
/*
36
 * Get contents of AltiVec register state in task TASK
37
 */
38
static inline int get_vrregs32(unsigned long data, struct task_struct *task)
39
{
40
        if(copy_to_user((void *)data,&task->thread.vr[0],
41
                        offsetof(struct thread_struct,vrsave)-
42
                        offsetof(struct thread_struct,vr[0])))
43
                return -EFAULT;
44
        data+=offsetof(struct thread_struct,vrsave[1])-
45
                offsetof(struct thread_struct,vr[0]);
46
        if (put_user(task->thread.vrsave[1],((u32 *)data)))
47
                return -EFAULT;
48
        return 0;
49
}
50
 
51
/*
52
 * Write contents of AltiVec register state into task TASK.
53
 */
54
static inline int set_vrregs32(struct task_struct *task, unsigned long data)
55
{
56
        if(copy_from_user(&task->thread.vr[0],(void *)data,
57
                        offsetof(struct thread_struct,vrsave)-
58
                          offsetof(struct thread_struct,vr[0])))
59
                return -EFAULT;
60
        data+=offsetof(struct thread_struct,vrsave[1])-
61
                offsetof(struct thread_struct,vr[0]);
62
        if (get_user(task->thread.vrsave[1],((u32 *)data)))
63
                return -EFAULT;
64
        return 0;
65
}
66
#endif
67
 
68
/*
69
 * Set of msr bits that gdb can change on behalf of a process.
70
 */
71
#define MSR_DEBUGCHANGE (MSR_FE0 | MSR_SE | MSR_BE | MSR_FE1)
72
 
73
/*
74
 * does not yet catch signals sent when the child dies.
75
 * in exit.c or in signal.c.
76
 */
77
 
78
/*
79
 * Get contents of register REGNO in task TASK.
80
 */
81
static inline unsigned long get_reg(struct task_struct *task, int regno)
82
{
83
        if (regno < sizeof(struct pt_regs) / sizeof(unsigned long))
84
                return ((unsigned long *)task->thread.regs)[regno];
85
        return (0);
86
}
87
 
88
/*
89
 * Write contents of register REGNO in task TASK.
90
 * (Put DATA into task TASK's register REGNO.)
91
 */
92
static inline int put_reg(struct task_struct *task, int regno, unsigned long data)
93
{
94
        if (regno < PT_SOFTE)
95
  {
96
                if (regno == PT_MSR)
97
                        data = (data & MSR_DEBUGCHANGE) | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
98
                ((unsigned long *)task->thread.regs)[regno] = data;
99
                return 0;
100
        }
101
        return -EIO;
102
}
103
 
104
static inline void
105
set_single_step(struct task_struct *task)
106
{
107
        struct pt_regs *regs = task->thread.regs;
108
        if (regs != NULL)
109
                regs->msr |= MSR_SE;
110
}
111
 
112
static inline void
113
clear_single_step(struct task_struct *task)
114
{
115
        struct pt_regs *regs = task->thread.regs;
116
        if (regs != NULL)
117
                regs->msr &= ~MSR_SE;
118
}
119
 
120
int sys32_ptrace(long request, long pid, unsigned long addr, unsigned long data)
121
{
122
        struct task_struct *child;
123
        int ret = -EPERM;
124
 
125
        lock_kernel();
126
        if (request == PTRACE_TRACEME) {
127
                /* are we already being traced? */
128
                if (current->ptrace & PT_PTRACED)
129
                        goto out;
130
                /* set the ptrace bit in the process flags. */
131
                current->ptrace |= PT_PTRACED;
132
                ret = 0;
133
                goto out;
134
        }
135
        ret = -ESRCH;
136
        read_lock(&tasklist_lock);
137
        child = find_task_by_pid(pid);
138
        if (child)
139
                get_task_struct(child);
140
        read_unlock(&tasklist_lock);
141
        if (!child)
142
                goto out;
143
 
144
        ret = -EPERM;
145
        if (pid == 1)           /* you may not mess with init */
146
                goto out_tsk;
147
 
148
        if (request == PTRACE_ATTACH) {
149
                ret = ptrace_attach(child);
150
                goto out_tsk;
151
        }
152
 
153
        ret = ptrace_check_attach(child, request == PTRACE_KILL);
154
        if (ret < 0)
155
                goto out_tsk;
156
 
157
        switch (request) {
158
        /* Read word at location ADDR */
159
        /* when I and D space are separate, these will need to be fixed. */
160
        case PTRACE_PEEKTEXT: /* read word at location addr. */
161
        case PTRACE_PEEKDATA:
162
        {
163
                unsigned int  tmp_mem_value;
164
                int copied;
165
 
166
                copied = access_process_vm(child, addr, &tmp_mem_value, sizeof(tmp_mem_value), 0);
167
                ret = -EIO;
168
                if (copied != sizeof(tmp_mem_value))
169
                        break;
170
                ret = put_user(tmp_mem_value, (u32*)data);  // copy 4 bytes of data into the user location specified by the 8 byte pointer in "data".
171
                break;
172
        }
173
 
174
        /*
175
         * Read 4 bytes of the other process' storage
176
         *  data is a pointer specifying where the user wants the
177
         *      4 bytes copied into
178
         *  addr is a pointer in the user's storage that contains an 8 byte
179
         *      address in the other process of the 4 bytes that is to be read
180
         * (this is run in a 32-bit process looking at a 64-bit process)
181
         * when I and D space are separate, these will need to be fixed.
182
         */
183
        case PPC_PTRACE_PEEKTEXT_3264:
184
        case PPC_PTRACE_PEEKDATA_3264:
185
        {
186
                u32  tmp_mem_value;
187
                int  copied;
188
                u32* addrOthers;
189
 
190
                ret = -EIO;
191
 
192
                /* Get the addr in the other process that we want to read */
193
                if (get_user(addrOthers, (u32**)addr) != 0)
194
                        break;
195
 
196
                copied = access_process_vm(child, (u64)addrOthers, &tmp_mem_value, sizeof(tmp_mem_value), 0);
197
                if (copied != sizeof(tmp_mem_value))
198
                        break;
199
                ret = put_user(tmp_mem_value, (u32*)data);  // copy 4 bytes of data into the user location specified by the 8 byte pointer in "data".
200
                break;
201
        }
202
 
203
        /* Read a register (specified by ADDR) out of the "user area" */
204
        case PTRACE_PEEKUSR: {
205
                int index;
206
                unsigned int reg32bits;
207
                unsigned long tmp_reg_value;
208
 
209
                ret = -EIO;
210
                /* convert to index and check */
211
                index = (unsigned long) addr >> 2;
212
                if ((addr & 3) || (index > PT_FPSCR32))
213
                        break;
214
 
215
                if (index < PT_FPR0) {
216
                        tmp_reg_value = get_reg(child,  index);
217
                } else {
218
                        if (child->thread.regs->msr & MSR_FP)
219
                                giveup_fpu(child);
220
                        /*
221
                         * the user space code considers the floating point
222
                         * to be an array of unsigned int (32 bits) - the
223
                         * index passed in is based on this assumption.
224
                         */
225
                        tmp_reg_value = ((unsigned int *)child->thread.fpr)[index - PT_FPR0];
226
                }
227
                reg32bits = tmp_reg_value;
228
                ret = put_user(reg32bits, (u32*)data);  // copy 4 bytes of data into the user location specified by the 8 byte pointer in "data".
229
                break;
230
        }
231
 
232
        /*
233
         * Read 4 bytes out of the other process' pt_regs area
234
         *  data is a pointer specifying where the user wants the
235
         *      4 bytes copied into
236
         *  addr is the offset into the other process' pt_regs structure
237
         *      that is to be read
238
         * (this is run in a 32-bit process looking at a 64-bit process)
239
         */
240
        case PPC_PTRACE_PEEKUSR_3264: {
241
                u32 index;
242
                u32 reg32bits;
243
                u64 tmp_reg_value;
244
                u32 numReg;
245
                u32 part;
246
 
247
                ret = -EIO;
248
                /* Determine which register the user wants */
249
                index = (u64)addr >> 2;  /* Divide addr by 4 */
250
                numReg = index / 2;
251
                /* Determine which part of the register the user wants */
252
                if (index % 2)
253
                        part = 1;  /* want the 2nd half of the register (right-most). */
254
                else
255
                        part = 0;  /* want the 1st half of the register (left-most). */
256
 
257
                /* Validate the input - check to see if address is on the wrong boundary or beyond the end of the user area */
258
                if ((addr & 3) || numReg > PT_FPSCR)
259
                        break;
260
 
261
                if (numReg >= PT_FPR0) {
262
                        if (child->thread.regs->msr & MSR_FP)
263
                                giveup_fpu(child);
264
                        if (numReg == PT_FPSCR)
265
                                tmp_reg_value = ((unsigned long *)child->thread.fpscr);
266
                        else
267
                                tmp_reg_value = ((unsigned long *)child->thread.fpr)[numReg - PT_FPR0];
268
                } else { /* register within PT_REGS struct */
269
                    tmp_reg_value = get_reg(child, numReg);
270
                }
271
                reg32bits = ((u32*)&tmp_reg_value)[part];
272
                ret = put_user(reg32bits, (u32*)data);  /* copy 4 bytes of data into the user location specified by the 8 byte pointer in "data". */
273
                break;
274
        }
275
 
276
        /* Write the word at location ADDR */
277
        /* If I and D space are separate, this will have to be fixed. */
278
        case PTRACE_POKETEXT: /* write the word at location addr. */
279
        case PTRACE_POKEDATA: {
280
                unsigned int tmp_value_to_write;
281
                tmp_value_to_write =  data;
282
                ret = 0;
283
                if (access_process_vm(child, addr, &tmp_value_to_write, sizeof(tmp_value_to_write), 1) == sizeof(tmp_value_to_write))
284
                        break;
285
                ret = -EIO;
286
                break;
287
        }
288
 
289
        /*
290
         * Write 4 bytes into the other process' storage
291
         *  data is the 4 bytes that the user wants written
292
         *  addr is a pointer in the user's storage that contains an
293
         *      8 byte address in the other process where the 4 bytes
294
         *      that is to be written
295
         * (this is run in a 32-bit process looking at a 64-bit process)
296
         * when I and D space are separate, these will need to be fixed.
297
         */
298
        case PPC_PTRACE_POKETEXT_3264:
299
        case PPC_PTRACE_POKEDATA_3264:
300
        {
301
                u32  tmp_value_to_write = data;
302
                u32* addrOthers;
303
                int  bytesWritten;
304
 
305
                /* Get the addr in the other process that we want to write into */
306
                ret = -EIO;
307
                if (get_user(addrOthers,(u32**)addr) != 0)
308
                        break;
309
                ret = 0;
310
                bytesWritten = access_process_vm(child, (u64)addrOthers, &tmp_value_to_write, sizeof(tmp_value_to_write), 1);
311
                if (bytesWritten == sizeof(tmp_value_to_write))
312
                        break;
313
                ret = -EIO;
314
                break;
315
        }
316
 
317
        /* Write DATA into location ADDR within the USER area  */
318
        case PTRACE_POKEUSR: {
319
                unsigned long index;
320
 
321
                ret = -EIO;
322
                /* convert to index and check */
323
                index = (unsigned long) addr >> 2;
324
                if ((addr & 3) || (index > PT_FPSCR32))
325
                        break;
326
 
327
                if (index == PT_ORIG_R3)
328
                        break;
329
                if (index < PT_FPR0) {
330
                        ret = put_reg(child, index, data);
331
                } else {
332
                        if (child->thread.regs->msr & MSR_FP)
333
                                giveup_fpu(child);
334
                        /*
335
                         * the user space code considers the floating point
336
                         * to be an array of unsigned int (32 bits) - the
337
                         * index passed in is based on this assumption.
338
                         */
339
                        ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data;
340
                        ret = 0;
341
                }
342
                break;
343
        }
344
 
345
        /*
346
         * Write 4 bytes into the other process' pt_regs area
347
         *  data is the 4 bytes that the user wants written
348
         *  addr is the offset into the other process' pt_regs structure
349
         *      that is to be written into
350
         * (this is run in a 32-bit process looking at a 64-bit process)
351
         */
352
        case PPC_PTRACE_POKEUSR_3264: {
353
                u32 index;
354
                u32 numReg;
355
 
356
                ret = -EIO;
357
                /* Determine which register the user wants */
358
                index = (u64)addr >> 2;  /* Divide addr by 4 */
359
                numReg = index / 2;
360
                /*
361
                 * Validate the input - check to see if address is on the
362
                 * wrong boundary or beyond the end of the user area
363
                 */
364
                if ((addr & 3) || (numReg > PT_FPSCR))
365
                        break;
366
                /* Insure it is a register we let them change */
367
                if ((numReg == PT_ORIG_R3)
368
                                || ((numReg > PT_CCR) && (numReg < PT_FPR0)))
369
                        break;
370
                if (numReg >= PT_FPR0) {
371
                        if (child->thread.regs->msr & MSR_FP)
372
                                giveup_fpu(child);
373
                }
374
                if (numReg == PT_MSR)
375
                        data = (data & MSR_DEBUGCHANGE)
376
                                | (child->thread.regs->msr & ~MSR_DEBUGCHANGE);
377
                ((u32*)child->thread.regs)[index] = data;
378
                ret = 0;
379
                break;
380
        }
381
 
382
        case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
383
        case PTRACE_CONT: { /* restart after signal. */
384
                ret = -EIO;
385
                if ((unsigned long) data > _NSIG)
386
                        break;
387
                if (request == PTRACE_SYSCALL)
388
                        child->ptrace |= PT_TRACESYS;
389
                else
390
                        child->ptrace &= ~PT_TRACESYS;
391
                child->exit_code = data;
392
                /* make sure the single step bit is not set. */
393
                clear_single_step(child);
394
                wake_up_process(child);
395
                ret = 0;
396
                break;
397
        }
398
 
399
        /*
400
         * make the child exit.  Best I can do is send it a sigkill.
401
         * perhaps it should be put in the status that it wants to
402
         * exit.
403
         */
404
        case PTRACE_KILL: {
405
                ret = 0;
406
                if (child->state == TASK_ZOMBIE)        /* already dead */
407
                        break;
408
                child->exit_code = SIGKILL;
409
                /* make sure the single step bit is not set. */
410
                clear_single_step(child);
411
                wake_up_process(child);
412
                break;
413
        }
414
 
415
        case PTRACE_SINGLESTEP: {  /* set the trap flag. */
416
                ret = -EIO;
417
                if ((unsigned long) data > _NSIG)
418
                        break;
419
                child->ptrace &= ~PT_TRACESYS;
420
                set_single_step(child);
421
                child->exit_code = data;
422
                /* give it a chance to run. */
423
                wake_up_process(child);
424
                ret = 0;
425
                break;
426
        }
427
 
428
        case PTRACE_DETACH:
429
                ret = ptrace_detach(child, data);
430
                break;
431
 
432
        case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
433
                int i;
434
                unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
435
                unsigned int *tmp = (unsigned int *)addr;
436
 
437
                for (i = 0; i < 32; i++) {
438
                        ret = put_user(*reg, tmp);
439
                        if (ret)
440
                                break;
441
                        reg++;
442
                        tmp++;
443
                }
444
                break;
445
        }
446
 
447
        case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
448
                int i;
449
                unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
450
                unsigned int *tmp = (unsigned int *)addr;
451
 
452
                for (i = 0; i < 32; i++) {
453
                        ret = get_user(*reg, tmp);
454
                        if (ret)
455
                                break;
456
                        reg++;
457
                        tmp++;
458
                }
459
                break;
460
        }
461
 
462
        case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
463
                int i;
464
                unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
465
                unsigned int *tmp = (unsigned int *)addr;
466
 
467
                if (child->thread.regs->msr & MSR_FP)
468
                        giveup_fpu(child);
469
 
470
                for (i = 0; i < 32; i++) {
471
                        ret = put_user(*reg, tmp);
472
                        if (ret)
473
                                break;
474
                        reg++;
475
                        tmp++;
476
                }
477
                break;
478
        }
479
 
480
        case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
481
                int i;
482
                unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
483
                unsigned int *tmp = (unsigned int *)addr;
484
 
485
                if (child->thread.regs->msr & MSR_FP)
486
                        giveup_fpu(child);
487
 
488
                for (i = 0; i < 32; i++) {
489
                        ret = get_user(*reg, tmp);
490
                        if (ret)
491
                                break;
492
                        reg++;
493
                        tmp++;
494
                }
495
                break;
496
        }
497
 
498
 
499
 
500
#ifdef CONFIG_ALTIVEC
501
        case PTRACE_GETVRREGS:
502
                /* Get the child altivec register state. */
503
                if (child->thread.regs->msr & MSR_VEC)
504
                        giveup_altivec(child);
505
                ret = get_vrregs32((unsigned long)data, child);
506
                break;
507
 
508
        case PTRACE_SETVRREGS:
509
                /* Set the child altivec register state. */
510
                /* this is to clear the MSR_VEC bit to force a reload
511
                 * of register state from memory */
512
                if (child->thread.regs->msr & MSR_VEC)
513
                        giveup_altivec(child);
514
                ret = set_vrregs32(child,(unsigned long)data);
515
                break;
516
#endif
517
        default:
518
                ret = -EIO;
519
                break;
520
        }
521
out_tsk:
522
        free_task_struct(child);
523
out:
524
        unlock_kernel();
525
        return ret;
526
}

powered by: WebSVN 2.1.0

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