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