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