llvm.org GIT mirror llvm / 93bb4aa
Add a new InvalidateInstructionCache method to sys::Memory. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52731 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
2 changed file(s) with 60 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
3838 /// @since 1.4
3939 /// @brief An abstraction for memory operations.
4040 class Memory {
41 /// @name Functions
42 /// @{
43 public:
44 /// This method allocates a block of Read/Write/Execute memory that is
45 /// suitable for executing dynamically generated code (e.g. JIT). An
46 /// attempt to allocate \p NumBytes bytes of virtual memory is made.
47 /// \p NearBlock may point to an existing allocation in which case
48 /// an attempt is made to allocate more memory near the existing block.
49 ///
50 /// On success, this returns a non-null memory block, otherwise it returns
51 /// a null memory block and fills in *ErrMsg.
52 ///
53 /// @brief Allocate Read/Write/Execute memory.
54 static MemoryBlock AllocateRWX(unsigned NumBytes,
55 const MemoryBlock *NearBlock,
56 std::string *ErrMsg = 0);
41 public:
42 /// This method allocates a block of Read/Write/Execute memory that is
43 /// suitable for executing dynamically generated code (e.g. JIT). An
44 /// attempt to allocate \p NumBytes bytes of virtual memory is made.
45 /// \p NearBlock may point to an existing allocation in which case
46 /// an attempt is made to allocate more memory near the existing block.
47 ///
48 /// On success, this returns a non-null memory block, otherwise it returns
49 /// a null memory block and fills in *ErrMsg.
50 ///
51 /// @brief Allocate Read/Write/Execute memory.
52 static MemoryBlock AllocateRWX(unsigned NumBytes,
53 const MemoryBlock *NearBlock,
54 std::string *ErrMsg = 0);
5755
58 /// This method releases a block of Read/Write/Execute memory that was
59 /// allocated with the AllocateRWX method. It should not be used to
60 /// release any memory block allocated any other way.
61 ///
62 /// On success, this returns false, otherwise it returns true and fills
63 /// in *ErrMsg.
64 /// @throws std::string if an error occurred.
65 /// @brief Release Read/Write/Execute memory.
66 static bool ReleaseRWX(MemoryBlock &block, std::string *ErrMsg = 0);
67 /// @}
56 /// This method releases a block of Read/Write/Execute memory that was
57 /// allocated with the AllocateRWX method. It should not be used to
58 /// release any memory block allocated any other way.
59 ///
60 /// On success, this returns false, otherwise it returns true and fills
61 /// in *ErrMsg.
62 /// @throws std::string if an error occurred.
63 /// @brief Release Read/Write/Execute memory.
64 static bool ReleaseRWX(MemoryBlock &block, std::string *ErrMsg = 0);
65
66
67 /// InvalidateInstructionCache - Before the JIT can run a block of code
68 /// that has been emitted it must invalidate the instruction cache on some
69 /// platforms.
70 static void InvalidateInstructionCache(const void *Addr, size_t Len);
6871 };
6972 }
7073 }
1616
1717 namespace llvm {
1818 using namespace sys;
19
20 //===----------------------------------------------------------------------===//
21 //=== WARNING: Implementation here must contain only TRULY operating system
22 //=== independent code.
23 //===----------------------------------------------------------------------===//
24
2519 }
2620
2721 // Include the platform-specific parts of this class.
3125 #ifdef LLVM_ON_WIN32
3226 #include "Win32/Memory.inc"
3327 #endif
28
29 /// InvalidateInstructionCache - Before the JIT can run a block of code
30 /// that has been emitted it must invalidate the instruction cache on some
31 /// platforms.
32 void llvm::sys::Memory::InvalidateInstructionCache(const void *Addr,
33 size_t Len) {
34
35 // icache invalidation for PPC.
36 #if (defined(__POWERPC__) || defined (__ppc__) || \
37 defined(_POWER) || defined(_ARCH_PPC))
38 #if defined(__APPLE__)
39 extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
40 sys_icache_invalidate(Addr, len);
41 #elif defined(__GNUC__)
42 const size_t LineSize = 32;
43
44 const intptr_t Mask = ~(LineSize - 1);
45 const intptr_t StartLine = ((intptr_t) Addr) & Mask;
46 const intptr_t EndLine = ((intptr_t) Addr + len + LineSize - 1) & Mask;
47
48 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
49 asm volatile("dcbf 0, %0" : : "r"(Line));
50 asm volatile("sync");
51
52 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
53 asm volatile("icbi 0, %0" : : "r"(Line));
54 asm volatile("isync");
55 #endif
56 #endif // end PPC
57
58 }