process.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. /*
  27. * Kernel thread creation. The desired kernel function is "wrapped"
  28. * in the kernel_thread_helper function, which does cleanup
  29. * afterwards.
  30. */
  31. static void __noreturn kernel_thread_helper(void *arg, int (*fn)(void *))
  32. {
  33. do_exit(fn(arg));
  34. }
  35. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  36. {
  37. struct pt_regs regs;
  38. memset(&regs, 0, sizeof(regs));
  39. /*
  40. * Yes, we're exploting illicit knowledge of the ABI here.
  41. */
  42. regs.r00 = (unsigned long) arg;
  43. regs.r01 = (unsigned long) fn;
  44. pt_set_elr(&regs, (unsigned long)kernel_thread_helper);
  45. pt_set_kmode(&regs);
  46. return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  47. }
  48. EXPORT_SYMBOL(kernel_thread);
  49. /*
  50. * Program thread launch. Often defined as a macro in processor.h,
  51. * but we're shooting for a small footprint and it's not an inner-loop
  52. * performance-critical operation.
  53. *
  54. * The Hexagon ABI specifies that R28 is zero'ed before program launch,
  55. * so that gets automatically done here. If we ever stop doing that here,
  56. * we'll probably want to define the ELF_PLAT_INIT macro.
  57. */
  58. void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
  59. {
  60. /* Set to run with user-mode data segmentation */
  61. set_fs(USER_DS);
  62. /* We want to zero all data-containing registers. Is this overkill? */
  63. memset(regs, 0, sizeof(*regs));
  64. /* We might want to also zero all Processor registers here */
  65. pt_set_usermode(regs);
  66. pt_set_elr(regs, pc);
  67. pt_set_rte_sp(regs, sp);
  68. }
  69. /*
  70. * Spin, or better still, do a hardware or VM wait instruction
  71. * If hardware or VM offer wait termination even though interrupts
  72. * are disabled.
  73. */
  74. static void default_idle(void)
  75. {
  76. __vmwait();
  77. }
  78. void (*idle_sleep)(void) = default_idle;
  79. void cpu_idle(void)
  80. {
  81. while (1) {
  82. tick_nohz_idle_enter();
  83. local_irq_disable();
  84. while (!need_resched()) {
  85. idle_sleep();
  86. /* interrupts wake us up, but aren't serviced */
  87. local_irq_enable(); /* service interrupt */
  88. local_irq_disable();
  89. }
  90. local_irq_enable();
  91. tick_nohz_idle_exit();
  92. schedule();
  93. }
  94. }
  95. /*
  96. * Return saved PC of a blocked thread
  97. */
  98. unsigned long thread_saved_pc(struct task_struct *tsk)
  99. {
  100. return 0;
  101. }
  102. /*
  103. * Copy architecture-specific thread state
  104. */
  105. int copy_thread(unsigned long clone_flags, unsigned long usp,
  106. unsigned long unused, struct task_struct *p,
  107. struct pt_regs *regs)
  108. {
  109. struct thread_info *ti = task_thread_info(p);
  110. struct hexagon_switch_stack *ss;
  111. struct pt_regs *childregs;
  112. asmlinkage void ret_from_fork(void);
  113. childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) -
  114. sizeof(*childregs));
  115. memcpy(childregs, regs, sizeof(*childregs));
  116. ti->regs = childregs;
  117. /*
  118. * Establish kernel stack pointer and initial PC for new thread
  119. */
  120. ss = (struct hexagon_switch_stack *) ((unsigned long) childregs -
  121. sizeof(*ss));
  122. ss->lr = (unsigned long)ret_from_fork;
  123. p->thread.switch_sp = ss;
  124. /* If User mode thread, set pt_reg stack pointer as per parameter */
  125. if (user_mode(childregs)) {
  126. pt_set_rte_sp(childregs, usp);
  127. /* Child sees zero return value */
  128. childregs->r00 = 0;
  129. /*
  130. * The clone syscall has the C signature:
  131. * int [r0] clone(int flags [r0],
  132. * void *child_frame [r1],
  133. * void *parent_tid [r2],
  134. * void *child_tid [r3],
  135. * void *thread_control_block [r4]);
  136. * ugp is used to provide TLS support.
  137. */
  138. if (clone_flags & CLONE_SETTLS)
  139. childregs->ugp = childregs->r04;
  140. /*
  141. * Parent sees new pid -- not necessary, not even possible at
  142. * this point in the fork process
  143. * Might also want to set things like ti->addr_limit
  144. */
  145. } else {
  146. /*
  147. * If kernel thread, resume stack is kernel stack base.
  148. * Note that this is pointer arithmetic on pt_regs *
  149. */
  150. pt_set_rte_sp(childregs, (unsigned long)(childregs + 1));
  151. /*
  152. * We need the current thread_info fast path pointer
  153. * set up in pt_regs. The register to be used is
  154. * parametric for assembler code, but the mechanism
  155. * doesn't drop neatly into C. Needs to be fixed.
  156. */
  157. childregs->THREADINFO_REG = (unsigned long) ti;
  158. }
  159. /*
  160. * thread_info pointer is pulled out of task_struct "stack"
  161. * field on switch_to.
  162. */
  163. p->stack = (void *)ti;
  164. return 0;
  165. }
  166. /*
  167. * Release any architecture-specific resources locked by thread
  168. */
  169. void release_thread(struct task_struct *dead_task)
  170. {
  171. }
  172. /*
  173. * Free any architecture-specific thread data structures, etc.
  174. */
  175. void exit_thread(void)
  176. {
  177. }
  178. /*
  179. * Some archs flush debug and FPU info here
  180. */
  181. void flush_thread(void)
  182. {
  183. }
  184. /*
  185. * The "wait channel" terminology is archaic, but what we want
  186. * is an identification of the point at which the scheduler
  187. * was invoked by a blocked thread.
  188. */
  189. unsigned long get_wchan(struct task_struct *p)
  190. {
  191. unsigned long fp, pc;
  192. unsigned long stack_page;
  193. int count = 0;
  194. if (!p || p == current || p->state == TASK_RUNNING)
  195. return 0;
  196. stack_page = (unsigned long)task_stack_page(p);
  197. fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp;
  198. do {
  199. if (fp < (stack_page + sizeof(struct thread_info)) ||
  200. fp >= (THREAD_SIZE - 8 + stack_page))
  201. return 0;
  202. pc = ((unsigned long *)fp)[1];
  203. if (!in_sched_functions(pc))
  204. return pc;
  205. fp = *(unsigned long *) fp;
  206. } while (count++ < 16);
  207. return 0;
  208. }
  209. /*
  210. * Borrowed from PowerPC -- basically allow smaller kernel stacks if we
  211. * go crazy with the page sizes.
  212. */
  213. #if THREAD_SHIFT < PAGE_SHIFT
  214. static struct kmem_cache *thread_info_cache;
  215. struct thread_info *alloc_thread_info_node(struct task_struct *tsk, int node)
  216. {
  217. struct thread_info *ti;
  218. ti = kmem_cache_alloc_node(thread_info_cache, GFP_KERNEL, node);
  219. if (unlikely(ti == NULL))
  220. return NULL;
  221. #ifdef CONFIG_DEBUG_STACK_USAGE
  222. memset(ti, 0, THREAD_SIZE);
  223. #endif
  224. return ti;
  225. }
  226. void free_thread_info(struct thread_info *ti)
  227. {
  228. kmem_cache_free(thread_info_cache, ti);
  229. }
  230. /* Weak symbol; called by init/main.c */
  231. void thread_info_cache_init(void)
  232. {
  233. thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
  234. THREAD_SIZE, 0, NULL);
  235. BUG_ON(thread_info_cache == NULL);
  236. }
  237. #endif /* THREAD_SHIFT < PAGE_SHIFT */
  238. /*
  239. * Required placeholder.
  240. */
  241. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  242. {
  243. return 0;
  244. }