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