process.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/stddef.h>
  17. #include <linux/unistd.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/slab.h>
  20. #include <linux/user.h>
  21. #include <linux/a.out.h>
  22. #include <linux/delay.h>
  23. #include <linux/reboot.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/kallsyms.h>
  26. #include <linux/init.h>
  27. #include <linux/cpu.h>
  28. #include <linux/elfcore.h>
  29. #include <linux/pm.h>
  30. #include <asm/leds.h>
  31. #include <asm/processor.h>
  32. #include <asm/system.h>
  33. #include <asm/thread_notify.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/mach/time.h>
  36. extern const char *processor_modes[];
  37. extern void setup_mm_for_reboot(char mode);
  38. static volatile int hlt_counter;
  39. #include <asm/arch/system.h>
  40. void disable_hlt(void)
  41. {
  42. hlt_counter++;
  43. }
  44. EXPORT_SYMBOL(disable_hlt);
  45. void enable_hlt(void)
  46. {
  47. hlt_counter--;
  48. }
  49. EXPORT_SYMBOL(enable_hlt);
  50. static int __init nohlt_setup(char *__unused)
  51. {
  52. hlt_counter = 1;
  53. return 1;
  54. }
  55. static int __init hlt_setup(char *__unused)
  56. {
  57. hlt_counter = 0;
  58. return 1;
  59. }
  60. __setup("nohlt", nohlt_setup);
  61. __setup("hlt", hlt_setup);
  62. void arm_machine_restart(char mode)
  63. {
  64. /*
  65. * Clean and disable cache, and turn off interrupts
  66. */
  67. cpu_proc_fin();
  68. /*
  69. * Tell the mm system that we are going to reboot -
  70. * we may need it to insert some 1:1 mappings so that
  71. * soft boot works.
  72. */
  73. setup_mm_for_reboot(mode);
  74. /*
  75. * Now call the architecture specific reboot code.
  76. */
  77. arch_reset(mode);
  78. /*
  79. * Whoops - the architecture was unable to reboot.
  80. * Tell the user!
  81. */
  82. mdelay(1000);
  83. printk("Reboot failed -- System halted\n");
  84. while (1);
  85. }
  86. /*
  87. * Function pointers to optional machine specific functions
  88. */
  89. void (*pm_idle)(void);
  90. EXPORT_SYMBOL(pm_idle);
  91. void (*pm_power_off)(void);
  92. EXPORT_SYMBOL(pm_power_off);
  93. void (*arm_pm_restart)(char str) = arm_machine_restart;
  94. EXPORT_SYMBOL_GPL(arm_pm_restart);
  95. /*
  96. * This is our default idle handler. We need to disable
  97. * interrupts here to ensure we don't miss a wakeup call.
  98. */
  99. static void default_idle(void)
  100. {
  101. if (hlt_counter)
  102. cpu_relax();
  103. else {
  104. local_irq_disable();
  105. if (!need_resched()) {
  106. timer_dyn_reprogram();
  107. arch_idle();
  108. }
  109. local_irq_enable();
  110. }
  111. }
  112. /*
  113. * The idle thread. We try to conserve power, while trying to keep
  114. * overall latency low. The architecture specific idle is passed
  115. * a value to indicate the level of "idleness" of the system.
  116. */
  117. void cpu_idle(void)
  118. {
  119. local_fiq_enable();
  120. /* endless idle loop with no priority at all */
  121. while (1) {
  122. void (*idle)(void) = pm_idle;
  123. #ifdef CONFIG_HOTPLUG_CPU
  124. if (cpu_is_offline(smp_processor_id())) {
  125. leds_event(led_idle_start);
  126. cpu_die();
  127. }
  128. #endif
  129. if (!idle)
  130. idle = default_idle;
  131. leds_event(led_idle_start);
  132. while (!need_resched())
  133. idle();
  134. leds_event(led_idle_end);
  135. preempt_enable_no_resched();
  136. schedule();
  137. preempt_disable();
  138. }
  139. }
  140. static char reboot_mode = 'h';
  141. int __init reboot_setup(char *str)
  142. {
  143. reboot_mode = str[0];
  144. return 1;
  145. }
  146. __setup("reboot=", reboot_setup);
  147. void machine_halt(void)
  148. {
  149. }
  150. void machine_power_off(void)
  151. {
  152. if (pm_power_off)
  153. pm_power_off();
  154. }
  155. void machine_restart(char * __unused)
  156. {
  157. arm_pm_restart(reboot_mode);
  158. }
  159. void __show_regs(struct pt_regs *regs)
  160. {
  161. unsigned long flags = condition_codes(regs);
  162. printk("CPU: %d\n", smp_processor_id());
  163. print_symbol("PC is at %s\n", instruction_pointer(regs));
  164. print_symbol("LR is at %s\n", regs->ARM_lr);
  165. printk("pc : [<%08lx>] lr : [<%08lx>] %s\n"
  166. "sp : %08lx ip : %08lx fp : %08lx\n",
  167. instruction_pointer(regs),
  168. regs->ARM_lr, print_tainted(), regs->ARM_sp,
  169. regs->ARM_ip, regs->ARM_fp);
  170. printk("r10: %08lx r9 : %08lx r8 : %08lx\n",
  171. regs->ARM_r10, regs->ARM_r9,
  172. regs->ARM_r8);
  173. printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
  174. regs->ARM_r7, regs->ARM_r6,
  175. regs->ARM_r5, regs->ARM_r4);
  176. printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
  177. regs->ARM_r3, regs->ARM_r2,
  178. regs->ARM_r1, regs->ARM_r0);
  179. printk("Flags: %c%c%c%c",
  180. flags & PSR_N_BIT ? 'N' : 'n',
  181. flags & PSR_Z_BIT ? 'Z' : 'z',
  182. flags & PSR_C_BIT ? 'C' : 'c',
  183. flags & PSR_V_BIT ? 'V' : 'v');
  184. printk(" IRQs o%s FIQs o%s Mode %s%s Segment %s\n",
  185. interrupts_enabled(regs) ? "n" : "ff",
  186. fast_interrupts_enabled(regs) ? "n" : "ff",
  187. processor_modes[processor_mode(regs)],
  188. thumb_mode(regs) ? " (T)" : "",
  189. get_fs() == get_ds() ? "kernel" : "user");
  190. #if CONFIG_CPU_CP15
  191. {
  192. unsigned int ctrl;
  193. __asm__ (
  194. " mrc p15, 0, %0, c1, c0\n"
  195. : "=r" (ctrl));
  196. printk("Control: %04X\n", ctrl);
  197. }
  198. #ifdef CONFIG_CPU_CP15_MMU
  199. {
  200. unsigned int transbase, dac;
  201. __asm__ (
  202. " mrc p15, 0, %0, c2, c0\n"
  203. " mrc p15, 0, %1, c3, c0\n"
  204. : "=r" (transbase), "=r" (dac));
  205. printk("Table: %08X DAC: %08X\n",
  206. transbase, dac);
  207. }
  208. #endif
  209. #endif
  210. }
  211. void show_regs(struct pt_regs * regs)
  212. {
  213. printk("\n");
  214. printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
  215. __show_regs(regs);
  216. __backtrace();
  217. }
  218. void show_fpregs(struct user_fp *regs)
  219. {
  220. int i;
  221. for (i = 0; i < 8; i++) {
  222. unsigned long *p;
  223. char type;
  224. p = (unsigned long *)(regs->fpregs + i);
  225. switch (regs->ftype[i]) {
  226. case 1: type = 'f'; break;
  227. case 2: type = 'd'; break;
  228. case 3: type = 'e'; break;
  229. default: type = '?'; break;
  230. }
  231. if (regs->init_flag)
  232. type = '?';
  233. printk(" f%d(%c): %08lx %08lx %08lx%c",
  234. i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
  235. }
  236. printk("FPSR: %08lx FPCR: %08lx\n",
  237. (unsigned long)regs->fpsr,
  238. (unsigned long)regs->fpcr);
  239. }
  240. /*
  241. * Free current thread data structures etc..
  242. */
  243. void exit_thread(void)
  244. {
  245. }
  246. ATOMIC_NOTIFIER_HEAD(thread_notify_head);
  247. EXPORT_SYMBOL_GPL(thread_notify_head);
  248. void flush_thread(void)
  249. {
  250. struct thread_info *thread = current_thread_info();
  251. struct task_struct *tsk = current;
  252. memset(thread->used_cp, 0, sizeof(thread->used_cp));
  253. memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
  254. memset(&thread->fpstate, 0, sizeof(union fp_state));
  255. thread_notify(THREAD_NOTIFY_FLUSH, thread);
  256. }
  257. void release_thread(struct task_struct *dead_task)
  258. {
  259. struct thread_info *thread = task_thread_info(dead_task);
  260. thread_notify(THREAD_NOTIFY_RELEASE, thread);
  261. }
  262. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  263. int
  264. copy_thread(int nr, unsigned long clone_flags, unsigned long stack_start,
  265. unsigned long stk_sz, struct task_struct *p, struct pt_regs *regs)
  266. {
  267. struct thread_info *thread = task_thread_info(p);
  268. struct pt_regs *childregs = task_pt_regs(p);
  269. *childregs = *regs;
  270. childregs->ARM_r0 = 0;
  271. childregs->ARM_sp = stack_start;
  272. memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
  273. thread->cpu_context.sp = (unsigned long)childregs;
  274. thread->cpu_context.pc = (unsigned long)ret_from_fork;
  275. if (clone_flags & CLONE_SETTLS)
  276. thread->tp_value = regs->ARM_r3;
  277. return 0;
  278. }
  279. /*
  280. * fill in the fpe structure for a core dump...
  281. */
  282. int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
  283. {
  284. struct thread_info *thread = current_thread_info();
  285. int used_math = thread->used_cp[1] | thread->used_cp[2];
  286. if (used_math)
  287. memcpy(fp, &thread->fpstate.soft, sizeof (*fp));
  288. return used_math != 0;
  289. }
  290. EXPORT_SYMBOL(dump_fpu);
  291. /*
  292. * fill in the user structure for a core dump..
  293. */
  294. void dump_thread(struct pt_regs * regs, struct user * dump)
  295. {
  296. struct task_struct *tsk = current;
  297. dump->magic = CMAGIC;
  298. dump->start_code = tsk->mm->start_code;
  299. dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
  300. dump->u_tsize = (tsk->mm->end_code - tsk->mm->start_code) >> PAGE_SHIFT;
  301. dump->u_dsize = (tsk->mm->brk - tsk->mm->start_data + PAGE_SIZE - 1) >> PAGE_SHIFT;
  302. dump->u_ssize = 0;
  303. dump->u_debugreg[0] = tsk->thread.debug.bp[0].address;
  304. dump->u_debugreg[1] = tsk->thread.debug.bp[1].address;
  305. dump->u_debugreg[2] = tsk->thread.debug.bp[0].insn.arm;
  306. dump->u_debugreg[3] = tsk->thread.debug.bp[1].insn.arm;
  307. dump->u_debugreg[4] = tsk->thread.debug.nsaved;
  308. if (dump->start_stack < 0x04000000)
  309. dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
  310. dump->regs = *regs;
  311. dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
  312. }
  313. EXPORT_SYMBOL(dump_thread);
  314. /*
  315. * Shuffle the argument into the correct register before calling the
  316. * thread function. r1 is the thread argument, r2 is the pointer to
  317. * the thread function, and r3 points to the exit function.
  318. */
  319. extern void kernel_thread_helper(void);
  320. asm( ".section .text\n"
  321. " .align\n"
  322. " .type kernel_thread_helper, #function\n"
  323. "kernel_thread_helper:\n"
  324. " mov r0, r1\n"
  325. " mov lr, r3\n"
  326. " mov pc, r2\n"
  327. " .size kernel_thread_helper, . - kernel_thread_helper\n"
  328. " .previous");
  329. /*
  330. * Create a kernel thread.
  331. */
  332. pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  333. {
  334. struct pt_regs regs;
  335. memset(&regs, 0, sizeof(regs));
  336. regs.ARM_r1 = (unsigned long)arg;
  337. regs.ARM_r2 = (unsigned long)fn;
  338. regs.ARM_r3 = (unsigned long)do_exit;
  339. regs.ARM_pc = (unsigned long)kernel_thread_helper;
  340. regs.ARM_cpsr = SVC_MODE;
  341. return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  342. }
  343. EXPORT_SYMBOL(kernel_thread);
  344. unsigned long get_wchan(struct task_struct *p)
  345. {
  346. unsigned long fp, lr;
  347. unsigned long stack_start, stack_end;
  348. int count = 0;
  349. if (!p || p == current || p->state == TASK_RUNNING)
  350. return 0;
  351. stack_start = (unsigned long)end_of_stack(p);
  352. stack_end = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  353. fp = thread_saved_fp(p);
  354. do {
  355. if (fp < stack_start || fp > stack_end)
  356. return 0;
  357. lr = pc_pointer (((unsigned long *)fp)[-1]);
  358. if (!in_sched_functions(lr))
  359. return lr;
  360. fp = *(unsigned long *) (fp - 12);
  361. } while (count ++ < 16);
  362. return 0;
  363. }