]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/arch/arm/kernel/process.c | |
3 | * | |
4 | * Copyright (C) 1996-2000 Russell King - Converted to ARM. | |
5 | * Original 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 | ||
1da177e4 LT |
13 | #include <linux/module.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> | |
1da177e4 | 19 | #include <linux/user.h> |
1da177e4 LT |
20 | #include <linux/delay.h> |
21 | #include <linux/reboot.h> | |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/kallsyms.h> | |
24 | #include <linux/init.h> | |
a054a811 | 25 | #include <linux/cpu.h> |
84dff1a7 | 26 | #include <linux/elfcore.h> |
74617fb6 | 27 | #include <linux/pm.h> |
9e4559dd | 28 | #include <linux/tick.h> |
154c772e | 29 | #include <linux/utsname.h> |
33fa9b13 | 30 | #include <linux/uaccess.h> |
1da177e4 | 31 | |
9ca03a21 | 32 | #include <asm/cacheflush.h> |
1da177e4 LT |
33 | #include <asm/leds.h> |
34 | #include <asm/processor.h> | |
78ff18a4 | 35 | #include <asm/system.h> |
d6551e88 | 36 | #include <asm/thread_notify.h> |
2d7c11bf | 37 | #include <asm/stacktrace.h> |
2ea83398 | 38 | #include <asm/mach/time.h> |
1da177e4 | 39 | |
ae0a846e RK |
40 | static const char *processor_modes[] = { |
41 | "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" , | |
42 | "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26", | |
43 | "USER_32", "FIQ_32" , "IRQ_32" , "SVC_32" , "UK4_32" , "UK5_32" , "UK6_32" , "ABT_32" , | |
44 | "UK8_32" , "UK9_32" , "UK10_32", "UND_32" , "UK12_32", "UK13_32", "UK14_32", "SYS_32" | |
45 | }; | |
46 | ||
909d6c6c GD |
47 | static const char *isa_modes[] = { |
48 | "ARM" , "Thumb" , "Jazelle", "ThumbEE" | |
49 | }; | |
50 | ||
1da177e4 LT |
51 | extern void setup_mm_for_reboot(char mode); |
52 | ||
53 | static volatile int hlt_counter; | |
54 | ||
a09e64fb | 55 | #include <mach/system.h> |
1da177e4 LT |
56 | |
57 | void disable_hlt(void) | |
58 | { | |
59 | hlt_counter++; | |
60 | } | |
61 | ||
62 | EXPORT_SYMBOL(disable_hlt); | |
63 | ||
64 | void enable_hlt(void) | |
65 | { | |
66 | hlt_counter--; | |
67 | } | |
68 | ||
69 | EXPORT_SYMBOL(enable_hlt); | |
70 | ||
71 | static int __init nohlt_setup(char *__unused) | |
72 | { | |
73 | hlt_counter = 1; | |
74 | return 1; | |
75 | } | |
76 | ||
77 | static int __init hlt_setup(char *__unused) | |
78 | { | |
79 | hlt_counter = 0; | |
80 | return 1; | |
81 | } | |
82 | ||
83 | __setup("nohlt", nohlt_setup); | |
84 | __setup("hlt", hlt_setup); | |
85 | ||
be093beb | 86 | void arm_machine_restart(char mode, const char *cmd) |
74617fb6 | 87 | { |
9ca03a21 RK |
88 | /* Disable interrupts first */ |
89 | local_irq_disable(); | |
90 | local_fiq_disable(); | |
74617fb6 RP |
91 | |
92 | /* | |
93 | * Tell the mm system that we are going to reboot - | |
94 | * we may need it to insert some 1:1 mappings so that | |
95 | * soft boot works. | |
96 | */ | |
97 | setup_mm_for_reboot(mode); | |
98 | ||
9ca03a21 RK |
99 | /* Clean and invalidate caches */ |
100 | flush_cache_all(); | |
101 | ||
102 | /* Turn off caching */ | |
103 | cpu_proc_fin(); | |
104 | ||
105 | /* Push out any further dirty data, and ensure cache is empty */ | |
106 | flush_cache_all(); | |
107 | ||
74617fb6 RP |
108 | /* |
109 | * Now call the architecture specific reboot code. | |
110 | */ | |
be093beb | 111 | arch_reset(mode, cmd); |
74617fb6 RP |
112 | |
113 | /* | |
114 | * Whoops - the architecture was unable to reboot. | |
115 | * Tell the user! | |
116 | */ | |
117 | mdelay(1000); | |
118 | printk("Reboot failed -- System halted\n"); | |
119 | while (1); | |
120 | } | |
121 | ||
1da177e4 | 122 | /* |
74617fb6 | 123 | * Function pointers to optional machine specific functions |
1da177e4 | 124 | */ |
1da177e4 LT |
125 | void (*pm_power_off)(void); |
126 | EXPORT_SYMBOL(pm_power_off); | |
127 | ||
be093beb | 128 | void (*arm_pm_restart)(char str, const char *cmd) = arm_machine_restart; |
74617fb6 RP |
129 | EXPORT_SYMBOL_GPL(arm_pm_restart); |
130 | ||
131 | ||
1da177e4 LT |
132 | /* |
133 | * This is our default idle handler. We need to disable | |
134 | * interrupts here to ensure we don't miss a wakeup call. | |
135 | */ | |
84dff1a7 | 136 | static void default_idle(void) |
1da177e4 | 137 | { |
9ccdac36 RK |
138 | if (!need_resched()) |
139 | arch_idle(); | |
140 | local_irq_enable(); | |
1da177e4 LT |
141 | } |
142 | ||
9ccdac36 RK |
143 | void (*pm_idle)(void) = default_idle; |
144 | EXPORT_SYMBOL(pm_idle); | |
145 | ||
1da177e4 | 146 | /* |
9ccdac36 RK |
147 | * The idle thread, has rather strange semantics for calling pm_idle, |
148 | * but this is what x86 does and we need to do the same, so that | |
149 | * things like cpuidle get called in the same way. The only difference | |
150 | * is that we always respect 'hlt_counter' to prevent low power idle. | |
1da177e4 LT |
151 | */ |
152 | void cpu_idle(void) | |
153 | { | |
154 | local_fiq_enable(); | |
155 | ||
156 | /* endless idle loop with no priority at all */ | |
157 | while (1) { | |
9ccdac36 RK |
158 | tick_nohz_stop_sched_tick(1); |
159 | leds_event(led_idle_start); | |
160 | while (!need_resched()) { | |
a054a811 | 161 | #ifdef CONFIG_HOTPLUG_CPU |
9ccdac36 RK |
162 | if (cpu_is_offline(smp_processor_id())) |
163 | cpu_die(); | |
a054a811 RK |
164 | #endif |
165 | ||
9ccdac36 RK |
166 | local_irq_disable(); |
167 | if (hlt_counter) { | |
168 | local_irq_enable(); | |
169 | cpu_relax(); | |
170 | } else { | |
171 | stop_critical_timings(); | |
172 | pm_idle(); | |
173 | start_critical_timings(); | |
174 | /* | |
175 | * This will eventually be removed - pm_idle | |
176 | * functions should always return with IRQs | |
177 | * enabled. | |
178 | */ | |
179 | WARN_ON(irqs_disabled()); | |
180 | local_irq_enable(); | |
181 | } | |
182 | } | |
1da177e4 | 183 | leds_event(led_idle_end); |
9e4559dd | 184 | tick_nohz_restart_sched_tick(); |
5bfb5d69 | 185 | preempt_enable_no_resched(); |
1da177e4 | 186 | schedule(); |
5bfb5d69 | 187 | preempt_disable(); |
1da177e4 LT |
188 | } |
189 | } | |
190 | ||
191 | static char reboot_mode = 'h'; | |
192 | ||
193 | int __init reboot_setup(char *str) | |
194 | { | |
195 | reboot_mode = str[0]; | |
196 | return 1; | |
197 | } | |
198 | ||
199 | __setup("reboot=", reboot_setup); | |
200 | ||
201 | void machine_halt(void) | |
202 | { | |
203 | } | |
204 | ||
1da177e4 LT |
205 | |
206 | void machine_power_off(void) | |
207 | { | |
208 | if (pm_power_off) | |
209 | pm_power_off(); | |
210 | } | |
211 | ||
be093beb | 212 | void machine_restart(char *cmd) |
1da177e4 | 213 | { |
be093beb | 214 | arm_pm_restart(reboot_mode, cmd); |
1da177e4 LT |
215 | } |
216 | ||
652a12ef | 217 | void __show_regs(struct pt_regs *regs) |
1da177e4 | 218 | { |
154c772e RK |
219 | unsigned long flags; |
220 | char buf[64]; | |
1da177e4 | 221 | |
154c772e | 222 | printk("CPU: %d %s (%s %.*s)\n", |
22325525 RV |
223 | raw_smp_processor_id(), print_tainted(), |
224 | init_utsname()->release, | |
154c772e RK |
225 | (int)strcspn(init_utsname()->version, " "), |
226 | init_utsname()->version); | |
1da177e4 LT |
227 | print_symbol("PC is at %s\n", instruction_pointer(regs)); |
228 | print_symbol("LR is at %s\n", regs->ARM_lr); | |
154c772e | 229 | printk("pc : [<%08lx>] lr : [<%08lx>] psr: %08lx\n" |
1da177e4 | 230 | "sp : %08lx ip : %08lx fp : %08lx\n", |
154c772e RK |
231 | regs->ARM_pc, regs->ARM_lr, regs->ARM_cpsr, |
232 | regs->ARM_sp, regs->ARM_ip, regs->ARM_fp); | |
1da177e4 LT |
233 | printk("r10: %08lx r9 : %08lx r8 : %08lx\n", |
234 | regs->ARM_r10, regs->ARM_r9, | |
235 | regs->ARM_r8); | |
236 | printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n", | |
237 | regs->ARM_r7, regs->ARM_r6, | |
238 | regs->ARM_r5, regs->ARM_r4); | |
239 | printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n", | |
240 | regs->ARM_r3, regs->ARM_r2, | |
241 | regs->ARM_r1, regs->ARM_r0); | |
154c772e RK |
242 | |
243 | flags = regs->ARM_cpsr; | |
244 | buf[0] = flags & PSR_N_BIT ? 'N' : 'n'; | |
245 | buf[1] = flags & PSR_Z_BIT ? 'Z' : 'z'; | |
246 | buf[2] = flags & PSR_C_BIT ? 'C' : 'c'; | |
247 | buf[3] = flags & PSR_V_BIT ? 'V' : 'v'; | |
248 | buf[4] = '\0'; | |
249 | ||
909d6c6c | 250 | printk("Flags: %s IRQs o%s FIQs o%s Mode %s ISA %s Segment %s\n", |
154c772e | 251 | buf, interrupts_enabled(regs) ? "n" : "ff", |
1da177e4 LT |
252 | fast_interrupts_enabled(regs) ? "n" : "ff", |
253 | processor_modes[processor_mode(regs)], | |
909d6c6c | 254 | isa_modes[isa_mode(regs)], |
1da177e4 | 255 | get_fs() == get_ds() ? "kernel" : "user"); |
154c772e | 256 | #ifdef CONFIG_CPU_CP15 |
1da177e4 | 257 | { |
f12d0d7c | 258 | unsigned int ctrl; |
154c772e RK |
259 | |
260 | buf[0] = '\0'; | |
f12d0d7c | 261 | #ifdef CONFIG_CPU_CP15_MMU |
154c772e RK |
262 | { |
263 | unsigned int transbase, dac; | |
264 | asm("mrc p15, 0, %0, c2, c0\n\t" | |
265 | "mrc p15, 0, %1, c3, c0\n" | |
266 | : "=r" (transbase), "=r" (dac)); | |
267 | snprintf(buf, sizeof(buf), " Table: %08x DAC: %08x", | |
268 | transbase, dac); | |
269 | } | |
f12d0d7c | 270 | #endif |
154c772e RK |
271 | asm("mrc p15, 0, %0, c1, c0\n" : "=r" (ctrl)); |
272 | ||
273 | printk("Control: %08x%s\n", ctrl, buf); | |
274 | } | |
f12d0d7c | 275 | #endif |
1da177e4 LT |
276 | } |
277 | ||
652a12ef RK |
278 | void show_regs(struct pt_regs * regs) |
279 | { | |
280 | printk("\n"); | |
19c5870c | 281 | printk("Pid: %d, comm: %20s\n", task_pid_nr(current), current->comm); |
652a12ef RK |
282 | __show_regs(regs); |
283 | __backtrace(); | |
284 | } | |
285 | ||
797245f5 RK |
286 | ATOMIC_NOTIFIER_HEAD(thread_notify_head); |
287 | ||
288 | EXPORT_SYMBOL_GPL(thread_notify_head); | |
289 | ||
1da177e4 LT |
290 | /* |
291 | * Free current thread data structures etc.. | |
292 | */ | |
293 | void exit_thread(void) | |
294 | { | |
797245f5 | 295 | thread_notify(THREAD_NOTIFY_EXIT, current_thread_info()); |
1da177e4 LT |
296 | } |
297 | ||
1da177e4 LT |
298 | void flush_thread(void) |
299 | { | |
300 | struct thread_info *thread = current_thread_info(); | |
301 | struct task_struct *tsk = current; | |
302 | ||
303 | memset(thread->used_cp, 0, sizeof(thread->used_cp)); | |
304 | memset(&tsk->thread.debug, 0, sizeof(struct debug_info)); | |
d6551e88 RK |
305 | memset(&thread->fpstate, 0, sizeof(union fp_state)); |
306 | ||
307 | thread_notify(THREAD_NOTIFY_FLUSH, thread); | |
1da177e4 LT |
308 | } |
309 | ||
310 | void release_thread(struct task_struct *dead_task) | |
311 | { | |
1da177e4 LT |
312 | } |
313 | ||
314 | asmlinkage void ret_from_fork(void) __asm__("ret_from_fork"); | |
315 | ||
316 | int | |
6f2c55b8 | 317 | copy_thread(unsigned long clone_flags, unsigned long stack_start, |
1da177e4 LT |
318 | unsigned long stk_sz, struct task_struct *p, struct pt_regs *regs) |
319 | { | |
815d5ec8 AV |
320 | struct thread_info *thread = task_thread_info(p); |
321 | struct pt_regs *childregs = task_pt_regs(p); | |
1da177e4 | 322 | |
1da177e4 LT |
323 | *childregs = *regs; |
324 | childregs->ARM_r0 = 0; | |
325 | childregs->ARM_sp = stack_start; | |
326 | ||
327 | memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save)); | |
328 | thread->cpu_context.sp = (unsigned long)childregs; | |
329 | thread->cpu_context.pc = (unsigned long)ret_from_fork; | |
330 | ||
331 | if (clone_flags & CLONE_SETTLS) | |
332 | thread->tp_value = regs->ARM_r3; | |
333 | ||
334 | return 0; | |
335 | } | |
336 | ||
cde3f860 AB |
337 | /* |
338 | * Fill in the task's elfregs structure for a core dump. | |
339 | */ | |
340 | int dump_task_regs(struct task_struct *t, elf_gregset_t *elfregs) | |
341 | { | |
342 | elf_core_copy_regs(elfregs, task_pt_regs(t)); | |
343 | return 1; | |
344 | } | |
345 | ||
1da177e4 LT |
346 | /* |
347 | * fill in the fpe structure for a core dump... | |
348 | */ | |
349 | int dump_fpu (struct pt_regs *regs, struct user_fp *fp) | |
350 | { | |
351 | struct thread_info *thread = current_thread_info(); | |
352 | int used_math = thread->used_cp[1] | thread->used_cp[2]; | |
353 | ||
354 | if (used_math) | |
355 | memcpy(fp, &thread->fpstate.soft, sizeof (*fp)); | |
356 | ||
357 | return used_math != 0; | |
358 | } | |
359 | EXPORT_SYMBOL(dump_fpu); | |
360 | ||
1da177e4 LT |
361 | /* |
362 | * Shuffle the argument into the correct register before calling the | |
ac78884e RK |
363 | * thread function. r4 is the thread argument, r5 is the pointer to |
364 | * the thread function, and r6 points to the exit function. | |
1da177e4 LT |
365 | */ |
366 | extern void kernel_thread_helper(void); | |
4260415f | 367 | asm( ".pushsection .text\n" |
1da177e4 LT |
368 | " .align\n" |
369 | " .type kernel_thread_helper, #function\n" | |
370 | "kernel_thread_helper:\n" | |
ac78884e RK |
371 | #ifdef CONFIG_TRACE_IRQFLAGS |
372 | " bl trace_hardirqs_on\n" | |
373 | #endif | |
374 | " msr cpsr_c, r7\n" | |
375 | " mov r0, r4\n" | |
376 | " mov lr, r6\n" | |
377 | " mov pc, r5\n" | |
1da177e4 | 378 | " .size kernel_thread_helper, . - kernel_thread_helper\n" |
4260415f | 379 | " .popsection"); |
1da177e4 | 380 | |
feb97c36 CM |
381 | #ifdef CONFIG_ARM_UNWIND |
382 | extern void kernel_thread_exit(long code); | |
4260415f | 383 | asm( ".pushsection .text\n" |
feb97c36 CM |
384 | " .align\n" |
385 | " .type kernel_thread_exit, #function\n" | |
386 | "kernel_thread_exit:\n" | |
387 | " .fnstart\n" | |
388 | " .cantunwind\n" | |
389 | " bl do_exit\n" | |
390 | " nop\n" | |
391 | " .fnend\n" | |
392 | " .size kernel_thread_exit, . - kernel_thread_exit\n" | |
4260415f | 393 | " .popsection"); |
feb97c36 CM |
394 | #else |
395 | #define kernel_thread_exit do_exit | |
396 | #endif | |
397 | ||
1da177e4 LT |
398 | /* |
399 | * Create a kernel thread. | |
400 | */ | |
401 | pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) | |
402 | { | |
403 | struct pt_regs regs; | |
404 | ||
405 | memset(®s, 0, sizeof(regs)); | |
406 | ||
ac78884e RK |
407 | regs.ARM_r4 = (unsigned long)arg; |
408 | regs.ARM_r5 = (unsigned long)fn; | |
409 | regs.ARM_r6 = (unsigned long)kernel_thread_exit; | |
410 | regs.ARM_r7 = SVC_MODE | PSR_ENDSTATE | PSR_ISETSTATE; | |
1da177e4 | 411 | regs.ARM_pc = (unsigned long)kernel_thread_helper; |
ac78884e | 412 | regs.ARM_cpsr = regs.ARM_r7 | PSR_I_BIT; |
1da177e4 LT |
413 | |
414 | return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, ®s, 0, NULL, NULL); | |
415 | } | |
416 | EXPORT_SYMBOL(kernel_thread); | |
417 | ||
418 | unsigned long get_wchan(struct task_struct *p) | |
419 | { | |
2d7c11bf | 420 | struct stackframe frame; |
1da177e4 LT |
421 | int count = 0; |
422 | if (!p || p == current || p->state == TASK_RUNNING) | |
423 | return 0; | |
424 | ||
2d7c11bf CM |
425 | frame.fp = thread_saved_fp(p); |
426 | frame.sp = thread_saved_sp(p); | |
427 | frame.lr = 0; /* recovered from the stack */ | |
428 | frame.pc = thread_saved_pc(p); | |
1da177e4 | 429 | do { |
2d7c11bf CM |
430 | int ret = unwind_frame(&frame); |
431 | if (ret < 0) | |
1da177e4 | 432 | return 0; |
2d7c11bf CM |
433 | if (!in_sched_functions(frame.pc)) |
434 | return frame.pc; | |
1da177e4 LT |
435 | } while (count ++ < 16); |
436 | return 0; | |
437 | } |