Skip to content

Commit 09c953f

Browse files
James Hogangregkh
James Hogan
authored andcommitted
MIPS: KGDB: Use kernel context for sleeping threads
commit 162b270 upstream. KGDB is a kernel debug stub and it can't be used to debug userland as it can only safely access kernel memory. On MIPS however KGDB has always got the register state of sleeping processes from the userland register context at the beginning of the kernel stack. This is meaningless for kernel threads (which never enter userland), and for user threads it prevents the user seeing what it is doing while in the kernel: (gdb) info threads Id Target Id Frame ... 3 Thread 2 (kthreadd) 0x0000000000000000 in ?? () 2 Thread 1 (init) 0x000000007705c4b4 in ?? () 1 Thread -2 (shadowCPU0) 0xffffffff8012524c in arch_kgdb_breakpoint () at arch/mips/kernel/kgdb.c:201 Get the register state instead from the (partial) kernel register context stored in the task's thread_struct for resume() to restore. All threads now correctly appear to be in context_switch(): (gdb) info threads Id Target Id Frame ... 3 Thread 2 (kthreadd) context_switch (rq=<optimized out>, cookie=..., next=<optimized out>, prev=0x0) at kernel/sched/core.c:2903 2 Thread 1 (init) context_switch (rq=<optimized out>, cookie=..., next=<optimized out>, prev=0x0) at kernel/sched/core.c:2903 1 Thread -2 (shadowCPU0) 0xffffffff8012524c in arch_kgdb_breakpoint () at arch/mips/kernel/kgdb.c:201 Call clobbered registers which aren't saved and exception registers (BadVAddr & Cause) which can't be easily determined without stack unwinding are reported as 0. The PC is taken from the return address, such that the state presented matches that found immediately after returning from resume(). Fixes: 8854700 ("[MIPS] kgdb: add arch support for the kernel's kgdb core") Signed-off-by: James Hogan <[email protected]> Cc: Jason Wessel <[email protected]> Cc: [email protected] Patchwork: https://patchwork.linux-mips.org/patch/15829/ Signed-off-by: Ralf Baechle <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4a71345 commit 09c953f

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

arch/mips/kernel/kgdb.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -244,35 +244,53 @@ static int compute_signal(int tt)
244244
void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
245245
{
246246
int reg;
247-
struct thread_info *ti = task_thread_info(p);
248-
unsigned long ksp = (unsigned long)ti + THREAD_SIZE - 32;
249-
struct pt_regs *regs = (struct pt_regs *)ksp - 1;
250247
#if (KGDB_GDB_REG_SIZE == 32)
251248
u32 *ptr = (u32 *)gdb_regs;
252249
#else
253250
u64 *ptr = (u64 *)gdb_regs;
254251
#endif
255252

256253
for (reg = 0; reg < 16; reg++)
257-
*(ptr++) = regs->regs[reg];
254+
*(ptr++) = 0;
258255

259256
/* S0 - S7 */
260-
for (reg = 16; reg < 24; reg++)
261-
*(ptr++) = regs->regs[reg];
257+
*(ptr++) = p->thread.reg16;
258+
*(ptr++) = p->thread.reg17;
259+
*(ptr++) = p->thread.reg18;
260+
*(ptr++) = p->thread.reg19;
261+
*(ptr++) = p->thread.reg20;
262+
*(ptr++) = p->thread.reg21;
263+
*(ptr++) = p->thread.reg22;
264+
*(ptr++) = p->thread.reg23;
262265

263266
for (reg = 24; reg < 28; reg++)
264267
*(ptr++) = 0;
265268

266269
/* GP, SP, FP, RA */
267-
for (reg = 28; reg < 32; reg++)
268-
*(ptr++) = regs->regs[reg];
269-
270-
*(ptr++) = regs->cp0_status;
271-
*(ptr++) = regs->lo;
272-
*(ptr++) = regs->hi;
273-
*(ptr++) = regs->cp0_badvaddr;
274-
*(ptr++) = regs->cp0_cause;
275-
*(ptr++) = regs->cp0_epc;
270+
*(ptr++) = (long)p;
271+
*(ptr++) = p->thread.reg29;
272+
*(ptr++) = p->thread.reg30;
273+
*(ptr++) = p->thread.reg31;
274+
275+
*(ptr++) = p->thread.cp0_status;
276+
277+
/* lo, hi */
278+
*(ptr++) = 0;
279+
*(ptr++) = 0;
280+
281+
/*
282+
* BadVAddr, Cause
283+
* Ideally these would come from the last exception frame up the stack
284+
* but that requires unwinding, otherwise we can't know much for sure.
285+
*/
286+
*(ptr++) = 0;
287+
*(ptr++) = 0;
288+
289+
/*
290+
* PC
291+
* use return address (RA), i.e. the moment after return from resume()
292+
*/
293+
*(ptr++) = p->thread.reg31;
276294
}
277295

278296
void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)

0 commit comments

Comments
 (0)