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