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