Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
40ac5d47 | 7 | * Copyright (C) 2005, 2006 by Ralf Baechle (ralf@linux-mips.org) |
1da177e4 LT |
8 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc. |
9 | * Copyright (C) 2004 Thiemo Seufer | |
10 | */ | |
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> | |
16 | #include <linux/mm.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> | |
28 | ||
e50c0a8f | 29 | #include <asm/abi.h> |
1da177e4 LT |
30 | #include <asm/bootinfo.h> |
31 | #include <asm/cpu.h> | |
e50c0a8f | 32 | #include <asm/dsp.h> |
1da177e4 LT |
33 | #include <asm/fpu.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> | |
39 | #include <asm/io.h> | |
40 | #include <asm/elf.h> | |
41 | #include <asm/isadep.h> | |
42 | #include <asm/inst.h> | |
43 | ||
1da177e4 LT |
44 | /* |
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) | |
48 | */ | |
49 | ATTRIB_NORET void cpu_idle(void) | |
50 | { | |
51 | /* endless idle loop with no priority at all */ | |
52 | while (1) { | |
53 | while (!need_resched()) | |
54 | if (cpu_wait) | |
55 | (*cpu_wait)(); | |
5bfb5d69 | 56 | preempt_enable_no_resched(); |
1da177e4 | 57 | schedule(); |
5bfb5d69 | 58 | preempt_disable(); |
1da177e4 LT |
59 | } |
60 | } | |
61 | ||
40ac5d47 RB |
62 | extern void do_signal(struct pt_regs *regs); |
63 | extern void do_signal32(struct pt_regs *regs); | |
e50c0a8f RB |
64 | |
65 | /* | |
66 | * Native o32 and N64 ABI without DSP ASE | |
67 | */ | |
129bc8f7 | 68 | extern int setup_frame(struct k_sigaction * ka, struct pt_regs *regs, |
e50c0a8f | 69 | int signr, sigset_t *set); |
129bc8f7 | 70 | extern int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs, |
e50c0a8f RB |
71 | int signr, sigset_t *set, siginfo_t *info); |
72 | ||
73 | struct mips_abi mips_abi = { | |
74 | .do_signal = do_signal, | |
75 | #ifdef CONFIG_TRAD_SIGNALS | |
76 | .setup_frame = setup_frame, | |
77 | #endif | |
78 | .setup_rt_frame = setup_rt_frame | |
79 | }; | |
80 | ||
81 | #ifdef CONFIG_MIPS32_O32 | |
82 | /* | |
83 | * o32 compatibility on 64-bit kernels, without DSP ASE | |
84 | */ | |
129bc8f7 | 85 | extern int setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs, |
e50c0a8f | 86 | int signr, sigset_t *set); |
129bc8f7 | 87 | extern int setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs, |
e50c0a8f RB |
88 | int signr, sigset_t *set, siginfo_t *info); |
89 | ||
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 | |
94 | }; | |
95 | #endif /* CONFIG_MIPS32_O32 */ | |
96 | ||
97 | #ifdef CONFIG_MIPS32_N32 | |
98 | /* | |
99 | * N32 on 64-bit kernels, without DSP ASE | |
100 | */ | |
129bc8f7 | 101 | extern int setup_rt_frame_n32(struct k_sigaction * ka, struct pt_regs *regs, |
e50c0a8f RB |
102 | int signr, sigset_t *set, siginfo_t *info); |
103 | ||
104 | struct mips_abi mips_abi_n32 = { | |
105 | .do_signal = do_signal, | |
106 | .setup_rt_frame = setup_rt_frame_n32 | |
107 | }; | |
108 | #endif /* CONFIG_MIPS32_N32 */ | |
109 | ||
1da177e4 LT |
110 | asmlinkage void ret_from_fork(void); |
111 | ||
112 | void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp) | |
113 | { | |
114 | unsigned long status; | |
115 | ||
116 | /* New thread loses kernel privileges. */ | |
117 | status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK); | |
875d43e7 | 118 | #ifdef CONFIG_64BIT |
1da177e4 LT |
119 | status &= ~ST0_FR; |
120 | status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR; | |
121 | #endif | |
122 | status |= KU_USER; | |
123 | regs->cp0_status = status; | |
124 | clear_used_math(); | |
125 | lose_fpu(); | |
e50c0a8f RB |
126 | if (cpu_has_dsp) |
127 | __init_dsp(); | |
1da177e4 LT |
128 | regs->cp0_epc = pc; |
129 | regs->regs[29] = sp; | |
130 | current_thread_info()->addr_limit = USER_DS; | |
131 | } | |
132 | ||
133 | void exit_thread(void) | |
134 | { | |
135 | } | |
136 | ||
137 | void flush_thread(void) | |
138 | { | |
139 | } | |
140 | ||
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) | |
143 | { | |
75bb07e7 | 144 | struct thread_info *ti = task_thread_info(p); |
1da177e4 LT |
145 | struct pt_regs *childregs; |
146 | long childksp; | |
3c37026d | 147 | p->set_child_tid = p->clear_child_tid = NULL; |
1da177e4 | 148 | |
75bb07e7 | 149 | childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32; |
1da177e4 LT |
150 | |
151 | preempt_disable(); | |
152 | ||
e50c0a8f | 153 | if (is_fpu_owner()) |
1da177e4 | 154 | save_fp(p); |
e50c0a8f RB |
155 | |
156 | if (cpu_has_dsp) | |
157 | save_dsp(p); | |
1da177e4 LT |
158 | |
159 | preempt_enable(); | |
160 | ||
161 | /* set up new TSS. */ | |
162 | childregs = (struct pt_regs *) childksp - 1; | |
163 | *childregs = *regs; | |
164 | childregs->regs[7] = 0; /* Clear error flag */ | |
165 | ||
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; | |
170 | regs->regs[3] = 0; | |
171 | } | |
172 | #endif | |
173 | childregs->regs[2] = 0; /* Child gets zero as return value */ | |
174 | regs->regs[2] = p->pid; | |
175 | ||
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; | |
180 | } else { | |
181 | childregs->regs[29] = usp; | |
182 | ti->addr_limit = USER_DS; | |
183 | } | |
184 | p->thread.reg29 = (unsigned long) childregs; | |
185 | p->thread.reg31 = (unsigned long) ret_from_fork; | |
186 | ||
187 | /* | |
188 | * New tasks lose permission to use the fpu. This accelerates context | |
189 | * switching for most programs since they don't use the fpu. | |
190 | */ | |
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); | |
194 | ||
3c37026d RB |
195 | if (clone_flags & CLONE_SETTLS) |
196 | ti->tp_value = regs->regs[7]; | |
197 | ||
1da177e4 LT |
198 | return 0; |
199 | } | |
200 | ||
201 | /* Fill in the fpu structure for a core dump.. */ | |
202 | int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r) | |
203 | { | |
204 | memcpy(r, ¤t->thread.fpu, sizeof(current->thread.fpu)); | |
205 | ||
206 | return 1; | |
207 | } | |
208 | ||
d56efda4 | 209 | void elf_dump_regs(elf_greg_t *gp, struct pt_regs *regs) |
1da177e4 LT |
210 | { |
211 | int i; | |
212 | ||
213 | for (i = 0; i < EF_R0; i++) | |
214 | gp[i] = 0; | |
215 | gp[EF_R0] = 0; | |
216 | for (i = 1; i <= 31; i++) | |
217 | gp[EF_R0 + i] = regs->regs[i]; | |
218 | gp[EF_R26] = 0; | |
219 | gp[EF_R27] = 0; | |
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; | |
226 | #ifdef EF_UNUSED0 | |
227 | gp[EF_UNUSED0] = 0; | |
228 | #endif | |
229 | } | |
230 | ||
71e0e556 RB |
231 | int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs) |
232 | { | |
40bc9c67 | 233 | elf_dump_regs(*regs, task_pt_regs(tsk)); |
71e0e556 RB |
234 | return 1; |
235 | } | |
236 | ||
1da177e4 LT |
237 | int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr) |
238 | { | |
239 | memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu)); | |
240 | ||
241 | return 1; | |
242 | } | |
243 | ||
244 | /* | |
245 | * Create a kernel thread | |
246 | */ | |
247 | ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *)) | |
248 | { | |
249 | do_exit(fn(arg)); | |
250 | } | |
251 | ||
252 | long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) | |
253 | { | |
254 | struct pt_regs regs; | |
255 | ||
256 | memset(®s, 0, sizeof(regs)); | |
257 | ||
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; | |
265 | #else | |
266 | regs.cp0_status |= ST0_EXL; | |
267 | #endif | |
268 | ||
269 | /* Ok, create the new process.. */ | |
270 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL); | |
271 | } | |
272 | ||
dc953df1 TS |
273 | static struct mips_frame_info { |
274 | void *func; | |
275 | int omit_fp; /* compiled without fno-omit-frame-pointer */ | |
1da177e4 LT |
276 | int frame_offset; |
277 | int pc_offset; | |
dc953df1 TS |
278 | } schedule_frame, mfinfo[] = { |
279 | { schedule, 0 }, /* must be first */ | |
280 | /* arch/mips/kernel/semaphore.c */ | |
281 | { __down, 1 }, | |
282 | { __down_interruptible, 1 }, | |
283 | /* kernel/sched.c */ | |
284 | #ifdef CONFIG_PREEMPT | |
285 | { preempt_schedule, 0 }, | |
286 | #endif | |
287 | { wait_for_completion, 0 }, | |
288 | { interruptible_sleep_on, 0 }, | |
289 | { interruptible_sleep_on_timeout, 0 }, | |
290 | { sleep_on, 0 }, | |
291 | { sleep_on_timeout, 0 }, | |
292 | { yield, 0 }, | |
293 | { io_schedule, 0 }, | |
294 | { io_schedule_timeout, 0 }, | |
295 | #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT) | |
296 | { __preempt_spin_lock, 0 }, | |
297 | { __preempt_write_lock, 0 }, | |
298 | #endif | |
299 | /* kernel/timer.c */ | |
300 | { schedule_timeout, 1 }, | |
301 | /* { nanosleep_restart, 1 }, */ | |
302 | /* lib/rwsem-spinlock.c */ | |
303 | { __down_read, 1 }, | |
304 | { __down_write, 1 }, | |
1da177e4 | 305 | }; |
dc953df1 | 306 | |
1da177e4 | 307 | static int mips_frame_info_initialized; |
dc953df1 | 308 | static int __init get_frame_info(struct mips_frame_info *info) |
1da177e4 LT |
309 | { |
310 | int i; | |
dc953df1 | 311 | void *func = info->func; |
1da177e4 LT |
312 | union mips_instruction *ip = (union mips_instruction *)func; |
313 | info->pc_offset = -1; | |
dc953df1 | 314 | info->frame_offset = info->omit_fp ? 0 : -1; |
1da177e4 LT |
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))) | |
321 | break; | |
322 | ||
323 | if ( | |
875d43e7 | 324 | #ifdef CONFIG_32BIT |
1da177e4 LT |
325 | ip->i_format.opcode == sw_op && |
326 | #endif | |
875d43e7 | 327 | #ifdef CONFIG_64BIT |
1da177e4 LT |
328 | ip->i_format.opcode == sd_op && |
329 | #endif | |
330 | ip->i_format.rs == 29) | |
331 | { | |
332 | /* sw / sd $ra, offset($sp) */ | |
333 | if (ip->i_format.rt == 31) { | |
334 | if (info->pc_offset != -1) | |
dc953df1 | 335 | continue; |
1da177e4 LT |
336 | info->pc_offset = |
337 | ip->i_format.simmediate / sizeof(long); | |
338 | } | |
339 | /* sw / sd $s8, offset($sp) */ | |
340 | if (ip->i_format.rt == 30) { | |
dc953df1 | 341 | //#if 0 /* gcc 3.4 does aggressive optimization... */ |
1da177e4 | 342 | if (info->frame_offset != -1) |
dc953df1 TS |
343 | continue; |
344 | //#endif | |
1da177e4 LT |
345 | info->frame_offset = |
346 | ip->i_format.simmediate / sizeof(long); | |
347 | } | |
348 | } | |
349 | } | |
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; | |
354 | return -1; | |
355 | } | |
356 | ||
357 | return 0; | |
358 | } | |
359 | ||
360 | static int __init frame_info_init(void) | |
361 | { | |
dc953df1 TS |
362 | int i, found; |
363 | for (i = 0; i < ARRAY_SIZE(mfinfo); i++) | |
364 | if (get_frame_info(&mfinfo[i])) | |
365 | return -1; | |
366 | schedule_frame = mfinfo[0]; | |
367 | /* bubble sort */ | |
368 | do { | |
369 | struct mips_frame_info tmp; | |
370 | found = 0; | |
371 | for (i = 1; i < ARRAY_SIZE(mfinfo); i++) { | |
372 | if (mfinfo[i-1].func > mfinfo[i].func) { | |
373 | tmp = mfinfo[i]; | |
374 | mfinfo[i] = mfinfo[i-1]; | |
375 | mfinfo[i-1] = tmp; | |
376 | found = 1; | |
377 | } | |
378 | } | |
379 | } while (found); | |
380 | mips_frame_info_initialized = 1; | |
1da177e4 LT |
381 | return 0; |
382 | } | |
383 | ||
384 | arch_initcall(frame_info_init); | |
385 | ||
386 | /* | |
387 | * Return saved PC of a blocked thread. | |
388 | */ | |
389 | unsigned long thread_saved_pc(struct task_struct *tsk) | |
390 | { | |
391 | struct thread_struct *t = &tsk->thread; | |
392 | ||
393 | /* New born processes are a special case */ | |
394 | if (t->reg31 == (unsigned long) ret_from_fork) | |
395 | return t->reg31; | |
396 | ||
397 | if (schedule_frame.pc_offset < 0) | |
398 | return 0; | |
399 | return ((unsigned long *)t->reg29)[schedule_frame.pc_offset]; | |
400 | } | |
401 | ||
402 | /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */ | |
403 | unsigned long get_wchan(struct task_struct *p) | |
404 | { | |
dc953df1 | 405 | unsigned long stack_page; |
1da177e4 LT |
406 | unsigned long frame, pc; |
407 | ||
408 | if (!p || p == current || p->state == TASK_RUNNING) | |
409 | return 0; | |
410 | ||
75bb07e7 | 411 | stack_page = (unsigned long)task_stack_page(p); |
dc953df1 | 412 | if (!stack_page || !mips_frame_info_initialized) |
1da177e4 | 413 | return 0; |
dc953df1 | 414 | |
1da177e4 LT |
415 | pc = thread_saved_pc(p); |
416 | if (!in_sched_functions(pc)) | |
dc953df1 | 417 | return pc; |
1da177e4 | 418 | |
1da177e4 | 419 | frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset]; |
dc953df1 TS |
420 | do { |
421 | int i; | |
1da177e4 | 422 | |
dc953df1 TS |
423 | if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32) |
424 | return 0; | |
1da177e4 | 425 | |
dc953df1 TS |
426 | for (i = ARRAY_SIZE(mfinfo) - 1; i >= 0; i--) { |
427 | if (pc >= (unsigned long) mfinfo[i].func) | |
428 | break; | |
429 | } | |
430 | if (i < 0) | |
431 | break; | |
1da177e4 | 432 | |
dc953df1 TS |
433 | if (mfinfo[i].omit_fp) |
434 | break; | |
435 | pc = ((unsigned long *)frame)[mfinfo[i].pc_offset]; | |
436 | frame = ((unsigned long *)frame)[mfinfo[i].frame_offset]; | |
437 | } while (in_sched_functions(pc)); | |
1da177e4 LT |
438 | |
439 | return pc; | |
440 | } | |
441 | ||
442 | EXPORT_SYMBOL(get_wchan); |