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