process.c 6.0 KB

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