File: /usr/src/linux/arch/mips64/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/errno.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/stddef.h>
14 #include <linux/unistd.h>
15 #include <linux/ptrace.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
22 #include <asm/bootinfo.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/mipsregs.h>
26 #include <asm/processor.h>
27 #include <asm/stackframe.h>
28 #include <asm/uaccess.h>
29 #include <asm/io.h>
30 #include <asm/elf.h>
31
32 asmlinkage int cpu_idle(void)
33 {
34 /* endless idle loop with no priority at all */
35 init_idle();
36 current->nice = 20;
37 current->counter = -100;
38 while (1) {
39 while (!current->need_resched)
40 if (wait_available)
41 __asm__("wait");
42 schedule();
43 check_pgt_cache();
44 }
45 }
46
47 struct task_struct *last_task_used_math = NULL;
48
49 asmlinkage void ret_from_fork(void);
50
51 void exit_thread(void)
52 {
53 /* Forget lazy fpu state */
54 if (IS_FPU_OWNER()) {
55 set_cp0_status(ST0_CU1, ST0_CU1);
56 __asm__ __volatile__("cfc1\t$0,$31");
57 CLEAR_FPU_OWNER();
58 }
59 }
60
61 void flush_thread(void)
62 {
63 /* Forget lazy fpu state */
64 if (IS_FPU_OWNER()) {
65 set_cp0_status(ST0_CU1, ST0_CU1);
66 __asm__ __volatile__("cfc1\t$0,$31");
67 CLEAR_FPU_OWNER();
68 }
69 }
70
71 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
72 unsigned long unused,
73 struct task_struct * p, struct pt_regs * regs)
74 {
75 struct pt_regs * childregs;
76 long childksp;
77
78 childksp = (unsigned long)p + KERNEL_STACK_SIZE - 32;
79
80 if (IS_FPU_OWNER()) {
81 save_fp(p);
82 }
83 /* set up new TSS. */
84 childregs = (struct pt_regs *) childksp - 1;
85 *childregs = *regs;
86 childregs->regs[7] = 0; /* Clear error flag */
87 if (current->personality == PER_LINUX) {
88 childregs->regs[2] = 0; /* Child gets zero as return value */
89 regs->regs[2] = p->pid;
90 } else {
91 /* Under IRIX things are a little different. */
92 childregs->regs[2] = 0;
93 childregs->regs[3] = 1;
94 regs->regs[2] = p->pid;
95 regs->regs[3] = 0;
96 }
97 if (childregs->cp0_status & ST0_CU0) {
98 childregs->regs[28] = (unsigned long) p;
99 childregs->regs[29] = childksp;
100 p->thread.current_ds = KERNEL_DS;
101 } else {
102 childregs->regs[29] = usp;
103 p->thread.current_ds = USER_DS;
104 }
105 p->thread.reg29 = (unsigned long) childregs;
106 p->thread.reg31 = (unsigned long) ret_from_fork;
107
108 /*
109 * New tasks loose permission to use the fpu. This accelerates context
110 * switching for most programs since they don't use the fpu.
111 */
112 p->thread.cp0_status = read_32bit_cp0_register(CP0_STATUS) &
113 ~(ST0_CU3|ST0_CU2|ST0_CU1|ST0_KSU);
114 childregs->cp0_status &= ~(ST0_CU3|ST0_CU2|ST0_CU1);
115
116 return 0;
117 }
118
119 /* Fill in the fpu structure for a core dump.. */
120 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
121 {
122 /* We actually store the FPU info in the task->thread
123 * area.
124 */
125 if(regs->cp0_status & ST0_CU1) {
126 memcpy(r, ¤t->thread.fpu, sizeof(current->thread.fpu));
127 return 1;
128 }
129 return 0; /* Task didn't use the fpu at all. */
130 }
131
132 /* Fill in the user structure for a core dump.. */
133 void dump_thread(struct pt_regs *regs, struct user *dump)
134 {
135 dump->magic = CMAGIC;
136 dump->start_code = current->mm->start_code;
137 dump->start_data = current->mm->start_data;
138 dump->start_stack = regs->regs[29] & ~(PAGE_SIZE - 1);
139 dump->u_tsize = (current->mm->end_code - dump->start_code)
140 >> PAGE_SHIFT;
141 dump->u_dsize = (current->mm->brk + (PAGE_SIZE - 1) - dump->start_data)
142 >> PAGE_SHIFT;
143 dump->u_ssize = (current->mm->start_stack - dump->start_stack +
144 PAGE_SIZE - 1) >> PAGE_SHIFT;
145 memcpy(&dump->regs[0], regs, sizeof(struct pt_regs));
146 memcpy(&dump->regs[EF_SIZE/4], ¤t->thread.fpu,
147 sizeof(current->thread.fpu));
148 }
149
150 /*
151 * Create a kernel thread
152 */
153 int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
154 {
155 int retval;
156
157 __asm__ __volatile__(
158 "move\t$6, $sp\n\t"
159 "move\t$4, %5\n\t"
160 "li\t$2, %1\n\t"
161 "syscall\n\t"
162 "beq\t$6, $sp, 1f\n\t"
163 "move\t$4, %3\n\t"
164 "jalr\t%4\n\t"
165 "move\t$4, $2\n\t"
166 "li\t$2, %2\n\t"
167 "syscall\n"
168 "1:\tmove\t%0, $2"
169 :"=r" (retval)
170 :"i" (__NR_clone), "i" (__NR_exit), "r" (arg), "r" (fn),
171 "r" (flags | CLONE_VM)
172
173 /* The called subroutine might have destroyed any of the
174 * at, result, argument or temporary registers ... */
175 :"$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8",
176 "$9","$10","$11","$12","$13","$14","$15","$24","$25");
177
178 return retval;
179 }
180
181 /*
182 * These bracket the sleeping functions..
183 */
184 extern void scheduling_functions_start_here(void);
185 extern void scheduling_functions_end_here(void);
186 #define first_sched ((unsigned long) scheduling_functions_start_here)
187 #define last_sched ((unsigned long) scheduling_functions_end_here)
188
189 /* get_wchan - a maintenance nightmare ... */
190 unsigned long get_wchan(struct task_struct *p)
191 {
192 unsigned long frame, pc;
193
194 if (!p || p == current || p->state == TASK_RUNNING)
195 return 0;
196
197 pc = thread_saved_pc(&p->thread);
198 if (pc < first_sched || pc >= last_sched)
199 goto out;
200
201 if (pc >= (unsigned long) sleep_on_timeout)
202 goto schedule_timeout_caller;
203 if (pc >= (unsigned long) sleep_on)
204 goto schedule_caller;
205 if (pc >= (unsigned long) interruptible_sleep_on_timeout)
206 goto schedule_timeout_caller;
207 if (pc >= (unsigned long)interruptible_sleep_on)
208 goto schedule_caller;
209 goto schedule_timeout_caller;
210
211 schedule_caller:
212 frame = ((unsigned long *)p->thread.reg30)[10];
213 pc = ((unsigned long *)frame)[7];
214 goto out;
215
216 schedule_timeout_caller:
217 /* Must be schedule_timeout ... */
218 pc = ((unsigned long *)p->thread.reg30)[11];
219 frame = ((unsigned long *)p->thread.reg30)[10];
220
221 /* The schedule_timeout frame ... */
222 pc = ((unsigned long *)frame)[9];
223 frame = ((unsigned long *)frame)[8];
224
225 if (pc >= first_sched && pc < last_sched) {
226 /* schedule_timeout called by interruptible_sleep_on_timeout */
227 pc = ((unsigned long *)frame)[7];
228 frame = ((unsigned long *)frame)[6];
229 }
230
231 out:
232 if (current->thread.mflags & MF_32BIT) /* Kludge for 32-bit ps */
233 pc &= 0xffffffff;
234
235 return pc;
236 }
237