blob: 62e17edae11dda39287b9484b15845d4b48576fe [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
26#include "pub_tool_basics.h" // Addr, SizeT
27#include "pub_tool_debuginfo.h" // VG_(get_objname)()
28#include "pub_tool_libcassert.h" // tl_assert()
29#include "pub_tool_libcbase.h" // VG_(memset)
30#include "pub_tool_libcprint.h" // VG_(printf)
31#include "pub_tool_machine.h" // VG_(get_IP)()
32#include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free)
33#include "pub_drd_bitmap.h"
34#include "drd_bitmap.h"
35#include "drd_error.h"
36#include "drd_suppression.h"
37
38
bartf647d342008-03-24 19:12:12 +000039/* Forward declarations. */
sewardjaf44c822007-11-25 14:01:38 +000040
bartf647d342008-03-24 19:12:12 +000041struct bitmap2;
sewardjaf44c822007-11-25 14:01:38 +000042
43
bartf647d342008-03-24 19:12:12 +000044/* Local function declarations. */
sewardjaf44c822007-11-25 14:01:38 +000045
46static void bm2_merge(struct bitmap2* const bm2l,
47 const struct bitmap2* const bm2r);
48
49
bartf647d342008-03-24 19:12:12 +000050/* Local constants. */
51
52static ULong s_bitmap_creation_count;
53
54
55/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000056
57struct bitmap* bm_new()
58{
bart33e56c92008-03-24 06:41:30 +000059 unsigned i;
bart3772a982008-03-15 08:11:03 +000060 struct bitmap* bm;
sewardjaf44c822007-11-25 14:01:38 +000061
bart3772a982008-03-15 08:11:03 +000062 // If this assert fails, fix the definition of BITS_PER_BITS_PER_UWORD
63 // in drd_bitmap.h.
64 tl_assert((1 << BITS_PER_BITS_PER_UWORD) == BITS_PER_UWORD);
sewardjaf44c822007-11-25 14:01:38 +000065
bart3772a982008-03-15 08:11:03 +000066 bm = VG_(malloc)(sizeof(*bm));
67 tl_assert(bm);
bart33e56c92008-03-24 06:41:30 +000068 for (i = 0; i < N_CACHE_ELEM; i++)
69 {
70 bm->cache[i].a1 = 0;
71 bm->cache[i].bm2 = 0;
72 }
bartf647d342008-03-24 19:12:12 +000073 bm->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free));
sewardjaf44c822007-11-25 14:01:38 +000074
bart3772a982008-03-15 08:11:03 +000075 s_bitmap_creation_count++;
sewardjaf44c822007-11-25 14:01:38 +000076
bart3772a982008-03-15 08:11:03 +000077 return bm;
sewardjaf44c822007-11-25 14:01:38 +000078}
79
80void bm_delete(struct bitmap* const bm)
81{
bartf647d342008-03-24 19:12:12 +000082 struct bitmap2* bm2;
83 struct bitmap2ref* bm2ref;
84
bart3772a982008-03-15 08:11:03 +000085 tl_assert(bm);
bartf647d342008-03-24 19:12:12 +000086
87 VG_(OSetGen_ResetIter)(bm->oset);
88 for ( ; (bm2ref = VG_(OSetGen_Next)(bm->oset)) != 0; )
89 {
90 bm2 = bm2ref->bm2;
91 tl_assert(bm2->refcnt >= 1);
92 if (--bm2->refcnt == 0)
93 {
94 VG_(free)(bm2);
95 }
96 }
97
bart3772a982008-03-15 08:11:03 +000098 VG_(OSetGen_Destroy)(bm->oset);
99 VG_(free)(bm);
sewardjaf44c822007-11-25 14:01:38 +0000100}
101
102/**
bart36556122008-03-13 19:24:30 +0000103 * Record an access of type access_type at addresses a .. a + size - 1 in
sewardjaf44c822007-11-25 14:01:38 +0000104 * bitmap bm.
105 */
barta79df6e2008-03-14 17:07:51 +0000106static
sewardjaf44c822007-11-25 14:01:38 +0000107void bm_access_range(struct bitmap* const bm,
bart9036dea2008-03-13 19:10:06 +0000108 const Addr a1, const Addr a2,
bart0268dfa2008-03-11 20:10:21 +0000109 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +0000110{
bart3772a982008-03-15 08:11:03 +0000111 Addr b, b_next;
bart36556122008-03-13 19:24:30 +0000112
bart3772a982008-03-15 08:11:03 +0000113 tl_assert(bm);
114 tl_assert(a1 < a2);
sewardjaf44c822007-11-25 14:01:38 +0000115
bart3772a982008-03-15 08:11:03 +0000116 for (b = a1; b < a2; b = b_next)
117 {
118 Addr b_start;
119 Addr b_end;
120 struct bitmap2* bm2;
121 SPLIT_ADDRESS(b);
bart36556122008-03-13 19:24:30 +0000122
bart3772a982008-03-15 08:11:03 +0000123 b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT;
124 if (b_next > a2)
125 {
126 b_next = a2;
127 }
bart36556122008-03-13 19:24:30 +0000128
bartf647d342008-03-24 19:12:12 +0000129 bm2 = bm2_lookup_or_insert_exclusive(bm, b1);
bart3772a982008-03-15 08:11:03 +0000130 tl_assert(bm2);
bart36556122008-03-13 19:24:30 +0000131
bart3772a982008-03-15 08:11:03 +0000132 if ((bm2->addr << ADDR0_BITS) < a1)
133 b_start = a1;
134 else
135 if ((bm2->addr << ADDR0_BITS) < a2)
136 b_start = (bm2->addr << ADDR0_BITS);
bart36556122008-03-13 19:24:30 +0000137 else
bart3772a982008-03-15 08:11:03 +0000138 break;
139 tl_assert(a1 <= b_start && b_start <= a2);
bart36556122008-03-13 19:24:30 +0000140
bart3772a982008-03-15 08:11:03 +0000141 if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2)
142 b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT;
143 else
144 b_end = a2;
145 tl_assert(a1 <= b_end && b_end <= a2);
146 tl_assert(b_start < b_end);
147 tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK));
bart36556122008-03-13 19:24:30 +0000148
bart0008f5b2008-03-22 17:07:39 +0000149 if (access_type == eLoad)
bart3772a982008-03-15 08:11:03 +0000150 {
bart0008f5b2008-03-22 17:07:39 +0000151 for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end - 1) & ADDR0_MASK); b0++)
bart36556122008-03-13 19:24:30 +0000152 {
bart3772a982008-03-15 08:11:03 +0000153 bm0_set(bm2->bm1.bm0_r, b0);
sewardjaf44c822007-11-25 14:01:38 +0000154 }
bart0008f5b2008-03-22 17:07:39 +0000155 }
156 else
157 {
158 for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end - 1) & ADDR0_MASK); b0++)
bart3772a982008-03-15 08:11:03 +0000159 {
160 bm0_set(bm2->bm1.bm0_w, b0);
161 }
162 }
163 }
sewardjaf44c822007-11-25 14:01:38 +0000164}
165
barta79df6e2008-03-14 17:07:51 +0000166static inline
167void bm_access_aligned_load(struct bitmap* const bm,
bartf8bc71d2008-03-15 11:42:34 +0000168 const Addr a1, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000169{
bart3772a982008-03-15 08:11:03 +0000170 struct bitmap2* bm2;
barta79df6e2008-03-14 17:07:51 +0000171
bartf647d342008-03-24 19:12:12 +0000172 bm2 = bm2_lookup_or_insert_exclusive(bm, a1 >> ADDR0_BITS);
bartf8bc71d2008-03-15 11:42:34 +0000173 bm0_set_range(bm2->bm1.bm0_r, a1 & ADDR0_MASK, size);
barta79df6e2008-03-14 17:07:51 +0000174}
175
176static inline
177void bm_access_aligned_store(struct bitmap* const bm,
bartf8bc71d2008-03-15 11:42:34 +0000178 const Addr a1, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000179{
bart3772a982008-03-15 08:11:03 +0000180 struct bitmap2* bm2;
barta79df6e2008-03-14 17:07:51 +0000181
bartf647d342008-03-24 19:12:12 +0000182 bm2 = bm2_lookup_or_insert_exclusive(bm, a1 >> ADDR0_BITS);
bartf8bc71d2008-03-15 11:42:34 +0000183 bm0_set_range(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size);
barta79df6e2008-03-14 17:07:51 +0000184}
185
bart36556122008-03-13 19:24:30 +0000186void bm_access_range_load(struct bitmap* const bm,
187 const Addr a1, const Addr a2)
188{
bart3772a982008-03-15 08:11:03 +0000189 bm_access_range(bm, a1, a2, eLoad);
bart36556122008-03-13 19:24:30 +0000190}
191
barta79df6e2008-03-14 17:07:51 +0000192void bm_access_load_1(struct bitmap* const bm, const Addr a1)
193{
bartf8bc71d2008-03-15 11:42:34 +0000194 bm_access_aligned_load(bm, a1, 1);
barta79df6e2008-03-14 17:07:51 +0000195}
196
197void bm_access_load_2(struct bitmap* const bm, const Addr a1)
198{
bart3772a982008-03-15 08:11:03 +0000199 if ((a1 & 1) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000200 bm_access_aligned_load(bm, a1, 2);
bart3772a982008-03-15 08:11:03 +0000201 else
202 bm_access_range(bm, a1, a1 + 2, eLoad);
barta79df6e2008-03-14 17:07:51 +0000203}
204
205void bm_access_load_4(struct bitmap* const bm, const Addr a1)
206{
bart3772a982008-03-15 08:11:03 +0000207 if ((a1 & 3) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000208 bm_access_aligned_load(bm, a1, 4);
bart3772a982008-03-15 08:11:03 +0000209 else
210 bm_access_range(bm, a1, a1 + 4, eLoad);
barta79df6e2008-03-14 17:07:51 +0000211}
212
213void bm_access_load_8(struct bitmap* const bm, const Addr a1)
214{
bart3772a982008-03-15 08:11:03 +0000215 if ((a1 & 7) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000216 bm_access_aligned_load(bm, a1, 8);
bart3772a982008-03-15 08:11:03 +0000217 else if ((a1 & 3) == 0)
218 {
bartf8bc71d2008-03-15 11:42:34 +0000219 bm_access_aligned_load(bm, a1 + 0, 4);
220 bm_access_aligned_load(bm, a1 + 4, 4);
bart3772a982008-03-15 08:11:03 +0000221 }
222 else
223 bm_access_range(bm, a1, a1 + 8, eLoad);
barta79df6e2008-03-14 17:07:51 +0000224}
225
226void bm_access_store_1(struct bitmap* const bm, const Addr a1)
227{
bartf8bc71d2008-03-15 11:42:34 +0000228 bm_access_aligned_store(bm, a1, 1);
barta79df6e2008-03-14 17:07:51 +0000229}
230
231void bm_access_store_2(struct bitmap* const bm, const Addr a1)
232{
bart3772a982008-03-15 08:11:03 +0000233 if ((a1 & 1) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000234 bm_access_aligned_store(bm, a1, 2);
bart3772a982008-03-15 08:11:03 +0000235 else
236 bm_access_range(bm, a1, a1 + 2, eStore);
barta79df6e2008-03-14 17:07:51 +0000237}
238
239void bm_access_store_4(struct bitmap* const bm, const Addr a1)
240{
bart3772a982008-03-15 08:11:03 +0000241 if ((a1 & 3) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000242 bm_access_aligned_store(bm, a1, 4);
bart3772a982008-03-15 08:11:03 +0000243 else
244 bm_access_range(bm, a1, a1 + 4, eStore);
barta79df6e2008-03-14 17:07:51 +0000245}
246
247void bm_access_store_8(struct bitmap* const bm, const Addr a1)
248{
bart3772a982008-03-15 08:11:03 +0000249 if ((a1 & 7) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000250 bm_access_aligned_store(bm, a1, 8);
bart3772a982008-03-15 08:11:03 +0000251 else if ((a1 & 3) == 0)
252 {
bartf8bc71d2008-03-15 11:42:34 +0000253 bm_access_aligned_store(bm, a1 + 0, 4);
254 bm_access_aligned_store(bm, a1 + 4, 4);
bart3772a982008-03-15 08:11:03 +0000255 }
256 else
257 bm_access_range(bm, a1, a1 + 8, eStore);
barta79df6e2008-03-14 17:07:51 +0000258}
259
bart36556122008-03-13 19:24:30 +0000260void bm_access_range_store(struct bitmap* const bm,
261 const Addr a1, const Addr a2)
262{
bart3772a982008-03-15 08:11:03 +0000263 bm_access_range(bm, a1, a2, eStore);
bart36556122008-03-13 19:24:30 +0000264}
265
266Bool bm_has(const struct bitmap* const bm, const Addr a1, const Addr a2,
sewardjaf44c822007-11-25 14:01:38 +0000267 const BmAccessTypeT access_type)
268{
bart3772a982008-03-15 08:11:03 +0000269 Addr b;
270 for (b = a1; b < a2; b++)
271 {
272 if (! bm_has_1(bm, b, access_type))
273 {
274 return False;
275 }
276 }
277 return True;
sewardjaf44c822007-11-25 14:01:38 +0000278}
279
280Bool bm_has_any(const struct bitmap* const bm,
bart36556122008-03-13 19:24:30 +0000281 const Addr a1, const Addr a2,
sewardjaf44c822007-11-25 14:01:38 +0000282 const BmAccessTypeT access_type)
283{
bart3772a982008-03-15 08:11:03 +0000284 Addr b;
sewardjaf44c822007-11-25 14:01:38 +0000285
bart3772a982008-03-15 08:11:03 +0000286 tl_assert(bm);
sewardjaf44c822007-11-25 14:01:38 +0000287
bart3772a982008-03-15 08:11:03 +0000288 for (b = a1; b < a2; b++)
289 {
290 if (bm_has_1(bm, b, access_type))
291 {
292 return True;
293 }
294 }
295 return False;
sewardjaf44c822007-11-25 14:01:38 +0000296}
297
298/* Return a non-zero value if there is a read access, write access or both */
299/* to any of the addresses in the range [ a1, a2 [ in bitmap bm. */
300UWord bm_has_any_access(const struct bitmap* const bm,
301 const Addr a1,
302 const Addr a2)
303{
bart3772a982008-03-15 08:11:03 +0000304 Addr b, b_next;
sewardjaf44c822007-11-25 14:01:38 +0000305
bart3772a982008-03-15 08:11:03 +0000306 tl_assert(bm);
sewardjaf44c822007-11-25 14:01:38 +0000307
bart3772a982008-03-15 08:11:03 +0000308 for (b = a1; b < a2; b = b_next)
309 {
bartf647d342008-03-24 19:12:12 +0000310 const struct bitmap2* bm2 = bm2_lookup(bm, b >> ADDR0_BITS);
sewardjaf44c822007-11-25 14:01:38 +0000311
bart3772a982008-03-15 08:11:03 +0000312 b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT;
313 if (b_next > a2)
314 {
315 b_next = a2;
316 }
sewardjaf44c822007-11-25 14:01:38 +0000317
bart3772a982008-03-15 08:11:03 +0000318 if (bm2)
319 {
320 Addr b_start;
321 Addr b_end;
322 UWord b0;
323 const struct bitmap1* const p1 = &bm2->bm1;
sewardjaf44c822007-11-25 14:01:38 +0000324
bart3772a982008-03-15 08:11:03 +0000325 if ((bm2->addr << ADDR0_BITS) < a1)
326 b_start = a1;
327 else
328 if ((bm2->addr << ADDR0_BITS) < a2)
329 b_start = (bm2->addr << ADDR0_BITS);
330 else
331 break;
332 tl_assert(a1 <= b_start && b_start <= a2);
sewardjaf44c822007-11-25 14:01:38 +0000333
bart3772a982008-03-15 08:11:03 +0000334 if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2)
335 b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT;
336 else
337 b_end = a2;
338 tl_assert(a1 <= b_end && b_end <= a2);
339 tl_assert(b_start < b_end);
340 tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK));
sewardjaf44c822007-11-25 14:01:38 +0000341
bart3772a982008-03-15 08:11:03 +0000342 for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end-1) & ADDR0_MASK); b0++)
343 {
344 const UWord mask
345 = bm0_is_set(p1->bm0_r, b0) | bm0_is_set(p1->bm0_w, b0);
346 if (mask)
347 {
348 return mask;
349 }
sewardjaf44c822007-11-25 14:01:38 +0000350 }
bart3772a982008-03-15 08:11:03 +0000351 }
352 }
353 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000354}
355
356/**
357 * Report whether an access of type access_type at address a is recorded in
358 * bitmap bm.
359 * @return != 0 means true, and == 0 means false
360 */
361UWord bm_has_1(const struct bitmap* const bm,
362 const Addr a,
363 const BmAccessTypeT access_type)
364{
bartf647d342008-03-24 19:12:12 +0000365 const struct bitmap2* p2;
366 const struct bitmap1* p1;
367 const UWord* p0;
bart3772a982008-03-15 08:11:03 +0000368 const UWord a0 = a & ADDR0_MASK;
sewardjaf44c822007-11-25 14:01:38 +0000369
bart3772a982008-03-15 08:11:03 +0000370 tl_assert(bm);
sewardjaf44c822007-11-25 14:01:38 +0000371
bart11d0b502008-03-22 16:44:03 +0000372 p2 = bm2_lookup(bm, a >> ADDR0_BITS);
bart3772a982008-03-15 08:11:03 +0000373 if (p2)
374 {
375 p1 = &p2->bm1;
376 p0 = (access_type == eLoad) ? p1->bm0_r : p1->bm0_w;
377 return bm0_is_set(p0, a0);
378 }
379 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000380}
381
382static __inline__
383void bm1_clear(struct bitmap1* const bm1, const Addr a1, const Addr a2)
384{
bart3772a982008-03-15 08:11:03 +0000385 UWord idx;
386 UWord mask;
sewardjaf44c822007-11-25 14:01:38 +0000387
388#if 0
bart3772a982008-03-15 08:11:03 +0000389 /* Commented out the statements below because of performance reasons. */
390 tl_assert(a1);
391 tl_assert(a1 <= a2);
392 tl_assert(UWORD_MSB(a1) == UWORD_MSB(a2)
393 || UWORD_MSB(a1) == UWORD_MSB(a2 - 1));
sewardjaf44c822007-11-25 14:01:38 +0000394#endif
395
bart3772a982008-03-15 08:11:03 +0000396 idx = (a1 & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD;
397 /* mask: a contiguous series of one bits. The first bit set is bit */
398 /* UWORD_LSB(a2-1), and the last bit set is UWORD_LSB(a1). */
399 mask = UWORD_LSB(a2) ? bm0_mask(a2) - bm0_mask(a1) : - bm0_mask(a1);
400 bm1->bm0_r[idx] &= ~mask;
401 bm1->bm0_w[idx] &= ~mask;
sewardjaf44c822007-11-25 14:01:38 +0000402}
403
sewardjaf44c822007-11-25 14:01:38 +0000404void bm_clear(const struct bitmap* const bm,
405 const Addr a1,
406 const Addr a2)
407{
bart3772a982008-03-15 08:11:03 +0000408 Addr b, b_next;
sewardjaf44c822007-11-25 14:01:38 +0000409
bart3772a982008-03-15 08:11:03 +0000410 tl_assert(bm);
411 tl_assert(a1);
412 tl_assert(a1 <= a2);
sewardjaf44c822007-11-25 14:01:38 +0000413
bart3772a982008-03-15 08:11:03 +0000414 for (b = a1; b < a2; b = b_next)
415 {
bartf647d342008-03-24 19:12:12 +0000416 struct bitmap2* const p2 = bm2_lookup_exclusive(bm, b >> ADDR0_BITS);
sewardjaf44c822007-11-25 14:01:38 +0000417
bart3772a982008-03-15 08:11:03 +0000418 b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT;
419 if (b_next > a2)
420 {
421 b_next = a2;
422 }
423
424 if (p2)
425 {
426 Addr c = b;
bartf647d342008-03-24 19:12:12 +0000427 /* If the first address in the bitmap that must be cleared does not */
428 /* start on an UWord boundary, start clearing the first addresses */
429 /* by calling bm1_clear(). */
bart3772a982008-03-15 08:11:03 +0000430 if (UWORD_LSB(c))
sewardjaf44c822007-11-25 14:01:38 +0000431 {
bart3772a982008-03-15 08:11:03 +0000432 Addr c_next = UWORD_MSB(c) + BITS_PER_UWORD;
433 if (c_next > b_next)
434 c_next = b_next;
435 bm1_clear(&p2->bm1, c, c_next);
436 c = c_next;
sewardjaf44c822007-11-25 14:01:38 +0000437 }
bartf647d342008-03-24 19:12:12 +0000438 /* If some UWords have to be cleared entirely, do this now. */
bart3772a982008-03-15 08:11:03 +0000439 if (UWORD_LSB(c) == 0)
sewardjaf44c822007-11-25 14:01:38 +0000440 {
bart3772a982008-03-15 08:11:03 +0000441 const Addr c_next = UWORD_MSB(b_next);
442 tl_assert(UWORD_LSB(c) == 0);
443 tl_assert(UWORD_LSB(c_next) == 0);
444 tl_assert(c_next <= b_next);
445 tl_assert(c <= c_next);
446 if (c_next > c)
447 {
448 UWord idx = (c & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD;
449 VG_(memset)(&p2->bm1.bm0_r[idx], 0, (c_next - c) / 8);
450 VG_(memset)(&p2->bm1.bm0_w[idx], 0, (c_next - c) / 8);
451 c = c_next;
452 }
sewardjaf44c822007-11-25 14:01:38 +0000453 }
bartf647d342008-03-24 19:12:12 +0000454 /* If the last address in the bitmap that must be cleared does not */
455 /* fall on an UWord boundary, clear the last addresses by calling */
456 /* bm1_clear(). */
bart3772a982008-03-15 08:11:03 +0000457 if (c != b_next)
458 {
459 bm1_clear(&p2->bm1, c, b_next);
460 }
461 }
462 }
sewardjaf44c822007-11-25 14:01:38 +0000463}
sewardjaf44c822007-11-25 14:01:38 +0000464
bart36556122008-03-13 19:24:30 +0000465Bool bm_has_conflict_with(const struct bitmap* const bm,
466 const Addr a1, const Addr a2,
467 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +0000468{
bart3772a982008-03-15 08:11:03 +0000469 Addr b, b_next;
sewardjaf44c822007-11-25 14:01:38 +0000470
bart3772a982008-03-15 08:11:03 +0000471 tl_assert(bm);
sewardjaf44c822007-11-25 14:01:38 +0000472
bart3772a982008-03-15 08:11:03 +0000473 for (b = a1; b < a2; b = b_next)
474 {
bartf647d342008-03-24 19:12:12 +0000475 const struct bitmap2* bm2 = bm2_lookup(bm, b >> ADDR0_BITS);
bart36556122008-03-13 19:24:30 +0000476
bart3772a982008-03-15 08:11:03 +0000477 b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT;
478 if (b_next > a2)
479 {
480 b_next = a2;
481 }
bart36556122008-03-13 19:24:30 +0000482
bart3772a982008-03-15 08:11:03 +0000483 if (bm2)
484 {
485 Addr b_start;
486 Addr b_end;
487 UWord b0;
488 const struct bitmap1* const p1 = &bm2->bm1;
bart36556122008-03-13 19:24:30 +0000489
bart3772a982008-03-15 08:11:03 +0000490 if ((bm2->addr << ADDR0_BITS) < a1)
491 b_start = a1;
492 else
493 if ((bm2->addr << ADDR0_BITS) < a2)
494 b_start = (bm2->addr << ADDR0_BITS);
495 else
496 break;
497 tl_assert(a1 <= b_start && b_start <= a2);
bart36556122008-03-13 19:24:30 +0000498
bart3772a982008-03-15 08:11:03 +0000499 if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2)
500 b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT;
501 else
502 b_end = a2;
503 tl_assert(a1 <= b_end && b_end <= a2);
504 tl_assert(b_start < b_end);
505 tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK));
bart36556122008-03-13 19:24:30 +0000506
bart3772a982008-03-15 08:11:03 +0000507 for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end-1) & ADDR0_MASK); b0++)
508 {
509 if (access_type == eLoad)
510 {
511 if (bm0_is_set(p1->bm0_w, b0))
512 {
513 return True;
514 }
515 }
516 else
517 {
518 tl_assert(access_type == eStore);
519 if (bm0_is_set(p1->bm0_r, b0)
520 | bm0_is_set(p1->bm0_w, b0))
521 {
522 return True;
523 }
524 }
sewardjaf44c822007-11-25 14:01:38 +0000525 }
bart3772a982008-03-15 08:11:03 +0000526 }
527 }
528 return False;
sewardjaf44c822007-11-25 14:01:38 +0000529}
530
barta79df6e2008-03-14 17:07:51 +0000531static inline
532Bool bm_aligned_load_has_conflict_with(const struct bitmap* const bm,
bartf8bc71d2008-03-15 11:42:34 +0000533 const Addr a1, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000534{
bartf647d342008-03-24 19:12:12 +0000535 const struct bitmap2* bm2;
barta79df6e2008-03-14 17:07:51 +0000536
bart11d0b502008-03-22 16:44:03 +0000537 bm2 = bm2_lookup(bm, a1 >> ADDR0_BITS);
barta79df6e2008-03-14 17:07:51 +0000538
bartf8bc71d2008-03-15 11:42:34 +0000539 return (bm2 && bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size));
barta79df6e2008-03-14 17:07:51 +0000540}
541
542static inline
543Bool bm_aligned_store_has_conflict_with(const struct bitmap* const bm,
bartf8bc71d2008-03-15 11:42:34 +0000544 const Addr a1, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000545{
bartf647d342008-03-24 19:12:12 +0000546 const struct bitmap2* bm2;
barta79df6e2008-03-14 17:07:51 +0000547
bart11d0b502008-03-22 16:44:03 +0000548 bm2 = bm2_lookup(bm, a1 >> ADDR0_BITS);
barta79df6e2008-03-14 17:07:51 +0000549
bart3772a982008-03-15 08:11:03 +0000550 if (bm2)
551 {
bartf8bc71d2008-03-15 11:42:34 +0000552 if (bm0_is_any_set(bm2->bm1.bm0_r, a1 & ADDR0_MASK, size)
553 | bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size))
bart3772a982008-03-15 08:11:03 +0000554 {
555 return True;
556 }
557 }
558 return False;
barta79df6e2008-03-14 17:07:51 +0000559}
560
bart36556122008-03-13 19:24:30 +0000561Bool bm_load_has_conflict_with(const struct bitmap* const bm,
562 const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +0000563{
bart3772a982008-03-15 08:11:03 +0000564 return bm_has_conflict_with(bm, a1, a2, eLoad);
bart36556122008-03-13 19:24:30 +0000565}
566
barta79df6e2008-03-14 17:07:51 +0000567Bool bm_load_1_has_conflict_with(const struct bitmap* const bm, const Addr a1)
568{
bartf8bc71d2008-03-15 11:42:34 +0000569 return bm_aligned_load_has_conflict_with(bm, a1, 1);
barta79df6e2008-03-14 17:07:51 +0000570}
571
572Bool bm_load_2_has_conflict_with(const struct bitmap* const bm, const Addr a1)
573{
bart3772a982008-03-15 08:11:03 +0000574 if ((a1 & 1) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000575 return bm_aligned_load_has_conflict_with(bm, a1, 2);
bart3772a982008-03-15 08:11:03 +0000576 else
577 return bm_has_conflict_with(bm, a1, a1 + 2, eLoad);
barta79df6e2008-03-14 17:07:51 +0000578}
579
580Bool bm_load_4_has_conflict_with(const struct bitmap* const bm, const Addr a1)
581{
bart3772a982008-03-15 08:11:03 +0000582 if ((a1 & 3) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000583 return bm_aligned_load_has_conflict_with(bm, a1, 4);
bart3772a982008-03-15 08:11:03 +0000584 else
585 return bm_has_conflict_with(bm, a1, a1 + 4, eLoad);
barta79df6e2008-03-14 17:07:51 +0000586}
587
588Bool bm_load_8_has_conflict_with(const struct bitmap* const bm, const Addr a1)
589{
bart3772a982008-03-15 08:11:03 +0000590 if ((a1 & 7) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000591 return bm_aligned_load_has_conflict_with(bm, a1, 8);
bart3772a982008-03-15 08:11:03 +0000592 else
593 return bm_has_conflict_with(bm, a1, a1 + 8, eLoad);
barta79df6e2008-03-14 17:07:51 +0000594}
595
596Bool bm_store_1_has_conflict_with(const struct bitmap* const bm, const Addr a1)
597{
bartf8bc71d2008-03-15 11:42:34 +0000598 return bm_aligned_store_has_conflict_with(bm, a1, 1);
barta79df6e2008-03-14 17:07:51 +0000599}
600
601Bool bm_store_2_has_conflict_with(const struct bitmap* const bm, const Addr a1)
602{
bart3772a982008-03-15 08:11:03 +0000603 if ((a1 & 1) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000604 return bm_aligned_store_has_conflict_with(bm, a1, 2);
bart3772a982008-03-15 08:11:03 +0000605 else
606 return bm_has_conflict_with(bm, a1, a1 + 2, eStore);
barta79df6e2008-03-14 17:07:51 +0000607}
608
609Bool bm_store_4_has_conflict_with(const struct bitmap* const bm, const Addr a1)
610{
bart3772a982008-03-15 08:11:03 +0000611 if ((a1 & 3) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000612 return bm_aligned_store_has_conflict_with(bm, a1, 4);
bart3772a982008-03-15 08:11:03 +0000613 else
614 return bm_has_conflict_with(bm, a1, a1 + 4, eStore);
barta79df6e2008-03-14 17:07:51 +0000615}
616
617Bool bm_store_8_has_conflict_with(const struct bitmap* const bm, const Addr a1)
618{
bart3772a982008-03-15 08:11:03 +0000619 if ((a1 & 7) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000620 return bm_aligned_store_has_conflict_with(bm, a1, 8);
bart3772a982008-03-15 08:11:03 +0000621 else
622 return bm_has_conflict_with(bm, a1, a1 + 8, eStore);
barta79df6e2008-03-14 17:07:51 +0000623}
624
bart36556122008-03-13 19:24:30 +0000625Bool bm_store_has_conflict_with(const struct bitmap* const bm,
626 const Addr a1, const Addr a2)
627{
bart3772a982008-03-15 08:11:03 +0000628 return bm_has_conflict_with(bm, a1, a2, eStore);
sewardjaf44c822007-11-25 14:01:38 +0000629}
630
631void bm_swap(struct bitmap* const bm1, struct bitmap* const bm2)
632{
bart3772a982008-03-15 08:11:03 +0000633 OSet* const tmp = bm1->oset;
634 bm1->oset = bm2->oset;
635 bm2->oset = tmp;
sewardjaf44c822007-11-25 14:01:38 +0000636}
637
bartf647d342008-03-24 19:12:12 +0000638/** Merge bitmaps *lhs and *rhs into *lhs. */
sewardjaf44c822007-11-25 14:01:38 +0000639void bm_merge2(struct bitmap* const lhs,
640 const struct bitmap* const rhs)
641{
bart3772a982008-03-15 08:11:03 +0000642 struct bitmap2* bm2l;
bartf647d342008-03-24 19:12:12 +0000643 struct bitmap2ref* bm2l_ref;
644 struct bitmap2* bm2r;
645 const struct bitmap2ref* bm2r_ref;
sewardjaf44c822007-11-25 14:01:38 +0000646
bart3772a982008-03-15 08:11:03 +0000647 VG_(OSetGen_ResetIter)(rhs->oset);
sewardjaf44c822007-11-25 14:01:38 +0000648
bartf647d342008-03-24 19:12:12 +0000649 for ( ; (bm2r_ref = VG_(OSetGen_Next)(rhs->oset)) != 0; )
bart3772a982008-03-15 08:11:03 +0000650 {
bartf647d342008-03-24 19:12:12 +0000651 bm2r = bm2r_ref->bm2;
652 bm2l_ref = VG_(OSetGen_Lookup)(lhs->oset, &bm2r->addr);
653 if (bm2l_ref)
bart3772a982008-03-15 08:11:03 +0000654 {
bartf647d342008-03-24 19:12:12 +0000655 bm2l = bm2l_ref->bm2;
656 if (bm2l != bm2r)
657 {
658 if (bm2l->refcnt > 1)
659 bm2l = bm2_make_exclusive(lhs, bm2l_ref);
660 bm2_merge(bm2l, bm2r);
661 }
662 }
663 else
664 {
665 bm2_insert_addref(lhs, bm2r);
666 }
bart3772a982008-03-15 08:11:03 +0000667 }
sewardjaf44c822007-11-25 14:01:38 +0000668}
669
670/**
671 * Report whether there are any RW / WR / WW patterns in lhs and rhs.
672 * @param lhs First bitmap.
673 * @param rhs Bitmap to be compared with lhs.
674 * @return !=0 if there are data races, == 0 if there are none.
675 */
676int bm_has_races(const struct bitmap* const lhs,
677 const struct bitmap* const rhs)
678{
bart3772a982008-03-15 08:11:03 +0000679 VG_(OSetGen_ResetIter)(lhs->oset);
680 VG_(OSetGen_ResetIter)(rhs->oset);
sewardjaf44c822007-11-25 14:01:38 +0000681
bart3772a982008-03-15 08:11:03 +0000682 for (;;)
683 {
bartf647d342008-03-24 19:12:12 +0000684 const struct bitmap2ref* bm2l_ref;
685 const struct bitmap2ref* bm2r_ref;
686 const struct bitmap2* bm2l;
687 const struct bitmap2* bm2r;
bart3772a982008-03-15 08:11:03 +0000688 const struct bitmap1* bm1l;
689 const struct bitmap1* bm1r;
690 unsigned k;
sewardjaf44c822007-11-25 14:01:38 +0000691
bartf647d342008-03-24 19:12:12 +0000692 bm2l_ref = VG_(OSetGen_Next)(lhs->oset);
693 bm2l = bm2l_ref->bm2;
694 bm2r_ref = VG_(OSetGen_Next)(rhs->oset);
695 bm2r = bm2r_ref->bm2;
bart3772a982008-03-15 08:11:03 +0000696 while (bm2l && bm2r && bm2l->addr != bm2r->addr)
697 {
698 if (bm2l->addr < bm2r->addr)
bartf647d342008-03-24 19:12:12 +0000699 bm2l = (bm2l_ref = VG_(OSetGen_Next)(lhs->oset))->bm2;
bart3772a982008-03-15 08:11:03 +0000700 else
bartf647d342008-03-24 19:12:12 +0000701 bm2r = (bm2r_ref = VG_(OSetGen_Next)(rhs->oset))->bm2;
bart3772a982008-03-15 08:11:03 +0000702 }
703 if (bm2l == 0 || bm2r == 0)
704 break;
705
706 bm1l = &bm2l->bm1;
707 bm1r = &bm2r->bm1;
708
709 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
710 {
711 unsigned b;
712 for (b = 0; b < BITS_PER_UWORD; b++)
sewardjaf44c822007-11-25 14:01:38 +0000713 {
bart3772a982008-03-15 08:11:03 +0000714 UWord const access
715 = ((bm1l->bm0_r[k] & bm0_mask(b)) ? LHS_R : 0)
716 | ((bm1l->bm0_w[k] & bm0_mask(b)) ? LHS_W : 0)
717 | ((bm1r->bm0_r[k] & bm0_mask(b)) ? RHS_R : 0)
718 | ((bm1r->bm0_w[k] & bm0_mask(b)) ? RHS_W : 0);
719 Addr const a = MAKE_ADDRESS(bm2l->addr, k * BITS_PER_UWORD | b);
720 if (HAS_RACE(access) && ! drd_is_suppressed(a, a + 1))
721 {
722 return 1;
723 }
sewardjaf44c822007-11-25 14:01:38 +0000724 }
bart3772a982008-03-15 08:11:03 +0000725 }
726 }
727 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000728}
729
sewardjaf44c822007-11-25 14:01:38 +0000730void bm_print(const struct bitmap* const bm)
731{
bart3772a982008-03-15 08:11:03 +0000732 struct bitmap2* bm2;
bartf647d342008-03-24 19:12:12 +0000733 struct bitmap2ref* bm2ref;
sewardjaf44c822007-11-25 14:01:38 +0000734
bart3772a982008-03-15 08:11:03 +0000735 VG_(OSetGen_ResetIter)(bm->oset);
sewardjaf44c822007-11-25 14:01:38 +0000736
bartf647d342008-03-24 19:12:12 +0000737 for ( ; (bm2ref = VG_(OSetGen_Next)(bm->oset)) != 0; )
bart3772a982008-03-15 08:11:03 +0000738 {
bartf647d342008-03-24 19:12:12 +0000739 const struct bitmap1* bm1;
bart0008f5b2008-03-22 17:07:39 +0000740 unsigned b;
bartf647d342008-03-24 19:12:12 +0000741
742 bm2 = bm2ref->bm2;
743 bm1 = &bm2->bm1;
bart0008f5b2008-03-22 17:07:39 +0000744 for (b = 0; b < ADDR0_COUNT; b++)
bart3772a982008-03-15 08:11:03 +0000745 {
bart0008f5b2008-03-22 17:07:39 +0000746 const Addr a = (bm2->addr << ADDR0_BITS) | b;
747 const Bool r = bm0_is_set(bm1->bm0_r, b) != 0;
748 const Bool w = bm0_is_set(bm1->bm0_w, b) != 0;
749 if (r || w)
sewardjaf44c822007-11-25 14:01:38 +0000750 {
bart0008f5b2008-03-22 17:07:39 +0000751 VG_(printf)("0x%08lx %c %c\n",
752 a,
753 w ? 'W' : ' ',
754 r ? 'R' : ' ');
sewardjaf44c822007-11-25 14:01:38 +0000755 }
bart3772a982008-03-15 08:11:03 +0000756 }
757 }
sewardjaf44c822007-11-25 14:01:38 +0000758}
759
760ULong bm_get_bitmap_creation_count(void)
761{
bart3772a982008-03-15 08:11:03 +0000762 return s_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +0000763}
764
765ULong bm_get_bitmap2_creation_count(void)
766{
bart3772a982008-03-15 08:11:03 +0000767 return s_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +0000768}
769
bartf647d342008-03-24 19:12:12 +0000770/** Allocate and initialize a second level bitmap. */
771static struct bitmap2* bm2_new(const UWord a1)
772{
773 struct bitmap2* bm2;
774
775 bm2 = VG_(malloc)(sizeof(*bm2));
776 bm2->addr = a1;
777 bm2->refcnt = 1;
778
779 s_bitmap2_creation_count++;
780
781 return bm2;
782}
783
784/** Make a copy of a shared second level bitmap such that the copy can be
785 * modified.
786 *
787 * @param a1 client address shifted right by ADDR0_BITS.
788 * @param bm bitmap pointer.
789 */
790static
791struct bitmap2* bm2_make_exclusive(struct bitmap* const bm,
792 struct bitmap2ref* const bm2ref)
793{
794 UWord a1;
795 struct bitmap2* bm2;
796 struct bitmap2* bm2_copy;
797
798 tl_assert(bm);
799 tl_assert(bm2ref);
800 bm2 = bm2ref->bm2;
801 tl_assert(bm2);
802 tl_assert(bm2->refcnt > 1);
803 bm2->refcnt--;
804 tl_assert(bm2->refcnt >= 1);
805 a1 = bm2->addr;
806 bm2_copy = bm2_new(a1);
807 tl_assert(bm2_copy);
808 tl_assert(bm2_copy->addr == a1);
809 tl_assert(bm2_copy->refcnt == 1);
810 VG_(memcpy)(&bm2_copy->bm1, &bm2->bm1, sizeof(bm2->bm1));
811 bm2ref->bm2 = bm2_copy;
812
813 bm_update_cache(bm, a1, bm2_copy);
814
815 return bm2_copy;
816}
817
sewardjaf44c822007-11-25 14:01:38 +0000818static void bm2_merge(struct bitmap2* const bm2l,
819 const struct bitmap2* const bm2r)
820{
bart3772a982008-03-15 08:11:03 +0000821 unsigned k;
sewardjaf44c822007-11-25 14:01:38 +0000822
bart33e56c92008-03-24 06:41:30 +0000823 tl_assert(bm2l);
824 tl_assert(bm2r);
bart3772a982008-03-15 08:11:03 +0000825 tl_assert(bm2l->addr == bm2r->addr);
bartf647d342008-03-24 19:12:12 +0000826 tl_assert(bm2l->refcnt == 1);
sewardjaf44c822007-11-25 14:01:38 +0000827
bart3772a982008-03-15 08:11:03 +0000828 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
829 {
830 bm2l->bm1.bm0_r[k] |= bm2r->bm1.bm0_r[k];
831 }
832 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
833 {
834 bm2l->bm1.bm0_w[k] |= bm2r->bm1.bm0_w[k];
835 }
sewardjaf44c822007-11-25 14:01:38 +0000836}
837
838#if 0
839
840/* Unit test */
841static
842struct { Addr address; SizeT size; BmAccessTypeT access_type; }
bart3772a982008-03-15 08:11:03 +0000843 s_args[] = {
bart0008f5b2008-03-22 17:07:39 +0000844 { 0 + ADDR0_COUNT, 1, eLoad },
845 { 666 + ADDR0_COUNT, 4, eLoad },
846 { 667 + ADDR0_COUNT, 2, eStore },
bart33e56c92008-03-24 06:41:30 +0000847 { -2 + 2*ADDR0_COUNT, 1, eStore },
bart0008f5b2008-03-22 17:07:39 +0000848 { 0x0001ffffUL, 1, eLoad },
849 { 0x0002ffffUL, 1, eLoad },
850 { 0x00ffffffUL, 1, eLoad },
851 { 0xffffffffUL, 1, eStore },
bart3772a982008-03-15 08:11:03 +0000852 };
sewardjaf44c822007-11-25 14:01:38 +0000853
854void bm_test(void)
855{
bart3772a982008-03-15 08:11:03 +0000856 struct bitmap* bm;
857 struct bitmap* bm2;
bart0008f5b2008-03-22 17:07:39 +0000858 unsigned i, j;
sewardjaf44c822007-11-25 14:01:38 +0000859
bart3772a982008-03-15 08:11:03 +0000860 VG_(printf)("Start of DRD BM unit test.\n");
sewardjaf44c822007-11-25 14:01:38 +0000861
bart3772a982008-03-15 08:11:03 +0000862 bm = bm_new();
sewardjaf44c822007-11-25 14:01:38 +0000863
bart3772a982008-03-15 08:11:03 +0000864 for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++)
865 {
866 bm_access_range(bm,
867 s_args[i].address,
868 s_args[i].address + s_args[i].size,
869 s_args[i].access_type);
870 }
sewardjaf44c822007-11-25 14:01:38 +0000871
bart3772a982008-03-15 08:11:03 +0000872 VG_(printf)("Map contents -- should contain 10 addresses:\n");
873 bm_print(bm);
sewardjaf44c822007-11-25 14:01:38 +0000874
bart3772a982008-03-15 08:11:03 +0000875 for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++)
876 {
877 for (j = 0; j < s_args[i].size; j++)
878 {
879 tl_assert(bm_has_1(bm, s_args[i].address + j, s_args[i].access_type));
880 }
881 }
sewardjaf44c822007-11-25 14:01:38 +0000882
bart3772a982008-03-15 08:11:03 +0000883 VG_(printf)("Merge result:\n");
bart0008f5b2008-03-22 17:07:39 +0000884 bm2 = bm_new();
885 bm_merge2(bm2, bm);
886 bm_merge2(bm2, bm);
bart3772a982008-03-15 08:11:03 +0000887 bm_print(bm);
sewardjaf44c822007-11-25 14:01:38 +0000888
bart0008f5b2008-03-22 17:07:39 +0000889 VG_(printf)("Deleting bitmap bm\n");
bart3772a982008-03-15 08:11:03 +0000890 bm_delete(bm);
bart0008f5b2008-03-22 17:07:39 +0000891 VG_(printf)("Deleting bitmap bm2\n");
bart3772a982008-03-15 08:11:03 +0000892 bm_delete(bm2);
sewardjaf44c822007-11-25 14:01:38 +0000893
bart3772a982008-03-15 08:11:03 +0000894 VG_(printf)("End of DRD BM unit test.\n");
sewardjaf44c822007-11-25 14:01:38 +0000895}
896#endif