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
|
/*
* Windows NT kernel wrapper for KQEMU
*
* Copyright (C) 2005 Filip Navara
*
* 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.
*/
#include <stddef.h>
#include <ddk/ntddk.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
#undef CDECL
#include "kqemu-kernel.h"
/* XXX: make it dynamic according to available RAM */
#define MAX_LOCKED_PAGES (16386 / 4)
struct kqemu_instance {
struct kqemu_state *state;
PIRP current_irp;
};
FAST_MUTEX instance_lock;
struct kqemu_instance *active_instance;
struct kqemu_global_state *kqemu_gs;
/* lock the page at virtual address 'user_addr' and return its
page index. Return -1 if error */
struct kqemu_user_page *CDECL kqemu_lock_user_page(unsigned long *ppage_index,
unsigned long user_addr)
{
PMDL mdl;
PPFN_NUMBER mdl_pages;
if (user_addr & 0xfff) {
DbgPrint("kqemu: unaligned user memory\n");
return NULL;
}
mdl = ExAllocatePool(NonPagedPool, sizeof(MDL) + sizeof(PFN_NUMBER));
if (mdl == NULL) {
DbgPrint("kqemu: Not enough memory for MDL structure\n");
return NULL;
}
mdl_pages = (PPFN_NUMBER)(mdl + 1);
MmInitializeMdl(mdl, user_addr, PAGE_SIZE);
/* XXX: Protect with SEH. */
MmProbeAndLockPages(mdl, KernelMode, IoModifyAccess);
*ppage_index = mdl_pages[0];
return (struct kqemu_user_page *)mdl;
}
void CDECL kqemu_unlock_user_page(struct kqemu_user_page *page)
{
PMDL mdl = (PMDL)page;
MmUnlockPages(mdl);
ExFreePool(mdl);
}
struct kqemu_page *CDECL kqemu_alloc_zeroed_page(unsigned long *ppage_index)
{
void *ptr;
LARGE_INTEGER pa;
ptr = MmAllocateNonCachedMemory(PAGE_SIZE);
if (!ptr)
return NULL;
RtlZeroMemory(ptr, PAGE_SIZE);
pa = MmGetPhysicalAddress(ptr);
*ppage_index = (unsigned long)(pa.QuadPart >> PAGE_SHIFT);
return (struct kqemu_page *)ptr;
}
void CDECL kqemu_free_page(struct kqemu_page *page)
{
void *ptr = page;
if (!ptr)
return;
MmFreeNonCachedMemory(ptr, PAGE_SIZE);
}
void * CDECL kqemu_page_kaddr(struct kqemu_page *page)
{
void *ptr = page;
return ptr;
}
void * CDECL kqemu_vmalloc(unsigned int size)
{
void * ptr;
ptr = ExAllocatePoolWithTag(NonPagedPool, size, TAG('K','Q','M','U'));
if (!ptr)
return NULL;
RtlZeroMemory(ptr, size);
return ptr;
}
void CDECL kqemu_vfree(void *ptr)
{
if (!ptr)
return;
ExFreePool(ptr);
}
unsigned long CDECL kqemu_vmalloc_to_phys(const void *vaddr)
{
LARGE_INTEGER pa;
pa = MmGetPhysicalAddress((void *)vaddr);
return (unsigned long)(pa.QuadPart >> PAGE_SHIFT);
}
/* Map a IO area in the kernel address space and return its
address. Return NULL if error or not implemented. */
void * CDECL kqemu_io_map(unsigned long page_index, unsigned int size)
{
#if 1
PHYSICAL_ADDRESS pa;
pa.QuadPart = page_index << PAGE_SHIFT;
return MmMapIoSpace(pa, size, MmNonCached);
#else
/* XXX: mingw32 tools too old */
return NULL;
#endif
}
/* Unmap the IO area */
void CDECL kqemu_io_unmap(void *ptr, unsigned int size)
{
return MmUnmapIoSpace(ptr, size);
}
/* return TRUE if a signal is pending (i.e. the guest must stop
execution) */
int CDECL kqemu_schedule(void)
{
#if 0
return active_instance->current_irp->Cancel;
#else
/* XXX: temporary "fix" to correct the CancelIO() problem. A
proper solution may be to add a new KQEMU_INTERRUPT ioctl. */
return TRUE;
#endif
}
void CDECL kqemu_log(const char *fmt, ...)
{
char log_buf[1024];
va_list ap;
va_start(ap, fmt);
_vsnprintf(log_buf, sizeof(log_buf), fmt, ap);
DbgPrint("kqemu: %s", log_buf);
va_end(ap);
}
NTSTATUS STDCALL
KQemuCreate(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
struct kqemu_instance *State;
State = kqemu_vmalloc(sizeof(struct kqemu_instance));
if (State == NULL)
{
Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_INSUFFICIENT_RESOURCES;
}
IrpStack->FileObject->FsContext = State;
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
NTSTATUS STDCALL
KQemuClose(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
struct kqemu_instance *State = IrpStack->FileObject->FsContext;
if (State->state) {
kqemu_delete(State->state);
State->state = NULL;
}
kqemu_vfree(State);
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
NTSTATUS STDCALL
KQemuDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
struct kqemu_instance *State = IrpStack->FileObject->FsContext;
NTSTATUS Status;
int ret;
Irp->IoStatus.Information = 0;
switch (IrpStack->Parameters.DeviceIoControl.IoControlCode)
{
case KQEMU_INIT:
if (State->state) {
Status = STATUS_INVALID_PARAMETER;
break;
}
if (IrpStack->Parameters.DeviceIoControl.InputBufferLength <
sizeof(struct kqemu_init))
{
Status = STATUS_INVALID_PARAMETER;
break;
}
State->state = kqemu_init((struct kqemu_init *)Irp->AssociatedIrp.SystemBuffer,
kqemu_gs);
if (!State->state) {
Status = STATUS_INSUFFICIENT_RESOURCES;
break;
}
Status = STATUS_SUCCESS;
break;
case KQEMU_EXEC:
{
struct kqemu_cpu_state *ctx;
if (!State->state) {
Status = STATUS_INVALID_PARAMETER;
break;
}
if (IrpStack->Parameters.DeviceIoControl.InputBufferLength <
sizeof(*ctx) ||
IrpStack->Parameters.DeviceIoControl.OutputBufferLength <
sizeof(*ctx))
{
Status = STATUS_INVALID_PARAMETER;
break;
}
ExAcquireFastMutex(&instance_lock);
active_instance = State;
State->current_irp = Irp;
ctx = kqemu_get_cpu_state(State->state);
RtlCopyMemory(ctx, Irp->AssociatedIrp.SystemBuffer,
sizeof(*ctx));
ret = kqemu_exec(State->state);
RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, ctx, sizeof(*ctx));
ExReleaseFastMutex(&instance_lock);
Irp->IoStatus.Information = sizeof(*ctx);
Status = STATUS_SUCCESS;
}
break;
case KQEMU_GET_VERSION:
if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength <
sizeof(int))
{
Status = STATUS_INVALID_PARAMETER;
break;
}
*((int *)Irp->AssociatedIrp.SystemBuffer) = KQEMU_VERSION;
Irp->IoStatus.Information = sizeof(int);
Status = STATUS_SUCCESS;
break;
default:
Status = STATUS_INVALID_PARAMETER;
break;
}
Irp->IoStatus.Status = Status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return Status;
}
VOID STDCALL
KQemuUnload(PDRIVER_OBJECT DriverObject)
{
UNICODE_STRING SymlinkName;
RtlInitUnicodeString(&SymlinkName, L"\\??\\kqemu");
IoDeleteSymbolicLink(&SymlinkName);
IoDeleteDevice(DriverObject->DeviceObject);
if (kqemu_gs) {
kqemu_global_delete(kqemu_gs);
kqemu_gs = NULL;
}
}
NTSTATUS STDCALL
DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
{
PDEVICE_OBJECT DeviceObject;
UNICODE_STRING DeviceName;
UNICODE_STRING SymlinkName;
NTSTATUS Status;
DbgPrint("QEMU Accelerator Module version %d.%d.%d\n",
(KQEMU_VERSION >> 16),
(KQEMU_VERSION >> 8) & 0xff,
(KQEMU_VERSION) & 0xff);
MmLockPagableCodeSection(DriverEntry);
ExInitializeFastMutex(&instance_lock);
kqemu_gs = kqemu_global_init(MAX_LOCKED_PAGES);
DriverObject->MajorFunction[IRP_MJ_CREATE] = KQemuCreate;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = KQemuClose;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = KQemuDeviceControl;
DriverObject->DriverUnload = KQemuUnload;
RtlInitUnicodeString(&DeviceName, L"\\Device\\kqemu");
RtlInitUnicodeString(&SymlinkName, L"\\??\\kqemu");
Status = IoCreateDevice(DriverObject, 0,
&DeviceName, FILE_DEVICE_UNKNOWN, 0, FALSE,
&DeviceObject);
if (!NT_SUCCESS(Status))
{
return Status;
}
/* Create the dos device link */
Status = IoCreateSymbolicLink(&SymlinkName, &DeviceName);
if (!NT_SUCCESS(Status))
{
IoDeleteDevice(DeviceObject);
return Status;
}
return STATUS_SUCCESS;
}
|