1f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//===- llvm/Support/Memory.h - Memory Support -------------------*- C++ -*-===//
263b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
3e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer//                     The LLVM Compiler Infrastructure
4e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
763b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
8e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer//===----------------------------------------------------------------------===//
9e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer//
10e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer// This file declares the llvm::sys::Memory class.
11e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer//
12e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer//===----------------------------------------------------------------------===//
13e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer
14674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_SUPPORT_MEMORY_H
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_SUPPORT_MEMORY_H
16e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer
171f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h"
18bed22d89020ea8a46c48bc1aa6128321a445dbb2Chris Lattner#include <string>
19c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include <system_error>
20bed22d89020ea8a46c48bc1aa6128321a445dbb2Chris Lattner
21e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencernamespace llvm {
22e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencernamespace sys {
23e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer
2433189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  /// This class encapsulates the notion of a memory block which has an address
2533189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  /// and a size. It is used by the Memory class (a friend) as the result of
2633189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  /// various memory allocation operations.
2733189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  /// @see Memory
2833189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  /// @brief Memory block abstraction.
2933189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  class MemoryBlock {
3033189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  public:
31dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    MemoryBlock() : Address(nullptr), Size(0) { }
3210b4fc552f984dc978298d50c09c97c0764962fcReid Kleckner    MemoryBlock(void *addr, size_t size) : Address(addr), Size(size) { }
33bed22d89020ea8a46c48bc1aa6128321a445dbb2Chris Lattner    void *base() const { return Address; }
3410b4fc552f984dc978298d50c09c97c0764962fcReid Kleckner    size_t size() const { return Size; }
35f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
3633189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  private:
37bed22d89020ea8a46c48bc1aa6128321a445dbb2Chris Lattner    void *Address;    ///< Address of first byte of memory area
3810b4fc552f984dc978298d50c09c97c0764962fcReid Kleckner    size_t Size;      ///< Size, in bytes of the memory area
3933189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer    friend class Memory;
4033189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  };
4133189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer
4233189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  /// This class provides various memory handling functions that manipulate
4333189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  /// MemoryBlock instances.
44e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer  /// @since 1.4
4533189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer  /// @brief An abstraction for memory operations.
46e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer  class Memory {
4793bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner  public:
48bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    enum ProtectionFlags {
49bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor      MF_READ  = 0x1000000,
50bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor      MF_WRITE = 0x2000000,
51bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor      MF_EXEC  = 0x4000000
52bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    };
53bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor
54bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// This method allocates a block of memory that is suitable for loading
55bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// dynamically generated code (e.g. JIT). An attempt to allocate
56bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// \p NumBytes bytes of virtual memory is made.
57bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// \p NearBlock may point to an existing allocation in which case
58bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// an attempt is made to allocate more memory near the existing block.
59bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// The actual allocated address is not guaranteed to be near the requested
60bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// address.
61bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// \p Flags is used to set the initial protection flags for the block
62bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// of the memory.
63bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// \p EC [out] returns an object describing any error that occurs.
64bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    ///
65bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// This method may allocate more than the number of bytes requested.  The
66bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// actual number of bytes allocated is indicated in the returned
67bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// MemoryBlock.
68bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    ///
69bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// The start of the allocated block must be aligned with the
70bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// system allocation granularity (64K on Windows, page size on Linux).
71bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// If the address following \p NearBlock is not so aligned, it will be
72bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// rounded up to the next allocation granularity boundary.
73bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    ///
74f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    /// \r a non-null MemoryBlock if the function was successful,
75bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// otherwise a null MemoryBlock is with \p EC describing the error.
76bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    ///
77bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// @brief Allocate mapped memory.
78bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    static MemoryBlock allocateMappedMemory(size_t NumBytes,
79bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor                                            const MemoryBlock *const NearBlock,
80bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor                                            unsigned Flags,
81c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                            std::error_code &EC);
82bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor
83bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// This method releases a block of memory that was allocated with the
84bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// allocateMappedMemory method. It should not be used to release any
85bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// memory block allocated any other way.
86bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// \p Block describes the memory to be released.
87bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    ///
88bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// \r error_success if the function was successful, or an error_code
89bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// describing the failure if an error occurred.
90f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    ///
91bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// @brief Release mapped memory.
92c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    static std::error_code releaseMappedMemory(MemoryBlock &Block);
93bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor
94bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// This method sets the protection flags for a block of memory to the
95bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// state specified by /p Flags.  The behavior is not specified if the
96bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// memory was not allocated using the allocateMappedMemory method.
97bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// \p Block describes the memory block to be protected.
98bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// \p Flags specifies the new protection state to be assigned to the block.
9936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    /// \p ErrMsg [out] returns a string describing any error that occurred.
100bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    ///
101bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// If \p Flags is MF_WRITE, the actual behavior varies
102652b28dbcac156bb6a3b596bdf29aaf17e89eda5Andrew Kaylor    /// with the operating system (i.e. MF_READ | MF_WRITE on Windows) and the
103652b28dbcac156bb6a3b596bdf29aaf17e89eda5Andrew Kaylor    /// target architecture (i.e. MF_WRITE -> MF_READ | MF_WRITE on i386).
104bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    ///
105bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// \r error_success if the function was successful, or an error_code
106bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// describing the failure if an error occurred.
107bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    ///
108bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor    /// @brief Set memory protection state.
109c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    static std::error_code protectMappedMemory(const MemoryBlock &Block,
110c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                               unsigned Flags);
111bbf628b6cefc8d817eb9ec04c2a357ad3f27d618Andrew Kaylor
11293bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// This method allocates a block of Read/Write/Execute memory that is
11393bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// suitable for executing dynamically generated code (e.g. JIT). An
11493bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// attempt to allocate \p NumBytes bytes of virtual memory is made.
11593bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// \p NearBlock may point to an existing allocation in which case
11693bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// an attempt is made to allocate more memory near the existing block.
11793bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    ///
11893bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// On success, this returns a non-null memory block, otherwise it returns
11993bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// a null memory block and fills in *ErrMsg.
1201f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer    ///
12193bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// @brief Allocate Read/Write/Execute memory.
12210b4fc552f984dc978298d50c09c97c0764962fcReid Kleckner    static MemoryBlock AllocateRWX(size_t NumBytes,
12393bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner                                   const MemoryBlock *NearBlock,
124dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                   std::string *ErrMsg = nullptr);
12533189e787b98c79bd03be466ec19dfb2f65eb65fReid Spencer
12693bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// This method releases a block of Read/Write/Execute memory that was
12793bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// allocated with the AllocateRWX method. It should not be used to
12893bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// release any memory block allocated any other way.
12993bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    ///
13093bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// On success, this returns false, otherwise it returns true and fills
13193bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// in *ErrMsg.
13293bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// @brief Release Read/Write/Execute memory.
133dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    static bool ReleaseRWX(MemoryBlock &block, std::string *ErrMsg = nullptr);
1341f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer
13593bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// InvalidateInstructionCache - Before the JIT can run a block of code
13693bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// that has been emitted it must invalidate the instruction cache on some
13793bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    /// platforms.
13893bb4aa0ce43bea18401e2444bf6d34af19c6084Chris Lattner    static void InvalidateInstructionCache(const void *Addr, size_t Len);
139bc4707a2554ac04ba006bf70035e7bc7270236a9Evan Cheng
140cce6c297c54b4c9c8615c77e97cd64e70812ea60Jim Grosbach    /// setExecutable - Before the JIT can run a block of code, it has to be
141bc4707a2554ac04ba006bf70035e7bc7270236a9Evan Cheng    /// given read and executable privilege. Return true if it is already r-x
142bc4707a2554ac04ba006bf70035e7bc7270236a9Evan Cheng    /// or the system is able to change its previlege.
143dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    static bool setExecutable(MemoryBlock &M, std::string *ErrMsg = nullptr);
144cce6c297c54b4c9c8615c77e97cd64e70812ea60Jim Grosbach
145cce6c297c54b4c9c8615c77e97cd64e70812ea60Jim Grosbach    /// setWritable - When adding to a block of code, the JIT may need
146cce6c297c54b4c9c8615c77e97cd64e70812ea60Jim Grosbach    /// to mark a block of code as RW since the protections are on page
147cce6c297c54b4c9c8615c77e97cd64e70812ea60Jim Grosbach    /// boundaries, and the JIT internal allocations are not page aligned.
148dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    static bool setWritable(MemoryBlock &M, std::string *ErrMsg = nullptr);
149932a32d2512353478d16c4101582adff404a55a5Jim Grosbach
1501f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer    /// setRangeExecutable - Mark the page containing a range of addresses
151932a32d2512353478d16c4101582adff404a55a5Jim Grosbach    /// as executable.
152932a32d2512353478d16c4101582adff404a55a5Jim Grosbach    static bool setRangeExecutable(const void *Addr, size_t Size);
153932a32d2512353478d16c4101582adff404a55a5Jim Grosbach
1541f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer    /// setRangeWritable - Mark the page containing a range of addresses
155932a32d2512353478d16c4101582adff404a55a5Jim Grosbach    /// as writable.
156932a32d2512353478d16c4101582adff404a55a5Jim Grosbach    static bool setRangeWritable(const void *Addr, size_t Size);
157e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer  };
158f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
159f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// Owning version of MemoryBlock.
160f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  class OwningMemoryBlock {
161f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  public:
162f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    OwningMemoryBlock() = default;
163f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    explicit OwningMemoryBlock(MemoryBlock M) : M(M) {}
164f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    OwningMemoryBlock(OwningMemoryBlock &&Other) {
165f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      M = Other.M;
166f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      Other.M = MemoryBlock();
167f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    }
168f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    OwningMemoryBlock& operator=(OwningMemoryBlock &&Other) {
169f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      M = Other.M;
170f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      Other.M = MemoryBlock();
171f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      return *this;
172f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    }
173f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    ~OwningMemoryBlock() {
174f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      Memory::releaseMappedMemory(M);
175f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    }
176f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    void *base() const { return M.base(); }
177f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    size_t size() const { return M.size(); }
178f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    MemoryBlock getMemoryBlock() const { return M; }
179f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  private:
180f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    MemoryBlock M;
181f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  };
182f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
183e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer}
184e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer}
185e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer
186e5d6ca43d71e9e541d2b515c954406cb9a199446Reid Spencer#endif
187