Skip to content

Commit bb9bc46

Browse files
paulburtonralfbaechle
authored andcommitted
MIPS: Calculate microMIPS ra properly when unwinding the stack
get_frame_info() calculates the offset of the return address within a stack frame simply by dividing a the bottom 16 bits of the instruction, treated as a signed integer, by the size of a long. Whilst this works for MIPS32 & MIPS64 ISAs where the sw or sd instructions are used, it's incorrect for microMIPS where encodings differ. The result is that we typically completely fail to unwind the stack on microMIPS. Fix this by adjusting is_ra_save_ins() to calculate the return address offset, and take into account the various different encodings there in the same place as we consider whether an instruction is storing the ra/$31 register. With this we are now able to unwind the stack for kernels targetting the microMIPS ISA, for example we can produce: Call Trace: [<80109e1f>] show_stack+0x63/0x7c [<8011ea17>] __warn+0x9b/0xac [<8011ea45>] warn_slowpath_fmt+0x1d/0x20 [<8013fe53>] register_console+0x43/0x314 [<8067c58d>] of_setup_earlycon+0x1dd/0x1ec [<8067f63f>] early_init_dt_scan_chosen_stdout+0xe7/0xf8 [<8066c115>] do_early_param+0x75/0xac [<801302f9>] parse_args+0x1dd/0x308 [<8066c459>] parse_early_options+0x25/0x28 [<8066c48b>] parse_early_param+0x2f/0x38 [<8066e8cf>] setup_arch+0x113/0x488 [<8066c4f3>] start_kernel+0x57/0x328 ---[ end trace 0000000000000000 ]--- Whereas previously we only produced: Call Trace: [<80109e1f>] show_stack+0x63/0x7c ---[ end trace 0000000000000000 ]--- Signed-off-by: Paul Burton <paul.burton@imgtec.com> Fixes: 34c2f66 ("MIPS: microMIPS: Add unaligned access support.") Cc: Leonid Yegoshin <leonid.yegoshin@imgtec.com> Cc: linux-mips@linux-mips.org Cc: <stable@vger.kernel.org> # v3.10+ Patchwork: https://patchwork.linux-mips.org/patch/14532/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 67c7505 commit bb9bc46

1 file changed

Lines changed: 63 additions & 20 deletions

File tree

‎arch/mips/kernel/process.c‎

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ struct mips_frame_info {
196196
#define J_TARGET(pc,target) \
197197
(((unsigned long)(pc) & 0xf0000000) | ((target) << 2))
198198

199-
static inline int is_ra_save_ins(union mips_instruction *ip)
199+
static inline int is_ra_save_ins(union mips_instruction *ip, int *poff)
200200
{
201201
#ifdef CONFIG_CPU_MICROMIPS
202202
/*
@@ -209,25 +209,70 @@ static inline int is_ra_save_ins(union mips_instruction *ip)
209209
* microMIPS is way more fun...
210210
*/
211211
if (mm_insn_16bit(ip->halfword[1])) {
212-
return (ip->mm16_r5_format.opcode == mm_swsp16_op &&
213-
ip->mm16_r5_format.rt == 31) ||
214-
(ip->mm16_m_format.opcode == mm_pool16c_op &&
215-
ip->mm16_m_format.func == mm_swm16_op);
212+
switch (ip->mm16_r5_format.opcode) {
213+
case mm_swsp16_op:
214+
if (ip->mm16_r5_format.rt != 31)
215+
return 0;
216+
217+
*poff = ip->mm16_r5_format.simmediate;
218+
*poff = (*poff << 2) / sizeof(ulong);
219+
return 1;
220+
221+
case mm_pool16c_op:
222+
switch (ip->mm16_m_format.func) {
223+
case mm_swm16_op:
224+
*poff = ip->mm16_m_format.imm;
225+
*poff += 1 + ip->mm16_m_format.rlist;
226+
*poff = (*poff << 2) / sizeof(ulong);
227+
return 1;
228+
229+
default:
230+
return 0;
231+
}
232+
233+
default:
234+
return 0;
235+
}
216236
}
217-
else {
218-
return (ip->mm_m_format.opcode == mm_pool32b_op &&
219-
ip->mm_m_format.rd > 9 &&
220-
ip->mm_m_format.base == 29 &&
221-
ip->mm_m_format.func == mm_swm32_func) ||
222-
(ip->i_format.opcode == mm_sw32_op &&
223-
ip->i_format.rs == 29 &&
224-
ip->i_format.rt == 31);
237+
238+
switch (ip->i_format.opcode) {
239+
case mm_sw32_op:
240+
if (ip->i_format.rs != 29)
241+
return 0;
242+
if (ip->i_format.rt != 31)
243+
return 0;
244+
245+
*poff = ip->i_format.simmediate / sizeof(ulong);
246+
return 1;
247+
248+
case mm_pool32b_op:
249+
switch (ip->mm_m_format.func) {
250+
case mm_swm32_func:
251+
if (ip->mm_m_format.rd < 0x10)
252+
return 0;
253+
if (ip->mm_m_format.base != 29)
254+
return 0;
255+
256+
*poff = ip->mm_m_format.simmediate;
257+
*poff += (ip->mm_m_format.rd & 0xf) * sizeof(u32);
258+
*poff /= sizeof(ulong);
259+
return 1;
260+
default:
261+
return 0;
262+
}
263+
264+
default:
265+
return 0;
225266
}
226267
#else
227268
/* sw / sd $ra, offset($sp) */
228-
return (ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
229-
ip->i_format.rs == 29 &&
230-
ip->i_format.rt == 31;
269+
if ((ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
270+
ip->i_format.rs == 29 && ip->i_format.rt == 31) {
271+
*poff = ip->i_format.simmediate / sizeof(ulong);
272+
return 1;
273+
}
274+
275+
return 0;
231276
#endif
232277
}
233278

@@ -350,11 +395,9 @@ static int get_frame_info(struct mips_frame_info *info)
350395
}
351396
continue;
352397
}
353-
if (info->pc_offset == -1 && is_ra_save_ins(&insn)) {
354-
info->pc_offset =
355-
ip->i_format.simmediate / sizeof(long);
398+
if (info->pc_offset == -1 &&
399+
is_ra_save_ins(&insn, &info->pc_offset))
356400
break;
357-
}
358401
}
359402
if (info->frame_size && info->pc_offset >= 0) /* nested */
360403
return 0;

0 commit comments

Comments
 (0)