Skip to content

Commit

Permalink
Simplify the logic, fix asynchronous tsc when reboot into macOs
Browse files Browse the repository at this point in the history
  • Loading branch information
Seey6 committed Oct 25, 2023
1 parent 2b585e6 commit dd020c3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 36 deletions.
34 changes: 3 additions & 31 deletions CpuTscSync/CpuTscSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
#include "CpuTscSync.hpp"

static CpuTscSyncPlugin *callbackCpuf = nullptr;
_Atomic(bool) CpuTscSyncPlugin::tsc_synced = true;
_Atomic(bool) CpuTscSyncPlugin::kernel_is_awake = false;
_Atomic(bool) CpuTscSyncPlugin::tsc_synced = false;


//stamp the tsc
Expand Down Expand Up @@ -47,28 +46,12 @@ void CpuTscSyncPlugin::init()
}


IOReturn CpuTscSyncPlugin::IOHibernateSystemHasSlept()
{
DBGLOG("cputs", "IOHibernateSystemHasSlept is called");
tsc_synced = false;
kernel_is_awake = false;
return FunctionCast(IOHibernateSystemHasSlept, callbackCpuf->orgIOHibernateSystemHasSlept)();
}

IOReturn CpuTscSyncPlugin::IOHibernateSystemWake()
{
IOReturn result = FunctionCast(IOHibernateSystemWake, callbackCpuf->orgIOHibernateSystemWake)();
kernel_is_awake = true;
return result;
}

void CpuTscSyncPlugin::IOPMrootDomain_tracePoint( void *that, uint8_t point )
{

FunctionCast(IOPMrootDomain_tracePoint, callbackCpuf->orgIOPMrootDomain_tracePoint)(that, point);

if(point == kIOPMTracePointWakeCPUs){
kernel_is_awake = true;
tsc_synced = false;
}
}
Expand All @@ -77,7 +60,7 @@ void CpuTscSyncPlugin::clock_get_calendar_microtime(clock_sec_t *secs, clock_use
{
FunctionCast(clock_get_calendar_microtime, callbackCpuf->org_clock_get_calendar_microtime)(secs, microsecs);

if (!tsc_synced && kernel_is_awake) {
if (!tsc_synced) {
DBGLOG("cputs", "clock_get_calendar_microtime is called after wake");
tsc_adjust_or_reset();
}
Expand All @@ -87,23 +70,12 @@ void CpuTscSyncPlugin::processKernel(KernelPatcher &patcher)
{
if (!kernel_routed)
{
KernelPatcher::RouteRequest requests_for_long_jump[] {
{"_IOHibernateSystemHasSlept", IOHibernateSystemHasSlept, orgIOHibernateSystemHasSlept},
{"_IOHibernateSystemWake", IOHibernateSystemWake, orgIOHibernateSystemWake}
};

size_t size = arrsize(requests_for_long_jump);
if (!patcher.routeMultipleLong(KernelPatcher::KernelID, requests_for_long_jump, size))
SYSLOG("cputs", "patcher.routeMultiple for %s is failed with error %d", requests_for_long_jump[0].symbol, patcher.getError());

patcher.clearError();

KernelPatcher::RouteRequest requests[] {
{"__ZN14IOPMrootDomain10tracePointEh", IOPMrootDomain_tracePoint, orgIOPMrootDomain_tracePoint},
{"_clock_get_calendar_microtime", clock_get_calendar_microtime, org_clock_get_calendar_microtime }
};

size = arrsize(requests);
unsigned int size = arrsize(requests);
if (!patcher.routeMultiple(KernelPatcher::KernelID, requests, size))
SYSLOG("cputs", "patcher.routeMultiple for %s is failed with error %d", requests[0].symbol, patcher.getError());

Expand Down
5 changes: 0 additions & 5 deletions CpuTscSync/CpuTscSync.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,17 @@ class CpuTscSyncPlugin {

private:
_Atomic(bool) kernel_routed = false;
static _Atomic(bool) kernel_is_awake;

private:
/**
* Trampolines for original resource load callback
*/
mach_vm_address_t orgIOHibernateSystemHasSlept {0};
mach_vm_address_t orgIOHibernateSystemWake {0};
mach_vm_address_t orgIOPMrootDomain_tracePoint {0};
mach_vm_address_t org_clock_get_calendar_microtime {0};

/**
* Hooked functions
*/
static IOReturn IOHibernateSystemHasSlept();
static IOReturn IOHibernateSystemWake();
static void IOPMrootDomain_tracePoint( void *that, uint8_t point );
static void clock_get_calendar_microtime(clock_sec_t *secs, clock_usec_t *microsecs);

Expand Down

0 comments on commit dd020c3

Please sign in to comment.