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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [arch/] [or32/] [kernel/] [ptrace.c] - Blame information for rev 666

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 666 simons
/*
2
 *  linux/arch/m68knommu/kernel/ptrace.c
3
 *
4
 *  Copyright (C) 1998  D. Jeff Dionne <jeff@ryeham.ee.ryerson.ca>,
5
 *                      Kenneth Albanowski <kjahds@kjahds.com>,
6
 *                      The Silver Hammer Group, Ltd.
7
 *
8
 *  Based on:
9
 *
10
 *  linux/arch/m68k/kernel/ptrace
11
 *
12
 *  Copyright (C) 1994 by Hamish Macdonald
13
 *  Taken from linux/kernel/ptrace.c and modified for M680x0.
14
 *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
15
 *
16
 * This file is subject to the terms and conditions of the GNU General
17
 * Public License.  See the file COPYING in the main directory of
18
 * this archive for more details.
19
 */
20
 
21
#include <stddef.h>
22
#include <linux/kernel.h>
23
#include <linux/sched.h>
24
#include <linux/string.h>
25
 
26
#include <linux/mm.h>
27
#include <linux/errno.h>
28
#include <linux/ptrace.h>
29
#include <linux/user.h>
30
 
31
#include <asm/segment.h>
32
#include <asm/page.h>
33
#include <asm/pgtable.h>
34
#include <asm/system.h>
35
 
36
/*
37
 * does not yet catch signals sent when the child dies.
38
 * in exit.c or in signal.c.
39
 */
40
 
41
/* determines which bits in the SR the user has access to. */
42
/* 1 = access 0 = no access */
43
#define SR_MASK 0x001f
44
 
45
/* sets the trace bits. */
46
#define TRACE_BITS 0x8000
47
 
48
/* Find the stack offset for a register, relative to tss.esp0. */
49
#define PT_REG(reg)     ((long)&((struct pt_regs *)0)->reg)
50
#define SW_REG(reg)     ((long)&((struct switch_stack *)0)->reg \
51
                         - sizeof(struct switch_stack))
52
/* Mapping from PT_xxx to the stack offset at which the register is
53
   saved.  Notice that usp has no stack-slot and needs to be treated
54
   specially (see get_reg/put_reg below). */
55
static int regoff[] = {0,0,0,0,0,0};
56
 
57
/* change a pid into a task struct. */
58
static inline struct task_struct * get_task(int pid)
59
{
60
        int i;
61
 
62
        for (i = 1; i < NR_TASKS; i++) {
63
                if (task[i] != NULL && (task[i]->pid == pid))
64
                        return task[i];
65
        }
66
        return NULL;
67
}
68
 
69
/*
70
 * Get contents of register REGNO in task TASK.
71
 */
72
static inline long get_reg(struct task_struct *task, int regno)
73
{
74
        unsigned long *addr;
75
/* SIMON */
76
#if 0
77
        if (regno == PT_USP)
78
                addr = &task->tss.usp;
79
        else if (regno < sizeof(regoff)/sizeof(regoff[0]))
80
                addr = (unsigned long *)(task->tss.esp0 + regoff[regno]);
81
        else
82
                return 0;
83
#endif
84
        return *addr;
85
}
86
 
87
/*
88
 * Write contents of register REGNO in task TASK.
89
 */
90
static inline int put_reg(struct task_struct *task, int regno,
91
                          unsigned long data)
92
{
93
/* SIMON */
94
#if 0
95
        unsigned long *addr;
96
        if (regno == PT_USP)
97
                addr = &task->tss.usp;
98
        else if (regno < sizeof(regoff)/sizeof(regoff[0]))
99
                addr = (unsigned long *) (task->tss.esp0 + regoff[regno]);
100
        else
101
                return -1;
102
        *addr = data;
103
#endif
104
        return 0;
105
}
106
 
107
inline
108
static unsigned long get_long(struct task_struct * tsk,
109
        struct vm_area_struct * vma, unsigned long addr)
110
{
111
        return *(unsigned long*)addr;
112
}
113
 
114
inline
115
static void put_long(struct task_struct * tsk, struct vm_area_struct * vma, unsigned long addr,
116
        unsigned long data)
117
{
118
        *(unsigned long*)addr = data;
119
}
120
 
121
 
122
inline
123
static int read_long(struct task_struct * tsk, unsigned long addr,
124
        unsigned long * result)
125
{
126
        *result = *(unsigned long *)addr;
127
        return 0;
128
}
129
 
130
inline
131
static int write_long(struct task_struct * tsk, unsigned long addr,
132
        unsigned long data)
133
{
134
        *(unsigned long *)addr = data;
135
        return 0;
136
}
137
 
138
asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
139
{
140
/* SIMON */
141
#if 0
142
        struct task_struct *child;
143
        struct user * dummy;
144
        dummy = NULL;
145
 
146
        if (request == PTRACE_TRACEME) {
147
                /* are we already being traced? */
148
                if (current->flags & PF_PTRACED)
149
                        return -EPERM;
150
                /* set the ptrace bit in the process flags. */
151
                current->flags |= PF_PTRACED;
152
                return 0;
153
        }
154
        if (pid == 1)           /* you may not mess with init */
155
                return -EPERM;
156
        if (!(child = get_task(pid)))
157
                return -ESRCH;
158
        if (request == PTRACE_ATTACH) {
159
                if (child == current)
160
                        return -EPERM;
161
                if ((!child->dumpable ||
162
                    (current->uid != child->euid) ||
163
                    (current->uid != child->suid) ||
164
                    (current->uid != child->uid) ||
165
                    (current->gid != child->egid) ||
166
                    (current->gid != child->sgid) ||
167
                    (current->gid != child->gid)) && !suser())
168
                        return -EPERM;
169
                /* the same process cannot be attached many times */
170
                if (child->flags & PF_PTRACED)
171
                        return -EPERM;
172
                child->flags |= PF_PTRACED;
173
                if (child->p_pptr != current) {
174
                        REMOVE_LINKS(child);
175
                        child->p_pptr = current;
176
                        SET_LINKS(child);
177
                }
178
                send_sig(SIGSTOP, child, 1);
179
                return 0;
180
        }
181
        if (!(child->flags & PF_PTRACED))
182
                return -ESRCH;
183
        if (child->state != TASK_STOPPED) {
184
                if (request != PTRACE_KILL)
185
                        return -ESRCH;
186
        }
187
        if (child->p_pptr != current)
188
                return -ESRCH;
189
 
190
        switch (request) {
191
        /* when I and D space are separate, these will need to be fixed. */
192
                case PTRACE_PEEKTEXT: /* read word at location addr. */
193
                case PTRACE_PEEKDATA: {
194
                        unsigned long tmp;
195
                        int res;
196
 
197
                        res = read_long(child, addr, &tmp);
198
                        if (res < 0)
199
                                return res;
200
                        res = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
201
                        if (!res)
202
                                put_user(tmp, (unsigned long *) data);
203
                        return res;
204
                }
205
 
206
        /* read the word at location addr in the USER area. */
207
                case PTRACE_PEEKUSR: {
208
                        unsigned long tmp;
209
                        int res;
210
 
211
                        if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
212
                                return -EIO;
213
 
214
                        res = verify_area(VERIFY_WRITE, (void *) data,
215
                                          sizeof(long));
216
                        if (res)
217
                                return res;
218
                        tmp = 0;  /* Default return condition */
219
                        addr = addr >> 2; /* temporary hack. */
220
                        if (addr < 19) {
221
                                tmp = get_reg(child, addr);
222
                                if (addr == PT_SR)
223
                                        tmp >>= 16;
224
                        }
225
                        else if (addr >= 21 && addr < 49)
226
                                tmp = child->tss.fp[addr - 21];
227
                        else if (addr == 49)
228
                                tmp = child->mm->start_code;
229
                        else if (addr == 50)
230
                                tmp = child->mm->start_data;
231
                        else
232
                                return -EIO;
233
                        put_user(tmp,(unsigned long *) data);
234
                        return 0;
235
                }
236
 
237
      /* when I and D space are separate, this will have to be fixed. */
238
                case PTRACE_POKETEXT: /* write the word at location addr. */
239
                case PTRACE_POKEDATA:
240
                        return write_long(child,addr,data);
241
 
242
                case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
243
                        if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
244
                                return -EIO;
245
 
246
                        addr = addr >> 2; /* temporary hack. */
247
 
248
                        if (addr == PT_ORIG_D0)
249
                                return -EIO;
250
                        if (addr == PT_SR) {
251
                                data &= SR_MASK;
252
                                data <<= 16;
253
                                data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
254
                        }
255
                        if (addr < 19) {
256
                                if (put_reg(child, addr, data))
257
                                        return -EIO;
258
                                return 0;
259
                        }
260
                        if (addr >= 21 && addr < 48)
261
                        {
262
                                child->tss.fp[addr - 21] = data;
263
                                return 0;
264
                        }
265
                        return -EIO;
266
 
267
                case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
268
                case PTRACE_CONT: { /* restart after signal. */
269
                        long tmp;
270
 
271
                        if ((unsigned long) data >= NSIG)
272
                                return -EIO;
273
                        if (request == PTRACE_SYSCALL)
274
                                child->flags |= PF_TRACESYS;
275
                        else
276
                                child->flags &= ~PF_TRACESYS;
277
                        child->exit_code = data;
278
                        wake_up_process(child);
279
                        /* make sure the single step bit is not set. */
280
                        tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
281
                        put_reg(child, PT_SR, tmp);
282
                        return 0;
283
                }
284
 
285
/*
286
 * make the child exit.  Best I can do is send it a sigkill.
287
 * perhaps it should be put in the status that it wants to
288
 * exit.
289
 */
290
                case PTRACE_KILL: {
291
                        long tmp;
292
 
293
                        if (child->state == TASK_ZOMBIE) /* already dead */
294
                                return 0;
295
                        wake_up_process(child);
296
                        child->exit_code = SIGKILL;
297
        /* make sure the single step bit is not set. */
298
                        tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
299
                        put_reg(child, PT_SR, tmp);
300
                        return 0;
301
                }
302
 
303
                case PTRACE_SINGLESTEP: {  /* set the trap flag. */
304
                        long tmp;
305
 
306
                        if ((unsigned long) data >= NSIG)
307
                                return -EIO;
308
                        child->flags &= ~PF_TRACESYS;
309
                        tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
310
                        put_reg(child, PT_SR, tmp);
311
 
312
                        wake_up_process(child);
313
                        child->exit_code = data;
314
        /* give it a chance to run. */
315
                        return 0;
316
                }
317
 
318
                case PTRACE_DETACH: { /* detach a process that was attached. */
319
                        long tmp;
320
 
321
                        if ((unsigned long) data >= NSIG)
322
                                return -EIO;
323
                        child->flags &= ~(PF_PTRACED|PF_TRACESYS);
324
                        wake_up_process(child);
325
                        child->exit_code = data;
326
                        REMOVE_LINKS(child);
327
                        child->p_pptr = child->p_opptr;
328
                        SET_LINKS(child);
329
                        /* make sure the single step bit is not set. */
330
                        tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
331
                        put_reg(child, PT_SR, tmp);
332
                        return 0;
333
                }
334
 
335
                default:
336
                        return -EIO;
337
        }
338
#endif
339
        return 0;
340
}
341
 
342
asmlinkage void syscall_trace(void)
343
{
344
        if ((current->flags & (PF_PTRACED|PF_TRACESYS))
345
                        != (PF_PTRACED|PF_TRACESYS))
346
                return;
347
        current->exit_code = SIGTRAP;
348
        current->state = TASK_STOPPED;
349
        notify_parent(current, SIGCHLD);
350
        schedule();
351
        /*
352
         * this isn't the same as continuing with a signal, but it will do
353
         * for normal use.  strace only continues with a signal if the
354
         * stopping signal is not SIGTRAP.  -brl
355
         */
356
        if (current->exit_code)
357
                current->signal |= (1 << (current->exit_code - 1));
358
        current->exit_code = 0;
359
        return;
360
}

powered by: WebSVN 2.1.0

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