process.c 6.2 KB

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