blob: 2dafceb0d0c601cb99c35066b8c3b0d67e96f999 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Baechle40ac5d42006-02-08 13:38:18 +00007 * Copyright (C) 2005, 2006 by Ralf Baechle (ralf@linux-mips.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 * Copyright (C) 2004 Thiemo Seufer
Leonid Yegoshin34c2f662013-03-25 13:18:07 -050010 * Copyright (C) 2013 Imagination Technologies Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
Ralf Baechle7bcf7712007-10-11 23:46:09 +010014#include <linux/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/mm.h>
17#include <linux/stddef.h>
18#include <linux/unistd.h>
Paul Gortmakercae39d12011-07-28 18:46:31 -040019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mman.h>
22#include <linux/personality.h>
23#include <linux/sys.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/completion.h>
Atsushi Nemoto63077512006-02-08 01:48:03 +090026#include <linux/kallsyms.h>
Franck Bui-Huu94109102007-07-19 14:04:21 +020027#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Franck Bui-Huu94109102007-07-19 14:04:21 +020029#include <asm/asm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/bootinfo.h>
31#include <asm/cpu.h>
Ralf Baechlee50c0a82005-05-31 11:49:19 +000032#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/fpu.h>
Paul Burton1db1af82014-01-27 15:23:11 +000034#include <asm/msa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/mipsregs.h>
37#include <asm/processor.h>
Alex Smith60be9392014-07-23 14:40:15 +010038#include <asm/reg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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 Nemoto1df0f0f2006-09-26 23:44:01 +090044#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Ralf Baechle1b2bc752009-06-23 10:00:31 +010046#ifdef CONFIG_HOTPLUG_CPU
Thomas Gleixnercdbedc62013-03-21 22:49:52 +010047void 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 Baechle1b2bc752009-06-23 10:00:31 +010053#endif
Thomas Gleixnercdbedc62013-03-21 22:49:52 +010054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055asmlinkage void ret_from_fork(void);
Al Viro8f54bca2012-10-09 16:27:45 -040056asmlinkage void ret_from_kernel_thread(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58void 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 Dearmanbbaf2382007-12-13 22:42:19 +000063 status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|ST0_FR|KU_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 status |= KU_USER;
65 regs->cp0_status = status;
66 clear_used_math();
Atsushi Nemotoe04582b2006-10-09 00:10:01 +090067 clear_fpu_owner();
Paul Burtona3056b12013-11-19 17:30:38 +000068 init_dsp();
Paul Burton1db1af82014-01-27 15:23:11 +000069 clear_thread_flag(TIF_MSA_CTX_LIVE);
70 disable_msa();
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 regs->cp0_epc = pc;
72 regs->regs[29] = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75void exit_thread(void)
76{
77}
78
79void flush_thread(void)
80{
81}
82
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -070083int copy_thread(unsigned long clone_flags, unsigned long usp,
Al Viroafa86fc2012-10-22 22:51:14 -040084 unsigned long arg, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Al Viro75bb07e2006-01-12 01:06:08 -080086 struct thread_info *ti = task_thread_info(p);
Al Viroafa86fc2012-10-22 22:51:14 -040087 struct pt_regs *childregs, *regs = current_pt_regs();
David Daney484889f2009-07-08 10:07:50 -070088 unsigned long childksp;
Ralf Baechle3c370262005-04-13 17:43:59 +000089 p->set_child_tid = p->clear_child_tid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Al Viro75bb07e2006-01-12 01:06:08 -080091 childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 preempt_disable();
94
Paul Burton1db1af82014-01-27 15:23:11 +000095 if (is_msa_enabled())
96 save_msa(p);
97 else if (is_fpu_owner())
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 save_fp(p);
Ralf Baechlee50c0a82005-05-31 11:49:19 +000099
100 if (cpu_has_dsp)
101 save_dsp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 preempt_enable();
104
105 /* set up new TSS. */
106 childregs = (struct pt_regs *) childksp - 1;
David Daney484889f2009-07-08 10:07:50 -0700107 /* Put the stack after the struct pt_regs. */
108 childksp = (unsigned long) childregs;
Al Viro8f54bca2012-10-09 16:27:45 -0400109 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 Torvalds1da177e2005-04-16 15:20:36 -0700127 *childregs = *regs;
Ralf Baechle70342282013-01-22 12:59:30 +0100128 childregs->regs[7] = 0; /* Clear error flag */
129 childregs->regs[2] = 0; /* Child gets zero as return value */
Al Viro64b31222012-12-27 11:52:32 -0500130 if (usp)
131 childregs->regs[29] = usp;
Al Viro8f54bca2012-10-09 16:27:45 -0400132 ti->addr_limit = USER_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 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 Torvalds1da177e2005-04-16 15:20:36 -0700141 childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 clear_tsk_thread_flag(p, TIF_USEDFPU);
144
Ralf Baechlef088fc82006-04-05 09:45:47 +0100145#ifdef CONFIG_MIPS_MT_FPAFF
Ralf Baechle6657fe02008-09-09 15:19:10 +0200146 clear_tsk_thread_flag(p, TIF_FPUBOUND);
Ralf Baechlef088fc82006-04-05 09:45:47 +0100147#endif /* CONFIG_MIPS_MT_FPAFF */
148
Ralf Baechle3c370262005-04-13 17:43:59 +0000149 if (clone_flags & CLONE_SETTLS)
150 ti->tp_value = regs->regs[7];
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return 0;
153}
154
Gregory Fong36ecafc2013-06-12 17:08:54 +0000155#ifdef CONFIG_CC_STACKPROTECTOR
156#include <linux/stackprotector.h>
157unsigned long __stack_chk_guard __read_mostly;
158EXPORT_SYMBOL(__stack_chk_guard);
159#endif
160
Franck Bui-Huub5943182006-08-18 16:18:09 +0200161struct mips_frame_info {
162 void *func;
163 unsigned long func_size;
164 int frame_size;
165 int pc_offset;
166};
Thiemo Seuferdc953df2005-02-21 10:55:16 +0000167
Tony Wu5000653e2013-05-12 15:05:34 +0000168#define J_TARGET(pc,target) \
169 (((unsigned long)(pc) & 0xf0000000) | ((target) << 2))
170
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200171static inline int is_ra_save_ins(union mips_instruction *ip)
172{
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500173#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-Huuc0efbb62006-08-03 09:29:15 +0200204 /* 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 Yegoshin34c2f662013-03-25 13:18:07 -0500208#endif
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200209}
210
Tony Wue7438c42013-05-12 15:04:29 +0000211static inline int is_jump_ins(union mips_instruction *ip)
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200212{
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500213#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 Wue7438c42013-05-12 15:04:29 +0000235 if (ip->j_format.opcode == j_op)
236 return 1;
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200237 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 Yegoshin34c2f662013-03-25 13:18:07 -0500242#endif
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200243}
244
245static inline int is_sp_move_ins(union mips_instruction *ip)
246{
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500247#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-Huuc0efbb62006-08-03 09:29:15 +0200268 /* 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 Yegoshin34c2f662013-03-25 13:18:07 -0500273#endif
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200274 return 0;
275}
276
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900277static int get_frame_info(struct mips_frame_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500279#ifdef CONFIG_CPU_MICROMIPS
280 union mips_instruction *ip = (void *) (((char *) info->func) - 1);
281#else
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200282 union mips_instruction *ip = info->func;
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500283#endif
Franck Bui-Huu29b376f2006-08-18 16:18:08 +0200284 unsigned max_insns = info->func_size / sizeof(union mips_instruction);
285 unsigned i;
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 info->pc_offset = -1;
Atsushi Nemoto63077512006-02-08 01:48:03 +0900288 info->frame_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Franck Bui-Huu29b376f2006-08-18 16:18:08 +0200290 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-Huuc0efbb62006-08-03 09:29:15 +0200297 for (i = 0; i < max_insns; i++, ip++) {
298
Tony Wue7438c42013-05-12 15:04:29 +0000299 if (is_jump_ins(ip))
Atsushi Nemoto63077512006-02-08 01:48:03 +0900300 break;
Franck Bui-Huu0cceb4a2006-08-03 09:29:20 +0200301 if (!info->frame_size) {
302 if (is_sp_move_ins(ip))
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500303 {
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-Huu0cceb4a2006-08-03 09:29:20 +0200321 info->frame_size = - ip->i_format.simmediate;
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500322 }
Franck Bui-Huu0cceb4a2006-08-03 09:29:20 +0200323 continue;
Atsushi Nemoto63077512006-02-08 01:48:03 +0900324 }
Franck Bui-Huu0cceb4a2006-08-03 09:29:20 +0200325 if (info->pc_offset == -1 && is_ra_save_ins(ip)) {
Atsushi Nemoto63077512006-02-08 01:48:03 +0900326 info->pc_offset =
327 ip->i_format.simmediate / sizeof(long);
Franck Bui-Huu0cceb4a2006-08-03 09:29:20 +0200328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 }
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900331 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-Huu29b376f2006-08-18 16:18:08 +0200336err:
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900337 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Franck Bui-Huub5943182006-08-18 16:18:09 +0200340static struct mips_frame_info schedule_mfi __read_mostly;
341
Tony Wu5000653e2013-05-12 15:05:34 +0000342#ifdef CONFIG_KALLSYMS
343static unsigned long get___schedule_addr(void)
344{
345 return kallsyms_lookup_name("__schedule");
346}
347#else
348static 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 Torvalds1da177e2005-04-16 15:20:36 -0700362static int __init frame_info_init(void)
363{
Franck Bui-Huub5943182006-08-18 16:18:09 +0200364 unsigned long size = 0;
Atsushi Nemoto63077512006-02-08 01:48:03 +0900365#ifdef CONFIG_KALLSYMS
Franck Bui-Huub5943182006-08-18 16:18:09 +0200366 unsigned long ofs;
Atsushi Nemoto63077512006-02-08 01:48:03 +0900367#endif
Tony Wu5000653e2013-05-12 15:05:34 +0000368 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-Huub5943182006-08-18 16:18:09 +0200378 schedule_mfi.func_size = size;
379
380 get_frame_info(&schedule_mfi);
Franck Bui-Huu6057a792006-08-03 09:29:18 +0200381
382 /*
383 * Without schedule() frame info, result given by
384 * thread_saved_pc() and get_wchan() are not reliable.
385 */
Franck Bui-Huub5943182006-08-18 16:18:09 +0200386 if (schedule_mfi.pc_offset < 0)
Franck Bui-Huu6057a792006-08-03 09:29:18 +0200387 printk("Can't analyze schedule() prologue at %p\n", schedule);
Atsushi Nemoto63077512006-02-08 01:48:03 +0900388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
390}
391
392arch_initcall(frame_info_init);
393
394/*
395 * Return saved PC of a blocked thread.
396 */
397unsigned 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-Huub5943182006-08-18 16:18:09 +0200404 if (schedule_mfi.pc_offset < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
Franck Bui-Huub5943182006-08-18 16:18:09 +0200406 return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900410#ifdef CONFIG_KALLSYMS
Daniel Kalmar94ea09c2011-05-13 08:38:04 -0400411/* generic stack unwinding function */
412unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
413 unsigned long *sp,
414 unsigned long pc,
415 unsigned long *ra)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900416{
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900417 struct mips_frame_info info;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900418 unsigned long size, ofs;
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200419 int leaf;
Atsushi Nemoto19246002006-09-29 18:02:51 +0900420 extern void ret_from_irq(void);
421 extern void ret_from_exception(void);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900422
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900423 if (!stack_page)
424 return 0;
425
Atsushi Nemoto19246002006-09-29 18:02:51 +0900426 /*
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-Huu55b74282006-10-13 13:37:35 +0200445 if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900446 return 0;
Franck Bui-Huu1fd69092006-08-18 16:18:07 +0200447 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300448 * Return ra if an exception occurred at the first instruction
Franck Bui-Huu1fd69092006-08-18 16:18:07 +0200449 */
Atsushi Nemoto19246002006-09-29 18:02:51 +0900450 if (unlikely(ofs == 0)) {
451 pc = *ra;
452 *ra = 0;
453 return pc;
454 }
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900455
456 info.func = (void *)(pc - ofs);
457 info.func_size = ofs; /* analyze from start to ofs */
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200458 leaf = get_frame_info(&info);
459 if (leaf < 0)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900460 return 0;
461
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200462 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 Nemoto19246002006-09-29 18:02:51 +0900473 pc = pc != *ra ? *ra : 0;
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200474 else
475 pc = ((unsigned long *)(*sp))[info.pc_offset];
476
477 *sp += info.frame_size;
Atsushi Nemoto19246002006-09-29 18:02:51 +0900478 *ra = 0;
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200479 return __kernel_text_address(pc) ? pc : 0;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900480}
Daniel Kalmar94ea09c2011-05-13 08:38:04 -0400481EXPORT_SYMBOL(unwind_stack_by_address);
482
483/* used by show_backtrace() */
484unsigned 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 Nemotof66686f2006-07-29 23:27:20 +0900490#endif
Franck Bui-Huub5943182006-08-18 16:18:09 +0200491
492/*
493 * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
494 */
495unsigned long get_wchan(struct task_struct *task)
496{
497 unsigned long pc = 0;
498#ifdef CONFIG_KALLSYMS
499 unsigned long sp;
Atsushi Nemoto19246002006-09-29 18:02:51 +0900500 unsigned long ra = 0;
Franck Bui-Huub5943182006-08-18 16:18:09 +0200501#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 Nemoto19246002006-09-29 18:02:51 +0900514 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huub5943182006-08-18 16:18:09 +0200515#endif
516
517out:
518 return pc;
519}
Franck Bui-Huu94109102007-07-19 14:04:21 +0200520
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 */
525unsigned 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}