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"
19 // The Windows.h header must be the last one included.
24 DWORD getWindowsProtectionFlags(unsigned Flags) {
26 // Contrary to what you might expect, the Windows page protection flags
27 // are not a bitwise combination of RWX values
28 case llvm::sys::Memory::MF_READ:
30 case llvm::sys::Memory::MF_WRITE:
31 // Note: PAGE_WRITE is not supported by VirtualProtect
32 return PAGE_READWRITE;
33 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_WRITE:
34 return PAGE_READWRITE;
35 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_EXEC:
36 return PAGE_EXECUTE_READ;
37 case llvm::sys::Memory::MF_READ |
38 llvm::sys::Memory::MF_WRITE |
39 llvm::sys::Memory::MF_EXEC:
40 return PAGE_EXECUTE_READWRITE;
41 case llvm::sys::Memory::MF_EXEC:
44 llvm_unreachable("Illegal memory protection flag specified!");
46 // Provide a default return value as required by some compilers.
50 size_t getAllocationGranularity() {
52 ::GetSystemInfo(&Info);
53 if (Info.dwPageSize > Info.dwAllocationGranularity)
54 return Info.dwPageSize;
56 return Info.dwAllocationGranularity;
64 //===----------------------------------------------------------------------===//
65 //=== WARNING: Implementation here must contain only Win32 specific code
66 //=== and must not be UNIX code
67 //===----------------------------------------------------------------------===//
69 MemoryBlock Memory::allocateMappedMemory(size_t NumBytes,
70 const MemoryBlock *const NearBlock,
73 EC = error_code::success();
77 // While we'd be happy to allocate single pages, the Windows allocation
78 // granularity may be larger than a single page (in practice, it is 64K)
79 // so mapping less than that will create an unreachable fragment of memory.
80 static const size_t Granularity = getAllocationGranularity();
81 const size_t NumBlocks = (NumBytes+Granularity-1)/Granularity;
83 uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
87 // If the requested address is not aligned to the allocation granularity,
88 // round up to get beyond NearBlock. VirtualAlloc would have rounded down.
89 if (Start && Start % Granularity != 0)
90 Start += Granularity - Start % Granularity;
92 DWORD Protect = getWindowsProtectionFlags(Flags);
94 void *PA = ::VirtualAlloc(reinterpret_cast<void*>(Start),
95 NumBlocks*Granularity,
96 MEM_RESERVE | MEM_COMMIT, Protect);
99 // Try again without the NearBlock hint
100 return allocateMappedMemory(NumBytes, NULL, Flags, EC);
102 EC = error_code(::GetLastError(), system_category());
103 return MemoryBlock();
108 Result.Size = NumBlocks*Granularity;
111 Memory::InvalidateInstructionCache(Result.Address, Result.Size);
116 error_code Memory::releaseMappedMemory(MemoryBlock &M) {
117 if (M.Address == 0 || M.Size == 0)
118 return error_code::success();
120 if (!VirtualFree(M.Address, 0, MEM_RELEASE))
121 return error_code(::GetLastError(), system_category());
126 return error_code::success();
129 error_code Memory::protectMappedMemory(const MemoryBlock &M,
131 if (M.Address == 0 || M.Size == 0)
132 return error_code::success();
134 DWORD Protect = getWindowsProtectionFlags(Flags);
137 if (!VirtualProtect(M.Address, M.Size, Protect, &OldFlags))
138 return error_code(::GetLastError(), system_category());
141 Memory::InvalidateInstructionCache(M.Address, M.Size);
143 return error_code::success();
146 /// InvalidateInstructionCache - Before the JIT can run a block of code
147 /// that has been emitted it must invalidate the instruction cache on some
149 void Memory::InvalidateInstructionCache(
150 const void *Addr, size_t Len) {
151 FlushInstructionCache(GetCurrentProcess(), Addr, Len);
155 MemoryBlock Memory::AllocateRWX(size_t NumBytes,
156 const MemoryBlock *NearBlock,
157 std::string *ErrMsg) {
160 MB = allocateMappedMemory(NumBytes, NearBlock,
161 MF_READ|MF_WRITE|MF_EXEC, EC);
162 if (EC != error_code::success() && ErrMsg) {
163 MakeErrMsg(ErrMsg, EC.message());
168 bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
169 error_code EC = releaseMappedMemory(M);
170 if (EC == error_code::success())
172 MakeErrMsg(ErrMsg, EC.message());
176 static DWORD getProtection(const void *addr) {
177 MEMORY_BASIC_INFORMATION info;
178 if (sizeof(info) == ::VirtualQuery(addr, &info, sizeof(info))) {
184 bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
185 if (!setRangeWritable(M.Address, M.Size)) {
186 return MakeErrMsg(ErrMsg, "Cannot set memory to writeable: ");
191 bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
192 if (!setRangeExecutable(M.Address, M.Size)) {
193 return MakeErrMsg(ErrMsg, "Cannot set memory to executable: ");
198 bool Memory::setRangeWritable(const void *Addr, size_t Size) {
199 DWORD prot = getProtection(Addr);
203 if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) {
204 prot = PAGE_EXECUTE_READWRITE;
205 } else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) {
206 prot = PAGE_READWRITE;
210 Memory::InvalidateInstructionCache(Addr, Size);
211 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
215 bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
216 DWORD prot = getProtection(Addr);
220 if (prot == PAGE_NOACCESS) {
222 } else if (prot == PAGE_READONLY) {
223 prot = PAGE_EXECUTE_READ;
224 } else if (prot == PAGE_READWRITE) {
225 prot = PAGE_EXECUTE_READWRITE;
229 Memory::InvalidateInstructionCache(Addr, Size);
230 return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)