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>
|
2022-06-14 00:20:13 +00:00
|
|
|
#include <fmt/format.h>
|
2014-10-20 00:31:15 +00:00
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
2022-05-19 19:17:46 +00:00
|
|
|
if (!EMM::IsExceptionHandlerSupported())
|
|
|
|
{
|
|
|
|
// TODO: Use GTEST_SKIP() instead when GTest is updated to 1.10+
|
|
|
|
return;
|
|
|
|
}
|
2014-10-20 00:31:15 +00:00
|
|
|
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();
|
|
|
|
|
2022-06-14 00:20:13 +00:00
|
|
|
auto difference_in_nanoseconds = [](auto start, auto end) {
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
|
|
|
|
};
|
2014-10-20 00:31:15 +00:00
|
|
|
|
|
|
|
EMM::UninstallExceptionHandler();
|
2018-12-02 13:16:17 +00:00
|
|
|
JitInterface::SetJit(nullptr);
|
2014-10-20 00:31:15 +00:00
|
|
|
|
2022-06-14 00:20:13 +00:00
|
|
|
fmt::print("page fault timing:\n");
|
|
|
|
fmt::print("start->HandleFault {} ns\n",
|
|
|
|
difference_in_nanoseconds(start, pfjit.m_pre_unprotect_time));
|
|
|
|
fmt::print("UnWriteProtectMemory {} ns\n",
|
|
|
|
difference_in_nanoseconds(pfjit.m_pre_unprotect_time, pfjit.m_post_unprotect_time));
|
|
|
|
fmt::print("HandleFault->end {} ns\n",
|
|
|
|
difference_in_nanoseconds(pfjit.m_post_unprotect_time, end));
|
|
|
|
fmt::print("total {} ns\n", difference_in_nanoseconds(start, end));
|
2014-10-20 00:31:15 +00:00
|
|
|
}
|