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 <stdint.h> 38 39 namespace WTF { 40 41 #if OS(WIN) 42 static const size_t kPageAllocationGranularityShift = 16; // 64KB 43 #else 44 static const size_t kPageAllocationGranularityShift = 12; // 4KB 45 #endif 46 static const size_t kPageAllocationGranularity = 1 << kPageAllocationGranularityShift; 47 static const size_t kPageAllocationGranularityOffsetMask = kPageAllocationGranularity - 1; 48 static const size_t kPageAllocationGranularityBaseMask = ~kPageAllocationGranularityOffsetMask; 49 50 // All Blink-supported systems have 4096 sized system pages and can handle 51 // permissions and commit / decommit at this granularity. 52 static const size_t kSystemPageSize = 4096; 53 static const size_t kSystemPageOffsetMask = kSystemPageSize - 1; 54 static const size_t kSystemPageBaseMask = ~kSystemPageOffsetMask; 55 56 // Allocate one or more pages. Addresses in the range will be readable and 57 // writeable but not executable. 58 // The requested address is just a hint; the actual address returned may 59 // differ. The returned address will be aligned at least to align bytes. 60 // len is in bytes, and must be a multiple of kPageAllocationGranularity. 61 // align is in bytes, and must be a power-of-two multiple of 62 // kPageAllocationGranularity. 63 // If addr is null, then a suitable and randomized address will be chosen 64 // automatically. 65 // This call will exit the process if the allocation cannot be satisfied. 66 WTF_EXPORT void* allocPages(void* addr, size_t len, size_t align); 67 68 // Free one or more pages. 69 // addr and len must match a previous call to allocPages(). 70 WTF_EXPORT void freePages(void* addr, size_t len); 71 72 // Mark one or more system pages as being inaccessible. This is not reversible. 73 // Subsequently accessing any address in the range will fault, the addresses 74 // will not be re-used by future allocations. 75 // len must be a multiple of kSystemPageSize bytes. 76 WTF_EXPORT void setSystemPagesInaccessible(void* addr, size_t len); 77 78 // Decommit one or more system pages. Decommitted means that the physical memory 79 // is released to the system, but the virtual address space remains reserved. 80 // System pages are re-committed by writing to them. 81 // Clients should not make any assumptions about the contents of decommitted 82 // system pages, before or after they write to the page. The only guarantee 83 // provided is that the contents of the system page will be deterministic again 84 // after writing to it. In particlar note that system pages are not guaranteed 85 // to be zero-filled upon re-commit. 86 // len must be a multiple of kSystemPageSize bytes. 87 WTF_EXPORT void decommitSystemPages(void* addr, size_t len); 88 89 } // namespace WTF 90 91 #endif // WTF_PageAllocator_h 92