process.c 7.0 KB

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