1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org> |
---|
5 | * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org> |
---|
6 | * All rights reserved. |
---|
7 | * |
---|
8 | * Redistribution and use in source and binary forms, with or without |
---|
9 | * modification, are permitted provided that the following conditions |
---|
10 | * are met: |
---|
11 | * 1. Redistributions of source code must retain the above copyright |
---|
12 | * notice unmodified, this list of conditions, and the following |
---|
13 | * disclaimer. |
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer in the |
---|
16 | * documentation and/or other materials provided with the distribution. |
---|
17 | * |
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | */ |
---|
29 | |
---|
30 | /* |
---|
31 | * uma_dbg.c Debugging features for UMA users |
---|
32 | * |
---|
33 | */ |
---|
34 | |
---|
35 | #include <sys/cdefs.h> |
---|
36 | __FBSDID("$FreeBSD$"); |
---|
37 | |
---|
38 | #include <rtems/bsd/sys/param.h> |
---|
39 | #include <sys/systm.h> |
---|
40 | #include <sys/kernel.h> |
---|
41 | #include <sys/types.h> |
---|
42 | #include <sys/queue.h> |
---|
43 | #include <rtems/bsd/sys/lock.h> |
---|
44 | #include <sys/mutex.h> |
---|
45 | #include <sys/malloc.h> |
---|
46 | |
---|
47 | #include <vm/vm.h> |
---|
48 | #include <vm/vm_object.h> |
---|
49 | #include <vm/vm_page.h> |
---|
50 | #include <vm/uma.h> |
---|
51 | #include <vm/uma_int.h> |
---|
52 | #include <vm/uma_dbg.h> |
---|
53 | |
---|
54 | static const u_int32_t uma_junk = 0xdeadc0de; |
---|
55 | |
---|
56 | /* |
---|
57 | * Checks an item to make sure it hasn't been overwritten since it was freed, |
---|
58 | * prior to subsequent reallocation. |
---|
59 | * |
---|
60 | * Complies with standard ctor arg/return |
---|
61 | * |
---|
62 | */ |
---|
63 | int |
---|
64 | trash_ctor(void *mem, int size, void *arg, int flags) |
---|
65 | { |
---|
66 | int cnt; |
---|
67 | u_int32_t *p; |
---|
68 | |
---|
69 | cnt = size / sizeof(uma_junk); |
---|
70 | |
---|
71 | for (p = mem; cnt > 0; cnt--, p++) |
---|
72 | if (*p != uma_junk) { |
---|
73 | printf("Memory modified after free %p(%d) val=%x @ %p\n", |
---|
74 | mem, size, *p, p); |
---|
75 | return (0); |
---|
76 | } |
---|
77 | return (0); |
---|
78 | } |
---|
79 | |
---|
80 | /* |
---|
81 | * Fills an item with predictable garbage |
---|
82 | * |
---|
83 | * Complies with standard dtor arg/return |
---|
84 | * |
---|
85 | */ |
---|
86 | void |
---|
87 | trash_dtor(void *mem, int size, void *arg) |
---|
88 | { |
---|
89 | int cnt; |
---|
90 | u_int32_t *p; |
---|
91 | |
---|
92 | cnt = size / sizeof(uma_junk); |
---|
93 | |
---|
94 | for (p = mem; cnt > 0; cnt--, p++) |
---|
95 | *p = uma_junk; |
---|
96 | } |
---|
97 | |
---|
98 | /* |
---|
99 | * Fills an item with predictable garbage |
---|
100 | * |
---|
101 | * Complies with standard init arg/return |
---|
102 | * |
---|
103 | */ |
---|
104 | int |
---|
105 | trash_init(void *mem, int size, int flags) |
---|
106 | { |
---|
107 | trash_dtor(mem, size, NULL); |
---|
108 | return (0); |
---|
109 | } |
---|
110 | |
---|
111 | /* |
---|
112 | * Checks an item to make sure it hasn't been overwritten since it was freed. |
---|
113 | * |
---|
114 | * Complies with standard fini arg/return |
---|
115 | * |
---|
116 | */ |
---|
117 | void |
---|
118 | trash_fini(void *mem, int size) |
---|
119 | { |
---|
120 | (void)trash_ctor(mem, size, NULL, 0); |
---|
121 | } |
---|
122 | |
---|
123 | int |
---|
124 | mtrash_ctor(void *mem, int size, void *arg, int flags) |
---|
125 | { |
---|
126 | struct malloc_type **ksp; |
---|
127 | u_int32_t *p = mem; |
---|
128 | int cnt; |
---|
129 | |
---|
130 | size -= sizeof(struct malloc_type *); |
---|
131 | ksp = (struct malloc_type **)mem; |
---|
132 | ksp += size / sizeof(struct malloc_type *); |
---|
133 | cnt = size / sizeof(uma_junk); |
---|
134 | |
---|
135 | for (p = mem; cnt > 0; cnt--, p++) |
---|
136 | if (*p != uma_junk) { |
---|
137 | printf("Memory modified after free %p(%d) val=%x @ %p\n", |
---|
138 | mem, size, *p, p); |
---|
139 | panic("Most recently used by %s\n", (*ksp == NULL)? |
---|
140 | "none" : (*ksp)->ks_shortdesc); |
---|
141 | } |
---|
142 | return (0); |
---|
143 | } |
---|
144 | |
---|
145 | /* |
---|
146 | * Fills an item with predictable garbage |
---|
147 | * |
---|
148 | * Complies with standard dtor arg/return |
---|
149 | * |
---|
150 | */ |
---|
151 | void |
---|
152 | mtrash_dtor(void *mem, int size, void *arg) |
---|
153 | { |
---|
154 | int cnt; |
---|
155 | u_int32_t *p; |
---|
156 | |
---|
157 | size -= sizeof(struct malloc_type *); |
---|
158 | cnt = size / sizeof(uma_junk); |
---|
159 | |
---|
160 | for (p = mem; cnt > 0; cnt--, p++) |
---|
161 | *p = uma_junk; |
---|
162 | } |
---|
163 | |
---|
164 | /* |
---|
165 | * Fills an item with predictable garbage |
---|
166 | * |
---|
167 | * Complies with standard init arg/return |
---|
168 | * |
---|
169 | */ |
---|
170 | int |
---|
171 | mtrash_init(void *mem, int size, int flags) |
---|
172 | { |
---|
173 | struct malloc_type **ksp; |
---|
174 | |
---|
175 | mtrash_dtor(mem, size, NULL); |
---|
176 | |
---|
177 | ksp = (struct malloc_type **)mem; |
---|
178 | ksp += (size / sizeof(struct malloc_type *)) - 1; |
---|
179 | *ksp = NULL; |
---|
180 | return (0); |
---|
181 | } |
---|
182 | |
---|
183 | /* |
---|
184 | * Checks an item to make sure it hasn't been overwritten since it was freed, |
---|
185 | * prior to freeing it back to available memory. |
---|
186 | * |
---|
187 | * Complies with standard fini arg/return |
---|
188 | * |
---|
189 | */ |
---|
190 | void |
---|
191 | mtrash_fini(void *mem, int size) |
---|
192 | { |
---|
193 | (void)mtrash_ctor(mem, size, NULL, 0); |
---|
194 | } |
---|
195 | |
---|
196 | static uma_slab_t |
---|
197 | uma_dbg_getslab(uma_zone_t zone, void *item) |
---|
198 | { |
---|
199 | uma_slab_t slab; |
---|
200 | uma_keg_t keg; |
---|
201 | u_int8_t *mem; |
---|
202 | |
---|
203 | mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK)); |
---|
204 | if (zone->uz_flags & UMA_ZONE_VTOSLAB) { |
---|
205 | slab = vtoslab((vm_offset_t)mem); |
---|
206 | } else { |
---|
207 | keg = LIST_FIRST(&zone->uz_kegs)->kl_keg; |
---|
208 | if (keg->uk_flags & UMA_ZONE_HASH) |
---|
209 | slab = hash_sfind(&keg->uk_hash, mem); |
---|
210 | else |
---|
211 | slab = (uma_slab_t)(mem + keg->uk_pgoff); |
---|
212 | } |
---|
213 | |
---|
214 | return (slab); |
---|
215 | } |
---|
216 | |
---|
217 | /* |
---|
218 | * Set up the slab's freei data such that uma_dbg_free can function. |
---|
219 | * |
---|
220 | */ |
---|
221 | |
---|
222 | void |
---|
223 | uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) |
---|
224 | { |
---|
225 | uma_keg_t keg; |
---|
226 | uma_slabrefcnt_t slabref; |
---|
227 | int freei; |
---|
228 | |
---|
229 | if (slab == NULL) { |
---|
230 | slab = uma_dbg_getslab(zone, item); |
---|
231 | if (slab == NULL) |
---|
232 | panic("uma: item %p did not belong to zone %s\n", |
---|
233 | item, zone->uz_name); |
---|
234 | } |
---|
235 | keg = slab->us_keg; |
---|
236 | |
---|
237 | freei = ((unsigned long)item - (unsigned long)slab->us_data) |
---|
238 | / keg->uk_rsize; |
---|
239 | |
---|
240 | if (keg->uk_flags & UMA_ZONE_REFCNT) { |
---|
241 | slabref = (uma_slabrefcnt_t)slab; |
---|
242 | slabref->us_freelist[freei].us_item = 255; |
---|
243 | } else { |
---|
244 | slab->us_freelist[freei].us_item = 255; |
---|
245 | } |
---|
246 | |
---|
247 | return; |
---|
248 | } |
---|
249 | |
---|
250 | /* |
---|
251 | * Verifies freed addresses. Checks for alignment, valid slab membership |
---|
252 | * and duplicate frees. |
---|
253 | * |
---|
254 | */ |
---|
255 | |
---|
256 | void |
---|
257 | uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) |
---|
258 | { |
---|
259 | uma_keg_t keg; |
---|
260 | uma_slabrefcnt_t slabref; |
---|
261 | int freei; |
---|
262 | |
---|
263 | if (slab == NULL) { |
---|
264 | slab = uma_dbg_getslab(zone, item); |
---|
265 | if (slab == NULL) |
---|
266 | panic("uma: Freed item %p did not belong to zone %s\n", |
---|
267 | item, zone->uz_name); |
---|
268 | } |
---|
269 | keg = slab->us_keg; |
---|
270 | |
---|
271 | freei = ((unsigned long)item - (unsigned long)slab->us_data) |
---|
272 | / keg->uk_rsize; |
---|
273 | |
---|
274 | if (freei >= keg->uk_ipers) |
---|
275 | panic("zone: %s(%p) slab %p freelist %d out of range 0-%d\n", |
---|
276 | zone->uz_name, zone, slab, freei, keg->uk_ipers-1); |
---|
277 | |
---|
278 | if (((freei * keg->uk_rsize) + slab->us_data) != item) { |
---|
279 | printf("zone: %s(%p) slab %p freed address %p unaligned.\n", |
---|
280 | zone->uz_name, zone, slab, item); |
---|
281 | panic("should be %p\n", |
---|
282 | (freei * keg->uk_rsize) + slab->us_data); |
---|
283 | } |
---|
284 | |
---|
285 | if (keg->uk_flags & UMA_ZONE_REFCNT) { |
---|
286 | slabref = (uma_slabrefcnt_t)slab; |
---|
287 | if (slabref->us_freelist[freei].us_item != 255) { |
---|
288 | printf("Slab at %p, freei %d = %d.\n", |
---|
289 | slab, freei, slabref->us_freelist[freei].us_item); |
---|
290 | panic("Duplicate free of item %p from zone %p(%s)\n", |
---|
291 | item, zone, zone->uz_name); |
---|
292 | } |
---|
293 | |
---|
294 | /* |
---|
295 | * When this is actually linked into the slab this will change. |
---|
296 | * Until then the count of valid slabs will make sure we don't |
---|
297 | * accidentally follow this and assume it's a valid index. |
---|
298 | */ |
---|
299 | slabref->us_freelist[freei].us_item = 0; |
---|
300 | } else { |
---|
301 | if (slab->us_freelist[freei].us_item != 255) { |
---|
302 | printf("Slab at %p, freei %d = %d.\n", |
---|
303 | slab, freei, slab->us_freelist[freei].us_item); |
---|
304 | panic("Duplicate free of item %p from zone %p(%s)\n", |
---|
305 | item, zone, zone->uz_name); |
---|
306 | } |
---|
307 | |
---|
308 | /* |
---|
309 | * When this is actually linked into the slab this will change. |
---|
310 | * Until then the count of valid slabs will make sure we don't |
---|
311 | * accidentally follow this and assume it's a valid index. |
---|
312 | */ |
---|
313 | slab->us_freelist[freei].us_item = 0; |
---|
314 | } |
---|
315 | } |
---|