27 #include "precompiled.h"
57 ENSURE(bytesReturned ==
sizeof(out));
65 return (
u8)(value & 0xFF);
72 return (
u16)(value & 0xFFFF);
85 in.
value = (DWORD32)value;
132 LPOVERLAPPED ovl = 0;
136 ENSURE(bytesReturned ==
sizeof(out));
137 volatile void* virtualAddress = (
volatile void*)(uintptr_t)out.
virtualAddress;
138 return virtualAddress;
150 LPOVERLAPPED ovl = 0;
163 LPOVERLAPPED ovl = 0;
164 const BOOL ok = DeviceIoControl(
hAken, ioctl, &in,
sizeof(in), &out,
sizeof(out), &bytesReturned, ovl);
167 ENSURE(bytesReturned ==
sizeof(out));
188 LPOVERLAPPED ovl = 0;
201 LPCWSTR machineName = 0;
202 LPCWSTR databaseName = 0;
203 SC_HANDLE hSCM = OpenSCManagerW(machineName, databaseName, access);
207 ENSURE(GetLastError() == ERROR_ACCESS_DENIED);
225 SC_HANDLE hService = OpenServiceW(hSCM,
AKEN_NAME, SERVICE_STOP|SERVICE_INTERROGATE);
230 SERVICE_STATUS serviceStatus;
231 if(!ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus))
235 const DWORD err = GetLastError();
236 ENSURE(err == ERROR_SERVICE_NOT_ACTIVE || err == ERROR_SERVICE_CANNOT_ACCEPT_CTRL);
241 ok = DeleteService(hService);
243 ok = CloseServiceHandle(hService);
246 ok = CloseServiceHandle(hSCM);
251 #if CONFIG2_MAHAF_ATTEMPT_DRIVER_START
253 static void StartDriver(
const OsPath& driverPathname)
258 ENSURE(GetLastError() == ERROR_ACCESS_DENIED);
263 SC_HANDLE hService = OpenServiceW(hSCM,
AKEN_NAME, SERVICE_START);
272 BOOL ok = CloseServiceHandle(hService);
283 LPCWSTR startName = 0;
286 SERVICE_START, SERVICE_KERNEL_DRIVER, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
287 OsString(driverPathname).c_str(), 0, 0, 0, startName, 0);
293 BOOL ok = StartService(hService, numArgs, 0);
296 switch(GetLastError())
298 case ERROR_SERVICE_ALREADY_RUNNING:
301 case ERROR_ACCESS_DENIED:
304 case ERROR_INVALID_IMAGE_HASH:
316 CloseServiceHandle(hService);
317 CloseServiceHandle(hSCM);
321 static bool Is64BitOs()
330 static OsPath DriverPathname()
332 const char*
const bits = Is64BitOs()?
"64" :
"";
334 const char*
const debug =
"";
336 const char*
const debug =
"d";
343 #endif // CONFIG2_MAHAF_ATTEMPT_DRIVER_START
355 #if CONFIG2_MAHAF_ATTEMPT_DRIVER_START
357 const OsPath driverPathname = DriverPathname();
358 StartDriver(driverPathname);
363 const DWORD shareMode = 0;
364 hAken = CreateFileW(L
"\\\\.\\Aken", GENERIC_READ, shareMode, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
365 if(
hAken == INVALID_HANDLE_VALUE)
382 hAken = INVALID_HANDLE_VALUE;
u16 mahaf_ReadPort16(u16 port)
static u32 ReadPort(u16 port, u8 numBytes)
u32 mahaf_ReadPort32(u16 port)
some WinAPI functions SetLastError(0) on success, which is bad because it can hide previous errors...
static void out(const wchar_t *fmt,...)
#define IOCTL_AKEN_READ_MSR
const Status INVALID_HANDLE
int sprintf_s(char *buf, size_t max_chars, const char *fmt,...) PRINTF_ARGS(3)
static u64 ReadRegister(DWORD ioctl, u64 reg)
const Status NOT_SUPPORTED
void mahaf_WritePort8(u16 port, u8 value)
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
intptr_t ModuleInitState
initialization state of a module (class, source file, etc.) must be initialized to zero (e...
void mahaf_WritePort32(u16 port, u32 value)
void mahaf_UnmapPhysicalMemory(volatile void *virtualAddress)
void mahaf_WriteModelSpecificRegister(u64 reg, u64 value)
u64 mahaf_ReadModelSpecificRegister(u64 reg)
i64 Status
Error handling system.
T bits(T num, size_t lo_idx, size_t hi_idx)
extract the value of bits hi_idx:lo_idx within num
volatile void * mahaf_MapPhysicalMemory(uintptr_t physicalAddress, size_t numBytes)
void mahaf_WritePort16(u16 port, u16 value)
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
#define IOCTL_AKEN_READ_PMC
static ModuleInitState initState
Status ModuleShutdown(volatile ModuleInitState *initState, void(*shutdown)())
calls a user-defined shutdown function if initState is "initialized".
bool mahaf_IsPhysicalMappingDangerous()
const OsPath & wutil_ExecutablePath()
#define WARN_IF_FALSE(expression)
static void WritePort(u16 port, u32 value, u8 numBytes)
#define IOCTL_AKEN_READ_PORT
u8 mahaf_ReadPort8(u16 port)
bool wutil_HasCommandLineArgument(const wchar_t *arg)
static void UninstallDriver()
Status ModuleInit(volatile ModuleInitState *initState, Status(*init)())
calls a user-defined init function if initState is zero.
#define IOCTL_AKEN_WRITE_PORT
static std::string OsString(const OsPath &path)
static SC_HANDLE OpenServiceControlManager(DWORD access)
#define WARN_RETURN_0_IF_FALSE(expression)
u64 mahaf_ReadPerformanceMonitoringCounter(u64 reg)
#define IOCTL_AKEN_WRITE_MSR