blob: b6ae45ece53701642948cb597610d910a108dd1e [file] [log] [blame]
sewardj55f9d1a2005-04-25 11:11:44 +00001
2/*--------------------------------------------------------------------*/
njn43b9a8a2005-05-10 04:37:01 +00003/*--- The address space manager. pub_core_aspacemgr.h ---*/
sewardj55f9d1a2005-04-25 11:11:44 +00004/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj4d474d02008-02-11 11:34:59 +000010 Copyright (C) 2000-2008 Julian Seward
sewardj55f9d1a2005-04-25 11:11:44 +000011 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
njn4802b382005-06-11 04:58:29 +000042#include "pub_tool_aspacemgr.h"
sewardj55f9d1a2005-04-25 11:11:44 +000043
sewardj45f4e7c2005-09-27 19:20:21 +000044//--------------------------------------------------------------
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. */
62extern 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
sewardjfa2a2462006-10-17 01:30:07 +000069 segments. This returns a 'NSegment const *' - a pointer to
70 readonly data. */
sewardj45f4e7c2005-09-27 19:20:21 +000071// Is in tool-visible header file.
sewardjfa2a2462006-10-17 01:30:07 +000072// extern NSegment const * VG_(am_find_nsegment) ( Addr a );
sewardj45f4e7c2005-09-27 19:20:21 +000073
74/* Find the next segment along from 'here', if it is a file/anon/resvn
75 segment. */
sewardjfa2a2462006-10-17 01:30:07 +000076extern NSegment const* VG_(am_next_nsegment) ( NSegment* here, Bool fwds );
sewardj45f4e7c2005-09-27 19:20:21 +000077
78/* Is the area [start .. start+len-1] validly accessible by the
79 client with at least the permissions 'prot' ? To find out
80 simply if said area merely belongs to the client, pass
81 VKI_PROT_NONE as 'prot'. Will return False if any part of the
82 area does not belong to the client or does not have at least
83 the stated permissions. */
84// Is in tool-visible header file.
85// extern Bool VG_(am_is_valid_for_client)
86// ( Addr start, SizeT len, UInt prot );
87
88/* Variant of VG_(am_is_valid_for_client) which allows free areas to
89 be consider part of the client's addressable space. It also
90 considers reservations to be allowable, since from the client's
91 point of view they don't exist. */
92extern Bool VG_(am_is_valid_for_client_or_free_or_resvn)
93 ( Addr start, SizeT len, UInt prot );
94
95/* Trivial fn: return the total amount of space in anonymous mappings,
96 both for V and the client. Is used for printing stats in
97 out-of-memory messages. */
98extern ULong VG_(am_get_anonsize_total)( void );
99
100/* Show the segment array on the debug log, at given loglevel. */
101extern void VG_(am_show_nsegments) ( Int logLevel, HChar* who );
102
103/* Get the filename corresponding to this segment, if known and if it
104 has one. The returned name's storage cannot be assumed to be
105 persistent, so the caller should immediately copy the name
sewardj7cf4e6b2008-05-01 20:24:26 +0000106 elsewhere. This may return NULL if the file name is not known or
107 for arbitrary other implementation-dependent reasons, so callers
108 need to be able to handle a NULL return value. */
tombcaf0472005-11-16 00:11:14 +0000109// Is in tool-visible header file.
110// extern HChar* VG_(am_get_filename)( NSegment* );
sewardj45f4e7c2005-09-27 19:20:21 +0000111
112/* VG_(am_get_segment_starts) is also part of this section, but its
113 prototype is tool-visible, hence not in this header file. */
114
115/* Sanity check: check that Valgrind and the kernel agree on the
116 address space layout. Prints offending segments and call point if
117 a discrepancy is detected, but does not abort the system. Returned
118 Bool is False if a discrepancy was found. */
119
120extern Bool VG_(am_do_sync_check) ( const HChar* fn,
121 const HChar* file, Int line );
122
123
124//--------------------------------------------------------------
125// Functions pertaining to the central query-notify mechanism
126// used to handle mmap/munmap/mprotect resulting from client
127// syscalls.
128
129/* Describes a request for VG_(am_get_advisory). */
130typedef
131 struct {
132 enum { MFixed, MHint, MAny } rkind;
133 Addr start;
134 Addr len;
135 }
136 MapRequest;
137
138/* Query aspacem to ask where a mapping should go. On success, the
139 advised placement is returned, and *ok is set to True. On failure,
140 zero is returned and *ok is set to False. Note that *ok must be
141 consulted by the caller to establish success or failure; that
142 cannot be established reliably from the returned value. If *ok is
143 set to False, it means aspacem has vetoed the mapping, and so the
144 caller should not proceed with it. */
145extern Addr VG_(am_get_advisory)
146 ( MapRequest* req, Bool forClient, /*OUT*/Bool* ok );
147
148/* Convenience wrapper for VG_(am_get_advisory) for client floating or
149 fixed requests. If start is zero, a floating request is issued; if
150 nonzero, a fixed request at that address is issued. Same comments
151 about return values apply. */
152extern Addr VG_(am_get_advisory_client_simple)
153 ( Addr start, SizeT len, /*OUT*/Bool* ok );
154
155/* Notifies aspacem that the client completed an mmap successfully.
156 The segment array is updated accordingly. If the returned Bool is
157 True, the caller should immediately discard translations from the
158 specified address range. */
159extern Bool VG_(am_notify_client_mmap)
sewardj274461d2005-10-02 17:01:41 +0000160 ( Addr a, SizeT len, UInt prot, UInt flags, Int fd, Off64T offset );
sewardj45f4e7c2005-09-27 19:20:21 +0000161
tom1340c352005-10-04 15:59:54 +0000162/* Notifies aspacem that the client completed a shmat successfully.
163 The segment array is updated accordingly. If the returned Bool is
164 True, the caller should immediately discard translations from the
165 specified address range. */
166extern Bool VG_(am_notify_client_shmat)( Addr a, SizeT len, UInt prot );
167
sewardj45f4e7c2005-09-27 19:20:21 +0000168/* Notifies aspacem that an mprotect was completed successfully. The
169 segment array is updated accordingly. Note, as with
170 VG_(am_notify_munmap), it is not the job of this function to reject
171 stupid mprotects, for example the client doing mprotect of
172 non-client areas. Such requests should be intercepted earlier, by
173 the syscall wrapper for mprotect. This function merely records
174 whatever it is told. If the returned Bool is True, the caller
175 should immediately discard translations from the specified address
176 range. */
177extern Bool VG_(am_notify_mprotect)( Addr start, SizeT len, UInt prot );
178
179/* Notifies aspacem that an munmap completed successfully. The
180 segment array is updated accordingly. As with
181 VG_(am_notify_munmap), we merely record the given info, and don't
182 check it for sensibleness. If the returned Bool is True, the
183 caller should immediately discard translations from the specified
184 address range. */
185extern Bool VG_(am_notify_munmap)( Addr start, SizeT len );
186
sewardj45f4e7c2005-09-27 19:20:21 +0000187/* Hand a raw mmap to the kernel, without aspacem updating the segment
188 array. THIS FUNCTION IS DANGEROUS -- it will cause aspacem's view
189 of the address space to diverge from that of the kernel. DO NOT
190 USE IT UNLESS YOU UNDERSTAND the request-notify model used by
191 aspacem. In short, DO NOT USE THIS FUNCTION. */
192extern SysRes VG_(am_do_mmap_NO_NOTIFY)
sewardj274461d2005-10-02 17:01:41 +0000193 ( Addr start, SizeT length, UInt prot, UInt flags, UInt fd, Off64T offset);
sewardj45f4e7c2005-09-27 19:20:21 +0000194
195
196//--------------------------------------------------------------
sewardjfa2a2462006-10-17 01:30:07 +0000197// Functions pertaining to AIX5-specific notifications.
198
199/* Describes followup actions that need to be done following a call to
200 VG_(am_aix5_reread_procmap). When acquire==True, the specified
201 code and data segments have been mapped into the process, and so
202 m_debuginfo needs to read info for it; also m_redir needs to know,
203 and the tool needs to be told. When acquire==False, the specified
204 segments have been unloaded and m_debuginfo, m_redir and the tool
205 (and m_transtab?) need to notified appropriately. */
206typedef
207 struct {
208 Addr code_start;
209 Word code_len;
210 Addr data_start;
211 Word data_len;
212 UChar* file_name;
213 UChar* mem_name;
214 Bool is_mainexe;
215 Bool acquire;
216 }
217 AixCodeSegChange;
218
219/* Tell aspacem that /proc/<pid>/map may have changed (eg following
220 __loadx) and so it should be re-read, and the code/data segment
221 list updated accordingly. The resulting array of AixCodeChangeSeg
222 directives are written to 'directives', and the number of entries
223 to *ndirectives. */
224extern void VG_(am_aix5_reread_procmap)
225 ( /*OUT*/AixCodeSegChange* directives, /*OUT*/Int* ndirectives );
226
227/* Find out the size of the AixCodeSegChange that must be
228 presented to VG_(am_aix5_reread_procmap). */
229extern Int VG_(am_aix5_reread_procmap_howmany_directives)(void);
230
231/* Tell aspacem where the initial client stack is, so that it
232 can later produce a faked-up NSegment in response to
233 VG_(am_find_nsegment) for athat address, if asked. */
234extern void VG_(am_aix5_set_initial_client_sp)( Addr );
235
236/* The AIX5 aspacem implementation needs to be told when it is and
237 isn't allowed to use sbrk to allocate memory. Hence: */
238extern Bool VG_(am_aix5_sbrk_allowed);
239
240
241//--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +0000242// Dealing with mappings which do not arise directly from the
243// simulation of the client. These are typically used for
244// loading the client and building its stack/data segment, before
245// execution begins. Also for V's own administrative use.
246
247/* --- --- --- map, unmap, protect --- --- --- */
248
249/* Map a file at a fixed address for the client, and update the
250 segment array accordingly. */
251extern SysRes VG_(am_mmap_file_fixed_client)
sewardj274461d2005-10-02 17:01:41 +0000252 ( Addr start, SizeT length, UInt prot, Int fd, Off64T offset );
sewardj45f4e7c2005-09-27 19:20:21 +0000253
254/* Map anonymously at a fixed address for the client, and update
255 the segment array accordingly. */
256extern SysRes VG_(am_mmap_anon_fixed_client)
257 ( Addr start, SizeT length, UInt prot );
258
sewardjfa2a2462006-10-17 01:30:07 +0000259
sewardj45f4e7c2005-09-27 19:20:21 +0000260/* Map anonymously at an unconstrained address for the client, and
261 update the segment array accordingly. */
262extern SysRes VG_(am_mmap_anon_float_client) ( SizeT length, Int prot );
263
sewardjfa2a2462006-10-17 01:30:07 +0000264/* Similarly, acquire new address space for the client but with
265 considerable restrictions on what can be done with it: (1) the
266 actual protections may exceed those stated in 'prot', (2) the
267 area's protections cannot be later changed using any form of
268 mprotect, and (3) the area cannot be freed using any form of
269 munmap. On Linux this behaves the same as
270 VG_(am_mmap_anon_float_client). On AIX5 this *may* allocate memory
271 by using sbrk, so as to make use of large pages on AIX. */
272extern SysRes VG_(am_sbrk_anon_float_client) ( SizeT length, Int prot );
273
274
sewardj45f4e7c2005-09-27 19:20:21 +0000275/* Map anonymously at an unconstrained address for V, and update the
276 segment array accordingly. This is fundamentally how V allocates
277 itself more address space when needed. */
278extern SysRes VG_(am_mmap_anon_float_valgrind)( SizeT cszB );
279
sewardjfa2a2462006-10-17 01:30:07 +0000280/* Same comments apply as per VG_(am_sbrk_anon_float_client). On
281 Linux this behaves the same as VG_(am_mmap_anon_float_valgrind). */
282extern SysRes VG_(am_sbrk_anon_float_valgrind)( SizeT cszB );
283
284
sewardj45f4e7c2005-09-27 19:20:21 +0000285/* Map a file at an unconstrained address for V, and update the
286 segment array accordingly. This is used by V for transiently
287 mapping in object files to read their debug info. */
288extern SysRes VG_(am_mmap_file_float_valgrind)
sewardj274461d2005-10-02 17:01:41 +0000289 ( SizeT length, UInt prot, Int fd, Off64T offset );
sewardj45f4e7c2005-09-27 19:20:21 +0000290
291/* Unmap the given address range and update the segment array
292 accordingly. This fails if the range isn't valid for the client.
293 If *need_discard is True after a successful return, the caller
294 should immediately discard translations from the specified address
295 range. */
296extern SysRes VG_(am_munmap_client)( /*OUT*/Bool* need_discard,
297 Addr start, SizeT length );
298
sewardj45f4e7c2005-09-27 19:20:21 +0000299/* Let (start,len) denote an area within a single Valgrind-owned
300 segment (anon or file). Change the ownership of [start, start+len)
301 to the client instead. Fails if (start,len) does not denote a
302 suitable segment. */
303extern Bool VG_(am_change_ownership_v_to_c)( Addr start, SizeT len );
304
sewardjfa2a2462006-10-17 01:30:07 +0000305/* 'seg' must be NULL or have been obtained from
306 VG_(am_find_nsegment), and still valid. If non-NULL, and if it
307 denotes a SkAnonC (anonymous client mapping) area, set the .isCH
308 (is-client-heap) flag for that area. Otherwise do nothing.
309 (Bizarre interface so that the same code works for both Linux and
310 AIX and does not impose inefficiencies on the Linux version.) */
311extern void VG_(am_set_segment_isCH_if_SkAnonC)( NSegment* seg );
312
313/* Same idea as VG_(am_set_segment_isCH_if_SkAnonC), except set the
314 segment's hasT bit (has-cached-code) if this is SkFileC or SkAnonC
315 segment. */
316extern void VG_(am_set_segment_hasT_if_SkFileC_or_SkAnonC)( NSegment* );
317
sewardj45f4e7c2005-09-27 19:20:21 +0000318/* --- --- --- reservations --- --- --- */
319
320/* Create a reservation from START .. START+LENGTH-1, with the given
321 ShrinkMode. When checking whether the reservation can be created,
322 also ensure that at least abs(EXTRA) extra free bytes will remain
323 above (> 0) or below (< 0) the reservation.
324
325 The reservation will only be created if it, plus the extra-zone,
326 falls entirely within a single free segment. The returned Bool
327 indicates whether the creation succeeded. */
328extern Bool VG_(am_create_reservation)
329 ( Addr start, SizeT length, ShrinkMode smode, SSizeT extra );
330
331/* Let SEG be an anonymous client mapping. This fn extends the
332 mapping by DELTA bytes, taking the space from a reservation section
333 which must be adjacent. If DELTA is positive, the segment is
334 extended forwards in the address space, and the reservation must be
335 the next one along. If DELTA is negative, the segment is extended
336 backwards in the address space and the reservation must be the
sewardj6684d2a2005-09-28 01:46:31 +0000337 previous one. DELTA must be page aligned. abs(DELTA) must not
338 exceed the size of the reservation segment minus one page, that is,
339 the reservation segment after the operation must be at least one
340 page long. */
sewardj45f4e7c2005-09-27 19:20:21 +0000341extern Bool VG_(am_extend_into_adjacent_reservation_client)
342 ( NSegment* seg, SSizeT delta );
343
344/* --- --- --- resizing/move a mapping --- --- --- */
345
346/* Let SEG be a client mapping (anonymous or file). This fn extends
347 the mapping forwards only by DELTA bytes, and trashes whatever was
348 in the new area. Fails if SEG is not a single client mapping or if
349 the new area is not accessible to the client. Fails if DELTA is
350 not page aligned. *seg is invalid after a successful return. If
351 *need_discard is True after a successful return, the caller should
352 immediately discard translations from the new area. */
353extern Bool VG_(am_extend_map_client)( /*OUT*/Bool* need_discard,
354 NSegment* seg, SizeT delta );
355
356/* Remap the old address range to the new address range. Fails if any
357 parameter is not page aligned, if the either size is zero, if any
358 wraparound is implied, if the old address range does not fall
359 entirely within a single segment, if the new address range overlaps
360 with the old one, or if the old address range is not a valid client
361 mapping. If *need_discard is True after a successful return, the
362 caller should immediately discard translations from both specified
363 address ranges. */
364extern Bool VG_(am_relocate_nooverlap_client)( /*OUT*/Bool* need_discard,
365 Addr old_addr, SizeT old_len,
366 Addr new_addr, SizeT new_len );
367
368//--------------------------------------------------------------
369// Valgrind (non-client) thread stacks. V itself runs on such
370// stacks. The address space manager provides and suitably
371// protects such stacks.
372
sewardje66f2e02006-12-30 17:45:08 +0000373#if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
374# define VG_STACK_GUARD_SZB 65536 // 1 or 16 pages
375# define VG_STACK_ACTIVE_SZB 131072 // 2 or 32 pages
376#else
377# define VG_STACK_GUARD_SZB 8192 // 2 pages
378# define VG_STACK_ACTIVE_SZB 65536 // 16 pages
379#endif
sewardj45f4e7c2005-09-27 19:20:21 +0000380
381typedef
382 struct {
383 HChar bytes[VG_STACK_GUARD_SZB
384 + VG_STACK_ACTIVE_SZB
385 + VG_STACK_GUARD_SZB];
386 }
387 VgStack;
388
389
390/* Allocate and initialise a VgStack (anonymous client space).
391 Protect the stack active area and the guard areas appropriately.
392 Returns NULL on failure, else the address of the bottom of the
393 stack. On success, also sets *initial_sp to what the stack pointer
394 should be set to. */
395
396extern VgStack* VG_(am_alloc_VgStack)( /*OUT*/Addr* initial_sp );
397
398/* Figure out how many bytes of the stack's active area have not
399 been used. Used for estimating if we are close to overflowing it. */
400
401extern Int VG_(am_get_VgStack_unused_szB)( VgStack* stack );
402
njne3f06352005-06-01 03:48:33 +0000403
sewardj55f9d1a2005-04-25 11:11:44 +0000404#endif // __PUB_CORE_ASPACEMGR_H
405
406/*--------------------------------------------------------------------*/
njnaf839f52005-06-23 03:27:57 +0000407/*--- end ---*/
sewardj55f9d1a2005-04-25 11:11:44 +0000408/*--------------------------------------------------------------------*/