1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2006 Peter Wemm 5 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * from: FreeBSD: src/sys/i386/i386/minidump_machdep.c,v 1.6 2008/08/17 23:27:27 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_watchdog.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/conf.h> 40 #include <sys/cons.h> 41 #include <sys/kernel.h> 42 #include <sys/kerneldump.h> 43 #include <sys/msgbuf.h> 44 #include <sys/watchdog.h> 45 #include <vm/vm.h> 46 #include <vm/vm_param.h> 47 #include <vm/vm_page.h> 48 #include <vm/vm_phys.h> 49 #include <vm/vm_dumpset.h> 50 #include <vm/pmap.h> 51 #include <machine/atomic.h> 52 #include <machine/cpu.h> 53 #include <machine/elf.h> 54 #include <machine/md_var.h> 55 #include <machine/minidump.h> 56 57 CTASSERT(sizeof(struct kerneldumpheader) == 512); 58 59 static struct kerneldumpheader kdh; 60 61 /* Handle chunked writes. */ 62 static size_t fragsz; 63 static void *dump_va; 64 65 static int 66 blk_flush(struct dumperinfo *di) 67 { 68 int error; 69 70 if (fragsz == 0) 71 return (0); 72 73 error = dump_append(di, dump_va, fragsz); 74 fragsz = 0; 75 return (error); 76 } 77 78 static int 79 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz) 80 { 81 size_t len; 82 int error, i, c; 83 u_int maxdumpsz; 84 85 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE); 86 if (maxdumpsz == 0) /* seatbelt */ 87 maxdumpsz = PAGE_SIZE; 88 error = 0; 89 if (ptr != NULL && pa != 0) { 90 printf("cant have both va and pa!\n"); 91 return (EINVAL); 92 } 93 if (pa != 0) { 94 if ((sz % PAGE_SIZE) != 0) { 95 printf("size not page aligned\n"); 96 return (EINVAL); 97 } 98 if ((pa & PAGE_MASK) != 0) { 99 printf("address not page aligned\n"); 100 return (EINVAL); 101 } 102 } 103 if (ptr != NULL) { 104 /* Flush any pre-existing pa pages before a virtual dump. */ 105 error = blk_flush(di); 106 if (error) 107 return (error); 108 } 109 while (sz) { 110 len = maxdumpsz - fragsz; 111 if (len > sz) 112 len = sz; 113 114 dumpsys_pb_progress(len); 115 wdog_kern_pat(WD_LASTVAL); 116 117 if (ptr) { 118 error = dump_append(di, ptr, len); 119 if (error) 120 return (error); 121 ptr += len; 122 sz -= len; 123 } else { 124 for (i = 0; i < len; i += PAGE_SIZE) 125 dump_va = pmap_kenter_temporary(pa + i, 126 (i + fragsz) >> PAGE_SHIFT); 127 fragsz += len; 128 pa += len; 129 sz -= len; 130 if (fragsz == maxdumpsz) { 131 error = blk_flush(di); 132 if (error) 133 return (error); 134 } 135 } 136 137 /* Check for user abort. */ 138 c = cncheckc(); 139 if (c == 0x03) 140 return (ECANCELED); 141 if (c != -1) 142 printf(" (CTRL-C to abort) "); 143 } 144 145 return (0); 146 } 147 148 /* A buffer for general use. Its size must be one page at least. */ 149 static char dumpbuf[PAGE_SIZE] __aligned(sizeof(uint64_t)); 150 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0); 151 152 int 153 cpu_minidumpsys(struct dumperinfo *di, const struct minidumpstate *state) 154 { 155 struct minidumphdr mdhdr; 156 struct msgbuf *mbp; 157 uint64_t dumpsize, *dump_avail_buf; 158 uint32_t ptesize; 159 uint32_t pa, prev_pa = 0, count = 0; 160 vm_offset_t va, kva_end; 161 int error, i; 162 char *addr; 163 164 /* 165 * Flush caches. Note that in the SMP case this operates only on the 166 * current CPU's L1 cache. Before we reach this point, code in either 167 * the system shutdown or kernel debugger has called stop_cpus() to stop 168 * all cores other than this one. Part of the ARM handling of 169 * stop_cpus() is to call wbinv_all() on that core's local L1 cache. So 170 * by time we get to here, all that remains is to flush the L1 for the 171 * current CPU, then the L2. 172 */ 173 dcache_wbinv_poc_all(); 174 175 /* Snapshot the KVA upper bound in case it grows. */ 176 kva_end = kernel_vm_end; 177 178 /* 179 * Walk the kernel page table pages, setting the active entries in the 180 * dump bitmap. 181 */ 182 ptesize = 0; 183 for (va = KERNBASE; va < kva_end; va += PAGE_SIZE) { 184 pa = pmap_dump_kextract(va, NULL); 185 if (pa != 0 && vm_phys_is_dumpable(pa)) 186 vm_page_dump_add(state->dump_bitset, pa); 187 ptesize += sizeof(pt2_entry_t); 188 } 189 190 /* Calculate dump size. */ 191 mbp = state->msgbufp; 192 dumpsize = ptesize; 193 dumpsize += round_page(mbp->msg_size); 194 dumpsize += round_page(nitems(dump_avail) * sizeof(uint64_t)); 195 dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages)); 196 VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) { 197 /* Clear out undumpable pages now if needed */ 198 if (vm_phys_is_dumpable(pa)) 199 dumpsize += PAGE_SIZE; 200 else 201 vm_page_dump_drop(state->dump_bitset, pa); 202 } 203 dumpsize += PAGE_SIZE; 204 205 dumpsys_pb_init(dumpsize); 206 207 /* Initialize mdhdr */ 208 bzero(&mdhdr, sizeof(mdhdr)); 209 strcpy(mdhdr.magic, MINIDUMP_MAGIC); 210 mdhdr.version = MINIDUMP_VERSION; 211 mdhdr.msgbufsize = mbp->msg_size; 212 mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages)); 213 mdhdr.ptesize = ptesize; 214 mdhdr.kernbase = KERNBASE; 215 mdhdr.arch = __ARM_ARCH; 216 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6; 217 mdhdr.dumpavailsize = round_page(nitems(dump_avail) * sizeof(uint64_t)); 218 219 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION, 220 dumpsize); 221 222 error = dump_start(di, &kdh); 223 if (error != 0) 224 goto fail; 225 226 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576); 227 printf("Dumping %llu MB:", (long long)dumpsize >> 20); 228 229 /* Dump my header */ 230 bzero(dumpbuf, sizeof(dumpbuf)); 231 bcopy(&mdhdr, dumpbuf, sizeof(mdhdr)); 232 error = blk_write(di, dumpbuf, 0, PAGE_SIZE); 233 if (error) 234 goto fail; 235 236 /* Dump msgbuf up front */ 237 error = blk_write(di, mbp->msg_ptr, 0, round_page(mbp->msg_size)); 238 if (error) 239 goto fail; 240 241 /* Dump dump_avail. Make a copy using 64-bit physical addresses. */ 242 _Static_assert(nitems(dump_avail) * sizeof(uint64_t) <= sizeof(dumpbuf), 243 "Large dump_avail not handled"); 244 bzero(dumpbuf, sizeof(dumpbuf)); 245 dump_avail_buf = (uint64_t *)dumpbuf; 246 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) { 247 dump_avail_buf[i] = dump_avail[i]; 248 dump_avail_buf[i + 1] = dump_avail[i + 1]; 249 } 250 error = blk_write(di, dumpbuf, 0, PAGE_SIZE); 251 if (error) 252 goto fail; 253 254 /* Dump bitmap */ 255 error = blk_write(di, (char *)state->dump_bitset, 0, 256 round_page(BITSET_SIZE(vm_page_dump_pages))); 257 if (error) 258 goto fail; 259 260 /* Dump kernel page table pages */ 261 addr = dumpbuf; 262 for (va = KERNBASE; va < kva_end; va += PAGE_SIZE) { 263 pmap_dump_kextract(va, (pt2_entry_t *)addr); 264 addr += sizeof(pt2_entry_t); 265 if (addr == dumpbuf + sizeof(dumpbuf)) { 266 error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf)); 267 if (error != 0) 268 goto fail; 269 addr = dumpbuf; 270 } 271 } 272 if (addr != dumpbuf) { 273 error = blk_write(di, dumpbuf, 0, addr - dumpbuf); 274 if (error != 0) 275 goto fail; 276 } 277 278 /* Dump memory chunks */ 279 VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) { 280 if (!count) { 281 prev_pa = pa; 282 count++; 283 } else { 284 if (pa == (prev_pa + count * PAGE_SIZE)) 285 count++; 286 else { 287 error = blk_write(di, NULL, prev_pa, 288 count * PAGE_SIZE); 289 if (error) 290 goto fail; 291 count = 1; 292 prev_pa = pa; 293 } 294 } 295 } 296 if (count) { 297 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE); 298 if (error) 299 goto fail; 300 count = 0; 301 prev_pa = 0; 302 } 303 304 error = blk_flush(di); 305 if (error) 306 goto fail; 307 308 error = dump_finish(di, &kdh); 309 if (error != 0) 310 goto fail; 311 312 printf("\nDump complete\n"); 313 return (0); 314 315 fail: 316 if (error < 0) 317 error = -error; 318 319 if (error == ECANCELED) 320 printf("\nDump aborted\n"); 321 else if (error == E2BIG || error == ENOSPC) { 322 printf("\nDump failed. Partition too small (about %lluMB were " 323 "needed this time).\n", (long long)dumpsize >> 20); 324 } else 325 printf("\n** DUMP FAILED (ERROR %d) **\n", error); 326 return (error); 327 } 328