process.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 definition
  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 succeeded */
  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 committing 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. raw_local_irq_disable();
  95. }
  96. #else /* ARC700 */
  97. void arch_cpu_idle(void)
  98. {
  99. /* sleep, but enable both set E1/E2 (levels of interrupts) before committing */
  100. __asm__ __volatile__("sleep 0x3 \n");
  101. raw_local_irq_disable();
  102. }
  103. #endif
  104. asmlinkage void ret_from_fork(void);
  105. /*
  106. * Copy architecture-specific thread state
  107. *
  108. * Layout of Child kernel mode stack as setup at the end of this function is
  109. *
  110. * | ... |
  111. * | ... |
  112. * | unused |
  113. * | |
  114. * ------------------
  115. * | r25 | <==== top of Stack (thread_info.ksp)
  116. * ~ ~
  117. * | --to-- | (CALLEE Regs of kernel mode)
  118. * | r13 |
  119. * ------------------
  120. * | fp |
  121. * | blink | @ret_from_fork
  122. * ------------------
  123. * | |
  124. * ~ ~
  125. * ~ ~
  126. * | |
  127. * ------------------
  128. * | r12 |
  129. * ~ ~
  130. * | --to-- | (scratch Regs of user mode)
  131. * | r0 |
  132. * ------------------
  133. * | SP |
  134. * | orig_r0 |
  135. * | event/ECR |
  136. * ------------------ <===== END of PAGE
  137. */
  138. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  139. {
  140. unsigned long clone_flags = args->flags;
  141. unsigned long usp = args->stack;
  142. unsigned long tls = args->tls;
  143. struct pt_regs *c_regs; /* child's pt_regs */
  144. unsigned long *childksp; /* to unwind out of __switch_to() */
  145. struct callee_regs *c_callee; /* child's callee regs */
  146. struct callee_regs *parent_callee; /* paren't callee */
  147. struct pt_regs *regs = current_pt_regs();
  148. /* Mark the specific anchors to begin with (see pic above) */
  149. c_regs = task_pt_regs(p);
  150. childksp = (unsigned long *)c_regs - 2; /* 2 words for FP/BLINK */
  151. c_callee = ((struct callee_regs *)childksp) - 1;
  152. /*
  153. * __switch_to() uses thread_info.ksp to start unwinding stack
  154. * For kernel threads we don't need to create callee regs, the
  155. * stack layout nevertheless needs to remain the same.
  156. * Also, since __switch_to anyways unwinds callee regs, we use
  157. * this to populate kernel thread entry-pt/args into callee regs,
  158. * so that ret_from_kernel_thread() becomes simpler.
  159. */
  160. task_thread_info(p)->ksp = (unsigned long)c_callee; /* THREAD_INFO_KSP */
  161. /* __switch_to expects FP(0), BLINK(return addr) at top */
  162. childksp[0] = 0; /* fp */
  163. childksp[1] = (unsigned long)ret_from_fork; /* blink */
  164. if (unlikely(args->fn)) {
  165. memset(c_regs, 0, sizeof(struct pt_regs));
  166. c_callee->r13 = (unsigned long)args->fn_arg;
  167. c_callee->r14 = (unsigned long)args->fn;
  168. return 0;
  169. }
  170. /*--------- User Task Only --------------*/
  171. /* __switch_to expects FP(0), BLINK(return addr) at top of stack */
  172. childksp[0] = 0; /* for POP fp */
  173. childksp[1] = (unsigned long)ret_from_fork; /* for POP blink */
  174. /* Copy parents pt regs on child's kernel mode stack */
  175. *c_regs = *regs;
  176. if (usp)
  177. c_regs->sp = usp;
  178. c_regs->r0 = 0; /* fork returns 0 in child */
  179. parent_callee = ((struct callee_regs *)regs) - 1;
  180. *c_callee = *parent_callee;
  181. if (unlikely(clone_flags & CLONE_SETTLS)) {
  182. /*
  183. * set task's userland tls data ptr from 4th arg
  184. * clone C-lib call is difft from clone sys-call
  185. */
  186. task_thread_info(p)->thr_ptr = tls;
  187. } else {
  188. /* Normal fork case: set parent's TLS ptr in child */
  189. task_thread_info(p)->thr_ptr =
  190. task_thread_info(current)->thr_ptr;
  191. }
  192. /*
  193. * setup usermode thread pointer #1:
  194. * when child is picked by scheduler, __switch_to() uses @c_callee to
  195. * populate usermode callee regs: this works (despite being in a kernel
  196. * function) since special return path for child @ret_from_fork()
  197. * ensures those regs are not clobbered all the way to RTIE to usermode
  198. */
  199. c_callee->r25 = task_thread_info(p)->thr_ptr;
  200. return 0;
  201. }
  202. /*
  203. * Do necessary setup to start up a new user task
  204. */
  205. void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long usp)
  206. {
  207. regs->sp = usp;
  208. regs->ret = pc;
  209. /*
  210. * [U]ser Mode bit set
  211. * [L] ZOL loop inhibited to begin with - cleared by a LP insn
  212. * Interrupts enabled
  213. */
  214. regs->status32 = STATUS_U_MASK | STATUS_L_MASK | ISA_INIT_STATUS_BITS;
  215. fpu_init_task(regs);
  216. /* bogus seed values for debugging */
  217. regs->lp_start = 0x10;
  218. regs->lp_end = 0x80;
  219. }
  220. /*
  221. * Some archs flush debug and FPU info here
  222. */
  223. void flush_thread(void)
  224. {
  225. }
  226. int elf_check_arch(const struct elf32_hdr *x)
  227. {
  228. unsigned int eflags;
  229. if (x->e_machine != EM_ARC_INUSE) {
  230. pr_err("ELF not built for %s ISA\n",
  231. is_isa_arcompact() ? "ARCompact":"ARCv2");
  232. return 0;
  233. }
  234. eflags = x->e_flags;
  235. if ((eflags & EF_ARC_OSABI_MSK) != EF_ARC_OSABI_CURRENT) {
  236. pr_err("ABI mismatch - you need newer toolchain\n");
  237. force_fatal_sig(SIGSEGV);
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. EXPORT_SYMBOL(elf_check_arch);