blob: ee758a2747a4d92044046090004933ad5c2947f6 [file] [log] [blame]
Jim Grosbach0f435d02012-09-05 16:50:34 +00001//===- 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 Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_TOOLS_LLI_REMOTETARGET_H
16#define LLVM_TOOLS_LLI_REMOTETARGET_H
Jim Grosbach0f435d02012-09-05 16:50:34 +000017
Craig Topperbc40d7e2012-09-15 18:45:38 +000018#include "llvm/ADT/SmallVector.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000019#include "llvm/ADT/StringRef.h"
Craig Topperbc40d7e2012-09-15 18:45:38 +000020#include "llvm/Support/DataTypes.h"
21#include "llvm/Support/Memory.h"
Jim Grosbach0f435d02012-09-05 16:50:34 +000022#include <stdlib.h>
23#include <string>
24
25namespace llvm {
26
27class RemoteTarget {
Jim Grosbach0f435d02012-09-05 16:50:34 +000028 bool IsRunning;
29
Alp Tokere2641f12014-01-23 11:04:42 +000030 typedef SmallVector<sys::MemoryBlock, 16> AllocMapType;
31 AllocMapType Allocations;
Jim Grosbach0f435d02012-09-05 16:50:34 +000032
Renato Golin695895c2014-01-14 22:43:43 +000033protected:
34 std::string ErrorMsg;
35
Jim Grosbach0f435d02012-09-05 16:50:34 +000036public:
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 Golin695895c2014-01-14 22:43:43 +000045 /// @returns True on success. On failure, ErrorMsg is updated with
Jim Grosbach0f435d02012-09-05 16:50:34 +000046 /// descriptive text of the encountered error.
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000047 virtual bool allocateSpace(size_t Size,
48 unsigned Alignment,
49 uint64_t &Address);
Jim Grosbach0f435d02012-09-05 16:50:34 +000050
Alp Tokere2641f12014-01-23 11:04:42 +000051 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 Grosbach0f435d02012-09-05 16:50:34 +000063 /// 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 Golin695895c2014-01-14 22:43:43 +000069 /// @returns True on success. On failure, ErrorMsg is updated with
Jim Grosbach0f435d02012-09-05 16:50:34 +000070 /// descriptive text of the encountered error.
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000071 virtual bool loadData(uint64_t Address,
72 const void *Data,
73 size_t Size);
Jim Grosbach0f435d02012-09-05 16:50:34 +000074
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 Golin695895c2014-01-14 22:43:43 +000081 /// @returns True on success. On failure, ErrorMsg is updated with
Jim Grosbach0f435d02012-09-05 16:50:34 +000082 /// descriptive text of the encountered error.
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000083 virtual bool loadCode(uint64_t Address,
84 const void *Data,
85 size_t Size);
Jim Grosbach0f435d02012-09-05 16:50:34 +000086
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 Golin695895c2014-01-14 22:43:43 +000094 /// @returns True on success. On failure, ErrorMsg is updated with
Jim Grosbach0f435d02012-09-05 16:50:34 +000095 /// descriptive text of the encountered error.
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000096 virtual bool executeCode(uint64_t Address,
97 int &RetVal);
Jim Grosbach0f435d02012-09-05 16:50:34 +000098
Alp Tokercb402912014-01-24 17:20:08 +000099 /// Minimum alignment for memory permissions. Used to separate code and
Jim Grosbach0f435d02012-09-05 16:50:34 +0000100 /// 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 Kaylorc2ebf3f2013-10-02 17:12:36 +0000104 virtual unsigned getPageAlignment() { return 4096; }
Jim Grosbach0f435d02012-09-05 16:50:34 +0000105
106 /// Start the remote process.
Renato Golin695895c2014-01-14 22:43:43 +0000107 virtual bool create();
Jim Grosbach0f435d02012-09-05 16:50:34 +0000108
109 /// Terminate the remote process.
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000110 virtual void stop();
Jim Grosbach0f435d02012-09-05 16:50:34 +0000111
Renato Golin695895c2014-01-14 22:43:43 +0000112 RemoteTarget() : IsRunning(false), ErrorMsg("") {}
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000113 virtual ~RemoteTarget() { if (IsRunning) stop(); }
Jim Grosbach0f435d02012-09-05 16:50:34 +0000114private:
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