Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Ralf Baechle | 40ac5d4 | 2006-02-08 13:38:18 +0000 | [diff] [blame] | 7 | * Copyright (C) 2005, 2006 by Ralf Baechle (ralf@linux-mips.org) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc. |
| 9 | * Copyright (C) 2004 Thiemo Seufer |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 10 | * Copyright (C) 2013 Imagination Technologies Ltd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/sched.h> |
Ralf Baechle | 7bcf771 | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 14 | #include <linux/tick.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/mm.h> |
| 17 | #include <linux/stddef.h> |
| 18 | #include <linux/unistd.h> |
Paul Gortmaker | cae39d1 | 2011-07-28 18:46:31 -0400 | [diff] [blame] | 19 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/mman.h> |
| 22 | #include <linux/personality.h> |
| 23 | #include <linux/sys.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/completion.h> |
Atsushi Nemoto | 6307751 | 2006-02-08 01:48:03 +0900 | [diff] [blame] | 26 | #include <linux/kallsyms.h> |
Franck Bui-Huu | 9410910 | 2007-07-19 14:04:21 +0200 | [diff] [blame] | 27 | #include <linux/random.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Franck Bui-Huu | 9410910 | 2007-07-19 14:04:21 +0200 | [diff] [blame] | 29 | #include <asm/asm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/bootinfo.h> |
| 31 | #include <asm/cpu.h> |
Ralf Baechle | e50c0a8 | 2005-05-31 11:49:19 +0000 | [diff] [blame] | 32 | #include <asm/dsp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/fpu.h> |
Paul Burton | 1db1af8 | 2014-01-27 15:23:11 +0000 | [diff] [blame] | 34 | #include <asm/msa.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/pgtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/mipsregs.h> |
| 37 | #include <asm/processor.h> |
Alex Smith | 60be939 | 2014-07-23 14:40:15 +0100 | [diff] [blame^] | 38 | #include <asm/reg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/uaccess.h> |
| 40 | #include <asm/io.h> |
| 41 | #include <asm/elf.h> |
| 42 | #include <asm/isadep.h> |
| 43 | #include <asm/inst.h> |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 44 | #include <asm/stacktrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Ralf Baechle | 1b2bc75 | 2009-06-23 10:00:31 +0100 | [diff] [blame] | 46 | #ifdef CONFIG_HOTPLUG_CPU |
Thomas Gleixner | cdbedc6 | 2013-03-21 22:49:52 +0100 | [diff] [blame] | 47 | void arch_cpu_idle_dead(void) |
| 48 | { |
| 49 | /* What the heck is this check doing ? */ |
| 50 | if (!cpu_isset(smp_processor_id(), cpu_callin_map)) |
| 51 | play_dead(); |
| 52 | } |
Ralf Baechle | 1b2bc75 | 2009-06-23 10:00:31 +0100 | [diff] [blame] | 53 | #endif |
Thomas Gleixner | cdbedc6 | 2013-03-21 22:49:52 +0100 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | asmlinkage void ret_from_fork(void); |
Al Viro | 8f54bca | 2012-10-09 16:27:45 -0400 | [diff] [blame] | 56 | asmlinkage void ret_from_kernel_thread(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp) |
| 59 | { |
| 60 | unsigned long status; |
| 61 | |
| 62 | /* New thread loses kernel privileges. */ |
Chris Dearman | bbaf238 | 2007-12-13 22:42:19 +0000 | [diff] [blame] | 63 | status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|ST0_FR|KU_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | status |= KU_USER; |
| 65 | regs->cp0_status = status; |
| 66 | clear_used_math(); |
Atsushi Nemoto | e04582b | 2006-10-09 00:10:01 +0900 | [diff] [blame] | 67 | clear_fpu_owner(); |
Paul Burton | a3056b1 | 2013-11-19 17:30:38 +0000 | [diff] [blame] | 68 | init_dsp(); |
Paul Burton | 1db1af8 | 2014-01-27 15:23:11 +0000 | [diff] [blame] | 69 | clear_thread_flag(TIF_MSA_CTX_LIVE); |
| 70 | disable_msa(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | regs->cp0_epc = pc; |
| 72 | regs->regs[29] = sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void exit_thread(void) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | void flush_thread(void) |
| 80 | { |
| 81 | } |
| 82 | |
Alexey Dobriyan | 6f2c55b | 2009-04-02 16:56:59 -0700 | [diff] [blame] | 83 | int copy_thread(unsigned long clone_flags, unsigned long usp, |
Al Viro | afa86fc | 2012-10-22 22:51:14 -0400 | [diff] [blame] | 84 | unsigned long arg, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Al Viro | 75bb07e | 2006-01-12 01:06:08 -0800 | [diff] [blame] | 86 | struct thread_info *ti = task_thread_info(p); |
Al Viro | afa86fc | 2012-10-22 22:51:14 -0400 | [diff] [blame] | 87 | struct pt_regs *childregs, *regs = current_pt_regs(); |
David Daney | 484889f | 2009-07-08 10:07:50 -0700 | [diff] [blame] | 88 | unsigned long childksp; |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 89 | p->set_child_tid = p->clear_child_tid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Al Viro | 75bb07e | 2006-01-12 01:06:08 -0800 | [diff] [blame] | 91 | childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | preempt_disable(); |
| 94 | |
Paul Burton | 1db1af8 | 2014-01-27 15:23:11 +0000 | [diff] [blame] | 95 | if (is_msa_enabled()) |
| 96 | save_msa(p); |
| 97 | else if (is_fpu_owner()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | save_fp(p); |
Ralf Baechle | e50c0a8 | 2005-05-31 11:49:19 +0000 | [diff] [blame] | 99 | |
| 100 | if (cpu_has_dsp) |
| 101 | save_dsp(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | preempt_enable(); |
| 104 | |
| 105 | /* set up new TSS. */ |
| 106 | childregs = (struct pt_regs *) childksp - 1; |
David Daney | 484889f | 2009-07-08 10:07:50 -0700 | [diff] [blame] | 107 | /* Put the stack after the struct pt_regs. */ |
| 108 | childksp = (unsigned long) childregs; |
Al Viro | 8f54bca | 2012-10-09 16:27:45 -0400 | [diff] [blame] | 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); |
| 121 | #else |
| 122 | status |= ST0_EXL; |
| 123 | #endif |
| 124 | childregs->cp0_status = status; |
| 125 | return 0; |
| 126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | *childregs = *regs; |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 128 | childregs->regs[7] = 0; /* Clear error flag */ |
| 129 | childregs->regs[2] = 0; /* Child gets zero as return value */ |
Al Viro | 64b3122 | 2012-12-27 11:52:32 -0500 | [diff] [blame] | 130 | if (usp) |
| 131 | childregs->regs[29] = usp; |
Al Viro | 8f54bca | 2012-10-09 16:27:45 -0400 | [diff] [blame] | 132 | ti->addr_limit = USER_DS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | p->thread.reg29 = (unsigned long) childregs; |
| 135 | p->thread.reg31 = (unsigned long) ret_from_fork; |
| 136 | |
| 137 | /* |
| 138 | * New tasks lose permission to use the fpu. This accelerates context |
| 139 | * switching for most programs since they don't use the fpu. |
| 140 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | childregs->cp0_status &= ~(ST0_CU2|ST0_CU1); |
Kevin D. Kissell | 9cc1236 | 2008-09-09 21:33:36 +0200 | [diff] [blame] | 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | clear_tsk_thread_flag(p, TIF_USEDFPU); |
| 144 | |
Ralf Baechle | f088fc8 | 2006-04-05 09:45:47 +0100 | [diff] [blame] | 145 | #ifdef CONFIG_MIPS_MT_FPAFF |
Ralf Baechle | 6657fe0 | 2008-09-09 15:19:10 +0200 | [diff] [blame] | 146 | clear_tsk_thread_flag(p, TIF_FPUBOUND); |
Ralf Baechle | f088fc8 | 2006-04-05 09:45:47 +0100 | [diff] [blame] | 147 | #endif /* CONFIG_MIPS_MT_FPAFF */ |
| 148 | |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 149 | if (clone_flags & CLONE_SETTLS) |
| 150 | ti->tp_value = regs->regs[7]; |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Gregory Fong | 36ecafc | 2013-06-12 17:08:54 +0000 | [diff] [blame] | 155 | #ifdef CONFIG_CC_STACKPROTECTOR |
| 156 | #include <linux/stackprotector.h> |
| 157 | unsigned long __stack_chk_guard __read_mostly; |
| 158 | EXPORT_SYMBOL(__stack_chk_guard); |
| 159 | #endif |
| 160 | |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 161 | struct mips_frame_info { |
| 162 | void *func; |
| 163 | unsigned long func_size; |
| 164 | int frame_size; |
| 165 | int pc_offset; |
| 166 | }; |
Thiemo Seufer | dc953df | 2005-02-21 10:55:16 +0000 | [diff] [blame] | 167 | |
Tony Wu | 5000653e | 2013-05-12 15:05:34 +0000 | [diff] [blame] | 168 | #define J_TARGET(pc,target) \ |
| 169 | (((unsigned long)(pc) & 0xf0000000) | ((target) << 2)) |
| 170 | |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 171 | static inline int is_ra_save_ins(union mips_instruction *ip) |
| 172 | { |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 173 | #ifdef CONFIG_CPU_MICROMIPS |
| 174 | union mips_instruction mmi; |
| 175 | |
| 176 | /* |
| 177 | * swsp ra,offset |
| 178 | * swm16 reglist,offset(sp) |
| 179 | * swm32 reglist,offset(sp) |
| 180 | * sw32 ra,offset(sp) |
| 181 | * jradiussp - NOT SUPPORTED |
| 182 | * |
| 183 | * microMIPS is way more fun... |
| 184 | */ |
| 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)); |
| 191 | } |
| 192 | else { |
| 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)); |
| 202 | } |
| 203 | #else |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 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; |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 208 | #endif |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Tony Wu | e7438c4 | 2013-05-12 15:04:29 +0000 | [diff] [blame] | 211 | static inline int is_jump_ins(union mips_instruction *ip) |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 212 | { |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 213 | #ifdef CONFIG_CPU_MICROMIPS |
| 214 | /* |
| 215 | * jr16,jrc,jalr16,jalr16 |
| 216 | * jal |
| 217 | * jalr/jr,jalr.hb/jr.hb,jalrs,jalrs.hb |
| 218 | * jraddiusp - NOT SUPPORTED |
| 219 | * |
| 220 | * microMIPS is kind of more fun... |
| 221 | */ |
| 222 | union mips_instruction mmi; |
| 223 | |
| 224 | mmi.word = (ip->halfword[0] << 16); |
| 225 | |
| 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) |
| 229 | return 1; |
| 230 | if (ip->r_format.opcode != mm_pool32a_op || |
| 231 | ip->r_format.func != mm_pool32axf_op) |
| 232 | return 0; |
| 233 | return (((ip->u_format.uimmediate >> 6) & mm_jalr_op) == mm_jalr_op); |
| 234 | #else |
Tony Wu | e7438c4 | 2013-05-12 15:04:29 +0000 | [diff] [blame] | 235 | if (ip->j_format.opcode == j_op) |
| 236 | return 1; |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 237 | if (ip->j_format.opcode == jal_op) |
| 238 | return 1; |
| 239 | if (ip->r_format.opcode != spec_op) |
| 240 | return 0; |
| 241 | return ip->r_format.func == jalr_op || ip->r_format.func == jr_op; |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 242 | #endif |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static inline int is_sp_move_ins(union mips_instruction *ip) |
| 246 | { |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 247 | #ifdef CONFIG_CPU_MICROMIPS |
| 248 | /* |
| 249 | * addiusp -imm |
| 250 | * addius5 sp,-imm |
| 251 | * addiu32 sp,sp,-imm |
| 252 | * jradiussp - NOT SUPPORTED |
| 253 | * |
| 254 | * microMIPS is not more fun... |
| 255 | */ |
| 256 | if (mm_insn_16bit(ip->halfword[0])) { |
| 257 | union mips_instruction mmi; |
| 258 | |
| 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)); |
| 264 | } |
| 265 | return (ip->mm_i_format.opcode == mm_addiu32_op && |
| 266 | ip->mm_i_format.rt == 29 && ip->mm_i_format.rs == 29); |
| 267 | #else |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 268 | /* addiu/daddiu sp,sp,-imm */ |
| 269 | if (ip->i_format.rs != 29 || ip->i_format.rt != 29) |
| 270 | return 0; |
| 271 | if (ip->i_format.opcode == addiu_op || ip->i_format.opcode == daddiu_op) |
| 272 | return 1; |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 273 | #endif |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 277 | static int get_frame_info(struct mips_frame_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 279 | #ifdef CONFIG_CPU_MICROMIPS |
| 280 | union mips_instruction *ip = (void *) (((char *) info->func) - 1); |
| 281 | #else |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 282 | union mips_instruction *ip = info->func; |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 283 | #endif |
Franck Bui-Huu | 29b376f | 2006-08-18 16:18:08 +0200 | [diff] [blame] | 284 | unsigned max_insns = info->func_size / sizeof(union mips_instruction); |
| 285 | unsigned i; |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | info->pc_offset = -1; |
Atsushi Nemoto | 6307751 | 2006-02-08 01:48:03 +0900 | [diff] [blame] | 288 | info->frame_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Franck Bui-Huu | 29b376f | 2006-08-18 16:18:08 +0200 | [diff] [blame] | 290 | if (!ip) |
| 291 | goto err; |
| 292 | |
| 293 | if (max_insns == 0) |
| 294 | max_insns = 128U; /* unknown function size */ |
| 295 | max_insns = min(128U, max_insns); |
| 296 | |
Franck Bui-Huu | c0efbb6 | 2006-08-03 09:29:15 +0200 | [diff] [blame] | 297 | for (i = 0; i < max_insns; i++, ip++) { |
| 298 | |
Tony Wu | e7438c4 | 2013-05-12 15:04:29 +0000 | [diff] [blame] | 299 | if (is_jump_ins(ip)) |
Atsushi Nemoto | 6307751 | 2006-02-08 01:48:03 +0900 | [diff] [blame] | 300 | break; |
Franck Bui-Huu | 0cceb4a | 2006-08-03 09:29:20 +0200 | [diff] [blame] | 301 | if (!info->frame_size) { |
| 302 | if (is_sp_move_ins(ip)) |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 303 | { |
| 304 | #ifdef CONFIG_CPU_MICROMIPS |
| 305 | if (mm_insn_16bit(ip->halfword[0])) |
| 306 | { |
| 307 | unsigned short tmp; |
| 308 | |
| 309 | if (ip->halfword[0] & mm_addiusp_func) |
| 310 | { |
| 311 | tmp = (((ip->halfword[0] >> 1) & 0x1ff) << 2); |
| 312 | info->frame_size = -(signed short)(tmp | ((tmp & 0x100) ? 0xfe00 : 0)); |
| 313 | } else { |
| 314 | tmp = (ip->halfword[0] >> 1); |
| 315 | info->frame_size = -(signed short)(tmp & 0xf); |
| 316 | } |
| 317 | ip = (void *) &ip->halfword[1]; |
| 318 | ip--; |
| 319 | } else |
| 320 | #endif |
Franck Bui-Huu | 0cceb4a | 2006-08-03 09:29:20 +0200 | [diff] [blame] | 321 | info->frame_size = - ip->i_format.simmediate; |
Leonid Yegoshin | 34c2f66 | 2013-03-25 13:18:07 -0500 | [diff] [blame] | 322 | } |
Franck Bui-Huu | 0cceb4a | 2006-08-03 09:29:20 +0200 | [diff] [blame] | 323 | continue; |
Atsushi Nemoto | 6307751 | 2006-02-08 01:48:03 +0900 | [diff] [blame] | 324 | } |
Franck Bui-Huu | 0cceb4a | 2006-08-03 09:29:20 +0200 | [diff] [blame] | 325 | if (info->pc_offset == -1 && is_ra_save_ins(ip)) { |
Atsushi Nemoto | 6307751 | 2006-02-08 01:48:03 +0900 | [diff] [blame] | 326 | info->pc_offset = |
| 327 | ip->i_format.simmediate / sizeof(long); |
Franck Bui-Huu | 0cceb4a | 2006-08-03 09:29:20 +0200 | [diff] [blame] | 328 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 331 | if (info->frame_size && info->pc_offset >= 0) /* nested */ |
| 332 | return 0; |
| 333 | if (info->pc_offset < 0) /* leaf */ |
| 334 | return 1; |
| 335 | /* prologue seems boggus... */ |
Franck Bui-Huu | 29b376f | 2006-08-18 16:18:08 +0200 | [diff] [blame] | 336 | err: |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 337 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 340 | static struct mips_frame_info schedule_mfi __read_mostly; |
| 341 | |
Tony Wu | 5000653e | 2013-05-12 15:05:34 +0000 | [diff] [blame] | 342 | #ifdef CONFIG_KALLSYMS |
| 343 | static unsigned long get___schedule_addr(void) |
| 344 | { |
| 345 | return kallsyms_lookup_name("__schedule"); |
| 346 | } |
| 347 | #else |
| 348 | static unsigned long get___schedule_addr(void) |
| 349 | { |
| 350 | union mips_instruction *ip = (void *)schedule; |
| 351 | int max_insns = 8; |
| 352 | int i; |
| 353 | |
| 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); |
| 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | #endif |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | static int __init frame_info_init(void) |
| 363 | { |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 364 | unsigned long size = 0; |
Atsushi Nemoto | 6307751 | 2006-02-08 01:48:03 +0900 | [diff] [blame] | 365 | #ifdef CONFIG_KALLSYMS |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 366 | unsigned long ofs; |
Atsushi Nemoto | 6307751 | 2006-02-08 01:48:03 +0900 | [diff] [blame] | 367 | #endif |
Tony Wu | 5000653e | 2013-05-12 15:05:34 +0000 | [diff] [blame] | 368 | unsigned long addr; |
| 369 | |
| 370 | addr = get___schedule_addr(); |
| 371 | if (!addr) |
| 372 | addr = (unsigned long)schedule; |
| 373 | |
| 374 | #ifdef CONFIG_KALLSYMS |
| 375 | kallsyms_lookup_size_offset(addr, &size, &ofs); |
| 376 | #endif |
| 377 | schedule_mfi.func = (void *)addr; |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 378 | schedule_mfi.func_size = size; |
| 379 | |
| 380 | get_frame_info(&schedule_mfi); |
Franck Bui-Huu | 6057a79 | 2006-08-03 09:29:18 +0200 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * Without schedule() frame info, result given by |
| 384 | * thread_saved_pc() and get_wchan() are not reliable. |
| 385 | */ |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 386 | if (schedule_mfi.pc_offset < 0) |
Franck Bui-Huu | 6057a79 | 2006-08-03 09:29:18 +0200 | [diff] [blame] | 387 | printk("Can't analyze schedule() prologue at %p\n", schedule); |
Atsushi Nemoto | 6307751 | 2006-02-08 01:48:03 +0900 | [diff] [blame] | 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | arch_initcall(frame_info_init); |
| 393 | |
| 394 | /* |
| 395 | * Return saved PC of a blocked thread. |
| 396 | */ |
| 397 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 398 | { |
| 399 | struct thread_struct *t = &tsk->thread; |
| 400 | |
| 401 | /* New born processes are a special case */ |
| 402 | if (t->reg31 == (unsigned long) ret_from_fork) |
| 403 | return t->reg31; |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 404 | if (schedule_mfi.pc_offset < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | return 0; |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 406 | return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 410 | #ifdef CONFIG_KALLSYMS |
Daniel Kalmar | 94ea09c | 2011-05-13 08:38:04 -0400 | [diff] [blame] | 411 | /* generic stack unwinding function */ |
| 412 | unsigned long notrace unwind_stack_by_address(unsigned long stack_page, |
| 413 | unsigned long *sp, |
| 414 | unsigned long pc, |
| 415 | unsigned long *ra) |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 416 | { |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 417 | struct mips_frame_info info; |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 418 | unsigned long size, ofs; |
Franck Bui-Huu | 4d157d5 | 2006-08-03 09:29:21 +0200 | [diff] [blame] | 419 | int leaf; |
Atsushi Nemoto | 1924600 | 2006-09-29 18:02:51 +0900 | [diff] [blame] | 420 | extern void ret_from_irq(void); |
| 421 | extern void ret_from_exception(void); |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 422 | |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 423 | if (!stack_page) |
| 424 | return 0; |
| 425 | |
Atsushi Nemoto | 1924600 | 2006-09-29 18:02:51 +0900 | [diff] [blame] | 426 | /* |
| 427 | * If we reached the bottom of interrupt context, |
| 428 | * return saved pc in pt_regs. |
| 429 | */ |
| 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; |
| 436 | pc = regs->cp0_epc; |
| 437 | if (__kernel_text_address(pc)) { |
| 438 | *sp = regs->regs[29]; |
| 439 | *ra = regs->regs[31]; |
| 440 | return pc; |
| 441 | } |
| 442 | } |
| 443 | return 0; |
| 444 | } |
Franck Bui-Huu | 55b7428 | 2006-10-13 13:37:35 +0200 | [diff] [blame] | 445 | if (!kallsyms_lookup_size_offset(pc, &size, &ofs)) |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 446 | return 0; |
Franck Bui-Huu | 1fd6909 | 2006-08-18 16:18:07 +0200 | [diff] [blame] | 447 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 448 | * Return ra if an exception occurred at the first instruction |
Franck Bui-Huu | 1fd6909 | 2006-08-18 16:18:07 +0200 | [diff] [blame] | 449 | */ |
Atsushi Nemoto | 1924600 | 2006-09-29 18:02:51 +0900 | [diff] [blame] | 450 | if (unlikely(ofs == 0)) { |
| 451 | pc = *ra; |
| 452 | *ra = 0; |
| 453 | return pc; |
| 454 | } |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 455 | |
| 456 | info.func = (void *)(pc - ofs); |
| 457 | info.func_size = ofs; /* analyze from start to ofs */ |
Franck Bui-Huu | 4d157d5 | 2006-08-03 09:29:21 +0200 | [diff] [blame] | 458 | leaf = get_frame_info(&info); |
| 459 | if (leaf < 0) |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 460 | return 0; |
| 461 | |
Franck Bui-Huu | 4d157d5 | 2006-08-03 09:29:21 +0200 | [diff] [blame] | 462 | if (*sp < stack_page || |
| 463 | *sp + info.frame_size > stack_page + THREAD_SIZE - 32) |
| 464 | return 0; |
| 465 | |
| 466 | if (leaf) |
| 467 | /* |
| 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 |
| 471 | * same value. |
| 472 | */ |
Atsushi Nemoto | 1924600 | 2006-09-29 18:02:51 +0900 | [diff] [blame] | 473 | pc = pc != *ra ? *ra : 0; |
Franck Bui-Huu | 4d157d5 | 2006-08-03 09:29:21 +0200 | [diff] [blame] | 474 | else |
| 475 | pc = ((unsigned long *)(*sp))[info.pc_offset]; |
| 476 | |
| 477 | *sp += info.frame_size; |
Atsushi Nemoto | 1924600 | 2006-09-29 18:02:51 +0900 | [diff] [blame] | 478 | *ra = 0; |
Franck Bui-Huu | 4d157d5 | 2006-08-03 09:29:21 +0200 | [diff] [blame] | 479 | return __kernel_text_address(pc) ? pc : 0; |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 480 | } |
Daniel Kalmar | 94ea09c | 2011-05-13 08:38:04 -0400 | [diff] [blame] | 481 | EXPORT_SYMBOL(unwind_stack_by_address); |
| 482 | |
| 483 | /* used by show_backtrace() */ |
| 484 | unsigned long unwind_stack(struct task_struct *task, unsigned long *sp, |
| 485 | unsigned long pc, unsigned long *ra) |
| 486 | { |
| 487 | unsigned long stack_page = (unsigned long)task_stack_page(task); |
| 488 | return unwind_stack_by_address(stack_page, sp, pc, ra); |
| 489 | } |
Atsushi Nemoto | f66686f | 2006-07-29 23:27:20 +0900 | [diff] [blame] | 490 | #endif |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * get_wchan - a maintenance nightmare^W^Wpain in the ass ... |
| 494 | */ |
| 495 | unsigned long get_wchan(struct task_struct *task) |
| 496 | { |
| 497 | unsigned long pc = 0; |
| 498 | #ifdef CONFIG_KALLSYMS |
| 499 | unsigned long sp; |
Atsushi Nemoto | 1924600 | 2006-09-29 18:02:51 +0900 | [diff] [blame] | 500 | unsigned long ra = 0; |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 501 | #endif |
| 502 | |
| 503 | if (!task || task == current || task->state == TASK_RUNNING) |
| 504 | goto out; |
| 505 | if (!task_stack_page(task)) |
| 506 | goto out; |
| 507 | |
| 508 | pc = thread_saved_pc(task); |
| 509 | |
| 510 | #ifdef CONFIG_KALLSYMS |
| 511 | sp = task->thread.reg29 + schedule_mfi.frame_size; |
| 512 | |
| 513 | while (in_sched_functions(pc)) |
Atsushi Nemoto | 1924600 | 2006-09-29 18:02:51 +0900 | [diff] [blame] | 514 | pc = unwind_stack(task, &sp, pc, &ra); |
Franck Bui-Huu | b594318 | 2006-08-18 16:18:09 +0200 | [diff] [blame] | 515 | #endif |
| 516 | |
| 517 | out: |
| 518 | return pc; |
| 519 | } |
Franck Bui-Huu | 9410910 | 2007-07-19 14:04:21 +0200 | [diff] [blame] | 520 | |
| 521 | /* |
| 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. |
| 524 | */ |
| 525 | unsigned long arch_align_stack(unsigned long sp) |
| 526 | { |
| 527 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) |
| 528 | sp -= get_random_int() & ~PAGE_MASK; |
| 529 | |
| 530 | return sp & ALMASK; |
| 531 | } |