]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/dump_machdep.c
MFV: r313101
[FreeBSD/FreeBSD.git] / sys / sparc64 / sparc64 / dump_machdep.c
1 /*-
2  * Copyright (c) 2002 Marcel Moolenaar
3  * Copyright (c) 2002 Thomas Moestl
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/conf.h>
34 #include <sys/cons.h>
35 #include <sys/kernel.h>
36 #include <sys/kerneldump.h>
37
38 #include <vm/vm.h>
39 #include <vm/vm_param.h>
40 #include <vm/pmap.h>
41
42 #include <machine/dump.h>
43 #include <machine/md_var.h>
44 #include <machine/metadata.h>
45 #include <machine/kerneldump.h>
46 #include <machine/ofw_mem.h>
47 #include <machine/tsb.h>
48 #include <machine/tlb.h>
49
50 static off_t fileofs;
51
52 extern off_t dumplo;
53 extern struct dump_pa dump_map[DUMPSYS_MD_PA_NPAIRS];
54
55 int do_minidump = 0;
56
57 void
58 dumpsys_map_chunk(vm_paddr_t pa, size_t chunk __unused, void **va)
59 {
60
61         *va = (void *)TLB_PHYS_TO_DIRECT(pa);
62 }
63
64 static int
65 reg_write(struct dumperinfo *di, vm_paddr_t pa, vm_size_t size)
66 {
67         struct sparc64_dump_reg r;
68
69         r.dr_pa = pa;
70         r.dr_size = size;
71         r.dr_offs = fileofs;
72         fileofs += size;
73         return (dumpsys_buf_write(di, (char *)&r, sizeof(r)));
74 }
75
76 int
77 dumpsys(struct dumperinfo *di)
78 {
79         static struct kerneldumpheader kdh;
80         struct sparc64_dump_hdr hdr;
81         vm_size_t size, totsize, hdrsize;
82         int error, i, nreg;
83
84         /* Set up dump_map and calculate dump size. */
85         size = 0;
86         nreg = sparc64_nmemreg;
87         memset(dump_map, 0, sizeof(dump_map));
88         for (i = 0; i < nreg; i++) {
89                 dump_map[i].pa_start = sparc64_memreg[i].mr_start;
90                 size += dump_map[i].pa_size = sparc64_memreg[i].mr_size;
91         }
92         /* Account for the header size. */
93         hdrsize = roundup2(sizeof(hdr) + sizeof(struct sparc64_dump_reg) * nreg,
94             DEV_BSIZE);
95         size += hdrsize;
96
97         totsize = size + 2 * di->blocksize +
98             kerneldumpcrypto_dumpkeysize(di->kdc);
99         if (totsize > di->mediasize) {
100                 printf("Insufficient space on device (need %ld, have %ld), "
101                     "refusing to dump.\n", (long)totsize,
102                     (long)di->mediasize);
103                 error = ENOSPC;
104                 goto fail;
105         }
106
107         /* Determine dump offset on device. */
108         dumplo = di->mediaoffset + di->mediasize - totsize;
109
110         /* Initialize kernel dump crypto. */
111         error = kerneldumpcrypto_init(di->kdc);
112         if (error)
113                 goto fail;
114
115         mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_SPARC64_VERSION, size,
116             kerneldumpcrypto_dumpkeysize(di->kdc), di->blocksize);
117
118         printf("Dumping %lu MB (%d chunks)\n", (u_long)(size >> 20), nreg);
119
120         /* Dump leader */
121         error = dump_write_header(di, &kdh, 0, dumplo);
122         if (error)
123                 goto fail;
124         dumplo += di->blocksize;
125
126         /* Dump key */
127         error = dump_write_key(di, 0, dumplo);
128         if (error)
129                 goto fail;
130         dumplo += kerneldumpcrypto_dumpkeysize(di->kdc);
131
132         /* Dump the private header. */
133         hdr.dh_hdr_size = hdrsize;
134         hdr.dh_tsb_pa = tsb_kernel_phys;
135         hdr.dh_tsb_size = tsb_kernel_size;
136         hdr.dh_tsb_mask = tsb_kernel_mask;
137         hdr.dh_nregions = nreg;
138
139         if (dumpsys_buf_write(di, (char *)&hdr, sizeof(hdr)) != 0)
140                 goto fail;
141
142         fileofs = hdrsize;
143         /* Now, write out the region descriptors. */
144         for (i = 0; i < nreg; i++) {
145                 error = reg_write(di, sparc64_memreg[i].mr_start,
146                     sparc64_memreg[i].mr_size);
147                 if (error != 0)
148                         goto fail;
149         }
150         dumpsys_buf_flush(di);
151
152         /* Dump memory chunks. */
153         error = dumpsys_foreach_chunk(dumpsys_cb_dumpdata, di);
154         if (error < 0)
155                 goto fail;
156
157         /* Dump trailer */
158         error = dump_write_header(di, &kdh, 0, dumplo);
159         if (error)
160                 goto fail;
161         dumplo += di->blocksize;
162
163         /* Signal completion, signoff and exit stage left. */
164         dump_write(di, NULL, 0, 0, 0);
165         printf("\nDump complete\n");
166         return (0);
167
168  fail:
169         if (error < 0)
170                 error = -error;
171
172         /* XXX It should look more like VMS :-) */
173         printf("** DUMP FAILED (ERROR %d) **\n", error);
174         return (error);
175 }