]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/minidump_machdep.c
Add kernel interfaces to call EFI Runtime Services.
[FreeBSD/FreeBSD.git] / sys / mips / mips / minidump_machdep.c
1 /*-
2  * Copyright (c) 2010 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * from: FreeBSD: src/sys/arm/arm/minidump_machdep.c v214223
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/cons.h>
37 #include <sys/kernel.h>
38 #include <sys/kerneldump.h>
39 #include <sys/msgbuf.h>
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 #include <machine/atomic.h>
43 #include <machine/elf.h>
44 #include <machine/md_var.h>
45 #include <machine/vmparam.h>
46 #include <machine/minidump.h>
47 #include <machine/cache.h>
48
49 CTASSERT(sizeof(struct kerneldumpheader) == 512);
50
51 /*
52  * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
53  * is to protect us from metadata and to protect metadata from us.
54  */
55 #define SIZEOF_METADATA         (64*1024)
56
57 uint32_t *vm_page_dump;
58 int vm_page_dump_size;
59
60 static struct kerneldumpheader kdh;
61 static off_t dumplo;
62 static off_t origdumplo;
63
64 /* Handle chunked writes. */
65 static uint64_t counter, progress;
66 /* Just auxiliary bufffer */
67 static char tmpbuffer[PAGE_SIZE];
68
69 extern pd_entry_t *kernel_segmap;
70
71 CTASSERT(sizeof(*vm_page_dump) == 4);
72
73 static int
74 is_dumpable(vm_paddr_t pa)
75 {
76         int i;
77
78         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
79                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
80                         return (1);
81         }
82         return (0);
83 }
84
85 void
86 dump_add_page(vm_paddr_t pa)
87 {
88         int idx, bit;
89
90         pa >>= PAGE_SHIFT;
91         idx = pa >> 5;          /* 2^5 = 32 */
92         bit = pa & 31;
93         atomic_set_int(&vm_page_dump[idx], 1ul << bit);
94 }
95
96 void
97 dump_drop_page(vm_paddr_t pa)
98 {
99         int idx, bit;
100
101         pa >>= PAGE_SHIFT;
102         idx = pa >> 5;          /* 2^5 = 32 */
103         bit = pa & 31;
104         atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
105 }
106
107 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
108
109 static int
110 write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
111 {
112         size_t len;
113         int error, c;
114         u_int maxdumpsz;
115
116         maxdumpsz = di->maxiosize;
117
118         if (maxdumpsz == 0)     /* seatbelt */
119                 maxdumpsz = PAGE_SIZE;
120
121         error = 0;
122
123         while (sz) {
124                 len = min(maxdumpsz, sz);
125                 counter += len;
126                 progress -= len;
127
128                 if (counter >> 22) {
129                         printf(" %jd", PG2MB(progress >> PAGE_SHIFT));
130                         counter &= (1<<22) - 1;
131                 }
132
133                 if (ptr) {
134                         error = dump_write(di, ptr, 0, dumplo, len);
135                         if (error)
136                                 return (error);
137                         dumplo += len;
138                         ptr += len;
139                         sz -= len;
140                 } else {
141                         panic("pa is not supported");
142                 }
143
144                 /* Check for user abort. */
145                 c = cncheckc();
146                 if (c == 0x03)
147                         return (ECANCELED);
148                 if (c != -1)
149                         printf(" (CTRL-C to abort) ");
150         }
151
152         return (0);
153 }
154
155 int
156 minidumpsys(struct dumperinfo *di)
157 {
158         struct minidumphdr mdhdr;
159         uint64_t dumpsize;
160         uint32_t ptesize;
161         uint32_t bits;
162         vm_paddr_t pa;
163         vm_offset_t prev_pte = 0;
164         uint32_t count = 0;
165         vm_offset_t va;
166         pt_entry_t *pte;
167         int i, bit, error;
168         void *dump_va;
169
170         /* Flush cache */
171         mips_dcache_wbinv_all();
172
173         counter = 0;
174         /* Walk page table pages, set bits in vm_page_dump */
175         ptesize = 0;
176
177         for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
178                 ptesize += PAGE_SIZE;
179                 pte = pmap_pte(kernel_pmap, va);
180                 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
181                 for (i = 0; i < NPTEPG; i++) {
182                         if (pte_test(&pte[i], PTE_V)) {
183                                 pa = TLBLO_PTE_TO_PA(pte[i]);
184                                 if (is_dumpable(pa))
185                                         dump_add_page(pa);
186                         }
187                 }
188         }
189
190         /*
191          * Now mark pages from 0 to phys_avail[0], that's where kernel 
192          * and pages allocated by pmap_steal reside
193          */
194         for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
195                 if (is_dumpable(pa))
196                         dump_add_page(pa);
197         }
198
199         /* Calculate dump size. */
200         dumpsize = ptesize;
201         dumpsize += round_page(msgbufp->msg_size);
202         dumpsize += round_page(vm_page_dump_size);
203
204         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
205                 bits = vm_page_dump[i];
206                 while (bits) {
207                         bit = ffs(bits) - 1;
208                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
209                             bit) * PAGE_SIZE;
210                         /* Clear out undumpable pages now if needed */
211                         if (is_dumpable(pa))
212                                 dumpsize += PAGE_SIZE;
213                         else
214                                 dump_drop_page(pa);
215                         bits &= ~(1ul << bit);
216                 }
217         }
218
219         dumpsize += PAGE_SIZE;
220
221         /* Determine dump offset on device. */
222         if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
223                 error = ENOSPC;
224                 goto fail;
225         }
226
227         origdumplo = dumplo = di->mediaoffset + di->mediasize - dumpsize;
228         dumplo -= sizeof(kdh) * 2;
229         progress = dumpsize;
230
231         /* Initialize mdhdr */
232         bzero(&mdhdr, sizeof(mdhdr));
233         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
234         mdhdr.version = MINIDUMP_VERSION;
235         mdhdr.msgbufsize = msgbufp->msg_size;
236         mdhdr.bitmapsize = vm_page_dump_size;
237         mdhdr.ptesize = ptesize;
238         mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
239
240         mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION, dumpsize,
241             di->blocksize);
242
243         printf("Physical memory: %ju MB\n", 
244             (uintmax_t)ptoa((uintmax_t)physmem) / 1048576);
245         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
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 my header */
254         bzero(tmpbuffer, sizeof(tmpbuffer));
255         bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
256         error = write_buffer(di, tmpbuffer, PAGE_SIZE);
257         if (error)
258                 goto fail;
259
260         /* Dump msgbuf up front */
261         error = write_buffer(di, (char *)msgbufp->msg_ptr, 
262             round_page(msgbufp->msg_size));
263         if (error)
264                 goto fail;
265
266         /* Dump bitmap */
267         error = write_buffer(di, (char *)vm_page_dump,
268             round_page(vm_page_dump_size));
269         if (error)
270                 goto fail;
271
272         /* Dump kernel page table pages */
273         for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
274                 pte = pmap_pte(kernel_pmap, va);
275                 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
276                 if (!count) {
277                         prev_pte = (vm_offset_t)pte;
278                         count++;
279                 }
280                 else {
281                         if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
282                                 count++;
283                         else {
284                                 error = write_buffer(di, (char*)prev_pte,
285                                     count * PAGE_SIZE);
286                                 if (error)
287                                         goto fail;
288                                 count = 1;
289                                 prev_pte = (vm_offset_t)pte;
290                         }
291                 }
292         }
293
294         if (count) {
295                 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
296                 if (error)
297                         goto fail;
298                 count = 0;
299                 prev_pte = 0;
300         }
301
302         /* Dump memory chunks  page by page*/
303         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
304                 bits = vm_page_dump[i];
305                 while (bits) {
306                         bit = ffs(bits) - 1;
307                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
308                             bit) * PAGE_SIZE;
309                         dump_va = pmap_kenter_temporary(pa, 0);
310                         error = write_buffer(di, dump_va, PAGE_SIZE);
311                         if (error)
312                                 goto fail;
313                         pmap_kenter_temporary_free(pa);
314                         bits &= ~(1ul << bit);
315                 }
316         }
317
318         /* Dump trailer */
319         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
320         if (error)
321                 goto fail;
322         dumplo += sizeof(kdh);
323
324         /* Signal completion, signoff and exit stage left. */
325         dump_write(di, NULL, 0, 0, 0);
326         printf("\nDump complete\n");
327         return (0);
328
329 fail:
330         if (error < 0)
331                 error = -error;
332
333         if (error == ECANCELED)
334                 printf("\nDump aborted\n");
335         else if (error == ENOSPC)
336                 printf("\nDump failed. Partition too small.\n");
337         else
338                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
339         return (error);
340 }