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) 2005, 2006 by Ralf Baechle (ralf@linux-mips.org)
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 * Copyright (C) 2004 Thiemo Seufer
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>
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>
30 #include <asm/bootinfo.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>
41 #include <asm/isadep.h>
45 * The idle thread. There's no useful work to be done, so just try to conserve
46 * power and have a low exit latency (ie sit in a loop waiting for somebody to
47 * say that they'd like to reschedule)
49 ATTRIB_NORET void cpu_idle(void)
51 /* endless idle loop with no priority at all */
53 while (!need_resched())
56 preempt_enable_no_resched();
62 extern void do_signal(struct pt_regs *regs);
63 extern void do_signal32(struct pt_regs *regs);
66 * Native o32 and N64 ABI without DSP ASE
68 extern int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
69 int signr, sigset_t *set);
70 extern int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
71 int signr, sigset_t *set, siginfo_t *info);
73 struct mips_abi mips_abi = {
74 .do_signal = do_signal,
75 #ifdef CONFIG_TRAD_SIGNALS
76 .setup_frame = setup_frame,
78 .setup_rt_frame = setup_rt_frame
81 #ifdef CONFIG_MIPS32_O32
83 * o32 compatibility on 64-bit kernels, without DSP ASE
85 extern int setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
86 int signr, sigset_t *set);
87 extern int setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
88 int signr, sigset_t *set, siginfo_t *info);
90 struct mips_abi mips_abi_32 = {
91 .do_signal = do_signal32,
92 .setup_frame = setup_frame_32,
93 .setup_rt_frame = setup_rt_frame_32
95 #endif /* CONFIG_MIPS32_O32 */
97 #ifdef CONFIG_MIPS32_N32
99 * N32 on 64-bit kernels, without DSP ASE
101 extern int setup_rt_frame_n32(struct k_sigaction * ka, struct pt_regs *regs,
102 int signr, sigset_t *set, siginfo_t *info);
104 struct mips_abi mips_abi_n32 = {
105 .do_signal = do_signal,
106 .setup_rt_frame = setup_rt_frame_n32
108 #endif /* CONFIG_MIPS32_N32 */
110 asmlinkage void ret_from_fork(void);
112 void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
114 unsigned long status;
116 /* New thread loses kernel privileges. */
117 status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK);
120 status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR;
123 regs->cp0_status = status;
130 current_thread_info()->addr_limit = USER_DS;
133 void exit_thread(void)
137 void flush_thread(void)
141 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
142 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
144 struct thread_info *ti = task_thread_info(p);
145 struct pt_regs *childregs;
147 p->set_child_tid = p->clear_child_tid = NULL;
149 childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
161 /* set up new TSS. */
162 childregs = (struct pt_regs *) childksp - 1;
164 childregs->regs[7] = 0; /* Clear error flag */
166 #if defined(CONFIG_BINFMT_IRIX)
167 if (current->personality != PER_LINUX) {
168 /* Under IRIX things are a little different. */
169 childregs->regs[3] = 1;
173 childregs->regs[2] = 0; /* Child gets zero as return value */
174 regs->regs[2] = p->pid;
176 if (childregs->cp0_status & ST0_CU0) {
177 childregs->regs[28] = (unsigned long) ti;
178 childregs->regs[29] = childksp;
179 ti->addr_limit = KERNEL_DS;
181 childregs->regs[29] = usp;
182 ti->addr_limit = USER_DS;
184 p->thread.reg29 = (unsigned long) childregs;
185 p->thread.reg31 = (unsigned long) ret_from_fork;
188 * New tasks lose permission to use the fpu. This accelerates context
189 * switching for most programs since they don't use the fpu.
191 p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
192 childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
193 clear_tsk_thread_flag(p, TIF_USEDFPU);
195 if (clone_flags & CLONE_SETTLS)
196 ti->tp_value = regs->regs[7];
201 /* Fill in the fpu structure for a core dump.. */
202 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
204 memcpy(r, ¤t->thread.fpu, sizeof(current->thread.fpu));
209 void elf_dump_regs(elf_greg_t *gp, struct pt_regs *regs)
213 for (i = 0; i < EF_R0; i++)
216 for (i = 1; i <= 31; i++)
217 gp[EF_R0 + i] = regs->regs[i];
220 gp[EF_LO] = regs->lo;
221 gp[EF_HI] = regs->hi;
222 gp[EF_CP0_EPC] = regs->cp0_epc;
223 gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
224 gp[EF_CP0_STATUS] = regs->cp0_status;
225 gp[EF_CP0_CAUSE] = regs->cp0_cause;
231 int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs)
233 elf_dump_regs(*regs, task_pt_regs(tsk));
237 int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr)
239 memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
245 * Create a kernel thread
247 ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
252 long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
256 memset(®s, 0, sizeof(regs));
258 regs.regs[4] = (unsigned long) arg;
259 regs.regs[5] = (unsigned long) fn;
260 regs.cp0_epc = (unsigned long) kernel_thread_helper;
261 regs.cp0_status = read_c0_status();
262 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
263 regs.cp0_status &= ~(ST0_KUP | ST0_IEC);
264 regs.cp0_status |= ST0_IEP;
266 regs.cp0_status |= ST0_EXL;
269 /* Ok, create the new process.. */
270 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL);
273 static struct mips_frame_info {
275 int omit_fp; /* compiled without fno-omit-frame-pointer */
278 } schedule_frame, mfinfo[] = {
279 { schedule, 0 }, /* must be first */
280 /* arch/mips/kernel/semaphore.c */
282 { __down_interruptible, 1 },
284 #ifdef CONFIG_PREEMPT
285 { preempt_schedule, 0 },
287 { wait_for_completion, 0 },
288 { interruptible_sleep_on, 0 },
289 { interruptible_sleep_on_timeout, 0 },
291 { sleep_on_timeout, 0 },
294 { io_schedule_timeout, 0 },
295 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
296 { __preempt_spin_lock, 0 },
297 { __preempt_write_lock, 0 },
300 { schedule_timeout, 1 },
301 /* { nanosleep_restart, 1 }, */
302 /* lib/rwsem-spinlock.c */
307 static int mips_frame_info_initialized;
308 static int __init get_frame_info(struct mips_frame_info *info)
311 void *func = info->func;
312 union mips_instruction *ip = (union mips_instruction *)func;
313 info->pc_offset = -1;
314 info->frame_offset = info->omit_fp ? 0 : -1;
315 for (i = 0; i < 128; i++, ip++) {
316 /* if jal, jalr, jr, stop. */
317 if (ip->j_format.opcode == jal_op ||
318 (ip->r_format.opcode == spec_op &&
319 (ip->r_format.func == jalr_op ||
320 ip->r_format.func == jr_op)))
325 ip->i_format.opcode == sw_op &&
328 ip->i_format.opcode == sd_op &&
330 ip->i_format.rs == 29)
332 /* sw / sd $ra, offset($sp) */
333 if (ip->i_format.rt == 31) {
334 if (info->pc_offset != -1)
337 ip->i_format.simmediate / sizeof(long);
339 /* sw / sd $s8, offset($sp) */
340 if (ip->i_format.rt == 30) {
341 //#if 0 /* gcc 3.4 does aggressive optimization... */
342 if (info->frame_offset != -1)
346 ip->i_format.simmediate / sizeof(long);
350 if (info->pc_offset == -1 || info->frame_offset == -1) {
351 printk("Can't analyze prologue code at %p\n", func);
352 info->pc_offset = -1;
353 info->frame_offset = -1;
360 static int __init frame_info_init(void)
363 for (i = 0; i < ARRAY_SIZE(mfinfo); i++)
364 if (get_frame_info(&mfinfo[i]))
366 schedule_frame = mfinfo[0];
369 struct mips_frame_info tmp;
371 for (i = 1; i < ARRAY_SIZE(mfinfo); i++) {
372 if (mfinfo[i-1].func > mfinfo[i].func) {
374 mfinfo[i] = mfinfo[i-1];
380 mips_frame_info_initialized = 1;
384 arch_initcall(frame_info_init);
387 * Return saved PC of a blocked thread.
389 unsigned long thread_saved_pc(struct task_struct *tsk)
391 struct thread_struct *t = &tsk->thread;
393 /* New born processes are a special case */
394 if (t->reg31 == (unsigned long) ret_from_fork)
397 if (schedule_frame.pc_offset < 0)
399 return ((unsigned long *)t->reg29)[schedule_frame.pc_offset];
402 /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */
403 unsigned long get_wchan(struct task_struct *p)
405 unsigned long stack_page;
406 unsigned long frame, pc;
408 if (!p || p == current || p->state == TASK_RUNNING)
411 stack_page = (unsigned long)task_stack_page(p);
412 if (!stack_page || !mips_frame_info_initialized)
415 pc = thread_saved_pc(p);
416 if (!in_sched_functions(pc))
419 frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
423 if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32)
426 for (i = ARRAY_SIZE(mfinfo) - 1; i >= 0; i--) {
427 if (pc >= (unsigned long) mfinfo[i].func)
433 if (mfinfo[i].omit_fp)
435 pc = ((unsigned long *)frame)[mfinfo[i].pc_offset];
436 frame = ((unsigned long *)frame)[mfinfo[i].frame_offset];
437 } while (in_sched_functions(pc));
442 EXPORT_SYMBOL(get_wchan);