VirtualBox

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

Last change on this file since 10603 was 10603, checked in by vboxsync, 16 years ago

*grpmf* copy & past error in the session counting code :/

File size: 29.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-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35/*
36 * Deal with conflicts first.
37 * PVM - BSD mess, that FreeBSD has correct a long time ago.
38 * iprt/types.h before sys/param.h - prevents UINT32_C and friends.
39 */
40#include <iprt/types.h>
41#include <sys/param.h>
42#undef PVM
43
44#include <IOKit/IOLib.h> /* Assert as function */
45
46#include "../SUPDrvInternal.h"
47#include <VBox/version.h>
48#include <iprt/initterm.h>
49#include <iprt/assert.h>
50#include <iprt/spinlock.h>
51#include <iprt/semaphore.h>
52#include <iprt/process.h>
53#include <iprt/alloc.h>
54#include <iprt/uuid.h>
55#include <iprt/err.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 <IOKit/IOService.h>
65#include <IOKit/IOUserclient.h>
66
67
68/*******************************************************************************
69* Defined Constants And Macros *
70*******************************************************************************/
71
72/** The module name. */
73#define DEVICE_NAME "vboxdrv"
74
75
76
77/*******************************************************************************
78* Internal Functions *
79*******************************************************************************/
80__BEGIN_DECLS
81static kern_return_t VBoxDrvDarwinStart(struct kmod_info *pKModInfo, void *pvData);
82static kern_return_t VBoxDrvDarwinStop(struct kmod_info *pKModInfo, void *pvData);
83
84static int VBoxDrvDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
85static int VBoxDrvDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
86static int VBoxDrvDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess);
87static int VBoxDrvDarwinIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess);
88
89static int VBoxDrvDarwinErr2DarwinErr(int rc);
90__END_DECLS
91
92
93/*******************************************************************************
94* Structures and Typedefs *
95*******************************************************************************/
96/**
97 * The service class.
98 * This is just a formality really.
99 */
100class org_virtualbox_SupDrv : public IOService
101{
102 OSDeclareDefaultStructors(org_virtualbox_SupDrv)
103
104public:
105 virtual bool init(OSDictionary *pDictionary = 0);
106 virtual void free(void);
107 virtual bool start(IOService *pProvider);
108 virtual void stop(IOService *pProvider);
109 virtual IOService *probe(IOService *pProvider, SInt32 *pi32Score);
110 virtual bool terminate(IOOptionBits fOptions);
111};
112
113OSDefineMetaClassAndStructors(org_virtualbox_SupDrv, IOService)
114
115
116/**
117 * An attempt at getting that clientDied() notification.
118 * I don't think it'll work as I cannot figure out where/what creates the correct
119 * port right.
120 */
121class org_virtualbox_SupDrvClient : public IOUserClient
122{
123 OSDeclareDefaultStructors(org_virtualbox_SupDrvClient)
124
125private:
126 PSUPDRVSESSION m_pSession; /**< The session. */
127 task_t m_Task; /**< The client task. */
128 org_virtualbox_SupDrv *m_pProvider; /**< The service provider. */
129
130public:
131 virtual bool initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type);
132 virtual bool start(IOService *pProvider);
133 virtual IOReturn clientClose(void);
134 virtual IOReturn clientDied(void);
135 virtual bool terminate(IOOptionBits fOptions = 0);
136 virtual bool finalize(IOOptionBits fOptions);
137 virtual void stop(IOService *pProvider);
138};
139
140OSDefineMetaClassAndStructors(org_virtualbox_SupDrvClient, IOUserClient)
141
142
143
144/*******************************************************************************
145* Global Variables *
146*******************************************************************************/
147/**
148 * Declare the module stuff.
149 */
150__BEGIN_DECLS
151extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
152extern kern_return_t _stop(struct kmod_info *pKModInfo, void *pvData);
153
154KMOD_EXPLICIT_DECL(VBoxDrv, VBOX_VERSION_STRING, _start, _stop)
155DECLHIDDEN(kmod_start_func_t *) _realmain = VBoxDrvDarwinStart;
156DECLHIDDEN(kmod_stop_func_t *) _antimain = VBoxDrvDarwinStop;
157DECLHIDDEN(int) _kext_apple_cc = __APPLE_CC__;
158__END_DECLS
159
160
161/**
162 * Device extention & session data association structure.
163 */
164static SUPDRVDEVEXT g_DevExt;
165
166/**
167 * The character device switch table for the driver.
168 */
169static struct cdevsw g_DevCW =
170{
171 /** @todo g++ doesn't like this syntax - it worked with gcc before renaming to .cpp. */
172 /*.d_open = */VBoxDrvDarwinOpen,
173 /*.d_close = */VBoxDrvDarwinClose,
174 /*.d_read = */eno_rdwrt,
175 /*.d_write = */eno_rdwrt,
176 /*.d_ioctl = */VBoxDrvDarwinIOCtl,
177 /*.d_stop = */eno_stop,
178 /*.d_reset = */eno_reset,
179 /*.d_ttys = */NULL,
180 /*.d_select= */eno_select,
181 /*.d_mmap = */eno_mmap,
182 /*.d_strategy = */eno_strat,
183 /*.d_getc = */eno_getc,
184 /*.d_putc = */eno_putc,
185 /*.d_type = */0
186};
187
188/** Major device number. */
189static int g_iMajorDeviceNo = -1;
190/** Registered devfs device handle. */
191static void *g_hDevFsDevice = NULL;
192
193/** Spinlock protecting g_apSessionHashTab. */
194static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
195/** Hash table */
196static PSUPDRVSESSION g_apSessionHashTab[19];
197/** Calculates the index into g_apSessionHashTab.*/
198#define SESSION_HASH(pid) ((pid) % RT_ELEMENTS(g_apSessionHashTab))
199/** The number of open sessions. */
200static int32_t volatile g_cSessions = 0;
201
202
203/*
204 * Drag in the rest of IRPT since we share it with the
205 * rest of the kernel modules on darwin.
206 */
207PFNRT g_apfnVBoxDrvIPRTDeps[] =
208{
209 (PFNRT)RTUuidCompare,
210 (PFNRT)RTErrConvertFromErrno,
211 NULL
212};
213
214
215/**
216 * Start the kernel module.
217 */
218static kern_return_t VBoxDrvDarwinStart(struct kmod_info *pKModInfo, void *pvData)
219{
220 int rc;
221 dprintf(("VBoxDrvDarwinStart\n"));
222
223 /*
224 * Initialize IPRT.
225 */
226 rc = RTR0Init(0);
227 if (RT_SUCCESS(rc))
228 {
229 /*
230 * Initialize the device extension.
231 */
232 rc = supdrvInitDevExt(&g_DevExt);
233 if (RT_SUCCESS(rc))
234 {
235 /*
236 * Initialize the session hash table.
237 */
238 memset(g_apSessionHashTab, 0, sizeof(g_apSessionHashTab)); /* paranoia */
239 rc = RTSpinlockCreate(&g_Spinlock);
240 if (RT_SUCCESS(rc))
241 {
242 /*
243 * Registering ourselves as a character device.
244 */
245 g_iMajorDeviceNo = cdevsw_add(-1, &g_DevCW);
246 if (g_iMajorDeviceNo >= 0)
247 {
248 /** @todo the UID, GID and mode mask should be configurable! This isn't very secure... */
249 g_hDevFsDevice = devfs_make_node(makedev(g_iMajorDeviceNo, 0), DEVFS_CHAR,
250 UID_ROOT, GID_WHEEL, 0666, DEVICE_NAME);
251 if (g_hDevFsDevice)
252 {
253 OSDBGPRINT(("VBoxDrv: Successfully started. (major=%d)\n", g_iMajorDeviceNo));
254 return KMOD_RETURN_SUCCESS;
255 }
256
257 OSDBGPRINT(("VBoxDrv: devfs_make_node(makedev(%d,0),,,,%s) failed\n",
258 g_iMajorDeviceNo, DEVICE_NAME));
259 cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
260 g_iMajorDeviceNo = -1;
261 }
262 else
263 OSDBGPRINT(("VBoxDrv: cdevsw_add failed (%d)\n", g_iMajorDeviceNo));
264 RTSpinlockDestroy(g_Spinlock);
265 g_Spinlock = NIL_RTSPINLOCK;
266 }
267 else
268 OSDBGPRINT(("VBoxDrv: RTSpinlockCreate failed (rc=%d)\n", rc));
269 supdrvDeleteDevExt(&g_DevExt);
270 }
271 else
272 OSDBGPRINT(("VBoxDrv: failed to initialize device extension (rc=%d)\n", rc));
273 RTR0Term();
274 }
275 else
276 OSDBGPRINT(("VBoxDrv: failed to initialize IPRT (rc=%d)\n", rc));
277
278 memset(&g_DevExt, 0, sizeof(g_DevExt));
279 return KMOD_RETURN_FAILURE;
280}
281
282
283/**
284 * Stop the kernel module.
285 */
286static kern_return_t VBoxDrvDarwinStop(struct kmod_info *pKModInfo, void *pvData)
287{
288 int rc;
289 dprintf(("VBoxDrvDarwinStop\n"));
290
291 /** @todo I've got a nagging feeling that we'll have to keep track of users and refuse
292 * unloading if we're busy. Investigate and implement this! */
293
294 /*
295 * Undo the work done during start (in reverse order).
296 */
297 devfs_remove(g_hDevFsDevice);
298 g_hDevFsDevice = NULL;
299
300 rc = cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
301 Assert(rc == g_iMajorDeviceNo);
302 g_iMajorDeviceNo = -1;
303
304 supdrvDeleteDevExt(&g_DevExt);
305
306 rc = RTSpinlockDestroy(g_Spinlock);
307 AssertRC(rc);
308 g_Spinlock = NIL_RTSPINLOCK;
309
310 RTR0Term();
311
312 memset(&g_DevExt, 0, sizeof(g_DevExt));
313 dprintf(("VBoxDrvDarwinStop - done\n"));
314 return KMOD_RETURN_SUCCESS;
315}
316
317
318/**
319 * Device open. Called on open /dev/vboxdrv
320 *
321 * @param pInode Pointer to inode info structure.
322 * @param pFilp Associated file pointer.
323 */
324static int VBoxDrvDarwinOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
325{
326 int rc;
327 PSUPDRVSESSION pSession;
328#ifdef DEBUG_DARWIN_GIP
329 char szName[128];
330 szName[0] = '\0';
331 proc_name(proc_pid(pProcess), szName, sizeof(szName));
332 dprintf(("VBoxDrvDarwinOpen: pid=%d '%s'\n", proc_pid(pProcess), szName));
333#endif
334
335 /*
336 * Create a new session.
337 */
338 rc = supdrvCreateSession(&g_DevExt, true /* fUser */, &pSession);
339 if (RT_SUCCESS(rc))
340 {
341 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
342 unsigned iHash;
343 struct ucred *pCred = proc_ucred(pProcess);
344 if (pCred)
345 {
346 pSession->Uid = pCred->cr_uid;
347 pSession->Gid = pCred->cr_gid;
348 }
349
350 /*
351 * Insert it into the hash table.
352 */
353 iHash = SESSION_HASH(pSession->Process);
354 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
355 pSession->pNextHash = g_apSessionHashTab[iHash];
356 g_apSessionHashTab[iHash] = pSession;
357 ASMAtomicIncS32(&g_cSessions);
358 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
359 }
360
361#ifdef DEBUG_DARWIN_GIP
362 OSDBGPRINT(("VBoxDrvDarwinOpen: pid=%d '%s' pSession=%p rc=%d\n", proc_pid(pProcess), szName, pSession, rc));
363#else
364 dprintf(("VBoxDrvDarwinOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, proc_pid(pProcess)));
365#endif
366 return VBoxDrvDarwinErr2DarwinErr(rc);
367}
368
369
370/**
371 * Close device.
372 */
373static int VBoxDrvDarwinClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
374{
375 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
376 const RTPROCESS Process = proc_pid(pProcess);
377 const unsigned iHash = SESSION_HASH(Process);
378 PSUPDRVSESSION pSession;
379
380 dprintf(("VBoxDrvDarwinClose: pid=%d\n", (int)Process));
381
382 /*
383 * Remove from the hash table.
384 */
385 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
386 pSession = g_apSessionHashTab[iHash];
387 if (pSession)
388 {
389 if (pSession->Process == Process)
390 {
391 g_apSessionHashTab[iHash] = pSession->pNextHash;
392 pSession->pNextHash = NULL;
393 ASMAtomicDecS32(&g_cSessions);
394 }
395 else
396 {
397 PSUPDRVSESSION pPrev = pSession;
398 pSession = pSession->pNextHash;
399 while (pSession)
400 {
401 if (pSession->Process == Process)
402 {
403 pPrev->pNextHash = pSession->pNextHash;
404 pSession->pNextHash = NULL;
405 ASMAtomicDecS32(&g_cSessions);
406 break;
407 }
408
409 /* next */
410 pPrev = pSession;
411 pSession = pSession->pNextHash;
412 }
413 }
414 }
415 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
416 if (!pSession)
417 {
418 OSDBGPRINT(("VBoxDrvDarwinClose: WHAT?!? pSession == NULL! This must be a mistake... pid=%d (close)\n",
419 (int)Process));
420 return EINVAL;
421 }
422
423 /*
424 * Close the session.
425 */
426 supdrvCloseSession(&g_DevExt, pSession);
427 return 0;
428}
429
430
431/**
432 * Device I/O Control entry point.
433 *
434 * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
435 * @param Dev The device number (major+minor).
436 * @param iCmd The IOCtl command.
437 * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
438 * @param fFlags Flag saying we're a character device (like we didn't know already).
439 * @param pProcess The process issuing this request.
440 */
441static int VBoxDrvDarwinIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
442{
443 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
444 const RTPROCESS Process = proc_pid(pProcess);
445 const unsigned iHash = SESSION_HASH(Process);
446 PSUPDRVSESSION pSession;
447
448 /*
449 * Find the session.
450 */
451 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
452 pSession = g_apSessionHashTab[iHash];
453 if (pSession && pSession->Process != Process)
454 {
455 do pSession = pSession->pNextHash;
456 while (pSession && pSession->Process != Process);
457 }
458 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
459 if (!pSession)
460 {
461 OSDBGPRINT(("VBoxDrvDarwinIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#x\n",
462 (int)Process, iCmd));
463 return EINVAL;
464 }
465
466 /*
467 * Deal with the two high-speed IOCtl that takes it's arguments from
468 * the session and iCmd, and only returns a VBox status code.
469 */
470 if ( iCmd == SUP_IOCTL_FAST_DO_RAW_RUN
471 || iCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
472 || iCmd == SUP_IOCTL_FAST_DO_NOP)
473 return supdrvIOCtlFast(iCmd, &g_DevExt, pSession);
474 return VBoxDrvDarwinIOCtlSlow(pSession, iCmd, pData, pProcess);
475}
476
477
478/**
479 * Worker for VBoxDrvDarwinIOCtl that takes the slow IOCtl functions.
480 *
481 * @returns Darwin errno.
482 *
483 * @param pSession The session.
484 * @param iCmd The IOCtl command.
485 * @param pData Pointer to the kernel copy of the SUPDRVIOCTLDATA buffer.
486 * @param pProcess The calling process.
487 */
488static int VBoxDrvDarwinIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess)
489{
490 dprintf(("VBoxDrvDarwinIOCtlSlow: pSession=%p iCmd=%p pData=%p pProcess=%p\n", pSession, iCmd, pData, pProcess));
491
492
493 /*
494 * Buffered or unbuffered?
495 */
496 PSUPREQHDR pHdr;
497 user_addr_t pUser = 0;
498 void *pvPageBuf = NULL;
499 uint32_t cbReq = IOCPARM_LEN(iCmd);
500 if ((IOC_DIRMASK & iCmd) == IOC_INOUT)
501 {
502 pHdr = (PSUPREQHDR)pData;
503 if (RT_UNLIKELY(cbReq < sizeof(*pHdr)))
504 {
505 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: cbReq=%#x < %#x; iCmd=%#lx\n", cbReq, (int)sizeof(*pHdr), iCmd));
506 return EINVAL;
507 }
508 if (RT_UNLIKELY((pHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
509 {
510 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: bad magic fFlags=%#x; iCmd=%#lx\n", pHdr->fFlags, iCmd));
511 return EINVAL;
512 }
513 if (RT_UNLIKELY( RT_MAX(pHdr->cbIn, pHdr->cbOut) != cbReq
514 || pHdr->cbIn < sizeof(*pHdr)
515 || pHdr->cbOut < sizeof(*pHdr)))
516 {
517 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: max(%#x,%#x) != %#x; iCmd=%#lx\n", pHdr->cbIn, pHdr->cbOut, cbReq, iCmd));
518 return EINVAL;
519 }
520 }
521 else if ((IOC_DIRMASK & iCmd) == IOC_VOID && !cbReq)
522 {
523 /*
524 * Get the header and figure out how much we're gonna have to read.
525 */
526 SUPREQHDR Hdr;
527 pUser = (user_addr_t)*(void **)pData;
528 int rc = copyin(pUser, &Hdr, sizeof(Hdr));
529 if (RT_UNLIKELY(rc))
530 {
531 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyin(%llx,Hdr,) -> %#x; iCmd=%#lx\n", (unsigned long long)pUser, rc, iCmd));
532 return rc;
533 }
534 if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
535 {
536 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: bad magic fFlags=%#x; iCmd=%#lx\n", Hdr.fFlags, iCmd));
537 return EINVAL;
538 }
539 cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
540 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
541 || Hdr.cbOut < sizeof(Hdr)
542 || cbReq > _1M*16))
543 {
544 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: max(%#x,%#x); iCmd=%#lx\n", Hdr.cbIn, Hdr.cbOut, iCmd));
545 return EINVAL;
546 }
547
548 /*
549 * Allocate buffer and copy in the data.
550 */
551 pHdr = (PSUPREQHDR)RTMemTmpAlloc(cbReq);
552 if (!pHdr)
553 pvPageBuf = pHdr = (PSUPREQHDR)IOMallocAligned(RT_ALIGN_Z(cbReq, PAGE_SIZE), 8);
554 if (RT_UNLIKELY(!pHdr))
555 {
556 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: failed to allocate buffer of %d bytes; iCmd=%#lx\n", cbReq, iCmd));
557 return ENOMEM;
558 }
559 rc = copyin(pUser, pHdr, Hdr.cbIn);
560 if (RT_UNLIKELY(rc))
561 {
562 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyin(%llx,%p,%#x) -> %#x; iCmd=%#lx\n",
563 (unsigned long long)pUser, pHdr, Hdr.cbIn, rc, iCmd));
564 if (pvPageBuf)
565 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
566 else
567 RTMemTmpFree(pHdr);
568 return rc;
569 }
570 }
571 else
572 {
573 dprintf(("VBoxDrvDarwinIOCtlSlow: huh? cbReq=%#x iCmd=%#lx\n", cbReq, iCmd));
574 return EINVAL;
575 }
576
577 /*
578 * Process the IOCtl.
579 */
580 int rc = supdrvIOCtl(iCmd, &g_DevExt, pSession, pHdr);
581 if (RT_LIKELY(!rc))
582 {
583 /*
584 * If not buffered, copy back the buffer before returning.
585 */
586 if (pUser)
587 {
588 uint32_t cbOut = pHdr->cbOut;
589 if (cbOut > cbReq)
590 {
591 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: too much output! %#x > %#x; uCmd=%#lx!\n", cbOut, cbReq, iCmd));
592 cbOut = cbReq;
593 }
594 rc = copyout(pHdr, pUser, cbOut);
595 if (RT_UNLIKELY(rc))
596 OSDBGPRINT(("VBoxDrvDarwinIOCtlSlow: copyout(%p,%llx,%#x) -> %d; uCmd=%#lx!\n",
597 pHdr, (unsigned long long)pUser, cbOut, rc, iCmd));
598
599 /* cleanup */
600 if (pvPageBuf)
601 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
602 else
603 RTMemTmpFree(pHdr);
604 }
605 }
606 else
607 {
608 /*
609 * The request failed, just clean up.
610 */
611 if (pUser)
612 {
613 if (pvPageBuf)
614 IOFreeAligned(pvPageBuf, RT_ALIGN_Z(cbReq, PAGE_SIZE));
615 else
616 RTMemTmpFree(pHdr);
617 }
618
619 dprintf(("VBoxDrvDarwinIOCtlSlow: pid=%d iCmd=%lx pData=%p failed, rc=%d\n", proc_pid(pProcess), iCmd, (void *)pData, rc));
620 rc = EINVAL;
621 }
622
623 dprintf2(("VBoxDrvDarwinIOCtlSlow: returns %d\n", rc));
624 return rc;
625}
626
627
628/**
629 * The SUPDRV IDC entry point.
630 *
631 * @returns VBox status code, see supdrvIDC.
632 * @param iReq The request code.
633 * @param pReq The request.
634 */
635int VBOXCALL SUPDrvDarwinIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
636{
637 PSUPDRVSESSION pSession;
638
639 /*
640 * Some quick validations.
641 */
642 if (RT_UNLIKELY(!VALID_PTR(pReq)))
643 return VERR_INVALID_POINTER;
644
645 pSession = pReq->pSession;
646 if (pSession)
647 {
648 if (RT_UNLIKELY(!VALID_PTR(pSession)))
649 return VERR_INVALID_PARAMETER;
650 if (RT_UNLIKELY(pSession->pDevExt != &g_DevExt))
651 return VERR_INVALID_PARAMETER;
652 }
653 else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
654 return VERR_INVALID_PARAMETER;
655
656 /*
657 * Do the job.
658 */
659 return supdrvIDC(uReq, &g_DevExt, pSession, pReq);
660}
661
662
663/**
664 * Initializes any OS specific object creator fields.
665 */
666void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
667{
668 NOREF(pObj);
669 NOREF(pSession);
670}
671
672
673/**
674 * Checks if the session can access the object.
675 *
676 * @returns true if a decision has been made.
677 * @returns false if the default access policy should be applied.
678 *
679 * @param pObj The object in question.
680 * @param pSession The session wanting to access the object.
681 * @param pszObjName The object name, can be NULL.
682 * @param prc Where to store the result when returning true.
683 */
684bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
685{
686 NOREF(pObj);
687 NOREF(pSession);
688 NOREF(pszObjName);
689 NOREF(prc);
690 return false;
691}
692
693
694bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
695{
696 NOREF(pDevExt);
697 return false;
698}
699
700
701/**
702 * Converts a supdrv error code to a darwin error code.
703 *
704 * @returns corresponding darwin error code.
705 * @param rc supdrv error code (SUPDRV_ERR_* defines).
706 */
707static int VBoxDrvDarwinErr2DarwinErr(int rc)
708{
709 switch (rc)
710 {
711 case 0: return 0;
712 case SUPDRV_ERR_GENERAL_FAILURE: return EACCES;
713 case SUPDRV_ERR_INVALID_PARAM: return EINVAL;
714 case SUPDRV_ERR_INVALID_MAGIC: return EILSEQ;
715 case SUPDRV_ERR_INVALID_HANDLE: return ENXIO;
716 case SUPDRV_ERR_INVALID_POINTER: return EFAULT;
717 case SUPDRV_ERR_LOCK_FAILED: return ENOLCK;
718 case SUPDRV_ERR_ALREADY_LOADED: return EEXIST;
719 case SUPDRV_ERR_PERMISSION_DENIED: return EPERM;
720 case SUPDRV_ERR_VERSION_MISMATCH: return ENOSYS;
721 }
722
723 return EPERM;
724}
725
726
727/** @todo move this to assembly where a simple "jmp printf" will to the trick. */
728RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
729{
730 va_list args;
731 char szMsg[512];
732
733 va_start(args, pszFormat);
734 vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, args);
735 va_end(args);
736
737 szMsg[sizeof(szMsg) - 1] = '\0';
738 printf("%s", szMsg);
739 return 0;
740}
741
742
743/** Runtime assert implementation for Darwin Ring-0. */
744RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
745{
746 printf("!!Assertion Failed!!\n"
747 "Expression: %s\n"
748 "Location : %s(%d) %s\n",
749 pszExpr, pszFile, uLine, pszFunction);
750}
751
752
753/** Runtime assert implementation for the Darwin Ring-0 driver.
754 * @todo this one needs fixing! */
755RTDECL(void) AssertMsg2(const char *pszFormat, ...)
756{ /* forwarder. */
757 va_list ap;
758 char msg[256];
759
760 va_start(ap, pszFormat);
761 vsnprintf(msg, sizeof(msg) - 1, pszFormat, ap);
762 msg[sizeof(msg) - 1] = '\0';
763 printf("%s", msg);
764 va_end(ap);
765}
766
767
768/*
769 *
770 * org_virtualbox_SupDrv
771 *
772 */
773
774
775/**
776 * Initialize the object.
777 */
778bool org_virtualbox_SupDrv::init(OSDictionary *pDictionary)
779{
780 dprintf(("org_virtualbox_SupDrv::init([%p], %p)\n", this, pDictionary));
781 if (IOService::init(pDictionary))
782 {
783 /* init members. */
784 return true;
785 }
786 return false;
787}
788
789
790/**
791 * Free the object.
792 */
793void org_virtualbox_SupDrv::free(void)
794{
795 dprintf(("IOService::free([%p])\n", this));
796 IOService::free();
797}
798
799
800/**
801 * Check if it's ok to start this service.
802 * It's always ok by us, so it's up to IOService to decide really.
803 */
804IOService *org_virtualbox_SupDrv::probe(IOService *pProvider, SInt32 *pi32Score)
805{
806 dprintf(("org_virtualbox_SupDrv::probe([%p])\n", this));
807 return IOService::probe(pProvider, pi32Score);
808}
809
810
811/**
812 * Start this service.
813 */
814bool org_virtualbox_SupDrv::start(IOService *pProvider)
815{
816 dprintf(("org_virtualbox_SupDrv::start([%p])\n", this));
817
818 if (IOService::start(pProvider))
819 {
820 /* register the service. */
821 registerService();
822 return true;
823 }
824 return false;
825}
826
827
828/**
829 * Stop this service.
830 */
831void org_virtualbox_SupDrv::stop(IOService *pProvider)
832{
833 dprintf(("org_virtualbox_SupDrv::stop([%p], %p)\n", this, pProvider));
834 IOService::stop(pProvider);
835}
836
837
838/**
839 * Termination request.
840 *
841 * @return true if we're ok with shutting down now, false if we're not.
842 * @param fOptions Flags.
843 */
844bool org_virtualbox_SupDrv::terminate(IOOptionBits fOptions)
845{
846 bool fRc;
847 dprintf(("org_virtualbox_SupDrv::terminate: reference_count=%d g_cSessions=%d (fOptions=%#x)\n",
848 KMOD_INFO_NAME.reference_count, ASMAtomicUoReadS32(&g_cSessions), fOptions));
849 if ( KMOD_INFO_NAME.reference_count != 0
850 || ASMAtomicUoReadS32(&g_cSessions))
851 fRc = false;
852 else
853 fRc = IOService::terminate(fOptions);
854 dprintf(("org_virtualbox_SupDrv::terminate: returns %d\n", fRc));
855 return fRc;
856}
857
858
859/*
860 *
861 * org_virtualbox_SupDrvClient
862 *
863 */
864
865
866/**
867 * Initializer called when the client opens the service.
868 */
869bool org_virtualbox_SupDrvClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
870{
871 dprintf(("org_virtualbox_SupDrvClient::initWithTask([%p], %#x, %p, %#x)\n", this, OwningTask, pvSecurityId, u32Type));
872
873 if (!OwningTask)
874 return false;
875 if (IOUserClient::initWithTask(OwningTask, pvSecurityId , u32Type))
876 {
877 m_Task = OwningTask;
878 m_pSession = NULL;
879 m_pProvider = NULL;
880 return true;
881 }
882 return false;
883}
884
885
886/**
887 * Start the client service.
888 */
889bool org_virtualbox_SupDrvClient::start(IOService *pProvider)
890{
891 dprintf(("org_virtualbox_SupDrvClient::start([%p], %p)\n", this, pProvider));
892 if (IOUserClient::start(pProvider))
893 {
894 m_pProvider = OSDynamicCast(org_virtualbox_SupDrv, pProvider);
895 if (m_pProvider)
896 {
897 /* this is where we could create the section. */
898 return true;
899 }
900 dprintf(("org_virtualbox_SupDrvClient::start: %p isn't org_virtualbox_SupDrv\n", pProvider));
901 }
902 return false;
903}
904
905
906/**
907 * Client exits normally.
908 */
909IOReturn org_virtualbox_SupDrvClient::clientClose(void)
910{
911 dprintf(("org_virtualbox_SupDrvClient::clientClose([%p])\n", this));
912
913 m_pProvider = NULL;
914 terminate();
915
916 return kIOReturnSuccess;
917}
918
919
920/**
921 * The client exits abnormally / forgets to do cleanups.
922 */
923IOReturn org_virtualbox_SupDrvClient::clientDied(void)
924{
925 dprintf(("org_virtualbox_SupDrvClient::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n",
926 this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
927
928 /*
929 * Do early session cleanup (if there is a session) so
930 * we avoid hanging in vm_map_remove().
931 */
932 const RTR0PROCESS R0Process = (RTR0PROCESS)m_Task;
933 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
934 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
935 for (unsigned i = 0; i < RT_ELEMENTS(g_apSessionHashTab); i++)
936 {
937 for (PSUPDRVSESSION pSession = g_apSessionHashTab[i]; pSession; pSession = pSession->pNextHash)
938 {
939 dprintf2(("pSession=%p R0Process=%p (=? %p)\n", pSession, pSession->R0Process, R0Process));
940 if (pSession->R0Process == R0Process)
941 {
942 /*
943 * It is safe to leave the spinlock here; the session shouldn't be able
944 * to go away while we're cleaning it up, changes to pNextHash will not
945 * harm us, and new sessions can't possibly be added for this process.
946 */
947 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
948 supdrvCleanupSession(&g_DevExt, pSession);
949 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
950 }
951 }
952 }
953 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
954
955 /* IOUserClient::clientDied() calls close... */
956 return IOUserClient::clientDied();
957}
958
959
960/**
961 * Terminate the service (initiate the destruction).
962 */
963bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
964{
965 dprintf(("org_virtualbox_SupDrvClient::terminate([%p], %#x)\n", this, fOptions));
966 return IOUserClient::terminate(fOptions);
967}
968
969
970/**
971 * The final stage of the client service destruction.
972 */
973bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
974{
975 dprintf(("org_virtualbox_SupDrvClient::finalize([%p], %#x)\n", this, fOptions));
976 return IOUserClient::finalize(fOptions);
977}
978
979
980/**
981 * Stop the client service.
982 */
983void org_virtualbox_SupDrvClient::stop(IOService *pProvider)
984{
985 dprintf(("org_virtualbox_SupDrvClient::stop([%p])\n", this));
986 IOUserClient::stop(pProvider);
987}
988
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