process.c 5.4 KB

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