process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1994 - 1999, 2000 by Ralf Baechle and others.
  7. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  8. * Copyright (C) 2004 Thiemo Seufer
  9. */
  10. #include <linux/config.h>
  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/stddef.h>
  17. #include <linux/unistd.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/slab.h>
  20. #include <linux/mman.h>
  21. #include <linux/personality.h>
  22. #include <linux/sys.h>
  23. #include <linux/user.h>
  24. #include <linux/a.out.h>
  25. #include <linux/init.h>
  26. #include <linux/completion.h>
  27. #include <asm/abi.h>
  28. #include <asm/bootinfo.h>
  29. #include <asm/cpu.h>
  30. #include <asm/dsp.h>
  31. #include <asm/fpu.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/system.h>
  34. #include <asm/mipsregs.h>
  35. #include <asm/processor.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/io.h>
  38. #include <asm/elf.h>
  39. #include <asm/isadep.h>
  40. #include <asm/inst.h>
  41. /*
  42. * The idle thread. There's no useful work to be done, so just try to conserve
  43. * power and have a low exit latency (ie sit in a loop waiting for somebody to
  44. * say that they'd like to reschedule)
  45. */
  46. ATTRIB_NORET void cpu_idle(void)
  47. {
  48. /* endless idle loop with no priority at all */
  49. while (1) {
  50. while (!need_resched())
  51. if (cpu_wait)
  52. (*cpu_wait)();
  53. preempt_enable_no_resched();
  54. schedule();
  55. preempt_disable();
  56. }
  57. }
  58. extern int do_signal(sigset_t *oldset, struct pt_regs *regs);
  59. extern int do_signal32(sigset_t *oldset, struct pt_regs *regs);
  60. /*
  61. * Native o32 and N64 ABI without DSP ASE
  62. */
  63. extern int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
  64. int signr, sigset_t *set);
  65. extern int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
  66. int signr, sigset_t *set, siginfo_t *info);
  67. struct mips_abi mips_abi = {
  68. .do_signal = do_signal,
  69. #ifdef CONFIG_TRAD_SIGNALS
  70. .setup_frame = setup_frame,
  71. #endif
  72. .setup_rt_frame = setup_rt_frame
  73. };
  74. #ifdef CONFIG_MIPS32_O32
  75. /*
  76. * o32 compatibility on 64-bit kernels, without DSP ASE
  77. */
  78. extern int setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
  79. int signr, sigset_t *set);
  80. extern int setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
  81. int signr, sigset_t *set, siginfo_t *info);
  82. struct mips_abi mips_abi_32 = {
  83. .do_signal = do_signal32,
  84. .setup_frame = setup_frame_32,
  85. .setup_rt_frame = setup_rt_frame_32
  86. };
  87. #endif /* CONFIG_MIPS32_O32 */
  88. #ifdef CONFIG_MIPS32_N32
  89. /*
  90. * N32 on 64-bit kernels, without DSP ASE
  91. */
  92. extern int setup_rt_frame_n32(struct k_sigaction * ka, struct pt_regs *regs,
  93. int signr, sigset_t *set, siginfo_t *info);
  94. struct mips_abi mips_abi_n32 = {
  95. .do_signal = do_signal,
  96. .setup_rt_frame = setup_rt_frame_n32
  97. };
  98. #endif /* CONFIG_MIPS32_N32 */
  99. asmlinkage void ret_from_fork(void);
  100. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  101. {
  102. unsigned long status;
  103. /* New thread loses kernel privileges. */
  104. status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK);
  105. #ifdef CONFIG_64BIT
  106. status &= ~ST0_FR;
  107. status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR;
  108. #endif
  109. status |= KU_USER;
  110. regs->cp0_status = status;
  111. clear_used_math();
  112. lose_fpu();
  113. if (cpu_has_dsp)
  114. __init_dsp();
  115. regs->cp0_epc = pc;
  116. regs->regs[29] = sp;
  117. current_thread_info()->addr_limit = USER_DS;
  118. }
  119. void exit_thread(void)
  120. {
  121. }
  122. void flush_thread(void)
  123. {
  124. }
  125. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  126. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  127. {
  128. struct thread_info *ti = p->thread_info;
  129. struct pt_regs *childregs;
  130. long childksp;
  131. p->set_child_tid = p->clear_child_tid = NULL;
  132. childksp = (unsigned long)ti + THREAD_SIZE - 32;
  133. preempt_disable();
  134. if (is_fpu_owner())
  135. save_fp(p);
  136. if (cpu_has_dsp)
  137. save_dsp(p);
  138. preempt_enable();
  139. /* set up new TSS. */
  140. childregs = (struct pt_regs *) childksp - 1;
  141. *childregs = *regs;
  142. childregs->regs[7] = 0; /* Clear error flag */
  143. #if defined(CONFIG_BINFMT_IRIX)
  144. if (current->personality != PER_LINUX) {
  145. /* Under IRIX things are a little different. */
  146. childregs->regs[3] = 1;
  147. regs->regs[3] = 0;
  148. }
  149. #endif
  150. childregs->regs[2] = 0; /* Child gets zero as return value */
  151. regs->regs[2] = p->pid;
  152. if (childregs->cp0_status & ST0_CU0) {
  153. childregs->regs[28] = (unsigned long) ti;
  154. childregs->regs[29] = childksp;
  155. ti->addr_limit = KERNEL_DS;
  156. } else {
  157. childregs->regs[29] = usp;
  158. ti->addr_limit = USER_DS;
  159. }
  160. p->thread.reg29 = (unsigned long) childregs;
  161. p->thread.reg31 = (unsigned long) ret_from_fork;
  162. /*
  163. * New tasks lose permission to use the fpu. This accelerates context
  164. * switching for most programs since they don't use the fpu.
  165. */
  166. p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
  167. childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
  168. clear_tsk_thread_flag(p, TIF_USEDFPU);
  169. if (clone_flags & CLONE_SETTLS)
  170. ti->tp_value = regs->regs[7];
  171. return 0;
  172. }
  173. /* Fill in the fpu structure for a core dump.. */
  174. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  175. {
  176. memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
  177. return 1;
  178. }
  179. void elf_dump_regs(elf_greg_t *gp, struct pt_regs *regs)
  180. {
  181. int i;
  182. for (i = 0; i < EF_R0; i++)
  183. gp[i] = 0;
  184. gp[EF_R0] = 0;
  185. for (i = 1; i <= 31; i++)
  186. gp[EF_R0 + i] = regs->regs[i];
  187. gp[EF_R26] = 0;
  188. gp[EF_R27] = 0;
  189. gp[EF_LO] = regs->lo;
  190. gp[EF_HI] = regs->hi;
  191. gp[EF_CP0_EPC] = regs->cp0_epc;
  192. gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  193. gp[EF_CP0_STATUS] = regs->cp0_status;
  194. gp[EF_CP0_CAUSE] = regs->cp0_cause;
  195. #ifdef EF_UNUSED0
  196. gp[EF_UNUSED0] = 0;
  197. #endif
  198. }
  199. int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs)
  200. {
  201. struct thread_info *ti = tsk->thread_info;
  202. long ksp = (unsigned long)ti + THREAD_SIZE - 32;
  203. elf_dump_regs(&(*regs)[0], (struct pt_regs *) ksp - 1);
  204. return 1;
  205. }
  206. int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr)
  207. {
  208. memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
  209. return 1;
  210. }
  211. /*
  212. * Create a kernel thread
  213. */
  214. ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
  215. {
  216. do_exit(fn(arg));
  217. }
  218. long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  219. {
  220. struct pt_regs regs;
  221. memset(&regs, 0, sizeof(regs));
  222. regs.regs[4] = (unsigned long) arg;
  223. regs.regs[5] = (unsigned long) fn;
  224. regs.cp0_epc = (unsigned long) kernel_thread_helper;
  225. regs.cp0_status = read_c0_status();
  226. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  227. regs.cp0_status &= ~(ST0_KUP | ST0_IEC);
  228. regs.cp0_status |= ST0_IEP;
  229. #else
  230. regs.cp0_status |= ST0_EXL;
  231. #endif
  232. /* Ok, create the new process.. */
  233. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  234. }
  235. static struct mips_frame_info {
  236. void *func;
  237. int omit_fp; /* compiled without fno-omit-frame-pointer */
  238. int frame_offset;
  239. int pc_offset;
  240. } schedule_frame, mfinfo[] = {
  241. { schedule, 0 }, /* must be first */
  242. /* arch/mips/kernel/semaphore.c */
  243. { __down, 1 },
  244. { __down_interruptible, 1 },
  245. /* kernel/sched.c */
  246. #ifdef CONFIG_PREEMPT
  247. { preempt_schedule, 0 },
  248. #endif
  249. { wait_for_completion, 0 },
  250. { interruptible_sleep_on, 0 },
  251. { interruptible_sleep_on_timeout, 0 },
  252. { sleep_on, 0 },
  253. { sleep_on_timeout, 0 },
  254. { yield, 0 },
  255. { io_schedule, 0 },
  256. { io_schedule_timeout, 0 },
  257. #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
  258. { __preempt_spin_lock, 0 },
  259. { __preempt_write_lock, 0 },
  260. #endif
  261. /* kernel/timer.c */
  262. { schedule_timeout, 1 },
  263. /* { nanosleep_restart, 1 }, */
  264. /* lib/rwsem-spinlock.c */
  265. { __down_read, 1 },
  266. { __down_write, 1 },
  267. };
  268. static int mips_frame_info_initialized;
  269. static int __init get_frame_info(struct mips_frame_info *info)
  270. {
  271. int i;
  272. void *func = info->func;
  273. union mips_instruction *ip = (union mips_instruction *)func;
  274. info->pc_offset = -1;
  275. info->frame_offset = info->omit_fp ? 0 : -1;
  276. for (i = 0; i < 128; i++, ip++) {
  277. /* if jal, jalr, jr, stop. */
  278. if (ip->j_format.opcode == jal_op ||
  279. (ip->r_format.opcode == spec_op &&
  280. (ip->r_format.func == jalr_op ||
  281. ip->r_format.func == jr_op)))
  282. break;
  283. if (
  284. #ifdef CONFIG_32BIT
  285. ip->i_format.opcode == sw_op &&
  286. #endif
  287. #ifdef CONFIG_64BIT
  288. ip->i_format.opcode == sd_op &&
  289. #endif
  290. ip->i_format.rs == 29)
  291. {
  292. /* sw / sd $ra, offset($sp) */
  293. if (ip->i_format.rt == 31) {
  294. if (info->pc_offset != -1)
  295. continue;
  296. info->pc_offset =
  297. ip->i_format.simmediate / sizeof(long);
  298. }
  299. /* sw / sd $s8, offset($sp) */
  300. if (ip->i_format.rt == 30) {
  301. //#if 0 /* gcc 3.4 does aggressive optimization... */
  302. if (info->frame_offset != -1)
  303. continue;
  304. //#endif
  305. info->frame_offset =
  306. ip->i_format.simmediate / sizeof(long);
  307. }
  308. }
  309. }
  310. if (info->pc_offset == -1 || info->frame_offset == -1) {
  311. printk("Can't analyze prologue code at %p\n", func);
  312. info->pc_offset = -1;
  313. info->frame_offset = -1;
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. static int __init frame_info_init(void)
  319. {
  320. int i, found;
  321. for (i = 0; i < ARRAY_SIZE(mfinfo); i++)
  322. if (get_frame_info(&mfinfo[i]))
  323. return -1;
  324. schedule_frame = mfinfo[0];
  325. /* bubble sort */
  326. do {
  327. struct mips_frame_info tmp;
  328. found = 0;
  329. for (i = 1; i < ARRAY_SIZE(mfinfo); i++) {
  330. if (mfinfo[i-1].func > mfinfo[i].func) {
  331. tmp = mfinfo[i];
  332. mfinfo[i] = mfinfo[i-1];
  333. mfinfo[i-1] = tmp;
  334. found = 1;
  335. }
  336. }
  337. } while (found);
  338. mips_frame_info_initialized = 1;
  339. return 0;
  340. }
  341. arch_initcall(frame_info_init);
  342. /*
  343. * Return saved PC of a blocked thread.
  344. */
  345. unsigned long thread_saved_pc(struct task_struct *tsk)
  346. {
  347. struct thread_struct *t = &tsk->thread;
  348. /* New born processes are a special case */
  349. if (t->reg31 == (unsigned long) ret_from_fork)
  350. return t->reg31;
  351. if (schedule_frame.pc_offset < 0)
  352. return 0;
  353. return ((unsigned long *)t->reg29)[schedule_frame.pc_offset];
  354. }
  355. /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */
  356. unsigned long get_wchan(struct task_struct *p)
  357. {
  358. unsigned long stack_page;
  359. unsigned long frame, pc;
  360. if (!p || p == current || p->state == TASK_RUNNING)
  361. return 0;
  362. stack_page = (unsigned long)p->thread_info;
  363. if (!stack_page || !mips_frame_info_initialized)
  364. return 0;
  365. pc = thread_saved_pc(p);
  366. if (!in_sched_functions(pc))
  367. return pc;
  368. frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
  369. do {
  370. int i;
  371. if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32)
  372. return 0;
  373. for (i = ARRAY_SIZE(mfinfo) - 1; i >= 0; i--) {
  374. if (pc >= (unsigned long) mfinfo[i].func)
  375. break;
  376. }
  377. if (i < 0)
  378. break;
  379. if (mfinfo[i].omit_fp)
  380. break;
  381. pc = ((unsigned long *)frame)[mfinfo[i].pc_offset];
  382. frame = ((unsigned long *)frame)[mfinfo[i].frame_offset];
  383. } while (in_sched_functions(pc));
  384. return pc;
  385. }
  386. EXPORT_SYMBOL(get_wchan);