2 * linux/arch/arm/kernel/process.c
4 * Copyright (C) 1996-2000 Russell King - Converted to ARM.
5 * Original 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.
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.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>
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)
48 EXPORT_SYMBOL(disable_hlt);
55 EXPORT_SYMBOL(enable_hlt);
57 static int __init nohlt_setup(char *__unused)
63 static int __init hlt_setup(char *__unused)
69 __setup("nohlt", nohlt_setup);
70 __setup("hlt", hlt_setup);
73 * The following aren't currently used.
75 void (*pm_idle)(void);
76 void (*pm_power_off)(void);
79 * This is our default idle handler. We need to disable
80 * interrupts here to ensure we don't miss a wakeup call.
82 void default_idle(void)
85 if (!need_resched() && !hlt_counter)
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.
97 /* endless idle loop with no priority at all */
99 void (*idle)(void) = pm_idle;
103 leds_event(led_idle_start);
104 while (!need_resched())
106 leds_event(led_idle_end);
112 static char reboot_mode = 'h';
114 int __init reboot_setup(char *str)
116 reboot_mode = str[0];
120 __setup("reboot=", reboot_setup);
122 void machine_halt(void)
126 EXPORT_SYMBOL(machine_halt);
128 void machine_power_off(void)
134 EXPORT_SYMBOL(machine_power_off);
136 void machine_restart(char * __unused)
139 * Clean and disable cache, and turn off interrupts
144 * Tell the mm system that we are going to reboot -
145 * we may need it to insert some 1:1 mappings so that
148 setup_mm_for_reboot(reboot_mode);
151 * Now call the architecture specific reboot code.
153 arch_reset(reboot_mode);
156 * Whoops - the architecture was unable to reboot.
160 printk("Reboot failed -- System halted\n");
164 EXPORT_SYMBOL(machine_restart);
166 void show_regs(struct pt_regs * regs)
170 flags = condition_codes(regs);
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,
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");
200 unsigned int ctrl, transbase, dac;
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);
211 void show_fpregs(struct user_fp *regs)
215 for (i = 0; i < 8; i++) {
219 p = (unsigned long *)(regs->fpregs + i);
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;
230 printk(" f%d(%c): %08lx %08lx %08lx%c",
231 i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
235 printk("FPSR: %08lx FPCR: %08lx\n",
236 (unsigned long)regs->fpsr,
237 (unsigned long)regs->fpcr);
241 * Task structure and kernel stack allocation.
243 static unsigned long *thread_info_head;
244 static unsigned int nr_thread_info;
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)
250 struct thread_info *alloc_thread_info(struct task_struct *task)
252 struct thread_info *thread = NULL;
254 if (EXTRA_TASK_STRUCT) {
255 unsigned long *p = thread_info_head;
258 thread_info_head = (unsigned long *)p[0];
261 thread = (struct thread_info *)p;
265 thread = ll_alloc_task_struct();
267 #ifdef CONFIG_MAGIC_SYSRQ
269 * The stack must be cleared if you want SYSRQ-T to
270 * give sensible stack usage information
273 char *p = (char *)thread;
274 memzero(p+KERNEL_STACK_SIZE, KERNEL_STACK_SIZE);
280 void free_thread_info(struct thread_info *thread)
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;
288 ll_free_task_struct(thread);
292 * Free current thread data structures etc..
294 void exit_thread(void)
298 static void default_fp_init(union fp_state *fp)
300 memset(fp, 0, sizeof(union fp_state));
303 void (*fp_init)(union fp_state *) = default_fp_init;
305 void flush_thread(void)
307 struct thread_info *thread = current_thread_info();
308 struct task_struct *tsk = current;
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);
315 void release_thread(struct task_struct *dead_task)
319 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
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)
325 struct thread_info *thread = p->thread_info;
326 struct pt_regs *childregs;
328 childregs = ((struct pt_regs *)((unsigned long)thread + THREAD_SIZE - 8)) - 1;
330 childregs->ARM_r0 = 0;
331 childregs->ARM_sp = stack_start;
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;
341 * fill in the fpe structure for a core dump...
343 int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
345 struct thread_info *thread = current_thread_info();
346 int used_math = thread->used_cp[1] | thread->used_cp[2];
349 memcpy(fp, &thread->fpstate.soft, sizeof (*fp));
351 return used_math != 0;
355 * fill in the user structure for a core dump..
357 void dump_thread(struct pt_regs * regs, struct user * dump)
359 struct task_struct *tsk = current;
361 dump->magic = CMAGIC;
362 dump->start_code = tsk->mm->start_code;
363 dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
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;
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;
375 if (dump->start_stack < 0x04000000)
376 dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
379 dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
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.
387 extern void kernel_thread_helper(void);
389 " .type kernel_thread_helper, #function\n"
390 "kernel_thread_helper:\n"
394 " .size kernel_thread_helper, . - kernel_thread_helper");
397 * Create a kernel thread.
399 pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
403 memset(®s, 0, sizeof(regs));
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;
411 return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, ®s, 0, NULL, NULL);
415 * These bracket the sleeping functions..
417 #define first_sched ((unsigned long) scheduling_functions_start_here)
418 #define last_sched ((unsigned long) scheduling_functions_end_here)
420 unsigned long get_wchan(struct task_struct *p)
422 unsigned long fp, lr;
423 unsigned long stack_page;
425 if (!p || p == current || p->state == TASK_RUNNING)
428 stack_page = 4096 + (unsigned long)p->thread_info;
429 fp = thread_saved_fp(p);
431 if (fp < stack_page || fp > 4092+stack_page)
433 lr = pc_pointer (((unsigned long *)fp)[-1]);
434 if (lr < first_sched || lr > last_sched)
436 fp = *(unsigned long *) (fp - 12);
437 } while (count ++ < 16);