summaryrefslogtreecommitdiffstats
path: root/llvm/include/MCJITMemoryManager.h
blob: 33059a5e3f41a066b90e12fe6844c3b139022d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//===-- MCJITMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Interface of the MCJIT memory manager base class.
//
//===----------------------------------------------------------------------===//

#ifndef __MCJITMEMORYMANAGER_H
#define __MCJITMEMORYMANAGER_H

#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm-debug.h"
#include "utils.h"

using namespace llvm;

#define MIN_CODE_CACHE_SIZE         (1 * 1024 * 1024)
#define DEFAULT_GLOBAL_SIZE         (64 * 1024)
#define DEFAULT_THRESHOLD           (32 * 1024)

// RuntimeDyld clients often want to handle the memory management of
// what gets placed where. For JIT clients, this is the subset of
// JITMemoryManager required for dynamic loading of binaries.
//
// FIXME: As the RuntimeDyld fills out, additional routines will be needed
//        for the varying types of objects to be allocated.
class DefaultMCJITMemoryManager : public RTDyldMemoryManager {
  uint8_t *TraceCache;
  size_t TraceCacheSize;

  uint8_t *GlobalBase;  /* section for global data used by QEMU helpers */
  uint8_t *CodeBase;    /* section for emitting trace code */
  uint8_t *CodeGenPtr;

  size_t GlobalRemain;
  size_t CodeRemain;
  size_t Threshold;

  hqemu::Mutex lock;

  SymbolMap Symbols;

public:
  DefaultMCJITMemoryManager(uint8_t *Cache, size_t Size)
    : TraceCache(Cache), TraceCacheSize(Size), Threshold(DEFAULT_THRESHOLD)
  {
    GlobalBase = TraceCache;
    GlobalRemain = DEFAULT_GLOBAL_SIZE;

    CodeBase = GlobalBase + DEFAULT_GLOBAL_SIZE;
    CodeBase = (uint8_t *)(((uintptr_t)CodeBase + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
    CodeRemain = (uintptr_t)TraceCache + TraceCacheSize - (uintptr_t)CodeBase;
    CodeGenPtr = CodeBase;
  }
  ~DefaultMCJITMemoryManager() {}

  /// Allocate a memory block of (at least) the given size suitable for
  /// executable code. The SectionID is a unique identifier assigned by the JIT
  /// engine, and optionally recorded by the memory manager to access a loaded
  /// section.
  uint8_t *allocateCodeSection(
    uintptr_t Size, unsigned Alignment, unsigned SectionID,
    StringRef SectionName) override {
    hqemu::MutexGuard locked(lock);

    if (!Alignment)
      Alignment = 16;

    if (Alignment & (Alignment - 1))
      hqemu_error("Alignment must be a power of two.\n");

    uintptr_t CurGenPtr = (uintptr_t)CodeGenPtr;
    CurGenPtr = (CurGenPtr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
    CodeGenPtr = (uint8_t *)((CurGenPtr + Size + CODE_GEN_ALIGN - 1) &
                             ~(uintptr_t)(CODE_GEN_ALIGN - 1));
    CodeRemain = (uintptr_t)TraceCache + TraceCacheSize - (uintptr_t)CodeGenPtr;
    return (uint8_t *)CurGenPtr;
  }

  /// Allocate a memory block of (at least) the given size suitable for data.
  /// The SectionID is a unique identifier assigned by the JIT engine, and
  /// optionally recorded by the memory manager to access a loaded section.
  uint8_t *allocateDataSection(
    uintptr_t Size, unsigned Alignment, unsigned SectionID,
    StringRef SectionName, bool IsReadOnly) override {
    return allocateCodeSection(Size, Alignment, SectionID, SectionName);
  }

  /// Inform the memory manager about the total amount of memory required to
  /// allocate all sections to be loaded:
  /// \p CodeSize - the total size of all code sections
  /// \p DataSizeRO - the total size of all read-only data sections
  /// \p DataSizeRW - the total size of all read-write data sections
  /// 
  /// Note that by default the callback is disabled. To enable it
  /// redefine the method needsToReserveAllocationSpace to return true.
  void reserveAllocationSpace(
    uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) {
    hqemu_error("fixme.\n");
  }
  
  /// Override to return true to enable the reserveAllocationSpace callback.
  bool needsToReserveAllocationSpace() { return false; }

  /// Register the EH frames with the runtime so that c++ exceptions work.
  ///
  /// \p Addr parameter provides the local address of the EH frame section
  /// data, while \p LoadAddr provides the address of the data in the target
  /// address space.  If the section has not been remapped (which will usually
  /// be the case for local execution) these two values will be the same.
  void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
    hqemu_error("fixme.\n");
  }

  void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
    hqemu_error("fixme.\n");
  }

  /// This method returns the address of the specified function or variable.
  /// It is used to resolve symbols during module linking.
  uint64_t getSymbolAddress(const std::string &Name) {
    hqemu::MutexGuard locked(lock);
    if (Symbols.find(Name) == Symbols.end()) {
      std::string ErrMsg = "Program used external symbol '" + Name +
                           "'which could not be resolved!\n";
      hqemu_error(ErrMsg.c_str());
    }
    return Symbols[Name];
  }

  /// This method returns the address of the specified function. As such it is
  /// only useful for resolving library symbols, not code generated symbols.
  ///
  /// If \p AbortOnFailure is false and no function with the given name is
  /// found, this function returns a null pointer. Otherwise, it prints a
  /// message to stderr and aborts.
  ///
  /// This function is deprecated for memory managers to be used with
  /// MCJIT or RuntimeDyld.  Use getSymbolAddress instead.
  void *getPointerToNamedFunction(const std::string &Name,
                                  bool AbortOnFailure = true) {
    if (AbortOnFailure) {
      std::string ErrMsg = "Program used external symbol '" + Name +
                           "'which could not be resolved!\n";
      hqemu_error(ErrMsg.c_str());
    }
    return nullptr;
  }

  /// This method is called after an object has been loaded into memory but
  /// before relocations are applied to the loaded sections.  The object load
  /// may have been initiated by MCJIT to resolve an external symbol for another
  /// object that is being finalized.  In that case, the object about which
  /// the memory manager is being notified will be finalized immediately after
  /// the memory manager returns from this call.
  ///
  /// Memory managers which are preparing code for execution in an external
  /// address space can use this call to remap the section addresses for the
  /// newly loaded object.
#if defined(LLVM_V35)
  void notifyObjectLoaded(ExecutionEngine *EE,
                          const ObjectImage *Obj) {
  }
#else
  void notifyObjectLoaded(RuntimeDyld &RTDyld,
		          const object::ObjectFile &Obj) {
  }
#endif

  /// This method is called when object loading is complete and section page
  /// permissions can be applied.  It is up to the memory manager implementation
  /// to decide whether or not to act on this method.  The memory manager will
  /// typically allocate all sections as read-write and then apply specific
  /// permissions when this method is called.  Code sections cannot be executed
  /// until this function has been called.  In addition, any cache coherency
  /// operations needed to reliably use the memory are also performed.
  ///
  /// Returns true if an error occurred, false otherwise.
  bool finalizeMemory(std::string *ErrMsg = nullptr) override {
    return false;
  }

  void AddSymbols(SymbolMap &symbols) {
    Symbols = symbols;
  }

  size_t getCodeSize()      { return CodeGenPtr - CodeBase; }
  bool isSizeAvailable()    {
    hqemu::MutexGuard locked(lock);
    return CodeRemain >= Threshold ? 1 : 0;
  }
  void Flush() {
    CodeGenPtr = CodeBase;
    CodeRemain = (uintptr_t)TraceCache + TraceCacheSize - (uintptr_t)CodeBase;
  }

  static DefaultMCJITMemoryManager *Create(uint8_t *Cache, size_t Size) {
    if (Size < MIN_CODE_CACHE_SIZE) {
      std::string ErrMsg = "Trace cache size is too small (" +
                           std::to_string(Size) + ")\n.";
      hqemu_error(ErrMsg.c_str());
    }
    return new DefaultMCJITMemoryManager(Cache, Size);
  }
};

#endif
OpenPOWER on IntegriCloud