process.c 8.5 KB

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