]> git.saurik.com Git - apple/javascriptcore.git/blame - jit/ExecutableAllocator.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / jit / ExecutableAllocator.h
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 2008 Apple 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
6 * are met:
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.
12 *
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.
24 */
25
26#ifndef ExecutableAllocator_h
27#define ExecutableAllocator_h
6fe7ccc8 28#include "JITCompilationEffort.h"
f9bf01c6 29#include <stddef.h> // for ptrdiff_t
ba379fdc 30#include <limits>
9dae56ea 31#include <wtf/Assertions.h>
6fe7ccc8 32#include <wtf/MetaAllocatorHandle.h>
93a37866 33#include <wtf/MetaAllocator.h>
14957cd0 34#include <wtf/PageAllocation.h>
9dae56ea
A
35#include <wtf/RefCounted.h>
36#include <wtf/Vector.h>
37
14957cd0 38#if OS(IOS)
ba379fdc 39#include <libkern/OSCacheControl.h>
f9bf01c6
A
40#endif
41
81345200 42#if OS(IOS)
6fe7ccc8 43#include <sys/mman.h>
f9bf01c6
A
44#endif
45
4e4e5a6f
A
46#if CPU(MIPS) && OS(LINUX)
47#include <sys/cachectl.h>
48#endif
49
14957cd0
A
50#if CPU(SH4) && OS(LINUX)
51#include <asm/cachectl.h>
52#include <asm/unistd.h>
53#include <sys/syscall.h>
54#include <unistd.h>
55#endif
56
6fe7ccc8 57#define JIT_ALLOCATOR_LARGE_ALLOC_SIZE (pageSize() * 4)
9dae56ea 58
ba379fdc
A
59#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
60#define PROTECTION_FLAGS_RW (PROT_READ | PROT_WRITE)
61#define PROTECTION_FLAGS_RX (PROT_READ | PROT_EXEC)
14957cd0 62#define EXECUTABLE_POOL_WRITABLE false
ba379fdc 63#else
14957cd0 64#define EXECUTABLE_POOL_WRITABLE true
ba379fdc
A
65#endif
66
67namespace JSC {
68
93a37866
A
69class VM;
70void releaseExecutableMemory(VM&);
71
72static const unsigned jitAllocationGranule = 32;
6fe7ccc8 73
6fe7ccc8
A
74typedef WTF::MetaAllocatorHandle ExecutableMemoryHandle;
75
76#if ENABLE(ASSEMBLER)
14957cd0 77
14957cd0 78#if ENABLE(EXECUTABLE_ALLOCATOR_DEMAND)
6fe7ccc8 79class DemandExecutableAllocator;
14957cd0 80#endif
9dae56ea 81
93a37866 82#if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
ed1e77d3 83#if CPU(ARM)
93a37866 84static const size_t fixedExecutableMemoryPoolSize = 16 * 1024 * 1024;
ed1e77d3
A
85#elif CPU(ARM64)
86static const size_t fixedExecutableMemoryPoolSize = 32 * 1024 * 1024;
93a37866
A
87#elif CPU(X86_64)
88static const size_t fixedExecutableMemoryPoolSize = 1024 * 1024 * 1024;
89#else
90static const size_t fixedExecutableMemoryPoolSize = 32 * 1024 * 1024;
91#endif
ed1e77d3 92static const double executablePoolReservationFraction = 0.25;
93a37866
A
93
94extern uintptr_t startOfFixedExecutableMemoryPool;
95#endif
96
9dae56ea 97class ExecutableAllocator {
14957cd0 98 enum ProtectionSetting { Writable, Executable };
ba379fdc 99
9dae56ea 100public:
93a37866 101 ExecutableAllocator(VM&);
6fe7ccc8
A
102 ~ExecutableAllocator();
103
104 static void initializeAllocator();
9dae56ea 105
4e4e5a6f 106 bool isValid() const;
14957cd0
A
107
108 static bool underMemoryPressure();
6fe7ccc8
A
109
110 static double memoryPressureMultiplier(size_t addedMemoryUsage);
111
112#if ENABLE(META_ALLOCATOR_PROFILE)
113 static void dumpProfile();
114#else
115 static void dumpProfile() { }
116#endif
14957cd0 117
ed1e77d3 118 RefPtr<ExecutableMemoryHandle> allocate(VM&, size_t sizeInBytes, void* ownerUID, JITCompilationEffort);
9dae56ea 119
ba379fdc
A
120#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
121 static void makeWritable(void* start, size_t size)
122 {
123 reprotectRegion(start, size, Writable);
124 }
125
126 static void makeExecutable(void* start, size_t size)
127 {
128 reprotectRegion(start, size, Executable);
129 }
130#else
131 static void makeWritable(void*, size_t) {}
132 static void makeExecutable(void*, size_t) {}
133#endif
134
14957cd0 135 static size_t committedByteCount();
ba379fdc 136
9dae56ea 137private:
ba379fdc
A
138
139#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
14957cd0 140 static void reprotectRegion(void*, size_t, ProtectionSetting);
6fe7ccc8
A
141#if ENABLE(EXECUTABLE_ALLOCATOR_DEMAND)
142 // We create a MetaAllocator for each JS global object.
ed1e77d3 143 std::unique_ptr<DemandExecutableAllocator> m_allocator;
6fe7ccc8
A
144 DemandExecutableAllocator* allocator() { return m_allocator.get(); }
145#endif
ba379fdc
A
146#endif
147
9dae56ea
A
148};
149
14957cd0 150#endif // ENABLE(JIT) && ENABLE(ASSEMBLER)
9dae56ea 151
6fe7ccc8
A
152} // namespace JSC
153
9dae56ea 154#endif // !defined(ExecutableAllocator)