
Date: Thu, 21 Jun 2012 10:39:45 +0900
Subject: [PATCH] ppc: fix up back trace for panic or active tasks problem

When dump file is not contained enough NT_PRSTATUS notes for all online cpus,
ppc diskdump and netdump can not do correct "bt".
Relocate NT_PRSTATUS instances into corresponding cpu maps.

This can do only once when first "bt" command is required.
I couldn't found out good entry point for this while initialization time.

Signed-off-by: Toshikazu Nakayama <nakayama.ts@ncos.nec.co.jp>
---
 diskdump.c |   81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 netdump.c  |    3 ++
 ppc.c      |    2 +
 3 files changed, 85 insertions(+), 1 deletions(-)

diff --git a/diskdump.c b/diskdump.c
index e3f04e8..4781659 100644
--- a/diskdump.c
+++ b/diskdump.c
@@ -1012,12 +1012,91 @@ get_diskdump_panic_task(void)
 extern void get_netdump_regs_x86(struct bt_info *, ulong *, ulong *);
 extern void get_netdump_regs_x86_64(struct bt_info *, ulong *, ulong *);
 
+/*
+ * Try to relocate NT_PRSTATUS notes according by in kernel crash_notes.
+ * Function is only called from ppc's get_regs.
+ */
+
+static int
+verify_crash_note_in_kernel_ppc(int cpu)
+{
+	int ret;
+	Elf32_Nhdr *note32;
+	ulong crash_notes_ptr;
+	char *buf, *name;
+
+	ret = TRUE;
+	if (!readmem(symbol_value("crash_notes"), KVADDR, &crash_notes_ptr,
+		     sizeof(ulong), "crash_notes", QUIET|RETURN_ON_ERROR) ||
+	    !crash_notes_ptr)
+		goto out;
+
+	buf = GETBUF(SIZE(note_buf));
+	if ((kt->flags & SMP) && (kt->flags & PER_CPU_OFF))
+		crash_notes_ptr += kt->__per_cpu_offset[cpu];
+	if (!readmem(crash_notes_ptr, KVADDR, buf, SIZE(note_buf),
+		     "cpu crash_notes", QUIET|RETURN_ON_ERROR))
+		goto freebuf;
+
+	note32 = (Elf32_Nhdr *)buf;
+	name = (char *)(note32 + 1);
+	if (note32->n_type != NT_PRSTATUS ||
+	    note32->n_namesz != strlen("CORE") + 1 ||
+	    strncmp(name, "CORE", note32->n_namesz) ||
+	    note32->n_descsz != SIZE(elf_prstatus))
+		ret = FALSE;
+freebuf:
+	FREEBUF(buf);
+out:
+	return ret;
+}
+
+void
+relocate_nt_prstatus_percpu_ppc(void **nt_prstatus_percpu,
+				int *num_prstatus_notes)
+{
+	static int relocated = FALSE;
+	void **nt_ptr;
+	int i, j, nrcpus;
+	size_t size;
+
+	/* relocation is possible only once */
+	if (relocated == TRUE)
+		return;
+	relocated = TRUE;
+	if (!symbol_exists("crash_notes") ||
+	    !VALID_STRUCT(note_buf) || !VALID_STRUCT(elf_prstatus))
+		return;
+
+	size = NR_CPUS * sizeof(void *);
+	nt_ptr = (void **)GETBUF(size);
+	BCOPY(nt_prstatus_percpu, nt_ptr, size);
+	BZERO(nt_prstatus_percpu, size);
+
+	*num_prstatus_notes = 0;
+	nrcpus = (kt->kernel_NR_CPUS ? kt->kernel_NR_CPUS : NR_CPUS);
+	for (i = 0, j = 0; i < nrcpus; i++) {
+		if (!in_cpu_map(ONLINE, i))
+			continue;
+		if (verify_crash_note_in_kernel_ppc(i))
+			nt_prstatus_percpu[i] = nt_ptr[j++];
+		else if (CRASHDEBUG(1))
+			error(WARNING, "cpu#%d: not saved crash_notes\n", i);
+		/* num_prstatus_notes is always equal to online cpus in ppc */
+		(*num_prstatus_notes)++;
+	}
+	FREEBUF(nt_ptr);
+}
+
 static void
 get_diskdump_regs_ppc(struct bt_info *bt, ulong *eip, ulong *esp)
 {
 	Elf32_Nhdr *note;
 	int len;
 
+	if (KDUMP_CMPRS_VALID())
+		relocate_nt_prstatus_percpu_ppc(dd->nt_prstatus_percpu,
+						&dd->num_prstatus_notes);
 	if (KDUMP_CMPRS_VALID() &&
 		(bt->task == tt->panic_task || 
 		(is_task_active(bt->task) && dd->num_prstatus_notes > 1))) {
@@ -1030,7 +1109,7 @@ get_diskdump_regs_ppc(struct bt_info *bt, ulong *eip, ulong *esp)
 					"panic" : "active", bt->task);
 		len = sizeof(Elf32_Nhdr);
 		len = roundup(len + note->n_namesz, 4);
-		 bt->machdep = (void *)((char *)note + len +
+		bt->machdep = (void *)((char *)note + len +
 			MEMBER_OFFSET("elf_prstatus", "pr_reg"));
 	}
 
diff --git a/netdump.c b/netdump.c
index 51bbc35..6cfc1e9 100644
--- a/netdump.c
+++ b/netdump.c
@@ -2589,12 +2589,15 @@ next_sysrq:
 	machdep->get_stack_frame(bt, eip, esp);
 }
 
+extern void relocate_nt_prstatus_percpu_ppc(void **, int *);
 static void
 get_netdump_regs_ppc(struct bt_info *bt, ulong *eip, ulong *esp)
 {
 	Elf32_Nhdr *note;
 	size_t len;
 
+	relocate_nt_prstatus_percpu_ppc(nd->nt_prstatus_percpu,
+					&nd->num_prstatus_notes);
 	if ((bt->task == tt->panic_task) ||
 		(is_task_active(bt->task) && nd->num_prstatus_notes > 1)) {
 		/*	
diff --git a/ppc.c b/ppc.c
index bc5f201..7c5f3be 100755
--- a/ppc.c
+++ b/ppc.c
@@ -414,6 +414,8 @@ ppc_init(int when)
 			symbol_exists("hardirq_ctx"))
 			STRUCT_SIZE_INIT(irq_ctx, "hardirq_ctx");
 
+		STRUCT_SIZE_INIT(note_buf, "note_buf_t");
+		STRUCT_SIZE_INIT(elf_prstatus, "elf_prstatus");
 		break;
 
 	case POST_INIT:
-- 
1.7.0.4

