process.c 8.6 KB

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