[Memory] Move new watches to heap-aware Memory from MMIOHandler
This commit is contained in:
@@ -30,9 +30,6 @@ typedef void (*MMIOWriteCallback)(void* ppc_context, void* callback_context,
|
||||
uint32_t addr, uint32_t value);
|
||||
typedef void (*AccessWatchCallback)(void* context_ptr, void* data_ptr,
|
||||
uint32_t address);
|
||||
typedef void (*PhysicalWriteWatchCallback)(void* context_ptr,
|
||||
uint32_t page_first,
|
||||
uint32_t page_last);
|
||||
|
||||
struct MMIORange {
|
||||
uint32_t address;
|
||||
@@ -48,15 +45,22 @@ class MMIOHandler {
|
||||
public:
|
||||
virtual ~MMIOHandler();
|
||||
|
||||
typedef bool (*AccessViolationCallback)(void* context, size_t host_address,
|
||||
bool is_write);
|
||||
|
||||
enum WatchType {
|
||||
kWatchInvalid = 0,
|
||||
kWatchWrite = 1,
|
||||
kWatchReadWrite = 2,
|
||||
};
|
||||
|
||||
static std::unique_ptr<MMIOHandler> Install(uint8_t* virtual_membase,
|
||||
uint8_t* physical_membase,
|
||||
uint8_t* membase_end);
|
||||
// access_violation_callback is called in global_critical_region, so if
|
||||
// multiple threads trigger an access violation in the same page, the callback
|
||||
// will be called only once.
|
||||
static std::unique_ptr<MMIOHandler> Install(
|
||||
uint8_t* virtual_membase, uint8_t* physical_membase, uint8_t* membase_end,
|
||||
AccessViolationCallback access_violation_callback,
|
||||
void* access_violation_callback_context);
|
||||
static MMIOHandler* global_handler() { return global_handler_; }
|
||||
|
||||
bool RegisterRange(uint32_t virtual_address, uint32_t mask, uint32_t size,
|
||||
@@ -79,59 +83,10 @@ class MMIOHandler {
|
||||
void* callback_context, void* callback_data);
|
||||
void CancelAccessWatch(uintptr_t watch_handle);
|
||||
|
||||
// Physical memory write watching, allowing subsystems to invalidate cached
|
||||
// data that depends on memory contents.
|
||||
//
|
||||
// Placing a watch simply marks the pages (of the system page size) as
|
||||
// watched, individual watched ranges (or which specific subscribers are
|
||||
// watching specific pages) are not stored. Because of this, callbacks may be
|
||||
// triggered multiple times for a single range, and for any watched page every
|
||||
// registered callbacks is triggered. This is a very simple one-shot method
|
||||
// for use primarily for cache invalidation - there may be spurious firing,
|
||||
// for example, if the game only changes the protection level without writing
|
||||
// anything.
|
||||
//
|
||||
// A range of pages can be watched at any time, but pages are only unwatched
|
||||
// when watches are triggered (since multiple subscribers can depend on the
|
||||
// same memory, and one subscriber shouldn't interfere with another).
|
||||
//
|
||||
// Callbacks can be triggered for one page (if the guest just stores words) or
|
||||
// for multiple pages (for file reading, protection level changes).
|
||||
//
|
||||
// Only guest physical memory mappings are watched - the host-only mapping is
|
||||
// not protected so it can be used to bypass the write protection (for file
|
||||
// reads, for example - in this case, watches are triggered manually).
|
||||
//
|
||||
// Ranges passed to ProtectAndWatchPhysicalMemory must not contain read-only
|
||||
// or inaccessible pages - this must be checked externally! Otherwise the MMIO
|
||||
// handler will make them read-only, but when a read is attempted, it will
|
||||
// make them read-write!
|
||||
//
|
||||
// IMPORTANT NOTE: When a watch is triggered, the watched page is unprotected
|
||||
// ***ONLY IN THE HEAP WHERE THE ADDRESS IS LOCATED***! Since different
|
||||
// virtual memory mappings of physical memory can have different protection
|
||||
// levels for the same pages, and watches must not be placed on read-only or
|
||||
// totally inaccessible pages, there are significant difficulties with
|
||||
// synchronizing all the three ranges.
|
||||
//
|
||||
// TODO(Triang3l): Allow the callbacks to unwatch regions larger than one page
|
||||
// (for instance, 64 KB) so there are less access violations. All callbacks
|
||||
// must agree to unwatch larger ranges because in some cases (like regions
|
||||
// near the locations that render targets have been resolved to) it is
|
||||
// necessary to invalidate only a single page and none more.
|
||||
void* RegisterPhysicalWriteWatch(PhysicalWriteWatchCallback callback,
|
||||
void* callback_context);
|
||||
void UnregisterPhysicalWriteWatch(void* watch_handle);
|
||||
// Force-protects the range in ***ONE SPECIFIC HEAP***, either 0xA0000000,
|
||||
// 0xC0000000 or 0xE0000000, depending on the higher bits of the address.
|
||||
void ProtectAndWatchPhysicalMemory(uint32_t physical_address_and_heap,
|
||||
uint32_t length);
|
||||
|
||||
// Fires and clears any write watches that overlap this range in one heap.
|
||||
// Unprotecting can be inhibited if this is called right before applying
|
||||
// different protection to the same range.
|
||||
void InvalidateRange(uint32_t physical_address_and_heap, uint32_t length,
|
||||
bool unprotect = true);
|
||||
void InvalidateRange(uint32_t physical_address_and_heap, uint32_t length);
|
||||
|
||||
// Returns true if /all/ of this range is watched.
|
||||
// TODO(Triang3l): Remove when legacy watches are removed.
|
||||
@@ -147,13 +102,10 @@ class MMIOHandler {
|
||||
void* callback_data;
|
||||
};
|
||||
|
||||
struct PhysicalWriteWatchEntry {
|
||||
PhysicalWriteWatchCallback callback;
|
||||
void* callback_context;
|
||||
};
|
||||
|
||||
MMIOHandler(uint8_t* virtual_membase, uint8_t* physical_membase,
|
||||
uint8_t* membase_end);
|
||||
uint8_t* membase_end,
|
||||
AccessViolationCallback access_violation_callback,
|
||||
void* access_violation_callback_context);
|
||||
|
||||
static bool ExceptionCallbackThunk(Exception* ex, void* data);
|
||||
bool ExceptionCallback(Exception* ex);
|
||||
@@ -162,24 +114,18 @@ class MMIOHandler {
|
||||
void ClearAccessWatch(AccessWatchEntry* entry);
|
||||
bool CheckAccessWatch(uint32_t guest_address, uint32_t guest_heap_address);
|
||||
|
||||
uint32_t system_page_size_log2_;
|
||||
|
||||
uint8_t* virtual_membase_;
|
||||
uint8_t* physical_membase_;
|
||||
uint8_t* memory_end_;
|
||||
|
||||
std::vector<MMIORange> mapped_ranges_;
|
||||
|
||||
AccessViolationCallback access_violation_callback_;
|
||||
void* access_violation_callback_context_;
|
||||
|
||||
xe::global_critical_region global_critical_region_;
|
||||
// TODO(benvanik): data structure magic.
|
||||
std::list<AccessWatchEntry*> access_watches_;
|
||||
std::vector<PhysicalWriteWatchEntry*> physical_write_watches_;
|
||||
// For each page, there are 4 bits (16 pages in each word):
|
||||
// 0 - whether the page is protected in A0000000.
|
||||
// 1 - whether the page is protected in C0000000.
|
||||
// 2 - whether the page is protected in E0000000.
|
||||
// 3 - unused, always zero.
|
||||
std::vector<uint64_t> physical_write_watched_pages_;
|
||||
|
||||
static MMIOHandler* global_handler_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user