process.c 9.3 KB

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