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
|
---|
66 | static kern_return_t VBoxSupDrvStart(struct kmod_info *pKModInfo, void *pvData);
|
---|
67 | static kern_return_t VBoxSupDrvStop(struct kmod_info *pKModInfo, void *pvData);
|
---|
68 |
|
---|
69 | static int VBoxSupDrvOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
|
---|
70 | static int VBoxSupDrvClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess);
|
---|
71 | static int VBoxSupDrvIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess);
|
---|
72 | static int VBoxSupDrvIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess);
|
---|
73 |
|
---|
74 | static 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 | */
|
---|
85 | class org_virtualbox_SupDrv : public IOService
|
---|
86 | {
|
---|
87 | OSDeclareDefaultStructors(org_virtualbox_SupDrv)
|
---|
88 |
|
---|
89 | public:
|
---|
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 |
|
---|
97 | OSDefineMetaClassAndStructors(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 | */
|
---|
105 | class org_virtualbox_SupDrvClient : public IOUserClient
|
---|
106 | {
|
---|
107 | OSDeclareDefaultStructors(org_virtualbox_SupDrvClient)
|
---|
108 |
|
---|
109 | private:
|
---|
110 | PSUPDRVSESSION m_pSession; /**< The session. */
|
---|
111 | task_t m_Task; /**< The client task. */
|
---|
112 | org_virtualbox_SupDrv *m_pProvider; /**< The service provider. */
|
---|
113 |
|
---|
114 | public:
|
---|
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 |
|
---|
124 | OSDefineMetaClassAndStructors(org_virtualbox_SupDrvClient, IOUserClient)
|
---|
125 |
|
---|
126 |
|
---|
127 |
|
---|
128 | /*******************************************************************************
|
---|
129 | * Global Variables *
|
---|
130 | *******************************************************************************/
|
---|
131 | /**
|
---|
132 | * Declare the module stuff.
|
---|
133 | */
|
---|
134 | __BEGIN_DECLS
|
---|
135 | extern kern_return_t _start(struct kmod_info *pKModInfo, void *pvData);
|
---|
136 | extern 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 |
|
---|
141 | KMOD_EXPLICIT_DECL(VBoxDrv, VBOX_VERSION_STRING, _start, _stop)
|
---|
142 | kmod_start_func_t *_realmain = VBoxSupDrvStart;
|
---|
143 | kmod_stop_func_t *_antimain = VBoxSupDrvStop;
|
---|
144 | int _kext_apple_cc = __APPLE_CC__;
|
---|
145 | __END_DECLS
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Device extention & session data association structure.
|
---|
150 | */
|
---|
151 | static SUPDRVDEVEXT g_DevExt;
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * The character device switch table for the driver.
|
---|
155 | */
|
---|
156 | static 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. */
|
---|
176 | static int g_iMajorDeviceNo = -1;
|
---|
177 | /** Registered devfs device handle. */
|
---|
178 | static void *g_hDevFsDevice = NULL;
|
---|
179 |
|
---|
180 | /** Spinlock protecting g_apSessionHashTab. */
|
---|
181 | static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
|
---|
182 | /** Hash table */
|
---|
183 | static 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 | */
|
---|
191 | static 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 | */
|
---|
258 | static 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 | */
|
---|
297 | static 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 | static int VBoxSupDrvClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
|
---|
340 | {
|
---|
341 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
342 | const RTPROCESS Process = proc_pid(pProcess);
|
---|
343 | const unsigned iHash = SESSION_HASH(Process);
|
---|
344 | PSUPDRVSESSION pSession;
|
---|
345 |
|
---|
346 | dprintf(("VBoxSupDrvClose: pid=%d\n", (int)Process));
|
---|
347 |
|
---|
348 | /*
|
---|
349 | * Remove from the hash table.
|
---|
350 | */
|
---|
351 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
352 | pSession = g_apSessionHashTab[iHash];
|
---|
353 | if (pSession)
|
---|
354 | {
|
---|
355 | if (pSession->Process == Process)
|
---|
356 | {
|
---|
357 | g_apSessionHashTab[iHash] = pSession->pNextHash;
|
---|
358 | pSession->pNextHash = NULL;
|
---|
359 | }
|
---|
360 | else
|
---|
361 | {
|
---|
362 | PSUPDRVSESSION pPrev = pSession;
|
---|
363 | pSession = pSession->pNextHash;
|
---|
364 | while (pSession)
|
---|
365 | {
|
---|
366 | if (pSession->Process == Process)
|
---|
367 | {
|
---|
368 | pPrev->pNextHash = pSession->pNextHash;
|
---|
369 | pSession->pNextHash = NULL;
|
---|
370 | break;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /* next */
|
---|
374 | pPrev = pSession;
|
---|
375 | pSession = pSession->pNextHash;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | }
|
---|
379 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
380 | if (!pSession)
|
---|
381 | {
|
---|
382 | OSDBGPRINT(("VBoxSupDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
|
---|
383 | return EINVAL;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * Close the session.
|
---|
388 | */
|
---|
389 | supdrvCloseSession(&g_DevExt, pSession);
|
---|
390 | return 0;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Device I/O Control entry point.
|
---|
396 | *
|
---|
397 | * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
|
---|
398 | * @param Dev The device number (major+minor).
|
---|
399 | * @param iCmd The IOCtl command.
|
---|
400 | * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
|
---|
401 | * @param fFlags Flag saying we're a character device (like we didn't know already).
|
---|
402 | * @param pProcess The process issuing this request.
|
---|
403 | */
|
---|
404 | static int VBoxSupDrvIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
|
---|
405 | {
|
---|
406 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
407 | const RTPROCESS Process = proc_pid(pProcess);
|
---|
408 | const unsigned iHash = SESSION_HASH(Process);
|
---|
409 | PSUPDRVSESSION pSession;
|
---|
410 |
|
---|
411 | /*
|
---|
412 | * Find the session.
|
---|
413 | */
|
---|
414 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
415 | pSession = g_apSessionHashTab[iHash];
|
---|
416 | if (pSession && pSession->Process != Process)
|
---|
417 | {
|
---|
418 | do pSession = pSession->pNextHash;
|
---|
419 | while (pSession && pSession->Process != Process);
|
---|
420 | }
|
---|
421 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
422 | if (!pSession)
|
---|
423 | {
|
---|
424 | OSDBGPRINT(("VBoxSupDrvIoctl: WHUT?!? pSession == NULL! This must be a mistake... pid=%d\n", (int)Process));
|
---|
425 | return EINVAL;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /*
|
---|
429 | * Deal with the two high-speed IOCtl that takes it's arguments from
|
---|
430 | * the session and iCmd, and only returns a VBox status code.
|
---|
431 | */
|
---|
432 | if ( iCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
433 | || iCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
|
---|
434 | || iCmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
435 | return supdrvIOCtlFast(iCmd, &g_DevExt, pSession);
|
---|
436 | return VBoxSupDrvIOCtlSlow(pSession, iCmd, pData, pProcess);
|
---|
437 | }
|
---|
438 |
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Worker for VBoxSupDrvIOCtl that takes the slow IOCtl functions.
|
---|
442 | *
|
---|
443 | * @returns Darwin errno.
|
---|
444 | *
|
---|
445 | * @param pSession The session.
|
---|
446 | * @param iCmd The IOCtl command.
|
---|
447 | * @param pData Pointer to the kernel copy of the SUPDRVIOCTLDATA buffer.
|
---|
448 | * @param pProcess The calling process.
|
---|
449 | */
|
---|
450 | static int VBoxSupDrvIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess)
|
---|
451 | {
|
---|
452 | int rc;
|
---|
453 | void *pvPageBuf = NULL;
|
---|
454 | void *pvBuf = NULL;
|
---|
455 | unsigned long cbBuf = 0;
|
---|
456 | unsigned cbOut = 0;
|
---|
457 | PSUPDRVIOCTLDATA pArgs = (PSUPDRVIOCTLDATA)pData;
|
---|
458 | dprintf(("VBoxSupDrvIOCtl: pSession=%p iCmd=%p pData=%p pProcess=%p\n", pSession, iCmd, pData, pProcess));
|
---|
459 |
|
---|
460 | /*
|
---|
461 | * Copy ioctl data structure from user space.
|
---|
462 | */
|
---|
463 | if (IOCPARM_LEN(iCmd) != sizeof(SUPDRVIOCTLDATA))
|
---|
464 | {
|
---|
465 | dprintf(("VBoxSupDrvIOCtl: incorrect input length! cbArgs=%d\n", IOCPARM_LEN(iCmd)));
|
---|
466 | return EINVAL;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * Allocate and copy user space input data buffer to kernel space.
|
---|
471 | */
|
---|
472 | if (pArgs->cbIn > 0 || pArgs->cbOut > 0)
|
---|
473 | {
|
---|
474 | cbBuf = max(pArgs->cbIn, pArgs->cbOut);
|
---|
475 | pvBuf = RTMemTmpAlloc(cbBuf);
|
---|
476 | if (pvBuf == NULL)
|
---|
477 | pvPageBuf = pvBuf = IOMallocAligned(cbBuf, 8);
|
---|
478 | if (pvBuf == NULL)
|
---|
479 | {
|
---|
480 | dprintf(("VBoxSupDrvIOCtl: failed to allocate buffer of %d bytes.\n", cbBuf));
|
---|
481 | return ENOMEM;
|
---|
482 | }
|
---|
483 | rc = copyin((const user_addr_t)pArgs->pvIn, pvBuf, pArgs->cbIn);
|
---|
484 | if (rc)
|
---|
485 | {
|
---|
486 | dprintf(("VBoxSupDrvIOCtl: copyin(%p,,%d) failed.\n", pArgs->pvIn, cbBuf));
|
---|
487 | if (pvPageBuf)
|
---|
488 | IOFreeAligned(pvPageBuf, cbBuf);
|
---|
489 | else
|
---|
490 | RTMemTmpFree(pvBuf);
|
---|
491 | return rc;
|
---|
492 | }
|
---|
493 | }
|
---|
494 |
|
---|
495 | /*
|
---|
496 | * Process the IOCtl.
|
---|
497 | */
|
---|
498 | rc = supdrvIOCtl(iCmd, &g_DevExt, pSession,
|
---|
499 | pvBuf, pArgs->cbIn, pvBuf, pArgs->cbOut, &cbOut);
|
---|
500 |
|
---|
501 | /*
|
---|
502 | * Copy ioctl data and output buffer back to user space.
|
---|
503 | */
|
---|
504 | if (rc)
|
---|
505 | {
|
---|
506 | dprintf(("VBoxSupDrvIOCtl: pid=%d iCmd=%x pData=%p failed, rc=%d (darwin rc=%d)\n",
|
---|
507 | proc_pid(pProcess), iCmd, (void *)pData, rc, VBoxSupDrvErr2DarwinErr(rc)));
|
---|
508 | rc = VBoxSupDrvErr2DarwinErr(rc);
|
---|
509 | }
|
---|
510 | else if (cbOut > 0)
|
---|
511 | {
|
---|
512 | if (pvBuf != NULL && cbOut <= cbBuf)
|
---|
513 | {
|
---|
514 | int rc2 = copyout(pvBuf, (user_addr_t)pArgs->pvOut, cbOut);
|
---|
515 | if (rc2)
|
---|
516 | {
|
---|
517 | dprintf(("VBoxSupDrvIOCtl: copyout(,%p,%d) failed.\n", pArgs->pvOut, cbBuf));
|
---|
518 | rc = rc2;
|
---|
519 | }
|
---|
520 | }
|
---|
521 | else
|
---|
522 | {
|
---|
523 | dprintf(("WHAT!?! supdrvIOCtl messed up! cbOut=%d cbBuf=%d pvBuf=%p\n", cbOut, cbBuf, pvBuf));
|
---|
524 | rc = EPERM;
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | if (pvPageBuf)
|
---|
529 | IOFreeAligned(pvPageBuf, cbBuf);
|
---|
530 | else if (pvBuf)
|
---|
531 | RTMemTmpFree(pvBuf);
|
---|
532 |
|
---|
533 | dprintf2(("VBoxSupDrvIOCtl: returns %d\n", rc));
|
---|
534 | return rc;
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * Initializes any OS specific object creator fields.
|
---|
541 | */
|
---|
542 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
543 | {
|
---|
544 | NOREF(pObj);
|
---|
545 | NOREF(pSession);
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Checks if the session can access the object.
|
---|
551 | *
|
---|
552 | * @returns true if a decision has been made.
|
---|
553 | * @returns false if the default access policy should be applied.
|
---|
554 | *
|
---|
555 | * @param pObj The object in question.
|
---|
556 | * @param pSession The session wanting to access the object.
|
---|
557 | * @param pszObjName The object name, can be NULL.
|
---|
558 | * @param prc Where to store the result when returning true.
|
---|
559 | */
|
---|
560 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
561 | {
|
---|
562 | NOREF(pObj);
|
---|
563 | NOREF(pSession);
|
---|
564 | NOREF(pszObjName);
|
---|
565 | NOREF(prc);
|
---|
566 | return false;
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Converts a supdrv error code to a darwin error code.
|
---|
572 | *
|
---|
573 | * @returns corresponding darwin error code.
|
---|
574 | * @param rc supdrv error code (SUPDRV_ERR_* defines).
|
---|
575 | */
|
---|
576 | static int VBoxSupDrvErr2DarwinErr(int rc)
|
---|
577 | {
|
---|
578 | switch (rc)
|
---|
579 | {
|
---|
580 | case 0: return 0;
|
---|
581 | case SUPDRV_ERR_GENERAL_FAILURE: return EACCES;
|
---|
582 | case SUPDRV_ERR_INVALID_PARAM: return EINVAL;
|
---|
583 | case SUPDRV_ERR_INVALID_MAGIC: return EILSEQ;
|
---|
584 | case SUPDRV_ERR_INVALID_HANDLE: return ENXIO;
|
---|
585 | case SUPDRV_ERR_INVALID_POINTER: return EFAULT;
|
---|
586 | case SUPDRV_ERR_LOCK_FAILED: return ENOLCK;
|
---|
587 | case SUPDRV_ERR_ALREADY_LOADED: return EEXIST;
|
---|
588 | case SUPDRV_ERR_PERMISSION_DENIED: return EPERM;
|
---|
589 | case SUPDRV_ERR_VERSION_MISMATCH: return ENOSYS;
|
---|
590 | }
|
---|
591 |
|
---|
592 | return EPERM;
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | /** @todo move this to assembly where a simple "jmp printf" will to the trick. */
|
---|
597 | RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
598 | {
|
---|
599 | va_list args;
|
---|
600 | char szMsg[512];
|
---|
601 |
|
---|
602 | va_start(args, pszFormat);
|
---|
603 | vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, args);
|
---|
604 | va_end(args);
|
---|
605 |
|
---|
606 | szMsg[sizeof(szMsg) - 1] = '\0';
|
---|
607 | printf("%s", szMsg);
|
---|
608 | return 0;
|
---|
609 | }
|
---|
610 |
|
---|
611 |
|
---|
612 | /** Runtime assert implementation for Darwin Ring-0. */
|
---|
613 | RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
|
---|
614 | {
|
---|
615 | printf("!!Assertion Failed!!\n"
|
---|
616 | "Expression: %s\n"
|
---|
617 | "Location : %s(%d) %s\n",
|
---|
618 | pszExpr, pszFile, uLine, pszFunction);
|
---|
619 | }
|
---|
620 |
|
---|
621 |
|
---|
622 | /** Runtime assert implementation for the Darwin Ring-0 driver.
|
---|
623 | * @todo this one needs fixing! */
|
---|
624 | RTDECL(void) AssertMsg2(const char *pszFormat, ...)
|
---|
625 | { /* forwarder. */
|
---|
626 | va_list ap;
|
---|
627 | char msg[256];
|
---|
628 |
|
---|
629 | va_start(ap, pszFormat);
|
---|
630 | vsnprintf(msg, sizeof(msg) - 1, pszFormat, ap);
|
---|
631 | msg[sizeof(msg) - 1] = '\0';
|
---|
632 | printf("%s", msg);
|
---|
633 | va_end(ap);
|
---|
634 | }
|
---|
635 |
|
---|
636 |
|
---|
637 | /*
|
---|
638 | *
|
---|
639 | * org_virtualbox_SupDrv
|
---|
640 | *
|
---|
641 | */
|
---|
642 |
|
---|
643 |
|
---|
644 | /**
|
---|
645 | * Initialize the object.
|
---|
646 | */
|
---|
647 | bool org_virtualbox_SupDrv::init(OSDictionary *pDictionary)
|
---|
648 | {
|
---|
649 | dprintf(("org_virtualbox_SupDrv::init([%p], %p)\n", this, pDictionary));
|
---|
650 | if (IOService::init(pDictionary))
|
---|
651 | {
|
---|
652 | /* init members. */
|
---|
653 | return true;
|
---|
654 | }
|
---|
655 | return false;
|
---|
656 | }
|
---|
657 |
|
---|
658 |
|
---|
659 | /**
|
---|
660 | * Free the object.
|
---|
661 | */
|
---|
662 | void org_virtualbox_SupDrv::free(void)
|
---|
663 | {
|
---|
664 | dprintf(("IOService::free([%p])\n", this));
|
---|
665 | IOService::free();
|
---|
666 | }
|
---|
667 |
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * Check if it's ok to start this service.
|
---|
671 | * It's always ok by us, so it's up to IOService to decide really.
|
---|
672 | */
|
---|
673 | IOService *org_virtualbox_SupDrv::probe(IOService *pProvider, SInt32 *pi32Score)
|
---|
674 | {
|
---|
675 | dprintf(("org_virtualbox_SupDrv::probe([%p])\n", this));
|
---|
676 | return IOService::probe(pProvider, pi32Score);
|
---|
677 | }
|
---|
678 |
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Start this service.
|
---|
682 | */
|
---|
683 | bool org_virtualbox_SupDrv::start(IOService *pProvider)
|
---|
684 | {
|
---|
685 | dprintf(("org_virtualbox_SupDrv::start([%p])\n", this));
|
---|
686 |
|
---|
687 | if (IOService::start(pProvider))
|
---|
688 | {
|
---|
689 | /* register the service. */
|
---|
690 | registerService();
|
---|
691 | return true;
|
---|
692 | }
|
---|
693 | return false;
|
---|
694 | }
|
---|
695 |
|
---|
696 |
|
---|
697 | /**
|
---|
698 | * Stop this service.
|
---|
699 | */
|
---|
700 | void org_virtualbox_SupDrv::stop(IOService *pProvider)
|
---|
701 | {
|
---|
702 | dprintf(("org_virtualbox_SupDrv::stop([%p], %p)\n", this, pProvider));
|
---|
703 | IOService::stop(pProvider);
|
---|
704 | }
|
---|
705 |
|
---|
706 |
|
---|
707 | /*
|
---|
708 | *
|
---|
709 | * org_virtualbox_SupDrvClient
|
---|
710 | *
|
---|
711 | */
|
---|
712 |
|
---|
713 |
|
---|
714 | /**
|
---|
715 | * Initializer called when the client opens the service.
|
---|
716 | */
|
---|
717 | bool org_virtualbox_SupDrvClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
|
---|
718 | {
|
---|
719 | dprintf(("org_virtualbox_SupDrvClient::initWithTask([%p], %#x, %p, %#x)\n", this, OwningTask, pvSecurityId, u32Type));
|
---|
720 |
|
---|
721 | if (!OwningTask)
|
---|
722 | return false;
|
---|
723 | if (IOUserClient::initWithTask(OwningTask, pvSecurityId , u32Type))
|
---|
724 | {
|
---|
725 | m_Task = OwningTask;
|
---|
726 | m_pSession = NULL;
|
---|
727 | m_pProvider = NULL;
|
---|
728 | return true;
|
---|
729 | }
|
---|
730 | return false;
|
---|
731 | }
|
---|
732 |
|
---|
733 |
|
---|
734 | /**
|
---|
735 | * Start the client service.
|
---|
736 | */
|
---|
737 | bool org_virtualbox_SupDrvClient::start(IOService *pProvider)
|
---|
738 | {
|
---|
739 | dprintf(("org_virtualbox_SupDrvClient::start([%p], %p)\n", this, pProvider));
|
---|
740 | if (IOUserClient::start(pProvider))
|
---|
741 | {
|
---|
742 | m_pProvider = OSDynamicCast(org_virtualbox_SupDrv, pProvider);
|
---|
743 | if (m_pProvider)
|
---|
744 | {
|
---|
745 | /* this is where we could create the section. */
|
---|
746 | return true;
|
---|
747 | }
|
---|
748 | dprintf(("org_virtualbox_SupDrvClient::start: %p isn't org_virtualbox_SupDrv\n", pProvider));
|
---|
749 | }
|
---|
750 | return false;
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Client exits normally.
|
---|
756 | */
|
---|
757 | IOReturn org_virtualbox_SupDrvClient::clientClose(void)
|
---|
758 | {
|
---|
759 | dprintf(("org_virtualbox_SupDrvClient::clientClose([%p])\n", this));
|
---|
760 |
|
---|
761 | m_pProvider = NULL;
|
---|
762 | terminate();
|
---|
763 |
|
---|
764 | return kIOReturnSuccess;
|
---|
765 | }
|
---|
766 |
|
---|
767 |
|
---|
768 | /**
|
---|
769 | * The client exits abnormally / forgets to do cleanups.
|
---|
770 | */
|
---|
771 | IOReturn org_virtualbox_SupDrvClient::clientDied(void)
|
---|
772 | {
|
---|
773 | dprintf(("org_virtualbox_SupDrvClient::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n",
|
---|
774 | this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
|
---|
775 |
|
---|
776 | /*
|
---|
777 | * Do early session cleanup (if there is a session) so
|
---|
778 | * we avoid hanging in vm_map_remove().
|
---|
779 | */
|
---|
780 | const RTR0PROCESS R0Process = (RTR0PROCESS)m_Task;
|
---|
781 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
782 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
783 | for (unsigned i = 0; i < RT_ELEMENTS(g_apSessionHashTab); i++)
|
---|
784 | {
|
---|
785 | for (PSUPDRVSESSION pSession = g_apSessionHashTab[i]; pSession; pSession = pSession->pNextHash)
|
---|
786 | {
|
---|
787 | dprintf2(("pSession=%p R0Process=%p (=? %p)\n", pSession, pSession->R0Process, R0Process));
|
---|
788 | if (pSession->R0Process == R0Process)
|
---|
789 | {
|
---|
790 | /*
|
---|
791 | * It is safe to leave the spinlock here; the session shouldn't be able
|
---|
792 | * to go away while we're cleaning it up, changes to pNextHash will not
|
---|
793 | * harm us, and new sessions can't possibly be added for this process.
|
---|
794 | */
|
---|
795 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
796 | supdrvCleanupSession(&g_DevExt, pSession);
|
---|
797 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
798 | }
|
---|
799 | }
|
---|
800 | }
|
---|
801 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
802 |
|
---|
803 | /* IOUserClient::clientDied() calls close... */
|
---|
804 | return IOUserClient::clientDied();
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | /**
|
---|
809 | * Terminate the service (initiate the destruction).
|
---|
810 | */
|
---|
811 | bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
|
---|
812 | {
|
---|
813 | dprintf(("org_virtualbox_SupDrvClient::terminate([%p], %#x)\n", this, fOptions));
|
---|
814 | return IOUserClient::terminate(fOptions);
|
---|
815 | }
|
---|
816 |
|
---|
817 |
|
---|
818 | /**
|
---|
819 | * The final stage of the client service destruction.
|
---|
820 | */
|
---|
821 | bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
|
---|
822 | {
|
---|
823 | dprintf(("org_virtualbox_SupDrvClient::finalize([%p], %#x)\n", this, fOptions));
|
---|
824 | return IOUserClient::finalize(fOptions);
|
---|
825 | }
|
---|
826 |
|
---|
827 |
|
---|
828 | /**
|
---|
829 | * Stop the client service.
|
---|
830 | */
|
---|
831 | void org_virtualbox_SupDrvClient::stop(IOService *pProvider)
|
---|
832 | {
|
---|
833 | dprintf(("org_virtualbox_SupDrvClient::stop([%p])\n", this));
|
---|
834 | IOUserClient::stop(pProvider);
|
---|
835 | }
|
---|
836 |
|
---|