]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/sun4v/sun4v/dump_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / sun4v / sun4v / 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  * $FreeBSD$
28  */
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
37 #include <vm/vm.h>
38 #include <vm/vm_param.h>
39 #include <vm/pmap.h>
40
41 #include <machine/metadata.h>
42 #include <machine/kerneldump.h>
43 #include <machine/ofw_mem.h>
44 #include <machine/tte.h>
45 #include <machine/tsb.h>
46
47
48 CTASSERT(sizeof(struct kerneldumpheader) == DEV_BSIZE);
49
50 static struct kerneldumpheader kdh;
51 static off_t dumplo, dumppos;
52
53 /* Handle buffered writes. */
54 static char buffer[DEV_BSIZE];
55 static vm_size_t fragsz;
56
57 #define MAXDUMPSZ       (MAXDUMPPGS << PAGE_SHIFT)
58
59 static int
60 buf_write(struct dumperinfo *di, char *ptr, size_t sz)
61 {
62         size_t len;
63         int error;
64
65         while (sz) {
66                 len = DEV_BSIZE - fragsz;
67                 if (len > sz)
68                         len = sz;
69                 bcopy(ptr, buffer + fragsz, len);
70                 fragsz += len;
71                 ptr += len;
72                 sz -= len;
73                 if (fragsz == DEV_BSIZE) {
74                         error = dump_write(di, buffer, 0, dumplo,
75                             DEV_BSIZE);
76                         if (error)
77                                 return error;
78                         dumplo += DEV_BSIZE;
79                         fragsz = 0;
80                 }
81         }
82
83         return (0);
84 }
85
86 static int
87 buf_flush(struct dumperinfo *di)
88 {
89         int error;
90
91         if (fragsz == 0)
92                 return (0);
93
94         error = dump_write(di, buffer, 0, dumplo, DEV_BSIZE);
95         dumplo += DEV_BSIZE;
96         return (error);
97 }
98
99 static int
100 reg_write(struct dumperinfo *di, vm_paddr_t pa, vm_size_t size)
101 {
102         struct sparc64_dump_reg r;
103
104         r.dr_pa = pa;
105         r.dr_size = size;
106         r.dr_offs = dumppos;
107         dumppos += size;
108         return (buf_write(di, (char *)&r, sizeof(r)));
109 }
110
111 static int
112 blk_dump(struct dumperinfo *di, vm_paddr_t pa, vm_size_t size)
113 {
114         vm_size_t pos, rsz;
115         vm_offset_t va;
116         int c, counter, error, twiddle;
117
118         printf("  chunk at %#lx: %ld bytes ", (u_long)pa, (long)size);
119
120         va = 0L;
121         error = counter = twiddle = 0;
122         for (pos = 0; pos < size; pos += MAXDUMPSZ, counter++) {
123                 if (counter % 128 == 0)
124                         printf("%c\b", "|/-\\"[twiddle++ & 3]);
125                 rsz = size - pos;
126                 rsz = (rsz > MAXDUMPSZ) ? MAXDUMPSZ : rsz;
127 #ifdef notyet
128                 va = TLB_PHYS_TO_DIRECT(pa + pos);
129 #endif
130                 error = dump_write(di, (void *)va, 0, dumplo, rsz);
131                 if (error)
132                         break;
133                 dumplo += rsz;
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 void
147 dumpsys(struct dumperinfo *di)
148 {
149         struct sparc64_dump_hdr hdr;
150         vm_size_t size, totsize, hdrsize;
151         int error, i, nreg;
152
153         /* Calculate dump size. */
154         size = 0;
155         nreg = sparc64_nmemreg;
156         for (i = 0; i < sparc64_nmemreg; i++)
157                 size += sparc64_memreg[i].mr_size;
158         /* Account for the header size. */
159         hdrsize = roundup2(sizeof(hdr) + sizeof(struct sparc64_dump_reg) * nreg,
160             DEV_BSIZE);
161         size += hdrsize;
162
163         totsize = size + 2 * sizeof(kdh);
164         if (totsize > di->mediasize) {
165                 printf("Insufficient space on device (need %ld, have %ld), "
166                     "refusing to dump.\n", (long)totsize,
167                     (long)di->mediasize);
168                 error = ENOSPC;
169                 goto fail;
170         }
171
172         /* Determine dump offset on device. */
173         dumplo = di->mediaoffset + di->mediasize - totsize;
174
175         mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_SPARC64_VERSION, size, di->blocksize);
176
177         printf("Dumping %lu MB (%d chunks)\n", (u_long)(size >> 20), nreg);
178
179         /* Dump leader */
180         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
181         if (error)
182                 goto fail;
183         dumplo += sizeof(kdh);
184
185         /* Dump the private header. */
186         hdr.dh_hdr_size = hdrsize;
187 #ifdef notyet
188         /* XXX SUN4V_FIXME */
189         hdr.dh_tsb_pa = tsb_kernel_phys;
190         hdr.dh_tsb_size = tsb_kernel_size;
191 #endif
192         hdr.dh_nregions = nreg;
193
194         if (buf_write(di, (char *)&hdr, sizeof(hdr)) != 0)
195                 goto fail;
196
197         dumppos = hdrsize;
198         /* Now, write out the region descriptors. */
199         for (i = 0; i < sparc64_nmemreg; i++) {
200                 error = reg_write(di, sparc64_memreg[i].mr_start,
201                     sparc64_memreg[i].mr_size);
202                 if (error != 0)
203                         goto fail;
204         }
205         buf_flush(di);
206
207         /* Dump memory chunks. */
208         for (i = 0; i < sparc64_nmemreg; i++) {
209                 error = blk_dump(di, sparc64_memreg[i].mr_start,
210                     sparc64_memreg[i].mr_size);
211                 if (error != 0)
212                         goto fail;
213         }
214
215         /* Dump trailer */
216         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
217         if (error)
218                 goto fail;
219
220         /* Signal completion, signoff and exit stage left. */
221         dump_write(di, NULL, 0, 0, 0);
222         printf("\nDump complete\n");
223         return;
224
225  fail:
226         /* XXX It should look more like VMS :-) */
227         printf("** DUMP FAILED (ERROR %d) **\n", error);
228 }