PageRenderTime 93ms CodeModel.GetById 10ms app.highlight 75ms RepoModel.GetById 1ms app.codeStats 1ms

/arch/sh64/kernel/process.c

https://bitbucket.org/evzijst/gittest
C | 962 lines | 699 code | 138 blank | 125 comment | 59 complexity | 0148d78261e39cbdcb8d90cc8208bdac MD5 | raw file
  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
 66struct task_struct *last_task_used_math = NULL;
 67
 68#ifdef IDLE_TRACE
 69#ifdef VM_SHOW_TABLES
 70/* For testing */
 71static 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 */
 93static 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 */
113static 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
173static 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
226extern unsigned long volatile jiffies;
227int once = 0;
228unsigned long old_jiffies;
229int pid = -1, pgid = -1;
230
231void 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
279static int hlt_counter = 1;
280
281#define HARD_IDLE_TIMEOUT (HZ / 3)
282
283void disable_hlt(void)
284{
285	hlt_counter++;
286}
287
288void enable_hlt(void)
289{
290	hlt_counter--;
291}
292
293static int __init nohlt_setup(char *__unused)
294{
295	hlt_counter = 1;
296	return 1;
297}
298
299static 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
308static inline void hlt(void)
309{
310	if (hlt_counter)
311		return;
312
313	__asm__ __volatile__ ("sleep" : : : "memory");
314}
315
316/*
317 * The idle loop on a uniprocessor SH..
318 */
319void default_idle(void)
320{
321	/* endless idle loop with no priority at all */
322	while (1) {
323		if (hlt_counter) {
324			while (1)
325				if (need_resched())
326					break;
327		} else {
328			local_irq_disable();
329			while (!need_resched()) {
330				local_irq_enable();
331				idle_trace();
332				hlt();
333				local_irq_disable();
334			}
335			local_irq_enable();
336		}
337		schedule();
338	}
339}
340
341void cpu_idle(void)
342{
343	default_idle();
344}
345
346void machine_restart(char * __unused)
347{
348	extern void phys_stext(void);
349
350	phys_stext();
351}
352
353void machine_halt(void)
354{
355	for (;;);
356}
357
358void machine_power_off(void)
359{
360	extern void enter_deep_standby(void);
361
362	enter_deep_standby();
363}
364
365void show_regs(struct pt_regs * regs)
366{
367	unsigned long long ah, al, bh, bl, ch, cl;
368
369	printk("\n");
370
371	ah = (regs->pc) >> 32;
372	al = (regs->pc) & 0xffffffff;
373	bh = (regs->regs[18]) >> 32;
374	bl = (regs->regs[18]) & 0xffffffff;
375	ch = (regs->regs[15]) >> 32;
376	cl = (regs->regs[15]) & 0xffffffff;
377	printk("PC  : %08Lx%08Lx LINK: %08Lx%08Lx SP  : %08Lx%08Lx\n",
378	       ah, al, bh, bl, ch, cl);
379
380	ah = (regs->sr) >> 32;
381	al = (regs->sr) & 0xffffffff;
382        asm volatile ("getcon   " __TEA ", %0" : "=r" (bh));
383        asm volatile ("getcon   " __TEA ", %0" : "=r" (bl));
384	bh = (bh) >> 32;
385	bl = (bl) & 0xffffffff;
386        asm volatile ("getcon   " __KCR0 ", %0" : "=r" (ch));
387        asm volatile ("getcon   " __KCR0 ", %0" : "=r" (cl));
388	ch = (ch) >> 32;
389	cl = (cl) & 0xffffffff;
390	printk("SR  : %08Lx%08Lx TEA : %08Lx%08Lx KCR0: %08Lx%08Lx\n",
391	       ah, al, bh, bl, ch, cl);
392
393	ah = (regs->regs[0]) >> 32;
394	al = (regs->regs[0]) & 0xffffffff;
395	bh = (regs->regs[1]) >> 32;
396	bl = (regs->regs[1]) & 0xffffffff;
397	ch = (regs->regs[2]) >> 32;
398	cl = (regs->regs[2]) & 0xffffffff;
399	printk("R0  : %08Lx%08Lx R1  : %08Lx%08Lx R2  : %08Lx%08Lx\n",
400	       ah, al, bh, bl, ch, cl);
401
402	ah = (regs->regs[3]) >> 32;
403	al = (regs->regs[3]) & 0xffffffff;
404	bh = (regs->regs[4]) >> 32;
405	bl = (regs->regs[4]) & 0xffffffff;
406	ch = (regs->regs[5]) >> 32;
407	cl = (regs->regs[5]) & 0xffffffff;
408	printk("R3  : %08Lx%08Lx R4  : %08Lx%08Lx R5  : %08Lx%08Lx\n",
409	       ah, al, bh, bl, ch, cl);
410
411	ah = (regs->regs[6]) >> 32;
412	al = (regs->regs[6]) & 0xffffffff;
413	bh = (regs->regs[7]) >> 32;
414	bl = (regs->regs[7]) & 0xffffffff;
415	ch = (regs->regs[8]) >> 32;
416	cl = (regs->regs[8]) & 0xffffffff;
417	printk("R6  : %08Lx%08Lx R7  : %08Lx%08Lx R8  : %08Lx%08Lx\n",
418	       ah, al, bh, bl, ch, cl);
419
420	ah = (regs->regs[9]) >> 32;
421	al = (regs->regs[9]) & 0xffffffff;
422	bh = (regs->regs[10]) >> 32;
423	bl = (regs->regs[10]) & 0xffffffff;
424	ch = (regs->regs[11]) >> 32;
425	cl = (regs->regs[11]) & 0xffffffff;
426	printk("R9  : %08Lx%08Lx R10 : %08Lx%08Lx R11 : %08Lx%08Lx\n",
427	       ah, al, bh, bl, ch, cl);
428
429	ah = (regs->regs[12]) >> 32;
430	al = (regs->regs[12]) & 0xffffffff;
431	bh = (regs->regs[13]) >> 32;
432	bl = (regs->regs[13]) & 0xffffffff;
433	ch = (regs->regs[14]) >> 32;
434	cl = (regs->regs[14]) & 0xffffffff;
435	printk("R12 : %08Lx%08Lx R13 : %08Lx%08Lx R14 : %08Lx%08Lx\n",
436	       ah, al, bh, bl, ch, cl);
437
438	ah = (regs->regs[16]) >> 32;
439	al = (regs->regs[16]) & 0xffffffff;
440	bh = (regs->regs[17]) >> 32;
441	bl = (regs->regs[17]) & 0xffffffff;
442	ch = (regs->regs[19]) >> 32;
443	cl = (regs->regs[19]) & 0xffffffff;
444	printk("R16 : %08Lx%08Lx R17 : %08Lx%08Lx R19 : %08Lx%08Lx\n",
445	       ah, al, bh, bl, ch, cl);
446
447	ah = (regs->regs[20]) >> 32;
448	al = (regs->regs[20]) & 0xffffffff;
449	bh = (regs->regs[21]) >> 32;
450	bl = (regs->regs[21]) & 0xffffffff;
451	ch = (regs->regs[22]) >> 32;
452	cl = (regs->regs[22]) & 0xffffffff;
453	printk("R20 : %08Lx%08Lx R21 : %08Lx%08Lx R22 : %08Lx%08Lx\n",
454	       ah, al, bh, bl, ch, cl);
455
456	ah = (regs->regs[23]) >> 32;
457	al = (regs->regs[23]) & 0xffffffff;
458	bh = (regs->regs[24]) >> 32;
459	bl = (regs->regs[24]) & 0xffffffff;
460	ch = (regs->regs[25]) >> 32;
461	cl = (regs->regs[25]) & 0xffffffff;
462	printk("R23 : %08Lx%08Lx R24 : %08Lx%08Lx R25 : %08Lx%08Lx\n",
463	       ah, al, bh, bl, ch, cl);
464
465	ah = (regs->regs[26]) >> 32;
466	al = (regs->regs[26]) & 0xffffffff;
467	bh = (regs->regs[27]) >> 32;
468	bl = (regs->regs[27]) & 0xffffffff;
469	ch = (regs->regs[28]) >> 32;
470	cl = (regs->regs[28]) & 0xffffffff;
471	printk("R26 : %08Lx%08Lx R27 : %08Lx%08Lx R28 : %08Lx%08Lx\n",
472	       ah, al, bh, bl, ch, cl);
473
474	ah = (regs->regs[29]) >> 32;
475	al = (regs->regs[29]) & 0xffffffff;
476	bh = (regs->regs[30]) >> 32;
477	bl = (regs->regs[30]) & 0xffffffff;
478	ch = (regs->regs[31]) >> 32;
479	cl = (regs->regs[31]) & 0xffffffff;
480	printk("R29 : %08Lx%08Lx R30 : %08Lx%08Lx R31 : %08Lx%08Lx\n",
481	       ah, al, bh, bl, ch, cl);
482
483	ah = (regs->regs[32]) >> 32;
484	al = (regs->regs[32]) & 0xffffffff;
485	bh = (regs->regs[33]) >> 32;
486	bl = (regs->regs[33]) & 0xffffffff;
487	ch = (regs->regs[34]) >> 32;
488	cl = (regs->regs[34]) & 0xffffffff;
489	printk("R32 : %08Lx%08Lx R33 : %08Lx%08Lx R34 : %08Lx%08Lx\n",
490	       ah, al, bh, bl, ch, cl);
491
492	ah = (regs->regs[35]) >> 32;
493	al = (regs->regs[35]) & 0xffffffff;
494	bh = (regs->regs[36]) >> 32;
495	bl = (regs->regs[36]) & 0xffffffff;
496	ch = (regs->regs[37]) >> 32;
497	cl = (regs->regs[37]) & 0xffffffff;
498	printk("R35 : %08Lx%08Lx R36 : %08Lx%08Lx R37 : %08Lx%08Lx\n",
499	       ah, al, bh, bl, ch, cl);
500
501	ah = (regs->regs[38]) >> 32;
502	al = (regs->regs[38]) & 0xffffffff;
503	bh = (regs->regs[39]) >> 32;
504	bl = (regs->regs[39]) & 0xffffffff;
505	ch = (regs->regs[40]) >> 32;
506	cl = (regs->regs[40]) & 0xffffffff;
507	printk("R38 : %08Lx%08Lx R39 : %08Lx%08Lx R40 : %08Lx%08Lx\n",
508	       ah, al, bh, bl, ch, cl);
509
510	ah = (regs->regs[41]) >> 32;
511	al = (regs->regs[41]) & 0xffffffff;
512	bh = (regs->regs[42]) >> 32;
513	bl = (regs->regs[42]) & 0xffffffff;
514	ch = (regs->regs[43]) >> 32;
515	cl = (regs->regs[43]) & 0xffffffff;
516	printk("R41 : %08Lx%08Lx R42 : %08Lx%08Lx R43 : %08Lx%08Lx\n",
517	       ah, al, bh, bl, ch, cl);
518
519	ah = (regs->regs[44]) >> 32;
520	al = (regs->regs[44]) & 0xffffffff;
521	bh = (regs->regs[45]) >> 32;
522	bl = (regs->regs[45]) & 0xffffffff;
523	ch = (regs->regs[46]) >> 32;
524	cl = (regs->regs[46]) & 0xffffffff;
525	printk("R44 : %08Lx%08Lx R45 : %08Lx%08Lx R46 : %08Lx%08Lx\n",
526	       ah, al, bh, bl, ch, cl);
527
528	ah = (regs->regs[47]) >> 32;
529	al = (regs->regs[47]) & 0xffffffff;
530	bh = (regs->regs[48]) >> 32;
531	bl = (regs->regs[48]) & 0xffffffff;
532	ch = (regs->regs[49]) >> 32;
533	cl = (regs->regs[49]) & 0xffffffff;
534	printk("R47 : %08Lx%08Lx R48 : %08Lx%08Lx R49 : %08Lx%08Lx\n",
535	       ah, al, bh, bl, ch, cl);
536
537	ah = (regs->regs[50]) >> 32;
538	al = (regs->regs[50]) & 0xffffffff;
539	bh = (regs->regs[51]) >> 32;
540	bl = (regs->regs[51]) & 0xffffffff;
541	ch = (regs->regs[52]) >> 32;
542	cl = (regs->regs[52]) & 0xffffffff;
543	printk("R50 : %08Lx%08Lx R51 : %08Lx%08Lx R52 : %08Lx%08Lx\n",
544	       ah, al, bh, bl, ch, cl);
545
546	ah = (regs->regs[53]) >> 32;
547	al = (regs->regs[53]) & 0xffffffff;
548	bh = (regs->regs[54]) >> 32;
549	bl = (regs->regs[54]) & 0xffffffff;
550	ch = (regs->regs[55]) >> 32;
551	cl = (regs->regs[55]) & 0xffffffff;
552	printk("R53 : %08Lx%08Lx R54 : %08Lx%08Lx R55 : %08Lx%08Lx\n",
553	       ah, al, bh, bl, ch, cl);
554
555	ah = (regs->regs[56]) >> 32;
556	al = (regs->regs[56]) & 0xffffffff;
557	bh = (regs->regs[57]) >> 32;
558	bl = (regs->regs[57]) & 0xffffffff;
559	ch = (regs->regs[58]) >> 32;
560	cl = (regs->regs[58]) & 0xffffffff;
561	printk("R56 : %08Lx%08Lx R57 : %08Lx%08Lx R58 : %08Lx%08Lx\n",
562	       ah, al, bh, bl, ch, cl);
563
564	ah = (regs->regs[59]) >> 32;
565	al = (regs->regs[59]) & 0xffffffff;
566	bh = (regs->regs[60]) >> 32;
567	bl = (regs->regs[60]) & 0xffffffff;
568	ch = (regs->regs[61]) >> 32;
569	cl = (regs->regs[61]) & 0xffffffff;
570	printk("R59 : %08Lx%08Lx R60 : %08Lx%08Lx R61 : %08Lx%08Lx\n",
571	       ah, al, bh, bl, ch, cl);
572
573	ah = (regs->regs[62]) >> 32;
574	al = (regs->regs[62]) & 0xffffffff;
575	bh = (regs->tregs[0]) >> 32;
576	bl = (regs->tregs[0]) & 0xffffffff;
577	ch = (regs->tregs[1]) >> 32;
578	cl = (regs->tregs[1]) & 0xffffffff;
579	printk("R62 : %08Lx%08Lx T0  : %08Lx%08Lx T1  : %08Lx%08Lx\n",
580	       ah, al, bh, bl, ch, cl);
581
582	ah = (regs->tregs[2]) >> 32;
583	al = (regs->tregs[2]) & 0xffffffff;
584	bh = (regs->tregs[3]) >> 32;
585	bl = (regs->tregs[3]) & 0xffffffff;
586	ch = (regs->tregs[4]) >> 32;
587	cl = (regs->tregs[4]) & 0xffffffff;
588	printk("T2  : %08Lx%08Lx T3  : %08Lx%08Lx T4  : %08Lx%08Lx\n",
589	       ah, al, bh, bl, ch, cl);
590
591	ah = (regs->tregs[5]) >> 32;
592	al = (regs->tregs[5]) & 0xffffffff;
593	bh = (regs->tregs[6]) >> 32;
594	bl = (regs->tregs[6]) & 0xffffffff;
595	ch = (regs->tregs[7]) >> 32;
596	cl = (regs->tregs[7]) & 0xffffffff;
597	printk("T5  : %08Lx%08Lx T6  : %08Lx%08Lx T7  : %08Lx%08Lx\n",
598	       ah, al, bh, bl, ch, cl);
599
600	/*
601	 * If we're in kernel mode, dump the stack too..
602	 */
603	if (!user_mode(regs)) {
604		void show_stack(struct task_struct *tsk, unsigned long *sp);
605		unsigned long sp = regs->regs[15] & 0xffffffff;
606		struct task_struct *tsk = get_current();
607
608		tsk->thread.kregs = regs;
609
610		show_stack(tsk, (unsigned long *)sp);
611	}
612}
613
614struct task_struct * alloc_task_struct(void)
615{
616	/* Get task descriptor pages */
617	return (struct task_struct *)
618		__get_free_pages(GFP_KERNEL, get_order(THREAD_SIZE));
619}
620
621void free_task_struct(struct task_struct *p)
622{
623	free_pages((unsigned long) p, get_order(THREAD_SIZE));
624}
625
626/*
627 * Create a kernel thread
628 */
629
630/*
631 * This is the mechanism for creating a new kernel thread.
632 *
633 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
634 * who haven't done an "execve()") should use this: it will work within
635 * a system call from a "real" process, but the process memory space will
636 * not be free'd until both the parent and the child have exited.
637 */
638int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
639{
640	/* A bit less processor dependent than older sh ... */
641	unsigned int reply;
642
643static __inline__ _syscall2(int,clone,unsigned long,flags,unsigned long,newsp)
644static __inline__ _syscall1(int,exit,int,ret)
645
646	reply = clone(flags | CLONE_VM, 0);
647	if (!reply) {
648		/* Child */
649		reply = exit(fn(arg));
650	}
651
652	return reply;
653}
654
655/*
656 * Free current thread data structures etc..
657 */
658void exit_thread(void)
659{
660	/* See arch/sparc/kernel/process.c for the precedent for doing this -- RPC.
661
662	   The SH-5 FPU save/restore approach relies on last_task_used_math
663	   pointing to a live task_struct.  When another task tries to use the
664	   FPU for the 1st time, the FPUDIS trap handling (see
665	   arch/sh64/kernel/fpu.c) will save the existing FPU state to the
666	   FP regs field within last_task_used_math before re-loading the new
667	   task's FPU state (or initialising it if the FPU has been used
668	   before).  So if last_task_used_math is stale, and its page has already been
669	   re-allocated for another use, the consequences are rather grim. Unless we
670	   null it here, there is no other path through which it would get safely
671	   nulled. */
672
673#ifdef CONFIG_SH_FPU
674	if (last_task_used_math == current) {
675		last_task_used_math = NULL;
676	}
677#endif
678}
679
680void flush_thread(void)
681{
682
683	/* Called by fs/exec.c (flush_old_exec) to remove traces of a
684	 * previously running executable. */
685#ifdef CONFIG_SH_FPU
686	if (last_task_used_math == current) {
687		last_task_used_math = NULL;
688	}
689	/* Force FPU state to be reinitialised after exec */
690	clear_used_math();
691#endif
692
693	/* if we are a kernel thread, about to change to user thread,
694         * update kreg
695         */
696	if(current->thread.kregs==&fake_swapper_regs) {
697          current->thread.kregs =
698             ((struct pt_regs *)(THREAD_SIZE + (unsigned long) current) - 1);
699	  current->thread.uregs = current->thread.kregs;
700	}
701}
702
703void release_thread(struct task_struct *dead_task)
704{
705	/* do nothing */
706}
707
708/* Fill in the fpu structure for a core dump.. */
709int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
710{
711#ifdef CONFIG_SH_FPU
712	int fpvalid;
713	struct task_struct *tsk = current;
714
715	fpvalid = !!tsk_used_math(tsk);
716	if (fpvalid) {
717		if (current == last_task_used_math) {
718			grab_fpu();
719			fpsave(&tsk->thread.fpu.hard);
720			release_fpu();
721			last_task_used_math = 0;
722			regs->sr |= SR_FD;
723		}
724
725		memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
726	}
727
728	return fpvalid;
729#else
730	return 0; /* Task didn't use the fpu at all. */
731#endif
732}
733
734asmlinkage void ret_from_fork(void);
735
736int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
737		unsigned long unused,
738		struct task_struct *p, struct pt_regs *regs)
739{
740	struct pt_regs *childregs;
741	unsigned long long se;			/* Sign extension */
742
743#ifdef CONFIG_SH_FPU
744	if(last_task_used_math == current) {
745		grab_fpu();
746		fpsave(&current->thread.fpu.hard);
747		release_fpu();
748		last_task_used_math = NULL;
749		regs->sr |= SR_FD;
750	}
751#endif
752	/* Copy from sh version */
753	childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long) p->thread_info )) - 1;
754
755	*childregs = *regs;
756
757	if (user_mode(regs)) {
758		childregs->regs[15] = usp;
759		p->thread.uregs = childregs;
760	} else {
761		childregs->regs[15] = (unsigned long)p->thread_info + THREAD_SIZE;
762	}
763
764	childregs->regs[9] = 0; /* Set return value for child */
765	childregs->sr |= SR_FD; /* Invalidate FPU flag */
766
767	p->thread.sp = (unsigned long) childregs;
768	p->thread.pc = (unsigned long) ret_from_fork;
769
770	/*
771	 * Sign extend the edited stack.
772         * Note that thread.pc and thread.pc will stay
773	 * 32-bit wide and context switch must take care
774	 * of NEFF sign extension.
775	 */
776
777	se = childregs->regs[15];
778	se = (se & NEFF_SIGN) ? (se | NEFF_MASK) : se;
779	childregs->regs[15] = se;
780
781	return 0;
782}
783
784/*
785 * fill in the user structure for a core dump..
786 */
787void dump_thread(struct pt_regs * regs, struct user * dump)
788{
789	dump->magic = CMAGIC;
790	dump->start_code = current->mm->start_code;
791	dump->start_data  = current->mm->start_data;
792	dump->start_stack = regs->regs[15] & ~(PAGE_SIZE - 1);
793	dump->u_tsize = (current->mm->end_code - dump->start_code) >> PAGE_SHIFT;
794	dump->u_dsize = (current->mm->brk + (PAGE_SIZE-1) - dump->start_data) >> PAGE_SHIFT;
795	dump->u_ssize = (current->mm->start_stack - dump->start_stack +
796			 PAGE_SIZE - 1) >> PAGE_SHIFT;
797	/* Debug registers will come here. */
798
799	dump->regs = *regs;
800
801	dump->u_fpvalid = dump_fpu(regs, &dump->fpu);
802}
803
804asmlinkage int sys_fork(unsigned long r2, unsigned long r3,
805			unsigned long r4, unsigned long r5,
806			unsigned long r6, unsigned long r7,
807			struct pt_regs *pregs)
808{
809	return do_fork(SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
810}
811
812asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
813			 unsigned long r4, unsigned long r5,
814			 unsigned long r6, unsigned long r7,
815			 struct pt_regs *pregs)
816{
817	if (!newsp)
818		newsp = pregs->regs[15];
819	return do_fork(clone_flags, newsp, pregs, 0, 0, 0);
820}
821
822/*
823 * This is trivial, and on the face of it looks like it
824 * could equally well be done in user mode.
825 *
826 * Not so, for quite unobvious reasons - register pressure.
827 * In user mode vfork() cannot have a stack frame, and if
828 * done by calling the "clone()" system call directly, you
829 * do not have enough call-clobbered registers to hold all
830 * the information you need.
831 */
832asmlinkage int sys_vfork(unsigned long r2, unsigned long r3,
833			 unsigned long r4, unsigned long r5,
834			 unsigned long r6, unsigned long r7,
835			 struct pt_regs *pregs)
836{
837	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, pregs->regs[15], pregs, 0, 0, 0);
838}
839
840/*
841 * sys_execve() executes a new program.
842 */
843asmlinkage int sys_execve(char *ufilename, char **uargv,
844			  char **uenvp, unsigned long r5,
845			  unsigned long r6, unsigned long r7,
846			  struct pt_regs *pregs)
847{
848	int error;
849	char *filename;
850
851	lock_kernel();
852	filename = getname((char __user *)ufilename);
853	error = PTR_ERR(filename);
854	if (IS_ERR(filename))
855		goto out;
856
857	error = do_execve(filename,
858			  (char __user * __user *)uargv,
859			  (char __user * __user *)uenvp,
860			  pregs);
861	if (error == 0) {
862		task_lock(current);
863		current->ptrace &= ~PT_DTRACE;
864		task_unlock(current);
865	}
866	putname(filename);
867out:
868	unlock_kernel();
869	return error;
870}
871
872/*
873 * These bracket the sleeping functions..
874 */
875extern void interruptible_sleep_on(wait_queue_head_t *q);
876
877#define mid_sched	((unsigned long) interruptible_sleep_on)
878
879static int in_sh64_switch_to(unsigned long pc)
880{
881	extern char __sh64_switch_to_end;
882	/* For a sleeping task, the PC is somewhere in the middle of the function,
883	   so we don't have to worry about masking the LSB off */
884	return (pc >= (unsigned long) sh64_switch_to) &&
885	       (pc < (unsigned long) &__sh64_switch_to_end);
886}
887
888unsigned long get_wchan(struct task_struct *p)
889{
890	unsigned long schedule_fp;
891	unsigned long sh64_switch_to_fp;
892	unsigned long schedule_caller_pc;
893	unsigned long pc;
894
895	if (!p || p == current || p->state == TASK_RUNNING)
896		return 0;
897
898	/*
899	 * The same comment as on the Alpha applies here, too ...
900	 */
901	pc = thread_saved_pc(p);
902
903#ifdef CONFIG_FRAME_POINTER
904	if (in_sh64_switch_to(pc)) {
905		sh64_switch_to_fp = (long) p->thread.sp;
906		/* r14 is saved at offset 4 in the sh64_switch_to frame */
907		schedule_fp = *(unsigned long *) (long)(sh64_switch_to_fp + 4);
908
909		/* and the caller of 'schedule' is (currently!) saved at offset 24
910		   in the frame of schedule (from disasm) */
911		schedule_caller_pc = *(unsigned long *) (long)(schedule_fp + 24);
912		return schedule_caller_pc;
913	}
914#endif
915	return pc;
916}
917
918/* Provide a /proc/asids file that lists out the
919   ASIDs currently associated with the processes.  (If the DM.PC register is
920   examined through the debug link, this shows ASID + PC.  To make use of this,
921   the PID->ASID relationship needs to be known.  This is primarily for
922   debugging.)
923   */
924
925#if defined(CONFIG_SH64_PROC_ASIDS)
926#include <linux/init.h>
927#include <linux/proc_fs.h>
928
929static int
930asids_proc_info(char *buf, char **start, off_t fpos, int length, int *eof, void *data)
931{
932	int len=0;
933	struct task_struct *p;
934	read_lock(&tasklist_lock);
935	for_each_process(p) {
936		int pid = p->pid;
937		struct mm_struct *mm;
938		if (!pid) continue;
939		mm = p->mm;
940		if (mm) {
941			unsigned long asid, context;
942			context = mm->context;
943			asid = (context & 0xff);
944			len += sprintf(buf+len, "%5d : %02lx\n", pid, asid);
945		} else {
946			len += sprintf(buf+len, "%5d : (none)\n", pid);
947		}
948	}
949	read_unlock(&tasklist_lock);
950	*eof = 1;
951	return len;
952}
953
954static int __init register_proc_asids(void)
955{
956  create_proc_read_entry("asids", 0, NULL, asids_proc_info, NULL);
957  return 0;
958}
959
960__initcall(register_proc_asids);
961#endif
962