1 /* $OpenBSD: stubs.c,v 1.13 2021/05/16 03:39:27 jsg Exp $ */
2 /* $NetBSD: stubs.c,v 1.14 2003/07/15 00:24:42 lukem Exp $ */
3
4 /*
5 * Copyright (c) 1994-1998 Mark Brinicombe.
6 * Copyright (c) 1994 Brini.
7 * All rights reserved.
8 *
9 * This code is derived from software written for Brini by Mark Brinicombe
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Mark Brinicombe
22 * for the NetBSD Project.
23 * 4. The name of the company nor the name of the author may be used to
24 * endorse or promote products derived from this software without specific
25 * prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * Routines that are temporary or do not have a home yet.
40 *
41 * Created : 17/09/94
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/conf.h>
48 #include <sys/exec.h>
49 #include <sys/core.h>
50 #include <sys/kcore.h>
51 #include <uvm/uvm_extern.h>
52 #include <machine/bootconfig.h>
53 #include <machine/cpu.h>
54 #include <machine/pcb.h>
55 #include <arm/kcore.h>
56 #include <arm/machdep.h>
57
58 extern dev_t dumpdev;
59
60 /*
61 * These variables are needed by /sbin/savecore
62 */
63 u_int32_t dumpmag = 0x8fca0101; /* magic number */
64 int dumpsize = 0; /* pages */
65 long dumplo = 0; /* blocks */
66 cpu_kcore_hdr_t cpu_kcore_hdr;
67 struct pcb dumppcb;
68
69 /*
70 * This is called by main to set dumplo and dumpsize.
71 * Dumps always skip the first CLBYTES of disk space
72 * in case there might be a disk label stored there.
73 * If there is extra space, put dump at the end to
74 * reduce the chance that swapping trashes it.
75 */
76
77 void dumpconf(void);
78
79 void
dumpconf(void)80 dumpconf(void)
81 {
82 int nblks, block;
83
84 if (dumpdev == NODEV ||
85 (nblks = (bdevsw[major(dumpdev)].d_psize)(dumpdev)) == 0)
86 return;
87 if (nblks <= ctod(1))
88 return;
89
90 dumpsize = physmem;
91
92 /* Always skip the first CLBYTES, in case there is a label there. */
93 if (dumplo < ctod(1))
94 dumplo = ctod(1);
95
96 /* Put dump at end of partition, and make it fit. */
97 if (dumpsize + 1 > dtoc(nblks - dumplo))
98 dumpsize = dtoc(nblks - dumplo) - 1;
99 if (dumplo < nblks - ctod(dumpsize) - 1)
100 dumplo = nblks - ctod(dumpsize) - 1;
101
102 for (block = 0; block < bootconfig.dramblocks; block++) {
103 cpu_kcore_hdr.ram_segs[block].start =
104 bootconfig.dram[block].address;
105 cpu_kcore_hdr.ram_segs[block].size =
106 ptoa(bootconfig.dram[block].pages);
107 }
108 }
109
110 /* This should be moved to machdep.c */
111
112 extern char *memhook; /* XXX */
113
114 /*
115 * Doadump comes here after turning off memory management and
116 * getting on the dump stack, either when called above, or by
117 * the auto-restart code.
118 */
119
120 void
dumpsys(void)121 dumpsys(void)
122 {
123 const struct bdevsw *bdev;
124 daddr_t blkno;
125 int psize;
126 int error;
127 int addr;
128 int block;
129 int len;
130 vaddr_t dumpspace;
131 kcore_seg_t *kseg_p;
132 cpu_kcore_hdr_t *chdr_p;
133 char dump_hdr[dbtob(1)]; /* assumes header fits in one block */
134
135 /* Save registers. */
136 savectx(&dumppcb);
137 /* flush everything out of caches */
138 cpu_dcache_wbinv_all();
139 cpu_sdcache_wbinv_all();
140
141 if (dumpdev == NODEV)
142 return;
143 if (dumpsize == 0) {
144 dumpconf();
145 if (dumpsize == 0)
146 return;
147 }
148 if (dumplo <= 0) {
149 printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
150 minor(dumpdev));
151 return;
152 }
153 printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
154 minor(dumpdev), dumplo);
155
156 blkno = dumplo;
157 dumpspace = (vaddr_t) memhook;
158
159 bdev = bdevsw_lookup(dumpdev);
160 if (bdev == NULL || bdev->d_psize == NULL)
161 return;
162 psize = (*bdev->d_psize)(dumpdev);
163 printf("dump ");
164 if (psize == -1) {
165 printf("area unavailable\n");
166 return;
167 }
168
169 /* Setup the dump header */
170 kseg_p = (kcore_seg_t *)dump_hdr;
171 chdr_p = (cpu_kcore_hdr_t *)&dump_hdr[ALIGN(sizeof(*kseg_p))];
172 bzero(dump_hdr, sizeof(dump_hdr));
173
174 CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
175 kseg_p->c_size = sizeof(dump_hdr) - ALIGN(sizeof(*kseg_p));
176 *chdr_p = cpu_kcore_hdr;
177
178 error = (*bdev->d_dump)(dumpdev, blkno++, (caddr_t)dump_hdr,
179 sizeof(dump_hdr));
180 if (error != 0)
181 goto abort;
182
183 len = 0;
184 for (block = 0; block < bootconfig.dramblocks && error == 0; ++block) {
185 addr = bootconfig.dram[block].address;
186 for (;addr < (bootconfig.dram[block].address
187 + (bootconfig.dram[block].pages * PAGE_SIZE));
188 addr += PAGE_SIZE) {
189 if ((len % (1024*1024)) == 0)
190 printf("%d ", len / (1024*1024));
191 pmap_kenter_pa(dumpspace, addr, PROT_READ);
192 pmap_update(pmap_kernel());
193
194 error = (*bdev->d_dump)(dumpdev,
195 blkno, (caddr_t) dumpspace, PAGE_SIZE);
196 pmap_kremove(dumpspace, PAGE_SIZE);
197 pmap_update(pmap_kernel());
198 if (error) break;
199 blkno += btodb(PAGE_SIZE);
200 len += PAGE_SIZE;
201 }
202 }
203
204 abort:
205 switch (error) {
206 case ENXIO:
207 printf("device bad\n");
208 break;
209
210 case EFAULT:
211 printf("device not ready\n");
212 break;
213
214 case EINVAL:
215 printf("area improper\n");
216 break;
217
218 case EIO:
219 printf("i/o error\n");
220 break;
221
222 case EINTR:
223 printf("aborted from console\n");
224 break;
225
226 default:
227 printf("succeeded\n");
228 break;
229 }
230 printf("\n\n");
231 delay(1000000);
232 }
233