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 //===----------------------------------------------------------------------===//
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/Process.h"
22 DWORD getWindowsProtectionFlags(unsigned Flags) {
24 // Contrary to what you might expect, the Windows page protection flags
25 // are not a bitwise combination of RWX values
26 case llvm::sys::Memory::MF_READ:
28 case llvm::sys::Memory::MF_WRITE:
29 // Note: PAGE_WRITE is not supported by VirtualProtect
30 return PAGE_READWRITE;
31 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_WRITE:
32 return PAGE_READWRITE;
33 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_EXEC:
34 return PAGE_EXECUTE_READ;
35 case llvm::sys::Memory::MF_READ |
36 llvm::sys::Memory::MF_WRITE |
37 llvm::sys::Memory::MF_EXEC:
38 return PAGE_EXECUTE_READWRITE;
39 case llvm::sys::Memory::MF_EXEC:
42 llvm_unreachable("Illegal memory protection flag specified!");
44 // Provide a default return value as required by some compilers.
48 size_t getAllocationGranularity() {
50 ::GetSystemInfo(&Info);
51 if (Info.dwPageSize > Info.dwAllocationGranularity)
52 return Info.dwPageSize;
54 return Info.dwAllocationGranularity;
62 //===----------------------------------------------------------------------===//
63 //=== WARNING: Implementation here must contain only Win32 specific code
64 //=== and must not be UNIX code
65 //===----------------------------------------------------------------------===//
67 MemoryBlock Memory::allocateMappedMemory(size_t NumBytes,
68 const MemoryBlock *const NearBlock,
71 EC = error_code::success();
75 // While we'd be happy to allocate single pages, the Windows allocation
76 // granularity may be larger than a single page (in practice, it is 64K)
77 // so mapping less than that will create an unreachable fragment of memory.
78 static const size_t Granularity = getAllocationGranularity();
79 const size_t NumBlocks = (NumBytes+Granularity-1)/Granularity;
81 uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
85 // If the requested address is not aligned to the allocation granularity,
86 // round up to get beyond NearBlock. VirtualAlloc would have rounded down.
87 if (Start && Start % Granularity != 0)
88 Start += Granularity - Start % Granularity;
90 DWORD Protect = getWindowsProtectionFlags(Flags);
92 void *PA = ::VirtualAlloc(reinterpret_cast<void*>(Start),
93 NumBlocks*Granularity,
94 MEM_RESERVE | MEM_COMMIT, Protect);
97 // Try again without the NearBlock hint
98 return allocateMappedMemory(NumBytes, NULL, Flags, EC);
100 EC = error_code(::GetLastError(), system_category());
101 return MemoryBlock();
106 Result.Size = NumBlocks*Granularity;
109 Memory::InvalidateInstructionCache(Result.Address, Result.Size);
114 error_code Memory::releaseMappedMemory(MemoryBlock &M) {
115 if (M.Address == 0 || M.Size == 0)
116 return error_code::success();
118 if (!VirtualFree(M.Address, 0, MEM_RELEASE))
119 return error_code(::GetLastError(), system_category());
124 return error_code::success();
127 error_code Memory::protectMappedMemory(const MemoryBlock &M,
129 if (M.Address == 0 || M.Size == 0)
130 return error_code::success();
132 DWORD Protect = getWindowsProtectionFlags(Flags);
135 if (!VirtualProtect(M.Address, M.Size, Protect, &OldFlags))
136 return error_code(::GetLastError(), system_category());
139 Memory::InvalidateInstructionCache(M.Address, M.Size);
141 return error_code::success();
144 /// InvalidateInstructionCache - Before the JIT can run a block of code
145 /// that has been emitted it must invalidate the instruction cache on some
147 void Memory::InvalidateInstructionCache(
148 const void *Addr, size_t Len) {
149 FlushInstructionCache(GetCurrentProcess(), Addr, Len);
153 MemoryBlock Memory::AllocateRWX(size_t NumBytes,
154 const MemoryBlock *NearBlock,
155 std::string *ErrMsg) {
158 MB = allocateMappedMemory(NumBytes, NearBlock,
159 MF_READ|MF_WRITE|MF_EXEC, EC);
160 if (EC != error_code::success() && ErrMsg) {
161 MakeErrMsg(ErrMsg, EC.message());
166 bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
167 error_code EC = releaseMappedMemory(M);
168 if (EC == error_code::success())
170 MakeErrMsg(ErrMsg, EC.message());
174 static DWORD getProtection(const void *addr) {
175 MEMORY_BASIC_INFORMATION info;
176 if (sizeof(info) == ::VirtualQuery(addr, &info, sizeof(info))) {
182 bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
183 if (!setRangeWritable(M.Address, M.Size)) {
184 return MakeErrMsg(ErrMsg, "Cannot set memory to writeable: ");
189 bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
190 if (!setRangeExecutable(M.Address, M.Size)) {
191 return MakeErrMsg(ErrMsg, "Cannot set memory to executable: ");
196 bool Memory::setRangeWritable(const void *Addr, size_t Size) {
197 DWORD prot = getProtection(Addr);
201 if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) {
202 prot = PAGE_EXECUTE_READWRITE;
203 } else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) {
204 prot = PAGE_READWRITE;
208 Memory::InvalidateInstructionCache(Addr, Size);
209 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
213 bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
214 DWORD prot = getProtection(Addr);
218 if (prot == PAGE_NOACCESS) {
220 } else if (prot == PAGE_READONLY) {
221 prot = PAGE_EXECUTE_READ;
222 } else if (prot == PAGE_READWRITE) {
223 prot = PAGE_EXECUTE_READWRITE;
227 Memory::InvalidateInstructionCache(Addr, Size);
228 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)