blob: 372c7873262f3e14658471bc17e67f6059219ca1 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
bart922304f2011-03-13 12:02:44 +00004 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00005
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
bart33e56c92008-03-24 06:41:30 +000025#ifndef __DRD_BITMAP_H
26#define __DRD_BITMAP_H
sewardjaf44c822007-11-25 14:01:38 +000027
28
bart7b706b32009-05-10 06:55:39 +000029#include "pub_drd_bitmap.h"
bart82a0c5d2009-02-14 14:49:23 +000030#include "pub_tool_basics.h"
sewardjaf44c822007-11-25 14:01:38 +000031#include "pub_tool_oset.h"
bartd59bb0f2008-06-08 08:08:31 +000032#include "pub_tool_libcbase.h"
bart67cb4fb2010-09-02 14:43:50 +000033#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
34#include "pub_tool_libcassert.h"
35#endif
sewardjaf44c822007-11-25 14:01:38 +000036
37
bart7b706b32009-05-10 06:55:39 +000038/* 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 * ------------------------------------------------------
bart31b983d2010-02-21 14:52:59 +000046 * | Address MSB | Address LSB | Ignored bits |
bart7b706b32009-05-10 06:55:39 +000047 * ------------------------------------------------------
48 * | Address MSB | UWord MSB | UWord LSB | Ignored bits |
49 * ------------------------------------------------------
50 */
sewardjaf44c822007-11-25 14:01:38 +000051
52
sewardjaf44c822007-11-25 14:01:38 +000053
bart7b706b32009-05-10 06:55:39 +000054/* Address MSB / LSB split. */
sewardjaf44c822007-11-25 14:01:38 +000055
sewardjaf44c822007-11-25 14:01:38 +000056
bart7b706b32009-05-10 06:55:39 +000057/** 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)
sewardjaf44c822007-11-25 14:01:38 +000061
bart8f822af2009-06-08 18:20:42 +000062/**
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.
bart7b706b32009-05-10 06:55:39 +000066 */
67#define SCALED_SIZE(a) \
68 (((((a) - 1U) | ADDR_IGNORED_MASK) + 1U) >> ADDR_IGNORED_BITS)
sewardjaf44c822007-11-25 14:01:38 +000069
bart8f822af2009-06-08 18:20:42 +000070/**
71 * Number of bits assigned to the least significant component of an address.
bart7b706b32009-05-10 06:55:39 +000072 */
73#define ADDR_LSB_BITS 12
sewardjaf44c822007-11-25 14:01:38 +000074
bart8f822af2009-06-08 18:20:42 +000075/**
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.
bart7b706b32009-05-10 06:55:39 +000079 */
80#define ADDR_LSB_MASK (((UWord)1 << ADDR_LSB_BITS) - 1U)
81
82/** Compute least significant bits of an address of type Addr. */
83static __inline__
84UWord 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 */
91static __inline__
92Addr 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 */
101static __inline__
102Addr 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. */
108static __inline__
109UWord address_msb(const Addr a)
110{ return a >> (ADDR_LSB_BITS + ADDR_IGNORED_BITS); }
111
112static __inline__
113Addr 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
bart8f822af2009-06-08 18:20:42 +0000119/**
120 * Convert LSB and MSB back into an address.
bart7b706b32009-05-10 06:55:39 +0000121 *
bart8f822af2009-06-08 18:20:42 +0000122 * @note It is assumed that sizeof(Addr) == sizeof(UWord).
bart7b706b32009-05-10 06:55:39 +0000123 */
124static __inline__
125Addr 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
bart8f822af2009-06-08 18:20:42 +0000135/** Number of bits that fit in a variable of type UWord. */
bart7b706b32009-05-10 06:55:39 +0000136#define BITS_PER_UWORD (8U * sizeof(UWord))
137
bart8f822af2009-06-08 18:20:42 +0000138/** Log2 of BITS_PER_UWORD. */
sewardj4cb6bf72010-01-01 18:31:41 +0000139#if defined(VGA_x86) || defined(VGA_ppc32) || defined(VGA_arm)
sewardjaf44c822007-11-25 14:01:38 +0000140#define BITS_PER_BITS_PER_UWORD 5
sewardjb5b87402011-03-07 16:05:35 +0000141#elif defined(VGA_amd64) || defined(VGA_ppc64) || defined(VGA_s390x)
sewardjaf44c822007-11-25 14:01:38 +0000142#define BITS_PER_BITS_PER_UWORD 6
143#else
144#error Unknown platform.
145#endif
146
bart8f822af2009-06-08 18:20:42 +0000147/** Number of UWord's needed to store one bit per address LSB. */
bart7b706b32009-05-10 06:55:39 +0000148#define BITMAP1_UWORD_COUNT (1U << (ADDR_LSB_BITS - BITS_PER_BITS_PER_UWORD))
sewardjaf44c822007-11-25 14:01:38 +0000149
bart8f822af2009-06-08 18:20:42 +0000150/**
151 * Mask that has to be applied to an (Addr >> ADDR_IGNORED_BITS) expression
152 * in order to compute the least significant part of an UWord.
bart7b706b32009-05-10 06:55:39 +0000153 */
154#define UWORD_LSB_MASK (((UWord)1 << BITS_PER_BITS_PER_UWORD) - 1)
sewardjaf44c822007-11-25 14:01:38 +0000155
bart8f822af2009-06-08 18:20:42 +0000156/**
157 * Compute index into bm0[] array.
bart7b706b32009-05-10 06:55:39 +0000158 *
bart8f822af2009-06-08 18:20:42 +0000159 * @param a Address shifted right ADDR_IGNORED_BITS bits.
bart7b706b32009-05-10 06:55:39 +0000160 */
161static __inline__
162UWord uword_msb(const UWord a)
163{
164#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
165 tl_assert(a < (1U << ADDR_LSB_BITS));
166#endif
167 return a >> BITS_PER_BITS_PER_UWORD;
168}
sewardjaf44c822007-11-25 14:01:38 +0000169
bart8f822af2009-06-08 18:20:42 +0000170/**
171 * Return the least significant bits.
bart7b706b32009-05-10 06:55:39 +0000172 *
bart8f822af2009-06-08 18:20:42 +0000173 * @param a Address shifted right ADDR_IGNORED_BITS bits.
bart7b706b32009-05-10 06:55:39 +0000174 */
175static __inline__
176UWord uword_lsb(const UWord a)
177{
178#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
179 tl_assert(a < (1U << ADDR_LSB_BITS));
180#endif
181 return a & UWORD_LSB_MASK;
182}
183
bart8f822af2009-06-08 18:20:42 +0000184/**
185 * Compute the highest address lower than a for which
186 * uword_lsb(address_lsb(a)) == 0.
bart7b706b32009-05-10 06:55:39 +0000187 *
bart8f822af2009-06-08 18:20:42 +0000188 * @param a Address.
bart7b706b32009-05-10 06:55:39 +0000189 */
190static __inline__
191Addr first_address_with_same_uword_lsb(const Addr a)
192{
193 return (a & (~UWORD_LSB_MASK << ADDR_IGNORED_BITS));
194}
195
196/**
197 * First address that will go in the UWord past the one 'a' goes in.
198 *
199 * @param a Address.
200 */
201static __inline__
202Addr first_address_with_higher_uword_msb(const Addr a)
203{
204 return ((a | ((UWORD_LSB_MASK << ADDR_IGNORED_BITS) | ADDR_IGNORED_MASK))
205 + 1);
206}
207
sewardjaf44c822007-11-25 14:01:38 +0000208
209
bart33e56c92008-03-24 06:41:30 +0000210/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +0000211
212static ULong s_bitmap2_creation_count;
213
214
bart0008f5b2008-03-22 17:07:39 +0000215
216/*********************************************************************/
217/* Functions for manipulating a struct bitmap1. */
218/*********************************************************************/
219
220
bart7b706b32009-05-10 06:55:39 +0000221/* Lowest level, corresponding to the lowest ADDR_LSB_BITS of an address. */
sewardjaf44c822007-11-25 14:01:38 +0000222struct bitmap1
223{
bartbedfd232009-03-26 19:07:15 +0000224 UWord bm0_r[BITMAP1_UWORD_COUNT];
225 UWord bm0_w[BITMAP1_UWORD_COUNT];
sewardjaf44c822007-11-25 14:01:38 +0000226};
227
bart7b706b32009-05-10 06:55:39 +0000228static __inline__ UWord bm0_mask(const UWord a)
sewardjaf44c822007-11-25 14:01:38 +0000229{
bart3c9989f2008-06-11 19:17:01 +0000230#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000231 tl_assert(address_msb(make_address(0, a)) == 0);
bart3c9989f2008-06-11 19:17:01 +0000232#endif
bart7b706b32009-05-10 06:55:39 +0000233 return ((UWord)1 << uword_lsb(a));
barta79df6e2008-03-14 17:07:51 +0000234}
235
bart7b706b32009-05-10 06:55:39 +0000236/** Set the bit corresponding to address a in bitmap bm0. */
237static __inline__ void bm0_set(UWord* bm0, const UWord a)
238{
239#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
240 tl_assert(address_msb(make_address(0, a)) == 0);
241#endif
242 bm0[uword_msb(a)] |= (UWord)1 << uword_lsb(a);
243}
244
245/**
246 * Set the bits corresponding to all of the addresses in range
247 * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [
248 * in bitmap bm0.
249 */
bartf8bc71d2008-03-15 11:42:34 +0000250static __inline__ void bm0_set_range(UWord* bm0,
bart7b706b32009-05-10 06:55:39 +0000251 const UWord a, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000252{
bart8b4b2ee2008-06-11 13:17:56 +0000253#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000254 tl_assert(size > 0);
bart7b706b32009-05-10 06:55:39 +0000255 tl_assert(address_msb(make_address(0, a)) == 0);
256 tl_assert(address_msb(make_address(0, a + size - 1)) == 0);
257 tl_assert(uword_msb(a) == uword_msb(a + size - 1));
barta79df6e2008-03-14 17:07:51 +0000258#endif
bart7b706b32009-05-10 06:55:39 +0000259 bm0[uword_msb(a)]
260 |= (((UWord)1 << size) - 1) << uword_lsb(a);
sewardjaf44c822007-11-25 14:01:38 +0000261}
262
bart7b706b32009-05-10 06:55:39 +0000263/** Clear the bit corresponding to address a in bitmap bm0. */
264static __inline__ void bm0_clear(UWord* bm0, const UWord a)
sewardjaf44c822007-11-25 14:01:38 +0000265{
bart3c9989f2008-06-11 19:17:01 +0000266#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000267 tl_assert(address_msb(make_address(0, a)) == 0);
bart3c9989f2008-06-11 19:17:01 +0000268#endif
bart7b706b32009-05-10 06:55:39 +0000269 bm0[uword_msb(a)] &= ~((UWord)1 << uword_lsb(a));
sewardjaf44c822007-11-25 14:01:38 +0000270}
271
bart7b706b32009-05-10 06:55:39 +0000272/**
273 * Clear all of the addresses in range
274 * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [
275 * in bitmap bm0.
276 */
bart5955f332008-03-25 17:19:20 +0000277static __inline__ void bm0_clear_range(UWord* bm0,
bart7b706b32009-05-10 06:55:39 +0000278 const UWord a, const SizeT size)
bart5955f332008-03-25 17:19:20 +0000279{
bart8b4b2ee2008-06-11 13:17:56 +0000280#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000281 tl_assert(address_msb(make_address(0, a)) == 0);
282 tl_assert(size == 0 || address_msb(make_address(0, a + size - 1)) == 0);
283 tl_assert(size == 0 || uword_msb(a) == uword_msb(a + size - 1));
bart5955f332008-03-25 17:19:20 +0000284#endif
bart8c046732009-04-25 06:52:01 +0000285 /*
bart31b983d2010-02-21 14:52:59 +0000286 * Note: although the expression below yields a correct result even if
bart8c046732009-04-25 06:52:01 +0000287 * size == 0, do not touch bm0[] if size == 0 because this might otherwise
288 * cause an access of memory just past the end of the bm0[] array.
289 */
290 if (size > 0)
291 {
bart7b706b32009-05-10 06:55:39 +0000292 bm0[uword_msb(a)]
293 &= ~((((UWord)1 << size) - 1) << uword_lsb(a));
bart8c046732009-04-25 06:52:01 +0000294 }
bart5955f332008-03-25 17:19:20 +0000295}
296
bart7b706b32009-05-10 06:55:39 +0000297/** Test whether the bit corresponding to address a is set in bitmap bm0. */
298static __inline__ UWord bm0_is_set(const UWord* bm0, const UWord a)
sewardjaf44c822007-11-25 14:01:38 +0000299{
bart3c9989f2008-06-11 19:17:01 +0000300#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000301 tl_assert(address_msb(make_address(0, a)) == 0);
bart3c9989f2008-06-11 19:17:01 +0000302#endif
bart7b706b32009-05-10 06:55:39 +0000303 return (bm0[uword_msb(a)] & ((UWord)1 << uword_lsb(a)));
sewardjaf44c822007-11-25 14:01:38 +0000304}
305
bart7b706b32009-05-10 06:55:39 +0000306/**
307 * Return true if a bit corresponding to any of the addresses in range
308 * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [
309 * is set in bm0.
310 */
barta79df6e2008-03-14 17:07:51 +0000311static __inline__ UWord bm0_is_any_set(const UWord* bm0,
bart7b706b32009-05-10 06:55:39 +0000312 const Addr a, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000313{
bart8b4b2ee2008-06-11 13:17:56 +0000314#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000315 tl_assert(size > 0);
bart7b706b32009-05-10 06:55:39 +0000316 tl_assert(address_msb(make_address(0, a)) == 0);
317 tl_assert(address_msb(make_address(0, a + size - 1)) == 0);
318 tl_assert(uword_msb(a) == uword_msb(a + size - 1));
barta79df6e2008-03-14 17:07:51 +0000319#endif
bart7b706b32009-05-10 06:55:39 +0000320 return (bm0[uword_msb(a)] & ((((UWord)1 << size) - 1) << uword_lsb(a)));
barta79df6e2008-03-14 17:07:51 +0000321}
sewardjaf44c822007-11-25 14:01:38 +0000322
bartadbdf142008-03-19 17:00:12 +0000323
bart0008f5b2008-03-22 17:07:39 +0000324
325/*********************************************************************/
326/* Functions for manipulating a struct bitmap. */
bartadbdf142008-03-19 17:00:12 +0000327/*********************************************************************/
328
329
bart33e56c92008-03-24 06:41:30 +0000330/* Second level bitmap. */
sewardjaf44c822007-11-25 14:01:38 +0000331struct bitmap2
332{
bart7b706b32009-05-10 06:55:39 +0000333 Addr addr; ///< address_msb(...)
bart8f822af2009-06-08 18:20:42 +0000334 Bool recalc;
bartbedfd232009-03-26 19:07:15 +0000335 struct bitmap1 bm1;
sewardjaf44c822007-11-25 14:01:38 +0000336};
337
bart33e56c92008-03-24 06:41:30 +0000338
bart7b706b32009-05-10 06:55:39 +0000339static void bm2_clear(struct bitmap2* const bm2);
340static __inline__
341struct bitmap2* bm2_insert(struct bitmap* const bm, const UWord a1);
342
343
bartf647d342008-03-24 19:12:12 +0000344
bart8f822af2009-06-08 18:20:42 +0000345/**
346 * Rotate elements cache[0..n-1] such that the element at position n-1 is
347 * moved to position 0. This allows to speed up future cache lookups.
bart7e81a172008-06-09 19:50:51 +0000348 */
349static __inline__
350void bm_cache_rotate(struct bm_cache_elem cache[], const int n)
351{
bart45af5ea2008-06-12 06:04:59 +0000352#if 0
bartbedfd232009-03-26 19:07:15 +0000353 struct bm_cache_elem t;
bart7e81a172008-06-09 19:50:51 +0000354
bartbedfd232009-03-26 19:07:15 +0000355 tl_assert(2 <= n && n <= 8);
bartea2fa4c2008-06-10 06:32:49 +0000356
bartbedfd232009-03-26 19:07:15 +0000357 t = cache[0];
358 if (n > 1)
359 cache[0] = cache[1];
360 if (n > 2)
361 cache[1] = cache[2];
362 if (n > 3)
363 cache[2] = cache[3];
364 if (n > 4)
365 cache[3] = cache[4];
366 if (n > 5)
367 cache[4] = cache[5];
368 if (n > 6)
369 cache[5] = cache[6];
370 if (n > 7)
371 cache[6] = cache[7];
372 cache[n - 1] = t;
bartea2fa4c2008-06-10 06:32:49 +0000373#endif
bart7e81a172008-06-09 19:50:51 +0000374}
bartf647d342008-03-24 19:12:12 +0000375
bartf647d342008-03-24 19:12:12 +0000376static __inline__
bart7e81a172008-06-09 19:50:51 +0000377Bool bm_cache_lookup(struct bitmap* const bm, const UWord a1,
bartf29205e2008-03-25 18:51:06 +0000378 struct bitmap2** bm2)
bart33e56c92008-03-24 06:41:30 +0000379{
bart8b4b2ee2008-06-11 13:17:56 +0000380#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000381 tl_assert(bm);
382 tl_assert(bm2);
bart7e81a172008-06-09 19:50:51 +0000383#endif
bart33e56c92008-03-24 06:41:30 +0000384
bart8f822af2009-06-08 18:20:42 +0000385#if DRD_BITMAP_N_CACHE_ELEM > 8
bart33e56c92008-03-24 06:41:30 +0000386#error Please update the code below.
387#endif
bart8f822af2009-06-08 18:20:42 +0000388#if DRD_BITMAP_N_CACHE_ELEM >= 1
bartbedfd232009-03-26 19:07:15 +0000389 if (a1 == bm->cache[0].a1)
390 {
391 *bm2 = bm->cache[0].bm2;
392 return True;
393 }
bart33e56c92008-03-24 06:41:30 +0000394#endif
bart8f822af2009-06-08 18:20:42 +0000395#if DRD_BITMAP_N_CACHE_ELEM >= 2
bartbedfd232009-03-26 19:07:15 +0000396 if (a1 == bm->cache[1].a1)
397 {
398 *bm2 = bm->cache[1].bm2;
399 return True;
400 }
bart33e56c92008-03-24 06:41:30 +0000401#endif
bart8f822af2009-06-08 18:20:42 +0000402#if DRD_BITMAP_N_CACHE_ELEM >= 3
bartbedfd232009-03-26 19:07:15 +0000403 if (a1 == bm->cache[2].a1)
404 {
405 *bm2 = bm->cache[2].bm2;
406 bm_cache_rotate(bm->cache, 3);
407 return True;
408 }
bart33e56c92008-03-24 06:41:30 +0000409#endif
bart8f822af2009-06-08 18:20:42 +0000410#if DRD_BITMAP_N_CACHE_ELEM >= 4
bartbedfd232009-03-26 19:07:15 +0000411 if (a1 == bm->cache[3].a1)
412 {
413 *bm2 = bm->cache[3].bm2;
414 bm_cache_rotate(bm->cache, 4);
415 return True;
416 }
bart33e56c92008-03-24 06:41:30 +0000417#endif
bart8f822af2009-06-08 18:20:42 +0000418#if DRD_BITMAP_N_CACHE_ELEM >= 5
bartbedfd232009-03-26 19:07:15 +0000419 if (a1 == bm->cache[4].a1)
420 {
421 *bm2 = bm->cache[4].bm2;
422 bm_cache_rotate(bm->cache, 5);
423 return True;
424 }
bart33e56c92008-03-24 06:41:30 +0000425#endif
bart8f822af2009-06-08 18:20:42 +0000426#if DRD_BITMAP_N_CACHE_ELEM >= 6
bartbedfd232009-03-26 19:07:15 +0000427 if (a1 == bm->cache[5].a1)
428 {
429 *bm2 = bm->cache[5].bm2;
430 bm_cache_rotate(bm->cache, 6);
431 return True;
432 }
bart33e56c92008-03-24 06:41:30 +0000433#endif
bart8f822af2009-06-08 18:20:42 +0000434#if DRD_BITMAP_N_CACHE_ELEM >= 7
bartbedfd232009-03-26 19:07:15 +0000435 if (a1 == bm->cache[6].a1)
436 {
437 *bm2 = bm->cache[6].bm2;
438 bm_cache_rotate(bm->cache, 7);
439 return True;
440 }
bart33e56c92008-03-24 06:41:30 +0000441#endif
bart8f822af2009-06-08 18:20:42 +0000442#if DRD_BITMAP_N_CACHE_ELEM >= 8
bartbedfd232009-03-26 19:07:15 +0000443 if (a1 == bm->cache[7].a1)
444 {
445 *bm2 = bm->cache[7].bm2;
446 bm_cache_rotate(bm->cache, 8);
447 return True;
448 }
bart33e56c92008-03-24 06:41:30 +0000449#endif
bartbedfd232009-03-26 19:07:15 +0000450 *bm2 = 0;
451 return False;
bart33e56c92008-03-24 06:41:30 +0000452}
453
454static __inline__
455void bm_update_cache(struct bitmap* const bm,
456 const UWord a1,
457 struct bitmap2* const bm2)
458{
bart3c9989f2008-06-11 19:17:01 +0000459#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000460 tl_assert(bm);
bart3c9989f2008-06-11 19:17:01 +0000461#endif
bart33e56c92008-03-24 06:41:30 +0000462
bart8f822af2009-06-08 18:20:42 +0000463#if DRD_BITMAP_N_CACHE_ELEM > 8
bart33e56c92008-03-24 06:41:30 +0000464#error Please update the code below.
465#endif
bart8f822af2009-06-08 18:20:42 +0000466#if DRD_BITMAP_N_CACHE_ELEM >= 8
bartbedfd232009-03-26 19:07:15 +0000467 bm->cache[7] = bm->cache[6];
bart33e56c92008-03-24 06:41:30 +0000468#endif
bart8f822af2009-06-08 18:20:42 +0000469#if DRD_BITMAP_N_CACHE_ELEM >= 7
bartbedfd232009-03-26 19:07:15 +0000470 bm->cache[6] = bm->cache[5];
bart33e56c92008-03-24 06:41:30 +0000471#endif
bart8f822af2009-06-08 18:20:42 +0000472#if DRD_BITMAP_N_CACHE_ELEM >= 6
bartbedfd232009-03-26 19:07:15 +0000473 bm->cache[5] = bm->cache[4];
bart33e56c92008-03-24 06:41:30 +0000474#endif
bart8f822af2009-06-08 18:20:42 +0000475#if DRD_BITMAP_N_CACHE_ELEM >= 5
bartbedfd232009-03-26 19:07:15 +0000476 bm->cache[4] = bm->cache[3];
bart33e56c92008-03-24 06:41:30 +0000477#endif
bart8f822af2009-06-08 18:20:42 +0000478#if DRD_BITMAP_N_CACHE_ELEM >= 4
bartbedfd232009-03-26 19:07:15 +0000479 bm->cache[3] = bm->cache[2];
bart33e56c92008-03-24 06:41:30 +0000480#endif
bart8f822af2009-06-08 18:20:42 +0000481#if DRD_BITMAP_N_CACHE_ELEM >= 3
bartbedfd232009-03-26 19:07:15 +0000482 bm->cache[2] = bm->cache[1];
bart33e56c92008-03-24 06:41:30 +0000483#endif
bart8f822af2009-06-08 18:20:42 +0000484#if DRD_BITMAP_N_CACHE_ELEM >= 2
bartbedfd232009-03-26 19:07:15 +0000485 bm->cache[1] = bm->cache[0];
bart33e56c92008-03-24 06:41:30 +0000486#endif
bartbedfd232009-03-26 19:07:15 +0000487 bm->cache[0].a1 = a1;
488 bm->cache[0].bm2 = bm2;
bart33e56c92008-03-24 06:41:30 +0000489}
490
bart8f822af2009-06-08 18:20:42 +0000491/**
492 * Look up the address a1 in bitmap bm and return a pointer to a potentially
493 * shared second level bitmap. The bitmap where the returned pointer points
494 * at may not be modified by the caller.
bartf647d342008-03-24 19:12:12 +0000495 *
bart8f822af2009-06-08 18:20:42 +0000496 * @param a1 client address shifted right by ADDR_LSB_BITS.
497 * @param bm bitmap pointer.
bart0008f5b2008-03-22 17:07:39 +0000498 */
sewardjaf44c822007-11-25 14:01:38 +0000499static __inline__
bart7e81a172008-06-09 19:50:51 +0000500const struct bitmap2* bm2_lookup(struct bitmap* const bm, const UWord a1)
sewardjaf44c822007-11-25 14:01:38 +0000501{
bart7b706b32009-05-10 06:55:39 +0000502 struct bitmap2* bm2;
bartf647d342008-03-24 19:12:12 +0000503
bart3c9989f2008-06-11 19:17:01 +0000504#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000505 tl_assert(bm);
bart3c9989f2008-06-11 19:17:01 +0000506#endif
bart7b706b32009-05-10 06:55:39 +0000507
bartbedfd232009-03-26 19:07:15 +0000508 if (! bm_cache_lookup(bm, a1, &bm2))
509 {
bart7b706b32009-05-10 06:55:39 +0000510 bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1);
511 bm_update_cache(bm, a1, bm2);
bartbedfd232009-03-26 19:07:15 +0000512 }
513 return bm2;
bartf647d342008-03-24 19:12:12 +0000514}
515
bart8f822af2009-06-08 18:20:42 +0000516/**
517 * Look up the address a1 in bitmap bm and return a pointer to a second
518 * level bitmap that is not shared and hence may be modified.
bartf647d342008-03-24 19:12:12 +0000519 *
bart8f822af2009-06-08 18:20:42 +0000520 * @param a1 client address shifted right by ADDR_LSB_BITS.
521 * @param bm bitmap pointer.
bartf647d342008-03-24 19:12:12 +0000522 */
523static __inline__
524struct bitmap2*
bart7e81a172008-06-09 19:50:51 +0000525bm2_lookup_exclusive(struct bitmap* const bm, const UWord a1)
bartf647d342008-03-24 19:12:12 +0000526{
bartbedfd232009-03-26 19:07:15 +0000527 struct bitmap2* bm2;
bartf647d342008-03-24 19:12:12 +0000528
bart3c9989f2008-06-11 19:17:01 +0000529#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000530 tl_assert(bm);
bart3c9989f2008-06-11 19:17:01 +0000531#endif
bartf647d342008-03-24 19:12:12 +0000532
bart7b706b32009-05-10 06:55:39 +0000533 if (! bm_cache_lookup(bm, a1, &bm2))
bartbedfd232009-03-26 19:07:15 +0000534 {
bart7b706b32009-05-10 06:55:39 +0000535 bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1);
bartbedfd232009-03-26 19:07:15 +0000536 }
bartf647d342008-03-24 19:12:12 +0000537
bartbedfd232009-03-26 19:07:15 +0000538 return bm2;
sewardjaf44c822007-11-25 14:01:38 +0000539}
540
bart6584b692009-06-21 10:11:15 +0000541/** Clear the content of the second-level bitmap. */
542static __inline__
543void bm2_clear(struct bitmap2* const bm2)
544{
545#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
546 tl_assert(bm2);
547#endif
548 VG_(memset)(&bm2->bm1, 0, sizeof(bm2->bm1));
549}
550
bart8f822af2009-06-08 18:20:42 +0000551/**
552 * Insert an uninitialized second level bitmap for the address a1.
bartf647d342008-03-24 19:12:12 +0000553 *
bart8f822af2009-06-08 18:20:42 +0000554 * @param bm bitmap pointer.
555 * @param a1 client address shifted right by ADDR_LSB_BITS.
556 *
557 * @note bitmap2::recalc isn't initialized here on purpose.
bartf647d342008-03-24 19:12:12 +0000558 */
sewardjaf44c822007-11-25 14:01:38 +0000559static __inline__
bart7e81a172008-06-09 19:50:51 +0000560struct bitmap2* bm2_insert(struct bitmap* const bm, const UWord a1)
sewardjaf44c822007-11-25 14:01:38 +0000561{
bartbedfd232009-03-26 19:07:15 +0000562 struct bitmap2* bm2;
bart33e56c92008-03-24 06:41:30 +0000563
bart7b706b32009-05-10 06:55:39 +0000564#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
565 tl_assert(bm);
566#endif
567
568 s_bitmap2_creation_count++;
569
570 bm2 = VG_(OSetGen_AllocNode)(bm->oset, sizeof(*bm2));
571 bm2->addr = a1;
572 VG_(OSetGen_Insert)(bm->oset, bm2);
573
574 bm_update_cache(bm, a1, bm2);
bartf647d342008-03-24 19:12:12 +0000575
bartbedfd232009-03-26 19:07:15 +0000576 return bm2;
bartf647d342008-03-24 19:12:12 +0000577}
578
bartf647d342008-03-24 19:12:12 +0000579static __inline__
bart7b706b32009-05-10 06:55:39 +0000580struct bitmap2* bm2_insert_copy(struct bitmap* const bm,
581 struct bitmap2* const bm2)
bartf647d342008-03-24 19:12:12 +0000582{
bart7b706b32009-05-10 06:55:39 +0000583 struct bitmap2* bm2_copy;
bartf647d342008-03-24 19:12:12 +0000584
bart7b706b32009-05-10 06:55:39 +0000585 bm2_copy = bm2_insert(bm, bm2->addr);
586 VG_(memcpy)(&bm2_copy->bm1, &bm2->bm1, sizeof(bm2->bm1));
587 return bm2_copy;
sewardjaf44c822007-11-25 14:01:38 +0000588}
589
bart8f822af2009-06-08 18:20:42 +0000590/**
591 * Look up the address a1 in bitmap bm, and insert it if not found.
592 * The returned second level bitmap may not be modified.
bart0008f5b2008-03-22 17:07:39 +0000593 *
bart8f822af2009-06-08 18:20:42 +0000594 * @param bm bitmap pointer.
595 * @param a1 client address shifted right by ADDR_LSB_BITS.
bart0008f5b2008-03-22 17:07:39 +0000596 */
sewardjaf44c822007-11-25 14:01:38 +0000597static __inline__
bart7e81a172008-06-09 19:50:51 +0000598struct bitmap2* bm2_lookup_or_insert(struct bitmap* const bm, const UWord a1)
sewardjaf44c822007-11-25 14:01:38 +0000599{
bartbedfd232009-03-26 19:07:15 +0000600 struct bitmap2* bm2;
barta79df6e2008-03-14 17:07:51 +0000601
bart3c9989f2008-06-11 19:17:01 +0000602#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000603 tl_assert(bm);
bart3c9989f2008-06-11 19:17:01 +0000604#endif
bart7b706b32009-05-10 06:55:39 +0000605
bartbedfd232009-03-26 19:07:15 +0000606 if (bm_cache_lookup(bm, a1, &bm2))
607 {
608 if (bm2 == 0)
609 {
610 bm2 = bm2_insert(bm, a1);
bart7b706b32009-05-10 06:55:39 +0000611 bm2_clear(bm2);
bartbedfd232009-03-26 19:07:15 +0000612 }
613 }
614 else
615 {
bart7b706b32009-05-10 06:55:39 +0000616 bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1);
617 if (! bm2)
bartbedfd232009-03-26 19:07:15 +0000618 {
619 bm2 = bm2_insert(bm, a1);
bart7b706b32009-05-10 06:55:39 +0000620 bm2_clear(bm2);
bartbedfd232009-03-26 19:07:15 +0000621 }
bart7b706b32009-05-10 06:55:39 +0000622 bm_update_cache(bm, a1, bm2);
bartbedfd232009-03-26 19:07:15 +0000623 }
624 return bm2;
sewardjaf44c822007-11-25 14:01:38 +0000625}
626
bart8f822af2009-06-08 18:20:42 +0000627/**
628 * Look up the address a1 in bitmap bm, and insert it if not found.
629 * The returned second level bitmap may be modified.
bartf647d342008-03-24 19:12:12 +0000630 *
bart8f822af2009-06-08 18:20:42 +0000631 * @param a1 client address shifted right by ADDR_LSB_BITS.
632 * @param bm bitmap pointer.
bartf647d342008-03-24 19:12:12 +0000633 */
634static __inline__
635struct bitmap2* bm2_lookup_or_insert_exclusive(struct bitmap* const bm,
636 const UWord a1)
637{
bart7b706b32009-05-10 06:55:39 +0000638 return bm2_lookup_or_insert(bm, a1);
bartf647d342008-03-24 19:12:12 +0000639}
640
bartd59bb0f2008-06-08 08:08:31 +0000641static __inline__
bart8f822af2009-06-08 18:20:42 +0000642void bm2_remove(struct bitmap* const bm, const UWord a1)
643{
644 struct bitmap2* bm2;
645
646#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
647 tl_assert(bm);
648#endif
649
650 bm2 = VG_(OSetGen_Remove)(bm->oset, &a1);
651 VG_(OSetGen_FreeNode)(bm->oset, bm2);
652
653 bm_update_cache(bm, a1, NULL);
654}
655
656static __inline__
bartd59bb0f2008-06-08 08:08:31 +0000657void bm_access_aligned_load(struct bitmap* const bm,
658 const Addr a1, const SizeT size)
659{
bartbedfd232009-03-26 19:07:15 +0000660 struct bitmap2* bm2;
bartd59bb0f2008-06-08 08:08:31 +0000661
bart7b706b32009-05-10 06:55:39 +0000662#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
663 tl_assert(bm);
664#endif
665
666 bm2 = bm2_lookup_or_insert_exclusive(bm, address_msb(a1));
667 bm0_set_range(bm2->bm1.bm0_r,
668 (a1 >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK,
669 SCALED_SIZE(size));
bartd59bb0f2008-06-08 08:08:31 +0000670}
671
672static __inline__
673void bm_access_aligned_store(struct bitmap* const bm,
674 const Addr a1, const SizeT size)
675{
bartbedfd232009-03-26 19:07:15 +0000676 struct bitmap2* bm2;
bartd59bb0f2008-06-08 08:08:31 +0000677
bart7b706b32009-05-10 06:55:39 +0000678#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
679 tl_assert(bm);
680#endif
681
682 bm2 = bm2_lookup_or_insert_exclusive(bm, address_msb(a1));
683 bm0_set_range(bm2->bm1.bm0_w,
684 (a1 >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK,
685 SCALED_SIZE(size));
bartd59bb0f2008-06-08 08:08:31 +0000686}
687
688static __inline__
bart7e81a172008-06-09 19:50:51 +0000689Bool bm_aligned_load_has_conflict_with(struct bitmap* const bm,
bart7b706b32009-05-10 06:55:39 +0000690 const Addr a, const SizeT size)
bartd59bb0f2008-06-08 08:08:31 +0000691{
bartbedfd232009-03-26 19:07:15 +0000692 const struct bitmap2* bm2;
bartd59bb0f2008-06-08 08:08:31 +0000693
bart7b706b32009-05-10 06:55:39 +0000694#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
695 tl_assert(bm);
696#endif
bartd59bb0f2008-06-08 08:08:31 +0000697
bart7b706b32009-05-10 06:55:39 +0000698 bm2 = bm2_lookup(bm, address_msb(a));
699 return (bm2
700 && bm0_is_any_set(bm2->bm1.bm0_w,
701 address_lsb(a),
702 SCALED_SIZE(size)));
bartd59bb0f2008-06-08 08:08:31 +0000703}
704
705static __inline__
bart7e81a172008-06-09 19:50:51 +0000706Bool bm_aligned_store_has_conflict_with(struct bitmap* const bm,
bart7b706b32009-05-10 06:55:39 +0000707 const Addr a, const SizeT size)
bartd59bb0f2008-06-08 08:08:31 +0000708{
bartbedfd232009-03-26 19:07:15 +0000709 const struct bitmap2* bm2;
bartd59bb0f2008-06-08 08:08:31 +0000710
bart7b706b32009-05-10 06:55:39 +0000711#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
712 tl_assert(bm);
713#endif
bartd59bb0f2008-06-08 08:08:31 +0000714
bart7b706b32009-05-10 06:55:39 +0000715 bm2 = bm2_lookup(bm, address_msb(a));
bartbedfd232009-03-26 19:07:15 +0000716 if (bm2)
717 {
bart7b706b32009-05-10 06:55:39 +0000718 if (bm0_is_any_set(bm2->bm1.bm0_r, address_lsb(a), SCALED_SIZE(size))
719 | bm0_is_any_set(bm2->bm1.bm0_w, address_lsb(a), SCALED_SIZE(size)))
bartbedfd232009-03-26 19:07:15 +0000720 {
721 return True;
722 }
723 }
724 return False;
bartd59bb0f2008-06-08 08:08:31 +0000725}
sewardjaf44c822007-11-25 14:01:38 +0000726
bart33e56c92008-03-24 06:41:30 +0000727#endif /* __DRD_BITMAP_H */