process.c 9.3 KB

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