process.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Process creation support for Hexagon
  3. *
  4. * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <linux/sched.h>
  21. #include <linux/types.h>
  22. #include <linux/module.h>
  23. #include <linux/tick.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/slab.h>
  26. #include <linux/tracehook.h>
  27. /*
  28. * Program thread launch. Often defined as a macro in processor.h,
  29. * but we're shooting for a small footprint and it's not an inner-loop
  30. * performance-critical operation.
  31. *
  32. * The Hexagon ABI specifies that R28 is zero'ed before program launch,
  33. * so that gets automatically done here. If we ever stop doing that here,
  34. * we'll probably want to define the ELF_PLAT_INIT macro.
  35. */
  36. void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
  37. {
  38. /* Set to run with user-mode data segmentation */
  39. set_fs(USER_DS);
  40. /* We want to zero all data-containing registers. Is this overkill? */
  41. memset(regs, 0, sizeof(*regs));
  42. /* We might want to also zero all Processor registers here */
  43. pt_set_usermode(regs);
  44. pt_set_elr(regs, pc);
  45. pt_set_rte_sp(regs, sp);
  46. }
  47. /*
  48. * Spin, or better still, do a hardware or VM wait instruction
  49. * If hardware or VM offer wait termination even though interrupts
  50. * are disabled.
  51. */
  52. static void default_idle(void)
  53. {
  54. __vmwait();
  55. }
  56. void (*idle_sleep)(void) = default_idle;
  57. void cpu_idle(void)
  58. {
  59. while (1) {
  60. tick_nohz_idle_enter();
  61. local_irq_disable();
  62. while (!need_resched()) {
  63. idle_sleep();
  64. /* interrupts wake us up, but aren't serviced */
  65. local_irq_enable(); /* service interrupt */
  66. local_irq_disable();
  67. }
  68. local_irq_enable();
  69. tick_nohz_idle_exit();
  70. schedule();
  71. }
  72. }
  73. /*
  74. * Return saved PC of a blocked thread
  75. */
  76. unsigned long thread_saved_pc(struct task_struct *tsk)
  77. {
  78. return 0;
  79. }
  80. /*
  81. * Copy architecture-specific thread state
  82. */
  83. int copy_thread(unsigned long clone_flags, unsigned long usp,
  84. unsigned long arg, struct task_struct *p)
  85. {
  86. struct thread_info *ti = task_thread_info(p);
  87. struct hexagon_switch_stack *ss;
  88. struct pt_regs *childregs;
  89. asmlinkage void ret_from_fork(void);
  90. childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) -
  91. sizeof(*childregs));
  92. ti->regs = childregs;
  93. /*
  94. * Establish kernel stack pointer and initial PC for new thread
  95. * Note that unlike the usual situation, we do not copy the
  96. * parent's callee-saved here; those are in pt_regs and whatever
  97. * we leave here will be overridden on return to userland.
  98. */
  99. ss = (struct hexagon_switch_stack *) ((unsigned long) childregs -
  100. sizeof(*ss));
  101. ss->lr = (unsigned long)ret_from_fork;
  102. p->thread.switch_sp = ss;
  103. if (unlikely(p->flags & PF_KTHREAD)) {
  104. memset(childregs, 0, sizeof(struct pt_regs));
  105. /* r24 <- fn, r25 <- arg */
  106. ss->r24 = usp;
  107. ss->r25 = arg;
  108. pt_set_kmode(childregs);
  109. return 0;
  110. }
  111. memcpy(childregs, current_pt_regs(), sizeof(*childregs));
  112. ss->r2524 = 0;
  113. if (usp)
  114. pt_set_rte_sp(childregs, usp);
  115. /* Child sees zero return value */
  116. childregs->r00 = 0;
  117. /*
  118. * The clone syscall has the C signature:
  119. * int [r0] clone(int flags [r0],
  120. * void *child_frame [r1],
  121. * void *parent_tid [r2],
  122. * void *child_tid [r3],
  123. * void *thread_control_block [r4]);
  124. * ugp is used to provide TLS support.
  125. */
  126. if (clone_flags & CLONE_SETTLS)
  127. childregs->ugp = childregs->r04;
  128. /*
  129. * Parent sees new pid -- not necessary, not even possible at
  130. * this point in the fork process
  131. * Might also want to set things like ti->addr_limit
  132. */
  133. return 0;
  134. }
  135. /*
  136. * Release any architecture-specific resources locked by thread
  137. */
  138. void release_thread(struct task_struct *dead_task)
  139. {
  140. }
  141. /*
  142. * Free any architecture-specific thread data structures, etc.
  143. */
  144. void exit_thread(void)
  145. {
  146. }
  147. /*
  148. * Some archs flush debug and FPU info here
  149. */
  150. void flush_thread(void)
  151. {
  152. }
  153. /*
  154. * The "wait channel" terminology is archaic, but what we want
  155. * is an identification of the point at which the scheduler
  156. * was invoked by a blocked thread.
  157. */
  158. unsigned long get_wchan(struct task_struct *p)
  159. {
  160. unsigned long fp, pc;
  161. unsigned long stack_page;
  162. int count = 0;
  163. if (!p || p == current || p->state == TASK_RUNNING)
  164. return 0;
  165. stack_page = (unsigned long)task_stack_page(p);
  166. fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp;
  167. do {
  168. if (fp < (stack_page + sizeof(struct thread_info)) ||
  169. fp >= (THREAD_SIZE - 8 + stack_page))
  170. return 0;
  171. pc = ((unsigned long *)fp)[1];
  172. if (!in_sched_functions(pc))
  173. return pc;
  174. fp = *(unsigned long *) fp;
  175. } while (count++ < 16);
  176. return 0;
  177. }
  178. /*
  179. * Required placeholder.
  180. */
  181. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  182. {
  183. return 0;
  184. }
  185. /*
  186. * Called on the exit path of event entry; see vm_entry.S
  187. *
  188. * Interrupts will already be disabled.
  189. *
  190. * Returns 0 if there's no need to re-check for more work.
  191. */
  192. int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)
  193. {
  194. if (!(thread_info_flags & _TIF_WORK_MASK)) {
  195. return 0;
  196. } /* shortcut -- no work to be done */
  197. local_irq_enable();
  198. if (thread_info_flags & _TIF_NEED_RESCHED) {
  199. schedule();
  200. return 1;
  201. }
  202. if (thread_info_flags & _TIF_SIGPENDING) {
  203. do_signal(regs);
  204. return 1;
  205. }
  206. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  207. clear_thread_flag(TIF_NOTIFY_RESUME);
  208. tracehook_notify_resume(regs);
  209. }
  210. /* Should not even reach here */
  211. panic("%s: bad thread_info flags 0x%08x\n", __func__,
  212. thread_info_flags);
  213. }