1 /* Determine the virtual memory area of a given address.
2 Copyright (C) 2006, 2008-2010 Bruno Haible <bruno@clisp.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* mincore() is a system call that allows to inquire the status of a
19 range of pages of virtual memory. In particular, it allows to inquire
20 whether a page is mapped at all.
21 As of 2006, mincore() is supported by: possible bits:
22 - Linux, since Linux 2.4 and glibc 2.2, 1
23 - Solaris, since Solaris 9, 1
24 - MacOS X, since MacOS X 10.3 (at least), 1
25 - FreeBSD, since FreeBSD 6.0, MINCORE_{INCORE,REFERENCED,MODIFIED}
26 - NetBSD, since NetBSD 3.0 (at least), 1
27 - OpenBSD, since OpenBSD 2.6 (at least), 1
28 However, while the API allows to easily determine the bounds of mapped
29 virtual memory, it does not make it easy the bounds of _unmapped_ virtual
30 memory ranges. We try to work around this, but it may still be slow. */
37 #include <sys/types.h>
40 /* The glibc declaration of mincore() uses 'unsigned char *', whereas the BSD
41 declaration uses 'char *'. */
43 typedef unsigned char pageinfo_t
;
45 typedef char pageinfo_t
;
48 /* Cache for getpagesize(). */
49 static unsigned long pagesize
;
51 /* Initialize pagesize. */
56 pagesize
= getpagesize ();
57 #elif HAVE_SYSCONF_PAGESIZE
58 pagesize
= sysconf (_SC_PAGESIZE
);
64 /* Test whether the page starting at ADDR is among the address range.
65 ADDR must be a multiple of pagesize. */
67 is_mapped (unsigned long addr
)
70 return mincore ((void *) addr
, pagesize
, vec
) >= 0;
73 /* Assuming that the page starting at ADDR is among the address range,
74 return the start of its virtual memory range.
75 ADDR must be a multiple of pagesize. */
77 mapped_range_start (unsigned long addr
)
79 /* Use a moderately sized VEC here, small enough that it fits on the stack
80 (without requiring malloc). */
82 unsigned long stepsize
= sizeof (vec
);
86 unsigned long max_remaining
;
91 max_remaining
= addr
/ pagesize
;
92 if (stepsize
> max_remaining
)
93 stepsize
= max_remaining
;
94 if (mincore ((void *) (addr
- stepsize
* pagesize
),
95 stepsize
* pagesize
, vec
) < 0)
96 /* Time to search in smaller steps. */
98 /* The entire range exists. Continue searching in large steps. */
99 addr
-= stepsize
* pagesize
;
103 unsigned long halfstepsize1
;
104 unsigned long halfstepsize2
;
109 /* Here we know that less than stepsize pages exist starting at addr. */
110 halfstepsize1
= (stepsize
+ 1) / 2;
111 halfstepsize2
= stepsize
/ 2;
112 /* halfstepsize1 + halfstepsize2 = stepsize. */
114 if (mincore ((void *) (addr
- halfstepsize1
* pagesize
),
115 halfstepsize1
* pagesize
, vec
) < 0)
116 stepsize
= halfstepsize1
;
119 addr
-= halfstepsize1
* pagesize
;
120 stepsize
= halfstepsize2
;
125 /* Assuming that the page starting at ADDR is among the address range,
126 return the end of its virtual memory range + 1.
127 ADDR must be a multiple of pagesize. */
129 mapped_range_end (unsigned long addr
)
131 /* Use a moderately sized VEC here, small enough that it fits on the stack
132 (without requiring malloc). */
133 pageinfo_t vec
[1024];
134 unsigned long stepsize
= sizeof (vec
);
139 unsigned long max_remaining
;
141 if (addr
== 0) /* wrapped around? */
144 max_remaining
= (- addr
) / pagesize
;
145 if (stepsize
> max_remaining
)
146 stepsize
= max_remaining
;
147 if (mincore ((void *) addr
, stepsize
* pagesize
, vec
) < 0)
148 /* Time to search in smaller steps. */
150 /* The entire range exists. Continue searching in large steps. */
151 addr
+= stepsize
* pagesize
;
155 unsigned long halfstepsize1
;
156 unsigned long halfstepsize2
;
161 /* Here we know that less than stepsize pages exist starting at addr. */
162 halfstepsize1
= (stepsize
+ 1) / 2;
163 halfstepsize2
= stepsize
/ 2;
164 /* halfstepsize1 + halfstepsize2 = stepsize. */
166 if (mincore ((void *) addr
, halfstepsize1
* pagesize
, vec
) < 0)
167 stepsize
= halfstepsize1
;
170 addr
+= halfstepsize1
* pagesize
;
171 stepsize
= halfstepsize2
;
176 /* Determine whether an address range [ADDR1..ADDR2] is completely unmapped.
177 ADDR1 must be <= ADDR2. */
179 is_unmapped (unsigned long addr1
, unsigned long addr2
)
182 unsigned long stepsize
;
184 /* Round addr1 down. */
185 addr1
= (addr1
/ pagesize
) * pagesize
;
186 /* Round addr2 up and turn it into an exclusive bound. */
187 addr2
= ((addr2
/ pagesize
) + 1) * pagesize
;
189 /* This is slow: mincore() does not provide a way to determine the bounds
190 of the gaps directly. So we have to use mincore() on individual pages
191 over and over again. Only after we've verified that all pages are
192 unmapped, we know that the range is completely unmapped.
193 If we were to traverse the pages from bottom to top or from top to bottom,
194 it would be slow even in the average case. To speed up the search, we
195 exploit the fact that mapped memory ranges are larger than one page on
196 average, therefore we have good chances of hitting a mapped area if we
197 traverse only every second, or only fourth page, etc. This doesn't
198 decrease the worst-case runtime, only the average runtime. */
199 count
= (addr2
- addr1
) / pagesize
;
200 /* We have to test is_mapped (addr1 + i * pagesize) for 0 <= i < count. */
201 for (stepsize
= 1; stepsize
< count
; )
202 stepsize
= 2 * stepsize
;
205 unsigned long addr_stepsize
;
209 stepsize
= stepsize
/ 2;
212 addr_stepsize
= stepsize
* pagesize
;
213 for (i
= stepsize
, addr
= addr1
+ addr_stepsize
;
215 i
+= 2 * stepsize
, addr
+= 2 * addr_stepsize
)
216 /* Here addr = addr1 + i * pagesize. */
217 if (is_mapped (addr
))
223 #if STACK_DIRECTION < 0
225 /* Info about the gap between this VMA and the previous one.
226 addr must be < vma->start. */
228 mincore_is_near_this (unsigned long addr
, struct vma_struct
*vma
)
230 /* vma->start - addr <= (vma->start - vma->prev_end) / 2
231 is mathematically equivalent to
232 vma->prev_end <= 2 * addr - vma->start
233 <==> is_unmapped (2 * addr - vma->start, vma->start - 1).
234 But be careful about overflow: if 2 * addr - vma->start is negative,
235 we consider a tiny "guard page" mapping [0, 0] to be present around
236 NULL; it intersects the range (2 * addr - vma->start, vma->start - 1),
237 therefore return false. */
238 unsigned long testaddr
= addr
- (vma
->start
- addr
);
239 if (testaddr
> addr
) /* overflow? */
241 /* Here testaddr <= addr < vma->start. */
242 return is_unmapped (testaddr
, vma
->start
- 1);
246 #if STACK_DIRECTION > 0
248 /* Info about the gap between this VMA and the next one.
249 addr must be > vma->end - 1. */
251 mincore_is_near_this (unsigned long addr
, struct vma_struct
*vma
)
253 /* addr - vma->end < (vma->next_start - vma->end) / 2
254 is mathematically equivalent to
255 vma->next_start > 2 * addr - vma->end
256 <==> is_unmapped (vma->end, 2 * addr - vma->end).
257 But be careful about overflow: if 2 * addr - vma->end is > ~0UL,
258 we consider a tiny "guard page" mapping [0, 0] to be present around
259 NULL; it intersects the range (vma->end, 2 * addr - vma->end),
260 therefore return false. */
261 unsigned long testaddr
= addr
+ (addr
- vma
->end
);
262 if (testaddr
< addr
) /* overflow? */
264 /* Here vma->end - 1 < addr <= testaddr. */
265 return is_unmapped (vma
->end
, testaddr
);
274 sigsegv_get_vma (unsigned long address
, struct vma_struct
*vma
)
278 address
= (address
/ pagesize
) * pagesize
;
279 vma
->start
= mapped_range_start (address
);
280 vma
->end
= mapped_range_end (address
);
281 vma
->is_near_this
= mincore_is_near_this
;