process.c 5.4 KB

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