| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-02-23 23:56:46 +01:00
										 |  |  |  * Windows crashdump (target specific implementations) | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2018 Virtuozzo International GmbH | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This work is licensed under the terms of the GNU GPL, version 2 or later. | 
					
						
							|  |  |  |  * See the COPYING file in the top-level directory. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "qemu/osdep.h"
 | 
					
						
							|  |  |  | #include "sysemu/dump.h"
 | 
					
						
							|  |  |  | #include "qapi/error.h"
 | 
					
						
							| 
									
										
										
										
											2023-03-15 17:43:13 +00:00
										 |  |  | #include "qemu/error-report.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | #include "qapi/qmp/qerror.h"
 | 
					
						
							| 
									
										
										
										
											2023-02-23 23:56:46 +01:00
										 |  |  | #include "exec/cpu-defs.h"
 | 
					
						
							|  |  |  | #include "hw/core/cpu.h"
 | 
					
						
							|  |  |  | #include "qemu/win_dump_defs.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | #include "win_dump.h"
 | 
					
						
							| 
									
										
										
										
											2023-02-23 23:56:46 +01:00
										 |  |  | #include "cpu.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-23 23:58:16 +01:00
										 |  |  | #if defined(TARGET_X86_64)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool win_dump_available(Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static size_t win_dump_ptr_size(bool x64) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return x64 ? sizeof(uint64_t) : sizeof(uint32_t); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | #define _WIN_DUMP_FIELD(f) (x64 ? h->x64.f : h->x32.f)
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  | #define WIN_DUMP_FIELD(field) _WIN_DUMP_FIELD(field)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | #define _WIN_DUMP_FIELD_PTR(f) (x64 ? (void *)&h->x64.f : (void *)&h->x32.f)
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  | #define WIN_DUMP_FIELD_PTR(field) _WIN_DUMP_FIELD_PTR(field)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | #define _WIN_DUMP_FIELD_SIZE(f) (x64 ? sizeof(h->x64.f) : sizeof(h->x32.f))
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  | #define WIN_DUMP_FIELD_SIZE(field) _WIN_DUMP_FIELD_SIZE(field)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static size_t win_dump_ctx_size(bool x64) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return x64 ? sizeof(WinContext64) : sizeof(WinContext32); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static size_t write_run(uint64_t base_page, uint64_t page_count, | 
					
						
							|  |  |  |         int fd, Error **errp) | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     void *buf; | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     uint64_t addr = base_page << TARGET_PAGE_BITS; | 
					
						
							|  |  |  |     uint64_t size = page_count << TARGET_PAGE_BITS; | 
					
						
							| 
									
										
										
										
											2018-08-29 21:30:56 +03:00
										 |  |  |     uint64_t len, l; | 
					
						
							|  |  |  |     size_t total = 0; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 21:30:56 +03:00
										 |  |  |     while (size) { | 
					
						
							|  |  |  |         len = size; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 21:30:56 +03:00
										 |  |  |         buf = cpu_physical_memory_map(addr, &len, false); | 
					
						
							|  |  |  |         if (!buf) { | 
					
						
							|  |  |  |             error_setg(errp, "win-dump: failed to map physical range" | 
					
						
							|  |  |  |                              " 0x%016" PRIx64 "-0x%016" PRIx64, addr, addr + size - 1); | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 21:30:56 +03:00
										 |  |  |         l = qemu_write_full(fd, buf, len); | 
					
						
							|  |  |  |         cpu_physical_memory_unmap(buf, addr, false, len); | 
					
						
							|  |  |  |         if (l != len) { | 
					
						
							|  |  |  |             error_setg(errp, QERR_IO_ERROR); | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         addr += l; | 
					
						
							|  |  |  |         size -= l; | 
					
						
							|  |  |  |         total += l; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 21:30:56 +03:00
										 |  |  |     return total; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static void write_runs(DumpState *s, WinDumpHeader *h, bool x64, Error **errp) | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     uint64_t BasePage, PageCount; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     Error *local_err = NULL; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     for (i = 0; i < WIN_DUMP_FIELD(PhysicalMemoryBlock.NumberOfRuns); i++) { | 
					
						
							|  |  |  |         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); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |         if (local_err) { | 
					
						
							|  |  |  |             error_propagate(errp, local_err); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static int cpu_read_ptr(bool x64, CPUState *cpu, uint64_t addr, uint64_t *ptr) | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     uint32_t ptr32; | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     uint64_t ptr64; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     ret = cpu_memory_rw_debug(cpu, addr, x64 ? (void *)&ptr64 : (void *)&ptr32, | 
					
						
							|  |  |  |             win_dump_ptr_size(x64), 0); | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     *ptr = x64 ? ptr64 : ptr32; | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static void patch_mm_pfn_database(WinDumpHeader *h, bool x64, Error **errp) | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     if (cpu_memory_rw_debug(first_cpu, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |             WIN_DUMP_FIELD(KdDebuggerDataBlock) + KDBG_MM_PFN_DATABASE_OFFSET, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |             WIN_DUMP_FIELD_PTR(PfnDatabase), | 
					
						
							|  |  |  |             WIN_DUMP_FIELD_SIZE(PfnDatabase), 0)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |         error_setg(errp, "win-dump: failed to read MmPfnDatabase"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static void patch_bugcheck_data(WinDumpHeader *h, bool x64, Error **errp) | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     uint64_t KiBugcheckData; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     if (cpu_read_ptr(x64, first_cpu, | 
					
						
							|  |  |  |             WIN_DUMP_FIELD(KdDebuggerDataBlock) + KDBG_KI_BUGCHECK_DATA_OFFSET, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |             &KiBugcheckData)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |         error_setg(errp, "win-dump: failed to read KiBugcheckData"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     if (cpu_memory_rw_debug(first_cpu, KiBugcheckData, | 
					
						
							|  |  |  |             WIN_DUMP_FIELD(BugcheckData), | 
					
						
							|  |  |  |             WIN_DUMP_FIELD_SIZE(BugcheckData), 0)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |         error_setg(errp, "win-dump: failed to read bugcheck data"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * If BugcheckCode wasn't saved, we consider guest OS as alive. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     if (!WIN_DUMP_FIELD(BugcheckCode)) { | 
					
						
							|  |  |  |         *(uint32_t *)WIN_DUMP_FIELD_PTR(BugcheckCode) = LIVE_SYSTEM_DUMP; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * This routine tries to correct mistakes in crashdump header. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static void patch_header(WinDumpHeader *h, bool x64) | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     Error *local_err = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     if (x64) { | 
					
						
							|  |  |  |         h->x64.RequiredDumpSpace = sizeof(WinDumpHeader64) + | 
					
						
							|  |  |  |             (h->x64.PhysicalMemoryBlock.NumberOfPages << TARGET_PAGE_BITS); | 
					
						
							|  |  |  |         h->x64.PhysicalMemoryBlock.unused = 0; | 
					
						
							|  |  |  |         h->x64.unused1 = 0; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         h->x32.RequiredDumpSpace = sizeof(WinDumpHeader32) + | 
					
						
							|  |  |  |             (h->x32.PhysicalMemoryBlock.NumberOfPages << TARGET_PAGE_BITS); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     patch_mm_pfn_database(h, x64, &local_err); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     if (local_err) { | 
					
						
							|  |  |  |         warn_report_err(local_err); | 
					
						
							|  |  |  |         local_err = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     patch_bugcheck_data(h, x64, &local_err); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     if (local_err) { | 
					
						
							|  |  |  |         warn_report_err(local_err); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static bool check_header(WinDumpHeader *h, bool *x64, Error **errp) | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     const char Signature[] = "PAGE"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (memcmp(h->Signature, Signature, sizeof(h->Signature))) { | 
					
						
							|  |  |  |         error_setg(errp, "win-dump: invalid header, expected '%.4s'," | 
					
						
							|  |  |  |                          " got '%.4s'", Signature, h->Signature); | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     if (!memcmp(h->ValidDump, "DUMP", sizeof(h->ValidDump))) { | 
					
						
							|  |  |  |         *x64 = false; | 
					
						
							|  |  |  |     } else if (!memcmp(h->ValidDump, "DU64", sizeof(h->ValidDump))) { | 
					
						
							|  |  |  |         *x64 = true; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         error_setg(errp, "win-dump: invalid header, expected 'DUMP' or 'DU64'," | 
					
						
							|  |  |  |                    " got '%.4s'", h->ValidDump); | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static void check_kdbg(WinDumpHeader *h, bool x64, Error **errp) | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     const char OwnerTag[] = "KDBG"; | 
					
						
							|  |  |  |     char read_OwnerTag[4]; | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     uint64_t KdDebuggerDataBlock = WIN_DUMP_FIELD(KdDebuggerDataBlock); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:41 +03:00
										 |  |  |     bool try_fallback = true; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:41 +03:00
										 |  |  | try_again: | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     if (cpu_memory_rw_debug(first_cpu, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |             KdDebuggerDataBlock + KDBG_OWNER_TAG_OFFSET, | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |             (uint8_t *)&read_OwnerTag, sizeof(read_OwnerTag), 0)) { | 
					
						
							|  |  |  |         error_setg(errp, "win-dump: failed to read OwnerTag"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (memcmp(read_OwnerTag, OwnerTag, sizeof(read_OwnerTag))) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:41 +03:00
										 |  |  |         if (try_fallback) { | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * If attempt to use original KDBG failed | 
					
						
							|  |  |  |              * (most likely because of its encryption), | 
					
						
							|  |  |  |              * we try to use KDBG obtained by guest driver. | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |             KdDebuggerDataBlock = WIN_DUMP_FIELD(BugcheckParameter1); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:41 +03:00
										 |  |  |             try_fallback = false; | 
					
						
							|  |  |  |             goto try_again; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             error_setg(errp, "win-dump: invalid KDBG OwnerTag," | 
					
						
							|  |  |  |                              " expected '%.4s', got '%.4s'", | 
					
						
							|  |  |  |                              OwnerTag, read_OwnerTag); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:41 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     if (x64) { | 
					
						
							|  |  |  |         h->x64.KdDebuggerDataBlock = KdDebuggerDataBlock; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         h->x32.KdDebuggerDataBlock = KdDebuggerDataBlock; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  | struct saved_context { | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     WinContext ctx; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |     uint64_t addr; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static void patch_and_save_context(WinDumpHeader *h, bool x64, | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |                                    struct saved_context *saved_ctx, | 
					
						
							|  |  |  |                                    Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     uint64_t KdDebuggerDataBlock = WIN_DUMP_FIELD(KdDebuggerDataBlock); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |     uint64_t KiProcessorBlock; | 
					
						
							|  |  |  |     uint16_t OffsetPrcbContext; | 
					
						
							|  |  |  |     CPUState *cpu; | 
					
						
							|  |  |  |     int i = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     if (cpu_read_ptr(x64, first_cpu, | 
					
						
							|  |  |  |             KdDebuggerDataBlock + KDBG_KI_PROCESSOR_BLOCK_OFFSET, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |             &KiProcessorBlock)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |         error_setg(errp, "win-dump: failed to read KiProcessorBlock"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (cpu_memory_rw_debug(first_cpu, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |             KdDebuggerDataBlock + KDBG_OFFSET_PRCB_CONTEXT_OFFSET, | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |             (uint8_t *)&OffsetPrcbContext, sizeof(OffsetPrcbContext), 0)) { | 
					
						
							|  |  |  |         error_setg(errp, "win-dump: failed to read OffsetPrcbContext"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CPU_FOREACH(cpu) { | 
					
						
							|  |  |  |         X86CPU *x86_cpu = X86_CPU(cpu); | 
					
						
							|  |  |  |         CPUX86State *env = &x86_cpu->env; | 
					
						
							|  |  |  |         uint64_t Prcb; | 
					
						
							|  |  |  |         uint64_t Context; | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |         WinContext ctx; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-20 02:59:48 +03:00
										 |  |  |         if (i >= WIN_DUMP_FIELD(NumberProcessors)) { | 
					
						
							|  |  |  |             warn_report("win-dump: number of QEMU CPUs is bigger than" | 
					
						
							|  |  |  |                         " NumberProcessors (%u) in guest Windows", | 
					
						
							|  |  |  |                         WIN_DUMP_FIELD(NumberProcessors)); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |         if (cpu_read_ptr(x64, first_cpu, | 
					
						
							|  |  |  |                 KiProcessorBlock + i * win_dump_ptr_size(x64), | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |                 &Prcb)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |             error_setg(errp, "win-dump: failed to read" | 
					
						
							|  |  |  |                              " CPU #%d PRCB location", i); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |         if (cpu_read_ptr(x64, first_cpu, | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |                 Prcb + OffsetPrcbContext, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |                 &Context)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |             error_setg(errp, "win-dump: failed to read" | 
					
						
							|  |  |  |                              " CPU #%d ContextFrame location", i); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         saved_ctx[i].addr = Context; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |         if (x64) { | 
					
						
							|  |  |  |             ctx.x64 = (WinContext64){ | 
					
						
							|  |  |  |                 .ContextFlags = WIN_CTX64_ALL, | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |                 .MxCsr = env->mxcsr, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 .SegEs = env->segs[0].selector, | 
					
						
							|  |  |  |                 .SegCs = env->segs[1].selector, | 
					
						
							|  |  |  |                 .SegSs = env->segs[2].selector, | 
					
						
							|  |  |  |                 .SegDs = env->segs[3].selector, | 
					
						
							|  |  |  |                 .SegFs = env->segs[4].selector, | 
					
						
							|  |  |  |                 .SegGs = env->segs[5].selector, | 
					
						
							|  |  |  |                 .EFlags = cpu_compute_eflags(env), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 .Dr0 = env->dr[0], | 
					
						
							|  |  |  |                 .Dr1 = env->dr[1], | 
					
						
							|  |  |  |                 .Dr2 = env->dr[2], | 
					
						
							|  |  |  |                 .Dr3 = env->dr[3], | 
					
						
							|  |  |  |                 .Dr6 = env->dr[6], | 
					
						
							|  |  |  |                 .Dr7 = env->dr[7], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 .Rax = env->regs[R_EAX], | 
					
						
							|  |  |  |                 .Rbx = env->regs[R_EBX], | 
					
						
							|  |  |  |                 .Rcx = env->regs[R_ECX], | 
					
						
							|  |  |  |                 .Rdx = env->regs[R_EDX], | 
					
						
							|  |  |  |                 .Rsp = env->regs[R_ESP], | 
					
						
							|  |  |  |                 .Rbp = env->regs[R_EBP], | 
					
						
							|  |  |  |                 .Rsi = env->regs[R_ESI], | 
					
						
							|  |  |  |                 .Rdi = env->regs[R_EDI], | 
					
						
							|  |  |  |                 .R8  = env->regs[8], | 
					
						
							|  |  |  |                 .R9  = env->regs[9], | 
					
						
							|  |  |  |                 .R10 = env->regs[10], | 
					
						
							|  |  |  |                 .R11 = env->regs[11], | 
					
						
							|  |  |  |                 .R12 = env->regs[12], | 
					
						
							|  |  |  |                 .R13 = env->regs[13], | 
					
						
							|  |  |  |                 .R14 = env->regs[14], | 
					
						
							|  |  |  |                 .R15 = env->regs[15], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 .Rip = env->eip, | 
					
						
							|  |  |  |                 .FltSave = { | 
					
						
							|  |  |  |                     .MxCsr = env->mxcsr, | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             ctx.x32 = (WinContext32){ | 
					
						
							|  |  |  |                 .ContextFlags = WIN_CTX32_FULL | WIN_CTX_DBG, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 .SegEs = env->segs[0].selector, | 
					
						
							|  |  |  |                 .SegCs = env->segs[1].selector, | 
					
						
							|  |  |  |                 .SegSs = env->segs[2].selector, | 
					
						
							|  |  |  |                 .SegDs = env->segs[3].selector, | 
					
						
							|  |  |  |                 .SegFs = env->segs[4].selector, | 
					
						
							|  |  |  |                 .SegGs = env->segs[5].selector, | 
					
						
							|  |  |  |                 .EFlags = cpu_compute_eflags(env), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 .Dr0 = env->dr[0], | 
					
						
							|  |  |  |                 .Dr1 = env->dr[1], | 
					
						
							|  |  |  |                 .Dr2 = env->dr[2], | 
					
						
							|  |  |  |                 .Dr3 = env->dr[3], | 
					
						
							|  |  |  |                 .Dr6 = env->dr[6], | 
					
						
							|  |  |  |                 .Dr7 = env->dr[7], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 .Eax = env->regs[R_EAX], | 
					
						
							|  |  |  |                 .Ebx = env->regs[R_EBX], | 
					
						
							|  |  |  |                 .Ecx = env->regs[R_ECX], | 
					
						
							|  |  |  |                 .Edx = env->regs[R_EDX], | 
					
						
							|  |  |  |                 .Esp = env->regs[R_ESP], | 
					
						
							|  |  |  |                 .Ebp = env->regs[R_EBP], | 
					
						
							|  |  |  |                 .Esi = env->regs[R_ESI], | 
					
						
							|  |  |  |                 .Edi = env->regs[R_EDI], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 .Eip = env->eip, | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (cpu_memory_rw_debug(first_cpu, Context, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |                 &saved_ctx[i].ctx, win_dump_ctx_size(x64), 0)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |             error_setg(errp, "win-dump: failed to save CPU #%d context", i); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (cpu_memory_rw_debug(first_cpu, Context, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |                 &ctx, win_dump_ctx_size(x64), 1)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |             error_setg(errp, "win-dump: failed to write CPU #%d context", i); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         i++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  | static void restore_context(WinDumpHeader *h, bool x64, | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |                             struct saved_context *saved_ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     for (i = 0; i < WIN_DUMP_FIELD(NumberProcessors); i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |         if (cpu_memory_rw_debug(first_cpu, saved_ctx[i].addr, | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |                 &saved_ctx[i].ctx, win_dump_ctx_size(x64), 1)) { | 
					
						
							| 
									
										
										
										
											2020-03-24 18:36:27 +03:00
										 |  |  |             warn_report("win-dump: failed to restore CPU #%d context", i); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | void create_win_dump(DumpState *s, Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     WinDumpHeader *h = (void *)(s->guest_note + VMCOREINFO_ELF_NOTE_HDR_SIZE); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:40 +03:00
										 |  |  |     X86CPU *first_x86_cpu = X86_CPU(first_cpu); | 
					
						
							|  |  |  |     uint64_t saved_cr3 = first_x86_cpu->env.cr[3]; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |     struct saved_context *saved_ctx = NULL; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     Error *local_err = NULL; | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     bool x64 = true; | 
					
						
							|  |  |  |     size_t hdr_size; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     if (s->guest_note_size != VMCOREINFO_WIN_DUMP_NOTE_SIZE32 && | 
					
						
							|  |  |  |             s->guest_note_size != VMCOREINFO_WIN_DUMP_NOTE_SIZE64) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |         error_setg(errp, "win-dump: invalid vmcoreinfo note size"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     if (!check_header(h, &x64, &local_err)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |         error_propagate(errp, local_err); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     hdr_size = x64 ? sizeof(WinDumpHeader64) : sizeof(WinDumpHeader32); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:40 +03:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Further access to kernel structures by virtual addresses | 
					
						
							|  |  |  |      * should be made from system context. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     first_x86_cpu->env.cr[3] = WIN_DUMP_FIELD(DirectoryTableBase); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     check_kdbg(h, x64, &local_err); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     if (local_err) { | 
					
						
							|  |  |  |         error_propagate(errp, local_err); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:40 +03:00
										 |  |  |         goto out_cr3; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     patch_header(h, x64); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     saved_ctx = g_new(struct saved_context, WIN_DUMP_FIELD(NumberProcessors)); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Always patch context because there is no way | 
					
						
							|  |  |  |      * to determine if the system-saved context is valid | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     patch_and_save_context(h, x64, saved_ctx, &local_err); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |     if (local_err) { | 
					
						
							|  |  |  |         error_propagate(errp, local_err); | 
					
						
							|  |  |  |         goto out_free; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:56 +03:00
										 |  |  |     s->total_size = WIN_DUMP_FIELD(RequiredDumpSpace); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     s->written_size = qemu_write_full(s->fd, h, hdr_size); | 
					
						
							|  |  |  |     if (s->written_size != hdr_size) { | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |         error_setg(errp, QERR_IO_ERROR); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |         goto out_restore; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     write_runs(s, h, x64, &local_err); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     if (local_err) { | 
					
						
							|  |  |  |         error_propagate(errp, local_err); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  |         goto out_restore; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  | out_restore: | 
					
						
							| 
									
										
										
										
											2022-04-06 20:15:58 +03:00
										 |  |  |     restore_context(h, x64, saved_ctx); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:42 +03:00
										 |  |  | out_free: | 
					
						
							|  |  |  |     g_free(saved_ctx); | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:40 +03:00
										 |  |  | out_cr3: | 
					
						
							|  |  |  |     first_x86_cpu->env.cr[3] = saved_cr3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2018-05-17 19:23:39 +03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-02-23 23:58:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #else /* !TARGET_X86_64 */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool win_dump_available(Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     error_setg(errp, "Windows dump is only available for x86-64"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-23 23:59:19 +01:00
										 |  |  | void create_win_dump(DumpState *s, Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     win_dump_available(errp); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-23 23:58:16 +01:00
										 |  |  | #endif
 |