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 | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 44 | /* Parses /proc/self/maps, calling `record_mapping' for each entry. */ |
| 45 | extern |
| 46 | void VG_(parse_procselfmaps) ( |
| 47 | void (*record_mapping)( Addr addr, SizeT len, UInt prot, |
| 48 | UInt dev, UInt ino, ULong foff, |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame^] | 49 | const UChar *filename ), |
| 50 | void (*record_gap)( Addr addr, SizeT len ) ); |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 51 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame^] | 52 | //-------------------------------------------------------------- |
| 53 | // Definition of address-space segments |
| 54 | |
| 55 | /* types SegKind, ShrinkMode and NSegment are described in |
| 56 | the tool-visible header file, not here. */ |
| 57 | |
| 58 | |
| 59 | //-------------------------------------------------------------- |
| 60 | // Initialisation |
| 61 | |
| 62 | /* Initialise the address space manager, setting up the initial |
| 63 | segment list, and reading /proc/self/maps into it. This must |
| 64 | be called before any other function. |
| 65 | |
| 66 | Takes a pointer to the SP at the time V gained control. This is |
| 67 | taken to be the highest usable address (more or less). Based on |
| 68 | that (and general consultation of tea leaves, etc) return a |
| 69 | suggested end address for the client's stack. */ |
| 70 | extern Addr VG_(am_startup) ( Addr sp_at_startup ); |
| 71 | |
| 72 | |
| 73 | //-------------------------------------------------------------- |
| 74 | // Querying current status |
| 75 | |
| 76 | /* Finds the segment containing 'a'. Only returns file/anon/resvn |
| 77 | segments. */ |
| 78 | // Is in tool-visible header file. |
| 79 | // extern NSegment* VG_(am_find_nsegment) ( Addr a ); |
| 80 | |
| 81 | /* Find the next segment along from 'here', if it is a file/anon/resvn |
| 82 | segment. */ |
| 83 | extern NSegment* VG_(am_next_nsegment) ( NSegment* here, Bool fwds ); |
| 84 | |
| 85 | /* Is the area [start .. start+len-1] validly accessible by the |
| 86 | client with at least the permissions 'prot' ? To find out |
| 87 | simply if said area merely belongs to the client, pass |
| 88 | VKI_PROT_NONE as 'prot'. Will return False if any part of the |
| 89 | area does not belong to the client or does not have at least |
| 90 | the stated permissions. */ |
| 91 | // Is in tool-visible header file. |
| 92 | // extern Bool VG_(am_is_valid_for_client) |
| 93 | // ( Addr start, SizeT len, UInt prot ); |
| 94 | |
| 95 | /* Variant of VG_(am_is_valid_for_client) which allows free areas to |
| 96 | be consider part of the client's addressable space. It also |
| 97 | considers reservations to be allowable, since from the client's |
| 98 | point of view they don't exist. */ |
| 99 | extern Bool VG_(am_is_valid_for_client_or_free_or_resvn) |
| 100 | ( Addr start, SizeT len, UInt prot ); |
| 101 | |
| 102 | /* Trivial fn: return the total amount of space in anonymous mappings, |
| 103 | both for V and the client. Is used for printing stats in |
| 104 | out-of-memory messages. */ |
| 105 | extern ULong VG_(am_get_anonsize_total)( void ); |
| 106 | |
| 107 | /* Show the segment array on the debug log, at given loglevel. */ |
| 108 | extern void VG_(am_show_nsegments) ( Int logLevel, HChar* who ); |
| 109 | |
| 110 | /* Get the filename corresponding to this segment, if known and if it |
| 111 | has one. The returned name's storage cannot be assumed to be |
| 112 | persistent, so the caller should immediately copy the name |
| 113 | elsewhere. */ |
| 114 | extern HChar* VG_(am_get_filename)( NSegment* ); |
| 115 | |
| 116 | /* VG_(am_get_segment_starts) is also part of this section, but its |
| 117 | prototype is tool-visible, hence not in this header file. */ |
| 118 | |
| 119 | /* Sanity check: check that Valgrind and the kernel agree on the |
| 120 | address space layout. Prints offending segments and call point if |
| 121 | a discrepancy is detected, but does not abort the system. Returned |
| 122 | Bool is False if a discrepancy was found. */ |
| 123 | |
| 124 | extern Bool VG_(am_do_sync_check) ( const HChar* fn, |
| 125 | const HChar* file, Int line ); |
| 126 | |
| 127 | |
| 128 | //-------------------------------------------------------------- |
| 129 | // Functions pertaining to the central query-notify mechanism |
| 130 | // used to handle mmap/munmap/mprotect resulting from client |
| 131 | // syscalls. |
| 132 | |
| 133 | /* Describes a request for VG_(am_get_advisory). */ |
| 134 | typedef |
| 135 | struct { |
| 136 | enum { MFixed, MHint, MAny } rkind; |
| 137 | Addr start; |
| 138 | Addr len; |
| 139 | } |
| 140 | MapRequest; |
| 141 | |
| 142 | /* Query aspacem to ask where a mapping should go. On success, the |
| 143 | advised placement is returned, and *ok is set to True. On failure, |
| 144 | zero is returned and *ok is set to False. Note that *ok must be |
| 145 | consulted by the caller to establish success or failure; that |
| 146 | cannot be established reliably from the returned value. If *ok is |
| 147 | set to False, it means aspacem has vetoed the mapping, and so the |
| 148 | caller should not proceed with it. */ |
| 149 | extern Addr VG_(am_get_advisory) |
| 150 | ( MapRequest* req, Bool forClient, /*OUT*/Bool* ok ); |
| 151 | |
| 152 | /* Convenience wrapper for VG_(am_get_advisory) for client floating or |
| 153 | fixed requests. If start is zero, a floating request is issued; if |
| 154 | nonzero, a fixed request at that address is issued. Same comments |
| 155 | about return values apply. */ |
| 156 | extern Addr VG_(am_get_advisory_client_simple) |
| 157 | ( Addr start, SizeT len, /*OUT*/Bool* ok ); |
| 158 | |
| 159 | /* Notifies aspacem that the client completed an mmap successfully. |
| 160 | The segment array is updated accordingly. If the returned Bool is |
| 161 | True, the caller should immediately discard translations from the |
| 162 | specified address range. */ |
| 163 | extern Bool VG_(am_notify_client_mmap) |
| 164 | ( Addr a, SizeT len, UInt prot, UInt flags, Int fd, SizeT offset ); |
| 165 | |
| 166 | /* Notifies aspacem that an mprotect was completed successfully. The |
| 167 | segment array is updated accordingly. Note, as with |
| 168 | VG_(am_notify_munmap), it is not the job of this function to reject |
| 169 | stupid mprotects, for example the client doing mprotect of |
| 170 | non-client areas. Such requests should be intercepted earlier, by |
| 171 | the syscall wrapper for mprotect. This function merely records |
| 172 | whatever it is told. If the returned Bool is True, the caller |
| 173 | should immediately discard translations from the specified address |
| 174 | range. */ |
| 175 | extern Bool VG_(am_notify_mprotect)( Addr start, SizeT len, UInt prot ); |
| 176 | |
| 177 | /* Notifies aspacem that an munmap completed successfully. The |
| 178 | segment array is updated accordingly. As with |
| 179 | VG_(am_notify_munmap), we merely record the given info, and don't |
| 180 | check it for sensibleness. If the returned Bool is True, the |
| 181 | caller should immediately discard translations from the specified |
| 182 | address range. */ |
| 183 | extern Bool VG_(am_notify_munmap)( Addr start, SizeT len ); |
| 184 | |
| 185 | |
| 186 | /* Hand a raw mmap to the kernel, without aspacem updating the segment |
| 187 | array. THIS FUNCTION IS DANGEROUS -- it will cause aspacem's view |
| 188 | of the address space to diverge from that of the kernel. DO NOT |
| 189 | USE IT UNLESS YOU UNDERSTAND the request-notify model used by |
| 190 | aspacem. In short, DO NOT USE THIS FUNCTION. */ |
| 191 | extern SysRes VG_(am_do_mmap_NO_NOTIFY) |
| 192 | ( Addr start, SizeT length, UInt prot, UInt flags, UInt fd, OffT offset); |
| 193 | |
| 194 | |
| 195 | //-------------------------------------------------------------- |
| 196 | // Dealing with mappings which do not arise directly from the |
| 197 | // simulation of the client. These are typically used for |
| 198 | // loading the client and building its stack/data segment, before |
| 199 | // execution begins. Also for V's own administrative use. |
| 200 | |
| 201 | /* --- --- --- map, unmap, protect --- --- --- */ |
| 202 | |
| 203 | /* Map a file at a fixed address for the client, and update the |
| 204 | segment array accordingly. */ |
| 205 | extern SysRes VG_(am_mmap_file_fixed_client) |
| 206 | ( Addr start, SizeT length, UInt prot, Int fd, SizeT offset ); |
| 207 | |
| 208 | /* Map anonymously at a fixed address for the client, and update |
| 209 | the segment array accordingly. */ |
| 210 | extern SysRes VG_(am_mmap_anon_fixed_client) |
| 211 | ( Addr start, SizeT length, UInt prot ); |
| 212 | |
| 213 | /* Map anonymously at an unconstrained address for the client, and |
| 214 | update the segment array accordingly. */ |
| 215 | extern SysRes VG_(am_mmap_anon_float_client) ( SizeT length, Int prot ); |
| 216 | |
| 217 | /* Map anonymously at an unconstrained address for V, and update the |
| 218 | segment array accordingly. This is fundamentally how V allocates |
| 219 | itself more address space when needed. */ |
| 220 | extern SysRes VG_(am_mmap_anon_float_valgrind)( SizeT cszB ); |
| 221 | |
| 222 | /* Map a file at an unconstrained address for V, and update the |
| 223 | segment array accordingly. This is used by V for transiently |
| 224 | mapping in object files to read their debug info. */ |
| 225 | extern SysRes VG_(am_mmap_file_float_valgrind) |
| 226 | ( SizeT length, UInt prot, Int fd, SizeT offset ); |
| 227 | |
| 228 | /* Unmap the given address range and update the segment array |
| 229 | accordingly. This fails if the range isn't valid for the client. |
| 230 | If *need_discard is True after a successful return, the caller |
| 231 | should immediately discard translations from the specified address |
| 232 | range. */ |
| 233 | extern SysRes VG_(am_munmap_client)( /*OUT*/Bool* need_discard, |
| 234 | Addr start, SizeT length ); |
| 235 | |
| 236 | /* Unmap the given address range and update the segment array |
| 237 | accordingly. This fails if the range isn't valid for valgrind. */ |
| 238 | extern SysRes VG_(am_munmap_valgrind)( Addr start, SizeT length ); |
| 239 | |
| 240 | /* Let (start,len) denote an area within a single Valgrind-owned |
| 241 | segment (anon or file). Change the ownership of [start, start+len) |
| 242 | to the client instead. Fails if (start,len) does not denote a |
| 243 | suitable segment. */ |
| 244 | extern Bool VG_(am_change_ownership_v_to_c)( Addr start, SizeT len ); |
| 245 | |
| 246 | /* --- --- --- reservations --- --- --- */ |
| 247 | |
| 248 | /* Create a reservation from START .. START+LENGTH-1, with the given |
| 249 | ShrinkMode. When checking whether the reservation can be created, |
| 250 | also ensure that at least abs(EXTRA) extra free bytes will remain |
| 251 | above (> 0) or below (< 0) the reservation. |
| 252 | |
| 253 | The reservation will only be created if it, plus the extra-zone, |
| 254 | falls entirely within a single free segment. The returned Bool |
| 255 | indicates whether the creation succeeded. */ |
| 256 | extern Bool VG_(am_create_reservation) |
| 257 | ( Addr start, SizeT length, ShrinkMode smode, SSizeT extra ); |
| 258 | |
| 259 | /* Let SEG be an anonymous client mapping. This fn extends the |
| 260 | mapping by DELTA bytes, taking the space from a reservation section |
| 261 | which must be adjacent. If DELTA is positive, the segment is |
| 262 | extended forwards in the address space, and the reservation must be |
| 263 | the next one along. If DELTA is negative, the segment is extended |
| 264 | backwards in the address space and the reservation must be the |
| 265 | previous one. DELTA must be page aligned and must not exceed the |
| 266 | size of the reservation segment. */ |
| 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 | /*--------------------------------------------------------------------*/ |