process.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. int uval = -EFAULT;
  46. /*
  47. * This is only for old cores lacking LLOCK/SCOND, which by defintion
  48. * can't possibly be SMP. Thus doesn't need to be SMP safe.
  49. * And this also helps reduce the overhead for serializing in
  50. * the UP case
  51. */
  52. WARN_ON_ONCE(IS_ENABLED(CONFIG_SMP));
  53. /* Z indicates to userspace if operation succeded */
  54. regs->status32 &= ~STATUS_Z_MASK;
  55. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
  56. return -EFAULT;
  57. preempt_disable();
  58. if (__get_user(uval, uaddr))
  59. goto done;
  60. if (uval == expected) {
  61. if (!__put_user(new, uaddr))
  62. regs->status32 |= STATUS_Z_MASK;
  63. }
  64. done:
  65. preempt_enable();
  66. return uval;
  67. }
  68. #ifdef CONFIG_ISA_ARCV2
  69. void arch_cpu_idle(void)
  70. {
  71. /* Re-enable interrupts <= default irq priority before commiting SLEEP */
  72. const unsigned int arg = 0x10 | ARCV2_IRQ_DEF_PRIO;
  73. __asm__ __volatile__(
  74. "sleep %0 \n"
  75. :
  76. :"I"(arg)); /* can't be "r" has to be embedded const */
  77. }
  78. #elif defined(CONFIG_EZNPS_MTM_EXT) /* ARC700 variant in NPS */
  79. void arch_cpu_idle(void)
  80. {
  81. /* only the calling HW thread needs to sleep */
  82. __asm__ __volatile__(
  83. ".word %0 \n"
  84. :
  85. :"i"(CTOP_INST_HWSCHD_WFT_IE12));
  86. }
  87. #else /* ARC700 */
  88. void arch_cpu_idle(void)
  89. {
  90. /* sleep, but enable both set E1/E2 (levels of interrutps) before committing */
  91. __asm__ __volatile__("sleep 0x3 \n");
  92. }
  93. #endif
  94. asmlinkage void ret_from_fork(void);
  95. /*
  96. * Copy architecture-specific thread state
  97. *
  98. * Layout of Child kernel mode stack as setup at the end of this function is
  99. *
  100. * | ... |
  101. * | ... |
  102. * | unused |
  103. * | |
  104. * ------------------
  105. * | r25 | <==== top of Stack (thread.ksp)
  106. * ~ ~
  107. * | --to-- | (CALLEE Regs of kernel mode)
  108. * | r13 |
  109. * ------------------
  110. * | fp |
  111. * | blink | @ret_from_fork
  112. * ------------------
  113. * | |
  114. * ~ ~
  115. * ~ ~
  116. * | |
  117. * ------------------
  118. * | r12 |
  119. * ~ ~
  120. * | --to-- | (scratch Regs of user mode)
  121. * | r0 |
  122. * ------------------
  123. * | SP |
  124. * | orig_r0 |
  125. * | event/ECR |
  126. * | user_r25 |
  127. * ------------------ <===== END of PAGE
  128. */
  129. int copy_thread(unsigned long clone_flags,
  130. unsigned long usp, unsigned long kthread_arg,
  131. struct task_struct *p)
  132. {
  133. struct pt_regs *c_regs; /* child's pt_regs */
  134. unsigned long *childksp; /* to unwind out of __switch_to() */
  135. struct callee_regs *c_callee; /* child's callee regs */
  136. struct callee_regs *parent_callee; /* paren't callee */
  137. struct pt_regs *regs = current_pt_regs();
  138. /* Mark the specific anchors to begin with (see pic above) */
  139. c_regs = task_pt_regs(p);
  140. childksp = (unsigned long *)c_regs - 2; /* 2 words for FP/BLINK */
  141. c_callee = ((struct callee_regs *)childksp) - 1;
  142. /*
  143. * __switch_to() uses thread.ksp to start unwinding stack
  144. * For kernel threads we don't need to create callee regs, the
  145. * stack layout nevertheless needs to remain the same.
  146. * Also, since __switch_to anyways unwinds callee regs, we use
  147. * this to populate kernel thread entry-pt/args into callee regs,
  148. * so that ret_from_kernel_thread() becomes simpler.
  149. */
  150. p->thread.ksp = (unsigned long)c_callee; /* THREAD_KSP */
  151. /* __switch_to expects FP(0), BLINK(return addr) at top */
  152. childksp[0] = 0; /* fp */
  153. childksp[1] = (unsigned long)ret_from_fork; /* blink */
  154. if (unlikely(p->flags & PF_KTHREAD)) {
  155. memset(c_regs, 0, sizeof(struct pt_regs));
  156. c_callee->r13 = kthread_arg;
  157. c_callee->r14 = usp; /* function */
  158. return 0;
  159. }
  160. /*--------- User Task Only --------------*/
  161. /* __switch_to expects FP(0), BLINK(return addr) at top of stack */
  162. childksp[0] = 0; /* for POP fp */
  163. childksp[1] = (unsigned long)ret_from_fork; /* for POP blink */
  164. /* Copy parents pt regs on child's kernel mode stack */
  165. *c_regs = *regs;
  166. if (usp)
  167. c_regs->sp = usp;
  168. c_regs->r0 = 0; /* fork returns 0 in child */
  169. parent_callee = ((struct callee_regs *)regs) - 1;
  170. *c_callee = *parent_callee;
  171. if (unlikely(clone_flags & CLONE_SETTLS)) {
  172. /*
  173. * set task's userland tls data ptr from 4th arg
  174. * clone C-lib call is difft from clone sys-call
  175. */
  176. task_thread_info(p)->thr_ptr = regs->r3;
  177. } else {
  178. /* Normal fork case: set parent's TLS ptr in child */
  179. task_thread_info(p)->thr_ptr =
  180. task_thread_info(current)->thr_ptr;
  181. }
  182. return 0;
  183. }
  184. /*
  185. * Do necessary setup to start up a new user task
  186. */
  187. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long usp)
  188. {
  189. regs->sp = usp;
  190. regs->ret = pc;
  191. /*
  192. * [U]ser Mode bit set
  193. * [L] ZOL loop inhibited to begin with - cleared by a LP insn
  194. * Interrupts enabled
  195. */
  196. regs->status32 = STATUS_U_MASK | STATUS_L_MASK | ISA_INIT_STATUS_BITS;
  197. #ifdef CONFIG_EZNPS_MTM_EXT
  198. regs->eflags = 0;
  199. #endif
  200. /* bogus seed values for debugging */
  201. regs->lp_start = 0x10;
  202. regs->lp_end = 0x80;
  203. }
  204. /*
  205. * Some archs flush debug and FPU info here
  206. */
  207. void flush_thread(void)
  208. {
  209. }
  210. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  211. {
  212. return 0;
  213. }
  214. int elf_check_arch(const struct elf32_hdr *x)
  215. {
  216. unsigned int eflags;
  217. if (x->e_machine != EM_ARC_INUSE) {
  218. pr_err("ELF not built for %s ISA\n",
  219. is_isa_arcompact() ? "ARCompact":"ARCv2");
  220. return 0;
  221. }
  222. eflags = x->e_flags;
  223. if ((eflags & EF_ARC_OSABI_MSK) != EF_ARC_OSABI_CURRENT) {
  224. pr_err("ABI mismatch - you need newer toolchain\n");
  225. force_sigsegv(SIGSEGV, current);
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. EXPORT_SYMBOL(elf_check_arch);