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
6 * Copyright (C) 1994 - 1999, 2000 by Ralf Baechle and others.
7 * Copyright (C) 2005, 2006 by Ralf Baechle (ralf@linux-mips.org)
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 * Copyright (C) 2004 Thiemo Seufer
10 * Copyright (C) 2013 Imagination Technologies Ltd.
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/tick.h>
15 #include <linux/kernel.h>
17 #include <linux/stddef.h>
18 #include <linux/unistd.h>
19 #include <linux/export.h>
20 #include <linux/ptrace.h>
21 #include <linux/mman.h>
22 #include <linux/personality.h>
23 #include <linux/sys.h>
24 #include <linux/user.h>
25 #include <linux/init.h>
26 #include <linux/completion.h>
27 #include <linux/kallsyms.h>
28 #include <linux/random.h>
31 #include <asm/bootinfo.h>
36 #include <asm/pgtable.h>
37 #include <asm/mipsregs.h>
38 #include <asm/processor.h>
39 #include <asm/uaccess.h>
42 #include <asm/isadep.h>
44 #include <asm/stacktrace.h>
46 #ifdef CONFIG_HOTPLUG_CPU
47 void arch_cpu_idle_dead(void)
49 /* What the heck is this check doing ? */
50 if (!cpu_isset(smp_processor_id(), cpu_callin_map))
55 asmlinkage void ret_from_fork(void);
56 asmlinkage void ret_from_kernel_thread(void);
58 void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
62 /* New thread loses kernel privileges. */
63 status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|ST0_FR|KU_MASK);
65 regs->cp0_status = status;
69 clear_thread_flag(TIF_MSA_CTX_LIVE);
75 void exit_thread(void)
79 void flush_thread(void)
83 int copy_thread(unsigned long clone_flags, unsigned long usp,
84 unsigned long arg, struct task_struct *p)
86 struct thread_info *ti = task_thread_info(p);
87 struct pt_regs *childregs, *regs = current_pt_regs();
88 unsigned long childksp;
89 p->set_child_tid = p->clear_child_tid = NULL;
91 childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
97 else if (is_fpu_owner())
105 /* set up new TSS. */
106 childregs = (struct pt_regs *) childksp - 1;
107 /* Put the stack after the struct pt_regs. */
108 childksp = (unsigned long) childregs;
109 p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
110 if (unlikely(p->flags & PF_KTHREAD)) {
111 unsigned long status = p->thread.cp0_status;
112 memset(childregs, 0, sizeof(struct pt_regs));
113 ti->addr_limit = KERNEL_DS;
114 p->thread.reg16 = usp; /* fn */
115 p->thread.reg17 = arg;
116 p->thread.reg29 = childksp;
117 p->thread.reg31 = (unsigned long) ret_from_kernel_thread;
118 #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
119 status = (status & ~(ST0_KUP | ST0_IEP | ST0_IEC)) |
120 ((status & (ST0_KUC | ST0_IEC)) << 2);
124 childregs->cp0_status = status;
128 childregs->regs[7] = 0; /* Clear error flag */
129 childregs->regs[2] = 0; /* Child gets zero as return value */
131 childregs->regs[29] = usp;
132 ti->addr_limit = USER_DS;
134 p->thread.reg29 = (unsigned long) childregs;
135 p->thread.reg31 = (unsigned long) ret_from_fork;
138 * New tasks lose permission to use the fpu. This accelerates context
139 * switching for most programs since they don't use the fpu.
141 childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
143 clear_tsk_thread_flag(p, TIF_USEDFPU);
145 #ifdef CONFIG_MIPS_MT_FPAFF
146 clear_tsk_thread_flag(p, TIF_FPUBOUND);
147 #endif /* CONFIG_MIPS_MT_FPAFF */
149 if (clone_flags & CLONE_SETTLS)
150 ti->tp_value = regs->regs[7];
155 #ifdef CONFIG_CC_STACKPROTECTOR
156 #include <linux/stackprotector.h>
157 unsigned long __stack_chk_guard __read_mostly;
158 EXPORT_SYMBOL(__stack_chk_guard);
161 struct mips_frame_info {
163 unsigned long func_size;
168 #define J_TARGET(pc,target) \
169 (((unsigned long)(pc) & 0xf0000000) | ((target) << 2))
171 static inline int is_ra_save_ins(union mips_instruction *ip)
173 #ifdef CONFIG_CPU_MICROMIPS
174 union mips_instruction mmi;
178 * swm16 reglist,offset(sp)
179 * swm32 reglist,offset(sp)
181 * jradiussp - NOT SUPPORTED
183 * microMIPS is way more fun...
185 if (mm_insn_16bit(ip->halfword[0])) {
186 mmi.word = (ip->halfword[0] << 16);
187 return ((mmi.mm16_r5_format.opcode == mm_swsp16_op &&
188 mmi.mm16_r5_format.rt == 31) ||
189 (mmi.mm16_m_format.opcode == mm_pool16c_op &&
190 mmi.mm16_m_format.func == mm_swm16_op));
193 mmi.halfword[0] = ip->halfword[1];
194 mmi.halfword[1] = ip->halfword[0];
195 return ((mmi.mm_m_format.opcode == mm_pool32b_op &&
196 mmi.mm_m_format.rd > 9 &&
197 mmi.mm_m_format.base == 29 &&
198 mmi.mm_m_format.func == mm_swm32_func) ||
199 (mmi.i_format.opcode == mm_sw32_op &&
200 mmi.i_format.rs == 29 &&
201 mmi.i_format.rt == 31));
204 /* sw / sd $ra, offset($sp) */
205 return (ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
206 ip->i_format.rs == 29 &&
207 ip->i_format.rt == 31;
211 static inline int is_jump_ins(union mips_instruction *ip)
213 #ifdef CONFIG_CPU_MICROMIPS
215 * jr16,jrc,jalr16,jalr16
217 * jalr/jr,jalr.hb/jr.hb,jalrs,jalrs.hb
218 * jraddiusp - NOT SUPPORTED
220 * microMIPS is kind of more fun...
222 union mips_instruction mmi;
224 mmi.word = (ip->halfword[0] << 16);
226 if ((mmi.mm16_r5_format.opcode == mm_pool16c_op &&
227 (mmi.mm16_r5_format.rt & mm_jr16_op) == mm_jr16_op) ||
228 ip->j_format.opcode == mm_jal32_op)
230 if (ip->r_format.opcode != mm_pool32a_op ||
231 ip->r_format.func != mm_pool32axf_op)
233 return (((ip->u_format.uimmediate >> 6) & mm_jalr_op) == mm_jalr_op);
235 if (ip->j_format.opcode == j_op)
237 if (ip->j_format.opcode == jal_op)
239 if (ip->r_format.opcode != spec_op)
241 return ip->r_format.func == jalr_op || ip->r_format.func == jr_op;
245 static inline int is_sp_move_ins(union mips_instruction *ip)
247 #ifdef CONFIG_CPU_MICROMIPS
252 * jradiussp - NOT SUPPORTED
254 * microMIPS is not more fun...
256 if (mm_insn_16bit(ip->halfword[0])) {
257 union mips_instruction mmi;
259 mmi.word = (ip->halfword[0] << 16);
260 return ((mmi.mm16_r3_format.opcode == mm_pool16d_op &&
261 mmi.mm16_r3_format.simmediate && mm_addiusp_func) ||
262 (mmi.mm16_r5_format.opcode == mm_pool16d_op &&
263 mmi.mm16_r5_format.rt == 29));
265 return (ip->mm_i_format.opcode == mm_addiu32_op &&
266 ip->mm_i_format.rt == 29 && ip->mm_i_format.rs == 29);
268 /* addiu/daddiu sp,sp,-imm */
269 if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
271 if (ip->i_format.opcode == addiu_op || ip->i_format.opcode == daddiu_op)
277 static int get_frame_info(struct mips_frame_info *info)
279 #ifdef CONFIG_CPU_MICROMIPS
280 union mips_instruction *ip = (void *) (((char *) info->func) - 1);
282 union mips_instruction *ip = info->func;
284 unsigned max_insns = info->func_size / sizeof(union mips_instruction);
287 info->pc_offset = -1;
288 info->frame_size = 0;
294 max_insns = 128U; /* unknown function size */
295 max_insns = min(128U, max_insns);
297 for (i = 0; i < max_insns; i++, ip++) {
301 if (!info->frame_size) {
302 if (is_sp_move_ins(ip))
304 #ifdef CONFIG_CPU_MICROMIPS
305 if (mm_insn_16bit(ip->halfword[0]))
309 if (ip->halfword[0] & mm_addiusp_func)
311 tmp = (((ip->halfword[0] >> 1) & 0x1ff) << 2);
312 info->frame_size = -(signed short)(tmp | ((tmp & 0x100) ? 0xfe00 : 0));
314 tmp = (ip->halfword[0] >> 1);
315 info->frame_size = -(signed short)(tmp & 0xf);
317 ip = (void *) &ip->halfword[1];
321 info->frame_size = - ip->i_format.simmediate;
325 if (info->pc_offset == -1 && is_ra_save_ins(ip)) {
327 ip->i_format.simmediate / sizeof(long);
331 if (info->frame_size && info->pc_offset >= 0) /* nested */
333 if (info->pc_offset < 0) /* leaf */
335 /* prologue seems boggus... */
340 static struct mips_frame_info schedule_mfi __read_mostly;
342 #ifdef CONFIG_KALLSYMS
343 static unsigned long get___schedule_addr(void)
345 return kallsyms_lookup_name("__schedule");
348 static unsigned long get___schedule_addr(void)
350 union mips_instruction *ip = (void *)schedule;
354 for (i = 0; i < max_insns; i++, ip++) {
355 if (ip->j_format.opcode == j_op)
356 return J_TARGET(ip, ip->j_format.target);
362 static int __init frame_info_init(void)
364 unsigned long size = 0;
365 #ifdef CONFIG_KALLSYMS
370 addr = get___schedule_addr();
372 addr = (unsigned long)schedule;
374 #ifdef CONFIG_KALLSYMS
375 kallsyms_lookup_size_offset(addr, &size, &ofs);
377 schedule_mfi.func = (void *)addr;
378 schedule_mfi.func_size = size;
380 get_frame_info(&schedule_mfi);
383 * Without schedule() frame info, result given by
384 * thread_saved_pc() and get_wchan() are not reliable.
386 if (schedule_mfi.pc_offset < 0)
387 printk("Can't analyze schedule() prologue at %p\n", schedule);
392 arch_initcall(frame_info_init);
395 * Return saved PC of a blocked thread.
397 unsigned long thread_saved_pc(struct task_struct *tsk)
399 struct thread_struct *t = &tsk->thread;
401 /* New born processes are a special case */
402 if (t->reg31 == (unsigned long) ret_from_fork)
404 if (schedule_mfi.pc_offset < 0)
406 return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
410 #ifdef CONFIG_KALLSYMS
411 /* generic stack unwinding function */
412 unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
417 struct mips_frame_info info;
418 unsigned long size, ofs;
420 extern void ret_from_irq(void);
421 extern void ret_from_exception(void);
427 * If we reached the bottom of interrupt context,
428 * return saved pc in pt_regs.
430 if (pc == (unsigned long)ret_from_irq ||
431 pc == (unsigned long)ret_from_exception) {
432 struct pt_regs *regs;
433 if (*sp >= stack_page &&
434 *sp + sizeof(*regs) <= stack_page + THREAD_SIZE - 32) {
435 regs = (struct pt_regs *)*sp;
437 if (__kernel_text_address(pc)) {
438 *sp = regs->regs[29];
439 *ra = regs->regs[31];
445 if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
448 * Return ra if an exception occurred at the first instruction
450 if (unlikely(ofs == 0)) {
456 info.func = (void *)(pc - ofs);
457 info.func_size = ofs; /* analyze from start to ofs */
458 leaf = get_frame_info(&info);
462 if (*sp < stack_page ||
463 *sp + info.frame_size > stack_page + THREAD_SIZE - 32)
468 * For some extreme cases, get_frame_info() can
469 * consider wrongly a nested function as a leaf
470 * one. In that cases avoid to return always the
473 pc = pc != *ra ? *ra : 0;
475 pc = ((unsigned long *)(*sp))[info.pc_offset];
477 *sp += info.frame_size;
479 return __kernel_text_address(pc) ? pc : 0;
481 EXPORT_SYMBOL(unwind_stack_by_address);
483 /* used by show_backtrace() */
484 unsigned long unwind_stack(struct task_struct *task, unsigned long *sp,
485 unsigned long pc, unsigned long *ra)
487 unsigned long stack_page = (unsigned long)task_stack_page(task);
488 return unwind_stack_by_address(stack_page, sp, pc, ra);
493 * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
495 unsigned long get_wchan(struct task_struct *task)
497 unsigned long pc = 0;
498 #ifdef CONFIG_KALLSYMS
500 unsigned long ra = 0;
503 if (!task || task == current || task->state == TASK_RUNNING)
505 if (!task_stack_page(task))
508 pc = thread_saved_pc(task);
510 #ifdef CONFIG_KALLSYMS
511 sp = task->thread.reg29 + schedule_mfi.frame_size;
513 while (in_sched_functions(pc))
514 pc = unwind_stack(task, &sp, pc, &ra);
522 * Don't forget that the stack pointer must be aligned on a 8 bytes
523 * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
525 unsigned long arch_align_stack(unsigned long sp)
527 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
528 sp -= get_random_int() & ~PAGE_MASK;