process.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * arch/v850/kernel/process.c -- Arch-dependent process handling
  3. *
  4. * Copyright (C) 2001,02,03 NEC Electronics Corporation
  5. * Copyright (C) 2001,02,03 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. #include <linux/config.h>
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/smp.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/stddef.h>
  21. #include <linux/unistd.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/slab.h>
  24. #include <linux/user.h>
  25. #include <linux/a.out.h>
  26. #include <linux/reboot.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/system.h>
  29. #include <asm/pgtable.h>
  30. void (*pm_power_off)(void) = NULL;
  31. EXPORT_SYMBOL(pm_power_off);
  32. extern void ret_from_fork (void);
  33. /* The idle loop. */
  34. static void default_idle (void)
  35. {
  36. while (! need_resched ())
  37. asm ("halt; nop; nop; nop; nop; nop" ::: "cc");
  38. }
  39. void (*idle)(void) = default_idle;
  40. /*
  41. * The idle thread. There's no useful work to be
  42. * done, so just try to conserve power and have a
  43. * low exit latency (ie sit in a loop waiting for
  44. * somebody to say that they'd like to reschedule)
  45. */
  46. void cpu_idle (void)
  47. {
  48. /* endless idle loop with no priority at all */
  49. while (1) {
  50. while (!need_resched())
  51. (*idle) ();
  52. preempt_enable_no_resched();
  53. schedule();
  54. preempt_disable();
  55. }
  56. }
  57. /*
  58. * This is the mechanism for creating a new kernel thread.
  59. *
  60. * NOTE! Only a kernel-only process (ie the swapper or direct descendants who
  61. * haven't done an "execve()") should use this: it will work within a system
  62. * call from a "real" process, but the process memory space will not be free'd
  63. * until both the parent and the child have exited.
  64. */
  65. int kernel_thread (int (*fn)(void *), void *arg, unsigned long flags)
  66. {
  67. register mm_segment_t fs = get_fs ();
  68. register unsigned long syscall asm (SYSCALL_NUM);
  69. register unsigned long arg0 asm (SYSCALL_ARG0);
  70. register unsigned long ret asm (SYSCALL_RET);
  71. set_fs (KERNEL_DS);
  72. /* Clone this thread. Note that we don't pass the clone syscall's
  73. second argument -- it's ignored for calls from kernel mode (the
  74. child's SP is always set to the top of the kernel stack). */
  75. arg0 = flags | CLONE_VM;
  76. syscall = __NR_clone;
  77. asm volatile ("trap " SYSCALL_SHORT_TRAP
  78. : "=r" (ret), "=r" (syscall)
  79. : "1" (syscall), "r" (arg0)
  80. : SYSCALL_SHORT_CLOBBERS);
  81. if (ret == 0) {
  82. /* In child thread, call FN and exit. */
  83. arg0 = (*fn) (arg);
  84. syscall = __NR_exit;
  85. asm volatile ("trap " SYSCALL_SHORT_TRAP
  86. : "=r" (ret), "=r" (syscall)
  87. : "1" (syscall), "r" (arg0)
  88. : SYSCALL_SHORT_CLOBBERS);
  89. }
  90. /* In parent. */
  91. set_fs (fs);
  92. return ret;
  93. }
  94. void flush_thread (void)
  95. {
  96. set_fs (USER_DS);
  97. }
  98. int copy_thread (int nr, unsigned long clone_flags,
  99. unsigned long stack_start, unsigned long stack_size,
  100. struct task_struct *p, struct pt_regs *regs)
  101. {
  102. /* Start pushing stuff from the top of the child's kernel stack. */
  103. unsigned long orig_ksp = task_tos(p);
  104. unsigned long ksp = orig_ksp;
  105. /* We push two `state save' stack fames (see entry.S) on the new
  106. kernel stack:
  107. 1) The innermost one is what switch_thread would have
  108. pushed, and is used when we context switch to the child
  109. thread for the first time. It's set up to return to
  110. ret_from_fork in entry.S.
  111. 2) The outermost one (nearest the top) is what a syscall
  112. trap would have pushed, and is set up to return to the
  113. same location as the parent thread, but with a return
  114. value of 0. */
  115. struct pt_regs *child_switch_regs, *child_trap_regs;
  116. /* Trap frame. */
  117. ksp -= STATE_SAVE_SIZE;
  118. child_trap_regs = (struct pt_regs *)(ksp + STATE_SAVE_PT_OFFSET);
  119. /* Switch frame. */
  120. ksp -= STATE_SAVE_SIZE;
  121. child_switch_regs = (struct pt_regs *)(ksp + STATE_SAVE_PT_OFFSET);
  122. /* First copy parent's register state to child. */
  123. *child_switch_regs = *regs;
  124. *child_trap_regs = *regs;
  125. /* switch_thread returns to the restored value of the lp
  126. register (r31), so we make that the place where we want to
  127. jump when the child thread begins running. */
  128. child_switch_regs->gpr[GPR_LP] = (v850_reg_t)ret_from_fork;
  129. if (regs->kernel_mode)
  130. /* Since we're returning to kernel-mode, make sure the child's
  131. stored kernel stack pointer agrees with what the actual
  132. stack pointer will be at that point (the trap return code
  133. always restores the SP, even when returning to
  134. kernel-mode). */
  135. child_trap_regs->gpr[GPR_SP] = orig_ksp;
  136. else
  137. /* Set the child's user-mode stack-pointer (the name
  138. `stack_start' is a misnomer, it's just the initial SP
  139. value). */
  140. child_trap_regs->gpr[GPR_SP] = stack_start;
  141. /* Thread state for the child (everything else is on the stack). */
  142. p->thread.ksp = ksp;
  143. return 0;
  144. }
  145. /*
  146. * sys_execve() executes a new program.
  147. */
  148. int sys_execve (char *name, char **argv, char **envp, struct pt_regs *regs)
  149. {
  150. char *filename = getname (name);
  151. int error = PTR_ERR (filename);
  152. if (! IS_ERR (filename)) {
  153. error = do_execve (filename, argv, envp, regs);
  154. putname (filename);
  155. }
  156. return error;
  157. }
  158. /*
  159. * These bracket the sleeping functions..
  160. */
  161. #define first_sched ((unsigned long)__sched_text_start)
  162. #define last_sched ((unsigned long)__sched_text_end)
  163. unsigned long get_wchan (struct task_struct *p)
  164. {
  165. #if 0 /* Barf. Figure out the stack-layout later. XXX */
  166. unsigned long fp, pc;
  167. int count = 0;
  168. if (!p || p == current || p->state == TASK_RUNNING)
  169. return 0;
  170. pc = thread_saved_pc (p);
  171. /* This quite disgusting function walks up the stack, following
  172. saved return address, until it something that's out of bounds
  173. (as defined by `first_sched' and `last_sched'). It then
  174. returns the last PC that was in-bounds. */
  175. do {
  176. if (fp < stack_page + sizeof (struct task_struct) ||
  177. fp >= 8184+stack_page)
  178. return 0;
  179. pc = ((unsigned long *)fp)[1];
  180. if (pc < first_sched || pc >= last_sched)
  181. return pc;
  182. fp = *(unsigned long *) fp;
  183. } while (count++ < 16);
  184. #endif
  185. return 0;
  186. }