Support the MIPS32 / MIPS64 DSP ASE.
[deliverable/linux.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 * Copyright (C) 2004 Thiemo Seufer
9 */
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>
15 #include <linux/mm.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>
27
28 #include <asm/abi.h>
29 #include <asm/bootinfo.h>
30 #include <asm/cpu.h>
31 #include <asm/dsp.h>
32 #include <asm/fpu.h>
33 #include <asm/pgtable.h>
34 #include <asm/system.h>
35 #include <asm/mipsregs.h>
36 #include <asm/processor.h>
37 #include <asm/uaccess.h>
38 #include <asm/io.h>
39 #include <asm/elf.h>
40 #include <asm/isadep.h>
41 #include <asm/inst.h>
42
43 /*
44 * The idle thread. There's no useful work to be done, so just try to conserve
45 * power and have a low exit latency (ie sit in a loop waiting for somebody to
46 * say that they'd like to reschedule)
47 */
48 ATTRIB_NORET void cpu_idle(void)
49 {
50 /* endless idle loop with no priority at all */
51 while (1) {
52 while (!need_resched())
53 if (cpu_wait)
54 (*cpu_wait)();
55 schedule();
56 }
57 }
58
59 extern int do_signal(sigset_t *oldset, struct pt_regs *regs);
60 extern int do_signal32(sigset_t *oldset, struct pt_regs *regs);
61
62 /*
63 * Native o32 and N64 ABI without DSP ASE
64 */
65 extern void setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
66 int signr, sigset_t *set);
67 extern void setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
68 int signr, sigset_t *set, siginfo_t *info);
69
70 struct mips_abi mips_abi = {
71 .do_signal = do_signal,
72 #ifdef CONFIG_TRAD_SIGNALS
73 .setup_frame = setup_frame,
74 #endif
75 .setup_rt_frame = setup_rt_frame
76 };
77
78 #ifdef CONFIG_MIPS32_O32
79 /*
80 * o32 compatibility on 64-bit kernels, without DSP ASE
81 */
82 extern void setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
83 int signr, sigset_t *set);
84 extern void setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
85 int signr, sigset_t *set, siginfo_t *info);
86
87 struct mips_abi mips_abi_32 = {
88 .do_signal = do_signal32,
89 .setup_frame = setup_frame_32,
90 .setup_rt_frame = setup_rt_frame_32
91 };
92 #endif /* CONFIG_MIPS32_O32 */
93
94 #ifdef CONFIG_MIPS32_N32
95 /*
96 * N32 on 64-bit kernels, without DSP ASE
97 */
98 extern void setup_rt_frame_n32(struct k_sigaction * ka, struct pt_regs *regs,
99 int signr, sigset_t *set, siginfo_t *info);
100
101 struct mips_abi mips_abi_n32 = {
102 .do_signal = do_signal,
103 .setup_rt_frame = setup_rt_frame_n32
104 };
105 #endif /* CONFIG_MIPS32_N32 */
106
107 asmlinkage void ret_from_fork(void);
108
109 void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
110 {
111 unsigned long status;
112
113 /* New thread loses kernel privileges. */
114 status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK);
115 #ifdef CONFIG_64BIT
116 status &= ~ST0_FR;
117 status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR;
118 #endif
119 status |= KU_USER;
120 regs->cp0_status = status;
121 clear_used_math();
122 lose_fpu();
123 if (cpu_has_dsp)
124 __init_dsp();
125 regs->cp0_epc = pc;
126 regs->regs[29] = sp;
127 current_thread_info()->addr_limit = USER_DS;
128 }
129
130 void exit_thread(void)
131 {
132 }
133
134 void flush_thread(void)
135 {
136 }
137
138 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
139 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
140 {
141 struct thread_info *ti = p->thread_info;
142 struct pt_regs *childregs;
143 long childksp;
144 p->set_child_tid = p->clear_child_tid = NULL;
145
146 childksp = (unsigned long)ti + THREAD_SIZE - 32;
147
148 preempt_disable();
149
150 if (is_fpu_owner())
151 save_fp(p);
152
153 if (cpu_has_dsp)
154 save_dsp(p);
155
156 preempt_enable();
157
158 /* set up new TSS. */
159 childregs = (struct pt_regs *) childksp - 1;
160 *childregs = *regs;
161 childregs->regs[7] = 0; /* Clear error flag */
162
163 #if defined(CONFIG_BINFMT_IRIX)
164 if (current->personality != PER_LINUX) {
165 /* Under IRIX things are a little different. */
166 childregs->regs[3] = 1;
167 regs->regs[3] = 0;
168 }
169 #endif
170 childregs->regs[2] = 0; /* Child gets zero as return value */
171 regs->regs[2] = p->pid;
172
173 if (childregs->cp0_status & ST0_CU0) {
174 childregs->regs[28] = (unsigned long) ti;
175 childregs->regs[29] = childksp;
176 ti->addr_limit = KERNEL_DS;
177 } else {
178 childregs->regs[29] = usp;
179 ti->addr_limit = USER_DS;
180 }
181 p->thread.reg29 = (unsigned long) childregs;
182 p->thread.reg31 = (unsigned long) ret_from_fork;
183
184 /*
185 * New tasks lose permission to use the fpu. This accelerates context
186 * switching for most programs since they don't use the fpu.
187 */
188 p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
189 childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
190 clear_tsk_thread_flag(p, TIF_USEDFPU);
191
192 if (clone_flags & CLONE_SETTLS)
193 ti->tp_value = regs->regs[7];
194
195 return 0;
196 }
197
198 /* Fill in the fpu structure for a core dump.. */
199 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
200 {
201 memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
202
203 return 1;
204 }
205
206 void dump_regs(elf_greg_t *gp, struct pt_regs *regs)
207 {
208 int i;
209
210 for (i = 0; i < EF_R0; i++)
211 gp[i] = 0;
212 gp[EF_R0] = 0;
213 for (i = 1; i <= 31; i++)
214 gp[EF_R0 + i] = regs->regs[i];
215 gp[EF_R26] = 0;
216 gp[EF_R27] = 0;
217 gp[EF_LO] = regs->lo;
218 gp[EF_HI] = regs->hi;
219 gp[EF_CP0_EPC] = regs->cp0_epc;
220 gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
221 gp[EF_CP0_STATUS] = regs->cp0_status;
222 gp[EF_CP0_CAUSE] = regs->cp0_cause;
223 #ifdef EF_UNUSED0
224 gp[EF_UNUSED0] = 0;
225 #endif
226 }
227
228 int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs)
229 {
230 struct thread_info *ti = tsk->thread_info;
231 long ksp = (unsigned long)ti + THREAD_SIZE - 32;
232 dump_regs(&(*regs)[0], (struct pt_regs *) ksp - 1);
233 return 1;
234 }
235
236 int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr)
237 {
238 memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
239
240 return 1;
241 }
242
243 /*
244 * Create a kernel thread
245 */
246 ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
247 {
248 do_exit(fn(arg));
249 }
250
251 long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
252 {
253 struct pt_regs regs;
254
255 memset(&regs, 0, sizeof(regs));
256
257 regs.regs[4] = (unsigned long) arg;
258 regs.regs[5] = (unsigned long) fn;
259 regs.cp0_epc = (unsigned long) kernel_thread_helper;
260 regs.cp0_status = read_c0_status();
261 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
262 regs.cp0_status &= ~(ST0_KUP | ST0_IEC);
263 regs.cp0_status |= ST0_IEP;
264 #else
265 regs.cp0_status |= ST0_EXL;
266 #endif
267
268 /* Ok, create the new process.. */
269 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
270 }
271
272 static struct mips_frame_info {
273 void *func;
274 int omit_fp; /* compiled without fno-omit-frame-pointer */
275 int frame_offset;
276 int pc_offset;
277 } schedule_frame, mfinfo[] = {
278 { schedule, 0 }, /* must be first */
279 /* arch/mips/kernel/semaphore.c */
280 { __down, 1 },
281 { __down_interruptible, 1 },
282 /* kernel/sched.c */
283 #ifdef CONFIG_PREEMPT
284 { preempt_schedule, 0 },
285 #endif
286 { wait_for_completion, 0 },
287 { interruptible_sleep_on, 0 },
288 { interruptible_sleep_on_timeout, 0 },
289 { sleep_on, 0 },
290 { sleep_on_timeout, 0 },
291 { yield, 0 },
292 { io_schedule, 0 },
293 { io_schedule_timeout, 0 },
294 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
295 { __preempt_spin_lock, 0 },
296 { __preempt_write_lock, 0 },
297 #endif
298 /* kernel/timer.c */
299 { schedule_timeout, 1 },
300 /* { nanosleep_restart, 1 }, */
301 /* lib/rwsem-spinlock.c */
302 { __down_read, 1 },
303 { __down_write, 1 },
304 };
305
306 static int mips_frame_info_initialized;
307 static int __init get_frame_info(struct mips_frame_info *info)
308 {
309 int i;
310 void *func = info->func;
311 union mips_instruction *ip = (union mips_instruction *)func;
312 info->pc_offset = -1;
313 info->frame_offset = info->omit_fp ? 0 : -1;
314 for (i = 0; i < 128; i++, ip++) {
315 /* if jal, jalr, jr, stop. */
316 if (ip->j_format.opcode == jal_op ||
317 (ip->r_format.opcode == spec_op &&
318 (ip->r_format.func == jalr_op ||
319 ip->r_format.func == jr_op)))
320 break;
321
322 if (
323 #ifdef CONFIG_32BIT
324 ip->i_format.opcode == sw_op &&
325 #endif
326 #ifdef CONFIG_64BIT
327 ip->i_format.opcode == sd_op &&
328 #endif
329 ip->i_format.rs == 29)
330 {
331 /* sw / sd $ra, offset($sp) */
332 if (ip->i_format.rt == 31) {
333 if (info->pc_offset != -1)
334 continue;
335 info->pc_offset =
336 ip->i_format.simmediate / sizeof(long);
337 }
338 /* sw / sd $s8, offset($sp) */
339 if (ip->i_format.rt == 30) {
340 //#if 0 /* gcc 3.4 does aggressive optimization... */
341 if (info->frame_offset != -1)
342 continue;
343 //#endif
344 info->frame_offset =
345 ip->i_format.simmediate / sizeof(long);
346 }
347 }
348 }
349 if (info->pc_offset == -1 || info->frame_offset == -1) {
350 printk("Can't analyze prologue code at %p\n", func);
351 info->pc_offset = -1;
352 info->frame_offset = -1;
353 return -1;
354 }
355
356 return 0;
357 }
358
359 static int __init frame_info_init(void)
360 {
361 int i, found;
362 for (i = 0; i < ARRAY_SIZE(mfinfo); i++)
363 if (get_frame_info(&mfinfo[i]))
364 return -1;
365 schedule_frame = mfinfo[0];
366 /* bubble sort */
367 do {
368 struct mips_frame_info tmp;
369 found = 0;
370 for (i = 1; i < ARRAY_SIZE(mfinfo); i++) {
371 if (mfinfo[i-1].func > mfinfo[i].func) {
372 tmp = mfinfo[i];
373 mfinfo[i] = mfinfo[i-1];
374 mfinfo[i-1] = tmp;
375 found = 1;
376 }
377 }
378 } while (found);
379 mips_frame_info_initialized = 1;
380 return 0;
381 }
382
383 arch_initcall(frame_info_init);
384
385 /*
386 * Return saved PC of a blocked thread.
387 */
388 unsigned long thread_saved_pc(struct task_struct *tsk)
389 {
390 struct thread_struct *t = &tsk->thread;
391
392 /* New born processes are a special case */
393 if (t->reg31 == (unsigned long) ret_from_fork)
394 return t->reg31;
395
396 if (schedule_frame.pc_offset < 0)
397 return 0;
398 return ((unsigned long *)t->reg29)[schedule_frame.pc_offset];
399 }
400
401 /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */
402 unsigned long get_wchan(struct task_struct *p)
403 {
404 unsigned long stack_page;
405 unsigned long frame, pc;
406
407 if (!p || p == current || p->state == TASK_RUNNING)
408 return 0;
409
410 stack_page = (unsigned long)p->thread_info;
411 if (!stack_page || !mips_frame_info_initialized)
412 return 0;
413
414 pc = thread_saved_pc(p);
415 if (!in_sched_functions(pc))
416 return pc;
417
418 frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
419 do {
420 int i;
421
422 if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32)
423 return 0;
424
425 for (i = ARRAY_SIZE(mfinfo) - 1; i >= 0; i--) {
426 if (pc >= (unsigned long) mfinfo[i].func)
427 break;
428 }
429 if (i < 0)
430 break;
431
432 if (mfinfo[i].omit_fp)
433 break;
434 pc = ((unsigned long *)frame)[mfinfo[i].pc_offset];
435 frame = ((unsigned long *)frame)[mfinfo[i].frame_offset];
436 } while (in_sched_functions(pc));
437
438 return pc;
439 }
440
441 EXPORT_SYMBOL(get_wchan);
This page took 0.058798 seconds and 5 git commands to generate.