2
0

dump.c 65 KB

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