cb8abc3ae5
72f7ccd94 oaknut: 1.1.3 0b5745e4e oaknut: Add Windows on Arm support (#1) 5de40335d oaknut: 1.1.2 2952b759f oaknut: Correct MOV (UMOV alias) c90eb31ca oaknut: 1.1.1 7c777a28f oaknut: Fix ADR and ADRP 7470c7611 oaknut: Add ARMv8.2 instructions 9eb7cca88 oaknut: Update README 3fe32849a oaknut: 1.1.0 542128b51 oaknut: Add ARMv8.1 instructions 9acafdcdd oaknut: fpsimd MOV and UMOV corrections 636f91bac oaknut: MOV: Fix MOVN case 9cb332621 oaknut: Implement arranged accessors from DReg and QReg ba2dc2afe oaknut: dx 94bf56b08 oaknut: align aa7a3519f oaknut: Add dw 898f666ec oaknut: Add common system registers git-subtree-dir: externals/oaknut git-subtree-split: 72f7ccd9409dadf6a4ab98bad1fb11fbf0ca4d74
130 lines
3.3 KiB
C++
130 lines
3.3 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 <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__)
|
|
m_memory = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE | MAP_JIT, -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__)
|
|
pthread_jit_write_protect_np(1);
|
|
#endif
|
|
}
|
|
|
|
void unprotect()
|
|
{
|
|
#if defined(__APPLE__)
|
|
pthread_jit_write_protect_np(0);
|
|
#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
|