win_dump.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Windows crashdump
  3. *
  4. * Copyright (c) 2018 Virtuozzo International GmbH
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qemu/cutils.h"
  12. #include "elf.h"
  13. #include "exec/hwaddr.h"
  14. #include "monitor/monitor.h"
  15. #include "sysemu/kvm.h"
  16. #include "sysemu/dump.h"
  17. #include "sysemu/memory_mapping.h"
  18. #include "sysemu/cpus.h"
  19. #include "qapi/error.h"
  20. #include "qapi/qmp/qerror.h"
  21. #include "qemu/error-report.h"
  22. #include "hw/misc/vmcoreinfo.h"
  23. #include "win_dump.h"
  24. static size_t write_run(WinDumpPhyMemRun64 *run, int fd, Error **errp)
  25. {
  26. void *buf;
  27. uint64_t addr = run->BasePage << TARGET_PAGE_BITS;
  28. uint64_t size = run->PageCount << TARGET_PAGE_BITS;
  29. uint64_t len, l;
  30. size_t total = 0;
  31. while (size) {
  32. len = size;
  33. buf = cpu_physical_memory_map(addr, &len, false);
  34. if (!buf) {
  35. error_setg(errp, "win-dump: failed to map physical range"
  36. " 0x%016" PRIx64 "-0x%016" PRIx64, addr, addr + size - 1);
  37. return 0;
  38. }
  39. l = qemu_write_full(fd, buf, len);
  40. cpu_physical_memory_unmap(buf, addr, false, len);
  41. if (l != len) {
  42. error_setg(errp, QERR_IO_ERROR);
  43. return 0;
  44. }
  45. addr += l;
  46. size -= l;
  47. total += l;
  48. }
  49. return total;
  50. }
  51. static void write_runs(DumpState *s, WinDumpHeader64 *h, Error **errp)
  52. {
  53. WinDumpPhyMemDesc64 *desc = &h->PhysicalMemoryBlock;
  54. WinDumpPhyMemRun64 *run = desc->Run;
  55. Error *local_err = NULL;
  56. int i;
  57. for (i = 0; i < desc->NumberOfRuns; i++) {
  58. s->written_size += write_run(run + i, s->fd, &local_err);
  59. if (local_err) {
  60. error_propagate(errp, local_err);
  61. return;
  62. }
  63. }
  64. }
  65. static void patch_mm_pfn_database(WinDumpHeader64 *h, Error **errp)
  66. {
  67. if (cpu_memory_rw_debug(first_cpu,
  68. h->KdDebuggerDataBlock + KDBG_MM_PFN_DATABASE_OFFSET64,
  69. (uint8_t *)&h->PfnDatabase, sizeof(h->PfnDatabase), 0)) {
  70. error_setg(errp, "win-dump: failed to read MmPfnDatabase");
  71. return;
  72. }
  73. }
  74. static void patch_bugcheck_data(WinDumpHeader64 *h, Error **errp)
  75. {
  76. uint64_t KiBugcheckData;
  77. if (cpu_memory_rw_debug(first_cpu,
  78. h->KdDebuggerDataBlock + KDBG_KI_BUGCHECK_DATA_OFFSET64,
  79. (uint8_t *)&KiBugcheckData, sizeof(KiBugcheckData), 0)) {
  80. error_setg(errp, "win-dump: failed to read KiBugcheckData");
  81. return;
  82. }
  83. if (cpu_memory_rw_debug(first_cpu,
  84. KiBugcheckData,
  85. h->BugcheckData, sizeof(h->BugcheckData), 0)) {
  86. error_setg(errp, "win-dump: failed to read bugcheck data");
  87. return;
  88. }
  89. /*
  90. * If BugcheckCode wasn't saved, we consider guest OS as alive.
  91. */
  92. if (!h->BugcheckCode) {
  93. h->BugcheckCode = LIVE_SYSTEM_DUMP;
  94. }
  95. }
  96. /*
  97. * This routine tries to correct mistakes in crashdump header.
  98. */
  99. static void patch_header(WinDumpHeader64 *h)
  100. {
  101. Error *local_err = NULL;
  102. h->RequiredDumpSpace = sizeof(WinDumpHeader64) +
  103. (h->PhysicalMemoryBlock.NumberOfPages << TARGET_PAGE_BITS);
  104. h->PhysicalMemoryBlock.unused = 0;
  105. h->unused1 = 0;
  106. patch_mm_pfn_database(h, &local_err);
  107. if (local_err) {
  108. warn_report_err(local_err);
  109. local_err = NULL;
  110. }
  111. patch_bugcheck_data(h, &local_err);
  112. if (local_err) {
  113. warn_report_err(local_err);
  114. }
  115. }
  116. static void check_header(WinDumpHeader64 *h, Error **errp)
  117. {
  118. const char Signature[] = "PAGE";
  119. const char ValidDump[] = "DU64";
  120. if (memcmp(h->Signature, Signature, sizeof(h->Signature))) {
  121. error_setg(errp, "win-dump: invalid header, expected '%.4s',"
  122. " got '%.4s'", Signature, h->Signature);
  123. return;
  124. }
  125. if (memcmp(h->ValidDump, ValidDump, sizeof(h->ValidDump))) {
  126. error_setg(errp, "win-dump: invalid header, expected '%.4s',"
  127. " got '%.4s'", ValidDump, h->ValidDump);
  128. return;
  129. }
  130. }
  131. static void check_kdbg(WinDumpHeader64 *h, Error **errp)
  132. {
  133. const char OwnerTag[] = "KDBG";
  134. char read_OwnerTag[4];
  135. uint64_t KdDebuggerDataBlock = h->KdDebuggerDataBlock;
  136. bool try_fallback = true;
  137. try_again:
  138. if (cpu_memory_rw_debug(first_cpu,
  139. KdDebuggerDataBlock + KDBG_OWNER_TAG_OFFSET64,
  140. (uint8_t *)&read_OwnerTag, sizeof(read_OwnerTag), 0)) {
  141. error_setg(errp, "win-dump: failed to read OwnerTag");
  142. return;
  143. }
  144. if (memcmp(read_OwnerTag, OwnerTag, sizeof(read_OwnerTag))) {
  145. if (try_fallback) {
  146. /*
  147. * If attempt to use original KDBG failed
  148. * (most likely because of its encryption),
  149. * we try to use KDBG obtained by guest driver.
  150. */
  151. KdDebuggerDataBlock = h->BugcheckParameter1;
  152. try_fallback = false;
  153. goto try_again;
  154. } else {
  155. error_setg(errp, "win-dump: invalid KDBG OwnerTag,"
  156. " expected '%.4s', got '%.4s'",
  157. OwnerTag, read_OwnerTag);
  158. return;
  159. }
  160. }
  161. h->KdDebuggerDataBlock = KdDebuggerDataBlock;
  162. }
  163. struct saved_context {
  164. WinContext ctx;
  165. uint64_t addr;
  166. };
  167. static void patch_and_save_context(WinDumpHeader64 *h,
  168. struct saved_context *saved_ctx,
  169. Error **errp)
  170. {
  171. uint64_t KiProcessorBlock;
  172. uint16_t OffsetPrcbContext;
  173. CPUState *cpu;
  174. int i = 0;
  175. if (cpu_memory_rw_debug(first_cpu,
  176. h->KdDebuggerDataBlock + KDBG_KI_PROCESSOR_BLOCK_OFFSET64,
  177. (uint8_t *)&KiProcessorBlock, sizeof(KiProcessorBlock), 0)) {
  178. error_setg(errp, "win-dump: failed to read KiProcessorBlock");
  179. return;
  180. }
  181. if (cpu_memory_rw_debug(first_cpu,
  182. h->KdDebuggerDataBlock + KDBG_OFFSET_PRCB_CONTEXT_OFFSET64,
  183. (uint8_t *)&OffsetPrcbContext, sizeof(OffsetPrcbContext), 0)) {
  184. error_setg(errp, "win-dump: failed to read OffsetPrcbContext");
  185. return;
  186. }
  187. CPU_FOREACH(cpu) {
  188. X86CPU *x86_cpu = X86_CPU(cpu);
  189. CPUX86State *env = &x86_cpu->env;
  190. uint64_t Prcb;
  191. uint64_t Context;
  192. WinContext ctx;
  193. if (cpu_memory_rw_debug(first_cpu,
  194. KiProcessorBlock + i * sizeof(uint64_t),
  195. (uint8_t *)&Prcb, sizeof(Prcb), 0)) {
  196. error_setg(errp, "win-dump: failed to read"
  197. " CPU #%d PRCB location", i);
  198. return;
  199. }
  200. if (cpu_memory_rw_debug(first_cpu,
  201. Prcb + OffsetPrcbContext,
  202. (uint8_t *)&Context, sizeof(Context), 0)) {
  203. error_setg(errp, "win-dump: failed to read"
  204. " CPU #%d ContextFrame location", i);
  205. return;
  206. }
  207. saved_ctx[i].addr = Context;
  208. ctx = (WinContext){
  209. .ContextFlags = WIN_CTX_ALL,
  210. .MxCsr = env->mxcsr,
  211. .SegEs = env->segs[0].selector,
  212. .SegCs = env->segs[1].selector,
  213. .SegSs = env->segs[2].selector,
  214. .SegDs = env->segs[3].selector,
  215. .SegFs = env->segs[4].selector,
  216. .SegGs = env->segs[5].selector,
  217. .EFlags = cpu_compute_eflags(env),
  218. .Dr0 = env->dr[0],
  219. .Dr1 = env->dr[1],
  220. .Dr2 = env->dr[2],
  221. .Dr3 = env->dr[3],
  222. .Dr6 = env->dr[6],
  223. .Dr7 = env->dr[7],
  224. .Rax = env->regs[R_EAX],
  225. .Rbx = env->regs[R_EBX],
  226. .Rcx = env->regs[R_ECX],
  227. .Rdx = env->regs[R_EDX],
  228. .Rsp = env->regs[R_ESP],
  229. .Rbp = env->regs[R_EBP],
  230. .Rsi = env->regs[R_ESI],
  231. .Rdi = env->regs[R_EDI],
  232. .R8 = env->regs[8],
  233. .R9 = env->regs[9],
  234. .R10 = env->regs[10],
  235. .R11 = env->regs[11],
  236. .R12 = env->regs[12],
  237. .R13 = env->regs[13],
  238. .R14 = env->regs[14],
  239. .R15 = env->regs[15],
  240. .Rip = env->eip,
  241. .FltSave = {
  242. .MxCsr = env->mxcsr,
  243. },
  244. };
  245. if (cpu_memory_rw_debug(first_cpu, Context,
  246. (uint8_t *)&saved_ctx[i].ctx, sizeof(WinContext), 0)) {
  247. error_setg(errp, "win-dump: failed to save CPU #%d context", i);
  248. return;
  249. }
  250. if (cpu_memory_rw_debug(first_cpu, Context,
  251. (uint8_t *)&ctx, sizeof(WinContext), 1)) {
  252. error_setg(errp, "win-dump: failed to write CPU #%d context", i);
  253. return;
  254. }
  255. i++;
  256. }
  257. }
  258. static void restore_context(WinDumpHeader64 *h,
  259. struct saved_context *saved_ctx)
  260. {
  261. int i;
  262. for (i = 0; i < h->NumberProcessors; i++) {
  263. if (cpu_memory_rw_debug(first_cpu, saved_ctx[i].addr,
  264. (uint8_t *)&saved_ctx[i].ctx, sizeof(WinContext), 1)) {
  265. warn_report("win-dump: failed to restore CPU #%d context", i);
  266. }
  267. }
  268. }
  269. void create_win_dump(DumpState *s, Error **errp)
  270. {
  271. WinDumpHeader64 *h = (WinDumpHeader64 *)(s->guest_note +
  272. VMCOREINFO_ELF_NOTE_HDR_SIZE);
  273. X86CPU *first_x86_cpu = X86_CPU(first_cpu);
  274. uint64_t saved_cr3 = first_x86_cpu->env.cr[3];
  275. struct saved_context *saved_ctx = NULL;
  276. Error *local_err = NULL;
  277. if (s->guest_note_size != sizeof(WinDumpHeader64) +
  278. VMCOREINFO_ELF_NOTE_HDR_SIZE) {
  279. error_setg(errp, "win-dump: invalid vmcoreinfo note size");
  280. return;
  281. }
  282. check_header(h, &local_err);
  283. if (local_err) {
  284. error_propagate(errp, local_err);
  285. return;
  286. }
  287. /*
  288. * Further access to kernel structures by virtual addresses
  289. * should be made from system context.
  290. */
  291. first_x86_cpu->env.cr[3] = h->DirectoryTableBase;
  292. check_kdbg(h, &local_err);
  293. if (local_err) {
  294. error_propagate(errp, local_err);
  295. goto out_cr3;
  296. }
  297. patch_header(h);
  298. saved_ctx = g_new(struct saved_context, h->NumberProcessors);
  299. /*
  300. * Always patch context because there is no way
  301. * to determine if the system-saved context is valid
  302. */
  303. patch_and_save_context(h, saved_ctx, &local_err);
  304. if (local_err) {
  305. error_propagate(errp, local_err);
  306. goto out_free;
  307. }
  308. s->total_size = h->RequiredDumpSpace;
  309. s->written_size = qemu_write_full(s->fd, h, sizeof(*h));
  310. if (s->written_size != sizeof(*h)) {
  311. error_setg(errp, QERR_IO_ERROR);
  312. goto out_restore;
  313. }
  314. write_runs(s, h, &local_err);
  315. if (local_err) {
  316. error_propagate(errp, local_err);
  317. goto out_restore;
  318. }
  319. out_restore:
  320. restore_context(h, saved_ctx);
  321. out_free:
  322. g_free(saved_ctx);
  323. out_cr3:
  324. first_x86_cpu->env.cr[3] = saved_cr3;
  325. return;
  326. }