process.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Amit Bhor, Kanika Nema: Codito Technologies 2004
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/fs.h>
  15. #include <linux/unistd.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/slab.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/elf.h>
  20. #include <linux/tick.h>
  21. SYSCALL_DEFINE1(arc_settls, void *, user_tls_data_ptr)
  22. {
  23. task_thread_info(current)->thr_ptr = (unsigned int)user_tls_data_ptr;
  24. return 0;
  25. }
  26. /*
  27. * We return the user space TLS data ptr as sys-call return code
  28. * Ideally it should be copy to user.
  29. * However we can cheat by the fact that some sys-calls do return
  30. * absurdly high values
  31. * Since the tls dat aptr is not going to be in range of 0xFFFF_xxxx
  32. * it won't be considered a sys-call error
  33. * and it will be loads better than copy-to-user, which is a definite
  34. * D-TLB Miss
  35. */
  36. SYSCALL_DEFINE0(arc_gettls)
  37. {
  38. return task_thread_info(current)->thr_ptr;
  39. }
  40. static inline void arch_idle(void)
  41. {
  42. /* sleep, but enable all interrupts before committing */
  43. __asm__("sleep 0x3");
  44. }
  45. void cpu_idle(void)
  46. {
  47. /* Since we SLEEP in idle loop, TIF_POLLING_NRFLAG can't be set */
  48. /* endless idle loop with no priority at all */
  49. while (1) {
  50. tick_nohz_idle_enter();
  51. rcu_idle_enter();
  52. doze:
  53. local_irq_disable();
  54. if (!need_resched()) {
  55. arch_idle();
  56. goto doze;
  57. } else {
  58. local_irq_enable();
  59. }
  60. rcu_idle_exit();
  61. tick_nohz_idle_exit();
  62. schedule_preempt_disabled();
  63. }
  64. }
  65. asmlinkage void ret_from_fork(void);
  66. /* Layout of Child kernel mode stack as setup at the end of this function is
  67. *
  68. * | ... |
  69. * | ... |
  70. * | unused |
  71. * | |
  72. * ------------------ <==== top of Stack (thread.ksp)
  73. * | UNUSED 1 word|
  74. * ------------------
  75. * | r25 |
  76. * ~ ~
  77. * | --to-- | (CALLEE Regs of user mode)
  78. * | r13 |
  79. * ------------------
  80. * | fp |
  81. * | blink | @ret_from_fork
  82. * ------------------
  83. * | |
  84. * ~ ~
  85. * ~ ~
  86. * | |
  87. * ------------------
  88. * | r12 |
  89. * ~ ~
  90. * | --to-- | (scratch Regs of user mode)
  91. * | r0 |
  92. * ------------------
  93. * | UNUSED 1 word|
  94. * ------------------ <===== END of PAGE
  95. */
  96. int copy_thread(unsigned long clone_flags,
  97. unsigned long usp, unsigned long arg,
  98. struct task_struct *p)
  99. {
  100. struct pt_regs *c_regs; /* child's pt_regs */
  101. unsigned long *childksp; /* to unwind out of __switch_to() */
  102. struct callee_regs *c_callee; /* child's callee regs */
  103. struct callee_regs *parent_callee; /* paren't callee */
  104. struct pt_regs *regs = current_pt_regs();
  105. /* Mark the specific anchors to begin with (see pic above) */
  106. c_regs = task_pt_regs(p);
  107. childksp = (unsigned long *)c_regs - 2; /* 2 words for FP/BLINK */
  108. c_callee = ((struct callee_regs *)childksp) - 1;
  109. /*
  110. * __switch_to() uses thread.ksp to start unwinding stack
  111. * For kernel threads we don't need to create callee regs, the
  112. * stack layout nevertheless needs to remain the same.
  113. * Also, since __switch_to anyways unwinds callee regs, we use
  114. * this to populate kernel thread entry-pt/args into callee regs,
  115. * so that ret_from_kernel_thread() becomes simpler.
  116. */
  117. p->thread.ksp = (unsigned long)c_callee; /* THREAD_KSP */
  118. /* __switch_to expects FP(0), BLINK(return addr) at top */
  119. childksp[0] = 0; /* fp */
  120. childksp[1] = (unsigned long)ret_from_fork; /* blink */
  121. if (unlikely(p->flags & PF_KTHREAD)) {
  122. memset(c_regs, 0, sizeof(struct pt_regs));
  123. c_callee->r13 = arg; /* argument to kernel thread */
  124. c_callee->r14 = usp; /* function */
  125. return 0;
  126. }
  127. /*--------- User Task Only --------------*/
  128. /* __switch_to expects FP(0), BLINK(return addr) at top of stack */
  129. childksp[0] = 0; /* for POP fp */
  130. childksp[1] = (unsigned long)ret_from_fork; /* for POP blink */
  131. /* Copy parents pt regs on child's kernel mode stack */
  132. *c_regs = *regs;
  133. if (usp)
  134. c_regs->sp = usp;
  135. c_regs->r0 = 0; /* fork returns 0 in child */
  136. parent_callee = ((struct callee_regs *)regs) - 1;
  137. *c_callee = *parent_callee;
  138. if (unlikely(clone_flags & CLONE_SETTLS)) {
  139. /*
  140. * set task's userland tls data ptr from 4th arg
  141. * clone C-lib call is difft from clone sys-call
  142. */
  143. task_thread_info(p)->thr_ptr = regs->r3;
  144. } else {
  145. /* Normal fork case: set parent's TLS ptr in child */
  146. task_thread_info(p)->thr_ptr =
  147. task_thread_info(current)->thr_ptr;
  148. }
  149. return 0;
  150. }
  151. /*
  152. * Some archs flush debug and FPU info here
  153. */
  154. void flush_thread(void)
  155. {
  156. }
  157. /*
  158. * Free any architecture-specific thread data structures, etc.
  159. */
  160. void exit_thread(void)
  161. {
  162. }
  163. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  164. {
  165. return 0;
  166. }
  167. /*
  168. * API: expected by schedular Code: If thread is sleeping where is that.
  169. * What is this good for? it will be always the scheduler or ret_from_fork.
  170. * So we hard code that anyways.
  171. */
  172. unsigned long thread_saved_pc(struct task_struct *t)
  173. {
  174. struct pt_regs *regs = task_pt_regs(t);
  175. unsigned long blink = 0;
  176. /*
  177. * If the thread being queried for in not itself calling this, then it
  178. * implies it is not executing, which in turn implies it is sleeping,
  179. * which in turn implies it got switched OUT by the schedular.
  180. * In that case, it's kernel mode blink can reliably retrieved as per
  181. * the picture above (right above pt_regs).
  182. */
  183. if (t != current && t->state != TASK_RUNNING)
  184. blink = *((unsigned int *)regs - 1);
  185. return blink;
  186. }
  187. int elf_check_arch(const struct elf32_hdr *x)
  188. {
  189. unsigned int eflags;
  190. if (x->e_machine != EM_ARCOMPACT)
  191. return 0;
  192. eflags = x->e_flags;
  193. if ((eflags & EF_ARC_OSABI_MSK) < EF_ARC_OSABI_CURRENT) {
  194. pr_err("ABI mismatch - you need newer toolchain\n");
  195. force_sigsegv(SIGSEGV, current);
  196. return 0;
  197. }
  198. return 1;
  199. }
  200. EXPORT_SYMBOL(elf_check_arch);