99c0a73f91
d0488d932 oaknut: 2.0.0 40ad78bbf oaknut: Implement DualCodeBlock and related support 9f131cfb5 oaknut: add configuration for standalone installation 69799b43c oaknut: Test building for Android on CI 1d51f5512 oaknut: 1.2.2 918bd94f0 oaknut: Eliminate -Wconversion warnings 316d8869e oaknut: Fix edgecases in MOVP2R on +/-4GiB boundary d8634eaa1 oaknut: Fix page boundary error in ADP d0ca9a24e oaknut: Update README examples for CPU feature detection dbeec268b oaknut: feature_detection_freebsd: Warn about incompatibility with earlier FreeBSD versions 86e5386e2 oaknut: feature_detect: Support NetBSD df4cf2d48 oaknut: feature_detect: Support OpenBSD 99dfff25a oaknut: feature_detection: Read ID registers 319b3d2c9 oaknut: Add basic CPU feature detection 23e9ddb4c oaknut: CI: Don't run slow tests on OpenBSD 734f1bdb4 oaknut: CI: Use up-to-date qemu f462c9774 oaknut: CI: Build on OpenBSD 19cd42204 oaknut: code_block: Add NetBSD and OpenBSD support 18b86a3ec oaknut: SystemReg: Add more EL0 accessible registers 53c43bf0c oaknut/tests: Reduce iterations for MOVP2R cc37df19e oaknut: Test on FreeBSD a66b32d26 oaknut: Fix crossing sign boundary in PageOffset 206468d72 oaknut: CI: Add macos-arm64 build e6eecc3f9 oaknut: 1.2.1 4252d8f4a oaknut: CMakeLists: Warnings are errors on MSVC 408eed65f oaknut: arm64_encode_helpers: remove unreachable code bfc8eedfb oaknut: arm64_encode_helpers: p maybe unused ff4456eca oaknut: Avoid negation of unsigned values b4ac8fd6c oaknut: Fix MOV for applications of MOVN 0575cadc4 oaknut: Disable certain functionality where absolute addressing is not available 394a3c8f0 oaknut: Appease MSVC 011183670 oaknut: 1.2.0 e83c9f327 oaknut: Add VectorCodeGenerator 5eb122cc5 oaknut: Tidy up public header 45c5a7b25 oaknut: Fix clang-format errors 36243256f oaknut: Add `const` qualifier to `AddrOffset` ctor 4af500cb5 oaknut: Add `ptr` accessor to `Label` bccb06669 oaknut: CodeGenerator const correctness da0590a86 oaknut: github: Update package repositories git-subtree-dir: externals/oaknut git-subtree-split: d0488d9320ae673167dd9117223e3453d5ff102f
143 lines
4.1 KiB
C++
143 lines
4.1 KiB
C++
// SPDX-FileCopyrightText: Copyright (c) 2022 merryhime <https://mary.rs>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <new>
|
|
|
|
#if defined(_WIN32)
|
|
# define NOMINMAX
|
|
# include <windows.h>
|
|
#elif defined(__APPLE__)
|
|
# include <TargetConditionals.h>
|
|
# include <libkern/OSCacheControl.h>
|
|
# include <pthread.h>
|
|
# include <sys/mman.h>
|
|
# include <unistd.h>
|
|
#else
|
|
# include <sys/mman.h>
|
|
#endif
|
|
|
|
namespace oaknut {
|
|
|
|
class CodeBlock {
|
|
public:
|
|
explicit CodeBlock(std::size_t size)
|
|
: m_size(size)
|
|
{
|
|
#if defined(_WIN32)
|
|
m_memory = (std::uint32_t*)VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
#elif defined(__APPLE__)
|
|
# if TARGET_OS_IPHONE
|
|
m_memory = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
# else
|
|
m_memory = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE | MAP_JIT, -1, 0);
|
|
# endif
|
|
#elif defined(__NetBSD__)
|
|
m_memory = (std::uint32_t*)mmap(nullptr, size, PROT_MPROTECT(PROT_READ | PROT_WRITE | PROT_EXEC), MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
#elif defined(__OpenBSD__)
|
|
m_memory = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
#else
|
|
m_memory = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
#endif
|
|
|
|
if (m_memory == nullptr)
|
|
throw std::bad_alloc{};
|
|
}
|
|
|
|
~CodeBlock()
|
|
{
|
|
if (m_memory == nullptr)
|
|
return;
|
|
|
|
#if defined(_WIN32)
|
|
VirtualFree((void*)m_memory, 0, MEM_RELEASE);
|
|
#else
|
|
munmap(m_memory, m_size);
|
|
#endif
|
|
}
|
|
|
|
CodeBlock(const CodeBlock&) = delete;
|
|
CodeBlock& operator=(const CodeBlock&) = delete;
|
|
CodeBlock(CodeBlock&&) = delete;
|
|
CodeBlock& operator=(CodeBlock&&) = delete;
|
|
|
|
std::uint32_t* ptr() const
|
|
{
|
|
return m_memory;
|
|
}
|
|
|
|
void protect()
|
|
{
|
|
#if defined(__APPLE__) && !TARGET_OS_IPHONE
|
|
pthread_jit_write_protect_np(1);
|
|
#elif defined(__APPLE__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
|
mprotect(m_memory, m_size, PROT_READ | PROT_EXEC);
|
|
#endif
|
|
}
|
|
|
|
void unprotect()
|
|
{
|
|
#if defined(__APPLE__) && !TARGET_OS_IPHONE
|
|
pthread_jit_write_protect_np(0);
|
|
#elif defined(__APPLE__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
|
mprotect(m_memory, m_size, PROT_READ | PROT_WRITE);
|
|
#endif
|
|
}
|
|
|
|
void invalidate(std::uint32_t* mem, std::size_t size)
|
|
{
|
|
#if defined(__APPLE__)
|
|
sys_icache_invalidate(mem, size);
|
|
#elif defined(_WIN32)
|
|
FlushInstructionCache(GetCurrentProcess(), mem, size);
|
|
#else
|
|
static std::size_t icache_line_size = 0x10000, dcache_line_size = 0x10000;
|
|
|
|
std::uint64_t ctr;
|
|
__asm__ volatile("mrs %0, ctr_el0"
|
|
: "=r"(ctr));
|
|
|
|
const std::size_t isize = icache_line_size = std::min<std::size_t>(icache_line_size, 4 << ((ctr >> 0) & 0xf));
|
|
const std::size_t dsize = dcache_line_size = std::min<std::size_t>(dcache_line_size, 4 << ((ctr >> 16) & 0xf));
|
|
|
|
const std::uintptr_t end = (std::uintptr_t)mem + size;
|
|
|
|
for (std::uintptr_t addr = ((std::uintptr_t)mem) & ~(dsize - 1); addr < end; addr += dsize) {
|
|
__asm__ volatile("dc cvau, %0"
|
|
:
|
|
: "r"(addr)
|
|
: "memory");
|
|
}
|
|
__asm__ volatile("dsb ish\n"
|
|
:
|
|
:
|
|
: "memory");
|
|
|
|
for (std::uintptr_t addr = ((std::uintptr_t)mem) & ~(isize - 1); addr < end; addr += isize) {
|
|
__asm__ volatile("ic ivau, %0"
|
|
:
|
|
: "r"(addr)
|
|
: "memory");
|
|
}
|
|
__asm__ volatile("dsb ish\nisb\n"
|
|
:
|
|
:
|
|
: "memory");
|
|
#endif
|
|
}
|
|
|
|
void invalidate_all()
|
|
{
|
|
invalidate(m_memory, m_size);
|
|
}
|
|
|
|
protected:
|
|
std::uint32_t* m_memory;
|
|
std::size_t m_size = 0;
|
|
};
|
|
|
|
} // namespace oaknut
|