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