process.c 11 KB

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