Files
0ad/source/lib/sysdep/os/win/mahaf.cpp
T
janwas 98770fa4cc # fix race conditions in ModuleInit and related cleanup.
CAS: uintptr_t->intptr_t to allow use of both cpu_CAS and cpu_AtomicAdd
topology: remove non-thread safe caching, expose ApicIds, use ModuleInit
x86_x64: use ModuleInit instead of unsafe static flags; zero-init regs
instead of just setting ecx
ModuleInitState now holds the LibError returned by the init callback (so
that the second init doesn't appear to succeed despite the first
failing)
wnuma: cleanup, add ACPI SLIT relative distance detection

This was SVN commit r7741.
2010-07-12 12:57:58 +00:00

337 lines
8.4 KiB
C++

/* 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/win.h"
#include <winioctl.h>
#include "lib/sysdep/os/win/aken/aken.h"
#include "lib/sysdep/os/win/wutil.h"
#include "lib/module_init.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
BOOL ok = DeviceIoControl(hAken, (DWORD)IOCTL_AKEN_READ_PORT, &in, sizeof(in), &out, sizeof(out), &bytesReturned, ovl);
if(!ok)
{
WARN_WIN32_ERR;
return 0;
}
debug_assert(bytesReturned == sizeof(out));
const u32 value = out.value;
return value;
}
u8 mahaf_ReadPort8(u16 port)
{
const u32 value = ReadPort(port, 1);
debug_assert(value <= 0xFF);
return (u8)(value & 0xFF);
}
u16 mahaf_ReadPort16(u16 port)
{
const u32 value = ReadPort(port, 2);
debug_assert(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()
{
// WinXP introduced checks that ensure we don't re-map pages with
// incompatible attributes. without this, mapping physical pages risks
// disaster due to TLB corruption.
if(wutil_WindowsVersion() < WUTIL_VERSION_XP)
return true;
return false;
}
volatile void* mahaf_MapPhysicalMemory(uintptr_t physicalAddress, size_t numBytes)
{
debug_assert(!mahaf_IsPhysicalMappingDangerous());
AkenMapIn in;
in.physicalAddress = (DWORD64)physicalAddress;
in.numBytes = (DWORD64)numBytes;
AkenMapOut out;
DWORD bytesReturned;
LPOVERLAPPED ovl = 0; // synchronous
BOOL ok = DeviceIoControl(hAken, (DWORD)IOCTL_AKEN_MAP, &in, sizeof(in), &out, sizeof(out), &bytesReturned, ovl);
if(!ok)
{
WARN_WIN32_ERR;
return 0;
}
debug_assert(bytesReturned == sizeof(out));
volatile void* virtualAddress = (volatile void*)(uintptr_t)out.virtualAddress;
return virtualAddress;
}
void mahaf_UnmapPhysicalMemory(volatile void* virtualAddress)
{
debug_assert(!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);
}
//-----------------------------------------------------------------------------
// driver installation
//-----------------------------------------------------------------------------
static SC_HANDLE OpenServiceControlManager()
{
LPCWSTR machineName = 0; // local
LPCWSTR databaseName = 0; // default
SC_HANDLE hSCM = OpenSCManagerW(machineName, databaseName, SC_MANAGER_ALL_ACCESS);
if(!hSCM)
{
// administrator privileges are required. note: installing the
// service and having it start automatically would allow
// Least-Permission accounts to use it, but is too invasive and
// thus out of the question.
// make sure the error is as expected, otherwise something is afoot.
debug_assert(GetLastError() == ERROR_ACCESS_DENIED);
return 0;
}
return hSCM; // success
}
static void UninstallDriver()
{
SC_HANDLE hSCM = OpenServiceControlManager();
if(!hSCM)
return;
SC_HANDLE hService = OpenServiceW(hSCM, AKEN_NAME, SERVICE_ALL_ACCESS);
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();
debug_assert(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);
}
static void StartDriver(const fs::wpath& driverPathname)
{
const SC_HANDLE hSCM = OpenServiceControlManager();
if(!hSCM)
return;
SC_HANDLE hService = OpenServiceW(hSCM, AKEN_NAME, SERVICE_ALL_ACCESS);
// 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
hService = CreateServiceW(hSCM, AKEN_NAME, AKEN_NAME,
SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
driverPathname.string().c_str(), 0, 0, 0, startName, 0);
debug_assert(hService != 0);
}
// start service
{
DWORD numArgs = 0;
BOOL ok = StartService(hService, numArgs, 0);
if(!ok)
{
if(GetLastError() != ERROR_SERVICE_ALREADY_RUNNING)
{
// starting failed. don't raise a warning because this
// always happens on least-permission user accounts.
//WARN_IF_FALSE(0);
}
}
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
}
static bool Is64BitOs()
{
#if OS_WIN64
return true;
#else
return wutil_IsWow64();
#endif
}
static fs::wpath DriverPathname()
{
const wchar_t* const bits = Is64BitOs()? L"64" : L"";
#ifdef NDEBUG
const wchar_t* const debug = L"";
#else
const wchar_t* const debug = L"d";
#endif
wchar_t filename[PATH_MAX];
swprintf_s(filename, ARRAY_SIZE(filename), L"aken%ls%ls.sys", bits, debug);
return wutil_ExecutablePath()/filename;
}
//-----------------------------------------------------------------------------
static LibError Init()
{
if(wutil_HasCommandLineArgument(L"-wNoMahaf"))
return ERR::NOT_SUPPORTED;
{
const fs::wpath driverPathname = DriverPathname();
StartDriver(driverPathname);
}
{
const DWORD shareMode = 0;
hAken = CreateFileW(L"\\\\.\\Aken", GENERIC_READ, shareMode, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(hAken == INVALID_HANDLE_VALUE)
return ERR::INVALID_HANDLE;
}
return INFO::OK;
}
static void Shutdown()
{
CloseHandle(hAken);
hAken = INVALID_HANDLE_VALUE;
UninstallDriver();
}
static ModuleInitState initState;
LibError mahaf_Init()
{
return ModuleInit(&initState, Init);
}
void mahaf_Shutdown()
{
ModuleShutdown(&initState, Shutdown);
}