Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 1 | //===- RemoteTarget.cpp - 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 | // Implementation of the RemoteTarget class which executes JITed code in a |
| 11 | // separate address range from where it was built. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "RemoteTarget.h" |
Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame^] | 16 | #include "RemoteTargetExternal.h" |
Craig Topper | dbf5457 | 2012-09-15 18:45:38 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/Support/DataTypes.h" |
| 19 | #include "llvm/Support/Memory.h" |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string> |
Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame^] | 22 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Andrew Kaylor | 0ab5c6c | 2013-10-02 17:12:36 +0000 | [diff] [blame^] | 25 | // Static methods |
| 26 | RemoteTarget *RemoteTarget::createRemoteTarget() { |
| 27 | return new RemoteTarget; |
| 28 | } |
| 29 | |
| 30 | RemoteTarget *RemoteTarget::createExternalRemoteTarget(std::string &ChildName) { |
| 31 | #ifdef LLVM_ON_UNIX |
| 32 | return new RemoteTargetExternal(ChildName); |
| 33 | #else |
| 34 | return 0; |
| 35 | #endif |
| 36 | } |
| 37 | |
| 38 | bool RemoteTarget::hostSupportsExternalRemoteTarget() { |
| 39 | #ifdef LLVM_ON_UNIX |
| 40 | return true; |
| 41 | #else |
| 42 | return false; |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | |
| 47 | //////////////////////////////////////////////////////////////////////////////// |
| 48 | // Simulated remote execution |
| 49 | // |
| 50 | // This implementation will simply move generated code and data to a new memory |
| 51 | // location in the current executable and let it run from there. |
| 52 | //////////////////////////////////////////////////////////////////////////////// |
| 53 | |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 54 | bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment, |
| 55 | uint64_t &Address) { |
| 56 | sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : NULL; |
| 57 | sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg); |
| 58 | if (Mem.base() == NULL) |
| 59 | return true; |
| 60 | if ((uintptr_t)Mem.base() % Alignment) { |
| 61 | ErrorMsg = "unable to allocate sufficiently aligned memory"; |
| 62 | return true; |
| 63 | } |
| 64 | Address = reinterpret_cast<uint64_t>(Mem.base()); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) { |
| 69 | memcpy ((void*)Address, Data, Size); |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
| 73 | bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) { |
| 74 | memcpy ((void*)Address, Data, Size); |
Andrew Kaylor | 2932284 | 2012-10-31 20:37:14 +0000 | [diff] [blame] | 75 | sys::MemoryBlock Mem((void*)Address, Size); |
| 76 | sys::Memory::setExecutable(Mem, &ErrorMsg); |
Jim Grosbach | 706f03a | 2012-09-05 16:50:34 +0000 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
| 80 | bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) { |
| 81 | int (*fn)(void) = (int(*)(void))Address; |
| 82 | RetVal = fn(); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | void RemoteTarget::create() { |
| 87 | } |
| 88 | |
| 89 | void RemoteTarget::stop() { |
| 90 | for (unsigned i = 0, e = Allocations.size(); i != e; ++i) |
| 91 | sys::Memory::ReleaseRWX(Allocations[i]); |
| 92 | } |