blob: c20629c82176d56b1476016ee63ba5a992a9784b [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael J. Spencer447762d2010-11-29 18:16:10 +00006//
Reid Spencer2896c952004-09-11 04:57:25 +00007//===----------------------------------------------------------------------===//
8//
9// This file defines some functions for various memory management utilities.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Unix.h"
Nico Weber432a3882018-04-30 14:59:11 +000014#include "llvm/Config/config.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
Petr Hosekfc9b29b2018-06-06 06:26:18 +000027#ifdef __Fuchsia__
28#include <zircon/syscalls.h>
29#endif
30
Chandler Carrutha699b6a2012-09-11 01:17:24 +000031#if defined(__mips__)
32# if defined(__OpenBSD__)
33# include <mips64/sysarch.h>
John Baldwin3e94e442017-10-25 14:53:16 +000034# elif !defined(__FreeBSD__)
Chandler Carrutha699b6a2012-09-11 01:17:24 +000035# include <sys/cachectl.h>
36# endif
37#endif
38
Petr Hosekfc9b29b2018-06-06 06:26:18 +000039#if defined(__APPLE__)
Andrew Kaylor1f661002012-09-19 20:46:12 +000040extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
Bob Wilson111b0b62013-05-19 20:33:51 +000041#else
Rafael Espindolae16befb2013-05-14 18:06:14 +000042extern "C" void __clear_cache(void *, void*);
Bob Wilson111b0b62013-05-19 20:33:51 +000043#endif
Andrew Kaylor1f661002012-09-19 20:46:12 +000044
45namespace {
46
47int getPosixProtectionFlags(unsigned Flags) {
48 switch (Flags) {
49 case llvm::sys::Memory::MF_READ:
50 return PROT_READ;
51 case llvm::sys::Memory::MF_WRITE:
52 return PROT_WRITE;
53 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_WRITE:
54 return PROT_READ | PROT_WRITE;
55 case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_EXEC:
56 return PROT_READ | PROT_EXEC;
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +000057 case llvm::sys::Memory::MF_READ | llvm::sys::Memory::MF_WRITE |
58 llvm::sys::Memory::MF_EXEC:
Andrew Kaylor1f661002012-09-19 20:46:12 +000059 return PROT_READ | PROT_WRITE | PROT_EXEC;
60 case llvm::sys::Memory::MF_EXEC:
Krzysztof Parzyszek798679e2013-02-20 18:24:30 +000061#if defined(__FreeBSD__)
Krzysztof Parzyszek12ba7112013-02-20 19:25:09 +000062 // On PowerPC, having an executable page that has no read permission
63 // can have unintended consequences. The function InvalidateInstruction-
64 // Cache uses instructions dcbf and icbi, both of which are treated by
65 // the processor as loads. If the page has no read permissions,
66 // executing these instructions will result in a segmentation fault.
67 // Somehow, this problem is not present on Linux, but it does happen
68 // on FreeBSD.
Krzysztof Parzyszek798679e2013-02-20 18:24:30 +000069 return PROT_READ | PROT_EXEC;
70#else
Andrew Kaylor1f661002012-09-19 20:46:12 +000071 return PROT_EXEC;
Krzysztof Parzyszek798679e2013-02-20 18:24:30 +000072#endif
Andrew Kaylor1f661002012-09-19 20:46:12 +000073 default:
74 llvm_unreachable("Illegal memory protection flag specified!");
75 }
76 // Provide a default return value as required by some compilers.
77 return PROT_NONE;
78}
79
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000080} // anonymous namespace
Andrew Kaylor1f661002012-09-19 20:46:12 +000081
82namespace llvm {
83namespace sys {
84
85MemoryBlock
86Memory::allocateMappedMemory(size_t NumBytes,
87 const MemoryBlock *const NearBlock,
88 unsigned PFlags,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000089 std::error_code &EC) {
90 EC = std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +000091 if (NumBytes == 0)
92 return MemoryBlock();
93
Rafael Espindolac0610bf2014-12-04 16:59:36 +000094 static const size_t PageSize = Process::getPageSize();
Andrew Kaylor1f661002012-09-19 20:46:12 +000095 const size_t NumPages = (NumBytes+PageSize-1)/PageSize;
96
97 int fd = -1;
Andrew Kaylor1f661002012-09-19 20:46:12 +000098
99 int MMFlags = MAP_PRIVATE |
Joerg Sonnenberger10c45e22016-09-30 20:17:23 +0000100#ifdef MAP_ANONYMOUS
Andrew Kaylor1f661002012-09-19 20:46:12 +0000101 MAP_ANONYMOUS
102#else
103 MAP_ANON
104#endif
105 ; // Ends statement above
106
107 int Protect = getPosixProtectionFlags(PFlags);
108
Lang Hamesafcb70d2017-11-16 23:04:44 +0000109#if defined(__NetBSD__) && defined(PROT_MPROTECT)
110 Protect |= PROT_MPROTECT(PROT_READ | PROT_WRITE | PROT_EXEC);
111#endif
112
Andrew Kaylor1f661002012-09-19 20:46:12 +0000113 // Use any near hint and the page size to set a page-aligned starting address
114 uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
115 NearBlock->size() : 0;
116 if (Start && Start % PageSize)
117 Start += PageSize - Start % PageSize;
118
119 void *Addr = ::mmap(reinterpret_cast<void*>(Start), PageSize*NumPages,
120 Protect, MMFlags, fd, 0);
121 if (Addr == MAP_FAILED) {
122 if (NearBlock) //Try again without a near hint
Craig Toppere73658d2014-04-28 04:05:08 +0000123 return allocateMappedMemory(NumBytes, nullptr, PFlags, EC);
Andrew Kaylor1f661002012-09-19 20:46:12 +0000124
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000125 EC = std::error_code(errno, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000126 return MemoryBlock();
127 }
128
129 MemoryBlock Result;
130 Result.Address = Addr;
131 Result.Size = NumPages*PageSize;
132
Peter Smitha9392572017-11-28 12:34:05 +0000133 // Rely on protectMappedMemory to invalidate instruction cache.
134 if (PFlags & MF_EXEC) {
135 EC = Memory::protectMappedMemory (Result, PFlags);
136 if (EC != std::error_code())
137 return MemoryBlock();
138 }
Andrew Kaylor1f661002012-09-19 20:46:12 +0000139
140 return Result;
141}
142
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000143std::error_code
Andrew Kaylor1f661002012-09-19 20:46:12 +0000144Memory::releaseMappedMemory(MemoryBlock &M) {
Craig Toppere73658d2014-04-28 04:05:08 +0000145 if (M.Address == nullptr || M.Size == 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000146 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000147
148 if (0 != ::munmap(M.Address, M.Size))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000149 return std::error_code(errno, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000150
Craig Toppere73658d2014-04-28 04:05:08 +0000151 M.Address = nullptr;
Andrew Kaylor1f661002012-09-19 20:46:12 +0000152 M.Size = 0;
153
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000154 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000155}
156
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000157std::error_code
Andrew Kaylor1f661002012-09-19 20:46:12 +0000158Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) {
Keno Fischer94f181a2015-12-16 11:13:23 +0000159 static const size_t PageSize = Process::getPageSize();
Craig Toppere73658d2014-04-28 04:05:08 +0000160 if (M.Address == nullptr || M.Size == 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000161 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000162
163 if (!Flags)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000164 return std::error_code(EINVAL, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000165
166 int Protect = getPosixProtectionFlags(Flags);
Alina Sbirleaf802c3f2016-11-05 04:22:15 +0000167 uintptr_t Start = alignAddr((uint8_t *)M.Address - PageSize + 1, PageSize);
168 uintptr_t End = alignAddr((uint8_t *)M.Address + M.Size, PageSize);
Peter Smitha9392572017-11-28 12:34:05 +0000169
170 bool InvalidateCache = (Flags & MF_EXEC);
171
172#if defined(__arm__) || defined(__aarch64__)
173 // Certain ARM implementations treat icache clear instruction as a memory read,
174 // and CPU segfaults on trying to clear cache on !PROT_READ page. Therefore we need
175 // to temporarily add PROT_READ for the sake of flushing the instruction caches.
176 if (InvalidateCache && !(Protect & PROT_READ)) {
177 int Result = ::mprotect((void *)Start, End - Start, Protect | PROT_READ);
178 if (Result != 0)
179 return std::error_code(errno, std::generic_category());
180
181 Memory::InvalidateInstructionCache(M.Address, M.Size);
182 InvalidateCache = false;
183 }
184#endif
185
Alina Sbirleaf802c3f2016-11-05 04:22:15 +0000186 int Result = ::mprotect((void *)Start, End - Start, Protect);
187
Andrew Kaylor1f661002012-09-19 20:46:12 +0000188 if (Result != 0)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000189 return std::error_code(errno, std::generic_category());
Andrew Kaylor1f661002012-09-19 20:46:12 +0000190
Peter Smitha9392572017-11-28 12:34:05 +0000191 if (InvalidateCache)
Andrew Kaylor1f661002012-09-19 20:46:12 +0000192 Memory::InvalidateInstructionCache(M.Address, M.Size);
193
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000194 return std::error_code();
Andrew Kaylor1f661002012-09-19 20:46:12 +0000195}
196
Andrew Kaylor1f661002012-09-19 20:46:12 +0000197/// InvalidateInstructionCache - Before the JIT can run a block of code
198/// that has been emitted it must invalidate the instruction cache on some
199/// platforms.
200void Memory::InvalidateInstructionCache(const void *Addr,
201 size_t Len) {
202
203// icache invalidation for PPC and ARM.
204#if defined(__APPLE__)
205
Rafael Espindola05b5a462013-07-26 22:13:57 +0000206# if (defined(__POWERPC__) || defined (__ppc__) || \
Tim Northover00ed9962014-03-29 10:18:08 +0000207 defined(_POWER) || defined(_ARCH_PPC) || defined(__arm__) || \
208 defined(__arm64__))
Andrew Kaylor1f661002012-09-19 20:46:12 +0000209 sys_icache_invalidate(const_cast<void *>(Addr), Len);
210# endif
211
Petr Hosekfc9b29b2018-06-06 06:26:18 +0000212#elif defined(__Fuchsia__)
213
214 zx_status_t Status = zx_cache_flush(Addr, Len, ZX_CACHE_FLUSH_INSN);
215 assert(Status == ZX_OK && "cannot invalidate instruction cache");
216
Andrew Kaylor1f661002012-09-19 20:46:12 +0000217#else
218
Rafael Espindola05b5a462013-07-26 22:13:57 +0000219# if (defined(__POWERPC__) || defined (__ppc__) || \
Andrew Kaylor1f661002012-09-19 20:46:12 +0000220 defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__)
221 const size_t LineSize = 32;
222
223 const intptr_t Mask = ~(LineSize - 1);
224 const intptr_t StartLine = ((intptr_t) Addr) & Mask;
225 const intptr_t EndLine = ((intptr_t) Addr + Len + LineSize - 1) & Mask;
226
227 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
228 asm volatile("dcbf 0, %0" : : "r"(Line));
229 asm volatile("sync");
230
231 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
232 asm volatile("icbi 0, %0" : : "r"(Line));
233 asm volatile("isync");
Petar Jovanovic4a118492015-01-27 23:30:18 +0000234# elif (defined(__arm__) || defined(__aarch64__) || defined(__mips__)) && \
235 defined(__GNUC__)
Andrew Kaylor1f661002012-09-19 20:46:12 +0000236 // FIXME: Can we safely always call this for __GNUC__ everywhere?
237 const char *Start = static_cast<const char *>(Addr);
238 const char *End = Start + Len;
239 __clear_cache(const_cast<char *>(Start), const_cast<char *>(End));
Andrew Kaylor1f661002012-09-19 20:46:12 +0000240# endif
241
242#endif // end apple
243
244 ValgrindDiscardTranslations(Addr, Len);
245}
246
247} // namespace sys
248} // namespace llvm