process.c 7.4 KB

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