xref: /netbsd-src/sys/kern/subr_debug.c (revision ed84275d914f0cf6d00973d97538da24eb40c530)
1*ed84275dSad /*	$NetBSD: subr_debug.c,v 1.7 2008/04/30 20:20:53 ad Exp $	*/
2b07ec3fcSad 
3b07ec3fcSad /*-
4*ed84275dSad  * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5b07ec3fcSad  * All rights reserved.
6b07ec3fcSad  *
7b07ec3fcSad  * This code is derived from software contributed to The NetBSD Foundation
8b07ec3fcSad  * by Andrew Doran.
9b07ec3fcSad  *
10b07ec3fcSad  * Redistribution and use in source and binary forms, with or without
11b07ec3fcSad  * modification, are permitted provided that the following conditions
12b07ec3fcSad  * are met:
13b07ec3fcSad  * 1. Redistributions of source code must retain the above copyright
14b07ec3fcSad  *    notice, this list of conditions and the following disclaimer.
15b07ec3fcSad  * 2. Redistributions in binary form must reproduce the above copyright
16b07ec3fcSad  *    notice, this list of conditions and the following disclaimer in the
17b07ec3fcSad  *    documentation and/or other materials provided with the distribution.
18b07ec3fcSad  *
19b07ec3fcSad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b07ec3fcSad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b07ec3fcSad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b07ec3fcSad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b07ec3fcSad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b07ec3fcSad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b07ec3fcSad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b07ec3fcSad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b07ec3fcSad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b07ec3fcSad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b07ec3fcSad  * POSSIBILITY OF SUCH DAMAGE.
30b07ec3fcSad  */
31b07ec3fcSad 
32b07ec3fcSad /*
33b07ec3fcSad  * Shared support code for kernels built with the DEBUG option.
34b07ec3fcSad  */
35b07ec3fcSad 
36b07ec3fcSad #include <sys/cdefs.h>
37*ed84275dSad __KERNEL_RCSID(0, "$NetBSD: subr_debug.c,v 1.7 2008/04/30 20:20:53 ad Exp $");
38b07ec3fcSad 
39b07ec3fcSad #include "opt_ddb.h"
40b07ec3fcSad 
41b07ec3fcSad #include <sys/param.h>
42b07ec3fcSad #include <sys/proc.h>
43b07ec3fcSad #include <sys/systm.h>
44b07ec3fcSad #include <sys/kmem.h>
45b07ec3fcSad #include <sys/debug.h>
46*ed84275dSad #include <sys/atomic.h>
47*ed84275dSad #include <sys/cpu.h>
48b07ec3fcSad 
49b07ec3fcSad #include <uvm/uvm_extern.h>
50b07ec3fcSad 
51b07ec3fcSad #include <machine/lock.h>
52b07ec3fcSad 
53b07ec3fcSad /*
54b07ec3fcSad  * Allocation/free validation by pointer address.  Introduces
55b07ec3fcSad  * significant overhead and is not enabled by default.  Patch
56b07ec3fcSad  * `debug_freecheck' to 1 at boot time to enable.
57b07ec3fcSad  */
58b07ec3fcSad #define	FREECHECK_BYTES		(8*1024*1024)
59b07ec3fcSad 
60b07ec3fcSad typedef struct fcitem {
61b07ec3fcSad 	void		*i_addr;
62b07ec3fcSad 	struct fcitem	*i_next;
63b07ec3fcSad } fcitem_t;
64b07ec3fcSad 
65b07ec3fcSad fcitem_t		*freecheck_free;
66b07ec3fcSad __cpu_simple_lock_t	freecheck_lock;
67*ed84275dSad u_int			debug_freecheck;
68b07ec3fcSad 
69b07ec3fcSad void
debug_init(void)70b07ec3fcSad debug_init(void)
71b07ec3fcSad {
72b07ec3fcSad 	size_t cnt;
73b07ec3fcSad 	fcitem_t *i;
74b07ec3fcSad 
75b07ec3fcSad 	__cpu_simple_lock_init(&freecheck_lock);
76b07ec3fcSad 
77b07ec3fcSad 	if (debug_freecheck) {
78b07ec3fcSad 		i = (fcitem_t *)uvm_km_alloc(kernel_map, FREECHECK_BYTES, 0,
79b07ec3fcSad 		    UVM_KMF_WIRED);
80b07ec3fcSad 		if (i == NULL) {
81b07ec3fcSad 			printf("freecheck_init: unable to allocate memory");
82b07ec3fcSad 			return;
83b07ec3fcSad 		}
84b07ec3fcSad 
85b07ec3fcSad 		for (cnt = FREECHECK_BYTES / sizeof(*i); cnt != 0; cnt--) {
86b07ec3fcSad 			i->i_next = freecheck_free;
87b07ec3fcSad 			freecheck_free = i++;
88b07ec3fcSad 		}
89b07ec3fcSad 	}
90b07ec3fcSad }
91b07ec3fcSad 
92b07ec3fcSad void
freecheck_out(void ** head,void * addr)93b07ec3fcSad freecheck_out(void **head, void *addr)
94b07ec3fcSad {
95b07ec3fcSad 	fcitem_t *i;
96b07ec3fcSad 	int s;
97b07ec3fcSad 
98b07ec3fcSad 	if (!debug_freecheck)
99b07ec3fcSad 		return;
100b07ec3fcSad 
101b07ec3fcSad 	s = splvm();
102b07ec3fcSad 	__cpu_simple_lock(&freecheck_lock);
10388ab7da9Sad 	for (i = *head; i != NULL; i = i->i_next) {
10488ab7da9Sad 		if (i->i_addr != addr)
10588ab7da9Sad 			continue;
10688ab7da9Sad 		__cpu_simple_unlock(&freecheck_lock);
10788ab7da9Sad 		splx(s);
10888ab7da9Sad 		panic("freecheck_out: %p already out", addr);
10988ab7da9Sad 	}
110b07ec3fcSad 	if ((i = freecheck_free) != NULL) {
111b07ec3fcSad 		freecheck_free = i->i_next;
112b07ec3fcSad 		i->i_addr = addr;
113b07ec3fcSad 		i->i_next = *head;
114b07ec3fcSad 		*head = i;
115b07ec3fcSad 	}
116b07ec3fcSad 	__cpu_simple_unlock(&freecheck_lock);
117b07ec3fcSad 	splx(s);
118b07ec3fcSad 
119b07ec3fcSad 	if (i == NULL) {
120*ed84275dSad 		if (atomic_swap_uint(&debug_freecheck, 1) == 0)
1211c679832Syamt 			printf("freecheck_out: no more slots\n");
122b07ec3fcSad 	}
123b07ec3fcSad }
124b07ec3fcSad 
125b07ec3fcSad void
freecheck_in(void ** head,void * addr)126b07ec3fcSad freecheck_in(void **head, void *addr)
127b07ec3fcSad {
128b07ec3fcSad 	fcitem_t *i;
129b07ec3fcSad 	void *pp;
130b07ec3fcSad 	int s;
131b07ec3fcSad 
132b07ec3fcSad 	if (!debug_freecheck)
133b07ec3fcSad 		return;
134b07ec3fcSad 
135b07ec3fcSad 	s = splvm();
136b07ec3fcSad 	__cpu_simple_lock(&freecheck_lock);
137b07ec3fcSad 	for (i = *head, pp = head; i != NULL; pp = &i->i_next, i = i->i_next) {
138b07ec3fcSad 		if (i->i_addr == addr) {
139b07ec3fcSad 			*(fcitem_t **)pp = i->i_next;
140b07ec3fcSad 			i->i_next = freecheck_free;
141b07ec3fcSad 			freecheck_free = i;
142b07ec3fcSad 			break;
143b07ec3fcSad 		}
144b07ec3fcSad 	}
145b07ec3fcSad 	__cpu_simple_unlock(&freecheck_lock);
146b07ec3fcSad 	splx(s);
147b07ec3fcSad 
148b07ec3fcSad 	if (i != NULL)
149b07ec3fcSad 		return;
150b07ec3fcSad 
151b07ec3fcSad #ifdef DDB
1521c679832Syamt 	printf("freecheck_in: %p not out\n", addr);
153b07ec3fcSad 	Debugger();
154b07ec3fcSad #else
155b07ec3fcSad 	panic("freecheck_in: %p not out", addr);
156b07ec3fcSad #endif
157b07ec3fcSad }
158