process.c 11 KB

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