process.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * linux/arch/m68k/kernel/process.c
  3. *
  4. * Copyright (C) 1995 Hamish Macdonald
  5. *
  6. * 68060 fixes by Jesper Skov
  7. */
  8. /*
  9. * This file handles the architecture-dependent parts of process handling..
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/smp.h>
  19. #include <linux/stddef.h>
  20. #include <linux/unistd.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/user.h>
  23. #include <linux/reboot.h>
  24. #include <linux/init_task.h>
  25. #include <linux/mqueue.h>
  26. #include <linux/rcupdate.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/traps.h>
  29. #include <asm/machdep.h>
  30. #include <asm/setup.h>
  31. #include <asm/pgtable.h>
  32. asmlinkage void ret_from_fork(void);
  33. /*
  34. * Return saved PC from a blocked thread
  35. */
  36. unsigned long thread_saved_pc(struct task_struct *tsk)
  37. {
  38. struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
  39. /* Check whether the thread is blocked in resume() */
  40. if (in_sched_functions(sw->retpc))
  41. return ((unsigned long *)sw->a6)[1];
  42. else
  43. return sw->retpc;
  44. }
  45. /*
  46. * The idle loop on an m68k..
  47. */
  48. static void default_idle(void)
  49. {
  50. if (!need_resched())
  51. #if defined(MACH_ATARI_ONLY)
  52. /* block out HSYNC on the atari (falcon) */
  53. __asm__("stop #0x2200" : : : "cc");
  54. #else
  55. __asm__("stop #0x2000" : : : "cc");
  56. #endif
  57. }
  58. void (*idle)(void) = default_idle;
  59. /*
  60. * The idle thread. There's no useful work to be
  61. * done, so just try to conserve power and have a
  62. * low exit latency (ie sit in a loop waiting for
  63. * somebody to say that they'd like to reschedule)
  64. */
  65. void cpu_idle(void)
  66. {
  67. /* endless idle loop with no priority at all */
  68. while (1) {
  69. rcu_idle_enter();
  70. while (!need_resched())
  71. idle();
  72. rcu_idle_exit();
  73. schedule_preempt_disabled();
  74. }
  75. }
  76. void machine_restart(char * __unused)
  77. {
  78. if (mach_reset)
  79. mach_reset();
  80. for (;;);
  81. }
  82. void machine_halt(void)
  83. {
  84. if (mach_halt)
  85. mach_halt();
  86. for (;;);
  87. }
  88. void machine_power_off(void)
  89. {
  90. if (mach_power_off)
  91. mach_power_off();
  92. for (;;);
  93. }
  94. void (*pm_power_off)(void) = machine_power_off;
  95. EXPORT_SYMBOL(pm_power_off);
  96. void show_regs(struct pt_regs * regs)
  97. {
  98. printk("\n");
  99. printk("Format %02x Vector: %04x PC: %08lx Status: %04x %s\n",
  100. regs->format, regs->vector, regs->pc, regs->sr, print_tainted());
  101. printk("ORIG_D0: %08lx D0: %08lx A2: %08lx A1: %08lx\n",
  102. regs->orig_d0, regs->d0, regs->a2, regs->a1);
  103. printk("A0: %08lx D5: %08lx D4: %08lx\n",
  104. regs->a0, regs->d5, regs->d4);
  105. printk("D3: %08lx D2: %08lx D1: %08lx\n",
  106. regs->d3, regs->d2, regs->d1);
  107. if (!(regs->sr & PS_S))
  108. printk("USP: %08lx\n", rdusp());
  109. }
  110. /*
  111. * Create a kernel thread
  112. */
  113. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  114. {
  115. int pid;
  116. mm_segment_t fs;
  117. fs = get_fs();
  118. set_fs (KERNEL_DS);
  119. {
  120. register long retval __asm__ ("d0");
  121. register long clone_arg __asm__ ("d1") = flags | CLONE_VM | CLONE_UNTRACED;
  122. retval = __NR_clone;
  123. __asm__ __volatile__
  124. ("clrl %%d2\n\t"
  125. "trap #0\n\t" /* Linux/m68k system call */
  126. "tstl %0\n\t" /* child or parent */
  127. "jne 1f\n\t" /* parent - jump */
  128. #ifdef CONFIG_MMU
  129. "lea %%sp@(%c7),%6\n\t" /* reload current */
  130. "movel %6@,%6\n\t"
  131. #endif
  132. "movel %3,%%sp@-\n\t" /* push argument */
  133. "jsr %4@\n\t" /* call fn */
  134. "movel %0,%%d1\n\t" /* pass exit value */
  135. "movel %2,%%d0\n\t" /* exit */
  136. "trap #0\n"
  137. "1:"
  138. : "+d" (retval)
  139. : "i" (__NR_clone), "i" (__NR_exit),
  140. "r" (arg), "a" (fn), "d" (clone_arg), "r" (current),
  141. "i" (-THREAD_SIZE)
  142. : "d2");
  143. pid = retval;
  144. }
  145. set_fs (fs);
  146. return pid;
  147. }
  148. EXPORT_SYMBOL(kernel_thread);
  149. void flush_thread(void)
  150. {
  151. current->thread.fs = __USER_DS;
  152. #ifdef CONFIG_FPU
  153. if (!FPU_IS_EMU) {
  154. unsigned long zero = 0;
  155. asm volatile("frestore %0": :"m" (zero));
  156. }
  157. #endif
  158. }
  159. /*
  160. * "m68k_fork()".. By the time we get here, the
  161. * non-volatile registers have also been saved on the
  162. * stack. We do some ugly pointer stuff here.. (see
  163. * also copy_thread)
  164. */
  165. asmlinkage int m68k_fork(struct pt_regs *regs)
  166. {
  167. #ifdef CONFIG_MMU
  168. return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
  169. #else
  170. return -EINVAL;
  171. #endif
  172. }
  173. asmlinkage int m68k_vfork(struct pt_regs *regs)
  174. {
  175. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0,
  176. NULL, NULL);
  177. }
  178. asmlinkage int m68k_clone(struct pt_regs *regs)
  179. {
  180. unsigned long clone_flags;
  181. unsigned long newsp;
  182. int __user *parent_tidptr, *child_tidptr;
  183. /* syscall2 puts clone_flags in d1 and usp in d2 */
  184. clone_flags = regs->d1;
  185. newsp = regs->d2;
  186. parent_tidptr = (int __user *)regs->d3;
  187. child_tidptr = (int __user *)regs->d4;
  188. if (!newsp)
  189. newsp = rdusp();
  190. return do_fork(clone_flags, newsp, regs, 0,
  191. parent_tidptr, child_tidptr);
  192. }
  193. int copy_thread(unsigned long clone_flags, unsigned long usp,
  194. unsigned long unused,
  195. struct task_struct * p, struct pt_regs * regs)
  196. {
  197. struct pt_regs * childregs;
  198. struct switch_stack * childstack, *stack;
  199. unsigned long *retp;
  200. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  201. *childregs = *regs;
  202. childregs->d0 = 0;
  203. retp = ((unsigned long *) regs);
  204. stack = ((struct switch_stack *) retp) - 1;
  205. childstack = ((struct switch_stack *) childregs) - 1;
  206. *childstack = *stack;
  207. childstack->retpc = (unsigned long)ret_from_fork;
  208. p->thread.usp = usp;
  209. p->thread.ksp = (unsigned long)childstack;
  210. if (clone_flags & CLONE_SETTLS)
  211. task_thread_info(p)->tp_value = regs->d5;
  212. /*
  213. * Must save the current SFC/DFC value, NOT the value when
  214. * the parent was last descheduled - RGH 10-08-96
  215. */
  216. p->thread.fs = get_fs().seg;
  217. #ifdef CONFIG_FPU
  218. if (!FPU_IS_EMU) {
  219. /* Copy the current fpu state */
  220. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  221. if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2]) {
  222. if (CPU_IS_COLDFIRE) {
  223. asm volatile ("fmovemd %/fp0-%/fp7,%0\n\t"
  224. "fmovel %/fpiar,%1\n\t"
  225. "fmovel %/fpcr,%2\n\t"
  226. "fmovel %/fpsr,%3"
  227. :
  228. : "m" (p->thread.fp[0]),
  229. "m" (p->thread.fpcntl[0]),
  230. "m" (p->thread.fpcntl[1]),
  231. "m" (p->thread.fpcntl[2])
  232. : "memory");
  233. } else {
  234. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  235. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  236. :
  237. : "m" (p->thread.fp[0]),
  238. "m" (p->thread.fpcntl[0])
  239. : "memory");
  240. }
  241. }
  242. /* Restore the state in case the fpu was busy */
  243. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  244. }
  245. #endif /* CONFIG_FPU */
  246. return 0;
  247. }
  248. /* Fill in the fpu structure for a core dump. */
  249. #ifdef CONFIG_FPU
  250. int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  251. {
  252. char fpustate[216];
  253. if (FPU_IS_EMU) {
  254. int i;
  255. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  256. memcpy(fpu->fpregs, current->thread.fp, 96);
  257. /* Convert internal fpu reg representation
  258. * into long double format
  259. */
  260. for (i = 0; i < 24; i += 3)
  261. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  262. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  263. return 1;
  264. }
  265. /* First dump the fpu context to avoid protocol violation. */
  266. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  267. if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
  268. return 0;
  269. if (CPU_IS_COLDFIRE) {
  270. asm volatile ("fmovel %/fpiar,%0\n\t"
  271. "fmovel %/fpcr,%1\n\t"
  272. "fmovel %/fpsr,%2\n\t"
  273. "fmovemd %/fp0-%/fp7,%3"
  274. :
  275. : "m" (fpu->fpcntl[0]),
  276. "m" (fpu->fpcntl[1]),
  277. "m" (fpu->fpcntl[2]),
  278. "m" (fpu->fpregs[0])
  279. : "memory");
  280. } else {
  281. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  282. :
  283. : "m" (fpu->fpcntl[0])
  284. : "memory");
  285. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  286. :
  287. : "m" (fpu->fpregs[0])
  288. : "memory");
  289. }
  290. return 1;
  291. }
  292. EXPORT_SYMBOL(dump_fpu);
  293. #endif /* CONFIG_FPU */
  294. /*
  295. * sys_execve() executes a new program.
  296. */
  297. asmlinkage int sys_execve(const char __user *name,
  298. const char __user *const __user *argv,
  299. const char __user *const __user *envp)
  300. {
  301. int error;
  302. char * filename;
  303. struct pt_regs *regs = (struct pt_regs *) &name;
  304. filename = getname(name);
  305. error = PTR_ERR(filename);
  306. if (IS_ERR(filename))
  307. return error;
  308. error = do_execve(filename, argv, envp, regs);
  309. putname(filename);
  310. return error;
  311. }
  312. unsigned long get_wchan(struct task_struct *p)
  313. {
  314. unsigned long fp, pc;
  315. unsigned long stack_page;
  316. int count = 0;
  317. if (!p || p == current || p->state == TASK_RUNNING)
  318. return 0;
  319. stack_page = (unsigned long)task_stack_page(p);
  320. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  321. do {
  322. if (fp < stack_page+sizeof(struct thread_info) ||
  323. fp >= 8184+stack_page)
  324. return 0;
  325. pc = ((unsigned long *)fp)[1];
  326. if (!in_sched_functions(pc))
  327. return pc;
  328. fp = *(unsigned long *) fp;
  329. } while (count++ < 16);
  330. return 0;
  331. }