CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/CodeBlock.h
Views: 1401
// Copyright 2013 Dolphin Emulator Project1// Licensed under GPLv22// Refer to the license.txt file included.34#pragma once56#include <cstddef>78#include "Common/CommonTypes.h"9#include "Common/Log.h"10#include "Common/MemoryUtil.h"1112#if PPSSPP_PLATFORM(SWITCH)13#include <cstdio>14#include <switch.h>15#endif // PPSSPP_PLATFORM(SWITCH)1617// Everything that needs to generate code should inherit from this.18// You get memory management for free, plus, you can use all emitter functions without19// having to prefix them with gen-> or something similar.20// Example implementation:21// class JIT : public CodeBlock<ARMXEmitter>, public JitInterface {}2223class CodeBlockCommon {24public:25CodeBlockCommon() {}26virtual ~CodeBlockCommon() {}2728bool IsInSpace(const u8 *ptr) const {29return (ptr >= region) && (ptr < (region + region_size));30}3132virtual const u8 *GetCodePtr() const = 0;3334u8 *GetBasePtr() const {35return region;36}3738size_t GetOffset(const u8 *ptr) const {39return ptr - region;40}4142virtual const u8 *GetCodePtrFromWritablePtr(u8 *ptr) = 0;43virtual u8 *GetWritablePtrFromCodePtr(const u8 *ptr) = 0;4445protected:46virtual void SetCodePtr(u8 *ptr) = 0;4748// Note: this should be the readable/executable side if writable is a different pointer.49u8 *region = nullptr;50size_t region_size = 0;51};5253template<class T> class CodeBlock : public CodeBlockCommon, public T {54private:55CodeBlock(const CodeBlock &) = delete;56void operator=(const CodeBlock &) = delete;5758// A privately used function to set the executable RAM space to something invalid.59// For debugging usefulness it should be used to set the RAM to a host specific breakpoint instruction60virtual void PoisonMemory(int offset) = 0;6162public:63CodeBlock() {}64~CodeBlock() {65if (region)66FreeCodeSpace();67}6869// Call this before you generate any code.70void AllocCodeSpace(int size) {71region_size = size;72#if PPSSPP_PLATFORM(SWITCH)73Result rc = jitCreate(&jitController, size);74if(R_FAILED(rc)) {75printf("Failed to create Jitbuffer of size 0x%x err: 0x%x\n", size, rc);76}77printf("[NXJIT]: Initialized RX: %p RW: %p\n", jitController.rx_addr, jitController.rw_addr);7879region = (u8 *)jitController.rx_addr;80writableRegion = (u8 *)jitController.rw_addr;81#else // PPSSPP_PLATFORM(SWITCH)82// The protection will be set to RW if PlatformIsWXExclusive.83region = (u8 *)AllocateExecutableMemory(region_size);84writableRegion = region;85#endif // !PPSSPP_PLATFORM(SWITCH)86T::SetCodePointer(region, writableRegion);87}8889// Always clear code space with breakpoints, so that if someone accidentally executes90// uninitialized, it just breaks into the debugger.91void ClearCodeSpace(int offset) {92if (!region) {93return;94}95if (PlatformIsWXExclusive()) {96ProtectMemoryPages(region, region_size, MEM_PROT_READ | MEM_PROT_WRITE);97}98// If not WX Exclusive, no need to call ProtectMemoryPages because we never change the protection from RWX.99PoisonMemory(offset);100ResetCodePtr(offset);101if (PlatformIsWXExclusive() && offset != 0) {102// Need to re-protect the part we didn't clear.103ProtectMemoryPages(region, offset, MEM_PROT_READ | MEM_PROT_EXEC);104}105}106107// BeginWrite/EndWrite assume that we keep appending.108// If you don't specify a size and we later encounter an executable non-writable block, we're screwed.109// These CANNOT be nested. We rely on the memory protection starting at READ|WRITE after start and reset.110void BeginWrite(size_t sizeEstimate = 1) {111_dbg_assert_msg_(!writeStart_, "Can't nest BeginWrite calls");112113// In case the last block made the current page exec/no-write, let's fix that.114if (PlatformIsWXExclusive()) {115writeStart_ = GetCodePtr();116if (writeStart_ + sizeEstimate - region > (ptrdiff_t)region_size)117sizeEstimate = region_size - (writeStart_ - region);118writeEstimated_ = sizeEstimate;119ProtectMemoryPages(writeStart_, sizeEstimate, MEM_PROT_READ | MEM_PROT_WRITE);120}121}122123// In case you now know your original estimate is wrong.124void ContinueWrite(size_t sizeEstimate = 1) {125_dbg_assert_msg_(writeStart_, "Must have already called BeginWrite()");126if (PlatformIsWXExclusive()) {127const uint8_t *pos = GetCodePtr();128if (pos + sizeEstimate - region > (ptrdiff_t)region_size)129sizeEstimate = region_size - (pos - region);130writeEstimated_ = pos - writeStart_ + sizeEstimate;131ProtectMemoryPages(pos, sizeEstimate, MEM_PROT_READ | MEM_PROT_WRITE);132}133}134135void EndWrite() {136// OK, we're done. Re-protect the memory we touched.137if (PlatformIsWXExclusive() && writeStart_ != nullptr) {138const uint8_t *end = GetCodePtr();139size_t sz = end - writeStart_;140if (sz > writeEstimated_)141WARN_LOG(Log::JIT, "EndWrite(): Estimated %d bytes, wrote %d", (int)writeEstimated_, (int)sz);142// If we protected and wrote less, we may need to unprotect.143// Especially if we're linking blocks or similar.144if (sz < writeEstimated_)145sz = writeEstimated_;146ProtectMemoryPages(writeStart_, sz, MEM_PROT_READ | MEM_PROT_EXEC);147writeStart_ = nullptr;148}149}150151// Call this when shutting down. Don't rely on the destructor, even though it'll do the job.152void FreeCodeSpace() {153#if !PPSSPP_PLATFORM(SWITCH)154ProtectMemoryPages(region, region_size, MEM_PROT_READ | MEM_PROT_WRITE);155FreeExecutableMemory(region, region_size);156#else // !PPSSPP_PLATFORM(SWITCH)157jitClose(&jitController);158printf("[NXJIT]: Jit closed\n");159#endif // PPSSPP_PLATFORM(SWITCH)160region = nullptr;161writableRegion = nullptr;162region_size = 0;163}164165const u8 *GetCodePtr() const override {166return T::GetCodePointer();167}168169void ResetCodePtr(size_t offset) {170T::SetCodePointer(region + offset, writableRegion + offset);171}172173size_t GetSpaceLeft() const {174return region_size - (T::GetCodePointer() - region);175}176177const u8 *GetCodePtrFromWritablePtr(u8 *ptr) override {178// So we can adjust region to writable space. Might be zero.179ptrdiff_t writable = T::GetWritableCodePtr() - T::GetCodePointer();180return ptr - writable;181}182183u8 *GetWritablePtrFromCodePtr(const u8 *ptr) override {184// So we can adjust region to writable space. Might be zero.185ptrdiff_t writable = T::GetWritableCodePtr() - T::GetCodePointer();186return (u8 *)ptr + writable;187}188189protected:190void SetCodePtr(u8 *ptr) override {191T::SetCodePointer(ptr, GetWritablePtrFromCodePtr(ptr));192}193194private:195// Note: this is a readable pointer.196const uint8_t *writeStart_ = nullptr;197uint8_t *writableRegion = nullptr;198size_t writeEstimated_ = 0;199#if PPSSPP_PLATFORM(SWITCH)200Jit jitController;201#endif // PPSSPP_PLATFORM(SWITCH)202};203204205