OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #ifndef WTF_PageAllocator_h | |
32 #define WTF_PageAllocator_h | |
33 | |
34 #include "wtf/Assertions.h" | |
35 #include "wtf/CPU.h" | |
36 #include "wtf/WTFExport.h" | |
37 #include <cstddef> | |
38 #include <stdint.h> | |
39 | |
40 namespace WTF { | |
41 | |
42 #if OS(WIN) | |
43 static const size_t kPageAllocationGranularityShift = 16; // 64KB | |
44 #else | |
45 static const size_t kPageAllocationGranularityShift = 12; // 4KB | |
46 #endif | |
47 static const size_t kPageAllocationGranularity = 1 << kPageAllocationGranularity
Shift; | |
48 static const size_t kPageAllocationGranularityOffsetMask = kPageAllocationGranul
arity - 1; | |
49 static const size_t kPageAllocationGranularityBaseMask = ~kPageAllocationGranula
rityOffsetMask; | |
50 | |
51 // All Blink-supported systems have 4096 sized system pages and can handle | |
52 // permissions and commit / decommit at this granularity. | |
53 static const size_t kSystemPageSize = 4096; | |
54 static const size_t kSystemPageOffsetMask = kSystemPageSize - 1; | |
55 static const size_t kSystemPageBaseMask = ~kSystemPageOffsetMask; | |
56 | |
57 enum PageAccessibilityConfiguration { | |
58 PageAccessible, | |
59 PageInaccessible, | |
60 }; | |
61 | |
62 // Allocate one or more pages. | |
63 // The requested address is just a hint; the actual address returned may | |
64 // differ. The returned address will be aligned at least to align bytes. | |
65 // len is in bytes, and must be a multiple of kPageAllocationGranularity. | |
66 // align is in bytes, and must be a power-of-two multiple of | |
67 // kPageAllocationGranularity. | |
68 // If addr is null, then a suitable and randomized address will be chosen | |
69 // automatically. | |
70 // PageAccessibilityConfiguration controls the permission of the | |
71 // allocated pages. | |
72 // This call will return null if the allocation cannot be satisfied. | |
73 WTF_EXPORT void* allocPages(void* addr, size_t len, size_t align, PageAccessibil
ityConfiguration); | |
74 | |
75 // Free one or more pages. | |
76 // addr and len must match a previous call to allocPages(). | |
77 WTF_EXPORT void freePages(void* addr, size_t len); | |
78 | |
79 // Mark one or more system pages as being inaccessible. | |
80 // Subsequently accessing any address in the range will fault, and the | |
81 // addresses will not be re-used by future allocations. | |
82 // len must be a multiple of kSystemPageSize bytes. | |
83 WTF_EXPORT void setSystemPagesInaccessible(void* addr, size_t len); | |
84 | |
85 // Mark one or more system pages as being accessible. | |
86 // The pages will be readable and writeable. | |
87 // len must be a multiple of kSystemPageSize bytes. | |
88 // The result bool value indicates whether the permission | |
89 // change succeeded or not. You must check the result | |
90 // (in most cases you need to RELEASE_ASSERT that it is | |
91 // true). | |
92 WTF_EXPORT WARN_UNUSED_RETURN bool setSystemPagesAccessible(void* addr, size_t l
en); | |
93 | |
94 // Decommit one or more system pages. Decommitted means that the physical memory | |
95 // is released to the system, but the virtual address space remains reserved. | |
96 // System pages are re-committed by calling recommitSystemPages(). Touching | |
97 // a decommitted page _may_ fault. | |
98 // Clients should not make any assumptions about the contents of decommitted | |
99 // system pages, before or after they write to the page. The only guarantee | |
100 // provided is that the contents of the system page will be deterministic again | |
101 // after recommitting and writing to it. In particlar note that system pages are | |
102 // not guaranteed to be zero-filled upon re-commit. len must be a multiple of | |
103 // kSystemPageSize bytes. | |
104 WTF_EXPORT void decommitSystemPages(void* addr, size_t len); | |
105 | |
106 // Recommit one or more system pages. Decommitted system pages must be | |
107 // recommitted before they are read are written again. | |
108 // Note that this operation may be a no-op on some platforms. | |
109 // len must be a multiple of kSystemPageSize bytes. | |
110 WTF_EXPORT void recommitSystemPages(void* addr, size_t len); | |
111 | |
112 // Discard one or more system pages. Discarding is a hint to the system that | |
113 // the page is no longer required. The hint may: | |
114 // - Do nothing. | |
115 // - Discard the page immediately, freeing up physical pages. | |
116 // - Discard the page at some time in the future in response to memory pressure. | |
117 // Only committed pages should be discarded. Discarding a page does not | |
118 // decommit it, and it is valid to discard an already-discarded page. | |
119 // A read or write to a discarded page will not fault. | |
120 // Reading from a discarded page may return the original page content, or a | |
121 // page full of zeroes. | |
122 // Writing to a discarded page is the only guaranteed way to tell the system | |
123 // that the page is required again. Once written to, the content of the page is | |
124 // guaranteed stable once more. After being written to, the page content may be | |
125 // based on the original page content, or a page of zeroes. | |
126 // len must be a multiple of kSystemPageSize bytes. | |
127 WTF_EXPORT void discardSystemPages(void* addr, size_t len); | |
128 | |
129 WTF_EXPORT ALWAYS_INLINE uintptr_t roundUpToSystemPage(uintptr_t address) | |
130 { | |
131 return (address + kSystemPageOffsetMask) & kSystemPageBaseMask; | |
132 } | |
133 | |
134 WTF_EXPORT ALWAYS_INLINE uintptr_t roundDownToSystemPage(uintptr_t address) | |
135 { | |
136 return address & kSystemPageBaseMask; | |
137 } | |
138 | |
139 // Only allowed inside WTF for investigating WTF::initializeWithoutV8 crashes. | |
140 // Guess, the function fails because of mmap (or VirtualAlloc) failure. | |
141 // The following function returns errno (or GetLastError code) when mmap | |
142 // (or VirtualAlloc) fails. | |
143 uint32_t getAllocPageErrorCode(); | |
144 | |
145 } // namespace WTF | |
146 | |
147 #endif // WTF_PageAllocator_h | |
OLD | NEW |