[PATCH] mips: remove #include <linux/audit.h> two times
[safe/jmp/linux-2.6] / arch / mips / kernel / ptrace.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1992 Ross Biro
7  * Copyright (C) Linus Torvalds
8  * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
9  * Copyright (C) 1996 David S. Miller
10  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
11  * Copyright (C) 1999 MIPS Technologies, Inc.
12  * Copyright (C) 2000 Ulf Carlsson
13  *
14  * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
15  * binaries.
16  */
17 #include <linux/config.h>
18 #include <linux/compiler.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/mm.h>
22 #include <linux/errno.h>
23 #include <linux/ptrace.h>
24 #include <linux/audit.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/user.h>
28 #include <linux/security.h>
29
30 #include <asm/cpu.h>
31 #include <asm/fpu.h>
32 #include <asm/mipsregs.h>
33 #include <asm/pgtable.h>
34 #include <asm/page.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <asm/bootinfo.h>
38
39 /*
40  * Called by kernel/ptrace.c when detaching..
41  *
42  * Make sure single step bits etc are not set.
43  */
44 void ptrace_disable(struct task_struct *child)
45 {
46         /* Nothing to do.. */
47 }
48
49 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
50 {
51         struct task_struct *child;
52         int ret;
53
54 #if 0
55         printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
56                (int) request, (int) pid, (unsigned long) addr,
57                (unsigned long) data);
58 #endif
59         lock_kernel();
60         ret = -EPERM;
61         if (request == PTRACE_TRACEME) {
62                 /* are we already being traced? */
63                 if (current->ptrace & PT_PTRACED)
64                         goto out;
65                 if ((ret = security_ptrace(current->parent, current)))
66                         goto out;
67                 /* set the ptrace bit in the process flags. */
68                 current->ptrace |= PT_PTRACED;
69                 ret = 0;
70                 goto out;
71         }
72         ret = -ESRCH;
73         read_lock(&tasklist_lock);
74         child = find_task_by_pid(pid);
75         if (child)
76                 get_task_struct(child);
77         read_unlock(&tasklist_lock);
78         if (!child)
79                 goto out;
80
81         ret = -EPERM;
82         if (pid == 1)           /* you may not mess with init */
83                 goto out_tsk;
84
85         if (request == PTRACE_ATTACH) {
86                 ret = ptrace_attach(child);
87                 goto out_tsk;
88         }
89
90         ret = ptrace_check_attach(child, request == PTRACE_KILL);
91         if (ret < 0)
92                 goto out_tsk;
93
94         switch (request) {
95         /* when I and D space are separate, these will need to be fixed. */
96         case PTRACE_PEEKTEXT: /* read word at location addr. */
97         case PTRACE_PEEKDATA: {
98                 unsigned long tmp;
99                 int copied;
100
101                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
102                 ret = -EIO;
103                 if (copied != sizeof(tmp))
104                         break;
105                 ret = put_user(tmp,(unsigned long *) data);
106                 break;
107         }
108
109         /* Read the word at location addr in the USER area. */
110         case PTRACE_PEEKUSR: {
111                 struct pt_regs *regs;
112                 unsigned long tmp = 0;
113
114                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
115                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
116                 ret = 0;  /* Default return value. */
117
118                 switch (addr) {
119                 case 0 ... 31:
120                         tmp = regs->regs[addr];
121                         break;
122                 case FPR_BASE ... FPR_BASE + 31:
123                         if (tsk_used_math(child)) {
124                                 fpureg_t *fregs = get_fpu_regs(child);
125
126 #ifdef CONFIG_MIPS32
127                                 /*
128                                  * The odd registers are actually the high
129                                  * order bits of the values stored in the even
130                                  * registers - unless we're using r2k_switch.S.
131                                  */
132                                 if (addr & 1)
133                                         tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
134                                 else
135                                         tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
136 #endif
137 #ifdef CONFIG_MIPS64
138                                 tmp = fregs[addr - FPR_BASE];
139 #endif
140                         } else {
141                                 tmp = -1;       /* FP not yet used  */
142                         }
143                         break;
144                 case PC:
145                         tmp = regs->cp0_epc;
146                         break;
147                 case CAUSE:
148                         tmp = regs->cp0_cause;
149                         break;
150                 case BADVADDR:
151                         tmp = regs->cp0_badvaddr;
152                         break;
153                 case MMHI:
154                         tmp = regs->hi;
155                         break;
156                 case MMLO:
157                         tmp = regs->lo;
158                         break;
159                 case FPC_CSR:
160                         if (cpu_has_fpu)
161                                 tmp = child->thread.fpu.hard.fcr31;
162                         else
163                                 tmp = child->thread.fpu.soft.fcr31;
164                         break;
165                 case FPC_EIR: { /* implementation / version register */
166                         unsigned int flags;
167
168                         if (!cpu_has_fpu)
169                                 break;
170
171                         flags = read_c0_status();
172                         __enable_fpu();
173                         __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
174                         write_c0_status(flags);
175                         break;
176                 }
177                 default:
178                         tmp = 0;
179                         ret = -EIO;
180                         goto out_tsk;
181                 }
182                 ret = put_user(tmp, (unsigned long *) data);
183                 break;
184         }
185
186         /* when I and D space are separate, this will have to be fixed. */
187         case PTRACE_POKETEXT: /* write the word at location addr. */
188         case PTRACE_POKEDATA:
189                 ret = 0;
190                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
191                     == sizeof(data))
192                         break;
193                 ret = -EIO;
194                 break;
195
196         case PTRACE_POKEUSR: {
197                 struct pt_regs *regs;
198                 ret = 0;
199                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
200                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
201
202                 switch (addr) {
203                 case 0 ... 31:
204                         regs->regs[addr] = data;
205                         break;
206                 case FPR_BASE ... FPR_BASE + 31: {
207                         fpureg_t *fregs = get_fpu_regs(child);
208
209                         if (!tsk_used_math(child)) {
210                                 /* FP not yet used  */
211                                 memset(&child->thread.fpu.hard, ~0,
212                                        sizeof(child->thread.fpu.hard));
213                                 child->thread.fpu.hard.fcr31 = 0;
214                         }
215 #ifdef CONFIG_MIPS32
216                         /*
217                          * The odd registers are actually the high order bits
218                          * of the values stored in the even registers - unless
219                          * we're using r2k_switch.S.
220                          */
221                         if (addr & 1) {
222                                 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
223                                 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
224                         } else {
225                                 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
226                                 fregs[addr - FPR_BASE] |= data;
227                         }
228 #endif
229 #ifdef CONFIG_MIPS64
230                         fregs[addr - FPR_BASE] = data;
231 #endif
232                         break;
233                 }
234                 case PC:
235                         regs->cp0_epc = data;
236                         break;
237                 case MMHI:
238                         regs->hi = data;
239                         break;
240                 case MMLO:
241                         regs->lo = data;
242                         break;
243                 case FPC_CSR:
244                         if (cpu_has_fpu)
245                                 child->thread.fpu.hard.fcr31 = data;
246                         else
247                                 child->thread.fpu.soft.fcr31 = data;
248                         break;
249                 default:
250                         /* The rest are not allowed. */
251                         ret = -EIO;
252                         break;
253                 }
254                 break;
255                 }
256
257         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
258         case PTRACE_CONT: { /* restart after signal. */
259                 ret = -EIO;
260                 if ((unsigned long) data > _NSIG)
261                         break;
262                 if (request == PTRACE_SYSCALL) {
263                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
264                 }
265                 else {
266                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
267                 }
268                 child->exit_code = data;
269                 wake_up_process(child);
270                 ret = 0;
271                 break;
272         }
273
274         /*
275          * make the child exit.  Best I can do is send it a sigkill.
276          * perhaps it should be put in the status that it wants to
277          * exit.
278          */
279         case PTRACE_KILL:
280                 ret = 0;
281                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
282                         break;
283                 child->exit_code = SIGKILL;
284                 wake_up_process(child);
285                 break;
286
287         case PTRACE_DETACH: /* detach a process that was attached. */
288                 ret = ptrace_detach(child, data);
289                 break;
290
291         default:
292                 ret = ptrace_request(child, request, addr, data);
293                 break;
294         }
295
296 out_tsk:
297         put_task_struct(child);
298 out:
299         unlock_kernel();
300         return ret;
301 }
302
303 /*
304  * Notification of system call entry/exit
305  * - triggered by current->work.syscall_trace
306  */
307 asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
308 {
309         if (unlikely(current->audit_context)) {
310                 if (!entryexit)
311                         audit_syscall_entry(current, regs->regs[2],
312                                             regs->regs[4], regs->regs[5],
313                                             regs->regs[6], regs->regs[7]);
314                 else
315                         audit_syscall_exit(current, regs->regs[2]);
316         }
317
318         if (!test_thread_flag(TIF_SYSCALL_TRACE))
319                 return;
320         if (!(current->ptrace & PT_PTRACED))
321                 return;
322
323         /* The 0x80 provides a way for the tracing parent to distinguish
324            between a syscall stop and SIGTRAP delivery */
325         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
326                                  0x80 : 0));
327
328         /*
329          * this isn't the same as continuing with a signal, but it will do
330          * for normal use.  strace only continues with a signal if the
331          * stopping signal is not SIGTRAP.  -brl
332          */
333         if (current->exit_code) {
334                 send_sig(current->exit_code, current, 1);
335                 current->exit_code = 0;
336         }
337 }