process.c 9.7 KB

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