process_mm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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("frestore %0": :"m" (zero));
  153. }
  154. /*
  155. * "m68k_fork()".. By the time we get here, the
  156. * non-volatile registers have also been saved on the
  157. * stack. We do some ugly pointer stuff here.. (see
  158. * also copy_thread)
  159. */
  160. asmlinkage int m68k_fork(struct pt_regs *regs)
  161. {
  162. return do_fork(SIGCHLD, rdusp(), regs, 0, NULL, NULL);
  163. }
  164. asmlinkage int m68k_vfork(struct pt_regs *regs)
  165. {
  166. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0,
  167. NULL, NULL);
  168. }
  169. asmlinkage int m68k_clone(struct pt_regs *regs)
  170. {
  171. unsigned long clone_flags;
  172. unsigned long newsp;
  173. int __user *parent_tidptr, *child_tidptr;
  174. /* syscall2 puts clone_flags in d1 and usp in d2 */
  175. clone_flags = regs->d1;
  176. newsp = regs->d2;
  177. parent_tidptr = (int __user *)regs->d3;
  178. child_tidptr = (int __user *)regs->d4;
  179. if (!newsp)
  180. newsp = rdusp();
  181. return do_fork(clone_flags, newsp, regs, 0,
  182. parent_tidptr, child_tidptr);
  183. }
  184. int copy_thread(unsigned long clone_flags, unsigned long usp,
  185. unsigned long unused,
  186. struct task_struct * p, struct pt_regs * regs)
  187. {
  188. struct pt_regs * childregs;
  189. struct switch_stack * childstack, *stack;
  190. unsigned long *retp;
  191. childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
  192. *childregs = *regs;
  193. childregs->d0 = 0;
  194. retp = ((unsigned long *) regs);
  195. stack = ((struct switch_stack *) retp) - 1;
  196. childstack = ((struct switch_stack *) childregs) - 1;
  197. *childstack = *stack;
  198. childstack->retpc = (unsigned long)ret_from_fork;
  199. p->thread.usp = usp;
  200. p->thread.ksp = (unsigned long)childstack;
  201. if (clone_flags & CLONE_SETTLS)
  202. task_thread_info(p)->tp_value = regs->d5;
  203. /*
  204. * Must save the current SFC/DFC value, NOT the value when
  205. * the parent was last descheduled - RGH 10-08-96
  206. */
  207. p->thread.fs = get_fs().seg;
  208. if (!FPU_IS_EMU) {
  209. /* Copy the current fpu state */
  210. asm volatile ("fsave %0" : : "m" (p->thread.fpstate[0]) : "memory");
  211. if (!CPU_IS_060 ? p->thread.fpstate[0] : p->thread.fpstate[2]) {
  212. if (CPU_IS_COLDFIRE) {
  213. asm volatile ("fmovemd %/fp0-%/fp7,%0\n\t"
  214. "fmovel %/fpiar,%1\n\t"
  215. "fmovel %/fpcr,%2\n\t"
  216. "fmovel %/fpsr,%3"
  217. :
  218. : "m" (p->thread.fp[0]),
  219. "m" (p->thread.fpcntl[0]),
  220. "m" (p->thread.fpcntl[1]),
  221. "m" (p->thread.fpcntl[2])
  222. : "memory");
  223. } else {
  224. asm volatile ("fmovemx %/fp0-%/fp7,%0\n\t"
  225. "fmoveml %/fpiar/%/fpcr/%/fpsr,%1"
  226. :
  227. : "m" (p->thread.fp[0]),
  228. "m" (p->thread.fpcntl[0])
  229. : "memory");
  230. }
  231. }
  232. /* Restore the state in case the fpu was busy */
  233. asm volatile ("frestore %0" : : "m" (p->thread.fpstate[0]));
  234. }
  235. return 0;
  236. }
  237. /* Fill in the fpu structure for a core dump. */
  238. int dump_fpu (struct pt_regs *regs, struct user_m68kfp_struct *fpu)
  239. {
  240. char fpustate[216];
  241. if (FPU_IS_EMU) {
  242. int i;
  243. memcpy(fpu->fpcntl, current->thread.fpcntl, 12);
  244. memcpy(fpu->fpregs, current->thread.fp, 96);
  245. /* Convert internal fpu reg representation
  246. * into long double format
  247. */
  248. for (i = 0; i < 24; i += 3)
  249. fpu->fpregs[i] = ((fpu->fpregs[i] & 0xffff0000) << 15) |
  250. ((fpu->fpregs[i] & 0x0000ffff) << 16);
  251. return 1;
  252. }
  253. /* First dump the fpu context to avoid protocol violation. */
  254. asm volatile ("fsave %0" :: "m" (fpustate[0]) : "memory");
  255. if (!CPU_IS_060 ? !fpustate[0] : !fpustate[2])
  256. return 0;
  257. if (CPU_IS_COLDFIRE) {
  258. asm volatile ("fmovel %/fpiar,%0\n\t"
  259. "fmovel %/fpcr,%1\n\t"
  260. "fmovel %/fpsr,%2\n\t"
  261. "fmovemd %/fp0-%/fp7,%3"
  262. :
  263. : "m" (fpu->fpcntl[0]),
  264. "m" (fpu->fpcntl[1]),
  265. "m" (fpu->fpcntl[2]),
  266. "m" (fpu->fpregs[0])
  267. : "memory");
  268. } else {
  269. asm volatile ("fmovem %/fpiar/%/fpcr/%/fpsr,%0"
  270. :
  271. : "m" (fpu->fpcntl[0])
  272. : "memory");
  273. asm volatile ("fmovemx %/fp0-%/fp7,%0"
  274. :
  275. : "m" (fpu->fpregs[0])
  276. : "memory");
  277. }
  278. return 1;
  279. }
  280. EXPORT_SYMBOL(dump_fpu);
  281. /*
  282. * sys_execve() executes a new program.
  283. */
  284. asmlinkage int sys_execve(const char __user *name,
  285. const char __user *const __user *argv,
  286. const char __user *const __user *envp)
  287. {
  288. int error;
  289. char * filename;
  290. struct pt_regs *regs = (struct pt_regs *) &name;
  291. filename = getname(name);
  292. error = PTR_ERR(filename);
  293. if (IS_ERR(filename))
  294. return error;
  295. error = do_execve(filename, argv, envp, regs);
  296. putname(filename);
  297. return error;
  298. }
  299. unsigned long get_wchan(struct task_struct *p)
  300. {
  301. unsigned long fp, pc;
  302. unsigned long stack_page;
  303. int count = 0;
  304. if (!p || p == current || p->state == TASK_RUNNING)
  305. return 0;
  306. stack_page = (unsigned long)task_stack_page(p);
  307. fp = ((struct switch_stack *)p->thread.ksp)->a6;
  308. do {
  309. if (fp < stack_page+sizeof(struct thread_info) ||
  310. fp >= 8184+stack_page)
  311. return 0;
  312. pc = ((unsigned long *)fp)[1];
  313. if (!in_sched_functions(pc))
  314. return pc;
  315. fp = *(unsigned long *) fp;
  316. } while (count++ < 16);
  317. return 0;
  318. }