process.c 6.9 KB

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