47415671da0cbc3a8877ab7167802a4d29b20a9f
[safe/jmp/linux-2.6] / arch / sh / kernel / process_64.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  * arch/sh64/kernel/process.c
7  *
8  * Copyright (C) 2000, 2001  Paolo Alberelli
9  * Copyright (C) 2003  Paul Mundt
10  * Copyright (C) 2003, 2004 Richard Curnow
11  *
12  * Started from SH3/4 version:
13  *   Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
14  *
15  *   In turn started from i386 version:
16  *     Copyright (C) 1995  Linus Torvalds
17  *
18  */
19
20 /*
21  * This file handles the architecture-dependent parts of process handling..
22  */
23 #include <linux/mm.h>
24 #include <linux/fs.h>
25 #include <linux/ptrace.h>
26 #include <linux/reboot.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/proc_fs.h>
30 #include <linux/io.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgtable.h>
33
34 struct task_struct *last_task_used_math = NULL;
35
36 static int hlt_counter = 1;
37
38 #define HARD_IDLE_TIMEOUT (HZ / 3)
39
40 void disable_hlt(void)
41 {
42         hlt_counter++;
43 }
44
45 void enable_hlt(void)
46 {
47         hlt_counter--;
48 }
49
50 static int __init nohlt_setup(char *__unused)
51 {
52         hlt_counter = 1;
53         return 1;
54 }
55
56 static int __init hlt_setup(char *__unused)
57 {
58         hlt_counter = 0;
59         return 1;
60 }
61
62 __setup("nohlt", nohlt_setup);
63 __setup("hlt", hlt_setup);
64
65 static inline void hlt(void)
66 {
67         __asm__ __volatile__ ("sleep" : : : "memory");
68 }
69
70 /*
71  * The idle loop on a uniprocessor SH..
72  */
73 void cpu_idle(void)
74 {
75         /* endless idle loop with no priority at all */
76         while (1) {
77                 if (hlt_counter) {
78                         while (!need_resched())
79                                 cpu_relax();
80                 } else {
81                         local_irq_disable();
82                         while (!need_resched()) {
83                                 local_irq_enable();
84                                 hlt();
85                                 local_irq_disable();
86                         }
87                         local_irq_enable();
88                 }
89                 preempt_enable_no_resched();
90                 schedule();
91                 preempt_disable();
92         }
93
94 }
95
96 void machine_restart(char * __unused)
97 {
98         extern void phys_stext(void);
99
100         phys_stext();
101 }
102
103 void machine_halt(void)
104 {
105         for (;;);
106 }
107
108 void machine_power_off(void)
109 {
110 #if 0
111         /* Disable watchdog timer */
112         ctrl_outl(0xa5000000, WTCSR);
113         /* Configure deep standby on sleep */
114         ctrl_outl(0x03, STBCR);
115 #endif
116
117         __asm__ __volatile__ (
118                 "sleep\n\t"
119                 "synci\n\t"
120                 "nop;nop;nop;nop\n\t"
121         );
122
123         panic("Unexpected wakeup!\n");
124 }
125
126 void (*pm_power_off)(void) = machine_power_off;
127 EXPORT_SYMBOL(pm_power_off);
128
129 void show_regs(struct pt_regs * regs)
130 {
131         unsigned long long ah, al, bh, bl, ch, cl;
132
133         printk("\n");
134
135         ah = (regs->pc) >> 32;
136         al = (regs->pc) & 0xffffffff;
137         bh = (regs->regs[18]) >> 32;
138         bl = (regs->regs[18]) & 0xffffffff;
139         ch = (regs->regs[15]) >> 32;
140         cl = (regs->regs[15]) & 0xffffffff;
141         printk("PC  : %08Lx%08Lx LINK: %08Lx%08Lx SP  : %08Lx%08Lx\n",
142                ah, al, bh, bl, ch, cl);
143
144         ah = (regs->sr) >> 32;
145         al = (regs->sr) & 0xffffffff;
146         asm volatile ("getcon   " __TEA ", %0" : "=r" (bh));
147         asm volatile ("getcon   " __TEA ", %0" : "=r" (bl));
148         bh = (bh) >> 32;
149         bl = (bl) & 0xffffffff;
150         asm volatile ("getcon   " __KCR0 ", %0" : "=r" (ch));
151         asm volatile ("getcon   " __KCR0 ", %0" : "=r" (cl));
152         ch = (ch) >> 32;
153         cl = (cl) & 0xffffffff;
154         printk("SR  : %08Lx%08Lx TEA : %08Lx%08Lx KCR0: %08Lx%08Lx\n",
155                ah, al, bh, bl, ch, cl);
156
157         ah = (regs->regs[0]) >> 32;
158         al = (regs->regs[0]) & 0xffffffff;
159         bh = (regs->regs[1]) >> 32;
160         bl = (regs->regs[1]) & 0xffffffff;
161         ch = (regs->regs[2]) >> 32;
162         cl = (regs->regs[2]) & 0xffffffff;
163         printk("R0  : %08Lx%08Lx R1  : %08Lx%08Lx R2  : %08Lx%08Lx\n",
164                ah, al, bh, bl, ch, cl);
165
166         ah = (regs->regs[3]) >> 32;
167         al = (regs->regs[3]) & 0xffffffff;
168         bh = (regs->regs[4]) >> 32;
169         bl = (regs->regs[4]) & 0xffffffff;
170         ch = (regs->regs[5]) >> 32;
171         cl = (regs->regs[5]) & 0xffffffff;
172         printk("R3  : %08Lx%08Lx R4  : %08Lx%08Lx R5  : %08Lx%08Lx\n",
173                ah, al, bh, bl, ch, cl);
174
175         ah = (regs->regs[6]) >> 32;
176         al = (regs->regs[6]) & 0xffffffff;
177         bh = (regs->regs[7]) >> 32;
178         bl = (regs->regs[7]) & 0xffffffff;
179         ch = (regs->regs[8]) >> 32;
180         cl = (regs->regs[8]) & 0xffffffff;
181         printk("R6  : %08Lx%08Lx R7  : %08Lx%08Lx R8  : %08Lx%08Lx\n",
182                ah, al, bh, bl, ch, cl);
183
184         ah = (regs->regs[9]) >> 32;
185         al = (regs->regs[9]) & 0xffffffff;
186         bh = (regs->regs[10]) >> 32;
187         bl = (regs->regs[10]) & 0xffffffff;
188         ch = (regs->regs[11]) >> 32;
189         cl = (regs->regs[11]) & 0xffffffff;
190         printk("R9  : %08Lx%08Lx R10 : %08Lx%08Lx R11 : %08Lx%08Lx\n",
191                ah, al, bh, bl, ch, cl);
192
193         ah = (regs->regs[12]) >> 32;
194         al = (regs->regs[12]) & 0xffffffff;
195         bh = (regs->regs[13]) >> 32;
196         bl = (regs->regs[13]) & 0xffffffff;
197         ch = (regs->regs[14]) >> 32;
198         cl = (regs->regs[14]) & 0xffffffff;
199         printk("R12 : %08Lx%08Lx R13 : %08Lx%08Lx R14 : %08Lx%08Lx\n",
200                ah, al, bh, bl, ch, cl);
201
202         ah = (regs->regs[16]) >> 32;
203         al = (regs->regs[16]) & 0xffffffff;
204         bh = (regs->regs[17]) >> 32;
205         bl = (regs->regs[17]) & 0xffffffff;
206         ch = (regs->regs[19]) >> 32;
207         cl = (regs->regs[19]) & 0xffffffff;
208         printk("R16 : %08Lx%08Lx R17 : %08Lx%08Lx R19 : %08Lx%08Lx\n",
209                ah, al, bh, bl, ch, cl);
210
211         ah = (regs->regs[20]) >> 32;
212         al = (regs->regs[20]) & 0xffffffff;
213         bh = (regs->regs[21]) >> 32;
214         bl = (regs->regs[21]) & 0xffffffff;
215         ch = (regs->regs[22]) >> 32;
216         cl = (regs->regs[22]) & 0xffffffff;
217         printk("R20 : %08Lx%08Lx R21 : %08Lx%08Lx R22 : %08Lx%08Lx\n",
218                ah, al, bh, bl, ch, cl);
219
220         ah = (regs->regs[23]) >> 32;
221         al = (regs->regs[23]) & 0xffffffff;
222         bh = (regs->regs[24]) >> 32;
223         bl = (regs->regs[24]) & 0xffffffff;
224         ch = (regs->regs[25]) >> 32;
225         cl = (regs->regs[25]) & 0xffffffff;
226         printk("R23 : %08Lx%08Lx R24 : %08Lx%08Lx R25 : %08Lx%08Lx\n",
227                ah, al, bh, bl, ch, cl);
228
229         ah = (regs->regs[26]) >> 32;
230         al = (regs->regs[26]) & 0xffffffff;
231         bh = (regs->regs[27]) >> 32;
232         bl = (regs->regs[27]) & 0xffffffff;
233         ch = (regs->regs[28]) >> 32;
234         cl = (regs->regs[28]) & 0xffffffff;
235         printk("R26 : %08Lx%08Lx R27 : %08Lx%08Lx R28 : %08Lx%08Lx\n",
236                ah, al, bh, bl, ch, cl);
237
238         ah = (regs->regs[29]) >> 32;
239         al = (regs->regs[29]) & 0xffffffff;
240         bh = (regs->regs[30]) >> 32;
241         bl = (regs->regs[30]) & 0xffffffff;
242         ch = (regs->regs[31]) >> 32;
243         cl = (regs->regs[31]) & 0xffffffff;
244         printk("R29 : %08Lx%08Lx R30 : %08Lx%08Lx R31 : %08Lx%08Lx\n",
245                ah, al, bh, bl, ch, cl);
246
247         ah = (regs->regs[32]) >> 32;
248         al = (regs->regs[32]) & 0xffffffff;
249         bh = (regs->regs[33]) >> 32;
250         bl = (regs->regs[33]) & 0xffffffff;
251         ch = (regs->regs[34]) >> 32;
252         cl = (regs->regs[34]) & 0xffffffff;
253         printk("R32 : %08Lx%08Lx R33 : %08Lx%08Lx R34 : %08Lx%08Lx\n",
254                ah, al, bh, bl, ch, cl);
255
256         ah = (regs->regs[35]) >> 32;
257         al = (regs->regs[35]) & 0xffffffff;
258         bh = (regs->regs[36]) >> 32;
259         bl = (regs->regs[36]) & 0xffffffff;
260         ch = (regs->regs[37]) >> 32;
261         cl = (regs->regs[37]) & 0xffffffff;
262         printk("R35 : %08Lx%08Lx R36 : %08Lx%08Lx R37 : %08Lx%08Lx\n",
263                ah, al, bh, bl, ch, cl);
264
265         ah = (regs->regs[38]) >> 32;
266         al = (regs->regs[38]) & 0xffffffff;
267         bh = (regs->regs[39]) >> 32;
268         bl = (regs->regs[39]) & 0xffffffff;
269         ch = (regs->regs[40]) >> 32;
270         cl = (regs->regs[40]) & 0xffffffff;
271         printk("R38 : %08Lx%08Lx R39 : %08Lx%08Lx R40 : %08Lx%08Lx\n",
272                ah, al, bh, bl, ch, cl);
273
274         ah = (regs->regs[41]) >> 32;
275         al = (regs->regs[41]) & 0xffffffff;
276         bh = (regs->regs[42]) >> 32;
277         bl = (regs->regs[42]) & 0xffffffff;
278         ch = (regs->regs[43]) >> 32;
279         cl = (regs->regs[43]) & 0xffffffff;
280         printk("R41 : %08Lx%08Lx R42 : %08Lx%08Lx R43 : %08Lx%08Lx\n",
281                ah, al, bh, bl, ch, cl);
282
283         ah = (regs->regs[44]) >> 32;
284         al = (regs->regs[44]) & 0xffffffff;
285         bh = (regs->regs[45]) >> 32;
286         bl = (regs->regs[45]) & 0xffffffff;
287         ch = (regs->regs[46]) >> 32;
288         cl = (regs->regs[46]) & 0xffffffff;
289         printk("R44 : %08Lx%08Lx R45 : %08Lx%08Lx R46 : %08Lx%08Lx\n",
290                ah, al, bh, bl, ch, cl);
291
292         ah = (regs->regs[47]) >> 32;
293         al = (regs->regs[47]) & 0xffffffff;
294         bh = (regs->regs[48]) >> 32;
295         bl = (regs->regs[48]) & 0xffffffff;
296         ch = (regs->regs[49]) >> 32;
297         cl = (regs->regs[49]) & 0xffffffff;
298         printk("R47 : %08Lx%08Lx R48 : %08Lx%08Lx R49 : %08Lx%08Lx\n",
299                ah, al, bh, bl, ch, cl);
300
301         ah = (regs->regs[50]) >> 32;
302         al = (regs->regs[50]) & 0xffffffff;
303         bh = (regs->regs[51]) >> 32;
304         bl = (regs->regs[51]) & 0xffffffff;
305         ch = (regs->regs[52]) >> 32;
306         cl = (regs->regs[52]) & 0xffffffff;
307         printk("R50 : %08Lx%08Lx R51 : %08Lx%08Lx R52 : %08Lx%08Lx\n",
308                ah, al, bh, bl, ch, cl);
309
310         ah = (regs->regs[53]) >> 32;
311         al = (regs->regs[53]) & 0xffffffff;
312         bh = (regs->regs[54]) >> 32;
313         bl = (regs->regs[54]) & 0xffffffff;
314         ch = (regs->regs[55]) >> 32;
315         cl = (regs->regs[55]) & 0xffffffff;
316         printk("R53 : %08Lx%08Lx R54 : %08Lx%08Lx R55 : %08Lx%08Lx\n",
317                ah, al, bh, bl, ch, cl);
318
319         ah = (regs->regs[56]) >> 32;
320         al = (regs->regs[56]) & 0xffffffff;
321         bh = (regs->regs[57]) >> 32;
322         bl = (regs->regs[57]) & 0xffffffff;
323         ch = (regs->regs[58]) >> 32;
324         cl = (regs->regs[58]) & 0xffffffff;
325         printk("R56 : %08Lx%08Lx R57 : %08Lx%08Lx R58 : %08Lx%08Lx\n",
326                ah, al, bh, bl, ch, cl);
327
328         ah = (regs->regs[59]) >> 32;
329         al = (regs->regs[59]) & 0xffffffff;
330         bh = (regs->regs[60]) >> 32;
331         bl = (regs->regs[60]) & 0xffffffff;
332         ch = (regs->regs[61]) >> 32;
333         cl = (regs->regs[61]) & 0xffffffff;
334         printk("R59 : %08Lx%08Lx R60 : %08Lx%08Lx R61 : %08Lx%08Lx\n",
335                ah, al, bh, bl, ch, cl);
336
337         ah = (regs->regs[62]) >> 32;
338         al = (regs->regs[62]) & 0xffffffff;
339         bh = (regs->tregs[0]) >> 32;
340         bl = (regs->tregs[0]) & 0xffffffff;
341         ch = (regs->tregs[1]) >> 32;
342         cl = (regs->tregs[1]) & 0xffffffff;
343         printk("R62 : %08Lx%08Lx T0  : %08Lx%08Lx T1  : %08Lx%08Lx\n",
344                ah, al, bh, bl, ch, cl);
345
346         ah = (regs->tregs[2]) >> 32;
347         al = (regs->tregs[2]) & 0xffffffff;
348         bh = (regs->tregs[3]) >> 32;
349         bl = (regs->tregs[3]) & 0xffffffff;
350         ch = (regs->tregs[4]) >> 32;
351         cl = (regs->tregs[4]) & 0xffffffff;
352         printk("T2  : %08Lx%08Lx T3  : %08Lx%08Lx T4  : %08Lx%08Lx\n",
353                ah, al, bh, bl, ch, cl);
354
355         ah = (regs->tregs[5]) >> 32;
356         al = (regs->tregs[5]) & 0xffffffff;
357         bh = (regs->tregs[6]) >> 32;
358         bl = (regs->tregs[6]) & 0xffffffff;
359         ch = (regs->tregs[7]) >> 32;
360         cl = (regs->tregs[7]) & 0xffffffff;
361         printk("T5  : %08Lx%08Lx T6  : %08Lx%08Lx T7  : %08Lx%08Lx\n",
362                ah, al, bh, bl, ch, cl);
363
364         /*
365          * If we're in kernel mode, dump the stack too..
366          */
367         if (!user_mode(regs)) {
368                 void show_stack(struct task_struct *tsk, unsigned long *sp);
369                 unsigned long sp = regs->regs[15] & 0xffffffff;
370                 struct task_struct *tsk = get_current();
371
372                 tsk->thread.kregs = regs;
373
374                 show_stack(tsk, (unsigned long *)sp);
375         }
376 }
377
378 struct task_struct * alloc_task_struct(void)
379 {
380         /* Get task descriptor pages */
381         return (struct task_struct *)
382                 __get_free_pages(GFP_KERNEL, get_order(THREAD_SIZE));
383 }
384
385 void free_task_struct(struct task_struct *p)
386 {
387         free_pages((unsigned long) p, get_order(THREAD_SIZE));
388 }
389
390 /*
391  * Create a kernel thread
392  */
393 ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
394 {
395         do_exit(fn(arg));
396 }
397
398 /*
399  * This is the mechanism for creating a new kernel thread.
400  *
401  * NOTE! Only a kernel-only process(ie the swapper or direct descendants
402  * who haven't done an "execve()") should use this: it will work within
403  * a system call from a "real" process, but the process memory space will
404  * not be freed until both the parent and the child have exited.
405  */
406 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
407 {
408         struct pt_regs regs;
409
410         memset(&regs, 0, sizeof(regs));
411         regs.regs[2] = (unsigned long)arg;
412         regs.regs[3] = (unsigned long)fn;
413
414         regs.pc = (unsigned long)kernel_thread_helper;
415         regs.sr = (1 << 30);
416
417         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
418                        &regs, 0, NULL, NULL);
419 }
420
421 /*
422  * Free current thread data structures etc..
423  */
424 void exit_thread(void)
425 {
426         /* See arch/sparc/kernel/process.c for the precedent for doing this -- RPC.
427
428            The SH-5 FPU save/restore approach relies on last_task_used_math
429            pointing to a live task_struct.  When another task tries to use the
430            FPU for the 1st time, the FPUDIS trap handling (see
431            arch/sh64/kernel/fpu.c) will save the existing FPU state to the
432            FP regs field within last_task_used_math before re-loading the new
433            task's FPU state (or initialising it if the FPU has been used
434            before).  So if last_task_used_math is stale, and its page has already been
435            re-allocated for another use, the consequences are rather grim. Unless we
436            null it here, there is no other path through which it would get safely
437            nulled. */
438
439 #ifdef CONFIG_SH_FPU
440         if (last_task_used_math == current) {
441                 last_task_used_math = NULL;
442         }
443 #endif
444 }
445
446 void flush_thread(void)
447 {
448
449         /* Called by fs/exec.c (flush_old_exec) to remove traces of a
450          * previously running executable. */
451 #ifdef CONFIG_SH_FPU
452         if (last_task_used_math == current) {
453                 last_task_used_math = NULL;
454         }
455         /* Force FPU state to be reinitialised after exec */
456         clear_used_math();
457 #endif
458
459         /* if we are a kernel thread, about to change to user thread,
460          * update kreg
461          */
462         if(current->thread.kregs==&fake_swapper_regs) {
463           current->thread.kregs =
464              ((struct pt_regs *)(THREAD_SIZE + (unsigned long) current) - 1);
465           current->thread.uregs = current->thread.kregs;
466         }
467 }
468
469 void release_thread(struct task_struct *dead_task)
470 {
471         /* do nothing */
472 }
473
474 /* Fill in the fpu structure for a core dump.. */
475 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
476 {
477 #ifdef CONFIG_SH_FPU
478         int fpvalid;
479         struct task_struct *tsk = current;
480
481         fpvalid = !!tsk_used_math(tsk);
482         if (fpvalid) {
483                 if (current == last_task_used_math) {
484                         enable_fpu();
485                         fpsave(&tsk->thread.fpu.hard);
486                         disable_fpu();
487                         last_task_used_math = 0;
488                         regs->sr |= SR_FD;
489                 }
490
491                 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
492         }
493
494         return fpvalid;
495 #else
496         return 0; /* Task didn't use the fpu at all. */
497 #endif
498 }
499
500 asmlinkage void ret_from_fork(void);
501
502 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
503                 unsigned long unused,
504                 struct task_struct *p, struct pt_regs *regs)
505 {
506         struct pt_regs *childregs;
507         unsigned long long se;                  /* Sign extension */
508
509 #ifdef CONFIG_SH_FPU
510         if(last_task_used_math == current) {
511                 enable_fpu();
512                 fpsave(&current->thread.fpu.hard);
513                 disable_fpu();
514                 last_task_used_math = NULL;
515                 regs->sr |= SR_FD;
516         }
517 #endif
518         /* Copy from sh version */
519         childregs = (struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1;
520
521         *childregs = *regs;
522
523         if (user_mode(regs)) {
524                 childregs->regs[15] = usp;
525                 p->thread.uregs = childregs;
526         } else {
527                 childregs->regs[15] = (unsigned long)task_stack_page(p) + THREAD_SIZE;
528         }
529
530         childregs->regs[9] = 0; /* Set return value for child */
531         childregs->sr |= SR_FD; /* Invalidate FPU flag */
532
533         p->thread.sp = (unsigned long) childregs;
534         p->thread.pc = (unsigned long) ret_from_fork;
535
536         /*
537          * Sign extend the edited stack.
538          * Note that thread.pc and thread.pc will stay
539          * 32-bit wide and context switch must take care
540          * of NEFF sign extension.
541          */
542
543         se = childregs->regs[15];
544         se = (se & NEFF_SIGN) ? (se | NEFF_MASK) : se;
545         childregs->regs[15] = se;
546
547         return 0;
548 }
549
550 asmlinkage int sys_fork(unsigned long r2, unsigned long r3,
551                         unsigned long r4, unsigned long r5,
552                         unsigned long r6, unsigned long r7,
553                         struct pt_regs *pregs)
554 {
555         return do_fork(SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
556 }
557
558 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
559                          unsigned long r4, unsigned long r5,
560                          unsigned long r6, unsigned long r7,
561                          struct pt_regs *pregs)
562 {
563         if (!newsp)
564                 newsp = pregs->regs[15];
565         return do_fork(clone_flags, newsp, pregs, 0, 0, 0);
566 }
567
568 /*
569  * This is trivial, and on the face of it looks like it
570  * could equally well be done in user mode.
571  *
572  * Not so, for quite unobvious reasons - register pressure.
573  * In user mode vfork() cannot have a stack frame, and if
574  * done by calling the "clone()" system call directly, you
575  * do not have enough call-clobbered registers to hold all
576  * the information you need.
577  */
578 asmlinkage int sys_vfork(unsigned long r2, unsigned long r3,
579                          unsigned long r4, unsigned long r5,
580                          unsigned long r6, unsigned long r7,
581                          struct pt_regs *pregs)
582 {
583         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
584 }
585
586 /*
587  * sys_execve() executes a new program.
588  */
589 asmlinkage int sys_execve(char *ufilename, char **uargv,
590                           char **uenvp, unsigned long r5,
591                           unsigned long r6, unsigned long r7,
592                           struct pt_regs *pregs)
593 {
594         int error;
595         char *filename;
596
597         lock_kernel();
598         filename = getname((char __user *)ufilename);
599         error = PTR_ERR(filename);
600         if (IS_ERR(filename))
601                 goto out;
602
603         error = do_execve(filename,
604                           (char __user * __user *)uargv,
605                           (char __user * __user *)uenvp,
606                           pregs);
607         if (error == 0) {
608                 task_lock(current);
609                 current->ptrace &= ~PT_DTRACE;
610                 task_unlock(current);
611         }
612         putname(filename);
613 out:
614         unlock_kernel();
615         return error;
616 }
617
618 /*
619  * These bracket the sleeping functions..
620  */
621 extern void interruptible_sleep_on(wait_queue_head_t *q);
622
623 #define mid_sched       ((unsigned long) interruptible_sleep_on)
624
625 static int in_sh64_switch_to(unsigned long pc)
626 {
627         extern char __sh64_switch_to_end;
628         /* For a sleeping task, the PC is somewhere in the middle of the function,
629            so we don't have to worry about masking the LSB off */
630         return (pc >= (unsigned long) sh64_switch_to) &&
631                (pc < (unsigned long) &__sh64_switch_to_end);
632 }
633
634 unsigned long get_wchan(struct task_struct *p)
635 {
636         unsigned long schedule_fp;
637         unsigned long sh64_switch_to_fp;
638         unsigned long schedule_caller_pc;
639         unsigned long pc;
640
641         if (!p || p == current || p->state == TASK_RUNNING)
642                 return 0;
643
644         /*
645          * The same comment as on the Alpha applies here, too ...
646          */
647         pc = thread_saved_pc(p);
648
649 #ifdef CONFIG_FRAME_POINTER
650         if (in_sh64_switch_to(pc)) {
651                 sh64_switch_to_fp = (long) p->thread.sp;
652                 /* r14 is saved at offset 4 in the sh64_switch_to frame */
653                 schedule_fp = *(unsigned long *) (long)(sh64_switch_to_fp + 4);
654
655                 /* and the caller of 'schedule' is (currently!) saved at offset 24
656                    in the frame of schedule (from disasm) */
657                 schedule_caller_pc = *(unsigned long *) (long)(schedule_fp + 24);
658                 return schedule_caller_pc;
659         }
660 #endif
661         return pc;
662 }
663
664 /* Provide a /proc/asids file that lists out the
665    ASIDs currently associated with the processes.  (If the DM.PC register is
666    examined through the debug link, this shows ASID + PC.  To make use of this,
667    the PID->ASID relationship needs to be known.  This is primarily for
668    debugging.)
669    */
670
671 #if defined(CONFIG_SH64_PROC_ASIDS)
672 static int
673 asids_proc_info(char *buf, char **start, off_t fpos, int length, int *eof, void *data)
674 {
675         int len=0;
676         struct task_struct *p;
677         read_lock(&tasklist_lock);
678         for_each_process(p) {
679                 int pid = p->pid;
680                 struct mm_struct *mm;
681                 if (!pid) continue;
682                 mm = p->mm;
683                 if (mm) {
684                         unsigned long asid, context;
685                         context = mm->context;
686                         asid = (context & 0xff);
687                         len += sprintf(buf+len, "%5d : %02lx\n", pid, asid);
688                 } else {
689                         len += sprintf(buf+len, "%5d : (none)\n", pid);
690                 }
691         }
692         read_unlock(&tasklist_lock);
693         *eof = 1;
694         return len;
695 }
696
697 static int __init register_proc_asids(void)
698 {
699         create_proc_read_entry("asids", 0, NULL, asids_proc_info, NULL);
700         return 0;
701 }
702 __initcall(register_proc_asids);
703 #endif