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: releng/6.0/sys/amd64/amd64/dump_machdep.c 147740 2005-07-02 19:57:31Z marcel $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/conf.h>
33 #include <sys/cons.h>
34 #include <sys/kernel.h>
35 #include <sys/kerneldump.h>
36 #include <vm/vm.h>
37 #include <vm/pmap.h>
38 #include <machine/elf.h>
39 #include <machine/md_var.h>
40
41 CTASSERT(sizeof(struct kerneldumpheader) == 512);
42
43 /*
44 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
45 * is to protect us from metadata and to protect metadata from us.
46 */
47 #define SIZEOF_METADATA (64*1024)
48
49 #define MD_ALIGN(x) (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
50 #define DEV_ALIGN(x) (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
51
52 struct md_pa {
53 vm_paddr_t md_start;
54 vm_paddr_t md_size;
55 };
56
57 typedef int callback_t(struct md_pa *, int, void *);
58
59 static struct kerneldumpheader kdh;
60 static off_t dumplo, fileofs;
61
62 /* Handle buffered writes. */
63 static char buffer[DEV_BSIZE];
64 static size_t fragsz;
65
66 /* 20 phys_avail entry pairs correspond to 10 md_pa's */
67 static struct md_pa dump_map[10];
68
69 static void
70 md_pa_init(void)
71 {
72 int n, idx;
73
74 bzero(dump_map, sizeof(dump_map));
75 for (n = 0; n < sizeof(dump_map) / sizeof(dump_map[0]); n++) {
76 idx = n * 2;
77 if (dump_avail[idx] == 0 && dump_avail[idx + 1] == 0)
78 break;
79 dump_map[n].md_start = dump_avail[idx];
80 dump_map[n].md_size = dump_avail[idx + 1] - dump_avail[idx];
81 }
82 }
83
84 static struct md_pa *
85 md_pa_first(void)
86 {
87
88 return (&dump_map[0]);
89 }
90
91 static struct md_pa *
92 md_pa_next(struct md_pa *mdp)
93 {
94
95 mdp++;
96 if (mdp->md_size == 0)
97 mdp = NULL;
98 return (mdp);
99 }
100
101 /* XXX should be MI */
102 static void
103 mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen,
104 uint32_t blksz)
105 {
106
107 bzero(kdh, sizeof(*kdh));
108 strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic));
109 strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
110 kdh->version = htod32(KERNELDUMPVERSION);
111 kdh->architectureversion = htod32(archver);
112 kdh->dumplength = htod64(dumplen);
113 kdh->dumptime = htod64(time_second);
114 kdh->blocksize = htod32(blksz);
115 strncpy(kdh->hostname, hostname, sizeof(kdh->hostname));
116 strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
117 if (panicstr != NULL)
118 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
119 kdh->parity = kerneldump_parity(kdh);
120 }
121
122 static int
123 buf_write(struct dumperinfo *di, char *ptr, size_t sz)
124 {
125 size_t len;
126 int error;
127
128 while (sz) {
129 len = DEV_BSIZE - fragsz;
130 if (len > sz)
131 len = sz;
132 bcopy(ptr, buffer + fragsz, len);
133 fragsz += len;
134 ptr += len;
135 sz -= len;
136 if (fragsz == DEV_BSIZE) {
137 error = di->dumper(di->priv, buffer, 0, dumplo,
138 DEV_BSIZE);
139 if (error)
140 return error;
141 dumplo += DEV_BSIZE;
142 fragsz = 0;
143 }
144 }
145
146 return (0);
147 }
148
149 static int
150 buf_flush(struct dumperinfo *di)
151 {
152 int error;
153
154 if (fragsz == 0)
155 return (0);
156
157 error = di->dumper(di->priv, buffer, 0, dumplo, DEV_BSIZE);
158 dumplo += DEV_BSIZE;
159 fragsz = 0;
160 return (error);
161 }
162
163 #define PG2MB(pgs) ((pgs + (1 << 8) - 1) >> 8)
164
165 static int
166 cb_dumpdata(struct md_pa *mdp, int seqnr, void *arg)
167 {
168 struct dumperinfo *di = (struct dumperinfo*)arg;
169 vm_paddr_t a, pa;
170 void *va;
171 uint64_t pgs;
172 size_t counter, sz, chunk;
173 int i, c, error, twiddle;
174
175 error = 0; /* catch case in which chunk size is 0 */
176 counter = 0; /* Update twiddle every 16MB */
177 twiddle = 0;
178 va = 0;
179 pgs = mdp->md_size / PAGE_SIZE;
180 pa = mdp->md_start;
181
182 printf(" chunk %d: %ldMB (%ld pages)", seqnr, PG2MB(pgs), pgs);
183
184 while (pgs) {
185 chunk = pgs;
186 if (chunk > MAXDUMPPGS)
187 chunk = MAXDUMPPGS;
188 sz = chunk << PAGE_SHIFT;
189 counter += sz;
190 if (counter >> 24) {
191 printf(" %ld", PG2MB(pgs));
192 counter &= (1<<24) - 1;
193 }
194 for (i = 0; i < chunk; i++) {
195 a = pa + i * PAGE_SIZE;
196 va = pmap_kenter_temporary(trunc_page(a), i);
197 }
198 error = di->dumper(di->priv, va, 0, dumplo, sz);
199 if (error)
200 break;
201 dumplo += sz;
202 pgs -= chunk;
203 pa += sz;
204
205 /* Check for user abort. */
206 c = cncheckc();
207 if (c == 0x03)
208 return (ECANCELED);
209 if (c != -1)
210 printf(" (CTRL-C to abort) ");
211 }
212 printf(" ... %s\n", (error) ? "fail" : "ok");
213 return (error);
214 }
215
216 static int
217 cb_dumphdr(struct md_pa *mdp, int seqnr, void *arg)
218 {
219 struct dumperinfo *di = (struct dumperinfo*)arg;
220 Elf_Phdr phdr;
221 uint64_t size;
222 int error;
223
224 size = mdp->md_size;
225 bzero(&phdr, sizeof(phdr));
226 phdr.p_type = PT_LOAD;
227 phdr.p_flags = PF_R; /* XXX */
228 phdr.p_offset = fileofs;
229 phdr.p_vaddr = mdp->md_start;
230 phdr.p_paddr = mdp->md_start;
231 phdr.p_filesz = size;
232 phdr.p_memsz = size;
233 phdr.p_align = PAGE_SIZE;
234
235 error = buf_write(di, (char*)&phdr, sizeof(phdr));
236 fileofs += phdr.p_filesz;
237 return (error);
238 }
239
240 static int
241 cb_size(struct md_pa *mdp, int seqnr, void *arg)
242 {
243 uint64_t *sz = (uint64_t*)arg;
244
245 *sz += (uint64_t)mdp->md_size;
246 return (0);
247 }
248
249 static int
250 foreach_chunk(callback_t cb, void *arg)
251 {
252 struct md_pa *mdp;
253 int error, seqnr;
254
255 seqnr = 0;
256 mdp = md_pa_first();
257 while (mdp != NULL) {
258 error = (*cb)(mdp, seqnr++, arg);
259 if (error)
260 return (-error);
261 mdp = md_pa_next(mdp);
262 }
263 return (seqnr);
264 }
265
266 void
267 dumpsys(struct dumperinfo *di)
268 {
269 Elf_Ehdr ehdr;
270 uint64_t dumpsize;
271 off_t hdrgap;
272 size_t hdrsz;
273 int error;
274
275 bzero(&ehdr, sizeof(ehdr));
276 ehdr.e_ident[EI_MAG0] = ELFMAG0;
277 ehdr.e_ident[EI_MAG1] = ELFMAG1;
278 ehdr.e_ident[EI_MAG2] = ELFMAG2;
279 ehdr.e_ident[EI_MAG3] = ELFMAG3;
280 ehdr.e_ident[EI_CLASS] = ELF_CLASS;
281 #if BYTE_ORDER == LITTLE_ENDIAN
282 ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
283 #else
284 ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
285 #endif
286 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
287 ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE; /* XXX big picture? */
288 ehdr.e_type = ET_CORE;
289 ehdr.e_machine = EM_X86_64;
290 ehdr.e_phoff = sizeof(ehdr);
291 ehdr.e_flags = 0;
292 ehdr.e_ehsize = sizeof(ehdr);
293 ehdr.e_phentsize = sizeof(Elf_Phdr);
294 ehdr.e_shentsize = sizeof(Elf_Shdr);
295
296 md_pa_init();
297
298 /* Calculate dump size. */
299 dumpsize = 0L;
300 ehdr.e_phnum = foreach_chunk(cb_size, &dumpsize);
301 hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
302 fileofs = MD_ALIGN(hdrsz);
303 dumpsize += fileofs;
304 hdrgap = fileofs - DEV_ALIGN(hdrsz);
305
306 /* Determine dump offset on device. */
307 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
308 error = ENOSPC;
309 goto fail;
310 }
311 dumplo = di->mediaoffset + di->mediasize - dumpsize;
312 dumplo -= sizeof(kdh) * 2;
313
314 mkdumpheader(&kdh, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
315
316 printf("Dumping %llu MB (%d chunks)\n", (long long)dumpsize >> 20,
317 ehdr.e_phnum);
318
319 /* Dump leader */
320 error = di->dumper(di->priv, &kdh, 0, dumplo, sizeof(kdh));
321 if (error)
322 goto fail;
323 dumplo += sizeof(kdh);
324
325 /* Dump ELF header */
326 error = buf_write(di, (char*)&ehdr, sizeof(ehdr));
327 if (error)
328 goto fail;
329
330 /* Dump program headers */
331 error = foreach_chunk(cb_dumphdr, di);
332 if (error < 0)
333 goto fail;
334 buf_flush(di);
335
336 /*
337 * All headers are written using blocked I/O, so we know the
338 * current offset is (still) block aligned. Skip the alignement
339 * in the file to have the segment contents aligned at page
340 * boundary. We cannot use MD_ALIGN on dumplo, because we don't
341 * care and may very well be unaligned within the dump device.
342 */
343 dumplo += hdrgap;
344
345 /* Dump memory chunks (updates dumplo) */
346 error = foreach_chunk(cb_dumpdata, di);
347 if (error < 0)
348 goto fail;
349
350 /* Dump trailer */
351 error = di->dumper(di->priv, &kdh, 0, dumplo, sizeof(kdh));
352 if (error)
353 goto fail;
354
355 /* Signal completion, signoff and exit stage left. */
356 di->dumper(di->priv, NULL, 0, 0, 0);
357 printf("\nDump complete\n");
358 return;
359
360 fail:
361 if (error < 0)
362 error = -error;
363
364 if (error == ECANCELED)
365 printf("\nDump aborted\n");
366 else if (error == ENOSPC)
367 printf("\nDump failed. Partition too small.\n");
368 else
369 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
370 }
Cache object: 6c9ea4ee8144177eac2f7ad5915fbbff
|