1 /* $NetBSD: stubs.c,v 1.22 2009/11/07 07:27:41 cegger 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.22 2009/11/07 07:27:41 cegger 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 u_int32_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 const struct bdevsw *bdev; 90 int nblks, dumpblks; /* size of dump area */ 91 92 if (dumpdev == NODEV) 93 return; 94 bdev = bdevsw_lookup(dumpdev); 95 if (bdev == NULL) 96 panic("dumpconf: bad dumpdev=0x%"PRIx64"", dumpdev); 97 if (bdev->d_psize == NULL) 98 return; 99 nblks = (*bdev->d_psize)(dumpdev); 100 if (nblks <= ctod(1)) 101 return; 102 103 dumpblks = cpu_dumpsize(); 104 if (dumpblks < 0) 105 goto bad; 106 dumpblks += ctod(cpu_dump_mempagecnt()); 107 108 /* If dump won't fit (incl. room for possible label), punt. */ 109 if (dumpblks > (nblks - ctod(1))) 110 goto bad; 111 112 /* Put dump at end of partition */ 113 dumplo = nblks - dumpblks; 114 115 /* dumpsize is in page units, and doesn't include headers. */ 116 dumpsize = cpu_dump_mempagecnt(); 117 return; 118 119 bad: 120 dumpsize = 0; 121 } 122 123 /* 124 * cpu_dump: dump the machine-dependent kernel core dump headers. 125 */ 126 int 127 cpu_dump(void) 128 { 129 int (*dump)(dev_t, daddr_t, void *, size_t); 130 char bf[dbtob(1)]; 131 kcore_seg_t *segp; 132 cpu_kcore_hdr_t *cpuhdrp; 133 phys_ram_seg_t *memsegp; 134 const struct bdevsw *bdev; 135 int i; 136 137 bdev = bdevsw_lookup(dumpdev); 138 if (bdev == NULL) 139 return (ENXIO); 140 dump = bdev->d_dump; 141 142 memset(bf, 0, sizeof bf); 143 segp = (kcore_seg_t *)bf; 144 cpuhdrp = (cpu_kcore_hdr_t *)&bf[ALIGN(sizeof(*segp))]; 145 memsegp = (phys_ram_seg_t *)&bf[ ALIGN(sizeof(*segp)) + 146 ALIGN(sizeof(*cpuhdrp))]; 147 148 /* 149 * Generate a segment header. 150 */ 151 CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU); 152 segp->c_size = dbtob(1) - ALIGN(sizeof(*segp)); 153 154 /* 155 * Add the machine-dependent header info. 156 */ 157 cpuhdrp->version = 1; 158 cpuhdrp->PAKernelL1Table = pmap_kernel_L1_addr(); 159 cpuhdrp->UserL1TableSize = 0; 160 cpuhdrp->nmemsegs = bootconfig.dramblocks; 161 cpuhdrp->omemsegs = ALIGN(sizeof(*cpuhdrp)); 162 163 /* 164 * Fill in the memory segment descriptors. 165 */ 166 for (i = 0; i < bootconfig.dramblocks; i++) { 167 memsegp[i].start = bootconfig.dram[i].address; 168 memsegp[i].size = bootconfig.dram[i].pages * PAGE_SIZE; 169 } 170 171 return (dump(dumpdev, dumplo, bf, dbtob(1))); 172 } 173 174 /* 175 * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers. 176 */ 177 int 178 cpu_dumpsize(void) 179 { 180 int size; 181 182 size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) + 183 ALIGN( bootconfig.dramblocks * sizeof(phys_ram_seg_t)); 184 if (roundup(size, dbtob(1)) != dbtob(1)) 185 return (-1); 186 187 return (1); 188 } 189 190 191 /* 192 * cpu_dump_mempagecnt: calculate the size of RAM (in pages) to be dumped. 193 */ 194 u_long 195 cpu_dump_mempagecnt(void) 196 { 197 u_long i, n; 198 199 n = 0; 200 for (i = 0; i < bootconfig.dramblocks; i++) { 201 n += bootconfig.dram[i].pages; 202 } 203 204 return (n); 205 } 206 207 /* This should be moved to machdep.c */ 208 209 extern vaddr_t memhook; /* XXX */ 210 211 /* 212 * Doadump comes here after turning off memory management and 213 * getting on the dump stack, either when called above, or by 214 * the auto-restart code. 215 */ 216 void dodumpsys(void); 217 218 void 219 dodumpsys(void) 220 { 221 const struct bdevsw *bdev; 222 daddr_t blkno; 223 int psize; 224 int error; 225 int addr; 226 int block; 227 int len; 228 vaddr_t dumpspace; 229 230 /* flush everything out of caches */ 231 cpu_dcache_wbinv_all(); 232 233 if (dumpdev == NODEV) 234 return; 235 if (dumpsize == 0) { 236 cpu_dumpconf(); 237 } 238 if (dumplo <= 0 || dumpsize == 0) { 239 printf("\ndump to dev %u,%u not possible\n", 240 major(dumpdev), minor(dumpdev)); 241 delay(5000000); 242 return; 243 } 244 printf("\ndumping to dev %u,%u offset %ld\n", 245 major(dumpdev), minor(dumpdev), dumplo); 246 247 248 bdev = bdevsw_lookup(dumpdev); 249 if (bdev == NULL || bdev->d_psize == NULL) 250 return; 251 psize = (*bdev->d_psize)(dumpdev); 252 printf("dump "); 253 if (psize == -1) { 254 printf("area unavailable\n"); 255 return; 256 } 257 258 if ((error = cpu_dump()) != 0) 259 goto err; 260 261 blkno = dumplo + cpu_dumpsize(); 262 dumpspace = memhook; 263 error = 0; 264 len = 0; 265 266 for (block = 0; block < bootconfig.dramblocks && error == 0; ++block) { 267 addr = bootconfig.dram[block].address; 268 for (;addr < (bootconfig.dram[block].address 269 + (bootconfig.dram[block].pages * PAGE_SIZE)); 270 addr += PAGE_SIZE) { 271 if ((len % (1024*1024)) == 0) 272 printf("%d ", len / (1024*1024)); 273 274 pmap_kenter_pa(dumpspace, addr, VM_PROT_READ, 0); 275 pmap_update(pmap_kernel()); 276 error = (*bdev->d_dump)(dumpdev, 277 blkno, (void *) dumpspace, PAGE_SIZE); 278 279 if (error) goto err; 280 blkno += btodb(PAGE_SIZE); 281 len += PAGE_SIZE; 282 } 283 } 284 err: 285 switch (error) { 286 case ENXIO: 287 printf("device bad\n"); 288 break; 289 290 case EFAULT: 291 printf("device not ready\n"); 292 break; 293 294 case EINVAL: 295 printf("area improper\n"); 296 break; 297 298 case EIO: 299 printf("i/o error\n"); 300 break; 301 302 case EINTR: 303 printf("aborted from console\n"); 304 break; 305 306 case 0: 307 printf("succeeded\n"); 308 break; 309 310 default: 311 printf("error %d\n", error); 312 break; 313 } 314 printf("\n\n"); 315 delay(5000000); 316 } 317 318 /* End of stubs.c */ 319