2015-05-24 04:32:32 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-05-24 04:32:32 +00:00
|
|
|
|
2014-10-20 00:31:15 +00:00
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Timer.h"
|
|
|
|
#include "Core/MemTools.h"
|
|
|
|
#include "Core/PowerPC/JitCommon/JitBase.h"
|
2018-12-02 13:16:17 +00:00
|
|
|
#include "Core/PowerPC/JitInterface.h"
|
2014-10-20 00:31:15 +00:00
|
|
|
|
|
|
|
// include order is important
|
2015-03-01 13:52:09 +00:00
|
|
|
#include <gtest/gtest.h> // NOLINT
|
2014-10-20 00:31:15 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
PAGE_GRAN = 0x10000
|
|
|
|
#else
|
|
|
|
PAGE_GRAN = 0x1000
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
class PageFaultFakeJit : public JitBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// CPUCoreBase methods
|
|
|
|
void Init() override {}
|
|
|
|
void Shutdown() override {}
|
|
|
|
void ClearCache() override {}
|
|
|
|
void Run() override {}
|
|
|
|
void SingleStep() override {}
|
2018-03-24 19:59:20 +00:00
|
|
|
const char* GetName() const override { return nullptr; }
|
2014-10-20 00:31:15 +00:00
|
|
|
// JitBase methods
|
|
|
|
JitBaseBlockCache* GetBlockCache() override { return nullptr; }
|
|
|
|
void Jit(u32 em_address) override {}
|
|
|
|
const CommonAsmRoutinesBase* GetAsmRoutines() override { return nullptr; }
|
|
|
|
virtual bool HandleFault(uintptr_t access_address, SContext* ctx) override
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2014-10-20 00:31:15 +00:00
|
|
|
m_pre_unprotect_time = std::chrono::high_resolution_clock::now();
|
2016-08-07 17:03:07 +00:00
|
|
|
Common::UnWriteProtectMemory(m_data, PAGE_GRAN, /*allowExecute*/ false);
|
2014-10-20 00:31:15 +00:00
|
|
|
m_post_unprotect_time = std::chrono::high_resolution_clock::now();
|
|
|
|
return true;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2014-10-20 00:31:15 +00:00
|
|
|
|
2021-09-04 04:43:19 +00:00
|
|
|
void* m_data = nullptr;
|
2014-10-20 00:31:15 +00:00
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> m_pre_unprotect_time,
|
|
|
|
m_post_unprotect_time;
|
|
|
|
};
|
|
|
|
|
2020-08-19 04:54:08 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define ASAN_DISABLE __declspec(no_sanitize_address)
|
|
|
|
#else
|
|
|
|
#define ASAN_DISABLE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void ASAN_DISABLE perform_invalid_access(void* data)
|
|
|
|
{
|
|
|
|
*(volatile int*)data = 5;
|
|
|
|
}
|
|
|
|
|
2014-10-20 00:31:15 +00:00
|
|
|
TEST(PageFault, PageFault)
|
|
|
|
{
|
|
|
|
EMM::InstallExceptionHandler();
|
2016-08-07 17:03:07 +00:00
|
|
|
void* data = Common::AllocateMemoryPages(PAGE_GRAN);
|
2014-10-20 00:31:15 +00:00
|
|
|
EXPECT_NE(data, nullptr);
|
2016-08-07 17:03:07 +00:00
|
|
|
Common::WriteProtectMemory(data, PAGE_GRAN, false);
|
2014-10-20 00:31:15 +00:00
|
|
|
|
|
|
|
PageFaultFakeJit pfjit;
|
2018-12-02 13:16:17 +00:00
|
|
|
JitInterface::SetJit(&pfjit);
|
2014-10-20 00:31:15 +00:00
|
|
|
pfjit.m_data = data;
|
|
|
|
|
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2020-08-19 04:54:08 +00:00
|
|
|
perform_invalid_access(data);
|
2014-10-20 00:31:15 +00:00
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
#define AS_NS(diff) \
|
|
|
|
((unsigned long long)std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count())
|
|
|
|
|
|
|
|
EMM::UninstallExceptionHandler();
|
2018-12-02 13:16:17 +00:00
|
|
|
JitInterface::SetJit(nullptr);
|
2014-10-20 00:31:15 +00:00
|
|
|
|
|
|
|
printf("page fault timing:\n");
|
|
|
|
printf("start->HandleFault %llu ns\n", AS_NS(pfjit.m_pre_unprotect_time - start));
|
|
|
|
printf("UnWriteProtectMemory %llu ns\n",
|
|
|
|
AS_NS(pfjit.m_post_unprotect_time - pfjit.m_pre_unprotect_time));
|
|
|
|
printf("HandleFault->end %llu ns\n", AS_NS(end - pfjit.m_post_unprotect_time));
|
|
|
|
printf("total %llu ns\n", AS_NS(end - start));
|
|
|
|
}
|