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 - 2000 by Ralf Baechle and others.
7 * Copyright (C) 1999 Silicon Graphics, Inc.
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
13 #include <linux/stddef.h>
14 #include <linux/unistd.h>
15 #include <linux/personality.h>
16 #include <linux/slab.h>
17 #include <linux/mman.h>
18 #include <linux/sys.h>
19 #include <linux/user.h>
20 #include <linux/a.out.h>
21 #include <linux/init.h>
22 #include <linux/completion.h>
24 #include <asm/bootinfo.h>
27 #include <asm/pgtable.h>
28 #include <asm/system.h>
29 #include <asm/mipsregs.h>
30 #include <asm/processor.h>
31 #include <asm/ptrace.h>
32 #include <asm/uaccess.h>
35 #include <asm/isadep.h>
38 ATTRIB_NORET void cpu_idle(void)
40 /* endless idle loop with no priority at all */
42 current->counter = -100;
46 while (!current->need_resched)
54 asmlinkage void ret_from_fork(void);
56 void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
58 regs->cp0_status &= ~(ST0_CU0|ST0_KSU|ST0_CU1);
59 regs->cp0_status |= KU_USER;
60 current->used_math = 0;
64 current->thread.current_ds = USER_DS;
67 void exit_thread(void)
71 void flush_thread(void)
75 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
77 struct task_struct * p, struct pt_regs * regs)
79 struct pt_regs * childregs;
82 childksp = (unsigned long)p + KERNEL_STACK_SIZE - 32;
89 childregs = (struct pt_regs *) childksp - 1;
91 childregs->regs[7] = 0; /* Clear error flag */
92 if(current->personality == PER_LINUX) {
93 childregs->regs[2] = 0; /* Child gets zero as return value */
94 regs->regs[2] = p->pid;
96 /* Under IRIX things are a little different. */
97 childregs->regs[2] = 0;
98 childregs->regs[3] = 1;
99 regs->regs[2] = p->pid;
102 if (childregs->cp0_status & ST0_CU0) {
103 childregs->regs[28] = (unsigned long) p;
104 childregs->regs[29] = childksp;
105 p->thread.current_ds = KERNEL_DS;
107 childregs->regs[29] = usp;
108 p->thread.current_ds = USER_DS;
110 p->thread.reg29 = (unsigned long) childregs;
111 p->thread.reg31 = (unsigned long) ret_from_fork;
114 * New tasks lose permission to use the fpu. This accelerates context
115 * switching for most programs since they don't use the fpu.
117 p->thread.cp0_status = read_c0_status() &
118 ~(ST0_CU2|ST0_CU1|KU_MASK);
119 childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
124 /* Fill in the fpu structure for a core dump.. */
125 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
127 memcpy(r, ¤t->thread.fpu, sizeof(current->thread.fpu));
132 * Create a kernel thread
134 int arch_kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
138 __asm__ __volatile__(
144 " beq $6, $sp, 1f \n"
151 "1: addiu $sp, 32 \n"
155 : "i" (__NR_clone), "i" (__NR_exit), "r" (arg), "r" (fn),
156 "r" (flags | CLONE_VM)
158 * The called subroutine might have destroyed any of the
159 * at, result, argument or temporary registers ...
161 : "$2", "$3", "$4", "$5", "$6", "$7", "$8",
162 "$9","$10","$11","$12","$13","$14","$15","$24","$25", "$31");
168 * These bracket the sleeping functions..
170 extern void scheduling_functions_start_here(void);
171 extern void scheduling_functions_end_here(void);
172 #define first_sched ((unsigned long) scheduling_functions_start_here)
173 #define last_sched ((unsigned long) scheduling_functions_end_here)
175 struct mips_frame_info schedule_frame;
176 static struct mips_frame_info schedule_timeout_frame;
177 static struct mips_frame_info sleep_on_frame;
178 static struct mips_frame_info sleep_on_timeout_frame;
179 static struct mips_frame_info wait_for_completion_frame;
180 static int mips_frame_info_initialized;
181 static int __init get_frame_info(struct mips_frame_info *info, void *func)
184 union mips_instruction *ip = (union mips_instruction *)func;
185 info->pc_offset = -1;
186 info->frame_offset = -1;
187 for (i = 0; i < 128; i++, ip++) {
188 /* if jal, jalr, jr, stop. */
189 if (ip->j_format.opcode == jal_op ||
190 (ip->r_format.opcode == spec_op &&
191 (ip->r_format.func == jalr_op ||
192 ip->r_format.func == jr_op)))
194 if (ip->i_format.opcode == sw_op &&
195 ip->i_format.rs == 29) {
196 /* sw $ra, offset($sp) */
197 if (ip->i_format.rt == 31) {
198 if (info->pc_offset != -1)
201 ip->i_format.simmediate / sizeof(long);
203 /* sw $s8, offset($sp) */
204 if (ip->i_format.rt == 30) {
205 if (info->frame_offset != -1)
208 ip->i_format.simmediate / sizeof(long);
212 if (info->pc_offset == -1 || info->frame_offset == -1) {
213 printk("Can't analyze prologue code at %p\n", func);
214 info->pc_offset = -1;
215 info->frame_offset = -1;
221 void __init frame_info_init(void)
223 mips_frame_info_initialized =
224 !get_frame_info(&schedule_frame, schedule) &&
225 !get_frame_info(&schedule_timeout_frame, schedule_timeout) &&
226 !get_frame_info(&sleep_on_frame, sleep_on) &&
227 !get_frame_info(&sleep_on_timeout_frame, sleep_on_timeout) &&
228 !get_frame_info(&wait_for_completion_frame, wait_for_completion);
231 /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */
232 unsigned long get_wchan(struct task_struct *p)
234 unsigned long frame, pc;
236 if (!p || p == current || p->state == TASK_RUNNING)
239 if (!mips_frame_info_initialized)
241 pc = thread_saved_pc(&p->thread);
242 if (pc < first_sched || pc >= last_sched) {
246 if (pc >= (unsigned long) sleep_on_timeout)
247 goto schedule_timeout_caller;
248 if (pc >= (unsigned long) sleep_on)
249 goto schedule_caller;
250 if (pc >= (unsigned long) interruptible_sleep_on_timeout)
251 goto schedule_timeout_caller;
252 if (pc >= (unsigned long)interruptible_sleep_on)
253 goto schedule_caller;
254 if (pc >= (unsigned long)wait_for_completion)
255 goto schedule_caller;
256 goto schedule_timeout_caller;
259 frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
260 if (pc >= (unsigned long) sleep_on)
261 pc = ((unsigned long *)frame)[sleep_on_frame.pc_offset];
263 pc = ((unsigned long *)frame)[wait_for_completion_frame.pc_offset];
266 schedule_timeout_caller:
268 * The schedule_timeout frame
270 frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
273 * frame now points to sleep_on_timeout's frame
275 pc = ((unsigned long *)frame)[schedule_timeout_frame.pc_offset];
277 if (pc >= first_sched && pc < last_sched) {
278 /* schedule_timeout called by [interruptible_]sleep_on_timeout */
279 frame = ((unsigned long *)frame)[schedule_timeout_frame.frame_offset];
280 pc = ((unsigned long *)frame)[sleep_on_timeout_frame.pc_offset];