2004-09-11 12:57:25 +08:00
|
|
|
//===- Unix/Memory.cpp - Generic UNIX System Configuration ------*- C++ -*-===//
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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"
|
2020-01-25 10:32:54 +08:00
|
|
|
#include "llvm/Support/Alignment.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
|
|
|
|
2020-01-10 01:38:50 +08:00
|
|
|
static int getPosixProtectionFlags(unsigned Flags) {
|
2019-02-28 10:47:34 +08:00
|
|
|
switch (Flags & llvm::sys::Memory::MF_RWE_MASK) {
|
2012-09-20 04:46:12 +08:00
|
|
|
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:
|
2019-06-04 00:20:59 +08:00
|
|
|
#if (defined(__FreeBSD__) || defined(__POWERPC__) || defined (__ppc__) || \
|
|
|
|
defined(_POWER) || defined(_ARCH_PPC))
|
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.
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2019-05-16 22:02:13 +08:00
|
|
|
// On platforms that have it, we can use MAP_ANON to get a memory-mapped
|
|
|
|
// page without file backing, but we need a fallback of opening /dev/zero
|
|
|
|
// for strictly POSIX platforms instead.
|
|
|
|
int fd;
|
|
|
|
#if defined(MAP_ANON)
|
|
|
|
fd = -1;
|
|
|
|
#else
|
|
|
|
fd = open("/dev/zero", O_RDWR);
|
|
|
|
if (fd == -1) {
|
|
|
|
EC = std::error_code(errno, std::generic_category());
|
|
|
|
return MemoryBlock();
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-20 04:46:12 +08:00
|
|
|
|
2019-05-16 22:02:13 +08:00
|
|
|
int MMFlags = MAP_PRIVATE;
|
|
|
|
#if defined(MAP_ANON)
|
|
|
|
MMFlags |= MAP_ANON;
|
|
|
|
#endif
|
2012-09-20 04:46:12 +08:00
|
|
|
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()) +
|
2019-05-21 04:53:05 +08:00
|
|
|
NearBlock->allocatedSize() : 0;
|
2019-05-08 10:11:07 +08:00
|
|
|
static const size_t PageSize = Process::getPageSizeEstimate();
|
2019-05-21 04:53:05 +08:00
|
|
|
const size_t NumPages = (NumBytes+PageSize-1)/PageSize;
|
|
|
|
|
2012-09-20 04:46:12 +08:00
|
|
|
if (Start && Start % PageSize)
|
|
|
|
Start += PageSize - Start % PageSize;
|
|
|
|
|
2019-02-28 10:47:34 +08:00
|
|
|
// FIXME: Handle huge page requests (MF_HUGE_HINT).
|
2019-05-21 04:53:05 +08:00
|
|
|
void *Addr = ::mmap(reinterpret_cast<void *>(Start), PageSize*NumPages, Protect,
|
2019-01-23 10:03:26 +08:00
|
|
|
MMFlags, fd, 0);
|
2012-09-20 04:46:12 +08:00
|
|
|
if (Addr == MAP_FAILED) {
|
2019-05-16 22:02:13 +08:00
|
|
|
if (NearBlock) { //Try again without a near hint
|
|
|
|
#if !defined(MAP_ANON)
|
|
|
|
close(fd);
|
|
|
|
#endif
|
2014-04-28 12:05:08 +08:00
|
|
|
return allocateMappedMemory(NumBytes, nullptr, PFlags, EC);
|
2019-05-16 22:02:13 +08:00
|
|
|
}
|
2012-09-20 04:46:12 +08:00
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
EC = std::error_code(errno, std::generic_category());
|
2019-05-16 22:02:13 +08:00
|
|
|
#if !defined(MAP_ANON)
|
|
|
|
close(fd);
|
|
|
|
#endif
|
2012-09-20 04:46:12 +08:00
|
|
|
return MemoryBlock();
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:02:13 +08:00
|
|
|
#if !defined(MAP_ANON)
|
|
|
|
close(fd);
|
|
|
|
#endif
|
|
|
|
|
2012-09-20 04:46:12 +08:00
|
|
|
MemoryBlock Result;
|
|
|
|
Result.Address = Addr;
|
2019-05-21 04:53:05 +08:00
|
|
|
Result.AllocatedSize = PageSize*NumPages;
|
2019-02-28 11:03:07 +08:00
|
|
|
Result.Flags = PFlags;
|
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
|
|
|
// 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) {
|
2019-05-21 04:53:05 +08:00
|
|
|
if (M.Address == nullptr || M.AllocatedSize == 0)
|
2014-06-13 10:24:39 +08:00
|
|
|
return std::error_code();
|
2012-09-20 04:46:12 +08:00
|
|
|
|
2019-05-21 04:53:05 +08:00
|
|
|
if (0 != ::munmap(M.Address, M.AllocatedSize))
|
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;
|
2019-05-21 04:53:05 +08:00
|
|
|
M.AllocatedSize = 0;
|
2012-09-20 04:46:12 +08:00
|
|
|
|
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) {
|
2019-10-14 21:14:34 +08:00
|
|
|
static const Align PageSize = Align(Process::getPageSizeEstimate());
|
2019-05-21 04:53:05 +08:00
|
|
|
if (M.Address == nullptr || M.AllocatedSize == 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);
|
2019-10-14 21:14:34 +08:00
|
|
|
uintptr_t Start = alignAddr((const uint8_t *)M.Address - PageSize.value() + 1, PageSize);
|
|
|
|
uintptr_t End = alignAddr((const uint8_t *)M.Address + M.AllocatedSize, 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());
|
|
|
|
|
2019-05-21 04:53:05 +08:00
|
|
|
Memory::InvalidateInstructionCache(M.Address, M.AllocatedSize);
|
[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
|
|
|
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)
|
2019-05-21 04:53:05 +08:00
|
|
|
Memory::InvalidateInstructionCache(M.Address, M.AllocatedSize);
|
2012-09-20 04:46:12 +08:00
|
|
|
|
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
|