process.c 5.5 KB

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