2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef ExecutableAllocator_h
27 #define ExecutableAllocator_h
28 #include "JITCompilationEffort.h"
29 #include <stddef.h> // for ptrdiff_t
31 #include <wtf/Assertions.h>
32 #include <wtf/MetaAllocatorHandle.h>
33 #include <wtf/MetaAllocator.h>
34 #include <wtf/PageAllocation.h>
35 #include <wtf/RefCounted.h>
36 #include <wtf/Vector.h>
39 #include <libkern/OSCacheControl.h>
46 #if CPU(MIPS) && OS(LINUX)
47 #include <sys/cachectl.h>
50 #if CPU(SH4) && OS(LINUX)
51 #include <asm/cachectl.h>
52 #include <asm/unistd.h>
53 #include <sys/syscall.h>
57 #define JIT_ALLOCATOR_LARGE_ALLOC_SIZE (pageSize() * 4)
59 #if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
60 #define PROTECTION_FLAGS_RW (PROT_READ | PROT_WRITE)
61 #define PROTECTION_FLAGS_RX (PROT_READ | PROT_EXEC)
62 #define EXECUTABLE_POOL_WRITABLE false
64 #define EXECUTABLE_POOL_WRITABLE true
70 void releaseExecutableMemory(VM
&);
72 static const unsigned jitAllocationGranule
= 32;
74 typedef WTF::MetaAllocatorHandle ExecutableMemoryHandle
;
78 #if ENABLE(EXECUTABLE_ALLOCATOR_DEMAND)
79 class DemandExecutableAllocator
;
82 #if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
84 static const size_t fixedExecutableMemoryPoolSize
= 16 * 1024 * 1024;
86 static const size_t fixedExecutableMemoryPoolSize
= 32 * 1024 * 1024;
88 static const size_t fixedExecutableMemoryPoolSize
= 1024 * 1024 * 1024;
90 static const size_t fixedExecutableMemoryPoolSize
= 32 * 1024 * 1024;
92 static const double executablePoolReservationFraction
= 0.25;
94 extern uintptr_t startOfFixedExecutableMemoryPool
;
97 class ExecutableAllocator
{
98 enum ProtectionSetting
{ Writable
, Executable
};
101 ExecutableAllocator(VM
&);
102 ~ExecutableAllocator();
104 static void initializeAllocator();
106 bool isValid() const;
108 static bool underMemoryPressure();
110 static double memoryPressureMultiplier(size_t addedMemoryUsage
);
112 #if ENABLE(META_ALLOCATOR_PROFILE)
113 static void dumpProfile();
115 static void dumpProfile() { }
118 RefPtr
<ExecutableMemoryHandle
> allocate(VM
&, size_t sizeInBytes
, void* ownerUID
, JITCompilationEffort
);
120 #if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
121 static void makeWritable(void* start
, size_t size
)
123 reprotectRegion(start
, size
, Writable
);
126 static void makeExecutable(void* start
, size_t size
)
128 reprotectRegion(start
, size
, Executable
);
131 static void makeWritable(void*, size_t) {}
132 static void makeExecutable(void*, size_t) {}
135 static size_t committedByteCount();
139 #if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
140 static void reprotectRegion(void*, size_t, ProtectionSetting
);
141 #if ENABLE(EXECUTABLE_ALLOCATOR_DEMAND)
142 // We create a MetaAllocator for each JS global object.
143 std::unique_ptr
<DemandExecutableAllocator
> m_allocator
;
144 DemandExecutableAllocator
* allocator() { return m_allocator
.get(); }
150 #endif // ENABLE(JIT) && ENABLE(ASSEMBLER)
154 #endif // !defined(ExecutableAllocator)