process_mm.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. preempt_enable_no_resched();
  72. schedule();
  73. preempt_disable();
  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. "lea %%sp@(%c7),%6\n\t" /* reload current */
  129. "movel %6@,%6\n\t"
  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. unsigned long zero = 0;
  150. current->thread.fs = __USER_DS;
  151. if (!FPU_IS_EMU)
  152. asm volatile (".chip 68k/68881\n\t"
  153. "frestore %0@\n\t"
  154. ".chip 68k" : : "a" (&zero));
  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. return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
  165. }
  166. asmlinkage int m68k_vfork(struct pt_regs *regs)
  167. {
  168. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0,
  169. NULL, NULL);
  170. }
  171. asmlinkage int m68k_clone(struct pt_regs *regs)
  172. {
  173. unsigned long clone_flags;
  174. unsigned long newsp;
  175. int __user *parent_tidptr, *child_tidptr;
  176. /* syscall2 puts clone_flags in d1 and usp in d2 */
  177. clone_flags = regs->d1;
  178. newsp = regs->d2;
  179. parent_tidptr = (int __user *)regs->d3;
  180. child_tidptr = (int __user *)regs->d4;
  181. if (!newsp)
  182. newsp = rdusp();
  183. return do_fork(clone_flags, newsp, regs, 0,
  184. parent_tidptr, child_tidptr);
  185. }
  186. int copy_thread(unsigned long clone_flags, unsigned long usp,
  187. unsigned long unused,
  188. struct task_struct * p, struct pt_regs * regs)
  189. {
  190. struct pt_regs * childregs;
  191. struct switch_stack * childstack, *stack;
  192. unsigned long *retp;
  193. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  194. *childregs = *regs;
  195. childregs->d0 = 0;
  196. retp = ((unsigned long *) regs);
  197. stack = ((struct switch_stack *) retp) - 1;
  198. childstack = ((struct switch_stack *) childregs) - 1;
  199. *childstack = *stack;
  200. childstack->retpc = (unsigned long)ret_from_fork;
  201. p->thread.usp = usp;
  202. p->thread.ksp = (unsigned long)childstack;
  203. if (clone_flags & CLONE_SETTLS)
  204. task_thread_info(p)->tp_value = regs->d5;
  205. /*
  206. * Must save the current SFC/DFC value, NOT the value when
  207. * the parent was last descheduled - RGH 10-08-96
  208. */
  209. p->thread.fs = get_fs().seg;
  210. if (!FPU_IS_EMU) {
  211. /* Copy the current fpu state */
  212. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  213. if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2])
  214. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  215. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  216. : : "m" (p->thread.fp[0]), "m" (p->thread.fpcntl[0])
  217. : "memory");
  218. /* Restore the state in case the fpu was busy */
  219. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  220. }
  221. return 0;
  222. }
  223. /* Fill in the fpu structure for a core dump. */
  224. int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  225. {
  226. char fpustate[216];
  227. if (FPU_IS_EMU) {
  228. int i;
  229. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  230. memcpy(fpu->fpregs, current->thread.fp, 96);
  231. /* Convert internal fpu reg representation
  232. * into long double format
  233. */
  234. for (i = 0; i < 24; i += 3)
  235. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  236. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  237. return 1;
  238. }
  239. /* First dump the fpu context to avoid protocol violation. */
  240. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  241. if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
  242. return 0;
  243. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  244. :: "m" (fpu->fpcntl[0])
  245. : "memory");
  246. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  247. :: "m" (fpu->fpregs[0])
  248. : "memory");
  249. return 1;
  250. }
  251. EXPORT_SYMBOL(dump_fpu);
  252. /*
  253. * sys_execve() executes a new program.
  254. */
  255. asmlinkage int sys_execve(const char __user *name,
  256. const char __user *const __user *argv,
  257. const char __user *const __user *envp)
  258. {
  259. int error;
  260. char * filename;
  261. struct pt_regs *regs = (struct pt_regs *) &name;
  262. filename = getname(name);
  263. error = PTR_ERR(filename);
  264. if (IS_ERR(filename))
  265. return error;
  266. error = do_execve(filename, argv, envp, regs);
  267. putname(filename);
  268. return error;
  269. }
  270. unsigned long get_wchan(struct task_struct *p)
  271. {
  272. unsigned long fp, pc;
  273. unsigned long stack_page;
  274. int count = 0;
  275. if (!p || p == current || p->state == TASK_RUNNING)
  276. return 0;
  277. stack_page = (unsigned long)task_stack_page(p);
  278. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  279. do {
  280. if (fp < stack_page+sizeof(struct thread_info) ||
  281. fp >= 8184+stack_page)
  282. return 0;
  283. pc = ((unsigned long *)fp)[1];
  284. if (!in_sched_functions(pc))
  285. return pc;
  286. fp = *(unsigned long *) fp;
  287. } while (count++ < 16);
  288. return 0;
  289. }