RemoteMemoryManager.h revision e3fd646e178f92dbe2737a5230d73577090d9d0e
1b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor//===- RemoteMemoryManager.h - LLI MCJIT recording memory manager ------===// 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// This memory manager allocates local storage and keeps a record of each 11706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach// allocation. Iterators are provided for all data and code allocations. 12706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach// 13706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach//===----------------------------------------------------------------------===// 14706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 15b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor#ifndef REMOTEMEMORYMANAGER_H 16b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor#define REMOTEMEMORYMANAGER_H 17706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 18b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor#include "llvm/ADT/DenseMap.h" 19706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include "llvm/ADT/SmallVector.h" 20706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include "llvm/ExecutionEngine/JITMemoryManager.h" 21706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include "llvm/Support/ErrorHandling.h" 22706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include "llvm/Support/Memory.h" 23706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include <utility> 24706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 25b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor#include "RemoteTarget.h" 26b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 27706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachnamespace llvm { 28706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 29b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylorclass RemoteMemoryManager : public JITMemoryManager { 30706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachpublic: 31b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // Notice that this structure takes ownership of the memory allocated. 32b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor struct Allocation { 33e3fd646e178f92dbe2737a5230d73577090d9d0eAndrew Kaylor Allocation() {} 34b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor Allocation(sys::MemoryBlock mb, unsigned a, bool code) 35b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor : MB(mb), Alignment(a), IsCode(code) {} 36b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 37b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor sys::MemoryBlock MB; 38b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor unsigned Alignment; 39b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor bool IsCode; 40b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor }; 41706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 42706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachprivate: 43b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // This vector contains Allocation objects for all sections which we have 44b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // allocated. This vector effectively owns the memory associated with the 45b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // allocations. 46b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor SmallVector<Allocation, 2> AllocatedSections; 47b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 48b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // This vector contains pointers to Allocation objects for any sections we 49b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // have allocated locally but have not yet remapped for the remote target. 50b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // When we receive notification of a completed module load, we will map 51b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // these sections into the remote target. 52e3fd646e178f92dbe2737a5230d73577090d9d0eAndrew Kaylor SmallVector<Allocation, 2> UnmappedSections; 53b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 54b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // This map tracks the sections we have remapped for the remote target 55b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // but have not yet copied to the target. 56e3fd646e178f92dbe2737a5230d73577090d9d0eAndrew Kaylor DenseMap<uint64_t, Allocation> MappedSections; 57706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 58b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor // FIXME: This is part of a work around to keep sections near one another 59b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor // when MCJIT performs relocations after code emission but before 60b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor // the generated code is moved to the remote target. 61b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor sys::MemoryBlock Near; 62b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor sys::MemoryBlock allocateSection(uintptr_t Size); 63b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor 64b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor RemoteTarget *Target; 65706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 66b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylorpublic: 67b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor RemoteMemoryManager() : Target(NULL) {} 68b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor virtual ~RemoteMemoryManager(); 69706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 70706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, 716eb43d295625cd2ff314c59b49d4fd11f3348cadFilip Pizlo unsigned SectionID, StringRef SectionName); 72706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 73706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, 746eb43d295625cd2ff314c59b49d4fd11f3348cadFilip Pizlo unsigned SectionID, StringRef SectionName, 756eb43d295625cd2ff314c59b49d4fd11f3348cadFilip Pizlo bool IsReadOnly); 76706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 77706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach void *getPointerToNamedFunction(const std::string &Name, 78706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach bool AbortOnFailure = true); 7953608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor 80b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor void notifyObjectLoaded(ExecutionEngine *EE, const ObjectImage *Obj); 81b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 82b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor bool finalizeMemory(std::string *ErrMsg); 83b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 84b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // This is a non-interface function used by lli 85b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor void setRemoteTarget(RemoteTarget *T) { Target = T; } 8653608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor 87706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach // The following obsolete JITMemoryManager calls are stubbed out for 88706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach // this model. 89706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach void setMemoryWritable(); 90706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach void setMemoryExecutable(); 91706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach void setPoisonMemory(bool poison); 92706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach void AllocateGOT(); 93706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *getGOTBase() const; 94706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *startFunctionBody(const Function *F, uintptr_t &ActualSize); 95706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize, 96706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach unsigned Alignment); 97706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach void endFunctionBody(const Function *F, uint8_t *FunctionStart, 98706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *FunctionEnd); 99706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *allocateSpace(intptr_t Size, unsigned Alignment); 100706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment); 101706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach void deallocateFunctionBody(void *Body); 102706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach}; 103706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 104706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach} // end namespace llvm 105706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 106706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#endif 107