1 //===- Unix/Memory.cpp - Generic UNIX System Configuration ------*- 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 defines some functions for various memory management utilities.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/Process.h"
19 #ifdef HAVE_SYS_MMAN_H
24 #include <mach/mach.h>
28 # if defined(__OpenBSD__)
29 # include <mips64/sysarch.h>
31 # include <sys/cachectl.h>
35 extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
39 int getPosixProtectionFlags(unsigned Flags) {
41 case llvm::sys::Memory::MF_READ:
43 case llvm::sys::Memory::MF_WRITE:
45 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_WRITE:
46 return PROT_READ | PROT_WRITE;
47 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_EXEC:
48 return PROT_READ | PROT_EXEC;
49 case llvm::sys::Memory::MF_READ |
50 llvm::sys::Memory::MF_WRITE |
51 llvm::sys::Memory::MF_EXEC:
52 return PROT_READ | PROT_WRITE | PROT_EXEC;
53 case llvm::sys::Memory::MF_EXEC:
56 llvm_unreachable("Illegal memory protection flag specified!");
58 // Provide a default return value as required by some compilers.
68 Memory::allocateMappedMemory(size_t NumBytes,
69 const MemoryBlock *const NearBlock,
72 EC = error_code::success();
76 static const size_t PageSize = Process::GetPageSize();
77 const size_t NumPages = (NumBytes+PageSize-1)/PageSize;
80 #ifdef NEED_DEV_ZERO_FOR_MMAP
81 static int zero_fd = open("/dev/zero", O_RDWR);
83 EC = error_code(errno, system_category());
89 int MMFlags = MAP_PRIVATE |
90 #ifdef HAVE_MMAP_ANONYMOUS
95 ; // Ends statement above
97 int Protect = getPosixProtectionFlags(PFlags);
99 // Use any near hint and the page size to set a page-aligned starting address
100 uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
101 NearBlock->size() : 0;
102 if (Start && Start % PageSize)
103 Start += PageSize - Start % PageSize;
105 void *Addr = ::mmap(reinterpret_cast<void*>(Start), PageSize*NumPages,
106 Protect, MMFlags, fd, 0);
107 if (Addr == MAP_FAILED) {
108 if (NearBlock) //Try again without a near hint
109 return allocateMappedMemory(NumBytes, 0, PFlags, EC);
111 EC = error_code(errno, system_category());
112 return MemoryBlock();
116 Result.Address = Addr;
117 Result.Size = NumPages*PageSize;
119 if (PFlags & MF_EXEC)
120 Memory::InvalidateInstructionCache(Result.Address, Result.Size);
126 Memory::releaseMappedMemory(MemoryBlock &M) {
127 if (M.Address == 0 || M.Size == 0)
128 return error_code::success();
130 if (0 != ::munmap(M.Address, M.Size))
131 return error_code(errno, system_category());
136 return error_code::success();
140 Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) {
141 if (M.Address == 0 || M.Size == 0)
142 return error_code::success();
145 return error_code(EINVAL, generic_category());
147 int Protect = getPosixProtectionFlags(Flags);
149 int Result = ::mprotect(M.Address, M.Size, Protect);
151 return error_code(errno, system_category());
154 Memory::InvalidateInstructionCache(M.Address, M.Size);
156 return error_code::success();
159 /// AllocateRWX - Allocate a slab of memory with read/write/execute
160 /// permissions. This is typically used for JIT applications where we want
161 /// to emit code to the memory then jump to it. Getting this type of memory
162 /// is very OS specific.
165 Memory::AllocateRWX(size_t NumBytes, const MemoryBlock* NearBlock,
166 std::string *ErrMsg) {
167 if (NumBytes == 0) return MemoryBlock();
169 size_t pageSize = Process::GetPageSize();
170 size_t NumPages = (NumBytes+pageSize-1)/pageSize;
173 #ifdef NEED_DEV_ZERO_FOR_MMAP
174 static int zero_fd = open("/dev/zero", O_RDWR);
176 MakeErrMsg(ErrMsg, "Can't open /dev/zero device");
177 return MemoryBlock();
182 int flags = MAP_PRIVATE |
183 #ifdef HAVE_MMAP_ANONYMOUS
190 void* start = NearBlock ? (unsigned char*)NearBlock->base() +
191 NearBlock->size() : 0;
193 #if defined(__APPLE__) && defined(__arm__)
194 void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_EXEC,
197 void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
200 if (pa == MAP_FAILED) {
201 if (NearBlock) //Try again without a near hint
202 return AllocateRWX(NumBytes, 0);
204 MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
205 return MemoryBlock();
208 #if defined(__APPLE__) && defined(__arm__)
209 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)pa,
210 (vm_size_t)(pageSize*NumPages), 0,
211 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
212 if (KERN_SUCCESS != kr) {
213 MakeErrMsg(ErrMsg, "vm_protect max RX failed");
214 return MemoryBlock();
217 kr = vm_protect(mach_task_self(), (vm_address_t)pa,
218 (vm_size_t)(pageSize*NumPages), 0,
219 VM_PROT_READ | VM_PROT_WRITE);
220 if (KERN_SUCCESS != kr) {
221 MakeErrMsg(ErrMsg, "vm_protect RW failed");
222 return MemoryBlock();
228 result.Size = NumPages*pageSize;
233 bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
234 if (M.Address == 0 || M.Size == 0) return false;
235 if (0 != ::munmap(M.Address, M.Size))
236 return MakeErrMsg(ErrMsg, "Can't release RWX Memory");
240 bool Memory::setWritable (MemoryBlock &M, std::string *ErrMsg) {
241 #if defined(__APPLE__) && defined(__arm__)
242 if (M.Address == 0 || M.Size == 0) return false;
243 Memory::InvalidateInstructionCache(M.Address, M.Size);
244 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
245 (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_WRITE);
246 return KERN_SUCCESS == kr;
252 bool Memory::setExecutable (MemoryBlock &M, std::string *ErrMsg) {
253 #if defined(__APPLE__) && defined(__arm__)
254 if (M.Address == 0 || M.Size == 0) return false;
255 Memory::InvalidateInstructionCache(M.Address, M.Size);
256 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
257 (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
258 return KERN_SUCCESS == kr;
264 bool Memory::setRangeWritable(const void *Addr, size_t Size) {
265 #if defined(__APPLE__) && defined(__arm__)
266 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
268 VM_PROT_READ | VM_PROT_WRITE);
269 return KERN_SUCCESS == kr;
275 bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
276 #if defined(__APPLE__) && defined(__arm__)
277 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
279 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
280 return KERN_SUCCESS == kr;
286 /// InvalidateInstructionCache - Before the JIT can run a block of code
287 /// that has been emitted it must invalidate the instruction cache on some
289 void Memory::InvalidateInstructionCache(const void *Addr,
292 // icache invalidation for PPC and ARM.
293 #if defined(__APPLE__)
295 # if (defined(__POWERPC__) || defined (__ppc__) || \
296 defined(_POWER) || defined(_ARCH_PPC)) || defined(__arm__)
297 sys_icache_invalidate(const_cast<void *>(Addr), Len);
302 # if (defined(__POWERPC__) || defined (__ppc__) || \
303 defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__)
304 const size_t LineSize = 32;
306 const intptr_t Mask = ~(LineSize - 1);
307 const intptr_t StartLine = ((intptr_t) Addr) & Mask;
308 const intptr_t EndLine = ((intptr_t) Addr + Len + LineSize - 1) & Mask;
310 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
311 asm volatile("dcbf 0, %0" : : "r"(Line));
312 asm volatile("sync");
314 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
315 asm volatile("icbi 0, %0" : : "r"(Line));
316 asm volatile("isync");
317 # elif defined(__arm__) && defined(__GNUC__)
318 // FIXME: Can we safely always call this for __GNUC__ everywhere?
319 const char *Start = static_cast<const char *>(Addr);
320 const char *End = Start + Len;
321 __clear_cache(const_cast<char *>(Start), const_cast<char *>(End));
322 # elif defined(__mips__)
323 const char *Start = static_cast<const char *>(Addr);
324 cacheflush(const_cast<char *>(Start), Len, BCACHE);
329 ValgrindDiscardTranslations(Addr, Len);