process.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. void machine_restart(char * __unused)
  90. {
  91. /*
  92. * Clean and disable cache, and turn off interrupts
  93. */
  94. cpu_proc_fin();
  95. /*
  96. * Tell the mm system that we are going to reboot -
  97. * we may need it to insert some 1:1 mappings so that
  98. * soft boot works.
  99. */
  100. setup_mm_for_reboot(reboot_mode);
  101. /*
  102. * copy branch instruction to reset location and call it
  103. */
  104. *(unsigned long *)0 = *(unsigned long *)0x03800000;
  105. ((void(*)(void))0)();
  106. /*
  107. * Whoops - the architecture was unable to reboot.
  108. * Tell the user! Should never happen...
  109. */
  110. mdelay(1000);
  111. printk("Reboot failed -- System halted\n");
  112. while (1);
  113. }
  114. void show_regs(struct pt_regs * regs)
  115. {
  116. unsigned long flags;
  117. flags = condition_codes(regs);
  118. printk("pc : [<%08lx>] lr : [<%08lx>] %s\n"
  119. "sp : %08lx ip : %08lx fp : %08lx\n",
  120. instruction_pointer(regs),
  121. regs->ARM_lr, print_tainted(), regs->ARM_sp,
  122. regs->ARM_ip, regs->ARM_fp);
  123. printk("r10: %08lx r9 : %08lx r8 : %08lx\n",
  124. regs->ARM_r10, regs->ARM_r9,
  125. regs->ARM_r8);
  126. printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
  127. regs->ARM_r7, regs->ARM_r6,
  128. regs->ARM_r5, regs->ARM_r4);
  129. printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
  130. regs->ARM_r3, regs->ARM_r2,
  131. regs->ARM_r1, regs->ARM_r0);
  132. printk("Flags: %c%c%c%c",
  133. flags & PSR_N_BIT ? 'N' : 'n',
  134. flags & PSR_Z_BIT ? 'Z' : 'z',
  135. flags & PSR_C_BIT ? 'C' : 'c',
  136. flags & PSR_V_BIT ? 'V' : 'v');
  137. printk(" IRQs o%s FIQs o%s Mode %s Segment %s\n",
  138. interrupts_enabled(regs) ? "n" : "ff",
  139. fast_interrupts_enabled(regs) ? "n" : "ff",
  140. processor_modes[processor_mode(regs)],
  141. get_fs() == get_ds() ? "kernel" : "user");
  142. }
  143. void show_fpregs(struct user_fp *regs)
  144. {
  145. int i;
  146. for (i = 0; i < 8; i++) {
  147. unsigned long *p;
  148. char type;
  149. p = (unsigned long *)(regs->fpregs + i);
  150. switch (regs->ftype[i]) {
  151. case 1: type = 'f'; break;
  152. case 2: type = 'd'; break;
  153. case 3: type = 'e'; break;
  154. default: type = '?'; break;
  155. }
  156. if (regs->init_flag)
  157. type = '?';
  158. printk(" f%d(%c): %08lx %08lx %08lx%c",
  159. i, type, p[0], p[1], p[2], i & 1 ? '\n' : ' ');
  160. }
  161. printk("FPSR: %08lx FPCR: %08lx\n",
  162. (unsigned long)regs->fpsr,
  163. (unsigned long)regs->fpcr);
  164. }
  165. /*
  166. * Task structure and kernel stack allocation.
  167. */
  168. static unsigned long *thread_info_head;
  169. static unsigned int nr_thread_info;
  170. extern unsigned long get_page_8k(int priority);
  171. extern void free_page_8k(unsigned long page);
  172. // FIXME - is this valid?
  173. #define EXTRA_TASK_STRUCT 0
  174. #define ll_alloc_task_struct() ((struct thread_info *)get_page_8k(GFP_KERNEL))
  175. #define ll_free_task_struct(p) free_page_8k((unsigned long)(p))
  176. //FIXME - do we use *task param below looks like we dont, which is ok?
  177. //FIXME - if EXTRA_TASK_STRUCT is zero we can optimise the below away permanently. *IF* its supposed to be zero.
  178. struct thread_info *alloc_thread_info(struct task_struct *task)
  179. {
  180. struct thread_info *thread = NULL;
  181. if (EXTRA_TASK_STRUCT) {
  182. unsigned long *p = thread_info_head;
  183. if (p) {
  184. thread_info_head = (unsigned long *)p[0];
  185. nr_thread_info -= 1;
  186. }
  187. thread = (struct thread_info *)p;
  188. }
  189. if (!thread)
  190. thread = ll_alloc_task_struct();
  191. #ifdef CONFIG_MAGIC_SYSRQ
  192. /*
  193. * The stack must be cleared if you want SYSRQ-T to
  194. * give sensible stack usage information
  195. */
  196. if (thread) {
  197. char *p = (char *)thread;
  198. memzero(p+KERNEL_STACK_SIZE, KERNEL_STACK_SIZE);
  199. }
  200. #endif
  201. return thread;
  202. }
  203. void free_thread_info(struct thread_info *thread)
  204. {
  205. if (EXTRA_TASK_STRUCT && nr_thread_info < EXTRA_TASK_STRUCT) {
  206. unsigned long *p = (unsigned long *)thread;
  207. p[0] = (unsigned long)thread_info_head;
  208. thread_info_head = p;
  209. nr_thread_info += 1;
  210. } else
  211. ll_free_task_struct(thread);
  212. }
  213. /*
  214. * Free current thread data structures etc..
  215. */
  216. void exit_thread(void)
  217. {
  218. }
  219. void flush_thread(void)
  220. {
  221. struct thread_info *thread = current_thread_info();
  222. struct task_struct *tsk = current;
  223. memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
  224. memset(&thread->fpstate, 0, sizeof(union fp_state));
  225. clear_used_math();
  226. }
  227. void release_thread(struct task_struct *dead_task)
  228. {
  229. }
  230. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  231. int
  232. copy_thread(int nr, unsigned long clone_flags, unsigned long stack_start,
  233. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  234. {
  235. struct thread_info *thread = p->thread_info;
  236. struct pt_regs *childregs;
  237. childregs = __get_user_regs(thread);
  238. *childregs = *regs;
  239. childregs->ARM_r0 = 0;
  240. childregs->ARM_sp = stack_start;
  241. memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
  242. thread->cpu_context.sp = (unsigned long)childregs;
  243. thread->cpu_context.pc = (unsigned long)ret_from_fork | MODE_SVC26 | PSR_I_BIT;
  244. return 0;
  245. }
  246. /*
  247. * fill in the fpe structure for a core dump...
  248. */
  249. int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
  250. {
  251. struct thread_info *thread = current_thread_info();
  252. int used_math = !!used_math();
  253. if (used_math)
  254. memcpy(fp, &thread->fpstate.soft, sizeof (*fp));
  255. return used_math;
  256. }
  257. /*
  258. * fill in the user structure for a core dump..
  259. */
  260. void dump_thread(struct pt_regs * regs, struct user * dump)
  261. {
  262. struct task_struct *tsk = current;
  263. dump->magic = CMAGIC;
  264. dump->start_code = tsk->mm->start_code;
  265. dump->start_stack = regs->ARM_sp & ~(PAGE_SIZE - 1);
  266. dump->u_tsize = (tsk->mm->end_code - tsk->mm->start_code) >> PAGE_SHIFT;
  267. dump->u_dsize = (tsk->mm->brk - tsk->mm->start_data + PAGE_SIZE - 1) >> PAGE_SHIFT;
  268. dump->u_ssize = 0;
  269. dump->u_debugreg[0] = tsk->thread.debug.bp[0].address;
  270. dump->u_debugreg[1] = tsk->thread.debug.bp[1].address;
  271. dump->u_debugreg[2] = tsk->thread.debug.bp[0].insn;
  272. dump->u_debugreg[3] = tsk->thread.debug.bp[1].insn;
  273. dump->u_debugreg[4] = tsk->thread.debug.nsaved;
  274. if (dump->start_stack < 0x04000000)
  275. dump->u_ssize = (0x04000000 - dump->start_stack) >> PAGE_SHIFT;
  276. dump->regs = *regs;
  277. dump->u_fpvalid = dump_fpu (regs, &dump->u_fp);
  278. }
  279. /*
  280. * Shuffle the argument into the correct register before calling the
  281. * thread function. r1 is the thread argument, r2 is the pointer to
  282. * the thread function, and r3 points to the exit function.
  283. * FIXME - make sure this is right - the older code used to zero fp
  284. * and cause the parent to call sys_exit (do_exit in this version)
  285. */
  286. extern void kernel_thread_helper(void);
  287. asm( ".section .text\n"
  288. " .align\n"
  289. " .type kernel_thread_helper, #function\n"
  290. "kernel_thread_helper:\n"
  291. " mov r0, r1\n"
  292. " mov lr, r3\n"
  293. " mov pc, r2\n"
  294. " .size kernel_thread_helper, . - kernel_thread_helper\n"
  295. " .previous");
  296. /*
  297. * Create a kernel thread.
  298. */
  299. pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  300. {
  301. struct pt_regs regs;
  302. memset(&regs, 0, sizeof(regs));
  303. regs.ARM_r1 = (unsigned long)arg;
  304. regs.ARM_r2 = (unsigned long)fn;
  305. regs.ARM_r3 = (unsigned long)do_exit;
  306. regs.ARM_pc = (unsigned long)kernel_thread_helper | MODE_SVC26;
  307. return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  308. }
  309. EXPORT_SYMBOL(kernel_thread);
  310. unsigned long get_wchan(struct task_struct *p)
  311. {
  312. unsigned long fp, lr;
  313. unsigned long stack_page;
  314. int count = 0;
  315. if (!p || p == current || p->state == TASK_RUNNING)
  316. return 0;
  317. stack_page = 4096 + (unsigned long)p;
  318. fp = thread_saved_fp(p);
  319. do {
  320. if (fp < stack_page || fp > 4092+stack_page)
  321. return 0;
  322. lr = pc_pointer (((unsigned long *)fp)[-1]);
  323. if (!in_sched_functions(lr))
  324. return lr;
  325. fp = *(unsigned long *) (fp - 12);
  326. } while (count ++ < 16);
  327. return 0;
  328. }