Home | History | Annotate | Line # | Download | only in arm32
stubs.c revision 1.24.18.1
      1 /*	$NetBSD: stubs.c,v 1.24.18.1 2016/07/20 23:50:54 pgoyette 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.18.1 2016/07/20 23:50:54 pgoyette 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, err;
    130 
    131 	bdev = bdevsw_lookup_acquire(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 	err = (dump(dumpdev, dumplo, bf, dbtob(1)));
    166 	bdevsw_release(bdev);
    167 	return err;
    168 }
    169 
    170 /*
    171  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
    172  */
    173 int
    174 cpu_dumpsize(void)
    175 {
    176 	int size;
    177 
    178 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
    179 	    ALIGN( bootconfig.dramblocks * sizeof(phys_ram_seg_t));
    180 	if (roundup(size, dbtob(1)) != dbtob(1))
    181 		return (-1);
    182 
    183 	return (1);
    184 }
    185 
    186 
    187 /*
    188  * cpu_dump_mempagecnt: calculate the size of RAM (in pages) to be dumped.
    189  */
    190 u_long
    191 cpu_dump_mempagecnt(void)
    192 {
    193 	u_long i, n;
    194 
    195 	n = 0;
    196 	for (i = 0; i < bootconfig.dramblocks; i++) {
    197 		n += bootconfig.dram[i].pages;
    198 	}
    199 
    200 	return (n);
    201 }
    202 
    203 /* This should be moved to machdep.c */
    204 
    205 extern vaddr_t memhook;		/* XXX */
    206 
    207 /*
    208  * Doadump comes here after turning off memory management and
    209  * getting on the dump stack, either when called above, or by
    210  * the auto-restart code.
    211  */
    212 void dodumpsys(void);
    213 
    214 void
    215 dodumpsys(void)
    216 {
    217 	const struct bdevsw *bdev;
    218 	daddr_t blkno;
    219 	int psize;
    220 	int error;
    221 	int addr;
    222 	int block;
    223 	int len;
    224 	vaddr_t dumpspace;
    225 
    226 	/* flush everything out of caches */
    227 	cpu_dcache_wbinv_all();
    228 
    229 	if (dumpdev == NODEV)
    230 		return;
    231 	if (dumpsize == 0) {
    232 		cpu_dumpconf();
    233 	}
    234 	if (dumplo <= 0 || dumpsize == 0) {
    235 		printf("\ndump to dev %u,%u not possible\n",
    236 		    major(dumpdev), minor(dumpdev));
    237 		delay(5000000);
    238 		return;
    239 	}
    240 	printf("\ndumping to dev %u,%u offset %ld\n",
    241 	    major(dumpdev), minor(dumpdev), dumplo);
    242 
    243 	bdev = bdevsw_lookup_acquire(dumpdev);
    244 	if (bdev == NULL)
    245 		return;
    246 	if (bdev->d_psize == NULL) {
    247 		bdevsw_release(bdev);
    248 		return;
    249 	}
    250 	psize = bdev_size(dumpdev);
    251 	printf("dump ");
    252 	if (psize == -1) {
    253 		bdevsw_release(bdev);
    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 	bdevsw_release(bdev);
    286 	switch (error) {
    287 	case ENXIO:
    288 		printf("device bad\n");
    289 		break;
    290 
    291 	case EFAULT:
    292 		printf("device not ready\n");
    293 		break;
    294 
    295 	case EINVAL:
    296 		printf("area improper\n");
    297 		break;
    298 
    299 	case EIO:
    300 		printf("i/o error\n");
    301 		break;
    302 
    303 	case EINTR:
    304 		printf("aborted from console\n");
    305 		break;
    306 
    307 	case 0:
    308 		printf("succeeded\n");
    309 		break;
    310 
    311 	default:
    312 		printf("error %d\n", error);
    313 		break;
    314 	}
    315 	printf("\n\n");
    316 	delay(5000000);
    317 }
    318 
    319 /* End of stubs.c */
    320