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 1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "RemoteTarget.h" 19b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor#include "llvm/ADT/DenseMap.h" 20706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include "llvm/ADT/SmallVector.h" 21706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include "llvm/ExecutionEngine/JITMemoryManager.h" 22706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include "llvm/Support/ErrorHandling.h" 23706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include "llvm/Support/Memory.h" 24706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#include <utility> 25706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 26706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachnamespace llvm { 27706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 28b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylorclass RemoteMemoryManager : public JITMemoryManager { 29706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachpublic: 30b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // Notice that this structure takes ownership of the memory allocated. 31b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor struct Allocation { 32e3fd646e178f92dbe2737a5230d73577090d9d0eAndrew Kaylor Allocation() {} 33b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor Allocation(sys::MemoryBlock mb, unsigned a, bool code) 34b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor : MB(mb), Alignment(a), IsCode(code) {} 35b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 36b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor sys::MemoryBlock MB; 37b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor unsigned Alignment; 38b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor bool IsCode; 39b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor }; 40706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 41706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbachprivate: 42b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // This vector contains Allocation objects for all sections which we have 43b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // allocated. This vector effectively owns the memory associated with the 44b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // allocations. 45b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor SmallVector<Allocation, 2> AllocatedSections; 46b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 47b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // This vector contains pointers to Allocation objects for any sections we 48b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // have allocated locally but have not yet remapped for the remote target. 49b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // When we receive notification of a completed module load, we will map 50b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // these sections into the remote target. 51e3fd646e178f92dbe2737a5230d73577090d9d0eAndrew Kaylor SmallVector<Allocation, 2> UnmappedSections; 52b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 53b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // This map tracks the sections we have remapped for the remote target 54b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // but have not yet copied to the target. 55e3fd646e178f92dbe2737a5230d73577090d9d0eAndrew Kaylor DenseMap<uint64_t, Allocation> MappedSections; 56706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 57b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor // FIXME: This is part of a work around to keep sections near one another 58b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor // when MCJIT performs relocations after code emission but before 59b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor // the generated code is moved to the remote target. 60b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor sys::MemoryBlock Near; 61b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor sys::MemoryBlock allocateSection(uintptr_t Size); 62b90cc2fa0b50321e32786e87f8425658d88982eeAndrew Kaylor 63b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor RemoteTarget *Target; 64706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 65b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylorpublic: 66dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines RemoteMemoryManager() : Target(nullptr) {} 67b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor virtual ~RemoteMemoryManager(); 68706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 69706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, 7036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned SectionID, 7136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines StringRef SectionName) override; 72706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 73706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, 746eb43d295625cd2ff314c59b49d4fd11f3348cadFilip Pizlo unsigned SectionID, StringRef SectionName, 7536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool IsReadOnly) override; 76706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 77528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor // For now, remote symbol resolution is not support in lli. The MCJIT 78528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor // interface does support this, but clients must provide their own 79528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor // mechanism for finding remote symbol addresses. MCJIT will resolve 80528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor // symbols from Modules it contains. 8136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines uint64_t getSymbolAddress(const std::string &Name) override { return 0; } 8253608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor 8336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void notifyObjectLoaded(ExecutionEngine *EE, const ObjectImage *Obj) override; 84b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 8536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool finalizeMemory(std::string *ErrMsg) override; 86b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor 87528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor // For now, remote EH frame registration isn't supported. Remote symbol 88528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor // resolution is a prerequisite to supporting remote EH frame registration. 8936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, 9036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines size_t Size) override {} 9136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, 9236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines size_t Size) override {} 93528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor 94b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor // This is a non-interface function used by lli 95b868e9101c138016aad5bd910b67f40a3213d6fcAndrew Kaylor void setRemoteTarget(RemoteTarget *T) { Target = T; } 9653608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor 97706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach // The following obsolete JITMemoryManager calls are stubbed out for 98706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach // this model. 9936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void setMemoryWritable() override; 10036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void setMemoryExecutable() override; 10136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void setPoisonMemory(bool poison) override; 10236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void AllocateGOT() override; 10336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines uint8_t *getGOTBase() const override; 10436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines uint8_t *startFunctionBody(const Function *F, uintptr_t &ActualSize) override; 105706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize, 10636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned Alignment) override; 107706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach void endFunctionBody(const Function *F, uint8_t *FunctionStart, 10836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines uint8_t *FunctionEnd) override; 10936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) override; 11036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) override; 11136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void deallocateFunctionBody(void *Body) override; 112706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach}; 113706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 114706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach} // end namespace llvm 115706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach 116706f03a35db7029b2dbd2925552eb0d0472dcbb4Jim Grosbach#endif 117