VirtualBox

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

Last change on this file since 1003 was 403, checked in by vboxsync, 18 years ago

Need RTThreadWait in ring-0 too when using the generic timers, so thread.cpp was ported to ring-0. Fixed a bug in RTTimerStart() (the generic code). (hope this doesn't break the other platforms...)

File size: 24.5 KB
Line 
1/** @file
2 * VBox host drivers - Ring-0 support drivers - Darwin host:
3 * Darwin driver C code
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26/* Deal with conflicts first.
27 * (This is mess inherited from BSD. The *BSDs has clean this up long ago.) */
28#include <sys/param.h>
29#undef PVM
30#include <IOKit/IOLib.h> /* Assert as function */
31
32#include "SUPDRV.h"
33#include <VBox/version.h>
34#include <iprt/types.h>
35#include <iprt/initterm.h>
36#include <iprt/assert.h>
37#include <iprt/spinlock.h>
38#include <iprt/semaphore.h>
39#include <iprt/process.h>
40#include <iprt/alloc.h>
41
42#include <mach/kmod.h>
43#include <miscfs/devfs/devfs.h>
44#include <sys/conf.h>
45#include <sys/errno.h>
46#include <sys/ioccom.h>
47#include <sys/malloc.h>
48#include <sys/proc.h>
49#include <IOKit/IOService.h>
50#include <IOKit/IOUserclient.h>
51
52
53/*******************************************************************************
54* Defined Constants And Macros *
55*******************************************************************************/
56
57/** The module name. */
58#define DEVICE_NAME "vboxdrv"
59
60
61
62/*******************************************************************************
63* Internal Functions *
64*******************************************************************************/
65__BEGIN_DECLS
66static kern_return_t VBoxSupDrvStart(struct kmod_info *pKModInfo, void *pvData);
67static kern_return_t VBoxSupDrvStop(struct kmod_info *pKModInfo, void *pvData);
68
69static int VBoxSupDrvOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
70static int VBoxSupDrvClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
71static int VBoxSupDrvIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess);
72static int VBoxSupDrvIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess);
73
74static int VBoxSupDrvErr2DarwinErr(int rc);
75__END_DECLS
76
77
78/*******************************************************************************
79* Structures and Typedefs *
80*******************************************************************************/
81/**
82 * The service class.
83 * This is just a formality really.
84 */
85class org_virtualbox_SupDrv : public IOService
86{
87 OSDeclareDefaultStructors(org_virtualbox_SupDrv)
88
89public:
90 virtual bool init(OSDictionary *pDictionary = 0);
91 virtual void free(void);
92 virtual bool start(IOService *pProvider);
93 virtual void stop(IOService *pProvider);
94 virtual IOService *probe(IOService *pProvider, SInt32 *pi32Score);
95};
96
97OSDefineMetaClassAndStructors(org_virtualbox_SupDrv, IOService)
98
99
100/**
101 * An attempt at getting that clientDied() notification.
102 * I don't think it'll work as I cannot figure out where/what creates the correct
103 * port right.
104 */
105class org_virtualbox_SupDrvClient : public IOUserClient
106{
107 OSDeclareDefaultStructors(org_virtualbox_SupDrvClient)
108
109private:
110 PSUPDRVSESSION m_pSession; /**< The session. */
111 task_t m_Task; /**< The client task. */
112 org_virtualbox_SupDrv *m_pProvider; /**< The service provider. */
113
114public:
115 virtual bool initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type);
116 virtual bool start(IOService *pProvider);
117 virtual IOReturn clientClose(void);
118 virtual IOReturn clientDied(void);
119 virtual bool terminate(IOOptionBits fOptions = 0);
120 virtual bool finalize(IOOptionBits fOptions);
121 virtual void stop(IOService *pProvider);
122};
123
124OSDefineMetaClassAndStructors(org_virtualbox_SupDrvClient, IOUserClient)
125
126
127
128/*******************************************************************************
129* Global Variables *
130*******************************************************************************/
131/**
132 * Declare the module stuff.
133 */
134__BEGIN_DECLS
135extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
136extern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
137__private_extern__ kmod_start_func_t *_realmain;
138__private_extern__ kmod_stop_func_t *_antimain;
139__private_extern__ int _kext_apple_cc;
140
141KMOD_EXPLICIT_DECL(VBoxDrv, VBOX_VERSION_STRING, _start, _stop)
142kmod_start_func_t *_realmain = VBoxSupDrvStart;
143kmod_stop_func_t *_antimain = VBoxSupDrvStop;
144int _kext_apple_cc = __APPLE_CC__;
145__END_DECLS
146
147
148/**
149 * Device extention & session data association structure.
150 */
151static SUPDRVDEVEXT g_DevExt;
152
153/**
154 * The character device switch table for the driver.
155 */
156static struct cdevsw g_DevCW =
157{
158 /** @todo g++ doesn't like this syntax - it worked with gcc before renaming to .cpp. */
159 /*.d_open = */VBoxSupDrvOpen,
160 /*.d_close = */VBoxSupDrvClose,
161 /*.d_read = */eno_rdwrt,
162 /*.d_write = */eno_rdwrt,
163 /*.d_ioctl = */VBoxSupDrvIOCtl,
164 /*.d_stop = */eno_stop,
165 /*.d_reset = */eno_reset,
166 /*.d_ttys = */NULL,
167 /*.d_select= */eno_select,
168 /*.d_mmap = */eno_mmap,
169 /*.d_strategy = */eno_strat,
170 /*.d_getc = */eno_getc,
171 /*.d_putc = */eno_putc,
172 /*.d_type = */0
173};
174
175/** Major device number. */
176static int g_iMajorDeviceNo = -1;
177/** Registered devfs device handle. */
178static void *g_hDevFsDevice = NULL;
179
180/** Spinlock protecting g_apSessionHashTab. */
181static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
182/** Hash table */
183static PSUPDRVSESSION g_apSessionHashTab[19];
184/** Calculates the index into g_apSessionHashTab.*/
185#define SESSION_HASH(pid) ((pid) % RT_ELEMENTS(g_apSessionHashTab))
186
187
188/**
189 * Start the kernel module.
190 */
191static kern_return_t VBoxSupDrvStart(struct kmod_info *pKModInfo, void *pvData)
192{
193 int rc;
194 dprintf(("VBoxSupDrvStart\n"));
195
196 /*
197 * Initialize IPRT.
198 */
199 rc = RTR0Init(0);
200 if (RT_SUCCESS(rc))
201 {
202 /*
203 * Initialize the device extension.
204 */
205 rc = supdrvInitDevExt(&g_DevExt);
206 if (RT_SUCCESS(rc))
207 {
208 /*
209 * Initialize the session hash table.
210 */
211 memset(g_apSessionHashTab, 0, sizeof(g_apSessionHashTab)); /* paranoia */
212 rc = RTSpinlockCreate(&g_Spinlock);
213 if (RT_SUCCESS(rc))
214 {
215 /*
216 * Registering ourselves as a character device.
217 */
218 g_iMajorDeviceNo = cdevsw_add(-1, &g_DevCW);
219 if (g_iMajorDeviceNo >= 0)
220 {
221 g_hDevFsDevice = devfs_make_node(makedev(g_iMajorDeviceNo, 0), DEVFS_CHAR,
222 UID_ROOT, GID_WHEEL, 0660, DEVICE_NAME); /** @todo the UID and GID should be configurable! */
223 if (g_hDevFsDevice)
224 {
225 OSDBGPRINT(("VBoxDrv: Successfully started. (major=%d)\n", g_iMajorDeviceNo));
226 return KMOD_RETURN_SUCCESS;
227 }
228
229 OSDBGPRINT(("VBoxDrv: devfs_make_node(makedev(%d,0),,,,%s) failed\n",
230 g_iMajorDeviceNo, DEVICE_NAME));
231 cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
232 g_iMajorDeviceNo = -1;
233 }
234 else
235 OSDBGPRINT(("VBoxDrv: cdevsw_add failed (%d)\n", g_iMajorDeviceNo));
236 RTSpinlockDestroy(g_Spinlock);
237 g_Spinlock = NIL_RTSPINLOCK;
238 }
239 else
240 OSDBGPRINT(("VBoxDrv: RTSpinlockCreate failed (rc=%d)\n", rc));
241 supdrvDeleteDevExt(&g_DevExt);
242 }
243 else
244 OSDBGPRINT(("VBoxDrv: failed to initialize device extension (rc=%d)\n", rc));
245 RTR0Term();
246 }
247 else
248 OSDBGPRINT(("VBoxDrv: failed to initialize IPRT (rc=%d)\n", rc));
249
250 memset(&g_DevExt, 0, sizeof(g_DevExt));
251 return KMOD_RETURN_FAILURE;
252}
253
254
255/**
256 * Stop the kernel module.
257 */
258static kern_return_t VBoxSupDrvStop(struct kmod_info *pKModInfo, void *pvData)
259{
260 int rc;
261 dprintf(("VBoxSupDrvStop\n"));
262
263 /** @todo I've got a nagging feeling that we'll have to keep track of users and refuse
264 * unloading if we're busy. Investigate and implement this! */
265
266 /*
267 * Undo the work done during start (in reverse order).
268 */
269 devfs_remove(g_hDevFsDevice);
270 g_hDevFsDevice = NULL;
271
272 rc = cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
273 Assert(rc == g_iMajorDeviceNo);
274 g_iMajorDeviceNo = -1;
275
276 rc = supdrvDeleteDevExt(&g_DevExt);
277 AssertRC(rc);
278
279 rc = RTSpinlockDestroy(g_Spinlock);
280 AssertRC(rc);
281 g_Spinlock = NIL_RTSPINLOCK;
282
283 RTR0Term();
284
285 memset(&g_DevExt, 0, sizeof(g_DevExt));
286 dprintf(("VBoxSupDrvStop - done\n"));
287 return KMOD_RETURN_SUCCESS;
288}
289
290
291/**
292 * Device open. Called on open /dev/vboxdrv
293 *
294 * @param pInode Pointer to inode info structure.
295 * @param pFilp Associated file pointer.
296 */
297static int VBoxSupDrvOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
298{
299 int rc;
300 PSUPDRVSESSION pSession;
301 dprintf(("VBoxSupDrvOpen:\n"));
302
303 /*
304 * Create a new session.
305 */
306 rc = supdrvCreateSession(&g_DevExt, &pSession);
307 if (RT_SUCCESS(rc))
308 {
309 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
310 unsigned iHash;
311 struct ucred *pCred = proc_ucred(pProcess);
312 if (pCred)
313 {
314 pSession->Uid = pCred->cr_uid;
315 pSession->Gid = pCred->cr_gid;
316 }
317 pSession->Process = RTProcSelf();
318 pSession->R0Process = RTR0ProcHandleSelf();
319
320 /*
321 * Insert it into the hash table.
322 */
323 iHash = SESSION_HASH(pSession->Process);
324 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
325 pSession->pNextHash = g_apSessionHashTab[iHash];
326 g_apSessionHashTab[iHash] = pSession;
327 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
328 }
329
330 dprintf(("VBoxSupDrvOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, proc_pid(pProcess)));
331
332 return VBoxSupDrvErr2DarwinErr(rc);
333}
334
335
336/**
337 * Close device.
338 *
339 */
340static int VBoxSupDrvClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
341{
342 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
343 const RTPROCESS Process = proc_pid(pProcess);
344 const unsigned iHash = SESSION_HASH(Process);
345 PSUPDRVSESSION pSession;
346
347 dprintf(("VBoxSupDrvClose: pid=%d\n", (int)Process));
348
349 /*
350 * Remove from the hash table.
351 */
352 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
353 pSession = g_apSessionHashTab[iHash];
354 if (pSession)
355 {
356 if (pSession->Process == Process)
357 {
358 g_apSessionHashTab[iHash] = pSession->pNextHash;
359 pSession->pNextHash = NULL;
360 }
361 else
362 {
363 PSUPDRVSESSION pPrev = pSession;
364 pSession = pSession->pNextHash;
365 while (pSession)
366 {
367 if (pSession->Process == Process)
368 {
369 pPrev->pNextHash = pSession->pNextHash;
370 pSession->pNextHash = NULL;
371 break;
372 }
373
374 /* next */
375 pPrev = pSession;
376 pSession = pSession->pNextHash;
377 }
378 }
379 }
380 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
381 if (!pSession)
382 {
383 OSDBGPRINT(("VBoxSupDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
384 return EINVAL;
385 }
386
387 /*
388 * Close the session.
389 */
390 supdrvCloseSession(&g_DevExt, pSession);
391 return 0;
392}
393
394
395/**
396 * Device I/O Control entry point.
397 *
398 * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
399 * @param Dev The device number (major+minor).
400 * @param iCmd The IOCtl command.
401 * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
402 * @param fFlags Flag saying we're a character device (like we didn't know already).
403 * @param pProcess The process issuing this request.
404 */
405static int VBoxSupDrvIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
406{
407 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
408 const RTPROCESS Process = proc_pid(pProcess);
409 const unsigned iHash = SESSION_HASH(Process);
410 PSUPDRVSESSION pSession;
411
412 /*
413 * Find the session.
414 */
415 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
416 pSession = g_apSessionHashTab[iHash];
417 if (pSession && pSession->Process != Process)
418 {
419 do pSession = pSession->pNextHash;
420 while (pSession && pSession->Process != Process);
421 }
422 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
423 if (!pSession)
424 {
425 OSDBGPRINT(("VBoxSupDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
426 return EINVAL;
427 }
428
429 /*
430 * Deal with the two high-speed IOCtl that takes it's arguments from
431 * the session and iCmd, and only returns a VBox status code.
432 */
433 if ( iCmd == SUP_IOCTL_FAST_DO_RAW_RUN
434 || iCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
435 || iCmd == SUP_IOCTL_FAST_DO_NOP)
436 return supdrvIOCtlFast(iCmd, &g_DevExt, pSession);
437 return VBoxSupDrvIOCtlSlow(pSession, iCmd, pData, pProcess);
438}
439
440
441/**
442 * Worker for VBoxSupDrvIOCtl that takes the slow IOCtl functions.
443 *
444 * @returns Darwin errno.
445 *
446 * @param pSession The session.
447 * @param iCmd The IOCtl command.
448 * @param pData Pointer to the kernel copy of the SUPDRVIOCTLDATA buffer.
449 * @param pProcess The calling process.
450 */
451static int VBoxSupDrvIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess)
452{
453 int rc;
454 void *pvPageBuf = NULL;
455 void *pvBuf = NULL;
456 unsigned long cbBuf = 0;
457 unsigned cbOut = 0;
458 PSUPDRVIOCTLDATA pArgs = (PSUPDRVIOCTLDATA)pData;
459 dprintf(("VBoxSupDrvIOCtl: pSession=%p iCmd=%p pData=%p pProcess=%p\n", pSession, iCmd, pData, pProcess));
460
461 /*
462 * Copy ioctl data structure from user space.
463 */
464 if (IOCPARM_LEN(iCmd) != sizeof(SUPDRVIOCTLDATA))
465 {
466 dprintf(("VBoxSupDrvIOCtl: incorrect input length! cbArgs=%d\n", IOCPARM_LEN(iCmd)));
467 return EINVAL;
468 }
469
470 /*
471 * Allocate and copy user space input data buffer to kernel space.
472 */
473 if (pArgs->cbIn > 0 || pArgs->cbOut > 0)
474 {
475 cbBuf = max(pArgs->cbIn, pArgs->cbOut);
476 pvBuf = RTMemTmpAlloc(cbBuf);
477 if (pvBuf == NULL)
478 pvPageBuf = pvBuf = IOMallocAligned(cbBuf, 8);
479 if (pvBuf == NULL)
480 {
481 dprintf(("VBoxSupDrvIOCtl: failed to allocate buffer of %d bytes.\n", cbBuf));
482 return ENOMEM;
483 }
484 rc = copyin((const user_addr_t)pArgs->pvIn, pvBuf, pArgs->cbIn);
485 if (rc)
486 {
487 dprintf(("VBoxSupDrvIOCtl: copyin(%p,,%d) failed.\n", pArgs->pvIn, cbBuf));
488 if (pvPageBuf)
489 IOFreeAligned(pvPageBuf, cbBuf);
490 else
491 RTMemTmpFree(pvBuf);
492 return rc;
493 }
494 }
495
496 /*
497 * Process the IOCtl.
498 */
499 rc = supdrvIOCtl(iCmd, &g_DevExt, pSession,
500 pvBuf, pArgs->cbIn, pvBuf, pArgs->cbOut, &cbOut);
501
502 /*
503 * Copy ioctl data and output buffer back to user space.
504 */
505 if (rc)
506 {
507 dprintf(("VBoxSupDrvIOCtl: pid=%d iCmd=%x pData=%p failed, rc=%d (darwin rc=%d)\n",
508 proc_pid(pProcess), iCmd, (void *)pData, rc, VBoxSupDrvErr2DarwinErr(rc)));
509 rc = VBoxSupDrvErr2DarwinErr(rc);
510 }
511 else if (cbOut > 0)
512 {
513 if (pvBuf != NULL && cbOut <= cbBuf)
514 {
515 int rc2 = copyout(pvBuf, (user_addr_t)pArgs->pvOut, cbOut);
516 if (rc2)
517 {
518 dprintf(("VBoxSupDrvIOCtl: copyout(,%p,%d) failed.\n", pArgs->pvOut, cbBuf));
519 rc = rc2;
520 }
521 }
522 else
523 {
524 dprintf(("WHAT!?! supdrvIOCtl messed up! cbOut=%d cbBuf=%d pvBuf=%p\n", cbOut, cbBuf, pvBuf));
525 rc = EPERM;
526 }
527 }
528
529 if (pvPageBuf)
530 IOFreeAligned(pvPageBuf, cbBuf);
531 else if (pvBuf)
532 RTMemTmpFree(pvBuf);
533
534 dprintf2(("VBoxSupDrvIOCtl: returns %d\n", rc));
535 return rc;
536}
537
538
539
540/**
541 * Initializes any OS specific object creator fields.
542 */
543void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
544{
545 NOREF(pObj);
546 NOREF(pSession);
547}
548
549
550/**
551 * Checks if the session can access the object.
552 *
553 * @returns true if a decision has been made.
554 * @returns false if the default access policy should be applied.
555 *
556 * @param pObj The object in question.
557 * @param pSession The session wanting to access the object.
558 * @param pszObjName The object name, can be NULL.
559 * @param prc Where to store the result when returning true.
560 */
561bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
562{
563 NOREF(pObj);
564 NOREF(pSession);
565 NOREF(pszObjName);
566 NOREF(prc);
567 return false;
568}
569
570
571/**
572 * Converts a supdrv error code to a darwin error code.
573 *
574 * @returns corresponding darwin error code.
575 * @param rc supdrv error code (SUPDRV_ERR_* defines).
576 */
577static int VBoxSupDrvErr2DarwinErr(int rc)
578{
579 switch (rc)
580 {
581 case 0: return 0;
582 case SUPDRV_ERR_GENERAL_FAILURE: return EACCES;
583 case SUPDRV_ERR_INVALID_PARAM: return EINVAL;
584 case SUPDRV_ERR_INVALID_MAGIC: return EILSEQ;
585 case SUPDRV_ERR_INVALID_HANDLE: return ENXIO;
586 case SUPDRV_ERR_INVALID_POINTER: return EFAULT;
587 case SUPDRV_ERR_LOCK_FAILED: return ENOLCK;
588 case SUPDRV_ERR_ALREADY_LOADED: return EEXIST;
589 case SUPDRV_ERR_PERMISSION_DENIED: return EPERM;
590 case SUPDRV_ERR_VERSION_MISMATCH: return ENOSYS;
591 }
592
593 return EPERM;
594}
595
596
597/** @todo move this to assembly where a simple "jmp printf" will to the trick. */
598RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
599{
600 va_list args;
601 char szMsg[512];
602
603 va_start(args, pszFormat);
604 vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, args);
605 va_end(args);
606
607 szMsg[sizeof(szMsg) - 1] = '\0';
608 printf("%s", szMsg);
609 return 0;
610}
611
612
613/** Runtime assert implementation for Darwin Ring-0. */
614RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
615{
616 printf("!!Assertion Failed!!\n"
617 "Expression: %s\n"
618 "Location : %s(%d) %s\n",
619 pszExpr, pszFile, uLine, pszFunction);
620}
621
622
623/** Runtime assert implementation for the Darwin Ring-0 driver.
624 * @todo this one needs fixing! */
625RTDECL(void) AssertMsg2(const char *pszFormat, ...)
626{ /* forwarder. */
627 va_list ap;
628 char msg[256];
629
630 va_start(ap, pszFormat);
631 vsnprintf(msg, sizeof(msg) - 1, pszFormat, ap);
632 msg[sizeof(msg) - 1] = '\0';
633 printf("%s", msg);
634 va_end(ap);
635}
636
637
638/*
639 *
640 * org_virtualbox_SupDrv
641 *
642 */
643
644
645/**
646 * Initialize the object.
647 */
648bool org_virtualbox_SupDrv::init(OSDictionary *pDictionary)
649{
650 dprintf(("org_virtualbox_SupDrv::init([%p], %p)\n", this, pDictionary));
651 if (IOService::init(pDictionary))
652 {
653 /* init members. */
654 return true;
655 }
656 return false;
657}
658
659
660/**
661 * Free the object.
662 */
663void org_virtualbox_SupDrv::free(void)
664{
665 dprintf(("IOService::free([%p])\n", this));
666 IOService::free();
667}
668
669
670/**
671 * Check if it's ok to start this service.
672 * It's always ok by us, so it's up to IOService to decide really.
673 */
674IOService *org_virtualbox_SupDrv::probe(IOService *pProvider, SInt32 *pi32Score)
675{
676 dprintf(("org_virtualbox_SupDrv::probe([%p])\n", this));
677 return IOService::probe(pProvider, pi32Score);
678}
679
680
681/**
682 * Start this service.
683 */
684bool org_virtualbox_SupDrv::start(IOService *pProvider)
685{
686 dprintf(("org_virtualbox_SupDrv::start([%p])\n", this));
687
688 if (IOService::start(pProvider))
689 {
690 /* register the service. */
691 registerService();
692 return true;
693 }
694 return false;
695}
696
697
698/**
699 * Stop this service.
700 */
701void org_virtualbox_SupDrv::stop(IOService *pProvider)
702{
703 dprintf(("org_virtualbox_SupDrv::stop([%p], %p)\n", this, pProvider));
704 IOService::stop(pProvider);
705}
706
707
708/*
709 *
710 * org_virtualbox_SupDrvClient
711 *
712 */
713
714
715/**
716 * Initializer called when the client opens the service.
717 */
718bool org_virtualbox_SupDrvClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
719{
720 dprintf(("org_virtualbox_SupDrvClient::initWithTask([%p], %#x, %p, %#x)\n", this, OwningTask, pvSecurityId, u32Type));
721
722 if (!OwningTask)
723 return false;
724 if (IOUserClient::initWithTask(OwningTask, pvSecurityId , u32Type))
725 {
726 m_Task = OwningTask;
727 m_pSession = NULL;
728 m_pProvider = NULL;
729 return true;
730 }
731 return false;
732}
733
734
735/**
736 * Start the client service.
737 */
738bool org_virtualbox_SupDrvClient::start(IOService *pProvider)
739{
740 dprintf(("org_virtualbox_SupDrvClient::start([%p], %p)\n", this, pProvider));
741 if (IOUserClient::start(pProvider))
742 {
743 m_pProvider = OSDynamicCast(org_virtualbox_SupDrv, pProvider);
744 if (m_pProvider)
745 {
746 /* this is where we could create the section. */
747 return true;
748 }
749 dprintf(("org_virtualbox_SupDrvClient::start: %p isn't org_virtualbox_SupDrv\n", pProvider));
750 }
751 return false;
752}
753
754
755/**
756 * Client exits normally.
757 */
758IOReturn org_virtualbox_SupDrvClient::clientClose(void)
759{
760 dprintf(("org_virtualbox_SupDrvClient::clientClose([%p])\n", this));
761
762 m_pProvider = NULL;
763 terminate();
764
765 return kIOReturnSuccess;
766}
767
768
769/**
770 * The client exits abnormally / forgets to do cleanups.
771 */
772IOReturn org_virtualbox_SupDrvClient::clientDied(void)
773{
774 dprintf(("org_virtualbox_SupDrvClient::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n",
775 this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
776
777 /*
778 * Do early session cleanup (if there is a session) so
779 * we avoid hanging in vm_map_remove().
780 */
781 const RTR0PROCESS R0Process = (RTR0PROCESS)m_Task;
782 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
783 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
784 for (unsigned i = 0; i < RT_ELEMENTS(g_apSessionHashTab); i++)
785 {
786 for (PSUPDRVSESSION pSession = g_apSessionHashTab[i]; pSession; pSession = pSession->pNextHash)
787 {
788 dprintf2(("pSession=%p R0Process=%p (=? %p)\n", pSession, pSession->R0Process, R0Process));
789 if (pSession->R0Process == R0Process)
790 {
791 /*
792 * It is safe to leave the spinlock here; the session shouldn't be able
793 * to go away while we're cleaning it up, changes to pNextHash will not
794 * harm us, and new sessions can't possibly be added for this process.
795 */
796 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
797 supdrvCleanupSession(&g_DevExt, pSession);
798 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
799 }
800 }
801 }
802 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
803
804 /* IOUserClient::clientDied() calls close... */
805 return IOUserClient::clientDied();
806}
807
808
809/**
810 * Terminate the service (initiate the destruction).
811 */
812bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
813{
814 dprintf(("org_virtualbox_SupDrvClient::terminate([%p], %#x)\n", this, fOptions));
815 return IOUserClient::terminate(fOptions);
816}
817
818
819/**
820 * The final stage of the client service destruction.
821 */
822bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
823{
824 dprintf(("org_virtualbox_SupDrvClient::finalize([%p], %#x)\n", this, fOptions));
825 return IOUserClient::finalize(fOptions);
826}
827
828
829/**
830 * Stop the client service.
831 */
832void org_virtualbox_SupDrvClient::stop(IOService *pProvider)
833{
834 dprintf(("org_virtualbox_SupDrvClient::stop([%p])\n", this));
835 IOUserClient::stop(pProvider);
836}
837
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