process.c 11 KB

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