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