VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-darwin.cpp@ 75713

Last change on this file since 75713 was 75712, checked in by vboxsync, 6 years ago

darwin/VBoxGuest: Kicked out the workloop and alternative interrupt code. bugref:4686

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 43.7 KB
Line 
1/* $Id: VBoxGuest-darwin.cpp 75712 2018-11-25 11:45:15Z vboxsync $ */
2/** @file
3 * VBoxGuest - Darwin Specifics.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP LOG_GROUP_VGDRV
32/*
33 * Deal with conflicts first.
34 * PVM - BSD mess, that FreeBSD has correct a long time ago.
35 * iprt/types.h before sys/param.h - prevents UINT32_C and friends.
36 */
37#include <iprt/types.h>
38#include <sys/param.h>
39#undef PVM
40
41#include <IOKit/IOLib.h> /* Assert as function */
42
43#include <VBox/version.h>
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46#include <iprt/initterm.h>
47#include <iprt/mem.h>
48#include <iprt/power.h>
49#include <iprt/process.h>
50#include <iprt/semaphore.h>
51#include <iprt/spinlock.h>
52#include <iprt/string.h>
53#include <VBox/err.h>
54#include <VBox/log.h>
55
56#include <mach/kmod.h>
57#include <miscfs/devfs/devfs.h>
58#include <sys/conf.h>
59#include <sys/errno.h>
60#include <sys/ioccom.h>
61#include <sys/malloc.h>
62#include <sys/proc.h>
63#include <sys/kauth.h>
64#include <IOKit/IOService.h>
65#include <IOKit/IOUserClient.h>
66#include <IOKit/pwr_mgt/RootDomain.h>
67#include <IOKit/pci/IOPCIDevice.h>
68#include <IOKit/IOBufferMemoryDescriptor.h>
69#include <IOKit/IOFilterInterruptEventSource.h>
70#include "VBoxGuestInternal.h"
71
72
73/*********************************************************************************************************************************
74* Defined Constants And Macros *
75*********************************************************************************************************************************/
76/** The system device node name. */
77#define DEVICE_NAME_SYS "vboxguest"
78/** The user device node name. */
79#define DEVICE_NAME_USR "vboxguestu"
80
81
82/** @name For debugging/whatever, now permanent.
83 * @{ */
84#define VBOX_PROC_SELFNAME_LEN 31
85#define VBOX_RETRIEVE_CUR_PROC_NAME(a_Name) char a_Name[VBOX_PROC_SELFNAME_LEN + 1]; \
86 proc_selfname(a_Name, VBOX_PROC_SELFNAME_LEN)
87/** @} */
88
89
90/*********************************************************************************************************************************
91* Internal Functions *
92*********************************************************************************************************************************/
93RT_C_DECLS_BEGIN
94static kern_return_t vgdrvDarwinStart(struct kmod_info *pKModInfo, void *pvData);
95static kern_return_t vgdrvDarwinStop(struct kmod_info *pKModInfo, void *pvData);
96static int vgdrvDarwinCharDevRemove(void);
97
98static int vgdrvDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
99static int vgdrvDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
100static int vgdrvDarwinIOCtlSlow(PVBOXGUESTSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess);
101static int vgdrvDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess);
102
103static int vgdrvDarwinErr2DarwinErr(int rc);
104
105static IOReturn vgdrvDarwinSleepHandler(void *pvTarget, void *pvRefCon, UInt32 uMessageType, IOService *pProvider, void *pvMessageArgument, vm_size_t argSize);
106RT_C_DECLS_END
107
108
109/*********************************************************************************************************************************
110* Structures and Typedefs *
111*********************************************************************************************************************************/
112/**
113 * The service class for handling the VMMDev PCI device.
114 *
115 * Instantiated when the module is loaded (and on PCI hotplugging?).
116 */
117class org_virtualbox_VBoxGuest : public IOService
118{
119 OSDeclareDefaultStructors(org_virtualbox_VBoxGuest);
120
121private:
122 IOPCIDevice *m_pIOPCIDevice;
123 IOMemoryMap *m_pMap;
124 IOFilterInterruptEventSource *m_pInterruptSrc;
125
126 bool setupVmmDevInterrupts(IOService *pProvider);
127 bool disableVmmDevInterrupts(void);
128 bool isVmmDev(IOPCIDevice *pIOPCIDevice);
129
130protected:
131 /** Non-NULL if interrupts are registered. Probably same as getProvider(). */
132 IOService *m_pInterruptProvider;
133
134public:
135 virtual bool init(OSDictionary *pDictionary = 0);
136 virtual void free(void);
137 virtual IOService *probe(IOService *pProvider, SInt32 *pi32Score);
138 virtual bool start(IOService *pProvider);
139 virtual void stop(IOService *pProvider);
140 virtual bool terminate(IOOptionBits fOptions);
141 static void vgdrvDarwinIrqHandler(OSObject *pTarget, void *pvRefCon, IOService *pNub, int iSrc);
142};
143
144OSDefineMetaClassAndStructors(org_virtualbox_VBoxGuest, IOService);
145
146
147/**
148 * An attempt at getting that clientDied() notification.
149 * I don't think it'll work as I cannot figure out where/what creates the correct
150 * port right.
151 *
152 * Instantiated when userland does IOServiceOpen().
153 */
154class org_virtualbox_VBoxGuestClient : public IOUserClient
155{
156 OSDeclareDefaultStructors(org_virtualbox_VBoxGuestClient);
157
158private:
159 /** Guard against the parent class growing and us using outdated headers. */
160 uint8_t m_abSafetyPadding[256];
161
162 PVBOXGUESTSESSION m_pSession; /**< The session. */
163 task_t m_Task; /**< The client task. */
164 org_virtualbox_VBoxGuest *m_pProvider; /**< The service provider. */
165
166public:
167 virtual bool initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type);
168 virtual bool start(IOService *pProvider);
169 static void sessionClose(RTPROCESS Process);
170 virtual IOReturn clientClose(void);
171 virtual IOReturn clientDied(void);
172 virtual bool terminate(IOOptionBits fOptions = 0);
173 virtual bool finalize(IOOptionBits fOptions);
174 virtual void stop(IOService *pProvider);
175
176 RTR0MEMEF_NEW_AND_DELETE_OPERATORS_IOKIT();
177};
178
179OSDefineMetaClassAndStructors(org_virtualbox_VBoxGuestClient, IOUserClient);
180
181
182
183/*********************************************************************************************************************************
184* Global Variables *
185*********************************************************************************************************************************/
186/**
187 * Declare the module stuff.
188 */
189RT_C_DECLS_BEGIN
190extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
191extern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
192
193KMOD_EXPLICIT_DECL(VBoxGuest, VBOX_VERSION_STRING, _start, _stop)
194DECLHIDDEN(kmod_start_func_t *) _realmain = vgdrvDarwinStart;
195DECLHIDDEN(kmod_stop_func_t *) _antimain = vgdrvDarwinStop;
196DECLHIDDEN(int) _kext_apple_cc = __APPLE_CC__;
197RT_C_DECLS_END
198
199
200/**
201 * Device extention & session data association structure.
202 */
203static VBOXGUESTDEVEXT g_DevExt;
204
205/**
206 * The character device switch table for the driver.
207 */
208static struct cdevsw g_DevCW =
209{
210 /*.d_open = */ vgdrvDarwinOpen,
211 /*.d_close = */ vgdrvDarwinClose,
212 /*.d_read = */ eno_rdwrt,
213 /*.d_write = */ eno_rdwrt,
214 /*.d_ioctl = */ vgdrvDarwinIOCtl,
215 /*.d_stop = */ eno_stop,
216 /*.d_reset = */ eno_reset,
217 /*.d_ttys = */ NULL,
218 /*.d_select = */ eno_select,
219 /*.d_mmap = */ eno_mmap,
220 /*.d_strategy = */ eno_strat,
221 /*.d_getc = */ (void *)(uintptr_t)&enodev, //eno_getc,
222 /*.d_putc = */ (void *)(uintptr_t)&enodev, //eno_putc,
223 /*.d_type = */ 0
224};
225
226/** Major device number. */
227static int g_iMajorDeviceNo = -1;
228/** Registered devfs device handle. */
229static void *g_hDevFsDeviceSys = NULL;
230/** Registered devfs device handle for the user device. */
231static void *g_hDevFsDeviceUsr = NULL; /**< @todo 4 later */
232
233/** Spinlock protecting g_apSessionHashTab. */
234static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
235/** Hash table */
236static PVBOXGUESTSESSION g_apSessionHashTab[19];
237/** Calculates the index into g_apSessionHashTab.*/
238#define SESSION_HASH(pid) ((pid) % RT_ELEMENTS(g_apSessionHashTab))
239/** The number of open sessions. */
240static int32_t volatile g_cSessions = 0;
241/** Makes sure there is only one org_virtualbox_VBoxGuest instance. */
242static bool volatile g_fInstantiated = 0;
243/** The notifier handle for the sleep callback handler. */
244static IONotifier *g_pSleepNotifier = NULL;
245
246
247/**
248 * Start the kernel module.
249 */
250static kern_return_t vgdrvDarwinStart(struct kmod_info *pKModInfo, void *pvData)
251{
252 RT_NOREF(pKModInfo, pvData);
253#ifdef DEBUG
254 printf("vgdrvDarwinStart\n");
255#endif
256#if 0
257 gIOKitDebug |= 0x001 //kIOLogAttach
258 | 0x002 //kIOLogProbe
259 | 0x004 //kIOLogStart
260 | 0x008 //kIOLogRegister
261 | 0x010 //kIOLogMatch
262 | 0x020 //kIOLogConfig
263 ;
264#endif
265
266 /*
267 * Initialize IPRT.
268 */
269 int rc = RTR0Init(0);
270 if (RT_SUCCESS(rc))
271 {
272 Log(("VBoxGuest: driver loaded\n"));
273 return KMOD_RETURN_SUCCESS;
274 }
275
276 RTLogBackdoorPrintf("VBoxGuest: RTR0Init failed with rc=%Rrc\n", rc);
277 printf("VBoxGuest: RTR0Init failed with rc=%d\n", rc);
278 return KMOD_RETURN_FAILURE;
279}
280
281
282/**
283 * Stop the kernel module.
284 */
285static kern_return_t vgdrvDarwinStop(struct kmod_info *pKModInfo, void *pvData)
286{
287 RT_NOREF(pKModInfo, pvData);
288
289 /** @todo we need to check for VBoxSF clients? */
290
291 RTLogBackdoorPrintf("VBoxGuest: calling RTR0TermForced ...\n");
292 RTR0TermForced();
293
294 RTLogBackdoorPrintf("VBoxGuest: vgdrvDarwinStop returns.\n");
295 printf("VBoxGuest: driver unloaded\n");
296 return KMOD_RETURN_SUCCESS;
297}
298
299
300/**
301 * Register VBoxGuest char device
302 */
303static int vgdrvDarwinCharDevInit(void)
304{
305 int rc = RTSpinlockCreate(&g_Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxGuestDarwin");
306 if (RT_SUCCESS(rc))
307 {
308 /*
309 * Registering ourselves as a character device.
310 */
311 g_iMajorDeviceNo = cdevsw_add(-1, &g_DevCW);
312 if (g_iMajorDeviceNo >= 0)
313 {
314 /** @todo limit /dev/vboxguest access. */
315 g_hDevFsDeviceSys = devfs_make_node(makedev(g_iMajorDeviceNo, 0), DEVFS_CHAR,
316 UID_ROOT, GID_WHEEL, 0666, DEVICE_NAME_SYS);
317 if (g_hDevFsDeviceSys != NULL)
318 {
319 /*
320 * And a all-user device.
321 */
322 g_hDevFsDeviceUsr = devfs_make_node(makedev(g_iMajorDeviceNo, 1), DEVFS_CHAR,
323 UID_ROOT, GID_WHEEL, 0666, DEVICE_NAME_USR);
324 if (g_hDevFsDeviceUsr != NULL)
325 {
326 /*
327 * Register a sleep/wakeup notification callback.
328 */
329 g_pSleepNotifier = registerPrioritySleepWakeInterest(&vgdrvDarwinSleepHandler, &g_DevExt, NULL);
330 if (g_pSleepNotifier != NULL)
331 return KMOD_RETURN_SUCCESS;
332 }
333 }
334 }
335 vgdrvDarwinCharDevRemove();
336 }
337 return KMOD_RETURN_FAILURE;
338}
339
340
341/**
342 * Unregister VBoxGuest char devices and associated session spinlock.
343 */
344static int vgdrvDarwinCharDevRemove(void)
345{
346 if (g_pSleepNotifier)
347 {
348 g_pSleepNotifier->remove();
349 g_pSleepNotifier = NULL;
350 }
351
352 if (g_hDevFsDeviceSys)
353 {
354 devfs_remove(g_hDevFsDeviceSys);
355 g_hDevFsDeviceSys = NULL;
356 }
357
358 if (g_hDevFsDeviceUsr)
359 {
360 devfs_remove(g_hDevFsDeviceUsr);
361 g_hDevFsDeviceUsr = NULL;
362 }
363
364 if (g_iMajorDeviceNo != -1)
365 {
366 int rc2 = cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
367 Assert(rc2 == g_iMajorDeviceNo); NOREF(rc2);
368 g_iMajorDeviceNo = -1;
369 }
370
371 if (g_Spinlock != NIL_RTSPINLOCK)
372 {
373 int rc2 = RTSpinlockDestroy(g_Spinlock); AssertRC(rc2);
374 g_Spinlock = NIL_RTSPINLOCK;
375 }
376
377 return KMOD_RETURN_SUCCESS;
378}
379
380
381/**
382 * Device open. Called on open /dev/vboxguest and (later) /dev/vboxguestu.
383 *
384 * @param Dev The device number.
385 * @param fFlags ???.
386 * @param fDevType ???.
387 * @param pProcess The process issuing this request.
388 */
389static int vgdrvDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
390{
391 RT_NOREF(fFlags, fDevType);
392
393 /*
394 * Only two minor devices numbers are allowed.
395 */
396 if (minor(Dev) != 0 && minor(Dev) != 1)
397 return EACCES;
398
399 /*
400 * The process issuing the request must be the current process.
401 */
402 RTPROCESS Process = RTProcSelf();
403 if ((int)Process != proc_pid(pProcess))
404 return EIO;
405
406 /*
407 * Find the session created by org_virtualbox_VBoxGuestClient, fail
408 * if no such session, and mark it as opened. We set the uid & gid
409 * here too, since that is more straight forward at this point.
410 */
411 const bool fUnrestricted = minor(Dev) == 0;
412 int rc = VINF_SUCCESS;
413 PVBOXGUESTSESSION pSession = NULL;
414 kauth_cred_t pCred = kauth_cred_proc_ref(pProcess);
415 if (pCred)
416 {
417 unsigned iHash = SESSION_HASH(Process);
418 RTSpinlockAcquire(g_Spinlock);
419
420 pSession = g_apSessionHashTab[iHash];
421 while (pSession && pSession->Process != Process)
422 pSession = pSession->pNextHash;
423 if (pSession)
424 {
425 if (!pSession->fOpened)
426 {
427 pSession->fOpened = true;
428 pSession->fUserSession = !fUnrestricted;
429 }
430 else
431 rc = VERR_ALREADY_LOADED;
432 }
433 else
434 rc = VERR_GENERAL_FAILURE;
435
436 RTSpinlockRelease(g_Spinlock);
437#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
438 kauth_cred_unref(&pCred);
439#else /* 10.4 */
440 /* The 10.4u SDK headers and 10.4.11 kernel source have inconsistent definitions
441 of kauth_cred_unref(), so use the other (now deprecated) API for releasing it. */
442 kauth_cred_rele(pCred);
443#endif /* 10.4 */
444 }
445 else
446 rc = VERR_INVALID_PARAMETER;
447
448 Log(("vgdrvDarwinOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, proc_pid(pProcess)));
449 return vgdrvDarwinErr2DarwinErr(rc);
450}
451
452
453/**
454 * Close device.
455 */
456static int vgdrvDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
457{
458 RT_NOREF(Dev, fFlags, fDevType, pProcess);
459 Log(("vgdrvDarwinClose: pid=%d\n", (int)RTProcSelf()));
460 Assert(proc_pid(pProcess) == (int)RTProcSelf());
461
462 /*
463 * Hand the session closing to org_virtualbox_VBoxGuestClient.
464 */
465 org_virtualbox_VBoxGuestClient::sessionClose(RTProcSelf());
466 return 0;
467}
468
469
470/**
471 * Device I/O Control entry point.
472 *
473 * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
474 * @param Dev The device number (major+minor).
475 * @param iCmd The IOCtl command.
476 * @param pData Pointer to the request data.
477 * @param fFlags Flag saying we're a character device (like we didn't know already).
478 * @param pProcess The process issuing this request.
479 */
480static int vgdrvDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
481{
482 RT_NOREF(Dev, fFlags);
483 const bool fUnrestricted = minor(Dev) == 0;
484 const RTPROCESS Process = proc_pid(pProcess);
485 const unsigned iHash = SESSION_HASH(Process);
486 PVBOXGUESTSESSION pSession;
487
488 /*
489 * Find the session.
490 */
491 RTSpinlockAcquire(g_Spinlock);
492 pSession = g_apSessionHashTab[iHash];
493 while (pSession && (pSession->Process != Process || pSession->fUserSession == fUnrestricted || !pSession->fOpened))
494 pSession = pSession->pNextHash;
495
496 //if (RT_LIKELY(pSession))
497 // supdrvSessionRetain(pSession);
498
499 RTSpinlockRelease(g_Spinlock);
500 if (!pSession)
501 {
502 Log(("VBoxDrvDarwinIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#lx\n",
503 (int)Process, iCmd));
504 return EINVAL;
505 }
506
507 /*
508 * Deal with the high-speed IOCtl.
509 */
510 int rc;
511 if (VBGL_IOCTL_IS_FAST(iCmd))
512 rc = VGDrvCommonIoCtlFast(iCmd, &g_DevExt, pSession);
513 else
514 rc = vgdrvDarwinIOCtlSlow(pSession, iCmd, pData, pProcess);
515
516 //supdrvSessionRelease(pSession);
517 return rc;
518}
519
520
521/**
522 * Worker for VBoxDrvDarwinIOCtl that takes the slow IOCtl functions.
523 *
524 * @returns Darwin errno.
525 *
526 * @param pSession The session.
527 * @param iCmd The IOCtl command.
528 * @param pData Pointer to the request data.
529 * @param pProcess The calling process.
530 */
531static int vgdrvDarwinIOCtlSlow(PVBOXGUESTSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess)
532{
533 RT_NOREF(pProcess);
534 LogFlow(("vgdrvDarwinIOCtlSlow: pSession=%p iCmd=%p pData=%p pProcess=%p\n", pSession, iCmd, pData, pProcess));
535
536
537 /*
538 * Buffered or unbuffered?
539 */
540 PVBGLREQHDR pHdr;
541 user_addr_t pUser = 0;
542 void *pvPageBuf = NULL;
543 uint32_t cbReq = IOCPARM_LEN(iCmd);
544 if ((IOC_DIRMASK & iCmd) == IOC_INOUT)
545 {
546 pHdr = (PVBGLREQHDR)pData;
547 if (RT_UNLIKELY(cbReq < sizeof(*pHdr)))
548 {
549 LogRel(("vgdrvDarwinIOCtlSlow: cbReq=%#x < %#x; iCmd=%#lx\n", cbReq, (int)sizeof(*pHdr), iCmd));
550 return EINVAL;
551 }
552 if (RT_UNLIKELY(pHdr->uVersion != VBGLREQHDR_VERSION))
553 {
554 LogRel(("vgdrvDarwinIOCtlSlow: bad uVersion=%#x; iCmd=%#lx\n", pHdr->uVersion, iCmd));
555 return EINVAL;
556 }
557 if (RT_UNLIKELY( RT_MAX(pHdr->cbIn, pHdr->cbOut) != cbReq
558 || pHdr->cbIn < sizeof(*pHdr)
559 || (pHdr->cbOut < sizeof(*pHdr) && pHdr->cbOut != 0)))
560 {
561 LogRel(("vgdrvDarwinIOCtlSlow: max(%#x,%#x) != %#x; iCmd=%#lx\n", pHdr->cbIn, pHdr->cbOut, cbReq, iCmd));
562 return EINVAL;
563 }
564 }
565 else if ((IOC_DIRMASK & iCmd) == IOC_VOID && !cbReq)
566 {
567 /*
568 * Get the header and figure out how much we're gonna have to read.
569 */
570 VBGLREQHDR Hdr;
571 pUser = (user_addr_t)*(void **)pData;
572 int rc = copyin(pUser, &Hdr, sizeof(Hdr));
573 if (RT_UNLIKELY(rc))
574 {
575 LogRel(("vgdrvDarwinIOCtlSlow: copyin(%llx,Hdr,) -> %#x; iCmd=%#lx\n", (unsigned long long)pUser, rc, iCmd));
576 return rc;
577 }
578 if (RT_UNLIKELY(Hdr.uVersion != VBGLREQHDR_VERSION))
579 {
580 LogRel(("vgdrvDarwinIOCtlSlow: bad uVersion=%#x; iCmd=%#lx\n", Hdr.uVersion, iCmd));
581 return EINVAL;
582 }
583 cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
584 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
585 || (Hdr.cbOut < sizeof(Hdr) && Hdr.cbOut != 0)
586 || cbReq > _1M*16))
587 {
588 LogRel(("vgdrvDarwinIOCtlSlow: max(%#x,%#x); iCmd=%#lx\n", Hdr.cbIn, Hdr.cbOut, iCmd));
589 return EINVAL;
590 }
591
592 /*
593 * Allocate buffer and copy in the data.
594 */
595 pHdr = (PVBGLREQHDR)RTMemTmpAlloc(cbReq);
596 if (!pHdr)
597 pvPageBuf = pHdr = (PVBGLREQHDR)IOMallocAligned(RT_ALIGN_Z(cbReq, PAGE_SIZE), 8);
598 if (RT_UNLIKELY(!pHdr))
599 {
600 LogRel(("vgdrvDarwinIOCtlSlow: failed to allocate buffer of %d bytes; iCmd=%#lx\n", cbReq, iCmd));
601 return ENOMEM;
602 }
603 rc = copyin(pUser, pHdr, Hdr.cbIn);
604 if (RT_UNLIKELY(rc))
605 {
606 LogRel(("vgdrvDarwinIOCtlSlow: copyin(%llx,%p,%#x) -> %#x; iCmd=%#lx\n",
607 (unsigned long long)pUser, pHdr, Hdr.cbIn, rc, iCmd));
608 if (pvPageBuf)
609 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
610 else
611 RTMemTmpFree(pHdr);
612 return rc;
613 }
614 if (Hdr.cbIn < cbReq)
615 RT_BZERO((uint8_t *)pHdr + Hdr.cbIn, cbReq - Hdr.cbIn);
616 }
617 else
618 {
619 Log(("vgdrvDarwinIOCtlSlow: huh? cbReq=%#x iCmd=%#lx\n", cbReq, iCmd));
620 return EINVAL;
621 }
622
623 /*
624 * Process the IOCtl.
625 */
626 int rc = VGDrvCommonIoCtl(iCmd, &g_DevExt, pSession, pHdr, cbReq);
627 if (RT_LIKELY(!rc))
628 {
629 /*
630 * If not buffered, copy back the buffer before returning.
631 */
632 if (pUser)
633 {
634 uint32_t cbOut = pHdr->cbOut;
635 if (cbOut > cbReq)
636 {
637 LogRel(("vgdrvDarwinIOCtlSlow: too much output! %#x > %#x; uCmd=%#lx!\n", cbOut, cbReq, iCmd));
638 cbOut = cbReq;
639 }
640 rc = copyout(pHdr, pUser, cbOut);
641 if (RT_UNLIKELY(rc))
642 LogRel(("vgdrvDarwinIOCtlSlow: copyout(%p,%llx,%#x) -> %d; uCmd=%#lx!\n",
643 pHdr, (unsigned long long)pUser, cbOut, rc, iCmd));
644
645 /* cleanup */
646 if (pvPageBuf)
647 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
648 else
649 RTMemTmpFree(pHdr);
650 }
651 }
652 else
653 {
654 /*
655 * The request failed, just clean up.
656 */
657 if (pUser)
658 {
659 if (pvPageBuf)
660 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
661 else
662 RTMemTmpFree(pHdr);
663 }
664
665 Log(("vgdrvDarwinIOCtlSlow: pid=%d iCmd=%lx pData=%p failed, rc=%d\n", proc_pid(pProcess), iCmd, (void *)pData, rc));
666 rc = EINVAL;
667 }
668
669 Log2(("vgdrvDarwinIOCtlSlow: returns %d\n", rc));
670 return rc;
671}
672
673
674/**
675 * @note This code is duplicated on other platforms with variations, so please
676 * keep them all up to date when making changes!
677 */
678int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
679{
680 /*
681 * Simple request validation (common code does the rest).
682 */
683 int rc;
684 if ( RT_VALID_PTR(pReqHdr)
685 && cbReq >= sizeof(*pReqHdr))
686 {
687 /*
688 * All requests except the connect one requires a valid session.
689 */
690 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
691 if (pSession)
692 {
693 if ( RT_VALID_PTR(pSession)
694 && pSession->pDevExt == &g_DevExt)
695 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
696 else
697 rc = VERR_INVALID_HANDLE;
698 }
699 else if (uReq == VBGL_IOCTL_IDC_CONNECT)
700 {
701 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
702 if (RT_SUCCESS(rc))
703 {
704 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
705 if (RT_FAILURE(rc))
706 VGDrvCommonCloseSession(&g_DevExt, pSession);
707 }
708 }
709 else
710 rc = VERR_INVALID_HANDLE;
711 }
712 else
713 rc = VERR_INVALID_POINTER;
714 return rc;
715}
716
717
718void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
719{
720 NOREF(pDevExt);
721}
722
723
724bool VGDrvNativeProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue)
725{
726 RT_NOREF(pDevExt); RT_NOREF(pszName); RT_NOREF(pszValue);
727 return false;
728}
729
730
731/**
732 * Callback for blah blah blah.
733 *
734 * @todo move to IPRT.
735 */
736static IOReturn vgdrvDarwinSleepHandler(void *pvTarget, void *pvRefCon, UInt32 uMessageType,
737 IOService *pProvider, void *pvMsgArg, vm_size_t cbMsgArg)
738{
739 RT_NOREF(pvTarget, pProvider, pvMsgArg, cbMsgArg);
740 LogFlow(("VBoxGuest: Got sleep/wake notice. Message type was %x\n", uMessageType));
741
742 if (uMessageType == kIOMessageSystemWillSleep)
743 RTPowerSignalEvent(RTPOWEREVENT_SUSPEND);
744 else if (uMessageType == kIOMessageSystemHasPoweredOn)
745 RTPowerSignalEvent(RTPOWEREVENT_RESUME);
746
747 acknowledgeSleepWakeNotification(pvRefCon);
748
749 return 0;
750}
751
752
753/**
754 * Converts an IPRT error code to a darwin error code.
755 *
756 * @returns corresponding darwin error code.
757 * @param rc IPRT status code.
758 */
759static int vgdrvDarwinErr2DarwinErr(int rc)
760{
761 switch (rc)
762 {
763 case VINF_SUCCESS: return 0;
764 case VERR_GENERAL_FAILURE: return EACCES;
765 case VERR_INVALID_PARAMETER: return EINVAL;
766 case VERR_INVALID_MAGIC: return EILSEQ;
767 case VERR_INVALID_HANDLE: return ENXIO;
768 case VERR_INVALID_POINTER: return EFAULT;
769 case VERR_LOCK_FAILED: return ENOLCK;
770 case VERR_ALREADY_LOADED: return EEXIST;
771 case VERR_PERMISSION_DENIED: return EPERM;
772 case VERR_VERSION_MISMATCH: return ENOSYS;
773 }
774
775 return EPERM;
776}
777
778
779/*
780 *
781 * org_virtualbox_VBoxGuest
782 *
783 * - IOService diff resync -
784 * - IOService diff resync -
785 * - IOService diff resync -
786 *
787 */
788
789
790/**
791 * Initialize the object.
792 */
793bool org_virtualbox_VBoxGuest::init(OSDictionary *pDictionary)
794{
795 LogFlow(("IOService::init([%p], %p)\n", this, pDictionary));
796 if (IOService::init(pDictionary))
797 {
798 /* init members. */
799 return true;
800 }
801 return false;
802}
803
804
805/**
806 * Free the object.
807 */
808void org_virtualbox_VBoxGuest::free(void)
809{
810 RTLogBackdoorPrintf("IOService::free([%p])\n", this); /* might go sideways if we use LogFlow() here. weird. */
811 IOService::free();
812}
813
814
815/**
816 * Check if it's ok to start this service.
817 * It's always ok by us, so it's up to IOService to decide really.
818 */
819IOService *org_virtualbox_VBoxGuest::probe(IOService *pProvider, SInt32 *pi32Score)
820{
821 LogFlow(("IOService::probe([%p])\n", this));
822 IOService *pRet = IOService::probe(pProvider, pi32Score);
823 LogFlow(("IOService::probe([%p]) returns %p *pi32Score=%d\n", this, pRet, pi32Score ? *pi32Score : -1));
824 return pRet;
825}
826
827
828/**
829 * Start this service.
830 */
831bool org_virtualbox_VBoxGuest::start(IOService *pProvider)
832{
833 LogFlow(("IOService::start([%p])\n", this));
834
835 /*
836 * Low level initialization / device initialization should be performed only once.
837 */
838 if (ASMAtomicCmpXchgBool(&g_fInstantiated, true, false))
839 {
840 /*
841 * Make sure it's a PCI device.
842 */
843 m_pIOPCIDevice = OSDynamicCast(IOPCIDevice, pProvider);
844 if (m_pIOPCIDevice)
845 {
846 /*
847 * Call parent.
848 */
849 if (IOService::start(pProvider))
850 {
851 /*
852 * Is it the VMM device?
853 */
854 if (isVmmDev(m_pIOPCIDevice))
855 {
856 /*
857 * Enable I/O port and memory regions on the device.
858 */
859 m_pIOPCIDevice->setMemoryEnable(true);
860 m_pIOPCIDevice->setIOEnable(true);
861
862 /*
863 * Region #0: I/O ports. Mandatory.
864 */
865 IOMemoryDescriptor *pMem = m_pIOPCIDevice->getDeviceMemoryWithIndex(0);
866 if (pMem)
867 {
868 IOPhysicalAddress IOPortBasePhys = pMem->getPhysicalAddress();
869 if ((IOPortBasePhys >> 16) == 0)
870 {
871 RTIOPORT IOPortBase = (RTIOPORT)IOPortBasePhys;
872 void *pvMMIOBase = NULL;
873 uint32_t cbMMIO = 0;
874
875 /*
876 * Region #1: Shared Memory. Technically optional.
877 */
878 m_pMap = m_pIOPCIDevice->mapDeviceMemoryWithIndex(1);
879 if (m_pMap)
880 {
881 pvMMIOBase = (void *)m_pMap->getVirtualAddress();
882 cbMMIO = m_pMap->getLength();
883 }
884
885 /*
886 * Initialize the device extension.
887 */
888 int rc = VGDrvCommonInitDevExt(&g_DevExt, IOPortBase, pvMMIOBase, cbMMIO,
889 ARCH_BITS == 64 ? VBOXOSTYPE_MacOS_x64 : VBOXOSTYPE_MacOS, 0);
890 if (RT_SUCCESS(rc))
891 {
892 /*
893 * Register the device nodes and enable interrupts.
894 */
895 rc = vgdrvDarwinCharDevInit();
896 if (rc == KMOD_RETURN_SUCCESS)
897 {
898 if (setupVmmDevInterrupts(pProvider))
899 {
900 /*
901 * Read host configuration.
902 */
903 VGDrvCommonProcessOptionsFromHost(&g_DevExt);
904
905 /*
906 * Just register the service and we're done!
907 */
908 registerService();
909
910 LogRel(("VBoxGuest: IOService started\n"));
911 return true;
912 }
913
914 LogRel(("VBoxGuest: Failed to set up interrupts\n"));
915 vgdrvDarwinCharDevRemove();
916 }
917 else
918 LogRel(("VBoxGuest: Failed to initialize character devices (rc=%#x).\n", rc));
919
920 VGDrvCommonDeleteDevExt(&g_DevExt);
921 }
922 else
923 LogRel(("VBoxGuest: Failed to initialize common code (rc=%Rrc).\n", rc));
924
925 if (m_pMap)
926 {
927 m_pMap->release();
928 m_pMap = NULL;
929 }
930 }
931 else
932 LogRel(("VBoxGuest: Bad I/O port address: %#RX64\n", (uint64_t)IOPortBasePhys));
933 }
934 else
935 LogRel(("VBoxGuest: The device missing is the I/O port range (#0).\n"));
936 }
937 else
938 LogRel(("VBoxGuest: Not the VMMDev (%#x:%#x).\n",
939 m_pIOPCIDevice->configRead16(kIOPCIConfigVendorID), m_pIOPCIDevice->configRead16(kIOPCIConfigDeviceID)));
940
941 IOService::stop(pProvider);
942 }
943 }
944 else
945 LogRel(("VBoxGuest: Provider is not an instance of IOPCIDevice.\n"));
946
947 ASMAtomicXchgBool(&g_fInstantiated, false);
948 }
949 return false;
950}
951
952
953/**
954 * Stop this service.
955 */
956void org_virtualbox_VBoxGuest::stop(IOService *pProvider)
957{
958#ifdef LOG_ENABLED
959 RTLogBackdoorPrintf("org_virtualbox_VBoxGuest::stop([%p], %p)\n", this, pProvider); /* Being cautious here, no Log(). */
960#endif
961 AssertReturnVoid(ASMAtomicReadBool(&g_fInstantiated));
962
963 /* Low level termination should be performed only once */
964 if (!disableVmmDevInterrupts())
965 printf("VBoxGuest: unable to unregister interrupt handler\n");
966
967 vgdrvDarwinCharDevRemove();
968 VGDrvCommonDeleteDevExt(&g_DevExt);
969
970 if (m_pMap)
971 {
972 m_pMap->release();
973 m_pMap = NULL;
974 }
975
976 IOService::stop(pProvider);
977
978 ASMAtomicWriteBool(&g_fInstantiated, false);
979
980 printf("VBoxGuest: IOService stopped\n");
981 RTLogBackdoorPrintf("org_virtualbox_VBoxGuest::stop: returning\n"); /* Being cautious here, no Log(). */
982}
983
984
985/**
986 * Termination request.
987 *
988 * @return true if we're ok with shutting down now, false if we're not.
989 * @param fOptions Flags.
990 */
991bool org_virtualbox_VBoxGuest::terminate(IOOptionBits fOptions)
992{
993#ifdef LOG_ENABLED
994 RTLogBackdoorPrintf("org_virtualbox_VBoxGuest::terminate: reference_count=%d g_cSessions=%d (fOptions=%#x)\n",
995 KMOD_INFO_NAME.reference_count, ASMAtomicUoReadS32(&g_cSessions), fOptions); /* Being cautious here, no Log(). */
996#endif
997
998 bool fRc;
999 if ( KMOD_INFO_NAME.reference_count != 0
1000 || ASMAtomicUoReadS32(&g_cSessions))
1001 fRc = false;
1002 else
1003 fRc = IOService::terminate(fOptions);
1004
1005#ifdef LOG_ENABLED
1006 RTLogBackdoorPrintf("org_virtualbox_SupDrv::terminate: returns %d\n", fRc); /* Being cautious here, no Log(). */
1007#endif
1008 return fRc;
1009}
1010
1011
1012/**
1013 * Implementes a IOInterruptHandler, called by provider when an interrupt occurs.
1014 */
1015/*static*/ void org_virtualbox_VBoxGuest::vgdrvDarwinIrqHandler(OSObject *pTarget, void *pvRefCon, IOService *pNub, int iSrc)
1016{
1017#ifdef LOG_ENABLED
1018 RTLogBackdoorPrintf("vgdrvDarwinIrqHandler: %p %p %p %d\n", pTarget, pvRefCon, pNub, iSrc);
1019#endif
1020 RT_NOREF(pvRefCon, pNub, iSrc);
1021
1022 VGDrvCommonISR(&g_DevExt);
1023 /* There is in fact no way of indicating that this is our interrupt, other
1024 than making the device lower it. So, the return code is ignored. */
1025}
1026
1027
1028/**
1029 * Sets up and enables interrupts on the device.
1030 *
1031 * Interrupts are handled directly, no messing around with workloops. The
1032 * rational here is is that the main job of our interrupt handler is waking up
1033 * other threads currently sitting in HGCM calls, i.e. little more effort than
1034 * waking up the workloop thread.
1035 *
1036 * @returns success indicator. Failures are fully logged.
1037 */
1038bool org_virtualbox_VBoxGuest::setupVmmDevInterrupts(IOService *pProvider)
1039{
1040 AssertReturn(pProvider, false);
1041
1042 if (m_pInterruptProvider != pProvider)
1043 {
1044 pProvider->retain();
1045 if (m_pInterruptProvider)
1046 m_pInterruptProvider->release();
1047 m_pInterruptProvider = pProvider;
1048 }
1049
1050 IOReturn rc = pProvider->registerInterrupt(0 /*intIndex*/, this, vgdrvDarwinIrqHandler, this);
1051 if (rc == kIOReturnSuccess)
1052 {
1053 rc = pProvider->enableInterrupt(0 /*intIndex*/);
1054 if (rc == kIOReturnSuccess)
1055 return true;
1056
1057 LogRel(("VBoxGuest: Failed to enable interrupt: %#x\n", rc));
1058 m_pInterruptProvider->unregisterInterrupt(0 /*intIndex*/);
1059 }
1060 else
1061 LogRel(("VBoxGuest: Failed to register interrupt: %#x\n", rc));
1062 return false;
1063}
1064
1065
1066/**
1067 * Counterpart to setupVmmDevInterrupts().
1068 */
1069bool org_virtualbox_VBoxGuest::disableVmmDevInterrupts(void)
1070{
1071 if (m_pInterruptProvider)
1072 {
1073 IOReturn rc = m_pInterruptProvider->disableInterrupt(0 /*intIndex*/);
1074 AssertMsg(rc == kIOReturnSuccess, ("%#x\n", rc));
1075 rc = m_pInterruptProvider->unregisterInterrupt(0 /*intIndex*/);
1076 AssertMsg(rc == kIOReturnSuccess, ("%#x\n", rc));
1077 RT_NOREF_PV(rc);
1078
1079 m_pInterruptProvider->release();
1080 m_pInterruptProvider = NULL;
1081 }
1082
1083 return true;
1084}
1085
1086
1087/**
1088 * Checks if it's the VMM device.
1089 *
1090 * @returns true if it is, false if it isn't.
1091 * @param pIOPCIDevice The PCI device we think might be the VMM device.
1092 */
1093bool org_virtualbox_VBoxGuest::isVmmDev(IOPCIDevice *pIOPCIDevice)
1094{
1095 if (pIOPCIDevice)
1096 {
1097 uint16_t idVendor = m_pIOPCIDevice->configRead16(kIOPCIConfigVendorID);
1098 if (idVendor == VMMDEV_VENDORID)
1099 {
1100 uint16_t idDevice = m_pIOPCIDevice->configRead16(kIOPCIConfigDeviceID);
1101 if (idDevice == VMMDEV_DEVICEID)
1102 return true;
1103 }
1104 }
1105 return false;
1106}
1107
1108
1109
1110/*
1111 *
1112 * org_virtualbox_VBoxGuestClient
1113 *
1114 */
1115
1116
1117/**
1118 * Initializer called when the client opens the service.
1119 */
1120bool org_virtualbox_VBoxGuestClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
1121{
1122 LogFlow(("org_virtualbox_VBoxGuestClient::initWithTask([%p], %#x, %p, %#x) (cur pid=%d proc=%p)\n",
1123 this, OwningTask, pvSecurityId, u32Type, RTProcSelf(), RTR0ProcHandleSelf()));
1124 AssertMsg((RTR0PROCESS)OwningTask == RTR0ProcHandleSelf(), ("%p %p\n", OwningTask, RTR0ProcHandleSelf()));
1125
1126 if (!OwningTask)
1127 return false;
1128
1129 if (u32Type != VBOXGUEST_DARWIN_IOSERVICE_COOKIE)
1130 {
1131 VBOX_RETRIEVE_CUR_PROC_NAME(szProcName);
1132 LogRelMax(10, ("org_virtualbox_VBoxGuestClient::initWithTask: Bad cookie %#x (%s)\n", u32Type, szProcName));
1133 return false;
1134 }
1135
1136 if (IOUserClient::initWithTask(OwningTask, pvSecurityId , u32Type))
1137 {
1138 /*
1139 * In theory we have to call task_reference() to make sure that the task is
1140 * valid during the lifetime of this object. The pointer is only used to check
1141 * for the context this object is called in though and never dereferenced
1142 * or passed to anything which might, so we just skip this step.
1143 */
1144 m_Task = OwningTask;
1145 m_pSession = NULL;
1146 m_pProvider = NULL;
1147 return true;
1148 }
1149 return false;
1150}
1151
1152
1153/**
1154 * Start the client service.
1155 */
1156bool org_virtualbox_VBoxGuestClient::start(IOService *pProvider)
1157{
1158 LogFlow(("org_virtualbox_VBoxGuestClient::start([%p], %p) (cur pid=%d proc=%p)\n",
1159 this, pProvider, RTProcSelf(), RTR0ProcHandleSelf() ));
1160 AssertMsgReturn((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(),
1161 ("%p %p\n", m_Task, RTR0ProcHandleSelf()),
1162 false);
1163
1164 if (IOUserClient::start(pProvider))
1165 {
1166 m_pProvider = OSDynamicCast(org_virtualbox_VBoxGuest, pProvider);
1167 if (m_pProvider)
1168 {
1169 Assert(!m_pSession);
1170
1171 /*
1172 * Create a new session.
1173 */
1174 int rc = VGDrvCommonCreateUserSession(&g_DevExt, VMMDEV_REQUESTOR_USERMODE, &m_pSession);
1175 if (RT_SUCCESS(rc))
1176 {
1177 m_pSession->fOpened = false;
1178 /* The Uid, Gid and fUnrestricted fields are set on open. */
1179
1180 /*
1181 * Insert it into the hash table, checking that there isn't
1182 * already one for this process first. (One session per proc!)
1183 */
1184 unsigned iHash = SESSION_HASH(m_pSession->Process);
1185 RTSpinlockAcquire(g_Spinlock);
1186
1187 PVBOXGUESTSESSION pCur = g_apSessionHashTab[iHash];
1188 while (pCur && pCur->Process != m_pSession->Process)
1189 pCur = pCur->pNextHash;
1190 if (!pCur)
1191 {
1192 m_pSession->pNextHash = g_apSessionHashTab[iHash];
1193 g_apSessionHashTab[iHash] = m_pSession;
1194 m_pSession->pvVBoxGuestClient = this;
1195 ASMAtomicIncS32(&g_cSessions);
1196 rc = VINF_SUCCESS;
1197 }
1198 else
1199 rc = VERR_ALREADY_LOADED;
1200
1201 RTSpinlockRelease(g_Spinlock);
1202 if (RT_SUCCESS(rc))
1203 {
1204 Log(("org_virtualbox_VBoxGuestClient::start: created session %p for pid %d\n", m_pSession, (int)RTProcSelf()));
1205 return true;
1206 }
1207
1208 LogFlow(("org_virtualbox_VBoxGuestClient::start: already got a session for this process (%p)\n", pCur));
1209 VGDrvCommonCloseSession(&g_DevExt, m_pSession); //supdrvSessionRelease(m_pSession);
1210 }
1211
1212 m_pSession = NULL;
1213 LogFlow(("org_virtualbox_VBoxGuestClient::start: rc=%Rrc from supdrvCreateSession\n", rc));
1214 }
1215 else
1216 LogFlow(("org_virtualbox_VBoxGuestClient::start: %p isn't org_virtualbox_VBoxGuest\n", pProvider));
1217 }
1218 return false;
1219}
1220
1221
1222/**
1223 * Common worker for clientClose and VBoxDrvDarwinClose.
1224 */
1225/* static */ void org_virtualbox_VBoxGuestClient::sessionClose(RTPROCESS Process)
1226{
1227 /*
1228 * Find the session and remove it from the hash table.
1229 *
1230 * Note! Only one session per process. (Both start() and
1231 * vgdrvDarwinOpen makes sure this is so.)
1232 */
1233 const unsigned iHash = SESSION_HASH(Process);
1234 RTSpinlockAcquire(g_Spinlock);
1235 PVBOXGUESTSESSION pSession = g_apSessionHashTab[iHash];
1236 if (pSession)
1237 {
1238 if (pSession->Process == Process)
1239 {
1240 g_apSessionHashTab[iHash] = pSession->pNextHash;
1241 pSession->pNextHash = NULL;
1242 ASMAtomicDecS32(&g_cSessions);
1243 }
1244 else
1245 {
1246 PVBOXGUESTSESSION pPrev = pSession;
1247 pSession = pSession->pNextHash;
1248 while (pSession)
1249 {
1250 if (pSession->Process == Process)
1251 {
1252 pPrev->pNextHash = pSession->pNextHash;
1253 pSession->pNextHash = NULL;
1254 ASMAtomicDecS32(&g_cSessions);
1255 break;
1256 }
1257
1258 /* next */
1259 pPrev = pSession;
1260 pSession = pSession->pNextHash;
1261 }
1262 }
1263 }
1264 RTSpinlockRelease(g_Spinlock);
1265 if (!pSession)
1266 {
1267 Log(("VBoxGuestClient::sessionClose: pSession == NULL, pid=%d; freed already?\n", (int)Process));
1268 return;
1269 }
1270
1271 /*
1272 * Remove it from the client object.
1273 */
1274 org_virtualbox_VBoxGuestClient *pThis = (org_virtualbox_VBoxGuestClient *)pSession->pvVBoxGuestClient;
1275 pSession->pvVBoxGuestClient = NULL;
1276 if (pThis)
1277 {
1278 Assert(pThis->m_pSession == pSession);
1279 pThis->m_pSession = NULL;
1280 }
1281
1282 /*
1283 * Close the session.
1284 */
1285 VGDrvCommonCloseSession(&g_DevExt, pSession); // supdrvSessionRelease(m_pSession);
1286}
1287
1288
1289/**
1290 * Client exits normally.
1291 */
1292IOReturn org_virtualbox_VBoxGuestClient::clientClose(void)
1293{
1294 LogFlow(("org_virtualbox_VBoxGuestClient::clientClose([%p]) (cur pid=%d proc=%p)\n", this, RTProcSelf(), RTR0ProcHandleSelf()));
1295 AssertMsg((RTR0PROCESS)m_Task == RTR0ProcHandleSelf(), ("%p %p\n", m_Task, RTR0ProcHandleSelf()));
1296
1297 /*
1298 * Clean up the session if it's still around.
1299 *
1300 * We cannot rely 100% on close, and in the case of a dead client
1301 * we'll end up hanging inside vm_map_remove() if we postpone it.
1302 */
1303 if (m_pSession)
1304 {
1305 sessionClose(RTProcSelf());
1306 Assert(!m_pSession);
1307 }
1308
1309 m_pProvider = NULL;
1310 terminate();
1311
1312 return kIOReturnSuccess;
1313}
1314
1315
1316/**
1317 * The client exits abnormally / forgets to do cleanups. (logging)
1318 */
1319IOReturn org_virtualbox_VBoxGuestClient::clientDied(void)
1320{
1321 LogFlow(("IOService::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n", this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
1322
1323 /* IOUserClient::clientDied() calls clientClose, so we'll just do the work there. */
1324 return IOUserClient::clientDied();
1325}
1326
1327
1328/**
1329 * Terminate the service (initiate the destruction). (logging)
1330 */
1331bool org_virtualbox_VBoxGuestClient::terminate(IOOptionBits fOptions)
1332{
1333 LogFlow(("IOService::terminate([%p], %#x)\n", this, fOptions));
1334 return IOUserClient::terminate(fOptions);
1335}
1336
1337
1338/**
1339 * The final stage of the client service destruction. (logging)
1340 */
1341bool org_virtualbox_VBoxGuestClient::finalize(IOOptionBits fOptions)
1342{
1343 LogFlow(("IOService::finalize([%p], %#x)\n", this, fOptions));
1344 return IOUserClient::finalize(fOptions);
1345}
1346
1347
1348/**
1349 * Stop the client service. (logging)
1350 */
1351void org_virtualbox_VBoxGuestClient::stop(IOService *pProvider)
1352{
1353 LogFlow(("IOService::stop([%p])\n", this));
1354 IOUserClient::stop(pProvider);
1355}
1356
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