blob: 636b0745d7c7e51ed4b0f1b1efeb4dbe6d8beb6a [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 Burton7daef8f2014-07-11 16:47:05 +010069 clear_thread_flag(TIF_USEDMSA);
Paul Burton1db1af82014-01-27 15:23:11 +000070 clear_thread_flag(TIF_MSA_CTX_LIVE);
71 disable_msa();
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 regs->cp0_epc = pc;
73 regs->regs[29] = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76void exit_thread(void)
77{
78}
79
80void flush_thread(void)
81{
82}
83
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -070084int copy_thread(unsigned long clone_flags, unsigned long usp,
Al Viroafa86fc2012-10-22 22:51:14 -040085 unsigned long arg, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Al Viro75bb07e2006-01-12 01:06:08 -080087 struct thread_info *ti = task_thread_info(p);
Al Viroafa86fc2012-10-22 22:51:14 -040088 struct pt_regs *childregs, *regs = current_pt_regs();
David Daney484889f2009-07-08 10:07:50 -070089 unsigned long childksp;
Ralf Baechle3c370262005-04-13 17:43:59 +000090 p->set_child_tid = p->clear_child_tid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Al Viro75bb07e2006-01-12 01:06:08 -080092 childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 preempt_disable();
95
Paul Burton1db1af82014-01-27 15:23:11 +000096 if (is_msa_enabled())
97 save_msa(p);
98 else if (is_fpu_owner())
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 save_fp(p);
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000100
101 if (cpu_has_dsp)
102 save_dsp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 preempt_enable();
105
106 /* set up new TSS. */
107 childregs = (struct pt_regs *) childksp - 1;
David Daney484889f2009-07-08 10:07:50 -0700108 /* Put the stack after the struct pt_regs. */
109 childksp = (unsigned long) childregs;
Al Viro8f54bca2012-10-09 16:27:45 -0400110 p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
111 if (unlikely(p->flags & PF_KTHREAD)) {
112 unsigned long status = p->thread.cp0_status;
113 memset(childregs, 0, sizeof(struct pt_regs));
114 ti->addr_limit = KERNEL_DS;
115 p->thread.reg16 = usp; /* fn */
116 p->thread.reg17 = arg;
117 p->thread.reg29 = childksp;
118 p->thread.reg31 = (unsigned long) ret_from_kernel_thread;
119#if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
120 status = (status & ~(ST0_KUP | ST0_IEP | ST0_IEC)) |
121 ((status & (ST0_KUC | ST0_IEC)) << 2);
122#else
123 status |= ST0_EXL;
124#endif
125 childregs->cp0_status = status;
126 return 0;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 *childregs = *regs;
Ralf Baechle70342282013-01-22 12:59:30 +0100129 childregs->regs[7] = 0; /* Clear error flag */
130 childregs->regs[2] = 0; /* Child gets zero as return value */
Al Viro64b31222012-12-27 11:52:32 -0500131 if (usp)
132 childregs->regs[29] = usp;
Al Viro8f54bca2012-10-09 16:27:45 -0400133 ti->addr_limit = USER_DS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 p->thread.reg29 = (unsigned long) childregs;
136 p->thread.reg31 = (unsigned long) ret_from_fork;
137
138 /*
139 * New tasks lose permission to use the fpu. This accelerates context
140 * switching for most programs since they don't use the fpu.
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 clear_tsk_thread_flag(p, TIF_USEDFPU);
Paul Burton7daef8f2014-07-11 16:47:05 +0100145 clear_tsk_thread_flag(p, TIF_USEDMSA);
146 clear_tsk_thread_flag(p, TIF_MSA_CTX_LIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Ralf Baechlef088fc82006-04-05 09:45:47 +0100148#ifdef CONFIG_MIPS_MT_FPAFF
Ralf Baechle6657fe02008-09-09 15:19:10 +0200149 clear_tsk_thread_flag(p, TIF_FPUBOUND);
Ralf Baechlef088fc82006-04-05 09:45:47 +0100150#endif /* CONFIG_MIPS_MT_FPAFF */
151
Ralf Baechle3c370262005-04-13 17:43:59 +0000152 if (clone_flags & CLONE_SETTLS)
153 ti->tp_value = regs->regs[7];
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
156}
157
Gregory Fong36ecafc2013-06-12 17:08:54 +0000158#ifdef CONFIG_CC_STACKPROTECTOR
159#include <linux/stackprotector.h>
160unsigned long __stack_chk_guard __read_mostly;
161EXPORT_SYMBOL(__stack_chk_guard);
162#endif
163
Franck Bui-Huub5943182006-08-18 16:18:09 +0200164struct mips_frame_info {
165 void *func;
166 unsigned long func_size;
167 int frame_size;
168 int pc_offset;
169};
Thiemo Seuferdc953df2005-02-21 10:55:16 +0000170
Tony Wu5000653e2013-05-12 15:05:34 +0000171#define J_TARGET(pc,target) \
172 (((unsigned long)(pc) & 0xf0000000) | ((target) << 2))
173
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200174static inline int is_ra_save_ins(union mips_instruction *ip)
175{
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500176#ifdef CONFIG_CPU_MICROMIPS
177 union mips_instruction mmi;
178
179 /*
180 * swsp ra,offset
181 * swm16 reglist,offset(sp)
182 * swm32 reglist,offset(sp)
183 * sw32 ra,offset(sp)
184 * jradiussp - NOT SUPPORTED
185 *
186 * microMIPS is way more fun...
187 */
188 if (mm_insn_16bit(ip->halfword[0])) {
189 mmi.word = (ip->halfword[0] << 16);
190 return ((mmi.mm16_r5_format.opcode == mm_swsp16_op &&
191 mmi.mm16_r5_format.rt == 31) ||
192 (mmi.mm16_m_format.opcode == mm_pool16c_op &&
193 mmi.mm16_m_format.func == mm_swm16_op));
194 }
195 else {
196 mmi.halfword[0] = ip->halfword[1];
197 mmi.halfword[1] = ip->halfword[0];
198 return ((mmi.mm_m_format.opcode == mm_pool32b_op &&
199 mmi.mm_m_format.rd > 9 &&
200 mmi.mm_m_format.base == 29 &&
201 mmi.mm_m_format.func == mm_swm32_func) ||
202 (mmi.i_format.opcode == mm_sw32_op &&
203 mmi.i_format.rs == 29 &&
204 mmi.i_format.rt == 31));
205 }
206#else
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200207 /* sw / sd $ra, offset($sp) */
208 return (ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
209 ip->i_format.rs == 29 &&
210 ip->i_format.rt == 31;
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500211#endif
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200212}
213
Tony Wue7438c42013-05-12 15:04:29 +0000214static inline int is_jump_ins(union mips_instruction *ip)
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200215{
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500216#ifdef CONFIG_CPU_MICROMIPS
217 /*
218 * jr16,jrc,jalr16,jalr16
219 * jal
220 * jalr/jr,jalr.hb/jr.hb,jalrs,jalrs.hb
221 * jraddiusp - NOT SUPPORTED
222 *
223 * microMIPS is kind of more fun...
224 */
225 union mips_instruction mmi;
226
227 mmi.word = (ip->halfword[0] << 16);
228
229 if ((mmi.mm16_r5_format.opcode == mm_pool16c_op &&
230 (mmi.mm16_r5_format.rt & mm_jr16_op) == mm_jr16_op) ||
231 ip->j_format.opcode == mm_jal32_op)
232 return 1;
233 if (ip->r_format.opcode != mm_pool32a_op ||
234 ip->r_format.func != mm_pool32axf_op)
235 return 0;
236 return (((ip->u_format.uimmediate >> 6) & mm_jalr_op) == mm_jalr_op);
237#else
Tony Wue7438c42013-05-12 15:04:29 +0000238 if (ip->j_format.opcode == j_op)
239 return 1;
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200240 if (ip->j_format.opcode == jal_op)
241 return 1;
242 if (ip->r_format.opcode != spec_op)
243 return 0;
244 return ip->r_format.func == jalr_op || ip->r_format.func == jr_op;
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500245#endif
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200246}
247
248static inline int is_sp_move_ins(union mips_instruction *ip)
249{
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500250#ifdef CONFIG_CPU_MICROMIPS
251 /*
252 * addiusp -imm
253 * addius5 sp,-imm
254 * addiu32 sp,sp,-imm
255 * jradiussp - NOT SUPPORTED
256 *
257 * microMIPS is not more fun...
258 */
259 if (mm_insn_16bit(ip->halfword[0])) {
260 union mips_instruction mmi;
261
262 mmi.word = (ip->halfword[0] << 16);
263 return ((mmi.mm16_r3_format.opcode == mm_pool16d_op &&
264 mmi.mm16_r3_format.simmediate && mm_addiusp_func) ||
265 (mmi.mm16_r5_format.opcode == mm_pool16d_op &&
266 mmi.mm16_r5_format.rt == 29));
267 }
268 return (ip->mm_i_format.opcode == mm_addiu32_op &&
269 ip->mm_i_format.rt == 29 && ip->mm_i_format.rs == 29);
270#else
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200271 /* addiu/daddiu sp,sp,-imm */
272 if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
273 return 0;
274 if (ip->i_format.opcode == addiu_op || ip->i_format.opcode == daddiu_op)
275 return 1;
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500276#endif
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200277 return 0;
278}
279
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900280static int get_frame_info(struct mips_frame_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500282#ifdef CONFIG_CPU_MICROMIPS
283 union mips_instruction *ip = (void *) (((char *) info->func) - 1);
284#else
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200285 union mips_instruction *ip = info->func;
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500286#endif
Franck Bui-Huu29b376f2006-08-18 16:18:08 +0200287 unsigned max_insns = info->func_size / sizeof(union mips_instruction);
288 unsigned i;
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 info->pc_offset = -1;
Atsushi Nemoto63077512006-02-08 01:48:03 +0900291 info->frame_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Franck Bui-Huu29b376f2006-08-18 16:18:08 +0200293 if (!ip)
294 goto err;
295
296 if (max_insns == 0)
297 max_insns = 128U; /* unknown function size */
298 max_insns = min(128U, max_insns);
299
Franck Bui-Huuc0efbb62006-08-03 09:29:15 +0200300 for (i = 0; i < max_insns; i++, ip++) {
301
Tony Wue7438c42013-05-12 15:04:29 +0000302 if (is_jump_ins(ip))
Atsushi Nemoto63077512006-02-08 01:48:03 +0900303 break;
Franck Bui-Huu0cceb4a2006-08-03 09:29:20 +0200304 if (!info->frame_size) {
305 if (is_sp_move_ins(ip))
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500306 {
307#ifdef CONFIG_CPU_MICROMIPS
308 if (mm_insn_16bit(ip->halfword[0]))
309 {
310 unsigned short tmp;
311
312 if (ip->halfword[0] & mm_addiusp_func)
313 {
314 tmp = (((ip->halfword[0] >> 1) & 0x1ff) << 2);
315 info->frame_size = -(signed short)(tmp | ((tmp & 0x100) ? 0xfe00 : 0));
316 } else {
317 tmp = (ip->halfword[0] >> 1);
318 info->frame_size = -(signed short)(tmp & 0xf);
319 }
320 ip = (void *) &ip->halfword[1];
321 ip--;
322 } else
323#endif
Franck Bui-Huu0cceb4a2006-08-03 09:29:20 +0200324 info->frame_size = - ip->i_format.simmediate;
Leonid Yegoshin34c2f662013-03-25 13:18:07 -0500325 }
Franck Bui-Huu0cceb4a2006-08-03 09:29:20 +0200326 continue;
Atsushi Nemoto63077512006-02-08 01:48:03 +0900327 }
Franck Bui-Huu0cceb4a2006-08-03 09:29:20 +0200328 if (info->pc_offset == -1 && is_ra_save_ins(ip)) {
Atsushi Nemoto63077512006-02-08 01:48:03 +0900329 info->pc_offset =
330 ip->i_format.simmediate / sizeof(long);
Franck Bui-Huu0cceb4a2006-08-03 09:29:20 +0200331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 }
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900334 if (info->frame_size && info->pc_offset >= 0) /* nested */
335 return 0;
336 if (info->pc_offset < 0) /* leaf */
337 return 1;
338 /* prologue seems boggus... */
Franck Bui-Huu29b376f2006-08-18 16:18:08 +0200339err:
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900340 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Franck Bui-Huub5943182006-08-18 16:18:09 +0200343static struct mips_frame_info schedule_mfi __read_mostly;
344
Tony Wu5000653e2013-05-12 15:05:34 +0000345#ifdef CONFIG_KALLSYMS
346static unsigned long get___schedule_addr(void)
347{
348 return kallsyms_lookup_name("__schedule");
349}
350#else
351static unsigned long get___schedule_addr(void)
352{
353 union mips_instruction *ip = (void *)schedule;
354 int max_insns = 8;
355 int i;
356
357 for (i = 0; i < max_insns; i++, ip++) {
358 if (ip->j_format.opcode == j_op)
359 return J_TARGET(ip, ip->j_format.target);
360 }
361 return 0;
362}
363#endif
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static int __init frame_info_init(void)
366{
Franck Bui-Huub5943182006-08-18 16:18:09 +0200367 unsigned long size = 0;
Atsushi Nemoto63077512006-02-08 01:48:03 +0900368#ifdef CONFIG_KALLSYMS
Franck Bui-Huub5943182006-08-18 16:18:09 +0200369 unsigned long ofs;
Atsushi Nemoto63077512006-02-08 01:48:03 +0900370#endif
Tony Wu5000653e2013-05-12 15:05:34 +0000371 unsigned long addr;
372
373 addr = get___schedule_addr();
374 if (!addr)
375 addr = (unsigned long)schedule;
376
377#ifdef CONFIG_KALLSYMS
378 kallsyms_lookup_size_offset(addr, &size, &ofs);
379#endif
380 schedule_mfi.func = (void *)addr;
Franck Bui-Huub5943182006-08-18 16:18:09 +0200381 schedule_mfi.func_size = size;
382
383 get_frame_info(&schedule_mfi);
Franck Bui-Huu6057a792006-08-03 09:29:18 +0200384
385 /*
386 * Without schedule() frame info, result given by
387 * thread_saved_pc() and get_wchan() are not reliable.
388 */
Franck Bui-Huub5943182006-08-18 16:18:09 +0200389 if (schedule_mfi.pc_offset < 0)
Franck Bui-Huu6057a792006-08-03 09:29:18 +0200390 printk("Can't analyze schedule() prologue at %p\n", schedule);
Atsushi Nemoto63077512006-02-08 01:48:03 +0900391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 0;
393}
394
395arch_initcall(frame_info_init);
396
397/*
398 * Return saved PC of a blocked thread.
399 */
400unsigned long thread_saved_pc(struct task_struct *tsk)
401{
402 struct thread_struct *t = &tsk->thread;
403
404 /* New born processes are a special case */
405 if (t->reg31 == (unsigned long) ret_from_fork)
406 return t->reg31;
Franck Bui-Huub5943182006-08-18 16:18:09 +0200407 if (schedule_mfi.pc_offset < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
Franck Bui-Huub5943182006-08-18 16:18:09 +0200409 return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900413#ifdef CONFIG_KALLSYMS
Daniel Kalmar94ea09c2011-05-13 08:38:04 -0400414/* generic stack unwinding function */
415unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
416 unsigned long *sp,
417 unsigned long pc,
418 unsigned long *ra)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900419{
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900420 struct mips_frame_info info;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900421 unsigned long size, ofs;
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200422 int leaf;
Atsushi Nemoto19246002006-09-29 18:02:51 +0900423 extern void ret_from_irq(void);
424 extern void ret_from_exception(void);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900425
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900426 if (!stack_page)
427 return 0;
428
Atsushi Nemoto19246002006-09-29 18:02:51 +0900429 /*
430 * If we reached the bottom of interrupt context,
431 * return saved pc in pt_regs.
432 */
433 if (pc == (unsigned long)ret_from_irq ||
434 pc == (unsigned long)ret_from_exception) {
435 struct pt_regs *regs;
436 if (*sp >= stack_page &&
437 *sp + sizeof(*regs) <= stack_page + THREAD_SIZE - 32) {
438 regs = (struct pt_regs *)*sp;
439 pc = regs->cp0_epc;
440 if (__kernel_text_address(pc)) {
441 *sp = regs->regs[29];
442 *ra = regs->regs[31];
443 return pc;
444 }
445 }
446 return 0;
447 }
Franck Bui-Huu55b74282006-10-13 13:37:35 +0200448 if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900449 return 0;
Franck Bui-Huu1fd69092006-08-18 16:18:07 +0200450 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300451 * Return ra if an exception occurred at the first instruction
Franck Bui-Huu1fd69092006-08-18 16:18:07 +0200452 */
Atsushi Nemoto19246002006-09-29 18:02:51 +0900453 if (unlikely(ofs == 0)) {
454 pc = *ra;
455 *ra = 0;
456 return pc;
457 }
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900458
459 info.func = (void *)(pc - ofs);
460 info.func_size = ofs; /* analyze from start to ofs */
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200461 leaf = get_frame_info(&info);
462 if (leaf < 0)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900463 return 0;
464
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200465 if (*sp < stack_page ||
466 *sp + info.frame_size > stack_page + THREAD_SIZE - 32)
467 return 0;
468
469 if (leaf)
470 /*
471 * For some extreme cases, get_frame_info() can
472 * consider wrongly a nested function as a leaf
473 * one. In that cases avoid to return always the
474 * same value.
475 */
Atsushi Nemoto19246002006-09-29 18:02:51 +0900476 pc = pc != *ra ? *ra : 0;
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200477 else
478 pc = ((unsigned long *)(*sp))[info.pc_offset];
479
480 *sp += info.frame_size;
Atsushi Nemoto19246002006-09-29 18:02:51 +0900481 *ra = 0;
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200482 return __kernel_text_address(pc) ? pc : 0;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900483}
Daniel Kalmar94ea09c2011-05-13 08:38:04 -0400484EXPORT_SYMBOL(unwind_stack_by_address);
485
486/* used by show_backtrace() */
487unsigned long unwind_stack(struct task_struct *task, unsigned long *sp,
488 unsigned long pc, unsigned long *ra)
489{
490 unsigned long stack_page = (unsigned long)task_stack_page(task);
491 return unwind_stack_by_address(stack_page, sp, pc, ra);
492}
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900493#endif
Franck Bui-Huub5943182006-08-18 16:18:09 +0200494
495/*
496 * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
497 */
498unsigned long get_wchan(struct task_struct *task)
499{
500 unsigned long pc = 0;
501#ifdef CONFIG_KALLSYMS
502 unsigned long sp;
Atsushi Nemoto19246002006-09-29 18:02:51 +0900503 unsigned long ra = 0;
Franck Bui-Huub5943182006-08-18 16:18:09 +0200504#endif
505
506 if (!task || task == current || task->state == TASK_RUNNING)
507 goto out;
508 if (!task_stack_page(task))
509 goto out;
510
511 pc = thread_saved_pc(task);
512
513#ifdef CONFIG_KALLSYMS
514 sp = task->thread.reg29 + schedule_mfi.frame_size;
515
516 while (in_sched_functions(pc))
Atsushi Nemoto19246002006-09-29 18:02:51 +0900517 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huub5943182006-08-18 16:18:09 +0200518#endif
519
520out:
521 return pc;
522}
Franck Bui-Huu94109102007-07-19 14:04:21 +0200523
524/*
525 * Don't forget that the stack pointer must be aligned on a 8 bytes
526 * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
527 */
528unsigned long arch_align_stack(unsigned long sp)
529{
530 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
531 sp -= get_random_int() & ~PAGE_MASK;
532
533 return sp & ALMASK;
534}