FlushICache.h (3288B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* Flush the instruction cache of instructions in an address range. */ 8 9 #ifndef jit_FlushICache_h 10 #define jit_FlushICache_h 11 12 #include "mozilla/Assertions.h" // MOZ_CRASH 13 14 #include <stddef.h> // size_t 15 16 namespace js { 17 namespace jit { 18 19 #if defined(JS_CODEGEN_X86) || defined(JS_CODEGEN_X64) 20 21 inline void FlushICache(void* code, size_t size) { 22 // No-op. Code and data caches are coherent on x86 and x64. 23 } 24 #elif (defined(JS_CODEGEN_ARM) || defined(JS_CODEGEN_ARM64)) || \ 25 defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_LOONG64) || \ 26 defined(JS_CODEGEN_RISCV64) 27 28 // Invalidate the given code range from the icache. This will also flush the 29 // execution context for this core. If this code is to be executed on another 30 // thread, that thread must perform an execution context flush first using 31 // `FlushExecutionContext` below. 32 extern void FlushICache(void* code, size_t size); 33 34 #elif defined(JS_CODEGEN_NONE) || defined(JS_CODEGEN_WASM32) 35 36 inline void FlushICache(void* code, size_t size) { MOZ_CRASH(); } 37 38 #else 39 # error "Unknown architecture!" 40 #endif 41 42 #if (defined(JS_CODEGEN_X86) || defined(JS_CODEGEN_X64)) || \ 43 defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_LOONG64) || \ 44 defined(JS_CODEGEN_RISCV64) 45 46 inline void FlushExecutionContext() { 47 // No-op. Execution context is coherent with instruction cache. 48 } 49 inline bool CanFlushExecutionContextForAllThreads() { return true; } 50 inline void FlushExecutionContextForAllThreads() { 51 // No-op. Execution context is coherent with instruction cache. 52 } 53 54 #elif defined(JS_CODEGEN_NONE) || defined(JS_CODEGEN_WASM32) 55 56 inline void FlushExecutionContext() { MOZ_CRASH(); } 57 inline bool CanFlushExecutionContextForAllThreads() { MOZ_CRASH(); } 58 inline void FlushExecutionContextForAllThreads() { MOZ_CRASH(); } 59 60 #elif defined(JS_CODEGEN_ARM) || defined(JS_CODEGEN_ARM64) 61 62 // ARM and ARM64 must flush the instruction pipeline of the current core 63 // before executing newly JIT'ed code. This will remove any stale data from 64 // the pipeline that may have referenced invalidated instructions. 65 // 66 // `FlushICache` will perform this for the thread that compiles the code, but 67 // other threads that may execute the code are responsible to call 68 // this method. 69 extern void FlushExecutionContext(); 70 71 // Some platforms can flush the excecution context for other threads using a 72 // syscall. This is required when JIT'ed code will be published to multiple 73 // threads without a synchronization point where a `FlushExecutionContext` 74 // could be inserted. 75 extern bool CanFlushExecutionContextForAllThreads(); 76 77 // Flushes the execution context of all threads in this process, equivalent to 78 // running `FlushExecutionContext` on every thread. 79 // 80 // Callers must ensure `CanFlushExecutionContextForAllThreads` is true, or 81 // else this will crash. 82 extern void FlushExecutionContextForAllThreads(); 83 84 #else 85 # error "Unknown architecture!" 86 #endif 87 88 } // namespace jit 89 } // namespace js 90 91 #endif // jit_FlushICache_h