process.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/sched/task.h>
  14. #include <linux/sched/task_stack.h>
  15. #include <linux/mm.h>
  16. #include <linux/fs.h>
  17. #include <linux/unistd.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/slab.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/elf.h>
  22. #include <linux/tick.h>
  23. SYSCALL_DEFINE1(arc_settls, void *, user_tls_data_ptr)
  24. {
  25. task_thread_info(current)->thr_ptr = (unsigned int)user_tls_data_ptr;
  26. return 0;
  27. }
  28. /*
  29. * We return the user space TLS data ptr as sys-call return code
  30. * Ideally it should be copy to user.
  31. * However we can cheat by the fact that some sys-calls do return
  32. * absurdly high values
  33. * Since the tls dat aptr is not going to be in range of 0xFFFF_xxxx
  34. * it won't be considered a sys-call error
  35. * and it will be loads better than copy-to-user, which is a definite
  36. * D-TLB Miss
  37. */
  38. SYSCALL_DEFINE0(arc_gettls)
  39. {
  40. return task_thread_info(current)->thr_ptr;
  41. }
  42. SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)
  43. {
  44. struct pt_regs *regs = current_pt_regs();
  45. u32 uval;
  46. int ret;
  47. /*
  48. * This is only for old cores lacking LLOCK/SCOND, which by defintion
  49. * can't possibly be SMP. Thus doesn't need to be SMP safe.
  50. * And this also helps reduce the overhead for serializing in
  51. * the UP case
  52. */
  53. WARN_ON_ONCE(IS_ENABLED(CONFIG_SMP));
  54. /* Z indicates to userspace if operation succeded */
  55. regs->status32 &= ~STATUS_Z_MASK;
  56. ret = access_ok(VERIFY_WRITE, uaddr, sizeof(*uaddr));
  57. if (!ret)
  58. goto fail;
  59. again:
  60. preempt_disable();
  61. ret = __get_user(uval, uaddr);
  62. if (ret)
  63. goto fault;
  64. if (uval != expected)
  65. goto out;
  66. ret = __put_user(new, uaddr);
  67. if (ret)
  68. goto fault;
  69. regs->status32 |= STATUS_Z_MASK;
  70. out:
  71. preempt_enable();
  72. return uval;
  73. fault:
  74. preempt_enable();
  75. if (unlikely(ret != -EFAULT))
  76. goto fail;
  77. down_read(&current->mm->mmap_sem);
  78. ret = fixup_user_fault(current, current->mm, (unsigned long) uaddr,
  79. FAULT_FLAG_WRITE, NULL);
  80. up_read(&current->mm->mmap_sem);
  81. if (likely(!ret))
  82. goto again;
  83. fail:
  84. force_sig(SIGSEGV, current);
  85. return ret;
  86. }
  87. #ifdef CONFIG_ISA_ARCV2
  88. void arch_cpu_idle(void)
  89. {
  90. /* Re-enable interrupts <= default irq priority before commiting SLEEP */
  91. const unsigned int arg = 0x10 | ARCV2_IRQ_DEF_PRIO;
  92. __asm__ __volatile__(
  93. "sleep %0 \n"
  94. :
  95. :"I"(arg)); /* can't be "r" has to be embedded const */
  96. }
  97. #elif defined(CONFIG_EZNPS_MTM_EXT) /* ARC700 variant in NPS */
  98. void arch_cpu_idle(void)
  99. {
  100. /* only the calling HW thread needs to sleep */
  101. __asm__ __volatile__(
  102. ".word %0 \n"
  103. :
  104. :"i"(CTOP_INST_HWSCHD_WFT_IE12));
  105. }
  106. #else /* ARC700 */
  107. void arch_cpu_idle(void)
  108. {
  109. /* sleep, but enable both set E1/E2 (levels of interrutps) before committing */
  110. __asm__ __volatile__("sleep 0x3 \n");
  111. }
  112. #endif
  113. asmlinkage void ret_from_fork(void);
  114. /*
  115. * Copy architecture-specific thread state
  116. *
  117. * Layout of Child kernel mode stack as setup at the end of this function is
  118. *
  119. * | ... |
  120. * | ... |
  121. * | unused |
  122. * | |
  123. * ------------------
  124. * | r25 | <==== top of Stack (thread.ksp)
  125. * ~ ~
  126. * | --to-- | (CALLEE Regs of kernel mode)
  127. * | r13 |
  128. * ------------------
  129. * | fp |
  130. * | blink | @ret_from_fork
  131. * ------------------
  132. * | |
  133. * ~ ~
  134. * ~ ~
  135. * | |
  136. * ------------------
  137. * | r12 |
  138. * ~ ~
  139. * | --to-- | (scratch Regs of user mode)
  140. * | r0 |
  141. * ------------------
  142. * | SP |
  143. * | orig_r0 |
  144. * | event/ECR |
  145. * | user_r25 |
  146. * ------------------ <===== END of PAGE
  147. */
  148. int copy_thread(unsigned long clone_flags,
  149. unsigned long usp, unsigned long kthread_arg,
  150. struct task_struct *p)
  151. {
  152. struct pt_regs *c_regs; /* child's pt_regs */
  153. unsigned long *childksp; /* to unwind out of __switch_to() */
  154. struct callee_regs *c_callee; /* child's callee regs */
  155. struct callee_regs *parent_callee; /* paren't callee */
  156. struct pt_regs *regs = current_pt_regs();
  157. /* Mark the specific anchors to begin with (see pic above) */
  158. c_regs = task_pt_regs(p);
  159. childksp = (unsigned long *)c_regs - 2; /* 2 words for FP/BLINK */
  160. c_callee = ((struct callee_regs *)childksp) - 1;
  161. /*
  162. * __switch_to() uses thread.ksp to start unwinding stack
  163. * For kernel threads we don't need to create callee regs, the
  164. * stack layout nevertheless needs to remain the same.
  165. * Also, since __switch_to anyways unwinds callee regs, we use
  166. * this to populate kernel thread entry-pt/args into callee regs,
  167. * so that ret_from_kernel_thread() becomes simpler.
  168. */
  169. p->thread.ksp = (unsigned long)c_callee; /* THREAD_KSP */
  170. /* __switch_to expects FP(0), BLINK(return addr) at top */
  171. childksp[0] = 0; /* fp */
  172. childksp[1] = (unsigned long)ret_from_fork; /* blink */
  173. if (unlikely(p->flags & PF_KTHREAD)) {
  174. memset(c_regs, 0, sizeof(struct pt_regs));
  175. c_callee->r13 = kthread_arg;
  176. c_callee->r14 = usp; /* function */
  177. return 0;
  178. }
  179. /*--------- User Task Only --------------*/
  180. /* __switch_to expects FP(0), BLINK(return addr) at top of stack */
  181. childksp[0] = 0; /* for POP fp */
  182. childksp[1] = (unsigned long)ret_from_fork; /* for POP blink */
  183. /* Copy parents pt regs on child's kernel mode stack */
  184. *c_regs = *regs;
  185. if (usp)
  186. c_regs->sp = usp;
  187. c_regs->r0 = 0; /* fork returns 0 in child */
  188. parent_callee = ((struct callee_regs *)regs) - 1;
  189. *c_callee = *parent_callee;
  190. if (unlikely(clone_flags & CLONE_SETTLS)) {
  191. /*
  192. * set task's userland tls data ptr from 4th arg
  193. * clone C-lib call is difft from clone sys-call
  194. */
  195. task_thread_info(p)->thr_ptr = regs->r3;
  196. } else {
  197. /* Normal fork case: set parent's TLS ptr in child */
  198. task_thread_info(p)->thr_ptr =
  199. task_thread_info(current)->thr_ptr;
  200. }
  201. /*
  202. * setup usermode thread pointer #1:
  203. * when child is picked by scheduler, __switch_to() uses @c_callee to
  204. * populate usermode callee regs: this works (despite being in a kernel
  205. * function) since special return path for child @ret_from_fork()
  206. * ensures those regs are not clobbered all the way to RTIE to usermode
  207. */
  208. c_callee->r25 = task_thread_info(p)->thr_ptr;
  209. #ifdef CONFIG_ARC_CURR_IN_REG
  210. /*
  211. * setup usermode thread pointer #2:
  212. * however for this special use of r25 in kernel, __switch_to() sets
  213. * r25 for kernel needs and only in the final return path is usermode
  214. * r25 setup, from pt_regs->user_r25. So set that up as well
  215. */
  216. c_regs->user_r25 = c_callee->r25;
  217. #endif
  218. return 0;
  219. }
  220. /*
  221. * Do necessary setup to start up a new user task
  222. */
  223. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long usp)
  224. {
  225. regs->sp = usp;
  226. regs->ret = pc;
  227. /*
  228. * [U]ser Mode bit set
  229. * [L] ZOL loop inhibited to begin with - cleared by a LP insn
  230. * Interrupts enabled
  231. */
  232. regs->status32 = STATUS_U_MASK | STATUS_L_MASK | ISA_INIT_STATUS_BITS;
  233. #ifdef CONFIG_EZNPS_MTM_EXT
  234. regs->eflags = 0;
  235. #endif
  236. /* bogus seed values for debugging */
  237. regs->lp_start = 0x10;
  238. regs->lp_end = 0x80;
  239. }
  240. /*
  241. * Some archs flush debug and FPU info here
  242. */
  243. void flush_thread(void)
  244. {
  245. }
  246. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  247. {
  248. return 0;
  249. }
  250. int elf_check_arch(const struct elf32_hdr *x)
  251. {
  252. unsigned int eflags;
  253. if (x->e_machine != EM_ARC_INUSE) {
  254. pr_err("ELF not built for %s ISA\n",
  255. is_isa_arcompact() ? "ARCompact":"ARCv2");
  256. return 0;
  257. }
  258. eflags = x->e_flags;
  259. if ((eflags & EF_ARC_OSABI_MSK) != EF_ARC_OSABI_CURRENT) {
  260. pr_err("ABI mismatch - you need newer toolchain\n");
  261. force_sigsegv(SIGSEGV, current);
  262. return 0;
  263. }
  264. return 1;
  265. }
  266. EXPORT_SYMBOL(elf_check_arch);