14 #include "llvm/Config/config.h" 19 #ifdef HAVE_SYS_MMAN_H 24 #include <mach/mach.h> 28 #include <zircon/syscalls.h> 32 # if defined(__OpenBSD__) 33 # include <mips64/sysarch.h> 34 # elif !defined(__FreeBSD__) 35 # include <sys/cachectl.h> 39 #if defined(__APPLE__) 40 extern "C" void sys_icache_invalidate(
const void *Addr,
size_t len);
42 extern "C" void __clear_cache(
void *,
void*);
45 static int getPosixProtectionFlags(
unsigned Flags) {
52 return PROT_READ | PROT_WRITE;
54 return PROT_READ | PROT_EXEC;
57 return PROT_READ | PROT_WRITE | PROT_EXEC;
59 #if (defined(__FreeBSD__) || defined(__POWERPC__) || defined (__ppc__) || \ 60 defined(_POWER) || defined(_ARCH_PPC)) 66 return PROT_READ | PROT_EXEC;
82 const MemoryBlock *
const NearBlock,
84 std::error_code &EC) {
85 EC = std::error_code();
96 fd = open(
"/dev/zero", O_RDWR);
98 EC = std::error_code(errno, std::generic_category());
103 int MMFlags = MAP_PRIVATE;
104 #if defined(MAP_ANON) 107 int Protect = getPosixProtectionFlags(PFlags);
109 #if defined(__NetBSD__) && defined(PROT_MPROTECT) 110 Protect |= PROT_MPROTECT(PROT_READ | PROT_WRITE | PROT_EXEC);
114 uintptr_t Start = NearBlock ?
reinterpret_cast<uintptr_t
>(NearBlock->base()) +
115 NearBlock->allocatedSize() : 0;
117 const size_t NumPages = (NumBytes+PageSize-1)/PageSize;
119 if (Start && Start % PageSize)
120 Start += PageSize - Start %
PageSize;
123 void *Addr = ::mmap(reinterpret_cast<void *>(Start), PageSize*NumPages, Protect,
125 if (Addr == MAP_FAILED) {
127 #if !defined(MAP_ANON) 133 EC = std::error_code(errno, std::generic_category());
134 #if !defined(MAP_ANON) 137 return MemoryBlock();
140 #if !defined(MAP_ANON) 145 Result.Address = Addr;
146 Result.AllocatedSize = PageSize*NumPages;
147 Result.Flags = PFlags;
152 if (EC != std::error_code())
153 return MemoryBlock();
161 if (M.Address ==
nullptr || M.AllocatedSize == 0)
162 return std::error_code();
164 if (0 != ::munmap(M.Address, M.AllocatedSize))
165 return std::error_code(errno, std::generic_category());
170 return std::error_code();
176 if (M.Address ==
nullptr || M.AllocatedSize == 0)
177 return std::error_code();
180 return std::error_code(EINVAL, std::generic_category());
182 int Protect = getPosixProtectionFlags(Flags);
183 uintptr_t Start =
alignAddr((
const uint8_t *)M.Address - PageSize.value() + 1,
PageSize);
184 uintptr_t End =
alignAddr((
const uint8_t *)M.Address + M.AllocatedSize, PageSize);
186 bool InvalidateCache = (Flags &
MF_EXEC);
188 #if defined(__arm__) || defined(__aarch64__) 192 if (InvalidateCache && !(Protect & PROT_READ)) {
193 int Result = ::mprotect((
void *)Start, End - Start, Protect | PROT_READ);
195 return std::error_code(errno, std::generic_category());
198 InvalidateCache =
false;
202 int Result = ::mprotect((
void *)Start, End - Start, Protect);
205 return std::error_code(errno, std::generic_category());
210 return std::error_code();
220 #if defined(__APPLE__) 222 # if (defined(__POWERPC__) || defined (__ppc__) || \ 223 defined(_POWER) || defined(_ARCH_PPC) || defined(__arm__) || \ 225 sys_icache_invalidate(const_cast<void *>(Addr), Len);
228 #elif defined(__Fuchsia__) 230 zx_status_t
Status = zx_cache_flush(Addr, Len, ZX_CACHE_FLUSH_INSN);
231 assert(Status == ZX_OK &&
"cannot invalidate instruction cache");
235 # if (defined(__POWERPC__) || defined (__ppc__) || \ 236 defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__) 237 const size_t LineSize = 32;
243 for (
intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
244 asm volatile(
"dcbf 0, %0" : :
"r"(Line));
245 asm volatile(
"sync");
247 for (
intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
248 asm volatile(
"icbi 0, %0" : :
"r"(Line));
249 asm volatile(
"isync");
250 # elif (defined(__arm__) || defined(__aarch64__) || defined(__mips__)) && \ 253 const char *Start =
static_cast<const char *
>(Addr);
254 const char *End = Start + Len;
255 __clear_cache(const_cast<char *>(Start), const_cast<char *>(End));
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This class represents lattice values for constants.
static std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method...
static MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)
This method allocates a block of memory that is suitable for loading dynamically generated code (e...
void ValgrindDiscardTranslations(const void *Addr, size_t Len)
static void InvalidateInstructionCache(const void *Addr, size_t Len)
InvalidateInstructionCache - Before the JIT can run a block of code that has been emitted it must inv...
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Provides a library for accessing information about this process and other processes on the operating ...
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
static std::error_code protectMappedMemory(const MemoryBlock &Block, unsigned Flags)
This method sets the protection flags for a block of memory to the state specified by /p Flags...