File: mahaf.cpp

package info (click to toggle)
0ad 0.0.23.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,412 kB
  • sloc: cpp: 245,162; ansic: 200,249; javascript: 19,244; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (398 lines) | stat: -rw-r--r-- 10,448 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* Copyright (C) 2010 Wildfire Games.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * user-mode interface to Aken driver
 */

#include "precompiled.h"
#include "lib/sysdep/os/win/mahaf.h"

#include "lib/config2.h"
#include "lib/module_init.h"

#include "lib/sysdep/os/win/wutil.h"
#include <winioctl.h>
#include "lib/sysdep/os/win/aken/aken.h"
#include "lib/sysdep/os/win/wversion.h"

static HANDLE hAken = INVALID_HANDLE_VALUE;	// handle to Aken driver


//-----------------------------------------------------------------------------
// ioctl wrappers
//-----------------------------------------------------------------------------

static u32 ReadPort(u16 port, u8 numBytes)
{
	AkenReadPortIn in;
	in.port = (USHORT)port;
	in.numBytes = (UCHAR)numBytes;
	AkenReadPortOut out;

	DWORD bytesReturned;
	LPOVERLAPPED ovl = 0;	// synchronous
	const BOOL ok = DeviceIoControl(hAken, (DWORD)IOCTL_AKEN_READ_PORT, &in, sizeof(in), &out, sizeof(out), &bytesReturned, ovl);
	WARN_RETURN_0_IF_FALSE(ok);

	ENSURE(bytesReturned == sizeof(out));
	return out.value;
}

u8 mahaf_ReadPort8(u16 port)
{
	const u32 value = ReadPort(port, 1);
	ENSURE(value <= 0xFF);
	return (u8)(value & 0xFF);
}

u16 mahaf_ReadPort16(u16 port)
{
	const u32 value = ReadPort(port, 2);
	ENSURE(value <= 0xFFFF);
	return (u16)(value & 0xFFFF);
}

u32 mahaf_ReadPort32(u16 port)
{
	const u32 value = ReadPort(port, 4);
	return value;
}


static void WritePort(u16 port, u32 value, u8 numBytes)
{
	AkenWritePortIn in;
	in.value = (DWORD32)value;
	in.port  = (USHORT)port;
	in.numBytes = (UCHAR)numBytes;

	DWORD bytesReturned;	// unused but must be passed to DeviceIoControl
	LPOVERLAPPED ovl = 0;	// synchronous
	BOOL ok = DeviceIoControl(hAken, (DWORD)IOCTL_AKEN_WRITE_PORT, &in, sizeof(in), 0, 0u, &bytesReturned, ovl);
	WARN_IF_FALSE(ok);
}

void mahaf_WritePort8(u16 port, u8 value)
{
	WritePort(port, (u32)value, 1);
}

void mahaf_WritePort16(u16 port, u16 value)
{
	WritePort(port, (u32)value, 2);
}

void mahaf_WritePort32(u16 port, u32 value)
{
	WritePort(port, value, 4);
}


bool mahaf_IsPhysicalMappingDangerous()
{
	// pre-XP versions don't prevent re-mapping pages with incompatible
	// attributes, which may lead to disaster due to TLB corruption.
	if(wversion_Number() < WVERSION_XP)
		return true;

	return false;
}


volatile void* mahaf_MapPhysicalMemory(uintptr_t physicalAddress, size_t numBytes)
{
	ENSURE(!mahaf_IsPhysicalMappingDangerous());

	AkenMapIn in;
	in.physicalAddress = (DWORD64)physicalAddress;
	in.numBytes        = (DWORD64)numBytes;
	AkenMapOut out;

	DWORD bytesReturned;
	LPOVERLAPPED ovl = 0;	// synchronous
	const BOOL ok = DeviceIoControl(hAken, (DWORD)IOCTL_AKEN_MAP, &in, sizeof(in), &out, sizeof(out), &bytesReturned, ovl);
	WARN_RETURN_0_IF_FALSE(ok);

	ENSURE(bytesReturned == sizeof(out));
	volatile void* virtualAddress = (volatile void*)(uintptr_t)out.virtualAddress;
	return virtualAddress;
}


void mahaf_UnmapPhysicalMemory(volatile void* virtualAddress)
{
	ENSURE(!mahaf_IsPhysicalMappingDangerous());

	AkenUnmapIn in;
	in.virtualAddress = (DWORD64)virtualAddress;

	DWORD bytesReturned;	// unused but must be passed to DeviceIoControl
	LPOVERLAPPED ovl = 0;	// synchronous
	BOOL ok = DeviceIoControl(hAken, (DWORD)IOCTL_AKEN_UNMAP, &in, sizeof(in), 0, 0u, &bytesReturned, ovl);
	WARN_IF_FALSE(ok);
}


static u64 ReadRegister(DWORD ioctl, u64 reg)
{
	AkenReadRegisterIn in;
	in.reg = reg;
	AkenReadRegisterOut out;

	DWORD bytesReturned;
	LPOVERLAPPED ovl = 0;	// synchronous
	const BOOL ok = DeviceIoControl(hAken, ioctl, &in, sizeof(in), &out, sizeof(out), &bytesReturned, ovl);
	WARN_RETURN_0_IF_FALSE(ok);

	ENSURE(bytesReturned == sizeof(out));
	return out.value;
}

u64 mahaf_ReadModelSpecificRegister(u64 reg)
{
	return ReadRegister((DWORD)IOCTL_AKEN_READ_MSR, reg);
}

u64 mahaf_ReadPerformanceMonitoringCounter(u64 reg)
{
	return ReadRegister((DWORD)IOCTL_AKEN_READ_PMC, reg);
}

void mahaf_WriteModelSpecificRegister(u64 reg, u64 value)
{
	AkenWriteRegisterIn in;
	in.reg = reg;
	in.value = value;

	DWORD bytesReturned;	// unused but must be passed to DeviceIoControl
	LPOVERLAPPED ovl = 0;	// synchronous
	BOOL ok = DeviceIoControl(hAken, (DWORD)IOCTL_AKEN_WRITE_MSR, &in, sizeof(in), 0, 0u, &bytesReturned, ovl);
	WARN_IF_FALSE(ok);
}


//-----------------------------------------------------------------------------
// driver installation
//-----------------------------------------------------------------------------

// @param access need not include SC_MANAGER_CONNECT ("implicitly specified")
static SC_HANDLE OpenServiceControlManager(DWORD access)
{
	LPCWSTR machineName = 0;	// local
	LPCWSTR databaseName = 0;	// default
	SC_HANDLE hSCM = OpenSCManagerW(machineName, databaseName, access);
	if(!hSCM)
	{
		// ensure no other problems arose
		ENSURE(GetLastError() == ERROR_ACCESS_DENIED);

		// administrator privileges are required for SC_MANAGER_CREATE_SERVICE.
		// this is a problem on Vista / Win7, so users will have to use the
		// separate aken_install.bat

		return 0;
	}

	return hSCM;	// success
}


static void UninstallDriver()
{
	SC_HANDLE hSCM = OpenServiceControlManager(SC_MANAGER_ENUMERATE_SERVICE);
	if(!hSCM)
		return;
	SC_HANDLE hService = OpenServiceW(hSCM, AKEN_NAME, SERVICE_STOP|SERVICE_INTERROGATE);
	if(!hService)
		return;

	// stop service
	SERVICE_STATUS serviceStatus;
	if(!ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus))
	{
		// if the problem wasn't that the service is already stopped,
		// something actually went wrong.
		const DWORD err = GetLastError();
		ENSURE(err == ERROR_SERVICE_NOT_ACTIVE || err == ERROR_SERVICE_CANNOT_ACCEPT_CTRL);
	}

	// delete service
	BOOL ok;
	ok = DeleteService(hService);
	WARN_IF_FALSE(ok);
	ok = CloseServiceHandle(hService);
	WARN_IF_FALSE(ok);

	ok = CloseServiceHandle(hSCM);
	WARN_IF_FALSE(ok);
}


#if CONFIG2_MAHAF_ATTEMPT_DRIVER_START

static void StartDriver(const OsPath& driverPathname)
{
	const SC_HANDLE hSCM = OpenServiceControlManager(SC_MANAGER_CREATE_SERVICE);
	if(!hSCM)
	{
		ENSURE(GetLastError() == ERROR_ACCESS_DENIED);
		SetLastError(0);
		return;
	}

	SC_HANDLE hService = OpenServiceW(hSCM, AKEN_NAME, SERVICE_START);

	// during development, we want to ensure the newest build is used, so
	// unload and re-create the service if it's running/installed.
	// as of 2008-03-24 no further changes to Aken are pending, so this is
	// disabled (thus also avoiding trouble when running multiple instances)
#if 0
	if(hService)
	{
		BOOL ok = CloseServiceHandle(hService);
		WARN_IF_FALSE(ok);
		hService = 0;
		UninstallDriver();
	}
#endif

	// create service (note: this just enters the service into SCM's DB;
	// no error is raised if the driver binary doesn't exist etc.)
	if(!hService)
	{
		LPCWSTR startName = 0;	// LocalSystem
		// NB: Windows 7 seems to insist upon backslashes (i.e. external_file_string)
		hService = CreateServiceW(hSCM, AKEN_NAME, AKEN_NAME,
			SERVICE_START, SERVICE_KERNEL_DRIVER, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
			OsString(driverPathname).c_str(), 0, 0, 0, startName, 0);
		ENSURE(hService != 0);
	}

	{
		DWORD numArgs = 0;
		BOOL ok = StartService(hService, numArgs, 0);
		if(!ok)
		{
			switch(GetLastError())
			{
			case ERROR_SERVICE_ALREADY_RUNNING:
				// ok, no action needed
				break;
			case ERROR_ACCESS_DENIED:
				// Win7, can't start service; must use aken_install.bat
				break;
			case ERROR_INVALID_IMAGE_HASH:
				// Win7 x86 rejects our code signing certificate; must enable
				// "test signing" mode via aken_install.bat
				break;
			default:
				// unexpected problem
				DEBUG_WARN_ERR(ERR::LOGIC);
				break;
			}
		}
	}

	CloseServiceHandle(hService);
	CloseServiceHandle(hSCM);
}


static bool Is64BitOs()
{
#if OS_WIN64
	return true;
#else
	return wutil_IsWow64();
#endif
}

static OsPath DriverPathname()
{
	const char* const bits = Is64BitOs()? "64" : "";
#ifdef NDEBUG
	const char* const debug = "";
#else
	const char* const debug = "d";
#endif
	char filename[PATH_MAX];
	sprintf_s(filename, ARRAY_SIZE(filename), "aken%s%s.sys", bits, debug);
	return wutil_ExecutablePath() / filename;
}

#endif // CONFIG2_MAHAF_ATTEMPT_DRIVER_START


//-----------------------------------------------------------------------------

static Status Init()
{
	WinScopedPreserveLastError s;

	if(wutil_HasCommandLineArgument(L"-wNoMahaf"))
		return ERR::NOT_SUPPORTED;	// NOWARN

#if CONFIG2_MAHAF_ATTEMPT_DRIVER_START
	{
		const OsPath driverPathname = DriverPathname();
		StartDriver(driverPathname);
	}
#endif

	{
		const DWORD shareMode = 0;
		hAken = CreateFileW(L"\\\\.\\Aken", GENERIC_READ, shareMode, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
		if(hAken == INVALID_HANDLE_VALUE)
		{
			// GetLastError() is ERROR_FILE_NOT_FOUND if the driver isn't running,
			// but this is also reached when a handle has already been opened
			// (e.g. by a second instance of the same program) - in which case we must
			// indicate failure so that clients won't engage in unsynchronized ring 0 operations.
			SetLastError(0);
			return ERR::INVALID_HANDLE;	// NOWARN (happens often due to security restrictions)
		}
	}

	return INFO::OK;
}

static void Shutdown()
{
	CloseHandle(hAken);
	hAken = INVALID_HANDLE_VALUE;

	UninstallDriver();
}


static ModuleInitState initState;

Status mahaf_Init()
{
	return ModuleInit(&initState, Init);
}

void mahaf_Shutdown()
{
	ModuleShutdown(&initState, Shutdown);
}