2015-05-24 04:32:32 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
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
|
2016-06-24 08:43:46 +00:00
|
|
|
#include <gtest/gtest.h> // NOLINT
|
2014-10-20 00:31:15 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
PAGE_GRAN = 0x10000
|
2014-10-20 00:31:15 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
PAGE_GRAN = 0x1000
|
2014-10-20 00:31:15 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
class PageFaultFakeJit : public JitBase
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
// 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; }
|
2016-06-24 08:43:46 +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
|
|
|
|
{
|
|
|
|
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);
|
2016-06-24 08:43:46 +00:00
|
|
|
m_post_unprotect_time = std::chrono::high_resolution_clock::now();
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-20 00:31:15 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void* m_data;
|
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> m_pre_unprotect_time,
|
|
|
|
m_post_unprotect_time;
|
2014-10-20 00:31:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(PageFault, PageFault)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
EMM::InstallExceptionHandler();
|
2016-08-07 17:03:07 +00:00
|
|
|
void* data = Common::AllocateMemoryPages(PAGE_GRAN);
|
2016-06-24 08:43:46 +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
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
PageFaultFakeJit pfjit;
|
2018-12-02 13:16:17 +00:00
|
|
|
JitInterface::SetJit(&pfjit);
|
2016-06-24 08:43:46 +00:00
|
|
|
pfjit.m_data = data;
|
2014-10-20 00:31:15 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
*(volatile int*)data = 5;
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
2014-10-20 00:31:15 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
#define AS_NS(diff) \
|
|
|
|
((unsigned long long)std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count())
|
2014-10-20 00:31:15 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
EMM::UninstallExceptionHandler();
|
2018-12-02 13:16:17 +00:00
|
|
|
JitInterface::SetJit(nullptr);
|
2014-10-20 00:31:15 +00:00
|
|
|
|
2016-06-24 08:43:46 +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));
|
2014-10-20 00:31:15 +00:00
|
|
|
}
|