blob: 9d21a1de5ca663719f33d0da195028df5d66a993 [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
sewardjaf44c822007-11-25 14:01:38 +0000382void bm_clear(const struct bitmap* const bm,
383 const Addr a1,
384 const Addr a2)
385{
bart3772a982008-03-15 08:11:03 +0000386 Addr b, b_next;
sewardjaf44c822007-11-25 14:01:38 +0000387
bart3772a982008-03-15 08:11:03 +0000388 tl_assert(bm);
389 tl_assert(a1);
390 tl_assert(a1 <= a2);
sewardjaf44c822007-11-25 14:01:38 +0000391
bart3772a982008-03-15 08:11:03 +0000392 for (b = a1; b < a2; b = b_next)
393 {
bartf647d342008-03-24 19:12:12 +0000394 struct bitmap2* const p2 = bm2_lookup_exclusive(bm, b >> ADDR0_BITS);
sewardjaf44c822007-11-25 14:01:38 +0000395
bart3772a982008-03-15 08:11:03 +0000396 b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT;
397 if (b_next > a2)
398 {
399 b_next = a2;
400 }
401
402 if (p2)
403 {
404 Addr c = b;
bartf647d342008-03-24 19:12:12 +0000405 /* If the first address in the bitmap that must be cleared does not */
bart5955f332008-03-25 17:19:20 +0000406 /* start on an UWord boundary, start clearing the first addresses. */
bart3772a982008-03-15 08:11:03 +0000407 if (UWORD_LSB(c))
sewardjaf44c822007-11-25 14:01:38 +0000408 {
bart3772a982008-03-15 08:11:03 +0000409 Addr c_next = UWORD_MSB(c) + BITS_PER_UWORD;
410 if (c_next > b_next)
411 c_next = b_next;
bart5955f332008-03-25 17:19:20 +0000412 bm0_clear_range(p2->bm1.bm0_r, c & ADDR0_MASK, c_next - c);
413 bm0_clear_range(p2->bm1.bm0_w, c & ADDR0_MASK, c_next - c);
bart3772a982008-03-15 08:11:03 +0000414 c = c_next;
sewardjaf44c822007-11-25 14:01:38 +0000415 }
bartf647d342008-03-24 19:12:12 +0000416 /* If some UWords have to be cleared entirely, do this now. */
bart3772a982008-03-15 08:11:03 +0000417 if (UWORD_LSB(c) == 0)
sewardjaf44c822007-11-25 14:01:38 +0000418 {
bart3772a982008-03-15 08:11:03 +0000419 const Addr c_next = UWORD_MSB(b_next);
420 tl_assert(UWORD_LSB(c) == 0);
421 tl_assert(UWORD_LSB(c_next) == 0);
422 tl_assert(c_next <= b_next);
423 tl_assert(c <= c_next);
424 if (c_next > c)
425 {
426 UWord idx = (c & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD;
427 VG_(memset)(&p2->bm1.bm0_r[idx], 0, (c_next - c) / 8);
428 VG_(memset)(&p2->bm1.bm0_w[idx], 0, (c_next - c) / 8);
429 c = c_next;
430 }
sewardjaf44c822007-11-25 14:01:38 +0000431 }
bartf647d342008-03-24 19:12:12 +0000432 /* If the last address in the bitmap that must be cleared does not */
bart5955f332008-03-25 17:19:20 +0000433 /* fall on an UWord boundary, clear the last addresses. */
434 /* tl_assert(c <= b_next); */
435 bm0_clear_range(p2->bm1.bm0_r, c & ADDR0_MASK, b_next - c);
436 bm0_clear_range(p2->bm1.bm0_w, c & ADDR0_MASK, b_next - c);
bart3772a982008-03-15 08:11:03 +0000437 }
438 }
sewardjaf44c822007-11-25 14:01:38 +0000439}
sewardjaf44c822007-11-25 14:01:38 +0000440
bart36556122008-03-13 19:24:30 +0000441Bool bm_has_conflict_with(const struct bitmap* const bm,
442 const Addr a1, const Addr a2,
443 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +0000444{
bart3772a982008-03-15 08:11:03 +0000445 Addr b, b_next;
sewardjaf44c822007-11-25 14:01:38 +0000446
bart3772a982008-03-15 08:11:03 +0000447 tl_assert(bm);
sewardjaf44c822007-11-25 14:01:38 +0000448
bart3772a982008-03-15 08:11:03 +0000449 for (b = a1; b < a2; b = b_next)
450 {
bartf647d342008-03-24 19:12:12 +0000451 const struct bitmap2* bm2 = bm2_lookup(bm, b >> ADDR0_BITS);
bart36556122008-03-13 19:24:30 +0000452
bart3772a982008-03-15 08:11:03 +0000453 b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT;
454 if (b_next > a2)
455 {
456 b_next = a2;
457 }
bart36556122008-03-13 19:24:30 +0000458
bart3772a982008-03-15 08:11:03 +0000459 if (bm2)
460 {
461 Addr b_start;
462 Addr b_end;
463 UWord b0;
464 const struct bitmap1* const p1 = &bm2->bm1;
bart36556122008-03-13 19:24:30 +0000465
bart3772a982008-03-15 08:11:03 +0000466 if ((bm2->addr << ADDR0_BITS) < a1)
467 b_start = a1;
468 else
469 if ((bm2->addr << ADDR0_BITS) < a2)
470 b_start = (bm2->addr << ADDR0_BITS);
471 else
472 break;
473 tl_assert(a1 <= b_start && b_start <= a2);
bart36556122008-03-13 19:24:30 +0000474
bart3772a982008-03-15 08:11:03 +0000475 if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2)
476 b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT;
477 else
478 b_end = a2;
479 tl_assert(a1 <= b_end && b_end <= a2);
480 tl_assert(b_start < b_end);
481 tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK));
bart36556122008-03-13 19:24:30 +0000482
bart3772a982008-03-15 08:11:03 +0000483 for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end-1) & ADDR0_MASK); b0++)
484 {
485 if (access_type == eLoad)
486 {
487 if (bm0_is_set(p1->bm0_w, b0))
488 {
489 return True;
490 }
491 }
492 else
493 {
494 tl_assert(access_type == eStore);
495 if (bm0_is_set(p1->bm0_r, b0)
496 | bm0_is_set(p1->bm0_w, b0))
497 {
498 return True;
499 }
500 }
sewardjaf44c822007-11-25 14:01:38 +0000501 }
bart3772a982008-03-15 08:11:03 +0000502 }
503 }
504 return False;
sewardjaf44c822007-11-25 14:01:38 +0000505}
506
barta79df6e2008-03-14 17:07:51 +0000507static inline
508Bool bm_aligned_load_has_conflict_with(const struct bitmap* const bm,
bartf8bc71d2008-03-15 11:42:34 +0000509 const Addr a1, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000510{
bartf647d342008-03-24 19:12:12 +0000511 const struct bitmap2* bm2;
barta79df6e2008-03-14 17:07:51 +0000512
bart11d0b502008-03-22 16:44:03 +0000513 bm2 = bm2_lookup(bm, a1 >> ADDR0_BITS);
barta79df6e2008-03-14 17:07:51 +0000514
bartf8bc71d2008-03-15 11:42:34 +0000515 return (bm2 && bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size));
barta79df6e2008-03-14 17:07:51 +0000516}
517
518static inline
519Bool bm_aligned_store_has_conflict_with(const struct bitmap* const bm,
bartf8bc71d2008-03-15 11:42:34 +0000520 const Addr a1, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000521{
bartf647d342008-03-24 19:12:12 +0000522 const struct bitmap2* bm2;
barta79df6e2008-03-14 17:07:51 +0000523
bart11d0b502008-03-22 16:44:03 +0000524 bm2 = bm2_lookup(bm, a1 >> ADDR0_BITS);
barta79df6e2008-03-14 17:07:51 +0000525
bart3772a982008-03-15 08:11:03 +0000526 if (bm2)
527 {
bartf8bc71d2008-03-15 11:42:34 +0000528 if (bm0_is_any_set(bm2->bm1.bm0_r, a1 & ADDR0_MASK, size)
529 | bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size))
bart3772a982008-03-15 08:11:03 +0000530 {
531 return True;
532 }
533 }
534 return False;
barta79df6e2008-03-14 17:07:51 +0000535}
536
bart36556122008-03-13 19:24:30 +0000537Bool bm_load_has_conflict_with(const struct bitmap* const bm,
538 const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +0000539{
bart3772a982008-03-15 08:11:03 +0000540 return bm_has_conflict_with(bm, a1, a2, eLoad);
bart36556122008-03-13 19:24:30 +0000541}
542
barta79df6e2008-03-14 17:07:51 +0000543Bool bm_load_1_has_conflict_with(const struct bitmap* const bm, const Addr a1)
544{
bartf8bc71d2008-03-15 11:42:34 +0000545 return bm_aligned_load_has_conflict_with(bm, a1, 1);
barta79df6e2008-03-14 17:07:51 +0000546}
547
548Bool bm_load_2_has_conflict_with(const struct bitmap* const bm, const Addr a1)
549{
bart3772a982008-03-15 08:11:03 +0000550 if ((a1 & 1) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000551 return bm_aligned_load_has_conflict_with(bm, a1, 2);
bart3772a982008-03-15 08:11:03 +0000552 else
553 return bm_has_conflict_with(bm, a1, a1 + 2, eLoad);
barta79df6e2008-03-14 17:07:51 +0000554}
555
556Bool bm_load_4_has_conflict_with(const struct bitmap* const bm, const Addr a1)
557{
bart3772a982008-03-15 08:11:03 +0000558 if ((a1 & 3) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000559 return bm_aligned_load_has_conflict_with(bm, a1, 4);
bart3772a982008-03-15 08:11:03 +0000560 else
561 return bm_has_conflict_with(bm, a1, a1 + 4, eLoad);
barta79df6e2008-03-14 17:07:51 +0000562}
563
564Bool bm_load_8_has_conflict_with(const struct bitmap* const bm, const Addr a1)
565{
bart3772a982008-03-15 08:11:03 +0000566 if ((a1 & 7) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000567 return bm_aligned_load_has_conflict_with(bm, a1, 8);
bart3772a982008-03-15 08:11:03 +0000568 else
569 return bm_has_conflict_with(bm, a1, a1 + 8, eLoad);
barta79df6e2008-03-14 17:07:51 +0000570}
571
572Bool bm_store_1_has_conflict_with(const struct bitmap* const bm, const Addr a1)
573{
bartf8bc71d2008-03-15 11:42:34 +0000574 return bm_aligned_store_has_conflict_with(bm, a1, 1);
barta79df6e2008-03-14 17:07:51 +0000575}
576
577Bool bm_store_2_has_conflict_with(const struct bitmap* const bm, const Addr a1)
578{
bart3772a982008-03-15 08:11:03 +0000579 if ((a1 & 1) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000580 return bm_aligned_store_has_conflict_with(bm, a1, 2);
bart3772a982008-03-15 08:11:03 +0000581 else
582 return bm_has_conflict_with(bm, a1, a1 + 2, eStore);
barta79df6e2008-03-14 17:07:51 +0000583}
584
585Bool bm_store_4_has_conflict_with(const struct bitmap* const bm, const Addr a1)
586{
bart3772a982008-03-15 08:11:03 +0000587 if ((a1 & 3) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000588 return bm_aligned_store_has_conflict_with(bm, a1, 4);
bart3772a982008-03-15 08:11:03 +0000589 else
590 return bm_has_conflict_with(bm, a1, a1 + 4, eStore);
barta79df6e2008-03-14 17:07:51 +0000591}
592
593Bool bm_store_8_has_conflict_with(const struct bitmap* const bm, const Addr a1)
594{
bart3772a982008-03-15 08:11:03 +0000595 if ((a1 & 7) == 0)
bartf8bc71d2008-03-15 11:42:34 +0000596 return bm_aligned_store_has_conflict_with(bm, a1, 8);
bart3772a982008-03-15 08:11:03 +0000597 else
598 return bm_has_conflict_with(bm, a1, a1 + 8, eStore);
barta79df6e2008-03-14 17:07:51 +0000599}
600
bart36556122008-03-13 19:24:30 +0000601Bool bm_store_has_conflict_with(const struct bitmap* const bm,
602 const Addr a1, const Addr a2)
603{
bart3772a982008-03-15 08:11:03 +0000604 return bm_has_conflict_with(bm, a1, a2, eStore);
sewardjaf44c822007-11-25 14:01:38 +0000605}
606
607void bm_swap(struct bitmap* const bm1, struct bitmap* const bm2)
608{
bart3772a982008-03-15 08:11:03 +0000609 OSet* const tmp = bm1->oset;
610 bm1->oset = bm2->oset;
611 bm2->oset = tmp;
sewardjaf44c822007-11-25 14:01:38 +0000612}
613
bartf647d342008-03-24 19:12:12 +0000614/** Merge bitmaps *lhs and *rhs into *lhs. */
sewardjaf44c822007-11-25 14:01:38 +0000615void bm_merge2(struct bitmap* const lhs,
616 const struct bitmap* const rhs)
617{
bart3772a982008-03-15 08:11:03 +0000618 struct bitmap2* bm2l;
bartf647d342008-03-24 19:12:12 +0000619 struct bitmap2ref* bm2l_ref;
620 struct bitmap2* bm2r;
621 const struct bitmap2ref* bm2r_ref;
sewardjaf44c822007-11-25 14:01:38 +0000622
bart3772a982008-03-15 08:11:03 +0000623 VG_(OSetGen_ResetIter)(rhs->oset);
sewardjaf44c822007-11-25 14:01:38 +0000624
bartf647d342008-03-24 19:12:12 +0000625 for ( ; (bm2r_ref = VG_(OSetGen_Next)(rhs->oset)) != 0; )
bart3772a982008-03-15 08:11:03 +0000626 {
bartf647d342008-03-24 19:12:12 +0000627 bm2r = bm2r_ref->bm2;
628 bm2l_ref = VG_(OSetGen_Lookup)(lhs->oset, &bm2r->addr);
629 if (bm2l_ref)
bart3772a982008-03-15 08:11:03 +0000630 {
bartf647d342008-03-24 19:12:12 +0000631 bm2l = bm2l_ref->bm2;
632 if (bm2l != bm2r)
633 {
634 if (bm2l->refcnt > 1)
635 bm2l = bm2_make_exclusive(lhs, bm2l_ref);
636 bm2_merge(bm2l, bm2r);
637 }
638 }
639 else
640 {
641 bm2_insert_addref(lhs, bm2r);
642 }
bart3772a982008-03-15 08:11:03 +0000643 }
sewardjaf44c822007-11-25 14:01:38 +0000644}
645
646/**
647 * Report whether there are any RW / WR / WW patterns in lhs and rhs.
648 * @param lhs First bitmap.
649 * @param rhs Bitmap to be compared with lhs.
650 * @return !=0 if there are data races, == 0 if there are none.
651 */
652int bm_has_races(const struct bitmap* const lhs,
653 const struct bitmap* const rhs)
654{
bart3772a982008-03-15 08:11:03 +0000655 VG_(OSetGen_ResetIter)(lhs->oset);
656 VG_(OSetGen_ResetIter)(rhs->oset);
sewardjaf44c822007-11-25 14:01:38 +0000657
bart3772a982008-03-15 08:11:03 +0000658 for (;;)
659 {
bartf647d342008-03-24 19:12:12 +0000660 const struct bitmap2ref* bm2l_ref;
661 const struct bitmap2ref* bm2r_ref;
662 const struct bitmap2* bm2l;
663 const struct bitmap2* bm2r;
bart3772a982008-03-15 08:11:03 +0000664 const struct bitmap1* bm1l;
665 const struct bitmap1* bm1r;
666 unsigned k;
sewardjaf44c822007-11-25 14:01:38 +0000667
bartf647d342008-03-24 19:12:12 +0000668 bm2l_ref = VG_(OSetGen_Next)(lhs->oset);
669 bm2l = bm2l_ref->bm2;
670 bm2r_ref = VG_(OSetGen_Next)(rhs->oset);
671 bm2r = bm2r_ref->bm2;
bart3772a982008-03-15 08:11:03 +0000672 while (bm2l && bm2r && bm2l->addr != bm2r->addr)
673 {
674 if (bm2l->addr < bm2r->addr)
bartf647d342008-03-24 19:12:12 +0000675 bm2l = (bm2l_ref = VG_(OSetGen_Next)(lhs->oset))->bm2;
bart3772a982008-03-15 08:11:03 +0000676 else
bartf647d342008-03-24 19:12:12 +0000677 bm2r = (bm2r_ref = VG_(OSetGen_Next)(rhs->oset))->bm2;
bart3772a982008-03-15 08:11:03 +0000678 }
679 if (bm2l == 0 || bm2r == 0)
680 break;
681
682 bm1l = &bm2l->bm1;
683 bm1r = &bm2r->bm1;
684
685 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
686 {
687 unsigned b;
688 for (b = 0; b < BITS_PER_UWORD; b++)
sewardjaf44c822007-11-25 14:01:38 +0000689 {
bart3772a982008-03-15 08:11:03 +0000690 UWord const access
691 = ((bm1l->bm0_r[k] & bm0_mask(b)) ? LHS_R : 0)
692 | ((bm1l->bm0_w[k] & bm0_mask(b)) ? LHS_W : 0)
693 | ((bm1r->bm0_r[k] & bm0_mask(b)) ? RHS_R : 0)
694 | ((bm1r->bm0_w[k] & bm0_mask(b)) ? RHS_W : 0);
695 Addr const a = MAKE_ADDRESS(bm2l->addr, k * BITS_PER_UWORD | b);
696 if (HAS_RACE(access) && ! drd_is_suppressed(a, a + 1))
697 {
698 return 1;
699 }
sewardjaf44c822007-11-25 14:01:38 +0000700 }
bart3772a982008-03-15 08:11:03 +0000701 }
702 }
703 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000704}
705
sewardjaf44c822007-11-25 14:01:38 +0000706void bm_print(const struct bitmap* const bm)
707{
bart3772a982008-03-15 08:11:03 +0000708 struct bitmap2* bm2;
bartf647d342008-03-24 19:12:12 +0000709 struct bitmap2ref* bm2ref;
sewardjaf44c822007-11-25 14:01:38 +0000710
bart3772a982008-03-15 08:11:03 +0000711 VG_(OSetGen_ResetIter)(bm->oset);
sewardjaf44c822007-11-25 14:01:38 +0000712
bartf647d342008-03-24 19:12:12 +0000713 for ( ; (bm2ref = VG_(OSetGen_Next)(bm->oset)) != 0; )
bart3772a982008-03-15 08:11:03 +0000714 {
bartf647d342008-03-24 19:12:12 +0000715 const struct bitmap1* bm1;
bart0008f5b2008-03-22 17:07:39 +0000716 unsigned b;
bartf647d342008-03-24 19:12:12 +0000717
718 bm2 = bm2ref->bm2;
719 bm1 = &bm2->bm1;
bart0008f5b2008-03-22 17:07:39 +0000720 for (b = 0; b < ADDR0_COUNT; b++)
bart3772a982008-03-15 08:11:03 +0000721 {
bart0008f5b2008-03-22 17:07:39 +0000722 const Addr a = (bm2->addr << ADDR0_BITS) | b;
723 const Bool r = bm0_is_set(bm1->bm0_r, b) != 0;
724 const Bool w = bm0_is_set(bm1->bm0_w, b) != 0;
725 if (r || w)
sewardjaf44c822007-11-25 14:01:38 +0000726 {
bart0008f5b2008-03-22 17:07:39 +0000727 VG_(printf)("0x%08lx %c %c\n",
728 a,
729 w ? 'W' : ' ',
730 r ? 'R' : ' ');
sewardjaf44c822007-11-25 14:01:38 +0000731 }
bart3772a982008-03-15 08:11:03 +0000732 }
733 }
sewardjaf44c822007-11-25 14:01:38 +0000734}
735
736ULong bm_get_bitmap_creation_count(void)
737{
bart3772a982008-03-15 08:11:03 +0000738 return s_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +0000739}
740
741ULong bm_get_bitmap2_creation_count(void)
742{
bart3772a982008-03-15 08:11:03 +0000743 return s_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +0000744}
745
bartf647d342008-03-24 19:12:12 +0000746/** Allocate and initialize a second level bitmap. */
747static struct bitmap2* bm2_new(const UWord a1)
748{
749 struct bitmap2* bm2;
750
751 bm2 = VG_(malloc)(sizeof(*bm2));
752 bm2->addr = a1;
753 bm2->refcnt = 1;
754
755 s_bitmap2_creation_count++;
756
757 return bm2;
758}
759
760/** Make a copy of a shared second level bitmap such that the copy can be
761 * modified.
762 *
763 * @param a1 client address shifted right by ADDR0_BITS.
764 * @param bm bitmap pointer.
765 */
766static
767struct bitmap2* bm2_make_exclusive(struct bitmap* const bm,
768 struct bitmap2ref* const bm2ref)
769{
770 UWord a1;
771 struct bitmap2* bm2;
772 struct bitmap2* bm2_copy;
773
774 tl_assert(bm);
775 tl_assert(bm2ref);
776 bm2 = bm2ref->bm2;
777 tl_assert(bm2);
778 tl_assert(bm2->refcnt > 1);
779 bm2->refcnt--;
780 tl_assert(bm2->refcnt >= 1);
781 a1 = bm2->addr;
782 bm2_copy = bm2_new(a1);
783 tl_assert(bm2_copy);
784 tl_assert(bm2_copy->addr == a1);
785 tl_assert(bm2_copy->refcnt == 1);
786 VG_(memcpy)(&bm2_copy->bm1, &bm2->bm1, sizeof(bm2->bm1));
787 bm2ref->bm2 = bm2_copy;
788
789 bm_update_cache(bm, a1, bm2_copy);
790
791 return bm2_copy;
792}
793
sewardjaf44c822007-11-25 14:01:38 +0000794static void bm2_merge(struct bitmap2* const bm2l,
795 const struct bitmap2* const bm2r)
796{
bart3772a982008-03-15 08:11:03 +0000797 unsigned k;
sewardjaf44c822007-11-25 14:01:38 +0000798
bart33e56c92008-03-24 06:41:30 +0000799 tl_assert(bm2l);
800 tl_assert(bm2r);
bart3772a982008-03-15 08:11:03 +0000801 tl_assert(bm2l->addr == bm2r->addr);
bartf647d342008-03-24 19:12:12 +0000802 tl_assert(bm2l->refcnt == 1);
sewardjaf44c822007-11-25 14:01:38 +0000803
bart3772a982008-03-15 08:11:03 +0000804 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
805 {
806 bm2l->bm1.bm0_r[k] |= bm2r->bm1.bm0_r[k];
807 }
808 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
809 {
810 bm2l->bm1.bm0_w[k] |= bm2r->bm1.bm0_w[k];
811 }
sewardjaf44c822007-11-25 14:01:38 +0000812}
813
814#if 0
815
816/* Unit test */
817static
818struct { Addr address; SizeT size; BmAccessTypeT access_type; }
bart3772a982008-03-15 08:11:03 +0000819 s_args[] = {
bart0008f5b2008-03-22 17:07:39 +0000820 { 0 + ADDR0_COUNT, 1, eLoad },
821 { 666 + ADDR0_COUNT, 4, eLoad },
822 { 667 + ADDR0_COUNT, 2, eStore },
bart33e56c92008-03-24 06:41:30 +0000823 { -2 + 2*ADDR0_COUNT, 1, eStore },
bart0008f5b2008-03-22 17:07:39 +0000824 { 0x0001ffffUL, 1, eLoad },
825 { 0x0002ffffUL, 1, eLoad },
826 { 0x00ffffffUL, 1, eLoad },
827 { 0xffffffffUL, 1, eStore },
bart3772a982008-03-15 08:11:03 +0000828 };
sewardjaf44c822007-11-25 14:01:38 +0000829
830void bm_test(void)
831{
bart3772a982008-03-15 08:11:03 +0000832 struct bitmap* bm;
833 struct bitmap* bm2;
bart0008f5b2008-03-22 17:07:39 +0000834 unsigned i, j;
sewardjaf44c822007-11-25 14:01:38 +0000835
bart3772a982008-03-15 08:11:03 +0000836 VG_(printf)("Start of DRD BM unit test.\n");
sewardjaf44c822007-11-25 14:01:38 +0000837
bart3772a982008-03-15 08:11:03 +0000838 bm = bm_new();
sewardjaf44c822007-11-25 14:01:38 +0000839
bart3772a982008-03-15 08:11:03 +0000840 for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++)
841 {
842 bm_access_range(bm,
843 s_args[i].address,
844 s_args[i].address + s_args[i].size,
845 s_args[i].access_type);
846 }
sewardjaf44c822007-11-25 14:01:38 +0000847
bart3772a982008-03-15 08:11:03 +0000848 VG_(printf)("Map contents -- should contain 10 addresses:\n");
849 bm_print(bm);
sewardjaf44c822007-11-25 14:01:38 +0000850
bart3772a982008-03-15 08:11:03 +0000851 for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++)
852 {
853 for (j = 0; j < s_args[i].size; j++)
854 {
855 tl_assert(bm_has_1(bm, s_args[i].address + j, s_args[i].access_type));
856 }
857 }
sewardjaf44c822007-11-25 14:01:38 +0000858
bart3772a982008-03-15 08:11:03 +0000859 VG_(printf)("Merge result:\n");
bart0008f5b2008-03-22 17:07:39 +0000860 bm2 = bm_new();
861 bm_merge2(bm2, bm);
862 bm_merge2(bm2, bm);
bart3772a982008-03-15 08:11:03 +0000863 bm_print(bm);
sewardjaf44c822007-11-25 14:01:38 +0000864
bart0008f5b2008-03-22 17:07:39 +0000865 VG_(printf)("Deleting bitmap bm\n");
bart3772a982008-03-15 08:11:03 +0000866 bm_delete(bm);
bart0008f5b2008-03-22 17:07:39 +0000867 VG_(printf)("Deleting bitmap bm2\n");
bart3772a982008-03-15 08:11:03 +0000868 bm_delete(bm2);
sewardjaf44c822007-11-25 14:01:38 +0000869
bart3772a982008-03-15 08:11:03 +0000870 VG_(printf)("End of DRD BM unit test.\n");
sewardjaf44c822007-11-25 14:01:38 +0000871}
872#endif