dump/win_dump: add helper macros for Windows dump header access

Perform read access to Windows dump header fields via helper macros.
This is preparation for the next 32-bit guest Windows dump support.

Signed-off-by: Viktor Prutyanov <viktor.prutyanov@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20220406171558.199263-3-viktor.prutyanov@redhat.com>
This commit is contained in:
Viktor Prutyanov 2022-04-06 20:15:56 +03:00 committed by Marc-André Lureau
parent a64b4e179a
commit fb21efe99a

View File

@ -23,11 +23,25 @@
#include "hw/misc/vmcoreinfo.h" #include "hw/misc/vmcoreinfo.h"
#include "win_dump.h" #include "win_dump.h"
static size_t write_run(WinDumpPhyMemRun64 *run, int fd, Error **errp) #define WIN_DUMP_PTR_SIZE sizeof(uint64_t)
#define _WIN_DUMP_FIELD(f) (h->f)
#define WIN_DUMP_FIELD(field) _WIN_DUMP_FIELD(field)
#define _WIN_DUMP_FIELD_PTR(f) ((void *)&h->f)
#define WIN_DUMP_FIELD_PTR(field) _WIN_DUMP_FIELD_PTR(field)
#define _WIN_DUMP_FIELD_SIZE(f) sizeof(h->f)
#define WIN_DUMP_FIELD_SIZE(field) _WIN_DUMP_FIELD_SIZE(field)
#define WIN_DUMP_CTX_SIZE sizeof(WinContext64)
static size_t write_run(uint64_t base_page, uint64_t page_count,
int fd, Error **errp)
{ {
void *buf; void *buf;
uint64_t addr = run->BasePage << TARGET_PAGE_BITS; uint64_t addr = base_page << TARGET_PAGE_BITS;
uint64_t size = run->PageCount << TARGET_PAGE_BITS; uint64_t size = page_count << TARGET_PAGE_BITS;
uint64_t len, l; uint64_t len, l;
size_t total = 0; size_t total = 0;
@ -58,13 +72,14 @@ static size_t write_run(WinDumpPhyMemRun64 *run, int fd, Error **errp)
static void write_runs(DumpState *s, WinDumpHeader64 *h, Error **errp) static void write_runs(DumpState *s, WinDumpHeader64 *h, Error **errp)
{ {
WinDumpPhyMemDesc64 *desc = &h->PhysicalMemoryBlock; uint64_t BasePage, PageCount;
WinDumpPhyMemRun64 *run = desc->Run;
Error *local_err = NULL; Error *local_err = NULL;
int i; int i;
for (i = 0; i < desc->NumberOfRuns; i++) { for (i = 0; i < WIN_DUMP_FIELD(PhysicalMemoryBlock.NumberOfRuns); i++) {
s->written_size += write_run(run + i, s->fd, &local_err); BasePage = WIN_DUMP_FIELD(PhysicalMemoryBlock.Run[i].BasePage);
PageCount = WIN_DUMP_FIELD(PhysicalMemoryBlock.Run[i].PageCount);
s->written_size += write_run(BasePage, PageCount, s->fd, &local_err);
if (local_err) { if (local_err) {
error_propagate(errp, local_err); error_propagate(errp, local_err);
return; return;
@ -72,11 +87,24 @@ static void write_runs(DumpState *s, WinDumpHeader64 *h, Error **errp)
} }
} }
static int cpu_read_ptr(CPUState *cpu, uint64_t addr, uint64_t *ptr)
{
int ret;
uint64_t ptr64;
ret = cpu_memory_rw_debug(cpu, addr, &ptr64, WIN_DUMP_PTR_SIZE, 0);
*ptr = ptr64;
return ret;
}
static void patch_mm_pfn_database(WinDumpHeader64 *h, Error **errp) static void patch_mm_pfn_database(WinDumpHeader64 *h, Error **errp)
{ {
if (cpu_memory_rw_debug(first_cpu, if (cpu_memory_rw_debug(first_cpu,
h->KdDebuggerDataBlock + KDBG_MM_PFN_DATABASE_OFFSET64, WIN_DUMP_FIELD(KdDebuggerDataBlock) + KDBG_MM_PFN_DATABASE_OFFSET64,
(uint8_t *)&h->PfnDatabase, sizeof(h->PfnDatabase), 0)) { WIN_DUMP_FIELD_PTR(PfnDatabase),
WIN_DUMP_FIELD_SIZE(PfnDatabase), 0)) {
error_setg(errp, "win-dump: failed to read MmPfnDatabase"); error_setg(errp, "win-dump: failed to read MmPfnDatabase");
return; return;
} }
@ -86,16 +114,17 @@ static void patch_bugcheck_data(WinDumpHeader64 *h, Error **errp)
{ {
uint64_t KiBugcheckData; uint64_t KiBugcheckData;
if (cpu_memory_rw_debug(first_cpu, if (cpu_read_ptr(first_cpu,
h->KdDebuggerDataBlock + KDBG_KI_BUGCHECK_DATA_OFFSET64, WIN_DUMP_FIELD(KdDebuggerDataBlock) +
(uint8_t *)&KiBugcheckData, sizeof(KiBugcheckData), 0)) { KDBG_KI_BUGCHECK_DATA_OFFSET64,
&KiBugcheckData)) {
error_setg(errp, "win-dump: failed to read KiBugcheckData"); error_setg(errp, "win-dump: failed to read KiBugcheckData");
return; return;
} }
if (cpu_memory_rw_debug(first_cpu, if (cpu_memory_rw_debug(first_cpu, KiBugcheckData,
KiBugcheckData, WIN_DUMP_FIELD(BugcheckData),
h->BugcheckData, sizeof(h->BugcheckData), 0)) { WIN_DUMP_FIELD_SIZE(BugcheckData), 0)) {
error_setg(errp, "win-dump: failed to read bugcheck data"); error_setg(errp, "win-dump: failed to read bugcheck data");
return; return;
} }
@ -104,8 +133,8 @@ static void patch_bugcheck_data(WinDumpHeader64 *h, Error **errp)
* If BugcheckCode wasn't saved, we consider guest OS as alive. * If BugcheckCode wasn't saved, we consider guest OS as alive.
*/ */
if (!h->BugcheckCode) { if (!WIN_DUMP_FIELD(BugcheckCode)) {
h->BugcheckCode = LIVE_SYSTEM_DUMP; *(uint32_t *)WIN_DUMP_FIELD_PTR(BugcheckCode) = LIVE_SYSTEM_DUMP;
} }
} }
@ -154,7 +183,7 @@ static void check_kdbg(WinDumpHeader64 *h, Error **errp)
{ {
const char OwnerTag[] = "KDBG"; const char OwnerTag[] = "KDBG";
char read_OwnerTag[4]; char read_OwnerTag[4];
uint64_t KdDebuggerDataBlock = h->KdDebuggerDataBlock; uint64_t KdDebuggerDataBlock = WIN_DUMP_FIELD(KdDebuggerDataBlock);
bool try_fallback = true; bool try_fallback = true;
try_again: try_again:
@ -173,7 +202,7 @@ try_again:
* we try to use KDBG obtained by guest driver. * we try to use KDBG obtained by guest driver.
*/ */
KdDebuggerDataBlock = h->BugcheckParameter1; KdDebuggerDataBlock = WIN_DUMP_FIELD(BugcheckParameter1);
try_fallback = false; try_fallback = false;
goto try_again; goto try_again;
} else { } else {
@ -196,20 +225,21 @@ static void patch_and_save_context(WinDumpHeader64 *h,
struct saved_context *saved_ctx, struct saved_context *saved_ctx,
Error **errp) Error **errp)
{ {
uint64_t KdDebuggerDataBlock = WIN_DUMP_FIELD(KdDebuggerDataBlock);
uint64_t KiProcessorBlock; uint64_t KiProcessorBlock;
uint16_t OffsetPrcbContext; uint16_t OffsetPrcbContext;
CPUState *cpu; CPUState *cpu;
int i = 0; int i = 0;
if (cpu_memory_rw_debug(first_cpu, if (cpu_read_ptr(first_cpu,
h->KdDebuggerDataBlock + KDBG_KI_PROCESSOR_BLOCK_OFFSET64, KdDebuggerDataBlock + KDBG_KI_PROCESSOR_BLOCK_OFFSET64,
(uint8_t *)&KiProcessorBlock, sizeof(KiProcessorBlock), 0)) { &KiProcessorBlock)) {
error_setg(errp, "win-dump: failed to read KiProcessorBlock"); error_setg(errp, "win-dump: failed to read KiProcessorBlock");
return; return;
} }
if (cpu_memory_rw_debug(first_cpu, if (cpu_memory_rw_debug(first_cpu,
h->KdDebuggerDataBlock + KDBG_OFFSET_PRCB_CONTEXT_OFFSET64, KdDebuggerDataBlock + KDBG_OFFSET_PRCB_CONTEXT_OFFSET64,
(uint8_t *)&OffsetPrcbContext, sizeof(OffsetPrcbContext), 0)) { (uint8_t *)&OffsetPrcbContext, sizeof(OffsetPrcbContext), 0)) {
error_setg(errp, "win-dump: failed to read OffsetPrcbContext"); error_setg(errp, "win-dump: failed to read OffsetPrcbContext");
return; return;
@ -222,17 +252,17 @@ static void patch_and_save_context(WinDumpHeader64 *h,
uint64_t Context; uint64_t Context;
WinContext64 ctx; WinContext64 ctx;
if (cpu_memory_rw_debug(first_cpu, if (cpu_read_ptr(first_cpu,
KiProcessorBlock + i * sizeof(uint64_t), KiProcessorBlock + i * WIN_DUMP_PTR_SIZE,
(uint8_t *)&Prcb, sizeof(Prcb), 0)) { &Prcb)) {
error_setg(errp, "win-dump: failed to read" error_setg(errp, "win-dump: failed to read"
" CPU #%d PRCB location", i); " CPU #%d PRCB location", i);
return; return;
} }
if (cpu_memory_rw_debug(first_cpu, if (cpu_read_ptr(first_cpu,
Prcb + OffsetPrcbContext, Prcb + OffsetPrcbContext,
(uint8_t *)&Context, sizeof(Context), 0)) { &Context)) {
error_setg(errp, "win-dump: failed to read" error_setg(errp, "win-dump: failed to read"
" CPU #%d ContextFrame location", i); " CPU #%d ContextFrame location", i);
return; return;
@ -283,13 +313,13 @@ static void patch_and_save_context(WinDumpHeader64 *h,
}; };
if (cpu_memory_rw_debug(first_cpu, Context, if (cpu_memory_rw_debug(first_cpu, Context,
(uint8_t *)&saved_ctx[i].ctx, sizeof(WinContext64), 0)) { &saved_ctx[i].ctx, WIN_DUMP_CTX_SIZE, 0)) {
error_setg(errp, "win-dump: failed to save CPU #%d context", i); error_setg(errp, "win-dump: failed to save CPU #%d context", i);
return; return;
} }
if (cpu_memory_rw_debug(first_cpu, Context, if (cpu_memory_rw_debug(first_cpu, Context,
(uint8_t *)&ctx, sizeof(WinContext64), 1)) { &ctx, WIN_DUMP_CTX_SIZE, 1)) {
error_setg(errp, "win-dump: failed to write CPU #%d context", i); error_setg(errp, "win-dump: failed to write CPU #%d context", i);
return; return;
} }
@ -303,9 +333,9 @@ static void restore_context(WinDumpHeader64 *h,
{ {
int i; int i;
for (i = 0; i < h->NumberProcessors; i++) { for (i = 0; i < WIN_DUMP_FIELD(NumberProcessors); i++) {
if (cpu_memory_rw_debug(first_cpu, saved_ctx[i].addr, if (cpu_memory_rw_debug(first_cpu, saved_ctx[i].addr,
(uint8_t *)&saved_ctx[i].ctx, sizeof(WinContext64), 1)) { &saved_ctx[i].ctx, WIN_DUMP_CTX_SIZE, 1)) {
warn_report("win-dump: failed to restore CPU #%d context", i); warn_report("win-dump: failed to restore CPU #%d context", i);
} }
} }
@ -337,7 +367,7 @@ void create_win_dump(DumpState *s, Error **errp)
* should be made from system context. * should be made from system context.
*/ */
first_x86_cpu->env.cr[3] = h->DirectoryTableBase; first_x86_cpu->env.cr[3] = WIN_DUMP_FIELD(DirectoryTableBase);
check_kdbg(h, &local_err); check_kdbg(h, &local_err);
if (local_err) { if (local_err) {
@ -347,7 +377,7 @@ void create_win_dump(DumpState *s, Error **errp)
patch_header(h); patch_header(h);
saved_ctx = g_new(struct saved_context, h->NumberProcessors); saved_ctx = g_new(struct saved_context, WIN_DUMP_FIELD(NumberProcessors));
/* /*
* Always patch context because there is no way * Always patch context because there is no way
@ -360,7 +390,7 @@ void create_win_dump(DumpState *s, Error **errp)
goto out_free; goto out_free;
} }
s->total_size = h->RequiredDumpSpace; s->total_size = WIN_DUMP_FIELD(RequiredDumpSpace);
s->written_size = qemu_write_full(s->fd, h, sizeof(*h)); s->written_size = qemu_write_full(s->fd, h, sizeof(*h));
if (s->written_size != sizeof(*h)) { if (s->written_size != sizeof(*h)) {