process.c 10 KB

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