process.c 5.4 KB

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