xref: /netbsd-src/sys/arch/arm/arm32/stubs.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: stubs.c,v 1.24 2012/11/12 18:00:35 skrll Exp $	*/
2 
3 /*
4  * Copyright (c) 1994-1998 Mark Brinicombe.
5  * Copyright (c) 1994 Brini.
6  * All rights reserved.
7  *
8  * This code is derived from software written for Brini by Mark Brinicombe
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by Mark Brinicombe
21  *	for the NetBSD Project.
22  * 4. The name of the company nor the name of the author may be used to
23  *    endorse or promote products derived from this software without specific
24  *    prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * Routines that are temporary or do not have a home yet.
39  *
40  * Created      : 17/09/94
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: stubs.c,v 1.24 2012/11/12 18:00:35 skrll Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/proc.h>
50 #include <sys/conf.h>
51 #include <sys/msgbuf.h>
52 #include <uvm/uvm_extern.h>
53 #include <machine/cpu.h>
54 #include <machine/intr.h>
55 #include <machine/bootconfig.h>
56 #include <machine/pcb.h>
57 #include <arm/arm32/machdep.h>
58 #include <arm/kcore.h>
59 #include <sys/kcore.h>
60 #include <sys/core.h>
61 #include <sys/exec_aout.h>
62 
63 extern dev_t dumpdev;
64 
65 int	cpu_dump(void);
66 int	cpu_dumpsize(void);
67 u_long	cpu_dump_mempagecnt(void);
68 
69 /*
70  * These variables are needed by /sbin/savecore
71  */
72 uint32_t dumpmag = 0x8fca0101;	/* magic number */
73 int 	dumpsize = 0;		/* pages */
74 long	dumplo = 0; 		/* blocks */
75 
76 struct pcb dumppcb;
77 
78 /*
79  * This is called by main to set dumplo and dumpsize.
80  * Dumps always skip the first PAGE_SIZE of disk space
81  * in case there might be a disk label stored there.
82  * If there is extra space, put dump at the end to
83  * reduce the chance that swapping trashes it.
84  */
85 
86 void
87 cpu_dumpconf(void)
88 {
89 	int nblks, dumpblks;	/* size of dump area */
90 
91 	if (dumpdev == NODEV)
92 		return;
93 	nblks = bdev_size(dumpdev);
94 	if (nblks <= ctod(1))
95 		return;
96 
97 	dumpblks = cpu_dumpsize();
98 	if (dumpblks < 0)
99 		goto bad;
100 	dumpblks += ctod(cpu_dump_mempagecnt());
101 
102 	/* If dump won't fit (incl. room for possible label), punt. */
103 	if (dumpblks > (nblks - ctod(1)))
104 		goto bad;
105 
106 	/* Put dump at end of partition */
107 	dumplo = nblks - dumpblks;
108 
109 	/* dumpsize is in page units, and doesn't include headers. */
110 	dumpsize = cpu_dump_mempagecnt();
111 	return;
112 
113  bad:
114 	dumpsize = 0;
115 }
116 
117 /*
118  * cpu_dump: dump the machine-dependent kernel core dump headers.
119  */
120 int
121 cpu_dump(void)
122 {
123 	int (*dump)(dev_t, daddr_t, void *, size_t);
124 	char bf[dbtob(1)];
125 	kcore_seg_t *segp;
126 	cpu_kcore_hdr_t *cpuhdrp;
127 	phys_ram_seg_t *memsegp;
128 	const struct bdevsw *bdev;
129 	int i;
130 
131 	bdev = bdevsw_lookup(dumpdev);
132 	if (bdev == NULL)
133 		return (ENXIO);
134 	dump = bdev->d_dump;
135 
136 	memset(bf, 0, sizeof bf);
137 	segp = (kcore_seg_t *)bf;
138 	cpuhdrp = (cpu_kcore_hdr_t *)&bf[ALIGN(sizeof(*segp))];
139 	memsegp = (phys_ram_seg_t *)&bf[ ALIGN(sizeof(*segp)) +
140 	    ALIGN(sizeof(*cpuhdrp))];
141 
142 	/*
143 	 * Generate a segment header.
144 	 */
145 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
146 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
147 
148 	/*
149 	 * Add the machine-dependent header info.
150 	 */
151 	cpuhdrp->version = 1;
152 	cpuhdrp->PAKernelL1Table = pmap_kernel_L1_addr();
153 	cpuhdrp->UserL1TableSize = 0;
154 	cpuhdrp->nmemsegs = bootconfig.dramblocks;
155 	cpuhdrp->omemsegs = ALIGN(sizeof(*cpuhdrp));
156 
157 	/*
158 	 * Fill in the memory segment descriptors.
159 	 */
160 	for (i = 0; i < bootconfig.dramblocks; i++) {
161 		memsegp[i].start = bootconfig.dram[i].address;
162 		memsegp[i].size = bootconfig.dram[i].pages * PAGE_SIZE;
163 	}
164 
165 	return (dump(dumpdev, dumplo, bf, dbtob(1)));
166 }
167 
168 /*
169  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
170  */
171 int
172 cpu_dumpsize(void)
173 {
174 	int size;
175 
176 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
177 	    ALIGN( bootconfig.dramblocks * sizeof(phys_ram_seg_t));
178 	if (roundup(size, dbtob(1)) != dbtob(1))
179 		return (-1);
180 
181 	return (1);
182 }
183 
184 
185 /*
186  * cpu_dump_mempagecnt: calculate the size of RAM (in pages) to be dumped.
187  */
188 u_long
189 cpu_dump_mempagecnt(void)
190 {
191 	u_long i, n;
192 
193 	n = 0;
194 	for (i = 0; i < bootconfig.dramblocks; i++) {
195 		n += bootconfig.dram[i].pages;
196 	}
197 
198 	return (n);
199 }
200 
201 /* This should be moved to machdep.c */
202 
203 extern vaddr_t memhook;		/* XXX */
204 
205 /*
206  * Doadump comes here after turning off memory management and
207  * getting on the dump stack, either when called above, or by
208  * the auto-restart code.
209  */
210 void dodumpsys(void);
211 
212 void
213 dodumpsys(void)
214 {
215 	const struct bdevsw *bdev;
216 	daddr_t blkno;
217 	int psize;
218 	int error;
219 	int addr;
220 	int block;
221 	int len;
222 	vaddr_t dumpspace;
223 
224 	/* flush everything out of caches */
225 	cpu_dcache_wbinv_all();
226 
227 	if (dumpdev == NODEV)
228 		return;
229 	if (dumpsize == 0) {
230 		cpu_dumpconf();
231 	}
232 	if (dumplo <= 0 || dumpsize == 0) {
233 		printf("\ndump to dev %u,%u not possible\n",
234 		    major(dumpdev), minor(dumpdev));
235 		delay(5000000);
236 		return;
237 	}
238 	printf("\ndumping to dev %u,%u offset %ld\n",
239 	    major(dumpdev), minor(dumpdev), dumplo);
240 
241 
242 	bdev = bdevsw_lookup(dumpdev);
243 	if (bdev == NULL || bdev->d_psize == NULL)
244 		return;
245 	psize = bdev_size(dumpdev);
246 	printf("dump ");
247 	if (psize == -1) {
248 		printf("area unavailable\n");
249 		return;
250 	}
251 
252 	if ((error = cpu_dump()) != 0)
253 		goto err;
254 
255 	blkno = dumplo + cpu_dumpsize();
256 	dumpspace = memhook;
257 	error = 0;
258 	len = 0;
259 
260 	for (block = 0; block < bootconfig.dramblocks && error == 0; ++block) {
261 		addr = bootconfig.dram[block].address;
262 		for (;addr < (bootconfig.dram[block].address
263 		    + (bootconfig.dram[block].pages * PAGE_SIZE));
264 		     addr += PAGE_SIZE) {
265 		    	if ((len % (1024*1024)) == 0)
266 		    		printf("%d ", len / (1024*1024));
267 
268 			pmap_kenter_pa(dumpspace, addr, VM_PROT_READ, 0);
269 			pmap_update(pmap_kernel());
270 			error = (*bdev->d_dump)(dumpdev,
271 			    blkno, (void *) dumpspace, PAGE_SIZE);
272 
273 			if (error) goto err;
274 			blkno += btodb(PAGE_SIZE);
275 			len += PAGE_SIZE;
276 		}
277 	}
278 err:
279 	switch (error) {
280 	case ENXIO:
281 		printf("device bad\n");
282 		break;
283 
284 	case EFAULT:
285 		printf("device not ready\n");
286 		break;
287 
288 	case EINVAL:
289 		printf("area improper\n");
290 		break;
291 
292 	case EIO:
293 		printf("i/o error\n");
294 		break;
295 
296 	case EINTR:
297 		printf("aborted from console\n");
298 		break;
299 
300 	case 0:
301 		printf("succeeded\n");
302 		break;
303 
304 	default:
305 		printf("error %d\n", error);
306 		break;
307 	}
308 	printf("\n\n");
309 	delay(5000000);
310 }
311 
312 /* End of stubs.c */
313