process_mm.c 8.3 KB

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