process.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* MN10300 Process handling code
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/smp.h>
  17. #include <linux/stddef.h>
  18. #include <linux/unistd.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/user.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/delay.h>
  23. #include <linux/reboot.h>
  24. #include <linux/percpu.h>
  25. #include <linux/err.h>
  26. #include <linux/fs.h>
  27. #include <linux/slab.h>
  28. #include <linux/rcupdate.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/io.h>
  32. #include <asm/processor.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/fpu.h>
  35. #include <asm/reset-regs.h>
  36. #include <asm/gdb-stub.h>
  37. #include "internal.h"
  38. /*
  39. * power management idle function, if any..
  40. */
  41. void (*pm_idle)(void);
  42. EXPORT_SYMBOL(pm_idle);
  43. /*
  44. * return saved PC of a blocked thread.
  45. */
  46. unsigned long thread_saved_pc(struct task_struct *tsk)
  47. {
  48. return ((unsigned long *) tsk->thread.sp)[3];
  49. }
  50. /*
  51. * power off function, if any
  52. */
  53. void (*pm_power_off)(void);
  54. EXPORT_SYMBOL(pm_power_off);
  55. #if !defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
  56. /*
  57. * we use this if we don't have any better idle routine
  58. */
  59. static void default_idle(void)
  60. {
  61. local_irq_disable();
  62. if (!need_resched())
  63. safe_halt();
  64. else
  65. local_irq_enable();
  66. }
  67. #else /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU */
  68. /*
  69. * On SMP it's slightly faster (but much more power-consuming!)
  70. * to poll the ->work.need_resched flag instead of waiting for the
  71. * cross-CPU IPI to arrive. Use this option with caution.
  72. */
  73. static inline void poll_idle(void)
  74. {
  75. int oldval;
  76. local_irq_enable();
  77. /*
  78. * Deal with another CPU just having chosen a thread to
  79. * run here:
  80. */
  81. oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
  82. if (!oldval) {
  83. set_thread_flag(TIF_POLLING_NRFLAG);
  84. while (!need_resched())
  85. cpu_relax();
  86. clear_thread_flag(TIF_POLLING_NRFLAG);
  87. } else {
  88. set_need_resched();
  89. }
  90. }
  91. #endif /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU */
  92. /*
  93. * the idle thread
  94. * - there's no useful work to be done, so just try to conserve power and have
  95. * a low exit latency (ie sit in a loop waiting for somebody to say that
  96. * they'd like to reschedule)
  97. */
  98. void cpu_idle(void)
  99. {
  100. /* endless idle loop with no priority at all */
  101. for (;;) {
  102. rcu_idle_enter();
  103. while (!need_resched()) {
  104. void (*idle)(void);
  105. smp_rmb();
  106. idle = pm_idle;
  107. if (!idle) {
  108. #if defined(CONFIG_SMP) && !defined(CONFIG_HOTPLUG_CPU)
  109. idle = poll_idle;
  110. #else /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
  111. idle = default_idle;
  112. #endif /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
  113. }
  114. idle();
  115. }
  116. rcu_idle_exit();
  117. schedule_preempt_disabled();
  118. }
  119. }
  120. void release_segments(struct mm_struct *mm)
  121. {
  122. }
  123. void machine_restart(char *cmd)
  124. {
  125. #ifdef CONFIG_KERNEL_DEBUGGER
  126. gdbstub_exit(0);
  127. #endif
  128. #ifdef mn10300_unit_hard_reset
  129. mn10300_unit_hard_reset();
  130. #else
  131. mn10300_proc_hard_reset();
  132. #endif
  133. }
  134. void machine_halt(void)
  135. {
  136. #ifdef CONFIG_KERNEL_DEBUGGER
  137. gdbstub_exit(0);
  138. #endif
  139. }
  140. void machine_power_off(void)
  141. {
  142. #ifdef CONFIG_KERNEL_DEBUGGER
  143. gdbstub_exit(0);
  144. #endif
  145. }
  146. void show_regs(struct pt_regs *regs)
  147. {
  148. }
  149. /*
  150. * create a kernel thread
  151. */
  152. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  153. {
  154. struct pt_regs regs;
  155. memset(&regs, 0, sizeof(regs));
  156. regs.a2 = (unsigned long) fn;
  157. regs.d2 = (unsigned long) arg;
  158. regs.pc = (unsigned long) kernel_thread_helper;
  159. local_save_flags(regs.epsw);
  160. regs.epsw |= EPSW_IE | EPSW_IM_7;
  161. /* Ok, create the new process.. */
  162. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0,
  163. NULL, NULL);
  164. }
  165. EXPORT_SYMBOL(kernel_thread);
  166. /*
  167. * free current thread data structures etc..
  168. */
  169. void exit_thread(void)
  170. {
  171. exit_fpu();
  172. }
  173. void flush_thread(void)
  174. {
  175. flush_fpu();
  176. }
  177. void release_thread(struct task_struct *dead_task)
  178. {
  179. }
  180. /*
  181. * we do not have to muck with descriptors here, that is
  182. * done in switch_mm() as needed.
  183. */
  184. void copy_segments(struct task_struct *p, struct mm_struct *new_mm)
  185. {
  186. }
  187. /*
  188. * this gets called before we allocate a new thread and copy the current task
  189. * into it so that we can store lazy state into memory
  190. */
  191. void prepare_to_copy(struct task_struct *tsk)
  192. {
  193. unlazy_fpu(tsk);
  194. }
  195. /*
  196. * set up the kernel stack for a new thread and copy arch-specific thread
  197. * control information
  198. */
  199. int copy_thread(unsigned long clone_flags,
  200. unsigned long c_usp, unsigned long ustk_size,
  201. struct task_struct *p, struct pt_regs *kregs)
  202. {
  203. struct thread_info *ti = task_thread_info(p);
  204. struct pt_regs *c_uregs, *c_kregs, *uregs;
  205. unsigned long c_ksp;
  206. uregs = current->thread.uregs;
  207. c_ksp = (unsigned long) task_stack_page(p) + THREAD_SIZE;
  208. /* allocate the userspace exception frame and set it up */
  209. c_ksp -= sizeof(struct pt_regs);
  210. c_uregs = (struct pt_regs *) c_ksp;
  211. p->thread.uregs = c_uregs;
  212. *c_uregs = *uregs;
  213. c_uregs->sp = c_usp;
  214. c_uregs->epsw &= ~EPSW_FE; /* my FPU */
  215. c_ksp -= 12; /* allocate function call ABI slack */
  216. /* the new TLS pointer is passed in as arg #5 to sys_clone() */
  217. if (clone_flags & CLONE_SETTLS)
  218. c_uregs->e2 = current_frame()->d3;
  219. /* set up the return kernel frame if called from kernel_thread() */
  220. c_kregs = c_uregs;
  221. if (kregs != uregs) {
  222. c_ksp -= sizeof(struct pt_regs);
  223. c_kregs = (struct pt_regs *) c_ksp;
  224. *c_kregs = *kregs;
  225. c_kregs->sp = c_usp;
  226. c_kregs->next = c_uregs;
  227. #ifdef CONFIG_MN10300_CURRENT_IN_E2
  228. c_kregs->e2 = (unsigned long) p; /* current */
  229. #endif
  230. c_ksp -= 12; /* allocate function call ABI slack */
  231. }
  232. /* set up things up so the scheduler can start the new task */
  233. ti->frame = c_kregs;
  234. p->thread.a3 = (unsigned long) c_kregs;
  235. p->thread.sp = c_ksp;
  236. p->thread.pc = (unsigned long) ret_from_fork;
  237. p->thread.wchan = (unsigned long) ret_from_fork;
  238. p->thread.usp = c_usp;
  239. return 0;
  240. }
  241. /*
  242. * clone a process
  243. * - tlsptr is retrieved by copy_thread() from current_frame()->d3
  244. */
  245. asmlinkage long sys_clone(unsigned long clone_flags, unsigned long newsp,
  246. int __user *parent_tidptr, int __user *child_tidptr,
  247. int __user *tlsptr)
  248. {
  249. return do_fork(clone_flags, newsp ?: current_frame()->sp,
  250. current_frame(), 0, parent_tidptr, child_tidptr);
  251. }
  252. asmlinkage long sys_fork(void)
  253. {
  254. return do_fork(SIGCHLD, current_frame()->sp,
  255. current_frame(), 0, NULL, NULL);
  256. }
  257. asmlinkage long sys_vfork(void)
  258. {
  259. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, current_frame()->sp,
  260. current_frame(), 0, NULL, NULL);
  261. }
  262. asmlinkage long sys_execve(const char __user *name,
  263. const char __user *const __user *argv,
  264. const char __user *const __user *envp)
  265. {
  266. char *filename;
  267. int error;
  268. filename = getname(name);
  269. error = PTR_ERR(filename);
  270. if (IS_ERR(filename))
  271. return error;
  272. error = do_execve(filename, argv, envp, current_frame());
  273. putname(filename);
  274. return error;
  275. }
  276. unsigned long get_wchan(struct task_struct *p)
  277. {
  278. return p->thread.wchan;
  279. }