SectionMemoryManager.h revision 327952
148187Skato//===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- C++ -*-===//
248516Skato//
348516Skato//                     The LLVM Compiler Infrastructure
448516Skato//
548516Skato// This file is distributed under the University of Illinois Open Source
648516Skato// License. See LICENSE.TXT for details.
748516Skato//
848516Skato//===----------------------------------------------------------------------===//
948516Skato//
1048516Skato// This file contains the declaration of a section-based memory manager used by
1148516Skato// the MCJIT execution engine and RuntimeDyld.
1248516Skato//
1348516Skato//===----------------------------------------------------------------------===//
1448516Skato
1548516Skato#ifndef LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
1648516Skato#define LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
1748516Skato
1848516Skato#include "llvm/ADT/SmallVector.h"
1948516Skato#include "llvm/ADT/StringRef.h"
2048516Skato#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
2148516Skato#include "llvm/Support/Memory.h"
2248516Skato#include <cstdint>
2348516Skato#include <string>
2448516Skato#include <system_error>
2548516Skato
2650477Speternamespace llvm {
2748187Skato
2848187Skato/// This is a simple memory manager which implements the methods called by
2948187Skato/// the RuntimeDyld class to allocate memory for section-based loading of
3048187Skato/// objects, usually those generated by the MCJIT execution engine.
3148187Skato///
3248187Skato/// This memory manager allocates all section memory as read-write.  The
3348187Skato/// RuntimeDyld will copy JITed section memory into these allocated blocks
3466870Skato/// and perform any necessary linking and relocations.
3566870Skato///
3648187Skato/// Any client using this memory manager MUST ensure that section-specific
3748187Skato/// page permissions have been applied before attempting to execute functions
3848187Skato/// in the JITed object.  Permissions can be applied either by calling
3948187Skato/// MCJIT::finalizeObject or by calling SectionMemoryManager::finalizeMemory
4048187Skato/// directly.  Clients of MCJIT should call MCJIT::finalizeObject.
4148187Skatoclass SectionMemoryManager : public RTDyldMemoryManager {
4280371Snyanpublic:
4380371Snyan  /// This enum describes the various reasons to allocate pages from
4448187Skato  /// allocateMappedMemory.
4580371Snyan  enum class AllocationPurpose {
4680371Snyan    Code,
4780371Snyan    ROData,
4848187Skato    RWData,
4948187Skato  };
5048187Skato
5180371Snyan  /// Implementations of this interface are used by SectionMemoryManager to
5280371Snyan  /// request pages from the operating system.
5380371Snyan  class MemoryMapper {
5480371Snyan  public:
5580371Snyan    /// This method attempts to allocate \p NumBytes bytes of virtual memory for
5680371Snyan    /// \p Purpose.  \p NearBlock may point to an existing allocation, in which
5780371Snyan    /// case an attempt is made to allocate more memory near the existing block.
5880371Snyan    /// The actual allocated address is not guaranteed to be near the requested
5980371Snyan    /// address.  \p Flags is used to set the initial protection flags for the
6080371Snyan    /// block of the memory.  \p EC [out] returns an object describing any error
6180371Snyan    /// that occurs.
6280371Snyan    ///
6380371Snyan    /// This method may allocate more than the number of bytes requested.  The
6480371Snyan    /// actual number of bytes allocated is indicated in the returned
6580371Snyan    /// MemoryBlock.
6680371Snyan    ///
6780371Snyan    /// The start of the allocated block must be aligned with the system
6848187Skato    /// allocation granularity (64K on Windows, page size on Linux).  If the
6980371Snyan    /// address following \p NearBlock is not so aligned, it will be rounded up
7080371Snyan    /// to the next allocation granularity boundary.
7180371Snyan    ///
7280371Snyan    /// \r a non-null MemoryBlock if the function was successful, otherwise a
7380371Snyan    /// null MemoryBlock with \p EC describing the error.
7480371Snyan    virtual sys::MemoryBlock
7580371Snyan    allocateMappedMemory(AllocationPurpose Purpose, size_t NumBytes,
7680371Snyan                         const sys::MemoryBlock *const NearBlock,
7780371Snyan                         unsigned Flags, std::error_code &EC) = 0;
7880371Snyan
7980371Snyan    /// This method sets the protection flags for a block of memory to the state
8080371Snyan    /// specified by \p Flags.  The behavior is not specified if the memory was
8180371Snyan    /// not allocated using the allocateMappedMemory method.
8280371Snyan    /// \p Block describes the memory block to be protected.
8380371Snyan    /// \p Flags specifies the new protection state to be assigned to the block.
8480371Snyan    ///
8580371Snyan    /// If \p Flags is MF_WRITE, the actual behavior varies with the operating
8680371Snyan    /// system (i.e. MF_READ | MF_WRITE on Windows) and the target architecture
8748187Skato    /// (i.e. MF_WRITE -> MF_READ | MF_WRITE on i386).
8848187Skato    ///
8948187Skato    /// \r error_success if the function was successful, or an error_code
9048187Skato    /// describing the failure if an error occurred.
9148187Skato    virtual std::error_code protectMappedMemory(const sys::MemoryBlock &Block,
9248187Skato                                                unsigned Flags) = 0;
9348187Skato
9448187Skato    /// This method releases a block of memory that was allocated with the
9548187Skato    /// allocateMappedMemory method. It should not be used to release any memory
96102265Snyan    /// block allocated any other way.
9748187Skato    /// \p Block describes the memory to be released.
9848187Skato    ///
9948187Skato    /// \r error_success if the function was successful, or an error_code
10048187Skato    /// describing the failure if an error occurred.
10148187Skato    virtual std::error_code releaseMappedMemory(sys::MemoryBlock &M) = 0;
10248187Skato
10348187Skato    virtual ~MemoryMapper();
10480371Snyan  };
10580371Snyan
106111119Simp  /// Creates a SectionMemoryManager instance with \p MM as the associated
107102265Snyan  /// memory mapper.  If \p MM is nullptr then a default memory mapper is used
10879702Snyan  /// that directly calls into the operating system.
10948187Skato  SectionMemoryManager(MemoryMapper *MM = nullptr);
11048187Skato  SectionMemoryManager(const SectionMemoryManager &) = delete;
11148187Skato  void operator=(const SectionMemoryManager &) = delete;
11248187Skato  ~SectionMemoryManager() override;
11348187Skato
11448187Skato  /// \brief Allocates a memory block of (at least) the given size suitable for
11548187Skato  /// executable code.
11648187Skato  ///
11748187Skato  /// The value of \p Alignment must be a power of two.  If \p Alignment is zero
11848187Skato  /// a default alignment of 16 will be used.
11948187Skato  uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
12048187Skato                               unsigned SectionID,
12148187Skato                               StringRef SectionName) override;
12248187Skato
12348187Skato  /// \brief Allocates a memory block of (at least) the given size suitable for
12448187Skato  /// executable code.
12548187Skato  ///
12648187Skato  /// The value of \p Alignment must be a power of two.  If \p Alignment is zero
12748187Skato  /// a default alignment of 16 will be used.
12848187Skato  uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
12948187Skato                               unsigned SectionID, StringRef SectionName,
13048187Skato                               bool isReadOnly) override;
13148187Skato
13248187Skato  /// \brief Update section-specific memory permissions and other attributes.
13348187Skato  ///
13448187Skato  /// This method is called when object loading is complete and section page
135102265Snyan  /// permissions can be applied.  It is up to the memory manager implementation
13648187Skato  /// to decide whether or not to act on this method.  The memory manager will
13748187Skato  /// typically allocate all sections as read-write and then apply specific
13848187Skato  /// permissions when this method is called.  Code sections cannot be executed
139102265Snyan  /// until this function has been called.  In addition, any cache coherency
14048187Skato  /// operations needed to reliably use the memory are also performed.
14148187Skato  ///
14248187Skato  /// \returns true if an error occurred, false otherwise.
14348187Skato  bool finalizeMemory(std::string *ErrMsg = nullptr) override;
14448187Skato
14579702Snyan  /// \brief Invalidate instruction cache for code sections.
14648187Skato  ///
14748187Skato  /// Some platforms with separate data cache and instruction cache require
14848187Skato  /// explicit cache flush, otherwise JIT code manipulations (like resolved
14948187Skato  /// relocations) will get to the data cache but not to the instruction cache.
15048187Skato  ///
15148187Skato  /// This method is called from finalizeMemory.
15248187Skato  virtual void invalidateInstructionCache();
15348187Skato
15448187Skatoprivate:
15548187Skato  struct FreeMemBlock {
15648187Skato    // The actual block of free memory
15748187Skato    sys::MemoryBlock Free;
15880371Snyan    // If there is a pending allocation from the same reservation right before
15980371Snyan    // this block, store it's index in PendingMem, to be able to update the
16048187Skato    // pending region if part of this block is allocated, rather than having to
16180371Snyan    // create a new one
16248187Skato    unsigned PendingPrefixIndex;
16380371Snyan  };
16448187Skato
16548187Skato  struct MemoryGroup {
16648187Skato    // PendingMem contains all blocks of memory (subblocks of AllocatedMem)
16748187Skato    // which have not yet had their permissions applied, but have been given
16848187Skato    // out to the user. FreeMem contains all block of memory, which have
16980371Snyan    // neither had their permissions applied, nor been given out to the user.
17080371Snyan    SmallVector<sys::MemoryBlock, 16> PendingMem;
17148187Skato    SmallVector<FreeMemBlock, 16> FreeMem;
17287886Snyan
17348187Skato    // All memory blocks that have been requested from the system
17487886Snyan    SmallVector<sys::MemoryBlock, 16> AllocatedMem;
17548187Skato
17648187Skato    sys::MemoryBlock Near;
17780371Snyan  };
17880371Snyan
17948187Skato  uint8_t *allocateSection(AllocationPurpose Purpose, uintptr_t Size,
18048187Skato                           unsigned Alignment);
18180371Snyan
18280371Snyan  std::error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup,
18348187Skato                                              unsigned Permissions);
18480371Snyan
18580371Snyan  MemoryGroup CodeMem;
18648187Skato  MemoryGroup RWDataMem;
18748187Skato  MemoryGroup RODataMem;
18848187Skato  MemoryMapper &MMapper;
18980371Snyan};
19080371Snyan
19180371Snyan} // end namespace llvm
19280371Snyan
19380371Snyan#endif // LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H
19480371Snyan