1/*-
2 * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org>
3 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice unmodified, this list of conditions, and the following
11 *    disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * uma_dbg.c	Debugging features for UMA users
30 *
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36#include "opt_vm.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/bitset.h>
41#include <sys/kernel.h>
42#include <sys/types.h>
43#include <sys/queue.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/malloc.h>
47
48#include <vm/vm.h>
49#include <vm/vm_object.h>
50#include <vm/vm_page.h>
51#include <vm/uma.h>
52#include <vm/uma_int.h>
53#include <vm/uma_dbg.h>
54#include <vm/memguard.h>
55
56static const uint32_t uma_junk = 0xdeadc0de;
57
58/*
59 * Checks an item to make sure it hasn't been overwritten since it was freed,
60 * prior to subsequent reallocation.
61 *
62 * Complies with standard ctor arg/return
63 */
64int
65trash_ctor(void *mem, int size, void *arg, int flags)
66{
67	int cnt;
68	uint32_t *p;
69
70#ifdef DEBUG_MEMGUARD
71	if (is_memguard_addr(mem))
72		return (0);
73#endif
74
75	cnt = size / sizeof(uma_junk);
76
77	for (p = mem; cnt > 0; cnt--, p++)
78		if (*p != uma_junk) {
79#ifdef INVARIANTS
80			panic("Memory modified after free %p(%d) val=%x @ %p\n",
81			    mem, size, *p, p);
82#else
83			printf("Memory modified after free %p(%d) val=%x @ %p\n",
84			    mem, size, *p, p);
85#endif
86			return (0);
87		}
88	return (0);
89}
90
91/*
92 * Fills an item with predictable garbage
93 *
94 * Complies with standard dtor arg/return
95 *
96 */
97void
98trash_dtor(void *mem, int size, void *arg)
99{
100	int cnt;
101	uint32_t *p;
102
103#ifdef DEBUG_MEMGUARD
104	if (is_memguard_addr(mem))
105		return;
106#endif
107
108	cnt = size / sizeof(uma_junk);
109
110	for (p = mem; cnt > 0; cnt--, p++)
111		*p = uma_junk;
112}
113
114/*
115 * Fills an item with predictable garbage
116 *
117 * Complies with standard init arg/return
118 *
119 */
120int
121trash_init(void *mem, int size, int flags)
122{
123	trash_dtor(mem, size, NULL);
124	return (0);
125}
126
127/*
128 * Checks an item to make sure it hasn't been overwritten since it was freed.
129 *
130 * Complies with standard fini arg/return
131 *
132 */
133void
134trash_fini(void *mem, int size)
135{
136	(void)trash_ctor(mem, size, NULL, 0);
137}
138
139int
140mtrash_ctor(void *mem, int size, void *arg, int flags)
141{
142	struct malloc_type **ksp;
143	uint32_t *p = mem;
144	int cnt;
145
146#ifdef DEBUG_MEMGUARD
147	if (is_memguard_addr(mem))
148		return (0);
149#endif
150
151	size -= sizeof(struct malloc_type *);
152	ksp = (struct malloc_type **)mem;
153	ksp += size / sizeof(struct malloc_type *);
154	cnt = size / sizeof(uma_junk);
155
156	for (p = mem; cnt > 0; cnt--, p++)
157		if (*p != uma_junk) {
158			printf("Memory modified after free %p(%d) val=%x @ %p\n",
159			    mem, size, *p, p);
160			panic("Most recently used by %s\n", (*ksp == NULL)?
161			    "none" : (*ksp)->ks_shortdesc);
162		}
163	return (0);
164}
165
166/*
167 * Fills an item with predictable garbage
168 *
169 * Complies with standard dtor arg/return
170 *
171 */
172void
173mtrash_dtor(void *mem, int size, void *arg)
174{
175	int cnt;
176	uint32_t *p;
177
178#ifdef DEBUG_MEMGUARD
179	if (is_memguard_addr(mem))
180		return;
181#endif
182
183	size -= sizeof(struct malloc_type *);
184	cnt = size / sizeof(uma_junk);
185
186	for (p = mem; cnt > 0; cnt--, p++)
187		*p = uma_junk;
188}
189
190/*
191 * Fills an item with predictable garbage
192 *
193 * Complies with standard init arg/return
194 *
195 */
196int
197mtrash_init(void *mem, int size, int flags)
198{
199	struct malloc_type **ksp;
200
201#ifdef DEBUG_MEMGUARD
202	if (is_memguard_addr(mem))
203		return (0);
204#endif
205
206	mtrash_dtor(mem, size, NULL);
207
208	ksp = (struct malloc_type **)mem;
209	ksp += (size / sizeof(struct malloc_type *)) - 1;
210	*ksp = NULL;
211	return (0);
212}
213
214/*
215 * Checks an item to make sure it hasn't been overwritten since it was freed,
216 * prior to freeing it back to available memory.
217 *
218 * Complies with standard fini arg/return
219 *
220 */
221void
222mtrash_fini(void *mem, int size)
223{
224	(void)mtrash_ctor(mem, size, NULL, 0);
225}
226