sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | 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 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 39 | /* Forward declarations. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 40 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 41 | struct bitmap2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 42 | |
| 43 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 44 | /* Local function declarations. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 45 | |
| 46 | static void bm2_merge(struct bitmap2* const bm2l, |
| 47 | const struct bitmap2* const bm2r); |
| 48 | |
| 49 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 50 | /* Local constants. */ |
| 51 | |
| 52 | static ULong s_bitmap_creation_count; |
| 53 | |
| 54 | |
| 55 | /* Function definitions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | |
| 57 | struct bitmap* bm_new() |
| 58 | { |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 59 | unsigned i; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 60 | struct bitmap* bm; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 61 | |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 62 | /* If this assert fails, fix the definition of BITS_PER_BITS_PER_UWORD */ |
| 63 | /* in drd_bitmap.h. */ |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 64 | tl_assert((1 << BITS_PER_BITS_PER_UWORD) == BITS_PER_UWORD); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 65 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 66 | bm = VG_(malloc)(sizeof(*bm)); |
| 67 | tl_assert(bm); |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 68 | /* Cache initialization. a1 is initialized with a value that never can */ |
| 69 | /* match any valid address: the upper ADDR0_BITS bits of a1 are always */ |
| 70 | /* zero for a valid cache entry. */ |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 71 | for (i = 0; i < N_CACHE_ELEM; i++) |
| 72 | { |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 73 | bm->cache[i].a1 = ~(UWord)1; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 74 | bm->cache[i].bm2 = 0; |
| 75 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 76 | bm->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 77 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 78 | s_bitmap_creation_count++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 79 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 80 | return bm; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void bm_delete(struct bitmap* const bm) |
| 84 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 85 | struct bitmap2* bm2; |
| 86 | struct bitmap2ref* bm2ref; |
| 87 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 88 | tl_assert(bm); |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 89 | |
| 90 | VG_(OSetGen_ResetIter)(bm->oset); |
| 91 | for ( ; (bm2ref = VG_(OSetGen_Next)(bm->oset)) != 0; ) |
| 92 | { |
| 93 | bm2 = bm2ref->bm2; |
| 94 | tl_assert(bm2->refcnt >= 1); |
| 95 | if (--bm2->refcnt == 0) |
| 96 | { |
| 97 | VG_(free)(bm2); |
| 98 | } |
| 99 | } |
| 100 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 101 | VG_(OSetGen_Destroy)(bm->oset); |
| 102 | VG_(free)(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 106 | * Record an access of type access_type at addresses a .. a + size - 1 in |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 107 | * bitmap bm. |
| 108 | */ |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 109 | static |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 110 | void bm_access_range(struct bitmap* const bm, |
bart | 9036dea | 2008-03-13 19:10:06 +0000 | [diff] [blame] | 111 | const Addr a1, const Addr a2, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 112 | const BmAccessTypeT access_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 113 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 114 | Addr b, b_next; |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 115 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 116 | tl_assert(bm); |
| 117 | tl_assert(a1 < a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 118 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 119 | for (b = a1; b < a2; b = b_next) |
| 120 | { |
| 121 | Addr b_start; |
| 122 | Addr b_end; |
| 123 | struct bitmap2* bm2; |
| 124 | SPLIT_ADDRESS(b); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 125 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 126 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 127 | if (b_next > a2) |
| 128 | { |
| 129 | b_next = a2; |
| 130 | } |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 131 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 132 | bm2 = bm2_lookup_or_insert_exclusive(bm, b1); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 133 | tl_assert(bm2); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 134 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 135 | if ((bm2->addr << ADDR0_BITS) < a1) |
| 136 | b_start = a1; |
| 137 | else |
| 138 | if ((bm2->addr << ADDR0_BITS) < a2) |
| 139 | b_start = (bm2->addr << ADDR0_BITS); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 140 | else |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 141 | break; |
| 142 | tl_assert(a1 <= b_start && b_start <= a2); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 143 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 144 | if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2) |
| 145 | b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT; |
| 146 | else |
| 147 | b_end = a2; |
| 148 | tl_assert(a1 <= b_end && b_end <= a2); |
| 149 | tl_assert(b_start < b_end); |
| 150 | tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK)); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 151 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 152 | if (access_type == eLoad) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 153 | { |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 154 | for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end - 1) & ADDR0_MASK); b0++) |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 155 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 156 | bm0_set(bm2->bm1.bm0_r, b0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 157 | } |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 158 | } |
| 159 | else |
| 160 | { |
| 161 | for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end - 1) & ADDR0_MASK); b0++) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 162 | { |
| 163 | bm0_set(bm2->bm1.bm0_w, b0); |
| 164 | } |
| 165 | } |
| 166 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 167 | } |
| 168 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 169 | static inline |
| 170 | void bm_access_aligned_load(struct bitmap* const bm, |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 171 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 172 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 173 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 174 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 175 | bm2 = bm2_lookup_or_insert_exclusive(bm, a1 >> ADDR0_BITS); |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 176 | bm0_set_range(bm2->bm1.bm0_r, a1 & ADDR0_MASK, size); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static inline |
| 180 | void bm_access_aligned_store(struct bitmap* const bm, |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 181 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 182 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 183 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 184 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 185 | bm2 = bm2_lookup_or_insert_exclusive(bm, a1 >> ADDR0_BITS); |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 186 | bm0_set_range(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 187 | } |
| 188 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 189 | void bm_access_range_load(struct bitmap* const bm, |
| 190 | const Addr a1, const Addr a2) |
| 191 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 192 | bm_access_range(bm, a1, a2, eLoad); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 193 | } |
| 194 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 195 | void bm_access_load_1(struct bitmap* const bm, const Addr a1) |
| 196 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 197 | bm_access_aligned_load(bm, a1, 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void bm_access_load_2(struct bitmap* const bm, const Addr a1) |
| 201 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 202 | if ((a1 & 1) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 203 | bm_access_aligned_load(bm, a1, 2); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 204 | else |
| 205 | bm_access_range(bm, a1, a1 + 2, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void bm_access_load_4(struct bitmap* const bm, const Addr a1) |
| 209 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 210 | if ((a1 & 3) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 211 | bm_access_aligned_load(bm, a1, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 212 | else |
| 213 | bm_access_range(bm, a1, a1 + 4, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void bm_access_load_8(struct bitmap* const bm, const Addr a1) |
| 217 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 218 | if ((a1 & 7) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 219 | bm_access_aligned_load(bm, a1, 8); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 220 | else if ((a1 & 3) == 0) |
| 221 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 222 | bm_access_aligned_load(bm, a1 + 0, 4); |
| 223 | bm_access_aligned_load(bm, a1 + 4, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 224 | } |
| 225 | else |
| 226 | bm_access_range(bm, a1, a1 + 8, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void bm_access_store_1(struct bitmap* const bm, const Addr a1) |
| 230 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 231 | bm_access_aligned_store(bm, a1, 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void bm_access_store_2(struct bitmap* const bm, const Addr a1) |
| 235 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 236 | if ((a1 & 1) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 237 | bm_access_aligned_store(bm, a1, 2); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 238 | else |
| 239 | bm_access_range(bm, a1, a1 + 2, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void bm_access_store_4(struct bitmap* const bm, const Addr a1) |
| 243 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 244 | if ((a1 & 3) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 245 | bm_access_aligned_store(bm, a1, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 246 | else |
| 247 | bm_access_range(bm, a1, a1 + 4, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void bm_access_store_8(struct bitmap* const bm, const Addr a1) |
| 251 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 252 | if ((a1 & 7) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 253 | bm_access_aligned_store(bm, a1, 8); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 254 | else if ((a1 & 3) == 0) |
| 255 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 256 | bm_access_aligned_store(bm, a1 + 0, 4); |
| 257 | bm_access_aligned_store(bm, a1 + 4, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 258 | } |
| 259 | else |
| 260 | bm_access_range(bm, a1, a1 + 8, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 261 | } |
| 262 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 263 | void bm_access_range_store(struct bitmap* const bm, |
| 264 | const Addr a1, const Addr a2) |
| 265 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 266 | bm_access_range(bm, a1, a2, eStore); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | Bool bm_has(const struct bitmap* const bm, const Addr a1, const Addr a2, |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 270 | const BmAccessTypeT access_type) |
| 271 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 272 | Addr b; |
| 273 | for (b = a1; b < a2; b++) |
| 274 | { |
| 275 | if (! bm_has_1(bm, b, access_type)) |
| 276 | { |
| 277 | return False; |
| 278 | } |
| 279 | } |
| 280 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | Bool bm_has_any(const struct bitmap* const bm, |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 284 | const Addr a1, const Addr a2, |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 285 | const BmAccessTypeT access_type) |
| 286 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 287 | Addr b; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 288 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 289 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 290 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 291 | for (b = a1; b < a2; b++) |
| 292 | { |
| 293 | if (bm_has_1(bm, b, access_type)) |
| 294 | { |
| 295 | return True; |
| 296 | } |
| 297 | } |
| 298 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* Return a non-zero value if there is a read access, write access or both */ |
| 302 | /* to any of the addresses in the range [ a1, a2 [ in bitmap bm. */ |
| 303 | UWord bm_has_any_access(const struct bitmap* const bm, |
| 304 | const Addr a1, |
| 305 | const Addr a2) |
| 306 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 307 | Addr b, b_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 308 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 309 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 310 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 311 | for (b = a1; b < a2; b = b_next) |
| 312 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 313 | const struct bitmap2* bm2 = bm2_lookup(bm, b >> ADDR0_BITS); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 314 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 315 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 316 | if (b_next > a2) |
| 317 | { |
| 318 | b_next = a2; |
| 319 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 320 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 321 | if (bm2) |
| 322 | { |
| 323 | Addr b_start; |
| 324 | Addr b_end; |
| 325 | UWord b0; |
| 326 | const struct bitmap1* const p1 = &bm2->bm1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 327 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 328 | if ((bm2->addr << ADDR0_BITS) < a1) |
| 329 | b_start = a1; |
| 330 | else |
| 331 | if ((bm2->addr << ADDR0_BITS) < a2) |
| 332 | b_start = (bm2->addr << ADDR0_BITS); |
| 333 | else |
| 334 | break; |
| 335 | tl_assert(a1 <= b_start && b_start <= a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 336 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 337 | if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2) |
| 338 | b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT; |
| 339 | else |
| 340 | b_end = a2; |
| 341 | tl_assert(a1 <= b_end && b_end <= a2); |
| 342 | tl_assert(b_start < b_end); |
| 343 | tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 344 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 345 | for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end-1) & ADDR0_MASK); b0++) |
| 346 | { |
| 347 | const UWord mask |
| 348 | = bm0_is_set(p1->bm0_r, b0) | bm0_is_set(p1->bm0_w, b0); |
| 349 | if (mask) |
| 350 | { |
| 351 | return mask; |
| 352 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 353 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Report whether an access of type access_type at address a is recorded in |
| 361 | * bitmap bm. |
| 362 | * @return != 0 means true, and == 0 means false |
| 363 | */ |
| 364 | UWord bm_has_1(const struct bitmap* const bm, |
| 365 | const Addr a, |
| 366 | const BmAccessTypeT access_type) |
| 367 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 368 | const struct bitmap2* p2; |
| 369 | const struct bitmap1* p1; |
| 370 | const UWord* p0; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 371 | const UWord a0 = a & ADDR0_MASK; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 372 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 373 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 374 | |
bart | 11d0b50 | 2008-03-22 16:44:03 +0000 | [diff] [blame] | 375 | p2 = bm2_lookup(bm, a >> ADDR0_BITS); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 376 | if (p2) |
| 377 | { |
| 378 | p1 = &p2->bm1; |
| 379 | p0 = (access_type == eLoad) ? p1->bm0_r : p1->bm0_w; |
| 380 | return bm0_is_set(p0, a0); |
| 381 | } |
| 382 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 383 | } |
| 384 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 385 | void bm_clear(const struct bitmap* const bm, |
| 386 | const Addr a1, |
| 387 | const Addr a2) |
| 388 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 389 | Addr b, b_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 390 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 391 | tl_assert(bm); |
| 392 | tl_assert(a1); |
| 393 | tl_assert(a1 <= a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 394 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 395 | for (b = a1; b < a2; b = b_next) |
| 396 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 397 | struct bitmap2* const p2 = bm2_lookup_exclusive(bm, b >> ADDR0_BITS); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 398 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 399 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 400 | if (b_next > a2) |
| 401 | { |
| 402 | b_next = a2; |
| 403 | } |
| 404 | |
| 405 | if (p2) |
| 406 | { |
| 407 | Addr c = b; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 408 | /* If the first address in the bitmap that must be cleared does not */ |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 409 | /* start on an UWord boundary, start clearing the first addresses. */ |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 410 | if (UWORD_LSB(c)) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 411 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 412 | Addr c_next = UWORD_MSB(c) + BITS_PER_UWORD; |
| 413 | if (c_next > b_next) |
| 414 | c_next = b_next; |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 415 | bm0_clear_range(p2->bm1.bm0_r, c & ADDR0_MASK, c_next - c); |
| 416 | bm0_clear_range(p2->bm1.bm0_w, c & ADDR0_MASK, c_next - c); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 417 | c = c_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 418 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 419 | /* If some UWords have to be cleared entirely, do this now. */ |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 420 | if (UWORD_LSB(c) == 0) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 421 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 422 | const Addr c_next = UWORD_MSB(b_next); |
| 423 | tl_assert(UWORD_LSB(c) == 0); |
| 424 | tl_assert(UWORD_LSB(c_next) == 0); |
| 425 | tl_assert(c_next <= b_next); |
| 426 | tl_assert(c <= c_next); |
| 427 | if (c_next > c) |
| 428 | { |
| 429 | UWord idx = (c & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD; |
| 430 | VG_(memset)(&p2->bm1.bm0_r[idx], 0, (c_next - c) / 8); |
| 431 | VG_(memset)(&p2->bm1.bm0_w[idx], 0, (c_next - c) / 8); |
| 432 | c = c_next; |
| 433 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 434 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 435 | /* If the last address in the bitmap that must be cleared does not */ |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 436 | /* fall on an UWord boundary, clear the last addresses. */ |
| 437 | /* tl_assert(c <= b_next); */ |
| 438 | bm0_clear_range(p2->bm1.bm0_r, c & ADDR0_MASK, b_next - c); |
| 439 | bm0_clear_range(p2->bm1.bm0_w, c & ADDR0_MASK, b_next - c); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 442 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 443 | |
bart | 9c4224c | 2008-03-29 14:40:08 +0000 | [diff] [blame] | 444 | /** Clear all references to loads in bitmap bm starting at address a1 and |
| 445 | * up to but not including address a2. |
| 446 | */ |
| 447 | void bm_clear_load(const struct bitmap* const bm, |
| 448 | const Addr a1, const Addr a2) |
| 449 | { |
| 450 | Addr a; |
| 451 | |
| 452 | for (a = a1; a < a2; a++) |
| 453 | { |
| 454 | struct bitmap2* const p2 = bm2_lookup_exclusive(bm, a >> ADDR0_BITS); |
| 455 | if (p2) |
| 456 | { |
| 457 | bm0_clear(p2->bm1.bm0_r, a & ADDR0_MASK); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /** Clear all references to stores in bitmap bm starting at address a1 and |
| 463 | * up to but not including address a2. |
| 464 | */ |
| 465 | void bm_clear_store(const struct bitmap* const bm, |
| 466 | const Addr a1, const Addr a2) |
| 467 | { |
| 468 | Addr a; |
| 469 | |
| 470 | for (a = a1; a < a2; a++) |
| 471 | { |
| 472 | struct bitmap2* const p2 = bm2_lookup_exclusive(bm, a >> ADDR0_BITS); |
| 473 | if (p2) |
| 474 | { |
| 475 | bm0_clear(p2->bm1.bm0_w, a & ADDR0_MASK); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 480 | Bool bm_has_conflict_with(const struct bitmap* const bm, |
| 481 | const Addr a1, const Addr a2, |
| 482 | const BmAccessTypeT access_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 483 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 484 | Addr b, b_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 485 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 486 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 487 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 488 | for (b = a1; b < a2; b = b_next) |
| 489 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 490 | const struct bitmap2* bm2 = bm2_lookup(bm, b >> ADDR0_BITS); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 491 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 492 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 493 | if (b_next > a2) |
| 494 | { |
| 495 | b_next = a2; |
| 496 | } |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 497 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 498 | if (bm2) |
| 499 | { |
| 500 | Addr b_start; |
| 501 | Addr b_end; |
| 502 | UWord b0; |
| 503 | const struct bitmap1* const p1 = &bm2->bm1; |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 504 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 505 | if ((bm2->addr << ADDR0_BITS) < a1) |
| 506 | b_start = a1; |
| 507 | else |
| 508 | if ((bm2->addr << ADDR0_BITS) < a2) |
| 509 | b_start = (bm2->addr << ADDR0_BITS); |
| 510 | else |
| 511 | break; |
| 512 | tl_assert(a1 <= b_start && b_start <= a2); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 513 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 514 | if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2) |
| 515 | b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT; |
| 516 | else |
| 517 | b_end = a2; |
| 518 | tl_assert(a1 <= b_end && b_end <= a2); |
| 519 | tl_assert(b_start < b_end); |
| 520 | tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK)); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 521 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 522 | for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end-1) & ADDR0_MASK); b0++) |
| 523 | { |
| 524 | if (access_type == eLoad) |
| 525 | { |
| 526 | if (bm0_is_set(p1->bm0_w, b0)) |
| 527 | { |
| 528 | return True; |
| 529 | } |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | tl_assert(access_type == eStore); |
| 534 | if (bm0_is_set(p1->bm0_r, b0) |
| 535 | | bm0_is_set(p1->bm0_w, b0)) |
| 536 | { |
| 537 | return True; |
| 538 | } |
| 539 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 540 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 544 | } |
| 545 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 546 | static inline |
| 547 | Bool bm_aligned_load_has_conflict_with(const struct bitmap* const bm, |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 548 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 549 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 550 | const struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 551 | |
bart | 11d0b50 | 2008-03-22 16:44:03 +0000 | [diff] [blame] | 552 | bm2 = bm2_lookup(bm, a1 >> ADDR0_BITS); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 553 | |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 554 | return (bm2 && bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static inline |
| 558 | Bool bm_aligned_store_has_conflict_with(const struct bitmap* const bm, |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 559 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 560 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 561 | const struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 562 | |
bart | 11d0b50 | 2008-03-22 16:44:03 +0000 | [diff] [blame] | 563 | bm2 = bm2_lookup(bm, a1 >> ADDR0_BITS); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 564 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 565 | if (bm2) |
| 566 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 567 | if (bm0_is_any_set(bm2->bm1.bm0_r, a1 & ADDR0_MASK, size) |
| 568 | | bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 569 | { |
| 570 | return True; |
| 571 | } |
| 572 | } |
| 573 | return False; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 574 | } |
| 575 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 576 | Bool bm_load_has_conflict_with(const struct bitmap* const bm, |
| 577 | const Addr a1, const Addr a2) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 578 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 579 | return bm_has_conflict_with(bm, a1, a2, eLoad); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 580 | } |
| 581 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 582 | Bool bm_load_1_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 583 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 584 | return bm_aligned_load_has_conflict_with(bm, a1, 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | Bool bm_load_2_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 588 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 589 | if ((a1 & 1) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 590 | return bm_aligned_load_has_conflict_with(bm, a1, 2); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 591 | else |
| 592 | return bm_has_conflict_with(bm, a1, a1 + 2, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | Bool bm_load_4_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 596 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 597 | if ((a1 & 3) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 598 | return bm_aligned_load_has_conflict_with(bm, a1, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 599 | else |
| 600 | return bm_has_conflict_with(bm, a1, a1 + 4, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | Bool bm_load_8_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 604 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 605 | if ((a1 & 7) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 606 | return bm_aligned_load_has_conflict_with(bm, a1, 8); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 607 | else |
| 608 | return bm_has_conflict_with(bm, a1, a1 + 8, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | Bool bm_store_1_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 612 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 613 | return bm_aligned_store_has_conflict_with(bm, a1, 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | Bool bm_store_2_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 617 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 618 | if ((a1 & 1) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 619 | return bm_aligned_store_has_conflict_with(bm, a1, 2); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 620 | else |
| 621 | return bm_has_conflict_with(bm, a1, a1 + 2, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | Bool bm_store_4_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 625 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 626 | if ((a1 & 3) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 627 | return bm_aligned_store_has_conflict_with(bm, a1, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 628 | else |
| 629 | return bm_has_conflict_with(bm, a1, a1 + 4, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | Bool bm_store_8_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 633 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 634 | if ((a1 & 7) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 635 | return bm_aligned_store_has_conflict_with(bm, a1, 8); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 636 | else |
| 637 | return bm_has_conflict_with(bm, a1, a1 + 8, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 638 | } |
| 639 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 640 | Bool bm_store_has_conflict_with(const struct bitmap* const bm, |
| 641 | const Addr a1, const Addr a2) |
| 642 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 643 | return bm_has_conflict_with(bm, a1, a2, eStore); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | void bm_swap(struct bitmap* const bm1, struct bitmap* const bm2) |
| 647 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 648 | OSet* const tmp = bm1->oset; |
| 649 | bm1->oset = bm2->oset; |
| 650 | bm2->oset = tmp; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 651 | } |
| 652 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 653 | /** Merge bitmaps *lhs and *rhs into *lhs. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 654 | void bm_merge2(struct bitmap* const lhs, |
| 655 | const struct bitmap* const rhs) |
| 656 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 657 | struct bitmap2* bm2l; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 658 | struct bitmap2ref* bm2l_ref; |
| 659 | struct bitmap2* bm2r; |
| 660 | const struct bitmap2ref* bm2r_ref; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 661 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 662 | VG_(OSetGen_ResetIter)(rhs->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 663 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 664 | for ( ; (bm2r_ref = VG_(OSetGen_Next)(rhs->oset)) != 0; ) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 665 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 666 | bm2r = bm2r_ref->bm2; |
| 667 | bm2l_ref = VG_(OSetGen_Lookup)(lhs->oset, &bm2r->addr); |
| 668 | if (bm2l_ref) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 669 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 670 | bm2l = bm2l_ref->bm2; |
| 671 | if (bm2l != bm2r) |
| 672 | { |
| 673 | if (bm2l->refcnt > 1) |
| 674 | bm2l = bm2_make_exclusive(lhs, bm2l_ref); |
| 675 | bm2_merge(bm2l, bm2r); |
| 676 | } |
| 677 | } |
| 678 | else |
| 679 | { |
| 680 | bm2_insert_addref(lhs, bm2r); |
| 681 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 682 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Report whether there are any RW / WR / WW patterns in lhs and rhs. |
| 687 | * @param lhs First bitmap. |
| 688 | * @param rhs Bitmap to be compared with lhs. |
| 689 | * @return !=0 if there are data races, == 0 if there are none. |
| 690 | */ |
| 691 | int bm_has_races(const struct bitmap* const lhs, |
| 692 | const struct bitmap* const rhs) |
| 693 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 694 | VG_(OSetGen_ResetIter)(lhs->oset); |
| 695 | VG_(OSetGen_ResetIter)(rhs->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 696 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 697 | for (;;) |
| 698 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 699 | const struct bitmap2ref* bm2l_ref; |
| 700 | const struct bitmap2ref* bm2r_ref; |
| 701 | const struct bitmap2* bm2l; |
| 702 | const struct bitmap2* bm2r; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 703 | const struct bitmap1* bm1l; |
| 704 | const struct bitmap1* bm1r; |
| 705 | unsigned k; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 706 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 707 | bm2l_ref = VG_(OSetGen_Next)(lhs->oset); |
| 708 | bm2l = bm2l_ref->bm2; |
| 709 | bm2r_ref = VG_(OSetGen_Next)(rhs->oset); |
| 710 | bm2r = bm2r_ref->bm2; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 711 | while (bm2l && bm2r && bm2l->addr != bm2r->addr) |
| 712 | { |
| 713 | if (bm2l->addr < bm2r->addr) |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 714 | bm2l = (bm2l_ref = VG_(OSetGen_Next)(lhs->oset))->bm2; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 715 | else |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 716 | bm2r = (bm2r_ref = VG_(OSetGen_Next)(rhs->oset))->bm2; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 717 | } |
| 718 | if (bm2l == 0 || bm2r == 0) |
| 719 | break; |
| 720 | |
| 721 | bm1l = &bm2l->bm1; |
| 722 | bm1r = &bm2r->bm1; |
| 723 | |
| 724 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 725 | { |
| 726 | unsigned b; |
| 727 | for (b = 0; b < BITS_PER_UWORD; b++) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 728 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 729 | UWord const access |
| 730 | = ((bm1l->bm0_r[k] & bm0_mask(b)) ? LHS_R : 0) |
| 731 | | ((bm1l->bm0_w[k] & bm0_mask(b)) ? LHS_W : 0) |
| 732 | | ((bm1r->bm0_r[k] & bm0_mask(b)) ? RHS_R : 0) |
| 733 | | ((bm1r->bm0_w[k] & bm0_mask(b)) ? RHS_W : 0); |
| 734 | Addr const a = MAKE_ADDRESS(bm2l->addr, k * BITS_PER_UWORD | b); |
| 735 | if (HAS_RACE(access) && ! drd_is_suppressed(a, a + 1)) |
| 736 | { |
| 737 | return 1; |
| 738 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 739 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 743 | } |
| 744 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 745 | void bm_print(const struct bitmap* const bm) |
| 746 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 747 | struct bitmap2* bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 748 | struct bitmap2ref* bm2ref; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 749 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 750 | VG_(OSetGen_ResetIter)(bm->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 751 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 752 | for ( ; (bm2ref = VG_(OSetGen_Next)(bm->oset)) != 0; ) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 753 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 754 | const struct bitmap1* bm1; |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 755 | unsigned b; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 756 | |
| 757 | bm2 = bm2ref->bm2; |
| 758 | bm1 = &bm2->bm1; |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 759 | for (b = 0; b < ADDR0_COUNT; b++) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 760 | { |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 761 | const Addr a = (bm2->addr << ADDR0_BITS) | b; |
| 762 | const Bool r = bm0_is_set(bm1->bm0_r, b) != 0; |
| 763 | const Bool w = bm0_is_set(bm1->bm0_w, b) != 0; |
| 764 | if (r || w) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 765 | { |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 766 | VG_(printf)("0x%08lx %c %c\n", |
| 767 | a, |
| 768 | w ? 'W' : ' ', |
| 769 | r ? 'R' : ' '); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 770 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | ULong bm_get_bitmap_creation_count(void) |
| 776 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 777 | return s_bitmap_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | ULong bm_get_bitmap2_creation_count(void) |
| 781 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 782 | return s_bitmap2_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 783 | } |
| 784 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 785 | /** Allocate and initialize a second level bitmap. */ |
| 786 | static struct bitmap2* bm2_new(const UWord a1) |
| 787 | { |
| 788 | struct bitmap2* bm2; |
| 789 | |
| 790 | bm2 = VG_(malloc)(sizeof(*bm2)); |
| 791 | bm2->addr = a1; |
| 792 | bm2->refcnt = 1; |
| 793 | |
| 794 | s_bitmap2_creation_count++; |
| 795 | |
| 796 | return bm2; |
| 797 | } |
| 798 | |
| 799 | /** Make a copy of a shared second level bitmap such that the copy can be |
| 800 | * modified. |
| 801 | * |
| 802 | * @param a1 client address shifted right by ADDR0_BITS. |
| 803 | * @param bm bitmap pointer. |
| 804 | */ |
| 805 | static |
| 806 | struct bitmap2* bm2_make_exclusive(struct bitmap* const bm, |
| 807 | struct bitmap2ref* const bm2ref) |
| 808 | { |
| 809 | UWord a1; |
| 810 | struct bitmap2* bm2; |
| 811 | struct bitmap2* bm2_copy; |
| 812 | |
| 813 | tl_assert(bm); |
| 814 | tl_assert(bm2ref); |
| 815 | bm2 = bm2ref->bm2; |
| 816 | tl_assert(bm2); |
| 817 | tl_assert(bm2->refcnt > 1); |
| 818 | bm2->refcnt--; |
| 819 | tl_assert(bm2->refcnt >= 1); |
| 820 | a1 = bm2->addr; |
| 821 | bm2_copy = bm2_new(a1); |
| 822 | tl_assert(bm2_copy); |
| 823 | tl_assert(bm2_copy->addr == a1); |
| 824 | tl_assert(bm2_copy->refcnt == 1); |
| 825 | VG_(memcpy)(&bm2_copy->bm1, &bm2->bm1, sizeof(bm2->bm1)); |
| 826 | bm2ref->bm2 = bm2_copy; |
| 827 | |
| 828 | bm_update_cache(bm, a1, bm2_copy); |
| 829 | |
| 830 | return bm2_copy; |
| 831 | } |
| 832 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 833 | static void bm2_merge(struct bitmap2* const bm2l, |
| 834 | const struct bitmap2* const bm2r) |
| 835 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 836 | unsigned k; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 837 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 838 | tl_assert(bm2l); |
| 839 | tl_assert(bm2r); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 840 | tl_assert(bm2l->addr == bm2r->addr); |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 841 | tl_assert(bm2l->refcnt == 1); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 842 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 843 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 844 | { |
| 845 | bm2l->bm1.bm0_r[k] |= bm2r->bm1.bm0_r[k]; |
| 846 | } |
| 847 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 848 | { |
| 849 | bm2l->bm1.bm0_w[k] |= bm2r->bm1.bm0_w[k]; |
| 850 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | #if 0 |
| 854 | |
| 855 | /* Unit test */ |
| 856 | static |
| 857 | struct { Addr address; SizeT size; BmAccessTypeT access_type; } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 858 | s_args[] = { |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 859 | { 0 + ADDR0_COUNT, 1, eLoad }, |
| 860 | { 666 + ADDR0_COUNT, 4, eLoad }, |
| 861 | { 667 + ADDR0_COUNT, 2, eStore }, |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 862 | { -2 + 2*ADDR0_COUNT, 1, eStore }, |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 863 | { 0x0001ffffUL, 1, eLoad }, |
| 864 | { 0x0002ffffUL, 1, eLoad }, |
| 865 | { 0x00ffffffUL, 1, eLoad }, |
| 866 | { 0xffffffffUL, 1, eStore }, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 867 | }; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 868 | |
| 869 | void bm_test(void) |
| 870 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 871 | struct bitmap* bm; |
| 872 | struct bitmap* bm2; |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 873 | unsigned i, j; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 874 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 875 | VG_(printf)("Start of DRD BM unit test.\n"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 876 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 877 | bm = bm_new(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 878 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 879 | for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++) |
| 880 | { |
| 881 | bm_access_range(bm, |
| 882 | s_args[i].address, |
| 883 | s_args[i].address + s_args[i].size, |
| 884 | s_args[i].access_type); |
| 885 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 886 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 887 | VG_(printf)("Map contents -- should contain 10 addresses:\n"); |
| 888 | bm_print(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 889 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 890 | for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++) |
| 891 | { |
| 892 | for (j = 0; j < s_args[i].size; j++) |
| 893 | { |
| 894 | tl_assert(bm_has_1(bm, s_args[i].address + j, s_args[i].access_type)); |
| 895 | } |
| 896 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 897 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 898 | VG_(printf)("Merge result:\n"); |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 899 | bm2 = bm_new(); |
| 900 | bm_merge2(bm2, bm); |
| 901 | bm_merge2(bm2, bm); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 902 | bm_print(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 903 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 904 | VG_(printf)("Deleting bitmap bm\n"); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 905 | bm_delete(bm); |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 906 | VG_(printf)("Deleting bitmap bm2\n"); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 907 | bm_delete(bm2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 908 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 909 | VG_(printf)("End of DRD BM unit test.\n"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 910 | } |
| 911 | #endif |