Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / arch / arm / kernel / process.c
blob6f482ff3936f2b78f7197d99d527ababd0c79cb1
1 /*
2 * linux/arch/arm/kernel/process.c
4 * Copyright (C) 1996-2000 Russell King - Converted to ARM.
5 * Origional Copyright (C) 1995 Linus Torvalds
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.
11 #include <stdarg.h>
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>
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>
36 extern const char *processor_modes[];
37 extern void setup_mm_for_reboot(char mode);
39 static volatile int hlt_counter;
41 #include <asm/arch/system.h>
43 void disable_hlt(void)
45 hlt_counter++;
48 void enable_hlt(void)
50 hlt_counter--;
53 static int __init nohlt_setup(char *__unused)
55 hlt_counter = 1;
56 return 1;
59 static int __init hlt_setup(char *__unused)
61 hlt_counter = 0;
62 return 1;
65 __setup("nohlt", nohlt_setup);
66 __setup("hlt", hlt_setup);
69 * The following aren't currently used.
71 void (*pm_idle)(void);
72 void (*pm_power_off)(void);
75 * This is our default idle handler. We need to disable
76 * interrupts here to ensure we don't miss a wakeup call.
78 void default_idle(void)
80 local_irq_disable();
81 if (!need_resched() && !hlt_counter)
82 arch_idle();
83 local_irq_enable();
87 * The idle thread. We try to conserve power, while trying to keep
88 * overall latency low. The architecture specific idle is passed
89 * a value to indicate the level of "idleness" of the system.
91 void cpu_idle(void)
93 /* endless idle loop with no priority at all */
94 while (1) {
95 void (*idle)(void) = pm_idle;
96 if (!idle)
97 idle = default_idle;
98 preempt_disable();
99 leds_event(led_idle_start);
100 while (!need_resched())
101 idle();
102 leds_event(led_idle_end);
103 preempt_enable();
104 schedule();
108 static char reboot_mode = 'h';
110 int __init reboot_setup(char *str)
112 reboot_mode = str[0];
113 return 1;
116 __setup("reboot=", reboot_setup);
118 void machine_halt(void)
120 leds_event(led_halted);
123 void machine_power_off(void)
125 leds_event(led_halted);
126 if (pm_power_off)
127 pm_power_off();
130 void machine_restart(char * __unused)
133 * Clean and disable cache, and turn off interrupts
135 cpu_proc_fin();
138 * Tell the mm system that we are going to reboot -
139 * we may need it to insert some 1:1 mappings so that
140 * soft boot works.
142 setup_mm_for_reboot(reboot_mode);
145 * Now call the architecture specific reboot code.
147 arch_reset(reboot_mode);
150 * Whoops - the architecture was unable to reboot.
151 * Tell the user!
153 mdelay(1000);
154 printk("Reboot failed -- System halted\n");
155 while (1);
158 void show_regs(struct pt_regs * regs)
160 unsigned long flags;
162 flags = condition_codes(regs);
164 print_symbol("PC is at %s\n", instruction_pointer(regs));
165 print_symbol("LR is at %s\n", regs->ARM_lr);
166 printk("pc : [<%08lx>] lr : [<%08lx>] %s\n"
167 "sp : %08lx ip : %08lx fp : %08lx\n",
168 instruction_pointer(regs),
169 regs->ARM_lr, print_tainted(), regs->ARM_sp,
170 regs->ARM_ip, regs->ARM_fp);
171 printk("r10: %08lx r9 : %08lx r8 : %08lx\n",
172 regs->ARM_r10, regs->ARM_r9,
173 regs->ARM_r8);
174 printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
175 regs->ARM_r7, regs->ARM_r6,
176 regs->ARM_r5, regs->ARM_r4);
177 printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
178 regs->ARM_r3, regs->ARM_r2,
179 regs->ARM_r1, regs->ARM_r0);
180 printk("Flags: %c%c%c%c",
181 flags & PSR_N_BIT ? 'N' : 'n',
182 flags & PSR_Z_BIT ? 'Z' : 'z',
183 flags & PSR_C_BIT ? 'C' : 'c',
184 flags & PSR_V_BIT ? 'V' : 'v');
185 printk(" IRQs o%s FIQs o%s Mode %s%s Segment %s\n",
186 interrupts_enabled(regs) ? "n" : "ff",
187 fast_interrupts_enabled(regs) ? "n" : "ff",
188 processor_modes[processor_mode(regs)],
189 thumb_mode(regs) ? " (T)" : "",
190 get_fs() == get_ds() ? "kernel" : "user");
191 #if defined(CONFIG_CPU_32)
193 unsigned int ctrl, transbase, dac;
194 __asm__ (
195 " mrc p15, 0, %0, c1, c0\n"
196 " mrc p15, 0, %1, c2, c0\n"
197 " mrc p15, 0, %2, c3, c0\n"
198 : "=r" (ctrl), "=r" (transbase), "=r" (dac));
199 printk("Control: %04X Table: %08X DAC: %08X\n",
200 ctrl, transbase, dac);
202 #endif
205 void show_fpregs(struct user_fp *regs)
207 int i;
209 for (i = 0; i < 8; i++) {
210 unsigned long *p;
211 char type;
213 p = (unsigned long *)(regs->fpregs + i);
215 switch (regs->ftype[i]) {
216 case 1: type = 'f'; break;
217 case 2: type = 'd'; break;
218 case 3: type = 'e'; break;
219 default: type = '?'; break;
221 if (regs->init_flag)
222 type = '?';
224 printk(" f%d(%c): %08lx %08lx %08lx%c",
225 i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
229 printk("FPSR: %08lx FPCR: %08lx\n",
230 (unsigned long)regs->fpsr,
231 (unsigned long)regs->fpcr);
235 * Task structure and kernel stack allocation.
237 static unsigned long *thread_info_head;
238 static unsigned int nr_thread_info;
240 #ifdef CONFIG_CPU_32
241 #define EXTRA_TASK_STRUCT 4
242 #define ll_alloc_task_struct() ((struct thread_info *) __get_free_pages(GFP_KERNEL,1))
243 #define ll_free_task_struct(p) free_pages((unsigned long)(p),1)
244 #else
245 extern unsigned long get_page_8k(int priority);
246 extern void free_page_8k(unsigned long page);
248 #define EXTRA_TASK_STRUCT 0
249 #define ll_alloc_task_struct() ((struct task_struct *)get_page_8k(GFP_KERNEL))
250 #define ll_free_task_struct(p) free_page_8k((unsigned long)(p))
251 #endif
253 struct thread_info *alloc_thread_info(void)
255 struct thread_info *thread = NULL;
257 if (EXTRA_TASK_STRUCT) {
258 unsigned long *p = thread_info_head;
260 if (p) {
261 thread_info_head = (unsigned long *)p[0];
262 nr_thread_info -= 1;
264 thread = (struct thread_info *)p;
267 if (!thread)
268 thread = ll_alloc_task_struct();
270 #ifdef CONFIG_SYSRQ
272 * The stack must be cleared if you want SYSRQ-T to
273 * give sensible stack usage information
275 if (thread) {
276 char *p = (char *)thread;
277 memzero(p+KERNEL_STACK_SIZE, KERNEL_STACK_SIZE);
279 #endif
280 return thread;
283 void free_thread_info(struct thread_info *thread)
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);
295 * Free current thread data structures etc..
297 void exit_thread(void)
301 static void default_fp_init(union fp_state *fp)
303 memset(fp, 0, sizeof(union fp_state));
306 void (*fp_init)(union fp_state *) = default_fp_init;
308 void flush_thread(void)
310 struct thread_info *thread = current_thread_info();
311 struct task_struct *tsk = current;
313 tsk->used_math = 0;
315 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
316 fp_init(&thread->fpstate);
319 void release_thread(struct task_struct *dead_task)
323 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
326 copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
327 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
329 struct thread_info *thread = p->thread_info;
330 struct pt_regs *childregs;
332 childregs = ((struct pt_regs *)((unsigned long)thread + THREAD_SIZE - 8)) - 1;
333 *childregs = *regs;
334 childregs->ARM_r0 = 0;
335 childregs->ARM_sp = esp;
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;
341 return 0;
345 * fill in the fpe structure for a core dump...
347 int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
349 struct thread_info *thread = current_thread_info();
350 int used_math = current->used_math;
352 if (used_math)
353 memcpy(fp, &thread->fpstate.soft, sizeof (*fp));
355 return used_math;
359 * fill in the user structure for a core dump..
361 void dump_thread(struct pt_regs * regs, struct user * dump)
363 struct task_struct *tsk = current;
365 dump->magic = CMAGIC;
366 dump->start_code = tsk->mm->start_code;
367 dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
369 dump->u_tsize = (tsk->mm->end_code - tsk->mm->start_code) >> PAGE_SHIFT;
370 dump->u_dsize = (tsk->mm->brk - tsk->mm->start_data + PAGE_SIZE - 1) >> PAGE_SHIFT;
371 dump->u_ssize = 0;
373 dump->u_debugreg[0] = tsk->thread.debug.bp[0].address;
374 dump->u_debugreg[1] = tsk->thread.debug.bp[1].address;
375 dump->u_debugreg[2] = tsk->thread.debug.bp[0].insn.arm;
376 dump->u_debugreg[3] = tsk->thread.debug.bp[1].insn.arm;
377 dump->u_debugreg[4] = tsk->thread.debug.nsaved;
379 if (dump->start_stack < 0x04000000)
380 dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
382 dump->regs = *regs;
383 dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
387 * This is the mechanism for creating a new kernel thread.
389 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
390 * who haven't done an "execve()") should use this: it will work within
391 * a system call from a "real" process, but the process memory space will
392 * not be free'd until both the parent and the child have exited.
394 pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
396 register unsigned int r0 asm("r0") = flags | CLONE_VM | CLONE_UNTRACED;
397 register unsigned int r1 asm("r1") = 0;
398 register pid_t __ret asm("r0");
400 __asm__ __volatile__(
401 __syscall(clone)" @ kernel_thread sys_clone \n\
402 movs %0, r0 @ if we are the child \n\
403 bne 1f \n\
404 mov fp, #0 @ ensure that fp is zero \n\
405 mov r0, %4 \n\
406 mov lr, pc \n\
407 mov pc, %3 \n\
408 b sys_exit \n\
409 1: "
410 : "=r" (__ret)
411 : "0" (r0), "r" (r1), "r" (fn), "r" (arg)
412 : "lr");
413 return __ret;
417 * These bracket the sleeping functions..
419 extern void scheduling_functions_start_here(void);
420 extern void scheduling_functions_end_here(void);
421 #define first_sched ((unsigned long) scheduling_functions_start_here)
422 #define last_sched ((unsigned long) scheduling_functions_end_here)
424 unsigned long get_wchan(struct task_struct *p)
426 unsigned long fp, lr;
427 unsigned long stack_page;
428 int count = 0;
429 if (!p || p == current || p->state == TASK_RUNNING)
430 return 0;
432 stack_page = 4096 + (unsigned long)p;
433 fp = thread_saved_fp(p);
434 do {
435 if (fp < stack_page || fp > 4092+stack_page)
436 return 0;
437 lr = pc_pointer (((unsigned long *)fp)[-1]);
438 if (lr < first_sched || lr > last_sched)
439 return lr;
440 fp = *(unsigned long *) (fp - 12);
441 } while (count ++ < 16);
442 return 0;