process.c 7.0 KB

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