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/System/Process.h"
17 #ifdef HAVE_SYS_MMAN_H
22 #include <mach/mach.h>
25 /// AllocateRWX - Allocate a slab of memory with read/write/execute
26 /// permissions. This is typically used for JIT applications where we want
27 /// to emit code to the memory then jump to it. Getting this type of memory
28 /// is very OS specific.
30 llvm::sys::MemoryBlock
31 llvm::sys::Memory::AllocateRWX(unsigned NumBytes, const MemoryBlock* NearBlock,
32 std::string *ErrMsg) {
33 if (NumBytes == 0) return MemoryBlock();
35 unsigned pageSize = Process::GetPageSize();
36 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
39 #ifdef NEED_DEV_ZERO_FOR_MMAP
40 static int zero_fd = open("/dev/zero", O_RDWR);
42 MakeErrMsg(ErrMsg, "Can't open /dev/zero device");
48 int flags = MAP_PRIVATE |
49 #ifdef HAVE_MMAP_ANONYMOUS
56 void* start = NearBlock ? (unsigned char*)NearBlock->base() +
57 NearBlock->size() : 0;
59 #if defined(__APPLE__) && defined(__arm__)
60 void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_EXEC,
63 void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
66 if (pa == MAP_FAILED) {
67 if (NearBlock) //Try again without a near hint
68 return AllocateRWX(NumBytes, 0);
70 MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
74 #if defined(__APPLE__) && defined(__arm__)
75 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)pa,
76 (vm_size_t)(pageSize*NumPages), 0,
77 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
78 if (KERN_SUCCESS != kr) {
79 MakeErrMsg(ErrMsg, "vm_protect max RX failed");
80 return sys::MemoryBlock();
83 kr = vm_protect(mach_task_self(), (vm_address_t)pa,
84 (vm_size_t)(pageSize*NumPages), 0,
85 VM_PROT_READ | VM_PROT_WRITE);
86 if (KERN_SUCCESS != kr) {
87 MakeErrMsg(ErrMsg, "vm_protect RW failed");
88 return sys::MemoryBlock();
94 result.Size = NumPages*pageSize;
99 bool llvm::sys::Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
100 if (M.Address == 0 || M.Size == 0) return false;
101 if (0 != ::munmap(M.Address, M.Size))
102 return MakeErrMsg(ErrMsg, "Can't release RWX Memory");
106 bool llvm::sys::Memory::setWritable (MemoryBlock &M, std::string *ErrMsg) {
107 #if defined(__APPLE__) && defined(__arm__)
108 if (M.Address == 0 || M.Size == 0) return false;
109 sys::Memory::InvalidateInstructionCache(M.Address, M.Size);
110 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
111 (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_WRITE);
112 return KERN_SUCCESS == kr;
118 bool llvm::sys::Memory::setExecutable (MemoryBlock &M, std::string *ErrMsg) {
119 #if defined(__APPLE__) && defined(__arm__)
120 if (M.Address == 0 || M.Size == 0) return false;
121 sys::Memory::InvalidateInstructionCache(M.Address, M.Size);
122 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
123 (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
124 return KERN_SUCCESS == kr;
130 bool llvm::sys::Memory::setRangeWritable(const void *Addr, size_t Size) {
131 #if defined(__APPLE__) && defined(__arm__)
132 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
134 VM_PROT_READ | VM_PROT_WRITE);
135 return KERN_SUCCESS == kr;
141 bool llvm::sys::Memory::setRangeExecutable(const void *Addr, size_t Size) {
142 #if defined(__APPLE__) && defined(__arm__)
143 kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
145 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
146 return KERN_SUCCESS == kr;