process.c 10 KB

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