ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / arm / kernel / process.c
1 /*
2  *  linux/arch/arm/kernel/process.c
3  *
4  *  Copyright (C) 1996-2000 Russell King - Converted to ARM.
5  *  Original Copyright (C) 1995  Linus Torvalds
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <stdarg.h>
12
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/stddef.h>
19 #include <linux/unistd.h>
20 #include <linux/ptrace.h>
21 #include <linux/slab.h>
22 #include <linux/user.h>
23 #include <linux/a.out.h>
24 #include <linux/delay.h>
25 #include <linux/reboot.h>
26 #include <linux/interrupt.h>
27 #include <linux/kallsyms.h>
28 #include <linux/init.h>
29
30 #include <asm/system.h>
31 #include <asm/io.h>
32 #include <asm/leds.h>
33 #include <asm/processor.h>
34 #include <asm/uaccess.h>
35
36 extern const char *processor_modes[];
37 extern void setup_mm_for_reboot(char mode);
38
39 static volatile int hlt_counter;
40
41 #include <asm/arch/system.h>
42
43 void disable_hlt(void)
44 {
45         hlt_counter++;
46 }
47
48 EXPORT_SYMBOL(disable_hlt);
49
50 void enable_hlt(void)
51 {
52         hlt_counter--;
53 }
54
55 EXPORT_SYMBOL(enable_hlt);
56
57 static int __init nohlt_setup(char *__unused)
58 {
59         hlt_counter = 1;
60         return 1;
61 }
62
63 static int __init hlt_setup(char *__unused)
64 {
65         hlt_counter = 0;
66         return 1;
67 }
68
69 __setup("nohlt", nohlt_setup);
70 __setup("hlt", hlt_setup);
71
72 /*
73  * The following aren't currently used.
74  */
75 void (*pm_idle)(void);
76 void (*pm_power_off)(void);
77
78 /*
79  * This is our default idle handler.  We need to disable
80  * interrupts here to ensure we don't miss a wakeup call.
81  */
82 void default_idle(void)
83 {
84         local_irq_disable();
85         if (!need_resched() && !hlt_counter)
86                 arch_idle();
87         local_irq_enable();
88 }
89
90 /*
91  * The idle thread.  We try to conserve power, while trying to keep
92  * overall latency low.  The architecture specific idle is passed
93  * a value to indicate the level of "idleness" of the system.
94  */
95 void cpu_idle(void)
96 {
97         /* endless idle loop with no priority at all */
98         while (1) {
99                 void (*idle)(void) = pm_idle;
100                 if (!idle)
101                         idle = default_idle;
102                 preempt_disable();
103                 leds_event(led_idle_start);
104                 while (!need_resched())
105                         idle();
106                 leds_event(led_idle_end);
107                 preempt_enable();
108                 schedule();
109         }
110 }
111
112 static char reboot_mode = 'h';
113
114 int __init reboot_setup(char *str)
115 {
116         reboot_mode = str[0];
117         return 1;
118 }
119
120 __setup("reboot=", reboot_setup);
121
122 void machine_halt(void)
123 {
124 }
125
126 EXPORT_SYMBOL(machine_halt);
127
128 void machine_power_off(void)
129 {
130         if (pm_power_off)
131                 pm_power_off();
132 }
133
134 EXPORT_SYMBOL(machine_power_off);
135
136 void machine_restart(char * __unused)
137 {
138         /*
139          * Clean and disable cache, and turn off interrupts
140          */
141         cpu_proc_fin();
142
143         /*
144          * Tell the mm system that we are going to reboot -
145          * we may need it to insert some 1:1 mappings so that
146          * soft boot works.
147          */
148         setup_mm_for_reboot(reboot_mode);
149
150         /*
151          * Now call the architecture specific reboot code.
152          */
153         arch_reset(reboot_mode);
154
155         /*
156          * Whoops - the architecture was unable to reboot.
157          * Tell the user!
158          */
159         mdelay(1000);
160         printk("Reboot failed -- System halted\n");
161         while (1);
162 }
163
164 EXPORT_SYMBOL(machine_restart);
165
166 void show_regs(struct pt_regs * regs)
167 {
168         unsigned long flags;
169
170         flags = condition_codes(regs);
171
172         print_symbol("PC is at %s\n", instruction_pointer(regs));
173         print_symbol("LR is at %s\n", regs->ARM_lr);
174         printk("pc : [<%08lx>]    lr : [<%08lx>]    %s\n"
175                "sp : %08lx  ip : %08lx  fp : %08lx\n",
176                 instruction_pointer(regs),
177                 regs->ARM_lr, print_tainted(), regs->ARM_sp,
178                 regs->ARM_ip, regs->ARM_fp);
179         printk("r10: %08lx  r9 : %08lx  r8 : %08lx\n",
180                 regs->ARM_r10, regs->ARM_r9,
181                 regs->ARM_r8);
182         printk("r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
183                 regs->ARM_r7, regs->ARM_r6,
184                 regs->ARM_r5, regs->ARM_r4);
185         printk("r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
186                 regs->ARM_r3, regs->ARM_r2,
187                 regs->ARM_r1, regs->ARM_r0);
188         printk("Flags: %c%c%c%c",
189                 flags & PSR_N_BIT ? 'N' : 'n',
190                 flags & PSR_Z_BIT ? 'Z' : 'z',
191                 flags & PSR_C_BIT ? 'C' : 'c',
192                 flags & PSR_V_BIT ? 'V' : 'v');
193         printk("  IRQs o%s  FIQs o%s  Mode %s%s  Segment %s\n",
194                 interrupts_enabled(regs) ? "n" : "ff",
195                 fast_interrupts_enabled(regs) ? "n" : "ff",
196                 processor_modes[processor_mode(regs)],
197                 thumb_mode(regs) ? " (T)" : "",
198                 get_fs() == get_ds() ? "kernel" : "user");
199         {
200                 unsigned int ctrl, transbase, dac;
201                   __asm__ (
202                 "       mrc p15, 0, %0, c1, c0\n"
203                 "       mrc p15, 0, %1, c2, c0\n"
204                 "       mrc p15, 0, %2, c3, c0\n"
205                 : "=r" (ctrl), "=r" (transbase), "=r" (dac));
206                 printk("Control: %04X  Table: %08X  DAC: %08X\n",
207                         ctrl, transbase, dac);
208         }
209 }
210
211 void show_fpregs(struct user_fp *regs)
212 {
213         int i;
214
215         for (i = 0; i < 8; i++) {
216                 unsigned long *p;
217                 char type;
218
219                 p = (unsigned long *)(regs->fpregs + i);
220
221                 switch (regs->ftype[i]) {
222                         case 1: type = 'f'; break;
223                         case 2: type = 'd'; break;
224                         case 3: type = 'e'; break;
225                         default: type = '?'; break;
226                 }
227                 if (regs->init_flag)
228                         type = '?';
229
230                 printk("  f%d(%c): %08lx %08lx %08lx%c",
231                         i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
232         }
233                         
234
235         printk("FPSR: %08lx FPCR: %08lx\n",
236                 (unsigned long)regs->fpsr,
237                 (unsigned long)regs->fpcr);
238 }
239
240 /*
241  * Task structure and kernel stack allocation.
242  */
243 static unsigned long *thread_info_head;
244 static unsigned int nr_thread_info;
245
246 #define EXTRA_TASK_STRUCT       4
247 #define ll_alloc_task_struct() ((struct thread_info *) __get_free_pages(GFP_KERNEL,1))
248 #define ll_free_task_struct(p) free_pages((unsigned long)(p),1)
249
250 struct thread_info *alloc_thread_info(struct task_struct *task)
251 {
252         struct thread_info *thread = NULL;
253
254         if (EXTRA_TASK_STRUCT) {
255                 unsigned long *p = thread_info_head;
256
257                 if (p) {
258                         thread_info_head = (unsigned long *)p[0];
259                         nr_thread_info -= 1;
260                 }
261                 thread = (struct thread_info *)p;
262         }
263
264         if (!thread)
265                 thread = ll_alloc_task_struct();
266
267 #ifdef CONFIG_MAGIC_SYSRQ
268         /*
269          * The stack must be cleared if you want SYSRQ-T to
270          * give sensible stack usage information
271          */
272         if (thread) {
273                 char *p = (char *)thread;
274                 memzero(p+KERNEL_STACK_SIZE, KERNEL_STACK_SIZE);
275         }
276 #endif
277         return thread;
278 }
279
280 void free_thread_info(struct thread_info *thread)
281 {
282         if (EXTRA_TASK_STRUCT && nr_thread_info < EXTRA_TASK_STRUCT) {
283                 unsigned long *p = (unsigned long *)thread;
284                 p[0] = (unsigned long)thread_info_head;
285                 thread_info_head = p;
286                 nr_thread_info += 1;
287         } else
288                 ll_free_task_struct(thread);
289 }
290
291 /*
292  * Free current thread data structures etc..
293  */
294 void exit_thread(void)
295 {
296 }
297
298 static void default_fp_init(union fp_state *fp)
299 {
300         memset(fp, 0, sizeof(union fp_state));
301 }
302
303 void (*fp_init)(union fp_state *) = default_fp_init;
304
305 void flush_thread(void)
306 {
307         struct thread_info *thread = current_thread_info();
308         struct task_struct *tsk = current;
309
310         memset(thread->used_cp, 0, sizeof(thread->used_cp));
311         memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
312         fp_init(&thread->fpstate);
313 }
314
315 void release_thread(struct task_struct *dead_task)
316 {
317 }
318
319 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
320
321 int
322 copy_thread(int nr, unsigned long clone_flags, unsigned long stack_start,
323             unsigned long stk_sz, struct task_struct *p, struct pt_regs *regs)
324 {
325         struct thread_info *thread = p->thread_info;
326         struct pt_regs *childregs;
327
328         childregs = ((struct pt_regs *)((unsigned long)thread + THREAD_SIZE - 8)) - 1;
329         *childregs = *regs;
330         childregs->ARM_r0 = 0;
331         childregs->ARM_sp = stack_start;
332
333         memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
334         thread->cpu_context.sp = (unsigned long)childregs;
335         thread->cpu_context.pc = (unsigned long)ret_from_fork;
336
337         return 0;
338 }
339
340 /*
341  * fill in the fpe structure for a core dump...
342  */
343 int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
344 {
345         struct thread_info *thread = current_thread_info();
346         int used_math = thread->used_cp[1] | thread->used_cp[2];
347
348         if (used_math)
349                 memcpy(fp, &thread->fpstate.soft, sizeof (*fp));
350
351         return used_math != 0;
352 }
353
354 /*
355  * fill in the user structure for a core dump..
356  */
357 void dump_thread(struct pt_regs * regs, struct user * dump)
358 {
359         struct task_struct *tsk = current;
360
361         dump->magic = CMAGIC;
362         dump->start_code = tsk->mm->start_code;
363         dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
364
365         dump->u_tsize = (tsk->mm->end_code - tsk->mm->start_code) >> PAGE_SHIFT;
366         dump->u_dsize = (tsk->mm->brk - tsk->mm->start_data + PAGE_SIZE - 1) >> PAGE_SHIFT;
367         dump->u_ssize = 0;
368
369         dump->u_debugreg[0] = tsk->thread.debug.bp[0].address;
370         dump->u_debugreg[1] = tsk->thread.debug.bp[1].address;
371         dump->u_debugreg[2] = tsk->thread.debug.bp[0].insn.arm;
372         dump->u_debugreg[3] = tsk->thread.debug.bp[1].insn.arm;
373         dump->u_debugreg[4] = tsk->thread.debug.nsaved;
374
375         if (dump->start_stack < 0x04000000)
376                 dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
377
378         dump->regs = *regs;
379         dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
380 }
381
382 /*
383  * Shuffle the argument into the correct register before calling the
384  * thread function.  r1 is the thread argument, r2 is the pointer to
385  * the thread function, and r3 points to the exit function.
386  */
387 extern void kernel_thread_helper(void);
388 asm(    ".align\n"
389 "       .type   kernel_thread_helper, #function\n"
390 "kernel_thread_helper:\n"
391 "       mov     r0, r1\n"
392 "       mov     lr, r3\n"
393 "       mov     pc, r2\n"
394 "       .size   kernel_thread_helper, . - kernel_thread_helper");
395
396 /*
397  * Create a kernel thread.
398  */
399 pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
400 {
401         struct pt_regs regs;
402
403         memset(&regs, 0, sizeof(regs));
404
405         regs.ARM_r1 = (unsigned long)arg;
406         regs.ARM_r2 = (unsigned long)fn;
407         regs.ARM_r3 = (unsigned long)do_exit;
408         regs.ARM_pc = (unsigned long)kernel_thread_helper;
409         regs.ARM_cpsr = SVC_MODE;
410
411         return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
412 }
413
414 /*
415  * These bracket the sleeping functions..
416  */
417 #define first_sched     ((unsigned long) scheduling_functions_start_here)
418 #define last_sched      ((unsigned long) scheduling_functions_end_here)
419
420 unsigned long get_wchan(struct task_struct *p)
421 {
422         unsigned long fp, lr;
423         unsigned long stack_page;
424         int count = 0;
425         if (!p || p == current || p->state == TASK_RUNNING)
426                 return 0;
427
428         stack_page = 4096 + (unsigned long)p->thread_info;
429         fp = thread_saved_fp(p);
430         do {
431                 if (fp < stack_page || fp > 4092+stack_page)
432                         return 0;
433                 lr = pc_pointer (((unsigned long *)fp)[-1]);
434                 if (lr < first_sched || lr > last_sched)
435                         return lr;
436                 fp = *(unsigned long *) (fp - 12);
437         } while (count ++ < 16);
438         return 0;
439 }