process.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * linux/arch/arm26/kernel/process.c
  3. *
  4. * Copyright (C) 2003 Ian Molton - adapted for ARM26
  5. * Copyright (C) 1996-2000 Russell King - Converted to ARM.
  6. * Origional Copyright (C) 1995 Linus Torvalds
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <stdarg.h>
  13. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/stddef.h>
  19. #include <linux/unistd.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/slab.h>
  22. #include <linux/user.h>
  23. #include <linux/a.out.h>
  24. #include <linux/delay.h>
  25. #include <linux/reboot.h>
  26. #include <linux/interrupt.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. extern const char *processor_modes[];
  34. extern void setup_mm_for_reboot(char mode);
  35. static volatile int hlt_counter;
  36. void disable_hlt(void)
  37. {
  38. hlt_counter++;
  39. }
  40. EXPORT_SYMBOL(disable_hlt);
  41. void enable_hlt(void)
  42. {
  43. hlt_counter--;
  44. }
  45. EXPORT_SYMBOL(enable_hlt);
  46. static int __init nohlt_setup(char *__unused)
  47. {
  48. hlt_counter = 1;
  49. return 1;
  50. }
  51. static int __init hlt_setup(char *__unused)
  52. {
  53. hlt_counter = 0;
  54. return 1;
  55. }
  56. __setup("nohlt", nohlt_setup);
  57. __setup("hlt", hlt_setup);
  58. /*
  59. * This is our default idle handler. We need to disable
  60. * interrupts here to ensure we don't miss a wakeup call.
  61. */
  62. void cpu_idle(void)
  63. {
  64. /* endless idle loop with no priority at all */
  65. preempt_disable();
  66. while (1) {
  67. while (!need_resched()) {
  68. local_irq_disable();
  69. if (!need_resched() && !hlt_counter)
  70. local_irq_enable();
  71. }
  72. }
  73. schedule();
  74. }
  75. static char reboot_mode = 'h';
  76. int __init reboot_setup(char *str)
  77. {
  78. reboot_mode = str[0];
  79. return 1;
  80. }
  81. __setup("reboot=", reboot_setup);
  82. /* ARM26 cant do these but we still need to define them. */
  83. void machine_halt(void)
  84. {
  85. }
  86. void machine_power_off(void)
  87. {
  88. }
  89. EXPORT_SYMBOL(machine_halt);
  90. EXPORT_SYMBOL(machine_power_off);
  91. void machine_restart(char * __unused)
  92. {
  93. /*
  94. * Clean and disable cache, and turn off interrupts
  95. */
  96. cpu_proc_fin();
  97. /*
  98. * Tell the mm system that we are going to reboot -
  99. * we may need it to insert some 1:1 mappings so that
  100. * soft boot works.
  101. */
  102. setup_mm_for_reboot(reboot_mode);
  103. /*
  104. * copy branch instruction to reset location and call it
  105. */
  106. *(unsigned long *)0 = *(unsigned long *)0x03800000;
  107. ((void(*)(void))0)();
  108. /*
  109. * Whoops - the architecture was unable to reboot.
  110. * Tell the user! Should never happen...
  111. */
  112. mdelay(1000);
  113. printk("Reboot failed -- System halted\n");
  114. while (1);
  115. }
  116. EXPORT_SYMBOL(machine_restart);
  117. void show_regs(struct pt_regs * regs)
  118. {
  119. unsigned long flags;
  120. flags = condition_codes(regs);
  121. printk("pc : [<%08lx>] lr : [<%08lx>] %s\n"
  122. "sp : %08lx ip : %08lx fp : %08lx\n",
  123. instruction_pointer(regs),
  124. regs->ARM_lr, print_tainted(), regs->ARM_sp,
  125. regs->ARM_ip, regs->ARM_fp);
  126. printk("r10: %08lx r9 : %08lx r8 : %08lx\n",
  127. regs->ARM_r10, regs->ARM_r9,
  128. regs->ARM_r8);
  129. printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
  130. regs->ARM_r7, regs->ARM_r6,
  131. regs->ARM_r5, regs->ARM_r4);
  132. printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
  133. regs->ARM_r3, regs->ARM_r2,
  134. regs->ARM_r1, regs->ARM_r0);
  135. printk("Flags: %c%c%c%c",
  136. flags & PSR_N_BIT ? 'N' : 'n',
  137. flags & PSR_Z_BIT ? 'Z' : 'z',
  138. flags & PSR_C_BIT ? 'C' : 'c',
  139. flags & PSR_V_BIT ? 'V' : 'v');
  140. printk(" IRQs o%s FIQs o%s Mode %s Segment %s\n",
  141. interrupts_enabled(regs) ? "n" : "ff",
  142. fast_interrupts_enabled(regs) ? "n" : "ff",
  143. processor_modes[processor_mode(regs)],
  144. get_fs() == get_ds() ? "kernel" : "user");
  145. }
  146. void show_fpregs(struct user_fp *regs)
  147. {
  148. int i;
  149. for (i = 0; i < 8; i++) {
  150. unsigned long *p;
  151. char type;
  152. p = (unsigned long *)(regs->fpregs + i);
  153. switch (regs->ftype[i]) {
  154. case 1: type = 'f'; break;
  155. case 2: type = 'd'; break;
  156. case 3: type = 'e'; break;
  157. default: type = '?'; break;
  158. }
  159. if (regs->init_flag)
  160. type = '?';
  161. printk(" f%d(%c): %08lx %08lx %08lx%c",
  162. i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
  163. }
  164. printk("FPSR: %08lx FPCR: %08lx\n",
  165. (unsigned long)regs->fpsr,
  166. (unsigned long)regs->fpcr);
  167. }
  168. /*
  169. * Task structure and kernel stack allocation.
  170. */
  171. static unsigned long *thread_info_head;
  172. static unsigned int nr_thread_info;
  173. extern unsigned long get_page_8k(int priority);
  174. extern void free_page_8k(unsigned long page);
  175. // FIXME - is this valid?
  176. #define EXTRA_TASK_STRUCT 0
  177. #define ll_alloc_task_struct() ((struct thread_info *)get_page_8k(GFP_KERNEL))
  178. #define ll_free_task_struct(p) free_page_8k((unsigned long)(p))
  179. //FIXME - do we use *task param below looks like we dont, which is ok?
  180. //FIXME - if EXTRA_TASK_STRUCT is zero we can optimise the below away permanently. *IF* its supposed to be zero.
  181. struct thread_info *alloc_thread_info(struct task_struct *task)
  182. {
  183. struct thread_info *thread = NULL;
  184. if (EXTRA_TASK_STRUCT) {
  185. unsigned long *p = thread_info_head;
  186. if (p) {
  187. thread_info_head = (unsigned long *)p[0];
  188. nr_thread_info -= 1;
  189. }
  190. thread = (struct thread_info *)p;
  191. }
  192. if (!thread)
  193. thread = ll_alloc_task_struct();
  194. #ifdef CONFIG_MAGIC_SYSRQ
  195. /*
  196. * The stack must be cleared if you want SYSRQ-T to
  197. * give sensible stack usage information
  198. */
  199. if (thread) {
  200. char *p = (char *)thread;
  201. memzero(p+KERNEL_STACK_SIZE, KERNEL_STACK_SIZE);
  202. }
  203. #endif
  204. return thread;
  205. }
  206. void free_thread_info(struct thread_info *thread)
  207. {
  208. if (EXTRA_TASK_STRUCT && nr_thread_info < EXTRA_TASK_STRUCT) {
  209. unsigned long *p = (unsigned long *)thread;
  210. p[0] = (unsigned long)thread_info_head;
  211. thread_info_head = p;
  212. nr_thread_info += 1;
  213. } else
  214. ll_free_task_struct(thread);
  215. }
  216. /*
  217. * Free current thread data structures etc..
  218. */
  219. void exit_thread(void)
  220. {
  221. }
  222. void flush_thread(void)
  223. {
  224. struct thread_info *thread = current_thread_info();
  225. struct task_struct *tsk = current;
  226. memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
  227. memset(&thread->fpstate, 0, sizeof(union fp_state));
  228. clear_used_math();
  229. }
  230. void release_thread(struct task_struct *dead_task)
  231. {
  232. }
  233. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  234. int
  235. copy_thread(int nr, unsigned long clone_flags, unsigned long stack_start,
  236. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  237. {
  238. struct thread_info *thread = p->thread_info;
  239. struct pt_regs *childregs;
  240. childregs = __get_user_regs(thread);
  241. *childregs = *regs;
  242. childregs->ARM_r0 = 0;
  243. childregs->ARM_sp = stack_start;
  244. memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
  245. thread->cpu_context.sp = (unsigned long)childregs;
  246. thread->cpu_context.pc = (unsigned long)ret_from_fork | MODE_SVC26 | PSR_I_BIT;
  247. return 0;
  248. }
  249. /*
  250. * fill in the fpe structure for a core dump...
  251. */
  252. int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
  253. {
  254. struct thread_info *thread = current_thread_info();
  255. int used_math = !!used_math();
  256. if (used_math)
  257. memcpy(fp, &thread->fpstate.soft, sizeof (*fp));
  258. return used_math;
  259. }
  260. /*
  261. * fill in the user structure for a core dump..
  262. */
  263. void dump_thread(struct pt_regs * regs, struct user * dump)
  264. {
  265. struct task_struct *tsk = current;
  266. dump->magic = CMAGIC;
  267. dump->start_code = tsk->mm->start_code;
  268. dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
  269. dump->u_tsize = (tsk->mm->end_code - tsk->mm->start_code) >> PAGE_SHIFT;
  270. dump->u_dsize = (tsk->mm->brk - tsk->mm->start_data + PAGE_SIZE - 1) >> PAGE_SHIFT;
  271. dump->u_ssize = 0;
  272. dump->u_debugreg[0] = tsk->thread.debug.bp[0].address;
  273. dump->u_debugreg[1] = tsk->thread.debug.bp[1].address;
  274. dump->u_debugreg[2] = tsk->thread.debug.bp[0].insn;
  275. dump->u_debugreg[3] = tsk->thread.debug.bp[1].insn;
  276. dump->u_debugreg[4] = tsk->thread.debug.nsaved;
  277. if (dump->start_stack < 0x04000000)
  278. dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
  279. dump->regs = *regs;
  280. dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
  281. }
  282. /*
  283. * Shuffle the argument into the correct register before calling the
  284. * thread function. r1 is the thread argument, r2 is the pointer to
  285. * the thread function, and r3 points to the exit function.
  286. * FIXME - make sure this is right - the older code used to zero fp
  287. * and cause the parent to call sys_exit (do_exit in this version)
  288. */
  289. extern void kernel_thread_helper(void);
  290. asm( ".section .text\n"
  291. " .align\n"
  292. " .type kernel_thread_helper, #function\n"
  293. "kernel_thread_helper:\n"
  294. " mov r0, r1\n"
  295. " mov lr, r3\n"
  296. " mov pc, r2\n"
  297. " .size kernel_thread_helper, . - kernel_thread_helper\n"
  298. " .previous");
  299. /*
  300. * Create a kernel thread.
  301. */
  302. pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  303. {
  304. struct pt_regs regs;
  305. memset(&regs, 0, sizeof(regs));
  306. regs.ARM_r1 = (unsigned long)arg;
  307. regs.ARM_r2 = (unsigned long)fn;
  308. regs.ARM_r3 = (unsigned long)do_exit;
  309. regs.ARM_pc = (unsigned long)kernel_thread_helper | MODE_SVC26;
  310. return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  311. }
  312. EXPORT_SYMBOL(kernel_thread);
  313. unsigned long get_wchan(struct task_struct *p)
  314. {
  315. unsigned long fp, lr;
  316. unsigned long stack_page;
  317. int count = 0;
  318. if (!p || p == current || p->state == TASK_RUNNING)
  319. return 0;
  320. stack_page = 4096 + (unsigned long)p;
  321. fp = thread_saved_fp(p);
  322. do {
  323. if (fp < stack_page || fp > 4092+stack_page)
  324. return 0;
  325. lr = pc_pointer (((unsigned long *)fp)[-1]);
  326. if (!in_sched_functions(lr))
  327. return lr;
  328. fp = *(unsigned long *) (fp - 12);
  329. } while (count ++ < 16);
  330. return 0;
  331. }