process.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Port on Texas Instruments TMS320C6x architecture
  3. *
  4. * Copyright (C) 2004, 2006, 2009, 2010, 2011 Texas Instruments Incorporated
  5. * Author: Aurelien Jacquiot (aurelien.jacquiot@jaluna.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/unistd.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/init_task.h>
  16. #include <linux/tick.h>
  17. #include <linux/mqueue.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/reboot.h>
  20. #include <asm/syscalls.h>
  21. /* hooks for board specific support */
  22. void (*c6x_restart)(void);
  23. void (*c6x_halt)(void);
  24. extern asmlinkage void ret_from_fork(void);
  25. /*
  26. * power off function, if any
  27. */
  28. void (*pm_power_off)(void);
  29. EXPORT_SYMBOL(pm_power_off);
  30. static void c6x_idle(void)
  31. {
  32. unsigned long tmp;
  33. /*
  34. * Put local_irq_enable and idle in same execute packet
  35. * to make them atomic and avoid race to idle with
  36. * interrupts enabled.
  37. */
  38. asm volatile (" mvc .s2 CSR,%0\n"
  39. " or .d2 1,%0,%0\n"
  40. " mvc .s2 %0,CSR\n"
  41. "|| idle\n"
  42. : "=b"(tmp));
  43. }
  44. /*
  45. * The idle loop for C64x
  46. */
  47. void cpu_idle(void)
  48. {
  49. /* endless idle loop with no priority at all */
  50. while (1) {
  51. tick_nohz_idle_enter();
  52. rcu_idle_enter();
  53. while (1) {
  54. local_irq_disable();
  55. if (need_resched()) {
  56. local_irq_enable();
  57. break;
  58. }
  59. c6x_idle(); /* enables local irqs */
  60. }
  61. rcu_idle_exit();
  62. tick_nohz_idle_exit();
  63. preempt_enable_no_resched();
  64. schedule();
  65. preempt_disable();
  66. }
  67. }
  68. static void halt_loop(void)
  69. {
  70. printk(KERN_EMERG "System Halted, OK to turn off power\n");
  71. local_irq_disable();
  72. while (1)
  73. asm volatile("idle\n");
  74. }
  75. void machine_restart(char *__unused)
  76. {
  77. if (c6x_restart)
  78. c6x_restart();
  79. halt_loop();
  80. }
  81. void machine_halt(void)
  82. {
  83. if (c6x_halt)
  84. c6x_halt();
  85. halt_loop();
  86. }
  87. void machine_power_off(void)
  88. {
  89. if (pm_power_off)
  90. pm_power_off();
  91. halt_loop();
  92. }
  93. static void kernel_thread_helper(int dummy, void *arg, int (*fn)(void *))
  94. {
  95. do_exit(fn(arg));
  96. }
  97. /*
  98. * Create a kernel thread
  99. */
  100. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  101. {
  102. struct pt_regs regs;
  103. /*
  104. * copy_thread sets a4 to zero (child return from fork)
  105. * so we can't just set things up to directly return to
  106. * fn.
  107. */
  108. memset(&regs, 0, sizeof(regs));
  109. regs.b4 = (unsigned long) arg;
  110. regs.a6 = (unsigned long) fn;
  111. regs.pc = (unsigned long) kernel_thread_helper;
  112. local_save_flags(regs.csr);
  113. regs.csr |= 1;
  114. regs.tsr = 5; /* Set GEE and GIE in TSR */
  115. /* Ok, create the new process.. */
  116. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, -1, &regs,
  117. 0, NULL, NULL);
  118. }
  119. EXPORT_SYMBOL(kernel_thread);
  120. void flush_thread(void)
  121. {
  122. }
  123. void exit_thread(void)
  124. {
  125. }
  126. SYSCALL_DEFINE1(c6x_clone, struct pt_regs *, regs)
  127. {
  128. unsigned long clone_flags;
  129. unsigned long newsp;
  130. /* syscall puts clone_flags in A4 and usp in B4 */
  131. clone_flags = regs->orig_a4;
  132. if (regs->b4)
  133. newsp = regs->b4;
  134. else
  135. newsp = regs->sp;
  136. return do_fork(clone_flags, newsp, regs, 0, (int __user *)regs->a6,
  137. (int __user *)regs->b6);
  138. }
  139. /*
  140. * Do necessary setup to start up a newly executed thread.
  141. */
  142. void start_thread(struct pt_regs *regs, unsigned int pc, unsigned long usp)
  143. {
  144. /*
  145. * The binfmt loader will setup a "full" stack, but the C6X
  146. * operates an "empty" stack. So we adjust the usp so that
  147. * argc doesn't get destroyed if an interrupt is taken before
  148. * it is read from the stack.
  149. *
  150. * NB: Library startup code needs to match this.
  151. */
  152. usp -= 8;
  153. set_fs(USER_DS);
  154. regs->pc = pc;
  155. regs->sp = usp;
  156. regs->tsr |= 0x40; /* set user mode */
  157. current->thread.usp = usp;
  158. }
  159. /*
  160. * Copy a new thread context in its stack.
  161. */
  162. int copy_thread(unsigned long clone_flags, unsigned long usp,
  163. unsigned long ustk_size,
  164. struct task_struct *p, struct pt_regs *regs)
  165. {
  166. struct pt_regs *childregs;
  167. childregs = task_pt_regs(p);
  168. *childregs = *regs;
  169. childregs->a4 = 0;
  170. if (usp == -1)
  171. /* case of __kernel_thread: we return to supervisor space */
  172. childregs->sp = (unsigned long)(childregs + 1);
  173. else
  174. /* Otherwise use the given stack */
  175. childregs->sp = usp;
  176. /* Set usp/ksp */
  177. p->thread.usp = childregs->sp;
  178. /* switch_to uses stack to save/restore 14 callee-saved regs */
  179. thread_saved_ksp(p) = (unsigned long)childregs - 8;
  180. p->thread.pc = (unsigned int) ret_from_fork;
  181. p->thread.wchan = (unsigned long) ret_from_fork;
  182. #ifdef __DSBT__
  183. {
  184. unsigned long dp;
  185. asm volatile ("mv .S2 b14,%0\n" : "=b"(dp));
  186. thread_saved_dp(p) = dp;
  187. if (usp == -1)
  188. childregs->dp = dp;
  189. }
  190. #endif
  191. return 0;
  192. }
  193. /*
  194. * c6x_execve() executes a new program.
  195. */
  196. SYSCALL_DEFINE4(c6x_execve, const char __user *, name,
  197. const char __user *const __user *, argv,
  198. const char __user *const __user *, envp,
  199. struct pt_regs *, regs)
  200. {
  201. int error;
  202. char *filename;
  203. filename = getname(name);
  204. error = PTR_ERR(filename);
  205. if (IS_ERR(filename))
  206. goto out;
  207. error = do_execve(filename, argv, envp, regs);
  208. putname(filename);
  209. out:
  210. return error;
  211. }
  212. unsigned long get_wchan(struct task_struct *p)
  213. {
  214. return p->thread.wchan;
  215. }