process.c 10 KB

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