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.
13 #include <linux/config.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
17 #include <linux/stddef.h>
18 #include <linux/unistd.h>
19 #include <linux/ptrace.h>
20 #include <linux/slab.h>
21 #include <linux/user.h>
22 #include <linux/delay.h>
23 #include <linux/reboot.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
27 #include <asm/system.h>
29 #include <asm/uaccess.h>
31 extern const char *processor_modes[];
32 extern void setup_mm_for_reboot(char mode);
34 static volatile int hlt_counter;
36 #include <asm/arch/system.h>
38 void disable_hlt(void)
48 static int __init nohlt_setup(char *__unused)
54 static int __init hlt_setup(char *__unused)
60 __setup("nohlt", nohlt_setup);
61 __setup("hlt", hlt_setup);
64 * The following aren't currently used.
66 void (*pm_idle)(void);
67 void (*pm_power_off)(void);
70 * This is our default idle handler. We need to disable
71 * interrupts here to ensure we don't miss a wakeup call.
73 void default_idle(void)
76 if (!current->need_resched && !hlt_counter)
82 * The idle thread. We try to conserve power, while trying to keep
83 * overall latency low. The architecture specific idle is passed
84 * a value to indicate the level of "idleness" of the system.
88 /* endless idle loop with no priority at all */
91 current->counter = -100;
94 void (*idle)(void) = pm_idle;
97 leds_event(led_idle_start);
98 while (!current->need_resched)
100 leds_event(led_idle_end);
102 #ifndef CONFIG_NO_PGT_CACHE
108 static char reboot_mode = 'h';
110 int __init reboot_setup(char *str)
112 reboot_mode = str[0];
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);
130 void machine_restart(char * __unused)
133 * Clean and disable cache, and turn off interrupts
138 * Tell the mm system that we are going to reboot -
139 * we may need it to insert some 1:1 mappings so that
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.
154 printk("Reboot failed -- System halted\n");
158 void show_regs(struct pt_regs * regs)
162 flags = condition_codes(regs);
164 printk("pc : [<%08lx>] lr : [<%08lx>] %s\n"
165 "sp : %08lx ip : %08lx fp : %08lx\n",
166 instruction_pointer(regs),
167 regs->ARM_lr, print_tainted(), regs->ARM_sp,
168 regs->ARM_ip, regs->ARM_fp);
169 printk("r10: %08lx r9 : %08lx r8 : %08lx\n",
170 regs->ARM_r10, regs->ARM_r9,
172 printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
173 regs->ARM_r7, regs->ARM_r6,
174 regs->ARM_r5, regs->ARM_r4);
175 printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
176 regs->ARM_r3, regs->ARM_r2,
177 regs->ARM_r1, regs->ARM_r0);
178 printk("Flags: %c%c%c%c",
179 flags & CC_N_BIT ? 'N' : 'n',
180 flags & CC_Z_BIT ? 'Z' : 'z',
181 flags & CC_C_BIT ? 'C' : 'c',
182 flags & CC_V_BIT ? 'V' : 'v');
183 printk(" IRQs o%s FIQs o%s Mode %s%s Segment %s\n",
184 interrupts_enabled(regs) ? "n" : "ff",
185 fast_interrupts_enabled(regs) ? "n" : "ff",
186 processor_modes[processor_mode(regs)],
187 thumb_mode(regs) ? " (T)" : "",
188 get_fs() == get_ds() ? "kernel" : "user");
189 #if defined(CONFIG_CPU_32)
191 unsigned int ctrl, transbase, dac;
193 " mrc p15, 0, %0, c1, c0\n"
194 " mrc p15, 0, %1, c2, c0\n"
195 " mrc p15, 0, %2, c3, c0\n"
196 : "=r" (ctrl), "=r" (transbase), "=r" (dac));
197 printk("Control: %04X Table: %08X DAC: %08X\n",
198 ctrl, transbase, dac);
203 void show_fpregs(struct user_fp *regs)
207 for (i = 0; i < 8; i++) {
211 p = (unsigned long *)(regs->fpregs + i);
213 switch (regs->ftype[i]) {
214 case 1: type = 'f'; break;
215 case 2: type = 'd'; break;
216 case 3: type = 'e'; break;
217 default: type = '?'; break;
222 printk(" f%d(%c): %08lx %08lx %08lx%c",
223 i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
227 printk("FPSR: %08lx FPCR: %08lx\n",
228 (unsigned long)regs->fpsr,
229 (unsigned long)regs->fpcr);
233 * Task structure and kernel stack allocation.
235 static struct task_struct *task_struct_head;
236 static unsigned int nr_task_struct;
239 #define EXTRA_TASK_STRUCT 4
241 #define EXTRA_TASK_STRUCT 0
244 struct task_struct *alloc_task_struct(void)
246 struct task_struct *tsk;
248 if (EXTRA_TASK_STRUCT)
249 tsk = task_struct_head;
254 task_struct_head = tsk->next_task;
257 tsk = ll_alloc_task_struct();
261 * The stack must be cleared if you want SYSRQ-T to
262 * give sensible stack usage information
265 char *p = (char *)tsk;
266 memzero(p+KERNEL_STACK_SIZE, KERNEL_STACK_SIZE);
272 void __free_task_struct(struct task_struct *p)
274 if (EXTRA_TASK_STRUCT && nr_task_struct < EXTRA_TASK_STRUCT) {
275 p->next_task = task_struct_head;
276 task_struct_head = p;
279 ll_free_task_struct(p);
283 * Free current thread data structures etc..
285 void exit_thread(void)
289 static void default_fp_init(union fp_state *fp)
291 memset(fp, 0, sizeof(union fp_state));
294 void (*fp_init)(union fp_state *) = default_fp_init;
296 void flush_thread(void)
298 struct task_struct *tsk = current;
300 tsk->flags &= ~PF_USEDFPU;
303 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
304 fp_init(&tsk->thread.fpstate);
307 void release_thread(struct task_struct *dead_task)
311 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
314 copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
315 unsigned long unused, struct task_struct * p, struct pt_regs * regs)
317 struct pt_regs *childregs;
318 struct context_save_struct * save;
320 atomic_set(&p->thread.refcount, 1);
322 childregs = ((struct pt_regs *)((unsigned long)p + 8192 - 8)) - 1;
324 childregs->ARM_r0 = 0;
325 childregs->ARM_sp = esp;
327 save = ((struct context_save_struct *)(childregs)) - 1;
329 save->pc |= (unsigned long)ret_from_fork;
331 p->thread.save = save;
337 * fill in the fpe structure for a core dump...
339 int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
341 int used_math = current->used_math;
344 memcpy(fp, ¤t->thread.fpstate.soft, sizeof (*fp));
350 * fill in the user structure for a core dump..
352 void dump_thread(struct pt_regs * regs, struct user * dump)
354 struct task_struct *tsk = current;
356 dump->magic = CMAGIC;
357 dump->start_code = tsk->mm->start_code;
358 dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
360 dump->u_tsize = (tsk->mm->end_code - tsk->mm->start_code) >> PAGE_SHIFT;
361 dump->u_dsize = (tsk->mm->brk - tsk->mm->start_data + PAGE_SIZE - 1) >> PAGE_SHIFT;
364 dump->u_debugreg[0] = tsk->thread.debug.bp[0].address;
365 dump->u_debugreg[1] = tsk->thread.debug.bp[1].address;
366 dump->u_debugreg[2] = tsk->thread.debug.bp[0].insn.arm;
367 dump->u_debugreg[3] = tsk->thread.debug.bp[1].insn.arm;
368 dump->u_debugreg[4] = tsk->thread.debug.nsaved;
370 if (dump->start_stack < 0x04000000)
371 dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
374 dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
378 * This is the mechanism for creating a new kernel thread.
380 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
381 * who haven't done an "execve()") should use this: it will work within
382 * a system call from a "real" process, but the process memory space will
383 * not be free'd until both the parent and the child have exited.
385 pid_t arch_kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
389 __asm__ __volatile__(
390 "orr r0, %1, %2 @ kernel_thread sys_clone \n\
392 "__syscall(clone)" \n\
393 movs %0, r0 @ if we are the child \n\
395 mov fp, #0 @ ensure that fp is zero \n\
402 : "Ir" (flags), "I" (CLONE_VM), "r" (fn), "r" (arg)
408 * These bracket the sleeping functions..
410 extern void scheduling_functions_start_here(void);
411 extern void scheduling_functions_end_here(void);
412 #define first_sched ((unsigned long) scheduling_functions_start_here)
413 #define last_sched ((unsigned long) scheduling_functions_end_here)
415 unsigned long get_wchan(struct task_struct *p)
417 unsigned long fp, lr;
418 unsigned long stack_page;
420 if (!p || p == current || p->state == TASK_RUNNING)
423 stack_page = 4096 + (unsigned long)p;
424 fp = thread_saved_fp(&p->thread);
426 if (fp < stack_page || fp > 4092+stack_page)
428 lr = pc_pointer (((unsigned long *)fp)[-1]);
429 if (lr < first_sched || lr > last_sched)
431 fp = *(unsigned long *) (fp - 12);
432 } while (count ++ < 16);