bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 4 | |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2010 Bart Van Assche <bart.vanassche@gmail.com>. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 26 | #ifndef __DRD_BITMAP_H |
| 27 | #define __DRD_BITMAP_H |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 28 | |
| 29 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 30 | #include "pub_drd_bitmap.h" |
bart | 82a0c5d | 2009-02-14 14:49:23 +0000 | [diff] [blame] | 31 | #include "pub_tool_basics.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 32 | #include "pub_tool_oset.h" |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 33 | #include "pub_tool_libcbase.h" |
bart | 67cb4fb | 2010-09-02 14:43:50 +0000 | [diff] [blame] | 34 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 35 | #include "pub_tool_libcassert.h" |
| 36 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 37 | |
| 38 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 39 | /* Bitmap representation. A bitmap is a data structure in which two bits are |
| 40 | * reserved per 32 bit address: one bit that indicates that the data at the |
| 41 | * specified address has been read, and one bit that indicates that the data |
| 42 | * has been written to. |
| 43 | */ |
| 44 | |
| 45 | /* Client addresses are split into bitfields as follows: |
| 46 | * ------------------------------------------------------ |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 47 | * | Address MSB | Address LSB | Ignored bits | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 48 | * ------------------------------------------------------ |
| 49 | * | Address MSB | UWord MSB | UWord LSB | Ignored bits | |
| 50 | * ------------------------------------------------------ |
| 51 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 52 | |
| 53 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 54 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 55 | /* Address MSB / LSB split. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 57 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 58 | /** Number of least significant address bits that are ignored. */ |
| 59 | #define ADDR_IGNORED_BITS 0 |
| 60 | #define ADDR_IGNORED_MASK ((1U << ADDR_IGNORED_BITS) - 1U) |
| 61 | #define ADDR_GRANULARITY (1U << ADDR_IGNORED_BITS) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 62 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Round argument a up to a multiple of (1 << ADDR_GRANULARITY), and next |
| 65 | * shift it right ADDR_GRANULARITY bits. The expression below is optimized |
| 66 | * for the case where a is a constant. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 67 | */ |
| 68 | #define SCALED_SIZE(a) \ |
| 69 | (((((a) - 1U) | ADDR_IGNORED_MASK) + 1U) >> ADDR_IGNORED_BITS) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 70 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 71 | /** |
| 72 | * Number of bits assigned to the least significant component of an address. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 73 | */ |
| 74 | #define ADDR_LSB_BITS 12 |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 75 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 76 | /** |
| 77 | * Mask that has to be applied to an address of type Addr in order to |
| 78 | * compute the least significant part of an address split, after having |
| 79 | * shifted the address bits ADDR_GRANULARITY to the right. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 80 | */ |
| 81 | #define ADDR_LSB_MASK (((UWord)1 << ADDR_LSB_BITS) - 1U) |
| 82 | |
| 83 | /** Compute least significant bits of an address of type Addr. */ |
| 84 | static __inline__ |
| 85 | UWord address_lsb(const Addr a) |
| 86 | { return (a >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK; } |
| 87 | |
| 88 | /** |
| 89 | * Compute the first address for which address_lsb() is equal to |
| 90 | * address_lsb(a). |
| 91 | */ |
| 92 | static __inline__ |
| 93 | Addr first_address_with_same_lsb(const Addr a) |
| 94 | { |
| 95 | return ((a | ADDR_IGNORED_MASK) ^ ADDR_IGNORED_MASK); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Compute the first address for which address_lsb() is greater than |
| 100 | * address_lsb(a). |
| 101 | */ |
| 102 | static __inline__ |
| 103 | Addr first_address_with_higher_lsb(const Addr a) |
| 104 | { |
| 105 | return ((a | ADDR_IGNORED_MASK) + 1U); |
| 106 | } |
| 107 | |
| 108 | /** Compute most significant bits of an address of type Addr. */ |
| 109 | static __inline__ |
| 110 | UWord address_msb(const Addr a) |
| 111 | { return a >> (ADDR_LSB_BITS + ADDR_IGNORED_BITS); } |
| 112 | |
| 113 | static __inline__ |
| 114 | Addr first_address_with_higher_msb(const Addr a) |
| 115 | { |
| 116 | return ((a | ((ADDR_LSB_MASK << ADDR_IGNORED_BITS) | ADDR_IGNORED_MASK)) |
| 117 | + 1U); |
| 118 | } |
| 119 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 120 | /** |
| 121 | * Convert LSB and MSB back into an address. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 122 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 123 | * @note It is assumed that sizeof(Addr) == sizeof(UWord). |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 124 | */ |
| 125 | static __inline__ |
| 126 | Addr make_address(const UWord a1, const UWord a0) |
| 127 | { |
| 128 | return ((a1 << (ADDR_LSB_BITS + ADDR_IGNORED_BITS)) |
| 129 | | (a0 << ADDR_IGNORED_BITS)); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 136 | /** Number of bits that fit in a variable of type UWord. */ |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 137 | #define BITS_PER_UWORD (8U * sizeof(UWord)) |
| 138 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 139 | /** Log2 of BITS_PER_UWORD. */ |
sewardj | 4cb6bf7 | 2010-01-01 18:31:41 +0000 | [diff] [blame] | 140 | #if defined(VGA_x86) || defined(VGA_ppc32) || defined(VGA_arm) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 141 | #define BITS_PER_BITS_PER_UWORD 5 |
| 142 | #elif defined(VGA_amd64) || defined(VGA_ppc64) |
| 143 | #define BITS_PER_BITS_PER_UWORD 6 |
| 144 | #else |
| 145 | #error Unknown platform. |
| 146 | #endif |
| 147 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 148 | /** Number of UWord's needed to store one bit per address LSB. */ |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 149 | #define BITMAP1_UWORD_COUNT (1U << (ADDR_LSB_BITS - BITS_PER_BITS_PER_UWORD)) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 150 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 151 | /** |
| 152 | * Mask that has to be applied to an (Addr >> ADDR_IGNORED_BITS) expression |
| 153 | * in order to compute the least significant part of an UWord. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 154 | */ |
| 155 | #define UWORD_LSB_MASK (((UWord)1 << BITS_PER_BITS_PER_UWORD) - 1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 156 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 157 | /** |
| 158 | * Compute index into bm0[] array. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 159 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 160 | * @param a Address shifted right ADDR_IGNORED_BITS bits. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 161 | */ |
| 162 | static __inline__ |
| 163 | UWord uword_msb(const UWord a) |
| 164 | { |
| 165 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 166 | tl_assert(a < (1U << ADDR_LSB_BITS)); |
| 167 | #endif |
| 168 | return a >> BITS_PER_BITS_PER_UWORD; |
| 169 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 170 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 171 | /** |
| 172 | * Return the least significant bits. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 173 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 174 | * @param a Address shifted right ADDR_IGNORED_BITS bits. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 175 | */ |
| 176 | static __inline__ |
| 177 | UWord uword_lsb(const UWord a) |
| 178 | { |
| 179 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 180 | tl_assert(a < (1U << ADDR_LSB_BITS)); |
| 181 | #endif |
| 182 | return a & UWORD_LSB_MASK; |
| 183 | } |
| 184 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 185 | /** |
| 186 | * Compute the highest address lower than a for which |
| 187 | * uword_lsb(address_lsb(a)) == 0. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 188 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 189 | * @param a Address. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 190 | */ |
| 191 | static __inline__ |
| 192 | Addr first_address_with_same_uword_lsb(const Addr a) |
| 193 | { |
| 194 | return (a & (~UWORD_LSB_MASK << ADDR_IGNORED_BITS)); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * First address that will go in the UWord past the one 'a' goes in. |
| 199 | * |
| 200 | * @param a Address. |
| 201 | */ |
| 202 | static __inline__ |
| 203 | Addr first_address_with_higher_uword_msb(const Addr a) |
| 204 | { |
| 205 | return ((a | ((UWORD_LSB_MASK << ADDR_IGNORED_BITS) | ADDR_IGNORED_MASK)) |
| 206 | + 1); |
| 207 | } |
| 208 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 209 | |
| 210 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 211 | /* Local variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 212 | |
| 213 | static ULong s_bitmap2_creation_count; |
| 214 | |
| 215 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 216 | |
| 217 | /*********************************************************************/ |
| 218 | /* Functions for manipulating a struct bitmap1. */ |
| 219 | /*********************************************************************/ |
| 220 | |
| 221 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 222 | /* Lowest level, corresponding to the lowest ADDR_LSB_BITS of an address. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 223 | struct bitmap1 |
| 224 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 225 | UWord bm0_r[BITMAP1_UWORD_COUNT]; |
| 226 | UWord bm0_w[BITMAP1_UWORD_COUNT]; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 229 | static __inline__ UWord bm0_mask(const UWord a) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 230 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 231 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 232 | tl_assert(address_msb(make_address(0, a)) == 0); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 233 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 234 | return ((UWord)1 << uword_lsb(a)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 235 | } |
| 236 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 237 | /** Set the bit corresponding to address a in bitmap bm0. */ |
| 238 | static __inline__ void bm0_set(UWord* bm0, const UWord a) |
| 239 | { |
| 240 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 241 | tl_assert(address_msb(make_address(0, a)) == 0); |
| 242 | #endif |
| 243 | bm0[uword_msb(a)] |= (UWord)1 << uword_lsb(a); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Set the bits corresponding to all of the addresses in range |
| 248 | * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [ |
| 249 | * in bitmap bm0. |
| 250 | */ |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 251 | static __inline__ void bm0_set_range(UWord* bm0, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 252 | const UWord a, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 253 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 254 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 255 | tl_assert(size > 0); |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 256 | tl_assert(address_msb(make_address(0, a)) == 0); |
| 257 | tl_assert(address_msb(make_address(0, a + size - 1)) == 0); |
| 258 | tl_assert(uword_msb(a) == uword_msb(a + size - 1)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 259 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 260 | bm0[uword_msb(a)] |
| 261 | |= (((UWord)1 << size) - 1) << uword_lsb(a); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 262 | } |
| 263 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 264 | /** Clear the bit corresponding to address a in bitmap bm0. */ |
| 265 | static __inline__ void bm0_clear(UWord* bm0, const UWord a) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 266 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 267 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 268 | tl_assert(address_msb(make_address(0, a)) == 0); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 269 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 270 | bm0[uword_msb(a)] &= ~((UWord)1 << uword_lsb(a)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 271 | } |
| 272 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 273 | /** |
| 274 | * Clear all of the addresses in range |
| 275 | * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [ |
| 276 | * in bitmap bm0. |
| 277 | */ |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 278 | static __inline__ void bm0_clear_range(UWord* bm0, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 279 | const UWord a, const SizeT size) |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 280 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 281 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 282 | tl_assert(address_msb(make_address(0, a)) == 0); |
| 283 | tl_assert(size == 0 || address_msb(make_address(0, a + size - 1)) == 0); |
| 284 | tl_assert(size == 0 || uword_msb(a) == uword_msb(a + size - 1)); |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 285 | #endif |
bart | 8c04673 | 2009-04-25 06:52:01 +0000 | [diff] [blame] | 286 | /* |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 287 | * Note: although the expression below yields a correct result even if |
bart | 8c04673 | 2009-04-25 06:52:01 +0000 | [diff] [blame] | 288 | * size == 0, do not touch bm0[] if size == 0 because this might otherwise |
| 289 | * cause an access of memory just past the end of the bm0[] array. |
| 290 | */ |
| 291 | if (size > 0) |
| 292 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 293 | bm0[uword_msb(a)] |
| 294 | &= ~((((UWord)1 << size) - 1) << uword_lsb(a)); |
bart | 8c04673 | 2009-04-25 06:52:01 +0000 | [diff] [blame] | 295 | } |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 296 | } |
| 297 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 298 | /** Test whether the bit corresponding to address a is set in bitmap bm0. */ |
| 299 | static __inline__ UWord bm0_is_set(const UWord* bm0, const UWord a) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 300 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 301 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 302 | tl_assert(address_msb(make_address(0, a)) == 0); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 303 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 304 | return (bm0[uword_msb(a)] & ((UWord)1 << uword_lsb(a))); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 305 | } |
| 306 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 307 | /** |
| 308 | * Return true if a bit corresponding to any of the addresses in range |
| 309 | * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [ |
| 310 | * is set in bm0. |
| 311 | */ |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 312 | static __inline__ UWord bm0_is_any_set(const UWord* bm0, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 313 | const Addr a, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 314 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 315 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 316 | tl_assert(size > 0); |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 317 | tl_assert(address_msb(make_address(0, a)) == 0); |
| 318 | tl_assert(address_msb(make_address(0, a + size - 1)) == 0); |
| 319 | tl_assert(uword_msb(a) == uword_msb(a + size - 1)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 320 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 321 | return (bm0[uword_msb(a)] & ((((UWord)1 << size) - 1) << uword_lsb(a))); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 322 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 323 | |
bart | adbdf14 | 2008-03-19 17:00:12 +0000 | [diff] [blame] | 324 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 325 | |
| 326 | /*********************************************************************/ |
| 327 | /* Functions for manipulating a struct bitmap. */ |
bart | adbdf14 | 2008-03-19 17:00:12 +0000 | [diff] [blame] | 328 | /*********************************************************************/ |
| 329 | |
| 330 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 331 | /* Second level bitmap. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 332 | struct bitmap2 |
| 333 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 334 | Addr addr; ///< address_msb(...) |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 335 | Bool recalc; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 336 | struct bitmap1 bm1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 339 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 340 | static void bm2_clear(struct bitmap2* const bm2); |
| 341 | static __inline__ |
| 342 | struct bitmap2* bm2_insert(struct bitmap* const bm, const UWord a1); |
| 343 | |
| 344 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 345 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 346 | /** |
| 347 | * Rotate elements cache[0..n-1] such that the element at position n-1 is |
| 348 | * moved to position 0. This allows to speed up future cache lookups. |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 349 | */ |
| 350 | static __inline__ |
| 351 | void bm_cache_rotate(struct bm_cache_elem cache[], const int n) |
| 352 | { |
bart | 45af5ea | 2008-06-12 06:04:59 +0000 | [diff] [blame] | 353 | #if 0 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 354 | struct bm_cache_elem t; |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 355 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 356 | tl_assert(2 <= n && n <= 8); |
bart | ea2fa4c | 2008-06-10 06:32:49 +0000 | [diff] [blame] | 357 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 358 | t = cache[0]; |
| 359 | if (n > 1) |
| 360 | cache[0] = cache[1]; |
| 361 | if (n > 2) |
| 362 | cache[1] = cache[2]; |
| 363 | if (n > 3) |
| 364 | cache[2] = cache[3]; |
| 365 | if (n > 4) |
| 366 | cache[3] = cache[4]; |
| 367 | if (n > 5) |
| 368 | cache[4] = cache[5]; |
| 369 | if (n > 6) |
| 370 | cache[5] = cache[6]; |
| 371 | if (n > 7) |
| 372 | cache[6] = cache[7]; |
| 373 | cache[n - 1] = t; |
bart | ea2fa4c | 2008-06-10 06:32:49 +0000 | [diff] [blame] | 374 | #endif |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 375 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 376 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 377 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 378 | Bool bm_cache_lookup(struct bitmap* const bm, const UWord a1, |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 379 | struct bitmap2** bm2) |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 380 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 381 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 382 | tl_assert(bm); |
| 383 | tl_assert(bm2); |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 384 | #endif |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 385 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 386 | #if DRD_BITMAP_N_CACHE_ELEM > 8 |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 387 | #error Please update the code below. |
| 388 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 389 | #if DRD_BITMAP_N_CACHE_ELEM >= 1 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 390 | if (a1 == bm->cache[0].a1) |
| 391 | { |
| 392 | *bm2 = bm->cache[0].bm2; |
| 393 | return True; |
| 394 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 395 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 396 | #if DRD_BITMAP_N_CACHE_ELEM >= 2 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 397 | if (a1 == bm->cache[1].a1) |
| 398 | { |
| 399 | *bm2 = bm->cache[1].bm2; |
| 400 | return True; |
| 401 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 402 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 403 | #if DRD_BITMAP_N_CACHE_ELEM >= 3 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 404 | if (a1 == bm->cache[2].a1) |
| 405 | { |
| 406 | *bm2 = bm->cache[2].bm2; |
| 407 | bm_cache_rotate(bm->cache, 3); |
| 408 | return True; |
| 409 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 410 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 411 | #if DRD_BITMAP_N_CACHE_ELEM >= 4 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 412 | if (a1 == bm->cache[3].a1) |
| 413 | { |
| 414 | *bm2 = bm->cache[3].bm2; |
| 415 | bm_cache_rotate(bm->cache, 4); |
| 416 | return True; |
| 417 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 418 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 419 | #if DRD_BITMAP_N_CACHE_ELEM >= 5 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 420 | if (a1 == bm->cache[4].a1) |
| 421 | { |
| 422 | *bm2 = bm->cache[4].bm2; |
| 423 | bm_cache_rotate(bm->cache, 5); |
| 424 | return True; |
| 425 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 426 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 427 | #if DRD_BITMAP_N_CACHE_ELEM >= 6 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 428 | if (a1 == bm->cache[5].a1) |
| 429 | { |
| 430 | *bm2 = bm->cache[5].bm2; |
| 431 | bm_cache_rotate(bm->cache, 6); |
| 432 | return True; |
| 433 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 434 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 435 | #if DRD_BITMAP_N_CACHE_ELEM >= 7 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 436 | if (a1 == bm->cache[6].a1) |
| 437 | { |
| 438 | *bm2 = bm->cache[6].bm2; |
| 439 | bm_cache_rotate(bm->cache, 7); |
| 440 | return True; |
| 441 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 442 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 443 | #if DRD_BITMAP_N_CACHE_ELEM >= 8 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 444 | if (a1 == bm->cache[7].a1) |
| 445 | { |
| 446 | *bm2 = bm->cache[7].bm2; |
| 447 | bm_cache_rotate(bm->cache, 8); |
| 448 | return True; |
| 449 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 450 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 451 | *bm2 = 0; |
| 452 | return False; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static __inline__ |
| 456 | void bm_update_cache(struct bitmap* const bm, |
| 457 | const UWord a1, |
| 458 | struct bitmap2* const bm2) |
| 459 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 460 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 461 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 462 | #endif |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 463 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 464 | #if DRD_BITMAP_N_CACHE_ELEM > 8 |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 465 | #error Please update the code below. |
| 466 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 467 | #if DRD_BITMAP_N_CACHE_ELEM >= 8 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 468 | bm->cache[7] = bm->cache[6]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 469 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 470 | #if DRD_BITMAP_N_CACHE_ELEM >= 7 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 471 | bm->cache[6] = bm->cache[5]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 472 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 473 | #if DRD_BITMAP_N_CACHE_ELEM >= 6 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 474 | bm->cache[5] = bm->cache[4]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 475 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 476 | #if DRD_BITMAP_N_CACHE_ELEM >= 5 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 477 | bm->cache[4] = bm->cache[3]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 478 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 479 | #if DRD_BITMAP_N_CACHE_ELEM >= 4 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 480 | bm->cache[3] = bm->cache[2]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 481 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 482 | #if DRD_BITMAP_N_CACHE_ELEM >= 3 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 483 | bm->cache[2] = bm->cache[1]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 484 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 485 | #if DRD_BITMAP_N_CACHE_ELEM >= 2 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 486 | bm->cache[1] = bm->cache[0]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 487 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 488 | bm->cache[0].a1 = a1; |
| 489 | bm->cache[0].bm2 = bm2; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 490 | } |
| 491 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 492 | /** |
| 493 | * Look up the address a1 in bitmap bm and return a pointer to a potentially |
| 494 | * shared second level bitmap. The bitmap where the returned pointer points |
| 495 | * at may not be modified by the caller. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 496 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 497 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
| 498 | * @param bm bitmap pointer. |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 499 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 500 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 501 | const struct bitmap2* bm2_lookup(struct bitmap* const bm, const UWord a1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 502 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 503 | struct bitmap2* bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 504 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 505 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 506 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 507 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 508 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 509 | if (! bm_cache_lookup(bm, a1, &bm2)) |
| 510 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 511 | bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1); |
| 512 | bm_update_cache(bm, a1, bm2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 513 | } |
| 514 | return bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 515 | } |
| 516 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 517 | /** |
| 518 | * Look up the address a1 in bitmap bm and return a pointer to a second |
| 519 | * level bitmap that is not shared and hence may be modified. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 520 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 521 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
| 522 | * @param bm bitmap pointer. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 523 | */ |
| 524 | static __inline__ |
| 525 | struct bitmap2* |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 526 | bm2_lookup_exclusive(struct bitmap* const bm, const UWord a1) |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 527 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 528 | struct bitmap2* bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 529 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 530 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 531 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 532 | #endif |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 533 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 534 | if (! bm_cache_lookup(bm, a1, &bm2)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 535 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 536 | bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 537 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 538 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 539 | return bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 540 | } |
| 541 | |
bart | 6584b69 | 2009-06-21 10:11:15 +0000 | [diff] [blame] | 542 | /** Clear the content of the second-level bitmap. */ |
| 543 | static __inline__ |
| 544 | void bm2_clear(struct bitmap2* const bm2) |
| 545 | { |
| 546 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 547 | tl_assert(bm2); |
| 548 | #endif |
| 549 | VG_(memset)(&bm2->bm1, 0, sizeof(bm2->bm1)); |
| 550 | } |
| 551 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 552 | /** |
| 553 | * Insert an uninitialized second level bitmap for the address a1. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 554 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 555 | * @param bm bitmap pointer. |
| 556 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
| 557 | * |
| 558 | * @note bitmap2::recalc isn't initialized here on purpose. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 559 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 560 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 561 | struct bitmap2* bm2_insert(struct bitmap* const bm, const UWord a1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 562 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 563 | struct bitmap2* bm2; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 564 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 565 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 566 | tl_assert(bm); |
| 567 | #endif |
| 568 | |
| 569 | s_bitmap2_creation_count++; |
| 570 | |
| 571 | bm2 = VG_(OSetGen_AllocNode)(bm->oset, sizeof(*bm2)); |
| 572 | bm2->addr = a1; |
| 573 | VG_(OSetGen_Insert)(bm->oset, bm2); |
| 574 | |
| 575 | bm_update_cache(bm, a1, bm2); |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 576 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 577 | return bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 578 | } |
| 579 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 580 | static __inline__ |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 581 | struct bitmap2* bm2_insert_copy(struct bitmap* const bm, |
| 582 | struct bitmap2* const bm2) |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 583 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 584 | struct bitmap2* bm2_copy; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 585 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 586 | bm2_copy = bm2_insert(bm, bm2->addr); |
| 587 | VG_(memcpy)(&bm2_copy->bm1, &bm2->bm1, sizeof(bm2->bm1)); |
| 588 | return bm2_copy; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 589 | } |
| 590 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 591 | /** |
| 592 | * Look up the address a1 in bitmap bm, and insert it if not found. |
| 593 | * The returned second level bitmap may not be modified. |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 594 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 595 | * @param bm bitmap pointer. |
| 596 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 597 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 598 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 599 | struct bitmap2* bm2_lookup_or_insert(struct bitmap* const bm, const UWord a1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 600 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 601 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 602 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 603 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 604 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 605 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 606 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 607 | if (bm_cache_lookup(bm, a1, &bm2)) |
| 608 | { |
| 609 | if (bm2 == 0) |
| 610 | { |
| 611 | bm2 = bm2_insert(bm, a1); |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 612 | bm2_clear(bm2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | else |
| 616 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 617 | bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1); |
| 618 | if (! bm2) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 619 | { |
| 620 | bm2 = bm2_insert(bm, a1); |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 621 | bm2_clear(bm2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 622 | } |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 623 | bm_update_cache(bm, a1, bm2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 624 | } |
| 625 | return bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 626 | } |
| 627 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 628 | /** |
| 629 | * Look up the address a1 in bitmap bm, and insert it if not found. |
| 630 | * The returned second level bitmap may be modified. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 631 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 632 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
| 633 | * @param bm bitmap pointer. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 634 | */ |
| 635 | static __inline__ |
| 636 | struct bitmap2* bm2_lookup_or_insert_exclusive(struct bitmap* const bm, |
| 637 | const UWord a1) |
| 638 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 639 | return bm2_lookup_or_insert(bm, a1); |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 640 | } |
| 641 | |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 642 | static __inline__ |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 643 | void bm2_remove(struct bitmap* const bm, const UWord a1) |
| 644 | { |
| 645 | struct bitmap2* bm2; |
| 646 | |
| 647 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 648 | tl_assert(bm); |
| 649 | #endif |
| 650 | |
| 651 | bm2 = VG_(OSetGen_Remove)(bm->oset, &a1); |
| 652 | VG_(OSetGen_FreeNode)(bm->oset, bm2); |
| 653 | |
| 654 | bm_update_cache(bm, a1, NULL); |
| 655 | } |
| 656 | |
| 657 | static __inline__ |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 658 | void bm_access_aligned_load(struct bitmap* const bm, |
| 659 | const Addr a1, const SizeT size) |
| 660 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 661 | struct bitmap2* bm2; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 662 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 663 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 664 | tl_assert(bm); |
| 665 | #endif |
| 666 | |
| 667 | bm2 = bm2_lookup_or_insert_exclusive(bm, address_msb(a1)); |
| 668 | bm0_set_range(bm2->bm1.bm0_r, |
| 669 | (a1 >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK, |
| 670 | SCALED_SIZE(size)); |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static __inline__ |
| 674 | void bm_access_aligned_store(struct bitmap* const bm, |
| 675 | const Addr a1, const SizeT size) |
| 676 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 677 | struct bitmap2* bm2; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 678 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 679 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 680 | tl_assert(bm); |
| 681 | #endif |
| 682 | |
| 683 | bm2 = bm2_lookup_or_insert_exclusive(bm, address_msb(a1)); |
| 684 | bm0_set_range(bm2->bm1.bm0_w, |
| 685 | (a1 >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK, |
| 686 | SCALED_SIZE(size)); |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 690 | Bool bm_aligned_load_has_conflict_with(struct bitmap* const bm, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 691 | const Addr a, const SizeT size) |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 692 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 693 | const struct bitmap2* bm2; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 694 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 695 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 696 | tl_assert(bm); |
| 697 | #endif |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 698 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 699 | bm2 = bm2_lookup(bm, address_msb(a)); |
| 700 | return (bm2 |
| 701 | && bm0_is_any_set(bm2->bm1.bm0_w, |
| 702 | address_lsb(a), |
| 703 | SCALED_SIZE(size))); |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 707 | Bool bm_aligned_store_has_conflict_with(struct bitmap* const bm, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 708 | const Addr a, const SizeT size) |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 709 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 710 | const struct bitmap2* bm2; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 711 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 712 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 713 | tl_assert(bm); |
| 714 | #endif |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 715 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 716 | bm2 = bm2_lookup(bm, address_msb(a)); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 717 | if (bm2) |
| 718 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 719 | if (bm0_is_any_set(bm2->bm1.bm0_r, address_lsb(a), SCALED_SIZE(size)) |
| 720 | | bm0_is_any_set(bm2->bm1.bm0_w, address_lsb(a), SCALED_SIZE(size))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 721 | { |
| 722 | return True; |
| 723 | } |
| 724 | } |
| 725 | return False; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 726 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 727 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 728 | #endif /* __DRD_BITMAP_H */ |