1 //===---- EPCGenericJITLinkMemoryManager.cpp -- Mem management via EPC ----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h" 10 #include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h" 11 12 #include <limits> 13 14 namespace llvm { 15 namespace orc { 16 17 class EPCGenericJITLinkMemoryManager::Alloc 18 : public jitlink::JITLinkMemoryManager::Allocation { 19 public: 20 struct SegInfo { 21 char *WorkingMem = nullptr; 22 ExecutorAddress TargetAddr; 23 uint64_t ContentSize = 0; 24 uint64_t ZeroFillSize = 0; 25 }; 26 using SegInfoMap = DenseMap<unsigned, SegInfo>; 27 28 Alloc(EPCGenericJITLinkMemoryManager &Parent, ExecutorAddress TargetAddr, 29 uint64_t TargetSize, std::unique_ptr<char[]> WorkingBuffer, 30 SegInfoMap Segs) 31 : Parent(Parent), TargetAddr(TargetAddr), TargetSize(TargetSize), 32 WorkingBuffer(std::move(WorkingBuffer)), Segs(std::move(Segs)) {} 33 34 MutableArrayRef<char> getWorkingMemory(ProtectionFlags Seg) override { 35 auto I = Segs.find(Seg); 36 assert(I != Segs.end() && "No allocation for seg"); 37 assert(I->second.ContentSize <= std::numeric_limits<size_t>::max()); 38 return {I->second.WorkingMem, static_cast<size_t>(I->second.ContentSize)}; 39 } 40 41 JITTargetAddress getTargetMemory(ProtectionFlags Seg) override { 42 auto I = Segs.find(Seg); 43 assert(I != Segs.end() && "No allocation for seg"); 44 return I->second.TargetAddr.getValue(); 45 } 46 47 void finalizeAsync(FinalizeContinuation OnFinalize) override { 48 char *WorkingMem = WorkingBuffer.get(); 49 tpctypes::FinalizeRequest FR; 50 for (auto &KV : Segs) { 51 assert(KV.second.ContentSize <= std::numeric_limits<size_t>::max()); 52 FR.push_back(tpctypes::SegFinalizeRequest{ 53 tpctypes::toWireProtectionFlags( 54 static_cast<sys::Memory::ProtectionFlags>(KV.first)), 55 KV.second.TargetAddr, 56 alignTo(KV.second.ContentSize + KV.second.ZeroFillSize, 57 Parent.EPC.getPageSize()), 58 {WorkingMem, static_cast<size_t>(KV.second.ContentSize)}}); 59 WorkingMem += KV.second.ContentSize; 60 } 61 Parent.EPC.callSPSWrapperAsync<shared::SPSOrcTargetProcessFinalize>( 62 [OnFinalize = std::move(OnFinalize)](Error SerializationErr, 63 Error FinalizeErr) { 64 if (SerializationErr) 65 OnFinalize(std::move(SerializationErr)); 66 else 67 OnFinalize(std::move(FinalizeErr)); 68 }, 69 Parent.FAs.Finalize.getValue(), std::move(FR)); 70 } 71 72 Error deallocate() override { 73 Error Err = Error::success(); 74 if (auto E2 = 75 Parent.EPC.callSPSWrapper<shared::SPSOrcTargetProcessDeallocate>( 76 Parent.FAs.Deallocate.getValue(), Err, TargetAddr, TargetSize)) 77 return E2; 78 return Err; 79 } 80 81 private: 82 EPCGenericJITLinkMemoryManager &Parent; 83 ExecutorAddress TargetAddr; 84 uint64_t TargetSize; 85 std::unique_ptr<char[]> WorkingBuffer; 86 SegInfoMap Segs; 87 }; 88 89 Expected<std::unique_ptr<jitlink::JITLinkMemoryManager::Allocation>> 90 EPCGenericJITLinkMemoryManager::allocate(const jitlink::JITLinkDylib *JD, 91 const SegmentsRequestMap &Request) { 92 Alloc::SegInfoMap Segs; 93 uint64_t AllocSize = 0; 94 size_t WorkingSize = 0; 95 for (auto &KV : Request) { 96 if (!isPowerOf2_64(KV.second.getAlignment())) 97 return make_error<StringError>("Alignment is not a power of two", 98 inconvertibleErrorCode()); 99 if (KV.second.getAlignment() > EPC.getPageSize()) 100 return make_error<StringError>("Alignment exceeds page size", 101 inconvertibleErrorCode()); 102 103 auto &Seg = Segs[KV.first]; 104 Seg.ContentSize = KV.second.getContentSize(); 105 Seg.ZeroFillSize = KV.second.getZeroFillSize(); 106 AllocSize += alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize()); 107 WorkingSize += Seg.ContentSize; 108 } 109 110 std::unique_ptr<char[]> WorkingBuffer; 111 if (WorkingSize > 0) 112 WorkingBuffer = std::make_unique<char[]>(WorkingSize); 113 Expected<ExecutorAddress> TargetAllocAddr((ExecutorAddress())); 114 if (auto Err = EPC.callSPSWrapper<shared::SPSOrcTargetProcessAllocate>( 115 FAs.Reserve.getValue(), TargetAllocAddr, AllocSize)) 116 return std::move(Err); 117 if (!TargetAllocAddr) 118 return TargetAllocAddr.takeError(); 119 120 char *WorkingMem = WorkingBuffer.get(); 121 JITTargetAddress SegAddr = TargetAllocAddr->getValue(); 122 for (auto &KV : Segs) { 123 auto &Seg = KV.second; 124 Seg.TargetAddr.setValue(SegAddr); 125 SegAddr += alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize()); 126 Seg.WorkingMem = WorkingMem; 127 WorkingMem += Seg.ContentSize; 128 } 129 130 return std::make_unique<Alloc>(*this, *TargetAllocAddr, AllocSize, 131 std::move(WorkingBuffer), std::move(Segs)); 132 } 133 134 } // end namespace orc 135 } // end namespace llvm 136