Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 1 | //===- RemoteTarget.h - LLVM Remote process JIT execution ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Definition of the RemoteTarget class which executes JITed code in a |
| 11 | // separate address range from where it was built. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_TOOLS_LLI_REMOTETARGET_H |
| 16 | #define LLVM_TOOLS_LLI_REMOTETARGET_H |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 17 | |
Craig Topper | bc40d7e | 2012-09-15 18:45:38 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
Craig Topper | bc40d7e | 2012-09-15 18:45:38 +0000 | [diff] [blame] | 20 | #include "llvm/Support/DataTypes.h" |
| 21 | #include "llvm/Support/Memory.h" |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string> |
| 24 | |
| 25 | namespace llvm { |
| 26 | |
| 27 | class RemoteTarget { |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 28 | bool IsRunning; |
| 29 | |
Alp Toker | e2641f1 | 2014-01-23 11:04:42 +0000 | [diff] [blame] | 30 | typedef SmallVector<sys::MemoryBlock, 16> AllocMapType; |
| 31 | AllocMapType Allocations; |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 32 | |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 33 | protected: |
| 34 | std::string ErrorMsg; |
| 35 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 36 | public: |
| 37 | StringRef getErrorMsg() const { return ErrorMsg; } |
| 38 | |
| 39 | /// Allocate space in the remote target address space. |
| 40 | /// |
| 41 | /// @param Size Amount of space, in bytes, to allocate. |
| 42 | /// @param Alignment Required minimum alignment for allocated space. |
| 43 | /// @param[out] Address Remote address of the allocated memory. |
| 44 | /// |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 45 | /// @returns True on success. On failure, ErrorMsg is updated with |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 46 | /// descriptive text of the encountered error. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 47 | virtual bool allocateSpace(size_t Size, |
| 48 | unsigned Alignment, |
| 49 | uint64_t &Address); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 50 | |
Alp Toker | e2641f1 | 2014-01-23 11:04:42 +0000 | [diff] [blame] | 51 | bool isAllocatedMemory(uint64_t Address, uint32_t Size) { |
| 52 | uint64_t AddressEnd = Address + Size; |
| 53 | for (AllocMapType::const_iterator I = Allocations.begin(), |
| 54 | E = Allocations.end(); |
| 55 | I != E; ++I) { |
| 56 | if (Address >= (uint64_t)I->base() && |
| 57 | AddressEnd <= (uint64_t)I->base() + I->size()) |
| 58 | return true; |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 63 | /// Load data into the target address space. |
| 64 | /// |
| 65 | /// @param Address Destination address in the target process. |
| 66 | /// @param Data Source address in the host process. |
| 67 | /// @param Size Number of bytes to copy. |
| 68 | /// |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 69 | /// @returns True on success. On failure, ErrorMsg is updated with |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 70 | /// descriptive text of the encountered error. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 71 | virtual bool loadData(uint64_t Address, |
| 72 | const void *Data, |
| 73 | size_t Size); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 74 | |
| 75 | /// Load code into the target address space and prepare it for execution. |
| 76 | /// |
| 77 | /// @param Address Destination address in the target process. |
| 78 | /// @param Data Source address in the host process. |
| 79 | /// @param Size Number of bytes to copy. |
| 80 | /// |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 81 | /// @returns True on success. On failure, ErrorMsg is updated with |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 82 | /// descriptive text of the encountered error. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 83 | virtual bool loadCode(uint64_t Address, |
| 84 | const void *Data, |
| 85 | size_t Size); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 86 | |
| 87 | /// Execute code in the target process. The called function is required |
| 88 | /// to be of signature int "(*)(void)". |
| 89 | /// |
| 90 | /// @param Address Address of the loaded function in the target |
| 91 | /// process. |
| 92 | /// @param[out] RetVal The integer return value of the called function. |
| 93 | /// |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 94 | /// @returns True on success. On failure, ErrorMsg is updated with |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 95 | /// descriptive text of the encountered error. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 96 | virtual bool executeCode(uint64_t Address, |
| 97 | int &RetVal); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 98 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 99 | /// Minimum alignment for memory permissions. Used to separate code and |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 100 | /// data regions to make sure data doesn't get marked as code or vice |
| 101 | /// versa. |
| 102 | /// |
| 103 | /// @returns Page alignment return value. Default of 4k. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 104 | virtual unsigned getPageAlignment() { return 4096; } |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 105 | |
| 106 | /// Start the remote process. |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 107 | virtual bool create(); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 108 | |
| 109 | /// Terminate the remote process. |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 110 | virtual void stop(); |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 111 | |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 112 | RemoteTarget() : IsRunning(false), ErrorMsg("") {} |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 113 | virtual ~RemoteTarget() { if (IsRunning) stop(); } |
Jim Grosbach | 0f435d0 | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 114 | private: |
| 115 | // Main processing function for the remote target process. Command messages |
| 116 | // are received on file descriptor CmdFD and responses come back on OutFD. |
| 117 | static void doRemoteTargeting(int CmdFD, int OutFD); |
| 118 | }; |
| 119 | |
| 120 | } // end namespace llvm |
| 121 | |
| 122 | #endif |