VirtualBox

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

Last change on this file since 6857 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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