VirtualBox

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

最後變更 在這個檔案從10729是 10714,由 vboxsync 提交於 17 年 前

darwin has AssertMsg1/2 in IPRT as everyone should.

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

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