1 //===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of various Memory
11 // management utilities
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/Process.h"
18 #include "llvm/Support/WindowsError.h"
20 // The Windows.h header must be the last one included.
21 #include "WindowsSupport.h"
25 DWORD getWindowsProtectionFlags(unsigned Flags) {
27 // Contrary to what you might expect, the Windows page protection flags
28 // are not a bitwise combination of RWX values
29 case llvm::sys::Memory::MF_READ:
31 case llvm::sys::Memory::MF_WRITE:
32 // Note: PAGE_WRITE is not supported by VirtualProtect
33 return PAGE_READWRITE;
34 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_WRITE:
35 return PAGE_READWRITE;
36 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_EXEC:
37 return PAGE_EXECUTE_READ;
38 case llvm::sys::Memory::MF_READ |
39 llvm::sys::Memory::MF_WRITE |
40 llvm::sys::Memory::MF_EXEC:
41 return PAGE_EXECUTE_READWRITE;
42 case llvm::sys::Memory::MF_EXEC:
45 llvm_unreachable("Illegal memory protection flag specified!");
47 // Provide a default return value as required by some compilers.
51 size_t getAllocationGranularity() {
53 ::GetSystemInfo(&Info);
54 if (Info.dwPageSize > Info.dwAllocationGranularity)
55 return Info.dwPageSize;
57 return Info.dwAllocationGranularity;
65 //===----------------------------------------------------------------------===//
66 //=== WARNING: Implementation here must contain only Win32 specific code
67 //=== and must not be UNIX code
68 //===----------------------------------------------------------------------===//
70 MemoryBlock Memory::allocateMappedMemory(size_t NumBytes,
71 const MemoryBlock *const NearBlock,
73 std::error_code &EC) {
74 EC = std::error_code();
78 // While we'd be happy to allocate single pages, the Windows allocation
79 // granularity may be larger than a single page (in practice, it is 64K)
80 // so mapping less than that will create an unreachable fragment of memory.
81 static const size_t Granularity = getAllocationGranularity();
82 const size_t NumBlocks = (NumBytes+Granularity-1)/Granularity;
84 uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
88 // If the requested address is not aligned to the allocation granularity,
89 // round up to get beyond NearBlock. VirtualAlloc would have rounded down.
90 if (Start && Start % Granularity != 0)
91 Start += Granularity - Start % Granularity;
93 DWORD Protect = getWindowsProtectionFlags(Flags);
95 void *PA = ::VirtualAlloc(reinterpret_cast<void*>(Start),
96 NumBlocks*Granularity,
97 MEM_RESERVE | MEM_COMMIT, Protect);
100 // Try again without the NearBlock hint
101 return allocateMappedMemory(NumBytes, NULL, Flags, EC);
103 EC = mapWindowsError(::GetLastError());
104 return MemoryBlock();
109 Result.Size = NumBlocks*Granularity;
112 Memory::InvalidateInstructionCache(Result.Address, Result.Size);
117 std::error_code Memory::releaseMappedMemory(MemoryBlock &M) {
118 if (M.Address == 0 || M.Size == 0)
119 return std::error_code();
121 if (!VirtualFree(M.Address, 0, MEM_RELEASE))
122 return mapWindowsError(::GetLastError());
127 return std::error_code();
130 std::error_code Memory::protectMappedMemory(const MemoryBlock &M,
132 if (M.Address == 0 || M.Size == 0)
133 return std::error_code();
135 DWORD Protect = getWindowsProtectionFlags(Flags);
138 if (!VirtualProtect(M.Address, M.Size, Protect, &OldFlags))
139 return mapWindowsError(::GetLastError());
142 Memory::InvalidateInstructionCache(M.Address, M.Size);
144 return std::error_code();
147 /// InvalidateInstructionCache - Before the JIT can run a block of code
148 /// that has been emitted it must invalidate the instruction cache on some
150 void Memory::InvalidateInstructionCache(
151 const void *Addr, size_t Len) {
152 FlushInstructionCache(GetCurrentProcess(), Addr, Len);
156 MemoryBlock Memory::AllocateRWX(size_t NumBytes,
157 const MemoryBlock *NearBlock,
158 std::string *ErrMsg) {
161 MB = allocateMappedMemory(NumBytes, NearBlock,
162 MF_READ|MF_WRITE|MF_EXEC, EC);
163 if (EC != std::error_code() && ErrMsg) {
164 MakeErrMsg(ErrMsg, EC.message());
169 bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
170 std::error_code EC = releaseMappedMemory(M);
171 if (EC == std::error_code())
173 MakeErrMsg(ErrMsg, EC.message());
177 static DWORD getProtection(const void *addr) {
178 MEMORY_BASIC_INFORMATION info;
179 if (sizeof(info) == ::VirtualQuery(addr, &info, sizeof(info))) {
185 bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
186 if (!setRangeWritable(M.Address, M.Size)) {
187 return MakeErrMsg(ErrMsg, "Cannot set memory to writeable: ");
192 bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
193 if (!setRangeExecutable(M.Address, M.Size)) {
194 return MakeErrMsg(ErrMsg, "Cannot set memory to executable: ");
199 bool Memory::setRangeWritable(const void *Addr, size_t Size) {
200 DWORD prot = getProtection(Addr);
204 if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) {
205 prot = PAGE_EXECUTE_READWRITE;
206 } else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) {
207 prot = PAGE_READWRITE;
211 Memory::InvalidateInstructionCache(Addr, Size);
212 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
216 bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
217 DWORD prot = getProtection(Addr);
221 if (prot == PAGE_NOACCESS) {
223 } else if (prot == PAGE_READONLY) {
224 prot = PAGE_EXECUTE_READ;
225 } else if (prot == PAGE_READWRITE) {
226 prot = PAGE_EXECUTE_READWRITE;
230 Memory::InvalidateInstructionCache(Addr, Size);
231 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)