511aebf84ec365442a245338f56fdb62c4b57845
[platform/upstream/js.git] / js / src / assembler / jit / ExecutableAllocator.h
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
28
29 #include <stddef.h> // for ptrdiff_t
30 #include <limits>
31 #include "assembler/wtf/Assertions.h"
32
33 #include "jsapi.h"
34 #include "jsprvtd.h"
35 #include "jsvector.h"
36 #include "jslock.h"
37
38 #if WTF_PLATFORM_IPHONE
39 #include <libkern/OSCacheControl.h>
40 #include <sys/mman.h>
41 #endif
42
43 #if WTF_PLATFORM_SYMBIAN
44 #include <e32std.h>
45 #endif
46
47 #if WTF_CPU_MIPS && WTF_PLATFORM_LINUX
48 #include <sys/cachectl.h>
49 #endif
50
51 #if WTF_PLATFORM_WINCE
52 // From pkfuncs.h (private header file from the Platform Builder)
53 #define CACHE_SYNC_ALL 0x07F
54 extern "C" __declspec(dllimport) void CacheRangeFlush(LPVOID pAddr, DWORD dwLength, DWORD dwFlags);
55 #endif
56
57 #define JIT_ALLOCATOR_PAGE_SIZE (ExecutableAllocator::pageSize)
58 /*
59  * On Windows, VirtualAlloc effectively allocates in 64K chunks. (Technically,
60  * it allocates in page chunks, but the starting address is always a multiple
61  * of 64K, so each allocation uses up 64K of address space.)  So a size less
62  * than that would be pointless.  But it turns out that 64KB is a reasonable
63  * size for all platforms.
64  */
65 #define JIT_ALLOCATOR_LARGE_ALLOC_SIZE (ExecutableAllocator::pageSize * 16)
66
67 #if ENABLE_ASSEMBLER_WX_EXCLUSIVE
68 #define PROTECTION_FLAGS_RW (PROT_READ | PROT_WRITE)
69 #define PROTECTION_FLAGS_RX (PROT_READ | PROT_EXEC)
70 #define INITIAL_PROTECTION_FLAGS PROTECTION_FLAGS_RX
71 #else
72 #define INITIAL_PROTECTION_FLAGS (PROT_READ | PROT_WRITE | PROT_EXEC)
73 #endif
74
75 namespace JSC {
76
77 // Something included via windows.h defines a macro with this name,
78 // which causes the function below to fail to compile.
79 #ifdef _MSC_VER
80 # undef max
81 #endif
82
83 const size_t OVERSIZE_ALLOCATION = size_t(-1);
84
85 inline size_t roundUpAllocationSize(size_t request, size_t granularity)
86 {
87     if ((std::numeric_limits<size_t>::max() - granularity) <= request)
88         return OVERSIZE_ALLOCATION;
89     
90     // Round up to next page boundary
91     size_t size = request + (granularity - 1);
92     size = size & ~(granularity - 1);
93     JS_ASSERT(size >= request);
94     return size;
95 }
96
97 }
98
99 #if ENABLE_ASSEMBLER
100
101 //#define DEBUG_STRESS_JSC_ALLOCATOR
102
103 namespace JSC {
104
105   // These are reference-counted. A new one (from the constructor or create)
106   // starts with a count of 1. 
107   class ExecutablePool {
108 private:
109     struct Allocation {
110         char* pages;
111         size_t size;
112 #if WTF_PLATFORM_SYMBIAN
113         RChunk* chunk;
114 #endif
115     };
116     typedef js::Vector<Allocation, 2, js::SystemAllocPolicy> AllocationList;
117
118     // Reference count for automatic reclamation.
119     unsigned m_refCount;
120
121 public:
122     // It should be impossible for us to roll over, because only small
123     // pools have multiple holders, and they have one holder per chunk
124     // of generated code, and they only hold 16KB or so of code.
125     void addRef()
126     {
127         JS_ASSERT(m_refCount);
128         ++m_refCount;
129     }
130
131     void release()
132     { 
133         JS_ASSERT(m_refCount != 0);
134         if (--m_refCount == 0)
135             js_delete(this);
136     }
137
138     static ExecutablePool* create(size_t n)
139     {
140         /* We can't (easily) use js_new() here because the constructor is private. */
141         void *memory = js_malloc(sizeof(ExecutablePool));
142         ExecutablePool *pool = memory ? new(memory) ExecutablePool(n) : NULL;
143         if (!pool || !pool->m_freePtr) {
144             js_delete(pool);
145             return NULL;
146         }
147         return pool;
148     }
149
150     void* alloc(size_t n)
151     {
152         JS_ASSERT(m_freePtr <= m_end);
153
154         // Round 'n' up to a multiple of word size; if all allocations are of
155         // word sized quantities, then all subsequent allocations will be aligned.
156         n = roundUpAllocationSize(n, sizeof(void*));
157         if (n == OVERSIZE_ALLOCATION)
158             return NULL;
159
160         if (static_cast<ptrdiff_t>(n) < (m_end - m_freePtr)) {
161             void* result = m_freePtr;
162             m_freePtr += n;
163             return result;
164         }
165
166         // Insufficient space to allocate in the existing pool
167         // so we need allocate into a new pool
168         return poolAllocate(n);
169     }
170     
171     ~ExecutablePool()
172     {
173         Allocation* end = m_pools.end();
174         for (Allocation* ptr = m_pools.begin(); ptr != end; ++ptr)
175             ExecutablePool::systemRelease(*ptr);
176     }
177
178     size_t available() const { return (m_pools.length() > 1) ? 0 : m_end - m_freePtr; }
179
180     // Flag for downstream use, whether to try to release references to this pool.
181     bool m_destroy;
182
183     // GC number in which the m_destroy flag was most recently set. Used downstream to
184     // remember whether m_destroy was computed for the currently active GC.
185     size_t m_gcNumber;
186
187 private:
188     // On OOM, this will return an Allocation where pages is NULL.
189     static Allocation systemAlloc(size_t n);
190     static void systemRelease(const Allocation& alloc);
191
192     ExecutablePool(size_t n);
193
194     void* poolAllocate(size_t n);
195
196     char* m_freePtr;
197     char* m_end;
198     AllocationList m_pools;
199 };
200
201 class ExecutableAllocator {
202     enum ProtectionSeting { Writable, Executable };
203
204     // Initialization can fail so we use a create method instead.
205     ExecutableAllocator() {}
206 public:
207     static size_t pageSize;
208
209     // Returns NULL on OOM.
210     static ExecutableAllocator *create()
211     {
212         /* We can't (easily) use js_new() here because the constructor is private. */
213         void *memory = js_malloc(sizeof(ExecutableAllocator));
214         ExecutableAllocator *allocator = memory ? new(memory) ExecutableAllocator() : NULL;
215         if (!allocator)
216             return allocator;
217
218         if (!pageSize)
219             intializePageSize();
220         ExecutablePool *pool = ExecutablePool::create(JIT_ALLOCATOR_LARGE_ALLOC_SIZE);
221         if (!pool) {
222             js_delete(allocator);
223             return NULL;
224         }
225         JS_ASSERT(allocator->m_smallAllocationPools.empty());
226         allocator->m_smallAllocationPools.append(pool);
227         return allocator;
228     }
229
230     ~ExecutableAllocator()
231     {
232         for (size_t i = 0; i < m_smallAllocationPools.length(); i++)
233             js_delete(m_smallAllocationPools[i]);
234     }
235
236     // poolForSize returns reference-counted objects. The caller owns a reference
237     // to the object; i.e., poolForSize increments the count before returning the
238     // object.
239
240     ExecutablePool* poolForSize(size_t n)
241     {
242 #ifndef DEBUG_STRESS_JSC_ALLOCATOR
243         // Try to fit in an existing small allocator.  Use the pool with the
244         // least available space that is big enough (best-fit).  This is the
245         // best strategy because (a) it maximizes the chance of the next
246         // allocation fitting in a small pool, and (b) it minimizes the
247         // potential waste when a small pool is next abandoned.
248         ExecutablePool *minPool = NULL;
249         for (size_t i = 0; i < m_smallAllocationPools.length(); i++) {
250             ExecutablePool *pool = m_smallAllocationPools[i];
251             if (n <= pool->available() && (!minPool || pool->available() < minPool->available()))
252                 minPool = pool;
253         }
254         if (minPool) {
255             minPool->addRef();
256             return minPool;
257         }
258 #endif
259
260         // If the request is large, we just provide a unshared allocator
261         if (n > JIT_ALLOCATOR_LARGE_ALLOC_SIZE)
262             return ExecutablePool::create(n);
263
264         // Create a new allocator
265         ExecutablePool* pool = ExecutablePool::create(JIT_ALLOCATOR_LARGE_ALLOC_SIZE);
266         if (!pool)
267             return NULL;
268             // At this point, local |pool| is the owner.
269
270         if (m_smallAllocationPools.length() < maxSmallPools) {
271             // We haven't hit the maximum number of live pools;  add the new pool.
272             m_smallAllocationPools.append(pool);
273             pool->addRef();
274         } else {
275             // Find the pool with the least space.
276             int iMin = 0;
277             for (size_t i = 1; i < m_smallAllocationPools.length(); i++)
278                 if (m_smallAllocationPools[i]->available() <
279                     m_smallAllocationPools[iMin]->available())
280                 {
281                     iMin = i;
282                 }
283
284             // If the new allocator will result in more free space than the small
285             // pool with the least space, then we will use it instead
286             ExecutablePool *minPool = m_smallAllocationPools[iMin];
287             if ((pool->available() - n) > minPool->available()) {
288                 minPool->release();
289                 m_smallAllocationPools[iMin] = pool;
290                 pool->addRef();
291             }
292         }
293
294             // Pass ownership to the caller.
295         return pool;
296     }
297
298 #if ENABLE_ASSEMBLER_WX_EXCLUSIVE
299     static void makeWritable(void* start, size_t size)
300     {
301         reprotectRegion(start, size, Writable);
302     }
303
304     static void makeExecutable(void* start, size_t size)
305     {
306         reprotectRegion(start, size, Executable);
307     }
308 #else
309     static void makeWritable(void*, size_t) {}
310     static void makeExecutable(void*, size_t) {}
311 #endif
312
313
314 #if WTF_CPU_X86 || WTF_CPU_X86_64
315     static void cacheFlush(void*, size_t)
316     {
317     }
318 #elif WTF_CPU_MIPS
319     static void cacheFlush(void* code, size_t size)
320     {
321 #if WTF_COMPILER_GCC && (GCC_VERSION >= 40300)
322 #if WTF_MIPS_ISA_REV(2) && (GCC_VERSION < 40403)
323         int lineSize;
324         asm("rdhwr %0, $1" : "=r" (lineSize));
325         //
326         // Modify "start" and "end" to avoid GCC 4.3.0-4.4.2 bug in
327         // mips_expand_synci_loop that may execute synci one more time.
328         // "start" points to the fisrt byte of the cache line.
329         // "end" points to the last byte of the line before the last cache line.
330         // Because size is always a multiple of 4, this is safe to set
331         // "end" to the last byte.
332         //
333         intptr_t start = reinterpret_cast<intptr_t>(code) & (-lineSize);
334         intptr_t end = ((reinterpret_cast<intptr_t>(code) + size - 1) & (-lineSize)) - 1;
335         __builtin___clear_cache(reinterpret_cast<char*>(start), reinterpret_cast<char*>(end));
336 #else
337         intptr_t end = reinterpret_cast<intptr_t>(code) + size;
338         __builtin___clear_cache(reinterpret_cast<char*>(code), reinterpret_cast<char*>(end));
339 #endif
340 #else
341         _flush_cache(reinterpret_cast<char*>(code), size, BCACHE);
342 #endif
343     }
344 #elif WTF_CPU_ARM_THUMB2 && WTF_PLATFORM_IPHONE
345     static void cacheFlush(void* code, size_t size)
346     {
347         sys_dcache_flush(code, size);
348         sys_icache_invalidate(code, size);
349     }
350 #elif WTF_CPU_ARM_THUMB2 && WTF_PLATFORM_LINUX
351     static void cacheFlush(void* code, size_t size)
352     {
353         asm volatile (
354             "push    {r7}\n"
355             "mov     r0, %0\n"
356             "mov     r1, %1\n"
357             "movw    r7, #0x2\n"
358             "movt    r7, #0xf\n"
359             "movs    r2, #0x0\n"
360             "svc     0x0\n"
361             "pop     {r7}\n"
362             :
363             : "r" (code), "r" (reinterpret_cast<char*>(code) + size)
364             : "r0", "r1", "r2");
365     }
366 #elif WTF_PLATFORM_SYMBIAN
367     static void cacheFlush(void* code, size_t size)
368     {
369         User::IMB_Range(code, static_cast<char*>(code) + size);
370     }
371 #elif WTF_CPU_ARM_TRADITIONAL && WTF_PLATFORM_LINUX && WTF_COMPILER_RVCT
372     static __asm void cacheFlush(void* code, size_t size);
373 #elif WTF_CPU_ARM_TRADITIONAL && (WTF_PLATFORM_LINUX || WTF_PLATFORM_ANDROID) && WTF_COMPILER_GCC
374     static void cacheFlush(void* code, size_t size)
375     {
376         asm volatile (
377             "push    {r7}\n"
378             "mov     r0, %0\n"
379             "mov     r1, %1\n"
380             "mov     r7, #0xf0000\n"
381             "add     r7, r7, #0x2\n"
382             "mov     r2, #0x0\n"
383             "svc     0x0\n"
384             "pop     {r7}\n"
385             :
386             : "r" (code), "r" (reinterpret_cast<char*>(code) + size)
387             : "r0", "r1", "r2");
388     }
389 #elif WTF_PLATFORM_WINCE
390     static void cacheFlush(void* code, size_t size)
391     {
392         CacheRangeFlush(code, size, CACHE_SYNC_ALL);
393     }
394 #else
395     #error "The cacheFlush support is missing on this platform."
396 #endif
397
398 private:
399
400 #if ENABLE_ASSEMBLER_WX_EXCLUSIVE
401     static void reprotectRegion(void*, size_t, ProtectionSeting);
402 #endif
403
404     static const size_t maxSmallPools = 4;
405     typedef js::Vector<ExecutablePool *, maxSmallPools, js::SystemAllocPolicy > SmallExecPoolVector;
406     SmallExecPoolVector m_smallAllocationPools;
407     static void intializePageSize();
408 };
409
410 // This constructor can fail due to OOM. If it does, m_freePtr will be
411 // set to NULL. 
412 inline ExecutablePool::ExecutablePool(size_t n) : m_refCount(1), m_destroy(false), m_gcNumber(0)
413 {
414     size_t allocSize = roundUpAllocationSize(n, JIT_ALLOCATOR_PAGE_SIZE);
415     if (allocSize == OVERSIZE_ALLOCATION) {
416         m_freePtr = NULL;
417         return;
418     }
419 #ifdef DEBUG_STRESS_JSC_ALLOCATOR
420     Allocation mem = systemAlloc(size_t(4294967291));
421 #else
422     Allocation mem = systemAlloc(allocSize);
423 #endif
424     if (!mem.pages) {
425         m_freePtr = NULL;
426         return;
427     }
428     if (!m_pools.append(mem)) {
429         systemRelease(mem);
430         m_freePtr = NULL;
431         return;
432     }
433     m_freePtr = mem.pages;
434     m_end = m_freePtr + allocSize;
435 }
436
437 inline void* ExecutablePool::poolAllocate(size_t n)
438 {
439     size_t allocSize = roundUpAllocationSize(n, JIT_ALLOCATOR_PAGE_SIZE);
440     if (allocSize == OVERSIZE_ALLOCATION)
441         return NULL;
442     
443 #ifdef DEBUG_STRESS_JSC_ALLOCATOR
444     Allocation result = systemAlloc(size_t(4294967291));
445 #else
446     Allocation result = systemAlloc(allocSize);
447 #endif
448     if (!result.pages)
449         return NULL;
450     
451     JS_ASSERT(m_end >= m_freePtr);
452     if ((allocSize - n) > static_cast<size_t>(m_end - m_freePtr)) {
453         // Replace allocation pool
454         m_freePtr = result.pages + n;
455         m_end = result.pages + allocSize;
456     }
457
458     m_pools.append(result);
459     return result.pages;
460 }
461
462 }
463
464 #endif // ENABLE(ASSEMBLER)
465
466 #endif // !defined(ExecutableAllocator)