process.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 = task_thread_info(p);
  129. struct pt_regs *childregs;
  130. long childksp;
  131. p->set_child_tid = p->clear_child_tid = NULL;
  132. childksp = (unsigned long)task_stack_page(p) + 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. elf_dump_regs(*regs, task_pt_regs(tsk));
  202. return 1;
  203. }
  204. int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr)
  205. {
  206. memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
  207. return 1;
  208. }
  209. /*
  210. * Create a kernel thread
  211. */
  212. ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
  213. {
  214. do_exit(fn(arg));
  215. }
  216. long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  217. {
  218. struct pt_regs regs;
  219. memset(&regs, 0, sizeof(regs));
  220. regs.regs[4] = (unsigned long) arg;
  221. regs.regs[5] = (unsigned long) fn;
  222. regs.cp0_epc = (unsigned long) kernel_thread_helper;
  223. regs.cp0_status = read_c0_status();
  224. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  225. regs.cp0_status &= ~(ST0_KUP | ST0_IEC);
  226. regs.cp0_status |= ST0_IEP;
  227. #else
  228. regs.cp0_status |= ST0_EXL;
  229. #endif
  230. /* Ok, create the new process.. */
  231. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  232. }
  233. static struct mips_frame_info {
  234. void *func;
  235. int omit_fp; /* compiled without fno-omit-frame-pointer */
  236. int frame_offset;
  237. int pc_offset;
  238. } schedule_frame, mfinfo[] = {
  239. { schedule, 0 }, /* must be first */
  240. /* arch/mips/kernel/semaphore.c */
  241. { __down, 1 },
  242. { __down_interruptible, 1 },
  243. /* kernel/sched.c */
  244. #ifdef CONFIG_PREEMPT
  245. { preempt_schedule, 0 },
  246. #endif
  247. { wait_for_completion, 0 },
  248. { interruptible_sleep_on, 0 },
  249. { interruptible_sleep_on_timeout, 0 },
  250. { sleep_on, 0 },
  251. { sleep_on_timeout, 0 },
  252. { yield, 0 },
  253. { io_schedule, 0 },
  254. { io_schedule_timeout, 0 },
  255. #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
  256. { __preempt_spin_lock, 0 },
  257. { __preempt_write_lock, 0 },
  258. #endif
  259. /* kernel/timer.c */
  260. { schedule_timeout, 1 },
  261. /* { nanosleep_restart, 1 }, */
  262. /* lib/rwsem-spinlock.c */
  263. { __down_read, 1 },
  264. { __down_write, 1 },
  265. };
  266. static int mips_frame_info_initialized;
  267. static int __init get_frame_info(struct mips_frame_info *info)
  268. {
  269. int i;
  270. void *func = info->func;
  271. union mips_instruction *ip = (union mips_instruction *)func;
  272. info->pc_offset = -1;
  273. info->frame_offset = info->omit_fp ? 0 : -1;
  274. for (i = 0; i < 128; i++, ip++) {
  275. /* if jal, jalr, jr, stop. */
  276. if (ip->j_format.opcode == jal_op ||
  277. (ip->r_format.opcode == spec_op &&
  278. (ip->r_format.func == jalr_op ||
  279. ip->r_format.func == jr_op)))
  280. break;
  281. if (
  282. #ifdef CONFIG_32BIT
  283. ip->i_format.opcode == sw_op &&
  284. #endif
  285. #ifdef CONFIG_64BIT
  286. ip->i_format.opcode == sd_op &&
  287. #endif
  288. ip->i_format.rs == 29)
  289. {
  290. /* sw / sd $ra, offset($sp) */
  291. if (ip->i_format.rt == 31) {
  292. if (info->pc_offset != -1)
  293. continue;
  294. info->pc_offset =
  295. ip->i_format.simmediate / sizeof(long);
  296. }
  297. /* sw / sd $s8, offset($sp) */
  298. if (ip->i_format.rt == 30) {
  299. //#if 0 /* gcc 3.4 does aggressive optimization... */
  300. if (info->frame_offset != -1)
  301. continue;
  302. //#endif
  303. info->frame_offset =
  304. ip->i_format.simmediate / sizeof(long);
  305. }
  306. }
  307. }
  308. if (info->pc_offset == -1 || info->frame_offset == -1) {
  309. printk("Can't analyze prologue code at %p\n", func);
  310. info->pc_offset = -1;
  311. info->frame_offset = -1;
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. static int __init frame_info_init(void)
  317. {
  318. int i, found;
  319. for (i = 0; i < ARRAY_SIZE(mfinfo); i++)
  320. if (get_frame_info(&mfinfo[i]))
  321. return -1;
  322. schedule_frame = mfinfo[0];
  323. /* bubble sort */
  324. do {
  325. struct mips_frame_info tmp;
  326. found = 0;
  327. for (i = 1; i < ARRAY_SIZE(mfinfo); i++) {
  328. if (mfinfo[i-1].func > mfinfo[i].func) {
  329. tmp = mfinfo[i];
  330. mfinfo[i] = mfinfo[i-1];
  331. mfinfo[i-1] = tmp;
  332. found = 1;
  333. }
  334. }
  335. } while (found);
  336. mips_frame_info_initialized = 1;
  337. return 0;
  338. }
  339. arch_initcall(frame_info_init);
  340. /*
  341. * Return saved PC of a blocked thread.
  342. */
  343. unsigned long thread_saved_pc(struct task_struct *tsk)
  344. {
  345. struct thread_struct *t = &tsk->thread;
  346. /* New born processes are a special case */
  347. if (t->reg31 == (unsigned long) ret_from_fork)
  348. return t->reg31;
  349. if (schedule_frame.pc_offset < 0)
  350. return 0;
  351. return ((unsigned long *)t->reg29)[schedule_frame.pc_offset];
  352. }
  353. /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */
  354. unsigned long get_wchan(struct task_struct *p)
  355. {
  356. unsigned long stack_page;
  357. unsigned long frame, pc;
  358. if (!p || p == current || p->state == TASK_RUNNING)
  359. return 0;
  360. stack_page = (unsigned long)task_stack_page(p);
  361. if (!stack_page || !mips_frame_info_initialized)
  362. return 0;
  363. pc = thread_saved_pc(p);
  364. if (!in_sched_functions(pc))
  365. return pc;
  366. frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
  367. do {
  368. int i;
  369. if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32)
  370. return 0;
  371. for (i = ARRAY_SIZE(mfinfo) - 1; i >= 0; i--) {
  372. if (pc >= (unsigned long) mfinfo[i].func)
  373. break;
  374. }
  375. if (i < 0)
  376. break;
  377. if (mfinfo[i].omit_fp)
  378. break;
  379. pc = ((unsigned long *)frame)[mfinfo[i].pc_offset];
  380. frame = ((unsigned long *)frame)[mfinfo[i].frame_offset];
  381. } while (in_sched_functions(pc));
  382. return pc;
  383. }
  384. EXPORT_SYMBOL(get_wchan);