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
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
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>
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>
28 #include <asm/bootinfo.h>
31 #include <asm/pgtable.h>
32 #include <asm/system.h>
33 #include <asm/mipsregs.h>
34 #include <asm/processor.h>
35 #include <asm/uaccess.h>
38 #include <asm/isadep.h>
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)
46 ATTRIB_NORET void cpu_idle(void)
48 /* endless idle loop with no priority at all */
50 while (!need_resched())
57 asmlinkage void ret_from_fork(void);
59 void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
63 /* New thread loses kernel privileges. */
64 status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK);
67 status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR;
70 regs->cp0_status = status;
75 current_thread_info()->addr_limit = USER_DS;
78 void exit_thread(void)
82 void flush_thread(void)
86 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
87 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
89 struct thread_info *ti = p->thread_info;
90 struct pt_regs *childregs;
92 p->set_child_tid = p->clear_child_tid = NULL;
94 childksp = (unsigned long)ti + THREAD_SIZE - 32;
104 /* set up new TSS. */
105 childregs = (struct pt_regs *) childksp - 1;
107 childregs->regs[7] = 0; /* Clear error flag */
109 #if defined(CONFIG_BINFMT_IRIX)
110 if (current->personality != PER_LINUX) {
111 /* Under IRIX things are a little different. */
112 childregs->regs[3] = 1;
116 childregs->regs[2] = 0; /* Child gets zero as return value */
117 regs->regs[2] = p->pid;
119 if (childregs->cp0_status & ST0_CU0) {
120 childregs->regs[28] = (unsigned long) ti;
121 childregs->regs[29] = childksp;
122 ti->addr_limit = KERNEL_DS;
124 childregs->regs[29] = usp;
125 ti->addr_limit = USER_DS;
127 p->thread.reg29 = (unsigned long) childregs;
128 p->thread.reg31 = (unsigned long) ret_from_fork;
131 * New tasks lose permission to use the fpu. This accelerates context
132 * switching for most programs since they don't use the fpu.
134 p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
135 childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
136 clear_tsk_thread_flag(p, TIF_USEDFPU);
138 if (clone_flags & CLONE_SETTLS)
139 ti->tp_value = regs->regs[7];
144 /* Fill in the fpu structure for a core dump.. */
145 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
147 memcpy(r, ¤t->thread.fpu, sizeof(current->thread.fpu));
152 void dump_regs(elf_greg_t *gp, struct pt_regs *regs)
156 for (i = 0; i < EF_R0; i++)
159 for (i = 1; i <= 31; i++)
160 gp[EF_R0 + i] = regs->regs[i];
163 gp[EF_LO] = regs->lo;
164 gp[EF_HI] = regs->hi;
165 gp[EF_CP0_EPC] = regs->cp0_epc;
166 gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
167 gp[EF_CP0_STATUS] = regs->cp0_status;
168 gp[EF_CP0_CAUSE] = regs->cp0_cause;
174 int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs)
176 struct thread_info *ti = tsk->thread_info;
177 long ksp = (unsigned long)ti + THREAD_SIZE - 32;
178 dump_regs(&(*regs)[0], (struct pt_regs *) ksp - 1);
182 int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr)
184 memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
190 * Create a kernel thread
192 ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
197 long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
201 memset(®s, 0, sizeof(regs));
203 regs.regs[4] = (unsigned long) arg;
204 regs.regs[5] = (unsigned long) fn;
205 regs.cp0_epc = (unsigned long) kernel_thread_helper;
206 regs.cp0_status = read_c0_status();
207 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
208 regs.cp0_status &= ~(ST0_KUP | ST0_IEC);
209 regs.cp0_status |= ST0_IEP;
211 regs.cp0_status |= ST0_EXL;
214 /* Ok, create the new process.. */
215 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL);
218 static struct mips_frame_info {
220 int omit_fp; /* compiled without fno-omit-frame-pointer */
223 } schedule_frame, mfinfo[] = {
224 { schedule, 0 }, /* must be first */
225 /* arch/mips/kernel/semaphore.c */
227 { __down_interruptible, 1 },
229 #ifdef CONFIG_PREEMPT
230 { preempt_schedule, 0 },
232 { wait_for_completion, 0 },
233 { interruptible_sleep_on, 0 },
234 { interruptible_sleep_on_timeout, 0 },
236 { sleep_on_timeout, 0 },
239 { io_schedule_timeout, 0 },
240 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
241 { __preempt_spin_lock, 0 },
242 { __preempt_write_lock, 0 },
245 { schedule_timeout, 1 },
246 /* { nanosleep_restart, 1 }, */
247 /* lib/rwsem-spinlock.c */
252 static int mips_frame_info_initialized;
253 static int __init get_frame_info(struct mips_frame_info *info)
256 void *func = info->func;
257 union mips_instruction *ip = (union mips_instruction *)func;
258 info->pc_offset = -1;
259 info->frame_offset = info->omit_fp ? 0 : -1;
260 for (i = 0; i < 128; i++, ip++) {
261 /* if jal, jalr, jr, stop. */
262 if (ip->j_format.opcode == jal_op ||
263 (ip->r_format.opcode == spec_op &&
264 (ip->r_format.func == jalr_op ||
265 ip->r_format.func == jr_op)))
270 ip->i_format.opcode == sw_op &&
273 ip->i_format.opcode == sd_op &&
275 ip->i_format.rs == 29)
277 /* sw / sd $ra, offset($sp) */
278 if (ip->i_format.rt == 31) {
279 if (info->pc_offset != -1)
282 ip->i_format.simmediate / sizeof(long);
284 /* sw / sd $s8, offset($sp) */
285 if (ip->i_format.rt == 30) {
286 //#if 0 /* gcc 3.4 does aggressive optimization... */
287 if (info->frame_offset != -1)
291 ip->i_format.simmediate / sizeof(long);
295 if (info->pc_offset == -1 || info->frame_offset == -1) {
296 printk("Can't analyze prologue code at %p\n", func);
297 info->pc_offset = -1;
298 info->frame_offset = -1;
305 static int __init frame_info_init(void)
308 for (i = 0; i < ARRAY_SIZE(mfinfo); i++)
309 if (get_frame_info(&mfinfo[i]))
311 schedule_frame = mfinfo[0];
314 struct mips_frame_info tmp;
316 for (i = 1; i < ARRAY_SIZE(mfinfo); i++) {
317 if (mfinfo[i-1].func > mfinfo[i].func) {
319 mfinfo[i] = mfinfo[i-1];
325 mips_frame_info_initialized = 1;
329 arch_initcall(frame_info_init);
332 * Return saved PC of a blocked thread.
334 unsigned long thread_saved_pc(struct task_struct *tsk)
336 struct thread_struct *t = &tsk->thread;
338 /* New born processes are a special case */
339 if (t->reg31 == (unsigned long) ret_from_fork)
342 if (schedule_frame.pc_offset < 0)
344 return ((unsigned long *)t->reg29)[schedule_frame.pc_offset];
347 /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */
348 unsigned long get_wchan(struct task_struct *p)
350 unsigned long stack_page;
351 unsigned long frame, pc;
353 if (!p || p == current || p->state == TASK_RUNNING)
356 stack_page = (unsigned long)p->thread_info;
357 if (!stack_page || !mips_frame_info_initialized)
360 pc = thread_saved_pc(p);
361 if (!in_sched_functions(pc))
364 frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
368 if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32)
371 for (i = ARRAY_SIZE(mfinfo) - 1; i >= 0; i--) {
372 if (pc >= (unsigned long) mfinfo[i].func)
378 if (mfinfo[i].omit_fp)
380 pc = ((unsigned long *)frame)[mfinfo[i].pc_offset];
381 frame = ((unsigned long *)frame)[mfinfo[i].frame_offset];
382 } while (in_sched_functions(pc));
387 EXPORT_SYMBOL(get_wchan);