]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ia64/ia64/dump_machdep.c
Merge ath_hal 0.10.5.10 to head.
[FreeBSD/FreeBSD.git] / sys / ia64 / ia64 / dump_machdep.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 <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 <sys/vimage.h>
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 #include <machine/efi.h>
40 #include <machine/elf.h>
41 #include <machine/md_var.h>
42
43 CTASSERT(sizeof(struct kerneldumpheader) == 512);
44
45 /*
46  * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
47  * is to protect us from metadata and to protect metadata from us.
48  */
49 #define SIZEOF_METADATA         (64*1024)
50
51 #define MD_ALIGN(x)     (((off_t)(x) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK)
52 #define DEV_ALIGN(x)    (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
53
54 typedef int callback_t(struct efi_md*, int, void*);
55
56 static struct kerneldumpheader kdh;
57 static off_t dumplo, fileofs;
58
59 /* Handle buffered writes. */
60 static char buffer[DEV_BSIZE];
61 static size_t fragsz;
62
63 /* XXX should be MI */
64 static void
65 mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen,
66     uint32_t blksz)
67 {
68
69         bzero(kdh, sizeof(*kdh));
70         strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic));
71         strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
72         kdh->version = htod32(KERNELDUMPVERSION);
73         kdh->architectureversion = htod32(archver);
74         kdh->dumplength = htod64(dumplen);
75         kdh->dumptime = htod64(time_second);
76         kdh->blocksize = htod32(blksz);
77         strncpy(kdh->hostname, G_hostname, sizeof(kdh->hostname));
78         strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
79         if (panicstr != NULL)
80                 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
81         kdh->parity = kerneldump_parity(kdh);
82 }
83
84 static int
85 buf_write(struct dumperinfo *di, char *ptr, size_t sz)
86 {
87         size_t len;
88         int error;
89
90         while (sz) {
91                 len = DEV_BSIZE - fragsz;
92                 if (len > sz)
93                         len = sz;
94                 bcopy(ptr, buffer + fragsz, len);
95                 fragsz += len;
96                 ptr += len;
97                 sz -= len;
98                 if (fragsz == DEV_BSIZE) {
99                         error = dump_write(di, buffer, 0, dumplo,
100                             DEV_BSIZE);
101                         if (error)
102                                 return error;
103                         dumplo += DEV_BSIZE;
104                         fragsz = 0;
105                 }
106         }
107
108         return (0);
109 }
110
111 static int
112 buf_flush(struct dumperinfo *di)
113 {
114         int error;
115
116         if (fragsz == 0)
117                 return (0);
118
119         error = dump_write(di, buffer, 0, dumplo, DEV_BSIZE);
120         dumplo += DEV_BSIZE;
121         fragsz = 0;
122         return (error);
123 }
124
125 static int
126 cb_dumpdata(struct efi_md *mdp, int seqnr, void *arg)
127 {
128         struct dumperinfo *di = (struct dumperinfo*)arg;
129         vm_offset_t pa;
130         uint64_t pgs;
131         size_t counter, sz;
132         int c, error, twiddle;
133
134         error = 0;      /* catch case in which mdp->md_pages is 0 */
135         counter = 0;    /* Update twiddle every 16MB */
136         twiddle = 0;
137         pgs = mdp->md_pages;
138         pa = IA64_PHYS_TO_RR7(mdp->md_phys);
139
140         printf("  chunk %d: %ld pages ", seqnr, (long)pgs);
141
142         while (pgs) {
143                 sz = (pgs > (DFLTPHYS >> EFI_PAGE_SHIFT))
144                     ? DFLTPHYS : pgs << EFI_PAGE_SHIFT;
145                 counter += sz;
146                 if (counter >> 24) {
147                         printf("%c\b", "|/-\\"[twiddle++ & 3]);
148                         counter &= (1<<24) - 1;
149                 }
150                 error = dump_write(di, (void*)pa, 0, dumplo, sz);
151                 if (error)
152                         break;
153                 dumplo += sz;
154                 pgs -= sz >> EFI_PAGE_SHIFT;
155                 pa += sz;
156
157                 /* Check for user abort. */
158                 c = cncheckc();
159                 if (c == 0x03)
160                         return (ECANCELED);
161                 if (c != -1)
162                         printf("(CTRL-C to abort)  ");
163         }
164         printf("... %s\n", (error) ? "fail" : "ok");
165         return (error);
166 }
167
168 static int
169 cb_dumphdr(struct efi_md *mdp, int seqnr, void *arg)
170 {
171         struct dumperinfo *di = (struct dumperinfo*)arg;
172         Elf64_Phdr phdr;
173         int error;
174
175         bzero(&phdr, sizeof(phdr));
176         phdr.p_type = PT_LOAD;
177         phdr.p_flags = PF_R;                    /* XXX */
178         phdr.p_offset = fileofs;
179         phdr.p_vaddr = (uintptr_t)mdp->md_virt; /* XXX probably bogus. */
180         phdr.p_paddr = mdp->md_phys;
181         phdr.p_filesz = mdp->md_pages << EFI_PAGE_SHIFT;
182         phdr.p_memsz = mdp->md_pages << EFI_PAGE_SHIFT;
183         phdr.p_align = EFI_PAGE_SIZE;
184
185         error = buf_write(di, (char*)&phdr, sizeof(phdr));
186         fileofs += phdr.p_filesz;
187         return (error);
188 }
189
190 static int
191 cb_size(struct efi_md *mdp, int seqnr, void *arg)
192 {
193         uint64_t *sz = (uint64_t*)arg;
194
195         *sz += (uint64_t)mdp->md_pages << EFI_PAGE_SHIFT;
196         return (0);
197 }
198
199 static int
200 foreach_chunk(callback_t cb, void *arg)
201 {
202         struct efi_md *mdp;
203         int error, seqnr;
204
205         seqnr = 0;
206         mdp = efi_md_first();
207         while (mdp != NULL) {
208                 if (mdp->md_type == EFI_MD_TYPE_FREE) {
209                         error = (*cb)(mdp, seqnr++, arg);
210                         if (error)
211                                 return (-error);
212                 }
213                 mdp = efi_md_next(mdp);
214         }
215         return (seqnr);
216 }
217
218 void
219 dumpsys(struct dumperinfo *di)
220 {
221         Elf64_Ehdr ehdr;
222         uint64_t dumpsize;
223         off_t hdrgap;
224         size_t hdrsz;
225         int error;
226
227         bzero(&ehdr, sizeof(ehdr));
228         ehdr.e_ident[EI_MAG0] = ELFMAG0;
229         ehdr.e_ident[EI_MAG1] = ELFMAG1;
230         ehdr.e_ident[EI_MAG2] = ELFMAG2;
231         ehdr.e_ident[EI_MAG3] = ELFMAG3;
232         ehdr.e_ident[EI_CLASS] = ELFCLASS64;
233 #if BYTE_ORDER == LITTLE_ENDIAN
234         ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
235 #else
236         ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
237 #endif
238         ehdr.e_ident[EI_VERSION] = EV_CURRENT;
239         ehdr.e_ident[EI_OSABI] = ELFOSABI_STANDALONE;   /* XXX big picture? */
240         ehdr.e_type = ET_CORE;
241         ehdr.e_machine = EM_IA_64;
242         ehdr.e_phoff = sizeof(ehdr);
243         ehdr.e_flags = EF_IA_64_ABSOLUTE;               /* XXX misuse? */
244         ehdr.e_ehsize = sizeof(ehdr);
245         ehdr.e_phentsize = sizeof(Elf64_Phdr);
246         ehdr.e_shentsize = sizeof(Elf64_Shdr);
247
248         /* Calculate dump size. */
249         dumpsize = 0L;
250         ehdr.e_phnum = foreach_chunk(cb_size, &dumpsize);
251         hdrsz = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize;
252         fileofs = MD_ALIGN(hdrsz);
253         dumpsize += fileofs;
254         hdrgap = fileofs - DEV_ALIGN(hdrsz);
255
256         /* Determine dump offset on device. */
257         if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
258                 error = ENOSPC;
259                 goto fail;
260         }
261         dumplo = di->mediaoffset + di->mediasize - dumpsize;
262         dumplo -= sizeof(kdh) * 2;
263
264         mkdumpheader(&kdh, KERNELDUMP_IA64_VERSION, dumpsize, di->blocksize);
265
266         printf("Dumping %llu MB (%d chunks)\n", (long long)dumpsize >> 20,
267             ehdr.e_phnum);
268
269         /* Dump leader */
270         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
271         if (error)
272                 goto fail;
273         dumplo += sizeof(kdh);
274
275         /* Dump ELF header */
276         error = buf_write(di, (char*)&ehdr, sizeof(ehdr));
277         if (error)
278                 goto fail;
279
280         /* Dump program headers */
281         error = foreach_chunk(cb_dumphdr, di);
282         if (error < 0)
283                 goto fail;
284         buf_flush(di);
285
286         /*
287          * All headers are written using blocked I/O, so we know the
288          * current offset is (still) block aligned. Skip the alignement
289          * in the file to have the segment contents aligned at page
290          * boundary. We cannot use MD_ALIGN on dumplo, because we don't
291          * care and may very well be unaligned within the dump device.
292          */
293         dumplo += hdrgap;
294
295         /* Dump memory chunks (updates dumplo) */
296         error = foreach_chunk(cb_dumpdata, di);
297         if (error < 0)
298                 goto fail;
299
300         /* Dump trailer */
301         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
302         if (error)
303                 goto fail;
304
305         /* Signal completion, signoff and exit stage left. */
306         dump_write(di, NULL, 0, 0, 0);
307         printf("\nDump complete\n");
308         return;
309
310  fail:
311         if (error < 0)
312                 error = -error;
313
314         if (error == ECANCELED)
315                 printf("\nDump aborted\n");
316         else
317                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
318 }