dump.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  1. /*
  2. * QEMU dump
  3. *
  4. * Copyright Fujitsu, Corp. 2011, 2012
  5. *
  6. * Authors:
  7. * Wen Congyang <wency@cn.fujitsu.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qemu/cutils.h"
  15. #include "elf.h"
  16. #include "qemu/bswap.h"
  17. #include "exec/target_page.h"
  18. #include "monitor/monitor.h"
  19. #include "system/dump.h"
  20. #include "system/runstate.h"
  21. #include "system/cpus.h"
  22. #include "qapi/error.h"
  23. #include "qapi/qapi-commands-dump.h"
  24. #include "qapi/qapi-events-dump.h"
  25. #include "qapi/qmp/qerror.h"
  26. #include "qemu/error-report.h"
  27. #include "qemu/main-loop.h"
  28. #include "hw/misc/vmcoreinfo.h"
  29. #include "migration/blocker.h"
  30. #include "hw/core/cpu.h"
  31. #include "win_dump.h"
  32. #include "qemu/range.h"
  33. #include <zlib.h>
  34. #ifdef CONFIG_LZO
  35. #include <lzo/lzo1x.h>
  36. #endif
  37. #ifdef CONFIG_SNAPPY
  38. #include <snappy-c.h>
  39. #endif
  40. #ifndef ELF_MACHINE_UNAME
  41. #define ELF_MACHINE_UNAME "Unknown"
  42. #endif
  43. #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
  44. static Error *dump_migration_blocker;
  45. #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
  46. ((DIV_ROUND_UP((hdr_size), 4) + \
  47. DIV_ROUND_UP((name_size), 4) + \
  48. DIV_ROUND_UP((desc_size), 4)) * 4)
  49. static inline bool dump_is_64bit(DumpState *s)
  50. {
  51. return s->dump_info.d_class == ELFCLASS64;
  52. }
  53. static inline bool dump_has_filter(DumpState *s)
  54. {
  55. return s->filter_area_length > 0;
  56. }
  57. uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
  58. {
  59. if (s->dump_info.d_endian == ELFDATA2LSB) {
  60. val = cpu_to_le16(val);
  61. } else {
  62. val = cpu_to_be16(val);
  63. }
  64. return val;
  65. }
  66. uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
  67. {
  68. if (s->dump_info.d_endian == ELFDATA2LSB) {
  69. val = cpu_to_le32(val);
  70. } else {
  71. val = cpu_to_be32(val);
  72. }
  73. return val;
  74. }
  75. uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
  76. {
  77. if (s->dump_info.d_endian == ELFDATA2LSB) {
  78. val = cpu_to_le64(val);
  79. } else {
  80. val = cpu_to_be64(val);
  81. }
  82. return val;
  83. }
  84. static int dump_cleanup(DumpState *s)
  85. {
  86. if (s->dump_info.arch_cleanup_fn) {
  87. s->dump_info.arch_cleanup_fn(s);
  88. }
  89. guest_phys_blocks_free(&s->guest_phys_blocks);
  90. memory_mapping_list_free(&s->list);
  91. close(s->fd);
  92. g_free(s->guest_note);
  93. g_clear_pointer(&s->string_table_buf, g_array_unref);
  94. s->guest_note = NULL;
  95. if (s->resume) {
  96. if (s->detached) {
  97. bql_lock();
  98. }
  99. vm_start();
  100. if (s->detached) {
  101. bql_unlock();
  102. }
  103. }
  104. migrate_del_blocker(&dump_migration_blocker);
  105. return 0;
  106. }
  107. static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
  108. {
  109. DumpState *s = opaque;
  110. size_t written_size;
  111. written_size = qemu_write_full(s->fd, buf, size);
  112. if (written_size != size) {
  113. return -errno;
  114. }
  115. return 0;
  116. }
  117. static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
  118. {
  119. /*
  120. * phnum in the elf header is 16 bit, if we have more segments we
  121. * set phnum to PN_XNUM and write the real number of segments to a
  122. * special section.
  123. */
  124. uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
  125. memset(elf_header, 0, sizeof(Elf64_Ehdr));
  126. memcpy(elf_header, ELFMAG, SELFMAG);
  127. elf_header->e_ident[EI_CLASS] = ELFCLASS64;
  128. elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
  129. elf_header->e_ident[EI_VERSION] = EV_CURRENT;
  130. elf_header->e_type = cpu_to_dump16(s, ET_CORE);
  131. elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
  132. elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
  133. elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
  134. elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
  135. elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
  136. elf_header->e_phnum = cpu_to_dump16(s, phnum);
  137. elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
  138. elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
  139. elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
  140. elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
  141. }
  142. static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
  143. {
  144. /*
  145. * phnum in the elf header is 16 bit, if we have more segments we
  146. * set phnum to PN_XNUM and write the real number of segments to a
  147. * special section.
  148. */
  149. uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
  150. memset(elf_header, 0, sizeof(Elf32_Ehdr));
  151. memcpy(elf_header, ELFMAG, SELFMAG);
  152. elf_header->e_ident[EI_CLASS] = ELFCLASS32;
  153. elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
  154. elf_header->e_ident[EI_VERSION] = EV_CURRENT;
  155. elf_header->e_type = cpu_to_dump16(s, ET_CORE);
  156. elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
  157. elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
  158. elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
  159. elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
  160. elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
  161. elf_header->e_phnum = cpu_to_dump16(s, phnum);
  162. elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
  163. elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
  164. elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
  165. elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
  166. }
  167. static void write_elf_header(DumpState *s, Error **errp)
  168. {
  169. Elf32_Ehdr elf32_header;
  170. Elf64_Ehdr elf64_header;
  171. size_t header_size;
  172. void *header_ptr;
  173. int ret;
  174. /* The NULL header and the shstrtab are always defined */
  175. assert(s->shdr_num >= 2);
  176. if (dump_is_64bit(s)) {
  177. prepare_elf64_header(s, &elf64_header);
  178. header_size = sizeof(elf64_header);
  179. header_ptr = &elf64_header;
  180. } else {
  181. prepare_elf32_header(s, &elf32_header);
  182. header_size = sizeof(elf32_header);
  183. header_ptr = &elf32_header;
  184. }
  185. ret = fd_write_vmcore(header_ptr, header_size, s);
  186. if (ret < 0) {
  187. error_setg_errno(errp, -ret, "dump: failed to write elf header");
  188. }
  189. }
  190. static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
  191. int phdr_index, hwaddr offset,
  192. hwaddr filesz, Error **errp)
  193. {
  194. Elf64_Phdr phdr;
  195. int ret;
  196. memset(&phdr, 0, sizeof(Elf64_Phdr));
  197. phdr.p_type = cpu_to_dump32(s, PT_LOAD);
  198. phdr.p_offset = cpu_to_dump64(s, offset);
  199. phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
  200. phdr.p_filesz = cpu_to_dump64(s, filesz);
  201. phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
  202. phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
  203. assert(memory_mapping->length >= filesz);
  204. ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
  205. if (ret < 0) {
  206. error_setg_errno(errp, -ret,
  207. "dump: failed to write program header table");
  208. }
  209. }
  210. static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
  211. int phdr_index, hwaddr offset,
  212. hwaddr filesz, Error **errp)
  213. {
  214. Elf32_Phdr phdr;
  215. int ret;
  216. memset(&phdr, 0, sizeof(Elf32_Phdr));
  217. phdr.p_type = cpu_to_dump32(s, PT_LOAD);
  218. phdr.p_offset = cpu_to_dump32(s, offset);
  219. phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
  220. phdr.p_filesz = cpu_to_dump32(s, filesz);
  221. phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
  222. phdr.p_vaddr =
  223. cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
  224. assert(memory_mapping->length >= filesz);
  225. ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
  226. if (ret < 0) {
  227. error_setg_errno(errp, -ret,
  228. "dump: failed to write program header table");
  229. }
  230. }
  231. static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
  232. {
  233. memset(phdr, 0, sizeof(*phdr));
  234. phdr->p_type = cpu_to_dump32(s, PT_NOTE);
  235. phdr->p_offset = cpu_to_dump64(s, s->note_offset);
  236. phdr->p_paddr = 0;
  237. phdr->p_filesz = cpu_to_dump64(s, s->note_size);
  238. phdr->p_memsz = cpu_to_dump64(s, s->note_size);
  239. phdr->p_vaddr = 0;
  240. }
  241. static inline int cpu_index(CPUState *cpu)
  242. {
  243. return cpu->cpu_index + 1;
  244. }
  245. static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
  246. Error **errp)
  247. {
  248. int ret;
  249. if (s->guest_note) {
  250. ret = f(s->guest_note, s->guest_note_size, s);
  251. if (ret < 0) {
  252. error_setg(errp, "dump: failed to write guest note");
  253. }
  254. }
  255. }
  256. static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
  257. Error **errp)
  258. {
  259. CPUState *cpu;
  260. int ret;
  261. int id;
  262. CPU_FOREACH(cpu) {
  263. id = cpu_index(cpu);
  264. ret = cpu_write_elf64_note(f, cpu, id, s);
  265. if (ret < 0) {
  266. error_setg(errp, "dump: failed to write elf notes");
  267. return;
  268. }
  269. }
  270. CPU_FOREACH(cpu) {
  271. ret = cpu_write_elf64_qemunote(f, cpu, s);
  272. if (ret < 0) {
  273. error_setg(errp, "dump: failed to write CPU status");
  274. return;
  275. }
  276. }
  277. write_guest_note(f, s, errp);
  278. }
  279. static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
  280. {
  281. memset(phdr, 0, sizeof(*phdr));
  282. phdr->p_type = cpu_to_dump32(s, PT_NOTE);
  283. phdr->p_offset = cpu_to_dump32(s, s->note_offset);
  284. phdr->p_paddr = 0;
  285. phdr->p_filesz = cpu_to_dump32(s, s->note_size);
  286. phdr->p_memsz = cpu_to_dump32(s, s->note_size);
  287. phdr->p_vaddr = 0;
  288. }
  289. static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
  290. Error **errp)
  291. {
  292. CPUState *cpu;
  293. int ret;
  294. int id;
  295. CPU_FOREACH(cpu) {
  296. id = cpu_index(cpu);
  297. ret = cpu_write_elf32_note(f, cpu, id, s);
  298. if (ret < 0) {
  299. error_setg(errp, "dump: failed to write elf notes");
  300. return;
  301. }
  302. }
  303. CPU_FOREACH(cpu) {
  304. ret = cpu_write_elf32_qemunote(f, cpu, s);
  305. if (ret < 0) {
  306. error_setg(errp, "dump: failed to write CPU status");
  307. return;
  308. }
  309. }
  310. write_guest_note(f, s, errp);
  311. }
  312. static void write_elf_phdr_note(DumpState *s, Error **errp)
  313. {
  314. Elf32_Phdr phdr32;
  315. Elf64_Phdr phdr64;
  316. void *phdr;
  317. size_t size;
  318. int ret;
  319. if (dump_is_64bit(s)) {
  320. prepare_elf64_phdr_note(s, &phdr64);
  321. size = sizeof(phdr64);
  322. phdr = &phdr64;
  323. } else {
  324. prepare_elf32_phdr_note(s, &phdr32);
  325. size = sizeof(phdr32);
  326. phdr = &phdr32;
  327. }
  328. ret = fd_write_vmcore(phdr, size, s);
  329. if (ret < 0) {
  330. error_setg_errno(errp, -ret,
  331. "dump: failed to write program header table");
  332. }
  333. }
  334. static void prepare_elf_section_hdr_zero(DumpState *s)
  335. {
  336. if (dump_is_64bit(s)) {
  337. Elf64_Shdr *shdr64 = s->elf_section_hdrs;
  338. shdr64->sh_info = cpu_to_dump32(s, s->phdr_num);
  339. } else {
  340. Elf32_Shdr *shdr32 = s->elf_section_hdrs;
  341. shdr32->sh_info = cpu_to_dump32(s, s->phdr_num);
  342. }
  343. }
  344. static void prepare_elf_section_hdr_string(DumpState *s, void *buff)
  345. {
  346. uint64_t index = s->string_table_buf->len;
  347. const char strtab[] = ".shstrtab";
  348. Elf32_Shdr shdr32 = {};
  349. Elf64_Shdr shdr64 = {};
  350. int shdr_size;
  351. void *shdr;
  352. g_array_append_vals(s->string_table_buf, strtab, sizeof(strtab));
  353. if (dump_is_64bit(s)) {
  354. shdr_size = sizeof(Elf64_Shdr);
  355. shdr64.sh_type = SHT_STRTAB;
  356. shdr64.sh_offset = s->section_offset + s->elf_section_data_size;
  357. shdr64.sh_name = index;
  358. shdr64.sh_size = s->string_table_buf->len;
  359. shdr = &shdr64;
  360. } else {
  361. shdr_size = sizeof(Elf32_Shdr);
  362. shdr32.sh_type = SHT_STRTAB;
  363. shdr32.sh_offset = s->section_offset + s->elf_section_data_size;
  364. shdr32.sh_name = index;
  365. shdr32.sh_size = s->string_table_buf->len;
  366. shdr = &shdr32;
  367. }
  368. memcpy(buff, shdr, shdr_size);
  369. }
  370. static bool prepare_elf_section_hdrs(DumpState *s, Error **errp)
  371. {
  372. size_t len, sizeof_shdr;
  373. void *buff_hdr;
  374. /*
  375. * Section ordering:
  376. * - HDR zero
  377. * - Arch section hdrs
  378. * - String table hdr
  379. */
  380. sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
  381. len = sizeof_shdr * s->shdr_num;
  382. s->elf_section_hdrs = g_malloc0(len);
  383. buff_hdr = s->elf_section_hdrs;
  384. /*
  385. * The first section header is ALWAYS a special initial section
  386. * header.
  387. *
  388. * The header should be 0 with one exception being that if
  389. * phdr_num is PN_XNUM then the sh_info field contains the real
  390. * number of segment entries.
  391. *
  392. * As we zero allocate the buffer we will only need to modify
  393. * sh_info for the PN_XNUM case.
  394. */
  395. if (s->phdr_num >= PN_XNUM) {
  396. prepare_elf_section_hdr_zero(s);
  397. }
  398. buff_hdr += sizeof_shdr;
  399. /* Add architecture defined section headers */
  400. if (s->dump_info.arch_sections_write_hdr_fn
  401. && s->shdr_num > 2) {
  402. buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr);
  403. if (s->shdr_num >= SHN_LORESERVE) {
  404. error_setg_errno(errp, EINVAL,
  405. "dump: too many architecture defined sections");
  406. return false;
  407. }
  408. }
  409. /*
  410. * String table is the last section since strings are added via
  411. * arch_sections_write_hdr().
  412. */
  413. prepare_elf_section_hdr_string(s, buff_hdr);
  414. return true;
  415. }
  416. static void write_elf_section_headers(DumpState *s, Error **errp)
  417. {
  418. size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
  419. int ret;
  420. if (!prepare_elf_section_hdrs(s, errp)) {
  421. return;
  422. }
  423. ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
  424. if (ret < 0) {
  425. error_setg_errno(errp, -ret, "dump: failed to write section headers");
  426. }
  427. g_free(s->elf_section_hdrs);
  428. }
  429. static void write_elf_sections(DumpState *s, Error **errp)
  430. {
  431. int ret;
  432. if (s->elf_section_data_size) {
  433. /* Write architecture section data */
  434. ret = fd_write_vmcore(s->elf_section_data,
  435. s->elf_section_data_size, s);
  436. if (ret < 0) {
  437. error_setg_errno(errp, -ret,
  438. "dump: failed to write architecture section data");
  439. return;
  440. }
  441. }
  442. /* Write string table */
  443. ret = fd_write_vmcore(s->string_table_buf->data,
  444. s->string_table_buf->len, s);
  445. if (ret < 0) {
  446. error_setg_errno(errp, -ret, "dump: failed to write string table data");
  447. }
  448. }
  449. static void write_data(DumpState *s, void *buf, int length, Error **errp)
  450. {
  451. int ret;
  452. ret = fd_write_vmcore(buf, length, s);
  453. if (ret < 0) {
  454. error_setg_errno(errp, -ret, "dump: failed to save memory");
  455. } else {
  456. s->written_size += length;
  457. }
  458. }
  459. /* write the memory to vmcore. 1 page per I/O. */
  460. static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
  461. int64_t size, Error **errp)
  462. {
  463. ERRP_GUARD();
  464. int64_t i;
  465. for (i = 0; i < size / s->dump_info.page_size; i++) {
  466. write_data(s, block->host_addr + start + i * s->dump_info.page_size,
  467. s->dump_info.page_size, errp);
  468. if (*errp) {
  469. return;
  470. }
  471. }
  472. if ((size % s->dump_info.page_size) != 0) {
  473. write_data(s, block->host_addr + start + i * s->dump_info.page_size,
  474. size % s->dump_info.page_size, errp);
  475. if (*errp) {
  476. return;
  477. }
  478. }
  479. }
  480. /* get the memory's offset and size in the vmcore */
  481. static void get_offset_range(hwaddr phys_addr,
  482. ram_addr_t mapping_length,
  483. DumpState *s,
  484. hwaddr *p_offset,
  485. hwaddr *p_filesz)
  486. {
  487. GuestPhysBlock *block;
  488. hwaddr offset = s->memory_offset;
  489. int64_t size_in_block, start;
  490. /* When the memory is not stored into vmcore, offset will be -1 */
  491. *p_offset = -1;
  492. *p_filesz = 0;
  493. if (dump_has_filter(s)) {
  494. if (phys_addr < s->filter_area_begin ||
  495. phys_addr >= s->filter_area_begin + s->filter_area_length) {
  496. return;
  497. }
  498. }
  499. QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
  500. if (dump_has_filter(s)) {
  501. if (!ranges_overlap(block->target_start,
  502. block->target_end - block->target_start,
  503. s->filter_area_begin,
  504. s->filter_area_length)) {
  505. /* This block is out of the range */
  506. continue;
  507. }
  508. if (s->filter_area_begin <= block->target_start) {
  509. start = block->target_start;
  510. } else {
  511. start = s->filter_area_begin;
  512. }
  513. size_in_block = block->target_end - start;
  514. if (s->filter_area_begin + s->filter_area_length < block->target_end) {
  515. size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
  516. }
  517. } else {
  518. start = block->target_start;
  519. size_in_block = block->target_end - block->target_start;
  520. }
  521. if (phys_addr >= start && phys_addr < start + size_in_block) {
  522. *p_offset = phys_addr - start + offset;
  523. /* The offset range mapped from the vmcore file must not spill over
  524. * the GuestPhysBlock, clamp it. The rest of the mapping will be
  525. * zero-filled in memory at load time; see
  526. * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
  527. */
  528. *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
  529. mapping_length :
  530. size_in_block - (phys_addr - start);
  531. return;
  532. }
  533. offset += size_in_block;
  534. }
  535. }
  536. static void write_elf_phdr_loads(DumpState *s, Error **errp)
  537. {
  538. ERRP_GUARD();
  539. hwaddr offset, filesz;
  540. MemoryMapping *memory_mapping;
  541. uint32_t phdr_index = 1;
  542. QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
  543. get_offset_range(memory_mapping->phys_addr,
  544. memory_mapping->length,
  545. s, &offset, &filesz);
  546. if (dump_is_64bit(s)) {
  547. write_elf64_load(s, memory_mapping, phdr_index++, offset,
  548. filesz, errp);
  549. } else {
  550. write_elf32_load(s, memory_mapping, phdr_index++, offset,
  551. filesz, errp);
  552. }
  553. if (*errp) {
  554. return;
  555. }
  556. if (phdr_index >= s->phdr_num) {
  557. break;
  558. }
  559. }
  560. }
  561. static void write_elf_notes(DumpState *s, Error **errp)
  562. {
  563. if (dump_is_64bit(s)) {
  564. write_elf64_notes(fd_write_vmcore, s, errp);
  565. } else {
  566. write_elf32_notes(fd_write_vmcore, s, errp);
  567. }
  568. }
  569. /* write elf header, PT_NOTE and elf note to vmcore. */
  570. static void dump_begin(DumpState *s, Error **errp)
  571. {
  572. ERRP_GUARD();
  573. /*
  574. * the vmcore's format is:
  575. * --------------
  576. * | elf header |
  577. * --------------
  578. * | sctn_hdr |
  579. * --------------
  580. * | PT_NOTE |
  581. * --------------
  582. * | PT_LOAD |
  583. * --------------
  584. * | ...... |
  585. * --------------
  586. * | PT_LOAD |
  587. * --------------
  588. * | elf note |
  589. * --------------
  590. * | memory |
  591. * --------------
  592. *
  593. * we only know where the memory is saved after we write elf note into
  594. * vmcore.
  595. */
  596. /* write elf header to vmcore */
  597. write_elf_header(s, errp);
  598. if (*errp) {
  599. return;
  600. }
  601. /* write section headers to vmcore */
  602. write_elf_section_headers(s, errp);
  603. if (*errp) {
  604. return;
  605. }
  606. /* write PT_NOTE to vmcore */
  607. write_elf_phdr_note(s, errp);
  608. if (*errp) {
  609. return;
  610. }
  611. /* write all PT_LOADs to vmcore */
  612. write_elf_phdr_loads(s, errp);
  613. if (*errp) {
  614. return;
  615. }
  616. /* write notes to vmcore */
  617. write_elf_notes(s, errp);
  618. }
  619. int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
  620. int64_t filter_area_start,
  621. int64_t filter_area_length)
  622. {
  623. int64_t size, left, right;
  624. /* No filter, return full size */
  625. if (!filter_area_length) {
  626. return block->target_end - block->target_start;
  627. }
  628. /* calculate the overlapped region. */
  629. left = MAX(filter_area_start, block->target_start);
  630. right = MIN(filter_area_start + filter_area_length, block->target_end);
  631. size = right - left;
  632. size = size > 0 ? size : 0;
  633. return size;
  634. }
  635. int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
  636. int64_t filter_area_start,
  637. int64_t filter_area_length)
  638. {
  639. if (filter_area_length) {
  640. /* return -1 if the block is not within filter area */
  641. if (!ranges_overlap(block->target_start,
  642. block->target_end - block->target_start,
  643. filter_area_start, filter_area_length)) {
  644. return -1;
  645. }
  646. if (filter_area_start > block->target_start) {
  647. return filter_area_start - block->target_start;
  648. }
  649. }
  650. return 0;
  651. }
  652. /* write all memory to vmcore */
  653. static void dump_iterate(DumpState *s, Error **errp)
  654. {
  655. ERRP_GUARD();
  656. GuestPhysBlock *block;
  657. int64_t memblock_size, memblock_start;
  658. QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
  659. memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
  660. if (memblock_start == -1) {
  661. continue;
  662. }
  663. memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
  664. /* Write the memory to file */
  665. write_memory(s, block, memblock_start, memblock_size, errp);
  666. if (*errp) {
  667. return;
  668. }
  669. }
  670. }
  671. static void dump_end(DumpState *s, Error **errp)
  672. {
  673. int rc;
  674. if (s->elf_section_data_size) {
  675. s->elf_section_data = g_malloc0(s->elf_section_data_size);
  676. }
  677. /* Adds the architecture defined section data to s->elf_section_data */
  678. if (s->dump_info.arch_sections_write_fn &&
  679. s->elf_section_data_size) {
  680. rc = s->dump_info.arch_sections_write_fn(s, s->elf_section_data);
  681. if (rc) {
  682. error_setg_errno(errp, rc,
  683. "dump: failed to get arch section data");
  684. g_free(s->elf_section_data);
  685. return;
  686. }
  687. }
  688. /* write sections to vmcore */
  689. write_elf_sections(s, errp);
  690. }
  691. static void create_vmcore(DumpState *s, Error **errp)
  692. {
  693. ERRP_GUARD();
  694. dump_begin(s, errp);
  695. if (*errp) {
  696. return;
  697. }
  698. /* Iterate over memory and dump it to file */
  699. dump_iterate(s, errp);
  700. if (*errp) {
  701. return;
  702. }
  703. /* Write the section data */
  704. dump_end(s, errp);
  705. }
  706. static int write_start_flat_header(DumpState *s)
  707. {
  708. MakedumpfileHeader *mh;
  709. int ret = 0;
  710. if (s->kdump_raw) {
  711. return 0;
  712. }
  713. QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
  714. mh = g_malloc0(MAX_SIZE_MDF_HEADER);
  715. memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
  716. MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
  717. mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
  718. mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
  719. size_t written_size;
  720. written_size = qemu_write_full(s->fd, mh, MAX_SIZE_MDF_HEADER);
  721. if (written_size != MAX_SIZE_MDF_HEADER) {
  722. ret = -1;
  723. }
  724. g_free(mh);
  725. return ret;
  726. }
  727. static int write_end_flat_header(DumpState *s)
  728. {
  729. MakedumpfileDataHeader mdh;
  730. if (s->kdump_raw) {
  731. return 0;
  732. }
  733. mdh.offset = END_FLAG_FLAT_HEADER;
  734. mdh.buf_size = END_FLAG_FLAT_HEADER;
  735. size_t written_size;
  736. written_size = qemu_write_full(s->fd, &mdh, sizeof(mdh));
  737. if (written_size != sizeof(mdh)) {
  738. return -1;
  739. }
  740. return 0;
  741. }
  742. static int write_buffer(DumpState *s, off_t offset, const void *buf, size_t size)
  743. {
  744. size_t written_size;
  745. MakedumpfileDataHeader mdh;
  746. off_t seek_loc;
  747. if (s->kdump_raw) {
  748. seek_loc = lseek(s->fd, offset, SEEK_SET);
  749. if (seek_loc == (off_t) -1) {
  750. return -1;
  751. }
  752. } else {
  753. mdh.offset = cpu_to_be64(offset);
  754. mdh.buf_size = cpu_to_be64(size);
  755. written_size = qemu_write_full(s->fd, &mdh, sizeof(mdh));
  756. if (written_size != sizeof(mdh)) {
  757. return -1;
  758. }
  759. }
  760. written_size = qemu_write_full(s->fd, buf, size);
  761. if (written_size != size) {
  762. return -1;
  763. }
  764. return 0;
  765. }
  766. static int buf_write_note(const void *buf, size_t size, void *opaque)
  767. {
  768. DumpState *s = opaque;
  769. /* note_buf is not enough */
  770. if (s->note_buf_offset + size > s->note_size) {
  771. return -1;
  772. }
  773. memcpy(s->note_buf + s->note_buf_offset, buf, size);
  774. s->note_buf_offset += size;
  775. return 0;
  776. }
  777. /*
  778. * This function retrieves various sizes from an elf header.
  779. *
  780. * @note has to be a valid ELF note. The return sizes are unmodified
  781. * (not padded or rounded up to be multiple of 4).
  782. */
  783. static void get_note_sizes(DumpState *s, const void *note,
  784. uint64_t *note_head_size,
  785. uint64_t *name_size,
  786. uint64_t *desc_size)
  787. {
  788. uint64_t note_head_sz;
  789. uint64_t name_sz;
  790. uint64_t desc_sz;
  791. if (dump_is_64bit(s)) {
  792. const Elf64_Nhdr *hdr = note;
  793. note_head_sz = sizeof(Elf64_Nhdr);
  794. name_sz = cpu_to_dump64(s, hdr->n_namesz);
  795. desc_sz = cpu_to_dump64(s, hdr->n_descsz);
  796. } else {
  797. const Elf32_Nhdr *hdr = note;
  798. note_head_sz = sizeof(Elf32_Nhdr);
  799. name_sz = cpu_to_dump32(s, hdr->n_namesz);
  800. desc_sz = cpu_to_dump32(s, hdr->n_descsz);
  801. }
  802. if (note_head_size) {
  803. *note_head_size = note_head_sz;
  804. }
  805. if (name_size) {
  806. *name_size = name_sz;
  807. }
  808. if (desc_size) {
  809. *desc_size = desc_sz;
  810. }
  811. }
  812. static bool note_name_equal(DumpState *s,
  813. const uint8_t *note, const char *name)
  814. {
  815. int len = strlen(name) + 1;
  816. uint64_t head_size, name_size;
  817. get_note_sizes(s, note, &head_size, &name_size, NULL);
  818. head_size = ROUND_UP(head_size, 4);
  819. return name_size == len && memcmp(note + head_size, name, len) == 0;
  820. }
  821. /* write common header, sub header and elf note to vmcore */
  822. static void create_header32(DumpState *s, Error **errp)
  823. {
  824. ERRP_GUARD();
  825. DiskDumpHeader32 *dh = NULL;
  826. KdumpSubHeader32 *kh = NULL;
  827. size_t size;
  828. uint32_t block_size;
  829. uint32_t sub_hdr_size;
  830. uint32_t bitmap_blocks;
  831. uint32_t status = 0;
  832. uint64_t offset_note;
  833. /* write common header, the version of kdump-compressed format is 6th */
  834. size = sizeof(DiskDumpHeader32);
  835. dh = g_malloc0(size);
  836. memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
  837. dh->header_version = cpu_to_dump32(s, 6);
  838. block_size = s->dump_info.page_size;
  839. dh->block_size = cpu_to_dump32(s, block_size);
  840. sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
  841. sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
  842. dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
  843. /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
  844. dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
  845. dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
  846. bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
  847. dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
  848. strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
  849. if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
  850. status |= DUMP_DH_COMPRESSED_ZLIB;
  851. }
  852. #ifdef CONFIG_LZO
  853. if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
  854. status |= DUMP_DH_COMPRESSED_LZO;
  855. }
  856. #endif
  857. #ifdef CONFIG_SNAPPY
  858. if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
  859. status |= DUMP_DH_COMPRESSED_SNAPPY;
  860. }
  861. #endif
  862. dh->status = cpu_to_dump32(s, status);
  863. if (write_buffer(s, 0, dh, size) < 0) {
  864. error_setg(errp, "dump: failed to write disk dump header");
  865. goto out;
  866. }
  867. /* write sub header */
  868. size = sizeof(KdumpSubHeader32);
  869. kh = g_malloc0(size);
  870. /* 64bit max_mapnr_64 */
  871. kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
  872. kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
  873. kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
  874. offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
  875. if (s->guest_note &&
  876. note_name_equal(s, s->guest_note, "VMCOREINFO")) {
  877. uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
  878. get_note_sizes(s, s->guest_note,
  879. &hsize, &name_size, &size_vmcoreinfo_desc);
  880. offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
  881. (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
  882. kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
  883. kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
  884. }
  885. kh->offset_note = cpu_to_dump64(s, offset_note);
  886. kh->note_size = cpu_to_dump32(s, s->note_size);
  887. if (write_buffer(s, DISKDUMP_HEADER_BLOCKS *
  888. block_size, kh, size) < 0) {
  889. error_setg(errp, "dump: failed to write kdump sub header");
  890. goto out;
  891. }
  892. /* write note */
  893. s->note_buf = g_malloc0(s->note_size);
  894. s->note_buf_offset = 0;
  895. /* use s->note_buf to store notes temporarily */
  896. write_elf32_notes(buf_write_note, s, errp);
  897. if (*errp) {
  898. goto out;
  899. }
  900. if (write_buffer(s, offset_note, s->note_buf,
  901. s->note_size) < 0) {
  902. error_setg(errp, "dump: failed to write notes");
  903. goto out;
  904. }
  905. /* get offset of dump_bitmap */
  906. s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
  907. block_size;
  908. /* get offset of page */
  909. s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
  910. block_size;
  911. out:
  912. g_free(dh);
  913. g_free(kh);
  914. g_free(s->note_buf);
  915. }
  916. /* write common header, sub header and elf note to vmcore */
  917. static void create_header64(DumpState *s, Error **errp)
  918. {
  919. ERRP_GUARD();
  920. DiskDumpHeader64 *dh = NULL;
  921. KdumpSubHeader64 *kh = NULL;
  922. size_t size;
  923. uint32_t block_size;
  924. uint32_t sub_hdr_size;
  925. uint32_t bitmap_blocks;
  926. uint32_t status = 0;
  927. uint64_t offset_note;
  928. /* write common header, the version of kdump-compressed format is 6th */
  929. size = sizeof(DiskDumpHeader64);
  930. dh = g_malloc0(size);
  931. memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
  932. dh->header_version = cpu_to_dump32(s, 6);
  933. block_size = s->dump_info.page_size;
  934. dh->block_size = cpu_to_dump32(s, block_size);
  935. sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
  936. sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
  937. dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
  938. /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
  939. dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
  940. dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
  941. bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
  942. dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
  943. strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
  944. if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
  945. status |= DUMP_DH_COMPRESSED_ZLIB;
  946. }
  947. #ifdef CONFIG_LZO
  948. if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
  949. status |= DUMP_DH_COMPRESSED_LZO;
  950. }
  951. #endif
  952. #ifdef CONFIG_SNAPPY
  953. if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
  954. status |= DUMP_DH_COMPRESSED_SNAPPY;
  955. }
  956. #endif
  957. dh->status = cpu_to_dump32(s, status);
  958. if (write_buffer(s, 0, dh, size) < 0) {
  959. error_setg(errp, "dump: failed to write disk dump header");
  960. goto out;
  961. }
  962. /* write sub header */
  963. size = sizeof(KdumpSubHeader64);
  964. kh = g_malloc0(size);
  965. /* 64bit max_mapnr_64 */
  966. kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
  967. kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
  968. kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
  969. offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
  970. if (s->guest_note &&
  971. note_name_equal(s, s->guest_note, "VMCOREINFO")) {
  972. uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
  973. get_note_sizes(s, s->guest_note,
  974. &hsize, &name_size, &size_vmcoreinfo_desc);
  975. offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
  976. (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
  977. kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
  978. kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
  979. }
  980. kh->offset_note = cpu_to_dump64(s, offset_note);
  981. kh->note_size = cpu_to_dump64(s, s->note_size);
  982. if (write_buffer(s, DISKDUMP_HEADER_BLOCKS *
  983. block_size, kh, size) < 0) {
  984. error_setg(errp, "dump: failed to write kdump sub header");
  985. goto out;
  986. }
  987. /* write note */
  988. s->note_buf = g_malloc0(s->note_size);
  989. s->note_buf_offset = 0;
  990. /* use s->note_buf to store notes temporarily */
  991. write_elf64_notes(buf_write_note, s, errp);
  992. if (*errp) {
  993. goto out;
  994. }
  995. if (write_buffer(s, offset_note, s->note_buf,
  996. s->note_size) < 0) {
  997. error_setg(errp, "dump: failed to write notes");
  998. goto out;
  999. }
  1000. /* get offset of dump_bitmap */
  1001. s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
  1002. block_size;
  1003. /* get offset of page */
  1004. s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
  1005. block_size;
  1006. out:
  1007. g_free(dh);
  1008. g_free(kh);
  1009. g_free(s->note_buf);
  1010. }
  1011. static void write_dump_header(DumpState *s, Error **errp)
  1012. {
  1013. if (dump_is_64bit(s)) {
  1014. create_header64(s, errp);
  1015. } else {
  1016. create_header32(s, errp);
  1017. }
  1018. }
  1019. static size_t dump_bitmap_get_bufsize(DumpState *s)
  1020. {
  1021. return s->dump_info.page_size;
  1022. }
  1023. /*
  1024. * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
  1025. * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
  1026. * set_dump_bitmap will always leave the recently set bit un-sync. And setting
  1027. * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
  1028. * vmcore, ie. synchronizing un-sync bit into vmcore.
  1029. */
  1030. static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
  1031. uint8_t *buf, DumpState *s)
  1032. {
  1033. off_t old_offset, new_offset;
  1034. off_t offset_bitmap1, offset_bitmap2;
  1035. uint32_t byte, bit;
  1036. size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
  1037. size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
  1038. /* should not set the previous place */
  1039. assert(last_pfn <= pfn);
  1040. /*
  1041. * if the bit needed to be set is not cached in buf, flush the data in buf
  1042. * to vmcore firstly.
  1043. * making new_offset be bigger than old_offset can also sync remained data
  1044. * into vmcore.
  1045. */
  1046. old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
  1047. new_offset = bitmap_bufsize * (pfn / bits_per_buf);
  1048. while (old_offset < new_offset) {
  1049. /* calculate the offset and write dump_bitmap */
  1050. offset_bitmap1 = s->offset_dump_bitmap + old_offset;
  1051. if (write_buffer(s, offset_bitmap1, buf,
  1052. bitmap_bufsize) < 0) {
  1053. return -1;
  1054. }
  1055. /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
  1056. offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
  1057. old_offset;
  1058. if (write_buffer(s, offset_bitmap2, buf,
  1059. bitmap_bufsize) < 0) {
  1060. return -1;
  1061. }
  1062. memset(buf, 0, bitmap_bufsize);
  1063. old_offset += bitmap_bufsize;
  1064. }
  1065. /* get the exact place of the bit in the buf, and set it */
  1066. byte = (pfn % bits_per_buf) / CHAR_BIT;
  1067. bit = (pfn % bits_per_buf) % CHAR_BIT;
  1068. if (value) {
  1069. buf[byte] |= 1u << bit;
  1070. } else {
  1071. buf[byte] &= ~(1u << bit);
  1072. }
  1073. return 0;
  1074. }
  1075. static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
  1076. {
  1077. int target_page_shift = ctz32(s->dump_info.page_size);
  1078. return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
  1079. }
  1080. static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
  1081. {
  1082. int target_page_shift = ctz32(s->dump_info.page_size);
  1083. return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
  1084. }
  1085. /*
  1086. * Return the page frame number and the page content in *bufptr. bufptr can be
  1087. * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
  1088. * memory. This is not necessarily the memory returned.
  1089. */
  1090. static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
  1091. uint8_t **bufptr, DumpState *s)
  1092. {
  1093. GuestPhysBlock *block = *blockptr;
  1094. uint32_t page_size = s->dump_info.page_size;
  1095. uint8_t *buf = NULL, *hbuf;
  1096. hwaddr addr;
  1097. /* block == NULL means the start of the iteration */
  1098. if (!block) {
  1099. block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
  1100. *blockptr = block;
  1101. addr = block->target_start;
  1102. *pfnptr = dump_paddr_to_pfn(s, addr);
  1103. } else {
  1104. *pfnptr += 1;
  1105. addr = dump_pfn_to_paddr(s, *pfnptr);
  1106. }
  1107. assert(block != NULL);
  1108. while (1) {
  1109. if (addr >= block->target_start && addr < block->target_end) {
  1110. size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
  1111. hbuf = block->host_addr + (addr - block->target_start);
  1112. if (!buf) {
  1113. if (n == page_size) {
  1114. /* this is a whole target page, go for it */
  1115. assert(addr % page_size == 0);
  1116. buf = hbuf;
  1117. break;
  1118. } else if (bufptr) {
  1119. assert(*bufptr);
  1120. buf = *bufptr;
  1121. memset(buf, 0, page_size);
  1122. } else {
  1123. return true;
  1124. }
  1125. }
  1126. memcpy(buf + addr % page_size, hbuf, n);
  1127. addr += n;
  1128. if (addr % page_size == 0 || addr >= block->target_end) {
  1129. /* we filled up the page or the current block is finished */
  1130. break;
  1131. }
  1132. } else {
  1133. /* the next page is in the next block */
  1134. *blockptr = block = QTAILQ_NEXT(block, next);
  1135. if (!block) {
  1136. break;
  1137. }
  1138. addr = block->target_start;
  1139. /* are we still in the same page? */
  1140. if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
  1141. if (buf) {
  1142. /* no, but we already filled something earlier, return it */
  1143. break;
  1144. } else {
  1145. /* else continue from there */
  1146. *pfnptr = dump_paddr_to_pfn(s, addr);
  1147. }
  1148. }
  1149. }
  1150. }
  1151. if (bufptr) {
  1152. *bufptr = buf;
  1153. }
  1154. return buf != NULL;
  1155. }
  1156. static void write_dump_bitmap(DumpState *s, Error **errp)
  1157. {
  1158. int ret = 0;
  1159. uint64_t last_pfn, pfn;
  1160. void *dump_bitmap_buf;
  1161. size_t num_dumpable;
  1162. GuestPhysBlock *block_iter = NULL;
  1163. size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
  1164. size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
  1165. /* dump_bitmap_buf is used to store dump_bitmap temporarily */
  1166. dump_bitmap_buf = g_malloc0(bitmap_bufsize);
  1167. num_dumpable = 0;
  1168. last_pfn = 0;
  1169. /*
  1170. * exam memory page by page, and set the bit in dump_bitmap corresponded
  1171. * to the existing page.
  1172. */
  1173. while (get_next_page(&block_iter, &pfn, NULL, s)) {
  1174. ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
  1175. if (ret < 0) {
  1176. error_setg(errp, "dump: failed to set dump_bitmap");
  1177. goto out;
  1178. }
  1179. last_pfn = pfn;
  1180. num_dumpable++;
  1181. }
  1182. /*
  1183. * set_dump_bitmap will always leave the recently set bit un-sync. Here we
  1184. * set the remaining bits from last_pfn to the end of the bitmap buffer to
  1185. * 0. With those set, the un-sync bit will be synchronized into the vmcore.
  1186. */
  1187. if (num_dumpable > 0) {
  1188. ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
  1189. dump_bitmap_buf, s);
  1190. if (ret < 0) {
  1191. error_setg(errp, "dump: failed to sync dump_bitmap");
  1192. goto out;
  1193. }
  1194. }
  1195. /* number of dumpable pages that will be dumped later */
  1196. s->num_dumpable = num_dumpable;
  1197. out:
  1198. g_free(dump_bitmap_buf);
  1199. }
  1200. static void prepare_data_cache(DataCache *data_cache, DumpState *s,
  1201. off_t offset)
  1202. {
  1203. data_cache->state = s;
  1204. data_cache->data_size = 0;
  1205. data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
  1206. data_cache->buf = g_malloc0(data_cache->buf_size);
  1207. data_cache->offset = offset;
  1208. }
  1209. static int write_cache(DataCache *dc, const void *buf, size_t size,
  1210. bool flag_sync)
  1211. {
  1212. /*
  1213. * dc->buf_size should not be less than size, otherwise dc will never be
  1214. * enough
  1215. */
  1216. assert(size <= dc->buf_size);
  1217. /*
  1218. * if flag_sync is set, synchronize data in dc->buf into vmcore.
  1219. * otherwise check if the space is enough for caching data in buf, if not,
  1220. * write the data in dc->buf to dc->state->fd and reset dc->buf
  1221. */
  1222. if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
  1223. (flag_sync && dc->data_size > 0)) {
  1224. if (write_buffer(dc->state, dc->offset, dc->buf, dc->data_size) < 0) {
  1225. return -1;
  1226. }
  1227. dc->offset += dc->data_size;
  1228. dc->data_size = 0;
  1229. }
  1230. if (!flag_sync) {
  1231. memcpy(dc->buf + dc->data_size, buf, size);
  1232. dc->data_size += size;
  1233. }
  1234. return 0;
  1235. }
  1236. static void free_data_cache(DataCache *data_cache)
  1237. {
  1238. g_free(data_cache->buf);
  1239. }
  1240. static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
  1241. {
  1242. switch (flag_compress) {
  1243. case DUMP_DH_COMPRESSED_ZLIB:
  1244. return compressBound(page_size);
  1245. case DUMP_DH_COMPRESSED_LZO:
  1246. /*
  1247. * LZO will expand incompressible data by a little amount. Please check
  1248. * the following URL to see the expansion calculation:
  1249. * http://www.oberhumer.com/opensource/lzo/lzofaq.php
  1250. */
  1251. return page_size + page_size / 16 + 64 + 3;
  1252. #ifdef CONFIG_SNAPPY
  1253. case DUMP_DH_COMPRESSED_SNAPPY:
  1254. return snappy_max_compressed_length(page_size);
  1255. #endif
  1256. }
  1257. return 0;
  1258. }
  1259. static void write_dump_pages(DumpState *s, Error **errp)
  1260. {
  1261. int ret = 0;
  1262. DataCache page_desc, page_data;
  1263. size_t len_buf_out, size_out;
  1264. #ifdef CONFIG_LZO
  1265. lzo_bytep wrkmem = NULL;
  1266. #endif
  1267. uint8_t *buf_out = NULL;
  1268. off_t offset_desc, offset_data;
  1269. PageDescriptor pd, pd_zero;
  1270. uint8_t *buf;
  1271. GuestPhysBlock *block_iter = NULL;
  1272. uint64_t pfn_iter;
  1273. g_autofree uint8_t *page = NULL;
  1274. /* get offset of page_desc and page_data in dump file */
  1275. offset_desc = s->offset_page;
  1276. offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
  1277. prepare_data_cache(&page_desc, s, offset_desc);
  1278. prepare_data_cache(&page_data, s, offset_data);
  1279. /* prepare buffer to store compressed data */
  1280. len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
  1281. assert(len_buf_out != 0);
  1282. #ifdef CONFIG_LZO
  1283. wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
  1284. #endif
  1285. buf_out = g_malloc(len_buf_out);
  1286. /*
  1287. * init zero page's page_desc and page_data, because every zero page
  1288. * uses the same page_data
  1289. */
  1290. pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
  1291. pd_zero.flags = cpu_to_dump32(s, 0);
  1292. pd_zero.offset = cpu_to_dump64(s, offset_data);
  1293. pd_zero.page_flags = cpu_to_dump64(s, 0);
  1294. buf = g_malloc0(s->dump_info.page_size);
  1295. ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
  1296. g_free(buf);
  1297. if (ret < 0) {
  1298. error_setg(errp, "dump: failed to write page data (zero page)");
  1299. goto out;
  1300. }
  1301. offset_data += s->dump_info.page_size;
  1302. page = g_malloc(s->dump_info.page_size);
  1303. /*
  1304. * dump memory to vmcore page by page. zero page will all be resided in the
  1305. * first page of page section
  1306. */
  1307. for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
  1308. /* check zero page */
  1309. if (buffer_is_zero(buf, s->dump_info.page_size)) {
  1310. ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
  1311. false);
  1312. if (ret < 0) {
  1313. error_setg(errp, "dump: failed to write page desc");
  1314. goto out;
  1315. }
  1316. } else {
  1317. /*
  1318. * not zero page, then:
  1319. * 1. compress the page
  1320. * 2. write the compressed page into the cache of page_data
  1321. * 3. get page desc of the compressed page and write it into the
  1322. * cache of page_desc
  1323. *
  1324. * only one compression format will be used here, for
  1325. * s->flag_compress is set. But when compression fails to work,
  1326. * we fall back to save in plaintext.
  1327. */
  1328. size_out = len_buf_out;
  1329. if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
  1330. (compress2(buf_out, (uLongf *)&size_out, buf,
  1331. s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
  1332. (size_out < s->dump_info.page_size)) {
  1333. pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
  1334. pd.size = cpu_to_dump32(s, size_out);
  1335. ret = write_cache(&page_data, buf_out, size_out, false);
  1336. if (ret < 0) {
  1337. error_setg(errp, "dump: failed to write page data");
  1338. goto out;
  1339. }
  1340. #ifdef CONFIG_LZO
  1341. } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
  1342. (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
  1343. (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
  1344. (size_out < s->dump_info.page_size)) {
  1345. pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
  1346. pd.size = cpu_to_dump32(s, size_out);
  1347. ret = write_cache(&page_data, buf_out, size_out, false);
  1348. if (ret < 0) {
  1349. error_setg(errp, "dump: failed to write page data");
  1350. goto out;
  1351. }
  1352. #endif
  1353. #ifdef CONFIG_SNAPPY
  1354. } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
  1355. (snappy_compress((char *)buf, s->dump_info.page_size,
  1356. (char *)buf_out, &size_out) == SNAPPY_OK) &&
  1357. (size_out < s->dump_info.page_size)) {
  1358. pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
  1359. pd.size = cpu_to_dump32(s, size_out);
  1360. ret = write_cache(&page_data, buf_out, size_out, false);
  1361. if (ret < 0) {
  1362. error_setg(errp, "dump: failed to write page data");
  1363. goto out;
  1364. }
  1365. #endif
  1366. } else {
  1367. /*
  1368. * fall back to save in plaintext, size_out should be
  1369. * assigned the target's page size
  1370. */
  1371. pd.flags = cpu_to_dump32(s, 0);
  1372. size_out = s->dump_info.page_size;
  1373. pd.size = cpu_to_dump32(s, size_out);
  1374. ret = write_cache(&page_data, buf,
  1375. s->dump_info.page_size, false);
  1376. if (ret < 0) {
  1377. error_setg(errp, "dump: failed to write page data");
  1378. goto out;
  1379. }
  1380. }
  1381. /* get and write page desc here */
  1382. pd.page_flags = cpu_to_dump64(s, 0);
  1383. pd.offset = cpu_to_dump64(s, offset_data);
  1384. offset_data += size_out;
  1385. ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
  1386. if (ret < 0) {
  1387. error_setg(errp, "dump: failed to write page desc");
  1388. goto out;
  1389. }
  1390. }
  1391. s->written_size += s->dump_info.page_size;
  1392. }
  1393. ret = write_cache(&page_desc, NULL, 0, true);
  1394. if (ret < 0) {
  1395. error_setg(errp, "dump: failed to sync cache for page_desc");
  1396. goto out;
  1397. }
  1398. ret = write_cache(&page_data, NULL, 0, true);
  1399. if (ret < 0) {
  1400. error_setg(errp, "dump: failed to sync cache for page_data");
  1401. goto out;
  1402. }
  1403. out:
  1404. free_data_cache(&page_desc);
  1405. free_data_cache(&page_data);
  1406. #ifdef CONFIG_LZO
  1407. g_free(wrkmem);
  1408. #endif
  1409. g_free(buf_out);
  1410. }
  1411. static void create_kdump_vmcore(DumpState *s, Error **errp)
  1412. {
  1413. ERRP_GUARD();
  1414. int ret;
  1415. /*
  1416. * the kdump-compressed format is:
  1417. * File offset
  1418. * +------------------------------------------+ 0x0
  1419. * | main header (struct disk_dump_header) |
  1420. * |------------------------------------------+ block 1
  1421. * | sub header (struct kdump_sub_header) |
  1422. * |------------------------------------------+ block 2
  1423. * | 1st-dump_bitmap |
  1424. * |------------------------------------------+ block 2 + X blocks
  1425. * | 2nd-dump_bitmap | (aligned by block)
  1426. * |------------------------------------------+ block 2 + 2 * X blocks
  1427. * | page desc for pfn 0 (struct page_desc) | (aligned by block)
  1428. * | page desc for pfn 1 (struct page_desc) |
  1429. * | : |
  1430. * |------------------------------------------| (not aligned by block)
  1431. * | page data (pfn 0) |
  1432. * | page data (pfn 1) |
  1433. * | : |
  1434. * +------------------------------------------+
  1435. */
  1436. ret = write_start_flat_header(s);
  1437. if (ret < 0) {
  1438. error_setg(errp, "dump: failed to write start flat header");
  1439. return;
  1440. }
  1441. write_dump_header(s, errp);
  1442. if (*errp) {
  1443. return;
  1444. }
  1445. write_dump_bitmap(s, errp);
  1446. if (*errp) {
  1447. return;
  1448. }
  1449. write_dump_pages(s, errp);
  1450. if (*errp) {
  1451. return;
  1452. }
  1453. ret = write_end_flat_header(s);
  1454. if (ret < 0) {
  1455. error_setg(errp, "dump: failed to write end flat header");
  1456. return;
  1457. }
  1458. }
  1459. static void get_max_mapnr(DumpState *s)
  1460. {
  1461. GuestPhysBlock *last_block;
  1462. last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
  1463. s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
  1464. }
  1465. static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
  1466. static void dump_state_prepare(DumpState *s)
  1467. {
  1468. /* zero the struct, setting status to active */
  1469. *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
  1470. }
  1471. bool qemu_system_dump_in_progress(void)
  1472. {
  1473. DumpState *state = &dump_state_global;
  1474. return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
  1475. }
  1476. /*
  1477. * calculate total size of memory to be dumped (taking filter into
  1478. * account.)
  1479. */
  1480. static int64_t dump_calculate_size(DumpState *s)
  1481. {
  1482. GuestPhysBlock *block;
  1483. int64_t total = 0;
  1484. QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
  1485. total += dump_filtered_memblock_size(block,
  1486. s->filter_area_begin,
  1487. s->filter_area_length);
  1488. }
  1489. return total;
  1490. }
  1491. static void vmcoreinfo_update_phys_base(DumpState *s)
  1492. {
  1493. uint64_t size, note_head_size, name_size, phys_base;
  1494. char **lines;
  1495. uint8_t *vmci;
  1496. size_t i;
  1497. if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
  1498. return;
  1499. }
  1500. get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
  1501. note_head_size = ROUND_UP(note_head_size, 4);
  1502. vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
  1503. *(vmci + size) = '\0';
  1504. lines = g_strsplit((char *)vmci, "\n", -1);
  1505. for (i = 0; lines[i]; i++) {
  1506. const char *prefix = NULL;
  1507. if (s->dump_info.d_machine == EM_X86_64) {
  1508. prefix = "NUMBER(phys_base)=";
  1509. } else if (s->dump_info.d_machine == EM_AARCH64) {
  1510. prefix = "NUMBER(PHYS_OFFSET)=";
  1511. }
  1512. if (prefix && g_str_has_prefix(lines[i], prefix)) {
  1513. if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
  1514. &phys_base) < 0) {
  1515. warn_report("Failed to read %s", prefix);
  1516. } else {
  1517. s->dump_info.phys_base = phys_base;
  1518. }
  1519. break;
  1520. }
  1521. }
  1522. g_strfreev(lines);
  1523. }
  1524. static void dump_init(DumpState *s, int fd, bool has_format,
  1525. DumpGuestMemoryFormat format, bool paging, bool has_filter,
  1526. int64_t begin, int64_t length, bool kdump_raw,
  1527. Error **errp)
  1528. {
  1529. ERRP_GUARD();
  1530. VMCoreInfoState *vmci = vmcoreinfo_find();
  1531. CPUState *cpu;
  1532. int nr_cpus;
  1533. int ret;
  1534. s->has_format = has_format;
  1535. s->format = format;
  1536. s->written_size = 0;
  1537. s->kdump_raw = kdump_raw;
  1538. /* kdump-compressed is conflict with paging and filter */
  1539. if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
  1540. assert(!paging && !has_filter);
  1541. }
  1542. if (runstate_is_running()) {
  1543. vm_stop(RUN_STATE_SAVE_VM);
  1544. s->resume = true;
  1545. } else {
  1546. s->resume = false;
  1547. }
  1548. /* If we use KVM, we should synchronize the registers before we get dump
  1549. * info or physmap info.
  1550. */
  1551. cpu_synchronize_all_states();
  1552. nr_cpus = 0;
  1553. CPU_FOREACH(cpu) {
  1554. nr_cpus++;
  1555. }
  1556. s->fd = fd;
  1557. if (has_filter && !length) {
  1558. error_setg(errp, "parameter 'length' expects a non-zero size");
  1559. goto cleanup;
  1560. }
  1561. s->filter_area_begin = begin;
  1562. s->filter_area_length = length;
  1563. /* First index is 0, it's the special null name */
  1564. s->string_table_buf = g_array_new(FALSE, TRUE, 1);
  1565. /*
  1566. * Allocate the null name, due to the clearing option set to true
  1567. * it will be 0.
  1568. */
  1569. g_array_set_size(s->string_table_buf, 1);
  1570. memory_mapping_list_init(&s->list);
  1571. guest_phys_blocks_init(&s->guest_phys_blocks);
  1572. guest_phys_blocks_append(&s->guest_phys_blocks);
  1573. s->total_size = dump_calculate_size(s);
  1574. #ifdef DEBUG_DUMP_GUEST_MEMORY
  1575. fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
  1576. #endif
  1577. /* it does not make sense to dump non-existent memory */
  1578. if (!s->total_size) {
  1579. error_setg(errp, "dump: no guest memory to dump");
  1580. goto cleanup;
  1581. }
  1582. /* get dump info: endian, class and architecture.
  1583. * If the target architecture is not supported, cpu_get_dump_info() will
  1584. * return -1.
  1585. */
  1586. ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
  1587. if (ret < 0) {
  1588. error_setg(errp,
  1589. "dumping guest memory is not supported on this target");
  1590. goto cleanup;
  1591. }
  1592. if (!s->dump_info.page_size) {
  1593. s->dump_info.page_size = qemu_target_page_size();
  1594. }
  1595. s->note_size = cpu_get_note_size(s->dump_info.d_class,
  1596. s->dump_info.d_machine, nr_cpus);
  1597. assert(s->note_size >= 0);
  1598. /*
  1599. * The goal of this block is to (a) update the previously guessed
  1600. * phys_base, (b) copy the guest note out of the guest.
  1601. * Failure to do so is not fatal for dumping.
  1602. */
  1603. if (vmci) {
  1604. uint64_t addr, note_head_size, name_size, desc_size;
  1605. uint32_t size;
  1606. uint16_t guest_format;
  1607. note_head_size = dump_is_64bit(s) ?
  1608. sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
  1609. guest_format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
  1610. size = le32_to_cpu(vmci->vmcoreinfo.size);
  1611. addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
  1612. if (!vmci->has_vmcoreinfo) {
  1613. warn_report("guest note is not present");
  1614. } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
  1615. warn_report("guest note size is invalid: %" PRIu32, size);
  1616. } else if (guest_format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
  1617. warn_report("guest note format is unsupported: %" PRIu16, guest_format);
  1618. } else {
  1619. s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
  1620. cpu_physical_memory_read(addr, s->guest_note, size);
  1621. get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
  1622. s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
  1623. desc_size);
  1624. if (name_size > MAX_GUEST_NOTE_SIZE ||
  1625. desc_size > MAX_GUEST_NOTE_SIZE ||
  1626. s->guest_note_size > size) {
  1627. warn_report("Invalid guest note header");
  1628. g_free(s->guest_note);
  1629. s->guest_note = NULL;
  1630. } else {
  1631. vmcoreinfo_update_phys_base(s);
  1632. s->note_size += s->guest_note_size;
  1633. }
  1634. }
  1635. }
  1636. /* get memory mapping */
  1637. if (paging) {
  1638. qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
  1639. if (*errp) {
  1640. goto cleanup;
  1641. }
  1642. } else {
  1643. qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
  1644. }
  1645. s->nr_cpus = nr_cpus;
  1646. get_max_mapnr(s);
  1647. uint64_t tmp;
  1648. tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
  1649. s->dump_info.page_size);
  1650. s->len_dump_bitmap = tmp * s->dump_info.page_size;
  1651. /* init for kdump-compressed format */
  1652. if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
  1653. switch (format) {
  1654. case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
  1655. s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
  1656. break;
  1657. case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
  1658. #ifdef CONFIG_LZO
  1659. if (lzo_init() != LZO_E_OK) {
  1660. error_setg(errp, "failed to initialize the LZO library");
  1661. goto cleanup;
  1662. }
  1663. #endif
  1664. s->flag_compress = DUMP_DH_COMPRESSED_LZO;
  1665. break;
  1666. case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
  1667. s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
  1668. break;
  1669. default:
  1670. s->flag_compress = 0;
  1671. }
  1672. return;
  1673. }
  1674. if (dump_has_filter(s)) {
  1675. memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
  1676. }
  1677. /*
  1678. * The first section header is always a special one in which most
  1679. * fields are 0. The section header string table is also always
  1680. * set.
  1681. */
  1682. s->shdr_num = 2;
  1683. /*
  1684. * Adds the number of architecture sections to shdr_num and sets
  1685. * elf_section_data_size so we know the offsets and sizes of all
  1686. * parts.
  1687. */
  1688. if (s->dump_info.arch_sections_add_fn) {
  1689. s->dump_info.arch_sections_add_fn(s);
  1690. }
  1691. /*
  1692. * calculate shdr_num so we know the offsets and sizes of all
  1693. * parts.
  1694. * Calculate phdr_num
  1695. *
  1696. * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
  1697. * sh_info is 32 bit. There's special handling once we go over
  1698. * UINT16_MAX - 1 but that is handled in the ehdr and section
  1699. * code.
  1700. */
  1701. s->phdr_num = 1; /* Reserve PT_NOTE */
  1702. if (s->list.num <= UINT32_MAX - 1) {
  1703. s->phdr_num += s->list.num;
  1704. } else {
  1705. s->phdr_num = UINT32_MAX;
  1706. }
  1707. /*
  1708. * Now that the number of section and program headers is known we
  1709. * can calculate the offsets of the headers and data.
  1710. */
  1711. if (dump_is_64bit(s)) {
  1712. s->shdr_offset = sizeof(Elf64_Ehdr);
  1713. s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
  1714. s->note_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
  1715. } else {
  1716. s->shdr_offset = sizeof(Elf32_Ehdr);
  1717. s->phdr_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
  1718. s->note_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
  1719. }
  1720. s->memory_offset = s->note_offset + s->note_size;
  1721. s->section_offset = s->memory_offset + s->total_size;
  1722. return;
  1723. cleanup:
  1724. dump_cleanup(s);
  1725. }
  1726. /* this operation might be time consuming. */
  1727. static void dump_process(DumpState *s, Error **errp)
  1728. {
  1729. ERRP_GUARD();
  1730. DumpQueryResult *result = NULL;
  1731. if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
  1732. create_win_dump(s, errp);
  1733. } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
  1734. create_kdump_vmcore(s, errp);
  1735. } else {
  1736. create_vmcore(s, errp);
  1737. }
  1738. /* make sure status is written after written_size updates */
  1739. smp_wmb();
  1740. qatomic_set(&s->status,
  1741. (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
  1742. /* send DUMP_COMPLETED message (unconditionally) */
  1743. result = qmp_query_dump(NULL);
  1744. /* should never fail */
  1745. assert(result);
  1746. qapi_event_send_dump_completed(result,
  1747. *errp ? error_get_pretty(*errp) : NULL);
  1748. qapi_free_DumpQueryResult(result);
  1749. dump_cleanup(s);
  1750. }
  1751. static void *dump_thread(void *data)
  1752. {
  1753. DumpState *s = (DumpState *)data;
  1754. dump_process(s, NULL);
  1755. return NULL;
  1756. }
  1757. DumpQueryResult *qmp_query_dump(Error **errp)
  1758. {
  1759. DumpQueryResult *result = g_new(DumpQueryResult, 1);
  1760. DumpState *state = &dump_state_global;
  1761. result->status = qatomic_read(&state->status);
  1762. /* make sure we are reading status and written_size in order */
  1763. smp_rmb();
  1764. result->completed = state->written_size;
  1765. result->total = state->total_size;
  1766. return result;
  1767. }
  1768. void qmp_dump_guest_memory(bool paging, const char *protocol,
  1769. bool has_detach, bool detach,
  1770. bool has_begin, int64_t begin,
  1771. bool has_length, int64_t length,
  1772. bool has_format, DumpGuestMemoryFormat format,
  1773. Error **errp)
  1774. {
  1775. ERRP_GUARD();
  1776. const char *p;
  1777. int fd;
  1778. DumpState *s;
  1779. bool detach_p = false;
  1780. bool kdump_raw = false;
  1781. if (runstate_check(RUN_STATE_INMIGRATE)) {
  1782. error_setg(errp, "Dump not allowed during incoming migration.");
  1783. return;
  1784. }
  1785. /* if there is a dump in background, we should wait until the dump
  1786. * finished */
  1787. if (qemu_system_dump_in_progress()) {
  1788. error_setg(errp, "There is a dump in process, please wait.");
  1789. return;
  1790. }
  1791. /*
  1792. * externally, we represent kdump-raw-* as separate formats, but internally
  1793. * they are handled the same, except for the "raw" flag
  1794. */
  1795. if (has_format) {
  1796. switch (format) {
  1797. case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB:
  1798. format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
  1799. kdump_raw = true;
  1800. break;
  1801. case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO:
  1802. format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
  1803. kdump_raw = true;
  1804. break;
  1805. case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY:
  1806. format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
  1807. kdump_raw = true;
  1808. break;
  1809. default:
  1810. break;
  1811. }
  1812. }
  1813. /*
  1814. * kdump-compressed format need the whole memory dumped, so paging or
  1815. * filter is not supported here.
  1816. */
  1817. if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
  1818. (paging || has_begin || has_length)) {
  1819. error_setg(errp, "kdump-compressed format doesn't support paging or "
  1820. "filter");
  1821. return;
  1822. }
  1823. if (has_begin && !has_length) {
  1824. error_setg(errp, QERR_MISSING_PARAMETER, "length");
  1825. return;
  1826. }
  1827. if (!has_begin && has_length) {
  1828. error_setg(errp, QERR_MISSING_PARAMETER, "begin");
  1829. return;
  1830. }
  1831. if (has_detach) {
  1832. detach_p = detach;
  1833. }
  1834. /* check whether lzo/snappy is supported */
  1835. #ifndef CONFIG_LZO
  1836. if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
  1837. error_setg(errp, "kdump-lzo is not available now");
  1838. return;
  1839. }
  1840. #endif
  1841. #ifndef CONFIG_SNAPPY
  1842. if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
  1843. error_setg(errp, "kdump-snappy is not available now");
  1844. return;
  1845. }
  1846. #endif
  1847. if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
  1848. && !win_dump_available(errp)) {
  1849. return;
  1850. }
  1851. if (strstart(protocol, "fd:", &p)) {
  1852. fd = monitor_get_fd(monitor_cur(), p, errp);
  1853. if (fd == -1) {
  1854. return;
  1855. }
  1856. } else if (strstart(protocol, "file:", &p)) {
  1857. fd = qemu_create(p, O_WRONLY | O_TRUNC | O_BINARY, S_IRUSR, errp);
  1858. if (fd < 0) {
  1859. return;
  1860. }
  1861. } else {
  1862. error_setg(errp,
  1863. "parameter 'protocol' must start with 'file:' or 'fd:'");
  1864. return;
  1865. }
  1866. if (kdump_raw && lseek(fd, 0, SEEK_CUR) == (off_t) -1) {
  1867. close(fd);
  1868. error_setg(errp, "kdump-raw formats require a seekable file");
  1869. return;
  1870. }
  1871. if (!dump_migration_blocker) {
  1872. error_setg(&dump_migration_blocker,
  1873. "Live migration disabled: dump-guest-memory in progress");
  1874. }
  1875. /*
  1876. * Allows even for -only-migratable, but forbid migration during the
  1877. * process of dump guest memory.
  1878. */
  1879. if (migrate_add_blocker_internal(&dump_migration_blocker, errp)) {
  1880. /* Remember to release the fd before passing it over to dump state */
  1881. close(fd);
  1882. return;
  1883. }
  1884. s = &dump_state_global;
  1885. dump_state_prepare(s);
  1886. dump_init(s, fd, has_format, format, paging, has_begin,
  1887. begin, length, kdump_raw, errp);
  1888. if (*errp) {
  1889. qatomic_set(&s->status, DUMP_STATUS_FAILED);
  1890. return;
  1891. }
  1892. if (detach_p) {
  1893. /* detached dump */
  1894. s->detached = true;
  1895. qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
  1896. s, QEMU_THREAD_DETACHED);
  1897. } else {
  1898. /* sync dump */
  1899. dump_process(s, errp);
  1900. }
  1901. }
  1902. DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
  1903. {
  1904. DumpGuestMemoryCapability *cap =
  1905. g_new0(DumpGuestMemoryCapability, 1);
  1906. DumpGuestMemoryFormatList **tail = &cap->formats;
  1907. /* elf is always available */
  1908. QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
  1909. /* kdump-zlib is always available */
  1910. QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
  1911. QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB);
  1912. /* add new item if kdump-lzo is available */
  1913. #ifdef CONFIG_LZO
  1914. QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
  1915. QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO);
  1916. #endif
  1917. /* add new item if kdump-snappy is available */
  1918. #ifdef CONFIG_SNAPPY
  1919. QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
  1920. QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY);
  1921. #endif
  1922. if (win_dump_available(NULL)) {
  1923. QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
  1924. }
  1925. return cap;
  1926. }