process.c 10.0 KB

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