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