2004-09-11 12:57:25 +08:00
|
|
|
//===- Unix/Memory.cpp - Generic UNIX System Configuration ------*- C++ -*-===//
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2004-09-11 12:57:25 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2004-09-11 12:57:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines some functions for various memory management utilities.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Unix.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2012-09-20 04:46:12 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2004-12-27 14:17:03 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
2004-09-11 12:57:25 +08:00
|
|
|
|
2008-09-18 15:54:21 +08:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#endif
|
|
|
|
|
2018-06-06 14:26:18 +08:00
|
|
|
#ifdef __Fuchsia__
|
|
|
|
#include <zircon/syscalls.h>
|
|
|
|
#endif
|
|
|
|
|
2012-09-11 09:17:24 +08:00
|
|
|
#if defined(__mips__)
|
|
|
|
# if defined(__OpenBSD__)
|
|
|
|
# include <mips64/sysarch.h>
|
2017-10-25 22:53:16 +08:00
|
|
|
# elif !defined(__FreeBSD__)
|
2012-09-11 09:17:24 +08:00
|
|
|
# include <sys/cachectl.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2018-06-06 14:26:18 +08:00
|
|
|
#if defined(__APPLE__)
|
2012-09-20 04:46:12 +08:00
|
|
|
extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
|
2013-05-20 04:33:51 +08:00
|
|
|
#else
|
2013-05-15 02:06:14 +08:00
|
|
|
extern "C" void __clear_cache(void *, void*);
|
2013-05-20 04:33:51 +08:00
|
|
|
#endif
|
2012-09-20 04:46:12 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
int getPosixProtectionFlags(unsigned Flags) {
|
|
|
|
switch (Flags) {
|
|
|
|
case llvm::sys::Memory::MF_READ:
|
|
|
|
return PROT_READ;
|
|
|
|
case llvm::sys::Memory::MF_WRITE:
|
|
|
|
return PROT_WRITE;
|
|
|
|
case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_WRITE:
|
|
|
|
return PROT_READ | PROT_WRITE;
|
|
|
|
case llvm::sys::Memory::MF_READ|llvm::sys::Memory::MF_EXEC:
|
|
|
|
return PROT_READ | PROT_EXEC;
|
2015-09-22 19:15:07 +08:00
|
|
|
case llvm::sys::Memory::MF_READ | llvm::sys::Memory::MF_WRITE |
|
|
|
|
llvm::sys::Memory::MF_EXEC:
|
2012-09-20 04:46:12 +08:00
|
|
|
return PROT_READ | PROT_WRITE | PROT_EXEC;
|
|
|
|
case llvm::sys::Memory::MF_EXEC:
|
2013-02-21 02:24:30 +08:00
|
|
|
#if defined(__FreeBSD__)
|
2013-02-21 03:25:09 +08:00
|
|
|
// On PowerPC, having an executable page that has no read permission
|
|
|
|
// can have unintended consequences. The function InvalidateInstruction-
|
|
|
|
// Cache uses instructions dcbf and icbi, both of which are treated by
|
|
|
|
// the processor as loads. If the page has no read permissions,
|
|
|
|
// executing these instructions will result in a segmentation fault.
|
|
|
|
// Somehow, this problem is not present on Linux, but it does happen
|
|
|
|
// on FreeBSD.
|
2013-02-21 02:24:30 +08:00
|
|
|
return PROT_READ | PROT_EXEC;
|
|
|
|
#else
|
2012-09-20 04:46:12 +08:00
|
|
|
return PROT_EXEC;
|
2013-02-21 02:24:30 +08:00
|
|
|
#endif
|
2012-09-20 04:46:12 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Illegal memory protection flag specified!");
|
|
|
|
}
|
|
|
|
// Provide a default return value as required by some compilers.
|
|
|
|
return PROT_NONE;
|
|
|
|
}
|
|
|
|
|
2016-01-27 02:48:36 +08:00
|
|
|
} // anonymous namespace
|
2012-09-20 04:46:12 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace sys {
|
|
|
|
|
|
|
|
MemoryBlock
|
|
|
|
Memory::allocateMappedMemory(size_t NumBytes,
|
|
|
|
const MemoryBlock *const NearBlock,
|
|
|
|
unsigned PFlags,
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code &EC) {
|
|
|
|
EC = std::error_code();
|
2012-09-20 04:46:12 +08:00
|
|
|
if (NumBytes == 0)
|
|
|
|
return MemoryBlock();
|
|
|
|
|
2014-12-05 00:59:36 +08:00
|
|
|
static const size_t PageSize = Process::getPageSize();
|
2012-09-20 04:46:12 +08:00
|
|
|
const size_t NumPages = (NumBytes+PageSize-1)/PageSize;
|
|
|
|
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
int MMFlags = MAP_PRIVATE |
|
2016-10-01 04:17:23 +08:00
|
|
|
#ifdef MAP_ANONYMOUS
|
2012-09-20 04:46:12 +08:00
|
|
|
MAP_ANONYMOUS
|
|
|
|
#else
|
|
|
|
MAP_ANON
|
|
|
|
#endif
|
|
|
|
; // Ends statement above
|
|
|
|
|
|
|
|
int Protect = getPosixProtectionFlags(PFlags);
|
|
|
|
|
2017-11-17 07:04:44 +08:00
|
|
|
#if defined(__NetBSD__) && defined(PROT_MPROTECT)
|
|
|
|
Protect |= PROT_MPROTECT(PROT_READ | PROT_WRITE | PROT_EXEC);
|
|
|
|
#endif
|
|
|
|
|
2012-09-20 04:46:12 +08:00
|
|
|
// Use any near hint and the page size to set a page-aligned starting address
|
|
|
|
uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
|
|
|
|
NearBlock->size() : 0;
|
|
|
|
if (Start && Start % PageSize)
|
|
|
|
Start += PageSize - Start % PageSize;
|
|
|
|
|
|
|
|
void *Addr = ::mmap(reinterpret_cast<void*>(Start), PageSize*NumPages,
|
|
|
|
Protect, MMFlags, fd, 0);
|
|
|
|
if (Addr == MAP_FAILED) {
|
|
|
|
if (NearBlock) //Try again without a near hint
|
2014-04-28 12:05:08 +08:00
|
|
|
return allocateMappedMemory(NumBytes, nullptr, PFlags, EC);
|
2012-09-20 04:46:12 +08:00
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
EC = std::error_code(errno, std::generic_category());
|
2012-09-20 04:46:12 +08:00
|
|
|
return MemoryBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryBlock Result;
|
|
|
|
Result.Address = Addr;
|
|
|
|
Result.Size = NumPages*PageSize;
|
|
|
|
|
[ARM][AArch64] Workaround ARM/AArch64 peculiarity in clearing icache.
Certain ARM implementations treat icache clear instruction as a memory read,
and CPU segfaults on trying to clear cache on !PROT_READ page.
We workaround this in Memory::protectMappedMemory by adding
PROT_READ to affected pages, clearing the cache, and then setting
desired protection.
This fixes "AllocationTests/MappedMemoryTest.***/3" unit-tests on
affected hardware.
Reviewers: psmith, zatrazz, kristof.beyls, lhames
Reviewed By: lhames
Subscribers: llvm-commits, krytarowski, peter.smith, jgreenhalgh, aemerson,
rengolin
Patch by maxim-kuvrykov!
Differential Revision: https://reviews.llvm.org/D40423
llvm-svn: 319166
2017-11-28 20:34:05 +08:00
|
|
|
// Rely on protectMappedMemory to invalidate instruction cache.
|
|
|
|
if (PFlags & MF_EXEC) {
|
|
|
|
EC = Memory::protectMappedMemory (Result, PFlags);
|
|
|
|
if (EC != std::error_code())
|
|
|
|
return MemoryBlock();
|
|
|
|
}
|
2012-09-20 04:46:12 +08:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code
|
2012-09-20 04:46:12 +08:00
|
|
|
Memory::releaseMappedMemory(MemoryBlock &M) {
|
2014-04-28 12:05:08 +08:00
|
|
|
if (M.Address == nullptr || M.Size == 0)
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2012-09-20 04:46:12 +08:00
|
|
|
|
|
|
|
if (0 != ::munmap(M.Address, M.Size))
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code(errno, std::generic_category());
|
2012-09-20 04:46:12 +08:00
|
|
|
|
2014-04-28 12:05:08 +08:00
|
|
|
M.Address = nullptr;
|
2012-09-20 04:46:12 +08:00
|
|
|
M.Size = 0;
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2012-09-20 04:46:12 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code
|
2012-09-20 04:46:12 +08:00
|
|
|
Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) {
|
2015-12-16 19:13:23 +08:00
|
|
|
static const size_t PageSize = Process::getPageSize();
|
2014-04-28 12:05:08 +08:00
|
|
|
if (M.Address == nullptr || M.Size == 0)
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2012-09-20 04:46:12 +08:00
|
|
|
|
|
|
|
if (!Flags)
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code(EINVAL, std::generic_category());
|
2012-09-20 04:46:12 +08:00
|
|
|
|
|
|
|
int Protect = getPosixProtectionFlags(Flags);
|
2016-11-05 12:22:15 +08:00
|
|
|
uintptr_t Start = alignAddr((uint8_t *)M.Address - PageSize + 1, PageSize);
|
|
|
|
uintptr_t End = alignAddr((uint8_t *)M.Address + M.Size, PageSize);
|
[ARM][AArch64] Workaround ARM/AArch64 peculiarity in clearing icache.
Certain ARM implementations treat icache clear instruction as a memory read,
and CPU segfaults on trying to clear cache on !PROT_READ page.
We workaround this in Memory::protectMappedMemory by adding
PROT_READ to affected pages, clearing the cache, and then setting
desired protection.
This fixes "AllocationTests/MappedMemoryTest.***/3" unit-tests on
affected hardware.
Reviewers: psmith, zatrazz, kristof.beyls, lhames
Reviewed By: lhames
Subscribers: llvm-commits, krytarowski, peter.smith, jgreenhalgh, aemerson,
rengolin
Patch by maxim-kuvrykov!
Differential Revision: https://reviews.llvm.org/D40423
llvm-svn: 319166
2017-11-28 20:34:05 +08:00
|
|
|
|
|
|
|
bool InvalidateCache = (Flags & MF_EXEC);
|
|
|
|
|
|
|
|
#if defined(__arm__) || defined(__aarch64__)
|
|
|
|
// Certain ARM implementations treat icache clear instruction as a memory read,
|
|
|
|
// and CPU segfaults on trying to clear cache on !PROT_READ page. Therefore we need
|
|
|
|
// to temporarily add PROT_READ for the sake of flushing the instruction caches.
|
|
|
|
if (InvalidateCache && !(Protect & PROT_READ)) {
|
|
|
|
int Result = ::mprotect((void *)Start, End - Start, Protect | PROT_READ);
|
|
|
|
if (Result != 0)
|
|
|
|
return std::error_code(errno, std::generic_category());
|
|
|
|
|
|
|
|
Memory::InvalidateInstructionCache(M.Address, M.Size);
|
|
|
|
InvalidateCache = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-05 12:22:15 +08:00
|
|
|
int Result = ::mprotect((void *)Start, End - Start, Protect);
|
|
|
|
|
2012-09-20 04:46:12 +08:00
|
|
|
if (Result != 0)
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code(errno, std::generic_category());
|
2012-09-20 04:46:12 +08:00
|
|
|
|
[ARM][AArch64] Workaround ARM/AArch64 peculiarity in clearing icache.
Certain ARM implementations treat icache clear instruction as a memory read,
and CPU segfaults on trying to clear cache on !PROT_READ page.
We workaround this in Memory::protectMappedMemory by adding
PROT_READ to affected pages, clearing the cache, and then setting
desired protection.
This fixes "AllocationTests/MappedMemoryTest.***/3" unit-tests on
affected hardware.
Reviewers: psmith, zatrazz, kristof.beyls, lhames
Reviewed By: lhames
Subscribers: llvm-commits, krytarowski, peter.smith, jgreenhalgh, aemerson,
rengolin
Patch by maxim-kuvrykov!
Differential Revision: https://reviews.llvm.org/D40423
llvm-svn: 319166
2017-11-28 20:34:05 +08:00
|
|
|
if (InvalidateCache)
|
2012-09-20 04:46:12 +08:00
|
|
|
Memory::InvalidateInstructionCache(M.Address, M.Size);
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2012-09-20 04:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// InvalidateInstructionCache - Before the JIT can run a block of code
|
|
|
|
/// that has been emitted it must invalidate the instruction cache on some
|
|
|
|
/// platforms.
|
|
|
|
void Memory::InvalidateInstructionCache(const void *Addr,
|
|
|
|
size_t Len) {
|
|
|
|
|
|
|
|
// icache invalidation for PPC and ARM.
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
2013-07-27 06:13:57 +08:00
|
|
|
# if (defined(__POWERPC__) || defined (__ppc__) || \
|
2014-03-29 18:18:08 +08:00
|
|
|
defined(_POWER) || defined(_ARCH_PPC) || defined(__arm__) || \
|
|
|
|
defined(__arm64__))
|
2012-09-20 04:46:12 +08:00
|
|
|
sys_icache_invalidate(const_cast<void *>(Addr), Len);
|
|
|
|
# endif
|
|
|
|
|
2018-06-06 14:26:18 +08:00
|
|
|
#elif defined(__Fuchsia__)
|
|
|
|
|
|
|
|
zx_status_t Status = zx_cache_flush(Addr, Len, ZX_CACHE_FLUSH_INSN);
|
|
|
|
assert(Status == ZX_OK && "cannot invalidate instruction cache");
|
|
|
|
|
2012-09-20 04:46:12 +08:00
|
|
|
#else
|
|
|
|
|
2013-07-27 06:13:57 +08:00
|
|
|
# if (defined(__POWERPC__) || defined (__ppc__) || \
|
2012-09-20 04:46:12 +08:00
|
|
|
defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__)
|
|
|
|
const size_t LineSize = 32;
|
|
|
|
|
|
|
|
const intptr_t Mask = ~(LineSize - 1);
|
|
|
|
const intptr_t StartLine = ((intptr_t) Addr) & Mask;
|
|
|
|
const intptr_t EndLine = ((intptr_t) Addr + Len + LineSize - 1) & Mask;
|
|
|
|
|
|
|
|
for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
|
|
|
|
asm volatile("dcbf 0, %0" : : "r"(Line));
|
|
|
|
asm volatile("sync");
|
|
|
|
|
|
|
|
for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
|
|
|
|
asm volatile("icbi 0, %0" : : "r"(Line));
|
|
|
|
asm volatile("isync");
|
2015-01-28 07:30:18 +08:00
|
|
|
# elif (defined(__arm__) || defined(__aarch64__) || defined(__mips__)) && \
|
|
|
|
defined(__GNUC__)
|
2012-09-20 04:46:12 +08:00
|
|
|
// FIXME: Can we safely always call this for __GNUC__ everywhere?
|
|
|
|
const char *Start = static_cast<const char *>(Addr);
|
|
|
|
const char *End = Start + Len;
|
|
|
|
__clear_cache(const_cast<char *>(Start), const_cast<char *>(End));
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#endif // end apple
|
|
|
|
|
|
|
|
ValgrindDiscardTranslations(Addr, Len);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sys
|
|
|
|
} // namespace llvm
|