VirtualBox

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

Last change on this file since 48392 was 47537, checked in by vboxsync, 11 years ago

HostDrivers/Support: Introduce reference counting for the session object to keep it alive while there is someone accessing it, fixes R0 assertion on OS X when killing the VM process

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.4 KB
Line 
1/* $Id: SUPDrv-darwin.cpp 47537 2013-08-05 10:00:02Z 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
527 if (RT_LIKELY(pSession))
528 supdrvSessionRetain(pSession);
529 RTSpinlockReleaseNoInts(g_Spinlock);
530 if (!pSession)
531 {
532 OSDBGPRINT(("VBoxDrvDarwinIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#lx\n",
533 (int)Process, iCmd));
534 return EINVAL;
535 }
536
537 /*
538 * Deal with the two high-speed IOCtl that takes it's arguments from
539 * the session and iCmd, and only returns a VBox status code.
540 */
541 int rc;
542 if ( ( iCmd == SUP_IOCTL_FAST_DO_RAW_RUN
543 || iCmd == SUP_IOCTL_FAST_DO_HM_RUN
544 || iCmd == SUP_IOCTL_FAST_DO_NOP)
545 && fUnrestricted)
546 rc = supdrvIOCtlFast(iCmd, *(uint32_t *)pData, &g_DevExt, pSession);
547 else
548 rc = VBoxDrvDarwinIOCtlSlow(pSession, iCmd, pData, pProcess);
549
550 supdrvSessionRelease(pSession);
551 return rc;
552}
553
554
555/**
556 * Worker for VBoxDrvDarwinIOCtl that takes the slow IOCtl functions.
557 *
558 * @returns Darwin errno.
559 *
560 * @param pSession The session.
561 * @param iCmd The IOCtl command.
562 * @param pData Pointer to the kernel copy of the SUPDRVIOCTLDATA buffer.
563 * @param pProcess The calling process.
564 */
565static int VBoxDrvDarwinIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess)
566{
567 LogFlow(("VBoxDrvDarwinIOCtlSlow: pSession=%p iCmd=%p pData=%p pProcess=%p\n", pSession, iCmd, pData, pProcess));
568
569
570 /*
571 * Buffered or unbuffered?
572 */
573 PSUPREQHDR pHdr;
574 user_addr_t pUser = 0;
575 void *pvPageBuf = NULL;
576 uint32_t cbReq = IOCPARM_LEN(iCmd);
577 if ((IOC_DIRMASK & iCmd) == IOC_INOUT)
578 {
579 pHdr = (PSUPREQHDR)pData;
580 if (RT_UNLIKELY(cbReq < sizeof(*pHdr)))
581 {
582 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: cbReq=%#x < %#x; iCmd=%#lx\n", cbReq, (int)sizeof(*pHdr), iCmd));
583 return EINVAL;
584 }
585 if (RT_UNLIKELY((pHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
586 {
587 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: bad magic fFlags=%#x; iCmd=%#lx\n", pHdr->fFlags, iCmd));
588 return EINVAL;
589 }
590 if (RT_UNLIKELY( RT_MAX(pHdr->cbIn, pHdr->cbOut) != cbReq
591 || pHdr->cbIn < sizeof(*pHdr)
592 || pHdr->cbOut < sizeof(*pHdr)))
593 {
594 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: max(%#x,%#x) != %#x; iCmd=%#lx\n", pHdr->cbIn, pHdr->cbOut, cbReq, iCmd));
595 return EINVAL;
596 }
597 }
598 else if ((IOC_DIRMASK & iCmd) == IOC_VOID && !cbReq)
599 {
600 /*
601 * Get the header and figure out how much we're gonna have to read.
602 */
603 SUPREQHDR Hdr;
604 pUser = (user_addr_t)*(void **)pData;
605 int rc = copyin(pUser, &Hdr, sizeof(Hdr));
606 if (RT_UNLIKELY(rc))
607 {
608 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyin(%llx,Hdr,) -> %#x; iCmd=%#lx\n", (unsigned long long)pUser, rc, iCmd));
609 return rc;
610 }
611 if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
612 {
613 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: bad magic fFlags=%#x; iCmd=%#lx\n", Hdr.fFlags, iCmd));
614 return EINVAL;
615 }
616 cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
617 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
618 || Hdr.cbOut < sizeof(Hdr)
619 || cbReq > _1M*16))
620 {
621 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: max(%#x,%#x); iCmd=%#lx\n", Hdr.cbIn, Hdr.cbOut, iCmd));
622 return EINVAL;
623 }
624
625 /*
626 * Allocate buffer and copy in the data.
627 */
628 pHdr = (PSUPREQHDR)RTMemTmpAlloc(cbReq);
629 if (!pHdr)
630 pvPageBuf = pHdr = (PSUPREQHDR)IOMallocAligned(RT_ALIGN_Z(cbReq, PAGE_SIZE), 8);
631 if (RT_UNLIKELY(!pHdr))
632 {
633 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: failed to allocate buffer of %d bytes; iCmd=%#lx\n", cbReq, iCmd));
634 return ENOMEM;
635 }
636 rc = copyin(pUser, pHdr, Hdr.cbIn);
637 if (RT_UNLIKELY(rc))
638 {
639 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyin(%llx,%p,%#x) -> %#x; iCmd=%#lx\n",
640 (unsigned long long)pUser, pHdr, Hdr.cbIn, rc, iCmd));
641 if (pvPageBuf)
642 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
643 else
644 RTMemTmpFree(pHdr);
645 return rc;
646 }
647 }
648 else
649 {
650 Log(("VBoxDrvDarwinIOCtlSlow: huh? cbReq=%#x iCmd=%#lx\n", cbReq, iCmd));
651 return EINVAL;
652 }
653
654 /*
655 * Process the IOCtl.
656 */
657 int rc = supdrvIOCtl(iCmd, &g_DevExt, pSession, pHdr);
658 if (RT_LIKELY(!rc))
659 {
660 /*
661 * If not buffered, copy back the buffer before returning.
662 */
663 if (pUser)
664 {
665 uint32_t cbOut = pHdr->cbOut;
666 if (cbOut > cbReq)
667 {
668 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: too much output! %#x > %#x; uCmd=%#lx!\n", cbOut, cbReq, iCmd));
669 cbOut = cbReq;
670 }
671 rc = copyout(pHdr, pUser, cbOut);
672 if (RT_UNLIKELY(rc))
673 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyout(%p,%llx,%#x) -> %d; uCmd=%#lx!\n",
674 pHdr, (unsigned long long)pUser, cbOut, rc, iCmd));
675
676 /* cleanup */
677 if (pvPageBuf)
678 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
679 else
680 RTMemTmpFree(pHdr);
681 }
682 }
683 else
684 {
685 /*
686 * The request failed, just clean up.
687 */
688 if (pUser)
689 {
690 if (pvPageBuf)
691 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
692 else
693 RTMemTmpFree(pHdr);
694 }
695
696 Log(("VBoxDrvDarwinIOCtlSlow: pid=%d iCmd=%lx pData=%p failed, rc=%d\n", proc_pid(pProcess), iCmd, (void *)pData, rc));
697 rc = EINVAL;
698 }
699
700 Log2(("VBoxDrvDarwinIOCtlSlow: returns %d\n", rc));
701 return rc;
702}
703
704
705/**
706 * The SUPDRV IDC entry point.
707 *
708 * @returns VBox status code, see supdrvIDC.
709 * @param iReq The request code.
710 * @param pReq The request.
711 */
712DECLEXPORT(int) VBOXCALL SUPDrvDarwinIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
713{
714 PSUPDRVSESSION pSession;
715
716 /*
717 * Some quick validations.
718 */
719 if (RT_UNLIKELY(!VALID_PTR(pReq)))
720 return VERR_INVALID_POINTER;
721
722 pSession = pReq->pSession;
723 if (pSession)
724 {
725 if (RT_UNLIKELY(!VALID_PTR(pSession)))
726 return VERR_INVALID_PARAMETER;
727 if (RT_UNLIKELY(pSession->pDevExt != &g_DevExt))
728 return VERR_INVALID_PARAMETER;
729 }
730 else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
731 return VERR_INVALID_PARAMETER;
732
733 /*
734 * Do the job.
735 */
736 return supdrvIDC(uReq, &g_DevExt, pSession, pReq);
737}
738
739
740/**
741 * Initializes any OS specific object creator fields.
742 */
743void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
744{
745 NOREF(pObj);
746 NOREF(pSession);
747}
748
749
750/**
751 * Checks if the session can access the object.
752 *
753 * @returns true if a decision has been made.
754 * @returns false if the default access policy should be applied.
755 *
756 * @param pObj The object in question.
757 * @param pSession The session wanting to access the object.
758 * @param pszObjName The object name, can be NULL.
759 * @param prc Where to store the result when returning true.
760 */
761bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
762{
763 NOREF(pObj);
764 NOREF(pSession);
765 NOREF(pszObjName);
766 NOREF(prc);
767 return false;
768}
769
770/**
771 * Callback for blah blah blah.
772 */
773IOReturn VBoxDrvDarwinSleepHandler(void * /* pvTarget */, void *pvRefCon, UInt32 uMessageType, IOService * /* pProvider */, void * /* pvMessageArgument */, vm_size_t /* argSize */)
774{
775 LogFlow(("VBoxDrv: Got sleep/wake notice. Message type was %X\n", (uint)uMessageType));
776
777 if (uMessageType == kIOMessageSystemWillSleep)
778 RTPowerSignalEvent(RTPOWEREVENT_SUSPEND);
779 else if (uMessageType == kIOMessageSystemHasPoweredOn)
780 RTPowerSignalEvent(RTPOWEREVENT_RESUME);
781
782 acknowledgeSleepWakeNotification(pvRefCon);
783
784 return 0;
785}
786
787
788/**
789 * @copydoc SUPR0EnableVTx
790 */
791int VBOXCALL supdrvOSEnableVTx(bool fEnable)
792{
793#ifdef VBOX_WITH_HOST_VMX
794 int rc;
795 if ( version_major >= 10 /* 10 = 10.6.x = Snow Leopard */
796 && g_pfnVmxSuspend
797 && g_pfnVmxResume
798 && g_pVmxUseCount)
799 {
800 if (fEnable)
801 {
802 rc = host_vmxon(false /* exclusive */);
803 if (rc == VMX_OK)
804 rc = VINF_SUCCESS;
805 else if (rc == VMX_UNSUPPORTED)
806 rc = VERR_VMX_NO_VMX;
807 else if (rc == VMX_INUSE)
808 rc = VERR_VMX_IN_VMX_ROOT_MODE;
809 else /* shouldn't happen, but just in case. */
810 {
811 LogRel(("host_vmxon returned %d\n", rc));
812 rc = VERR_UNRESOLVED_ERROR;
813 }
814 LogRel(("VBoxDrv: host_vmxon -> vmx_use_count=%d rc=%Rrc\n", *g_pVmxUseCount, rc));
815 }
816 else
817 {
818 host_vmxoff();
819 rc = VINF_SUCCESS;
820 LogRel(("VBoxDrv: host_vmxoff -> vmx_use_count=%d\n", *g_pVmxUseCount));
821 }
822 }
823 else
824 {
825 /* In 10.5.x the host_vmxon is severely broken! Don't use it, it will
826 frequnetly panic the host. */
827 rc = VERR_NOT_SUPPORTED;
828 }
829 return rc;
830#else
831 return VERR_NOT_SUPPORTED;
832#endif
833}
834
835
836/**
837 * @copydoc SUPR0SuspendVTxOnCpu
838 */
839bool VBOXCALL supdrvOSSuspendVTxOnCpu(void)
840{
841#ifdef VBOX_WITH_HOST_VMX
842 /*
843 * Consult the VMX usage counter, don't try suspend if not enabled.
844 *
845 * Note! The host_vmxon/off code is still race prone since, but this is
846 * currently the best we can do without always enable VMX when
847 * loading the driver.
848 */
849 if ( g_pVmxUseCount
850 && *g_pVmxUseCount > 0)
851 {
852 g_pfnVmxSuspend();
853 return true;
854 }
855 return false;
856#else
857 return false;
858#endif
859}
860
861
862/**
863 * @copydoc SUPR0ResumeVTxOnCpu
864 */
865void VBOXCALL supdrvOSResumeVTxOnCpu(bool fSuspended)
866{
867#ifdef VBOX_WITH_HOST_VMX
868 /*
869 * Don't consult the counter here, the state knows better.
870 * We're executing with interrupts disabled and anyone racing us with
871 * disabling VT-x will be waiting in the rendezvous code.
872 */
873 if ( fSuspended
874 && g_pfnVmxResume)
875 g_pfnVmxResume();
876 else
877 Assert(!fSuspended);
878#else
879 Assert(!fSuspended);
880#endif
881}
882
883
884bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
885{
886 NOREF(pDevExt);
887 return false;
888}
889
890
891void VBOXCALL supdrvOSLdrNotifyOpened(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
892{
893#if 1
894 NOREF(pDevExt); NOREF(pImage);
895#else
896 /*
897 * Try store the image load address in NVRAM so we can retrived it on panic.
898 * Note! This only works if you're root! - Acutally, it doesn't work at all at the moment. FIXME!
899 */
900 IORegistryEntry *pEntry = IORegistryEntry::fromPath("/options", gIODTPlane);
901 if (pEntry)
902 {
903 char szVar[80];
904 RTStrPrintf(szVar, sizeof(szVar), "vboximage"/*-%s*/, pImage->szName);
905 char szValue[48];
906 RTStrPrintf(szValue, sizeof(szValue), "%#llx,%#llx", (uint64_t)(uintptr_t)pImage->pvImage,
907 (uint64_t)(uintptr_t)pImage->pvImage + pImage->cbImageBits - 1);
908 bool fRc = pEntry->setProperty(szVar, szValue); NOREF(fRc);
909 pEntry->release();
910 SUPR0Printf("fRc=%d '%s'='%s'\n", fRc, szVar, szValue);
911 }
912 /*else
913 SUPR0Printf("failed to find /options in gIODTPlane\n");*/
914#endif
915}
916
917
918int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
919{
920 NOREF(pDevExt); NOREF(pImage); NOREF(pszFilename);
921 return VERR_NOT_SUPPORTED;
922}
923
924
925int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
926{
927 NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
928 return VERR_NOT_SUPPORTED;
929}
930
931
932int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
933{
934 NOREF(pDevExt); NOREF(pImage); NOREF(pbImageBits); NOREF(pReq);
935 return VERR_NOT_SUPPORTED;
936}
937
938
939void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
940{
941 NOREF(pDevExt); NOREF(pImage);
942}
943
944
945/**
946 * Converts an IPRT error code to a darwin error code.
947 *
948 * @returns corresponding darwin error code.
949 * @param rc IPRT status code.
950 */
951static int VBoxDrvDarwinErr2DarwinErr(int rc)
952{
953 switch (rc)
954 {
955 case VINF_SUCCESS: return 0;
956 case VERR_GENERAL_FAILURE: return EACCES;
957 case VERR_INVALID_PARAMETER: return EINVAL;
958 case VERR_INVALID_MAGIC: return EILSEQ;
959 case VERR_INVALID_HANDLE: return ENXIO;
960 case VERR_INVALID_POINTER: return EFAULT;
961 case VERR_LOCK_FAILED: return ENOLCK;
962 case VERR_ALREADY_LOADED: return EEXIST;
963 case VERR_PERMISSION_DENIED: return EPERM;
964 case VERR_VERSION_MISMATCH: return ENOSYS;
965 }
966
967 return EPERM;
968}
969
970
971RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
972{
973 va_list va;
974 char szMsg[512];
975
976 va_start(va, pszFormat);
977 RTStrPrintfV(szMsg, sizeof(szMsg) - 1, pszFormat, va);
978 va_end(va);
979 szMsg[sizeof(szMsg) - 1] = '\0';
980
981 printf("%s", szMsg);
982 return 0;
983}
984
985
986/*
987 *
988 * org_virtualbox_SupDrv
989 *
990 */
991
992
993/**
994 * Initialize the object.
995 */
996bool org_virtualbox_SupDrv::init(OSDictionary *pDictionary)
997{
998 LogFlow(("org_virtualbox_SupDrv::init([%p], %p)\n", this, pDictionary));
999 if (IOService::init(pDictionary))
1000 {
1001 /* init members. */
1002 return true;
1003 }
1004 return false;
1005}
1006
1007
1008/**
1009 * Free the object.
1010 */
1011void org_virtualbox_SupDrv::free(void)
1012{
1013 LogFlow(("IOService::free([%p])\n", this));
1014 IOService::free();
1015}
1016
1017
1018/**
1019 * Check if it's ok to start this service.
1020 * It's always ok by us, so it's up to IOService to decide really.
1021 */
1022IOService *org_virtualbox_SupDrv::probe(IOService *pProvider, SInt32 *pi32Score)
1023{
1024 LogFlow(("org_virtualbox_SupDrv::probe([%p])\n", this));
1025 return IOService::probe(pProvider, pi32Score);
1026}
1027
1028
1029/**
1030 * Start this service.
1031 */
1032bool org_virtualbox_SupDrv::start(IOService *pProvider)
1033{
1034 LogFlow(("org_virtualbox_SupDrv::start([%p])\n", this));
1035
1036 if (IOService::start(pProvider))
1037 {
1038 /* register the service. */
1039 registerService();
1040 return true;
1041 }
1042 return false;
1043}
1044
1045
1046/**
1047 * Stop this service.
1048 */
1049void org_virtualbox_SupDrv::stop(IOService *pProvider)
1050{
1051 LogFlow(("org_virtualbox_SupDrv::stop([%p], %p)\n", this, pProvider));
1052 IOService::stop(pProvider);
1053}
1054
1055
1056/**
1057 * Termination request.
1058 *
1059 * @return true if we're ok with shutting down now, false if we're not.
1060 * @param fOptions Flags.
1061 */
1062bool org_virtualbox_SupDrv::terminate(IOOptionBits fOptions)
1063{
1064 bool fRc;
1065 LogFlow(("org_virtualbox_SupDrv::terminate: reference_count=%d g_cSessions=%d (fOptions=%#x)\n",
1066 KMOD_INFO_NAME.reference_count, ASMAtomicUoReadS32(&g_cSessions), fOptions));
1067 if ( KMOD_INFO_NAME.reference_count != 0
1068 || ASMAtomicUoReadS32(&g_cSessions))
1069 fRc = false;
1070 else
1071 fRc = IOService::terminate(fOptions);
1072 LogFlow(("org_virtualbox_SupDrv::terminate: returns %d\n", fRc));
1073 return fRc;
1074}
1075
1076
1077/*
1078 *
1079 * org_virtualbox_SupDrvClient
1080 *
1081 */
1082
1083
1084/**
1085 * Initializer called when the client opens the service.
1086 */
1087bool org_virtualbox_SupDrvClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
1088{
1089 LogFlow(("org_virtualbox_SupDrvClient::initWithTask([%p], %#x, %p, %#x) (cur pid=%d proc=%p)\n",
1090 this, OwningTask, pvSecurityId, u32Type, RTProcSelf(), RTR0ProcHandleSelf()));
1091 AssertMsg((RTR0PROCESS)OwningTask == RTR0ProcHandleSelf(), ("%p %p\n", OwningTask, RTR0ProcHandleSelf()));
1092
1093 if (!OwningTask)
1094 return false;
1095 if (IOUserClient::initWithTask(OwningTask, pvSecurityId , u32Type))
1096 {
1097 m_Task = OwningTask;
1098 m_pSession = NULL;
1099 m_pProvider = NULL;
1100 return true;
1101 }
1102 return false;
1103}
1104
1105
1106/**
1107 * Start the client service.
1108 */
1109bool org_virtualbox_SupDrvClient::start(IOService *pProvider)
1110{
1111 LogFlow(("org_virtualbox_SupDrvClient::start([%p], %p) (cur pid=%d proc=%p)\n",
1112 this, pProvider, RTProcSelf(), RTR0ProcHandleSelf() ));
1113 AssertMsgReturn((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(),
1114 ("%p %p\n", m_Task, RTR0ProcHandleSelf()),
1115 false);
1116
1117 if (IOUserClient::start(pProvider))
1118 {
1119 m_pProvider = OSDynamicCast(org_virtualbox_SupDrv, pProvider);
1120 if (m_pProvider)
1121 {
1122 Assert(!m_pSession);
1123
1124 /*
1125 * Create a new session.
1126 */
1127 int rc = supdrvCreateSession(&g_DevExt, true /* fUser */, false /*fUnrestricted*/, &m_pSession);
1128 if (RT_SUCCESS(rc))
1129 {
1130 m_pSession->fOpened = false;
1131 /* The Uid, Gid and fUnrestricted fields are set on open. */
1132
1133 /*
1134 * Insert it into the hash table, checking that there isn't
1135 * already one for this process first. (One session per proc!)
1136 */
1137 unsigned iHash = SESSION_HASH(m_pSession->Process);
1138 RTSpinlockAcquire(g_Spinlock);
1139
1140 PSUPDRVSESSION pCur = g_apSessionHashTab[iHash];
1141 if (pCur && pCur->Process != m_pSession->Process)
1142 {
1143 do pCur = pCur->pNextHash;
1144 while (pCur && pCur->Process != m_pSession->Process);
1145 }
1146 if (!pCur)
1147 {
1148 m_pSession->pNextHash = g_apSessionHashTab[iHash];
1149 g_apSessionHashTab[iHash] = m_pSession;
1150 m_pSession->pvSupDrvClient = this;
1151 ASMAtomicIncS32(&g_cSessions);
1152 rc = VINF_SUCCESS;
1153 }
1154 else
1155 rc = VERR_ALREADY_LOADED;
1156
1157 RTSpinlockReleaseNoInts(g_Spinlock);
1158 if (RT_SUCCESS(rc))
1159 {
1160 Log(("org_virtualbox_SupDrvClient::start: created session %p for pid %d\n", m_pSession, (int)RTProcSelf()));
1161 return true;
1162 }
1163
1164 LogFlow(("org_virtualbox_SupDrvClient::start: already got a session for this process (%p)\n", pCur));
1165 supdrvSessionRelease(m_pSession);
1166 }
1167
1168 m_pSession = NULL;
1169 LogFlow(("org_virtualbox_SupDrvClient::start: rc=%Rrc from supdrvCreateSession\n", rc));
1170 }
1171 else
1172 LogFlow(("org_virtualbox_SupDrvClient::start: %p isn't org_virtualbox_SupDrv\n", pProvider));
1173 }
1174 return false;
1175}
1176
1177
1178/**
1179 * Common worker for clientClose and VBoxDrvDarwinClose.
1180 */
1181/* static */ void org_virtualbox_SupDrvClient::sessionClose(RTPROCESS Process)
1182{
1183 /*
1184 * Find the session and remove it from the hash table.
1185 *
1186 * Note! Only one session per process. (Both start() and
1187 * VBoxDrvDarwinOpen makes sure this is so.)
1188 */
1189 const unsigned iHash = SESSION_HASH(Process);
1190 RTSpinlockAcquire(g_Spinlock);
1191 PSUPDRVSESSION pSession = g_apSessionHashTab[iHash];
1192 if (pSession)
1193 {
1194 if (pSession->Process == Process)
1195 {
1196 g_apSessionHashTab[iHash] = pSession->pNextHash;
1197 pSession->pNextHash = NULL;
1198 ASMAtomicDecS32(&g_cSessions);
1199 }
1200 else
1201 {
1202 PSUPDRVSESSION pPrev = pSession;
1203 pSession = pSession->pNextHash;
1204 while (pSession)
1205 {
1206 if (pSession->Process == Process)
1207 {
1208 pPrev->pNextHash = pSession->pNextHash;
1209 pSession->pNextHash = NULL;
1210 ASMAtomicDecS32(&g_cSessions);
1211 break;
1212 }
1213
1214 /* next */
1215 pPrev = pSession;
1216 pSession = pSession->pNextHash;
1217 }
1218 }
1219 }
1220 RTSpinlockReleaseNoInts(g_Spinlock);
1221 if (!pSession)
1222 {
1223 Log(("SupDrvClient::sessionClose: pSession == NULL, pid=%d; freed already?\n", (int)Process));
1224 return;
1225 }
1226
1227 /*
1228 * Remove it from the client object.
1229 */
1230 org_virtualbox_SupDrvClient *pThis = (org_virtualbox_SupDrvClient *)pSession->pvSupDrvClient;
1231 pSession->pvSupDrvClient = NULL;
1232 if (pThis)
1233 {
1234 Assert(pThis->m_pSession == pSession);
1235 pThis->m_pSession = NULL;
1236 }
1237
1238 /*
1239 * Close the session.
1240 */
1241 supdrvSessionRelease(pSession);
1242}
1243
1244
1245/**
1246 * Client exits normally.
1247 */
1248IOReturn org_virtualbox_SupDrvClient::clientClose(void)
1249{
1250 LogFlow(("org_virtualbox_SupDrvClient::clientClose([%p]) (cur pid=%d proc=%p)\n", this, RTProcSelf(), RTR0ProcHandleSelf()));
1251 AssertMsg((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(), ("%p %p\n", m_Task, RTR0ProcHandleSelf()));
1252
1253 /*
1254 * Clean up the session if it's still around.
1255 *
1256 * We cannot rely 100% on close, and in the case of a dead client
1257 * we'll end up hanging inside vm_map_remove() if we postpone it.
1258 */
1259 if (m_pSession)
1260 {
1261 sessionClose(RTProcSelf());
1262 Assert(!m_pSession);
1263 }
1264
1265 m_pProvider = NULL;
1266 terminate();
1267
1268 return kIOReturnSuccess;
1269}
1270
1271
1272/**
1273 * The client exits abnormally / forgets to do cleanups. (logging)
1274 */
1275IOReturn org_virtualbox_SupDrvClient::clientDied(void)
1276{
1277 LogFlow(("org_virtualbox_SupDrvClient::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n",
1278 this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
1279
1280 /* IOUserClient::clientDied() calls clientClose, so we'll just do the work there. */
1281 return IOUserClient::clientDied();
1282}
1283
1284
1285/**
1286 * Terminate the service (initiate the destruction). (logging)
1287 */
1288bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
1289{
1290 LogFlow(("org_virtualbox_SupDrvClient::terminate([%p], %#x)\n", this, fOptions));
1291 return IOUserClient::terminate(fOptions);
1292}
1293
1294
1295/**
1296 * The final stage of the client service destruction. (logging)
1297 */
1298bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
1299{
1300 LogFlow(("org_virtualbox_SupDrvClient::finalize([%p], %#x)\n", this, fOptions));
1301 return IOUserClient::finalize(fOptions);
1302}
1303
1304
1305/**
1306 * Stop the client service. (logging)
1307 */
1308void org_virtualbox_SupDrvClient::stop(IOService *pProvider)
1309{
1310 LogFlow(("org_virtualbox_SupDrvClient::stop([%p])\n", this));
1311 IOUserClient::stop(pProvider);
1312}
1313
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