patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / mips / kernel / process.c
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) 1999, 2000 Silicon Graphics, Inc.
8  */
9 #include <linux/config.h>
10 #include <linux/errno.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/stddef.h>
16 #include <linux/unistd.h>
17 #include <linux/ptrace.h>
18 #include <linux/slab.h>
19 #include <linux/mman.h>
20 #include <linux/personality.h>
21 #include <linux/sys.h>
22 #include <linux/user.h>
23 #include <linux/a.out.h>
24 #include <linux/init.h>
25 #include <linux/completion.h>
26
27 #include <asm/bootinfo.h>
28 #include <asm/cpu.h>
29 #include <asm/fpu.h>
30 #include <asm/pgtable.h>
31 #include <asm/system.h>
32 #include <asm/mipsregs.h>
33 #include <asm/processor.h>
34 #include <asm/uaccess.h>
35 #include <asm/io.h>
36 #include <asm/elf.h>
37 #include <asm/isadep.h>
38 #include <asm/inst.h>
39
40 /*
41  * We use this if we don't have any better idle routine..
42  * (This to kill: kernel/platform.c.
43  */
44 void default_idle (void)
45 {
46 }
47
48 /*
49  * The idle thread. There's no useful work to be done, so just try to conserve
50  * power and have a low exit latency (ie sit in a loop waiting for somebody to
51  * say that they'd like to reschedule)
52  */
53 ATTRIB_NORET void cpu_idle(void)
54 {
55         /* endless idle loop with no priority at all */
56         while (1) {
57                 while (!need_resched())
58                         if (cpu_wait)
59                                 (*cpu_wait)();
60                 schedule();
61         }
62 }
63
64 asmlinkage void ret_from_fork(void);
65
66 void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
67 {
68         unsigned long status;
69
70         /* New thread loses kernel privileges. */
71         status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK);
72 #ifdef CONFIG_MIPS64
73         status &= ~ST0_FR;
74         status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR;
75 #endif
76         status |= KU_USER;
77         regs->cp0_status = status;
78         current->used_math = 0;
79         lose_fpu();
80         regs->cp0_epc = pc;
81         regs->regs[29] = sp;
82         current_thread_info()->addr_limit = USER_DS;
83 }
84
85 void exit_thread(void)
86 {
87 }
88
89 void flush_thread(void)
90 {
91 }
92
93 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
94         unsigned long unused, struct task_struct *p, struct pt_regs *regs)
95 {
96         struct thread_info *ti = p->thread_info;
97         struct pt_regs *childregs;
98         long childksp;
99
100         childksp = (unsigned long)ti + THREAD_SIZE - 32;
101
102         if (is_fpu_owner()) {
103                 save_fp(p);
104         }
105
106         /* set up new TSS. */
107         childregs = (struct pt_regs *) childksp - 1;
108         *childregs = *regs;
109         childregs->regs[7] = 0; /* Clear error flag */
110
111 #ifdef CONFIG_BINFMT_IRIX
112         if (current->personality != PER_LINUX) {
113                 /* Under IRIX things are a little different. */
114                 childregs->regs[2] = 0;
115                 childregs->regs[3] = 1;
116                 regs->regs[2] = p->pid;
117                 regs->regs[3] = 0;
118         } else
119 #endif
120         {
121                 childregs->regs[2] = 0; /* Child gets zero as return value */
122                 regs->regs[2] = p->pid;
123         }
124
125         if (childregs->cp0_status & ST0_CU0) {
126                 childregs->regs[28] = (unsigned long) ti;
127                 childregs->regs[29] = childksp;
128                 ti->addr_limit = KERNEL_DS;
129         } else {
130                 childregs->regs[29] = usp;
131                 ti->addr_limit = USER_DS;
132         }
133         p->thread.reg29 = (unsigned long) childregs;
134         p->thread.reg31 = (unsigned long) ret_from_fork;
135
136         /*
137          * New tasks lose permission to use the fpu. This accelerates context
138          * switching for most programs since they don't use the fpu.
139          */
140         p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
141         childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
142         clear_tsk_thread_flag(p, TIF_USEDFPU);
143         p->set_child_tid = p->clear_child_tid = NULL;
144
145         return 0;
146 }
147
148 /* Fill in the fpu structure for a core dump.. */
149 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
150 {
151         memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
152         return 1;
153 }
154
155 /*
156  * Create a kernel thread
157  */
158 long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
159 {
160         long retval;
161
162         __asm__ __volatile__(
163                 "       move    $6, $sp         \n"
164                 "       move    $4, %5          \n"
165                 "       li      $2, %1          \n"
166                 "       syscall                 \n"
167                 "       beq     $6, $sp, 1f     \n"
168 #ifdef CONFIG_MIPS32    /* On o32 the caller has to create the stackframe */
169                 "       subu    $sp, 32         \n"
170 #endif
171                 "       move    $4, %3          \n"
172                 "       jalr    %4              \n"
173                 "       move    $4, $2          \n"
174                 "       li      $2, %2          \n"
175                 "       syscall                 \n"
176 #ifdef CONFIG_MIPS32    /* On o32 the caller has to deallocate the stackframe */
177                 "       addiu   $sp, 32         \n"
178 #endif
179                 "1:     move    %0, $2"
180                 : "=r" (retval)
181                 : "i" (__NR_clone), "i" (__NR_exit), "r" (arg), "r" (fn),
182                   "r" (flags | CLONE_VM | CLONE_UNTRACED)
183                  /*
184                   * The called subroutine might have destroyed any of the
185                   * at, result, argument or temporary registers ...
186                   */
187                 : "$2", "$3", "$4", "$5", "$6", "$7", "$8",
188                   "$9","$10","$11","$12","$13","$14","$15","$24","$25","$31");
189
190         return retval;
191 }
192
193 struct mips_frame_info {
194         int frame_offset;
195         int pc_offset;
196 };
197 static struct mips_frame_info schedule_frame;
198 static struct mips_frame_info schedule_timeout_frame;
199 static struct mips_frame_info sleep_on_frame;
200 static struct mips_frame_info sleep_on_timeout_frame;
201 static struct mips_frame_info wait_for_completion_frame;
202 static int mips_frame_info_initialized;
203 static int __init get_frame_info(struct mips_frame_info *info, void *func)
204 {
205         int i;
206         union mips_instruction *ip = (union mips_instruction *)func;
207         info->pc_offset = -1;
208         info->frame_offset = -1;
209         for (i = 0; i < 128; i++, ip++) {
210                 /* if jal, jalr, jr, stop. */
211                 if (ip->j_format.opcode == jal_op ||
212                     (ip->r_format.opcode == spec_op &&
213                      (ip->r_format.func == jalr_op ||
214                       ip->r_format.func == jr_op)))
215                         break;
216
217                 if (
218 #ifdef CONFIG_MIPS32
219                     ip->i_format.opcode == sw_op &&
220 #endif
221 #ifdef CONFIG_MIPS64
222                     ip->i_format.opcode == sd_op &&
223 #endif
224                     ip->i_format.rs == 29)
225                 {
226                         /* sw / sd $ra, offset($sp) */
227                         if (ip->i_format.rt == 31) {
228                                 if (info->pc_offset != -1)
229                                         break;
230                                 info->pc_offset =
231                                         ip->i_format.simmediate / sizeof(long);
232                         }
233                         /* sw / sd $s8, offset($sp) */
234                         if (ip->i_format.rt == 30) {
235                                 if (info->frame_offset != -1)
236                                         break;
237                                 info->frame_offset =
238                                         ip->i_format.simmediate / sizeof(long);
239                         }
240                 }
241         }
242         if (info->pc_offset == -1 || info->frame_offset == -1) {
243                 printk("Can't analyze prologue code at %p\n", func);
244                 info->pc_offset = -1;
245                 info->frame_offset = -1;
246                 return -1;
247         }
248
249         return 0;
250 }
251
252 static int __init frame_info_init(void)
253 {
254         mips_frame_info_initialized =
255                 !get_frame_info(&schedule_frame, schedule) &&
256                 !get_frame_info(&schedule_timeout_frame, schedule_timeout) &&
257                 !get_frame_info(&sleep_on_frame, sleep_on) &&
258                 !get_frame_info(&sleep_on_timeout_frame, sleep_on_timeout) &&
259                 !get_frame_info(&wait_for_completion_frame, wait_for_completion);
260
261         return 0;
262 }
263
264 arch_initcall(frame_info_init);
265
266 /*
267  * Return saved PC of a blocked thread.
268  */
269 unsigned long thread_saved_pc(struct task_struct *tsk)
270 {
271         extern void ret_from_fork(void);
272         struct thread_struct *t = &tsk->thread;
273
274         /* New born processes are a special case */
275         if (t->reg31 == (unsigned long) ret_from_fork)
276                 return t->reg31;
277
278         if (schedule_frame.pc_offset < 0)
279                 return 0;
280         return ((unsigned long *)t->reg29)[schedule_frame.pc_offset];
281 }
282
283 /* get_wchan - a maintenance nightmare^W^Wpain in the ass ...  */
284 unsigned long get_wchan(struct task_struct *p)
285 {
286         unsigned long frame, pc;
287
288         if (!p || p == current || p->state == TASK_RUNNING)
289                 return 0;
290
291         if (!mips_frame_info_initialized)
292                 return 0;
293         pc = thread_saved_pc(p);
294         if (!in_sched_functions(pc))
295                 goto out;
296
297         if (pc >= (unsigned long) sleep_on_timeout)
298                 goto schedule_timeout_caller;
299         if (pc >= (unsigned long) sleep_on)
300                 goto schedule_caller;
301         if (pc >= (unsigned long) interruptible_sleep_on_timeout)
302                 goto schedule_timeout_caller;
303         if (pc >= (unsigned long)interruptible_sleep_on)
304                 goto schedule_caller;
305         if (pc >= (unsigned long)wait_for_completion)
306                 goto schedule_caller;
307         goto schedule_timeout_caller;
308
309 schedule_caller:
310         frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
311         if (pc >= (unsigned long) sleep_on)
312                 pc = ((unsigned long *)frame)[sleep_on_frame.pc_offset];
313         else
314                 pc = ((unsigned long *)frame)[wait_for_completion_frame.pc_offset];
315         goto out;
316
317 schedule_timeout_caller:
318         /*
319          * The schedule_timeout frame
320          */
321         frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
322
323         /*
324          * frame now points to sleep_on_timeout's frame
325          */
326         pc    = ((unsigned long *)frame)[schedule_timeout_frame.pc_offset];
327
328         if (in_sched_functions(pc)) {
329                 /* schedule_timeout called by [interruptible_]sleep_on_timeout */
330                 frame = ((unsigned long *)frame)[schedule_timeout_frame.frame_offset];
331                 pc    = ((unsigned long *)frame)[sleep_on_timeout_frame.pc_offset];
332         }
333
334 out:
335
336 #ifdef CONFIG_MIPS64
337         if (current->thread.mflags & MF_32BIT_REGS) /* Kludge for 32-bit ps  */
338                 pc &= 0xffffffffUL;
339 #endif
340
341         return pc;
342 }
343
344 EXPORT_SYMBOL(get_wchan);