1 //===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file provides the Win32 specific implementation of various Memory
10 // management utilities
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/Process.h"
17 #include "llvm/Support/WindowsError.h"
19 // The Windows.h header must be the last one included.
20 #include "WindowsSupport.h"
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,
72 std::error_code &EC) {
73 EC = std::error_code();
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 // Avoid using one-time initialization of static locals here, since they
81 // aren't thread safe with MSVC.
82 static volatile size_t GranularityCached;
83 size_t Granularity = GranularityCached;
84 if (Granularity == 0) {
85 Granularity = getAllocationGranularity();
86 GranularityCached = Granularity;
89 const size_t NumBlocks = (NumBytes+Granularity-1)/Granularity;
91 uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
95 // If the requested address is not aligned to the allocation granularity,
96 // round up to get beyond NearBlock. VirtualAlloc would have rounded down.
97 if (Start && Start % Granularity != 0)
98 Start += Granularity - Start % Granularity;
100 DWORD Protect = getWindowsProtectionFlags(Flags);
102 void *PA = ::VirtualAlloc(reinterpret_cast<void*>(Start),
103 NumBlocks*Granularity,
104 MEM_RESERVE | MEM_COMMIT, Protect);
107 // Try again without the NearBlock hint
108 return allocateMappedMemory(NumBytes, NULL, Flags, EC);
110 EC = mapWindowsError(::GetLastError());
111 return MemoryBlock();
116 Result.Size = NumBlocks*Granularity;
119 Memory::InvalidateInstructionCache(Result.Address, Result.Size);
124 std::error_code Memory::releaseMappedMemory(MemoryBlock &M) {
125 if (M.Address == 0 || M.Size == 0)
126 return std::error_code();
128 if (!VirtualFree(M.Address, 0, MEM_RELEASE))
129 return mapWindowsError(::GetLastError());
134 return std::error_code();
137 std::error_code Memory::protectMappedMemory(const MemoryBlock &M,
139 if (M.Address == 0 || M.Size == 0)
140 return std::error_code();
142 DWORD Protect = getWindowsProtectionFlags(Flags);
145 if (!VirtualProtect(M.Address, M.Size, Protect, &OldFlags))
146 return mapWindowsError(::GetLastError());
149 Memory::InvalidateInstructionCache(M.Address, M.Size);
151 return std::error_code();
154 /// InvalidateInstructionCache - Before the JIT can run a block of code
155 /// that has been emitted it must invalidate the instruction cache on some
157 void Memory::InvalidateInstructionCache(
158 const void *Addr, size_t Len) {
159 FlushInstructionCache(GetCurrentProcess(), Addr, Len);