process.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * linux/arch/cris/kernel/process.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Copyright (C) 2000-2002 Axis Communications AB
  6. *
  7. * Authors: Bjorn Wesen (bjornw@axis.com)
  8. * Mikael Starvik (starvik@axis.com)
  9. *
  10. * This file handles the architecture-dependent parts of process handling..
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/err.h>
  15. #include <linux/fs.h>
  16. #include <arch/svinto.h>
  17. #include <linux/init.h>
  18. #include <arch/system.h>
  19. #ifdef CONFIG_ETRAX_GPIO
  20. void etrax_gpio_wake_up_check(void); /* drivers/gpio.c */
  21. #endif
  22. /*
  23. * We use this if we don't have any better
  24. * idle routine..
  25. */
  26. void default_idle(void)
  27. {
  28. #ifdef CONFIG_ETRAX_GPIO
  29. etrax_gpio_wake_up_check();
  30. #endif
  31. }
  32. /*
  33. * Free current thread data structures etc..
  34. */
  35. void exit_thread(void)
  36. {
  37. /* Nothing needs to be done. */
  38. }
  39. /* if the watchdog is enabled, we can simply disable interrupts and go
  40. * into an eternal loop, and the watchdog will reset the CPU after 0.1s
  41. * if on the other hand the watchdog wasn't enabled, we just enable it and wait
  42. */
  43. void hard_reset_now (void)
  44. {
  45. /*
  46. * Don't declare this variable elsewhere. We don't want any other
  47. * code to know about it than the watchdog handler in entry.S and
  48. * this code, implementing hard reset through the watchdog.
  49. */
  50. #if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)
  51. extern int cause_of_death;
  52. #endif
  53. printk("*** HARD RESET ***\n");
  54. local_irq_disable();
  55. #if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)
  56. cause_of_death = 0xbedead;
  57. #else
  58. /* Since we dont plan to keep on resetting the watchdog,
  59. the key can be arbitrary hence three */
  60. *R_WATCHDOG = IO_FIELD(R_WATCHDOG, key, 3) |
  61. IO_STATE(R_WATCHDOG, enable, start);
  62. #endif
  63. while(1) /* waiting for RETRIBUTION! */ ;
  64. }
  65. /*
  66. * Return saved PC of a blocked thread.
  67. */
  68. unsigned long thread_saved_pc(struct task_struct *t)
  69. {
  70. return task_pt_regs(t)->irp;
  71. }
  72. static void kernel_thread_helper(void* dummy, int (*fn)(void *), void * arg)
  73. {
  74. fn(arg);
  75. do_exit(-1); /* Should never be called, return bad exit value */
  76. }
  77. /*
  78. * Create a kernel thread
  79. */
  80. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  81. {
  82. struct pt_regs regs;
  83. memset(&regs, 0, sizeof(regs));
  84. /* Don't use r10 since that is set to 0 in copy_thread */
  85. regs.r11 = (unsigned long)fn;
  86. regs.r12 = (unsigned long)arg;
  87. regs.irp = (unsigned long)kernel_thread_helper;
  88. regs.dccr = 1 << I_DCCR_BITNR;
  89. /* Ok, create the new process.. */
  90. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  91. }
  92. /* setup the child's kernel stack with a pt_regs and switch_stack on it.
  93. * it will be un-nested during _resume and _ret_from_sys_call when the
  94. * new thread is scheduled.
  95. *
  96. * also setup the thread switching structure which is used to keep
  97. * thread-specific data during _resumes.
  98. *
  99. */
  100. asmlinkage void ret_from_fork(void);
  101. int copy_thread(unsigned long clone_flags, unsigned long usp,
  102. unsigned long unused,
  103. struct task_struct *p, struct pt_regs *regs)
  104. {
  105. struct pt_regs * childregs;
  106. struct switch_stack *swstack;
  107. /* put the pt_regs structure at the end of the new kernel stack page and fix it up
  108. * remember that the task_struct doubles as the kernel stack for the task
  109. */
  110. childregs = task_pt_regs(p);
  111. *childregs = *regs; /* struct copy of pt_regs */
  112. p->set_child_tid = p->clear_child_tid = NULL;
  113. childregs->r10 = 0; /* child returns 0 after a fork/clone */
  114. /* put the switch stack right below the pt_regs */
  115. swstack = ((struct switch_stack *)childregs) - 1;
  116. swstack->r9 = 0; /* parameter to ret_from_sys_call, 0 == dont restart the syscall */
  117. /* we want to return into ret_from_sys_call after the _resume */
  118. swstack->return_ip = (unsigned long) ret_from_fork; /* Will call ret_from_sys_call */
  119. /* fix the user-mode stackpointer */
  120. p->thread.usp = usp;
  121. /* and the kernel-mode one */
  122. p->thread.ksp = (unsigned long) swstack;
  123. #ifdef DEBUG
  124. printk("copy_thread: new regs at 0x%p, as shown below:\n", childregs);
  125. show_registers(childregs);
  126. #endif
  127. return 0;
  128. }
  129. /*
  130. * Be aware of the "magic" 7th argument in the four system-calls below.
  131. * They need the latest stackframe, which is put as the 7th argument by
  132. * entry.S. The previous arguments are dummies or actually used, but need
  133. * to be defined to reach the 7th argument.
  134. *
  135. * N.B.: Another method to get the stackframe is to use current_regs(). But
  136. * it returns the latest stack-frame stacked when going from _user mode_ and
  137. * some of these (at least sys_clone) are called from kernel-mode sometimes
  138. * (for example during kernel_thread, above) and thus cannot use it. Thus,
  139. * to be sure not to get any surprises, we use the method for the other calls
  140. * as well.
  141. */
  142. asmlinkage int sys_fork(long r10, long r11, long r12, long r13, long mof, long srp,
  143. struct pt_regs *regs)
  144. {
  145. return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
  146. }
  147. /* if newusp is 0, we just grab the old usp */
  148. /* FIXME: Is parent_tid/child_tid really third/fourth argument? Update lib? */
  149. asmlinkage int sys_clone(unsigned long newusp, unsigned long flags,
  150. int* parent_tid, int* child_tid, long mof, long srp,
  151. struct pt_regs *regs)
  152. {
  153. if (!newusp)
  154. newusp = rdusp();
  155. return do_fork(flags, newusp, regs, 0, parent_tid, child_tid);
  156. }
  157. /* vfork is a system call in i386 because of register-pressure - maybe
  158. * we can remove it and handle it in libc but we put it here until then.
  159. */
  160. asmlinkage int sys_vfork(long r10, long r11, long r12, long r13, long mof, long srp,
  161. struct pt_regs *regs)
  162. {
  163. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL, NULL);
  164. }
  165. /*
  166. * sys_execve() executes a new program.
  167. */
  168. asmlinkage int sys_execve(const char *fname,
  169. const char *const *argv,
  170. const char *const *envp,
  171. long r13, long mof, long srp,
  172. struct pt_regs *regs)
  173. {
  174. int error;
  175. char *filename;
  176. filename = getname(fname);
  177. error = PTR_ERR(filename);
  178. if (IS_ERR(filename))
  179. goto out;
  180. error = do_execve(filename, argv, envp, regs);
  181. putname(filename);
  182. out:
  183. return error;
  184. }
  185. unsigned long get_wchan(struct task_struct *p)
  186. {
  187. #if 0
  188. /* YURGH. TODO. */
  189. unsigned long ebp, esp, eip;
  190. unsigned long stack_page;
  191. int count = 0;
  192. if (!p || p == current || p->state == TASK_RUNNING)
  193. return 0;
  194. stack_page = (unsigned long)p;
  195. esp = p->thread.esp;
  196. if (!stack_page || esp < stack_page || esp > 8188+stack_page)
  197. return 0;
  198. /* include/asm-i386/system.h:switch_to() pushes ebp last. */
  199. ebp = *(unsigned long *) esp;
  200. do {
  201. if (ebp < stack_page || ebp > 8184+stack_page)
  202. return 0;
  203. eip = *(unsigned long *) (ebp+4);
  204. if (!in_sched_functions(eip))
  205. return eip;
  206. ebp = *(unsigned long *) ebp;
  207. } while (count++ < 16);
  208. #endif
  209. return 0;
  210. }
  211. #undef last_sched
  212. #undef first_sched
  213. void show_regs(struct pt_regs * regs)
  214. {
  215. unsigned long usp = rdusp();
  216. printk("IRP: %08lx SRP: %08lx DCCR: %08lx USP: %08lx MOF: %08lx\n",
  217. regs->irp, regs->srp, regs->dccr, usp, regs->mof );
  218. printk(" r0: %08lx r1: %08lx r2: %08lx r3: %08lx\n",
  219. regs->r0, regs->r1, regs->r2, regs->r3);
  220. printk(" r4: %08lx r5: %08lx r6: %08lx r7: %08lx\n",
  221. regs->r4, regs->r5, regs->r6, regs->r7);
  222. printk(" r8: %08lx r9: %08lx r10: %08lx r11: %08lx\n",
  223. regs->r8, regs->r9, regs->r10, regs->r11);
  224. printk("r12: %08lx r13: %08lx oR10: %08lx\n",
  225. regs->r12, regs->r13, regs->orig_r10);
  226. }