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/tsb.h>
45 #include <machine/tlb.h>
47 CTASSERT(sizeof(struct kerneldumpheader) == DEV_BSIZE);
49 static struct kerneldumpheader kdh;
50 static off_t dumplo, dumppos;
52 /* Handle buffered writes. */
53 static char buffer[DEV_BSIZE];
54 static vm_size_t fragsz;
56 #define MAXDUMPSZ (MAXDUMPPGS << PAGE_SHIFT)
58 /* XXX should be MI */
60 mkdumpheader(struct kerneldumpheader *kdh, uint32_t archver, uint64_t dumplen,
64 bzero(kdh, sizeof(*kdh));
65 strncpy(kdh->magic, KERNELDUMPMAGIC, sizeof(kdh->magic));
66 strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
67 kdh->version = htod32(KERNELDUMPVERSION);
68 kdh->architectureversion = htod32(archver);
69 kdh->dumplength = htod64(dumplen);
70 kdh->dumptime = htod64(time_second);
71 kdh->blocksize = htod32(blksz);
72 strncpy(kdh->hostname, hostname, sizeof(kdh->hostname));
73 strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
75 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
76 kdh->parity = kerneldump_parity(kdh);
80 buf_write(struct dumperinfo *di, char *ptr, size_t sz)
86 len = DEV_BSIZE - fragsz;
89 bcopy(ptr, buffer + fragsz, len);
93 if (fragsz == DEV_BSIZE) {
94 error = dump_write(di, buffer, 0, dumplo,
107 buf_flush(struct dumperinfo *di)
114 error = dump_write(di, buffer, 0, dumplo, DEV_BSIZE);
120 reg_write(struct dumperinfo *di, vm_paddr_t pa, vm_size_t size)
122 struct sparc64_dump_reg r;
128 return (buf_write(di, (char *)&r, sizeof(r)));
132 blk_dump(struct dumperinfo *di, vm_paddr_t pa, vm_size_t size)
136 int c, counter, error, twiddle;
138 printf(" chunk at %#lx: %ld bytes ", (u_long)pa, (long)size);
141 error = counter = twiddle = 0;
142 for (pos = 0; pos < size; pos += MAXDUMPSZ, counter++) {
143 if (counter % 128 == 0)
144 printf("%c\b", "|/-\\"[twiddle++ & 3]);
146 rsz = (rsz > MAXDUMPSZ) ? MAXDUMPSZ : rsz;
147 va = TLB_PHYS_TO_DIRECT(pa + pos);
148 error = dump_write(di, (void *)va, 0, dumplo, rsz);
153 /* Check for user abort. */
158 printf("(CTRL-C to abort) ");
160 printf("... %s\n", (error) ? "fail" : "ok");
165 dumpsys(struct dumperinfo *di)
167 struct sparc64_dump_hdr hdr;
168 vm_size_t size, totsize, hdrsize;
171 /* Calculate dump size. */
173 nreg = sparc64_nmemreg;
174 for (i = 0; i < sparc64_nmemreg; i++)
175 size += sparc64_memreg[i].mr_size;
176 /* Account for the header size. */
177 hdrsize = roundup2(sizeof(hdr) + sizeof(struct sparc64_dump_reg) * nreg,
181 totsize = size + 2 * sizeof(kdh);
182 if (totsize > di->mediasize) {
183 printf("Insufficient space on device (need %ld, have %ld), "
184 "refusing to dump.\n", (long)totsize,
185 (long)di->mediasize);
190 /* Determine dump offset on device. */
191 dumplo = di->mediaoffset + di->mediasize - totsize;
193 mkdumpheader(&kdh, KERNELDUMP_SPARC64_VERSION, size, di->blocksize);
195 printf("Dumping %lu MB (%d chunks)\n", (u_long)(size >> 20), nreg);
198 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
201 dumplo += sizeof(kdh);
203 /* Dump the private header. */
204 hdr.dh_hdr_size = hdrsize;
205 hdr.dh_tsb_pa = tsb_kernel_phys;
206 hdr.dh_tsb_size = tsb_kernel_size;
207 hdr.dh_tsb_mask = tsb_kernel_mask;
208 hdr.dh_nregions = nreg;
210 if (buf_write(di, (char *)&hdr, sizeof(hdr)) != 0)
214 /* Now, write out the region descriptors. */
215 for (i = 0; i < sparc64_nmemreg; i++) {
216 error = reg_write(di, sparc64_memreg[i].mr_start,
217 sparc64_memreg[i].mr_size);
223 /* Dump memory chunks. */
224 for (i = 0; i < sparc64_nmemreg; i++) {
225 error = blk_dump(di, sparc64_memreg[i].mr_start,
226 sparc64_memreg[i].mr_size);
232 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
236 /* Signal completion, signoff and exit stage left. */
237 dump_write(di, NULL, 0, 0, 0);
238 printf("\nDump complete\n");
242 /* XXX It should look more like VMS :-) */
243 printf("** DUMP FAILED (ERROR %d) **\n", error);