2022-07-10 09:35:44 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright (c) 2022 merryhime <https://mary.rs>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-07-12 20:41:36 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
2022-07-10 09:35:44 +01:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2022-11-15 15:36:04 +00:00
|
|
|
# define NOMINMAX
|
2022-07-10 09:35:44 +01:00
|
|
|
# include <windows.h>
|
|
|
|
#elif defined(__APPLE__)
|
2023-04-27 22:20:30 +01:00
|
|
|
# include <TargetConditionals.h>
|
2022-07-10 09:35:44 +01:00
|
|
|
# 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__)
|
2023-04-27 22:20:30 +01:00
|
|
|
# if TARGET_OS_IPHONE
|
|
|
|
m_memory = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
|
|
# else
|
2022-07-10 09:35:44 +01:00
|
|
|
m_memory = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE | MAP_JIT, -1, 0);
|
2023-04-27 22:20:30 +01:00
|
|
|
# endif
|
2022-07-10 09:35:44 +01:00
|
|
|
#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__)
|
2023-04-27 22:20:30 +01:00
|
|
|
# if TARGET_OS_IPHONE
|
|
|
|
mprotect(m_memory, m_size, PROT_READ | PROT_EXEC);
|
|
|
|
# else
|
2022-07-10 09:35:44 +01:00
|
|
|
pthread_jit_write_protect_np(1);
|
2023-04-27 22:20:30 +01:00
|
|
|
# endif
|
2022-07-10 09:35:44 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void unprotect()
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__)
|
2023-04-27 22:20:30 +01:00
|
|
|
# if TARGET_OS_IPHONE
|
|
|
|
mprotect(m_memory, m_size, PROT_READ | PROT_WRITE);
|
|
|
|
# else
|
2022-07-10 09:35:44 +01:00
|
|
|
pthread_jit_write_protect_np(0);
|
2023-04-27 22:20:30 +01:00
|
|
|
# endif
|
2022-07-10 09:35:44 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void invalidate(std::uint32_t* mem, std::size_t size)
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
sys_icache_invalidate(mem, size);
|
2022-11-15 15:36:04 +00:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
FlushInstructionCache(GetCurrentProcess(), mem, size);
|
2022-07-10 09:35:44 +01:00
|
|
|
#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
|