VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/darwin/SUPDrv-darwin.cpp@ 44571

Last change on this file since 44571 was 44173, checked in by vboxsync, 12 years ago

SUPDrv,SUPLib: Introducing /dev/vboxdrvu on darwin (other platforms soon to follow).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.2 KB
Line 
1/* $Id: SUPDrv-darwin.cpp 44173 2012-12-19 18:12:31Z vboxsync $ */
2/** @file
3 * VirtualBox Support Driver - Darwin Specific Code.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
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* Header Files *
29*******************************************************************************/
30#define LOG_GROUP LOG_GROUP_SUP_DRV
31/*
32 * Deal with conflicts first.
33 * PVM - BSD mess, that FreeBSD has correct a long time ago.
34 * iprt/types.h before sys/param.h - prevents UINT32_C and friends.
35 */
36#include <iprt/types.h>
37#include <sys/param.h>
38#undef PVM
39
40#include <IOKit/IOLib.h> /* Assert as function */
41
42#include "../SUPDrvInternal.h"
43#include <VBox/version.h>
44#include <iprt/asm.h>
45#include <iprt/asm-amd64-x86.h>
46#include <iprt/initterm.h>
47#include <iprt/assert.h>
48#include <iprt/spinlock.h>
49#include <iprt/semaphore.h>
50#include <iprt/process.h>
51#include <iprt/alloc.h>
52#include <iprt/power.h>
53#include <iprt/dbg.h>
54#include <VBox/err.h>
55#include <VBox/log.h>
56
57#include <mach/kmod.h>
58#include <miscfs/devfs/devfs.h>
59#include <sys/conf.h>
60#include <sys/errno.h>
61#include <sys/ioccom.h>
62#include <sys/malloc.h>
63#include <sys/proc.h>
64#include <sys/kauth.h>
65#include <IOKit/IOService.h>
66#include <IOKit/IOUserClient.h>
67#include <IOKit/pwr_mgt/RootDomain.h>
68#include <IOKit/IODeviceTreeSupport.h>
69
70#ifdef VBOX_WITH_HOST_VMX
71# include <libkern/version.h>
72RT_C_DECLS_BEGIN
73# include <i386/vmx.h>
74RT_C_DECLS_END
75#endif
76
77
78/*******************************************************************************
79* Defined Constants And Macros *
80*******************************************************************************/
81
82/** The system device node name. */
83#define DEVICE_NAME_SYS "vboxdrv"
84/** The user device node name. */
85#define DEVICE_NAME_USR "vboxdrvu"
86
87
88
89/*******************************************************************************
90* Internal Functions *
91*******************************************************************************/
92RT_C_DECLS_BEGIN
93static kern_return_t VBoxDrvDarwinStart(struct kmod_info *pKModInfo, void *pvData);
94static kern_return_t VBoxDrvDarwinStop(struct kmod_info *pKModInfo, void *pvData);
95
96static int VBoxDrvDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
97static int VBoxDrvDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
98static int VBoxDrvDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess);
99static int VBoxDrvDarwinIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess);
100
101static int VBoxDrvDarwinErr2DarwinErr(int rc);
102
103static IOReturn VBoxDrvDarwinSleepHandler(void *pvTarget, void *pvRefCon, UInt32 uMessageType, IOService *pProvider, void *pvMessageArgument, vm_size_t argSize);
104RT_C_DECLS_END
105
106static void vboxdrvDarwinResolveSymbols(void);
107
108
109/*******************************************************************************
110* Structures and Typedefs *
111*******************************************************************************/
112/**
113 * The service class.
114 * This is just a formality really.
115 */
116class org_virtualbox_SupDrv : public IOService
117{
118 OSDeclareDefaultStructors(org_virtualbox_SupDrv);
119
120public:
121 virtual bool init(OSDictionary *pDictionary = 0);
122 virtual void free(void);
123 virtual bool start(IOService *pProvider);
124 virtual void stop(IOService *pProvider);
125 virtual IOService *probe(IOService *pProvider, SInt32 *pi32Score);
126 virtual bool terminate(IOOptionBits fOptions);
127};
128
129OSDefineMetaClassAndStructors(org_virtualbox_SupDrv, IOService);
130
131
132/**
133 * An attempt at getting that clientDied() notification.
134 * I don't think it'll work as I cannot figure out where/what creates the correct
135 * port right.
136 */
137class org_virtualbox_SupDrvClient : public IOUserClient
138{
139 OSDeclareDefaultStructors(org_virtualbox_SupDrvClient);
140
141private:
142 PSUPDRVSESSION m_pSession; /**< The session. */
143 task_t m_Task; /**< The client task. */
144 org_virtualbox_SupDrv *m_pProvider; /**< The service provider. */
145
146public:
147 virtual bool initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type);
148 virtual bool start(IOService *pProvider);
149 static void sessionClose(RTPROCESS Process);
150 virtual IOReturn clientClose(void);
151 virtual IOReturn clientDied(void);
152 virtual bool terminate(IOOptionBits fOptions = 0);
153 virtual bool finalize(IOOptionBits fOptions);
154 virtual void stop(IOService *pProvider);
155};
156
157OSDefineMetaClassAndStructors(org_virtualbox_SupDrvClient, IOUserClient);
158
159
160
161/*******************************************************************************
162* Global Variables *
163*******************************************************************************/
164/**
165 * Declare the module stuff.
166 */
167RT_C_DECLS_BEGIN
168extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
169extern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
170
171KMOD_EXPLICIT_DECL(VBoxDrv, VBOX_VERSION_STRING, _start, _stop)
172DECLHIDDEN(kmod_start_func_t *) _realmain = VBoxDrvDarwinStart;
173DECLHIDDEN(kmod_stop_func_t *) _antimain = VBoxDrvDarwinStop;
174DECLHIDDEN(int) _kext_apple_cc = __APPLE_CC__;
175RT_C_DECLS_END
176
177
178/**
179 * Device extention & session data association structure.
180 */
181static SUPDRVDEVEXT g_DevExt;
182
183/**
184 * The character device switch table for the driver.
185 */
186static struct cdevsw g_DevCW =
187{
188 /** @todo g++ doesn't like this syntax - it worked with gcc before renaming to .cpp. */
189 /*.d_open = */VBoxDrvDarwinOpen,
190 /*.d_close = */VBoxDrvDarwinClose,
191 /*.d_read = */eno_rdwrt,
192 /*.d_write = */eno_rdwrt,
193 /*.d_ioctl = */VBoxDrvDarwinIOCtl,
194 /*.d_stop = */eno_stop,
195 /*.d_reset = */eno_reset,
196 /*.d_ttys = */NULL,
197 /*.d_select= */eno_select,
198 /*.d_mmap = */eno_mmap,
199 /*.d_strategy = */eno_strat,
200 /*.d_getc = */eno_getc,
201 /*.d_putc = */eno_putc,
202 /*.d_type = */0
203};
204
205/** Major device number. */
206static int g_iMajorDeviceNo = -1;
207/** Registered devfs device handle for the system device. */
208static void *g_hDevFsDeviceSys = NULL;
209/** Registered devfs device handle for the user device. */
210static void *g_hDevFsDeviceUsr = NULL;
211
212/** Spinlock protecting g_apSessionHashTab. */
213static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
214/** Hash table */
215static PSUPDRVSESSION g_apSessionHashTab[19];
216/** Calculates the index into g_apSessionHashTab.*/
217#define SESSION_HASH(pid) ((pid) % RT_ELEMENTS(g_apSessionHashTab))
218/** The number of open sessions. */
219static int32_t volatile g_cSessions = 0;
220/** The notifier handle for the sleep callback handler. */
221static IONotifier *g_pSleepNotifier = NULL;
222
223/** Pointer to vmx_suspend(). */
224static PFNRT g_pfnVmxSuspend = NULL;
225/** Pointer to vmx_resume(). */
226static PFNRT g_pfnVmxResume = NULL;
227/** Pointer to vmx_use_count. */
228static int volatile *g_pVmxUseCount = NULL;
229
230
231/**
232 * Start the kernel module.
233 */
234static kern_return_t VBoxDrvDarwinStart(struct kmod_info *pKModInfo, void *pvData)
235{
236 int rc;
237#ifdef DEBUG
238 printf("VBoxDrvDarwinStart\n");
239#endif
240
241 /*
242 * Initialize IPRT.
243 */
244 rc = RTR0Init(0);
245 if (RT_SUCCESS(rc))
246 {
247 /*
248 * Initialize the device extension.
249 */
250 rc = supdrvInitDevExt(&g_DevExt, sizeof(SUPDRVSESSION));
251 if (RT_SUCCESS(rc))
252 {
253 /*
254 * Initialize the session hash table.
255 */
256 memset(g_apSessionHashTab, 0, sizeof(g_apSessionHashTab)); /* paranoia */
257 rc = RTSpinlockCreate(&g_Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxDrvDarwin");
258 if (RT_SUCCESS(rc))
259 {
260 /*
261 * Registering ourselves as a character device.
262 */
263 g_iMajorDeviceNo = cdevsw_add(-1, &g_DevCW);
264 if (g_iMajorDeviceNo >= 0)
265 {
266#ifdef VBOX_WITH_HARDENING
267 g_hDevFsDeviceSys = devfs_make_node(makedev(g_iMajorDeviceNo, 0), DEVFS_CHAR,
268 UID_ROOT, GID_WHEEL, 0600, DEVICE_NAME_SYS);
269#else
270 g_hDevFsDeviceSys = devfs_make_node(makedev(g_iMajorDeviceNo, 0), DEVFS_CHAR,
271 UID_ROOT, GID_WHEEL, 0666, DEVICE_NAME_SYS);
272#endif
273 if (g_hDevFsDeviceSys)
274 {
275 g_hDevFsDeviceUsr = devfs_make_node(makedev(g_iMajorDeviceNo, 1), DEVFS_CHAR,
276 UID_ROOT, GID_WHEEL, 0666, DEVICE_NAME_USR);
277 if (g_hDevFsDeviceUsr)
278 {
279 LogRel(("VBoxDrv: version " VBOX_VERSION_STRING " r%d; IOCtl version %#x; IDC version %#x; dev major=%d\n",
280 VBOX_SVN_REV, SUPDRV_IOC_VERSION, SUPDRV_IDC_VERSION, g_iMajorDeviceNo));
281
282 /* Register a sleep/wakeup notification callback */
283 g_pSleepNotifier = registerPrioritySleepWakeInterest(&VBoxDrvDarwinSleepHandler, &g_DevExt, NULL);
284 if (g_pSleepNotifier == NULL)
285 LogRel(("VBoxDrv: register for sleep/wakeup events failed\n"));
286
287 /* Find kernel symbols that are kind of optional. */
288 vboxdrvDarwinResolveSymbols();
289 return KMOD_RETURN_SUCCESS;
290 }
291
292 LogRel(("VBoxDrv: devfs_make_node(makedev(%d,1),,,,%s) failed\n", g_iMajorDeviceNo, DEVICE_NAME_USR));
293 devfs_remove(g_hDevFsDeviceSys);
294 g_hDevFsDeviceSys = NULL;
295 }
296 else
297 LogRel(("VBoxDrv: devfs_make_node(makedev(%d,0),,,,%s) failed\n", g_iMajorDeviceNo, DEVICE_NAME_SYS));
298
299 cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
300 g_iMajorDeviceNo = -1;
301 }
302 else
303 LogRel(("VBoxDrv: cdevsw_add failed (%d)\n", g_iMajorDeviceNo));
304 RTSpinlockDestroy(g_Spinlock);
305 g_Spinlock = NIL_RTSPINLOCK;
306 }
307 else
308 LogRel(("VBoxDrv: RTSpinlockCreate failed (rc=%d)\n", rc));
309 supdrvDeleteDevExt(&g_DevExt);
310 }
311 else
312 printf("VBoxDrv: failed to initialize device extension (rc=%d)\n", rc);
313 RTR0TermForced();
314 }
315 else
316 printf("VBoxDrv: failed to initialize IPRT (rc=%d)\n", rc);
317
318 memset(&g_DevExt, 0, sizeof(g_DevExt));
319 return KMOD_RETURN_FAILURE;
320}
321
322
323/**
324 * Resolves kernel symbols we want (but may do without).
325 */
326static void vboxdrvDarwinResolveSymbols(void)
327{
328 RTDBGKRNLINFO hKrnlInfo;
329 int rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0);
330 if (RT_SUCCESS(rc))
331 {
332 /* The VMX stuff. */
333 int rc1 = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "vmx_resume", (void **)&g_pfnVmxResume);
334 int rc2 = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "vmx_suspend", (void **)&g_pfnVmxSuspend);
335 int rc3 = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "vmx_use_count", (void **)&g_pVmxUseCount);
336 if (RT_SUCCESS(rc1) && RT_SUCCESS(rc2) && RT_SUCCESS(rc3))
337 {
338 LogRel(("VBoxDrv: vmx_resume=%p vmx_suspend=%p vmx_use_count=%p (%d) cr4=%#x\n",
339 g_pfnVmxResume, g_pfnVmxSuspend, g_pVmxUseCount, *g_pVmxUseCount, ASMGetCR4() ));
340 }
341 else
342 {
343 LogRel(("VBoxDrv: failed to resolve vmx stuff: vmx_resume=%Rrc vmx_suspend=%Rrc vmx_use_count=%Rrc", rc1, rc2, rc3));
344 g_pfnVmxResume = NULL;
345 g_pfnVmxSuspend = NULL;
346 g_pVmxUseCount = NULL;
347 }
348
349 RTR0DbgKrnlInfoRelease(hKrnlInfo);
350 }
351 else
352 LogRel(("VBoxDrv: Failed to open kernel symbols, rc=%Rrc\n", rc));
353}
354
355
356/**
357 * Stop the kernel module.
358 */
359static kern_return_t VBoxDrvDarwinStop(struct kmod_info *pKModInfo, void *pvData)
360{
361 int rc;
362 LogFlow(("VBoxDrvDarwinStop\n"));
363
364 /** @todo I've got a nagging feeling that we'll have to keep track of users and refuse
365 * unloading if we're busy. Investigate and implement this! */
366
367 /*
368 * Undo the work done during start (in reverse order).
369 */
370 if (g_pSleepNotifier)
371 {
372 g_pSleepNotifier->remove();
373 g_pSleepNotifier = NULL;
374 }
375
376 devfs_remove(g_hDevFsDeviceUsr);
377 g_hDevFsDeviceUsr = NULL;
378
379 devfs_remove(g_hDevFsDeviceSys);
380 g_hDevFsDeviceSys = NULL;
381
382 rc = cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
383 Assert(rc == g_iMajorDeviceNo);
384 g_iMajorDeviceNo = -1;
385
386 supdrvDeleteDevExt(&g_DevExt);
387
388 rc = RTSpinlockDestroy(g_Spinlock);
389 AssertRC(rc);
390 g_Spinlock = NIL_RTSPINLOCK;
391
392 RTR0TermForced();
393
394 memset(&g_DevExt, 0, sizeof(g_DevExt));
395#ifdef DEBUG
396 printf("VBoxDrvDarwinStop - done\n");
397#endif
398 return KMOD_RETURN_SUCCESS;
399}
400
401
402/**
403 * Device open. Called on open /dev/vboxdrv
404 *
405 * @param Dev The device number.
406 * @param fFlags ???.
407 * @param fDevType ???.
408 * @param pProcess The process issuing this request.
409 */
410static int VBoxDrvDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
411{
412#ifdef DEBUG_DARWIN_GIP
413 char szName[128];
414 szName[0] = '\0';
415 proc_name(proc_pid(pProcess), szName, sizeof(szName));
416 Log(("VBoxDrvDarwinOpen: pid=%d '%s'\n", proc_pid(pProcess), szName));
417#endif
418
419 /*
420 * Only two minor devices numbers are allowed.
421 */
422 if (minor(Dev) != 0 && minor(Dev) != 1)
423 return EACCES;
424
425 /*
426 * Find the session created by org_virtualbox_SupDrvClient, fail
427 * if no such session, and mark it as opened. We set the uid & gid
428 * here too, since that is more straight forward at this point.
429 */
430 const bool fUnrestricted = minor(Dev) == 0;
431 int rc = VINF_SUCCESS;
432 PSUPDRVSESSION pSession = NULL;
433 kauth_cred_t pCred = kauth_cred_proc_ref(pProcess);
434 if (pCred)
435 {
436#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
437 RTUID Uid = kauth_cred_getruid(pCred);
438 RTGID Gid = kauth_cred_getrgid(pCred);
439#else
440 RTUID Uid = pCred->cr_ruid;
441 RTGID Gid = pCred->cr_rgid;
442#endif
443 RTPROCESS Process = RTProcSelf();
444 unsigned iHash = SESSION_HASH(Process);
445 RTSpinlockAcquire(g_Spinlock);
446
447 pSession = g_apSessionHashTab[iHash];
448 while (pSession && pSession->Process != Process)
449 pSession = pSession->pNextHash;
450 if (pSession)
451 {
452 if (!pSession->fOpened)
453 {
454 pSession->fOpened = true;
455 pSession->fUnrestricted = fUnrestricted;
456 pSession->Uid = Uid;
457 pSession->Gid = Gid;
458 }
459 else
460 rc = VERR_ALREADY_LOADED;
461 }
462 else
463 rc = VERR_GENERAL_FAILURE;
464
465 RTSpinlockReleaseNoInts(g_Spinlock);
466#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
467 kauth_cred_unref(&pCred);
468#else /* 10.4 */
469 /* The 10.4u SDK headers and 10.4.11 kernel source have inconsistent definitions
470 of kauth_cred_unref(), so use the other (now deprecated) API for releasing it. */
471 kauth_cred_rele(pCred);
472#endif /* 10.4 */
473 }
474 else
475 rc = VERR_INVALID_PARAMETER;
476
477#ifdef DEBUG_DARWIN_GIP
478 OSDBGPRINT(("VBoxDrvDarwinOpen: pid=%d '%s' pSession=%p rc=%d\n", proc_pid(pProcess), szName, pSession, rc));
479#else
480 Log(("VBoxDrvDarwinOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, proc_pid(pProcess)));
481#endif
482 return VBoxDrvDarwinErr2DarwinErr(rc);
483}
484
485
486/**
487 * Close device.
488 */
489static int VBoxDrvDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
490{
491 Log(("VBoxDrvDarwinClose: pid=%d\n", (int)RTProcSelf()));
492 Assert(proc_pid(pProcess) == (int)RTProcSelf());
493
494 /*
495 * Hand the session closing to org_virtualbox_SupDrvClient.
496 */
497 org_virtualbox_SupDrvClient::sessionClose(RTProcSelf());
498 return 0;
499}
500
501
502/**
503 * Device I/O Control entry point.
504 *
505 * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
506 * @param Dev The device number (major+minor).
507 * @param iCmd The IOCtl command.
508 * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
509 * @param fFlags Flag saying we're a character device (like we didn't know already).
510 * @param pProcess The process issuing this request.
511 */
512static int VBoxDrvDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
513{
514 const bool fUnrestricted = minor(Dev) == 0;
515 const RTPROCESS Process = proc_pid(pProcess);
516 const unsigned iHash = SESSION_HASH(Process);
517 PSUPDRVSESSION pSession;
518
519 /*
520 * Find the session.
521 */
522 RTSpinlockAcquire(g_Spinlock);
523 pSession = g_apSessionHashTab[iHash];
524 while (pSession && pSession->Process != Process && pSession->fUnrestricted == fUnrestricted && pSession->fOpened)
525 pSession = pSession->pNextHash;
526 RTSpinlockReleaseNoInts(g_Spinlock);
527 if (!pSession)
528 {
529 OSDBGPRINT(("VBoxDrvDarwinIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#lx\n",
530 (int)Process, iCmd));
531 return EINVAL;
532 }
533
534 /*
535 * Deal with the two high-speed IOCtl that takes it's arguments from
536 * the session and iCmd, and only returns a VBox status code.
537 */
538 if ( ( iCmd == SUP_IOCTL_FAST_DO_RAW_RUN
539 || iCmd == SUP_IOCTL_FAST_DO_HM_RUN
540 || iCmd == SUP_IOCTL_FAST_DO_NOP)
541 && fUnrestricted)
542 return supdrvIOCtlFast(iCmd, *(uint32_t *)pData, &g_DevExt, pSession);
543 return VBoxDrvDarwinIOCtlSlow(pSession, iCmd, pData, pProcess);
544}
545
546
547/**
548 * Worker for VBoxDrvDarwinIOCtl that takes the slow IOCtl functions.
549 *
550 * @returns Darwin errno.
551 *
552 * @param pSession The session.
553 * @param iCmd The IOCtl command.
554 * @param pData Pointer to the kernel copy of the SUPDRVIOCTLDATA buffer.
555 * @param pProcess The calling process.
556 */
557static int VBoxDrvDarwinIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess)
558{
559 LogFlow(("VBoxDrvDarwinIOCtlSlow: pSession=%p iCmd=%p pData=%p pProcess=%p\n", pSession, iCmd, pData, pProcess));
560
561
562 /*
563 * Buffered or unbuffered?
564 */
565 PSUPREQHDR pHdr;
566 user_addr_t pUser = 0;
567 void *pvPageBuf = NULL;
568 uint32_t cbReq = IOCPARM_LEN(iCmd);
569 if ((IOC_DIRMASK & iCmd) == IOC_INOUT)
570 {
571 pHdr = (PSUPREQHDR)pData;
572 if (RT_UNLIKELY(cbReq < sizeof(*pHdr)))
573 {
574 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: cbReq=%#x < %#x; iCmd=%#lx\n", cbReq, (int)sizeof(*pHdr), iCmd));
575 return EINVAL;
576 }
577 if (RT_UNLIKELY((pHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
578 {
579 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: bad magic fFlags=%#x; iCmd=%#lx\n", pHdr->fFlags, iCmd));
580 return EINVAL;
581 }
582 if (RT_UNLIKELY( RT_MAX(pHdr->cbIn, pHdr->cbOut) != cbReq
583 || pHdr->cbIn < sizeof(*pHdr)
584 || pHdr->cbOut < sizeof(*pHdr)))
585 {
586 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: max(%#x,%#x) != %#x; iCmd=%#lx\n", pHdr->cbIn, pHdr->cbOut, cbReq, iCmd));
587 return EINVAL;
588 }
589 }
590 else if ((IOC_DIRMASK & iCmd) == IOC_VOID && !cbReq)
591 {
592 /*
593 * Get the header and figure out how much we're gonna have to read.
594 */
595 SUPREQHDR Hdr;
596 pUser = (user_addr_t)*(void **)pData;
597 int rc = copyin(pUser, &Hdr, sizeof(Hdr));
598 if (RT_UNLIKELY(rc))
599 {
600 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyin(%llx,Hdr,) -> %#x; iCmd=%#lx\n", (unsigned long long)pUser, rc, iCmd));
601 return rc;
602 }
603 if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
604 {
605 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: bad magic fFlags=%#x; iCmd=%#lx\n", Hdr.fFlags, iCmd));
606 return EINVAL;
607 }
608 cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
609 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
610 || Hdr.cbOut < sizeof(Hdr)
611 || cbReq > _1M*16))
612 {
613 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: max(%#x,%#x); iCmd=%#lx\n", Hdr.cbIn, Hdr.cbOut, iCmd));
614 return EINVAL;
615 }
616
617 /*
618 * Allocate buffer and copy in the data.
619 */
620 pHdr = (PSUPREQHDR)RTMemTmpAlloc(cbReq);
621 if (!pHdr)
622 pvPageBuf = pHdr = (PSUPREQHDR)IOMallocAligned(RT_ALIGN_Z(cbReq, PAGE_SIZE), 8);
623 if (RT_UNLIKELY(!pHdr))
624 {
625 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: failed to allocate buffer of %d bytes; iCmd=%#lx\n", cbReq, iCmd));
626 return ENOMEM;
627 }
628 rc = copyin(pUser, pHdr, Hdr.cbIn);
629 if (RT_UNLIKELY(rc))
630 {
631 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyin(%llx,%p,%#x) -> %#x; iCmd=%#lx\n",
632 (unsigned long long)pUser, pHdr, Hdr.cbIn, rc, iCmd));
633 if (pvPageBuf)
634 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
635 else
636 RTMemTmpFree(pHdr);
637 return rc;
638 }
639 }
640 else
641 {
642 Log(("VBoxDrvDarwinIOCtlSlow: huh? cbReq=%#x iCmd=%#lx\n", cbReq, iCmd));
643 return EINVAL;
644 }
645
646 /*
647 * Process the IOCtl.
648 */
649 int rc = supdrvIOCtl(iCmd, &g_DevExt, pSession, pHdr);
650 if (RT_LIKELY(!rc))
651 {
652 /*
653 * If not buffered, copy back the buffer before returning.
654 */
655 if (pUser)
656 {
657 uint32_t cbOut = pHdr->cbOut;
658 if (cbOut > cbReq)
659 {
660 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: too much output! %#x > %#x; uCmd=%#lx!\n", cbOut, cbReq, iCmd));
661 cbOut = cbReq;
662 }
663 rc = copyout(pHdr, pUser, cbOut);
664 if (RT_UNLIKELY(rc))
665 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyout(%p,%llx,%#x) -> %d; uCmd=%#lx!\n",
666 pHdr, (unsigned long long)pUser, cbOut, rc, iCmd));
667
668 /* cleanup */
669 if (pvPageBuf)
670 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
671 else
672 RTMemTmpFree(pHdr);
673 }
674 }
675 else
676 {
677 /*
678 * The request failed, just clean up.
679 */
680 if (pUser)
681 {
682 if (pvPageBuf)
683 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
684 else
685 RTMemTmpFree(pHdr);
686 }
687
688 Log(("VBoxDrvDarwinIOCtlSlow: pid=%d iCmd=%lx pData=%p failed, rc=%d\n", proc_pid(pProcess), iCmd, (void *)pData, rc));
689 rc = EINVAL;
690 }
691
692 Log2(("VBoxDrvDarwinIOCtlSlow: returns %d\n", rc));
693 return rc;
694}
695
696
697/**
698 * The SUPDRV IDC entry point.
699 *
700 * @returns VBox status code, see supdrvIDC.
701 * @param iReq The request code.
702 * @param pReq The request.
703 */
704int VBOXCALL SUPDrvDarwinIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
705{
706 PSUPDRVSESSION pSession;
707
708 /*
709 * Some quick validations.
710 */
711 if (RT_UNLIKELY(!VALID_PTR(pReq)))
712 return VERR_INVALID_POINTER;
713
714 pSession = pReq->pSession;
715 if (pSession)
716 {
717 if (RT_UNLIKELY(!VALID_PTR(pSession)))
718 return VERR_INVALID_PARAMETER;
719 if (RT_UNLIKELY(pSession->pDevExt != &g_DevExt))
720 return VERR_INVALID_PARAMETER;
721 }
722 else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
723 return VERR_INVALID_PARAMETER;
724
725 /*
726 * Do the job.
727 */
728 return supdrvIDC(uReq, &g_DevExt, pSession, pReq);
729}
730
731
732/**
733 * Initializes any OS specific object creator fields.
734 */
735void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
736{
737 NOREF(pObj);
738 NOREF(pSession);
739}
740
741
742/**
743 * Checks if the session can access the object.
744 *
745 * @returns true if a decision has been made.
746 * @returns false if the default access policy should be applied.
747 *
748 * @param pObj The object in question.
749 * @param pSession The session wanting to access the object.
750 * @param pszObjName The object name, can be NULL.
751 * @param prc Where to store the result when returning true.
752 */
753bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
754{
755 NOREF(pObj);
756 NOREF(pSession);
757 NOREF(pszObjName);
758 NOREF(prc);
759 return false;
760}
761
762/**
763 * Callback for blah blah blah.
764 */
765IOReturn VBoxDrvDarwinSleepHandler(void * /* pvTarget */, void *pvRefCon, UInt32 uMessageType, IOService * /* pProvider */, void * /* pvMessageArgument */, vm_size_t /* argSize */)
766{
767 LogFlow(("VBoxDrv: Got sleep/wake notice. Message type was %X\n", (uint)uMessageType));
768
769 if (uMessageType == kIOMessageSystemWillSleep)
770 RTPowerSignalEvent(RTPOWEREVENT_SUSPEND);
771 else if (uMessageType == kIOMessageSystemHasPoweredOn)
772 RTPowerSignalEvent(RTPOWEREVENT_RESUME);
773
774 acknowledgeSleepWakeNotification(pvRefCon);
775
776 return 0;
777}
778
779
780/**
781 * @copydoc SUPR0EnableVTx
782 */
783int VBOXCALL supdrvOSEnableVTx(bool fEnable)
784{
785#ifdef VBOX_WITH_HOST_VMX
786 int rc;
787 if ( version_major >= 10 /* 10 = 10.6.x = Snow Leopard */
788 && g_pfnVmxSuspend
789 && g_pfnVmxResume
790 && g_pVmxUseCount)
791 {
792 if (fEnable)
793 {
794 rc = host_vmxon(false /* exclusive */);
795 if (rc == VMX_OK)
796 rc = VINF_SUCCESS;
797 else if (rc == VMX_UNSUPPORTED)
798 rc = VERR_VMX_NO_VMX;
799 else if (rc == VMX_INUSE)
800 rc = VERR_VMX_IN_VMX_ROOT_MODE;
801 else /* shouldn't happen, but just in case. */
802 {
803 LogRel(("host_vmxon returned %d\n", rc));
804 rc = VERR_UNRESOLVED_ERROR;
805 }
806 LogRel(("VBoxDrv: host_vmxon -> vmx_use_count=%d rc=%Rrc\n", *g_pVmxUseCount, rc));
807 }
808 else
809 {
810 host_vmxoff();
811 rc = VINF_SUCCESS;
812 LogRel(("VBoxDrv: host_vmxoff -> vmx_use_count=%d\n", *g_pVmxUseCount));
813 }
814 }
815 else
816 {
817 /* In 10.5.x the host_vmxon is severely broken! Don't use it, it will
818 frequnetly panic the host. */
819 rc = VERR_NOT_SUPPORTED;
820 }
821 return rc;
822#else
823 return VERR_NOT_SUPPORTED;
824#endif
825}
826
827
828/**
829 * @copydoc SUPR0SuspendVTxOnCpu
830 */
831bool VBOXCALL supdrvOSSuspendVTxOnCpu(void)
832{
833#ifdef VBOX_WITH_HOST_VMX
834 /*
835 * Consult the VMX usage counter, don't try suspend if not enabled.
836 *
837 * Note! The host_vmxon/off code is still race prone since, but this is
838 * currently the best we can do without always enable VMX when
839 * loading the driver.
840 */
841 if ( g_pVmxUseCount
842 && *g_pVmxUseCount > 0)
843 {
844 g_pfnVmxSuspend();
845 return true;
846 }
847 return false;
848#else
849 return false;
850#endif
851}
852
853
854/**
855 * @copydoc SUPR0ResumeVTxOnCpu
856 */
857void VBOXCALL supdrvOSResumeVTxOnCpu(bool fSuspended)
858{
859#ifdef VBOX_WITH_HOST_VMX
860 /*
861 * Don't consult the counter here, the state knows better.
862 * We're executing with interrupts disabled and anyone racing us with
863 * disabling VT-x will be waiting in the rendezvous code.
864 */
865 if ( fSuspended
866 && g_pfnVmxResume)
867 g_pfnVmxResume();
868 else
869 Assert(!fSuspended);
870#else
871 Assert(!fSuspended);
872#endif
873}
874
875
876bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
877{
878 NOREF(pDevExt);
879 return false;
880}
881
882
883void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
884{
885#if 1
886 NOREF(pDevExt); NOREF(pImage);
887#else
888 /*
889 * Try store the image load address in NVRAM so we can retrived it on panic.
890 * Note! This only works if you're root! - Acutally, it doesn't work at all at the moment. FIXME!
891 */
892 IORegistryEntry *pEntry = IORegistryEntry::fromPath("/options", gIODTPlane);
893 if (pEntry)
894 {
895 char szVar[80];
896 RTStrPrintf(szVar, sizeof(szVar), "vboximage"/*-%s*/, pImage->szName);
897 char szValue[48];
898 RTStrPrintf(szValue, sizeof(szValue), "%#llx,%#llx", (uint64_t)(uintptr_t)pImage->pvImage,
899 (uint64_t)(uintptr_t)pImage->pvImage + pImage->cbImageBits - 1);
900 bool fRc = pEntry->setProperty(szVar, szValue); NOREF(fRc);
901 pEntry->release();
902 SUPR0Printf("fRc=%d '%s'='%s'\n", fRc, szVar, szValue);
903 }
904 /*else
905 SUPR0Printf("failed to find /options in gIODTPlane\n");*/
906#endif
907}
908
909
910int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
911{
912 NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
913 return VERR_NOT_SUPPORTED;
914}
915
916
917int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
918{
919 NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
920 return VERR_NOT_SUPPORTED;
921}
922
923
924int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
925{
926 NOREF(pDevExt); NOREF(pImage); NOREF(pbImageBits); NOREF(pReq);
927 return VERR_NOT_SUPPORTED;
928}
929
930
931void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
932{
933 NOREF(pDevExt); NOREF(pImage);
934}
935
936
937/**
938 * Converts an IPRT error code to a darwin error code.
939 *
940 * @returns corresponding darwin error code.
941 * @param rc IPRT status code.
942 */
943static int VBoxDrvDarwinErr2DarwinErr(int rc)
944{
945 switch (rc)
946 {
947 case VINF_SUCCESS: return 0;
948 case VERR_GENERAL_FAILURE: return EACCES;
949 case VERR_INVALID_PARAMETER: return EINVAL;
950 case VERR_INVALID_MAGIC: return EILSEQ;
951 case VERR_INVALID_HANDLE: return ENXIO;
952 case VERR_INVALID_POINTER: return EFAULT;
953 case VERR_LOCK_FAILED: return ENOLCK;
954 case VERR_ALREADY_LOADED: return EEXIST;
955 case VERR_PERMISSION_DENIED: return EPERM;
956 case VERR_VERSION_MISMATCH: return ENOSYS;
957 }
958
959 return EPERM;
960}
961
962
963RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
964{
965 va_list va;
966 char szMsg[512];
967
968 va_start(va, pszFormat);
969 RTStrPrintfV(szMsg, sizeof(szMsg) - 1, pszFormat, va);
970 va_end(va);
971 szMsg[sizeof(szMsg) - 1] = '\0';
972
973 printf("%s", szMsg);
974 return 0;
975}
976
977
978/*
979 *
980 * org_virtualbox_SupDrv
981 *
982 */
983
984
985/**
986 * Initialize the object.
987 */
988bool org_virtualbox_SupDrv::init(OSDictionary *pDictionary)
989{
990 LogFlow(("org_virtualbox_SupDrv::init([%p], %p)\n", this, pDictionary));
991 if (IOService::init(pDictionary))
992 {
993 /* init members. */
994 return true;
995 }
996 return false;
997}
998
999
1000/**
1001 * Free the object.
1002 */
1003void org_virtualbox_SupDrv::free(void)
1004{
1005 LogFlow(("IOService::free([%p])\n", this));
1006 IOService::free();
1007}
1008
1009
1010/**
1011 * Check if it's ok to start this service.
1012 * It's always ok by us, so it's up to IOService to decide really.
1013 */
1014IOService *org_virtualbox_SupDrv::probe(IOService *pProvider, SInt32 *pi32Score)
1015{
1016 LogFlow(("org_virtualbox_SupDrv::probe([%p])\n", this));
1017 return IOService::probe(pProvider, pi32Score);
1018}
1019
1020
1021/**
1022 * Start this service.
1023 */
1024bool org_virtualbox_SupDrv::start(IOService *pProvider)
1025{
1026 LogFlow(("org_virtualbox_SupDrv::start([%p])\n", this));
1027
1028 if (IOService::start(pProvider))
1029 {
1030 /* register the service. */
1031 registerService();
1032 return true;
1033 }
1034 return false;
1035}
1036
1037
1038/**
1039 * Stop this service.
1040 */
1041void org_virtualbox_SupDrv::stop(IOService *pProvider)
1042{
1043 LogFlow(("org_virtualbox_SupDrv::stop([%p], %p)\n", this, pProvider));
1044 IOService::stop(pProvider);
1045}
1046
1047
1048/**
1049 * Termination request.
1050 *
1051 * @return true if we're ok with shutting down now, false if we're not.
1052 * @param fOptions Flags.
1053 */
1054bool org_virtualbox_SupDrv::terminate(IOOptionBits fOptions)
1055{
1056 bool fRc;
1057 LogFlow(("org_virtualbox_SupDrv::terminate: reference_count=%d g_cSessions=%d (fOptions=%#x)\n",
1058 KMOD_INFO_NAME.reference_count, ASMAtomicUoReadS32(&g_cSessions), fOptions));
1059 if ( KMOD_INFO_NAME.reference_count != 0
1060 || ASMAtomicUoReadS32(&g_cSessions))
1061 fRc = false;
1062 else
1063 fRc = IOService::terminate(fOptions);
1064 LogFlow(("org_virtualbox_SupDrv::terminate: returns %d\n", fRc));
1065 return fRc;
1066}
1067
1068
1069/*
1070 *
1071 * org_virtualbox_SupDrvClient
1072 *
1073 */
1074
1075
1076/**
1077 * Initializer called when the client opens the service.
1078 */
1079bool org_virtualbox_SupDrvClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
1080{
1081 LogFlow(("org_virtualbox_SupDrvClient::initWithTask([%p], %#x, %p, %#x) (cur pid=%d proc=%p)\n",
1082 this, OwningTask, pvSecurityId, u32Type, RTProcSelf(), RTR0ProcHandleSelf()));
1083 AssertMsg((RTR0PROCESS)OwningTask == RTR0ProcHandleSelf(), ("%p %p\n", OwningTask, RTR0ProcHandleSelf()));
1084
1085 if (!OwningTask)
1086 return false;
1087 if (IOUserClient::initWithTask(OwningTask, pvSecurityId , u32Type))
1088 {
1089 m_Task = OwningTask;
1090 m_pSession = NULL;
1091 m_pProvider = NULL;
1092 return true;
1093 }
1094 return false;
1095}
1096
1097
1098/**
1099 * Start the client service.
1100 */
1101bool org_virtualbox_SupDrvClient::start(IOService *pProvider)
1102{
1103 LogFlow(("org_virtualbox_SupDrvClient::start([%p], %p) (cur pid=%d proc=%p)\n",
1104 this, pProvider, RTProcSelf(), RTR0ProcHandleSelf() ));
1105 AssertMsgReturn((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(),
1106 ("%p %p\n", m_Task, RTR0ProcHandleSelf()),
1107 false);
1108
1109 if (IOUserClient::start(pProvider))
1110 {
1111 m_pProvider = OSDynamicCast(org_virtualbox_SupDrv, pProvider);
1112 if (m_pProvider)
1113 {
1114 Assert(!m_pSession);
1115
1116 /*
1117 * Create a new session.
1118 */
1119 int rc = supdrvCreateSession(&g_DevExt, true /* fUser */, false /*fUnrestricted*/, &m_pSession);
1120 if (RT_SUCCESS(rc))
1121 {
1122 m_pSession->fOpened = false;
1123 /* The Uid, Gid and fUnrestricted fields are set on open. */
1124
1125 /*
1126 * Insert it into the hash table, checking that there isn't
1127 * already one for this process first. (One session per proc!)
1128 */
1129 unsigned iHash = SESSION_HASH(m_pSession->Process);
1130 RTSpinlockAcquire(g_Spinlock);
1131
1132 PSUPDRVSESSION pCur = g_apSessionHashTab[iHash];
1133 if (pCur && pCur->Process != m_pSession->Process)
1134 {
1135 do pCur = pCur->pNextHash;
1136 while (pCur && pCur->Process != m_pSession->Process);
1137 }
1138 if (!pCur)
1139 {
1140 m_pSession->pNextHash = g_apSessionHashTab[iHash];
1141 g_apSessionHashTab[iHash] = m_pSession;
1142 m_pSession->pvSupDrvClient = this;
1143 ASMAtomicIncS32(&g_cSessions);
1144 rc = VINF_SUCCESS;
1145 }
1146 else
1147 rc = VERR_ALREADY_LOADED;
1148
1149 RTSpinlockReleaseNoInts(g_Spinlock);
1150 if (RT_SUCCESS(rc))
1151 {
1152 Log(("org_virtualbox_SupDrvClient::start: created session %p for pid %d\n", m_pSession, (int)RTProcSelf()));
1153 return true;
1154 }
1155
1156 LogFlow(("org_virtualbox_SupDrvClient::start: already got a session for this process (%p)\n", pCur));
1157 supdrvCloseSession(&g_DevExt, m_pSession);
1158 }
1159
1160 m_pSession = NULL;
1161 LogFlow(("org_virtualbox_SupDrvClient::start: rc=%Rrc from supdrvCreateSession\n", rc));
1162 }
1163 else
1164 LogFlow(("org_virtualbox_SupDrvClient::start: %p isn't org_virtualbox_SupDrv\n", pProvider));
1165 }
1166 return false;
1167}
1168
1169
1170/**
1171 * Common worker for clientClose and VBoxDrvDarwinClose.
1172 */
1173/* static */ void org_virtualbox_SupDrvClient::sessionClose(RTPROCESS Process)
1174{
1175 /*
1176 * Find the session and remove it from the hash table.
1177 *
1178 * Note! Only one session per process. (Both start() and
1179 * VBoxDrvDarwinOpen makes sure this is so.)
1180 */
1181 const unsigned iHash = SESSION_HASH(Process);
1182 RTSpinlockAcquire(g_Spinlock);
1183 PSUPDRVSESSION pSession = g_apSessionHashTab[iHash];
1184 if (pSession)
1185 {
1186 if (pSession->Process == Process)
1187 {
1188 g_apSessionHashTab[iHash] = pSession->pNextHash;
1189 pSession->pNextHash = NULL;
1190 ASMAtomicDecS32(&g_cSessions);
1191 }
1192 else
1193 {
1194 PSUPDRVSESSION pPrev = pSession;
1195 pSession = pSession->pNextHash;
1196 while (pSession)
1197 {
1198 if (pSession->Process == Process)
1199 {
1200 pPrev->pNextHash = pSession->pNextHash;
1201 pSession->pNextHash = NULL;
1202 ASMAtomicDecS32(&g_cSessions);
1203 break;
1204 }
1205
1206 /* next */
1207 pPrev = pSession;
1208 pSession = pSession->pNextHash;
1209 }
1210 }
1211 }
1212 RTSpinlockReleaseNoInts(g_Spinlock);
1213 if (!pSession)
1214 {
1215 Log(("SupDrvClient::sessionClose: pSession == NULL, pid=%d; freed already?\n", (int)Process));
1216 return;
1217 }
1218
1219 /*
1220 * Remove it from the client object.
1221 */
1222 org_virtualbox_SupDrvClient *pThis = (org_virtualbox_SupDrvClient *)pSession->pvSupDrvClient;
1223 pSession->pvSupDrvClient = NULL;
1224 if (pThis)
1225 {
1226 Assert(pThis->m_pSession == pSession);
1227 pThis->m_pSession = NULL;
1228 }
1229
1230 /*
1231 * Close the session.
1232 */
1233 supdrvCloseSession(&g_DevExt, pSession);
1234}
1235
1236
1237/**
1238 * Client exits normally.
1239 */
1240IOReturn org_virtualbox_SupDrvClient::clientClose(void)
1241{
1242 LogFlow(("org_virtualbox_SupDrvClient::clientClose([%p]) (cur pid=%d proc=%p)\n", this, RTProcSelf(), RTR0ProcHandleSelf()));
1243 AssertMsg((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(), ("%p %p\n", m_Task, RTR0ProcHandleSelf()));
1244
1245 /*
1246 * Clean up the session if it's still around.
1247 *
1248 * We cannot rely 100% on close, and in the case of a dead client
1249 * we'll end up hanging inside vm_map_remove() if we postpone it.
1250 */
1251 if (m_pSession)
1252 {
1253 sessionClose(RTProcSelf());
1254 Assert(!m_pSession);
1255 }
1256
1257 m_pProvider = NULL;
1258 terminate();
1259
1260 return kIOReturnSuccess;
1261}
1262
1263
1264/**
1265 * The client exits abnormally / forgets to do cleanups. (logging)
1266 */
1267IOReturn org_virtualbox_SupDrvClient::clientDied(void)
1268{
1269 LogFlow(("org_virtualbox_SupDrvClient::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n",
1270 this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
1271
1272 /* IOUserClient::clientDied() calls clientClose, so we'll just do the work there. */
1273 return IOUserClient::clientDied();
1274}
1275
1276
1277/**
1278 * Terminate the service (initiate the destruction). (logging)
1279 */
1280bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
1281{
1282 LogFlow(("org_virtualbox_SupDrvClient::terminate([%p], %#x)\n", this, fOptions));
1283 return IOUserClient::terminate(fOptions);
1284}
1285
1286
1287/**
1288 * The final stage of the client service destruction. (logging)
1289 */
1290bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
1291{
1292 LogFlow(("org_virtualbox_SupDrvClient::finalize([%p], %#x)\n", this, fOptions));
1293 return IOUserClient::finalize(fOptions);
1294}
1295
1296
1297/**
1298 * Stop the client service. (logging)
1299 */
1300void org_virtualbox_SupDrvClient::stop(IOService *pProvider)
1301{
1302 LogFlow(("org_virtualbox_SupDrvClient::stop([%p])\n", this));
1303 IOUserClient::stop(pProvider);
1304}
1305
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette