1 | /** $Id: SUPLib-win.cpp 5021 2007-09-25 13:32:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Support Library - Windows NT specific parts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
23 | #include <Windows.h>
|
---|
24 |
|
---|
25 | #include <VBox/sup.h>
|
---|
26 | #include <VBox/types.h>
|
---|
27 | #include <VBox/err.h>
|
---|
28 | #include <VBox/param.h>
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include "SUPLibInternal.h"
|
---|
34 | #include "SUPDRVIOC.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | /*******************************************************************************
|
---|
38 | * Defined Constants And Macros *
|
---|
39 | *******************************************************************************/
|
---|
40 | /** The support service name. */
|
---|
41 | #define SERVICE_NAME "VBoxDrv"
|
---|
42 | /** Win32 Device name. */
|
---|
43 | #define DEVICE_NAME "\\\\.\\VBoxDrv"
|
---|
44 | /** NT Device name. */
|
---|
45 | #define DEVICE_NAME_NT L"\\Device\\VBoxDrv"
|
---|
46 | /** Win32 Symlink name. */
|
---|
47 | #define DEVICE_NAME_DOS L"\\DosDevices\\VBoxDrv"
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 | /*******************************************************************************
|
---|
52 | * Global Variables *
|
---|
53 | *******************************************************************************/
|
---|
54 | /** Handle to the open device. */
|
---|
55 | static HANDLE g_hDevice = INVALID_HANDLE_VALUE;
|
---|
56 | /** Flags whether or not we started the service. */
|
---|
57 | static bool g_fStartedService = false;
|
---|
58 | /** Pointer to the area of memory we reserve for SUPPageAlloc(). */
|
---|
59 | static void *g_pvReserved = NULL;
|
---|
60 | /** The number of bytes we reserved for SUPPageAlloc(). */
|
---|
61 | static size_t g_cbReserved = 0;
|
---|
62 |
|
---|
63 |
|
---|
64 | /*******************************************************************************
|
---|
65 | * Internal Functions *
|
---|
66 | *******************************************************************************/
|
---|
67 | static int suplibOsCreateService(void);
|
---|
68 | static int suplibOsUpdateService(void);
|
---|
69 | static int suplibOsDeleteService(void);
|
---|
70 | static int suplibOsStartService(void);
|
---|
71 | static int suplibOsStopService(void);
|
---|
72 | static int suplibConvertWin32Err(int);
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Initialize the OS specific part of the library.
|
---|
77 | * On Win32 this involves:
|
---|
78 | * - registering the device driver
|
---|
79 | * - start device driver.
|
---|
80 | * - open driver.
|
---|
81 | *
|
---|
82 | * @returns 0 on success.
|
---|
83 | * @returns current -1 on failure but this must be changed to proper error codes.
|
---|
84 | * @param cbReserve The number of bytes to reserver for contiguous virtual allocations.
|
---|
85 | */
|
---|
86 | int suplibOsInit(size_t cbReserve)
|
---|
87 | {
|
---|
88 | /*
|
---|
89 | * Check if already initialized.
|
---|
90 | */
|
---|
91 | if (g_hDevice != INVALID_HANDLE_VALUE)
|
---|
92 | return 0;
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Try open the device.
|
---|
96 | */
|
---|
97 | g_hDevice = CreateFile(DEVICE_NAME,
|
---|
98 | GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
99 | NULL,
|
---|
100 | OPEN_EXISTING,
|
---|
101 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
|
---|
102 | NULL);
|
---|
103 | if (g_hDevice == INVALID_HANDLE_VALUE)
|
---|
104 | {
|
---|
105 | /*
|
---|
106 | * Try start the service and retry opening it.
|
---|
107 | */
|
---|
108 | suplibOsStartService();
|
---|
109 | g_hDevice = CreateFile(DEVICE_NAME,
|
---|
110 | GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
111 | NULL,
|
---|
112 | OPEN_EXISTING,
|
---|
113 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
|
---|
114 | NULL);
|
---|
115 | if (g_hDevice == INVALID_HANDLE_VALUE)
|
---|
116 | {
|
---|
117 | int rc = GetLastError();
|
---|
118 | switch (rc)
|
---|
119 | {
|
---|
120 | /** @todo someone must test what is actually returned. */
|
---|
121 | case ERROR_DEV_NOT_EXIST:
|
---|
122 | case ERROR_DEVICE_NOT_CONNECTED:
|
---|
123 | case ERROR_BAD_DEVICE:
|
---|
124 | case ERROR_DEVICE_REMOVED:
|
---|
125 | case ERROR_DEVICE_NOT_AVAILABLE:
|
---|
126 | return VERR_VM_DRIVER_LOAD_ERROR;
|
---|
127 | case ERROR_PATH_NOT_FOUND:
|
---|
128 | case ERROR_FILE_NOT_FOUND:
|
---|
129 | return VERR_VM_DRIVER_NOT_INSTALLED;
|
---|
130 | case ERROR_ACCESS_DENIED:
|
---|
131 | case ERROR_SHARING_VIOLATION:
|
---|
132 | return VERR_VM_DRIVER_NOT_ACCESSIBLE;
|
---|
133 | default:
|
---|
134 | return VERR_VM_DRIVER_OPEN_ERROR;
|
---|
135 | }
|
---|
136 |
|
---|
137 | return -1 /** @todo define proper error codes for suplibOsInit failure. */;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Check driver version.
|
---|
143 | */
|
---|
144 | /** @todo implement driver version checking. */
|
---|
145 |
|
---|
146 | #if 0 /* obsolete code and restricts our virtual address space for the new allocation method */
|
---|
147 | /*
|
---|
148 | * Reserve memory.
|
---|
149 | */
|
---|
150 | if (cbReserve != 0)
|
---|
151 | {
|
---|
152 | /** 1 1/2 GB - (a bit more than) current VBox max. */
|
---|
153 | #define SUPLIB_MAX_RESERVE (_1G + _1M*512)
|
---|
154 | /*
|
---|
155 | * Find the right size to reserve.
|
---|
156 | */
|
---|
157 | if ( cbReserve == ~(size_t)0
|
---|
158 | || cbReserve > SUPLIB_MAX_RESERVE)
|
---|
159 | cbReserve = SUPLIB_MAX_RESERVE;
|
---|
160 | char szVar[64] = {0};
|
---|
161 | if (GetEnvironmentVariable("VBOX_RESERVE_MEM_LIMIT", szVar, sizeof(szVar) - 1))
|
---|
162 | {
|
---|
163 | uint64_t cb;
|
---|
164 | char *pszNext;
|
---|
165 | int rc = RTStrToUInt64Ex(szVar, &pszNext, 0, &cb);
|
---|
166 | if (VBOX_SUCCESS(rc))
|
---|
167 | {
|
---|
168 | switch (*pszNext)
|
---|
169 | {
|
---|
170 | case 'K':
|
---|
171 | case 'k':
|
---|
172 | cb *= _1K;
|
---|
173 | pszNext++;
|
---|
174 | break;
|
---|
175 | case 'M':
|
---|
176 | case 'm':
|
---|
177 | cb *= _1M;
|
---|
178 | pszNext++;
|
---|
179 | break;
|
---|
180 | case 'G':
|
---|
181 | case 'g':
|
---|
182 | cb *= _1G;
|
---|
183 | pszNext++;
|
---|
184 | break;
|
---|
185 | case '\0':
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | if (*pszNext == 'b' || *pszNext == 'B')
|
---|
189 | pszNext++;
|
---|
190 | if (!pszNext)
|
---|
191 | cbReserve = RT_MIN(SUPLIB_MAX_RESERVE, cb);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * Try reserve virtual address space, lowering the requirements in by _1M chunks.
|
---|
197 | * Make sure it's possible to get at least 3 chunks of 16MBs extra after the reservation.
|
---|
198 | */
|
---|
199 | for (cbReserve = RT_ALIGN_Z(cbReserve, _1M); cbReserve >= _1M * 64; cbReserve -= _1M)
|
---|
200 | {
|
---|
201 | void *pv = VirtualAlloc(NULL, cbReserve, MEM_RESERVE, PAGE_NOACCESS);
|
---|
202 | if (pv)
|
---|
203 | {
|
---|
204 | void *pv1 = VirtualAlloc(NULL, _1M * 16, MEM_RESERVE, PAGE_NOACCESS);
|
---|
205 | void *pv2 = VirtualAlloc(NULL, _1M * 16, MEM_RESERVE, PAGE_NOACCESS);
|
---|
206 | void *pv3 = VirtualAlloc(NULL, _1M * 16, MEM_RESERVE, PAGE_NOACCESS);
|
---|
207 | if (pv1)
|
---|
208 | VirtualFree(pv1, 0, MEM_RELEASE);
|
---|
209 | if (pv2)
|
---|
210 | VirtualFree(pv2, 0, MEM_RELEASE);
|
---|
211 | if (pv3)
|
---|
212 | VirtualFree(pv3, 0, MEM_RELEASE);
|
---|
213 | const int cFailures = !pv1 + !pv2 + !pv3;
|
---|
214 | if (!cFailures)
|
---|
215 | {
|
---|
216 | g_pvReserved = pv;
|
---|
217 | g_cbReserved = cbReserve;
|
---|
218 | #if 0 /* too early, no logging. */
|
---|
219 | Log(("suplibOsInit: Reserved %zu bytes at %p\n", cbReserve, g_pvReserved));
|
---|
220 | #endif
|
---|
221 | break;
|
---|
222 | }
|
---|
223 |
|
---|
224 | cbReserve -= cFailures > 2 ? _1M * 16 : _1M;
|
---|
225 | }
|
---|
226 | else
|
---|
227 | cbReserve -= _1M;
|
---|
228 | }
|
---|
229 | /* ignore errors */
|
---|
230 | }
|
---|
231 | #endif /* end of obsolete memory reservation hack */
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * We're done.
|
---|
235 | */
|
---|
236 | return VINF_SUCCESS;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Installs anything required by the support library.
|
---|
242 | *
|
---|
243 | * @returns 0 on success.
|
---|
244 | * @returns error code on failure.
|
---|
245 | */
|
---|
246 | int suplibOsInstall(void)
|
---|
247 | {
|
---|
248 | return suplibOsCreateService();
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Installs anything required by the support library.
|
---|
254 | *
|
---|
255 | * @returns 0 on success.
|
---|
256 | * @returns error code on failure.
|
---|
257 | */
|
---|
258 | int suplibOsUninstall(void)
|
---|
259 | {
|
---|
260 | int rc = suplibOsStopService();
|
---|
261 | if (!rc)
|
---|
262 | rc = suplibOsDeleteService();
|
---|
263 | return rc;
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Creates the service.
|
---|
269 | *
|
---|
270 | * @returns 0 on success.
|
---|
271 | * @returns -1 on failure.
|
---|
272 | */
|
---|
273 | int suplibOsCreateService(void)
|
---|
274 | {
|
---|
275 | /*
|
---|
276 | * Assume it didn't exist, so we'll create the service.
|
---|
277 | */
|
---|
278 | SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
279 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
280 | AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", LastError));
|
---|
281 | if (hSMgrCreate)
|
---|
282 | {
|
---|
283 | char szDriver[RTPATH_MAX];
|
---|
284 | int rc = RTPathProgram(szDriver, sizeof(szDriver) - sizeof("\\VBoxDrv.sys"));
|
---|
285 | if (VBOX_SUCCESS(rc))
|
---|
286 | {
|
---|
287 | strcat(szDriver, "\\VBoxDrv.sys");
|
---|
288 | SC_HANDLE hService = CreateService(hSMgrCreate,
|
---|
289 | SERVICE_NAME,
|
---|
290 | "VBox Support Driver",
|
---|
291 | SERVICE_QUERY_STATUS,
|
---|
292 | SERVICE_KERNEL_DRIVER,
|
---|
293 | SERVICE_DEMAND_START,
|
---|
294 | SERVICE_ERROR_NORMAL,
|
---|
295 | szDriver,
|
---|
296 | NULL, NULL, NULL, NULL, NULL);
|
---|
297 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
298 | AssertMsg(hService, ("CreateService failed! LastError=%Rwa szDriver=%s\n", LastError, szDriver));
|
---|
299 | CloseServiceHandle(hService);
|
---|
300 | CloseServiceHandle(hSMgrCreate);
|
---|
301 | return hService ? 0 : -1;
|
---|
302 | }
|
---|
303 | CloseServiceHandle(hSMgrCreate);
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 | return -1;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Stops a possibly running service.
|
---|
311 | *
|
---|
312 | * @returns 0 on success.
|
---|
313 | * @returns -1 on failure.
|
---|
314 | */
|
---|
315 | int suplibOsStopService(void)
|
---|
316 | {
|
---|
317 | /*
|
---|
318 | * Assume it didn't exist, so we'll create the service.
|
---|
319 | */
|
---|
320 | int rc = -1;
|
---|
321 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
|
---|
322 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
323 | AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
|
---|
324 | if (hSMgr)
|
---|
325 | {
|
---|
326 | SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS);
|
---|
327 | if (hService)
|
---|
328 | {
|
---|
329 | /*
|
---|
330 | * Stop the service.
|
---|
331 | */
|
---|
332 | SERVICE_STATUS Status;
|
---|
333 | QueryServiceStatus(hService, &Status);
|
---|
334 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
335 | rc = 0;
|
---|
336 | else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
|
---|
337 | {
|
---|
338 | int iWait = 100;
|
---|
339 | while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
|
---|
340 | {
|
---|
341 | Sleep(100);
|
---|
342 | QueryServiceStatus(hService, &Status);
|
---|
343 | }
|
---|
344 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
345 | rc = 0;
|
---|
346 | else
|
---|
347 | AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
|
---|
348 | }
|
---|
349 | else
|
---|
350 | {
|
---|
351 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
352 | AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
|
---|
353 | }
|
---|
354 | CloseServiceHandle(hService);
|
---|
355 | }
|
---|
356 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
357 | rc = 0;
|
---|
358 | else
|
---|
359 | {
|
---|
360 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
361 | AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
|
---|
362 | }
|
---|
363 | CloseServiceHandle(hSMgr);
|
---|
364 | }
|
---|
365 | return rc;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Deletes the service.
|
---|
371 | *
|
---|
372 | * @returns 0 on success.
|
---|
373 | * @returns -1 on failure.
|
---|
374 | */
|
---|
375 | int suplibOsDeleteService(void)
|
---|
376 | {
|
---|
377 | /*
|
---|
378 | * Assume it didn't exist, so we'll create the service.
|
---|
379 | */
|
---|
380 | int rc = -1;
|
---|
381 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
382 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
383 | AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
|
---|
384 | if (hSMgr)
|
---|
385 | {
|
---|
386 | SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE);
|
---|
387 | if (hService)
|
---|
388 | {
|
---|
389 | /*
|
---|
390 | * Delete the service.
|
---|
391 | */
|
---|
392 | if (DeleteService(hService))
|
---|
393 | rc = 0;
|
---|
394 | else
|
---|
395 | {
|
---|
396 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
397 | AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", LastError));
|
---|
398 | }
|
---|
399 | CloseServiceHandle(hService);
|
---|
400 | }
|
---|
401 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
402 | rc = 0;
|
---|
403 | else
|
---|
404 | {
|
---|
405 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
406 | AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
|
---|
407 | }
|
---|
408 | CloseServiceHandle(hSMgr);
|
---|
409 | }
|
---|
410 | return rc;
|
---|
411 | }
|
---|
412 |
|
---|
413 | #if 0
|
---|
414 | /**
|
---|
415 | * Creates the service.
|
---|
416 | *
|
---|
417 | * @returns 0 on success.
|
---|
418 | * @returns -1 on failure.
|
---|
419 | */
|
---|
420 | int suplibOsUpdateService(void)
|
---|
421 | {
|
---|
422 | /*
|
---|
423 | * Assume it didn't exist, so we'll create the service.
|
---|
424 | */
|
---|
425 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
426 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
427 | AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed LastError=%Rwa\n", LastError));
|
---|
428 | if (hSMgr)
|
---|
429 | {
|
---|
430 | SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_CHANGE_CONFIG);
|
---|
431 | if (hService)
|
---|
432 | {
|
---|
433 | char szDriver[RTPATH_MAX];
|
---|
434 | int rc = RTPathProgram(szDriver, sizeof(szDriver) - sizeof("\\VBoxDrv.sys"));
|
---|
435 | if (VBOX_SUCCESS(rc))
|
---|
436 | {
|
---|
437 | strcat(szDriver, "\\VBoxDrv.sys");
|
---|
438 |
|
---|
439 | SC_LOCK hLock = LockServiceDatabase(hSMgr);
|
---|
440 | if (ChangeServiceConfig(hService,
|
---|
441 | SERVICE_KERNEL_DRIVER,
|
---|
442 | SERVICE_DEMAND_START,
|
---|
443 | SERVICE_ERROR_NORMAL,
|
---|
444 | szDriver,
|
---|
445 | NULL, NULL, NULL, NULL, NULL, NULL))
|
---|
446 | {
|
---|
447 |
|
---|
448 | UnlockServiceDatabase(hLock);
|
---|
449 | CloseServiceHandle(hService);
|
---|
450 | CloseServiceHandle(hSMgr);
|
---|
451 | return 0;
|
---|
452 | }
|
---|
453 | else
|
---|
454 | {
|
---|
455 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
456 | AssertMsgFailed(("ChangeServiceConfig failed LastError=%Rwa\n", LastError));
|
---|
457 | }
|
---|
458 | }
|
---|
459 | UnlockServiceDatabase(hLock);
|
---|
460 | CloseServiceHandle(hService);
|
---|
461 | }
|
---|
462 | else
|
---|
463 | {
|
---|
464 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
465 | AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
|
---|
466 | }
|
---|
467 | CloseServiceHandle(hSMgr);
|
---|
468 | }
|
---|
469 | return -1;
|
---|
470 | }
|
---|
471 | #endif
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Attempts to start the service, creating it if necessary.
|
---|
475 | *
|
---|
476 | * @returns 0 on success.
|
---|
477 | * @returns -1 on failure.
|
---|
478 | * @param fRetry Indicates retry call.
|
---|
479 | */
|
---|
480 | int suplibOsStartService(void)
|
---|
481 | {
|
---|
482 | /*
|
---|
483 | * Check if the driver service is there.
|
---|
484 | */
|
---|
485 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_QUERY_STATUS | SERVICE_START);
|
---|
486 | if (hSMgr == NULL)
|
---|
487 | {
|
---|
488 | AssertMsgFailed(("couldn't open service manager in SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS mode!\n"));
|
---|
489 | return -1;
|
---|
490 | }
|
---|
491 |
|
---|
492 | /*
|
---|
493 | * Try open our service to check it's status.
|
---|
494 | */
|
---|
495 | SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_QUERY_STATUS | SERVICE_START);
|
---|
496 | if (!hService)
|
---|
497 | {
|
---|
498 | /*
|
---|
499 | * Create the service.
|
---|
500 | */
|
---|
501 | int rc = suplibOsCreateService();
|
---|
502 | if (rc)
|
---|
503 | return rc;
|
---|
504 |
|
---|
505 | /*
|
---|
506 | * Try open the service.
|
---|
507 | */
|
---|
508 | hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_QUERY_STATUS | SERVICE_START);
|
---|
509 | }
|
---|
510 |
|
---|
511 | /*
|
---|
512 | * Check if open and on demand create succeeded.
|
---|
513 | */
|
---|
514 | int rc = -1;
|
---|
515 | if (hService)
|
---|
516 | {
|
---|
517 |
|
---|
518 | /*
|
---|
519 | * Query service status to see if we need to start it or not.
|
---|
520 | */
|
---|
521 | SERVICE_STATUS Status;
|
---|
522 | BOOL fRc = QueryServiceStatus(hService, &Status);
|
---|
523 | Assert(fRc);
|
---|
524 | if ( Status.dwCurrentState != SERVICE_RUNNING
|
---|
525 | && Status.dwCurrentState != SERVICE_START_PENDING)
|
---|
526 | {
|
---|
527 | /*
|
---|
528 | * Start it.
|
---|
529 | */
|
---|
530 | fRc = StartService(hService, 0, NULL);
|
---|
531 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
532 | AssertMsg(fRc, ("StartService failed with LastError=%Rwa\n", LastError));
|
---|
533 | }
|
---|
534 |
|
---|
535 | /*
|
---|
536 | * Wait for the service to finish starting.
|
---|
537 | * We'll wait for 10 seconds then we'll give up.
|
---|
538 | */
|
---|
539 | QueryServiceStatus(hService, &Status);
|
---|
540 | if (Status.dwCurrentState == SERVICE_START_PENDING)
|
---|
541 | {
|
---|
542 | int iWait;
|
---|
543 | for (iWait = 100; iWait > 0 && Status.dwCurrentState == SERVICE_START_PENDING; iWait--)
|
---|
544 | {
|
---|
545 | Sleep(100);
|
---|
546 | QueryServiceStatus(hService, &Status);
|
---|
547 | }
|
---|
548 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
549 | AssertMsg(Status.dwCurrentState != SERVICE_RUNNING,
|
---|
550 | ("Failed to start. LastError=%Rwa iWait=%d status=%d\n",
|
---|
551 | LastError, iWait, Status.dwCurrentState));
|
---|
552 | }
|
---|
553 |
|
---|
554 | if (Status.dwCurrentState == SERVICE_RUNNING)
|
---|
555 | rc = 0;
|
---|
556 |
|
---|
557 | /*
|
---|
558 | * Close open handles.
|
---|
559 | */
|
---|
560 | CloseServiceHandle(hService);
|
---|
561 | }
|
---|
562 | else
|
---|
563 | {
|
---|
564 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
565 | AssertMsgFailed(("OpenService failed! LastError=%Rwa\n", LastError));
|
---|
566 | }
|
---|
567 | if (!CloseServiceHandle(hSMgr))
|
---|
568 | AssertFailed();
|
---|
569 |
|
---|
570 | return rc;
|
---|
571 | }
|
---|
572 |
|
---|
573 |
|
---|
574 | int suplibOsTerm(void)
|
---|
575 | {
|
---|
576 | /*
|
---|
577 | * Check if we're initited at all.
|
---|
578 | */
|
---|
579 | if (g_hDevice != INVALID_HANDLE_VALUE)
|
---|
580 | {
|
---|
581 | if (!CloseHandle(g_hDevice))
|
---|
582 | AssertFailed();
|
---|
583 | g_hDevice = INVALID_HANDLE_VALUE;
|
---|
584 | }
|
---|
585 |
|
---|
586 | /*
|
---|
587 | * If we started the service we might consider stopping it too.
|
---|
588 | *
|
---|
589 | * Since this won't work unless the the process starting it is the
|
---|
590 | * last user we might wanna skip this...
|
---|
591 | */
|
---|
592 | if (g_fStartedService)
|
---|
593 | {
|
---|
594 | suplibOsStopService();
|
---|
595 | g_fStartedService = false;
|
---|
596 | }
|
---|
597 |
|
---|
598 | return 0;
|
---|
599 | }
|
---|
600 |
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Send a I/O Control request to the device.
|
---|
604 | *
|
---|
605 | * @returns 0 on success.
|
---|
606 | * @returns VBOX error code on failure.
|
---|
607 | * @param uFunction IO Control function.
|
---|
608 | * @param pvIn The request buffer.
|
---|
609 | * @param cbReq The size of the request buffer.
|
---|
610 | */
|
---|
611 | int suplibOsIOCtl(uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
612 | {
|
---|
613 | AssertMsg(g_hDevice != INVALID_HANDLE_VALUE, ("SUPLIB not initiated successfully!\n"));
|
---|
614 |
|
---|
615 | /*
|
---|
616 | * Issue the device I/O control.
|
---|
617 | */
|
---|
618 | PSUPREQHDR pHdr = (PSUPREQHDR)pvReq;
|
---|
619 | Assert(cbReq == RT_MAX(pHdr->cbIn, pHdr->cbOut));
|
---|
620 | DWORD cbReturned = (ULONG)pHdr->cbOut;
|
---|
621 | if (DeviceIoControl(g_hDevice, uFunction, pvReq, pHdr->cbIn, pvReq, cbReturned, &cbReturned, NULL))
|
---|
622 | return 0;
|
---|
623 |
|
---|
624 | return suplibConvertWin32Err(GetLastError());
|
---|
625 | }
|
---|
626 |
|
---|
627 |
|
---|
628 | int suplibOsIOCtlFast(uintptr_t uFunction)
|
---|
629 | {
|
---|
630 | /*
|
---|
631 | * Issue device I/O control.
|
---|
632 | */
|
---|
633 | int rc = VERR_INTERNAL_ERROR;
|
---|
634 | DWORD cbReturned = (ULONG)sizeof(rc);
|
---|
635 | if (DeviceIoControl(g_hDevice, uFunction, NULL, 0, &rc, (DWORD)sizeof(rc), &cbReturned, NULL))
|
---|
636 | return rc;
|
---|
637 | return suplibConvertWin32Err(GetLastError());
|
---|
638 | }
|
---|
639 |
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * Allocate a number of zero-filled pages in user space.
|
---|
643 | *
|
---|
644 | * @returns VBox status code.
|
---|
645 | * @param cPages Number of pages to allocate.
|
---|
646 | * @param ppvPages Where to return the base pointer.
|
---|
647 | */
|
---|
648 | int suplibOsPageAlloc(size_t cPages, void **ppvPages)
|
---|
649 | {
|
---|
650 | if (g_pvReserved)
|
---|
651 | {
|
---|
652 | if (VirtualFree(g_pvReserved, 0, MEM_RELEASE))
|
---|
653 | Log(("suplibOsPageAlloc: Freed %zu bytes of reserved memory at %p.\n", g_cbReserved, g_pvReserved));
|
---|
654 | else
|
---|
655 | {
|
---|
656 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
657 | AssertMsgFailed(("LastError=%Rwa g_pvReserved=%p\n", LastError, g_pvReserved));
|
---|
658 | }
|
---|
659 | g_pvReserved = NULL;
|
---|
660 | }
|
---|
661 |
|
---|
662 | *ppvPages = VirtualAlloc(NULL, (size_t)cPages << PAGE_SHIFT, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
---|
663 | if (*ppvPages)
|
---|
664 | return VINF_SUCCESS;
|
---|
665 | return suplibConvertWin32Err(GetLastError());
|
---|
666 | }
|
---|
667 |
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * Frees pages allocated by suplibOsPageAlloc().
|
---|
671 | *
|
---|
672 | * @returns VBox status code.
|
---|
673 | * @param pvPages Pointer to pages.
|
---|
674 | */
|
---|
675 | int suplibOsPageFree(void *pvPages, size_t /* cPages */)
|
---|
676 | {
|
---|
677 | if (VirtualFree(pvPages, 0, MEM_RELEASE))
|
---|
678 | return VINF_SUCCESS;
|
---|
679 | return suplibConvertWin32Err(GetLastError());
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * Converts a supdrv error code to an nt status code.
|
---|
685 | *
|
---|
686 | * @returns corresponding SUPDRV_ERR_*.
|
---|
687 | * @param rc Win32 error code.
|
---|
688 | */
|
---|
689 | static int suplibConvertWin32Err(int rc)
|
---|
690 | {
|
---|
691 | /* Conversion program (link with ntdll.lib from ddk):
|
---|
692 | #define _WIN32_WINNT 0x0501
|
---|
693 | #include <windows.h>
|
---|
694 | #include <ntstatus.h>
|
---|
695 | #include <winternl.h>
|
---|
696 | #include <stdio.h>
|
---|
697 |
|
---|
698 | int main()
|
---|
699 | {
|
---|
700 | #define CONVERT(a) printf(#a " %#x -> %d\n", a, RtlNtStatusToDosError((a)))
|
---|
701 | CONVERT(STATUS_SUCCESS);
|
---|
702 | CONVERT(STATUS_NOT_SUPPORTED);
|
---|
703 | CONVERT(STATUS_INVALID_PARAMETER);
|
---|
704 | CONVERT(STATUS_UNKNOWN_REVISION);
|
---|
705 | CONVERT(STATUS_INVALID_HANDLE);
|
---|
706 | CONVERT(STATUS_INVALID_ADDRESS);
|
---|
707 | CONVERT(STATUS_NOT_LOCKED);
|
---|
708 | CONVERT(STATUS_IMAGE_ALREADY_LOADED);
|
---|
709 | CONVERT(STATUS_ACCESS_DENIED);
|
---|
710 | CONVERT(STATUS_REVISION_MISMATCH);
|
---|
711 |
|
---|
712 | return 0;
|
---|
713 | }
|
---|
714 | */
|
---|
715 |
|
---|
716 | switch (rc)
|
---|
717 | {
|
---|
718 | //case 0: return STATUS_SUCCESS;
|
---|
719 | case 0: return VINF_SUCCESS;
|
---|
720 | //case SUPDRV_ERR_GENERAL_FAILURE: return STATUS_NOT_SUPPORTED;
|
---|
721 | case ERROR_NOT_SUPPORTED: return VERR_GENERAL_FAILURE;
|
---|
722 | //case SUPDRV_ERR_INVALID_PARAM: return STATUS_INVALID_PARAMETER;
|
---|
723 | case ERROR_INVALID_PARAMETER: return VERR_INVALID_PARAMETER;
|
---|
724 | //case SUPDRV_ERR_INVALID_MAGIC: return STATUS_ACCESS_DENIED;
|
---|
725 | case ERROR_UNKNOWN_REVISION: return VERR_INVALID_MAGIC;
|
---|
726 | //case SUPDRV_ERR_INVALID_HANDLE: return STATUS_INVALID_HANDLE;
|
---|
727 | case ERROR_INVALID_HANDLE: return VERR_INVALID_HANDLE;
|
---|
728 | //case SUPDRV_ERR_INVALID_POINTER: return STATUS_INVALID_ADDRESS;
|
---|
729 | case ERROR_UNEXP_NET_ERR: return VERR_INVALID_POINTER;
|
---|
730 | //case SUPDRV_ERR_LOCK_FAILED: return STATUS_NOT_LOCKED;
|
---|
731 | case ERROR_NOT_LOCKED: return VERR_LOCK_FAILED;
|
---|
732 | //case SUPDRV_ERR_ALREADY_LOADED: return STATUS_IMAGE_ALREADY_LOADED;
|
---|
733 | case ERROR_SERVICE_ALREADY_RUNNING: return VERR_ALREADY_LOADED;
|
---|
734 | //case SUPDRV_ERR_PERMISSION_DENIED: return STATUS_ACCESS_DENIED;
|
---|
735 | case ERROR_ACCESS_DENIED: return VERR_PERMISSION_DENIED;
|
---|
736 | //case SUPDRV_ERR_VERSION_MISMATCH: return STATUS_REVISION_MISMATCH;
|
---|
737 | case ERROR_REVISION_MISMATCH: return VERR_VERSION_MISMATCH;
|
---|
738 | }
|
---|
739 |
|
---|
740 | /* fall back on the default conversion. */
|
---|
741 | return RTErrConvertFromWin32(rc);
|
---|
742 | }
|
---|
743 |
|
---|
744 |
|
---|
745 |
|
---|