2 * linux/arch/arm26/kernel/process.c
4 * Copyright (C) 2003 Ian Molton - adapted for ARM26
5 * Copyright (C) 1996-2000 Russell King - Converted to ARM.
6 * Origional Copyright (C) 1995 Linus Torvalds
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
19 #include <linux/stddef.h>
20 #include <linux/unistd.h>
21 #include <linux/ptrace.h>
22 #include <linux/slab.h>
23 #include <linux/user.h>
24 #include <linux/a.out.h>
25 #include <linux/delay.h>
26 #include <linux/reboot.h>
27 #include <linux/interrupt.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 void disable_hlt(void)
46 EXPORT_SYMBOL(disable_hlt);
53 EXPORT_SYMBOL(enable_hlt);
55 static int __init nohlt_setup(char *__unused)
61 static int __init hlt_setup(char *__unused)
67 __setup("nohlt", nohlt_setup);
68 __setup("hlt", hlt_setup);
71 * The following aren't currently used.
73 void (*pm_idle)(void);
74 void (*pm_power_off)(void);
77 * This is our default idle handler. We need to disable
78 * interrupts here to ensure we don't miss a wakeup call.
80 void default_idle(void)
83 if (!need_resched() && !hlt_counter)
88 * The idle thread. We try to conserve power, while trying to keep
89 * overall latency low. The architecture specific idle is passed
90 * a value to indicate the level of "idleness" of the system.
94 /* endless idle loop with no priority at all */
97 void (*idle)(void) = pm_idle;
100 leds_event(led_idle_start);
101 while (!need_resched())
103 leds_event(led_idle_end);
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 EXPORT_SYMBOL(machine_halt);
125 void machine_power_off(void)
127 leds_event(led_halted);
132 EXPORT_SYMBOL(machine_power_off);
134 void machine_restart(char * __unused)
137 * Clean and disable cache, and turn off interrupts
142 * Tell the mm system that we are going to reboot -
143 * we may need it to insert some 1:1 mappings so that
146 setup_mm_for_reboot(reboot_mode);
149 * copy branch instruction to reset location and call it
152 *(unsigned long *)0 = *(unsigned long *)0x03800000;
153 ((void(*)(void))0)();
156 * Whoops - the architecture was unable to reboot.
157 * Tell the user! Should never happen...
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 printk("pc : [<%08lx>] lr : [<%08lx>] %s\n"
173 "sp : %08lx ip : %08lx fp : %08lx\n",
174 instruction_pointer(regs),
175 regs->ARM_lr, print_tainted(), regs->ARM_sp,
176 regs->ARM_ip, regs->ARM_fp);
177 printk("r10: %08lx r9 : %08lx r8 : %08lx\n",
178 regs->ARM_r10, regs->ARM_r9,
180 printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
181 regs->ARM_r7, regs->ARM_r6,
182 regs->ARM_r5, regs->ARM_r4);
183 printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
184 regs->ARM_r3, regs->ARM_r2,
185 regs->ARM_r1, regs->ARM_r0);
186 printk("Flags: %c%c%c%c",
187 flags & PSR_N_BIT ? 'N' : 'n',
188 flags & PSR_Z_BIT ? 'Z' : 'z',
189 flags & PSR_C_BIT ? 'C' : 'c',
190 flags & PSR_V_BIT ? 'V' : 'v');
191 printk(" IRQs o%s FIQs o%s Mode %s Segment %s\n",
192 interrupts_enabled(regs) ? "n" : "ff",
193 fast_interrupts_enabled(regs) ? "n" : "ff",
194 processor_modes[processor_mode(regs)],
195 get_fs() == get_ds() ? "kernel" : "user");
198 void show_fpregs(struct user_fp *regs)
202 for (i = 0; i < 8; i++) {
206 p = (unsigned long *)(regs->fpregs + i);
208 switch (regs->ftype[i]) {
209 case 1: type = 'f'; break;
210 case 2: type = 'd'; break;
211 case 3: type = 'e'; break;
212 default: type = '?'; break;
217 printk(" f%d(%c): %08lx %08lx %08lx%c",
218 i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
222 printk("FPSR: %08lx FPCR: %08lx\n",
223 (unsigned long)regs->fpsr,
224 (unsigned long)regs->fpcr);
228 * Task structure and kernel stack allocation.
230 static unsigned long *thread_info_head;
231 static unsigned int nr_thread_info;
233 extern unsigned long get_page_8k(int priority);
234 extern void free_page_8k(unsigned long page);
236 // FIXME - is this valid?
237 #define EXTRA_TASK_STRUCT 0
238 #define ll_alloc_task_struct() ((struct thread_info *)get_page_8k(GFP_KERNEL))
239 #define ll_free_task_struct(p) free_page_8k((unsigned long)(p))
241 //FIXME - do we use *task param below looks like we dont, which is ok?
242 //FIXME - if EXTRA_TASK_STRUCT is zero we can optimise the below away permanently. *IF* its supposed to be zero.
243 struct thread_info *alloc_thread_info(struct task_struct *task)
245 struct thread_info *thread = NULL;
247 if (EXTRA_TASK_STRUCT) {
248 unsigned long *p = thread_info_head;
251 thread_info_head = (unsigned long *)p[0];
254 thread = (struct thread_info *)p;
258 thread = ll_alloc_task_struct();
260 #ifdef CONFIG_MAGIC_SYSRQ
262 * The stack must be cleared if you want SYSRQ-T to
263 * give sensible stack usage information
266 char *p = (char *)thread;
267 memzero(p+KERNEL_STACK_SIZE, KERNEL_STACK_SIZE);
273 void free_thread_info(struct thread_info *thread)
275 if (EXTRA_TASK_STRUCT && nr_thread_info < EXTRA_TASK_STRUCT) {
276 unsigned long *p = (unsigned long *)thread;
277 p[0] = (unsigned long)thread_info_head;
278 thread_info_head = p;
281 ll_free_task_struct(thread);
285 * Free current thread data structures etc..
287 void exit_thread(void)
291 void flush_thread(void)
293 struct thread_info *thread = current_thread_info();
294 struct task_struct *tsk = current;
296 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
297 memset(&thread->fpstate, 0, sizeof(union fp_state));
299 current->used_math = 0;
302 void release_thread(struct task_struct *dead_task)
306 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
309 copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
310 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
312 struct thread_info *thread = p->thread_info;
313 struct pt_regs *childregs;
315 childregs = __get_user_regs(thread);
317 childregs->ARM_r0 = 0;
318 childregs->ARM_sp = esp;
320 memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
321 thread->cpu_context.sp = (unsigned long)childregs;
322 thread->cpu_context.pc = (unsigned long)ret_from_fork | MODE_SVC26 | PSR_I_BIT;
328 * fill in the fpe structure for a core dump...
330 int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
332 struct thread_info *thread = current_thread_info();
333 int used_math = current->used_math;
336 memcpy(fp, &thread->fpstate.soft, sizeof (*fp));
342 * fill in the user structure for a core dump..
344 void dump_thread(struct pt_regs * regs, struct user * dump)
346 struct task_struct *tsk = current;
348 dump->magic = CMAGIC;
349 dump->start_code = tsk->mm->start_code;
350 dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
352 dump->u_tsize = (tsk->mm->end_code - tsk->mm->start_code) >> PAGE_SHIFT;
353 dump->u_dsize = (tsk->mm->brk - tsk->mm->start_data + PAGE_SIZE - 1) >> PAGE_SHIFT;
356 dump->u_debugreg[0] = tsk->thread.debug.bp[0].address;
357 dump->u_debugreg[1] = tsk->thread.debug.bp[1].address;
358 dump->u_debugreg[2] = tsk->thread.debug.bp[0].insn;
359 dump->u_debugreg[3] = tsk->thread.debug.bp[1].insn;
360 dump->u_debugreg[4] = tsk->thread.debug.nsaved;
362 if (dump->start_stack < 0x04000000)
363 dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
366 dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
370 * This is the mechanism for creating a new kernel thread.
372 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
373 * who haven't done an "execve()") should use this: it will work within
374 * a system call from a "real" process, but the process memory space will
375 * not be free'd until both the parent and the child have exited.
376 * FIXME - taken from arm32
378 pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
380 register unsigned int r0 asm("r0") = flags | CLONE_VM | CLONE_UNTRACED;
381 register unsigned int r1 asm("r1") = 0;
382 register pid_t __ret asm("r0");
384 __asm__ __volatile__(
385 __syscall(clone)" @ kernel_thread sys_clone \n\
386 movs %0, r0 @ if we are the child \n\
388 mov fp, #0 @ ensure that fp is zero \n\
395 : "0" (r0), "r" (r1), "r" (fn), "r" (arg)
400 unsigned long get_wchan(struct task_struct *p)
402 unsigned long fp, lr;
403 unsigned long stack_page;
405 if (!p || p == current || p->state == TASK_RUNNING)
408 stack_page = 4096 + (unsigned long)p;
409 fp = thread_saved_fp(p);
411 if (fp < stack_page || fp > 4092+stack_page)
413 lr = pc_pointer (((unsigned long *)fp)[-1]);
414 if (!in_sched_functions(lr))
416 fp = *(unsigned long *) (fp - 12);
417 } while (count ++ < 16);