process.c 8.6 KB

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