2 * Copyright (c) 2002 Marcel Moolenaar
3 * Copyright (c) 2002 Thomas Moestl
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/kerneldump.h>
38 #include <vm/vm_param.h>
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>
48 CTASSERT(sizeof(struct kerneldumpheader) == DEV_BSIZE);
50 static struct kerneldumpheader kdh;
51 static off_t dumplo, dumppos;
53 /* Handle buffered writes. */
54 static char buffer[DEV_BSIZE];
55 static vm_size_t fragsz;
57 #define MAXDUMPSZ (MAXDUMPPGS << PAGE_SHIFT)
59 /* XXX should be MI */
61 mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen,
65 bzero(kdh, sizeof(*kdh));
66 strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic));
67 strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
68 kdh->version = htod32(KERNELDUMPVERSION);
69 kdh->architectureversion = htod32(archver);
70 kdh->dumplength = htod64(dumplen);
71 kdh->dumptime = htod64(time_second);
72 kdh->blocksize = htod32(blksz);
73 strncpy(kdh->hostname, hostname, sizeof(kdh->hostname));
74 strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
76 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
77 kdh->parity = kerneldump_parity(kdh);
81 buf_write(struct dumperinfo *di, char *ptr, size_t sz)
87 len = DEV_BSIZE - fragsz;
90 bcopy(ptr, buffer + fragsz, len);
94 if (fragsz == DEV_BSIZE) {
95 error = dump_write(di, buffer, 0, dumplo,
108 buf_flush(struct dumperinfo *di)
115 error = dump_write(di, buffer, 0, dumplo, DEV_BSIZE);
121 reg_write(struct dumperinfo *di, vm_paddr_t pa, vm_size_t size)
123 struct sparc64_dump_reg r;
129 return (buf_write(di, (char *)&r, sizeof(r)));
133 blk_dump(struct dumperinfo *di, vm_paddr_t pa, vm_size_t size)
137 int c, counter, error, twiddle;
139 printf(" chunk at %#lx: %ld bytes ", (u_long)pa, (long)size);
142 error = counter = twiddle = 0;
143 for (pos = 0; pos < size; pos += MAXDUMPSZ, counter++) {
144 if (counter % 128 == 0)
145 printf("%c\b", "|/-\\"[twiddle++ & 3]);
147 rsz = (rsz > MAXDUMPSZ) ? MAXDUMPSZ : rsz;
149 va = TLB_PHYS_TO_DIRECT(pa + pos);
151 error = dump_write(di, (void *)va, 0, dumplo, rsz);
156 /* Check for user abort. */
161 printf("(CTRL-C to abort) ");
163 printf("... %s\n", (error) ? "fail" : "ok");
168 dumpsys(struct dumperinfo *di)
170 struct sparc64_dump_hdr hdr;
171 vm_size_t size, totsize, hdrsize;
174 /* Calculate dump size. */
176 nreg = sparc64_nmemreg;
177 for (i = 0; i < sparc64_nmemreg; i++)
178 size += sparc64_memreg[i].mr_size;
179 /* Account for the header size. */
180 hdrsize = roundup2(sizeof(hdr) + sizeof(struct sparc64_dump_reg) * nreg,
184 totsize = size + 2 * sizeof(kdh);
185 if (totsize > di->mediasize) {
186 printf("Insufficient space on device (need %ld, have %ld), "
187 "refusing to dump.\n", (long)totsize,
188 (long)di->mediasize);
193 /* Determine dump offset on device. */
194 dumplo = di->mediaoffset + di->mediasize - totsize;
196 mkdumpheader(&kdh, KERNELDUMP_SPARC64_VERSION, size, di->blocksize);
198 printf("Dumping %lu MB (%d chunks)\n", (u_long)(size >> 20), nreg);
201 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
204 dumplo += sizeof(kdh);
206 /* Dump the private header. */
207 hdr.dh_hdr_size = hdrsize;
209 /* XXX SUN4V_FIXME */
210 hdr.dh_tsb_pa = tsb_kernel_phys;
211 hdr.dh_tsb_size = tsb_kernel_size;
213 hdr.dh_nregions = nreg;
215 if (buf_write(di, (char *)&hdr, sizeof(hdr)) != 0)
219 /* Now, write out the region descriptors. */
220 for (i = 0; i < sparc64_nmemreg; i++) {
221 error = reg_write(di, sparc64_memreg[i].mr_start,
222 sparc64_memreg[i].mr_size);
228 /* Dump memory chunks. */
229 for (i = 0; i < sparc64_nmemreg; i++) {
230 error = blk_dump(di, sparc64_memreg[i].mr_start,
231 sparc64_memreg[i].mr_size);
237 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
241 /* Signal completion, signoff and exit stage left. */
242 dump_write(di, NULL, 0, 0, 0);
243 printf("\nDump complete\n");
247 /* XXX It should look more like VMS :-) */
248 printf("** DUMP FAILED (ERROR %d) **\n", error);