process.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/smp_lock.h>
  18. #include <linux/stddef.h>
  19. #include <linux/unistd.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/slab.h>
  22. #include <linux/user.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/reboot.h>
  26. #include <linux/percpu.h>
  27. #include <linux/err.h>
  28. #include <linux/fs.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/system.h>
  32. #include <asm/io.h>
  33. #include <asm/processor.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/fpu.h>
  36. #include <asm/reset-regs.h>
  37. #include <asm/gdb-stub.h>
  38. #include "internal.h"
  39. /*
  40. * power management idle function, if any..
  41. */
  42. void (*pm_idle)(void);
  43. EXPORT_SYMBOL(pm_idle);
  44. /*
  45. * return saved PC of a blocked thread.
  46. */
  47. unsigned long thread_saved_pc(struct task_struct *tsk)
  48. {
  49. return ((unsigned long *) tsk->thread.sp)[3];
  50. }
  51. /*
  52. * power off function, if any
  53. */
  54. void (*pm_power_off)(void);
  55. EXPORT_SYMBOL(pm_power_off);
  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. /*
  68. * the idle thread
  69. * - there's no useful work to be done, so just try to conserve power and have
  70. * a low exit latency (ie sit in a loop waiting for somebody to say that
  71. * they'd like to reschedule)
  72. */
  73. void cpu_idle(void)
  74. {
  75. int cpu = smp_processor_id();
  76. /* endless idle loop with no priority at all */
  77. for (;;) {
  78. while (!need_resched()) {
  79. void (*idle)(void);
  80. smp_rmb();
  81. idle = pm_idle;
  82. if (!idle)
  83. idle = default_idle;
  84. irq_stat[cpu].idle_timestamp = jiffies;
  85. idle();
  86. }
  87. preempt_enable_no_resched();
  88. schedule();
  89. preempt_disable();
  90. }
  91. }
  92. void release_segments(struct mm_struct *mm)
  93. {
  94. }
  95. void machine_restart(char *cmd)
  96. {
  97. #ifdef CONFIG_GDBSTUB
  98. gdbstub_exit(0);
  99. #endif
  100. #ifdef mn10300_unit_hard_reset
  101. mn10300_unit_hard_reset();
  102. #else
  103. mn10300_proc_hard_reset();
  104. #endif
  105. }
  106. void machine_halt(void)
  107. {
  108. #ifdef CONFIG_GDBSTUB
  109. gdbstub_exit(0);
  110. #endif
  111. }
  112. void machine_power_off(void)
  113. {
  114. #ifdef CONFIG_GDBSTUB
  115. gdbstub_exit(0);
  116. #endif
  117. }
  118. void show_regs(struct pt_regs *regs)
  119. {
  120. }
  121. /*
  122. * create a kernel thread
  123. */
  124. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  125. {
  126. struct pt_regs regs;
  127. memset(&regs, 0, sizeof(regs));
  128. regs.a2 = (unsigned long) fn;
  129. regs.d2 = (unsigned long) arg;
  130. regs.pc = (unsigned long) kernel_thread_helper;
  131. local_save_flags(regs.epsw);
  132. regs.epsw |= EPSW_IE | EPSW_IM_7;
  133. /* Ok, create the new process.. */
  134. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0,
  135. NULL, NULL);
  136. }
  137. EXPORT_SYMBOL(kernel_thread);
  138. /*
  139. * free current thread data structures etc..
  140. */
  141. void exit_thread(void)
  142. {
  143. exit_fpu();
  144. }
  145. void flush_thread(void)
  146. {
  147. flush_fpu();
  148. }
  149. void release_thread(struct task_struct *dead_task)
  150. {
  151. }
  152. /*
  153. * we do not have to muck with descriptors here, that is
  154. * done in switch_mm() as needed.
  155. */
  156. void copy_segments(struct task_struct *p, struct mm_struct *new_mm)
  157. {
  158. }
  159. /*
  160. * this gets called before we allocate a new thread and copy the current task
  161. * into it so that we can store lazy state into memory
  162. */
  163. void prepare_to_copy(struct task_struct *tsk)
  164. {
  165. unlazy_fpu(tsk);
  166. }
  167. /*
  168. * set up the kernel stack for a new thread and copy arch-specific thread
  169. * control information
  170. */
  171. int copy_thread(int nr, unsigned long clone_flags,
  172. unsigned long c_usp, unsigned long ustk_size,
  173. struct task_struct *p, struct pt_regs *kregs)
  174. {
  175. struct pt_regs *c_uregs, *c_kregs, *uregs;
  176. unsigned long c_ksp;
  177. uregs = current->thread.uregs;
  178. c_ksp = (unsigned long) task_stack_page(p) + THREAD_SIZE;
  179. /* allocate the userspace exception frame and set it up */
  180. c_ksp -= sizeof(struct pt_regs);
  181. c_uregs = (struct pt_regs *) c_ksp;
  182. p->thread.uregs = c_uregs;
  183. *c_uregs = *uregs;
  184. c_uregs->sp = c_usp;
  185. c_uregs->epsw &= ~EPSW_FE; /* my FPU */
  186. c_ksp -= 12; /* allocate function call ABI slack */
  187. /* the new TLS pointer is passed in as arg #5 to sys_clone() */
  188. if (clone_flags & CLONE_SETTLS)
  189. c_uregs->e2 = __frame->d3;
  190. /* set up the return kernel frame if called from kernel_thread() */
  191. c_kregs = c_uregs;
  192. if (kregs != uregs) {
  193. c_ksp -= sizeof(struct pt_regs);
  194. c_kregs = (struct pt_regs *) c_ksp;
  195. *c_kregs = *kregs;
  196. c_kregs->sp = c_usp;
  197. c_kregs->next = c_uregs;
  198. #ifdef CONFIG_MN10300_CURRENT_IN_E2
  199. c_kregs->e2 = (unsigned long) p; /* current */
  200. #endif
  201. c_ksp -= 12; /* allocate function call ABI slack */
  202. }
  203. /* set up things up so the scheduler can start the new task */
  204. p->thread.__frame = c_kregs;
  205. p->thread.a3 = (unsigned long) c_kregs;
  206. p->thread.sp = c_ksp;
  207. p->thread.pc = (unsigned long) ret_from_fork;
  208. p->thread.wchan = (unsigned long) ret_from_fork;
  209. p->thread.usp = c_usp;
  210. return 0;
  211. }
  212. /*
  213. * clone a process
  214. * - tlsptr is retrieved by copy_thread() from __frame->d3
  215. */
  216. asmlinkage long sys_clone(unsigned long clone_flags, unsigned long newsp,
  217. int __user *parent_tidptr, int __user *child_tidptr,
  218. int __user *tlsptr)
  219. {
  220. return do_fork(clone_flags, newsp ?: __frame->sp, __frame, 0,
  221. parent_tidptr, child_tidptr);
  222. }
  223. asmlinkage long sys_fork(void)
  224. {
  225. return do_fork(SIGCHLD, __frame->sp, __frame, 0, NULL, NULL);
  226. }
  227. asmlinkage long sys_vfork(void)
  228. {
  229. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, __frame->sp, __frame,
  230. 0, NULL, NULL);
  231. }
  232. asmlinkage long sys_execve(char __user *name,
  233. char __user * __user *argv,
  234. char __user * __user *envp)
  235. {
  236. char *filename;
  237. int error;
  238. lock_kernel();
  239. filename = getname(name);
  240. error = PTR_ERR(filename);
  241. if (!IS_ERR(filename)) {
  242. error = do_execve(filename, argv, envp, __frame);
  243. if (error == 0)
  244. current->ptrace &= ~PT_DTRACE;
  245. putname(filename);
  246. }
  247. unlock_kernel();
  248. return error;
  249. }
  250. unsigned long get_wchan(struct task_struct *p)
  251. {
  252. return p->thread.wchan;
  253. }