blob: 31480162c8766db4655723114ad7bc8ee3fdca35 [file] [log] [blame]
Reid Spencer2896c952004-09-11 04:57:25 +00001//===- Unix/Memory.cpp - Generic UNIX System Configuration ------*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer2896c952004-09-11 04:57:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer2896c952004-09-11 04:57:25 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines some functions for various memory management utilities.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Unix.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/DataTypes.h"
Andrew Kaylor1f661002012-09-19 20:46:12 +000016#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000017#include "llvm/Support/Process.h"
Reid Spencer47cd6532004-12-27 06:17:03 +000018
19#ifdef HAVE_SYS_MMAN_H
20#include <sys/mman.h>
21#endif
Reid Spencer2896c952004-09-11 04:57:25 +000022
Evan Cheng5cc53c32008-09-18 07:54:21 +000023#ifdef __APPLE__
24#include <mach/mach.h>
25#endif
26
Chandler Carrutha699b6a2012-09-11 01:17:24 +000027#if defined(__mips__)
28# if defined(__OpenBSD__)
29# include <mips64/sysarch.h>
John Baldwin3e94e442017-10-25 14:53:16 +000030# elif !defined(__FreeBSD__)
Chandler Carrutha699b6a2012-09-11 01:17:24 +000031# include <sys/cachectl.h>
32# endif
33#endif
34
Bob Wilson111b0b62013-05-19 20:33:51 +000035#ifdef __APPLE__
Andrew Kaylor1f661002012-09-19 20:46:12 +000036extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
Bob Wilson111b0b62013-05-19 20:33:51 +000037#else
Rafael Espindolae16befb2013-05-14 18:06:14 +000038extern "C" void __clear_cache(void *, void*);
Bob Wilson111b0b62013-05-19 20:33:51 +000039#endif
Andrew Kaylor1f661002012-09-19 20:46:12 +000040
41namespace {
42
43int getPosixProtectionFlags(unsigned Flags) {
44 switch (Flags) {
45 case llvm::sys::Memory::MF_READ:
46 return PROT_READ;
47 case llvm::sys::Memory::MF_WRITE:
48 return PROT_WRITE;
49 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_WRITE:
50 return PROT_READ | PROT_WRITE;
51 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_EXEC:
52 return PROT_READ | PROT_EXEC;
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +000053 case llvm::sys::Memory::MF_READ | llvm::sys::Memory::MF_WRITE |
54 llvm::sys::Memory::MF_EXEC:
Andrew Kaylor1f661002012-09-19 20:46:12 +000055 return PROT_READ | PROT_WRITE | PROT_EXEC;
56 case llvm::sys::Memory::MF_EXEC:
Krzysztof Parzyszek798679e2013-02-20 18:24:30 +000057#if defined(__FreeBSD__)
Krzysztof Parzyszek12ba7112013-02-20 19:25:09 +000058 // On PowerPC, having an executable page that has no read permission
59 // can have unintended consequences. The function InvalidateInstruction-
60 // Cache uses instructions dcbf and icbi, both of which are treated by
61 // the processor as loads. If the page has no read permissions,
62 // executing these instructions will result in a segmentation fault.
63 // Somehow, this problem is not present on Linux, but it does happen
64 // on FreeBSD.
Krzysztof Parzyszek798679e2013-02-20 18:24:30 +000065 return PROT_READ | PROT_EXEC;
66#else
Andrew Kaylor1f661002012-09-19 20:46:12 +000067 return PROT_EXEC;
Krzysztof Parzyszek798679e2013-02-20 18:24:30 +000068#endif
Andrew Kaylor1f661002012-09-19 20:46:12 +000069 default:
70 llvm_unreachable("Illegal memory protection flag specified!");
71 }
72 // Provide a default return value as required by some compilers.
73 return PROT_NONE;
74}
75
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000076} // anonymous namespace
Andrew Kaylor1f661002012-09-19 20:46:12 +000077
78namespace llvm {
79namespace sys {
80
81MemoryBlock
82Memory::allocateMappedMemory(size_t NumBytes,
83 const MemoryBlock *const NearBlock,
84 unsigned PFlags,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000085 std::error_code &EC) {
86 EC = std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +000087 if (NumBytes == 0)
88 return MemoryBlock();
89
Rafael Espindolac0610bf2014-12-04 16:59:36 +000090 static const size_t PageSize = Process::getPageSize();
Andrew Kaylor1f661002012-09-19 20:46:12 +000091 const size_t NumPages = (NumBytes+PageSize-1)/PageSize;
92
93 int fd = -1;
Andrew Kaylor1f661002012-09-19 20:46:12 +000094
95 int MMFlags = MAP_PRIVATE |
Joerg Sonnenberger10c45e22016-09-30 20:17:23 +000096#ifdef MAP_ANONYMOUS
Andrew Kaylor1f661002012-09-19 20:46:12 +000097 MAP_ANONYMOUS
98#else
99 MAP_ANON
100#endif
101 ; // Ends statement above
102
103 int Protect = getPosixProtectionFlags(PFlags);
104
Lang Hamesafcb70d2017-11-16 23:04:44 +0000105#if defined(__NetBSD__) && defined(PROT_MPROTECT)
106 Protect |= PROT_MPROTECT(PROT_READ | PROT_WRITE | PROT_EXEC);
107#endif
108
Andrew Kaylor1f661002012-09-19 20:46:12 +0000109 // Use any near hint and the page size to set a page-aligned starting address
110 uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
111 NearBlock->size() : 0;
112 if (Start && Start % PageSize)
113 Start += PageSize - Start % PageSize;
114
115 void *Addr = ::mmap(reinterpret_cast<void*>(Start), PageSize*NumPages,
116 Protect, MMFlags, fd, 0);
117 if (Addr == MAP_FAILED) {
118 if (NearBlock) //Try again without a near hint
Craig Toppere73658d2014-04-28 04:05:08 +0000119 return allocateMappedMemory(NumBytes, nullptr, PFlags, EC);
Andrew Kaylor1f661002012-09-19 20:46:12 +0000120
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000121 EC = std::error_code(errno, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000122 return MemoryBlock();
123 }
124
125 MemoryBlock Result;
126 Result.Address = Addr;
127 Result.Size = NumPages*PageSize;
128
129 if (PFlags & MF_EXEC)
130 Memory::InvalidateInstructionCache(Result.Address, Result.Size);
131
132 return Result;
133}
134
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000135std::error_code
Andrew Kaylor1f661002012-09-19 20:46:12 +0000136Memory::releaseMappedMemory(MemoryBlock &M) {
Craig Toppere73658d2014-04-28 04:05:08 +0000137 if (M.Address == nullptr || M.Size == 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000138 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000139
140 if (0 != ::munmap(M.Address, M.Size))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000141 return std::error_code(errno, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000142
Craig Toppere73658d2014-04-28 04:05:08 +0000143 M.Address = nullptr;
Andrew Kaylor1f661002012-09-19 20:46:12 +0000144 M.Size = 0;
145
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000146 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000147}
148
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000149std::error_code
Andrew Kaylor1f661002012-09-19 20:46:12 +0000150Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) {
Keno Fischer94f181a2015-12-16 11:13:23 +0000151 static const size_t PageSize = Process::getPageSize();
Craig Toppere73658d2014-04-28 04:05:08 +0000152 if (M.Address == nullptr || M.Size == 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000153 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000154
155 if (!Flags)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000156 return std::error_code(EINVAL, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000157
158 int Protect = getPosixProtectionFlags(Flags);
159
Alina Sbirleaf802c3f2016-11-05 04:22:15 +0000160 uintptr_t Start = alignAddr((uint8_t *)M.Address - PageSize + 1, PageSize);
161 uintptr_t End = alignAddr((uint8_t *)M.Address + M.Size, PageSize);
162 int Result = ::mprotect((void *)Start, End - Start, Protect);
163
Andrew Kaylor1f661002012-09-19 20:46:12 +0000164 if (Result != 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000165 return std::error_code(errno, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000166
167 if (Flags & MF_EXEC)
168 Memory::InvalidateInstructionCache(M.Address, M.Size);
169
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000170 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000171}
172
Andrew Kaylor1f661002012-09-19 20:46:12 +0000173/// InvalidateInstructionCache - Before the JIT can run a block of code
174/// that has been emitted it must invalidate the instruction cache on some
175/// platforms.
176void Memory::InvalidateInstructionCache(const void *Addr,
177 size_t Len) {
178
179// icache invalidation for PPC and ARM.
180#if defined(__APPLE__)
181
Rafael Espindola05b5a462013-07-26 22:13:57 +0000182# if (defined(__POWERPC__) || defined (__ppc__) || \
Tim Northover00ed9962014-03-29 10:18:08 +0000183 defined(_POWER) || defined(_ARCH_PPC) || defined(__arm__) || \
184 defined(__arm64__))
Andrew Kaylor1f661002012-09-19 20:46:12 +0000185 sys_icache_invalidate(const_cast<void *>(Addr), Len);
186# endif
187
188#else
189
Rafael Espindola05b5a462013-07-26 22:13:57 +0000190# if (defined(__POWERPC__) || defined (__ppc__) || \
Andrew Kaylor1f661002012-09-19 20:46:12 +0000191 defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__)
192 const size_t LineSize = 32;
193
194 const intptr_t Mask = ~(LineSize - 1);
195 const intptr_t StartLine = ((intptr_t) Addr) & Mask;
196 const intptr_t EndLine = ((intptr_t) Addr + Len + LineSize - 1) & Mask;
197
198 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
199 asm volatile("dcbf 0, %0" : : "r"(Line));
200 asm volatile("sync");
201
202 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
203 asm volatile("icbi 0, %0" : : "r"(Line));
204 asm volatile("isync");
Petar Jovanovic4a118492015-01-27 23:30:18 +0000205# elif (defined(__arm__) || defined(__aarch64__) || defined(__mips__)) && \
206 defined(__GNUC__)
Andrew Kaylor1f661002012-09-19 20:46:12 +0000207 // FIXME: Can we safely always call this for __GNUC__ everywhere?
208 const char *Start = static_cast<const char *>(Addr);
209 const char *End = Start + Len;
210 __clear_cache(const_cast<char *>(Start), const_cast<char *>(End));
Andrew Kaylor1f661002012-09-19 20:46:12 +0000211# endif
212
213#endif // end apple
214
215 ValgrindDiscardTranslations(Addr, Len);
216}
217
218} // namespace sys
219} // namespace llvm