Merge branch 'drm-forlinus' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[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
24 /* Temporary flags/tests. All to be removed/undefined. BEGIN */
25 #define IDLE_TRACE
26 #define VM_SHOW_TABLES
27 #define VM_TEST_FAULT
28 #define VM_TEST_RTLBMISS
29 #define VM_TEST_WTLBMISS
30
31 #undef VM_SHOW_TABLES
32 #undef IDLE_TRACE
33 /* Temporary flags/tests. All to be removed/undefined. END */
34
35 #define __KERNEL_SYSCALLS__
36 #include <stdarg.h>
37
38 #include <linux/config.h>
39 #include <linux/kernel.h>
40 #include <linux/rwsem.h>
41 #include <linux/mm.h>
42 #include <linux/smp.h>
43 #include <linux/smp_lock.h>
44 #include <linux/ptrace.h>
45 #include <linux/slab.h>
46 #include <linux/vmalloc.h>
47 #include <linux/user.h>
48 #include <linux/a.out.h>
49 #include <linux/interrupt.h>
50 #include <linux/unistd.h>
51 #include <linux/delay.h>
52 #include <linux/reboot.h>
53 #include <linux/init.h>
54
55 #include <asm/uaccess.h>
56 #include <asm/pgtable.h>
57 #include <asm/system.h>
58 #include <asm/io.h>
59 #include <asm/processor.h>              /* includes also <asm/registers.h> */
60 #include <asm/mmu_context.h>
61 #include <asm/elf.h>
62 #include <asm/page.h>
63
64 #include <linux/irq.h>
65
66 struct task_struct *last_task_used_math = NULL;
67
68 #ifdef IDLE_TRACE
69 #ifdef VM_SHOW_TABLES
70 /* For testing */
71 static void print_PTE(long base)
72 {
73         int i, skip=0;
74         long long x, y, *p = (long long *) base;
75
76         for (i=0; i< 512; i++, p++){
77                 if (*p == 0) {
78                         if (!skip) {
79                                 skip++;
80                                 printk("(0s) ");
81                         }
82                 } else {
83                         skip=0;
84                         x = (*p) >> 32;
85                         y = (*p) & 0xffffffff;
86                         printk("%08Lx%08Lx ", x, y);
87                         if (!((i+1)&0x3)) printk("\n");
88                 }
89         }
90 }
91
92 /* For testing */
93 static void print_DIR(long base)
94 {
95         int i, skip=0;
96         long *p = (long *) base;
97
98         for (i=0; i< 512; i++, p++){
99                 if (*p == 0) {
100                         if (!skip) {
101                                 skip++;
102                                 printk("(0s) ");
103                         }
104                 } else {
105                         skip=0;
106                         printk("%08lx ", *p);
107                         if (!((i+1)&0x7)) printk("\n");
108                 }
109         }
110 }
111
112 /* For testing */
113 static void print_vmalloc_first_tables(void)
114 {
115
116 #define PRESENT 0x800   /* Bit 11 */
117
118         /*
119          * Do it really dirty by looking at raw addresses,
120          * raw offsets, no types. If we used pgtable/pgalloc
121          * macros/definitions we could hide potential bugs.
122          *
123          * Note that pointers are 32-bit for CDC.
124          */
125         long pgdt, pmdt, ptet;
126
127         pgdt = (long) &swapper_pg_dir;
128         printk("-->PGD (0x%08lx):\n", pgdt);
129         print_DIR(pgdt);
130         printk("\n");
131
132         /* VMALLOC pool is mapped at 0xc0000000, second (pointer) entry in PGD */
133         pgdt += 4;
134         pmdt = (long) (* (long *) pgdt);
135         if (!(pmdt & PRESENT)) {
136                 printk("No PMD\n");
137                 return;
138         } else pmdt &= 0xfffff000;
139
140         printk("-->PMD (0x%08lx):\n", pmdt);
141         print_DIR(pmdt);
142         printk("\n");
143
144         /* Get the pmdt displacement for 0xc0000000 */
145         pmdt += 2048;
146
147         /* just look at first two address ranges ... */
148         /* ... 0xc0000000 ... */
149         ptet = (long) (* (long *) pmdt);
150         if (!(ptet & PRESENT)) {
151                 printk("No PTE0\n");
152                 return;
153         } else ptet &= 0xfffff000;
154
155         printk("-->PTE0 (0x%08lx):\n", ptet);
156         print_PTE(ptet);
157         printk("\n");
158
159         /* ... 0xc0001000 ... */
160         ptet += 4;
161         if (!(ptet & PRESENT)) {
162                 printk("No PTE1\n");
163                 return;
164         } else ptet &= 0xfffff000;
165         printk("-->PTE1 (0x%08lx):\n", ptet);
166         print_PTE(ptet);
167         printk("\n");
168 }
169 #else
170 #define print_vmalloc_first_tables()
171 #endif  /* VM_SHOW_TABLES */
172
173 static void test_VM(void)
174 {
175         void *a, *b, *c;
176
177 #ifdef VM_SHOW_TABLES
178         printk("Initial PGD/PMD/PTE\n");
179 #endif
180         print_vmalloc_first_tables();
181
182         printk("Allocating 2 bytes\n");
183         a = vmalloc(2);
184         print_vmalloc_first_tables();
185
186         printk("Allocating 4100 bytes\n");
187         b = vmalloc(4100);
188         print_vmalloc_first_tables();
189
190         printk("Allocating 20234 bytes\n");
191         c = vmalloc(20234);
192         print_vmalloc_first_tables();
193
194 #ifdef VM_TEST_FAULT
195         /* Here you may want to fault ! */
196
197 #ifdef VM_TEST_RTLBMISS
198         printk("Ready to fault upon read.\n");
199         if (* (char *) a) {
200                 printk("RTLBMISSed on area a !\n");
201         }
202         printk("RTLBMISSed on area a !\n");
203 #endif
204
205 #ifdef VM_TEST_WTLBMISS
206         printk("Ready to fault upon write.\n");
207         *((char *) b) = 'L';
208         printk("WTLBMISSed on area b !\n");
209 #endif
210
211 #endif  /* VM_TEST_FAULT */
212
213         printk("Deallocating the 4100 byte chunk\n");
214         vfree(b);
215         print_vmalloc_first_tables();
216
217         printk("Deallocating the 2 byte chunk\n");
218         vfree(a);
219         print_vmalloc_first_tables();
220
221         printk("Deallocating the last chunk\n");
222         vfree(c);
223         print_vmalloc_first_tables();
224 }
225
226 extern unsigned long volatile jiffies;
227 int once = 0;
228 unsigned long old_jiffies;
229 int pid = -1, pgid = -1;
230
231 void idle_trace(void)
232 {
233
234         _syscall0(int, getpid)
235         _syscall1(int, getpgid, int, pid)
236
237         if (!once) {
238                 /* VM allocation/deallocation simple test */
239                 test_VM();
240                 pid = getpid();
241
242                 printk("Got all through to Idle !!\n");
243                 printk("I'm now going to loop forever ...\n");
244                 printk("Any ! below is a timer tick.\n");
245                 printk("Any . below is a getpgid system call from pid = %d.\n", pid);
246
247
248                 old_jiffies = jiffies;
249                 once++;
250         }
251
252         if (old_jiffies != jiffies) {
253                 old_jiffies = jiffies - old_jiffies;
254                 switch (old_jiffies) {
255                 case 1:
256                         printk("!");
257                         break;
258                 case 2:
259                         printk("!!");
260                         break;
261                 case 3:
262                         printk("!!!");
263                         break;
264                 case 4:
265                         printk("!!!!");
266                         break;
267                 default:
268                         printk("(%d!)", (int) old_jiffies);
269                 }
270                 old_jiffies = jiffies;
271         }
272         pgid = getpgid(pid);
273         printk(".");
274 }
275 #else
276 #define idle_trace()    do { } while (0)
277 #endif  /* IDLE_TRACE */
278
279 static int hlt_counter = 1;
280
281 #define HARD_IDLE_TIMEOUT (HZ / 3)
282
283 void disable_hlt(void)
284 {
285         hlt_counter++;
286 }
287
288 void enable_hlt(void)
289 {
290         hlt_counter--;
291 }
292
293 static int __init nohlt_setup(char *__unused)
294 {
295         hlt_counter = 1;
296         return 1;
297 }
298
299 static int __init hlt_setup(char *__unused)
300 {
301         hlt_counter = 0;
302         return 1;
303 }
304
305 __setup("nohlt", nohlt_setup);
306 __setup("hlt", hlt_setup);
307
308 static inline void hlt(void)
309 {
310         __asm__ __volatile__ ("sleep" : : : "memory");
311 }
312
313 /*
314  * The idle loop on a uniprocessor SH..
315  */
316 void cpu_idle(void)
317 {
318         /* endless idle loop with no priority at all */
319         while (1) {
320                 if (hlt_counter) {
321                         while (!need_resched())
322                                 cpu_relax();
323                 } else {
324                         local_irq_disable();
325                         while (!need_resched()) {
326                                 local_irq_enable();
327                                 idle_trace();
328                                 hlt();
329                                 local_irq_disable();
330                         }
331                         local_irq_enable();
332                 }
333                 preempt_enable_no_resched();
334                 schedule();
335                 preempt_disable();
336         }
337
338 }
339
340 void machine_restart(char * __unused)
341 {
342         extern void phys_stext(void);
343
344         phys_stext();
345 }
346
347 void machine_halt(void)
348 {
349         for (;;);
350 }
351
352 void machine_power_off(void)
353 {
354         extern void enter_deep_standby(void);
355
356         enter_deep_standby();
357 }
358
359 void show_regs(struct pt_regs * regs)
360 {
361         unsigned long long ah, al, bh, bl, ch, cl;
362
363         printk("\n");
364
365         ah = (regs->pc) >> 32;
366         al = (regs->pc) & 0xffffffff;
367         bh = (regs->regs[18]) >> 32;
368         bl = (regs->regs[18]) & 0xffffffff;
369         ch = (regs->regs[15]) >> 32;
370         cl = (regs->regs[15]) & 0xffffffff;
371         printk("PC  : %08Lx%08Lx LINK: %08Lx%08Lx SP  : %08Lx%08Lx\n",
372                ah, al, bh, bl, ch, cl);
373
374         ah = (regs->sr) >> 32;
375         al = (regs->sr) & 0xffffffff;
376         asm volatile ("getcon   " __TEA ", %0" : "=r" (bh));
377         asm volatile ("getcon   " __TEA ", %0" : "=r" (bl));
378         bh = (bh) >> 32;
379         bl = (bl) & 0xffffffff;
380         asm volatile ("getcon   " __KCR0 ", %0" : "=r" (ch));
381         asm volatile ("getcon   " __KCR0 ", %0" : "=r" (cl));
382         ch = (ch) >> 32;
383         cl = (cl) & 0xffffffff;
384         printk("SR  : %08Lx%08Lx TEA : %08Lx%08Lx KCR0: %08Lx%08Lx\n",
385                ah, al, bh, bl, ch, cl);
386
387         ah = (regs->regs[0]) >> 32;
388         al = (regs->regs[0]) & 0xffffffff;
389         bh = (regs->regs[1]) >> 32;
390         bl = (regs->regs[1]) & 0xffffffff;
391         ch = (regs->regs[2]) >> 32;
392         cl = (regs->regs[2]) & 0xffffffff;
393         printk("R0  : %08Lx%08Lx R1  : %08Lx%08Lx R2  : %08Lx%08Lx\n",
394                ah, al, bh, bl, ch, cl);
395
396         ah = (regs->regs[3]) >> 32;
397         al = (regs->regs[3]) & 0xffffffff;
398         bh = (regs->regs[4]) >> 32;
399         bl = (regs->regs[4]) & 0xffffffff;
400         ch = (regs->regs[5]) >> 32;
401         cl = (regs->regs[5]) & 0xffffffff;
402         printk("R3  : %08Lx%08Lx R4  : %08Lx%08Lx R5  : %08Lx%08Lx\n",
403                ah, al, bh, bl, ch, cl);
404
405         ah = (regs->regs[6]) >> 32;
406         al = (regs->regs[6]) & 0xffffffff;
407         bh = (regs->regs[7]) >> 32;
408         bl = (regs->regs[7]) & 0xffffffff;
409         ch = (regs->regs[8]) >> 32;
410         cl = (regs->regs[8]) & 0xffffffff;
411         printk("R6  : %08Lx%08Lx R7  : %08Lx%08Lx R8  : %08Lx%08Lx\n",
412                ah, al, bh, bl, ch, cl);
413
414         ah = (regs->regs[9]) >> 32;
415         al = (regs->regs[9]) & 0xffffffff;
416         bh = (regs->regs[10]) >> 32;
417         bl = (regs->regs[10]) & 0xffffffff;
418         ch = (regs->regs[11]) >> 32;
419         cl = (regs->regs[11]) & 0xffffffff;
420         printk("R9  : %08Lx%08Lx R10 : %08Lx%08Lx R11 : %08Lx%08Lx\n",
421                ah, al, bh, bl, ch, cl);
422
423         ah = (regs->regs[12]) >> 32;
424         al = (regs->regs[12]) & 0xffffffff;
425         bh = (regs->regs[13]) >> 32;
426         bl = (regs->regs[13]) & 0xffffffff;
427         ch = (regs->regs[14]) >> 32;
428         cl = (regs->regs[14]) & 0xffffffff;
429         printk("R12 : %08Lx%08Lx R13 : %08Lx%08Lx R14 : %08Lx%08Lx\n",
430                ah, al, bh, bl, ch, cl);
431
432         ah = (regs->regs[16]) >> 32;
433         al = (regs->regs[16]) & 0xffffffff;
434         bh = (regs->regs[17]) >> 32;
435         bl = (regs->regs[17]) & 0xffffffff;
436         ch = (regs->regs[19]) >> 32;
437         cl = (regs->regs[19]) & 0xffffffff;
438         printk("R16 : %08Lx%08Lx R17 : %08Lx%08Lx R19 : %08Lx%08Lx\n",
439                ah, al, bh, bl, ch, cl);
440
441         ah = (regs->regs[20]) >> 32;
442         al = (regs->regs[20]) & 0xffffffff;
443         bh = (regs->regs[21]) >> 32;
444         bl = (regs->regs[21]) & 0xffffffff;
445         ch = (regs->regs[22]) >> 32;
446         cl = (regs->regs[22]) & 0xffffffff;
447         printk("R20 : %08Lx%08Lx R21 : %08Lx%08Lx R22 : %08Lx%08Lx\n",
448                ah, al, bh, bl, ch, cl);
449
450         ah = (regs->regs[23]) >> 32;
451         al = (regs->regs[23]) & 0xffffffff;
452         bh = (regs->regs[24]) >> 32;
453         bl = (regs->regs[24]) & 0xffffffff;
454         ch = (regs->regs[25]) >> 32;
455         cl = (regs->regs[25]) & 0xffffffff;
456         printk("R23 : %08Lx%08Lx R24 : %08Lx%08Lx R25 : %08Lx%08Lx\n",
457                ah, al, bh, bl, ch, cl);
458
459         ah = (regs->regs[26]) >> 32;
460         al = (regs->regs[26]) & 0xffffffff;
461         bh = (regs->regs[27]) >> 32;
462         bl = (regs->regs[27]) & 0xffffffff;
463         ch = (regs->regs[28]) >> 32;
464         cl = (regs->regs[28]) & 0xffffffff;
465         printk("R26 : %08Lx%08Lx R27 : %08Lx%08Lx R28 : %08Lx%08Lx\n",
466                ah, al, bh, bl, ch, cl);
467
468         ah = (regs->regs[29]) >> 32;
469         al = (regs->regs[29]) & 0xffffffff;
470         bh = (regs->regs[30]) >> 32;
471         bl = (regs->regs[30]) & 0xffffffff;
472         ch = (regs->regs[31]) >> 32;
473         cl = (regs->regs[31]) & 0xffffffff;
474         printk("R29 : %08Lx%08Lx R30 : %08Lx%08Lx R31 : %08Lx%08Lx\n",
475                ah, al, bh, bl, ch, cl);
476
477         ah = (regs->regs[32]) >> 32;
478         al = (regs->regs[32]) & 0xffffffff;
479         bh = (regs->regs[33]) >> 32;
480         bl = (regs->regs[33]) & 0xffffffff;
481         ch = (regs->regs[34]) >> 32;
482         cl = (regs->regs[34]) & 0xffffffff;
483         printk("R32 : %08Lx%08Lx R33 : %08Lx%08Lx R34 : %08Lx%08Lx\n",
484                ah, al, bh, bl, ch, cl);
485
486         ah = (regs->regs[35]) >> 32;
487         al = (regs->regs[35]) & 0xffffffff;
488         bh = (regs->regs[36]) >> 32;
489         bl = (regs->regs[36]) & 0xffffffff;
490         ch = (regs->regs[37]) >> 32;
491         cl = (regs->regs[37]) & 0xffffffff;
492         printk("R35 : %08Lx%08Lx R36 : %08Lx%08Lx R37 : %08Lx%08Lx\n",
493                ah, al, bh, bl, ch, cl);
494
495         ah = (regs->regs[38]) >> 32;
496         al = (regs->regs[38]) & 0xffffffff;
497         bh = (regs->regs[39]) >> 32;
498         bl = (regs->regs[39]) & 0xffffffff;
499         ch = (regs->regs[40]) >> 32;
500         cl = (regs->regs[40]) & 0xffffffff;
501         printk("R38 : %08Lx%08Lx R39 : %08Lx%08Lx R40 : %08Lx%08Lx\n",
502                ah, al, bh, bl, ch, cl);
503
504         ah = (regs->regs[41]) >> 32;
505         al = (regs->regs[41]) & 0xffffffff;
506         bh = (regs->regs[42]) >> 32;
507         bl = (regs->regs[42]) & 0xffffffff;
508         ch = (regs->regs[43]) >> 32;
509         cl = (regs->regs[43]) & 0xffffffff;
510         printk("R41 : %08Lx%08Lx R42 : %08Lx%08Lx R43 : %08Lx%08Lx\n",
511                ah, al, bh, bl, ch, cl);
512
513         ah = (regs->regs[44]) >> 32;
514         al = (regs->regs[44]) & 0xffffffff;
515         bh = (regs->regs[45]) >> 32;
516         bl = (regs->regs[45]) & 0xffffffff;
517         ch = (regs->regs[46]) >> 32;
518         cl = (regs->regs[46]) & 0xffffffff;
519         printk("R44 : %08Lx%08Lx R45 : %08Lx%08Lx R46 : %08Lx%08Lx\n",
520                ah, al, bh, bl, ch, cl);
521
522         ah = (regs->regs[47]) >> 32;
523         al = (regs->regs[47]) & 0xffffffff;
524         bh = (regs->regs[48]) >> 32;
525         bl = (regs->regs[48]) & 0xffffffff;
526         ch = (regs->regs[49]) >> 32;
527         cl = (regs->regs[49]) & 0xffffffff;
528         printk("R47 : %08Lx%08Lx R48 : %08Lx%08Lx R49 : %08Lx%08Lx\n",
529                ah, al, bh, bl, ch, cl);
530
531         ah = (regs->regs[50]) >> 32;
532         al = (regs->regs[50]) & 0xffffffff;
533         bh = (regs->regs[51]) >> 32;
534         bl = (regs->regs[51]) & 0xffffffff;
535         ch = (regs->regs[52]) >> 32;
536         cl = (regs->regs[52]) & 0xffffffff;
537         printk("R50 : %08Lx%08Lx R51 : %08Lx%08Lx R52 : %08Lx%08Lx\n",
538                ah, al, bh, bl, ch, cl);
539
540         ah = (regs->regs[53]) >> 32;
541         al = (regs->regs[53]) & 0xffffffff;
542         bh = (regs->regs[54]) >> 32;
543         bl = (regs->regs[54]) & 0xffffffff;
544         ch = (regs->regs[55]) >> 32;
545         cl = (regs->regs[55]) & 0xffffffff;
546         printk("R53 : %08Lx%08Lx R54 : %08Lx%08Lx R55 : %08Lx%08Lx\n",
547                ah, al, bh, bl, ch, cl);
548
549         ah = (regs->regs[56]) >> 32;
550         al = (regs->regs[56]) & 0xffffffff;
551         bh = (regs->regs[57]) >> 32;
552         bl = (regs->regs[57]) & 0xffffffff;
553         ch = (regs->regs[58]) >> 32;
554         cl = (regs->regs[58]) & 0xffffffff;
555         printk("R56 : %08Lx%08Lx R57 : %08Lx%08Lx R58 : %08Lx%08Lx\n",
556                ah, al, bh, bl, ch, cl);
557
558         ah = (regs->regs[59]) >> 32;
559         al = (regs->regs[59]) & 0xffffffff;
560         bh = (regs->regs[60]) >> 32;
561         bl = (regs->regs[60]) & 0xffffffff;
562         ch = (regs->regs[61]) >> 32;
563         cl = (regs->regs[61]) & 0xffffffff;
564         printk("R59 : %08Lx%08Lx R60 : %08Lx%08Lx R61 : %08Lx%08Lx\n",
565                ah, al, bh, bl, ch, cl);
566
567         ah = (regs->regs[62]) >> 32;
568         al = (regs->regs[62]) & 0xffffffff;
569         bh = (regs->tregs[0]) >> 32;
570         bl = (regs->tregs[0]) & 0xffffffff;
571         ch = (regs->tregs[1]) >> 32;
572         cl = (regs->tregs[1]) & 0xffffffff;
573         printk("R62 : %08Lx%08Lx T0  : %08Lx%08Lx T1  : %08Lx%08Lx\n",
574                ah, al, bh, bl, ch, cl);
575
576         ah = (regs->tregs[2]) >> 32;
577         al = (regs->tregs[2]) & 0xffffffff;
578         bh = (regs->tregs[3]) >> 32;
579         bl = (regs->tregs[3]) & 0xffffffff;
580         ch = (regs->tregs[4]) >> 32;
581         cl = (regs->tregs[4]) & 0xffffffff;
582         printk("T2  : %08Lx%08Lx T3  : %08Lx%08Lx T4  : %08Lx%08Lx\n",
583                ah, al, bh, bl, ch, cl);
584
585         ah = (regs->tregs[5]) >> 32;
586         al = (regs->tregs[5]) & 0xffffffff;
587         bh = (regs->tregs[6]) >> 32;
588         bl = (regs->tregs[6]) & 0xffffffff;
589         ch = (regs->tregs[7]) >> 32;
590         cl = (regs->tregs[7]) & 0xffffffff;
591         printk("T5  : %08Lx%08Lx T6  : %08Lx%08Lx T7  : %08Lx%08Lx\n",
592                ah, al, bh, bl, ch, cl);
593
594         /*
595          * If we're in kernel mode, dump the stack too..
596          */
597         if (!user_mode(regs)) {
598                 void show_stack(struct task_struct *tsk, unsigned long *sp);
599                 unsigned long sp = regs->regs[15] & 0xffffffff;
600                 struct task_struct *tsk = get_current();
601
602                 tsk->thread.kregs = regs;
603
604                 show_stack(tsk, (unsigned long *)sp);
605         }
606 }
607
608 struct task_struct * alloc_task_struct(void)
609 {
610         /* Get task descriptor pages */
611         return (struct task_struct *)
612                 __get_free_pages(GFP_KERNEL, get_order(THREAD_SIZE));
613 }
614
615 void free_task_struct(struct task_struct *p)
616 {
617         free_pages((unsigned long) p, get_order(THREAD_SIZE));
618 }
619
620 /*
621  * Create a kernel thread
622  */
623
624 /*
625  * This is the mechanism for creating a new kernel thread.
626  *
627  * NOTE! Only a kernel-only process(ie the swapper or direct descendants
628  * who haven't done an "execve()") should use this: it will work within
629  * a system call from a "real" process, but the process memory space will
630  * not be free'd until both the parent and the child have exited.
631  */
632 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
633 {
634         /* A bit less processor dependent than older sh ... */
635         unsigned int reply;
636
637 static __inline__ _syscall2(int,clone,unsigned long,flags,unsigned long,newsp)
638 static __inline__ _syscall1(int,exit,int,ret)
639
640         reply = clone(flags | CLONE_VM, 0);
641         if (!reply) {
642                 /* Child */
643                 reply = exit(fn(arg));
644         }
645
646         return reply;
647 }
648
649 /*
650  * Free current thread data structures etc..
651  */
652 void exit_thread(void)
653 {
654         /* See arch/sparc/kernel/process.c for the precedent for doing this -- RPC.
655
656            The SH-5 FPU save/restore approach relies on last_task_used_math
657            pointing to a live task_struct.  When another task tries to use the
658            FPU for the 1st time, the FPUDIS trap handling (see
659            arch/sh64/kernel/fpu.c) will save the existing FPU state to the
660            FP regs field within last_task_used_math before re-loading the new
661            task's FPU state (or initialising it if the FPU has been used
662            before).  So if last_task_used_math is stale, and its page has already been
663            re-allocated for another use, the consequences are rather grim. Unless we
664            null it here, there is no other path through which it would get safely
665            nulled. */
666
667 #ifdef CONFIG_SH_FPU
668         if (last_task_used_math == current) {
669                 last_task_used_math = NULL;
670         }
671 #endif
672 }
673
674 void flush_thread(void)
675 {
676
677         /* Called by fs/exec.c (flush_old_exec) to remove traces of a
678          * previously running executable. */
679 #ifdef CONFIG_SH_FPU
680         if (last_task_used_math == current) {
681                 last_task_used_math = NULL;
682         }
683         /* Force FPU state to be reinitialised after exec */
684         clear_used_math();
685 #endif
686
687         /* if we are a kernel thread, about to change to user thread,
688          * update kreg
689          */
690         if(current->thread.kregs==&fake_swapper_regs) {
691           current->thread.kregs =
692              ((struct pt_regs *)(THREAD_SIZE + (unsigned long) current) - 1);
693           current->thread.uregs = current->thread.kregs;
694         }
695 }
696
697 void release_thread(struct task_struct *dead_task)
698 {
699         /* do nothing */
700 }
701
702 /* Fill in the fpu structure for a core dump.. */
703 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
704 {
705 #ifdef CONFIG_SH_FPU
706         int fpvalid;
707         struct task_struct *tsk = current;
708
709         fpvalid = !!tsk_used_math(tsk);
710         if (fpvalid) {
711                 if (current == last_task_used_math) {
712                         grab_fpu();
713                         fpsave(&tsk->thread.fpu.hard);
714                         release_fpu();
715                         last_task_used_math = 0;
716                         regs->sr |= SR_FD;
717                 }
718
719                 memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
720         }
721
722         return fpvalid;
723 #else
724         return 0; /* Task didn't use the fpu at all. */
725 #endif
726 }
727
728 asmlinkage void ret_from_fork(void);
729
730 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
731                 unsigned long unused,
732                 struct task_struct *p, struct pt_regs *regs)
733 {
734         struct pt_regs *childregs;
735         unsigned long long se;                  /* Sign extension */
736
737 #ifdef CONFIG_SH_FPU
738         if(last_task_used_math == current) {
739                 grab_fpu();
740                 fpsave(&current->thread.fpu.hard);
741                 release_fpu();
742                 last_task_used_math = NULL;
743                 regs->sr |= SR_FD;
744         }
745 #endif
746         /* Copy from sh version */
747         childregs = (struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1;
748
749         *childregs = *regs;
750
751         if (user_mode(regs)) {
752                 childregs->regs[15] = usp;
753                 p->thread.uregs = childregs;
754         } else {
755                 childregs->regs[15] = (unsigned long)task_stack_page(p) + THREAD_SIZE;
756         }
757
758         childregs->regs[9] = 0; /* Set return value for child */
759         childregs->sr |= SR_FD; /* Invalidate FPU flag */
760
761         p->thread.sp = (unsigned long) childregs;
762         p->thread.pc = (unsigned long) ret_from_fork;
763
764         /*
765          * Sign extend the edited stack.
766          * Note that thread.pc and thread.pc will stay
767          * 32-bit wide and context switch must take care
768          * of NEFF sign extension.
769          */
770
771         se = childregs->regs[15];
772         se = (se & NEFF_SIGN) ? (se | NEFF_MASK) : se;
773         childregs->regs[15] = se;
774
775         return 0;
776 }
777
778 asmlinkage int sys_fork(unsigned long r2, unsigned long r3,
779                         unsigned long r4, unsigned long r5,
780                         unsigned long r6, unsigned long r7,
781                         struct pt_regs *pregs)
782 {
783         return do_fork(SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
784 }
785
786 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
787                          unsigned long r4, unsigned long r5,
788                          unsigned long r6, unsigned long r7,
789                          struct pt_regs *pregs)
790 {
791         if (!newsp)
792                 newsp = pregs->regs[15];
793         return do_fork(clone_flags, newsp, pregs, 0, 0, 0);
794 }
795
796 /*
797  * This is trivial, and on the face of it looks like it
798  * could equally well be done in user mode.
799  *
800  * Not so, for quite unobvious reasons - register pressure.
801  * In user mode vfork() cannot have a stack frame, and if
802  * done by calling the "clone()" system call directly, you
803  * do not have enough call-clobbered registers to hold all
804  * the information you need.
805  */
806 asmlinkage int sys_vfork(unsigned long r2, unsigned long r3,
807                          unsigned long r4, unsigned long r5,
808                          unsigned long r6, unsigned long r7,
809                          struct pt_regs *pregs)
810 {
811         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
812 }
813
814 /*
815  * sys_execve() executes a new program.
816  */
817 asmlinkage int sys_execve(char *ufilename, char **uargv,
818                           char **uenvp, unsigned long r5,
819                           unsigned long r6, unsigned long r7,
820                           struct pt_regs *pregs)
821 {
822         int error;
823         char *filename;
824
825         lock_kernel();
826         filename = getname((char __user *)ufilename);
827         error = PTR_ERR(filename);
828         if (IS_ERR(filename))
829                 goto out;
830
831         error = do_execve(filename,
832                           (char __user * __user *)uargv,
833                           (char __user * __user *)uenvp,
834                           pregs);
835         if (error == 0) {
836                 task_lock(current);
837                 current->ptrace &= ~PT_DTRACE;
838                 task_unlock(current);
839         }
840         putname(filename);
841 out:
842         unlock_kernel();
843         return error;
844 }
845
846 /*
847  * These bracket the sleeping functions..
848  */
849 extern void interruptible_sleep_on(wait_queue_head_t *q);
850
851 #define mid_sched       ((unsigned long) interruptible_sleep_on)
852
853 static int in_sh64_switch_to(unsigned long pc)
854 {
855         extern char __sh64_switch_to_end;
856         /* For a sleeping task, the PC is somewhere in the middle of the function,
857            so we don't have to worry about masking the LSB off */
858         return (pc >= (unsigned long) sh64_switch_to) &&
859                (pc < (unsigned long) &__sh64_switch_to_end);
860 }
861
862 unsigned long get_wchan(struct task_struct *p)
863 {
864         unsigned long schedule_fp;
865         unsigned long sh64_switch_to_fp;
866         unsigned long schedule_caller_pc;
867         unsigned long pc;
868
869         if (!p || p == current || p->state == TASK_RUNNING)
870                 return 0;
871
872         /*
873          * The same comment as on the Alpha applies here, too ...
874          */
875         pc = thread_saved_pc(p);
876
877 #ifdef CONFIG_FRAME_POINTER
878         if (in_sh64_switch_to(pc)) {
879                 sh64_switch_to_fp = (long) p->thread.sp;
880                 /* r14 is saved at offset 4 in the sh64_switch_to frame */
881                 schedule_fp = *(unsigned long *) (long)(sh64_switch_to_fp + 4);
882
883                 /* and the caller of 'schedule' is (currently!) saved at offset 24
884                    in the frame of schedule (from disasm) */
885                 schedule_caller_pc = *(unsigned long *) (long)(schedule_fp + 24);
886                 return schedule_caller_pc;
887         }
888 #endif
889         return pc;
890 }
891
892 /* Provide a /proc/asids file that lists out the
893    ASIDs currently associated with the processes.  (If the DM.PC register is
894    examined through the debug link, this shows ASID + PC.  To make use of this,
895    the PID->ASID relationship needs to be known.  This is primarily for
896    debugging.)
897    */
898
899 #if defined(CONFIG_SH64_PROC_ASIDS)
900 #include <linux/init.h>
901 #include <linux/proc_fs.h>
902
903 static int
904 asids_proc_info(char *buf, char **start, off_t fpos, int length, int *eof, void *data)
905 {
906         int len=0;
907         struct task_struct *p;
908         read_lock(&tasklist_lock);
909         for_each_process(p) {
910                 int pid = p->pid;
911                 struct mm_struct *mm;
912                 if (!pid) continue;
913                 mm = p->mm;
914                 if (mm) {
915                         unsigned long asid, context;
916                         context = mm->context;
917                         asid = (context & 0xff);
918                         len += sprintf(buf+len, "%5d : %02lx\n", pid, asid);
919                 } else {
920                         len += sprintf(buf+len, "%5d : (none)\n", pid);
921                 }
922         }
923         read_unlock(&tasklist_lock);
924         *eof = 1;
925         return len;
926 }
927
928 static int __init register_proc_asids(void)
929 {
930   create_proc_read_entry("asids", 0, NULL, asids_proc_info, NULL);
931   return 0;
932 }
933
934 __initcall(register_proc_asids);
935 #endif
936