]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_dump.c
MFV r323678: file 5.32
[FreeBSD/FreeBSD.git] / sys / kern / kern_dump.c
1 /*-
2  * Copyright (c) 2002 Marcel Moolenaar
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  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_watchdog.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/cons.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/kerneldump.h>
39 #ifdef SW_WATCHDOG
40 #include <sys/watchdog.h>
41 #endif
42 #include <vm/vm.h>
43 #include <vm/vm_param.h>
44 #include <vm/pmap.h>
45 #include <machine/dump.h>
46 #include <machine/elf.h>
47 #include <machine/md_var.h>
48 #include <machine/pcb.h>
49
50 CTASSERT(sizeof(struct kerneldumpheader) == 512);
51
52 #define MD_ALIGN(x)     roundup2((off_t)(x), PAGE_SIZE)
53
54 off_t dumplo;
55
56 /* Handle buffered writes. */
57 static size_t fragsz;
58
59 struct dump_pa dump_map[DUMPSYS_MD_PA_NPAIRS];
60
61 #if !defined(__powerpc__) && !defined(__sparc__)
62 void
63 dumpsys_gen_pa_init(void)
64 {
65         int n, idx;
66
67         bzero(dump_map, sizeof(dump_map));
68         for (n = 0; n < nitems(dump_map); n++) {
69                 idx = n * 2;
70                 if (dump_avail[idx] == 0 && dump_avail[idx + 1] == 0)
71                         break;
72                 dump_map[n].pa_start = dump_avail[idx];
73                 dump_map[n].pa_size = dump_avail[idx + 1] - dump_avail[idx];
74         }
75 }
76 #endif
77
78 struct dump_pa *
79 dumpsys_gen_pa_next(struct dump_pa *mdp)
80 {
81
82         if (mdp == NULL)
83                 return (&dump_map[0]);
84
85         mdp++;
86         if (mdp->pa_size == 0)
87                 mdp = NULL;
88         return (mdp);
89 }
90
91 void
92 dumpsys_gen_wbinv_all(void)
93 {
94
95 }
96
97 void
98 dumpsys_gen_unmap_chunk(vm_paddr_t pa __unused, size_t chunk __unused,
99     void *va __unused)
100 {
101
102 }
103
104 #if !defined(__sparc__)
105 int
106 dumpsys_gen_write_aux_headers(struct dumperinfo *di)
107 {
108
109         return (0);
110 }
111 #endif
112
113 int
114 dumpsys_buf_seek(struct dumperinfo *di, size_t sz)
115 {
116         static uint8_t buf[DEV_BSIZE];
117         size_t nbytes;
118         int error;
119
120         bzero(buf, sizeof(buf));
121
122         while (sz > 0) {
123                 nbytes = MIN(sz, sizeof(buf));
124
125                 error = dump_write(di, buf, 0, dumplo, nbytes);
126                 if (error)
127                         return (error);
128                 dumplo += nbytes;
129
130                 sz -= nbytes;
131         }
132
133         return (0);
134 }
135
136 int
137 dumpsys_buf_write(struct dumperinfo *di, char *ptr, size_t sz)
138 {
139         size_t len;
140         int error;
141
142         while (sz) {
143                 len = di->blocksize - fragsz;
144                 if (len > sz)
145                         len = sz;
146                 memcpy((char *)di->blockbuf + fragsz, ptr, len);
147                 fragsz += len;
148                 ptr += len;
149                 sz -= len;
150                 if (fragsz == di->blocksize) {
151                         error = dump_write(di, di->blockbuf, 0, dumplo,
152                             di->blocksize);
153                         if (error)
154                                 return (error);
155                         dumplo += di->blocksize;
156                         fragsz = 0;
157                 }
158         }
159         return (0);
160 }
161
162 int
163 dumpsys_buf_flush(struct dumperinfo *di)
164 {
165         int error;
166
167         if (fragsz == 0)
168                 return (0);
169
170         error = dump_write(di, di->blockbuf, 0, dumplo, di->blocksize);
171         dumplo += di->blocksize;
172         fragsz = 0;
173         return (error);
174 }
175
176 CTASSERT(PAGE_SHIFT < 20);
177 #define PG2MB(pgs) ((pgs + (1 << (20 - PAGE_SHIFT)) - 1) >> (20 - PAGE_SHIFT))
178
179 int
180 dumpsys_cb_dumpdata(struct dump_pa *mdp, int seqnr, void *arg)
181 {
182         struct dumperinfo *di = (struct dumperinfo*)arg;
183         vm_paddr_t pa;
184         void *va;
185         uint64_t pgs;
186         size_t counter, sz, chunk;
187         int c, error;
188         u_int maxdumppgs;
189
190         error = 0;      /* catch case in which chunk size is 0 */
191         counter = 0;    /* Update twiddle every 16MB */
192         va = NULL;
193         pgs = mdp->pa_size / PAGE_SIZE;
194         pa = mdp->pa_start;
195         maxdumppgs = min(di->maxiosize / PAGE_SIZE, MAXDUMPPGS);
196         if (maxdumppgs == 0)    /* seatbelt */
197                 maxdumppgs = 1;
198
199         printf("  chunk %d: %juMB (%ju pages)", seqnr, (uintmax_t)PG2MB(pgs),
200             (uintmax_t)pgs);
201
202         dumpsys_wbinv_all();
203         while (pgs) {
204                 chunk = pgs;
205                 if (chunk > maxdumppgs)
206                         chunk = maxdumppgs;
207                 sz = chunk << PAGE_SHIFT;
208                 counter += sz;
209                 if (counter >> 24) {
210                         printf(" %ju", (uintmax_t)PG2MB(pgs));
211                         counter &= (1 << 24) - 1;
212                 }
213
214                 dumpsys_map_chunk(pa, chunk, &va);
215 #ifdef SW_WATCHDOG
216                 wdog_kern_pat(WD_LASTVAL);
217 #endif
218
219                 error = dump_write(di, va, 0, dumplo, sz);
220                 dumpsys_unmap_chunk(pa, chunk, va);
221                 if (error)
222                         break;
223                 dumplo += sz;
224                 pgs -= chunk;
225                 pa += sz;
226
227                 /* Check for user abort. */
228                 c = cncheckc();
229                 if (c == 0x03)
230                         return (ECANCELED);
231                 if (c != -1)
232                         printf(" (CTRL-C to abort) ");
233         }
234         printf(" ... %s\n", (error) ? "fail" : "ok");
235         return (error);
236 }
237
238 int
239 dumpsys_foreach_chunk(dumpsys_callback_t cb, void *arg)
240 {
241         struct dump_pa *mdp;
242         int error, seqnr;
243
244         seqnr = 0;
245         mdp = dumpsys_pa_next(NULL);
246         while (mdp != NULL) {
247                 error = (*cb)(mdp, seqnr++, arg);
248                 if (error)
249                         return (-error);
250                 mdp = dumpsys_pa_next(mdp);
251         }
252         return (seqnr);
253 }
254
255 #if !defined(__sparc__)
256 static off_t fileofs;
257
258 static int
259 cb_dumphdr(struct dump_pa *mdp, int seqnr, void *arg)
260 {
261         struct dumperinfo *di = (struct dumperinfo*)arg;
262         Elf_Phdr phdr;
263         uint64_t size;
264         int error;
265
266         size = mdp->pa_size;
267         bzero(&phdr, sizeof(phdr));
268         phdr.p_type = PT_LOAD;
269         phdr.p_flags = PF_R;                    /* XXX */
270         phdr.p_offset = fileofs;
271 #ifdef __powerpc__
272         phdr.p_vaddr = (do_minidump? mdp->pa_start : ~0L);
273         phdr.p_paddr = (do_minidump? ~0L : mdp->pa_start);
274 #else
275         phdr.p_vaddr = mdp->pa_start;
276         phdr.p_paddr = mdp->pa_start;
277 #endif
278         phdr.p_filesz = size;
279         phdr.p_memsz = size;
280         phdr.p_align = PAGE_SIZE;
281
282         error = dumpsys_buf_write(di, (char*)&phdr, sizeof(phdr));
283         fileofs += phdr.p_filesz;
284         return (error);
285 }
286
287 static int
288 cb_size(struct dump_pa *mdp, int seqnr, void *arg)
289 {
290         uint64_t *sz;
291
292         sz = (uint64_t *)arg;
293         *sz += (uint64_t)mdp->pa_size;
294         return (0);
295 }
296
297 int
298 dumpsys_generic(struct dumperinfo *di)
299 {
300         static struct kerneldumpheader kdh;
301         Elf_Ehdr ehdr;
302         uint64_t dumpsize;
303         off_t hdrgap;
304         size_t hdrsz;
305         int error;
306
307 #ifndef __powerpc__
308         if (do_minidump)
309                 return (minidumpsys(di));
310 #endif
311
312         bzero(&ehdr, sizeof(ehdr));
313         ehdr.e_ident[EI_MAG0] = ELFMAG0;
314         ehdr.e_ident[EI_MAG1] = ELFMAG1;
315         ehdr.e_ident[EI_MAG2] = ELFMAG2;
316         ehdr.e_ident[EI_MAG3] = ELFMAG3;
317         ehdr.e_ident[EI_CLASS] = ELF_CLASS;
318 #if BYTE_ORDER == LITTLE_ENDIAN
319         ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
320 #else
321         ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
322 #endif
323         ehdr.e_ident[EI_VERSION] = EV_CURRENT;
324         ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE;   /* XXX big picture? */
325         ehdr.e_type = ET_CORE;
326         ehdr.e_machine = EM_VALUE;
327         ehdr.e_phoff = sizeof(ehdr);
328         ehdr.e_flags = 0;
329         ehdr.e_ehsize = sizeof(ehdr);
330         ehdr.e_phentsize = sizeof(Elf_Phdr);
331         ehdr.e_shentsize = sizeof(Elf_Shdr);
332
333         dumpsys_pa_init();
334
335         /* Calculate dump size. */
336         dumpsize = 0L;
337         ehdr.e_phnum = dumpsys_foreach_chunk(cb_size, &dumpsize) +
338             DUMPSYS_NUM_AUX_HDRS;
339         hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
340         fileofs = MD_ALIGN(hdrsz);
341         dumpsize += fileofs;
342         hdrgap = fileofs - roundup2((off_t)hdrsz, di->blocksize);
343
344         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARCH_VERSION,
345             dumpsize);
346
347         printf("Dumping %ju MB (%d chunks)\n", (uintmax_t)dumpsize >> 20,
348             ehdr.e_phnum - DUMPSYS_NUM_AUX_HDRS);
349
350         error = dump_start(di, &kdh, &dumplo);
351         if (error != 0)
352                 goto fail;
353
354         /* Dump ELF header */
355         error = dumpsys_buf_write(di, (char*)&ehdr, sizeof(ehdr));
356         if (error)
357                 goto fail;
358
359         /* Dump program headers */
360         error = dumpsys_foreach_chunk(cb_dumphdr, di);
361         if (error < 0)
362                 goto fail;
363         error = dumpsys_write_aux_headers(di);
364         if (error < 0)
365                 goto fail;
366         dumpsys_buf_flush(di);
367
368         /*
369          * All headers are written using blocked I/O, so we know the
370          * current offset is (still) block aligned. Skip the alignement
371          * in the file to have the segment contents aligned at page
372          * boundary. We cannot use MD_ALIGN on dumplo, because we don't
373          * care and may very well be unaligned within the dump device.
374          */
375         error = dumpsys_buf_seek(di, (size_t)hdrgap);
376         if (error)
377                 goto fail;
378
379         /* Dump memory chunks (updates dumplo) */
380         error = dumpsys_foreach_chunk(dumpsys_cb_dumpdata, di);
381         if (error < 0)
382                 goto fail;
383
384         error = dump_finish(di, &kdh, dumplo);
385         if (error != 0)
386                 goto fail;
387
388         printf("\nDump complete\n");
389         return (0);
390
391  fail:
392         if (error < 0)
393                 error = -error;
394
395         if (error == ECANCELED)
396                 printf("\nDump aborted\n");
397         else if (error == E2BIG || error == ENOSPC)
398                 printf("\nDump failed. Partition too small.\n");
399         else
400                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
401         return (error);
402 }
403 #endif