File: /usr/src/linux/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 * Origional 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/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/mm.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>
26
27 #include <asm/system.h>
28 #include <asm/io.h>
29 #include <asm/leds.h>
30 #include <asm/uaccess.h>
31
32 /*
33 * Values for cpu_do_idle()
34 */
35 #define IDLE_WAIT_SLOW 0
36 #define IDLE_WAIT_FAST 1
37 #define IDLE_CLOCK_SLOW 2
38 #define IDLE_CLOCK_FAST 3
39
40 extern const char *processor_modes[];
41 extern void setup_mm_for_reboot(char mode);
42
43 static volatile int hlt_counter;
44
45 #include <asm/arch/system.h>
46
47 void disable_hlt(void)
48 {
49 hlt_counter++;
50 }
51
52 void enable_hlt(void)
53 {
54 hlt_counter--;
55 }
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 void (*pm_power_off)(void);
77
78 /*
79 * The idle thread. We try to conserve power, while trying to keep
80 * overall latency low. The architecture specific idle is passed
81 * a value to indicate the level of "idleness" of the system.
82 */
83 void cpu_idle(void)
84 {
85 /* endless idle loop with no priority at all */
86 init_idle();
87 current->nice = 20;
88 current->counter = -100;
89
90 while (1) {
91 void (*idle)(void) = pm_idle;
92 if (!idle)
93 idle = arch_idle;
94 leds_event(led_idle_start);
95 while (!current->need_resched)
96 idle();
97 leds_event(led_idle_end);
98 schedule();
99 #ifndef CONFIG_NO_PGT_CACHE
100 check_pgt_cache();
101 #endif
102 }
103 }
104
105 static char reboot_mode = 'h';
106
107 int __init reboot_setup(char *str)
108 {
109 reboot_mode = str[0];
110 return 1;
111 }
112
113 __setup("reboot=", reboot_setup);
114
115 void machine_halt(void)
116 {
117 leds_event(led_halted);
118 }
119
120 void machine_power_off(void)
121 {
122 leds_event(led_halted);
123 if (pm_power_off)
124 pm_power_off();
125 }
126
127 void machine_restart(char * __unused)
128 {
129 /*
130 * Clean and disable cache, and turn off interrupts
131 */
132 cpu_proc_fin();
133
134 /*
135 * Tell the mm system that we are going to reboot -
136 * we may need it to insert some 1:1 mappings so that
137 * soft boot works.
138 */
139 setup_mm_for_reboot(reboot_mode);
140
141 /*
142 * Now call the architecture specific reboot code.
143 */
144 arch_reset(reboot_mode);
145
146 /*
147 * Whoops - the architecture was unable to reboot.
148 * Tell the user!
149 */
150 mdelay(1000);
151 printk("Reboot failed -- System halted\n");
152 while (1);
153 }
154
155 void show_regs(struct pt_regs * regs)
156 {
157 unsigned long flags;
158
159 flags = condition_codes(regs);
160
161 printk("pc : [<%08lx>] lr : [<%08lx>]\n"
162 "sp : %08lx ip : %08lx fp : %08lx\n",
163 instruction_pointer(regs),
164 regs->ARM_lr, regs->ARM_sp,
165 regs->ARM_ip, regs->ARM_fp);
166 printk("r10: %08lx r9 : %08lx r8 : %08lx\n",
167 regs->ARM_r10, regs->ARM_r9,
168 regs->ARM_r8);
169 printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
170 regs->ARM_r7, regs->ARM_r6,
171 regs->ARM_r5, regs->ARM_r4);
172 printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
173 regs->ARM_r3, regs->ARM_r2,
174 regs->ARM_r1, regs->ARM_r0);
175 printk("Flags: %c%c%c%c",
176 flags & CC_N_BIT ? 'N' : 'n',
177 flags & CC_Z_BIT ? 'Z' : 'z',
178 flags & CC_C_BIT ? 'C' : 'c',
179 flags & CC_V_BIT ? 'V' : 'v');
180 printk(" IRQs %s FIQs %s Mode %s%s Segment %s\n",
181 interrupts_enabled(regs) ? "on" : "off",
182 fast_interrupts_enabled(regs) ? "on" : "off",
183 processor_modes[processor_mode(regs)],
184 thumb_mode(regs) ? " (T)" : "",
185 get_fs() == get_ds() ? "kernel" : "user");
186 #if defined(CONFIG_CPU_32)
187 {
188 int ctrl, transbase, dac;
189 __asm__ (
190 " mrc p15, 0, %0, c1, c0\n"
191 " mrc p15, 0, %1, c2, c0\n"
192 " mrc p15, 0, %2, c3, c0\n"
193 : "=r" (ctrl), "=r" (transbase), "=r" (dac));
194 printk("Control: %04X Table: %08X DAC: %08X\n",
195 ctrl, transbase, dac);
196 }
197 #endif
198 }
199
200 void show_fpregs(struct user_fp *regs)
201 {
202 int i;
203
204 for (i = 0; i < 8; i++) {
205 unsigned long *p;
206 char type;
207
208 p = (unsigned long *)(regs->fpregs + i);
209
210 switch (regs->ftype[i]) {
211 case 1: type = 'f'; break;
212 case 2: type = 'd'; break;
213 case 3: type = 'e'; break;
214 default: type = '?'; break;
215 }
216 if (regs->init_flag)
217 type = '?';
218
219 printk(" f%d(%c): %08lx %08lx %08lx%c",
220 i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
221 }
222
223
224 printk("FPSR: %08lx FPCR: %08lx\n",
225 (unsigned long)regs->fpsr,
226 (unsigned long)regs->fpcr);
227 }
228
229 /*
230 * Task structure and kernel stack allocation.
231 */
232 static struct task_struct *task_struct_head;
233 static unsigned int nr_task_struct;
234
235 #ifdef CONFIG_CPU_32
236 #define EXTRA_TASK_STRUCT 4
237 #else
238 #define EXTRA_TASK_STRUCT 0
239 #endif
240
241 struct task_struct *alloc_task_struct(void)
242 {
243 struct task_struct *tsk;
244
245 if (EXTRA_TASK_STRUCT)
246 tsk = task_struct_head;
247 else
248 tsk = NULL;
249
250 if (tsk) {
251 task_struct_head = tsk->next_task;
252 nr_task_struct -= 1;
253 } else
254 tsk = ll_alloc_task_struct();
255
256 #ifdef CONFIG_SYSRQ
257 /*
258 * The stack must be cleared if you want SYSRQ-T to
259 * give sensible stack usage information
260 */
261 if (tsk) {
262 char *p = (char *)tsk;
263 memzero(p+KERNEL_STACK_SIZE, KERNEL_STACK_SIZE);
264 }
265 #endif
266 return tsk;
267 }
268
269 void __free_task_struct(struct task_struct *p)
270 {
271 if (EXTRA_TASK_STRUCT && nr_task_struct < EXTRA_TASK_STRUCT) {
272 p->next_task = task_struct_head;
273 task_struct_head = p;
274 nr_task_struct += 1;
275 } else
276 ll_free_task_struct(p);
277 }
278
279 /*
280 * Free current thread data structures etc..
281 */
282 void exit_thread(void)
283 {
284 }
285
286 void flush_thread(void)
287 {
288 memset(¤t->thread.debug, 0, sizeof(struct debug_info));
289 memset(¤t->thread.fpstate, 0, sizeof(union fp_state));
290 current->used_math = 0;
291 current->flags &= ~PF_USEDFPU;
292 }
293
294 void release_thread(struct task_struct *dead_task)
295 {
296 }
297
298 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
299
300 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
301 unsigned long unused,
302 struct task_struct * p, struct pt_regs * regs)
303 {
304 struct pt_regs * childregs;
305 struct context_save_struct * save;
306
307 atomic_set(&p->thread.refcount, 1);
308
309 childregs = ((struct pt_regs *)((unsigned long)p + 8192)) - 1;
310 *childregs = *regs;
311 childregs->ARM_r0 = 0;
312 childregs->ARM_sp = esp;
313
314 save = ((struct context_save_struct *)(childregs)) - 1;
315 *save = INIT_CSS;
316 save->pc |= (unsigned long)ret_from_fork;
317
318 p->thread.save = save;
319
320 return 0;
321 }
322
323 /*
324 * fill in the fpe structure for a core dump...
325 */
326 int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
327 {
328 if (current->used_math)
329 memcpy(fp, ¤t->thread.fpstate.soft, sizeof (*fp));
330
331 return current->used_math;
332 }
333
334 /*
335 * fill in the user structure for a core dump..
336 */
337 void dump_thread(struct pt_regs * regs, struct user * dump)
338 {
339 struct task_struct *tsk = current;
340
341 dump->magic = CMAGIC;
342 dump->start_code = tsk->mm->start_code;
343 dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
344
345 dump->u_tsize = (tsk->mm->end_code - tsk->mm->start_code) >> PAGE_SHIFT;
346 dump->u_dsize = (tsk->mm->brk - tsk->mm->start_data + PAGE_SIZE - 1) >> PAGE_SHIFT;
347 dump->u_ssize = 0;
348
349 dump->u_debugreg[0] = tsk->thread.debug.bp[0].address;
350 dump->u_debugreg[1] = tsk->thread.debug.bp[1].address;
351 dump->u_debugreg[2] = tsk->thread.debug.bp[0].insn;
352 dump->u_debugreg[3] = tsk->thread.debug.bp[1].insn;
353 dump->u_debugreg[4] = tsk->thread.debug.nsaved;
354
355 if (dump->start_stack < 0x04000000)
356 dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
357
358 dump->regs = *regs;
359 dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
360 }
361
362 /*
363 * This is the mechanism for creating a new kernel thread.
364 *
365 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
366 * who haven't done an "execve()") should use this: it will work within
367 * a system call from a "real" process, but the process memory space will
368 * not be free'd until both the parent and the child have exited.
369 */
370 pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
371 {
372 pid_t __ret;
373
374 __asm__ __volatile__(
375 "orr r0, %1, %2 @ kernel_thread sys_clone
376 mov r1, #0
377 "__syscall(clone)"
378 movs %0, r0 @ if we are the child
379 bne 1f
380 mov fp, #0 @ ensure that fp is zero
381 mov r0, %4
382 mov lr, pc
383 mov pc, %3
384 b sys_exit
385 1: "
386 : "=r" (__ret)
387 : "Ir" (flags), "I" (CLONE_VM), "r" (fn), "r" (arg)
388 : "r0", "r1", "lr");
389 return __ret;
390 }
391
392 /*
393 * These bracket the sleeping functions..
394 */
395 extern void scheduling_functions_start_here(void);
396 extern void scheduling_functions_end_here(void);
397 #define first_sched ((unsigned long) scheduling_functions_start_here)
398 #define last_sched ((unsigned long) scheduling_functions_end_here)
399
400 unsigned long get_wchan(struct task_struct *p)
401 {
402 unsigned long fp, lr;
403 unsigned long stack_page;
404 int count = 0;
405 if (!p || p == current || p->state == TASK_RUNNING)
406 return 0;
407
408 stack_page = 4096 + (unsigned long)p;
409 fp = get_css_fp(&p->thread);
410 do {
411 if (fp < stack_page || fp > 4092+stack_page)
412 return 0;
413 lr = pc_pointer (((unsigned long *)fp)[-1]);
414 if (lr < first_sched || lr > last_sched)
415 return lr;
416 fp = *(unsigned long *) (fp - 12);
417 } while (count ++ < 16);
418 return 0;
419 }
420