1706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//===- RemoteTarget.h - LLVM Remote process JIT execution ----------------===//
2706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//
3706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//                     The LLVM Compiler Infrastructure
4706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//
5706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach// This file is distributed under the University of Illinois Open Source
6706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach// License. See LICENSE.TXT for details.
7706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//
8706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//===----------------------------------------------------------------------===//
9706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//
10706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach// Definition of the RemoteTarget class which executes JITed code in a
11706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach// separate address range from where it was built.
12706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//
13706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//===----------------------------------------------------------------------===//
14706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
15706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#ifndef REMOTEPROCESS_H
16706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#define REMOTEPROCESS_H
17706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
18dbf545719a22bf03403c1d0137ae0f5726f36de3Craig Topper#include "llvm/ADT/SmallVector.h"
19f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/ADT/StringRef.h"
20dbf545719a22bf03403c1d0137ae0f5726f36de3Craig Topper#include "llvm/Support/DataTypes.h"
21dbf545719a22bf03403c1d0137ae0f5726f36de3Craig Topper#include "llvm/Support/Memory.h"
22706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include <stdlib.h>
23706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include <string>
24706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
25706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachnamespace llvm {
26706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
27706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachclass RemoteTarget {
28706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  bool IsRunning;
29706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
3036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef SmallVector<sys::MemoryBlock, 16> AllocMapType;
3136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  AllocMapType Allocations;
3236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
3336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesprotected:
3436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::string ErrorMsg;
35706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
36706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachpublic:
37706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  StringRef getErrorMsg() const { return ErrorMsg; }
38706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
39706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// Allocate space in the remote target address space.
40706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///
41706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param      Size      Amount of space, in bytes, to allocate.
42706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param      Alignment Required minimum alignment for allocated space.
43706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param[out] Address   Remote address of the allocated memory.
44706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///
4536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// @returns True on success. On failure, ErrorMsg is updated with
46706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///          descriptive text of the encountered error.
470ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  virtual bool allocateSpace(size_t Size,
480ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor                             unsigned Alignment,
490ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor                             uint64_t &Address);
50706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
5136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  bool isAllocatedMemory(uint64_t Address, uint32_t Size) {
5236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    uint64_t AddressEnd = Address + Size;
5336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    for (AllocMapType::const_iterator I = Allocations.begin(),
5436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                      E = Allocations.end();
5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         I != E; ++I) {
5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      if (Address >= (uint64_t)I->base() &&
5736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines          AddressEnd <= (uint64_t)I->base() + I->size())
5836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        return true;
5936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
6036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return false;
6136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
6236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
63706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// Load data into the target address space.
64706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///
65706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param      Address   Destination address in the target process.
66706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param      Data      Source address in the host process.
67706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param      Size      Number of bytes to copy.
68706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///
6936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// @returns True on success. On failure, ErrorMsg is updated with
70706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///          descriptive text of the encountered error.
710ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  virtual bool loadData(uint64_t Address,
720ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor                        const void *Data,
730ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor                        size_t Size);
74706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
75706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// Load code into the target address space and prepare it for execution.
76706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///
77706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param      Address   Destination address in the target process.
78706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param      Data      Source address in the host process.
79706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param      Size      Number of bytes to copy.
80706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///
8136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// @returns True on success. On failure, ErrorMsg is updated with
82706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///          descriptive text of the encountered error.
830ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  virtual bool loadCode(uint64_t Address,
840ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor                        const void *Data,
850ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor                        size_t Size);
86706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
87706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// Execute code in the target process. The called function is required
88706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// to be of signature int "(*)(void)".
89706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///
90706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param      Address   Address of the loaded function in the target
91706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///                       process.
92706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @param[out] RetVal    The integer return value of the called function.
93706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///
9436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// @returns True on success. On failure, ErrorMsg is updated with
95706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///          descriptive text of the encountered error.
960ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  virtual bool executeCode(uint64_t Address,
970ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor                           int &RetVal);
98706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
9936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// Minimum alignment for memory permissions. Used to separate code and
100706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// data regions to make sure data doesn't get marked as code or vice
101706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// versa.
102706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  ///
103706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// @returns Page alignment return value. Default of 4k.
1040ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  virtual unsigned getPageAlignment() { return 4096; }
105706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
106706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// Start the remote process.
10736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  virtual bool create();
108706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
109706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  /// Terminate the remote process.
1100ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  virtual void stop();
111706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
11236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  RemoteTarget() : IsRunning(false), ErrorMsg("") {}
1130ab5c6c16b1b09d76c3ba2d70443b10bcc26169cAndrew Kaylor  virtual ~RemoteTarget() { if (IsRunning) stop(); }
114706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachprivate:
115706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  // Main processing function for the remote target process. Command messages
116706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  // are received on file descriptor CmdFD and responses come back on OutFD.
117706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach  static void doRemoteTargeting(int CmdFD, int OutFD);
118706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach};
119706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
120706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach} // end namespace llvm
121706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach
122706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#endif
123