No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / subr_debug.c
blob0062546ecdce4848b294bb32a7221d01eabd58f6
1 /* $NetBSD: subr_debug.c,v 1.6 2008/04/28 20:24:04 martin Exp $ */
3 /*-
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
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.
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.
33 * Shared support code for kernels built with the DEBUG option.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: subr_debug.c,v 1.6 2008/04/28 20:24:04 martin Exp $");
39 #include "opt_ddb.h"
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 #include <sys/atomic.h>
47 #include <sys/cpu.h>
49 #include <uvm/uvm_extern.h>
51 #include <machine/lock.h>
54 * Allocation/free validation by pointer address. Introduces
55 * significant overhead and is not enabled by default. Patch
56 * `debug_freecheck' to 1 at boot time to enable.
58 #define FREECHECK_BYTES (8*1024*1024)
60 typedef struct fcitem {
61 void *i_addr;
62 struct fcitem *i_next;
63 } fcitem_t;
65 fcitem_t *freecheck_free;
66 __cpu_simple_lock_t freecheck_lock;
67 u_int debug_freecheck;
69 void
70 debug_init(void)
72 size_t cnt;
73 fcitem_t *i;
75 __cpu_simple_lock_init(&freecheck_lock);
77 if (debug_freecheck) {
78 i = (fcitem_t *)uvm_km_alloc(kernel_map, FREECHECK_BYTES, 0,
79 UVM_KMF_WIRED);
80 if (i == NULL) {
81 printf("freecheck_init: unable to allocate memory");
82 return;
85 for (cnt = FREECHECK_BYTES / sizeof(*i); cnt != 0; cnt--) {
86 i->i_next = freecheck_free;
87 freecheck_free = i++;
92 void
93 freecheck_out(void **head, void *addr)
95 fcitem_t *i;
96 int s;
98 if (!debug_freecheck)
99 return;
101 s = splvm();
102 __cpu_simple_lock(&freecheck_lock);
103 for (i = *head; i != NULL; i = i->i_next) {
104 if (i->i_addr != addr)
105 continue;
106 __cpu_simple_unlock(&freecheck_lock);
107 splx(s);
108 panic("freecheck_out: %p already out", addr);
110 if ((i = freecheck_free) != NULL) {
111 freecheck_free = i->i_next;
112 i->i_addr = addr;
113 i->i_next = *head;
114 *head = i;
116 __cpu_simple_unlock(&freecheck_lock);
117 splx(s);
119 if (i == NULL) {
120 if (atomic_swap_uint(&debug_freecheck, 1) == 0)
121 printf("freecheck_out: no more slots\n");
125 void
126 freecheck_in(void **head, void *addr)
128 fcitem_t *i;
129 void *pp;
130 int s;
132 if (!debug_freecheck)
133 return;
135 s = splvm();
136 __cpu_simple_lock(&freecheck_lock);
137 for (i = *head, pp = head; i != NULL; pp = &i->i_next, i = i->i_next) {
138 if (i->i_addr == addr) {
139 *(fcitem_t **)pp = i->i_next;
140 i->i_next = freecheck_free;
141 freecheck_free = i;
142 break;
145 __cpu_simple_unlock(&freecheck_lock);
146 splx(s);
148 if (i != NULL)
149 return;
151 #ifdef DDB
152 printf("freecheck_in: %p not out\n", addr);
153 Debugger();
154 #else
155 panic("freecheck_in: %p not out", addr);
156 #endif