process.c 8.4 KB

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