blob: adbfff2f59a5f16ceac62365278bb4cd12f7e841 [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"
Nico Weber432a3882018-04-30 14:59:11 +000015#include "llvm/Config/config.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000016#include "llvm/Support/DataTypes.h"
Andrew Kaylor1f661002012-09-19 20:46:12 +000017#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000018#include "llvm/Support/Process.h"
Reid Spencer47cd6532004-12-27 06:17:03 +000019
20#ifdef HAVE_SYS_MMAN_H
21#include <sys/mman.h>
22#endif
Reid Spencer2896c952004-09-11 04:57:25 +000023
Evan Cheng5cc53c32008-09-18 07:54:21 +000024#ifdef __APPLE__
25#include <mach/mach.h>
26#endif
27
Petr Hosekfc9b29b2018-06-06 06:26:18 +000028#ifdef __Fuchsia__
29#include <zircon/syscalls.h>
30#endif
31
Chandler Carrutha699b6a2012-09-11 01:17:24 +000032#if defined(__mips__)
33# if defined(__OpenBSD__)
34# include <mips64/sysarch.h>
John Baldwin3e94e442017-10-25 14:53:16 +000035# elif !defined(__FreeBSD__)
Chandler Carrutha699b6a2012-09-11 01:17:24 +000036# include <sys/cachectl.h>
37# endif
38#endif
39
Petr Hosekfc9b29b2018-06-06 06:26:18 +000040#if defined(__APPLE__)
Andrew Kaylor1f661002012-09-19 20:46:12 +000041extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
Bob Wilson111b0b62013-05-19 20:33:51 +000042#else
Rafael Espindolae16befb2013-05-14 18:06:14 +000043extern "C" void __clear_cache(void *, void*);
Bob Wilson111b0b62013-05-19 20:33:51 +000044#endif
Andrew Kaylor1f661002012-09-19 20:46:12 +000045
46namespace {
47
48int getPosixProtectionFlags(unsigned Flags) {
49 switch (Flags) {
50 case llvm::sys::Memory::MF_READ:
51 return PROT_READ;
52 case llvm::sys::Memory::MF_WRITE:
53 return PROT_WRITE;
54 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_WRITE:
55 return PROT_READ | PROT_WRITE;
56 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_EXEC:
57 return PROT_READ | PROT_EXEC;
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +000058 case llvm::sys::Memory::MF_READ | llvm::sys::Memory::MF_WRITE |
59 llvm::sys::Memory::MF_EXEC:
Andrew Kaylor1f661002012-09-19 20:46:12 +000060 return PROT_READ | PROT_WRITE | PROT_EXEC;
61 case llvm::sys::Memory::MF_EXEC:
Krzysztof Parzyszek798679e2013-02-20 18:24:30 +000062#if defined(__FreeBSD__)
Krzysztof Parzyszek12ba7112013-02-20 19:25:09 +000063 // On PowerPC, having an executable page that has no read permission
64 // can have unintended consequences. The function InvalidateInstruction-
65 // Cache uses instructions dcbf and icbi, both of which are treated by
66 // the processor as loads. If the page has no read permissions,
67 // executing these instructions will result in a segmentation fault.
68 // Somehow, this problem is not present on Linux, but it does happen
69 // on FreeBSD.
Krzysztof Parzyszek798679e2013-02-20 18:24:30 +000070 return PROT_READ | PROT_EXEC;
71#else
Andrew Kaylor1f661002012-09-19 20:46:12 +000072 return PROT_EXEC;
Krzysztof Parzyszek798679e2013-02-20 18:24:30 +000073#endif
Andrew Kaylor1f661002012-09-19 20:46:12 +000074 default:
75 llvm_unreachable("Illegal memory protection flag specified!");
76 }
77 // Provide a default return value as required by some compilers.
78 return PROT_NONE;
79}
80
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000081} // anonymous namespace
Andrew Kaylor1f661002012-09-19 20:46:12 +000082
83namespace llvm {
84namespace sys {
85
86MemoryBlock
87Memory::allocateMappedMemory(size_t NumBytes,
88 const MemoryBlock *const NearBlock,
89 unsigned PFlags,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000090 std::error_code &EC) {
91 EC = std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +000092 if (NumBytes == 0)
93 return MemoryBlock();
94
Rafael Espindolac0610bf2014-12-04 16:59:36 +000095 static const size_t PageSize = Process::getPageSize();
Andrew Kaylor1f661002012-09-19 20:46:12 +000096 const size_t NumPages = (NumBytes+PageSize-1)/PageSize;
97
98 int fd = -1;
Andrew Kaylor1f661002012-09-19 20:46:12 +000099
100 int MMFlags = MAP_PRIVATE |
Joerg Sonnenberger10c45e22016-09-30 20:17:23 +0000101#ifdef MAP_ANONYMOUS
Andrew Kaylor1f661002012-09-19 20:46:12 +0000102 MAP_ANONYMOUS
103#else
104 MAP_ANON
105#endif
106 ; // Ends statement above
107
108 int Protect = getPosixProtectionFlags(PFlags);
109
Lang Hamesafcb70d2017-11-16 23:04:44 +0000110#if defined(__NetBSD__) && defined(PROT_MPROTECT)
111 Protect |= PROT_MPROTECT(PROT_READ | PROT_WRITE | PROT_EXEC);
112#endif
113
Andrew Kaylor1f661002012-09-19 20:46:12 +0000114 // Use any near hint and the page size to set a page-aligned starting address
115 uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
116 NearBlock->size() : 0;
117 if (Start && Start % PageSize)
118 Start += PageSize - Start % PageSize;
119
120 void *Addr = ::mmap(reinterpret_cast<void*>(Start), PageSize*NumPages,
121 Protect, MMFlags, fd, 0);
122 if (Addr == MAP_FAILED) {
123 if (NearBlock) //Try again without a near hint
Craig Toppere73658d2014-04-28 04:05:08 +0000124 return allocateMappedMemory(NumBytes, nullptr, PFlags, EC);
Andrew Kaylor1f661002012-09-19 20:46:12 +0000125
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000126 EC = std::error_code(errno, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000127 return MemoryBlock();
128 }
129
130 MemoryBlock Result;
131 Result.Address = Addr;
132 Result.Size = NumPages*PageSize;
133
Peter Smitha9392572017-11-28 12:34:05 +0000134 // Rely on protectMappedMemory to invalidate instruction cache.
135 if (PFlags & MF_EXEC) {
136 EC = Memory::protectMappedMemory (Result, PFlags);
137 if (EC != std::error_code())
138 return MemoryBlock();
139 }
Andrew Kaylor1f661002012-09-19 20:46:12 +0000140
141 return Result;
142}
143
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000144std::error_code
Andrew Kaylor1f661002012-09-19 20:46:12 +0000145Memory::releaseMappedMemory(MemoryBlock &M) {
Craig Toppere73658d2014-04-28 04:05:08 +0000146 if (M.Address == nullptr || M.Size == 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000147 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000148
149 if (0 != ::munmap(M.Address, M.Size))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000150 return std::error_code(errno, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000151
Craig Toppere73658d2014-04-28 04:05:08 +0000152 M.Address = nullptr;
Andrew Kaylor1f661002012-09-19 20:46:12 +0000153 M.Size = 0;
154
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000155 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000156}
157
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000158std::error_code
Andrew Kaylor1f661002012-09-19 20:46:12 +0000159Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) {
Keno Fischer94f181a2015-12-16 11:13:23 +0000160 static const size_t PageSize = Process::getPageSize();
Craig Toppere73658d2014-04-28 04:05:08 +0000161 if (M.Address == nullptr || M.Size == 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000162 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000163
164 if (!Flags)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000165 return std::error_code(EINVAL, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000166
167 int Protect = getPosixProtectionFlags(Flags);
Alina Sbirleaf802c3f2016-11-05 04:22:15 +0000168 uintptr_t Start = alignAddr((uint8_t *)M.Address - PageSize + 1, PageSize);
169 uintptr_t End = alignAddr((uint8_t *)M.Address + M.Size, PageSize);
Peter Smitha9392572017-11-28 12:34:05 +0000170
171 bool InvalidateCache = (Flags & MF_EXEC);
172
173#if defined(__arm__) || defined(__aarch64__)
174 // Certain ARM implementations treat icache clear instruction as a memory read,
175 // and CPU segfaults on trying to clear cache on !PROT_READ page. Therefore we need
176 // to temporarily add PROT_READ for the sake of flushing the instruction caches.
177 if (InvalidateCache && !(Protect & PROT_READ)) {
178 int Result = ::mprotect((void *)Start, End - Start, Protect | PROT_READ);
179 if (Result != 0)
180 return std::error_code(errno, std::generic_category());
181
182 Memory::InvalidateInstructionCache(M.Address, M.Size);
183 InvalidateCache = false;
184 }
185#endif
186
Alina Sbirleaf802c3f2016-11-05 04:22:15 +0000187 int Result = ::mprotect((void *)Start, End - Start, Protect);
188
Andrew Kaylor1f661002012-09-19 20:46:12 +0000189 if (Result != 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000190 return std::error_code(errno, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000191
Peter Smitha9392572017-11-28 12:34:05 +0000192 if (InvalidateCache)
Andrew Kaylor1f661002012-09-19 20:46:12 +0000193 Memory::InvalidateInstructionCache(M.Address, M.Size);
194
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000195 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000196}
197
Andrew Kaylor1f661002012-09-19 20:46:12 +0000198/// InvalidateInstructionCache - Before the JIT can run a block of code
199/// that has been emitted it must invalidate the instruction cache on some
200/// platforms.
201void Memory::InvalidateInstructionCache(const void *Addr,
202 size_t Len) {
203
204// icache invalidation for PPC and ARM.
205#if defined(__APPLE__)
206
Rafael Espindola05b5a462013-07-26 22:13:57 +0000207# if (defined(__POWERPC__) || defined (__ppc__) || \
Tim Northover00ed9962014-03-29 10:18:08 +0000208 defined(_POWER) || defined(_ARCH_PPC) || defined(__arm__) || \
209 defined(__arm64__))
Andrew Kaylor1f661002012-09-19 20:46:12 +0000210 sys_icache_invalidate(const_cast<void *>(Addr), Len);
211# endif
212
Petr Hosekfc9b29b2018-06-06 06:26:18 +0000213#elif defined(__Fuchsia__)
214
215 zx_status_t Status = zx_cache_flush(Addr, Len, ZX_CACHE_FLUSH_INSN);
216 assert(Status == ZX_OK && "cannot invalidate instruction cache");
217
Andrew Kaylor1f661002012-09-19 20:46:12 +0000218#else
219
Rafael Espindola05b5a462013-07-26 22:13:57 +0000220# if (defined(__POWERPC__) || defined (__ppc__) || \
Andrew Kaylor1f661002012-09-19 20:46:12 +0000221 defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__)
222 const size_t LineSize = 32;
223
224 const intptr_t Mask = ~(LineSize - 1);
225 const intptr_t StartLine = ((intptr_t) Addr) & Mask;
226 const intptr_t EndLine = ((intptr_t) Addr + Len + LineSize - 1) & Mask;
227
228 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
229 asm volatile("dcbf 0, %0" : : "r"(Line));
230 asm volatile("sync");
231
232 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
233 asm volatile("icbi 0, %0" : : "r"(Line));
234 asm volatile("isync");
Petar Jovanovic4a118492015-01-27 23:30:18 +0000235# elif (defined(__arm__) || defined(__aarch64__) || defined(__mips__)) && \
236 defined(__GNUC__)
Andrew Kaylor1f661002012-09-19 20:46:12 +0000237 // FIXME: Can we safely always call this for __GNUC__ everywhere?
238 const char *Start = static_cast<const char *>(Addr);
239 const char *End = Start + Len;
240 __clear_cache(const_cast<char *>(Start), const_cast<char *>(End));
Andrew Kaylor1f661002012-09-19 20:46:12 +0000241# endif
242
243#endif // end apple
244
245 ValgrindDiscardTranslations(Addr, Len);
246}
247
248} // namespace sys
249} // namespace llvm