Home | History | Annotate | Line # | Download | only in kern
subr_debug.c revision 1.3.12.1
      1 /*	$NetBSD: subr_debug.c,v 1.3.12.1 2007/10/25 22:40:02 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      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 the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Shared support code for kernels built with the DEBUG option.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: subr_debug.c,v 1.3.12.1 2007/10/25 22:40:02 bouyer Exp $");
     45 
     46 #include "opt_ddb.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/proc.h>
     50 #include <sys/systm.h>
     51 #include <sys/kmem.h>
     52 #include <sys/debug.h>
     53 
     54 #include <uvm/uvm_extern.h>
     55 
     56 #include <machine/lock.h>
     57 #include <sys/cpu.h>
     58 
     59 /*
     60  * Allocation/free validation by pointer address.  Introduces
     61  * significant overhead and is not enabled by default.  Patch
     62  * `debug_freecheck' to 1 at boot time to enable.
     63  */
     64 #define	FREECHECK_BYTES		(8*1024*1024)
     65 
     66 typedef struct fcitem {
     67 	void		*i_addr;
     68 	struct fcitem	*i_next;
     69 } fcitem_t;
     70 
     71 fcitem_t		*freecheck_free;
     72 __cpu_simple_lock_t	freecheck_lock;
     73 int			debug_freecheck;
     74 
     75 void
     76 debug_init(void)
     77 {
     78 	size_t cnt;
     79 	fcitem_t *i;
     80 
     81 	__cpu_simple_lock_init(&freecheck_lock);
     82 
     83 	if (debug_freecheck) {
     84 		i = (fcitem_t *)uvm_km_alloc(kernel_map, FREECHECK_BYTES, 0,
     85 		    UVM_KMF_WIRED);
     86 		if (i == NULL) {
     87 			printf("freecheck_init: unable to allocate memory");
     88 			return;
     89 		}
     90 
     91 		for (cnt = FREECHECK_BYTES / sizeof(*i); cnt != 0; cnt--) {
     92 			i->i_next = freecheck_free;
     93 			freecheck_free = i++;
     94 		}
     95 	}
     96 }
     97 
     98 void
     99 freecheck_out(void **head, void *addr)
    100 {
    101 	fcitem_t *i;
    102 	int s;
    103 
    104 	if (!debug_freecheck)
    105 		return;
    106 
    107 	s = splvm();
    108 	__cpu_simple_lock(&freecheck_lock);
    109 	for (i = *head; i != NULL; i = i->i_next) {
    110 		if (i->i_addr != addr)
    111 			continue;
    112 		__cpu_simple_unlock(&freecheck_lock);
    113 		splx(s);
    114 		panic("freecheck_out: %p already out", addr);
    115 	}
    116 	if ((i = freecheck_free) != NULL) {
    117 		freecheck_free = i->i_next;
    118 		i->i_addr = addr;
    119 		i->i_next = *head;
    120 		*head = i;
    121 	}
    122 	__cpu_simple_unlock(&freecheck_lock);
    123 	splx(s);
    124 
    125 	if (i == NULL) {
    126 		printf("freecheck_out: no more slots");
    127 		debug_freecheck = 0;
    128 	}
    129 }
    130 
    131 void
    132 freecheck_in(void **head, void *addr)
    133 {
    134 	fcitem_t *i;
    135 	void *pp;
    136 	int s;
    137 
    138 	if (!debug_freecheck)
    139 		return;
    140 
    141 	s = splvm();
    142 	__cpu_simple_lock(&freecheck_lock);
    143 	for (i = *head, pp = head; i != NULL; pp = &i->i_next, i = i->i_next) {
    144 		if (i->i_addr == addr) {
    145 			*(fcitem_t **)pp = i->i_next;
    146 			i->i_next = freecheck_free;
    147 			freecheck_free = i;
    148 			break;
    149 		}
    150 	}
    151 	__cpu_simple_unlock(&freecheck_lock);
    152 	splx(s);
    153 
    154 	if (i != NULL)
    155 		return;
    156 
    157 #ifdef DDB
    158 	printf("freecheck_in: %p not out", addr);
    159 	Debugger();
    160 #else
    161 	panic("freecheck_in: %p not out", addr);
    162 #endif
    163 }
    164