blob: 488d895338fe77cfbd2b32a76ffcdfb18011d08d [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
Elliott Hughesed398002017-06-21 14:41:24 -07004 Copyright (C) 2006-2017 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. */
sewardj5db15402012-06-07 09:13:21 +0000139#if defined(VGA_x86) || defined(VGA_ppc32) || defined(VGA_arm) \
140 || defined(VGA_mips32)
sewardjaf44c822007-11-25 14:01:38 +0000141#define BITS_PER_BITS_PER_UWORD 5
carllcae0cc22014-08-07 23:17:29 +0000142#elif defined(VGA_amd64) || defined(VGA_ppc64be) || defined(VGA_ppc64le) \
Elliott Hughesed398002017-06-21 14:41:24 -0700143 || defined(VGA_s390x) || defined(VGA_mips64) || defined(VGA_arm64)
sewardjaf44c822007-11-25 14:01:38 +0000144#define BITS_PER_BITS_PER_UWORD 6
145#else
146#error Unknown platform.
147#endif
148
bart8f822af2009-06-08 18:20:42 +0000149/** Number of UWord's needed to store one bit per address LSB. */
bart7b706b32009-05-10 06:55:39 +0000150#define BITMAP1_UWORD_COUNT (1U << (ADDR_LSB_BITS - BITS_PER_BITS_PER_UWORD))
sewardjaf44c822007-11-25 14:01:38 +0000151
bart8f822af2009-06-08 18:20:42 +0000152/**
153 * Mask that has to be applied to an (Addr >> ADDR_IGNORED_BITS) expression
154 * in order to compute the least significant part of an UWord.
bart7b706b32009-05-10 06:55:39 +0000155 */
156#define UWORD_LSB_MASK (((UWord)1 << BITS_PER_BITS_PER_UWORD) - 1)
sewardjaf44c822007-11-25 14:01:38 +0000157
bart8f822af2009-06-08 18:20:42 +0000158/**
159 * Compute index into bm0[] array.
bart7b706b32009-05-10 06:55:39 +0000160 *
bart8f822af2009-06-08 18:20:42 +0000161 * @param a Address shifted right ADDR_IGNORED_BITS bits.
bart7b706b32009-05-10 06:55:39 +0000162 */
163static __inline__
164UWord uword_msb(const UWord a)
165{
166#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
167 tl_assert(a < (1U << ADDR_LSB_BITS));
168#endif
169 return a >> BITS_PER_BITS_PER_UWORD;
170}
sewardjaf44c822007-11-25 14:01:38 +0000171
bart8f822af2009-06-08 18:20:42 +0000172/**
173 * Return the least significant bits.
bart7b706b32009-05-10 06:55:39 +0000174 *
bart8f822af2009-06-08 18:20:42 +0000175 * @param a Address shifted right ADDR_IGNORED_BITS bits.
bart7b706b32009-05-10 06:55:39 +0000176 */
177static __inline__
178UWord uword_lsb(const UWord a)
179{
180#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
181 tl_assert(a < (1U << ADDR_LSB_BITS));
182#endif
183 return a & UWORD_LSB_MASK;
184}
185
bart8f822af2009-06-08 18:20:42 +0000186/**
187 * Compute the highest address lower than a for which
188 * uword_lsb(address_lsb(a)) == 0.
bart7b706b32009-05-10 06:55:39 +0000189 *
bart8f822af2009-06-08 18:20:42 +0000190 * @param a Address.
bart7b706b32009-05-10 06:55:39 +0000191 */
192static __inline__
193Addr first_address_with_same_uword_lsb(const Addr a)
194{
195 return (a & (~UWORD_LSB_MASK << ADDR_IGNORED_BITS));
196}
197
198/**
199 * First address that will go in the UWord past the one 'a' goes in.
200 *
201 * @param a Address.
202 */
203static __inline__
204Addr first_address_with_higher_uword_msb(const Addr a)
205{
206 return ((a | ((UWORD_LSB_MASK << ADDR_IGNORED_BITS) | ADDR_IGNORED_MASK))
207 + 1);
208}
209
sewardjaf44c822007-11-25 14:01:38 +0000210
211
bart33e56c92008-03-24 06:41:30 +0000212/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +0000213
214static ULong s_bitmap2_creation_count;
215
216
bart0008f5b2008-03-22 17:07:39 +0000217
218/*********************************************************************/
219/* Functions for manipulating a struct bitmap1. */
220/*********************************************************************/
221
222
bart7b706b32009-05-10 06:55:39 +0000223/* Lowest level, corresponding to the lowest ADDR_LSB_BITS of an address. */
sewardjaf44c822007-11-25 14:01:38 +0000224struct bitmap1
225{
bartbedfd232009-03-26 19:07:15 +0000226 UWord bm0_r[BITMAP1_UWORD_COUNT];
227 UWord bm0_w[BITMAP1_UWORD_COUNT];
sewardjaf44c822007-11-25 14:01:38 +0000228};
229
bart7b706b32009-05-10 06:55:39 +0000230static __inline__ UWord bm0_mask(const UWord a)
sewardjaf44c822007-11-25 14:01:38 +0000231{
bart3c9989f2008-06-11 19:17:01 +0000232#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000233 tl_assert(address_msb(make_address(0, a)) == 0);
bart3c9989f2008-06-11 19:17:01 +0000234#endif
bart7b706b32009-05-10 06:55:39 +0000235 return ((UWord)1 << uword_lsb(a));
barta79df6e2008-03-14 17:07:51 +0000236}
237
bart7b706b32009-05-10 06:55:39 +0000238/** Set the bit corresponding to address a in bitmap bm0. */
239static __inline__ void bm0_set(UWord* bm0, const UWord a)
240{
241#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
242 tl_assert(address_msb(make_address(0, a)) == 0);
243#endif
244 bm0[uword_msb(a)] |= (UWord)1 << uword_lsb(a);
245}
246
247/**
248 * Set the bits corresponding to all of the addresses in range
249 * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [
250 * in bitmap bm0.
251 */
bartf8bc71d2008-03-15 11:42:34 +0000252static __inline__ void bm0_set_range(UWord* bm0,
bart7b706b32009-05-10 06:55:39 +0000253 const UWord a, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000254{
bart8b4b2ee2008-06-11 13:17:56 +0000255#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000256 tl_assert(size > 0);
bart7b706b32009-05-10 06:55:39 +0000257 tl_assert(address_msb(make_address(0, a)) == 0);
258 tl_assert(address_msb(make_address(0, a + size - 1)) == 0);
259 tl_assert(uword_msb(a) == uword_msb(a + size - 1));
barta79df6e2008-03-14 17:07:51 +0000260#endif
bart7b706b32009-05-10 06:55:39 +0000261 bm0[uword_msb(a)]
262 |= (((UWord)1 << size) - 1) << uword_lsb(a);
sewardjaf44c822007-11-25 14:01:38 +0000263}
264
bart7b706b32009-05-10 06:55:39 +0000265/** Clear the bit corresponding to address a in bitmap bm0. */
266static __inline__ void bm0_clear(UWord* bm0, const UWord a)
sewardjaf44c822007-11-25 14:01:38 +0000267{
bart3c9989f2008-06-11 19:17:01 +0000268#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000269 tl_assert(address_msb(make_address(0, a)) == 0);
bart3c9989f2008-06-11 19:17:01 +0000270#endif
bart7b706b32009-05-10 06:55:39 +0000271 bm0[uword_msb(a)] &= ~((UWord)1 << uword_lsb(a));
sewardjaf44c822007-11-25 14:01:38 +0000272}
273
bart7b706b32009-05-10 06:55:39 +0000274/**
275 * Clear all of the addresses in range
276 * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [
277 * in bitmap bm0.
278 */
bart5955f332008-03-25 17:19:20 +0000279static __inline__ void bm0_clear_range(UWord* bm0,
bart7b706b32009-05-10 06:55:39 +0000280 const UWord a, const SizeT size)
bart5955f332008-03-25 17:19:20 +0000281{
bart8b4b2ee2008-06-11 13:17:56 +0000282#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000283 tl_assert(address_msb(make_address(0, a)) == 0);
284 tl_assert(size == 0 || address_msb(make_address(0, a + size - 1)) == 0);
285 tl_assert(size == 0 || uword_msb(a) == uword_msb(a + size - 1));
bart5955f332008-03-25 17:19:20 +0000286#endif
bart8c046732009-04-25 06:52:01 +0000287 /*
bart31b983d2010-02-21 14:52:59 +0000288 * Note: although the expression below yields a correct result even if
bart8c046732009-04-25 06:52:01 +0000289 * size == 0, do not touch bm0[] if size == 0 because this might otherwise
290 * cause an access of memory just past the end of the bm0[] array.
291 */
292 if (size > 0)
293 {
bart7b706b32009-05-10 06:55:39 +0000294 bm0[uword_msb(a)]
295 &= ~((((UWord)1 << size) - 1) << uword_lsb(a));
bart8c046732009-04-25 06:52:01 +0000296 }
bart5955f332008-03-25 17:19:20 +0000297}
298
bart7b706b32009-05-10 06:55:39 +0000299/** Test whether the bit corresponding to address a is set in bitmap bm0. */
300static __inline__ UWord bm0_is_set(const UWord* bm0, const UWord a)
sewardjaf44c822007-11-25 14:01:38 +0000301{
bart3c9989f2008-06-11 19:17:01 +0000302#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000303 tl_assert(address_msb(make_address(0, a)) == 0);
bart3c9989f2008-06-11 19:17:01 +0000304#endif
bart7b706b32009-05-10 06:55:39 +0000305 return (bm0[uword_msb(a)] & ((UWord)1 << uword_lsb(a)));
sewardjaf44c822007-11-25 14:01:38 +0000306}
307
bart7b706b32009-05-10 06:55:39 +0000308/**
309 * Return true if a bit corresponding to any of the addresses in range
310 * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [
311 * is set in bm0.
312 */
barta79df6e2008-03-14 17:07:51 +0000313static __inline__ UWord bm0_is_any_set(const UWord* bm0,
bart7b706b32009-05-10 06:55:39 +0000314 const Addr a, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000315{
bart8b4b2ee2008-06-11 13:17:56 +0000316#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000317 tl_assert(size > 0);
bart7b706b32009-05-10 06:55:39 +0000318 tl_assert(address_msb(make_address(0, a)) == 0);
319 tl_assert(address_msb(make_address(0, a + size - 1)) == 0);
320 tl_assert(uword_msb(a) == uword_msb(a + size - 1));
barta79df6e2008-03-14 17:07:51 +0000321#endif
bart7b706b32009-05-10 06:55:39 +0000322 return (bm0[uword_msb(a)] & ((((UWord)1 << size) - 1) << uword_lsb(a)));
barta79df6e2008-03-14 17:07:51 +0000323}
sewardjaf44c822007-11-25 14:01:38 +0000324
bartadbdf142008-03-19 17:00:12 +0000325
bart0008f5b2008-03-22 17:07:39 +0000326
327/*********************************************************************/
328/* Functions for manipulating a struct bitmap. */
bartadbdf142008-03-19 17:00:12 +0000329/*********************************************************************/
330
331
bart33e56c92008-03-24 06:41:30 +0000332/* Second level bitmap. */
sewardjaf44c822007-11-25 14:01:38 +0000333struct bitmap2
334{
bart7b706b32009-05-10 06:55:39 +0000335 Addr addr; ///< address_msb(...)
bart8f822af2009-06-08 18:20:42 +0000336 Bool recalc;
bartbedfd232009-03-26 19:07:15 +0000337 struct bitmap1 bm1;
sewardjaf44c822007-11-25 14:01:38 +0000338};
339
bart33e56c92008-03-24 06:41:30 +0000340
bart7b706b32009-05-10 06:55:39 +0000341static void bm2_clear(struct bitmap2* const bm2);
342static __inline__
343struct bitmap2* bm2_insert(struct bitmap* const bm, const UWord a1);
344
345
bartf647d342008-03-24 19:12:12 +0000346
bart8f822af2009-06-08 18:20:42 +0000347/**
348 * Rotate elements cache[0..n-1] such that the element at position n-1 is
349 * moved to position 0. This allows to speed up future cache lookups.
bart7e81a172008-06-09 19:50:51 +0000350 */
351static __inline__
352void bm_cache_rotate(struct bm_cache_elem cache[], const int n)
353{
bart45af5ea2008-06-12 06:04:59 +0000354#if 0
bartbedfd232009-03-26 19:07:15 +0000355 struct bm_cache_elem t;
bart7e81a172008-06-09 19:50:51 +0000356
bartbedfd232009-03-26 19:07:15 +0000357 tl_assert(2 <= n && n <= 8);
bartea2fa4c2008-06-10 06:32:49 +0000358
bartbedfd232009-03-26 19:07:15 +0000359 t = cache[0];
360 if (n > 1)
361 cache[0] = cache[1];
362 if (n > 2)
363 cache[1] = cache[2];
364 if (n > 3)
365 cache[2] = cache[3];
366 if (n > 4)
367 cache[3] = cache[4];
368 if (n > 5)
369 cache[4] = cache[5];
370 if (n > 6)
371 cache[5] = cache[6];
372 if (n > 7)
373 cache[6] = cache[7];
374 cache[n - 1] = t;
bartea2fa4c2008-06-10 06:32:49 +0000375#endif
bart7e81a172008-06-09 19:50:51 +0000376}
bartf647d342008-03-24 19:12:12 +0000377
bartf647d342008-03-24 19:12:12 +0000378static __inline__
bart7e81a172008-06-09 19:50:51 +0000379Bool bm_cache_lookup(struct bitmap* const bm, const UWord a1,
bartf29205e2008-03-25 18:51:06 +0000380 struct bitmap2** bm2)
bart33e56c92008-03-24 06:41:30 +0000381{
bart8b4b2ee2008-06-11 13:17:56 +0000382#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000383 tl_assert(bm);
384 tl_assert(bm2);
bart7e81a172008-06-09 19:50:51 +0000385#endif
bart33e56c92008-03-24 06:41:30 +0000386
bart8f822af2009-06-08 18:20:42 +0000387#if DRD_BITMAP_N_CACHE_ELEM > 8
bart33e56c92008-03-24 06:41:30 +0000388#error Please update the code below.
389#endif
bart8f822af2009-06-08 18:20:42 +0000390#if DRD_BITMAP_N_CACHE_ELEM >= 1
bartbedfd232009-03-26 19:07:15 +0000391 if (a1 == bm->cache[0].a1)
392 {
393 *bm2 = bm->cache[0].bm2;
394 return True;
395 }
bart33e56c92008-03-24 06:41:30 +0000396#endif
bart8f822af2009-06-08 18:20:42 +0000397#if DRD_BITMAP_N_CACHE_ELEM >= 2
bartbedfd232009-03-26 19:07:15 +0000398 if (a1 == bm->cache[1].a1)
399 {
400 *bm2 = bm->cache[1].bm2;
401 return True;
402 }
bart33e56c92008-03-24 06:41:30 +0000403#endif
bart8f822af2009-06-08 18:20:42 +0000404#if DRD_BITMAP_N_CACHE_ELEM >= 3
bartbedfd232009-03-26 19:07:15 +0000405 if (a1 == bm->cache[2].a1)
406 {
407 *bm2 = bm->cache[2].bm2;
408 bm_cache_rotate(bm->cache, 3);
409 return True;
410 }
bart33e56c92008-03-24 06:41:30 +0000411#endif
bart8f822af2009-06-08 18:20:42 +0000412#if DRD_BITMAP_N_CACHE_ELEM >= 4
bartbedfd232009-03-26 19:07:15 +0000413 if (a1 == bm->cache[3].a1)
414 {
415 *bm2 = bm->cache[3].bm2;
416 bm_cache_rotate(bm->cache, 4);
417 return True;
418 }
bart33e56c92008-03-24 06:41:30 +0000419#endif
bart8f822af2009-06-08 18:20:42 +0000420#if DRD_BITMAP_N_CACHE_ELEM >= 5
bartbedfd232009-03-26 19:07:15 +0000421 if (a1 == bm->cache[4].a1)
422 {
423 *bm2 = bm->cache[4].bm2;
424 bm_cache_rotate(bm->cache, 5);
425 return True;
426 }
bart33e56c92008-03-24 06:41:30 +0000427#endif
bart8f822af2009-06-08 18:20:42 +0000428#if DRD_BITMAP_N_CACHE_ELEM >= 6
bartbedfd232009-03-26 19:07:15 +0000429 if (a1 == bm->cache[5].a1)
430 {
431 *bm2 = bm->cache[5].bm2;
432 bm_cache_rotate(bm->cache, 6);
433 return True;
434 }
bart33e56c92008-03-24 06:41:30 +0000435#endif
bart8f822af2009-06-08 18:20:42 +0000436#if DRD_BITMAP_N_CACHE_ELEM >= 7
bartbedfd232009-03-26 19:07:15 +0000437 if (a1 == bm->cache[6].a1)
438 {
439 *bm2 = bm->cache[6].bm2;
440 bm_cache_rotate(bm->cache, 7);
441 return True;
442 }
bart33e56c92008-03-24 06:41:30 +0000443#endif
bart8f822af2009-06-08 18:20:42 +0000444#if DRD_BITMAP_N_CACHE_ELEM >= 8
bartbedfd232009-03-26 19:07:15 +0000445 if (a1 == bm->cache[7].a1)
446 {
447 *bm2 = bm->cache[7].bm2;
448 bm_cache_rotate(bm->cache, 8);
449 return True;
450 }
bart33e56c92008-03-24 06:41:30 +0000451#endif
bartbedfd232009-03-26 19:07:15 +0000452 *bm2 = 0;
453 return False;
bart33e56c92008-03-24 06:41:30 +0000454}
455
456static __inline__
457void bm_update_cache(struct bitmap* const bm,
458 const UWord a1,
459 struct bitmap2* const bm2)
460{
bart3c9989f2008-06-11 19:17:01 +0000461#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000462 tl_assert(bm);
bart3c9989f2008-06-11 19:17:01 +0000463#endif
bart33e56c92008-03-24 06:41:30 +0000464
bart8f822af2009-06-08 18:20:42 +0000465#if DRD_BITMAP_N_CACHE_ELEM > 8
bart33e56c92008-03-24 06:41:30 +0000466#error Please update the code below.
467#endif
bart8f822af2009-06-08 18:20:42 +0000468#if DRD_BITMAP_N_CACHE_ELEM >= 8
bartbedfd232009-03-26 19:07:15 +0000469 bm->cache[7] = bm->cache[6];
bart33e56c92008-03-24 06:41:30 +0000470#endif
bart8f822af2009-06-08 18:20:42 +0000471#if DRD_BITMAP_N_CACHE_ELEM >= 7
bartbedfd232009-03-26 19:07:15 +0000472 bm->cache[6] = bm->cache[5];
bart33e56c92008-03-24 06:41:30 +0000473#endif
bart8f822af2009-06-08 18:20:42 +0000474#if DRD_BITMAP_N_CACHE_ELEM >= 6
bartbedfd232009-03-26 19:07:15 +0000475 bm->cache[5] = bm->cache[4];
bart33e56c92008-03-24 06:41:30 +0000476#endif
bart8f822af2009-06-08 18:20:42 +0000477#if DRD_BITMAP_N_CACHE_ELEM >= 5
bartbedfd232009-03-26 19:07:15 +0000478 bm->cache[4] = bm->cache[3];
bart33e56c92008-03-24 06:41:30 +0000479#endif
bart8f822af2009-06-08 18:20:42 +0000480#if DRD_BITMAP_N_CACHE_ELEM >= 4
bartbedfd232009-03-26 19:07:15 +0000481 bm->cache[3] = bm->cache[2];
bart33e56c92008-03-24 06:41:30 +0000482#endif
bart8f822af2009-06-08 18:20:42 +0000483#if DRD_BITMAP_N_CACHE_ELEM >= 3
bartbedfd232009-03-26 19:07:15 +0000484 bm->cache[2] = bm->cache[1];
bart33e56c92008-03-24 06:41:30 +0000485#endif
bart8f822af2009-06-08 18:20:42 +0000486#if DRD_BITMAP_N_CACHE_ELEM >= 2
bartbedfd232009-03-26 19:07:15 +0000487 bm->cache[1] = bm->cache[0];
bart33e56c92008-03-24 06:41:30 +0000488#endif
bartbedfd232009-03-26 19:07:15 +0000489 bm->cache[0].a1 = a1;
490 bm->cache[0].bm2 = bm2;
bart33e56c92008-03-24 06:41:30 +0000491}
492
bart8f822af2009-06-08 18:20:42 +0000493/**
494 * Look up the address a1 in bitmap bm and return a pointer to a potentially
495 * shared second level bitmap. The bitmap where the returned pointer points
496 * at may not be modified by the caller.
bartf647d342008-03-24 19:12:12 +0000497 *
bart8f822af2009-06-08 18:20:42 +0000498 * @param a1 client address shifted right by ADDR_LSB_BITS.
499 * @param bm bitmap pointer.
bart0008f5b2008-03-22 17:07:39 +0000500 */
sewardjaf44c822007-11-25 14:01:38 +0000501static __inline__
bart7e81a172008-06-09 19:50:51 +0000502const struct bitmap2* bm2_lookup(struct bitmap* const bm, const UWord a1)
sewardjaf44c822007-11-25 14:01:38 +0000503{
bart7b706b32009-05-10 06:55:39 +0000504 struct bitmap2* bm2;
bartf647d342008-03-24 19:12:12 +0000505
bart3c9989f2008-06-11 19:17:01 +0000506#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000507 tl_assert(bm);
bart3c9989f2008-06-11 19:17:01 +0000508#endif
bart7b706b32009-05-10 06:55:39 +0000509
bartbedfd232009-03-26 19:07:15 +0000510 if (! bm_cache_lookup(bm, a1, &bm2))
511 {
bart7b706b32009-05-10 06:55:39 +0000512 bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1);
513 bm_update_cache(bm, a1, bm2);
bartbedfd232009-03-26 19:07:15 +0000514 }
515 return bm2;
bartf647d342008-03-24 19:12:12 +0000516}
517
bart8f822af2009-06-08 18:20:42 +0000518/**
519 * Look up the address a1 in bitmap bm and return a pointer to a second
520 * level bitmap that is not shared and hence may be modified.
bartf647d342008-03-24 19:12:12 +0000521 *
bart8f822af2009-06-08 18:20:42 +0000522 * @param a1 client address shifted right by ADDR_LSB_BITS.
523 * @param bm bitmap pointer.
bartf647d342008-03-24 19:12:12 +0000524 */
525static __inline__
526struct bitmap2*
bart7e81a172008-06-09 19:50:51 +0000527bm2_lookup_exclusive(struct bitmap* const bm, const UWord a1)
bartf647d342008-03-24 19:12:12 +0000528{
bartbedfd232009-03-26 19:07:15 +0000529 struct bitmap2* bm2;
bartf647d342008-03-24 19:12:12 +0000530
bart3c9989f2008-06-11 19:17:01 +0000531#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart7b706b32009-05-10 06:55:39 +0000532 tl_assert(bm);
bart3c9989f2008-06-11 19:17:01 +0000533#endif
bartf647d342008-03-24 19:12:12 +0000534
bart7b706b32009-05-10 06:55:39 +0000535 if (! bm_cache_lookup(bm, a1, &bm2))
bartbedfd232009-03-26 19:07:15 +0000536 {
bart7b706b32009-05-10 06:55:39 +0000537 bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1);
bartbedfd232009-03-26 19:07:15 +0000538 }
bartf647d342008-03-24 19:12:12 +0000539
bartbedfd232009-03-26 19:07:15 +0000540 return bm2;
sewardjaf44c822007-11-25 14:01:38 +0000541}
542
bart6584b692009-06-21 10:11:15 +0000543/** Clear the content of the second-level bitmap. */
544static __inline__
545void bm2_clear(struct bitmap2* const bm2)
546{
547#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
548 tl_assert(bm2);
549#endif
550 VG_(memset)(&bm2->bm1, 0, sizeof(bm2->bm1));
551}
552
bart8f822af2009-06-08 18:20:42 +0000553/**
554 * Insert an uninitialized second level bitmap for the address a1.
bartf647d342008-03-24 19:12:12 +0000555 *
bart8f822af2009-06-08 18:20:42 +0000556 * @param bm bitmap pointer.
557 * @param a1 client address shifted right by ADDR_LSB_BITS.
558 *
559 * @note bitmap2::recalc isn't initialized here on purpose.
bartf647d342008-03-24 19:12:12 +0000560 */
sewardjaf44c822007-11-25 14:01:38 +0000561static __inline__
bart7e81a172008-06-09 19:50:51 +0000562struct bitmap2* bm2_insert(struct bitmap* const bm, const UWord a1)
sewardjaf44c822007-11-25 14:01:38 +0000563{
bartbedfd232009-03-26 19:07:15 +0000564 struct bitmap2* bm2;
bart33e56c92008-03-24 06:41:30 +0000565
bart7b706b32009-05-10 06:55:39 +0000566#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
567 tl_assert(bm);
568#endif
569
570 s_bitmap2_creation_count++;
571
572 bm2 = VG_(OSetGen_AllocNode)(bm->oset, sizeof(*bm2));
573 bm2->addr = a1;
574 VG_(OSetGen_Insert)(bm->oset, bm2);
575
576 bm_update_cache(bm, a1, bm2);
bartf647d342008-03-24 19:12:12 +0000577
bartbedfd232009-03-26 19:07:15 +0000578 return bm2;
bartf647d342008-03-24 19:12:12 +0000579}
580
bartf647d342008-03-24 19:12:12 +0000581static __inline__
bart7b706b32009-05-10 06:55:39 +0000582struct bitmap2* bm2_insert_copy(struct bitmap* const bm,
583 struct bitmap2* const bm2)
bartf647d342008-03-24 19:12:12 +0000584{
bart7b706b32009-05-10 06:55:39 +0000585 struct bitmap2* bm2_copy;
bartf647d342008-03-24 19:12:12 +0000586
bart7b706b32009-05-10 06:55:39 +0000587 bm2_copy = bm2_insert(bm, bm2->addr);
588 VG_(memcpy)(&bm2_copy->bm1, &bm2->bm1, sizeof(bm2->bm1));
589 return bm2_copy;
sewardjaf44c822007-11-25 14:01:38 +0000590}
591
bart8f822af2009-06-08 18:20:42 +0000592/**
593 * Look up the address a1 in bitmap bm, and insert it if not found.
594 * The returned second level bitmap may not be modified.
bart0008f5b2008-03-22 17:07:39 +0000595 *
bart8f822af2009-06-08 18:20:42 +0000596 * @param bm bitmap pointer.
597 * @param a1 client address shifted right by ADDR_LSB_BITS.
bart0008f5b2008-03-22 17:07:39 +0000598 */
sewardjaf44c822007-11-25 14:01:38 +0000599static __inline__
bart7e81a172008-06-09 19:50:51 +0000600struct bitmap2* bm2_lookup_or_insert(struct bitmap* const bm, const UWord a1)
sewardjaf44c822007-11-25 14:01:38 +0000601{
bartbedfd232009-03-26 19:07:15 +0000602 struct bitmap2* bm2;
barta79df6e2008-03-14 17:07:51 +0000603
bart3c9989f2008-06-11 19:17:01 +0000604#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000605 tl_assert(bm);
bart3c9989f2008-06-11 19:17:01 +0000606#endif
bart7b706b32009-05-10 06:55:39 +0000607
bartbedfd232009-03-26 19:07:15 +0000608 if (bm_cache_lookup(bm, a1, &bm2))
609 {
610 if (bm2 == 0)
611 {
612 bm2 = bm2_insert(bm, a1);
bart7b706b32009-05-10 06:55:39 +0000613 bm2_clear(bm2);
bartbedfd232009-03-26 19:07:15 +0000614 }
615 }
616 else
617 {
bart7b706b32009-05-10 06:55:39 +0000618 bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1);
619 if (! bm2)
bartbedfd232009-03-26 19:07:15 +0000620 {
621 bm2 = bm2_insert(bm, a1);
bart7b706b32009-05-10 06:55:39 +0000622 bm2_clear(bm2);
bartbedfd232009-03-26 19:07:15 +0000623 }
bart7b706b32009-05-10 06:55:39 +0000624 bm_update_cache(bm, a1, bm2);
bartbedfd232009-03-26 19:07:15 +0000625 }
626 return bm2;
sewardjaf44c822007-11-25 14:01:38 +0000627}
628
bart8f822af2009-06-08 18:20:42 +0000629/**
630 * Look up the address a1 in bitmap bm, and insert it if not found.
631 * The returned second level bitmap may be modified.
bartf647d342008-03-24 19:12:12 +0000632 *
bart8f822af2009-06-08 18:20:42 +0000633 * @param a1 client address shifted right by ADDR_LSB_BITS.
634 * @param bm bitmap pointer.
bartf647d342008-03-24 19:12:12 +0000635 */
636static __inline__
637struct bitmap2* bm2_lookup_or_insert_exclusive(struct bitmap* const bm,
638 const UWord a1)
639{
bart7b706b32009-05-10 06:55:39 +0000640 return bm2_lookup_or_insert(bm, a1);
bartf647d342008-03-24 19:12:12 +0000641}
642
bartd59bb0f2008-06-08 08:08:31 +0000643static __inline__
bart8f822af2009-06-08 18:20:42 +0000644void bm2_remove(struct bitmap* const bm, const UWord a1)
645{
646 struct bitmap2* bm2;
647
648#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
649 tl_assert(bm);
650#endif
651
652 bm2 = VG_(OSetGen_Remove)(bm->oset, &a1);
653 VG_(OSetGen_FreeNode)(bm->oset, bm2);
654
655 bm_update_cache(bm, a1, NULL);
656}
657
658static __inline__
bartd59bb0f2008-06-08 08:08:31 +0000659void bm_access_aligned_load(struct bitmap* const bm,
660 const Addr a1, const SizeT size)
661{
bartbedfd232009-03-26 19:07:15 +0000662 struct bitmap2* bm2;
bartd59bb0f2008-06-08 08:08:31 +0000663
bart7b706b32009-05-10 06:55:39 +0000664#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
665 tl_assert(bm);
666#endif
667
668 bm2 = bm2_lookup_or_insert_exclusive(bm, address_msb(a1));
669 bm0_set_range(bm2->bm1.bm0_r,
670 (a1 >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK,
671 SCALED_SIZE(size));
bartd59bb0f2008-06-08 08:08:31 +0000672}
673
674static __inline__
675void bm_access_aligned_store(struct bitmap* const bm,
676 const Addr a1, const SizeT size)
677{
bartbedfd232009-03-26 19:07:15 +0000678 struct bitmap2* bm2;
bartd59bb0f2008-06-08 08:08:31 +0000679
bart7b706b32009-05-10 06:55:39 +0000680#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
681 tl_assert(bm);
682#endif
683
684 bm2 = bm2_lookup_or_insert_exclusive(bm, address_msb(a1));
685 bm0_set_range(bm2->bm1.bm0_w,
686 (a1 >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK,
687 SCALED_SIZE(size));
bartd59bb0f2008-06-08 08:08:31 +0000688}
689
690static __inline__
bart7e81a172008-06-09 19:50:51 +0000691Bool bm_aligned_load_has_conflict_with(struct bitmap* const bm,
bart7b706b32009-05-10 06:55:39 +0000692 const Addr a, const SizeT size)
bartd59bb0f2008-06-08 08:08:31 +0000693{
bartbedfd232009-03-26 19:07:15 +0000694 const struct bitmap2* bm2;
bartd59bb0f2008-06-08 08:08:31 +0000695
bart7b706b32009-05-10 06:55:39 +0000696#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
697 tl_assert(bm);
698#endif
bartd59bb0f2008-06-08 08:08:31 +0000699
bart7b706b32009-05-10 06:55:39 +0000700 bm2 = bm2_lookup(bm, address_msb(a));
701 return (bm2
702 && bm0_is_any_set(bm2->bm1.bm0_w,
703 address_lsb(a),
704 SCALED_SIZE(size)));
bartd59bb0f2008-06-08 08:08:31 +0000705}
706
707static __inline__
bart7e81a172008-06-09 19:50:51 +0000708Bool bm_aligned_store_has_conflict_with(struct bitmap* const bm,
bart7b706b32009-05-10 06:55:39 +0000709 const Addr a, const SizeT size)
bartd59bb0f2008-06-08 08:08:31 +0000710{
bartbedfd232009-03-26 19:07:15 +0000711 const struct bitmap2* bm2;
bartd59bb0f2008-06-08 08:08:31 +0000712
bart7b706b32009-05-10 06:55:39 +0000713#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
714 tl_assert(bm);
715#endif
bartd59bb0f2008-06-08 08:08:31 +0000716
bart7b706b32009-05-10 06:55:39 +0000717 bm2 = bm2_lookup(bm, address_msb(a));
bartbedfd232009-03-26 19:07:15 +0000718 if (bm2)
719 {
bart7b706b32009-05-10 06:55:39 +0000720 if (bm0_is_any_set(bm2->bm1.bm0_r, address_lsb(a), SCALED_SIZE(size))
721 | bm0_is_any_set(bm2->bm1.bm0_w, address_lsb(a), SCALED_SIZE(size)))
bartbedfd232009-03-26 19:07:15 +0000722 {
723 return True;
724 }
725 }
726 return False;
bartd59bb0f2008-06-08 08:08:31 +0000727}
sewardjaf44c822007-11-25 14:01:38 +0000728
bart33e56c92008-03-24 06:41:30 +0000729#endif /* __DRD_BITMAP_H */