process.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <asm/uaccess.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/io.h>
  31. #include <asm/processor.h>
  32. #include <asm/mmu_context.h>
  33. #include <asm/fpu.h>
  34. #include <asm/reset-regs.h>
  35. #include <asm/gdb-stub.h>
  36. #include "internal.h"
  37. /*
  38. * power management idle function, if any..
  39. */
  40. void (*pm_idle)(void);
  41. EXPORT_SYMBOL(pm_idle);
  42. /*
  43. * return saved PC of a blocked thread.
  44. */
  45. unsigned long thread_saved_pc(struct task_struct *tsk)
  46. {
  47. return ((unsigned long *) tsk->thread.sp)[3];
  48. }
  49. /*
  50. * power off function, if any
  51. */
  52. void (*pm_power_off)(void);
  53. EXPORT_SYMBOL(pm_power_off);
  54. #if !defined(CONFIG_SMP) || defined(CONFIG_HOTPLUG_CPU)
  55. /*
  56. * we use this if we don't have any better idle routine
  57. */
  58. static void default_idle(void)
  59. {
  60. local_irq_disable();
  61. if (!need_resched())
  62. safe_halt();
  63. else
  64. local_irq_enable();
  65. }
  66. #else /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU */
  67. /*
  68. * On SMP it's slightly faster (but much more power-consuming!)
  69. * to poll the ->work.need_resched flag instead of waiting for the
  70. * cross-CPU IPI to arrive. Use this option with caution.
  71. */
  72. static inline void poll_idle(void)
  73. {
  74. int oldval;
  75. local_irq_enable();
  76. /*
  77. * Deal with another CPU just having chosen a thread to
  78. * run here:
  79. */
  80. oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
  81. if (!oldval) {
  82. set_thread_flag(TIF_POLLING_NRFLAG);
  83. while (!need_resched())
  84. cpu_relax();
  85. clear_thread_flag(TIF_POLLING_NRFLAG);
  86. } else {
  87. set_need_resched();
  88. }
  89. }
  90. #endif /* !CONFIG_SMP || CONFIG_HOTPLUG_CPU */
  91. /*
  92. * the idle thread
  93. * - there's no useful work to be done, so just try to conserve power and have
  94. * a low exit latency (ie sit in a loop waiting for somebody to say that
  95. * they'd like to reschedule)
  96. */
  97. void cpu_idle(void)
  98. {
  99. /* endless idle loop with no priority at all */
  100. for (;;) {
  101. while (!need_resched()) {
  102. void (*idle)(void);
  103. smp_rmb();
  104. idle = pm_idle;
  105. if (!idle) {
  106. #if defined(CONFIG_SMP) && !defined(CONFIG_HOTPLUG_CPU)
  107. idle = poll_idle;
  108. #else /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
  109. idle = default_idle;
  110. #endif /* CONFIG_SMP && !CONFIG_HOTPLUG_CPU */
  111. }
  112. idle();
  113. }
  114. schedule_preempt_disabled();
  115. }
  116. }
  117. void release_segments(struct mm_struct *mm)
  118. {
  119. }
  120. void machine_restart(char *cmd)
  121. {
  122. #ifdef CONFIG_KERNEL_DEBUGGER
  123. gdbstub_exit(0);
  124. #endif
  125. #ifdef mn10300_unit_hard_reset
  126. mn10300_unit_hard_reset();
  127. #else
  128. mn10300_proc_hard_reset();
  129. #endif
  130. }
  131. void machine_halt(void)
  132. {
  133. #ifdef CONFIG_KERNEL_DEBUGGER
  134. gdbstub_exit(0);
  135. #endif
  136. }
  137. void machine_power_off(void)
  138. {
  139. #ifdef CONFIG_KERNEL_DEBUGGER
  140. gdbstub_exit(0);
  141. #endif
  142. }
  143. void show_regs(struct pt_regs *regs)
  144. {
  145. }
  146. /*
  147. * create a kernel thread
  148. */
  149. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  150. {
  151. struct pt_regs regs = {
  152. .a0 = (unsigned long) fn;
  153. .d0 = (unsigned long) arg;
  154. };
  155. local_save_flags(regs.epsw);
  156. regs.epsw |= EPSW_IE | EPSW_IM_7;
  157. /* Ok, create the new process.. */
  158. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0,
  159. NULL, NULL);
  160. }
  161. /*
  162. * free current thread data structures etc..
  163. */
  164. void exit_thread(void)
  165. {
  166. exit_fpu();
  167. }
  168. void flush_thread(void)
  169. {
  170. flush_fpu();
  171. }
  172. void release_thread(struct task_struct *dead_task)
  173. {
  174. }
  175. /*
  176. * we do not have to muck with descriptors here, that is
  177. * done in switch_mm() as needed.
  178. */
  179. void copy_segments(struct task_struct *p, struct mm_struct *new_mm)
  180. {
  181. }
  182. /*
  183. * this gets called so that we can store lazy state into memory and copy the
  184. * current task into the new thread.
  185. */
  186. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  187. {
  188. unlazy_fpu(src);
  189. *dst = *src;
  190. return 0;
  191. }
  192. /*
  193. * set up the kernel stack for a new thread and copy arch-specific thread
  194. * control information
  195. */
  196. int copy_thread(unsigned long clone_flags,
  197. unsigned long c_usp, unsigned long ustk_size,
  198. struct task_struct *p, struct pt_regs *kregs)
  199. {
  200. struct thread_info *ti = task_thread_info(p);
  201. struct pt_regs *c_regs;
  202. unsigned long c_ksp;
  203. c_ksp = (unsigned long) task_stack_page(p) + THREAD_SIZE;
  204. /* allocate the userspace exception frame and set it up */
  205. c_ksp -= sizeof(struct pt_regs);
  206. c_regs = (struct pt_regs *) c_ksp;
  207. p->thread.uregs = c_regs;
  208. *c_regs = *kregs;
  209. c_regs->sp = c_usp;
  210. c_regs->epsw &= ~EPSW_FE; /* my FPU */
  211. c_ksp -= 12; /* allocate function call ABI slack */
  212. /* the new TLS pointer is passed in as arg #5 to sys_clone() */
  213. if (clone_flags & CLONE_SETTLS)
  214. c_regs->e2 = current_frame()->d3;
  215. if (unlikely(!user_mode(kregs)))
  216. p->thread.pc = (unsigned long) ret_from_kernel_thread;
  217. else
  218. p->thread.pc = (unsigned long) ret_from_fork;
  219. /* set up things up so the scheduler can start the new task */
  220. ti->frame = c_regs;
  221. p->thread.a3 = (unsigned long) c_regs;
  222. p->thread.sp = c_ksp;
  223. p->thread.wchan = p->thread.pc;
  224. p->thread.usp = c_usp;
  225. return 0;
  226. }
  227. /*
  228. * clone a process
  229. * - tlsptr is retrieved by copy_thread() from current_frame()->d3
  230. */
  231. asmlinkage long sys_clone(unsigned long clone_flags, unsigned long newsp,
  232. int __user *parent_tidptr, int __user *child_tidptr,
  233. int __user *tlsptr)
  234. {
  235. return do_fork(clone_flags, newsp ?: current_frame()->sp,
  236. current_frame(), 0, parent_tidptr, child_tidptr);
  237. }
  238. asmlinkage long sys_fork(void)
  239. {
  240. return do_fork(SIGCHLD, current_frame()->sp,
  241. current_frame(), 0, NULL, NULL);
  242. }
  243. asmlinkage long sys_vfork(void)
  244. {
  245. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, current_frame()->sp,
  246. current_frame(), 0, NULL, NULL);
  247. }
  248. unsigned long get_wchan(struct task_struct *p)
  249. {
  250. return p->thread.wchan;
  251. }