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>
36 #include <sys/vimage.h>
39 #include <vm/vm_param.h>
42 #include <machine/metadata.h>
43 #include <machine/kerneldump.h>
44 #include <machine/ofw_mem.h>
45 #include <machine/tsb.h>
46 #include <machine/tlb.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)
60 buf_write(struct dumperinfo *di, char *ptr, size_t sz)
66 len = DEV_BSIZE - fragsz;
69 bcopy(ptr, buffer + fragsz, len);
73 if (fragsz == DEV_BSIZE) {
74 error = dump_write(di, buffer, 0, dumplo,
87 buf_flush(struct dumperinfo *di)
94 error = dump_write(di, buffer, 0, dumplo, DEV_BSIZE);
100 reg_write(struct dumperinfo *di, vm_paddr_t pa, vm_size_t size)
102 struct sparc64_dump_reg r;
108 return (buf_write(di, (char *)&r, sizeof(r)));
112 blk_dump(struct dumperinfo *di, vm_paddr_t pa, vm_size_t size)
116 int c, counter, error, twiddle;
118 printf(" chunk at %#lx: %ld bytes ", (u_long)pa, (long)size);
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]);
126 rsz = (rsz > MAXDUMPSZ) ? MAXDUMPSZ : rsz;
127 va = TLB_PHYS_TO_DIRECT(pa + pos);
128 error = dump_write(di, (void *)va, 0, dumplo, rsz);
133 /* Check for user abort. */
138 printf("(CTRL-C to abort) ");
140 printf("... %s\n", (error) ? "fail" : "ok");
145 dumpsys(struct dumperinfo *di)
147 struct sparc64_dump_hdr hdr;
148 vm_size_t size, totsize, hdrsize;
151 /* Calculate dump size. */
153 nreg = sparc64_nmemreg;
154 for (i = 0; i < sparc64_nmemreg; i++)
155 size += sparc64_memreg[i].mr_size;
156 /* Account for the header size. */
157 hdrsize = roundup2(sizeof(hdr) + sizeof(struct sparc64_dump_reg) * nreg,
161 totsize = size + 2 * sizeof(kdh);
162 if (totsize > di->mediasize) {
163 printf("Insufficient space on device (need %ld, have %ld), "
164 "refusing to dump.\n", (long)totsize,
165 (long)di->mediasize);
170 /* Determine dump offset on device. */
171 dumplo = di->mediaoffset + di->mediasize - totsize;
173 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_SPARC64_VERSION, size, di->blocksize);
175 printf("Dumping %lu MB (%d chunks)\n", (u_long)(size >> 20), nreg);
178 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
181 dumplo += sizeof(kdh);
183 /* Dump the private header. */
184 hdr.dh_hdr_size = hdrsize;
185 hdr.dh_tsb_pa = tsb_kernel_phys;
186 hdr.dh_tsb_size = tsb_kernel_size;
187 hdr.dh_tsb_mask = tsb_kernel_mask;
188 hdr.dh_nregions = nreg;
190 if (buf_write(di, (char *)&hdr, sizeof(hdr)) != 0)
194 /* Now, write out the region descriptors. */
195 for (i = 0; i < sparc64_nmemreg; i++) {
196 error = reg_write(di, sparc64_memreg[i].mr_start,
197 sparc64_memreg[i].mr_size);
203 /* Dump memory chunks. */
204 for (i = 0; i < sparc64_nmemreg; i++) {
205 error = blk_dump(di, sparc64_memreg[i].mr_start,
206 sparc64_memreg[i].mr_size);
212 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
216 /* Signal completion, signoff and exit stage left. */
217 dump_write(di, NULL, 0, 0, 0);
218 printf("\nDump complete\n");
222 /* XXX It should look more like VMS :-) */
223 printf("** DUMP FAILED (ERROR %d) **\n", error);