Changeset 44188 in vbox for trunk/src/VBox/HostDrivers
- Timestamp:
- Dec 20, 2012 10:18:30 AM (12 years ago)
- Location:
- trunk/src/VBox/HostDrivers/Support
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/Support/SUPDrv.c
r44177 r44188 666 666 int VBOXCALL supdrvCreateSession(PSUPDRVDEVEXT pDevExt, bool fUser, bool fUnrestricted, PSUPDRVSESSION *ppSession) 667 667 { 668 int rc; 669 PSUPDRVSESSION pSession; 670 671 if (!SUP_IS_DEVEXT_VALID(pDevExt)) 672 return VERR_INVALID_PARAMETER; 673 668 674 /* 669 675 * Allocate memory for the session data. 670 676 */ 671 int rc; 672 PSUPDRVSESSION pSession = *ppSession = (PSUPDRVSESSION)RTMemAllocZ(pDevExt->cbSession); 677 pSession = *ppSession = (PSUPDRVSESSION)RTMemAllocZ(pDevExt->cbSession); 673 678 if (pSession) 674 679 { -
trunk/src/VBox/HostDrivers/Support/SUPDrvInternal.h
r44173 r44188 199 199 ( VALID_PTR(pSession) \ 200 200 && pSession->u32Cookie == BIRD_INV) 201 202 /** 203 * Validates a device extension pointer. 204 * 205 * @returns true/false accordingly. 206 * @param pDevExt The device extension. 207 */ 208 #define SUP_IS_DEVEXT_VALID(pDevExt) \ 209 ( VALID_PTR(pDevExt)\ 210 && pDevExt->u32Cookie == BIRD) 201 211 202 212 … … 479 489 typedef struct SUPDRVDEVEXT 480 490 { 491 /** Global cookie. */ 492 uint32_t u32Cookie; 493 /** The actual size of SUPDRVSESSION. (SUPDRV_AGNOSTIC) */ 494 uint32_t cbSession; 495 481 496 /** Spinlock to serialize the initialization, usage counting and objects. */ 482 497 RTSPINLOCK Spinlock; … … 486 501 /** List of free object usage records. */ 487 502 PSUPDRVUSAGE volatile pUsageFree; 488 489 /** Global cookie. */490 uint32_t u32Cookie;491 /** The actual size of SUPDRVSESSION. (SUPDRV_AGNOSTIC) */492 uint32_t cbSession;493 503 494 504 /** Loader mutex. -
trunk/src/VBox/HostDrivers/Support/win/SUPDrv-win.cpp
r44173 r44188 47 47 /** The support service name. */ 48 48 #define SERVICE_NAME "VBoxDrv" 49 /** Win32 Device name. */50 #define DEVICE_NAME "\\\\.\\VBoxDrv"51 /** NT Device name. */52 #define DEVICE_NAME_NT L"\\Device\\VBoxDrv"53 /** Win Symlink name. */54 #define DEVICE_NAME_DOS L"\\DosDevices\\VBoxDrv"55 49 /** The Pool tag (VBox). */ 56 50 #define SUPDRV_NT_POOL_TAG 'xoBV' 51 52 /** Win32 device name for system access. */ 53 #define DEVICE_NAME_SYS "\\\\.\\VBoxDrv" 54 /** NT device name for system access. */ 55 #define DEVICE_NAME_NT_SYS L"\\Device\\VBoxDrv" 56 /** Win Symlink name for system access. */ 57 #define DEVICE_NAME_DOS_SYS L"\\DosDevices\\VBoxDrv" 58 59 /** Win32 device name for user access. */ 60 #define DEVICE_NAME_USR "\\\\.\\VBoxDrvU" 61 /** NT device name for user access. */ 62 #define DEVICE_NAME_NT_USR L"\\Device\\VBoxDrvU" 63 /** Win Symlink name for user access. */ 64 #define DEVICE_NAME_DOS_USR L"\\DosDevices\\VBoxDrvU" 57 65 58 66 … … 68 76 } SUPDRVEXECMEM, *PSUPDRVEXECMEM; 69 77 #endif 78 79 /** 80 * Device extension used by VBoxDrvU. 81 */ 82 typedef struct SUPDRVDEVEXTUSR 83 { 84 /** Global cookie (same location as in SUPDRVDEVEXT, different value). */ 85 uint32_t u32Cookie; 86 /** Pointer to the main driver extension. */ 87 PSUPDRVDEVEXT pMainDrvExt; 88 } SUPDRVDEVEXTUSR; 89 AssertCompileMembersAtSameOffset(SUPDRVDEVEXT, u32Cookie, SUPDRVDEVEXTUSR, u32Cookie); 90 /** Pointer to the VBoxDrvU device extension. */ 91 typedef SUPDRVDEVEXTUSR *PSUPDRVDEVEXTUSR; 92 /** Value of SUPDRVDEVEXTUSR::u32Cookie. */ 93 #define SUPDRVDEVEXTUSR_COOKIE UINT32_C(0x12345678) 94 95 /** Get the main device extension. */ 96 #define SUPDRVNT_GET_DEVEXT(pDevObj) \ 97 ( pDevObj != g_pDevObjUsr \ 98 ? (PSUPDRVDEVEXT)pDevObj->DeviceExtension \ 99 : ((PSUPDRVDEVEXTUSR)pDevObj->DeviceExtension)->pMainDrvExt ) 70 100 71 101 … … 93 123 94 124 125 /******************************************************************************* 126 * Global Variables * 127 *******************************************************************************/ 128 /** Pointer to the system device instance. */ 129 static PDEVICE_OBJECT g_pDevObjSys = NULL; 130 /** Pointer to the user device instance. */ 131 static PDEVICE_OBJECT g_pDevObjUsr = NULL; 132 133 134 /** 135 * Takes care of creating the devices and their symbolic links. 136 * 137 * @returns NT status code. 138 * @param pDrvObj Pointer to driver object. 139 */ 140 static NTSTATUS vboxdrvNtCreateDevices(PDRIVER_OBJECT pDrvObj) 141 { 142 /* 143 * System device. 144 */ 145 UNICODE_STRING DevName; 146 RtlInitUnicodeString(&DevName, DEVICE_NAME_NT_SYS); 147 NTSTATUS rcNt = IoCreateDevice(pDrvObj, sizeof(SUPDRVDEVEXT), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &g_pDevObjSys); 148 if (NT_SUCCESS(rcNt)) 149 { 150 UNICODE_STRING DosName; 151 RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS_SYS); 152 rcNt = IoCreateSymbolicLink(&DosName, &DevName); 153 if (NT_SUCCESS(rcNt)) 154 { 155 /* 156 * User device. 157 */ 158 RtlInitUnicodeString(&DevName, DEVICE_NAME_NT_USR); 159 rcNt = IoCreateDevice(pDrvObj, sizeof(SUPDRVDEVEXTUSR), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &g_pDevObjUsr); 160 if (NT_SUCCESS(rcNt)) 161 { 162 UNICODE_STRING DosName; 163 RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS_USR); 164 rcNt = IoCreateSymbolicLink(&DosName, &DevName); 165 if (NT_SUCCESS(rcNt)) 166 { 167 PSUPDRVDEVEXTUSR pDevExtUsr = (PSUPDRVDEVEXTUSR)g_pDevObjUsr->DeviceExtension; 168 pDevExtUsr->pMainDrvExt = (PSUPDRVDEVEXT)g_pDevObjSys->DeviceExtension; 169 pDevExtUsr->u32Cookie = SUPDRVDEVEXTUSR_COOKIE; 170 171 /* Done. */ 172 return rcNt; 173 } 174 175 /* Bail out. */ 176 IoDeleteDevice(g_pDevObjUsr); 177 g_pDevObjUsr = NULL; 178 } 179 IoDeleteSymbolicLink(&DosName); 180 } 181 IoDeleteDevice(g_pDevObjSys); 182 g_pDevObjSys = NULL; 183 } 184 return rcNt; 185 } 186 187 /** 188 * Destroys the devices and links created by vboxdrvNtCreateDevices. 189 */ 190 static void vboxdrvNtDestroyDevices(void) 191 { 192 UNICODE_STRING DosName; 193 RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS_SYS); 194 NTSTATUS rcNt = IoDeleteSymbolicLink(&DosName); 195 196 RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS_USR); 197 rcNt = IoDeleteSymbolicLink(&DosName); 198 199 PSUPDRVDEVEXTUSR pDevExtUsr = (PSUPDRVDEVEXTUSR)g_pDevObjUsr->DeviceExtension; 200 pDevExtUsr->pMainDrvExt = NULL; 201 202 IoDeleteDevice(g_pDevObjUsr); 203 g_pDevObjUsr = NULL; 204 IoDeleteDevice(g_pDevObjSys); 205 g_pDevObjSys = NULL; 206 } 207 208 95 209 /** 96 210 * Driver entry point. … … 102 216 ULONG _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath) 103 217 { 104 NTSTATUS rc;105 106 218 /* 107 219 * Create device. … … 109 221 * subsystems (OS/2, win32, ++) can access the device.) 110 222 */ 111 UNICODE_STRING DevName; 112 RtlInitUnicodeString(&DevName, DEVICE_NAME_NT); 113 PDEVICE_OBJECT pDevObj; 114 rc = IoCreateDevice(pDrvObj, sizeof(SUPDRVDEVEXT), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDevObj); 115 if (NT_SUCCESS(rc)) 116 { 117 UNICODE_STRING DosName; 118 RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS); 119 rc = IoCreateSymbolicLink(&DosName, &DevName); 120 if (NT_SUCCESS(rc)) 223 NTSTATUS rcNt = vboxdrvNtCreateDevices(pDrvObj); 224 if (NT_SUCCESS(rcNt)) 225 { 226 int vrc = RTR0Init(0); 227 if (RT_SUCCESS(vrc)) 121 228 { 122 int vrc = RTR0Init(0); 123 if (RT_SUCCESS(vrc)) 124 { 125 Log(("VBoxDrv::DriverEntry\n")); 126 229 Log(("VBoxDrv::DriverEntry\n")); 230 231 /* 232 * Initialize the device extension. 233 */ 234 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)g_pDevObjSys->DeviceExtension; 235 memset(pDevExt, 0, sizeof(*pDevExt)); 236 237 vrc = supdrvInitDevExt(pDevExt, sizeof(SUPDRVSESSION)); 238 if (!vrc) 239 { 127 240 /* 128 * Initialize the device extension.241 * Setup the driver entry points in pDrvObj. 129 242 */ 130 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension; 131 memset(pDevExt, 0, sizeof(*pDevExt)); 132 133 vrc = supdrvInitDevExt(pDevExt, sizeof(SUPDRVSESSION)); 134 if (!vrc) 135 { 136 /* 137 * Setup the driver entry points in pDrvObj. 138 */ 139 pDrvObj->DriverUnload = VBoxDrvNtUnload; 140 pDrvObj->MajorFunction[IRP_MJ_CREATE] = VBoxDrvNtCreate; 141 pDrvObj->MajorFunction[IRP_MJ_CLEANUP] = VBoxDrvNtCleanup; 142 pDrvObj->MajorFunction[IRP_MJ_CLOSE] = VBoxDrvNtClose; 143 pDrvObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxDrvNtDeviceControl; 144 pDrvObj->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = VBoxDrvNtInternalDeviceControl; 145 pDrvObj->MajorFunction[IRP_MJ_READ] = VBoxDrvNtNotSupportedStub; 146 pDrvObj->MajorFunction[IRP_MJ_WRITE] = VBoxDrvNtNotSupportedStub; 147 148 /* more? */ 149 150 /* Register ourselves for power state changes. */ 151 UNICODE_STRING CallbackName; 152 OBJECT_ATTRIBUTES Attr; 153 154 RtlInitUnicodeString(&CallbackName, L"\\Callback\\PowerState"); 155 InitializeObjectAttributes(&Attr, &CallbackName, OBJ_CASE_INSENSITIVE, NULL, NULL); 156 157 rc = ExCreateCallback(&pDevExt->pObjPowerCallback, &Attr, TRUE, TRUE); 158 if (rc == STATUS_SUCCESS) 159 pDevExt->hPowerCallback = ExRegisterCallback(pDevExt->pObjPowerCallback, VBoxPowerDispatchCallback, pDevObj); 160 161 Log(("VBoxDrv::DriverEntry returning STATUS_SUCCESS\n")); 162 return STATUS_SUCCESS; 163 } 164 165 Log(("supdrvInitDevExit failed with vrc=%d!\n", vrc)); 166 rc = VBoxDrvNtErr2NtStatus(vrc); 167 168 IoDeleteSymbolicLink(&DosName); 169 RTR0Term(); 170 } 171 else 172 { 173 Log(("RTR0Init failed with vrc=%d!\n", vrc)); 174 rc = VBoxDrvNtErr2NtStatus(vrc); 175 } 243 pDrvObj->DriverUnload = VBoxDrvNtUnload; 244 pDrvObj->MajorFunction[IRP_MJ_CREATE] = VBoxDrvNtCreate; 245 pDrvObj->MajorFunction[IRP_MJ_CLEANUP] = VBoxDrvNtCleanup; 246 pDrvObj->MajorFunction[IRP_MJ_CLOSE] = VBoxDrvNtClose; 247 pDrvObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxDrvNtDeviceControl; 248 pDrvObj->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = VBoxDrvNtInternalDeviceControl; 249 pDrvObj->MajorFunction[IRP_MJ_READ] = VBoxDrvNtNotSupportedStub; 250 pDrvObj->MajorFunction[IRP_MJ_WRITE] = VBoxDrvNtNotSupportedStub; 251 252 /* more? */ 253 254 /* Register ourselves for power state changes. */ 255 UNICODE_STRING CallbackName; 256 OBJECT_ATTRIBUTES Attr; 257 258 RtlInitUnicodeString(&CallbackName, L"\\Callback\\PowerState"); 259 InitializeObjectAttributes(&Attr, &CallbackName, OBJ_CASE_INSENSITIVE, NULL, NULL); 260 261 rcNt = ExCreateCallback(&pDevExt->pObjPowerCallback, &Attr, TRUE, TRUE); 262 if (rcNt == STATUS_SUCCESS) 263 pDevExt->hPowerCallback = ExRegisterCallback(pDevExt->pObjPowerCallback, VBoxPowerDispatchCallback, 264 g_pDevObjSys); 265 266 Log(("VBoxDrv::DriverEntry returning STATUS_SUCCESS\n")); 267 return STATUS_SUCCESS; 268 } 269 270 Log(("supdrvInitDevExit failed with vrc=%d!\n", vrc)); 271 rcNt = VBoxDrvNtErr2NtStatus(vrc); 272 273 RTR0Term(); 176 274 } 177 275 else 178 Log(("IoCreateSymbolicLink failed with rc=%#x!\n", rc)); 179 180 IoDeleteDevice(pDevObj); 181 } 182 else 183 Log(("IoCreateDevice failed with rc=%#x!\n", rc)); 184 185 if (NT_SUCCESS(rc)) 186 rc = STATUS_INVALID_PARAMETER; 187 Log(("VBoxDrv::DriverEntry returning %#x\n", rc)); 188 return rc; 276 { 277 Log(("RTR0Init failed with vrc=%d!\n", vrc)); 278 rcNt = VBoxDrvNtErr2NtStatus(vrc); 279 } 280 281 vboxdrvNtDestroyDevices(); 282 } 283 if (NT_SUCCESS(rcNt)) 284 rcNt = STATUS_INVALID_PARAMETER; 285 return rcNt; 189 286 } 190 287 … … 197 294 void _stdcall VBoxDrvNtUnload(PDRIVER_OBJECT pDrvObj) 198 295 { 199 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT) pDrvObj->DeviceObject->DeviceExtension;296 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)g_pDevObjSys->DeviceExtension; 200 297 201 298 Log(("VBoxDrvNtUnload at irql %d\n", KeGetCurrentIrql())); … … 209 306 /* 210 307 * We ASSUME that it's not possible to unload a driver with open handles. 211 * Start by deleting the symbolic link212 */213 UNICODE_STRING DosName;214 RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS);215 NTSTATUS rc = IoDeleteSymbolicLink(&DosName);216 217 /*218 * Terminate the GIP page and delete the device extension.219 308 */ 220 309 supdrvDeleteDevExt(pDevExt); 221 310 RTR0Term(); 222 IoDeleteDevice(pDrvObj->DeviceObject); 311 vboxdrvNtDestroyDevices(); 312 313 NOREF(pDrvObj); 223 314 } 224 315 … … 233 324 { 234 325 Log(("VBoxDrvNtCreate: RequestorMode=%d\n", pIrp->RequestorMode)); 326 const bool fUnrestricted = pDevObj == g_pDevObjSys; 235 327 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp); 236 328 PFILE_OBJECT pFileObj = pStack->FileObject; 237 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;329 PSUPDRVDEVEXT pDevExt = SUPDRVNT_GET_DEVEXT(pDevObj); 238 330 239 331 /* … … 265 357 pFileObj->FsContext = NULL; 266 358 PSUPDRVSESSION pSession; 267 int rc = supdrvCreateSession(pDevExt, true /*fUser*/, true /*fUnrestricted*/, &pSession);359 int rc = supdrvCreateSession(pDevExt, true /*fUser*/, fUnrestricted, &pSession); 268 360 if (!rc) 269 361 pFileObj->FsContext = pSession; … … 286 378 NTSTATUS _stdcall VBoxDrvNtCleanup(PDEVICE_OBJECT pDevObj, PIRP pIrp) 287 379 { 288 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;380 PSUPDRVDEVEXT pDevExt = SUPDRVNT_GET_DEVEXT(pDevObj); 289 381 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp); 290 382 PFILE_OBJECT pFileObj = pStack->FileObject; … … 314 406 NTSTATUS _stdcall VBoxDrvNtClose(PDEVICE_OBJECT pDevObj, PIRP pIrp) 315 407 { 316 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;408 PSUPDRVDEVEXT pDevExt = SUPDRVNT_GET_DEVEXT(pDevObj); 317 409 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp); 318 410 PFILE_OBJECT pFileObj = pStack->FileObject; … … 342 434 NTSTATUS _stdcall VBoxDrvNtDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp) 343 435 { 344 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;345 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);436 PSUPDRVDEVEXT pDevExt = SUPDRVNT_GET_DEVEXT(pDevObj); 437 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp); 346 438 PSUPDRVSESSION pSession = (PSUPDRVSESSION)pStack->FileObject->FsContext; 347 439 … … 355 447 */ 356 448 ULONG ulCmd = pStack->Parameters.DeviceIoControl.IoControlCode; 357 if ( ulCmd == SUP_IOCTL_FAST_DO_RAW_RUN 358 || ulCmd == SUP_IOCTL_FAST_DO_HM_RUN 359 || ulCmd == SUP_IOCTL_FAST_DO_NOP) 449 if ( ( ulCmd == SUP_IOCTL_FAST_DO_RAW_RUN 450 || ulCmd == SUP_IOCTL_FAST_DO_HM_RUN 451 || ulCmd == SUP_IOCTL_FAST_DO_NOP) 452 && pSession->fUnrestricted == true) 360 453 { 361 454 #ifdef VBOX_WITH_VMMR0_DISABLE_PREEMPTION … … 475 568 NTSTATUS _stdcall VBoxDrvNtInternalDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp) 476 569 { 477 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;478 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);570 PSUPDRVDEVEXT pDevExt = SUPDRVNT_GET_DEVEXT(pDevObj); 571 PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp); 479 572 PFILE_OBJECT pFileObj = pStack ? pStack->FileObject : NULL; 480 573 PSUPDRVSESSION pSession = pFileObj ? (PSUPDRVSESSION)pFileObj->FsContext : NULL; … … 566 659 { 567 660 Log(("VBoxDrvNtNotSupportedStub\n")); 568 pDevObj = pDevObj;661 NOREF(pDevObj); 569 662 570 663 pIrp->IoStatus.Information = 0;
Note:
See TracChangeset
for help on using the changeset viewer.