process.c 11 KB

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