njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 3 | /*--- MemCheck: Maintain bitmaps of memory, tracking the ---*/ |
| 4 | /*--- accessibility (A) and validity (V) status of each byte. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 5 | /*--- mc_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 9 | This file is part of MemCheck, a heavyweight Valgrind tool for |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 10 | detecting memory errors. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 11 | |
sewardj | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2008 Julian Seward |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 13 | jseward@acm.org |
| 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
| 30 | The GNU General Public License is contained in the file COPYING. |
| 31 | */ |
| 32 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_tool_basics.h" |
njn | 4802b38 | 2005-06-11 04:58:29 +0000 | [diff] [blame] | 34 | #include "pub_tool_aspacemgr.h" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 35 | #include "pub_tool_hashtable.h" // For mc_include.h |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 36 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 37 | #include "pub_tool_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 38 | #include "pub_tool_libcprint.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 39 | #include "pub_tool_machine.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 40 | #include "pub_tool_mallocfree.h" |
| 41 | #include "pub_tool_options.h" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 42 | #include "pub_tool_oset.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 43 | #include "pub_tool_replacemalloc.h" |
| 44 | #include "pub_tool_tooliface.h" |
| 45 | #include "pub_tool_threadstate.h" |
| 46 | |
| 47 | #include "mc_include.h" |
| 48 | #include "memcheck.h" /* for client requests */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 49 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 50 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 51 | /* Set to 1 to do a little more sanity checking */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 52 | #define VG_DEBUG_MEMORY 0 |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 53 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 54 | #define DEBUG(fmt, args...) //VG_(printf)(fmt, ## args) |
| 55 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 56 | static void ocache_sarp_Set_Origins ( Addr, UWord, UInt ); /* fwds */ |
| 57 | static void ocache_sarp_Clear_Origins ( Addr, UWord ); /* fwds */ |
| 58 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 59 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 60 | /*------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 61 | /*--- Fast-case knobs ---*/ |
| 62 | /*------------------------------------------------------------*/ |
| 63 | |
| 64 | // Comment these out to disable the fast cases (don't just set them to zero). |
| 65 | |
| 66 | #define PERF_FAST_LOADV 1 |
| 67 | #define PERF_FAST_STOREV 1 |
| 68 | |
| 69 | #define PERF_FAST_SARP 1 |
| 70 | |
| 71 | #define PERF_FAST_STACK 1 |
| 72 | #define PERF_FAST_STACK2 1 |
| 73 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 74 | /* Change this to 1 to enable assertions on origin tracking cache fast |
| 75 | paths */ |
| 76 | #define OC_ENABLE_ASSERTIONS 0 |
| 77 | |
| 78 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 79 | /*------------------------------------------------------------*/ |
| 80 | /*--- V bits and A bits ---*/ |
| 81 | /*------------------------------------------------------------*/ |
| 82 | |
| 83 | /* Conceptually, every byte value has 8 V bits, which track whether Memcheck |
| 84 | thinks the corresponding value bit is defined. And every memory byte |
| 85 | has an A bit, which tracks whether Memcheck thinks the program can access |
| 86 | it safely. So every N-bit register is shadowed with N V bits, and every |
| 87 | memory byte is shadowed with 8 V bits and one A bit. |
| 88 | |
| 89 | In the implementation, we use two forms of compression (compressed V bits |
| 90 | and distinguished secondary maps) to avoid the 9-bit-per-byte overhead |
| 91 | for memory. |
| 92 | |
| 93 | Memcheck also tracks extra information about each heap block that is |
| 94 | allocated, for detecting memory leaks and other purposes. |
| 95 | */ |
| 96 | |
| 97 | /*------------------------------------------------------------*/ |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 98 | /*--- Basic A/V bitmap representation. ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 99 | /*------------------------------------------------------------*/ |
| 100 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 101 | /* All reads and writes are checked against a memory map (a.k.a. shadow |
| 102 | memory), which records the state of all memory in the process. |
| 103 | |
| 104 | On 32-bit machines the memory map is organised as follows. |
| 105 | The top 16 bits of an address are used to index into a top-level |
| 106 | map table, containing 65536 entries. Each entry is a pointer to a |
| 107 | second-level map, which records the accesibililty and validity |
| 108 | permissions for the 65536 bytes indexed by the lower 16 bits of the |
| 109 | address. Each byte is represented by two bits (details are below). So |
| 110 | each second-level map contains 16384 bytes. This two-level arrangement |
| 111 | conveniently divides the 4G address space into 64k lumps, each size 64k |
| 112 | bytes. |
| 113 | |
| 114 | All entries in the primary (top-level) map must point to a valid |
| 115 | secondary (second-level) map. Since many of the 64kB chunks will |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 116 | have the same status for every bit -- ie. noaccess (for unused |
| 117 | address space) or entirely addressable and defined (for code segments) -- |
| 118 | there are three distinguished secondary maps, which indicate 'noaccess', |
| 119 | 'undefined' and 'defined'. For these uniform 64kB chunks, the primary |
| 120 | map entry points to the relevant distinguished map. In practice, |
| 121 | typically more than half of the addressable memory is represented with |
| 122 | the 'undefined' or 'defined' distinguished secondary map, so it gives a |
| 123 | good saving. It also lets us set the V+A bits of large address regions |
| 124 | quickly in set_address_range_perms(). |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 125 | |
| 126 | On 64-bit machines it's more complicated. If we followed the same basic |
| 127 | scheme we'd have a four-level table which would require too many memory |
| 128 | accesses. So instead the top-level map table has 2^19 entries (indexed |
| 129 | using bits 16..34 of the address); this covers the bottom 32GB. Any |
| 130 | accesses above 32GB are handled with a slow, sparse auxiliary table. |
| 131 | Valgrind's address space manager tries very hard to keep things below |
| 132 | this 32GB barrier so that performance doesn't suffer too much. |
| 133 | |
| 134 | Note that this file has a lot of different functions for reading and |
| 135 | writing shadow memory. Only a couple are strictly necessary (eg. |
| 136 | get_vabits2 and set_vabits2), most are just specialised for specific |
| 137 | common cases to improve performance. |
| 138 | |
| 139 | Aside: the V+A bits are less precise than they could be -- we have no way |
| 140 | of marking memory as read-only. It would be great if we could add an |
| 141 | extra state VA_BITSn_READONLY. But then we'd have 5 different states, |
| 142 | which requires 2.3 bits to hold, and there's no way to do that elegantly |
| 143 | -- we'd have to double up to 4 bits of metadata per byte, which doesn't |
| 144 | seem worth it. |
| 145 | */ |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 146 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 147 | /* --------------- Basic configuration --------------- */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 148 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 149 | /* Only change this. N_PRIMARY_MAP *must* be a power of 2. */ |
sewardj | 21f7ff4 | 2005-04-28 10:32:02 +0000 | [diff] [blame] | 150 | |
sewardj | e4ccc01 | 2005-05-02 12:53:38 +0000 | [diff] [blame] | 151 | #if VG_WORDSIZE == 4 |
sewardj | 21f7ff4 | 2005-04-28 10:32:02 +0000 | [diff] [blame] | 152 | |
| 153 | /* cover the entire address space */ |
| 154 | # define N_PRIMARY_BITS 16 |
| 155 | |
| 156 | #else |
| 157 | |
sewardj | 34483bc | 2005-09-28 11:50:20 +0000 | [diff] [blame] | 158 | /* Just handle the first 32G fast and the rest via auxiliary |
sewardj | 7244e71 | 2008-05-02 12:35:48 +0000 | [diff] [blame] | 159 | primaries. If you change this, Memcheck will assert at startup. |
| 160 | See the definition of UNALIGNED_OR_HIGH for extensive comments. */ |
sewardj | 34483bc | 2005-09-28 11:50:20 +0000 | [diff] [blame] | 161 | # define N_PRIMARY_BITS 19 |
sewardj | 21f7ff4 | 2005-04-28 10:32:02 +0000 | [diff] [blame] | 162 | |
| 163 | #endif |
| 164 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 165 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 166 | /* Do not change this. */ |
sewardj | e4ccc01 | 2005-05-02 12:53:38 +0000 | [diff] [blame] | 167 | #define N_PRIMARY_MAP ( ((UWord)1) << N_PRIMARY_BITS) |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 168 | |
| 169 | /* Do not change this. */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 170 | #define MAX_PRIMARY_ADDRESS (Addr)((((Addr)65536) * N_PRIMARY_MAP)-1) |
| 171 | |
| 172 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 173 | /* --------------- Secondary maps --------------- */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 174 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 175 | // Each byte of memory conceptually has an A bit, which indicates its |
| 176 | // addressability, and 8 V bits, which indicates its definedness. |
| 177 | // |
| 178 | // But because very few bytes are partially defined, we can use a nice |
| 179 | // compression scheme to reduce the size of shadow memory. Each byte of |
| 180 | // memory has 2 bits which indicates its state (ie. V+A bits): |
| 181 | // |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 182 | // 00: noaccess (unaddressable but treated as fully defined) |
| 183 | // 01: undefined (addressable and fully undefined) |
| 184 | // 10: defined (addressable and fully defined) |
| 185 | // 11: partdefined (addressable and partially defined) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 186 | // |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 187 | // In the "partdefined" case, we use a secondary table to store the V bits. |
| 188 | // Each entry in the secondary-V-bits table maps a byte address to its 8 V |
| 189 | // bits. |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 190 | // |
| 191 | // We store the compressed V+A bits in 8-bit chunks, ie. the V+A bits for |
| 192 | // four bytes (32 bits) of memory are in each chunk. Hence the name |
| 193 | // "vabits8". This lets us get the V+A bits for four bytes at a time |
| 194 | // easily (without having to do any shifting and/or masking), and that is a |
| 195 | // very common operation. (Note that although each vabits8 chunk |
| 196 | // is 8 bits in size, it represents 32 bits of memory.) |
| 197 | // |
| 198 | // The representation is "inverse" little-endian... each 4 bytes of |
| 199 | // memory is represented by a 1 byte value, where: |
| 200 | // |
| 201 | // - the status of byte (a+0) is held in bits [1..0] |
| 202 | // - the status of byte (a+1) is held in bits [3..2] |
| 203 | // - the status of byte (a+2) is held in bits [5..4] |
| 204 | // - the status of byte (a+3) is held in bits [7..6] |
| 205 | // |
| 206 | // It's "inverse" because endianness normally describes a mapping from |
| 207 | // value bits to memory addresses; in this case the mapping is inverted. |
| 208 | // Ie. instead of particular value bits being held in certain addresses, in |
| 209 | // this case certain addresses are represented by particular value bits. |
| 210 | // See insert_vabits2_into_vabits8() for an example. |
| 211 | // |
| 212 | // But note that we don't compress the V bits stored in registers; they |
| 213 | // need to be explicit to made the shadow operations possible. Therefore |
| 214 | // when moving values between registers and memory we need to convert |
| 215 | // between the expanded in-register format and the compressed in-memory |
| 216 | // format. This isn't so difficult, it just requires careful attention in a |
| 217 | // few places. |
| 218 | |
| 219 | // These represent eight bits of memory. |
| 220 | #define VA_BITS2_NOACCESS 0x0 // 00b |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 221 | #define VA_BITS2_UNDEFINED 0x1 // 01b |
| 222 | #define VA_BITS2_DEFINED 0x2 // 10b |
| 223 | #define VA_BITS2_PARTDEFINED 0x3 // 11b |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 224 | |
| 225 | // These represent 16 bits of memory. |
| 226 | #define VA_BITS4_NOACCESS 0x0 // 00_00b |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 227 | #define VA_BITS4_UNDEFINED 0x5 // 01_01b |
| 228 | #define VA_BITS4_DEFINED 0xa // 10_10b |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 229 | |
| 230 | // These represent 32 bits of memory. |
| 231 | #define VA_BITS8_NOACCESS 0x00 // 00_00_00_00b |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 232 | #define VA_BITS8_UNDEFINED 0x55 // 01_01_01_01b |
| 233 | #define VA_BITS8_DEFINED 0xaa // 10_10_10_10b |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 234 | |
| 235 | // These represent 64 bits of memory. |
| 236 | #define VA_BITS16_NOACCESS 0x0000 // 00_00_00_00b x 2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 237 | #define VA_BITS16_UNDEFINED 0x5555 // 01_01_01_01b x 2 |
| 238 | #define VA_BITS16_DEFINED 0xaaaa // 10_10_10_10b x 2 |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 239 | |
| 240 | |
| 241 | #define SM_CHUNKS 16384 |
| 242 | #define SM_OFF(aaa) (((aaa) & 0xffff) >> 2) |
| 243 | #define SM_OFF_16(aaa) (((aaa) & 0xffff) >> 3) |
| 244 | |
| 245 | // Paranoia: it's critical for performance that the requested inlining |
| 246 | // occurs. So try extra hard. |
| 247 | #define INLINE inline __attribute__((always_inline)) |
| 248 | |
| 249 | static INLINE Addr start_of_this_sm ( Addr a ) { |
| 250 | return (a & (~SM_MASK)); |
| 251 | } |
| 252 | static INLINE Bool is_start_of_sm ( Addr a ) { |
| 253 | return (start_of_this_sm(a) == a); |
| 254 | } |
| 255 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 256 | typedef |
| 257 | struct { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 258 | UChar vabits8[SM_CHUNKS]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 259 | } |
| 260 | SecMap; |
| 261 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 262 | // 3 distinguished secondary maps, one for no-access, one for |
| 263 | // accessible but undefined, and one for accessible and defined. |
| 264 | // Distinguished secondaries may never be modified. |
| 265 | #define SM_DIST_NOACCESS 0 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 266 | #define SM_DIST_UNDEFINED 1 |
| 267 | #define SM_DIST_DEFINED 2 |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 268 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 269 | static SecMap sm_distinguished[3]; |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 270 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 271 | static INLINE Bool is_distinguished_sm ( SecMap* sm ) { |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 272 | return sm >= &sm_distinguished[0] && sm <= &sm_distinguished[2]; |
| 273 | } |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 274 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 275 | // Forward declaration |
| 276 | static void update_SM_counts(SecMap* oldSM, SecMap* newSM); |
| 277 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 278 | /* dist_sm points to one of our three distinguished secondaries. Make |
| 279 | a copy of it so that we can write to it. |
| 280 | */ |
| 281 | static SecMap* copy_for_writing ( SecMap* dist_sm ) |
| 282 | { |
| 283 | SecMap* new_sm; |
| 284 | tl_assert(dist_sm == &sm_distinguished[0] |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 285 | || dist_sm == &sm_distinguished[1] |
| 286 | || dist_sm == &sm_distinguished[2]); |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 287 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 288 | new_sm = VG_(am_shadow_alloc)(sizeof(SecMap)); |
| 289 | if (new_sm == NULL) |
| 290 | VG_(out_of_memory_NORETURN)( "memcheck:allocate new SecMap", |
| 291 | sizeof(SecMap) ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 292 | VG_(memcpy)(new_sm, dist_sm, sizeof(SecMap)); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 293 | update_SM_counts(dist_sm, new_sm); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 294 | return new_sm; |
| 295 | } |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 296 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 297 | /* --------------- Stats --------------- */ |
| 298 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 299 | static Int n_issued_SMs = 0; |
| 300 | static Int n_deissued_SMs = 0; |
| 301 | static Int n_noaccess_SMs = N_PRIMARY_MAP; // start with many noaccess DSMs |
| 302 | static Int n_undefined_SMs = 0; |
| 303 | static Int n_defined_SMs = 0; |
| 304 | static Int n_non_DSM_SMs = 0; |
| 305 | static Int max_noaccess_SMs = 0; |
| 306 | static Int max_undefined_SMs = 0; |
| 307 | static Int max_defined_SMs = 0; |
| 308 | static Int max_non_DSM_SMs = 0; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 309 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 310 | /* # searches initiated in auxmap_L1, and # base cmps required */ |
| 311 | static ULong n_auxmap_L1_searches = 0; |
| 312 | static ULong n_auxmap_L1_cmps = 0; |
| 313 | /* # of searches that missed in auxmap_L1 and therefore had to |
| 314 | be handed to auxmap_L2. And the number of nodes inserted. */ |
| 315 | static ULong n_auxmap_L2_searches = 0; |
| 316 | static ULong n_auxmap_L2_nodes = 0; |
| 317 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 318 | static Int n_sanity_cheap = 0; |
| 319 | static Int n_sanity_expensive = 0; |
| 320 | |
| 321 | static Int n_secVBit_nodes = 0; |
| 322 | static Int max_secVBit_nodes = 0; |
| 323 | |
| 324 | static void update_SM_counts(SecMap* oldSM, SecMap* newSM) |
| 325 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 326 | if (oldSM == &sm_distinguished[SM_DIST_NOACCESS ]) n_noaccess_SMs --; |
| 327 | else if (oldSM == &sm_distinguished[SM_DIST_UNDEFINED]) n_undefined_SMs--; |
| 328 | else if (oldSM == &sm_distinguished[SM_DIST_DEFINED ]) n_defined_SMs --; |
| 329 | else { n_non_DSM_SMs --; |
| 330 | n_deissued_SMs ++; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 331 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 332 | if (newSM == &sm_distinguished[SM_DIST_NOACCESS ]) n_noaccess_SMs ++; |
| 333 | else if (newSM == &sm_distinguished[SM_DIST_UNDEFINED]) n_undefined_SMs++; |
| 334 | else if (newSM == &sm_distinguished[SM_DIST_DEFINED ]) n_defined_SMs ++; |
| 335 | else { n_non_DSM_SMs ++; |
| 336 | n_issued_SMs ++; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 337 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 338 | if (n_noaccess_SMs > max_noaccess_SMs ) max_noaccess_SMs = n_noaccess_SMs; |
| 339 | if (n_undefined_SMs > max_undefined_SMs) max_undefined_SMs = n_undefined_SMs; |
| 340 | if (n_defined_SMs > max_defined_SMs ) max_defined_SMs = n_defined_SMs; |
| 341 | if (n_non_DSM_SMs > max_non_DSM_SMs ) max_non_DSM_SMs = n_non_DSM_SMs; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 342 | } |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 343 | |
| 344 | /* --------------- Primary maps --------------- */ |
| 345 | |
| 346 | /* The main primary map. This covers some initial part of the address |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 347 | space, addresses 0 .. (N_PRIMARY_MAP << 16)-1. The rest of it is |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 348 | handled using the auxiliary primary map. |
| 349 | */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 350 | static SecMap* primary_map[N_PRIMARY_MAP]; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 351 | |
| 352 | |
| 353 | /* An entry in the auxiliary primary map. base must be a 64k-aligned |
| 354 | value, and sm points at the relevant secondary map. As with the |
| 355 | main primary map, the secondary may be either a real secondary, or |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 356 | one of the three distinguished secondaries. DO NOT CHANGE THIS |
| 357 | LAYOUT: the first word has to be the key for OSet fast lookups. |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 358 | */ |
| 359 | typedef |
| 360 | struct { |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 361 | Addr base; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 362 | SecMap* sm; |
| 363 | } |
| 364 | AuxMapEnt; |
| 365 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 366 | /* Tunable parameter: How big is the L1 queue? */ |
| 367 | #define N_AUXMAP_L1 24 |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 368 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 369 | /* Tunable parameter: How far along the L1 queue to insert |
| 370 | entries resulting from L2 lookups? */ |
| 371 | #define AUXMAP_L1_INSERT_IX 12 |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 372 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 373 | static struct { |
| 374 | Addr base; |
| 375 | AuxMapEnt* ent; // pointer to the matching auxmap_L2 node |
| 376 | } |
| 377 | auxmap_L1[N_AUXMAP_L1]; |
| 378 | |
| 379 | static OSet* auxmap_L2 = NULL; |
| 380 | |
| 381 | static void init_auxmap_L1_L2 ( void ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 382 | { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 383 | Int i; |
| 384 | for (i = 0; i < N_AUXMAP_L1; i++) { |
| 385 | auxmap_L1[i].base = 0; |
| 386 | auxmap_L1[i].ent = NULL; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 387 | } |
| 388 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 389 | tl_assert(0 == offsetof(AuxMapEnt,base)); |
| 390 | tl_assert(sizeof(Addr) == sizeof(void*)); |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 391 | auxmap_L2 = VG_(OSetGen_Create)( /*keyOff*/ offsetof(AuxMapEnt,base), |
| 392 | /*fastCmp*/ NULL, |
| 393 | VG_(malloc), VG_(free) ); |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 394 | } |
| 395 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 396 | /* Check representation invariants; if OK return NULL; else a |
| 397 | descriptive bit of text. Also return the number of |
| 398 | non-distinguished secondary maps referred to from the auxiliary |
| 399 | primary maps. */ |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 400 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 401 | static HChar* check_auxmap_L1_L2_sanity ( Word* n_secmaps_found ) |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 402 | { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 403 | Word i, j; |
| 404 | /* On a 32-bit platform, the L2 and L1 tables should |
| 405 | both remain empty forever. |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 406 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 407 | On a 64-bit platform: |
| 408 | In the L2 table: |
| 409 | all .base & 0xFFFF == 0 |
| 410 | all .base > MAX_PRIMARY_ADDRESS |
| 411 | In the L1 table: |
| 412 | all .base & 0xFFFF == 0 |
| 413 | all (.base > MAX_PRIMARY_ADDRESS |
| 414 | .base & 0xFFFF == 0 |
| 415 | and .ent points to an AuxMapEnt with the same .base) |
| 416 | or |
| 417 | (.base == 0 and .ent == NULL) |
| 418 | */ |
| 419 | *n_secmaps_found = 0; |
| 420 | if (sizeof(void*) == 4) { |
| 421 | /* 32-bit platform */ |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 422 | if (VG_(OSetGen_Size)(auxmap_L2) != 0) |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 423 | return "32-bit: auxmap_L2 is non-empty"; |
| 424 | for (i = 0; i < N_AUXMAP_L1; i++) |
| 425 | if (auxmap_L1[i].base != 0 || auxmap_L1[i].ent != NULL) |
| 426 | return "32-bit: auxmap_L1 is non-empty"; |
| 427 | } else { |
| 428 | /* 64-bit platform */ |
| 429 | UWord elems_seen = 0; |
| 430 | AuxMapEnt *elem, *res; |
| 431 | AuxMapEnt key; |
| 432 | /* L2 table */ |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 433 | VG_(OSetGen_ResetIter)(auxmap_L2); |
| 434 | while ( (elem = VG_(OSetGen_Next)(auxmap_L2)) ) { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 435 | elems_seen++; |
| 436 | if (0 != (elem->base & (Addr)0xFFFF)) |
| 437 | return "64-bit: nonzero .base & 0xFFFF in auxmap_L2"; |
| 438 | if (elem->base <= MAX_PRIMARY_ADDRESS) |
| 439 | return "64-bit: .base <= MAX_PRIMARY_ADDRESS in auxmap_L2"; |
| 440 | if (elem->sm == NULL) |
| 441 | return "64-bit: .sm in _L2 is NULL"; |
| 442 | if (!is_distinguished_sm(elem->sm)) |
| 443 | (*n_secmaps_found)++; |
| 444 | } |
| 445 | if (elems_seen != n_auxmap_L2_nodes) |
| 446 | return "64-bit: disagreement on number of elems in _L2"; |
| 447 | /* Check L1-L2 correspondence */ |
| 448 | for (i = 0; i < N_AUXMAP_L1; i++) { |
| 449 | if (auxmap_L1[i].base == 0 && auxmap_L1[i].ent == NULL) |
| 450 | continue; |
| 451 | if (0 != (auxmap_L1[i].base & (Addr)0xFFFF)) |
| 452 | return "64-bit: nonzero .base & 0xFFFF in auxmap_L1"; |
| 453 | if (auxmap_L1[i].base <= MAX_PRIMARY_ADDRESS) |
| 454 | return "64-bit: .base <= MAX_PRIMARY_ADDRESS in auxmap_L1"; |
| 455 | if (auxmap_L1[i].ent == NULL) |
| 456 | return "64-bit: .ent is NULL in auxmap_L1"; |
| 457 | if (auxmap_L1[i].ent->base != auxmap_L1[i].base) |
| 458 | return "64-bit: _L1 and _L2 bases are inconsistent"; |
| 459 | /* Look it up in auxmap_L2. */ |
| 460 | key.base = auxmap_L1[i].base; |
| 461 | key.sm = 0; |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 462 | res = VG_(OSetGen_Lookup)(auxmap_L2, &key); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 463 | if (res == NULL) |
| 464 | return "64-bit: _L1 .base not found in _L2"; |
| 465 | if (res != auxmap_L1[i].ent) |
| 466 | return "64-bit: _L1 .ent disagrees with _L2 entry"; |
| 467 | } |
| 468 | /* Check L1 contains no duplicates */ |
| 469 | for (i = 0; i < N_AUXMAP_L1; i++) { |
| 470 | if (auxmap_L1[i].base == 0) |
| 471 | continue; |
| 472 | for (j = i+1; j < N_AUXMAP_L1; j++) { |
| 473 | if (auxmap_L1[j].base == 0) |
| 474 | continue; |
| 475 | if (auxmap_L1[j].base == auxmap_L1[i].base) |
| 476 | return "64-bit: duplicate _L1 .base entries"; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | return NULL; /* ok */ |
| 481 | } |
| 482 | |
| 483 | static void insert_into_auxmap_L1_at ( Word rank, AuxMapEnt* ent ) |
| 484 | { |
| 485 | Word i; |
| 486 | tl_assert(ent); |
| 487 | tl_assert(rank >= 0 && rank < N_AUXMAP_L1); |
| 488 | for (i = N_AUXMAP_L1-1; i > rank; i--) |
| 489 | auxmap_L1[i] = auxmap_L1[i-1]; |
| 490 | auxmap_L1[rank].base = ent->base; |
| 491 | auxmap_L1[rank].ent = ent; |
| 492 | } |
| 493 | |
| 494 | static INLINE AuxMapEnt* maybe_find_in_auxmap ( Addr a ) |
| 495 | { |
| 496 | AuxMapEnt key; |
| 497 | AuxMapEnt* res; |
| 498 | Word i; |
| 499 | |
| 500 | tl_assert(a > MAX_PRIMARY_ADDRESS); |
| 501 | a &= ~(Addr)0xFFFF; |
| 502 | |
| 503 | /* First search the front-cache, which is a self-organising |
| 504 | list containing the most popular entries. */ |
| 505 | |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 506 | if (LIKELY(auxmap_L1[0].base == a)) |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 507 | return auxmap_L1[0].ent; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 508 | if (LIKELY(auxmap_L1[1].base == a)) { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 509 | Addr t_base = auxmap_L1[0].base; |
| 510 | AuxMapEnt* t_ent = auxmap_L1[0].ent; |
| 511 | auxmap_L1[0].base = auxmap_L1[1].base; |
| 512 | auxmap_L1[0].ent = auxmap_L1[1].ent; |
| 513 | auxmap_L1[1].base = t_base; |
| 514 | auxmap_L1[1].ent = t_ent; |
| 515 | return auxmap_L1[0].ent; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 516 | } |
| 517 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 518 | n_auxmap_L1_searches++; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 519 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 520 | for (i = 0; i < N_AUXMAP_L1; i++) { |
| 521 | if (auxmap_L1[i].base == a) { |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | tl_assert(i >= 0 && i <= N_AUXMAP_L1); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 526 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 527 | n_auxmap_L1_cmps += (ULong)(i+1); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 528 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 529 | if (i < N_AUXMAP_L1) { |
| 530 | if (i > 0) { |
| 531 | Addr t_base = auxmap_L1[i-1].base; |
| 532 | AuxMapEnt* t_ent = auxmap_L1[i-1].ent; |
| 533 | auxmap_L1[i-1].base = auxmap_L1[i-0].base; |
| 534 | auxmap_L1[i-1].ent = auxmap_L1[i-0].ent; |
| 535 | auxmap_L1[i-0].base = t_base; |
| 536 | auxmap_L1[i-0].ent = t_ent; |
| 537 | i--; |
| 538 | } |
| 539 | return auxmap_L1[i].ent; |
| 540 | } |
| 541 | |
| 542 | n_auxmap_L2_searches++; |
| 543 | |
| 544 | /* First see if we already have it. */ |
| 545 | key.base = a; |
| 546 | key.sm = 0; |
| 547 | |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 548 | res = VG_(OSetGen_Lookup)(auxmap_L2, &key); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 549 | if (res) |
| 550 | insert_into_auxmap_L1_at( AUXMAP_L1_INSERT_IX, res ); |
| 551 | return res; |
| 552 | } |
| 553 | |
| 554 | static AuxMapEnt* find_or_alloc_in_auxmap ( Addr a ) |
| 555 | { |
| 556 | AuxMapEnt *nyu, *res; |
| 557 | |
| 558 | /* First see if we already have it. */ |
| 559 | res = maybe_find_in_auxmap( a ); |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 560 | if (LIKELY(res)) |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 561 | return res; |
| 562 | |
| 563 | /* Ok, there's no entry in the secondary map, so we'll have |
| 564 | to allocate one. */ |
| 565 | a &= ~(Addr)0xFFFF; |
| 566 | |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 567 | nyu = (AuxMapEnt*) VG_(OSetGen_AllocNode)( auxmap_L2, sizeof(AuxMapEnt) ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 568 | tl_assert(nyu); |
| 569 | nyu->base = a; |
| 570 | nyu->sm = &sm_distinguished[SM_DIST_NOACCESS]; |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 571 | VG_(OSetGen_Insert)( auxmap_L2, nyu ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 572 | insert_into_auxmap_L1_at( AUXMAP_L1_INSERT_IX, nyu ); |
| 573 | n_auxmap_L2_nodes++; |
| 574 | return nyu; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 575 | } |
| 576 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 577 | /* --------------- SecMap fundamentals --------------- */ |
| 578 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 579 | // In all these, 'low' means it's definitely in the main primary map, |
| 580 | // 'high' means it's definitely in the auxiliary table. |
| 581 | |
| 582 | static INLINE SecMap** get_secmap_low_ptr ( Addr a ) |
| 583 | { |
| 584 | UWord pm_off = a >> 16; |
| 585 | # if VG_DEBUG_MEMORY >= 1 |
| 586 | tl_assert(pm_off < N_PRIMARY_MAP); |
| 587 | # endif |
| 588 | return &primary_map[ pm_off ]; |
| 589 | } |
| 590 | |
| 591 | static INLINE SecMap** get_secmap_high_ptr ( Addr a ) |
| 592 | { |
| 593 | AuxMapEnt* am = find_or_alloc_in_auxmap(a); |
| 594 | return &am->sm; |
| 595 | } |
| 596 | |
| 597 | static SecMap** get_secmap_ptr ( Addr a ) |
| 598 | { |
| 599 | return ( a <= MAX_PRIMARY_ADDRESS |
| 600 | ? get_secmap_low_ptr(a) |
| 601 | : get_secmap_high_ptr(a)); |
| 602 | } |
| 603 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 604 | static INLINE SecMap* get_secmap_for_reading_low ( Addr a ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 605 | { |
| 606 | return *get_secmap_low_ptr(a); |
| 607 | } |
| 608 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 609 | static INLINE SecMap* get_secmap_for_reading_high ( Addr a ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 610 | { |
| 611 | return *get_secmap_high_ptr(a); |
| 612 | } |
| 613 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 614 | static INLINE SecMap* get_secmap_for_writing_low(Addr a) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 615 | { |
| 616 | SecMap** p = get_secmap_low_ptr(a); |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 617 | if (UNLIKELY(is_distinguished_sm(*p))) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 618 | *p = copy_for_writing(*p); |
| 619 | return *p; |
| 620 | } |
| 621 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 622 | static INLINE SecMap* get_secmap_for_writing_high ( Addr a ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 623 | { |
| 624 | SecMap** p = get_secmap_high_ptr(a); |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 625 | if (UNLIKELY(is_distinguished_sm(*p))) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 626 | *p = copy_for_writing(*p); |
| 627 | return *p; |
| 628 | } |
| 629 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 630 | /* Produce the secmap for 'a', either from the primary map or by |
| 631 | ensuring there is an entry for it in the aux primary map. The |
| 632 | secmap may be a distinguished one as the caller will only want to |
| 633 | be able to read it. |
| 634 | */ |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 635 | static INLINE SecMap* get_secmap_for_reading ( Addr a ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 636 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 637 | return ( a <= MAX_PRIMARY_ADDRESS |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 638 | ? get_secmap_for_reading_low (a) |
| 639 | : get_secmap_for_reading_high(a) ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* Produce the secmap for 'a', either from the primary map or by |
| 643 | ensuring there is an entry for it in the aux primary map. The |
| 644 | secmap may not be a distinguished one, since the caller will want |
| 645 | to be able to write it. If it is a distinguished secondary, make a |
| 646 | writable copy of it, install it, and return the copy instead. (COW |
| 647 | semantics). |
| 648 | */ |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 649 | static SecMap* get_secmap_for_writing ( Addr a ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 650 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 651 | return ( a <= MAX_PRIMARY_ADDRESS |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 652 | ? get_secmap_for_writing_low (a) |
| 653 | : get_secmap_for_writing_high(a) ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /* If 'a' has a SecMap, produce it. Else produce NULL. But don't |
| 657 | allocate one if one doesn't already exist. This is used by the |
| 658 | leak checker. |
| 659 | */ |
| 660 | static SecMap* maybe_get_secmap_for ( Addr a ) |
| 661 | { |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 662 | if (a <= MAX_PRIMARY_ADDRESS) { |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 663 | return get_secmap_for_reading_low(a); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 664 | } else { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 665 | AuxMapEnt* am = maybe_find_in_auxmap(a); |
| 666 | return am ? am->sm : NULL; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 670 | /* --------------- Fundamental functions --------------- */ |
| 671 | |
| 672 | static INLINE |
| 673 | void insert_vabits2_into_vabits8 ( Addr a, UChar vabits2, UChar* vabits8 ) |
| 674 | { |
| 675 | UInt shift = (a & 3) << 1; // shift by 0, 2, 4, or 6 |
| 676 | *vabits8 &= ~(0x3 << shift); // mask out the two old bits |
| 677 | *vabits8 |= (vabits2 << shift); // mask in the two new bits |
| 678 | } |
| 679 | |
| 680 | static INLINE |
| 681 | void insert_vabits4_into_vabits8 ( Addr a, UChar vabits4, UChar* vabits8 ) |
| 682 | { |
| 683 | UInt shift; |
| 684 | tl_assert(VG_IS_2_ALIGNED(a)); // Must be 2-aligned |
| 685 | shift = (a & 2) << 1; // shift by 0 or 4 |
| 686 | *vabits8 &= ~(0xf << shift); // mask out the four old bits |
| 687 | *vabits8 |= (vabits4 << shift); // mask in the four new bits |
| 688 | } |
| 689 | |
| 690 | static INLINE |
| 691 | UChar extract_vabits2_from_vabits8 ( Addr a, UChar vabits8 ) |
| 692 | { |
| 693 | UInt shift = (a & 3) << 1; // shift by 0, 2, 4, or 6 |
| 694 | vabits8 >>= shift; // shift the two bits to the bottom |
| 695 | return 0x3 & vabits8; // mask out the rest |
| 696 | } |
| 697 | |
| 698 | static INLINE |
| 699 | UChar extract_vabits4_from_vabits8 ( Addr a, UChar vabits8 ) |
| 700 | { |
| 701 | UInt shift; |
| 702 | tl_assert(VG_IS_2_ALIGNED(a)); // Must be 2-aligned |
| 703 | shift = (a & 2) << 1; // shift by 0 or 4 |
| 704 | vabits8 >>= shift; // shift the four bits to the bottom |
| 705 | return 0xf & vabits8; // mask out the rest |
| 706 | } |
| 707 | |
| 708 | // Note that these four are only used in slow cases. The fast cases do |
| 709 | // clever things like combine the auxmap check (in |
| 710 | // get_secmap_{read,writ}able) with alignment checks. |
| 711 | |
| 712 | // *** WARNING! *** |
| 713 | // Any time this function is called, if it is possible that vabits2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 714 | // is equal to VA_BITS2_PARTDEFINED, then the corresponding entry in the |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 715 | // sec-V-bits table must also be set! |
| 716 | static INLINE |
| 717 | void set_vabits2 ( Addr a, UChar vabits2 ) |
| 718 | { |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 719 | SecMap* sm = get_secmap_for_writing(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 720 | UWord sm_off = SM_OFF(a); |
| 721 | insert_vabits2_into_vabits8( a, vabits2, &(sm->vabits8[sm_off]) ); |
| 722 | } |
| 723 | |
| 724 | static INLINE |
| 725 | UChar get_vabits2 ( Addr a ) |
| 726 | { |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 727 | SecMap* sm = get_secmap_for_reading(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 728 | UWord sm_off = SM_OFF(a); |
| 729 | UChar vabits8 = sm->vabits8[sm_off]; |
| 730 | return extract_vabits2_from_vabits8(a, vabits8); |
| 731 | } |
| 732 | |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 733 | // *** WARNING! *** |
| 734 | // Any time this function is called, if it is possible that any of the |
| 735 | // 4 2-bit fields in vabits8 are equal to VA_BITS2_PARTDEFINED, then the |
| 736 | // corresponding entry(s) in the sec-V-bits table must also be set! |
| 737 | static INLINE |
| 738 | UChar get_vabits8_for_aligned_word32 ( Addr a ) |
| 739 | { |
| 740 | SecMap* sm = get_secmap_for_reading(a); |
| 741 | UWord sm_off = SM_OFF(a); |
| 742 | UChar vabits8 = sm->vabits8[sm_off]; |
| 743 | return vabits8; |
| 744 | } |
| 745 | |
| 746 | static INLINE |
| 747 | void set_vabits8_for_aligned_word32 ( Addr a, UChar vabits8 ) |
| 748 | { |
| 749 | SecMap* sm = get_secmap_for_writing(a); |
| 750 | UWord sm_off = SM_OFF(a); |
| 751 | sm->vabits8[sm_off] = vabits8; |
| 752 | } |
| 753 | |
| 754 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 755 | // Forward declarations |
| 756 | static UWord get_sec_vbits8(Addr a); |
| 757 | static void set_sec_vbits8(Addr a, UWord vbits8); |
| 758 | |
| 759 | // Returns False if there was an addressability error. |
| 760 | static INLINE |
| 761 | Bool set_vbits8 ( Addr a, UChar vbits8 ) |
| 762 | { |
| 763 | Bool ok = True; |
| 764 | UChar vabits2 = get_vabits2(a); |
| 765 | if ( VA_BITS2_NOACCESS != vabits2 ) { |
| 766 | // Addressable. Convert in-register format to in-memory format. |
| 767 | // Also remove any existing sec V bit entry for the byte if no |
| 768 | // longer necessary. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 769 | if ( V_BITS8_DEFINED == vbits8 ) { vabits2 = VA_BITS2_DEFINED; } |
| 770 | else if ( V_BITS8_UNDEFINED == vbits8 ) { vabits2 = VA_BITS2_UNDEFINED; } |
| 771 | else { vabits2 = VA_BITS2_PARTDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 772 | set_sec_vbits8(a, vbits8); } |
| 773 | set_vabits2(a, vabits2); |
| 774 | |
| 775 | } else { |
| 776 | // Unaddressable! Do nothing -- when writing to unaddressable |
| 777 | // memory it acts as a black hole, and the V bits can never be seen |
| 778 | // again. So we don't have to write them at all. |
| 779 | ok = False; |
| 780 | } |
| 781 | return ok; |
| 782 | } |
| 783 | |
| 784 | // Returns False if there was an addressability error. In that case, we put |
| 785 | // all defined bits into vbits8. |
| 786 | static INLINE |
| 787 | Bool get_vbits8 ( Addr a, UChar* vbits8 ) |
| 788 | { |
| 789 | Bool ok = True; |
| 790 | UChar vabits2 = get_vabits2(a); |
| 791 | |
| 792 | // Convert the in-memory format to in-register format. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 793 | if ( VA_BITS2_DEFINED == vabits2 ) { *vbits8 = V_BITS8_DEFINED; } |
| 794 | else if ( VA_BITS2_UNDEFINED == vabits2 ) { *vbits8 = V_BITS8_UNDEFINED; } |
| 795 | else if ( VA_BITS2_NOACCESS == vabits2 ) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 796 | *vbits8 = V_BITS8_DEFINED; // Make V bits defined! |
| 797 | ok = False; |
| 798 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 799 | tl_assert( VA_BITS2_PARTDEFINED == vabits2 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 800 | *vbits8 = get_sec_vbits8(a); |
| 801 | } |
| 802 | return ok; |
| 803 | } |
| 804 | |
| 805 | |
| 806 | /* --------------- Secondary V bit table ------------ */ |
| 807 | |
| 808 | // This table holds the full V bit pattern for partially-defined bytes |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 809 | // (PDBs) that are represented by VA_BITS2_PARTDEFINED in the main shadow |
| 810 | // memory. |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 811 | // |
| 812 | // Note: the nodes in this table can become stale. Eg. if you write a PDB, |
| 813 | // then overwrite the same address with a fully defined byte, the sec-V-bit |
| 814 | // node will not necessarily be removed. This is because checking for |
| 815 | // whether removal is necessary would slow down the fast paths. |
| 816 | // |
| 817 | // To avoid the stale nodes building up too much, we periodically (once the |
| 818 | // table reaches a certain size) garbage collect (GC) the table by |
| 819 | // traversing it and evicting any "sufficiently stale" nodes, ie. nodes that |
| 820 | // are stale and haven't been touched for a certain number of collections. |
| 821 | // If more than a certain proportion of nodes survived, we increase the |
| 822 | // table size so that GCs occur less often. |
| 823 | // |
| 824 | // (So this a bit different to a traditional GC, where you definitely want |
| 825 | // to remove any dead nodes. It's more like we have a resizable cache and |
| 826 | // we're trying to find the right balance how many elements to evict and how |
| 827 | // big to make the cache.) |
| 828 | // |
| 829 | // This policy is designed to avoid bad table bloat in the worst case where |
| 830 | // a program creates huge numbers of stale PDBs -- we would get this bloat |
| 831 | // if we had no GC -- while handling well the case where a node becomes |
| 832 | // stale but shortly afterwards is rewritten with a PDB and so becomes |
| 833 | // non-stale again (which happens quite often, eg. in perf/bz2). If we just |
| 834 | // remove all stale nodes as soon as possible, we just end up re-adding a |
| 835 | // lot of them in later again. The "sufficiently stale" approach avoids |
| 836 | // this. (If a program has many live PDBs, performance will just suck, |
| 837 | // there's no way around that.) |
| 838 | |
| 839 | static OSet* secVBitTable; |
| 840 | |
| 841 | // Stats |
| 842 | static ULong sec_vbits_new_nodes = 0; |
| 843 | static ULong sec_vbits_updates = 0; |
| 844 | |
| 845 | // This must be a power of two; this is checked in mc_pre_clo_init(). |
| 846 | // The size chosen here is a trade-off: if the nodes are bigger (ie. cover |
| 847 | // a larger address range) they take more space but we can get multiple |
| 848 | // partially-defined bytes in one if they are close to each other, reducing |
| 849 | // the number of total nodes. In practice sometimes they are clustered (eg. |
| 850 | // perf/bz2 repeatedly writes then reads more than 20,000 in a contiguous |
| 851 | // row), but often not. So we choose something intermediate. |
| 852 | #define BYTES_PER_SEC_VBIT_NODE 16 |
| 853 | |
| 854 | // We make the table bigger if more than this many nodes survive a GC. |
| 855 | #define MAX_SURVIVOR_PROPORTION 0.5 |
| 856 | |
| 857 | // Each time we make the table bigger, we increase it by this much. |
| 858 | #define TABLE_GROWTH_FACTOR 2 |
| 859 | |
| 860 | // This defines "sufficiently stale" -- any node that hasn't been touched in |
| 861 | // this many GCs will be removed. |
| 862 | #define MAX_STALE_AGE 2 |
| 863 | |
| 864 | // We GC the table when it gets this many nodes in it, ie. it's effectively |
| 865 | // the table size. It can change. |
| 866 | static Int secVBitLimit = 1024; |
| 867 | |
| 868 | // The number of GCs done, used to age sec-V-bit nodes for eviction. |
| 869 | // Because it's unsigned, wrapping doesn't matter -- the right answer will |
| 870 | // come out anyway. |
| 871 | static UInt GCs_done = 0; |
| 872 | |
| 873 | typedef |
| 874 | struct { |
| 875 | Addr a; |
| 876 | UChar vbits8[BYTES_PER_SEC_VBIT_NODE]; |
| 877 | UInt last_touched; |
| 878 | } |
| 879 | SecVBitNode; |
| 880 | |
| 881 | static OSet* createSecVBitTable(void) |
| 882 | { |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 883 | return VG_(OSetGen_Create)( offsetof(SecVBitNode, a), |
| 884 | NULL, // use fast comparisons |
| 885 | VG_(malloc), VG_(free) ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | static void gcSecVBitTable(void) |
| 889 | { |
| 890 | OSet* secVBitTable2; |
| 891 | SecVBitNode* n; |
| 892 | Int i, n_nodes = 0, n_survivors = 0; |
| 893 | |
| 894 | GCs_done++; |
| 895 | |
| 896 | // Create the new table. |
| 897 | secVBitTable2 = createSecVBitTable(); |
| 898 | |
| 899 | // Traverse the table, moving fresh nodes into the new table. |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 900 | VG_(OSetGen_ResetIter)(secVBitTable); |
| 901 | while ( (n = VG_(OSetGen_Next)(secVBitTable)) ) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 902 | Bool keep = False; |
| 903 | if ( (GCs_done - n->last_touched) <= MAX_STALE_AGE ) { |
| 904 | // Keep node if it's been touched recently enough (regardless of |
| 905 | // freshness/staleness). |
| 906 | keep = True; |
| 907 | } else { |
| 908 | // Keep node if any of its bytes are non-stale. Using |
| 909 | // get_vabits2() for the lookup is not very efficient, but I don't |
| 910 | // think it matters. |
| 911 | for (i = 0; i < BYTES_PER_SEC_VBIT_NODE; i++) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 912 | if (VA_BITS2_PARTDEFINED == get_vabits2(n->a + i)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 913 | keep = True; // Found a non-stale byte, so keep |
| 914 | break; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | if ( keep ) { |
| 920 | // Insert a copy of the node into the new table. |
| 921 | SecVBitNode* n2 = |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 922 | VG_(OSetGen_AllocNode)(secVBitTable2, sizeof(SecVBitNode)); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 923 | *n2 = *n; |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 924 | VG_(OSetGen_Insert)(secVBitTable2, n2); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | // Get the before and after sizes. |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 929 | n_nodes = VG_(OSetGen_Size)(secVBitTable); |
| 930 | n_survivors = VG_(OSetGen_Size)(secVBitTable2); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 931 | |
| 932 | // Destroy the old table, and put the new one in its place. |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 933 | VG_(OSetGen_Destroy)(secVBitTable); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 934 | secVBitTable = secVBitTable2; |
| 935 | |
| 936 | if (VG_(clo_verbosity) > 1) { |
| 937 | Char percbuf[6]; |
| 938 | VG_(percentify)(n_survivors, n_nodes, 1, 6, percbuf); |
| 939 | VG_(message)(Vg_DebugMsg, "memcheck GC: %d nodes, %d survivors (%s)", |
| 940 | n_nodes, n_survivors, percbuf); |
| 941 | } |
| 942 | |
| 943 | // Increase table size if necessary. |
| 944 | if (n_survivors > (secVBitLimit * MAX_SURVIVOR_PROPORTION)) { |
| 945 | secVBitLimit *= TABLE_GROWTH_FACTOR; |
| 946 | if (VG_(clo_verbosity) > 1) |
| 947 | VG_(message)(Vg_DebugMsg, "memcheck GC: increase table size to %d", |
| 948 | secVBitLimit); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | static UWord get_sec_vbits8(Addr a) |
| 953 | { |
| 954 | Addr aAligned = VG_ROUNDDN(a, BYTES_PER_SEC_VBIT_NODE); |
| 955 | Int amod = a % BYTES_PER_SEC_VBIT_NODE; |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 956 | SecVBitNode* n = VG_(OSetGen_Lookup)(secVBitTable, &aAligned); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 957 | UChar vbits8; |
| 958 | tl_assert2(n, "get_sec_vbits8: no node for address %p (%p)\n", aAligned, a); |
| 959 | // Shouldn't be fully defined or fully undefined -- those cases shouldn't |
| 960 | // make it to the secondary V bits table. |
| 961 | vbits8 = n->vbits8[amod]; |
| 962 | tl_assert(V_BITS8_DEFINED != vbits8 && V_BITS8_UNDEFINED != vbits8); |
| 963 | return vbits8; |
| 964 | } |
| 965 | |
| 966 | static void set_sec_vbits8(Addr a, UWord vbits8) |
| 967 | { |
| 968 | Addr aAligned = VG_ROUNDDN(a, BYTES_PER_SEC_VBIT_NODE); |
| 969 | Int i, amod = a % BYTES_PER_SEC_VBIT_NODE; |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 970 | SecVBitNode* n = VG_(OSetGen_Lookup)(secVBitTable, &aAligned); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 971 | // Shouldn't be fully defined or fully undefined -- those cases shouldn't |
| 972 | // make it to the secondary V bits table. |
| 973 | tl_assert(V_BITS8_DEFINED != vbits8 && V_BITS8_UNDEFINED != vbits8); |
| 974 | if (n) { |
| 975 | n->vbits8[amod] = vbits8; // update |
| 976 | n->last_touched = GCs_done; |
| 977 | sec_vbits_updates++; |
| 978 | } else { |
| 979 | // New node: assign the specific byte, make the rest invalid (they |
| 980 | // should never be read as-is, but be cautious). |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 981 | n = VG_(OSetGen_AllocNode)(secVBitTable, sizeof(SecVBitNode)); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 982 | n->a = aAligned; |
| 983 | for (i = 0; i < BYTES_PER_SEC_VBIT_NODE; i++) { |
| 984 | n->vbits8[i] = V_BITS8_UNDEFINED; |
| 985 | } |
| 986 | n->vbits8[amod] = vbits8; |
| 987 | n->last_touched = GCs_done; |
| 988 | |
| 989 | // Do a table GC if necessary. Nb: do this before inserting the new |
| 990 | // node, to avoid erroneously GC'ing the new node. |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 991 | if (secVBitLimit == VG_(OSetGen_Size)(secVBitTable)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 992 | gcSecVBitTable(); |
| 993 | } |
| 994 | |
| 995 | // Insert the new node. |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 996 | VG_(OSetGen_Insert)(secVBitTable, n); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 997 | sec_vbits_new_nodes++; |
| 998 | |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 999 | n_secVBit_nodes = VG_(OSetGen_Size)(secVBitTable); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1000 | if (n_secVBit_nodes > max_secVBit_nodes) |
| 1001 | max_secVBit_nodes = n_secVBit_nodes; |
| 1002 | } |
| 1003 | } |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1004 | |
| 1005 | /* --------------- Endianness helpers --------------- */ |
| 1006 | |
| 1007 | /* Returns the offset in memory of the byteno-th most significant byte |
| 1008 | in a wordszB-sized word, given the specified endianness. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1009 | static INLINE UWord byte_offset_w ( UWord wordszB, Bool bigendian, |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1010 | UWord byteno ) { |
| 1011 | return bigendian ? (wordszB-1-byteno) : byteno; |
| 1012 | } |
| 1013 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1014 | |
| 1015 | /* --------------- Ignored address ranges --------------- */ |
| 1016 | |
| 1017 | #define M_IGNORE_RANGES 4 |
| 1018 | |
| 1019 | typedef |
| 1020 | struct { |
| 1021 | Int used; |
| 1022 | Addr start[M_IGNORE_RANGES]; |
| 1023 | Addr end[M_IGNORE_RANGES]; |
| 1024 | } |
| 1025 | IgnoreRanges; |
| 1026 | |
| 1027 | static IgnoreRanges ignoreRanges; |
| 1028 | |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 1029 | INLINE Bool MC_(in_ignored_range) ( Addr a ) |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1030 | { |
| 1031 | Int i; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1032 | if (LIKELY(ignoreRanges.used == 0)) |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1033 | return False; |
| 1034 | for (i = 0; i < ignoreRanges.used; i++) { |
| 1035 | if (a >= ignoreRanges.start[i] && a < ignoreRanges.end[i]) |
| 1036 | return True; |
| 1037 | } |
| 1038 | return False; |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | /* Parse a 32- or 64-bit hex number, including leading 0x, from string |
| 1043 | starting at *ppc, putting result in *result, and return True. Or |
| 1044 | fail, in which case *ppc and *result are undefined, and return |
| 1045 | False. */ |
| 1046 | |
| 1047 | static Bool isHex ( UChar c ) |
| 1048 | { |
| 1049 | return ((c >= '0' && c <= '9') |
| 1050 | || (c >= 'a' && c <= 'f') |
| 1051 | || (c >= 'A' && c <= 'F')); |
| 1052 | } |
| 1053 | |
| 1054 | static UInt fromHex ( UChar c ) |
| 1055 | { |
| 1056 | if (c >= '0' && c <= '9') |
| 1057 | return (UInt)c - (UInt)'0'; |
| 1058 | if (c >= 'a' && c <= 'f') |
| 1059 | return 10 + (UInt)c - (UInt)'a'; |
| 1060 | if (c >= 'A' && c <= 'F') |
| 1061 | return 10 + (UInt)c - (UInt)'A'; |
| 1062 | /*NOTREACHED*/ |
| 1063 | tl_assert(0); |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | static Bool parse_Addr ( UChar** ppc, Addr* result ) |
| 1068 | { |
| 1069 | Int used, limit = 2 * sizeof(Addr); |
| 1070 | if (**ppc != '0') |
| 1071 | return False; |
| 1072 | (*ppc)++; |
| 1073 | if (**ppc != 'x') |
| 1074 | return False; |
| 1075 | (*ppc)++; |
| 1076 | *result = 0; |
| 1077 | used = 0; |
| 1078 | while (isHex(**ppc)) { |
| 1079 | UInt d = fromHex(**ppc); |
| 1080 | tl_assert(d < 16); |
| 1081 | *result = ((*result) << 4) | fromHex(**ppc); |
| 1082 | (*ppc)++; |
| 1083 | used++; |
| 1084 | if (used > limit) return False; |
| 1085 | } |
| 1086 | if (used == 0) |
| 1087 | return False; |
| 1088 | return True; |
| 1089 | } |
| 1090 | |
| 1091 | /* Parse two such numbers separated by a dash, or fail. */ |
| 1092 | |
| 1093 | static Bool parse_range ( UChar** ppc, Addr* result1, Addr* result2 ) |
| 1094 | { |
| 1095 | Bool ok = parse_Addr(ppc, result1); |
| 1096 | if (!ok) |
| 1097 | return False; |
| 1098 | if (**ppc != '-') |
| 1099 | return False; |
| 1100 | (*ppc)++; |
| 1101 | ok = parse_Addr(ppc, result2); |
| 1102 | if (!ok) |
| 1103 | return False; |
| 1104 | return True; |
| 1105 | } |
| 1106 | |
| 1107 | /* Parse a set of ranges separated by commas into 'ignoreRanges', or |
| 1108 | fail. */ |
| 1109 | |
| 1110 | static Bool parse_ignore_ranges ( UChar* str0 ) |
| 1111 | { |
| 1112 | Addr start, end; |
| 1113 | Bool ok; |
| 1114 | UChar* str = str0; |
| 1115 | UChar** ppc = &str; |
| 1116 | ignoreRanges.used = 0; |
| 1117 | while (1) { |
| 1118 | ok = parse_range(ppc, &start, &end); |
| 1119 | if (!ok) |
| 1120 | return False; |
| 1121 | if (ignoreRanges.used >= M_IGNORE_RANGES) |
| 1122 | return False; |
| 1123 | ignoreRanges.start[ignoreRanges.used] = start; |
| 1124 | ignoreRanges.end[ignoreRanges.used] = end; |
| 1125 | ignoreRanges.used++; |
| 1126 | if (**ppc == 0) |
| 1127 | return True; |
| 1128 | if (**ppc != ',') |
| 1129 | return False; |
| 1130 | (*ppc)++; |
| 1131 | } |
| 1132 | /*NOTREACHED*/ |
| 1133 | return False; |
| 1134 | } |
| 1135 | |
| 1136 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1137 | /* --------------- Load/store slow cases. --------------- */ |
| 1138 | |
| 1139 | static |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1140 | #ifndef PERF_FAST_LOADV |
| 1141 | INLINE |
| 1142 | #endif |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 1143 | ULong mc_LOADVn_slow ( Addr a, SizeT nBits, Bool bigendian ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1144 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1145 | /* Make up a 64-bit result V word, which contains the loaded data for |
sewardj | f3d57dd | 2005-04-22 20:23:27 +0000 | [diff] [blame] | 1146 | valid addresses and Defined for invalid addresses. Iterate over |
| 1147 | the bytes in the word, from the most significant down to the |
| 1148 | least. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1149 | ULong vbits64 = V_BITS64_UNDEFINED; |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 1150 | SizeT szB = nBits / 8; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1151 | SSizeT i = szB-1; // Must be signed |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1152 | SizeT n_addrs_bad = 0; |
| 1153 | Addr ai; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1154 | Bool partial_load_exemption_applies; |
| 1155 | UChar vbits8; |
| 1156 | Bool ok; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1157 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 1158 | PROF_EVENT(30, "mc_LOADVn_slow"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1159 | |
| 1160 | /* ------------ BEGIN semi-fast cases ------------ */ |
| 1161 | /* These deal quickly-ish with the common auxiliary primary map |
| 1162 | cases on 64-bit platforms. Are merely a speedup hack; can be |
| 1163 | omitted without loss of correctness/functionality. Note that in |
| 1164 | both cases the "sizeof(void*) == 8" causes these cases to be |
| 1165 | folded out by compilers on 32-bit platforms. These are derived |
| 1166 | from LOADV64 and LOADV32. |
| 1167 | */ |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1168 | if (LIKELY(sizeof(void*) == 8 |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1169 | && nBits == 64 && VG_IS_8_ALIGNED(a))) { |
| 1170 | SecMap* sm = get_secmap_for_reading(a); |
| 1171 | UWord sm_off16 = SM_OFF_16(a); |
| 1172 | UWord vabits16 = ((UShort*)(sm->vabits8))[sm_off16]; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1173 | if (LIKELY(vabits16 == VA_BITS16_DEFINED)) |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1174 | return V_BITS64_DEFINED; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1175 | if (LIKELY(vabits16 == VA_BITS16_UNDEFINED)) |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1176 | return V_BITS64_UNDEFINED; |
| 1177 | /* else fall into the slow case */ |
| 1178 | } |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1179 | if (LIKELY(sizeof(void*) == 8 |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1180 | && nBits == 32 && VG_IS_4_ALIGNED(a))) { |
| 1181 | SecMap* sm = get_secmap_for_reading(a); |
| 1182 | UWord sm_off = SM_OFF(a); |
| 1183 | UWord vabits8 = sm->vabits8[sm_off]; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1184 | if (LIKELY(vabits8 == VA_BITS8_DEFINED)) |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1185 | return ((UWord)0xFFFFFFFF00000000ULL | (UWord)V_BITS32_DEFINED); |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1186 | if (LIKELY(vabits8 == VA_BITS8_UNDEFINED)) |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1187 | return ((UWord)0xFFFFFFFF00000000ULL | (UWord)V_BITS32_UNDEFINED); |
| 1188 | /* else fall into slow case */ |
| 1189 | } |
| 1190 | /* ------------ END semi-fast cases ------------ */ |
| 1191 | |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 1192 | tl_assert(nBits == 64 || nBits == 32 || nBits == 16 || nBits == 8); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1193 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1194 | for (i = szB-1; i >= 0; i--) { |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 1195 | PROF_EVENT(31, "mc_LOADVn_slow(loop)"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 1196 | ai = a + byte_offset_w(szB, bigendian, i); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1197 | ok = get_vbits8(ai, &vbits8); |
| 1198 | if (!ok) n_addrs_bad++; |
| 1199 | vbits64 <<= 8; |
| 1200 | vbits64 |= vbits8; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
sewardj | 0ded7a4 | 2005-11-08 02:25:37 +0000 | [diff] [blame] | 1203 | /* This is a hack which avoids producing errors for code which |
| 1204 | insists in stepping along byte strings in aligned word-sized |
| 1205 | chunks, and there is a partially defined word at the end. (eg, |
| 1206 | optimised strlen). Such code is basically broken at least WRT |
| 1207 | semantics of ANSI C, but sometimes users don't have the option |
| 1208 | to fix it, and so this option is provided. Note it is now |
| 1209 | defaulted to not-engaged. |
| 1210 | |
| 1211 | A load from a partially-addressible place is allowed if: |
| 1212 | - the command-line flag is set |
| 1213 | - it's a word-sized, word-aligned load |
| 1214 | - at least one of the addresses in the word *is* valid |
| 1215 | */ |
| 1216 | partial_load_exemption_applies |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1217 | = MC_(clo_partial_loads_ok) && szB == VG_WORDSIZE |
sewardj | 0ded7a4 | 2005-11-08 02:25:37 +0000 | [diff] [blame] | 1218 | && VG_IS_WORD_ALIGNED(a) |
| 1219 | && n_addrs_bad < VG_WORDSIZE; |
| 1220 | |
| 1221 | if (n_addrs_bad > 0 && !partial_load_exemption_applies) |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 1222 | MC_(record_address_error)( VG_(get_running_tid)(), a, szB, False ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1223 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1224 | return vbits64; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1228 | static |
| 1229 | #ifndef PERF_FAST_STOREV |
| 1230 | INLINE |
| 1231 | #endif |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 1232 | void mc_STOREVn_slow ( Addr a, SizeT nBits, ULong vbytes, Bool bigendian ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1233 | { |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 1234 | SizeT szB = nBits / 8; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1235 | SizeT i, n_addrs_bad = 0; |
| 1236 | UChar vbits8; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1237 | Addr ai; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1238 | Bool ok; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1239 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 1240 | PROF_EVENT(35, "mc_STOREVn_slow"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* ------------ BEGIN semi-fast cases ------------ */ |
| 1243 | /* These deal quickly-ish with the common auxiliary primary map |
| 1244 | cases on 64-bit platforms. Are merely a speedup hack; can be |
| 1245 | omitted without loss of correctness/functionality. Note that in |
| 1246 | both cases the "sizeof(void*) == 8" causes these cases to be |
| 1247 | folded out by compilers on 32-bit platforms. These are derived |
| 1248 | from STOREV64 and STOREV32. |
| 1249 | */ |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1250 | if (LIKELY(sizeof(void*) == 8 |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1251 | && nBits == 64 && VG_IS_8_ALIGNED(a))) { |
| 1252 | SecMap* sm = get_secmap_for_reading(a); |
| 1253 | UWord sm_off16 = SM_OFF_16(a); |
| 1254 | UWord vabits16 = ((UShort*)(sm->vabits8))[sm_off16]; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1255 | if (LIKELY( !is_distinguished_sm(sm) && |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1256 | (VA_BITS16_DEFINED == vabits16 || |
| 1257 | VA_BITS16_UNDEFINED == vabits16) )) { |
| 1258 | /* Handle common case quickly: a is suitably aligned, */ |
| 1259 | /* is mapped, and is addressible. */ |
| 1260 | // Convert full V-bits in register to compact 2-bit form. |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1261 | if (LIKELY(V_BITS64_DEFINED == vbytes)) { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1262 | ((UShort*)(sm->vabits8))[sm_off16] = (UShort)VA_BITS16_DEFINED; |
| 1263 | return; |
| 1264 | } else if (V_BITS64_UNDEFINED == vbytes) { |
| 1265 | ((UShort*)(sm->vabits8))[sm_off16] = (UShort)VA_BITS16_UNDEFINED; |
| 1266 | return; |
| 1267 | } |
| 1268 | /* else fall into the slow case */ |
| 1269 | } |
| 1270 | /* else fall into the slow case */ |
| 1271 | } |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1272 | if (LIKELY(sizeof(void*) == 8 |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1273 | && nBits == 32 && VG_IS_4_ALIGNED(a))) { |
| 1274 | SecMap* sm = get_secmap_for_reading(a); |
| 1275 | UWord sm_off = SM_OFF(a); |
| 1276 | UWord vabits8 = sm->vabits8[sm_off]; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1277 | if (LIKELY( !is_distinguished_sm(sm) && |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1278 | (VA_BITS8_DEFINED == vabits8 || |
| 1279 | VA_BITS8_UNDEFINED == vabits8) )) { |
| 1280 | /* Handle common case quickly: a is suitably aligned, */ |
| 1281 | /* is mapped, and is addressible. */ |
| 1282 | // Convert full V-bits in register to compact 2-bit form. |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1283 | if (LIKELY(V_BITS32_DEFINED == (vbytes & 0xFFFFFFFF))) { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 1284 | sm->vabits8[sm_off] = VA_BITS8_DEFINED; |
| 1285 | return; |
| 1286 | } else if (V_BITS32_UNDEFINED == (vbytes & 0xFFFFFFFF)) { |
| 1287 | sm->vabits8[sm_off] = VA_BITS8_UNDEFINED; |
| 1288 | return; |
| 1289 | } |
| 1290 | /* else fall into the slow case */ |
| 1291 | } |
| 1292 | /* else fall into the slow case */ |
| 1293 | } |
| 1294 | /* ------------ END semi-fast cases ------------ */ |
| 1295 | |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 1296 | tl_assert(nBits == 64 || nBits == 32 || nBits == 16 || nBits == 8); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1297 | |
| 1298 | /* Dump vbytes in memory, iterating from least to most significant |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 1299 | byte. At the same time establish addressibility of the location. */ |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1300 | for (i = 0; i < szB; i++) { |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 1301 | PROF_EVENT(36, "mc_STOREVn_slow(loop)"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 1302 | ai = a + byte_offset_w(szB, bigendian, i); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1303 | vbits8 = vbytes & 0xff; |
| 1304 | ok = set_vbits8(ai, vbits8); |
| 1305 | if (!ok) n_addrs_bad++; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1306 | vbytes >>= 8; |
| 1307 | } |
| 1308 | |
| 1309 | /* If an address error has happened, report it. */ |
| 1310 | if (n_addrs_bad > 0) |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 1311 | MC_(record_address_error)( VG_(get_running_tid)(), a, szB, True ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1315 | /*------------------------------------------------------------*/ |
| 1316 | /*--- Setting permissions over address ranges. ---*/ |
| 1317 | /*------------------------------------------------------------*/ |
| 1318 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1319 | static void set_address_range_perms ( Addr a, SizeT lenT, UWord vabits16, |
| 1320 | UWord dsm_num ) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1321 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1322 | UWord sm_off, sm_off16; |
| 1323 | UWord vabits2 = vabits16 & 0x3; |
| 1324 | SizeT lenA, lenB, len_to_next_secmap; |
| 1325 | Addr aNext; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 1326 | SecMap* sm; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1327 | SecMap** sm_ptr; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 1328 | SecMap* example_dsm; |
| 1329 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1330 | PROF_EVENT(150, "set_address_range_perms"); |
| 1331 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1332 | /* Check the V+A bits make sense. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1333 | tl_assert(VA_BITS16_NOACCESS == vabits16 || |
| 1334 | VA_BITS16_UNDEFINED == vabits16 || |
| 1335 | VA_BITS16_DEFINED == vabits16); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1336 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1337 | // This code should never write PDBs; ensure this. (See comment above |
| 1338 | // set_vabits2().) |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1339 | tl_assert(VA_BITS2_PARTDEFINED != vabits2); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1340 | |
| 1341 | if (lenT == 0) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1342 | return; |
| 1343 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1344 | if (lenT > 100 * 1000 * 1000) { |
| 1345 | if (VG_(clo_verbosity) > 0 && !VG_(clo_xml)) { |
| 1346 | Char* s = "unknown???"; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1347 | if (vabits16 == VA_BITS16_NOACCESS ) s = "noaccess"; |
| 1348 | if (vabits16 == VA_BITS16_UNDEFINED) s = "undefined"; |
| 1349 | if (vabits16 == VA_BITS16_DEFINED ) s = "defined"; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1350 | VG_(message)(Vg_UserMsg, "Warning: set address range perms: " |
| 1351 | "large range %lu (%s)", lenT, s); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1355 | #ifndef PERF_FAST_SARP |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1356 | /*------------------ debug-only case ------------------ */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1357 | { |
| 1358 | // Endianness doesn't matter here because all bytes are being set to |
| 1359 | // the same value. |
| 1360 | // Nb: We don't have to worry about updating the sec-V-bits table |
| 1361 | // after these set_vabits2() calls because this code never writes |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1362 | // VA_BITS2_PARTDEFINED values. |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1363 | SizeT i; |
| 1364 | for (i = 0; i < lenT; i++) { |
| 1365 | set_vabits2(a + i, vabits2); |
| 1366 | } |
| 1367 | return; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1368 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1369 | #endif |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1370 | |
| 1371 | /*------------------ standard handling ------------------ */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1372 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1373 | /* Get the distinguished secondary that we might want |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1374 | to use (part of the space-compression scheme). */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1375 | example_dsm = &sm_distinguished[dsm_num]; |
| 1376 | |
| 1377 | // We have to handle ranges covering various combinations of partial and |
| 1378 | // whole sec-maps. Here is how parts 1, 2 and 3 are used in each case. |
| 1379 | // Cases marked with a '*' are common. |
| 1380 | // |
| 1381 | // TYPE PARTS USED |
| 1382 | // ---- ---------- |
| 1383 | // * one partial sec-map (p) 1 |
| 1384 | // - one whole sec-map (P) 2 |
| 1385 | // |
| 1386 | // * two partial sec-maps (pp) 1,3 |
| 1387 | // - one partial, one whole sec-map (pP) 1,2 |
| 1388 | // - one whole, one partial sec-map (Pp) 2,3 |
| 1389 | // - two whole sec-maps (PP) 2,2 |
| 1390 | // |
| 1391 | // * one partial, one whole, one partial (pPp) 1,2,3 |
| 1392 | // - one partial, two whole (pPP) 1,2,2 |
| 1393 | // - two whole, one partial (PPp) 2,2,3 |
| 1394 | // - three whole (PPP) 2,2,2 |
| 1395 | // |
| 1396 | // * one partial, N-2 whole, one partial (pP...Pp) 1,2...2,3 |
| 1397 | // - one partial, N-1 whole (pP...PP) 1,2...2,2 |
| 1398 | // - N-1 whole, one partial (PP...Pp) 2,2...2,3 |
| 1399 | // - N whole (PP...PP) 2,2...2,3 |
| 1400 | |
| 1401 | // Break up total length (lenT) into two parts: length in the first |
| 1402 | // sec-map (lenA), and the rest (lenB); lenT == lenA + lenB. |
| 1403 | aNext = start_of_this_sm(a) + SM_SIZE; |
| 1404 | len_to_next_secmap = aNext - a; |
| 1405 | if ( lenT <= len_to_next_secmap ) { |
| 1406 | // Range entirely within one sec-map. Covers almost all cases. |
| 1407 | PROF_EVENT(151, "set_address_range_perms-single-secmap"); |
| 1408 | lenA = lenT; |
| 1409 | lenB = 0; |
| 1410 | } else if (is_start_of_sm(a)) { |
| 1411 | // Range spans at least one whole sec-map, and starts at the beginning |
| 1412 | // of a sec-map; skip to Part 2. |
| 1413 | PROF_EVENT(152, "set_address_range_perms-startof-secmap"); |
| 1414 | lenA = 0; |
| 1415 | lenB = lenT; |
| 1416 | goto part2; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1417 | } else { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1418 | // Range spans two or more sec-maps, first one is partial. |
| 1419 | PROF_EVENT(153, "set_address_range_perms-multiple-secmaps"); |
| 1420 | lenA = len_to_next_secmap; |
| 1421 | lenB = lenT - lenA; |
| 1422 | } |
| 1423 | |
| 1424 | //------------------------------------------------------------------------ |
| 1425 | // Part 1: Deal with the first sec_map. Most of the time the range will be |
| 1426 | // entirely within a sec_map and this part alone will suffice. Also, |
| 1427 | // doing it this way lets us avoid repeatedly testing for the crossing of |
| 1428 | // a sec-map boundary within these loops. |
| 1429 | //------------------------------------------------------------------------ |
| 1430 | |
| 1431 | // If it's distinguished, make it undistinguished if necessary. |
| 1432 | sm_ptr = get_secmap_ptr(a); |
| 1433 | if (is_distinguished_sm(*sm_ptr)) { |
| 1434 | if (*sm_ptr == example_dsm) { |
| 1435 | // Sec-map already has the V+A bits that we want, so skip. |
| 1436 | PROF_EVENT(154, "set_address_range_perms-dist-sm1-quick"); |
| 1437 | a = aNext; |
| 1438 | lenA = 0; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1439 | } else { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1440 | PROF_EVENT(155, "set_address_range_perms-dist-sm1"); |
| 1441 | *sm_ptr = copy_for_writing(*sm_ptr); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1442 | } |
| 1443 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1444 | sm = *sm_ptr; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1445 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1446 | // 1 byte steps |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1447 | while (True) { |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1448 | if (VG_IS_8_ALIGNED(a)) break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1449 | if (lenA < 1) break; |
| 1450 | PROF_EVENT(156, "set_address_range_perms-loop1a"); |
| 1451 | sm_off = SM_OFF(a); |
| 1452 | insert_vabits2_into_vabits8( a, vabits2, &(sm->vabits8[sm_off]) ); |
| 1453 | a += 1; |
| 1454 | lenA -= 1; |
| 1455 | } |
| 1456 | // 8-aligned, 8 byte steps |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1457 | while (True) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1458 | if (lenA < 8) break; |
| 1459 | PROF_EVENT(157, "set_address_range_perms-loop8a"); |
| 1460 | sm_off16 = SM_OFF_16(a); |
| 1461 | ((UShort*)(sm->vabits8))[sm_off16] = vabits16; |
| 1462 | a += 8; |
| 1463 | lenA -= 8; |
| 1464 | } |
| 1465 | // 1 byte steps |
| 1466 | while (True) { |
| 1467 | if (lenA < 1) break; |
| 1468 | PROF_EVENT(158, "set_address_range_perms-loop1b"); |
| 1469 | sm_off = SM_OFF(a); |
| 1470 | insert_vabits2_into_vabits8( a, vabits2, &(sm->vabits8[sm_off]) ); |
| 1471 | a += 1; |
| 1472 | lenA -= 1; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1475 | // We've finished the first sec-map. Is that it? |
| 1476 | if (lenB == 0) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1477 | return; |
| 1478 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1479 | //------------------------------------------------------------------------ |
| 1480 | // Part 2: Fast-set entire sec-maps at a time. |
| 1481 | //------------------------------------------------------------------------ |
| 1482 | part2: |
| 1483 | // 64KB-aligned, 64KB steps. |
| 1484 | // Nb: we can reach here with lenB < SM_SIZE |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1485 | while (True) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1486 | if (lenB < SM_SIZE) break; |
| 1487 | tl_assert(is_start_of_sm(a)); |
| 1488 | PROF_EVENT(159, "set_address_range_perms-loop64K"); |
| 1489 | sm_ptr = get_secmap_ptr(a); |
| 1490 | if (!is_distinguished_sm(*sm_ptr)) { |
| 1491 | PROF_EVENT(160, "set_address_range_perms-loop64K-free-dist-sm"); |
| 1492 | // Free the non-distinguished sec-map that we're replacing. This |
| 1493 | // case happens moderately often, enough to be worthwhile. |
| 1494 | VG_(am_munmap_valgrind)((Addr)*sm_ptr, sizeof(SecMap)); |
| 1495 | } |
| 1496 | update_SM_counts(*sm_ptr, example_dsm); |
| 1497 | // Make the sec-map entry point to the example DSM |
| 1498 | *sm_ptr = example_dsm; |
| 1499 | lenB -= SM_SIZE; |
| 1500 | a += SM_SIZE; |
| 1501 | } |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1502 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1503 | // We've finished the whole sec-maps. Is that it? |
| 1504 | if (lenB == 0) |
| 1505 | return; |
| 1506 | |
| 1507 | //------------------------------------------------------------------------ |
| 1508 | // Part 3: Finish off the final partial sec-map, if necessary. |
| 1509 | //------------------------------------------------------------------------ |
| 1510 | |
| 1511 | tl_assert(is_start_of_sm(a) && lenB < SM_SIZE); |
| 1512 | |
| 1513 | // If it's distinguished, make it undistinguished if necessary. |
| 1514 | sm_ptr = get_secmap_ptr(a); |
| 1515 | if (is_distinguished_sm(*sm_ptr)) { |
| 1516 | if (*sm_ptr == example_dsm) { |
| 1517 | // Sec-map already has the V+A bits that we want, so stop. |
| 1518 | PROF_EVENT(161, "set_address_range_perms-dist-sm2-quick"); |
| 1519 | return; |
| 1520 | } else { |
| 1521 | PROF_EVENT(162, "set_address_range_perms-dist-sm2"); |
| 1522 | *sm_ptr = copy_for_writing(*sm_ptr); |
| 1523 | } |
| 1524 | } |
| 1525 | sm = *sm_ptr; |
| 1526 | |
| 1527 | // 8-aligned, 8 byte steps |
| 1528 | while (True) { |
| 1529 | if (lenB < 8) break; |
| 1530 | PROF_EVENT(163, "set_address_range_perms-loop8b"); |
| 1531 | sm_off16 = SM_OFF_16(a); |
| 1532 | ((UShort*)(sm->vabits8))[sm_off16] = vabits16; |
| 1533 | a += 8; |
| 1534 | lenB -= 8; |
| 1535 | } |
| 1536 | // 1 byte steps |
| 1537 | while (True) { |
| 1538 | if (lenB < 1) return; |
| 1539 | PROF_EVENT(164, "set_address_range_perms-loop1c"); |
| 1540 | sm_off = SM_OFF(a); |
| 1541 | insert_vabits2_into_vabits8( a, vabits2, &(sm->vabits8[sm_off]) ); |
| 1542 | a += 1; |
| 1543 | lenB -= 1; |
| 1544 | } |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1545 | } |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1546 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1547 | |
| 1548 | /* --- Set permissions for arbitrary address ranges --- */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1549 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1550 | void MC_(make_mem_noaccess) ( Addr a, SizeT len ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1551 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1552 | PROF_EVENT(40, "MC_(make_mem_noaccess)"); |
| 1553 | DEBUG("MC_(make_mem_noaccess)(%p, %lu)\n", a, len); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1554 | set_address_range_perms ( a, len, VA_BITS16_NOACCESS, SM_DIST_NOACCESS ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1555 | if (UNLIKELY( MC_(clo_mc_level) == 3 )) |
| 1556 | ocache_sarp_Clear_Origins ( a, len ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1559 | static void make_mem_undefined ( Addr a, SizeT len ) |
| 1560 | { |
| 1561 | PROF_EVENT(41, "make_mem_undefined"); |
| 1562 | DEBUG("make_mem_undefined(%p, %lu)\n", a, len); |
| 1563 | set_address_range_perms ( a, len, VA_BITS16_UNDEFINED, SM_DIST_UNDEFINED ); |
| 1564 | } |
| 1565 | |
| 1566 | void MC_(make_mem_undefined_w_otag) ( Addr a, SizeT len, UInt otag ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1567 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1568 | PROF_EVENT(41, "MC_(make_mem_undefined)"); |
| 1569 | DEBUG("MC_(make_mem_undefined)(%p, %lu)\n", a, len); |
| 1570 | set_address_range_perms ( a, len, VA_BITS16_UNDEFINED, SM_DIST_UNDEFINED ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1571 | if (UNLIKELY( MC_(clo_mc_level) == 3 )) |
| 1572 | ocache_sarp_Set_Origins ( a, len, otag ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1575 | static |
| 1576 | void make_mem_undefined_w_tid_and_okind ( Addr a, SizeT len, |
| 1577 | ThreadId tid, UInt okind ) |
| 1578 | { |
| 1579 | UInt ecu; |
| 1580 | ExeContext* here; |
| 1581 | /* VG_(record_ExeContext) checks for validity of tid, and asserts |
| 1582 | if it is invalid. So no need to do it here. */ |
| 1583 | tl_assert(okind <= 3); |
| 1584 | here = VG_(record_ExeContext)( tid, 0/*first_ip_delta*/ ); |
| 1585 | tl_assert(here); |
| 1586 | ecu = VG_(get_ECU_from_ExeContext)(here); |
| 1587 | tl_assert(VG_(is_plausible_ECU)(ecu)); |
| 1588 | MC_(make_mem_undefined_w_otag) ( a, len, ecu | okind ); |
| 1589 | } |
| 1590 | |
| 1591 | static |
| 1592 | void make_mem_undefined_w_tid ( Addr a, SizeT len, ThreadId tid ) { |
| 1593 | make_mem_undefined_w_tid_and_okind ( a, len, tid, MC_OKIND_UNKNOWN ); |
| 1594 | } |
| 1595 | |
| 1596 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1597 | void MC_(make_mem_defined) ( Addr a, SizeT len ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1598 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1599 | PROF_EVENT(42, "MC_(make_mem_defined)"); |
| 1600 | DEBUG("MC_(make_mem_defined)(%p, %lu)\n", a, len); |
| 1601 | set_address_range_perms ( a, len, VA_BITS16_DEFINED, SM_DIST_DEFINED ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1602 | if (UNLIKELY( MC_(clo_mc_level) == 3 )) |
| 1603 | ocache_sarp_Clear_Origins ( a, len ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1606 | /* For each byte in [a,a+len), if the byte is addressable, make it be |
| 1607 | defined, but if it isn't addressible, leave it alone. In other |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1608 | words a version of MC_(make_mem_defined) that doesn't mess with |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1609 | addressibility. Low-performance implementation. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1610 | static void make_mem_defined_if_addressable ( Addr a, SizeT len ) |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1611 | { |
| 1612 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1613 | UChar vabits2; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1614 | DEBUG("make_mem_defined_if_addressable(%p, %llu)\n", a, (ULong)len); |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1615 | for (i = 0; i < len; i++) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1616 | vabits2 = get_vabits2( a+i ); |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1617 | if (LIKELY(VA_BITS2_NOACCESS != vabits2)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1618 | set_vabits2(a+i, VA_BITS2_DEFINED); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1619 | if (UNLIKELY(MC_(clo_mc_level) >= 3)) { |
| 1620 | MC_(helperc_b_store1)( a+i, 0 ); /* clear the origin tag */ |
| 1621 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1622 | } |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1626 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1627 | /* --- Block-copy permissions (needed for implementing realloc() and |
| 1628 | sys_mremap). --- */ |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1629 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1630 | void MC_(copy_address_range_state) ( Addr src, Addr dst, SizeT len ) |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1631 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1632 | SizeT i, j; |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1633 | UChar vabits2, vabits8; |
| 1634 | Bool aligned, nooverlap; |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1635 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1636 | DEBUG("MC_(copy_address_range_state)\n"); |
| 1637 | PROF_EVENT(50, "MC_(copy_address_range_state)"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1638 | |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1639 | if (len == 0 || src == dst) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1640 | return; |
| 1641 | |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1642 | aligned = VG_IS_4_ALIGNED(src) && VG_IS_4_ALIGNED(dst); |
| 1643 | nooverlap = src+len <= dst || dst+len <= src; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1644 | |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1645 | if (nooverlap && aligned) { |
| 1646 | |
| 1647 | /* Vectorised fast case, when no overlap and suitably aligned */ |
| 1648 | /* vector loop */ |
| 1649 | i = 0; |
| 1650 | while (len >= 4) { |
| 1651 | vabits8 = get_vabits8_for_aligned_word32( src+i ); |
| 1652 | set_vabits8_for_aligned_word32( dst+i, vabits8 ); |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 1653 | if (LIKELY(VA_BITS8_DEFINED == vabits8 |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1654 | || VA_BITS8_UNDEFINED == vabits8 |
| 1655 | || VA_BITS8_NOACCESS == vabits8)) { |
| 1656 | /* do nothing */ |
| 1657 | } else { |
| 1658 | /* have to copy secondary map info */ |
| 1659 | if (VA_BITS2_PARTDEFINED == get_vabits2( src+i+0 )) |
| 1660 | set_sec_vbits8( dst+i+0, get_sec_vbits8( src+i+0 ) ); |
| 1661 | if (VA_BITS2_PARTDEFINED == get_vabits2( src+i+1 )) |
| 1662 | set_sec_vbits8( dst+i+1, get_sec_vbits8( src+i+1 ) ); |
| 1663 | if (VA_BITS2_PARTDEFINED == get_vabits2( src+i+2 )) |
| 1664 | set_sec_vbits8( dst+i+2, get_sec_vbits8( src+i+2 ) ); |
| 1665 | if (VA_BITS2_PARTDEFINED == get_vabits2( src+i+3 )) |
| 1666 | set_sec_vbits8( dst+i+3, get_sec_vbits8( src+i+3 ) ); |
| 1667 | } |
| 1668 | i += 4; |
| 1669 | len -= 4; |
| 1670 | } |
| 1671 | /* fixup loop */ |
| 1672 | while (len >= 1) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1673 | vabits2 = get_vabits2( src+i ); |
| 1674 | set_vabits2( dst+i, vabits2 ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1675 | if (VA_BITS2_PARTDEFINED == vabits2) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1676 | set_sec_vbits8( dst+i, get_sec_vbits8( src+i ) ); |
| 1677 | } |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1678 | i++; |
| 1679 | len--; |
| 1680 | } |
| 1681 | |
| 1682 | } else { |
| 1683 | |
| 1684 | /* We have to do things the slow way */ |
| 1685 | if (src < dst) { |
| 1686 | for (i = 0, j = len-1; i < len; i++, j--) { |
| 1687 | PROF_EVENT(51, "MC_(copy_address_range_state)(loop)"); |
| 1688 | vabits2 = get_vabits2( src+j ); |
| 1689 | set_vabits2( dst+j, vabits2 ); |
| 1690 | if (VA_BITS2_PARTDEFINED == vabits2) { |
| 1691 | set_sec_vbits8( dst+j, get_sec_vbits8( src+j ) ); |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | if (src > dst) { |
| 1697 | for (i = 0; i < len; i++) { |
| 1698 | PROF_EVENT(52, "MC_(copy_address_range_state)(loop)"); |
| 1699 | vabits2 = get_vabits2( src+i ); |
| 1700 | set_vabits2( dst+i, vabits2 ); |
| 1701 | if (VA_BITS2_PARTDEFINED == vabits2) { |
| 1702 | set_sec_vbits8( dst+i, get_sec_vbits8( src+i ) ); |
| 1703 | } |
| 1704 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1705 | } |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1706 | } |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1707 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1711 | /*------------------------------------------------------------*/ |
| 1712 | /*--- Origin tracking stuff - cache basics ---*/ |
| 1713 | /*------------------------------------------------------------*/ |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1714 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1715 | /* Some background comments on the origin tracking implementation |
| 1716 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1717 | |
| 1718 | Note that this implementation draws inspiration from the "origin |
| 1719 | tracking by value piggybacking" scheme described in "Tracking Bad |
| 1720 | Apples: Reporting the Origin of Null and Undefined Value Errors" |
| 1721 | (Michael Bond, Nicholas Nethercote, Stephen Kent, Samuel Guyer, |
| 1722 | Kathryn McKinley, OOPSLA07, Montreal, Oct 2007) but in fact it is |
| 1723 | implemented completely differently. |
| 1724 | |
| 1725 | This implementation tracks the defining point of all values using |
| 1726 | so called "origin tags", which are 32-bit integers, rather than |
| 1727 | using the values themselves to encode the origins. The latter, |
| 1728 | so-called value piggybacking", is what the OOPSLA07 paper |
| 1729 | describes. |
| 1730 | |
| 1731 | Origin tags, as tracked by the machinery below, are 32-bit unsigned |
| 1732 | ints (UInts), regardless of the machine's word size. |
| 1733 | |
| 1734 | > Question: why is otag a UInt? Wouldn't a UWord be better? Isn't |
| 1735 | > it really just the address of the relevant ExeContext? |
| 1736 | |
| 1737 | Well, it's not the address, but a value which has a 1-1 mapping |
| 1738 | with ExeContexts, and is guaranteed not to be zero, since zero |
| 1739 | denotes (to memcheck) "unknown origin or defined value". So these |
| 1740 | UInts are just numbers starting at 1; each ExeContext is given a |
| 1741 | number when it is created. |
| 1742 | |
| 1743 | Making these otags 32-bit regardless of the machine's word size |
| 1744 | makes the 64-bit implementation easier (next para). And it doesn't |
| 1745 | really limit us in any way, since for the tags to overflow would |
| 1746 | require that the program somehow caused 2^32-1 different |
| 1747 | ExeContexts to be created, in which case it is probably in deep |
| 1748 | trouble. Not to mention V will have soaked up many tens of |
| 1749 | gigabytes of memory merely to store them all. |
| 1750 | |
| 1751 | So having 64-bit origins doesn't really buy you anything, and has |
| 1752 | the following downsides: |
| 1753 | |
| 1754 | Suppose that instead, an otag is a UWord. This would mean that, on |
| 1755 | a 64-bit target, |
| 1756 | |
| 1757 | 1. It becomes hard to shadow any element of guest state which is |
| 1758 | smaller than 8 bytes. To do so means you'd need to find some |
| 1759 | 8-byte-sized hole in the guest state which you don't want to |
| 1760 | shadow, and use that instead to hold the otag. On ppc64, the |
| 1761 | condition code register(s) are split into 20 UChar sized pieces, |
| 1762 | all of which need to be tracked (guest_XER_SO .. guest_CR7_0) |
| 1763 | and so that would entail finding 160 bytes somewhere else in the |
| 1764 | guest state. |
| 1765 | |
| 1766 | Even on x86, I want to track origins for %AH .. %DH (bits 15:8 |
| 1767 | of %EAX .. %EDX) that are separate from %AL .. %DL (bits 7:0 of |
| 1768 | same) and so I had to look for 4 untracked otag-sized areas in |
| 1769 | the guest state to make that possible. |
| 1770 | |
| 1771 | The same problem exists of course when origin tags are only 32 |
| 1772 | bits, but it's less extreme. |
| 1773 | |
| 1774 | 2. (More compelling) it doubles the size of the origin shadow |
| 1775 | memory. Given that the shadow memory is organised as a fixed |
| 1776 | size cache, and that accuracy of tracking is limited by origins |
| 1777 | falling out the cache due to space conflicts, this isn't good. |
| 1778 | |
| 1779 | > Another question: is the origin tracking perfect, or are there |
| 1780 | > cases where it fails to determine an origin? |
| 1781 | |
| 1782 | It is imperfect for at least for the following reasons, and |
| 1783 | probably more: |
| 1784 | |
| 1785 | * Insufficient capacity in the origin cache. When a line is |
| 1786 | evicted from the cache it is gone forever, and so subsequent |
| 1787 | queries for the line produce zero, indicating no origin |
| 1788 | information. Interestingly, a line containing all zeroes can be |
| 1789 | evicted "free" from the cache, since it contains no useful |
| 1790 | information, so there is scope perhaps for some cleverer cache |
| 1791 | management schemes. |
| 1792 | |
| 1793 | * The origin cache only stores one otag per 32-bits of address |
| 1794 | space, plus 4 bits indicating which of the 4 bytes has that tag |
| 1795 | and which are considered defined. The result is that if two |
| 1796 | undefined bytes in the same word are stored in memory, the first |
| 1797 | stored byte's origin will be lost and replaced by the origin for |
| 1798 | the second byte. |
| 1799 | |
| 1800 | * Nonzero origin tags for defined values. Consider a binary |
| 1801 | operator application op(x,y). Suppose y is undefined (and so has |
| 1802 | a valid nonzero origin tag), and x is defined, but erroneously |
| 1803 | has a nonzero origin tag (defined values should have tag zero). |
| 1804 | If the erroneous tag has a numeric value greater than y's tag, |
| 1805 | then the rule for propagating origin tags though binary |
| 1806 | operations, which is simply to take the unsigned max of the two |
| 1807 | tags, will erroneously propagate x's tag rather than y's. |
| 1808 | |
| 1809 | * Some obscure uses of x86/amd64 byte registers can cause lossage |
| 1810 | or confusion of origins. %AH .. %DH are treated as different |
| 1811 | from, and unrelated to, their parent registers, %EAX .. %EDX. |
| 1812 | So some wierd sequences like |
| 1813 | |
| 1814 | movb undefined-value, %AH |
| 1815 | movb defined-value, %AL |
| 1816 | .. use %AX or %EAX .. |
| 1817 | |
| 1818 | will cause the origin attributed to %AH to be ignored, since %AL, |
| 1819 | %AX, %EAX are treated as the same register, and %AH as a |
| 1820 | completely separate one. |
| 1821 | |
| 1822 | But having said all that, it actually seems to work fairly well in |
| 1823 | practice. |
| 1824 | */ |
| 1825 | |
| 1826 | static UWord stats_ocacheL1_find = 0; |
| 1827 | static UWord stats_ocacheL1_found_at_1 = 0; |
| 1828 | static UWord stats_ocacheL1_found_at_N = 0; |
| 1829 | static UWord stats_ocacheL1_misses = 0; |
| 1830 | static UWord stats_ocacheL1_lossage = 0; |
| 1831 | static UWord stats_ocacheL1_movefwds = 0; |
| 1832 | |
| 1833 | static UWord stats__ocacheL2_refs = 0; |
| 1834 | static UWord stats__ocacheL2_misses = 0; |
| 1835 | static UWord stats__ocacheL2_n_nodes_max = 0; |
| 1836 | |
| 1837 | /* Cache of 32-bit values, one every 32 bits of address space */ |
| 1838 | |
| 1839 | #define OC_BITS_PER_LINE 5 |
| 1840 | #define OC_W32S_PER_LINE (1 << (OC_BITS_PER_LINE - 2)) |
| 1841 | |
| 1842 | static INLINE UWord oc_line_offset ( Addr a ) { |
| 1843 | return (a >> 2) & (OC_W32S_PER_LINE - 1); |
| 1844 | } |
| 1845 | static INLINE Bool is_valid_oc_tag ( Addr tag ) { |
| 1846 | return 0 == (tag & ((1 << OC_BITS_PER_LINE) - 1)); |
| 1847 | } |
| 1848 | |
| 1849 | #define OC_LINES_PER_SET 2 |
| 1850 | |
| 1851 | #define OC_N_SET_BITS 20 |
| 1852 | #define OC_N_SETS (1 << OC_N_SET_BITS) |
| 1853 | |
| 1854 | /* These settings give: |
| 1855 | 64 bit host: ocache: 100,663,296 sizeB 67,108,864 useful |
| 1856 | 32 bit host: ocache: 92,274,688 sizeB 67,108,864 useful |
| 1857 | */ |
| 1858 | |
| 1859 | #define OC_MOVE_FORWARDS_EVERY_BITS 7 |
| 1860 | |
| 1861 | |
| 1862 | typedef |
| 1863 | struct { |
| 1864 | Addr tag; |
| 1865 | UInt w32[OC_W32S_PER_LINE]; |
| 1866 | UChar descr[OC_W32S_PER_LINE]; |
| 1867 | } |
| 1868 | OCacheLine; |
| 1869 | |
| 1870 | /* Classify and also sanity-check 'line'. Return 'e' (empty) if not |
| 1871 | in use, 'n' (nonzero) if it contains at least one valid origin tag, |
| 1872 | and 'z' if all the represented tags are zero. */ |
| 1873 | static UChar classify_OCacheLine ( OCacheLine* line ) |
| 1874 | { |
| 1875 | UWord i; |
| 1876 | if (line->tag == 1/*invalid*/) |
| 1877 | return 'e'; /* EMPTY */ |
| 1878 | tl_assert(is_valid_oc_tag(line->tag)); |
| 1879 | for (i = 0; i < OC_W32S_PER_LINE; i++) { |
| 1880 | tl_assert(0 == ((~0xF) & line->descr[i])); |
| 1881 | if (line->w32[i] > 0 && line->descr[i] > 0) |
| 1882 | return 'n'; /* NONZERO - contains useful info */ |
| 1883 | } |
| 1884 | return 'z'; /* ZERO - no useful info */ |
| 1885 | } |
| 1886 | |
| 1887 | typedef |
| 1888 | struct { |
| 1889 | OCacheLine line[OC_LINES_PER_SET]; |
| 1890 | } |
| 1891 | OCacheSet; |
| 1892 | |
| 1893 | typedef |
| 1894 | struct { |
| 1895 | OCacheSet set[OC_N_SETS]; |
| 1896 | } |
| 1897 | OCache; |
| 1898 | |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 1899 | static OCache* ocache = NULL; |
| 1900 | static UWord ocache_event_ctr = 0; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1901 | |
| 1902 | static void init_ocacheL2 ( void ); /* fwds */ |
| 1903 | static void init_OCache ( void ) |
| 1904 | { |
| 1905 | UWord line, set; |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 1906 | tl_assert(MC_(clo_mc_level) >= 3); |
| 1907 | tl_assert(ocache == NULL); |
| 1908 | ocache = VG_(am_shadow_alloc)(sizeof(OCache)); |
| 1909 | if (ocache == NULL) { |
| 1910 | VG_(out_of_memory_NORETURN)( "memcheck:allocate the OCache", |
| 1911 | sizeof(OCache) ); |
| 1912 | } |
| 1913 | tl_assert(ocache != NULL); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1914 | for (set = 0; set < OC_N_SETS; set++) { |
| 1915 | for (line = 0; line < OC_LINES_PER_SET; line++) { |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 1916 | ocache->set[set].line[line].tag = 1/*invalid*/; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | init_ocacheL2(); |
| 1920 | } |
| 1921 | |
| 1922 | static void moveLineForwards ( OCacheSet* set, UWord lineno ) |
| 1923 | { |
| 1924 | OCacheLine tmp; |
| 1925 | stats_ocacheL1_movefwds++; |
| 1926 | tl_assert(lineno > 0 && lineno < OC_LINES_PER_SET); |
| 1927 | tmp = set->line[lineno-1]; |
| 1928 | set->line[lineno-1] = set->line[lineno]; |
| 1929 | set->line[lineno] = tmp; |
| 1930 | } |
| 1931 | |
| 1932 | static void zeroise_OCacheLine ( OCacheLine* line, Addr tag ) { |
| 1933 | UWord i; |
| 1934 | for (i = 0; i < OC_W32S_PER_LINE; i++) { |
| 1935 | line->w32[i] = 0; /* NO ORIGIN */ |
| 1936 | line->descr[i] = 0; /* REALLY REALLY NO ORIGIN! */ |
| 1937 | } |
| 1938 | line->tag = tag; |
| 1939 | } |
| 1940 | |
| 1941 | ////////////////////////////////////////////////////////////// |
| 1942 | //// OCache backing store |
| 1943 | |
| 1944 | static OSet* ocacheL2 = NULL; |
| 1945 | |
| 1946 | static void* ocacheL2_malloc ( SizeT szB ) { |
| 1947 | return VG_(malloc)(szB); |
| 1948 | } |
| 1949 | static void ocacheL2_free ( void* v ) { |
| 1950 | VG_(free)( v ); |
| 1951 | } |
| 1952 | |
| 1953 | /* Stats: # nodes currently in tree */ |
| 1954 | static UWord stats__ocacheL2_n_nodes = 0; |
| 1955 | |
| 1956 | static void init_ocacheL2 ( void ) |
| 1957 | { |
| 1958 | tl_assert(!ocacheL2); |
| 1959 | tl_assert(sizeof(Word) == sizeof(Addr)); /* since OCacheLine.tag :: Addr */ |
| 1960 | tl_assert(0 == offsetof(OCacheLine,tag)); |
| 1961 | ocacheL2 |
| 1962 | = VG_(OSetGen_Create)( offsetof(OCacheLine,tag), |
| 1963 | NULL, /* fast cmp */ |
| 1964 | ocacheL2_malloc, ocacheL2_free ); |
| 1965 | tl_assert(ocacheL2); |
| 1966 | stats__ocacheL2_n_nodes = 0; |
| 1967 | } |
| 1968 | |
| 1969 | /* Find line with the given tag in the tree, or NULL if not found. */ |
| 1970 | static OCacheLine* ocacheL2_find_tag ( Addr tag ) |
| 1971 | { |
| 1972 | OCacheLine* line; |
| 1973 | tl_assert(is_valid_oc_tag(tag)); |
| 1974 | stats__ocacheL2_refs++; |
| 1975 | line = VG_(OSetGen_Lookup)( ocacheL2, &tag ); |
| 1976 | return line; |
| 1977 | } |
| 1978 | |
| 1979 | /* Delete the line with the given tag from the tree, if it is present, and |
| 1980 | free up the associated memory. */ |
| 1981 | static void ocacheL2_del_tag ( Addr tag ) |
| 1982 | { |
| 1983 | OCacheLine* line; |
| 1984 | tl_assert(is_valid_oc_tag(tag)); |
| 1985 | stats__ocacheL2_refs++; |
| 1986 | line = VG_(OSetGen_Remove)( ocacheL2, &tag ); |
| 1987 | if (line) { |
| 1988 | VG_(OSetGen_FreeNode)(ocacheL2, line); |
| 1989 | tl_assert(stats__ocacheL2_n_nodes > 0); |
| 1990 | stats__ocacheL2_n_nodes--; |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | /* Add a copy of the given line to the tree. It must not already be |
| 1995 | present. */ |
| 1996 | static void ocacheL2_add_line ( OCacheLine* line ) |
| 1997 | { |
| 1998 | OCacheLine* copy; |
| 1999 | tl_assert(is_valid_oc_tag(line->tag)); |
| 2000 | copy = VG_(OSetGen_AllocNode)( ocacheL2, sizeof(OCacheLine) ); |
| 2001 | tl_assert(copy); |
| 2002 | *copy = *line; |
| 2003 | stats__ocacheL2_refs++; |
| 2004 | VG_(OSetGen_Insert)( ocacheL2, copy ); |
| 2005 | stats__ocacheL2_n_nodes++; |
| 2006 | if (stats__ocacheL2_n_nodes > stats__ocacheL2_n_nodes_max) |
| 2007 | stats__ocacheL2_n_nodes_max = stats__ocacheL2_n_nodes; |
| 2008 | } |
| 2009 | |
| 2010 | //// |
| 2011 | ////////////////////////////////////////////////////////////// |
| 2012 | |
| 2013 | __attribute__((noinline)) |
| 2014 | static OCacheLine* find_OCacheLine_SLOW ( Addr a ) |
| 2015 | { |
| 2016 | OCacheLine *victim, *inL2; |
| 2017 | UChar c; |
| 2018 | UWord line; |
| 2019 | UWord setno = (a >> OC_BITS_PER_LINE) & (OC_N_SETS - 1); |
| 2020 | UWord tagmask = ~((1 << OC_BITS_PER_LINE) - 1); |
| 2021 | UWord tag = a & tagmask; |
| 2022 | tl_assert(setno >= 0 && setno < OC_N_SETS); |
| 2023 | |
| 2024 | /* we already tried line == 0; skip therefore. */ |
| 2025 | for (line = 1; line < OC_LINES_PER_SET; line++) { |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 2026 | if (ocache->set[setno].line[line].tag == tag) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2027 | if (line == 1) { |
| 2028 | stats_ocacheL1_found_at_1++; |
| 2029 | } else { |
| 2030 | stats_ocacheL1_found_at_N++; |
| 2031 | } |
| 2032 | if (UNLIKELY(0 == (ocache_event_ctr++ |
| 2033 | & ((1<<OC_MOVE_FORWARDS_EVERY_BITS)-1)))) { |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 2034 | moveLineForwards( &ocache->set[setno], line ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2035 | line--; |
| 2036 | } |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 2037 | return &ocache->set[setno].line[line]; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | /* A miss. Use the last slot. Implicitly this means we're |
| 2042 | ejecting the line in the last slot. */ |
| 2043 | stats_ocacheL1_misses++; |
| 2044 | tl_assert(line == OC_LINES_PER_SET); |
| 2045 | line--; |
| 2046 | tl_assert(line > 0); |
| 2047 | |
| 2048 | /* First, move the to-be-ejected line to the L2 cache. */ |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 2049 | victim = &ocache->set[setno].line[line]; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2050 | c = classify_OCacheLine(victim); |
| 2051 | switch (c) { |
| 2052 | case 'e': |
| 2053 | /* the line is empty (has invalid tag); ignore it. */ |
| 2054 | break; |
| 2055 | case 'z': |
| 2056 | /* line contains zeroes. We must ensure the backing store is |
| 2057 | updated accordingly, either by copying the line there |
| 2058 | verbatim, or by ensuring it isn't present there. We |
| 2059 | chosse the latter on the basis that it reduces the size of |
| 2060 | the backing store. */ |
| 2061 | ocacheL2_del_tag( victim->tag ); |
| 2062 | break; |
| 2063 | case 'n': |
| 2064 | /* line contains at least one real, useful origin. Copy it |
| 2065 | to the backing store. */ |
| 2066 | stats_ocacheL1_lossage++; |
| 2067 | inL2 = ocacheL2_find_tag( victim->tag ); |
| 2068 | if (inL2) { |
| 2069 | *inL2 = *victim; |
| 2070 | } else { |
| 2071 | ocacheL2_add_line( victim ); |
| 2072 | } |
| 2073 | break; |
| 2074 | default: |
| 2075 | tl_assert(0); |
| 2076 | } |
| 2077 | |
| 2078 | /* Now we must reload the L1 cache from the backing tree, if |
| 2079 | possible. */ |
| 2080 | tl_assert(tag != victim->tag); /* stay sane */ |
| 2081 | inL2 = ocacheL2_find_tag( tag ); |
| 2082 | if (inL2) { |
| 2083 | /* We're in luck. It's in the L2. */ |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 2084 | ocache->set[setno].line[line] = *inL2; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2085 | } else { |
| 2086 | /* Missed at both levels of the cache hierarchy. We have to |
| 2087 | declare it as full of zeroes (unknown origins). */ |
| 2088 | stats__ocacheL2_misses++; |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 2089 | zeroise_OCacheLine( &ocache->set[setno].line[line], tag ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | /* Move it one forwards */ |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 2093 | moveLineForwards( &ocache->set[setno], line ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2094 | line--; |
| 2095 | |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 2096 | return &ocache->set[setno].line[line]; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | static INLINE OCacheLine* find_OCacheLine ( Addr a ) |
| 2100 | { |
| 2101 | UWord setno = (a >> OC_BITS_PER_LINE) & (OC_N_SETS - 1); |
| 2102 | UWord tagmask = ~((1 << OC_BITS_PER_LINE) - 1); |
| 2103 | UWord tag = a & tagmask; |
| 2104 | |
| 2105 | stats_ocacheL1_find++; |
| 2106 | |
| 2107 | if (OC_ENABLE_ASSERTIONS) { |
| 2108 | tl_assert(setno >= 0 && setno < OC_N_SETS); |
| 2109 | tl_assert(0 == (tag & (4 * OC_W32S_PER_LINE - 1))); |
| 2110 | } |
| 2111 | |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 2112 | if (LIKELY(ocache->set[setno].line[0].tag == tag)) { |
| 2113 | return &ocache->set[setno].line[0]; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | return find_OCacheLine_SLOW( a ); |
| 2117 | } |
| 2118 | |
| 2119 | static INLINE void set_aligned_word64_Origin_to_undef ( Addr a, UInt otag ) |
| 2120 | { |
| 2121 | //// BEGIN inlined, specialised version of MC_(helperc_b_store8) |
| 2122 | //// Set the origins for a+0 .. a+7 |
| 2123 | { OCacheLine* line; |
| 2124 | UWord lineoff = oc_line_offset(a); |
| 2125 | if (OC_ENABLE_ASSERTIONS) { |
| 2126 | tl_assert(lineoff >= 0 |
| 2127 | && lineoff < OC_W32S_PER_LINE -1/*'cos 8-aligned*/); |
| 2128 | } |
| 2129 | line = find_OCacheLine( a ); |
| 2130 | line->descr[lineoff+0] = 0xF; |
| 2131 | line->descr[lineoff+1] = 0xF; |
| 2132 | line->w32[lineoff+0] = otag; |
| 2133 | line->w32[lineoff+1] = otag; |
| 2134 | } |
| 2135 | //// END inlined, specialised version of MC_(helperc_b_store8) |
| 2136 | } |
| 2137 | |
| 2138 | |
| 2139 | /*------------------------------------------------------------*/ |
| 2140 | /*--- Aligned fast case permission setters, ---*/ |
| 2141 | /*--- for dealing with stacks ---*/ |
| 2142 | /*------------------------------------------------------------*/ |
| 2143 | |
| 2144 | /*--------------------- 32-bit ---------------------*/ |
| 2145 | |
| 2146 | /* Nb: by "aligned" here we mean 4-byte aligned */ |
| 2147 | |
| 2148 | static INLINE void make_aligned_word32_undefined ( Addr a ) |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 2149 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2150 | UWord sm_off; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 2151 | SecMap* sm; |
| 2152 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2153 | PROF_EVENT(300, "make_aligned_word32_undefined"); |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 2154 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2155 | #ifndef PERF_FAST_STACK2 |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2156 | make_mem_undefined(a, 4); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2157 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 2158 | if (UNLIKELY(a > MAX_PRIMARY_ADDRESS)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2159 | PROF_EVENT(301, "make_aligned_word32_undefined-slow1"); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2160 | make_mem_undefined(a, 4); |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 2161 | return; |
| 2162 | } |
| 2163 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 2164 | sm = get_secmap_for_writing_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2165 | sm_off = SM_OFF(a); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2166 | sm->vabits8[sm_off] = VA_BITS8_UNDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2167 | #endif |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2170 | static INLINE |
| 2171 | void make_aligned_word32_undefined_w_otag ( Addr a, UInt otag ) |
| 2172 | { |
| 2173 | make_aligned_word32_undefined(a); |
| 2174 | //// BEGIN inlined, specialised version of MC_(helperc_b_store4) |
| 2175 | //// Set the origins for a+0 .. a+3 |
| 2176 | { OCacheLine* line; |
| 2177 | UWord lineoff = oc_line_offset(a); |
| 2178 | if (OC_ENABLE_ASSERTIONS) { |
| 2179 | tl_assert(lineoff >= 0 && lineoff < OC_W32S_PER_LINE); |
| 2180 | } |
| 2181 | line = find_OCacheLine( a ); |
| 2182 | line->descr[lineoff] = 0xF; |
| 2183 | line->w32[lineoff] = otag; |
| 2184 | } |
| 2185 | //// END inlined, specialised version of MC_(helperc_b_store4) |
| 2186 | } |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 2187 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2188 | static INLINE |
| 2189 | void make_aligned_word32_noaccess ( Addr a ) |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 2190 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2191 | UWord sm_off; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 2192 | SecMap* sm; |
| 2193 | |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 2194 | PROF_EVENT(310, "make_aligned_word32_noaccess"); |
| 2195 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2196 | #ifndef PERF_FAST_STACK2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2197 | MC_(make_mem_noaccess)(a, 4); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2198 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 2199 | if (UNLIKELY(a > MAX_PRIMARY_ADDRESS)) { |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 2200 | PROF_EVENT(311, "make_aligned_word32_noaccess-slow1"); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2201 | MC_(make_mem_noaccess)(a, 4); |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 2202 | return; |
| 2203 | } |
| 2204 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 2205 | sm = get_secmap_for_writing_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2206 | sm_off = SM_OFF(a); |
| 2207 | sm->vabits8[sm_off] = VA_BITS8_NOACCESS; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2208 | |
| 2209 | //// BEGIN inlined, specialised version of MC_(helperc_b_store4) |
| 2210 | //// Set the origins for a+0 .. a+3. |
| 2211 | if (UNLIKELY( MC_(clo_mc_level) == 3 )) { |
| 2212 | OCacheLine* line; |
| 2213 | UWord lineoff = oc_line_offset(a); |
| 2214 | if (OC_ENABLE_ASSERTIONS) { |
| 2215 | tl_assert(lineoff >= 0 && lineoff < OC_W32S_PER_LINE); |
| 2216 | } |
| 2217 | line = find_OCacheLine( a ); |
| 2218 | line->descr[lineoff] = 0; |
| 2219 | } |
| 2220 | //// END inlined, specialised version of MC_(helperc_b_store4) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2221 | #endif |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2224 | /*--------------------- 64-bit ---------------------*/ |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 2225 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 2226 | /* Nb: by "aligned" here we mean 8-byte aligned */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2227 | |
| 2228 | static INLINE void make_aligned_word64_undefined ( Addr a ) |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 2229 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2230 | UWord sm_off16; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 2231 | SecMap* sm; |
| 2232 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2233 | PROF_EVENT(320, "make_aligned_word64_undefined"); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 2234 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2235 | #ifndef PERF_FAST_STACK2 |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2236 | make_mem_undefined(a, 8); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2237 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 2238 | if (UNLIKELY(a > MAX_PRIMARY_ADDRESS)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2239 | PROF_EVENT(321, "make_aligned_word64_undefined-slow1"); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2240 | make_mem_undefined(a, 8); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 2241 | return; |
| 2242 | } |
| 2243 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 2244 | sm = get_secmap_for_writing_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2245 | sm_off16 = SM_OFF_16(a); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2246 | ((UShort*)(sm->vabits8))[sm_off16] = VA_BITS16_UNDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2247 | #endif |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2250 | static INLINE |
| 2251 | void make_aligned_word64_undefined_w_otag ( Addr a, UInt otag ) |
| 2252 | { |
| 2253 | make_aligned_word64_undefined(a); |
| 2254 | //// BEGIN inlined, specialised version of MC_(helperc_b_store8) |
| 2255 | //// Set the origins for a+0 .. a+7 |
| 2256 | { OCacheLine* line; |
| 2257 | UWord lineoff = oc_line_offset(a); |
| 2258 | tl_assert(lineoff >= 0 |
| 2259 | && lineoff < OC_W32S_PER_LINE -1/*'cos 8-aligned*/); |
| 2260 | line = find_OCacheLine( a ); |
| 2261 | line->descr[lineoff+0] = 0xF; |
| 2262 | line->descr[lineoff+1] = 0xF; |
| 2263 | line->w32[lineoff+0] = otag; |
| 2264 | line->w32[lineoff+1] = otag; |
| 2265 | } |
| 2266 | //// END inlined, specialised version of MC_(helperc_b_store8) |
| 2267 | } |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 2268 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2269 | static INLINE |
| 2270 | void make_aligned_word64_noaccess ( Addr a ) |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 2271 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2272 | UWord sm_off16; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 2273 | SecMap* sm; |
| 2274 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 2275 | PROF_EVENT(330, "make_aligned_word64_noaccess"); |
| 2276 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2277 | #ifndef PERF_FAST_STACK2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2278 | MC_(make_mem_noaccess)(a, 8); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2279 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 2280 | if (UNLIKELY(a > MAX_PRIMARY_ADDRESS)) { |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 2281 | PROF_EVENT(331, "make_aligned_word64_noaccess-slow1"); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2282 | MC_(make_mem_noaccess)(a, 8); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 2283 | return; |
| 2284 | } |
| 2285 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 2286 | sm = get_secmap_for_writing_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2287 | sm_off16 = SM_OFF_16(a); |
| 2288 | ((UShort*)(sm->vabits8))[sm_off16] = VA_BITS16_NOACCESS; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2289 | |
| 2290 | //// BEGIN inlined, specialised version of MC_(helperc_b_store8) |
| 2291 | //// Clear the origins for a+0 .. a+7. |
| 2292 | if (UNLIKELY( MC_(clo_mc_level) == 3 )) { |
| 2293 | OCacheLine* line; |
| 2294 | UWord lineoff = oc_line_offset(a); |
| 2295 | tl_assert(lineoff >= 0 |
| 2296 | && lineoff < OC_W32S_PER_LINE -1/*'cos 8-aligned*/); |
| 2297 | line = find_OCacheLine( a ); |
| 2298 | line->descr[lineoff+0] = 0; |
| 2299 | line->descr[lineoff+1] = 0; |
| 2300 | } |
| 2301 | //// END inlined, specialised version of MC_(helperc_b_store8) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2302 | #endif |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 2305 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2306 | /*------------------------------------------------------------*/ |
| 2307 | /*--- Stack pointer adjustment ---*/ |
| 2308 | /*------------------------------------------------------------*/ |
| 2309 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2310 | /*--------------- adjustment by 4 bytes ---------------*/ |
| 2311 | |
| 2312 | static void VG_REGPARM(2) mc_new_mem_stack_4_w_ECU(Addr new_SP, UInt ecu) |
| 2313 | { |
| 2314 | UInt otag = ecu | MC_OKIND_STACK; |
| 2315 | PROF_EVENT(110, "new_mem_stack_4"); |
| 2316 | if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2317 | make_aligned_word32_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP, otag ); |
| 2318 | } else { |
| 2319 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + new_SP, 4, otag ); |
| 2320 | } |
| 2321 | } |
| 2322 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2323 | static void VG_REGPARM(1) mc_new_mem_stack_4(Addr new_SP) |
| 2324 | { |
| 2325 | PROF_EVENT(110, "new_mem_stack_4"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2326 | if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2327 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2328 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2329 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + new_SP, 4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | static void VG_REGPARM(1) mc_die_mem_stack_4(Addr new_SP) |
| 2334 | { |
| 2335 | PROF_EVENT(120, "die_mem_stack_4"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2336 | if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2337 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2338 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2339 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-4, 4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2340 | } |
| 2341 | } |
| 2342 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2343 | /*--------------- adjustment by 8 bytes ---------------*/ |
| 2344 | |
| 2345 | static void VG_REGPARM(2) mc_new_mem_stack_8_w_ECU(Addr new_SP, UInt ecu) |
| 2346 | { |
| 2347 | UInt otag = ecu | MC_OKIND_STACK; |
| 2348 | PROF_EVENT(111, "new_mem_stack_8"); |
| 2349 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2350 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP, otag ); |
| 2351 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2352 | make_aligned_word32_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP , otag ); |
| 2353 | make_aligned_word32_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+4, otag ); |
| 2354 | } else { |
| 2355 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + new_SP, 8, otag ); |
| 2356 | } |
| 2357 | } |
| 2358 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2359 | static void VG_REGPARM(1) mc_new_mem_stack_8(Addr new_SP) |
| 2360 | { |
| 2361 | PROF_EVENT(111, "new_mem_stack_8"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2362 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2363 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2364 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2365 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2366 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2367 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2368 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + new_SP, 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | static void VG_REGPARM(1) mc_die_mem_stack_8(Addr new_SP) |
| 2373 | { |
| 2374 | PROF_EVENT(121, "die_mem_stack_8"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2375 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2376 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2377 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2378 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 ); |
| 2379 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2380 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2381 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-8, 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2382 | } |
| 2383 | } |
| 2384 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2385 | /*--------------- adjustment by 12 bytes ---------------*/ |
| 2386 | |
| 2387 | static void VG_REGPARM(2) mc_new_mem_stack_12_w_ECU(Addr new_SP, UInt ecu) |
| 2388 | { |
| 2389 | UInt otag = ecu | MC_OKIND_STACK; |
| 2390 | PROF_EVENT(112, "new_mem_stack_12"); |
| 2391 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2392 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP , otag ); |
| 2393 | make_aligned_word32_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+8, otag ); |
| 2394 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2395 | /* from previous test we don't have 8-alignment at offset +0, |
| 2396 | hence must have 8 alignment at offsets +4/-4. Hence safe to |
| 2397 | do 4 at +0 and then 8 at +4/. */ |
| 2398 | make_aligned_word32_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP , otag ); |
| 2399 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+4, otag ); |
| 2400 | } else { |
| 2401 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + new_SP, 12, otag ); |
| 2402 | } |
| 2403 | } |
| 2404 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2405 | static void VG_REGPARM(1) mc_new_mem_stack_12(Addr new_SP) |
| 2406 | { |
| 2407 | PROF_EVENT(112, "new_mem_stack_12"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2408 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2409 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2410 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2411 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2412 | /* from previous test we don't have 8-alignment at offset +0, |
| 2413 | hence must have 8 alignment at offsets +4/-4. Hence safe to |
| 2414 | do 4 at +0 and then 8 at +4/. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2415 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2416 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2417 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2418 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + new_SP, 12 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | static void VG_REGPARM(1) mc_die_mem_stack_12(Addr new_SP) |
| 2423 | { |
| 2424 | PROF_EVENT(122, "die_mem_stack_12"); |
| 2425 | /* Note the -12 in the test */ |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2426 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP-12 )) { |
| 2427 | /* We have 8-alignment at -12, hence ok to do 8 at -12 and 4 at |
| 2428 | -4. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2429 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 ); |
| 2430 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2431 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2432 | /* We have 4-alignment at +0, but we don't have 8-alignment at |
| 2433 | -12. So we must have 8-alignment at -8. Hence do 4 at -12 |
| 2434 | and then 8 at -8. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2435 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 ); |
| 2436 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2437 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2438 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-12, 12 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2439 | } |
| 2440 | } |
| 2441 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2442 | /*--------------- adjustment by 16 bytes ---------------*/ |
| 2443 | |
| 2444 | static void VG_REGPARM(2) mc_new_mem_stack_16_w_ECU(Addr new_SP, UInt ecu) |
| 2445 | { |
| 2446 | UInt otag = ecu | MC_OKIND_STACK; |
| 2447 | PROF_EVENT(113, "new_mem_stack_16"); |
| 2448 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2449 | /* Have 8-alignment at +0, hence do 8 at +0 and 8 at +8. */ |
| 2450 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP , otag ); |
| 2451 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+8, otag ); |
| 2452 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2453 | /* Have 4 alignment at +0 but not 8; hence 8 must be at +4. |
| 2454 | Hence do 4 at +0, 8 at +4, 4 at +12. */ |
| 2455 | make_aligned_word32_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP , otag ); |
| 2456 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+4 , otag ); |
| 2457 | make_aligned_word32_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+12, otag ); |
| 2458 | } else { |
| 2459 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + new_SP, 16, otag ); |
| 2460 | } |
| 2461 | } |
| 2462 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2463 | static void VG_REGPARM(1) mc_new_mem_stack_16(Addr new_SP) |
| 2464 | { |
| 2465 | PROF_EVENT(113, "new_mem_stack_16"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2466 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2467 | /* Have 8-alignment at +0, hence do 8 at +0 and 8 at +8. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2468 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2469 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2470 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2471 | /* Have 4 alignment at +0 but not 8; hence 8 must be at +4. |
| 2472 | Hence do 4 at +0, 8 at +4, 4 at +12. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2473 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2474 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 ); |
| 2475 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+12 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2476 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2477 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + new_SP, 16 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | static void VG_REGPARM(1) mc_die_mem_stack_16(Addr new_SP) |
| 2482 | { |
| 2483 | PROF_EVENT(123, "die_mem_stack_16"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2484 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2485 | /* Have 8-alignment at +0, hence do 8 at -16 and 8 at -8. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2486 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 2487 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2488 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2489 | /* 8 alignment must be at -12. Do 4 at -16, 8 at -12, 4 at -4. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2490 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 2491 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 ); |
| 2492 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2493 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2494 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-16, 16 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2495 | } |
| 2496 | } |
| 2497 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2498 | /*--------------- adjustment by 32 bytes ---------------*/ |
| 2499 | |
| 2500 | static void VG_REGPARM(2) mc_new_mem_stack_32_w_ECU(Addr new_SP, UInt ecu) |
| 2501 | { |
| 2502 | UInt otag = ecu | MC_OKIND_STACK; |
| 2503 | PROF_EVENT(114, "new_mem_stack_32"); |
| 2504 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2505 | /* Straightforward */ |
| 2506 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP , otag ); |
| 2507 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+8 , otag ); |
| 2508 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+16, otag ); |
| 2509 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+24, otag ); |
| 2510 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2511 | /* 8 alignment must be at +4. Hence do 8 at +4,+12,+20 and 4 at |
| 2512 | +0,+28. */ |
| 2513 | make_aligned_word32_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP , otag ); |
| 2514 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+4 , otag ); |
| 2515 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+12, otag ); |
| 2516 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+20, otag ); |
| 2517 | make_aligned_word32_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+28, otag ); |
| 2518 | } else { |
| 2519 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + new_SP, 32, otag ); |
| 2520 | } |
| 2521 | } |
| 2522 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2523 | static void VG_REGPARM(1) mc_new_mem_stack_32(Addr new_SP) |
| 2524 | { |
| 2525 | PROF_EVENT(114, "new_mem_stack_32"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2526 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2527 | /* Straightforward */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2528 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 2529 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2530 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 2531 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2532 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2533 | /* 8 alignment must be at +4. Hence do 8 at +4,+12,+20 and 4 at |
| 2534 | +0,+28. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2535 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 2536 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2537 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+12 ); |
| 2538 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+20 ); |
| 2539 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+28 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2540 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2541 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + new_SP, 32 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | static void VG_REGPARM(1) mc_die_mem_stack_32(Addr new_SP) |
| 2546 | { |
| 2547 | PROF_EVENT(124, "die_mem_stack_32"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2548 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2549 | /* Straightforward */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2550 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 2551 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 2552 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 2553 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2554 | } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 43fcfd9 | 2006-10-17 23:14:42 +0000 | [diff] [blame] | 2555 | /* 8 alignment must be at -4 etc. Hence do 8 at -12,-20,-28 and |
| 2556 | 4 at -32,-4. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2557 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 2558 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-28 ); |
| 2559 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-20 ); |
| 2560 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 ); |
| 2561 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2562 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2563 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-32, 32 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2564 | } |
| 2565 | } |
| 2566 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2567 | /*--------------- adjustment by 112 bytes ---------------*/ |
| 2568 | |
| 2569 | static void VG_REGPARM(2) mc_new_mem_stack_112_w_ECU(Addr new_SP, UInt ecu) |
| 2570 | { |
| 2571 | UInt otag = ecu | MC_OKIND_STACK; |
| 2572 | PROF_EVENT(115, "new_mem_stack_112"); |
| 2573 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2574 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP , otag ); |
| 2575 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+8 , otag ); |
| 2576 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+16, otag ); |
| 2577 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+24, otag ); |
| 2578 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+32, otag ); |
| 2579 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+40, otag ); |
| 2580 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+48, otag ); |
| 2581 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+56, otag ); |
| 2582 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+64, otag ); |
| 2583 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+72, otag ); |
| 2584 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+80, otag ); |
| 2585 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+88, otag ); |
| 2586 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+96, otag ); |
| 2587 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+104, otag ); |
| 2588 | } else { |
| 2589 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + new_SP, 112, otag ); |
| 2590 | } |
| 2591 | } |
| 2592 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2593 | static void VG_REGPARM(1) mc_new_mem_stack_112(Addr new_SP) |
| 2594 | { |
| 2595 | PROF_EVENT(115, "new_mem_stack_112"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2596 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2597 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 2598 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2599 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 2600 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
| 2601 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+32 ); |
| 2602 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+40 ); |
| 2603 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+48 ); |
| 2604 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+56 ); |
| 2605 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+64 ); |
| 2606 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+72 ); |
| 2607 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+80 ); |
| 2608 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+88 ); |
| 2609 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+96 ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2610 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+104 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2611 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2612 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + new_SP, 112 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | static void VG_REGPARM(1) mc_die_mem_stack_112(Addr new_SP) |
| 2617 | { |
| 2618 | PROF_EVENT(125, "die_mem_stack_112"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2619 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2620 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112); |
| 2621 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-104); |
| 2622 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-96 ); |
| 2623 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-88 ); |
| 2624 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-80 ); |
| 2625 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-72 ); |
| 2626 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-64 ); |
| 2627 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-56 ); |
| 2628 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-48 ); |
| 2629 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-40 ); |
| 2630 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 2631 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 2632 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 2633 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2634 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2635 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-112, 112 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2639 | /*--------------- adjustment by 128 bytes ---------------*/ |
| 2640 | |
| 2641 | static void VG_REGPARM(2) mc_new_mem_stack_128_w_ECU(Addr new_SP, UInt ecu) |
| 2642 | { |
| 2643 | UInt otag = ecu | MC_OKIND_STACK; |
| 2644 | PROF_EVENT(116, "new_mem_stack_128"); |
| 2645 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2646 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP , otag ); |
| 2647 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+8 , otag ); |
| 2648 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+16, otag ); |
| 2649 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+24, otag ); |
| 2650 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+32, otag ); |
| 2651 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+40, otag ); |
| 2652 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+48, otag ); |
| 2653 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+56, otag ); |
| 2654 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+64, otag ); |
| 2655 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+72, otag ); |
| 2656 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+80, otag ); |
| 2657 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+88, otag ); |
| 2658 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+96, otag ); |
| 2659 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+104, otag ); |
| 2660 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+112, otag ); |
| 2661 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+120, otag ); |
| 2662 | } else { |
| 2663 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + new_SP, 128, otag ); |
| 2664 | } |
| 2665 | } |
| 2666 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2667 | static void VG_REGPARM(1) mc_new_mem_stack_128(Addr new_SP) |
| 2668 | { |
| 2669 | PROF_EVENT(116, "new_mem_stack_128"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2670 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2671 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 2672 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2673 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 2674 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
| 2675 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+32 ); |
| 2676 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+40 ); |
| 2677 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+48 ); |
| 2678 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+56 ); |
| 2679 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+64 ); |
| 2680 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+72 ); |
| 2681 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+80 ); |
| 2682 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+88 ); |
| 2683 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+96 ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2684 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+104 ); |
| 2685 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+112 ); |
| 2686 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+120 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2687 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2688 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + new_SP, 128 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | static void VG_REGPARM(1) mc_die_mem_stack_128(Addr new_SP) |
| 2693 | { |
| 2694 | PROF_EVENT(126, "die_mem_stack_128"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2695 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2696 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-128); |
| 2697 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-120); |
| 2698 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112); |
| 2699 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-104); |
| 2700 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-96 ); |
| 2701 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-88 ); |
| 2702 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-80 ); |
| 2703 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-72 ); |
| 2704 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-64 ); |
| 2705 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-56 ); |
| 2706 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-48 ); |
| 2707 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-40 ); |
| 2708 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 2709 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 2710 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 2711 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2712 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2713 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-128, 128 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2714 | } |
| 2715 | } |
| 2716 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2717 | /*--------------- adjustment by 144 bytes ---------------*/ |
| 2718 | |
| 2719 | static void VG_REGPARM(2) mc_new_mem_stack_144_w_ECU(Addr new_SP, UInt ecu) |
| 2720 | { |
| 2721 | UInt otag = ecu | MC_OKIND_STACK; |
| 2722 | PROF_EVENT(117, "new_mem_stack_144"); |
| 2723 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2724 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP, otag ); |
| 2725 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+8, otag ); |
| 2726 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+16, otag ); |
| 2727 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+24, otag ); |
| 2728 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+32, otag ); |
| 2729 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+40, otag ); |
| 2730 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+48, otag ); |
| 2731 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+56, otag ); |
| 2732 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+64, otag ); |
| 2733 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+72, otag ); |
| 2734 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+80, otag ); |
| 2735 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+88, otag ); |
| 2736 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+96, otag ); |
| 2737 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+104, otag ); |
| 2738 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+112, otag ); |
| 2739 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+120, otag ); |
| 2740 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+128, otag ); |
| 2741 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+136, otag ); |
| 2742 | } else { |
| 2743 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + new_SP, 144, otag ); |
| 2744 | } |
| 2745 | } |
| 2746 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2747 | static void VG_REGPARM(1) mc_new_mem_stack_144(Addr new_SP) |
| 2748 | { |
| 2749 | PROF_EVENT(117, "new_mem_stack_144"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2750 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2751 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 2752 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2753 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 2754 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
| 2755 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+32 ); |
| 2756 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+40 ); |
| 2757 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+48 ); |
| 2758 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+56 ); |
| 2759 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+64 ); |
| 2760 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+72 ); |
| 2761 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+80 ); |
| 2762 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+88 ); |
| 2763 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+96 ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2764 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+104 ); |
| 2765 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+112 ); |
| 2766 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+120 ); |
| 2767 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+128 ); |
| 2768 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+136 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2769 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2770 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + new_SP, 144 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | static void VG_REGPARM(1) mc_die_mem_stack_144(Addr new_SP) |
| 2775 | { |
| 2776 | PROF_EVENT(127, "die_mem_stack_144"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2777 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2778 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-144); |
| 2779 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-136); |
| 2780 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-128); |
| 2781 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-120); |
| 2782 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112); |
| 2783 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-104); |
| 2784 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-96 ); |
| 2785 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-88 ); |
| 2786 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-80 ); |
| 2787 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-72 ); |
| 2788 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-64 ); |
| 2789 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-56 ); |
| 2790 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-48 ); |
| 2791 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-40 ); |
| 2792 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 2793 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 2794 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 2795 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2796 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2797 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-144, 144 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2798 | } |
| 2799 | } |
| 2800 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2801 | /*--------------- adjustment by 160 bytes ---------------*/ |
| 2802 | |
| 2803 | static void VG_REGPARM(2) mc_new_mem_stack_160_w_ECU(Addr new_SP, UInt ecu) |
| 2804 | { |
| 2805 | UInt otag = ecu | MC_OKIND_STACK; |
| 2806 | PROF_EVENT(118, "new_mem_stack_160"); |
| 2807 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
| 2808 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP, otag ); |
| 2809 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+8, otag ); |
| 2810 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+16, otag ); |
| 2811 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+24, otag ); |
| 2812 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+32, otag ); |
| 2813 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+40, otag ); |
| 2814 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+48, otag ); |
| 2815 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+56, otag ); |
| 2816 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+64, otag ); |
| 2817 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+72, otag ); |
| 2818 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+80, otag ); |
| 2819 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+88, otag ); |
| 2820 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+96, otag ); |
| 2821 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+104, otag ); |
| 2822 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+112, otag ); |
| 2823 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+120, otag ); |
| 2824 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+128, otag ); |
| 2825 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+136, otag ); |
| 2826 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+144, otag ); |
| 2827 | make_aligned_word64_undefined_w_otag ( -VG_STACK_REDZONE_SZB + new_SP+152, otag ); |
| 2828 | } else { |
| 2829 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + new_SP, 160, otag ); |
| 2830 | } |
| 2831 | } |
| 2832 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2833 | static void VG_REGPARM(1) mc_new_mem_stack_160(Addr new_SP) |
| 2834 | { |
| 2835 | PROF_EVENT(118, "new_mem_stack_160"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2836 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2837 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 2838 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2839 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 2840 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
| 2841 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+32 ); |
| 2842 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+40 ); |
| 2843 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+48 ); |
| 2844 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+56 ); |
| 2845 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+64 ); |
| 2846 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+72 ); |
| 2847 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+80 ); |
| 2848 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+88 ); |
| 2849 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+96 ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2850 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+104 ); |
| 2851 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+112 ); |
| 2852 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+120 ); |
| 2853 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+128 ); |
| 2854 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+136 ); |
| 2855 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+144 ); |
| 2856 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+152 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2857 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2858 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + new_SP, 160 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | static void VG_REGPARM(1) mc_die_mem_stack_160(Addr new_SP) |
| 2863 | { |
| 2864 | PROF_EVENT(128, "die_mem_stack_160"); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 2865 | if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2866 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-160); |
| 2867 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-152); |
| 2868 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-144); |
| 2869 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-136); |
| 2870 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-128); |
| 2871 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-120); |
| 2872 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112); |
| 2873 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-104); |
| 2874 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-96 ); |
| 2875 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-88 ); |
| 2876 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-80 ); |
| 2877 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-72 ); |
| 2878 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-64 ); |
| 2879 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-56 ); |
| 2880 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-48 ); |
| 2881 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-40 ); |
| 2882 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 2883 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 2884 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 2885 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2886 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2887 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-160, 160 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2888 | } |
| 2889 | } |
| 2890 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2891 | /*--------------- adjustment by N bytes ---------------*/ |
| 2892 | |
| 2893 | static void mc_new_mem_stack_w_ECU ( Addr a, SizeT len, UInt ecu ) |
| 2894 | { |
| 2895 | UInt otag = ecu | MC_OKIND_STACK; |
| 2896 | PROF_EVENT(115, "new_mem_stack_w_otag"); |
| 2897 | MC_(make_mem_undefined_w_otag) ( -VG_STACK_REDZONE_SZB + a, len, otag ); |
| 2898 | } |
| 2899 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2900 | static void mc_new_mem_stack ( Addr a, SizeT len ) |
| 2901 | { |
| 2902 | PROF_EVENT(115, "new_mem_stack"); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2903 | make_mem_undefined ( -VG_STACK_REDZONE_SZB + a, len ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | static void mc_die_mem_stack ( Addr a, SizeT len ) |
| 2907 | { |
| 2908 | PROF_EVENT(125, "die_mem_stack"); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2909 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + a, len ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2910 | } |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 2911 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2912 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2913 | /* The AMD64 ABI says: |
| 2914 | |
| 2915 | "The 128-byte area beyond the location pointed to by %rsp is considered |
| 2916 | to be reserved and shall not be modified by signal or interrupt |
| 2917 | handlers. Therefore, functions may use this area for temporary data |
| 2918 | that is not needed across function calls. In particular, leaf functions |
| 2919 | may use this area for their entire stack frame, rather than adjusting |
| 2920 | the stack pointer in the prologue and epilogue. This area is known as |
| 2921 | red zone [sic]." |
| 2922 | |
| 2923 | So after any call or return we need to mark this redzone as containing |
| 2924 | undefined values. |
| 2925 | |
| 2926 | Consider this: we're in function f. f calls g. g moves rsp down |
| 2927 | modestly (say 16 bytes) and writes stuff all over the red zone, making it |
| 2928 | defined. g returns. f is buggy and reads from parts of the red zone |
| 2929 | that it didn't write on. But because g filled that area in, f is going |
| 2930 | to be picking up defined V bits and so any errors from reading bits of |
| 2931 | the red zone it didn't write, will be missed. The only solution I could |
| 2932 | think of was to make the red zone undefined when g returns to f. |
| 2933 | |
| 2934 | This is in accordance with the ABI, which makes it clear the redzone |
| 2935 | is volatile across function calls. |
| 2936 | |
| 2937 | The problem occurs the other way round too: f could fill the RZ up |
| 2938 | with defined values and g could mistakenly read them. So the RZ |
| 2939 | also needs to be nuked on function calls. |
| 2940 | */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2941 | |
| 2942 | |
| 2943 | /* Here's a simple cache to hold nia -> ECU mappings. It could be |
| 2944 | improved so as to have a lower miss rate. */ |
| 2945 | |
| 2946 | static UWord stats__nia_cache_queries = 0; |
| 2947 | static UWord stats__nia_cache_misses = 0; |
| 2948 | |
| 2949 | typedef |
| 2950 | struct { UWord nia0; UWord ecu0; /* nia0 maps to ecu0 */ |
| 2951 | UWord nia1; UWord ecu1; } /* nia1 maps to ecu1 */ |
| 2952 | WCacheEnt; |
| 2953 | |
| 2954 | #define N_NIA_TO_ECU_CACHE 511 |
| 2955 | |
| 2956 | static WCacheEnt nia_to_ecu_cache[N_NIA_TO_ECU_CACHE]; |
| 2957 | |
| 2958 | static void init_nia_to_ecu_cache ( void ) |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 2959 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2960 | UWord i; |
| 2961 | Addr zero_addr = 0; |
| 2962 | ExeContext* zero_ec; |
| 2963 | UInt zero_ecu; |
| 2964 | /* Fill all the slots with an entry for address zero, and the |
| 2965 | relevant otags accordingly. Hence the cache is initially filled |
| 2966 | with valid data. */ |
| 2967 | zero_ec = VG_(make_depth_1_ExeContext_from_Addr)(zero_addr); |
| 2968 | tl_assert(zero_ec); |
| 2969 | zero_ecu = VG_(get_ECU_from_ExeContext)(zero_ec); |
| 2970 | tl_assert(VG_(is_plausible_ECU)(zero_ecu)); |
| 2971 | for (i = 0; i < N_NIA_TO_ECU_CACHE; i++) { |
| 2972 | nia_to_ecu_cache[i].nia0 = zero_addr; |
| 2973 | nia_to_ecu_cache[i].ecu0 = zero_ecu; |
| 2974 | nia_to_ecu_cache[i].nia1 = zero_addr; |
| 2975 | nia_to_ecu_cache[i].ecu1 = zero_ecu; |
| 2976 | } |
| 2977 | } |
| 2978 | |
| 2979 | static inline UInt convert_nia_to_ecu ( Addr nia ) |
| 2980 | { |
| 2981 | UWord i; |
| 2982 | UInt ecu; |
| 2983 | ExeContext* ec; |
| 2984 | |
| 2985 | tl_assert( sizeof(nia_to_ecu_cache[0].nia1) == sizeof(nia) ); |
| 2986 | |
| 2987 | stats__nia_cache_queries++; |
| 2988 | i = nia % N_NIA_TO_ECU_CACHE; |
| 2989 | tl_assert(i >= 0 && i < N_NIA_TO_ECU_CACHE); |
| 2990 | |
| 2991 | if (LIKELY( nia_to_ecu_cache[i].nia0 == nia )) |
| 2992 | return nia_to_ecu_cache[i].ecu0; |
| 2993 | |
| 2994 | if (LIKELY( nia_to_ecu_cache[i].nia1 == nia )) { |
| 2995 | # define SWAP(_w1,_w2) { UWord _t = _w1; _w1 = _w2; _w2 = _t; } |
| 2996 | SWAP( nia_to_ecu_cache[i].nia0, nia_to_ecu_cache[i].nia1 ); |
| 2997 | SWAP( nia_to_ecu_cache[i].ecu0, nia_to_ecu_cache[i].ecu1 ); |
| 2998 | # undef SWAP |
| 2999 | return nia_to_ecu_cache[i].ecu0; |
| 3000 | } |
| 3001 | |
| 3002 | stats__nia_cache_misses++; |
| 3003 | ec = VG_(make_depth_1_ExeContext_from_Addr)(nia); |
| 3004 | tl_assert(ec); |
| 3005 | ecu = VG_(get_ECU_from_ExeContext)(ec); |
| 3006 | tl_assert(VG_(is_plausible_ECU)(ecu)); |
| 3007 | |
| 3008 | nia_to_ecu_cache[i].nia1 = nia_to_ecu_cache[i].nia0; |
| 3009 | nia_to_ecu_cache[i].ecu1 = nia_to_ecu_cache[i].ecu0; |
| 3010 | |
| 3011 | nia_to_ecu_cache[i].nia0 = nia; |
| 3012 | nia_to_ecu_cache[i].ecu0 = (UWord)ecu; |
| 3013 | return ecu; |
| 3014 | } |
| 3015 | |
| 3016 | |
| 3017 | /* Note that this serves both the origin-tracking and |
| 3018 | no-origin-tracking modes. We assume that calls to it are |
| 3019 | sufficiently infrequent that it isn't worth specialising for the |
| 3020 | with/without origin-tracking cases. */ |
| 3021 | void MC_(helperc_MAKE_STACK_UNINIT) ( Addr base, UWord len, Addr nia ) |
| 3022 | { |
| 3023 | UInt otag; |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 3024 | tl_assert(sizeof(UWord) == sizeof(SizeT)); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3025 | if (0) |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3026 | VG_(printf)("helperc_MAKE_STACK_UNINIT (%p,%lu,nia=%p)\n", |
| 3027 | base, len, nia ); |
| 3028 | |
| 3029 | if (UNLIKELY( MC_(clo_mc_level) == 3 )) { |
| 3030 | UInt ecu = convert_nia_to_ecu ( nia ); |
| 3031 | tl_assert(VG_(is_plausible_ECU)(ecu)); |
| 3032 | otag = ecu | MC_OKIND_STACK; |
| 3033 | } else { |
| 3034 | tl_assert(nia == 0); |
| 3035 | otag = 0; |
| 3036 | } |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3037 | |
| 3038 | # if 0 |
| 3039 | /* Really slow version */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3040 | MC_(make_mem_undefined)(base, len, otag); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3041 | # endif |
| 3042 | |
| 3043 | # if 0 |
| 3044 | /* Slow(ish) version, which is fairly easily seen to be correct. |
| 3045 | */ |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3046 | if (LIKELY( VG_IS_8_ALIGNED(base) && len==128 )) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3047 | make_aligned_word64_undefined(base + 0, otag); |
| 3048 | make_aligned_word64_undefined(base + 8, otag); |
| 3049 | make_aligned_word64_undefined(base + 16, otag); |
| 3050 | make_aligned_word64_undefined(base + 24, otag); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3051 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3052 | make_aligned_word64_undefined(base + 32, otag); |
| 3053 | make_aligned_word64_undefined(base + 40, otag); |
| 3054 | make_aligned_word64_undefined(base + 48, otag); |
| 3055 | make_aligned_word64_undefined(base + 56, otag); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3056 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3057 | make_aligned_word64_undefined(base + 64, otag); |
| 3058 | make_aligned_word64_undefined(base + 72, otag); |
| 3059 | make_aligned_word64_undefined(base + 80, otag); |
| 3060 | make_aligned_word64_undefined(base + 88, otag); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3061 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3062 | make_aligned_word64_undefined(base + 96, otag); |
| 3063 | make_aligned_word64_undefined(base + 104, otag); |
| 3064 | make_aligned_word64_undefined(base + 112, otag); |
| 3065 | make_aligned_word64_undefined(base + 120, otag); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3066 | } else { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3067 | MC_(make_mem_undefined)(base, len, otag); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3068 | } |
| 3069 | # endif |
| 3070 | |
| 3071 | /* Idea is: go fast when |
| 3072 | * 8-aligned and length is 128 |
| 3073 | * the sm is available in the main primary map |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3074 | * the address range falls entirely with a single secondary map |
| 3075 | If all those conditions hold, just update the V+A bits by writing |
| 3076 | directly into the vabits array. (If the sm was distinguished, this |
| 3077 | will make a copy and then write to it.) |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3078 | */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3079 | |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3080 | if (LIKELY( len == 128 && VG_IS_8_ALIGNED(base) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3081 | /* Now we know the address range is suitably sized and aligned. */ |
| 3082 | UWord a_lo = (UWord)(base); |
sewardj | 3f5f556 | 2006-06-16 21:39:08 +0000 | [diff] [blame] | 3083 | UWord a_hi = (UWord)(base + 128 - 1); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3084 | tl_assert(a_lo < a_hi); // paranoia: detect overflow |
sewardj | 7244e71 | 2008-05-02 12:35:48 +0000 | [diff] [blame] | 3085 | if (a_hi <= MAX_PRIMARY_ADDRESS) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3086 | // Now we know the entire range is within the main primary map. |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3087 | SecMap* sm = get_secmap_for_writing_low(a_lo); |
| 3088 | SecMap* sm_hi = get_secmap_for_writing_low(a_hi); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3089 | /* Now we know that the entire address range falls within a |
| 3090 | single secondary map, and that that secondary 'lives' in |
| 3091 | the main primary map. */ |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3092 | if (LIKELY(sm == sm_hi)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3093 | // Finally, we know that the range is entirely within one secmap. |
| 3094 | UWord v_off = SM_OFF(a_lo); |
| 3095 | UShort* p = (UShort*)(&sm->vabits8[v_off]); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3096 | p[ 0] = VA_BITS16_UNDEFINED; |
| 3097 | p[ 1] = VA_BITS16_UNDEFINED; |
| 3098 | p[ 2] = VA_BITS16_UNDEFINED; |
| 3099 | p[ 3] = VA_BITS16_UNDEFINED; |
| 3100 | p[ 4] = VA_BITS16_UNDEFINED; |
| 3101 | p[ 5] = VA_BITS16_UNDEFINED; |
| 3102 | p[ 6] = VA_BITS16_UNDEFINED; |
| 3103 | p[ 7] = VA_BITS16_UNDEFINED; |
| 3104 | p[ 8] = VA_BITS16_UNDEFINED; |
| 3105 | p[ 9] = VA_BITS16_UNDEFINED; |
| 3106 | p[10] = VA_BITS16_UNDEFINED; |
| 3107 | p[11] = VA_BITS16_UNDEFINED; |
| 3108 | p[12] = VA_BITS16_UNDEFINED; |
| 3109 | p[13] = VA_BITS16_UNDEFINED; |
| 3110 | p[14] = VA_BITS16_UNDEFINED; |
| 3111 | p[15] = VA_BITS16_UNDEFINED; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3112 | if (UNLIKELY( MC_(clo_mc_level) == 3 )) { |
| 3113 | set_aligned_word64_Origin_to_undef( base + 8 * 0, otag ); |
| 3114 | set_aligned_word64_Origin_to_undef( base + 8 * 1, otag ); |
| 3115 | set_aligned_word64_Origin_to_undef( base + 8 * 2, otag ); |
| 3116 | set_aligned_word64_Origin_to_undef( base + 8 * 3, otag ); |
| 3117 | set_aligned_word64_Origin_to_undef( base + 8 * 4, otag ); |
| 3118 | set_aligned_word64_Origin_to_undef( base + 8 * 5, otag ); |
| 3119 | set_aligned_word64_Origin_to_undef( base + 8 * 6, otag ); |
| 3120 | set_aligned_word64_Origin_to_undef( base + 8 * 7, otag ); |
| 3121 | set_aligned_word64_Origin_to_undef( base + 8 * 8, otag ); |
| 3122 | set_aligned_word64_Origin_to_undef( base + 8 * 9, otag ); |
| 3123 | set_aligned_word64_Origin_to_undef( base + 8 * 10, otag ); |
| 3124 | set_aligned_word64_Origin_to_undef( base + 8 * 11, otag ); |
| 3125 | set_aligned_word64_Origin_to_undef( base + 8 * 12, otag ); |
| 3126 | set_aligned_word64_Origin_to_undef( base + 8 * 13, otag ); |
| 3127 | set_aligned_word64_Origin_to_undef( base + 8 * 14, otag ); |
| 3128 | set_aligned_word64_Origin_to_undef( base + 8 * 15, otag ); |
| 3129 | } |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3130 | return; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3131 | } |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3132 | } |
| 3133 | } |
| 3134 | |
sewardj | 2e1a677 | 2006-01-18 04:16:27 +0000 | [diff] [blame] | 3135 | /* 288 bytes (36 ULongs) is the magic value for ELF ppc64. */ |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3136 | if (LIKELY( len == 288 && VG_IS_8_ALIGNED(base) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3137 | /* Now we know the address range is suitably sized and aligned. */ |
| 3138 | UWord a_lo = (UWord)(base); |
sewardj | 3f5f556 | 2006-06-16 21:39:08 +0000 | [diff] [blame] | 3139 | UWord a_hi = (UWord)(base + 288 - 1); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3140 | tl_assert(a_lo < a_hi); // paranoia: detect overflow |
sewardj | 7244e71 | 2008-05-02 12:35:48 +0000 | [diff] [blame] | 3141 | if (a_hi <= MAX_PRIMARY_ADDRESS) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3142 | // Now we know the entire range is within the main primary map. |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3143 | SecMap* sm = get_secmap_for_writing_low(a_lo); |
| 3144 | SecMap* sm_hi = get_secmap_for_writing_low(a_hi); |
sewardj | 2e1a677 | 2006-01-18 04:16:27 +0000 | [diff] [blame] | 3145 | /* Now we know that the entire address range falls within a |
| 3146 | single secondary map, and that that secondary 'lives' in |
| 3147 | the main primary map. */ |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3148 | if (LIKELY(sm == sm_hi)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3149 | // Finally, we know that the range is entirely within one secmap. |
| 3150 | UWord v_off = SM_OFF(a_lo); |
| 3151 | UShort* p = (UShort*)(&sm->vabits8[v_off]); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3152 | p[ 0] = VA_BITS16_UNDEFINED; |
| 3153 | p[ 1] = VA_BITS16_UNDEFINED; |
| 3154 | p[ 2] = VA_BITS16_UNDEFINED; |
| 3155 | p[ 3] = VA_BITS16_UNDEFINED; |
| 3156 | p[ 4] = VA_BITS16_UNDEFINED; |
| 3157 | p[ 5] = VA_BITS16_UNDEFINED; |
| 3158 | p[ 6] = VA_BITS16_UNDEFINED; |
| 3159 | p[ 7] = VA_BITS16_UNDEFINED; |
| 3160 | p[ 8] = VA_BITS16_UNDEFINED; |
| 3161 | p[ 9] = VA_BITS16_UNDEFINED; |
| 3162 | p[10] = VA_BITS16_UNDEFINED; |
| 3163 | p[11] = VA_BITS16_UNDEFINED; |
| 3164 | p[12] = VA_BITS16_UNDEFINED; |
| 3165 | p[13] = VA_BITS16_UNDEFINED; |
| 3166 | p[14] = VA_BITS16_UNDEFINED; |
| 3167 | p[15] = VA_BITS16_UNDEFINED; |
| 3168 | p[16] = VA_BITS16_UNDEFINED; |
| 3169 | p[17] = VA_BITS16_UNDEFINED; |
| 3170 | p[18] = VA_BITS16_UNDEFINED; |
| 3171 | p[19] = VA_BITS16_UNDEFINED; |
| 3172 | p[20] = VA_BITS16_UNDEFINED; |
| 3173 | p[21] = VA_BITS16_UNDEFINED; |
| 3174 | p[22] = VA_BITS16_UNDEFINED; |
| 3175 | p[23] = VA_BITS16_UNDEFINED; |
| 3176 | p[24] = VA_BITS16_UNDEFINED; |
| 3177 | p[25] = VA_BITS16_UNDEFINED; |
| 3178 | p[26] = VA_BITS16_UNDEFINED; |
| 3179 | p[27] = VA_BITS16_UNDEFINED; |
| 3180 | p[28] = VA_BITS16_UNDEFINED; |
| 3181 | p[29] = VA_BITS16_UNDEFINED; |
| 3182 | p[30] = VA_BITS16_UNDEFINED; |
| 3183 | p[31] = VA_BITS16_UNDEFINED; |
| 3184 | p[32] = VA_BITS16_UNDEFINED; |
| 3185 | p[33] = VA_BITS16_UNDEFINED; |
| 3186 | p[34] = VA_BITS16_UNDEFINED; |
| 3187 | p[35] = VA_BITS16_UNDEFINED; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3188 | if (UNLIKELY( MC_(clo_mc_level) == 3 )) { |
| 3189 | set_aligned_word64_Origin_to_undef( base + 8 * 0, otag ); |
| 3190 | set_aligned_word64_Origin_to_undef( base + 8 * 1, otag ); |
| 3191 | set_aligned_word64_Origin_to_undef( base + 8 * 2, otag ); |
| 3192 | set_aligned_word64_Origin_to_undef( base + 8 * 3, otag ); |
| 3193 | set_aligned_word64_Origin_to_undef( base + 8 * 4, otag ); |
| 3194 | set_aligned_word64_Origin_to_undef( base + 8 * 5, otag ); |
| 3195 | set_aligned_word64_Origin_to_undef( base + 8 * 6, otag ); |
| 3196 | set_aligned_word64_Origin_to_undef( base + 8 * 7, otag ); |
| 3197 | set_aligned_word64_Origin_to_undef( base + 8 * 8, otag ); |
| 3198 | set_aligned_word64_Origin_to_undef( base + 8 * 9, otag ); |
| 3199 | set_aligned_word64_Origin_to_undef( base + 8 * 10, otag ); |
| 3200 | set_aligned_word64_Origin_to_undef( base + 8 * 11, otag ); |
| 3201 | set_aligned_word64_Origin_to_undef( base + 8 * 12, otag ); |
| 3202 | set_aligned_word64_Origin_to_undef( base + 8 * 13, otag ); |
| 3203 | set_aligned_word64_Origin_to_undef( base + 8 * 14, otag ); |
| 3204 | set_aligned_word64_Origin_to_undef( base + 8 * 15, otag ); |
| 3205 | set_aligned_word64_Origin_to_undef( base + 8 * 16, otag ); |
| 3206 | set_aligned_word64_Origin_to_undef( base + 8 * 17, otag ); |
| 3207 | set_aligned_word64_Origin_to_undef( base + 8 * 18, otag ); |
| 3208 | set_aligned_word64_Origin_to_undef( base + 8 * 19, otag ); |
| 3209 | set_aligned_word64_Origin_to_undef( base + 8 * 20, otag ); |
| 3210 | set_aligned_word64_Origin_to_undef( base + 8 * 21, otag ); |
| 3211 | set_aligned_word64_Origin_to_undef( base + 8 * 22, otag ); |
| 3212 | set_aligned_word64_Origin_to_undef( base + 8 * 23, otag ); |
| 3213 | set_aligned_word64_Origin_to_undef( base + 8 * 24, otag ); |
| 3214 | set_aligned_word64_Origin_to_undef( base + 8 * 25, otag ); |
| 3215 | set_aligned_word64_Origin_to_undef( base + 8 * 26, otag ); |
| 3216 | set_aligned_word64_Origin_to_undef( base + 8 * 27, otag ); |
| 3217 | set_aligned_word64_Origin_to_undef( base + 8 * 28, otag ); |
| 3218 | set_aligned_word64_Origin_to_undef( base + 8 * 29, otag ); |
| 3219 | set_aligned_word64_Origin_to_undef( base + 8 * 30, otag ); |
| 3220 | set_aligned_word64_Origin_to_undef( base + 8 * 31, otag ); |
| 3221 | set_aligned_word64_Origin_to_undef( base + 8 * 32, otag ); |
| 3222 | set_aligned_word64_Origin_to_undef( base + 8 * 33, otag ); |
| 3223 | set_aligned_word64_Origin_to_undef( base + 8 * 34, otag ); |
| 3224 | set_aligned_word64_Origin_to_undef( base + 8 * 35, otag ); |
| 3225 | } |
sewardj | 2e1a677 | 2006-01-18 04:16:27 +0000 | [diff] [blame] | 3226 | return; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3227 | } |
sewardj | 2e1a677 | 2006-01-18 04:16:27 +0000 | [diff] [blame] | 3228 | } |
| 3229 | } |
| 3230 | |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 3231 | /* else fall into slow case */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3232 | MC_(make_mem_undefined_w_otag)(base, len, otag); |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3236 | /*------------------------------------------------------------*/ |
| 3237 | /*--- Checking memory ---*/ |
| 3238 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3239 | |
sewardj | e4ccc01 | 2005-05-02 12:53:38 +0000 | [diff] [blame] | 3240 | typedef |
| 3241 | enum { |
| 3242 | MC_Ok = 5, |
| 3243 | MC_AddrErr = 6, |
| 3244 | MC_ValueErr = 7 |
| 3245 | } |
| 3246 | MC_ReadResult; |
| 3247 | |
| 3248 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3249 | /* Check permissions for address range. If inadequate permissions |
| 3250 | exist, *bad_addr is set to the offending address, so the caller can |
| 3251 | know what it is. */ |
| 3252 | |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 3253 | /* Returns True if [a .. a+len) is not addressible. Otherwise, |
| 3254 | returns False, and if bad_addr is non-NULL, sets *bad_addr to |
| 3255 | indicate the lowest failing address. Functions below are |
| 3256 | similar. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3257 | Bool MC_(check_mem_is_noaccess) ( Addr a, SizeT len, Addr* bad_addr ) |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 3258 | { |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 3259 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3260 | UWord vabits2; |
| 3261 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3262 | PROF_EVENT(60, "check_mem_is_noaccess"); |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 3263 | for (i = 0; i < len; i++) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3264 | PROF_EVENT(61, "check_mem_is_noaccess(loop)"); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3265 | vabits2 = get_vabits2(a); |
| 3266 | if (VA_BITS2_NOACCESS != vabits2) { |
| 3267 | if (bad_addr != NULL) *bad_addr = a; |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 3268 | return False; |
| 3269 | } |
| 3270 | a++; |
| 3271 | } |
| 3272 | return True; |
| 3273 | } |
| 3274 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3275 | static Bool is_mem_addressable ( Addr a, SizeT len, |
| 3276 | /*OUT*/Addr* bad_addr ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3277 | { |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 3278 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3279 | UWord vabits2; |
| 3280 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3281 | PROF_EVENT(62, "is_mem_addressable"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3282 | for (i = 0; i < len; i++) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3283 | PROF_EVENT(63, "is_mem_addressable(loop)"); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3284 | vabits2 = get_vabits2(a); |
| 3285 | if (VA_BITS2_NOACCESS == vabits2) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3286 | if (bad_addr != NULL) *bad_addr = a; |
| 3287 | return False; |
| 3288 | } |
| 3289 | a++; |
| 3290 | } |
| 3291 | return True; |
| 3292 | } |
| 3293 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3294 | static MC_ReadResult is_mem_defined ( Addr a, SizeT len, |
| 3295 | /*OUT*/Addr* bad_addr, |
| 3296 | /*OUT*/UInt* otag ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3297 | { |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 3298 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3299 | UWord vabits2; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3300 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3301 | PROF_EVENT(64, "is_mem_defined"); |
| 3302 | DEBUG("is_mem_defined\n"); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3303 | |
| 3304 | if (otag) *otag = 0; |
| 3305 | if (bad_addr) *bad_addr = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3306 | for (i = 0; i < len; i++) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3307 | PROF_EVENT(65, "is_mem_defined(loop)"); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3308 | vabits2 = get_vabits2(a); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3309 | if (VA_BITS2_DEFINED != vabits2) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3310 | // Error! Nb: Report addressability errors in preference to |
| 3311 | // definedness errors. And don't report definedeness errors unless |
| 3312 | // --undef-value-errors=yes. |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3313 | if (bad_addr) { |
| 3314 | *bad_addr = a; |
| 3315 | } |
| 3316 | if (VA_BITS2_NOACCESS == vabits2) { |
| 3317 | return MC_AddrErr; |
| 3318 | } |
| 3319 | if (MC_(clo_mc_level) >= 2) { |
| 3320 | if (otag && MC_(clo_mc_level) == 3) { |
| 3321 | *otag = MC_(helperc_b_load1)( a ); |
| 3322 | } |
| 3323 | return MC_ValueErr; |
| 3324 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3325 | } |
| 3326 | a++; |
| 3327 | } |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3328 | return MC_Ok; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | |
| 3332 | /* Check a zero-terminated ascii string. Tricky -- don't want to |
| 3333 | examine the actual bytes, to find the end, until we're sure it is |
| 3334 | safe to do so. */ |
| 3335 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3336 | static Bool mc_is_defined_asciiz ( Addr a, Addr* bad_addr, UInt* otag ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3337 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3338 | UWord vabits2; |
| 3339 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3340 | PROF_EVENT(66, "mc_is_defined_asciiz"); |
| 3341 | DEBUG("mc_is_defined_asciiz\n"); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3342 | |
| 3343 | if (otag) *otag = 0; |
| 3344 | if (bad_addr) *bad_addr = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3345 | while (True) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3346 | PROF_EVENT(67, "mc_is_defined_asciiz(loop)"); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3347 | vabits2 = get_vabits2(a); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3348 | if (VA_BITS2_DEFINED != vabits2) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3349 | // Error! Nb: Report addressability errors in preference to |
| 3350 | // definedness errors. And don't report definedeness errors unless |
| 3351 | // --undef-value-errors=yes. |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3352 | if (bad_addr) { |
| 3353 | *bad_addr = a; |
| 3354 | } |
| 3355 | if (VA_BITS2_NOACCESS == vabits2) { |
| 3356 | return MC_AddrErr; |
| 3357 | } |
| 3358 | if (MC_(clo_mc_level) >= 2) { |
| 3359 | if (otag && MC_(clo_mc_level) == 3) { |
| 3360 | *otag = MC_(helperc_b_load1)( a ); |
| 3361 | } |
| 3362 | return MC_ValueErr; |
| 3363 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3364 | } |
| 3365 | /* Ok, a is safe to read. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3366 | if (* ((UChar*)a) == 0) { |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3367 | return MC_Ok; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3368 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3369 | a++; |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | |
| 3374 | /*------------------------------------------------------------*/ |
| 3375 | /*--- Memory event handlers ---*/ |
| 3376 | /*------------------------------------------------------------*/ |
| 3377 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3378 | static |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3379 | void check_mem_is_addressable ( CorePart part, ThreadId tid, Char* s, |
| 3380 | Addr base, SizeT size ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3381 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3382 | Addr bad_addr; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3383 | Bool ok = is_mem_addressable ( base, size, &bad_addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3384 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3385 | if (!ok) { |
| 3386 | switch (part) { |
| 3387 | case Vg_CoreSysCall: |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 3388 | MC_(record_memparam_error) ( tid, bad_addr, |
| 3389 | /*isAddrErr*/True, s, 0/*otag*/ ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3390 | break; |
| 3391 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3392 | case Vg_CoreSignal: |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 3393 | MC_(record_core_mem_error)( tid, /*isAddrErr*/True, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3394 | break; |
| 3395 | |
| 3396 | default: |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3397 | VG_(tool_panic)("check_mem_is_addressable: unexpected CorePart"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3398 | } |
| 3399 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | static |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3403 | void check_mem_is_defined ( CorePart part, ThreadId tid, Char* s, |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 3404 | Addr base, SizeT size ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3405 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3406 | UInt otag = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3407 | Addr bad_addr; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3408 | MC_ReadResult res = is_mem_defined ( base, size, &bad_addr, &otag ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3409 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3410 | if (MC_Ok != res) { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 3411 | Bool isAddrErr = ( MC_AddrErr == res ? True : False ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3412 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3413 | switch (part) { |
| 3414 | case Vg_CoreSysCall: |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 3415 | MC_(record_memparam_error) ( tid, bad_addr, isAddrErr, s, |
| 3416 | isAddrErr ? 0 : otag ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3417 | break; |
| 3418 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3419 | /* If we're being asked to jump to a silly address, record an error |
| 3420 | message before potentially crashing the entire system. */ |
| 3421 | case Vg_CoreTranslate: |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 3422 | MC_(record_jump_error)( tid, bad_addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3423 | break; |
| 3424 | |
| 3425 | default: |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3426 | VG_(tool_panic)("check_mem_is_defined: unexpected CorePart"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3427 | } |
| 3428 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | static |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3432 | void check_mem_is_defined_asciiz ( CorePart part, ThreadId tid, |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 3433 | Char* s, Addr str ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3434 | { |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3435 | MC_ReadResult res; |
njn | 5ab96ac | 2005-05-08 02:59:50 +0000 | [diff] [blame] | 3436 | Addr bad_addr = 0; // shut GCC up |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3437 | UInt otag = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3438 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 3439 | tl_assert(part == Vg_CoreSysCall); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3440 | res = mc_is_defined_asciiz ( (Addr)str, &bad_addr, &otag ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3441 | if (MC_Ok != res) { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 3442 | Bool isAddrErr = ( MC_AddrErr == res ? True : False ); |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 3443 | MC_(record_memparam_error) ( tid, bad_addr, isAddrErr, s, |
| 3444 | isAddrErr ? 0 : otag ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3445 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3448 | static |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 3449 | void mc_new_mem_startup( Addr a, SizeT len, Bool rr, Bool ww, Bool xx ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3450 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3451 | /* Ignore the permissions, just make it defined. Seems to work... */ |
njn | ba7b458 | 2006-09-21 15:59:30 +0000 | [diff] [blame] | 3452 | // Because code is defined, initialised variables get put in the data |
| 3453 | // segment and are defined, and uninitialised variables get put in the |
| 3454 | // bss segment and are auto-zeroed (and so defined). |
| 3455 | // |
| 3456 | // It's possible that there will be padding between global variables. |
| 3457 | // This will also be auto-zeroed, and marked as defined by Memcheck. If |
| 3458 | // a program uses it, Memcheck will not complain. This is arguably a |
| 3459 | // false negative, but it's a grey area -- the behaviour is defined (the |
| 3460 | // padding is zeroed) but it's probably not what the user intended. And |
| 3461 | // we can't avoid it. |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 3462 | DEBUG("mc_new_mem_startup(%p, %llu, rr=%u, ww=%u, xx=%u)\n", |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3463 | a, (ULong)len, rr, ww, xx); |
| 3464 | MC_(make_mem_defined)(a, len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | static |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 3468 | void mc_new_mem_mmap ( Addr a, SizeT len, Bool rr, Bool ww, Bool xx ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3469 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3470 | MC_(make_mem_defined)(a, len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 3473 | static |
| 3474 | void mc_post_mem_write(CorePart part, ThreadId tid, Addr a, SizeT len) |
| 3475 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3476 | MC_(make_mem_defined)(a, len); |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 3477 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3478 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3479 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3480 | /*------------------------------------------------------------*/ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3481 | /*--- Register event handlers ---*/ |
| 3482 | /*------------------------------------------------------------*/ |
| 3483 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3484 | /* Try and get a nonzero origin for the guest state section of thread |
| 3485 | tid characterised by (offset,size). Return 0 if nothing to show |
| 3486 | for it. */ |
| 3487 | static UInt mb_get_origin_for_guest_offset ( ThreadId tid, |
| 3488 | Int offset, SizeT size ) |
| 3489 | { |
| 3490 | Int sh2off; |
| 3491 | UChar area[6]; |
| 3492 | UInt otag; |
| 3493 | sh2off = MC_(get_otrack_shadow_offset)( offset, size ); |
| 3494 | if (sh2off == -1) |
| 3495 | return 0; /* This piece of guest state is not tracked */ |
| 3496 | tl_assert(sh2off >= 0); |
| 3497 | tl_assert(0 == (sh2off % 4)); |
| 3498 | area[0] = 0x31; |
| 3499 | area[5] = 0x27; |
| 3500 | VG_(get_shadow_regs_area)( tid, &area[1], 2/*shadowno*/,sh2off,4 ); |
| 3501 | tl_assert(area[0] == 0x31); |
| 3502 | tl_assert(area[5] == 0x27); |
| 3503 | otag = *(UInt*)&area[1]; |
| 3504 | return otag; |
| 3505 | } |
| 3506 | |
| 3507 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3508 | /* When some chunk of guest state is written, mark the corresponding |
| 3509 | shadow area as valid. This is used to initialise arbitrarily large |
sewardj | 62eae5f | 2006-01-17 01:58:24 +0000 | [diff] [blame] | 3510 | chunks of guest state, hence the _SIZE value, which has to be as |
| 3511 | big as the biggest guest state. |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3512 | */ |
| 3513 | static void mc_post_reg_write ( CorePart part, ThreadId tid, |
| 3514 | OffT offset, SizeT size) |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3515 | { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 3516 | # define MAX_REG_WRITE_SIZE 1408 |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 3517 | UChar area[MAX_REG_WRITE_SIZE]; |
| 3518 | tl_assert(size <= MAX_REG_WRITE_SIZE); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3519 | VG_(memset)(area, V_BITS8_DEFINED, size); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3520 | VG_(set_shadow_regs_area)( tid, 1/*shadowNo*/,offset,size, area ); |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 3521 | # undef MAX_REG_WRITE_SIZE |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3524 | static |
| 3525 | void mc_post_reg_write_clientcall ( ThreadId tid, |
| 3526 | OffT offset, SizeT size, |
| 3527 | Addr f) |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3528 | { |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 3529 | mc_post_reg_write(/*dummy*/0, tid, offset, size); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3532 | /* Look at the definedness of the guest's shadow state for |
| 3533 | [offset, offset+len). If any part of that is undefined, record |
| 3534 | a parameter error. |
| 3535 | */ |
| 3536 | static void mc_pre_reg_read ( CorePart part, ThreadId tid, Char* s, |
| 3537 | OffT offset, SizeT size) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3538 | { |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3539 | Int i; |
| 3540 | Bool bad; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3541 | UInt otag; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3542 | |
| 3543 | UChar area[16]; |
| 3544 | tl_assert(size <= 16); |
| 3545 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3546 | VG_(get_shadow_regs_area)( tid, area, 1/*shadowNo*/,offset,size ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3547 | |
| 3548 | bad = False; |
| 3549 | for (i = 0; i < size; i++) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3550 | if (area[i] != V_BITS8_DEFINED) { |
sewardj | 2c27f70 | 2005-05-03 18:19:05 +0000 | [diff] [blame] | 3551 | bad = True; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3552 | break; |
| 3553 | } |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3556 | if (!bad) |
| 3557 | return; |
| 3558 | |
| 3559 | /* We've found some undefinedness. See if we can also find an |
| 3560 | origin for it. */ |
| 3561 | otag = mb_get_origin_for_guest_offset( tid, offset, size ); |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 3562 | MC_(record_regparam_error) ( tid, s, otag ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3563 | } |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3564 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3565 | |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3566 | /*------------------------------------------------------------*/ |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3567 | /*--- Functions called directly from generated code: ---*/ |
| 3568 | /*--- Load/store handlers. ---*/ |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3569 | /*------------------------------------------------------------*/ |
| 3570 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3571 | /* Types: LOADV32, LOADV16, LOADV8 are: |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3572 | UWord fn ( Addr a ) |
| 3573 | so they return 32-bits on 32-bit machines and 64-bits on |
| 3574 | 64-bit machines. Addr has the same size as a host word. |
| 3575 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3576 | LOADV64 is always ULong fn ( Addr a ) |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3577 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3578 | Similarly for STOREV8, STOREV16, STOREV32, the supplied vbits |
| 3579 | are a UWord, and for STOREV64 they are a ULong. |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3580 | */ |
| 3581 | |
sewardj | 7244e71 | 2008-05-02 12:35:48 +0000 | [diff] [blame] | 3582 | /* If any part of '_a' indicated by the mask is 1, either '_a' is not |
| 3583 | naturally '_sz/8'-aligned, or it exceeds the range covered by the |
| 3584 | primary map. This is all very tricky (and important!), so let's |
| 3585 | work through the maths by hand (below), *and* assert for these |
| 3586 | values at startup. */ |
| 3587 | #define MASK(_szInBytes) \ |
| 3588 | ( ~((0x10000UL-(_szInBytes)) | ((N_PRIMARY_MAP-1) << 16)) ) |
| 3589 | |
| 3590 | /* MASK only exists so as to define this macro. */ |
| 3591 | #define UNALIGNED_OR_HIGH(_a,_szInBits) \ |
| 3592 | ((_a) & MASK((_szInBits>>3))) |
| 3593 | |
| 3594 | /* On a 32-bit machine: |
| 3595 | |
| 3596 | N_PRIMARY_BITS == 16, so |
| 3597 | N_PRIMARY_MAP == 0x10000, so |
| 3598 | N_PRIMARY_MAP-1 == 0xFFFF, so |
| 3599 | (N_PRIMARY_MAP-1) << 16 == 0xFFFF0000, and so |
| 3600 | |
| 3601 | MASK(1) = ~ ( (0x10000 - 1) | 0xFFFF0000 ) |
| 3602 | = ~ ( 0xFFFF | 0xFFFF0000 ) |
| 3603 | = ~ 0xFFFF'FFFF |
| 3604 | = 0 |
| 3605 | |
| 3606 | MASK(2) = ~ ( (0x10000 - 2) | 0xFFFF0000 ) |
| 3607 | = ~ ( 0xFFFE | 0xFFFF0000 ) |
| 3608 | = ~ 0xFFFF'FFFE |
| 3609 | = 1 |
| 3610 | |
| 3611 | MASK(4) = ~ ( (0x10000 - 4) | 0xFFFF0000 ) |
| 3612 | = ~ ( 0xFFFC | 0xFFFF0000 ) |
| 3613 | = ~ 0xFFFF'FFFC |
| 3614 | = 3 |
| 3615 | |
| 3616 | MASK(8) = ~ ( (0x10000 - 8) | 0xFFFF0000 ) |
| 3617 | = ~ ( 0xFFF8 | 0xFFFF0000 ) |
| 3618 | = ~ 0xFFFF'FFF8 |
| 3619 | = 7 |
| 3620 | |
| 3621 | Hence in the 32-bit case, "a & MASK(1/2/4/8)" is a nonzero value |
| 3622 | precisely when a is not 1/2/4/8-bytes aligned. And obviously, for |
| 3623 | the 1-byte alignment case, it is always a zero value, since MASK(1) |
| 3624 | is zero. All as expected. |
| 3625 | |
| 3626 | On a 64-bit machine, it's more complex, since we're testing |
| 3627 | simultaneously for misalignment and for the address being at or |
| 3628 | above 32G: |
| 3629 | |
| 3630 | N_PRIMARY_BITS == 19, so |
| 3631 | N_PRIMARY_MAP == 0x80000, so |
| 3632 | N_PRIMARY_MAP-1 == 0x7FFFF, so |
| 3633 | (N_PRIMARY_MAP-1) << 16 == 0x7FFFF'0000, and so |
| 3634 | |
| 3635 | MASK(1) = ~ ( (0x10000 - 1) | 0x7FFFF'0000 ) |
| 3636 | = ~ ( 0xFFFF | 0x7FFFF'0000 ) |
| 3637 | = ~ 0x7FFFF'FFFF |
| 3638 | = 0xFFFF'FFF8'0000'0000 |
| 3639 | |
| 3640 | MASK(2) = ~ ( (0x10000 - 2) | 0x7FFFF'0000 ) |
| 3641 | = ~ ( 0xFFFE | 0x7FFFF'0000 ) |
| 3642 | = ~ 0x7FFFF'FFFE |
| 3643 | = 0xFFFF'FFF8'0000'0001 |
| 3644 | |
| 3645 | MASK(4) = ~ ( (0x10000 - 4) | 0x7FFFF'0000 ) |
| 3646 | = ~ ( 0xFFFC | 0x7FFFF'0000 ) |
| 3647 | = ~ 0x7FFFF'FFFC |
| 3648 | = 0xFFFF'FFF8'0000'0003 |
| 3649 | |
| 3650 | MASK(8) = ~ ( (0x10000 - 8) | 0x7FFFF'0000 ) |
| 3651 | = ~ ( 0xFFF8 | 0x7FFFF'0000 ) |
| 3652 | = ~ 0x7FFFF'FFF8 |
| 3653 | = 0xFFFF'FFF8'0000'0007 |
| 3654 | */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3655 | |
| 3656 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3657 | /* ------------------------ Size = 8 ------------------------ */ |
| 3658 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3659 | static INLINE |
| 3660 | ULong mc_LOADV64 ( Addr a, Bool isBigEndian ) |
| 3661 | { |
| 3662 | UWord sm_off16, vabits16; |
| 3663 | SecMap* sm; |
| 3664 | |
| 3665 | PROF_EVENT(200, "mc_LOADV64"); |
| 3666 | |
| 3667 | #ifndef PERF_FAST_LOADV |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3668 | return mc_LOADVn_slow( a, 64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3669 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3670 | if (UNLIKELY( UNALIGNED_OR_HIGH(a,64) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3671 | PROF_EVENT(201, "mc_LOADV64-slow1"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3672 | return (ULong)mc_LOADVn_slow( a, 64, isBigEndian ); |
sewardj | f9d8161 | 2005-04-23 23:25:49 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3675 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3676 | sm_off16 = SM_OFF_16(a); |
| 3677 | vabits16 = ((UShort*)(sm->vabits8))[sm_off16]; |
| 3678 | |
| 3679 | // Handle common case quickly: a is suitably aligned, is mapped, and |
| 3680 | // addressible. |
| 3681 | // Convert V bits from compact memory form to expanded register form. |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3682 | if (LIKELY(vabits16 == VA_BITS16_DEFINED)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3683 | return V_BITS64_DEFINED; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3684 | } else if (LIKELY(vabits16 == VA_BITS16_UNDEFINED)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3685 | return V_BITS64_UNDEFINED; |
| 3686 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3687 | /* Slow case: the 8 bytes are not all-defined or all-undefined. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3688 | PROF_EVENT(202, "mc_LOADV64-slow2"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3689 | return mc_LOADVn_slow( a, 64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3690 | } |
| 3691 | #endif |
| 3692 | } |
| 3693 | |
| 3694 | VG_REGPARM(1) ULong MC_(helperc_LOADV64be) ( Addr a ) |
| 3695 | { |
| 3696 | return mc_LOADV64(a, True); |
| 3697 | } |
| 3698 | VG_REGPARM(1) ULong MC_(helperc_LOADV64le) ( Addr a ) |
| 3699 | { |
| 3700 | return mc_LOADV64(a, False); |
| 3701 | } |
sewardj | f9d8161 | 2005-04-23 23:25:49 +0000 | [diff] [blame] | 3702 | |
sewardj | f9d8161 | 2005-04-23 23:25:49 +0000 | [diff] [blame] | 3703 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3704 | static INLINE |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3705 | void mc_STOREV64 ( Addr a, ULong vbits64, Bool isBigEndian ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3706 | { |
| 3707 | UWord sm_off16, vabits16; |
| 3708 | SecMap* sm; |
| 3709 | |
| 3710 | PROF_EVENT(210, "mc_STOREV64"); |
| 3711 | |
| 3712 | #ifndef PERF_FAST_STOREV |
| 3713 | // XXX: this slow case seems to be marginally faster than the fast case! |
| 3714 | // Investigate further. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3715 | mc_STOREVn_slow( a, 64, vbits64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3716 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3717 | if (UNLIKELY( UNALIGNED_OR_HIGH(a,64) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3718 | PROF_EVENT(211, "mc_STOREV64-slow1"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3719 | mc_STOREVn_slow( a, 64, vbits64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3720 | return; |
sewardj | f9d8161 | 2005-04-23 23:25:49 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3723 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3724 | sm_off16 = SM_OFF_16(a); |
| 3725 | vabits16 = ((UShort*)(sm->vabits8))[sm_off16]; |
| 3726 | |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3727 | if (LIKELY( !is_distinguished_sm(sm) && |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3728 | (VA_BITS16_DEFINED == vabits16 || |
| 3729 | VA_BITS16_UNDEFINED == vabits16) )) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3730 | { |
| 3731 | /* Handle common case quickly: a is suitably aligned, */ |
| 3732 | /* is mapped, and is addressible. */ |
| 3733 | // Convert full V-bits in register to compact 2-bit form. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3734 | if (V_BITS64_DEFINED == vbits64) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3735 | ((UShort*)(sm->vabits8))[sm_off16] = (UShort)VA_BITS16_DEFINED; |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3736 | } else if (V_BITS64_UNDEFINED == vbits64) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3737 | ((UShort*)(sm->vabits8))[sm_off16] = (UShort)VA_BITS16_UNDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3738 | } else { |
| 3739 | /* Slow but general case -- writing partially defined bytes. */ |
| 3740 | PROF_EVENT(212, "mc_STOREV64-slow2"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3741 | mc_STOREVn_slow( a, 64, vbits64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3742 | } |
| 3743 | } else { |
| 3744 | /* Slow but general case. */ |
| 3745 | PROF_EVENT(213, "mc_STOREV64-slow3"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3746 | mc_STOREVn_slow( a, 64, vbits64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3747 | } |
| 3748 | #endif |
| 3749 | } |
| 3750 | |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3751 | VG_REGPARM(1) void MC_(helperc_STOREV64be) ( Addr a, ULong vbits64 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3752 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3753 | mc_STOREV64(a, vbits64, True); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3754 | } |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3755 | VG_REGPARM(1) void MC_(helperc_STOREV64le) ( Addr a, ULong vbits64 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3756 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3757 | mc_STOREV64(a, vbits64, False); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3758 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3759 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3760 | |
| 3761 | /* ------------------------ Size = 4 ------------------------ */ |
| 3762 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3763 | static INLINE |
| 3764 | UWord mc_LOADV32 ( Addr a, Bool isBigEndian ) |
| 3765 | { |
| 3766 | UWord sm_off, vabits8; |
| 3767 | SecMap* sm; |
| 3768 | |
| 3769 | PROF_EVENT(220, "mc_LOADV32"); |
| 3770 | |
| 3771 | #ifndef PERF_FAST_LOADV |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3772 | return (UWord)mc_LOADVn_slow( a, 32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3773 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3774 | if (UNLIKELY( UNALIGNED_OR_HIGH(a,32) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3775 | PROF_EVENT(221, "mc_LOADV32-slow1"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3776 | return (UWord)mc_LOADVn_slow( a, 32, isBigEndian ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3779 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3780 | sm_off = SM_OFF(a); |
| 3781 | vabits8 = sm->vabits8[sm_off]; |
| 3782 | |
| 3783 | // Handle common case quickly: a is suitably aligned, is mapped, and the |
| 3784 | // entire word32 it lives in is addressible. |
| 3785 | // Convert V bits from compact memory form to expanded register form. |
| 3786 | // For 64-bit platforms, set the high 32 bits of retval to 1 (undefined). |
| 3787 | // Almost certainly not necessary, but be paranoid. |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3788 | if (LIKELY(vabits8 == VA_BITS8_DEFINED)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3789 | return ((UWord)0xFFFFFFFF00000000ULL | (UWord)V_BITS32_DEFINED); |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3790 | } else if (LIKELY(vabits8 == VA_BITS8_UNDEFINED)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3791 | return ((UWord)0xFFFFFFFF00000000ULL | (UWord)V_BITS32_UNDEFINED); |
| 3792 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3793 | /* Slow case: the 4 bytes are not all-defined or all-undefined. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3794 | PROF_EVENT(222, "mc_LOADV32-slow2"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3795 | return (UWord)mc_LOADVn_slow( a, 32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3796 | } |
| 3797 | #endif |
| 3798 | } |
| 3799 | |
| 3800 | VG_REGPARM(1) UWord MC_(helperc_LOADV32be) ( Addr a ) |
| 3801 | { |
| 3802 | return mc_LOADV32(a, True); |
| 3803 | } |
| 3804 | VG_REGPARM(1) UWord MC_(helperc_LOADV32le) ( Addr a ) |
| 3805 | { |
| 3806 | return mc_LOADV32(a, False); |
| 3807 | } |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3808 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3809 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3810 | static INLINE |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3811 | void mc_STOREV32 ( Addr a, UWord vbits32, Bool isBigEndian ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3812 | { |
| 3813 | UWord sm_off, vabits8; |
| 3814 | SecMap* sm; |
| 3815 | |
| 3816 | PROF_EVENT(230, "mc_STOREV32"); |
| 3817 | |
| 3818 | #ifndef PERF_FAST_STOREV |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3819 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3820 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3821 | if (UNLIKELY( UNALIGNED_OR_HIGH(a,32) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3822 | PROF_EVENT(231, "mc_STOREV32-slow1"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3823 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3824 | return; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3827 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3828 | sm_off = SM_OFF(a); |
| 3829 | vabits8 = sm->vabits8[sm_off]; |
| 3830 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3831 | // Cleverness: sometimes we don't have to write the shadow memory at |
| 3832 | // all, if we can tell that what we want to write is the same as what is |
sewardj | b6a4ece | 2008-05-03 05:24:57 +0000 | [diff] [blame^] | 3833 | // already there. The 64/16/8 bit cases also have cleverness at this |
| 3834 | // point, but it works a little differently to the code below. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3835 | if (V_BITS32_DEFINED == vbits32) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3836 | if (vabits8 == (UInt)VA_BITS8_DEFINED) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3837 | return; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3838 | } else if (!is_distinguished_sm(sm) && VA_BITS8_UNDEFINED == vabits8) { |
| 3839 | sm->vabits8[sm_off] = (UInt)VA_BITS8_DEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3840 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3841 | // not defined/undefined, or distinguished and changing state |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3842 | PROF_EVENT(232, "mc_STOREV32-slow2"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3843 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3844 | } |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3845 | } else if (V_BITS32_UNDEFINED == vbits32) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3846 | if (vabits8 == (UInt)VA_BITS8_UNDEFINED) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3847 | return; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3848 | } else if (!is_distinguished_sm(sm) && VA_BITS8_DEFINED == vabits8) { |
| 3849 | sm->vabits8[sm_off] = (UInt)VA_BITS8_UNDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3850 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3851 | // not defined/undefined, or distinguished and changing state |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3852 | PROF_EVENT(233, "mc_STOREV32-slow3"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3853 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3854 | } |
| 3855 | } else { |
| 3856 | // Partially defined word |
| 3857 | PROF_EVENT(234, "mc_STOREV32-slow4"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3858 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3859 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3860 | #endif |
| 3861 | } |
| 3862 | |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3863 | VG_REGPARM(2) void MC_(helperc_STOREV32be) ( Addr a, UWord vbits32 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3864 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3865 | mc_STOREV32(a, vbits32, True); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3866 | } |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3867 | VG_REGPARM(2) void MC_(helperc_STOREV32le) ( Addr a, UWord vbits32 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3868 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3869 | mc_STOREV32(a, vbits32, False); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3870 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3871 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3872 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3873 | /* ------------------------ Size = 2 ------------------------ */ |
| 3874 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3875 | static INLINE |
| 3876 | UWord mc_LOADV16 ( Addr a, Bool isBigEndian ) |
| 3877 | { |
| 3878 | UWord sm_off, vabits8; |
| 3879 | SecMap* sm; |
| 3880 | |
| 3881 | PROF_EVENT(240, "mc_LOADV16"); |
| 3882 | |
| 3883 | #ifndef PERF_FAST_LOADV |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3884 | return (UWord)mc_LOADVn_slow( a, 16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3885 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3886 | if (UNLIKELY( UNALIGNED_OR_HIGH(a,16) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3887 | PROF_EVENT(241, "mc_LOADV16-slow1"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3888 | return (UWord)mc_LOADVn_slow( a, 16, isBigEndian ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3891 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3892 | sm_off = SM_OFF(a); |
| 3893 | vabits8 = sm->vabits8[sm_off]; |
| 3894 | // Handle common case quickly: a is suitably aligned, is mapped, and is |
| 3895 | // addressible. |
| 3896 | // Convert V bits from compact memory form to expanded register form |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3897 | if (vabits8 == VA_BITS8_DEFINED ) { return V_BITS16_DEFINED; } |
| 3898 | else if (vabits8 == VA_BITS8_UNDEFINED) { return V_BITS16_UNDEFINED; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3899 | else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3900 | // The 4 (yes, 4) bytes are not all-defined or all-undefined, check |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3901 | // the two sub-bytes. |
| 3902 | UChar vabits4 = extract_vabits4_from_vabits8(a, vabits8); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3903 | if (vabits4 == VA_BITS4_DEFINED ) { return V_BITS16_DEFINED; } |
| 3904 | else if (vabits4 == VA_BITS4_UNDEFINED) { return V_BITS16_UNDEFINED; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3905 | else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3906 | /* Slow case: the two bytes are not all-defined or all-undefined. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3907 | PROF_EVENT(242, "mc_LOADV16-slow2"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3908 | return (UWord)mc_LOADVn_slow( a, 16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3909 | } |
| 3910 | } |
| 3911 | #endif |
| 3912 | } |
| 3913 | |
| 3914 | VG_REGPARM(1) UWord MC_(helperc_LOADV16be) ( Addr a ) |
| 3915 | { |
| 3916 | return mc_LOADV16(a, True); |
| 3917 | } |
| 3918 | VG_REGPARM(1) UWord MC_(helperc_LOADV16le) ( Addr a ) |
| 3919 | { |
| 3920 | return mc_LOADV16(a, False); |
| 3921 | } |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3922 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3923 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3924 | static INLINE |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3925 | void mc_STOREV16 ( Addr a, UWord vbits16, Bool isBigEndian ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3926 | { |
| 3927 | UWord sm_off, vabits8; |
| 3928 | SecMap* sm; |
| 3929 | |
| 3930 | PROF_EVENT(250, "mc_STOREV16"); |
| 3931 | |
| 3932 | #ifndef PERF_FAST_STOREV |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3933 | mc_STOREVn_slow( a, 16, (ULong)vbits16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3934 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3935 | if (UNLIKELY( UNALIGNED_OR_HIGH(a,16) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3936 | PROF_EVENT(251, "mc_STOREV16-slow1"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3937 | mc_STOREVn_slow( a, 16, (ULong)vbits16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3938 | return; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3941 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3942 | sm_off = SM_OFF(a); |
| 3943 | vabits8 = sm->vabits8[sm_off]; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3944 | if (LIKELY( !is_distinguished_sm(sm) && |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3945 | (VA_BITS8_DEFINED == vabits8 || |
| 3946 | VA_BITS8_UNDEFINED == vabits8) )) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3947 | { |
| 3948 | /* Handle common case quickly: a is suitably aligned, */ |
| 3949 | /* is mapped, and is addressible. */ |
| 3950 | // Convert full V-bits in register to compact 2-bit form. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3951 | if (V_BITS16_DEFINED == vbits16) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3952 | insert_vabits4_into_vabits8( a, VA_BITS4_DEFINED , |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3953 | &(sm->vabits8[sm_off]) ); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3954 | } else if (V_BITS16_UNDEFINED == vbits16) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3955 | insert_vabits4_into_vabits8( a, VA_BITS4_UNDEFINED, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3956 | &(sm->vabits8[sm_off]) ); |
| 3957 | } else { |
| 3958 | /* Slow but general case -- writing partially defined bytes. */ |
| 3959 | PROF_EVENT(252, "mc_STOREV16-slow2"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3960 | mc_STOREVn_slow( a, 16, (ULong)vbits16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3961 | } |
| 3962 | } else { |
| 3963 | /* Slow but general case. */ |
| 3964 | PROF_EVENT(253, "mc_STOREV16-slow3"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3965 | mc_STOREVn_slow( a, 16, (ULong)vbits16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3966 | } |
| 3967 | #endif |
| 3968 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3969 | |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3970 | VG_REGPARM(2) void MC_(helperc_STOREV16be) ( Addr a, UWord vbits16 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3971 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3972 | mc_STOREV16(a, vbits16, True); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3973 | } |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3974 | VG_REGPARM(2) void MC_(helperc_STOREV16le) ( Addr a, UWord vbits16 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3975 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3976 | mc_STOREV16(a, vbits16, False); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3977 | } |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 3978 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3979 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3980 | /* ------------------------ Size = 1 ------------------------ */ |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3981 | /* Note: endianness is irrelevant for size == 1 */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3982 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 3983 | VG_REGPARM(1) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3984 | UWord MC_(helperc_LOADV8) ( Addr a ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3985 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3986 | UWord sm_off, vabits8; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 3987 | SecMap* sm; |
| 3988 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3989 | PROF_EVENT(260, "mc_LOADV8"); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3990 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3991 | #ifndef PERF_FAST_LOADV |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3992 | return (UWord)mc_LOADVn_slow( a, 8, False/*irrelevant*/ ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3993 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 3994 | if (UNLIKELY( UNALIGNED_OR_HIGH(a,8) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3995 | PROF_EVENT(261, "mc_LOADV8-slow1"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3996 | return (UWord)mc_LOADVn_slow( a, 8, False/*irrelevant*/ ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3999 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4000 | sm_off = SM_OFF(a); |
| 4001 | vabits8 = sm->vabits8[sm_off]; |
| 4002 | // Convert V bits from compact memory form to expanded register form |
| 4003 | // Handle common case quickly: a is mapped, and the entire |
| 4004 | // word32 it lives in is addressible. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4005 | if (vabits8 == VA_BITS8_DEFINED ) { return V_BITS8_DEFINED; } |
| 4006 | else if (vabits8 == VA_BITS8_UNDEFINED) { return V_BITS8_UNDEFINED; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4007 | else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4008 | // The 4 (yes, 4) bytes are not all-defined or all-undefined, check |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4009 | // the single byte. |
| 4010 | UChar vabits2 = extract_vabits2_from_vabits8(a, vabits8); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4011 | if (vabits2 == VA_BITS2_DEFINED ) { return V_BITS8_DEFINED; } |
| 4012 | else if (vabits2 == VA_BITS2_UNDEFINED) { return V_BITS8_UNDEFINED; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4013 | else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4014 | /* Slow case: the byte is not all-defined or all-undefined. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4015 | PROF_EVENT(262, "mc_LOADV8-slow2"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 4016 | return (UWord)mc_LOADVn_slow( a, 8, False/*irrelevant*/ ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4017 | } |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 4018 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4019 | #endif |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4020 | } |
| 4021 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 4022 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 4023 | VG_REGPARM(2) |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 4024 | void MC_(helperc_STOREV8) ( Addr a, UWord vbits8 ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4025 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4026 | UWord sm_off, vabits8; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 4027 | SecMap* sm; |
| 4028 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4029 | PROF_EVENT(270, "mc_STOREV8"); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 4030 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4031 | #ifndef PERF_FAST_STOREV |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 4032 | mc_STOREVn_slow( a, 8, (ULong)vbits8, False/*irrelevant*/ ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4033 | #else |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 4034 | if (UNLIKELY( UNALIGNED_OR_HIGH(a,8) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4035 | PROF_EVENT(271, "mc_STOREV8-slow1"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 4036 | mc_STOREVn_slow( a, 8, (ULong)vbits8, False/*irrelevant*/ ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 4037 | return; |
| 4038 | } |
| 4039 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 4040 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4041 | sm_off = SM_OFF(a); |
| 4042 | vabits8 = sm->vabits8[sm_off]; |
bart | 5dd8e6a | 2008-03-22 08:04:29 +0000 | [diff] [blame] | 4043 | if (LIKELY |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4044 | ( !is_distinguished_sm(sm) && |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4045 | ( (VA_BITS8_DEFINED == vabits8 || VA_BITS8_UNDEFINED == vabits8) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4046 | || (VA_BITS2_NOACCESS != extract_vabits2_from_vabits8(a, vabits8)) |
| 4047 | ) |
| 4048 | ) |
| 4049 | ) |
| 4050 | { |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 4051 | /* Handle common case quickly: a is mapped, the entire word32 it |
| 4052 | lives in is addressible. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4053 | // Convert full V-bits in register to compact 2-bit form. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 4054 | if (V_BITS8_DEFINED == vbits8) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4055 | insert_vabits2_into_vabits8( a, VA_BITS2_DEFINED, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4056 | &(sm->vabits8[sm_off]) ); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 4057 | } else if (V_BITS8_UNDEFINED == vbits8) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4058 | insert_vabits2_into_vabits8( a, VA_BITS2_UNDEFINED, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4059 | &(sm->vabits8[sm_off]) ); |
| 4060 | } else { |
| 4061 | /* Slow but general case -- writing partially defined bytes. */ |
| 4062 | PROF_EVENT(272, "mc_STOREV8-slow2"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 4063 | mc_STOREVn_slow( a, 8, (ULong)vbits8, False/*irrelevant*/ ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4064 | } |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 4065 | } else { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4066 | /* Slow but general case. */ |
| 4067 | PROF_EVENT(273, "mc_STOREV8-slow3"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 4068 | mc_STOREVn_slow( a, 8, (ULong)vbits8, False/*irrelevant*/ ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 4069 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4070 | #endif |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 4074 | /*------------------------------------------------------------*/ |
| 4075 | /*--- Functions called directly from generated code: ---*/ |
| 4076 | /*--- Value-check failure handlers. ---*/ |
| 4077 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4078 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4079 | /* Call these ones when an origin is available ... */ |
| 4080 | VG_REGPARM(1) |
| 4081 | void MC_(helperc_value_check0_fail_w_o) ( UWord origin ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4082 | MC_(record_cond_error) ( VG_(get_running_tid)(), (UInt)origin ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4085 | VG_REGPARM(1) |
| 4086 | void MC_(helperc_value_check1_fail_w_o) ( UWord origin ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4087 | MC_(record_value_error) ( VG_(get_running_tid)(), 1, (UInt)origin ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4090 | VG_REGPARM(1) |
| 4091 | void MC_(helperc_value_check4_fail_w_o) ( UWord origin ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4092 | MC_(record_value_error) ( VG_(get_running_tid)(), 4, (UInt)origin ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4095 | VG_REGPARM(1) |
| 4096 | void MC_(helperc_value_check8_fail_w_o) ( UWord origin ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4097 | MC_(record_value_error) ( VG_(get_running_tid)(), 8, (UInt)origin ); |
sewardj | 11bcc4e | 2005-04-23 22:38:38 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4100 | VG_REGPARM(2) |
| 4101 | void MC_(helperc_value_checkN_fail_w_o) ( HWord sz, UWord origin ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4102 | MC_(record_value_error) ( VG_(get_running_tid)(), (Int)sz, (UInt)origin ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | /* ... and these when an origin isn't available. */ |
| 4106 | |
| 4107 | VG_REGPARM(0) |
| 4108 | void MC_(helperc_value_check0_fail_no_o) ( void ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4109 | MC_(record_cond_error) ( VG_(get_running_tid)(), 0/*origin*/ ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | VG_REGPARM(0) |
| 4113 | void MC_(helperc_value_check1_fail_no_o) ( void ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4114 | MC_(record_value_error) ( VG_(get_running_tid)(), 1, 0/*origin*/ ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | VG_REGPARM(0) |
| 4118 | void MC_(helperc_value_check4_fail_no_o) ( void ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4119 | MC_(record_value_error) ( VG_(get_running_tid)(), 4, 0/*origin*/ ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | VG_REGPARM(0) |
| 4123 | void MC_(helperc_value_check8_fail_no_o) ( void ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4124 | MC_(record_value_error) ( VG_(get_running_tid)(), 8, 0/*origin*/ ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | VG_REGPARM(1) |
| 4128 | void MC_(helperc_value_checkN_fail_no_o) ( HWord sz ) { |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4129 | MC_(record_value_error) ( VG_(get_running_tid)(), (Int)sz, 0/*origin*/ ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4132 | |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4133 | /*------------------------------------------------------------*/ |
| 4134 | /*--- Metadata get/set functions, for client requests. ---*/ |
| 4135 | /*------------------------------------------------------------*/ |
| 4136 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4137 | // Nb: this expands the V+A bits out into register-form V bits, even though |
| 4138 | // they're in memory. This is for backward compatibility, and because it's |
| 4139 | // probably what the user wants. |
| 4140 | |
| 4141 | /* Copy Vbits from/to address 'a'. Returns: 1 == OK, 2 == alignment |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4142 | error [no longer used], 3 == addressing error. */ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 4143 | /* Nb: We used to issue various definedness/addressability errors from here, |
| 4144 | but we took them out because they ranged from not-very-helpful to |
| 4145 | downright annoying, and they complicated the error data structures. */ |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4146 | static Int mc_get_or_set_vbits_for_client ( |
| 4147 | ThreadId tid, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4148 | Addr a, |
| 4149 | Addr vbits, |
| 4150 | SizeT szB, |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4151 | Bool setting /* True <=> set vbits, False <=> get vbits */ |
| 4152 | ) |
| 4153 | { |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4154 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4155 | Bool ok; |
| 4156 | UChar vbits8; |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4157 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4158 | /* Check that arrays are addressible before doing any getting/setting. */ |
| 4159 | for (i = 0; i < szB; i++) { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 4160 | if (VA_BITS2_NOACCESS == get_vabits2(a + i) || |
| 4161 | VA_BITS2_NOACCESS == get_vabits2(vbits + i)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4162 | return 3; |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4163 | } |
| 4164 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4165 | |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4166 | /* Do the copy */ |
| 4167 | if (setting) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4168 | /* setting */ |
| 4169 | for (i = 0; i < szB; i++) { |
| 4170 | ok = set_vbits8(a + i, ((UChar*)vbits)[i]); |
| 4171 | tl_assert(ok); |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4172 | } |
| 4173 | } else { |
| 4174 | /* getting */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4175 | for (i = 0; i < szB; i++) { |
| 4176 | ok = get_vbits8(a + i, &vbits8); |
| 4177 | tl_assert(ok); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4178 | ((UChar*)vbits)[i] = vbits8; |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4179 | } |
| 4180 | // The bytes in vbits[] have now been set, so mark them as such. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4181 | MC_(make_mem_defined)(vbits, szB); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4182 | } |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4183 | |
| 4184 | return 1; |
| 4185 | } |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 4186 | |
| 4187 | |
| 4188 | /*------------------------------------------------------------*/ |
| 4189 | /*--- Detecting leaked (unreachable) malloc'd blocks. ---*/ |
| 4190 | /*------------------------------------------------------------*/ |
| 4191 | |
| 4192 | /* For the memory leak detector, say whether an entire 64k chunk of |
| 4193 | address space is possibly in use, or not. If in doubt return |
| 4194 | True. |
| 4195 | */ |
| 4196 | static |
| 4197 | Bool mc_is_within_valid_secondary ( Addr a ) |
| 4198 | { |
| 4199 | SecMap* sm = maybe_get_secmap_for ( a ); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4200 | if (sm == NULL || sm == &sm_distinguished[SM_DIST_NOACCESS] |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4201 | || MC_(in_ignored_range)(a)) { |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 4202 | /* Definitely not in use. */ |
| 4203 | return False; |
| 4204 | } else { |
| 4205 | return True; |
| 4206 | } |
| 4207 | } |
| 4208 | |
| 4209 | |
| 4210 | /* For the memory leak detector, say whether or not a given word |
| 4211 | address is to be regarded as valid. */ |
| 4212 | static |
| 4213 | Bool mc_is_valid_aligned_word ( Addr a ) |
| 4214 | { |
| 4215 | tl_assert(sizeof(UWord) == 4 || sizeof(UWord) == 8); |
| 4216 | if (sizeof(UWord) == 4) { |
| 4217 | tl_assert(VG_IS_4_ALIGNED(a)); |
| 4218 | } else { |
| 4219 | tl_assert(VG_IS_8_ALIGNED(a)); |
| 4220 | } |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4221 | if (is_mem_defined( a, sizeof(UWord), NULL, NULL) == MC_Ok |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4222 | && !MC_(in_ignored_range)(a)) { |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 4223 | return True; |
| 4224 | } else { |
| 4225 | return False; |
| 4226 | } |
| 4227 | } |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 4228 | |
| 4229 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 4230 | /* Leak detector for this tool. We don't actually do anything, merely |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 4231 | run the generic leak detector with suitable parameters for this |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 4232 | tool. */ |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 4233 | static void mc_detect_memory_leaks ( ThreadId tid, LeakCheckMode mode ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4234 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4235 | MC_(do_detect_memory_leaks) ( |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 4236 | tid, |
| 4237 | mode, |
| 4238 | mc_is_within_valid_secondary, |
| 4239 | mc_is_valid_aligned_word |
| 4240 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 4244 | /*------------------------------------------------------------*/ |
| 4245 | /*--- Initialisation ---*/ |
| 4246 | /*------------------------------------------------------------*/ |
| 4247 | |
| 4248 | static void init_shadow_memory ( void ) |
| 4249 | { |
| 4250 | Int i; |
| 4251 | SecMap* sm; |
| 4252 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4253 | tl_assert(V_BIT_UNDEFINED == 1); |
| 4254 | tl_assert(V_BIT_DEFINED == 0); |
| 4255 | tl_assert(V_BITS8_UNDEFINED == 0xFF); |
| 4256 | tl_assert(V_BITS8_DEFINED == 0); |
| 4257 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 4258 | /* Build the 3 distinguished secondaries */ |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 4259 | sm = &sm_distinguished[SM_DIST_NOACCESS]; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4260 | for (i = 0; i < SM_CHUNKS; i++) sm->vabits8[i] = VA_BITS8_NOACCESS; |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 4261 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4262 | sm = &sm_distinguished[SM_DIST_UNDEFINED]; |
| 4263 | for (i = 0; i < SM_CHUNKS; i++) sm->vabits8[i] = VA_BITS8_UNDEFINED; |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 4264 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4265 | sm = &sm_distinguished[SM_DIST_DEFINED]; |
| 4266 | for (i = 0; i < SM_CHUNKS; i++) sm->vabits8[i] = VA_BITS8_DEFINED; |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 4267 | |
| 4268 | /* Set up the primary map. */ |
| 4269 | /* These entries gradually get overwritten as the used address |
| 4270 | space expands. */ |
| 4271 | for (i = 0; i < N_PRIMARY_MAP; i++) |
| 4272 | primary_map[i] = &sm_distinguished[SM_DIST_NOACCESS]; |
| 4273 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4274 | /* Auxiliary primary maps */ |
| 4275 | init_auxmap_L1_L2(); |
| 4276 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 4277 | /* auxmap_size = auxmap_used = 0; |
| 4278 | no ... these are statically initialised */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4279 | |
| 4280 | /* Secondary V bit table */ |
| 4281 | secVBitTable = createSecVBitTable(); |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | |
| 4285 | /*------------------------------------------------------------*/ |
| 4286 | /*--- Sanity check machinery (permanently engaged) ---*/ |
| 4287 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4288 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4289 | static Bool mc_cheap_sanity_check ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4290 | { |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4291 | n_sanity_cheap++; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 4292 | PROF_EVENT(490, "cheap_sanity_check"); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4293 | /* Check for sane operating level */ |
| 4294 | if (MC_(clo_mc_level) < 1 || MC_(clo_mc_level) > 3) |
| 4295 | return False; |
| 4296 | /* nothing else useful we can rapidly check */ |
jseward | 9800fd3 | 2004-01-04 23:08:04 +0000 | [diff] [blame] | 4297 | return True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4300 | static Bool mc_expensive_sanity_check ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4301 | { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4302 | Int i; |
| 4303 | Word n_secmaps_found; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4304 | SecMap* sm; |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4305 | HChar* errmsg; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4306 | Bool bad = False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4307 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4308 | if (0) VG_(printf)("expensive sanity check\n"); |
| 4309 | if (0) return True; |
| 4310 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4311 | n_sanity_expensive++; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 4312 | PROF_EVENT(491, "expensive_sanity_check"); |
| 4313 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4314 | /* Check for sane operating level */ |
| 4315 | if (MC_(clo_mc_level) < 1 || MC_(clo_mc_level) > 3) |
| 4316 | return False; |
| 4317 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4318 | /* Check that the 3 distinguished SMs are still as they should be. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4319 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4320 | /* Check noaccess DSM. */ |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4321 | sm = &sm_distinguished[SM_DIST_NOACCESS]; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4322 | for (i = 0; i < SM_CHUNKS; i++) |
| 4323 | if (sm->vabits8[i] != VA_BITS8_NOACCESS) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4324 | bad = True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4325 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4326 | /* Check undefined DSM. */ |
| 4327 | sm = &sm_distinguished[SM_DIST_UNDEFINED]; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4328 | for (i = 0; i < SM_CHUNKS; i++) |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4329 | if (sm->vabits8[i] != VA_BITS8_UNDEFINED) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4330 | bad = True; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4331 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4332 | /* Check defined DSM. */ |
| 4333 | sm = &sm_distinguished[SM_DIST_DEFINED]; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4334 | for (i = 0; i < SM_CHUNKS; i++) |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4335 | if (sm->vabits8[i] != VA_BITS8_DEFINED) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4336 | bad = True; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4337 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4338 | if (bad) { |
| 4339 | VG_(printf)("memcheck expensive sanity: " |
| 4340 | "distinguished_secondaries have changed\n"); |
| 4341 | return False; |
| 4342 | } |
| 4343 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4344 | /* If we're not checking for undefined value errors, the secondary V bit |
| 4345 | * table should be empty. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4346 | if (MC_(clo_mc_level) == 1) { |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 4347 | if (0 != VG_(OSetGen_Size)(secVBitTable)) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4348 | return False; |
| 4349 | } |
| 4350 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4351 | /* check the auxiliary maps, very thoroughly */ |
| 4352 | n_secmaps_found = 0; |
| 4353 | errmsg = check_auxmap_L1_L2_sanity( &n_secmaps_found ); |
| 4354 | if (errmsg) { |
| 4355 | VG_(printf)("memcheck expensive sanity, auxmaps:\n\t%s", errmsg); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4356 | return False; |
| 4357 | } |
| 4358 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4359 | /* n_secmaps_found is now the number referred to by the auxiliary |
| 4360 | primary map. Now add on the ones referred to by the main |
| 4361 | primary map. */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4362 | for (i = 0; i < N_PRIMARY_MAP; i++) { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4363 | if (primary_map[i] == NULL) { |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4364 | bad = True; |
| 4365 | } else { |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4366 | if (!is_distinguished_sm(primary_map[i])) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4367 | n_secmaps_found++; |
| 4368 | } |
| 4369 | } |
| 4370 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4371 | /* check that the number of secmaps issued matches the number that |
| 4372 | are reachable (iow, no secmap leaks) */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4373 | if (n_secmaps_found != (n_issued_SMs - n_deissued_SMs)) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4374 | bad = True; |
| 4375 | |
| 4376 | if (bad) { |
| 4377 | VG_(printf)("memcheck expensive sanity: " |
| 4378 | "apparent secmap leakage\n"); |
| 4379 | return False; |
| 4380 | } |
| 4381 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4382 | if (bad) { |
| 4383 | VG_(printf)("memcheck expensive sanity: " |
| 4384 | "auxmap covers wrong address space\n"); |
| 4385 | return False; |
| 4386 | } |
| 4387 | |
| 4388 | /* there is only one pointer to each secmap (expensive) */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4389 | |
| 4390 | return True; |
| 4391 | } |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4392 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4393 | /*------------------------------------------------------------*/ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 4394 | /*--- Command line args ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4395 | /*------------------------------------------------------------*/ |
| 4396 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4397 | Bool MC_(clo_partial_loads_ok) = False; |
sewardj | fa4ca3b | 2007-11-30 17:19:36 +0000 | [diff] [blame] | 4398 | Long MC_(clo_freelist_vol) = 10*1000*1000LL; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4399 | LeakCheckMode MC_(clo_leak_check) = LC_Summary; |
| 4400 | VgRes MC_(clo_leak_resolution) = Vg_LowRes; |
| 4401 | Bool MC_(clo_show_reachable) = False; |
| 4402 | Bool MC_(clo_workaround_gcc296_bugs) = False; |
sewardj | eb0fa93 | 2007-11-30 21:41:40 +0000 | [diff] [blame] | 4403 | Int MC_(clo_malloc_fill) = -1; |
| 4404 | Int MC_(clo_free_fill) = -1; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4405 | Int MC_(clo_mc_level) = 2; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4406 | |
| 4407 | static Bool mc_process_cmd_line_options(Char* arg) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4408 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4409 | tl_assert( MC_(clo_mc_level) >= 1 && MC_(clo_mc_level) <= 3 ); |
| 4410 | |
| 4411 | /* Set MC_(clo_mc_level): |
| 4412 | 1 = A bit tracking only |
| 4413 | 2 = A and V bit tracking, but no V bit origins |
| 4414 | 3 = A and V bit tracking, and V bit origins |
| 4415 | |
| 4416 | Do this by inspecting --undef-value-errors= and |
| 4417 | --track-origins=. Reject the case --undef-value-errors=no |
| 4418 | --track-origins=yes as meaningless. |
| 4419 | */ |
| 4420 | if (0 == VG_(strcmp)(arg, "--undef-value-errors=no")) { |
| 4421 | if (MC_(clo_mc_level) == 3) |
| 4422 | goto mc_level_error; |
| 4423 | MC_(clo_mc_level) = 1; |
| 4424 | return True; |
| 4425 | } |
| 4426 | if (0 == VG_(strcmp)(arg, "--undef-value-errors=yes")) { |
| 4427 | if (MC_(clo_mc_level) == 1) |
| 4428 | MC_(clo_mc_level) = 2; |
| 4429 | return True; |
| 4430 | } |
| 4431 | if (0 == VG_(strcmp)(arg, "--track-origins=no")) { |
| 4432 | if (MC_(clo_mc_level) == 3) |
| 4433 | MC_(clo_mc_level) = 2; |
| 4434 | return True; |
| 4435 | } |
| 4436 | if (0 == VG_(strcmp)(arg, "--track-origins=yes")) { |
| 4437 | if (MC_(clo_mc_level) == 1) |
| 4438 | goto mc_level_error; |
| 4439 | MC_(clo_mc_level) = 3; |
| 4440 | return True; |
| 4441 | } |
| 4442 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4443 | VG_BOOL_CLO(arg, "--partial-loads-ok", MC_(clo_partial_loads_ok)) |
| 4444 | else VG_BOOL_CLO(arg, "--show-reachable", MC_(clo_show_reachable)) |
| 4445 | else VG_BOOL_CLO(arg, "--workaround-gcc296-bugs",MC_(clo_workaround_gcc296_bugs)) |
| 4446 | |
sewardj | fa4ca3b | 2007-11-30 17:19:36 +0000 | [diff] [blame] | 4447 | else VG_BNUM_CLO(arg, "--freelist-vol", MC_(clo_freelist_vol), |
| 4448 | 0, 10*1000*1000*1000LL) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4449 | |
| 4450 | else if (VG_CLO_STREQ(arg, "--leak-check=no")) |
| 4451 | MC_(clo_leak_check) = LC_Off; |
| 4452 | else if (VG_CLO_STREQ(arg, "--leak-check=summary")) |
| 4453 | MC_(clo_leak_check) = LC_Summary; |
| 4454 | else if (VG_CLO_STREQ(arg, "--leak-check=yes") || |
| 4455 | VG_CLO_STREQ(arg, "--leak-check=full")) |
| 4456 | MC_(clo_leak_check) = LC_Full; |
| 4457 | |
| 4458 | else if (VG_CLO_STREQ(arg, "--leak-resolution=low")) |
| 4459 | MC_(clo_leak_resolution) = Vg_LowRes; |
| 4460 | else if (VG_CLO_STREQ(arg, "--leak-resolution=med")) |
| 4461 | MC_(clo_leak_resolution) = Vg_MedRes; |
| 4462 | else if (VG_CLO_STREQ(arg, "--leak-resolution=high")) |
| 4463 | MC_(clo_leak_resolution) = Vg_HighRes; |
| 4464 | |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4465 | else if (VG_CLO_STREQN(16,arg,"--ignore-ranges=")) { |
| 4466 | Int i; |
| 4467 | UChar* txt = (UChar*)(arg+16); |
| 4468 | Bool ok = parse_ignore_ranges(txt); |
| 4469 | if (!ok) |
| 4470 | return False; |
| 4471 | tl_assert(ignoreRanges.used >= 0); |
| 4472 | tl_assert(ignoreRanges.used < M_IGNORE_RANGES); |
| 4473 | for (i = 0; i < ignoreRanges.used; i++) { |
| 4474 | Addr s = ignoreRanges.start[i]; |
| 4475 | Addr e = ignoreRanges.end[i]; |
| 4476 | Addr limit = 0x4000000; /* 64M - entirely arbitrary limit */ |
| 4477 | if (e <= s) { |
| 4478 | VG_(message)(Vg_DebugMsg, |
| 4479 | "ERROR: --ignore-ranges: end <= start in range:"); |
| 4480 | VG_(message)(Vg_DebugMsg, |
| 4481 | " 0x%lx-0x%lx", s, e); |
| 4482 | return False; |
| 4483 | } |
| 4484 | if (e - s > limit) { |
| 4485 | VG_(message)(Vg_DebugMsg, |
| 4486 | "ERROR: --ignore-ranges: suspiciously large range:"); |
| 4487 | VG_(message)(Vg_DebugMsg, |
| 4488 | " 0x%lx-0x%lx (size %ld)", s, e, (UWord)(e-s)); |
| 4489 | return False; |
| 4490 | } |
| 4491 | } |
| 4492 | } |
| 4493 | |
sewardj | eb0fa93 | 2007-11-30 21:41:40 +0000 | [diff] [blame] | 4494 | else VG_BHEX_CLO(arg, "--malloc-fill", MC_(clo_malloc_fill), 0x00, 0xFF) |
| 4495 | else VG_BHEX_CLO(arg, "--free-fill", MC_(clo_free_fill), 0x00, 0xFF) |
| 4496 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4497 | else |
| 4498 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
| 4499 | |
| 4500 | return True; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4501 | /*NOTREACHED*/ |
| 4502 | |
| 4503 | mc_level_error: |
| 4504 | VG_(message)(Vg_DebugMsg, "ERROR: --track-origins=yes has no effect " |
| 4505 | "when --undef-value-errors=no"); |
| 4506 | return False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4509 | static void mc_print_usage(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4510 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4511 | VG_(printf)( |
| 4512 | " --leak-check=no|summary|full search for memory leaks at exit? [summary]\n" |
| 4513 | " --leak-resolution=low|med|high how much bt merging in leak check [low]\n" |
| 4514 | " --show-reachable=no|yes show reachable blocks in leak check? [no]\n" |
| 4515 | " --undef-value-errors=no|yes check for undefined value errors [yes]\n" |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4516 | " --track-origins=no|yes show origins of undefined values? [no]\n" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4517 | " --partial-loads-ok=no|yes too hard to explain here; see manual [no]\n" |
sewardj | fa4ca3b | 2007-11-30 17:19:36 +0000 | [diff] [blame] | 4518 | " --freelist-vol=<number> volume of freed blocks queue [10000000]\n" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4519 | " --workaround-gcc296-bugs=no|yes self explanatory [no]\n" |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 4520 | " --ignore-ranges=0xPP-0xQQ[,0xRR-0xSS] assume given addresses are OK\n" |
sewardj | eb0fa93 | 2007-11-30 21:41:40 +0000 | [diff] [blame] | 4521 | " --malloc-fill=<hexnumber> fill malloc'd areas with given value\n" |
| 4522 | " --free-fill=<hexnumber> fill free'd areas with given value\n" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4523 | ); |
| 4524 | VG_(replacement_malloc_print_usage)(); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4527 | static void mc_print_debug_usage(void) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 4528 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4529 | VG_(replacement_malloc_print_debug_usage)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
sewardj | f3418c0 | 2005-11-08 14:10:24 +0000 | [diff] [blame] | 4532 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4533 | /*------------------------------------------------------------*/ |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4534 | /*--- Client blocks ---*/ |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4535 | /*------------------------------------------------------------*/ |
| 4536 | |
| 4537 | /* Client block management: |
| 4538 | |
| 4539 | This is managed as an expanding array of client block descriptors. |
| 4540 | Indices of live descriptors are issued to the client, so it can ask |
| 4541 | to free them later. Therefore we cannot slide live entries down |
| 4542 | over dead ones. Instead we must use free/inuse flags and scan for |
| 4543 | an empty slot at allocation time. This in turn means allocation is |
| 4544 | relatively expensive, so we hope this does not happen too often. |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4545 | |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4546 | An unused block has start == size == 0 |
| 4547 | */ |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4548 | |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4549 | /* type CGenBlock is defined in mc_include.h */ |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4550 | |
| 4551 | /* This subsystem is self-initialising. */ |
sewardj | 56adc35 | 2008-05-02 11:25:17 +0000 | [diff] [blame] | 4552 | static UWord cgb_size = 0; |
| 4553 | static UWord cgb_used = 0; |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4554 | static CGenBlock* cgbs = NULL; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4555 | |
| 4556 | /* Stats for this subsystem. */ |
sewardj | 56adc35 | 2008-05-02 11:25:17 +0000 | [diff] [blame] | 4557 | static ULong cgb_used_MAX = 0; /* Max in use. */ |
| 4558 | static ULong cgb_allocs = 0; /* Number of allocs. */ |
| 4559 | static ULong cgb_discards = 0; /* Number of discards. */ |
| 4560 | static ULong cgb_search = 0; /* Number of searches. */ |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4561 | |
| 4562 | |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4563 | /* Get access to the client block array. */ |
| 4564 | void MC_(get_ClientBlock_array)( /*OUT*/CGenBlock** blocks, |
| 4565 | /*OUT*/UWord* nBlocks ) |
| 4566 | { |
| 4567 | *blocks = cgbs; |
| 4568 | *nBlocks = cgb_used; |
| 4569 | } |
| 4570 | |
| 4571 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4572 | static |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4573 | Int alloc_client_block ( void ) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4574 | { |
sewardj | 56adc35 | 2008-05-02 11:25:17 +0000 | [diff] [blame] | 4575 | UWord i, sz_new; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4576 | CGenBlock* cgbs_new; |
| 4577 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4578 | cgb_allocs++; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4579 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4580 | for (i = 0; i < cgb_used; i++) { |
| 4581 | cgb_search++; |
| 4582 | if (cgbs[i].start == 0 && cgbs[i].size == 0) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4583 | return i; |
| 4584 | } |
| 4585 | |
| 4586 | /* Not found. Try to allocate one at the end. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4587 | if (cgb_used < cgb_size) { |
| 4588 | cgb_used++; |
| 4589 | return cgb_used-1; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
| 4592 | /* Ok, we have to allocate a new one. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4593 | tl_assert(cgb_used == cgb_size); |
| 4594 | sz_new = (cgbs == NULL) ? 10 : (2 * cgb_size); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4595 | |
| 4596 | cgbs_new = VG_(malloc)( sz_new * sizeof(CGenBlock) ); |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4597 | for (i = 0; i < cgb_used; i++) |
| 4598 | cgbs_new[i] = cgbs[i]; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4599 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4600 | if (cgbs != NULL) |
| 4601 | VG_(free)( cgbs ); |
| 4602 | cgbs = cgbs_new; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4603 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4604 | cgb_size = sz_new; |
| 4605 | cgb_used++; |
| 4606 | if (cgb_used > cgb_used_MAX) |
| 4607 | cgb_used_MAX = cgb_used; |
| 4608 | return cgb_used-1; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | |
| 4612 | static void show_client_block_stats ( void ) |
| 4613 | { |
| 4614 | VG_(message)(Vg_DebugMsg, |
sewardj | 56adc35 | 2008-05-02 11:25:17 +0000 | [diff] [blame] | 4615 | "general CBs: %llu allocs, %llu discards, %llu maxinuse, %llu search", |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4616 | cgb_allocs, cgb_discards, cgb_used_MAX, cgb_search |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4617 | ); |
| 4618 | } |
| 4619 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4620 | |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4621 | /*------------------------------------------------------------*/ |
| 4622 | /*--- Client requests ---*/ |
| 4623 | /*------------------------------------------------------------*/ |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4624 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4625 | static Bool mc_handle_client_request ( ThreadId tid, UWord* arg, UWord* ret ) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4626 | { |
| 4627 | Int i; |
| 4628 | Bool ok; |
| 4629 | Addr bad_addr; |
| 4630 | |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 4631 | if (!VG_IS_TOOL_USERREQ('M','C',arg[0]) |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4632 | && VG_USERREQ__MALLOCLIKE_BLOCK != arg[0] |
| 4633 | && VG_USERREQ__FREELIKE_BLOCK != arg[0] |
| 4634 | && VG_USERREQ__CREATE_MEMPOOL != arg[0] |
| 4635 | && VG_USERREQ__DESTROY_MEMPOOL != arg[0] |
| 4636 | && VG_USERREQ__MEMPOOL_ALLOC != arg[0] |
| 4637 | && VG_USERREQ__MEMPOOL_FREE != arg[0] |
| 4638 | && VG_USERREQ__MEMPOOL_TRIM != arg[0] |
| 4639 | && VG_USERREQ__MOVE_MEMPOOL != arg[0] |
| 4640 | && VG_USERREQ__MEMPOOL_CHANGE != arg[0] |
| 4641 | && VG_USERREQ__MEMPOOL_EXISTS != arg[0]) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4642 | return False; |
| 4643 | |
| 4644 | switch (arg[0]) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4645 | case VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE: |
| 4646 | ok = is_mem_addressable ( arg[1], arg[2], &bad_addr ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4647 | if (!ok) |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4648 | MC_(record_user_error) ( tid, bad_addr, /*isAddrErr*/True, 0 ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4649 | *ret = ok ? (UWord)NULL : bad_addr; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4650 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4651 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4652 | case VG_USERREQ__CHECK_MEM_IS_DEFINED: { |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4653 | MC_ReadResult res; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4654 | UInt otag = 0; |
| 4655 | res = is_mem_defined ( arg[1], arg[2], &bad_addr, &otag ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4656 | if (MC_AddrErr == res) |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4657 | MC_(record_user_error) ( tid, bad_addr, /*isAddrErr*/True, 0 ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4658 | else if (MC_ValueErr == res) |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4659 | MC_(record_user_error) ( tid, bad_addr, /*isAddrErr*/False, otag ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4660 | *ret = ( res==MC_Ok ? (UWord)NULL : bad_addr ); |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4661 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
| 4664 | case VG_USERREQ__DO_LEAK_CHECK: |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 4665 | mc_detect_memory_leaks(tid, arg[1] ? LC_Summary : LC_Full); |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4666 | *ret = 0; /* return value is meaningless */ |
| 4667 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4668 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4669 | case VG_USERREQ__MAKE_MEM_NOACCESS: |
| 4670 | MC_(make_mem_noaccess) ( arg[1], arg[2] ); |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4671 | *ret = -1; |
| 4672 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4673 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4674 | case VG_USERREQ__MAKE_MEM_UNDEFINED: |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4675 | make_mem_undefined_w_tid_and_okind ( arg[1], arg[2], tid, |
| 4676 | MC_OKIND_USER ); |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4677 | *ret = -1; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4678 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4679 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4680 | case VG_USERREQ__MAKE_MEM_DEFINED: |
| 4681 | MC_(make_mem_defined) ( arg[1], arg[2] ); |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4682 | *ret = -1; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4683 | break; |
| 4684 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4685 | case VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE: |
| 4686 | make_mem_defined_if_addressable ( arg[1], arg[2] ); |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 4687 | *ret = -1; |
| 4688 | break; |
| 4689 | |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4690 | case VG_USERREQ__CREATE_BLOCK: /* describe a block */ |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4691 | if (arg[1] != 0 && arg[2] != 0) { |
| 4692 | i = alloc_client_block(); |
| 4693 | /* VG_(printf)("allocated %d %p\n", i, cgbs); */ |
| 4694 | cgbs[i].start = arg[1]; |
| 4695 | cgbs[i].size = arg[2]; |
| 4696 | cgbs[i].desc = VG_(strdup)((Char *)arg[3]); |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame] | 4697 | cgbs[i].where = VG_(record_ExeContext) ( tid, 0/*first_ip_delta*/ ); |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4698 | |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4699 | *ret = i; |
| 4700 | } else |
| 4701 | *ret = -1; |
| 4702 | break; |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4703 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4704 | case VG_USERREQ__DISCARD: /* discard */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4705 | if (cgbs == NULL |
| 4706 | || arg[2] >= cgb_used || |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4707 | (cgbs[arg[2]].start == 0 && cgbs[arg[2]].size == 0)) { |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4708 | *ret = 1; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4709 | } else { |
| 4710 | tl_assert(arg[2] >= 0 && arg[2] < cgb_used); |
| 4711 | cgbs[arg[2]].start = cgbs[arg[2]].size = 0; |
| 4712 | VG_(free)(cgbs[arg[2]].desc); |
| 4713 | cgb_discards++; |
| 4714 | *ret = 0; |
| 4715 | } |
| 4716 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4717 | |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4718 | case VG_USERREQ__GET_VBITS: |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4719 | *ret = mc_get_or_set_vbits_for_client |
| 4720 | ( tid, arg[1], arg[2], arg[3], False /* get them */ ); |
| 4721 | break; |
| 4722 | |
| 4723 | case VG_USERREQ__SET_VBITS: |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4724 | *ret = mc_get_or_set_vbits_for_client |
| 4725 | ( tid, arg[1], arg[2], arg[3], True /* set them */ ); |
| 4726 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4727 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4728 | case VG_USERREQ__COUNT_LEAKS: { /* count leaked bytes */ |
| 4729 | UWord** argp = (UWord**)arg; |
| 4730 | // MC_(bytes_leaked) et al were set by the last leak check (or zero |
| 4731 | // if no prior leak checks performed). |
| 4732 | *argp[1] = MC_(bytes_leaked) + MC_(bytes_indirect); |
| 4733 | *argp[2] = MC_(bytes_dubious); |
| 4734 | *argp[3] = MC_(bytes_reachable); |
| 4735 | *argp[4] = MC_(bytes_suppressed); |
| 4736 | // there is no argp[5] |
| 4737 | //*argp[5] = MC_(bytes_indirect); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4738 | // XXX need to make *argp[1-4] defined |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4739 | *ret = 0; |
| 4740 | return True; |
| 4741 | } |
| 4742 | case VG_USERREQ__MALLOCLIKE_BLOCK: { |
| 4743 | Addr p = (Addr)arg[1]; |
| 4744 | SizeT sizeB = arg[2]; |
| 4745 | UInt rzB = arg[3]; |
| 4746 | Bool is_zeroed = (Bool)arg[4]; |
| 4747 | |
| 4748 | MC_(new_block) ( tid, p, sizeB, /*ignored*/0, rzB, is_zeroed, |
| 4749 | MC_AllocCustom, MC_(malloc_list) ); |
| 4750 | return True; |
| 4751 | } |
| 4752 | case VG_USERREQ__FREELIKE_BLOCK: { |
| 4753 | Addr p = (Addr)arg[1]; |
| 4754 | UInt rzB = arg[2]; |
| 4755 | |
| 4756 | MC_(handle_free) ( tid, p, rzB, MC_AllocCustom ); |
| 4757 | return True; |
| 4758 | } |
| 4759 | |
| 4760 | case _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR: { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 4761 | Char* s = (Char*)arg[1]; |
| 4762 | Addr dst = (Addr) arg[2]; |
| 4763 | Addr src = (Addr) arg[3]; |
| 4764 | SizeT len = (SizeT)arg[4]; |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 4765 | MC_(record_overlap_error)(tid, s, src, dst, len); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4766 | return True; |
| 4767 | } |
| 4768 | |
| 4769 | case VG_USERREQ__CREATE_MEMPOOL: { |
| 4770 | Addr pool = (Addr)arg[1]; |
| 4771 | UInt rzB = arg[2]; |
| 4772 | Bool is_zeroed = (Bool)arg[3]; |
| 4773 | |
| 4774 | MC_(create_mempool) ( pool, rzB, is_zeroed ); |
| 4775 | return True; |
| 4776 | } |
| 4777 | |
| 4778 | case VG_USERREQ__DESTROY_MEMPOOL: { |
| 4779 | Addr pool = (Addr)arg[1]; |
| 4780 | |
| 4781 | MC_(destroy_mempool) ( pool ); |
| 4782 | return True; |
| 4783 | } |
| 4784 | |
| 4785 | case VG_USERREQ__MEMPOOL_ALLOC: { |
| 4786 | Addr pool = (Addr)arg[1]; |
| 4787 | Addr addr = (Addr)arg[2]; |
| 4788 | UInt size = arg[3]; |
| 4789 | |
| 4790 | MC_(mempool_alloc) ( tid, pool, addr, size ); |
| 4791 | return True; |
| 4792 | } |
| 4793 | |
| 4794 | case VG_USERREQ__MEMPOOL_FREE: { |
| 4795 | Addr pool = (Addr)arg[1]; |
| 4796 | Addr addr = (Addr)arg[2]; |
| 4797 | |
| 4798 | MC_(mempool_free) ( pool, addr ); |
| 4799 | return True; |
| 4800 | } |
| 4801 | |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 4802 | case VG_USERREQ__MEMPOOL_TRIM: { |
| 4803 | Addr pool = (Addr)arg[1]; |
| 4804 | Addr addr = (Addr)arg[2]; |
| 4805 | UInt size = arg[3]; |
| 4806 | |
| 4807 | MC_(mempool_trim) ( pool, addr, size ); |
| 4808 | return True; |
| 4809 | } |
| 4810 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 4811 | case VG_USERREQ__MOVE_MEMPOOL: { |
| 4812 | Addr poolA = (Addr)arg[1]; |
| 4813 | Addr poolB = (Addr)arg[2]; |
| 4814 | |
| 4815 | MC_(move_mempool) ( poolA, poolB ); |
| 4816 | return True; |
| 4817 | } |
| 4818 | |
| 4819 | case VG_USERREQ__MEMPOOL_CHANGE: { |
| 4820 | Addr pool = (Addr)arg[1]; |
| 4821 | Addr addrA = (Addr)arg[2]; |
| 4822 | Addr addrB = (Addr)arg[3]; |
| 4823 | UInt size = arg[4]; |
| 4824 | |
| 4825 | MC_(mempool_change) ( pool, addrA, addrB, size ); |
| 4826 | return True; |
| 4827 | } |
| 4828 | |
| 4829 | case VG_USERREQ__MEMPOOL_EXISTS: { |
| 4830 | Addr pool = (Addr)arg[1]; |
| 4831 | |
| 4832 | *ret = (UWord) MC_(mempool_exists) ( pool ); |
| 4833 | return True; |
| 4834 | } |
| 4835 | |
| 4836 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4837 | default: |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4838 | VG_(message)(Vg_UserMsg, |
| 4839 | "Warning: unknown memcheck client request code %llx", |
| 4840 | (ULong)arg[0]); |
| 4841 | return False; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4842 | } |
| 4843 | return True; |
| 4844 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4845 | |
| 4846 | /*------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4847 | /*--- Crude profiling machinery. ---*/ |
| 4848 | /*------------------------------------------------------------*/ |
| 4849 | |
| 4850 | // We track a number of interesting events (using PROF_EVENT) |
| 4851 | // if MC_PROFILE_MEMORY is defined. |
| 4852 | |
| 4853 | #ifdef MC_PROFILE_MEMORY |
| 4854 | |
| 4855 | UInt MC_(event_ctr)[N_PROF_EVENTS]; |
| 4856 | HChar* MC_(event_ctr_name)[N_PROF_EVENTS]; |
| 4857 | |
| 4858 | static void init_prof_mem ( void ) |
| 4859 | { |
| 4860 | Int i; |
| 4861 | for (i = 0; i < N_PROF_EVENTS; i++) { |
| 4862 | MC_(event_ctr)[i] = 0; |
| 4863 | MC_(event_ctr_name)[i] = NULL; |
| 4864 | } |
| 4865 | } |
| 4866 | |
| 4867 | static void done_prof_mem ( void ) |
| 4868 | { |
| 4869 | Int i; |
| 4870 | Bool spaced = False; |
| 4871 | for (i = 0; i < N_PROF_EVENTS; i++) { |
| 4872 | if (!spaced && (i % 10) == 0) { |
| 4873 | VG_(printf)("\n"); |
| 4874 | spaced = True; |
| 4875 | } |
| 4876 | if (MC_(event_ctr)[i] > 0) { |
| 4877 | spaced = False; |
| 4878 | VG_(printf)( "prof mem event %3d: %9d %s\n", |
| 4879 | i, MC_(event_ctr)[i], |
| 4880 | MC_(event_ctr_name)[i] |
| 4881 | ? MC_(event_ctr_name)[i] : "unnamed"); |
| 4882 | } |
| 4883 | } |
| 4884 | } |
| 4885 | |
| 4886 | #else |
| 4887 | |
| 4888 | static void init_prof_mem ( void ) { } |
| 4889 | static void done_prof_mem ( void ) { } |
| 4890 | |
| 4891 | #endif |
| 4892 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4893 | |
| 4894 | /*------------------------------------------------------------*/ |
| 4895 | /*--- Origin tracking stuff ---*/ |
| 4896 | /*------------------------------------------------------------*/ |
| 4897 | |
| 4898 | /*--------------------------------------------*/ |
| 4899 | /*--- Origin tracking: load handlers ---*/ |
| 4900 | /*--------------------------------------------*/ |
| 4901 | |
| 4902 | static INLINE UInt merge_origins ( UInt or1, UInt or2 ) { |
| 4903 | return or1 > or2 ? or1 : or2; |
| 4904 | } |
| 4905 | |
| 4906 | UWord VG_REGPARM(1) MC_(helperc_b_load1)( Addr a ) { |
| 4907 | OCacheLine* line; |
| 4908 | UChar descr; |
| 4909 | UWord lineoff = oc_line_offset(a); |
| 4910 | UWord byteoff = a & 3; /* 0, 1, 2 or 3 */ |
| 4911 | |
| 4912 | if (OC_ENABLE_ASSERTIONS) { |
| 4913 | tl_assert(lineoff >= 0 && lineoff < OC_W32S_PER_LINE); |
| 4914 | } |
| 4915 | |
| 4916 | line = find_OCacheLine( a ); |
| 4917 | |
| 4918 | descr = line->descr[lineoff]; |
| 4919 | if (OC_ENABLE_ASSERTIONS) { |
| 4920 | tl_assert(descr < 0x10); |
| 4921 | } |
| 4922 | |
| 4923 | if (LIKELY(0 == (descr & (1 << byteoff)))) { |
| 4924 | return 0; |
| 4925 | } else { |
| 4926 | return line->w32[lineoff]; |
| 4927 | } |
| 4928 | } |
| 4929 | |
| 4930 | UWord VG_REGPARM(1) MC_(helperc_b_load2)( Addr a ) { |
| 4931 | OCacheLine* line; |
| 4932 | UChar descr; |
| 4933 | UWord lineoff, byteoff; |
| 4934 | |
| 4935 | if (UNLIKELY(a & 1)) { |
| 4936 | /* Handle misaligned case, slowly. */ |
| 4937 | UInt oLo = (UInt)MC_(helperc_b_load1)( a + 0 ); |
| 4938 | UInt oHi = (UInt)MC_(helperc_b_load1)( a + 1 ); |
| 4939 | return merge_origins(oLo, oHi); |
| 4940 | } |
| 4941 | |
| 4942 | lineoff = oc_line_offset(a); |
| 4943 | byteoff = a & 3; /* 0 or 2 */ |
| 4944 | |
| 4945 | if (OC_ENABLE_ASSERTIONS) { |
| 4946 | tl_assert(lineoff >= 0 && lineoff < OC_W32S_PER_LINE); |
| 4947 | } |
| 4948 | line = find_OCacheLine( a ); |
| 4949 | |
| 4950 | descr = line->descr[lineoff]; |
| 4951 | if (OC_ENABLE_ASSERTIONS) { |
| 4952 | tl_assert(descr < 0x10); |
| 4953 | } |
| 4954 | |
| 4955 | if (LIKELY(0 == (descr & (3 << byteoff)))) { |
| 4956 | return 0; |
| 4957 | } else { |
| 4958 | return line->w32[lineoff]; |
| 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | UWord VG_REGPARM(1) MC_(helperc_b_load4)( Addr a ) { |
| 4963 | OCacheLine* line; |
| 4964 | UChar descr; |
| 4965 | UWord lineoff; |
| 4966 | |
| 4967 | if (UNLIKELY(a & 3)) { |
| 4968 | /* Handle misaligned case, slowly. */ |
| 4969 | UInt oLo = (UInt)MC_(helperc_b_load2)( a + 0 ); |
| 4970 | UInt oHi = (UInt)MC_(helperc_b_load2)( a + 2 ); |
| 4971 | return merge_origins(oLo, oHi); |
| 4972 | } |
| 4973 | |
| 4974 | lineoff = oc_line_offset(a); |
| 4975 | if (OC_ENABLE_ASSERTIONS) { |
| 4976 | tl_assert(lineoff >= 0 && lineoff < OC_W32S_PER_LINE); |
| 4977 | } |
| 4978 | |
| 4979 | line = find_OCacheLine( a ); |
| 4980 | |
| 4981 | descr = line->descr[lineoff]; |
| 4982 | if (OC_ENABLE_ASSERTIONS) { |
| 4983 | tl_assert(descr < 0x10); |
| 4984 | } |
| 4985 | |
| 4986 | if (LIKELY(0 == descr)) { |
| 4987 | return 0; |
| 4988 | } else { |
| 4989 | return line->w32[lineoff]; |
| 4990 | } |
| 4991 | } |
| 4992 | |
| 4993 | UWord VG_REGPARM(1) MC_(helperc_b_load8)( Addr a ) { |
| 4994 | OCacheLine* line; |
| 4995 | UChar descrLo, descrHi, descr; |
| 4996 | UWord lineoff; |
| 4997 | |
| 4998 | if (UNLIKELY(a & 7)) { |
| 4999 | /* Handle misaligned case, slowly. */ |
| 5000 | UInt oLo = (UInt)MC_(helperc_b_load4)( a + 0 ); |
| 5001 | UInt oHi = (UInt)MC_(helperc_b_load4)( a + 4 ); |
| 5002 | return merge_origins(oLo, oHi); |
| 5003 | } |
| 5004 | |
| 5005 | lineoff = oc_line_offset(a); |
| 5006 | if (OC_ENABLE_ASSERTIONS) { |
| 5007 | tl_assert(lineoff == (lineoff & 6)); /*0,2,4,6*//*since 8-aligned*/ |
| 5008 | } |
| 5009 | |
| 5010 | line = find_OCacheLine( a ); |
| 5011 | |
| 5012 | descrLo = line->descr[lineoff + 0]; |
| 5013 | descrHi = line->descr[lineoff + 1]; |
| 5014 | descr = descrLo | descrHi; |
| 5015 | if (OC_ENABLE_ASSERTIONS) { |
| 5016 | tl_assert(descr < 0x10); |
| 5017 | } |
| 5018 | |
| 5019 | if (LIKELY(0 == descr)) { |
| 5020 | return 0; /* both 32-bit chunks are defined */ |
| 5021 | } else { |
| 5022 | UInt oLo = descrLo == 0 ? 0 : line->w32[lineoff + 0]; |
| 5023 | UInt oHi = descrHi == 0 ? 0 : line->w32[lineoff + 1]; |
| 5024 | return merge_origins(oLo, oHi); |
| 5025 | } |
| 5026 | } |
| 5027 | |
| 5028 | UWord VG_REGPARM(1) MC_(helperc_b_load16)( Addr a ) { |
| 5029 | UInt oLo = (UInt)MC_(helperc_b_load8)( a + 0 ); |
| 5030 | UInt oHi = (UInt)MC_(helperc_b_load8)( a + 8 ); |
| 5031 | UInt oBoth = merge_origins(oLo, oHi); |
| 5032 | return (UWord)oBoth; |
| 5033 | } |
| 5034 | |
| 5035 | |
| 5036 | /*--------------------------------------------*/ |
| 5037 | /*--- Origin tracking: store handlers ---*/ |
| 5038 | /*--------------------------------------------*/ |
| 5039 | |
| 5040 | void VG_REGPARM(2) MC_(helperc_b_store1)( Addr a, UWord d32 ) { |
| 5041 | OCacheLine* line; |
| 5042 | UWord lineoff = oc_line_offset(a); |
| 5043 | UWord byteoff = a & 3; /* 0, 1, 2 or 3 */ |
| 5044 | |
| 5045 | if (OC_ENABLE_ASSERTIONS) { |
| 5046 | tl_assert(lineoff >= 0 && lineoff < OC_W32S_PER_LINE); |
| 5047 | } |
| 5048 | |
| 5049 | line = find_OCacheLine( a ); |
| 5050 | |
| 5051 | if (d32 == 0) { |
| 5052 | line->descr[lineoff] &= ~(1 << byteoff); |
| 5053 | } else { |
| 5054 | line->descr[lineoff] |= (1 << byteoff); |
| 5055 | line->w32[lineoff] = d32; |
| 5056 | } |
| 5057 | } |
| 5058 | |
| 5059 | void VG_REGPARM(2) MC_(helperc_b_store2)( Addr a, UWord d32 ) { |
| 5060 | OCacheLine* line; |
| 5061 | UWord lineoff, byteoff; |
| 5062 | |
| 5063 | if (UNLIKELY(a & 1)) { |
| 5064 | /* Handle misaligned case, slowly. */ |
| 5065 | MC_(helperc_b_store1)( a + 0, d32 ); |
| 5066 | MC_(helperc_b_store1)( a + 1, d32 ); |
| 5067 | return; |
| 5068 | } |
| 5069 | |
| 5070 | lineoff = oc_line_offset(a); |
| 5071 | byteoff = a & 3; /* 0 or 2 */ |
| 5072 | |
| 5073 | if (OC_ENABLE_ASSERTIONS) { |
| 5074 | tl_assert(lineoff >= 0 && lineoff < OC_W32S_PER_LINE); |
| 5075 | } |
| 5076 | |
| 5077 | line = find_OCacheLine( a ); |
| 5078 | |
| 5079 | if (d32 == 0) { |
| 5080 | line->descr[lineoff] &= ~(3 << byteoff); |
| 5081 | } else { |
| 5082 | line->descr[lineoff] |= (3 << byteoff); |
| 5083 | line->w32[lineoff] = d32; |
| 5084 | } |
| 5085 | } |
| 5086 | |
| 5087 | void VG_REGPARM(2) MC_(helperc_b_store4)( Addr a, UWord d32 ) { |
| 5088 | OCacheLine* line; |
| 5089 | UWord lineoff; |
| 5090 | |
| 5091 | if (UNLIKELY(a & 3)) { |
| 5092 | /* Handle misaligned case, slowly. */ |
| 5093 | MC_(helperc_b_store2)( a + 0, d32 ); |
| 5094 | MC_(helperc_b_store2)( a + 2, d32 ); |
| 5095 | return; |
| 5096 | } |
| 5097 | |
| 5098 | lineoff = oc_line_offset(a); |
| 5099 | if (OC_ENABLE_ASSERTIONS) { |
| 5100 | tl_assert(lineoff >= 0 && lineoff < OC_W32S_PER_LINE); |
| 5101 | } |
| 5102 | |
| 5103 | line = find_OCacheLine( a ); |
| 5104 | |
| 5105 | if (d32 == 0) { |
| 5106 | line->descr[lineoff] = 0; |
| 5107 | } else { |
| 5108 | line->descr[lineoff] = 0xF; |
| 5109 | line->w32[lineoff] = d32; |
| 5110 | } |
| 5111 | } |
| 5112 | |
| 5113 | void VG_REGPARM(2) MC_(helperc_b_store8)( Addr a, UWord d32 ) { |
| 5114 | OCacheLine* line; |
| 5115 | UWord lineoff; |
| 5116 | |
| 5117 | if (UNLIKELY(a & 7)) { |
| 5118 | /* Handle misaligned case, slowly. */ |
| 5119 | MC_(helperc_b_store4)( a + 0, d32 ); |
| 5120 | MC_(helperc_b_store4)( a + 4, d32 ); |
| 5121 | return; |
| 5122 | } |
| 5123 | |
| 5124 | lineoff = oc_line_offset(a); |
| 5125 | if (OC_ENABLE_ASSERTIONS) { |
| 5126 | tl_assert(lineoff == (lineoff & 6)); /*0,2,4,6*//*since 8-aligned*/ |
| 5127 | } |
| 5128 | |
| 5129 | line = find_OCacheLine( a ); |
| 5130 | |
| 5131 | if (d32 == 0) { |
| 5132 | line->descr[lineoff + 0] = 0; |
| 5133 | line->descr[lineoff + 1] = 0; |
| 5134 | } else { |
| 5135 | line->descr[lineoff + 0] = 0xF; |
| 5136 | line->descr[lineoff + 1] = 0xF; |
| 5137 | line->w32[lineoff + 0] = d32; |
| 5138 | line->w32[lineoff + 1] = d32; |
| 5139 | } |
| 5140 | } |
| 5141 | |
| 5142 | void VG_REGPARM(2) MC_(helperc_b_store16)( Addr a, UWord d32 ) { |
| 5143 | MC_(helperc_b_store8)( a + 0, d32 ); |
| 5144 | MC_(helperc_b_store8)( a + 8, d32 ); |
| 5145 | } |
| 5146 | |
| 5147 | |
| 5148 | /*--------------------------------------------*/ |
| 5149 | /*--- Origin tracking: sarp handlers ---*/ |
| 5150 | /*--------------------------------------------*/ |
| 5151 | |
| 5152 | __attribute__((noinline)) |
| 5153 | static void ocache_sarp_Set_Origins ( Addr a, UWord len, UInt otag ) { |
| 5154 | if ((a & 1) && len >= 1) { |
| 5155 | MC_(helperc_b_store1)( a, otag ); |
| 5156 | a++; |
| 5157 | len--; |
| 5158 | } |
| 5159 | if ((a & 2) && len >= 2) { |
| 5160 | MC_(helperc_b_store2)( a, otag ); |
| 5161 | a += 2; |
| 5162 | len -= 2; |
| 5163 | } |
| 5164 | if (len >= 4) |
| 5165 | tl_assert(0 == (a & 3)); |
| 5166 | while (len >= 4) { |
| 5167 | MC_(helperc_b_store4)( a, otag ); |
| 5168 | a += 4; |
| 5169 | len -= 4; |
| 5170 | } |
| 5171 | if (len >= 2) { |
| 5172 | MC_(helperc_b_store2)( a, otag ); |
| 5173 | a += 2; |
| 5174 | len -= 2; |
| 5175 | } |
| 5176 | if (len >= 1) { |
| 5177 | MC_(helperc_b_store1)( a, otag ); |
| 5178 | a++; |
| 5179 | len--; |
| 5180 | } |
| 5181 | tl_assert(len == 0); |
| 5182 | } |
| 5183 | |
| 5184 | __attribute__((noinline)) |
| 5185 | static void ocache_sarp_Clear_Origins ( Addr a, UWord len ) { |
| 5186 | if ((a & 1) && len >= 1) { |
| 5187 | MC_(helperc_b_store1)( a, 0 ); |
| 5188 | a++; |
| 5189 | len--; |
| 5190 | } |
| 5191 | if ((a & 2) && len >= 2) { |
| 5192 | MC_(helperc_b_store2)( a, 0 ); |
| 5193 | a += 2; |
| 5194 | len -= 2; |
| 5195 | } |
| 5196 | if (len >= 4) |
| 5197 | tl_assert(0 == (a & 3)); |
| 5198 | while (len >= 4) { |
| 5199 | MC_(helperc_b_store4)( a, 0 ); |
| 5200 | a += 4; |
| 5201 | len -= 4; |
| 5202 | } |
| 5203 | if (len >= 2) { |
| 5204 | MC_(helperc_b_store2)( a, 0 ); |
| 5205 | a += 2; |
| 5206 | len -= 2; |
| 5207 | } |
| 5208 | if (len >= 1) { |
| 5209 | MC_(helperc_b_store1)( a, 0 ); |
| 5210 | a++; |
| 5211 | len--; |
| 5212 | } |
| 5213 | tl_assert(len == 0); |
| 5214 | } |
| 5215 | |
| 5216 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5217 | /*------------------------------------------------------------*/ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5218 | /*--- Setup and finalisation ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5219 | /*------------------------------------------------------------*/ |
| 5220 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5221 | static void mc_post_clo_init ( void ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 5222 | { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 5223 | /* If we've been asked to emit XML, mash around various other |
| 5224 | options so as to constrain the output somewhat. */ |
| 5225 | if (VG_(clo_xml)) { |
| 5226 | /* Extract as much info as possible from the leak checker. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5227 | /* MC_(clo_show_reachable) = True; */ |
| 5228 | MC_(clo_leak_check) = LC_Full; |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 5229 | } |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5230 | |
| 5231 | tl_assert( MC_(clo_mc_level) >= 1 && MC_(clo_mc_level) <= 3 ); |
| 5232 | |
| 5233 | if (MC_(clo_mc_level) == 3) { |
| 5234 | /* We're doing origin tracking. */ |
| 5235 | # ifdef PERF_FAST_STACK |
| 5236 | VG_(track_new_mem_stack_4_w_ECU) ( mc_new_mem_stack_4_w_ECU ); |
| 5237 | VG_(track_new_mem_stack_8_w_ECU) ( mc_new_mem_stack_8_w_ECU ); |
| 5238 | VG_(track_new_mem_stack_12_w_ECU) ( mc_new_mem_stack_12_w_ECU ); |
| 5239 | VG_(track_new_mem_stack_16_w_ECU) ( mc_new_mem_stack_16_w_ECU ); |
| 5240 | VG_(track_new_mem_stack_32_w_ECU) ( mc_new_mem_stack_32_w_ECU ); |
| 5241 | VG_(track_new_mem_stack_112_w_ECU) ( mc_new_mem_stack_112_w_ECU ); |
| 5242 | VG_(track_new_mem_stack_128_w_ECU) ( mc_new_mem_stack_128_w_ECU ); |
| 5243 | VG_(track_new_mem_stack_144_w_ECU) ( mc_new_mem_stack_144_w_ECU ); |
| 5244 | VG_(track_new_mem_stack_160_w_ECU) ( mc_new_mem_stack_160_w_ECU ); |
| 5245 | # endif |
| 5246 | VG_(track_new_mem_stack_w_ECU) ( mc_new_mem_stack_w_ECU ); |
| 5247 | } else { |
| 5248 | /* Not doing origin tracking */ |
| 5249 | # ifdef PERF_FAST_STACK |
| 5250 | VG_(track_new_mem_stack_4) ( mc_new_mem_stack_4 ); |
| 5251 | VG_(track_new_mem_stack_8) ( mc_new_mem_stack_8 ); |
| 5252 | VG_(track_new_mem_stack_12) ( mc_new_mem_stack_12 ); |
| 5253 | VG_(track_new_mem_stack_16) ( mc_new_mem_stack_16 ); |
| 5254 | VG_(track_new_mem_stack_32) ( mc_new_mem_stack_32 ); |
| 5255 | VG_(track_new_mem_stack_112) ( mc_new_mem_stack_112 ); |
| 5256 | VG_(track_new_mem_stack_128) ( mc_new_mem_stack_128 ); |
| 5257 | VG_(track_new_mem_stack_144) ( mc_new_mem_stack_144 ); |
| 5258 | VG_(track_new_mem_stack_160) ( mc_new_mem_stack_160 ); |
| 5259 | # endif |
| 5260 | VG_(track_new_mem_stack) ( mc_new_mem_stack ); |
| 5261 | } |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 5262 | |
| 5263 | /* This origin tracking cache is huge (~100M), so only initialise |
| 5264 | if we need it. */ |
| 5265 | if (MC_(clo_mc_level) >= 3) { |
| 5266 | init_OCache(); |
| 5267 | tl_assert(ocache != NULL); |
| 5268 | tl_assert(ocacheL2 != NULL); |
| 5269 | } else { |
| 5270 | tl_assert(ocache == NULL); |
| 5271 | tl_assert(ocacheL2 == NULL); |
| 5272 | } |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 5273 | } |
| 5274 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5275 | static void print_SM_info(char* type, int n_SMs) |
| 5276 | { |
| 5277 | VG_(message)(Vg_DebugMsg, |
| 5278 | " memcheck: SMs: %s = %d (%dk, %dM)", |
| 5279 | type, |
| 5280 | n_SMs, |
| 5281 | n_SMs * sizeof(SecMap) / 1024, |
| 5282 | n_SMs * sizeof(SecMap) / (1024 * 1024) ); |
| 5283 | } |
| 5284 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5285 | static void mc_fini ( Int exitcode ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 5286 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5287 | MC_(print_malloc_stats)(); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 5288 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5289 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) { |
| 5290 | if (MC_(clo_leak_check) == LC_Off) |
| 5291 | VG_(message)(Vg_UserMsg, |
| 5292 | "For a detailed leak analysis, rerun with: --leak-check=yes"); |
| 5293 | |
| 5294 | VG_(message)(Vg_UserMsg, |
| 5295 | "For counts of detected errors, rerun with: -v"); |
| 5296 | } |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5297 | |
| 5298 | |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 5299 | if (MC_(any_value_errors) && !VG_(clo_xml) && VG_(clo_verbosity) >= 1 |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5300 | && MC_(clo_mc_level) == 2) { |
| 5301 | VG_(message)(Vg_UserMsg, |
| 5302 | "Use --track-origins=yes to see where " |
| 5303 | "uninitialised values come from"); |
| 5304 | } |
| 5305 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5306 | if (MC_(clo_leak_check) != LC_Off) |
| 5307 | mc_detect_memory_leaks(1/*bogus ThreadId*/, MC_(clo_leak_check)); |
| 5308 | |
| 5309 | done_prof_mem(); |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 5310 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 5311 | if (VG_(clo_verbosity) > 1) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5312 | SizeT max_secVBit_szB, max_SMs_szB, max_shmem_szB; |
| 5313 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 5314 | VG_(message)(Vg_DebugMsg, |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 5315 | " memcheck: sanity checks: %d cheap, %d expensive", |
| 5316 | n_sanity_cheap, n_sanity_expensive ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 5317 | VG_(message)(Vg_DebugMsg, |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 5318 | " memcheck: auxmaps: %d auxmap entries (%dk, %dM) in use", |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 5319 | n_auxmap_L2_nodes, |
| 5320 | n_auxmap_L2_nodes * 64, |
| 5321 | n_auxmap_L2_nodes / 16 ); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 5322 | VG_(message)(Vg_DebugMsg, |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 5323 | " memcheck: auxmaps_L1: %lld searches, %lld cmps, ratio %lld:10", |
| 5324 | n_auxmap_L1_searches, n_auxmap_L1_cmps, |
| 5325 | (10ULL * n_auxmap_L1_cmps) |
| 5326 | / (n_auxmap_L1_searches ? n_auxmap_L1_searches : 1) |
| 5327 | ); |
| 5328 | VG_(message)(Vg_DebugMsg, |
| 5329 | " memcheck: auxmaps_L2: %lld searches, %lld nodes", |
| 5330 | n_auxmap_L2_searches, n_auxmap_L2_nodes |
| 5331 | ); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 5332 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 5333 | print_SM_info("n_issued ", n_issued_SMs); |
| 5334 | print_SM_info("n_deissued ", n_deissued_SMs); |
| 5335 | print_SM_info("max_noaccess ", max_noaccess_SMs); |
| 5336 | print_SM_info("max_undefined", max_undefined_SMs); |
| 5337 | print_SM_info("max_defined ", max_defined_SMs); |
| 5338 | print_SM_info("max_non_DSM ", max_non_DSM_SMs); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5339 | |
| 5340 | // Three DSMs, plus the non-DSM ones |
| 5341 | max_SMs_szB = (3 + max_non_DSM_SMs) * sizeof(SecMap); |
| 5342 | // The 3*sizeof(Word) bytes is the AVL node metadata size. |
| 5343 | // The 4*sizeof(Word) bytes is the malloc metadata size. |
| 5344 | // Hardwiring these sizes in sucks, but I don't see how else to do it. |
| 5345 | max_secVBit_szB = max_secVBit_nodes * |
| 5346 | (sizeof(SecVBitNode) + 3*sizeof(Word) + 4*sizeof(Word)); |
| 5347 | max_shmem_szB = sizeof(primary_map) + max_SMs_szB + max_secVBit_szB; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 5348 | |
| 5349 | VG_(message)(Vg_DebugMsg, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5350 | " memcheck: max sec V bit nodes: %d (%dk, %dM)", |
| 5351 | max_secVBit_nodes, max_secVBit_szB / 1024, |
| 5352 | max_secVBit_szB / (1024 * 1024)); |
| 5353 | VG_(message)(Vg_DebugMsg, |
| 5354 | " memcheck: set_sec_vbits8 calls: %llu (new: %llu, updates: %llu)", |
| 5355 | sec_vbits_new_nodes + sec_vbits_updates, |
| 5356 | sec_vbits_new_nodes, sec_vbits_updates ); |
| 5357 | VG_(message)(Vg_DebugMsg, |
| 5358 | " memcheck: max shadow mem size: %dk, %dM", |
| 5359 | max_shmem_szB / 1024, max_shmem_szB / (1024 * 1024)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5360 | |
| 5361 | if (MC_(clo_mc_level) >= 3) { |
| 5362 | VG_(message)(Vg_DebugMsg, |
| 5363 | " ocacheL1: %,12lu refs %,12lu misses (%,lu lossage)", |
| 5364 | stats_ocacheL1_find, |
| 5365 | stats_ocacheL1_misses, |
| 5366 | stats_ocacheL1_lossage ); |
| 5367 | VG_(message)(Vg_DebugMsg, |
| 5368 | " ocacheL1: %,12lu at 0 %,12lu at 1", |
| 5369 | stats_ocacheL1_find - stats_ocacheL1_misses |
| 5370 | - stats_ocacheL1_found_at_1 |
| 5371 | - stats_ocacheL1_found_at_N, |
| 5372 | stats_ocacheL1_found_at_1 ); |
| 5373 | VG_(message)(Vg_DebugMsg, |
| 5374 | " ocacheL1: %,12lu at 2+ %,12lu move-fwds", |
| 5375 | stats_ocacheL1_found_at_N, |
| 5376 | stats_ocacheL1_movefwds ); |
| 5377 | VG_(message)(Vg_DebugMsg, |
| 5378 | " ocacheL1: %,12lu sizeB %,12lu useful", |
| 5379 | (UWord)sizeof(OCache), |
| 5380 | 4 * OC_W32S_PER_LINE * OC_LINES_PER_SET * OC_N_SETS ); |
| 5381 | VG_(message)(Vg_DebugMsg, |
| 5382 | " ocacheL2: %,12lu refs %,12lu misses", |
| 5383 | stats__ocacheL2_refs, |
| 5384 | stats__ocacheL2_misses ); |
| 5385 | VG_(message)(Vg_DebugMsg, |
| 5386 | " ocacheL2: %,9lu max nodes %,9lu curr nodes", |
| 5387 | stats__ocacheL2_n_nodes_max, |
| 5388 | stats__ocacheL2_n_nodes ); |
| 5389 | VG_(message)(Vg_DebugMsg, |
| 5390 | " niacache: %,12lu refs %,12lu misses", |
| 5391 | stats__nia_cache_queries, stats__nia_cache_misses); |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 5392 | } else { |
| 5393 | tl_assert(ocache == NULL); |
| 5394 | tl_assert(ocacheL2 == NULL); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5395 | } |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 5398 | if (0) { |
| 5399 | VG_(message)(Vg_DebugMsg, |
| 5400 | "------ Valgrind's client block stats follow ---------------" ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 5401 | show_client_block_stats(); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 5402 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5405 | static void mc_pre_clo_init(void) |
| 5406 | { |
| 5407 | VG_(details_name) ("Memcheck"); |
| 5408 | VG_(details_version) (NULL); |
| 5409 | VG_(details_description) ("a memory error detector"); |
| 5410 | VG_(details_copyright_author)( |
sewardj | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 5411 | "Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al."); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5412 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 5413 | VG_(details_avg_translation_sizeB) ( 556 ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5414 | |
| 5415 | VG_(basic_tool_funcs) (mc_post_clo_init, |
| 5416 | MC_(instrument), |
| 5417 | mc_fini); |
| 5418 | |
sewardj | 81651dc | 2007-08-28 06:05:20 +0000 | [diff] [blame] | 5419 | VG_(needs_final_IR_tidy_pass) ( MC_(final_tidy) ); |
| 5420 | |
| 5421 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5422 | VG_(needs_core_errors) (); |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 5423 | VG_(needs_tool_errors) (MC_(eq_Error), |
| 5424 | MC_(pp_Error), |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame] | 5425 | True,/*show TIDs for errors*/ |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 5426 | MC_(update_Error_extra), |
| 5427 | MC_(is_recognised_suppression), |
| 5428 | MC_(read_extra_suppression_info), |
| 5429 | MC_(error_matches_suppression), |
| 5430 | MC_(get_error_name), |
| 5431 | MC_(print_extra_suppression_info)); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5432 | VG_(needs_libc_freeres) (); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5433 | VG_(needs_command_line_options)(mc_process_cmd_line_options, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5434 | mc_print_usage, |
| 5435 | mc_print_debug_usage); |
| 5436 | VG_(needs_client_requests) (mc_handle_client_request); |
| 5437 | VG_(needs_sanity_checks) (mc_cheap_sanity_check, |
| 5438 | mc_expensive_sanity_check); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5439 | VG_(needs_malloc_replacement) (MC_(malloc), |
| 5440 | MC_(__builtin_new), |
| 5441 | MC_(__builtin_vec_new), |
| 5442 | MC_(memalign), |
| 5443 | MC_(calloc), |
| 5444 | MC_(free), |
| 5445 | MC_(__builtin_delete), |
| 5446 | MC_(__builtin_vec_delete), |
| 5447 | MC_(realloc), |
| 5448 | MC_MALLOC_REDZONE_SZB ); |
njn | ca54af3 | 2006-04-16 10:25:43 +0000 | [diff] [blame] | 5449 | VG_(needs_xml_output) (); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5450 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5451 | VG_(track_new_mem_startup) ( mc_new_mem_startup ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5452 | VG_(track_new_mem_stack_signal)( make_mem_undefined_w_tid ); |
| 5453 | VG_(track_new_mem_brk) ( make_mem_undefined_w_tid ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5454 | VG_(track_new_mem_mmap) ( mc_new_mem_mmap ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5455 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5456 | VG_(track_copy_mem_remap) ( MC_(copy_address_range_state) ); |
njn | 8162371 | 2005-10-07 04:48:37 +0000 | [diff] [blame] | 5457 | |
| 5458 | // Nb: we don't do anything with mprotect. This means that V bits are |
| 5459 | // preserved if a program, for example, marks some memory as inaccessible |
| 5460 | // and then later marks it as accessible again. |
| 5461 | // |
| 5462 | // If an access violation occurs (eg. writing to read-only memory) we let |
| 5463 | // it fault and print an informative termination message. This doesn't |
| 5464 | // happen if the program catches the signal, though, which is bad. If we |
| 5465 | // had two A bits (for readability and writability) that were completely |
| 5466 | // distinct from V bits, then we could handle all this properly. |
| 5467 | VG_(track_change_mem_mprotect) ( NULL ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5468 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 5469 | VG_(track_die_mem_stack_signal)( MC_(make_mem_noaccess) ); |
| 5470 | VG_(track_die_mem_brk) ( MC_(make_mem_noaccess) ); |
| 5471 | VG_(track_die_mem_munmap) ( MC_(make_mem_noaccess) ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5472 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5473 | /* Defer the specification of the new_mem_stack functions to the |
| 5474 | post_clo_init function, since we need to first parse the command |
| 5475 | line before deciding which set to use. */ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5476 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5477 | # ifdef PERF_FAST_STACK |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5478 | VG_(track_die_mem_stack_4) ( mc_die_mem_stack_4 ); |
| 5479 | VG_(track_die_mem_stack_8) ( mc_die_mem_stack_8 ); |
| 5480 | VG_(track_die_mem_stack_12) ( mc_die_mem_stack_12 ); |
| 5481 | VG_(track_die_mem_stack_16) ( mc_die_mem_stack_16 ); |
| 5482 | VG_(track_die_mem_stack_32) ( mc_die_mem_stack_32 ); |
| 5483 | VG_(track_die_mem_stack_112) ( mc_die_mem_stack_112 ); |
| 5484 | VG_(track_die_mem_stack_128) ( mc_die_mem_stack_128 ); |
| 5485 | VG_(track_die_mem_stack_144) ( mc_die_mem_stack_144 ); |
| 5486 | VG_(track_die_mem_stack_160) ( mc_die_mem_stack_160 ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5487 | # endif |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5488 | VG_(track_die_mem_stack) ( mc_die_mem_stack ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5489 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 5490 | VG_(track_ban_mem_stack) ( MC_(make_mem_noaccess) ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5491 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 5492 | VG_(track_pre_mem_read) ( check_mem_is_defined ); |
| 5493 | VG_(track_pre_mem_read_asciiz) ( check_mem_is_defined_asciiz ); |
| 5494 | VG_(track_pre_mem_write) ( check_mem_is_addressable ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5495 | VG_(track_post_mem_write) ( mc_post_mem_write ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5496 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5497 | if (MC_(clo_mc_level) >= 2) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5498 | VG_(track_pre_reg_read) ( mc_pre_reg_read ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5499 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5500 | VG_(track_post_reg_write) ( mc_post_reg_write ); |
| 5501 | VG_(track_post_reg_write_clientcall_return)( mc_post_reg_write_clientcall ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5502 | |
| 5503 | init_shadow_memory(); |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 5504 | MC_(malloc_list) = VG_(HT_construct)( "MC_(malloc_list)" ); |
| 5505 | MC_(mempool_list) = VG_(HT_construct)( "MC_(mempool_list)" ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5506 | init_prof_mem(); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5507 | |
| 5508 | tl_assert( mc_expensive_sanity_check() ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5509 | |
| 5510 | // {LOADV,STOREV}[8421] will all fail horribly if this isn't true. |
| 5511 | tl_assert(sizeof(UWord) == sizeof(Addr)); |
sewardj | 05a4673 | 2006-10-17 01:28:10 +0000 | [diff] [blame] | 5512 | // Call me paranoid. I don't care. |
| 5513 | tl_assert(sizeof(void*) == sizeof(Addr)); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5514 | |
| 5515 | // BYTES_PER_SEC_VBIT_NODE must be a power of two. |
| 5516 | tl_assert(-1 != VG_(log2)(BYTES_PER_SEC_VBIT_NODE)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5517 | |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 5518 | /* This is small. Always initialise it. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5519 | init_nia_to_ecu_cache(); |
sewardj | 7244e71 | 2008-05-02 12:35:48 +0000 | [diff] [blame] | 5520 | |
sewardj | 9d624d1 | 2008-05-02 13:35:29 +0000 | [diff] [blame] | 5521 | /* We can't initialise ocache/ocacheL2 yet, since we don't know if |
| 5522 | we need to, since the command line args haven't been processed |
| 5523 | yet. Hence defer it to mc_post_clo_init. */ |
| 5524 | tl_assert(ocache == NULL); |
| 5525 | tl_assert(ocacheL2 == NULL); |
| 5526 | |
sewardj | 7244e71 | 2008-05-02 12:35:48 +0000 | [diff] [blame] | 5527 | /* Check some important stuff. See extensive comments above |
| 5528 | re UNALIGNED_OR_HIGH for background. */ |
| 5529 | # if VG_WORDSIZE == 4 |
| 5530 | tl_assert(sizeof(void*) == 4); |
| 5531 | tl_assert(sizeof(Addr) == 4); |
| 5532 | tl_assert(sizeof(UWord) == 4); |
| 5533 | tl_assert(sizeof(Word) == 4); |
| 5534 | tl_assert(MAX_PRIMARY_ADDRESS == 0xFFFFFFFFUL); |
| 5535 | tl_assert(MASK(1) == 0UL); |
| 5536 | tl_assert(MASK(2) == 1UL); |
| 5537 | tl_assert(MASK(4) == 3UL); |
| 5538 | tl_assert(MASK(8) == 7UL); |
| 5539 | # else |
| 5540 | tl_assert(VG_WORDSIZE == 8); |
| 5541 | tl_assert(sizeof(void*) == 8); |
| 5542 | tl_assert(sizeof(Addr) == 8); |
| 5543 | tl_assert(sizeof(UWord) == 8); |
| 5544 | tl_assert(sizeof(Word) == 8); |
| 5545 | tl_assert(MAX_PRIMARY_ADDRESS == 0x7FFFFFFFFULL); |
| 5546 | tl_assert(MASK(1) == 0xFFFFFFF800000000ULL); |
| 5547 | tl_assert(MASK(2) == 0xFFFFFFF800000001ULL); |
| 5548 | tl_assert(MASK(4) == 0xFFFFFFF800000003ULL); |
| 5549 | tl_assert(MASK(8) == 0xFFFFFFF800000007ULL); |
| 5550 | # endif |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 5553 | VG_DETERMINE_INTERFACE_VERSION(mc_pre_clo_init) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 5554 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5555 | /*--------------------------------------------------------------------*/ |
sewardj | 7ce7166 | 2008-05-02 10:33:15 +0000 | [diff] [blame] | 5556 | /*--- end mc_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5557 | /*--------------------------------------------------------------------*/ |