process.c 11 KB

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