2e301f62f2be29316e638d2dda1266aa81e8129e
[oota-llvm.git] / lib / Support / Unix / Memory.inc
1 //===- Unix/Memory.cpp - Generic UNIX System Configuration ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines some functions for various memory management utilities.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Unix.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/Process.h"
17
18 #ifdef HAVE_SYS_MMAN_H
19 #include <sys/mman.h>
20 #endif
21
22 #ifdef __APPLE__
23 #include <mach/mach.h>
24 #endif
25
26 #if defined(__mips__)
27 #  if defined(__OpenBSD__)
28 #    include <mips64/sysarch.h>
29 #  else
30 #    include <sys/cachectl.h>
31 #  endif
32 #endif
33
34 /// AllocateRWX - Allocate a slab of memory with read/write/execute
35 /// permissions.  This is typically used for JIT applications where we want
36 /// to emit code to the memory then jump to it.  Getting this type of memory
37 /// is very OS specific.
38 ///
39 llvm::sys::MemoryBlock
40 llvm::sys::Memory::AllocateRWX(size_t NumBytes, const MemoryBlock* NearBlock,
41                                std::string *ErrMsg) {
42   if (NumBytes == 0) return MemoryBlock();
43
44   size_t pageSize = Process::GetPageSize();
45   size_t NumPages = (NumBytes+pageSize-1)/pageSize;
46
47   int fd = -1;
48 #ifdef NEED_DEV_ZERO_FOR_MMAP
49   static int zero_fd = open("/dev/zero", O_RDWR);
50   if (zero_fd == -1) {
51     MakeErrMsg(ErrMsg, "Can't open /dev/zero device");
52     return MemoryBlock();
53   }
54   fd = zero_fd;
55 #endif
56
57   int flags = MAP_PRIVATE |
58 #ifdef HAVE_MMAP_ANONYMOUS
59   MAP_ANONYMOUS
60 #else
61   MAP_ANON
62 #endif
63   ;
64
65   void* start = NearBlock ? (unsigned char*)NearBlock->base() +
66                             NearBlock->size() : 0;
67
68 #if defined(__APPLE__) && defined(__arm__)
69   void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_EXEC,
70                     flags, fd, 0);
71 #else
72   void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
73                     flags, fd, 0);
74 #endif
75   if (pa == MAP_FAILED) {
76     if (NearBlock) //Try again without a near hint
77       return AllocateRWX(NumBytes, 0);
78
79     MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
80     return MemoryBlock();
81   }
82
83 #if defined(__APPLE__) && defined(__arm__)
84   kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)pa,
85                                 (vm_size_t)(pageSize*NumPages), 0,
86                                 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
87   if (KERN_SUCCESS != kr) {
88     MakeErrMsg(ErrMsg, "vm_protect max RX failed");
89     return sys::MemoryBlock();
90   }
91
92   kr = vm_protect(mach_task_self(), (vm_address_t)pa,
93                   (vm_size_t)(pageSize*NumPages), 0,
94                   VM_PROT_READ | VM_PROT_WRITE);
95   if (KERN_SUCCESS != kr) {
96     MakeErrMsg(ErrMsg, "vm_protect RW failed");
97     return sys::MemoryBlock();
98   }
99 #endif
100
101   MemoryBlock result;
102   result.Address = pa;
103   result.Size = NumPages*pageSize;
104
105   return result;
106 }
107
108 bool llvm::sys::Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
109   if (M.Address == 0 || M.Size == 0) return false;
110   if (0 != ::munmap(M.Address, M.Size))
111     return MakeErrMsg(ErrMsg, "Can't release RWX Memory");
112   return false;
113 }
114
115 bool llvm::sys::Memory::setWritable (MemoryBlock &M, std::string *ErrMsg) {
116 #if defined(__APPLE__) && defined(__arm__)
117   if (M.Address == 0 || M.Size == 0) return false;
118   sys::Memory::InvalidateInstructionCache(M.Address, M.Size);
119   kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
120     (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_WRITE);
121   return KERN_SUCCESS == kr;
122 #else
123   return true;
124 #endif
125 }
126
127 bool llvm::sys::Memory::setExecutable (MemoryBlock &M, std::string *ErrMsg) {
128 #if defined(__APPLE__) && defined(__arm__)
129   if (M.Address == 0 || M.Size == 0) return false;
130   sys::Memory::InvalidateInstructionCache(M.Address, M.Size);
131   kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
132     (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
133   return KERN_SUCCESS == kr;
134 #else
135   return true;
136 #endif
137 }
138
139 bool llvm::sys::Memory::setRangeWritable(const void *Addr, size_t Size) {
140 #if defined(__APPLE__) && defined(__arm__)
141   kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
142                                 (vm_size_t)Size, 0,
143                                 VM_PROT_READ | VM_PROT_WRITE);
144   return KERN_SUCCESS == kr;
145 #else
146   return true;
147 #endif
148 }
149
150 bool llvm::sys::Memory::setRangeExecutable(const void *Addr, size_t Size) {
151 #if defined(__APPLE__) && defined(__arm__)
152   kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
153                                 (vm_size_t)Size, 0,
154                                 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
155   return KERN_SUCCESS == kr;
156 #else
157   return true;
158 #endif
159 }