sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 3 | /*--- The address space manager. pub_core_aspacemgr.h ---*/ |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
| 10 | Copyright (C) 2000-2005 Julian Seward |
| 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
| 31 | #ifndef __PUB_CORE_ASPACEMGR_H |
| 32 | #define __PUB_CORE_ASPACEMGR_H |
| 33 | |
| 34 | //-------------------------------------------------------------------- |
| 35 | // PURPOSE: This module deals with management of the entire process |
| 36 | // address space. Almost everything depends upon it, including dynamic |
| 37 | // memory management. Hence this module is almost completely |
| 38 | // standalone; the only module it uses is m_debuglog. DO NOT CHANGE |
| 39 | // THIS. |
| 40 | //-------------------------------------------------------------------- |
| 41 | |
njn | 4802b38 | 2005-06-11 04:58:29 +0000 | [diff] [blame] | 42 | #include "pub_tool_aspacemgr.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 43 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 44 | //-------------------------------------------------------------- |
| 45 | // Definition of address-space segments |
| 46 | |
| 47 | /* types SegKind, ShrinkMode and NSegment are described in |
| 48 | the tool-visible header file, not here. */ |
| 49 | |
| 50 | |
| 51 | //-------------------------------------------------------------- |
| 52 | // Initialisation |
| 53 | |
| 54 | /* Initialise the address space manager, setting up the initial |
| 55 | segment list, and reading /proc/self/maps into it. This must |
| 56 | be called before any other function. |
| 57 | |
| 58 | Takes a pointer to the SP at the time V gained control. This is |
| 59 | taken to be the highest usable address (more or less). Based on |
| 60 | that (and general consultation of tea leaves, etc) return a |
| 61 | suggested end address for the client's stack. */ |
| 62 | extern Addr VG_(am_startup) ( Addr sp_at_startup ); |
| 63 | |
| 64 | |
| 65 | //-------------------------------------------------------------- |
| 66 | // Querying current status |
| 67 | |
| 68 | /* Finds the segment containing 'a'. Only returns file/anon/resvn |
| 69 | segments. */ |
| 70 | // Is in tool-visible header file. |
| 71 | // extern NSegment* VG_(am_find_nsegment) ( Addr a ); |
| 72 | |
| 73 | /* Find the next segment along from 'here', if it is a file/anon/resvn |
| 74 | segment. */ |
| 75 | extern NSegment* VG_(am_next_nsegment) ( NSegment* here, Bool fwds ); |
| 76 | |
| 77 | /* Is the area [start .. start+len-1] validly accessible by the |
| 78 | client with at least the permissions 'prot' ? To find out |
| 79 | simply if said area merely belongs to the client, pass |
| 80 | VKI_PROT_NONE as 'prot'. Will return False if any part of the |
| 81 | area does not belong to the client or does not have at least |
| 82 | the stated permissions. */ |
| 83 | // Is in tool-visible header file. |
| 84 | // extern Bool VG_(am_is_valid_for_client) |
| 85 | // ( Addr start, SizeT len, UInt prot ); |
| 86 | |
| 87 | /* Variant of VG_(am_is_valid_for_client) which allows free areas to |
| 88 | be consider part of the client's addressable space. It also |
| 89 | considers reservations to be allowable, since from the client's |
| 90 | point of view they don't exist. */ |
| 91 | extern Bool VG_(am_is_valid_for_client_or_free_or_resvn) |
| 92 | ( Addr start, SizeT len, UInt prot ); |
| 93 | |
| 94 | /* Trivial fn: return the total amount of space in anonymous mappings, |
| 95 | both for V and the client. Is used for printing stats in |
| 96 | out-of-memory messages. */ |
| 97 | extern ULong VG_(am_get_anonsize_total)( void ); |
| 98 | |
| 99 | /* Show the segment array on the debug log, at given loglevel. */ |
| 100 | extern void VG_(am_show_nsegments) ( Int logLevel, HChar* who ); |
| 101 | |
| 102 | /* Get the filename corresponding to this segment, if known and if it |
| 103 | has one. The returned name's storage cannot be assumed to be |
| 104 | persistent, so the caller should immediately copy the name |
| 105 | elsewhere. */ |
| 106 | extern HChar* VG_(am_get_filename)( NSegment* ); |
| 107 | |
| 108 | /* VG_(am_get_segment_starts) is also part of this section, but its |
| 109 | prototype is tool-visible, hence not in this header file. */ |
| 110 | |
| 111 | /* Sanity check: check that Valgrind and the kernel agree on the |
| 112 | address space layout. Prints offending segments and call point if |
| 113 | a discrepancy is detected, but does not abort the system. Returned |
| 114 | Bool is False if a discrepancy was found. */ |
| 115 | |
| 116 | extern Bool VG_(am_do_sync_check) ( const HChar* fn, |
| 117 | const HChar* file, Int line ); |
| 118 | |
| 119 | |
| 120 | //-------------------------------------------------------------- |
| 121 | // Functions pertaining to the central query-notify mechanism |
| 122 | // used to handle mmap/munmap/mprotect resulting from client |
| 123 | // syscalls. |
| 124 | |
| 125 | /* Describes a request for VG_(am_get_advisory). */ |
| 126 | typedef |
| 127 | struct { |
| 128 | enum { MFixed, MHint, MAny } rkind; |
| 129 | Addr start; |
| 130 | Addr len; |
| 131 | } |
| 132 | MapRequest; |
| 133 | |
| 134 | /* Query aspacem to ask where a mapping should go. On success, the |
| 135 | advised placement is returned, and *ok is set to True. On failure, |
| 136 | zero is returned and *ok is set to False. Note that *ok must be |
| 137 | consulted by the caller to establish success or failure; that |
| 138 | cannot be established reliably from the returned value. If *ok is |
| 139 | set to False, it means aspacem has vetoed the mapping, and so the |
| 140 | caller should not proceed with it. */ |
| 141 | extern Addr VG_(am_get_advisory) |
| 142 | ( MapRequest* req, Bool forClient, /*OUT*/Bool* ok ); |
| 143 | |
| 144 | /* Convenience wrapper for VG_(am_get_advisory) for client floating or |
| 145 | fixed requests. If start is zero, a floating request is issued; if |
| 146 | nonzero, a fixed request at that address is issued. Same comments |
| 147 | about return values apply. */ |
| 148 | extern Addr VG_(am_get_advisory_client_simple) |
| 149 | ( Addr start, SizeT len, /*OUT*/Bool* ok ); |
| 150 | |
| 151 | /* Notifies aspacem that the client completed an mmap successfully. |
| 152 | The segment array is updated accordingly. If the returned Bool is |
| 153 | True, the caller should immediately discard translations from the |
| 154 | specified address range. */ |
| 155 | extern Bool VG_(am_notify_client_mmap) |
sewardj | 274461d | 2005-10-02 17:01:41 +0000 | [diff] [blame] | 156 | ( Addr a, SizeT len, UInt prot, UInt flags, Int fd, Off64T offset ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 157 | |
tom | 1340c35 | 2005-10-04 15:59:54 +0000 | [diff] [blame] | 158 | /* Notifies aspacem that the client completed a shmat successfully. |
| 159 | The segment array is updated accordingly. If the returned Bool is |
| 160 | True, the caller should immediately discard translations from the |
| 161 | specified address range. */ |
| 162 | extern Bool VG_(am_notify_client_shmat)( Addr a, SizeT len, UInt prot ); |
| 163 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 164 | /* Notifies aspacem that an mprotect was completed successfully. The |
| 165 | segment array is updated accordingly. Note, as with |
| 166 | VG_(am_notify_munmap), it is not the job of this function to reject |
| 167 | stupid mprotects, for example the client doing mprotect of |
| 168 | non-client areas. Such requests should be intercepted earlier, by |
| 169 | the syscall wrapper for mprotect. This function merely records |
| 170 | whatever it is told. If the returned Bool is True, the caller |
| 171 | should immediately discard translations from the specified address |
| 172 | range. */ |
| 173 | extern Bool VG_(am_notify_mprotect)( Addr start, SizeT len, UInt prot ); |
| 174 | |
| 175 | /* Notifies aspacem that an munmap completed successfully. The |
| 176 | segment array is updated accordingly. As with |
| 177 | VG_(am_notify_munmap), we merely record the given info, and don't |
| 178 | check it for sensibleness. If the returned Bool is True, the |
| 179 | caller should immediately discard translations from the specified |
| 180 | address range. */ |
| 181 | extern Bool VG_(am_notify_munmap)( Addr start, SizeT len ); |
| 182 | |
| 183 | |
| 184 | /* Hand a raw mmap to the kernel, without aspacem updating the segment |
| 185 | array. THIS FUNCTION IS DANGEROUS -- it will cause aspacem's view |
| 186 | of the address space to diverge from that of the kernel. DO NOT |
| 187 | USE IT UNLESS YOU UNDERSTAND the request-notify model used by |
| 188 | aspacem. In short, DO NOT USE THIS FUNCTION. */ |
| 189 | extern SysRes VG_(am_do_mmap_NO_NOTIFY) |
sewardj | 274461d | 2005-10-02 17:01:41 +0000 | [diff] [blame] | 190 | ( Addr start, SizeT length, UInt prot, UInt flags, UInt fd, Off64T offset); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 191 | |
| 192 | |
| 193 | //-------------------------------------------------------------- |
| 194 | // Dealing with mappings which do not arise directly from the |
| 195 | // simulation of the client. These are typically used for |
| 196 | // loading the client and building its stack/data segment, before |
| 197 | // execution begins. Also for V's own administrative use. |
| 198 | |
| 199 | /* --- --- --- map, unmap, protect --- --- --- */ |
| 200 | |
| 201 | /* Map a file at a fixed address for the client, and update the |
| 202 | segment array accordingly. */ |
| 203 | extern SysRes VG_(am_mmap_file_fixed_client) |
sewardj | 274461d | 2005-10-02 17:01:41 +0000 | [diff] [blame] | 204 | ( Addr start, SizeT length, UInt prot, Int fd, Off64T offset ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 205 | |
| 206 | /* Map anonymously at a fixed address for the client, and update |
| 207 | the segment array accordingly. */ |
| 208 | extern SysRes VG_(am_mmap_anon_fixed_client) |
| 209 | ( Addr start, SizeT length, UInt prot ); |
| 210 | |
| 211 | /* Map anonymously at an unconstrained address for the client, and |
| 212 | update the segment array accordingly. */ |
| 213 | extern SysRes VG_(am_mmap_anon_float_client) ( SizeT length, Int prot ); |
| 214 | |
| 215 | /* Map anonymously at an unconstrained address for V, and update the |
| 216 | segment array accordingly. This is fundamentally how V allocates |
| 217 | itself more address space when needed. */ |
| 218 | extern SysRes VG_(am_mmap_anon_float_valgrind)( SizeT cszB ); |
| 219 | |
| 220 | /* Map a file at an unconstrained address for V, and update the |
| 221 | segment array accordingly. This is used by V for transiently |
| 222 | mapping in object files to read their debug info. */ |
| 223 | extern SysRes VG_(am_mmap_file_float_valgrind) |
sewardj | 274461d | 2005-10-02 17:01:41 +0000 | [diff] [blame] | 224 | ( SizeT length, UInt prot, Int fd, Off64T offset ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 225 | |
| 226 | /* Unmap the given address range and update the segment array |
| 227 | accordingly. This fails if the range isn't valid for the client. |
| 228 | If *need_discard is True after a successful return, the caller |
| 229 | should immediately discard translations from the specified address |
| 230 | range. */ |
| 231 | extern SysRes VG_(am_munmap_client)( /*OUT*/Bool* need_discard, |
| 232 | Addr start, SizeT length ); |
| 233 | |
| 234 | /* Unmap the given address range and update the segment array |
| 235 | accordingly. This fails if the range isn't valid for valgrind. */ |
| 236 | extern SysRes VG_(am_munmap_valgrind)( Addr start, SizeT length ); |
| 237 | |
| 238 | /* Let (start,len) denote an area within a single Valgrind-owned |
| 239 | segment (anon or file). Change the ownership of [start, start+len) |
| 240 | to the client instead. Fails if (start,len) does not denote a |
| 241 | suitable segment. */ |
| 242 | extern Bool VG_(am_change_ownership_v_to_c)( Addr start, SizeT len ); |
| 243 | |
| 244 | /* --- --- --- reservations --- --- --- */ |
| 245 | |
| 246 | /* Create a reservation from START .. START+LENGTH-1, with the given |
| 247 | ShrinkMode. When checking whether the reservation can be created, |
| 248 | also ensure that at least abs(EXTRA) extra free bytes will remain |
| 249 | above (> 0) or below (< 0) the reservation. |
| 250 | |
| 251 | The reservation will only be created if it, plus the extra-zone, |
| 252 | falls entirely within a single free segment. The returned Bool |
| 253 | indicates whether the creation succeeded. */ |
| 254 | extern Bool VG_(am_create_reservation) |
| 255 | ( Addr start, SizeT length, ShrinkMode smode, SSizeT extra ); |
| 256 | |
| 257 | /* Let SEG be an anonymous client mapping. This fn extends the |
| 258 | mapping by DELTA bytes, taking the space from a reservation section |
| 259 | which must be adjacent. If DELTA is positive, the segment is |
| 260 | extended forwards in the address space, and the reservation must be |
| 261 | the next one along. If DELTA is negative, the segment is extended |
| 262 | backwards in the address space and the reservation must be the |
sewardj | 6684d2a | 2005-09-28 01:46:31 +0000 | [diff] [blame] | 263 | previous one. DELTA must be page aligned. abs(DELTA) must not |
| 264 | exceed the size of the reservation segment minus one page, that is, |
| 265 | the reservation segment after the operation must be at least one |
| 266 | page long. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 267 | extern Bool VG_(am_extend_into_adjacent_reservation_client) |
| 268 | ( NSegment* seg, SSizeT delta ); |
| 269 | |
| 270 | /* --- --- --- resizing/move a mapping --- --- --- */ |
| 271 | |
| 272 | /* Let SEG be a client mapping (anonymous or file). This fn extends |
| 273 | the mapping forwards only by DELTA bytes, and trashes whatever was |
| 274 | in the new area. Fails if SEG is not a single client mapping or if |
| 275 | the new area is not accessible to the client. Fails if DELTA is |
| 276 | not page aligned. *seg is invalid after a successful return. If |
| 277 | *need_discard is True after a successful return, the caller should |
| 278 | immediately discard translations from the new area. */ |
| 279 | extern Bool VG_(am_extend_map_client)( /*OUT*/Bool* need_discard, |
| 280 | NSegment* seg, SizeT delta ); |
| 281 | |
| 282 | /* Remap the old address range to the new address range. Fails if any |
| 283 | parameter is not page aligned, if the either size is zero, if any |
| 284 | wraparound is implied, if the old address range does not fall |
| 285 | entirely within a single segment, if the new address range overlaps |
| 286 | with the old one, or if the old address range is not a valid client |
| 287 | mapping. If *need_discard is True after a successful return, the |
| 288 | caller should immediately discard translations from both specified |
| 289 | address ranges. */ |
| 290 | extern Bool VG_(am_relocate_nooverlap_client)( /*OUT*/Bool* need_discard, |
| 291 | Addr old_addr, SizeT old_len, |
| 292 | Addr new_addr, SizeT new_len ); |
| 293 | |
| 294 | //-------------------------------------------------------------- |
| 295 | // Valgrind (non-client) thread stacks. V itself runs on such |
| 296 | // stacks. The address space manager provides and suitably |
| 297 | // protects such stacks. |
| 298 | |
| 299 | #define VG_STACK_GUARD_SZB 8192 // 2 pages |
| 300 | #define VG_STACK_ACTIVE_SZB 65536 // 16 pages |
| 301 | |
| 302 | typedef |
| 303 | struct { |
| 304 | HChar bytes[VG_STACK_GUARD_SZB |
| 305 | + VG_STACK_ACTIVE_SZB |
| 306 | + VG_STACK_GUARD_SZB]; |
| 307 | } |
| 308 | VgStack; |
| 309 | |
| 310 | |
| 311 | /* Allocate and initialise a VgStack (anonymous client space). |
| 312 | Protect the stack active area and the guard areas appropriately. |
| 313 | Returns NULL on failure, else the address of the bottom of the |
| 314 | stack. On success, also sets *initial_sp to what the stack pointer |
| 315 | should be set to. */ |
| 316 | |
| 317 | extern VgStack* VG_(am_alloc_VgStack)( /*OUT*/Addr* initial_sp ); |
| 318 | |
| 319 | /* Figure out how many bytes of the stack's active area have not |
| 320 | been used. Used for estimating if we are close to overflowing it. */ |
| 321 | |
| 322 | extern Int VG_(am_get_VgStack_unused_szB)( VgStack* stack ); |
| 323 | |
njn | e3f0635 | 2005-06-01 03:48:33 +0000 | [diff] [blame] | 324 | |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 325 | #endif // __PUB_CORE_ASPACEMGR_H |
| 326 | |
| 327 | /*--------------------------------------------------------------------*/ |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 328 | /*--- end ---*/ |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 329 | /*--------------------------------------------------------------------*/ |