minidump_machdep.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*-
  2. * Copyright (c) 2019 Leandro Lupori
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. * $FreeBSD$
  26. */
  27. #include <sys/types.h>
  28. #include <sys/param.h>
  29. #include <sys/cons.h>
  30. #include <sys/kerneldump.h>
  31. #include <sys/msgbuf.h>
  32. #include <sys/proc.h>
  33. #include <sys/sysctl.h>
  34. #include <vm/vm.h>
  35. #include <vm/vm_param.h>
  36. #include <vm/vm_page.h>
  37. #include <vm/vm_phys.h>
  38. #include <vm/vm_dumpset.h>
  39. #include <vm/pmap.h>
  40. #include <machine/atomic.h>
  41. #include <machine/dump.h>
  42. #include <machine/md_var.h>
  43. #include <machine/minidump.h>
  44. /* Debugging stuff */
  45. #define MINIDUMP_DEBUG 0
  46. #if MINIDUMP_DEBUG
  47. #define dprintf(fmt, ...) printf(fmt, ## __VA_ARGS__)
  48. #define DBG(...) __VA_ARGS__
  49. static size_t total, dumptotal;
  50. static void dump_total(const char *id, size_t sz);
  51. #else
  52. #define dprintf(fmt, ...)
  53. #define DBG(...)
  54. #define dump_total(...)
  55. #endif
  56. extern vm_offset_t __startkernel, __endkernel;
  57. static int dump_retry_count = 5;
  58. SYSCTL_INT(_machdep, OID_AUTO, dump_retry_count, CTLFLAG_RWTUN,
  59. &dump_retry_count, 0,
  60. "Number of times dump has to retry before bailing out");
  61. static struct kerneldumpheader kdh;
  62. static char pgbuf[PAGE_SIZE];
  63. static struct {
  64. int min_per;
  65. int max_per;
  66. int visited;
  67. } progress_track[10] = {
  68. { 0, 10, 0},
  69. { 10, 20, 0},
  70. { 20, 30, 0},
  71. { 30, 40, 0},
  72. { 40, 50, 0},
  73. { 50, 60, 0},
  74. { 60, 70, 0},
  75. { 70, 80, 0},
  76. { 80, 90, 0},
  77. { 90, 100, 0}
  78. };
  79. static size_t counter, dumpsize, progress;
  80. /* Handle chunked writes. */
  81. static size_t fragsz;
  82. int
  83. is_dumpable(vm_paddr_t pa)
  84. {
  85. vm_page_t m;
  86. int i;
  87. if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
  88. return ((m->flags & PG_NODUMP) == 0);
  89. for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
  90. if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
  91. return (1);
  92. }
  93. return (0);
  94. }
  95. static void
  96. pmap_kenter_temporary(vm_offset_t va, vm_paddr_t pa)
  97. {
  98. pmap_kremove(va);
  99. pmap_kenter(va, pa);
  100. }
  101. static void
  102. report_progress(void)
  103. {
  104. int sofar, i;
  105. sofar = 100 - ((progress * 100) / dumpsize);
  106. for (i = 0; i < nitems(progress_track); i++) {
  107. if (sofar < progress_track[i].min_per ||
  108. sofar > progress_track[i].max_per)
  109. continue;
  110. if (progress_track[i].visited)
  111. return;
  112. progress_track[i].visited = 1;
  113. printf("..%d%%", sofar);
  114. return;
  115. }
  116. }
  117. static int
  118. blk_flush(struct dumperinfo *di)
  119. {
  120. int error;
  121. if (fragsz == 0)
  122. return (0);
  123. error = dump_append(di, crashdumpmap, 0, fragsz);
  124. DBG(dumptotal += fragsz;)
  125. fragsz = 0;
  126. return (error);
  127. }
  128. static int
  129. blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
  130. {
  131. size_t len, maxdumpsz;
  132. int error, i, c;
  133. maxdumpsz = MIN(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
  134. if (maxdumpsz == 0) /* seatbelt */
  135. maxdumpsz = PAGE_SIZE;
  136. error = 0;
  137. if ((sz % PAGE_SIZE) != 0) {
  138. printf("Size not page aligned\n");
  139. return (EINVAL);
  140. }
  141. if (ptr != NULL && pa != 0) {
  142. printf("Can't have both va and pa!\n");
  143. return (EINVAL);
  144. }
  145. if ((pa % PAGE_SIZE) != 0) {
  146. printf("Address not page aligned 0x%lx\n", pa);
  147. return (EINVAL);
  148. }
  149. if (ptr != NULL) {
  150. /*
  151. * If we're doing a virtual dump, flush any pre-existing
  152. * pa pages
  153. */
  154. error = blk_flush(di);
  155. if (error)
  156. return (error);
  157. }
  158. while (sz) {
  159. len = maxdumpsz - fragsz;
  160. if (len > sz)
  161. len = sz;
  162. counter += len;
  163. progress -= len;
  164. if (counter >> 20) {
  165. report_progress();
  166. counter &= (1<<20) - 1;
  167. }
  168. if (ptr) {
  169. error = dump_append(di, ptr, 0, len);
  170. if (error)
  171. return (error);
  172. DBG(dumptotal += len;)
  173. ptr += len;
  174. } else {
  175. for (i = 0; i < len; i += PAGE_SIZE)
  176. pmap_kenter_temporary(
  177. (vm_offset_t)crashdumpmap + fragsz + i,
  178. pa + i);
  179. fragsz += len;
  180. pa += len;
  181. if (fragsz == maxdumpsz) {
  182. error = blk_flush(di);
  183. if (error)
  184. return (error);
  185. }
  186. }
  187. sz -= len;
  188. /* Check for user abort. */
  189. c = cncheckc();
  190. if (c == 0x03)
  191. return (ECANCELED);
  192. if (c != -1)
  193. printf(" (CTRL-C to abort) ");
  194. }
  195. return (0);
  196. }
  197. static int
  198. dump_pmap(struct dumperinfo *di)
  199. {
  200. void *ctx;
  201. char *buf;
  202. u_long nbytes;
  203. int error;
  204. ctx = dumpsys_dump_pmap_init(sizeof(pgbuf) / PAGE_SIZE);
  205. for (;;) {
  206. buf = dumpsys_dump_pmap(ctx, pgbuf, &nbytes);
  207. if (buf == NULL)
  208. break;
  209. error = blk_write(di, buf, 0, nbytes);
  210. if (error)
  211. return (error);
  212. }
  213. return (0);
  214. }
  215. int
  216. minidumpsys(struct dumperinfo *di)
  217. {
  218. vm_paddr_t pa;
  219. int error, i, retry_count;
  220. uint32_t pmapsize;
  221. struct minidumphdr mdhdr;
  222. retry_count = 0;
  223. retry:
  224. retry_count++;
  225. fragsz = 0;
  226. DBG(total = dumptotal = 0;)
  227. /* Reset progress */
  228. counter = 0;
  229. for (i = 0; i < nitems(progress_track); i++)
  230. progress_track[i].visited = 0;
  231. /* Build set of dumpable pages from kernel pmap */
  232. pmapsize = dumpsys_scan_pmap();
  233. if (pmapsize % PAGE_SIZE != 0) {
  234. printf("pmapsize not page aligned: 0x%x\n", pmapsize);
  235. return (EINVAL);
  236. }
  237. /* Calculate dump size */
  238. dumpsize = PAGE_SIZE; /* header */
  239. dumpsize += round_page(msgbufp->msg_size);
  240. dumpsize += round_page(sizeof(dump_avail));
  241. dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
  242. dumpsize += pmapsize;
  243. VM_PAGE_DUMP_FOREACH(pa) {
  244. /* Clear out undumpable pages now if needed */
  245. if (is_dumpable(pa))
  246. dumpsize += PAGE_SIZE;
  247. else
  248. dump_drop_page(pa);
  249. }
  250. progress = dumpsize;
  251. /* Initialize mdhdr */
  252. bzero(&mdhdr, sizeof(mdhdr));
  253. strcpy(mdhdr.magic, MINIDUMP_MAGIC);
  254. strncpy(mdhdr.mmu_name, pmap_mmu_name(), sizeof(mdhdr.mmu_name) - 1);
  255. mdhdr.version = MINIDUMP_VERSION;
  256. mdhdr.msgbufsize = msgbufp->msg_size;
  257. mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
  258. mdhdr.pmapsize = pmapsize;
  259. mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
  260. mdhdr.kernend = VM_MAX_SAFE_KERNEL_ADDRESS;
  261. mdhdr.dmapbase = DMAP_BASE_ADDRESS;
  262. mdhdr.dmapend = DMAP_MAX_ADDRESS;
  263. mdhdr.hw_direct_map = hw_direct_map;
  264. mdhdr.startkernel = __startkernel;
  265. mdhdr.endkernel = __endkernel;
  266. mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
  267. dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_POWERPC_VERSION,
  268. dumpsize);
  269. error = dump_start(di, &kdh);
  270. if (error)
  271. goto fail;
  272. printf("Dumping %lu out of %ju MB:", dumpsize >> 20,
  273. ptoa((uintmax_t)physmem) / 1048576);
  274. /* Dump minidump header */
  275. bzero(pgbuf, sizeof(pgbuf));
  276. memcpy(pgbuf, &mdhdr, sizeof(mdhdr));
  277. error = blk_write(di, pgbuf, 0, PAGE_SIZE);
  278. if (error)
  279. goto fail;
  280. dump_total("header", PAGE_SIZE);
  281. /* Dump msgbuf up front */
  282. error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
  283. round_page(msgbufp->msg_size));
  284. dump_total("msgbuf", round_page(msgbufp->msg_size));
  285. /* Dump dump_avail */
  286. _Static_assert(sizeof(dump_avail) <= sizeof(pgbuf),
  287. "Large dump_avail not handled");
  288. bzero(pgbuf, sizeof(mdhdr));
  289. memcpy(pgbuf, dump_avail, sizeof(dump_avail));
  290. error = blk_write(di, pgbuf, 0, PAGE_SIZE);
  291. if (error)
  292. goto fail;
  293. dump_total("dump_avail", round_page(sizeof(dump_avail)));
  294. /* Dump bitmap */
  295. error = blk_write(di, (char *)vm_page_dump, 0,
  296. round_page(BITSET_SIZE(vm_page_dump_pages)));
  297. if (error)
  298. goto fail;
  299. dump_total("bitmap", round_page(BITSET_SIZE(vm_page_dump_pages)));
  300. /* Dump kernel page directory pages */
  301. error = dump_pmap(di);
  302. if (error)
  303. goto fail;
  304. dump_total("pmap", pmapsize);
  305. /* Dump memory chunks */
  306. VM_PAGE_DUMP_FOREACH(pa) {
  307. error = blk_write(di, 0, pa, PAGE_SIZE);
  308. if (error)
  309. goto fail;
  310. }
  311. error = blk_flush(di);
  312. if (error)
  313. goto fail;
  314. dump_total("mem_chunks", dumpsize - total);
  315. error = dump_finish(di, &kdh);
  316. if (error)
  317. goto fail;
  318. printf("\nDump complete\n");
  319. return (0);
  320. fail:
  321. if (error < 0)
  322. error = -error;
  323. printf("\n");
  324. if (error == ENOSPC) {
  325. printf("Dump map grown while dumping. ");
  326. if (retry_count < dump_retry_count) {
  327. printf("Retrying...\n");
  328. goto retry;
  329. }
  330. printf("Dump failed.\n");
  331. } else if (error == ECANCELED)
  332. printf("Dump aborted\n");
  333. else if (error == E2BIG)
  334. printf("Dump failed. Partition too small.\n");
  335. else
  336. printf("** DUMP FAILED (ERROR %d) **\n", error);
  337. return (error);
  338. }
  339. #if MINIDUMP_DEBUG
  340. static void
  341. dump_total(const char *id, size_t sz)
  342. {
  343. total += sz;
  344. dprintf("\n%s=%08lx/%08lx/%08lx\n",
  345. id, sz, total, dumptotal);
  346. }
  347. #endif