2014-12-15 21:04:08 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-05-17 05:12:35 +00:00
|
|
|
#include <cstddef>
|
2020-10-04 07:29:36 +00:00
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Log.h"
|
|
|
|
#include "Common/MemoryUtil.h"
|
2014-12-15 21:04:08 +00:00
|
|
|
|
2023-06-27 12:21:52 +00:00
|
|
|
#if PPSSPP_PLATFORM(SWITCH)
|
|
|
|
#include <cstdio>
|
|
|
|
#include <switch.h>
|
|
|
|
#endif // PPSSPP_PLATFORM(SWITCH)
|
|
|
|
|
2014-12-15 21:04:08 +00:00
|
|
|
// Everything that needs to generate code should inherit from this.
|
|
|
|
// You get memory management for free, plus, you can use all emitter functions without
|
|
|
|
// having to prefix them with gen-> or something similar.
|
|
|
|
// Example implementation:
|
2016-04-30 23:20:21 +00:00
|
|
|
// class JIT : public CodeBlock<ARMXEmitter>, public JitInterface {}
|
2016-05-01 09:13:51 +00:00
|
|
|
|
|
|
|
class CodeBlockCommon {
|
|
|
|
public:
|
2020-05-17 04:12:31 +00:00
|
|
|
CodeBlockCommon() {}
|
2016-05-01 09:13:51 +00:00
|
|
|
virtual ~CodeBlockCommon() {}
|
|
|
|
|
2019-02-12 09:58:20 +00:00
|
|
|
bool IsInSpace(const u8 *ptr) const {
|
2016-05-01 09:13:51 +00:00
|
|
|
return (ptr >= region) && (ptr < (region + region_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const u8 *GetCodePtr() const = 0;
|
|
|
|
|
2023-08-06 07:15:46 +00:00
|
|
|
u8 *GetBasePtr() const {
|
2016-05-01 09:13:51 +00:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetOffset(const u8 *ptr) const {
|
|
|
|
return ptr - region;
|
|
|
|
}
|
|
|
|
|
2020-05-17 05:12:35 +00:00
|
|
|
virtual const u8 *GetCodePtrFromWritablePtr(u8 *ptr) = 0;
|
|
|
|
virtual u8 *GetWritablePtrFromCodePtr(const u8 *ptr) = 0;
|
|
|
|
|
2016-05-01 09:13:51 +00:00
|
|
|
protected:
|
2020-05-17 05:12:35 +00:00
|
|
|
virtual void SetCodePtr(u8 *ptr) = 0;
|
|
|
|
|
|
|
|
// Note: this should be the readable/executable side if writable is a different pointer.
|
2020-05-17 04:12:31 +00:00
|
|
|
u8 *region = nullptr;
|
|
|
|
size_t region_size = 0;
|
2016-05-01 09:13:51 +00:00
|
|
|
};
|
|
|
|
|
2017-08-31 15:13:18 +00:00
|
|
|
template<class T> class CodeBlock : public CodeBlockCommon, public T {
|
2014-12-15 21:04:08 +00:00
|
|
|
private:
|
2017-08-31 15:13:18 +00:00
|
|
|
CodeBlock(const CodeBlock &) = delete;
|
|
|
|
void operator=(const CodeBlock &) = delete;
|
|
|
|
|
2014-12-15 21:04:08 +00:00
|
|
|
// A privately used function to set the executable RAM space to something invalid.
|
|
|
|
// For debugging usefulness it should be used to set the RAM to a host specific breakpoint instruction
|
2017-05-26 13:39:27 +00:00
|
|
|
virtual void PoisonMemory(int offset) = 0;
|
2014-12-15 21:04:08 +00:00
|
|
|
|
|
|
|
public:
|
2020-05-17 04:12:31 +00:00
|
|
|
CodeBlock() {}
|
2022-12-11 04:32:12 +00:00
|
|
|
~CodeBlock() {
|
|
|
|
if (region)
|
|
|
|
FreeCodeSpace();
|
|
|
|
}
|
2014-12-15 21:04:08 +00:00
|
|
|
|
|
|
|
// Call this before you generate any code.
|
2016-08-28 10:09:01 +00:00
|
|
|
void AllocCodeSpace(int size) {
|
2014-12-15 21:04:08 +00:00
|
|
|
region_size = size;
|
2023-06-27 12:21:52 +00:00
|
|
|
#if PPSSPP_PLATFORM(SWITCH)
|
|
|
|
Result rc = jitCreate(&jitController, size);
|
|
|
|
if(R_FAILED(rc)) {
|
|
|
|
printf("Failed to create Jitbuffer of size 0x%x err: 0x%x\n", size, rc);
|
|
|
|
}
|
|
|
|
printf("[NXJIT]: Initialized RX: %p RW: %p\n", jitController.rx_addr, jitController.rw_addr);
|
|
|
|
|
|
|
|
region = (u8 *)jitController.rx_addr;
|
|
|
|
writableRegion = (u8 *)jitController.rw_addr;
|
|
|
|
#else // PPSSPP_PLATFORM(SWITCH)
|
2017-01-26 08:49:26 +00:00
|
|
|
// The protection will be set to RW if PlatformIsWXExclusive.
|
2020-05-17 04:12:31 +00:00
|
|
|
region = (u8 *)AllocateExecutableMemory(region_size);
|
2020-05-17 05:12:35 +00:00
|
|
|
writableRegion = region;
|
2023-06-27 12:21:52 +00:00
|
|
|
#endif // !PPSSPP_PLATFORM(SWITCH)
|
2020-05-17 05:12:35 +00:00
|
|
|
T::SetCodePointer(region, writableRegion);
|
2014-12-15 21:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Always clear code space with breakpoints, so that if someone accidentally executes
|
|
|
|
// uninitialized, it just breaks into the debugger.
|
2017-05-26 13:39:27 +00:00
|
|
|
void ClearCodeSpace(int offset) {
|
2020-12-01 23:01:29 +00:00
|
|
|
if (!region) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-28 12:52:08 +00:00
|
|
|
if (PlatformIsWXExclusive()) {
|
|
|
|
ProtectMemoryPages(region, region_size, MEM_PROT_READ | MEM_PROT_WRITE);
|
|
|
|
}
|
2017-05-26 13:39:27 +00:00
|
|
|
// If not WX Exclusive, no need to call ProtectMemoryPages because we never change the protection from RWX.
|
|
|
|
PoisonMemory(offset);
|
|
|
|
ResetCodePtr(offset);
|
2022-11-20 17:35:05 +00:00
|
|
|
if (PlatformIsWXExclusive() && offset != 0) {
|
2017-12-28 03:33:02 +00:00
|
|
|
// Need to re-protect the part we didn't clear.
|
|
|
|
ProtectMemoryPages(region, offset, MEM_PROT_READ | MEM_PROT_EXEC);
|
|
|
|
}
|
2016-08-28 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 08:49:26 +00:00
|
|
|
// BeginWrite/EndWrite assume that we keep appending.
|
|
|
|
// If you don't specify a size and we later encounter an executable non-writable block, we're screwed.
|
2016-08-28 16:07:54 +00:00
|
|
|
// These CANNOT be nested. We rely on the memory protection starting at READ|WRITE after start and reset.
|
2016-08-28 12:52:08 +00:00
|
|
|
void BeginWrite(size_t sizeEstimate = 1) {
|
2020-07-19 15:52:41 +00:00
|
|
|
_dbg_assert_msg_(!writeStart_, "Can't nest BeginWrite calls");
|
|
|
|
|
2016-08-28 12:52:08 +00:00
|
|
|
// In case the last block made the current page exec/no-write, let's fix that.
|
|
|
|
if (PlatformIsWXExclusive()) {
|
|
|
|
writeStart_ = GetCodePtr();
|
2022-11-20 17:00:04 +00:00
|
|
|
if (writeStart_ + sizeEstimate - region > (ptrdiff_t)region_size)
|
|
|
|
sizeEstimate = region_size - (writeStart_ - region);
|
|
|
|
writeEstimated_ = sizeEstimate;
|
2016-08-28 12:52:08 +00:00
|
|
|
ProtectMemoryPages(writeStart_, sizeEstimate, MEM_PROT_READ | MEM_PROT_WRITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:00:04 +00:00
|
|
|
// In case you now know your original estimate is wrong.
|
|
|
|
void ContinueWrite(size_t sizeEstimate = 1) {
|
|
|
|
_dbg_assert_msg_(writeStart_, "Must have already called BeginWrite()");
|
|
|
|
if (PlatformIsWXExclusive()) {
|
|
|
|
const uint8_t *pos = GetCodePtr();
|
|
|
|
if (pos + sizeEstimate - region > (ptrdiff_t)region_size)
|
|
|
|
sizeEstimate = region_size - (pos - region);
|
2024-10-10 08:24:40 +00:00
|
|
|
writeEstimated_ = (pos - writeStart_) + sizeEstimate;
|
2022-11-20 17:00:04 +00:00
|
|
|
ProtectMemoryPages(pos, sizeEstimate, MEM_PROT_READ | MEM_PROT_WRITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-28 12:52:08 +00:00
|
|
|
void EndWrite() {
|
|
|
|
// OK, we're done. Re-protect the memory we touched.
|
2017-01-26 09:01:14 +00:00
|
|
|
if (PlatformIsWXExclusive() && writeStart_ != nullptr) {
|
2016-08-28 12:52:08 +00:00
|
|
|
const uint8_t *end = GetCodePtr();
|
2022-11-20 17:00:04 +00:00
|
|
|
size_t sz = end - writeStart_;
|
|
|
|
if (sz > writeEstimated_)
|
2024-07-14 12:42:59 +00:00
|
|
|
WARN_LOG(Log::JIT, "EndWrite(): Estimated %d bytes, wrote %d", (int)writeEstimated_, (int)sz);
|
2022-11-20 17:00:04 +00:00
|
|
|
// If we protected and wrote less, we may need to unprotect.
|
|
|
|
// Especially if we're linking blocks or similar.
|
|
|
|
if (sz < writeEstimated_)
|
|
|
|
sz = writeEstimated_;
|
|
|
|
ProtectMemoryPages(writeStart_, sz, MEM_PROT_READ | MEM_PROT_EXEC);
|
2016-08-28 12:52:08 +00:00
|
|
|
writeStart_ = nullptr;
|
|
|
|
}
|
2014-12-15 21:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call this when shutting down. Don't rely on the destructor, even though it'll do the job.
|
2016-08-28 10:09:01 +00:00
|
|
|
void FreeCodeSpace() {
|
2023-06-27 12:21:52 +00:00
|
|
|
#if !PPSSPP_PLATFORM(SWITCH)
|
2017-01-25 19:34:57 +00:00
|
|
|
ProtectMemoryPages(region, region_size, MEM_PROT_READ | MEM_PROT_WRITE);
|
2023-04-24 09:42:27 +00:00
|
|
|
FreeExecutableMemory(region, region_size);
|
2023-06-27 12:21:52 +00:00
|
|
|
#else // !PPSSPP_PLATFORM(SWITCH)
|
|
|
|
jitClose(&jitController);
|
|
|
|
printf("[NXJIT]: Jit closed\n");
|
|
|
|
#endif // PPSSPP_PLATFORM(SWITCH)
|
2014-12-15 21:04:08 +00:00
|
|
|
region = nullptr;
|
2020-05-17 05:12:35 +00:00
|
|
|
writableRegion = nullptr;
|
2014-12-15 21:04:08 +00:00
|
|
|
region_size = 0;
|
|
|
|
}
|
|
|
|
|
2016-05-01 09:13:51 +00:00
|
|
|
const u8 *GetCodePtr() const override {
|
|
|
|
return T::GetCodePointer();
|
2014-12-15 21:04:08 +00:00
|
|
|
}
|
2014-12-15 21:32:55 +00:00
|
|
|
|
2020-05-17 05:12:35 +00:00
|
|
|
void ResetCodePtr(size_t offset) {
|
|
|
|
T::SetCodePointer(region + offset, writableRegion + offset);
|
2014-12-15 21:32:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 09:13:51 +00:00
|
|
|
size_t GetSpaceLeft() const {
|
|
|
|
return region_size - (T::GetCodePointer() - region);
|
2014-12-15 21:32:55 +00:00
|
|
|
}
|
2016-08-28 12:52:08 +00:00
|
|
|
|
2020-05-17 05:12:35 +00:00
|
|
|
const u8 *GetCodePtrFromWritablePtr(u8 *ptr) override {
|
|
|
|
// So we can adjust region to writable space. Might be zero.
|
|
|
|
ptrdiff_t writable = T::GetWritableCodePtr() - T::GetCodePointer();
|
|
|
|
return ptr - writable;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 *GetWritablePtrFromCodePtr(const u8 *ptr) override {
|
|
|
|
// So we can adjust region to writable space. Might be zero.
|
|
|
|
ptrdiff_t writable = T::GetWritableCodePtr() - T::GetCodePointer();
|
|
|
|
return (u8 *)ptr + writable;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void SetCodePtr(u8 *ptr) override {
|
|
|
|
T::SetCodePointer(ptr, GetWritablePtrFromCodePtr(ptr));
|
|
|
|
}
|
|
|
|
|
2016-08-28 12:52:08 +00:00
|
|
|
private:
|
2020-05-17 04:12:31 +00:00
|
|
|
// Note: this is a readable pointer.
|
|
|
|
const uint8_t *writeStart_ = nullptr;
|
2020-05-17 05:12:35 +00:00
|
|
|
uint8_t *writableRegion = nullptr;
|
2022-11-20 17:00:04 +00:00
|
|
|
size_t writeEstimated_ = 0;
|
2023-06-27 12:21:52 +00:00
|
|
|
#if PPSSPP_PLATFORM(SWITCH)
|
|
|
|
Jit jitController;
|
|
|
|
#endif // PPSSPP_PLATFORM(SWITCH)
|
2014-12-15 21:04:08 +00:00
|
|
|
};
|