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 | /** @todo the UID, GID and mode mask should be configurable! This isn't very secure... */
|
---|
222 | g_hDevFsDevice = devfs_make_node(makedev(g_iMajorDeviceNo, 0), DEVFS_CHAR,
|
---|
223 | UID_ROOT, GID_WHEEL, 0666, DEVICE_NAME);
|
---|
224 | if (g_hDevFsDevice)
|
---|
225 | {
|
---|
226 | OSDBGPRINT(("VBoxDrv: Successfully started. (major=%d)\n", g_iMajorDeviceNo));
|
---|
227 | return KMOD_RETURN_SUCCESS;
|
---|
228 | }
|
---|
229 |
|
---|
230 | OSDBGPRINT(("VBoxDrv: devfs_make_node(makedev(%d,0),,,,%s) failed\n",
|
---|
231 | g_iMajorDeviceNo, DEVICE_NAME));
|
---|
232 | cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
|
---|
233 | g_iMajorDeviceNo = -1;
|
---|
234 | }
|
---|
235 | else
|
---|
236 | OSDBGPRINT(("VBoxDrv: cdevsw_add failed (%d)\n", g_iMajorDeviceNo));
|
---|
237 | RTSpinlockDestroy(g_Spinlock);
|
---|
238 | g_Spinlock = NIL_RTSPINLOCK;
|
---|
239 | }
|
---|
240 | else
|
---|
241 | OSDBGPRINT(("VBoxDrv: RTSpinlockCreate failed (rc=%d)\n", rc));
|
---|
242 | supdrvDeleteDevExt(&g_DevExt);
|
---|
243 | }
|
---|
244 | else
|
---|
245 | OSDBGPRINT(("VBoxDrv: failed to initialize device extension (rc=%d)\n", rc));
|
---|
246 | RTR0Term();
|
---|
247 | }
|
---|
248 | else
|
---|
249 | OSDBGPRINT(("VBoxDrv: failed to initialize IPRT (rc=%d)\n", rc));
|
---|
250 |
|
---|
251 | memset(&g_DevExt, 0, sizeof(g_DevExt));
|
---|
252 | return KMOD_RETURN_FAILURE;
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Stop the kernel module.
|
---|
258 | */
|
---|
259 | static kern_return_t VBoxSupDrvStop(struct kmod_info *pKModInfo, void *pvData)
|
---|
260 | {
|
---|
261 | int rc;
|
---|
262 | dprintf(("VBoxSupDrvStop\n"));
|
---|
263 |
|
---|
264 | /** @todo I've got a nagging feeling that we'll have to keep track of users and refuse
|
---|
265 | * unloading if we're busy. Investigate and implement this! */
|
---|
266 |
|
---|
267 | /*
|
---|
268 | * Undo the work done during start (in reverse order).
|
---|
269 | */
|
---|
270 | devfs_remove(g_hDevFsDevice);
|
---|
271 | g_hDevFsDevice = NULL;
|
---|
272 |
|
---|
273 | rc = cdevsw_remove(g_iMajorDeviceNo, &g_DevCW);
|
---|
274 | Assert(rc == g_iMajorDeviceNo);
|
---|
275 | g_iMajorDeviceNo = -1;
|
---|
276 |
|
---|
277 | rc = supdrvDeleteDevExt(&g_DevExt);
|
---|
278 | AssertRC(rc);
|
---|
279 |
|
---|
280 | rc = RTSpinlockDestroy(g_Spinlock);
|
---|
281 | AssertRC(rc);
|
---|
282 | g_Spinlock = NIL_RTSPINLOCK;
|
---|
283 |
|
---|
284 | RTR0Term();
|
---|
285 |
|
---|
286 | memset(&g_DevExt, 0, sizeof(g_DevExt));
|
---|
287 | dprintf(("VBoxSupDrvStop - done\n"));
|
---|
288 | return KMOD_RETURN_SUCCESS;
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Device open. Called on open /dev/vboxdrv
|
---|
294 | *
|
---|
295 | * @param pInode Pointer to inode info structure.
|
---|
296 | * @param pFilp Associated file pointer.
|
---|
297 | */
|
---|
298 | static int VBoxSupDrvOpen(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
|
---|
299 | {
|
---|
300 | int rc;
|
---|
301 | PSUPDRVSESSION pSession;
|
---|
302 | #ifdef DEBUG
|
---|
303 | char szName[128];
|
---|
304 | szName[0] = '\0';
|
---|
305 | proc_name(proc_pid(pProcess), szName, sizeof(szName));
|
---|
306 | dprintf(("VBoxSupDrvOpen: pid=%d '%s'\n", proc_pid(pProcess), szName));
|
---|
307 | #endif
|
---|
308 |
|
---|
309 | /*
|
---|
310 | * Create a new session.
|
---|
311 | */
|
---|
312 | rc = supdrvCreateSession(&g_DevExt, &pSession);
|
---|
313 | if (RT_SUCCESS(rc))
|
---|
314 | {
|
---|
315 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
316 | unsigned iHash;
|
---|
317 | struct ucred *pCred = proc_ucred(pProcess);
|
---|
318 | if (pCred)
|
---|
319 | {
|
---|
320 | pSession->Uid = pCred->cr_uid;
|
---|
321 | pSession->Gid = pCred->cr_gid;
|
---|
322 | }
|
---|
323 | pSession->Process = RTProcSelf();
|
---|
324 | pSession->R0Process = RTR0ProcHandleSelf();
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * Insert it into the hash table.
|
---|
328 | */
|
---|
329 | iHash = SESSION_HASH(pSession->Process);
|
---|
330 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
331 | pSession->pNextHash = g_apSessionHashTab[iHash];
|
---|
332 | g_apSessionHashTab[iHash] = pSession;
|
---|
333 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
334 | }
|
---|
335 |
|
---|
336 | #if 1
|
---|
337 | dprintf(("VBoxSupDrvOpen: g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, proc_pid(pProcess)));
|
---|
338 | #else
|
---|
339 | OSDBGPRINT(("VBoxSupDrvOpen: pid=%d '%s' pSession=%p rc=%d\n", proc_pid(pProcess), szName, pSession, rc));
|
---|
340 | #endif
|
---|
341 | return VBoxSupDrvErr2DarwinErr(rc);
|
---|
342 | }
|
---|
343 |
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Close device.
|
---|
347 | */
|
---|
348 | static int VBoxSupDrvClose(dev_t Dev, int fFlags, int fDevType, struct proc *pProcess)
|
---|
349 | {
|
---|
350 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
351 | const RTPROCESS Process = proc_pid(pProcess);
|
---|
352 | const unsigned iHash = SESSION_HASH(Process);
|
---|
353 | PSUPDRVSESSION pSession;
|
---|
354 |
|
---|
355 | dprintf(("VBoxSupDrvClose: pid=%d\n", (int)Process));
|
---|
356 |
|
---|
357 | /*
|
---|
358 | * Remove from the hash table.
|
---|
359 | */
|
---|
360 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
361 | pSession = g_apSessionHashTab[iHash];
|
---|
362 | if (pSession)
|
---|
363 | {
|
---|
364 | if (pSession->Process == Process)
|
---|
365 | {
|
---|
366 | g_apSessionHashTab[iHash] = pSession->pNextHash;
|
---|
367 | pSession->pNextHash = NULL;
|
---|
368 | }
|
---|
369 | else
|
---|
370 | {
|
---|
371 | PSUPDRVSESSION pPrev = pSession;
|
---|
372 | pSession = pSession->pNextHash;
|
---|
373 | while (pSession)
|
---|
374 | {
|
---|
375 | if (pSession->Process == Process)
|
---|
376 | {
|
---|
377 | pPrev->pNextHash = pSession->pNextHash;
|
---|
378 | pSession->pNextHash = NULL;
|
---|
379 | break;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /* next */
|
---|
383 | pPrev = pSession;
|
---|
384 | pSession = pSession->pNextHash;
|
---|
385 | }
|
---|
386 | }
|
---|
387 | }
|
---|
388 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
389 | if (!pSession)
|
---|
390 | {
|
---|
391 | OSDBGPRINT(("VBoxSupDrvClose: WHAT?!? pSession == NULL! This must be a mistake... pid=%d (close)\n",
|
---|
392 | (int)Process));
|
---|
393 | return EINVAL;
|
---|
394 | }
|
---|
395 |
|
---|
396 | /*
|
---|
397 | * Close the session.
|
---|
398 | */
|
---|
399 | supdrvCloseSession(&g_DevExt, pSession);
|
---|
400 | return 0;
|
---|
401 | }
|
---|
402 |
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * Device I/O Control entry point.
|
---|
406 | *
|
---|
407 | * @returns Darwin for slow IOCtls and VBox status code for the fast ones.
|
---|
408 | * @param Dev The device number (major+minor).
|
---|
409 | * @param iCmd The IOCtl command.
|
---|
410 | * @param pData Pointer to the data (if any it's a SUPDRVIOCTLDATA (kernel copy)).
|
---|
411 | * @param fFlags Flag saying we're a character device (like we didn't know already).
|
---|
412 | * @param pProcess The process issuing this request.
|
---|
413 | */
|
---|
414 | static int VBoxSupDrvIOCtl(dev_t Dev, u_long iCmd, caddr_t pData, int fFlags, struct proc *pProcess)
|
---|
415 | {
|
---|
416 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
417 | const RTPROCESS Process = proc_pid(pProcess);
|
---|
418 | const unsigned iHash = SESSION_HASH(Process);
|
---|
419 | PSUPDRVSESSION pSession;
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * Find the session.
|
---|
423 | */
|
---|
424 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
425 | pSession = g_apSessionHashTab[iHash];
|
---|
426 | if (pSession && pSession->Process != Process)
|
---|
427 | {
|
---|
428 | do pSession = pSession->pNextHash;
|
---|
429 | while (pSession && pSession->Process != Process);
|
---|
430 | }
|
---|
431 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
432 | if (!pSession)
|
---|
433 | {
|
---|
434 | OSDBGPRINT(("VBoxSupDrvIOCtl: WHAT?!? pSession == NULL! This must be a mistake... pid=%d iCmd=%#x\n",
|
---|
435 | (int)Process, iCmd));
|
---|
436 | return EINVAL;
|
---|
437 | }
|
---|
438 |
|
---|
439 | /*
|
---|
440 | * Deal with the two high-speed IOCtl that takes it's arguments from
|
---|
441 | * the session and iCmd, and only returns a VBox status code.
|
---|
442 | */
|
---|
443 | if ( iCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
444 | || iCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
|
---|
445 | || iCmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
446 | return supdrvIOCtlFast(iCmd, &g_DevExt, pSession);
|
---|
447 | return VBoxSupDrvIOCtlSlow(pSession, iCmd, pData, pProcess);
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Worker for VBoxSupDrvIOCtl that takes the slow IOCtl functions.
|
---|
453 | *
|
---|
454 | * @returns Darwin errno.
|
---|
455 | *
|
---|
456 | * @param pSession The session.
|
---|
457 | * @param iCmd The IOCtl command.
|
---|
458 | * @param pData Pointer to the kernel copy of the SUPDRVIOCTLDATA buffer.
|
---|
459 | * @param pProcess The calling process.
|
---|
460 | */
|
---|
461 | static int VBoxSupDrvIOCtlSlow(PSUPDRVSESSION pSession, u_long iCmd, caddr_t pData, struct proc *pProcess)
|
---|
462 | {
|
---|
463 | int rc;
|
---|
464 | void *pvPageBuf = NULL;
|
---|
465 | void *pvBuf = NULL;
|
---|
466 | unsigned long cbBuf = 0;
|
---|
467 | unsigned cbOut = 0;
|
---|
468 | PSUPDRVIOCTLDATA pArgs = (PSUPDRVIOCTLDATA)pData;
|
---|
469 | dprintf(("VBoxSupDrvIOCtlSlow: pSession=%p iCmd=%p pData=%p pProcess=%p\n", pSession, iCmd, pData, pProcess));
|
---|
470 |
|
---|
471 | /*
|
---|
472 | * Copy ioctl data structure from user space.
|
---|
473 | */
|
---|
474 | if (IOCPARM_LEN(iCmd) != sizeof(SUPDRVIOCTLDATA))
|
---|
475 | {
|
---|
476 | dprintf(("VBoxSupDrvIOCtlSlow: incorrect input length! cbArgs=%d\n", IOCPARM_LEN(iCmd)));
|
---|
477 | return EINVAL;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /*
|
---|
481 | * Allocate and copy user space input data buffer to kernel space.
|
---|
482 | */
|
---|
483 | if (pArgs->cbIn > 0 || pArgs->cbOut > 0)
|
---|
484 | {
|
---|
485 | cbBuf = max(pArgs->cbIn, pArgs->cbOut);
|
---|
486 | pvBuf = RTMemTmpAlloc(cbBuf);
|
---|
487 | if (pvBuf == NULL)
|
---|
488 | pvPageBuf = pvBuf = IOMallocAligned(cbBuf, 8);
|
---|
489 | if (pvBuf == NULL)
|
---|
490 | {
|
---|
491 | dprintf(("VBoxSupDrvIOCtlSlow: failed to allocate buffer of %d bytes.\n", cbBuf));
|
---|
492 | return ENOMEM;
|
---|
493 | }
|
---|
494 | rc = copyin((const user_addr_t)pArgs->pvIn, pvBuf, pArgs->cbIn);
|
---|
495 | if (rc)
|
---|
496 | {
|
---|
497 | dprintf(("VBoxSupDrvIOCtlSlow: copyin(%p,,%d) failed.\n", pArgs->pvIn, cbBuf));
|
---|
498 | if (pvPageBuf)
|
---|
499 | IOFreeAligned(pvPageBuf, cbBuf);
|
---|
500 | else
|
---|
501 | RTMemTmpFree(pvBuf);
|
---|
502 | return rc;
|
---|
503 | }
|
---|
504 | }
|
---|
505 |
|
---|
506 | /*
|
---|
507 | * Process the IOCtl.
|
---|
508 | */
|
---|
509 | rc = supdrvIOCtl(iCmd, &g_DevExt, pSession,
|
---|
510 | pvBuf, pArgs->cbIn, pvBuf, pArgs->cbOut, &cbOut);
|
---|
511 |
|
---|
512 | /*
|
---|
513 | * Copy ioctl data and output buffer back to user space.
|
---|
514 | */
|
---|
515 | if (rc)
|
---|
516 | {
|
---|
517 | dprintf(("VBoxSupDrvIOCtlSlow: pid=%d iCmd=%x pData=%p failed, rc=%d (darwin rc=%d)\n",
|
---|
518 | proc_pid(pProcess), iCmd, (void *)pData, rc, VBoxSupDrvErr2DarwinErr(rc)));
|
---|
519 | rc = VBoxSupDrvErr2DarwinErr(rc);
|
---|
520 | }
|
---|
521 | else if (cbOut > 0)
|
---|
522 | {
|
---|
523 | if (pvBuf != NULL && cbOut <= cbBuf)
|
---|
524 | {
|
---|
525 | int rc2 = copyout(pvBuf, (user_addr_t)pArgs->pvOut, cbOut);
|
---|
526 | if (rc2)
|
---|
527 | {
|
---|
528 | dprintf(("VBoxSupDrvIOCtlSlow: copyout(,%p,%d) failed.\n", pArgs->pvOut, cbBuf));
|
---|
529 | rc = rc2;
|
---|
530 | }
|
---|
531 | }
|
---|
532 | else
|
---|
533 | {
|
---|
534 | dprintf(("WHAT!?! supdrvIOCtl messed up! cbOut=%d cbBuf=%d pvBuf=%p\n", cbOut, cbBuf, pvBuf));
|
---|
535 | rc = EPERM;
|
---|
536 | }
|
---|
537 | }
|
---|
538 |
|
---|
539 | if (pvPageBuf)
|
---|
540 | IOFreeAligned(pvPageBuf, cbBuf);
|
---|
541 | else if (pvBuf)
|
---|
542 | RTMemTmpFree(pvBuf);
|
---|
543 |
|
---|
544 | dprintf2(("VBoxSupDrvIOCtlSlow: returns %d\n", rc));
|
---|
545 | return rc;
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 |
|
---|
550 | /**
|
---|
551 | * Initializes any OS specific object creator fields.
|
---|
552 | */
|
---|
553 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
554 | {
|
---|
555 | NOREF(pObj);
|
---|
556 | NOREF(pSession);
|
---|
557 | }
|
---|
558 |
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * Checks if the session can access the object.
|
---|
562 | *
|
---|
563 | * @returns true if a decision has been made.
|
---|
564 | * @returns false if the default access policy should be applied.
|
---|
565 | *
|
---|
566 | * @param pObj The object in question.
|
---|
567 | * @param pSession The session wanting to access the object.
|
---|
568 | * @param pszObjName The object name, can be NULL.
|
---|
569 | * @param prc Where to store the result when returning true.
|
---|
570 | */
|
---|
571 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
572 | {
|
---|
573 | NOREF(pObj);
|
---|
574 | NOREF(pSession);
|
---|
575 | NOREF(pszObjName);
|
---|
576 | NOREF(prc);
|
---|
577 | return false;
|
---|
578 | }
|
---|
579 |
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Converts a supdrv error code to a darwin error code.
|
---|
583 | *
|
---|
584 | * @returns corresponding darwin error code.
|
---|
585 | * @param rc supdrv error code (SUPDRV_ERR_* defines).
|
---|
586 | */
|
---|
587 | static int VBoxSupDrvErr2DarwinErr(int rc)
|
---|
588 | {
|
---|
589 | switch (rc)
|
---|
590 | {
|
---|
591 | case 0: return 0;
|
---|
592 | case SUPDRV_ERR_GENERAL_FAILURE: return EACCES;
|
---|
593 | case SUPDRV_ERR_INVALID_PARAM: return EINVAL;
|
---|
594 | case SUPDRV_ERR_INVALID_MAGIC: return EILSEQ;
|
---|
595 | case SUPDRV_ERR_INVALID_HANDLE: return ENXIO;
|
---|
596 | case SUPDRV_ERR_INVALID_POINTER: return EFAULT;
|
---|
597 | case SUPDRV_ERR_LOCK_FAILED: return ENOLCK;
|
---|
598 | case SUPDRV_ERR_ALREADY_LOADED: return EEXIST;
|
---|
599 | case SUPDRV_ERR_PERMISSION_DENIED: return EPERM;
|
---|
600 | case SUPDRV_ERR_VERSION_MISMATCH: return ENOSYS;
|
---|
601 | }
|
---|
602 |
|
---|
603 | return EPERM;
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | /** @todo move this to assembly where a simple "jmp printf" will to the trick. */
|
---|
608 | RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
609 | {
|
---|
610 | va_list args;
|
---|
611 | char szMsg[512];
|
---|
612 |
|
---|
613 | va_start(args, pszFormat);
|
---|
614 | vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, args);
|
---|
615 | va_end(args);
|
---|
616 |
|
---|
617 | szMsg[sizeof(szMsg) - 1] = '\0';
|
---|
618 | printf("%s", szMsg);
|
---|
619 | return 0;
|
---|
620 | }
|
---|
621 |
|
---|
622 |
|
---|
623 | /** Runtime assert implementation for Darwin Ring-0. */
|
---|
624 | RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
|
---|
625 | {
|
---|
626 | printf("!!Assertion Failed!!\n"
|
---|
627 | "Expression: %s\n"
|
---|
628 | "Location : %s(%d) %s\n",
|
---|
629 | pszExpr, pszFile, uLine, pszFunction);
|
---|
630 | }
|
---|
631 |
|
---|
632 |
|
---|
633 | /** Runtime assert implementation for the Darwin Ring-0 driver.
|
---|
634 | * @todo this one needs fixing! */
|
---|
635 | RTDECL(void) AssertMsg2(const char *pszFormat, ...)
|
---|
636 | { /* forwarder. */
|
---|
637 | va_list ap;
|
---|
638 | char msg[256];
|
---|
639 |
|
---|
640 | va_start(ap, pszFormat);
|
---|
641 | vsnprintf(msg, sizeof(msg) - 1, pszFormat, ap);
|
---|
642 | msg[sizeof(msg) - 1] = '\0';
|
---|
643 | printf("%s", msg);
|
---|
644 | va_end(ap);
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|
648 | /*
|
---|
649 | *
|
---|
650 | * org_virtualbox_SupDrv
|
---|
651 | *
|
---|
652 | */
|
---|
653 |
|
---|
654 |
|
---|
655 | /**
|
---|
656 | * Initialize the object.
|
---|
657 | */
|
---|
658 | bool org_virtualbox_SupDrv::init(OSDictionary *pDictionary)
|
---|
659 | {
|
---|
660 | dprintf(("org_virtualbox_SupDrv::init([%p], %p)\n", this, pDictionary));
|
---|
661 | if (IOService::init(pDictionary))
|
---|
662 | {
|
---|
663 | /* init members. */
|
---|
664 | return true;
|
---|
665 | }
|
---|
666 | return false;
|
---|
667 | }
|
---|
668 |
|
---|
669 |
|
---|
670 | /**
|
---|
671 | * Free the object.
|
---|
672 | */
|
---|
673 | void org_virtualbox_SupDrv::free(void)
|
---|
674 | {
|
---|
675 | dprintf(("IOService::free([%p])\n", this));
|
---|
676 | IOService::free();
|
---|
677 | }
|
---|
678 |
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Check if it's ok to start this service.
|
---|
682 | * It's always ok by us, so it's up to IOService to decide really.
|
---|
683 | */
|
---|
684 | IOService *org_virtualbox_SupDrv::probe(IOService *pProvider, SInt32 *pi32Score)
|
---|
685 | {
|
---|
686 | dprintf(("org_virtualbox_SupDrv::probe([%p])\n", this));
|
---|
687 | return IOService::probe(pProvider, pi32Score);
|
---|
688 | }
|
---|
689 |
|
---|
690 |
|
---|
691 | /**
|
---|
692 | * Start this service.
|
---|
693 | */
|
---|
694 | bool org_virtualbox_SupDrv::start(IOService *pProvider)
|
---|
695 | {
|
---|
696 | dprintf(("org_virtualbox_SupDrv::start([%p])\n", this));
|
---|
697 |
|
---|
698 | if (IOService::start(pProvider))
|
---|
699 | {
|
---|
700 | /* register the service. */
|
---|
701 | registerService();
|
---|
702 | return true;
|
---|
703 | }
|
---|
704 | return false;
|
---|
705 | }
|
---|
706 |
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Stop this service.
|
---|
710 | */
|
---|
711 | void org_virtualbox_SupDrv::stop(IOService *pProvider)
|
---|
712 | {
|
---|
713 | dprintf(("org_virtualbox_SupDrv::stop([%p], %p)\n", this, pProvider));
|
---|
714 | IOService::stop(pProvider);
|
---|
715 | }
|
---|
716 |
|
---|
717 |
|
---|
718 | /*
|
---|
719 | *
|
---|
720 | * org_virtualbox_SupDrvClient
|
---|
721 | *
|
---|
722 | */
|
---|
723 |
|
---|
724 |
|
---|
725 | /**
|
---|
726 | * Initializer called when the client opens the service.
|
---|
727 | */
|
---|
728 | bool org_virtualbox_SupDrvClient::initWithTask(task_t OwningTask, void *pvSecurityId, UInt32 u32Type)
|
---|
729 | {
|
---|
730 | dprintf(("org_virtualbox_SupDrvClient::initWithTask([%p], %#x, %p, %#x)\n", this, OwningTask, pvSecurityId, u32Type));
|
---|
731 |
|
---|
732 | if (!OwningTask)
|
---|
733 | return false;
|
---|
734 | if (IOUserClient::initWithTask(OwningTask, pvSecurityId , u32Type))
|
---|
735 | {
|
---|
736 | m_Task = OwningTask;
|
---|
737 | m_pSession = NULL;
|
---|
738 | m_pProvider = NULL;
|
---|
739 | return true;
|
---|
740 | }
|
---|
741 | return false;
|
---|
742 | }
|
---|
743 |
|
---|
744 |
|
---|
745 | /**
|
---|
746 | * Start the client service.
|
---|
747 | */
|
---|
748 | bool org_virtualbox_SupDrvClient::start(IOService *pProvider)
|
---|
749 | {
|
---|
750 | dprintf(("org_virtualbox_SupDrvClient::start([%p], %p)\n", this, pProvider));
|
---|
751 | if (IOUserClient::start(pProvider))
|
---|
752 | {
|
---|
753 | m_pProvider = OSDynamicCast(org_virtualbox_SupDrv, pProvider);
|
---|
754 | if (m_pProvider)
|
---|
755 | {
|
---|
756 | /* this is where we could create the section. */
|
---|
757 | return true;
|
---|
758 | }
|
---|
759 | dprintf(("org_virtualbox_SupDrvClient::start: %p isn't org_virtualbox_SupDrv\n", pProvider));
|
---|
760 | }
|
---|
761 | return false;
|
---|
762 | }
|
---|
763 |
|
---|
764 |
|
---|
765 | /**
|
---|
766 | * Client exits normally.
|
---|
767 | */
|
---|
768 | IOReturn org_virtualbox_SupDrvClient::clientClose(void)
|
---|
769 | {
|
---|
770 | dprintf(("org_virtualbox_SupDrvClient::clientClose([%p])\n", this));
|
---|
771 |
|
---|
772 | m_pProvider = NULL;
|
---|
773 | terminate();
|
---|
774 |
|
---|
775 | return kIOReturnSuccess;
|
---|
776 | }
|
---|
777 |
|
---|
778 |
|
---|
779 | /**
|
---|
780 | * The client exits abnormally / forgets to do cleanups.
|
---|
781 | */
|
---|
782 | IOReturn org_virtualbox_SupDrvClient::clientDied(void)
|
---|
783 | {
|
---|
784 | dprintf(("org_virtualbox_SupDrvClient::clientDied([%p]) m_Task=%p R0Process=%p Process=%d\n",
|
---|
785 | this, m_Task, RTR0ProcHandleSelf(), RTProcSelf()));
|
---|
786 |
|
---|
787 | /*
|
---|
788 | * Do early session cleanup (if there is a session) so
|
---|
789 | * we avoid hanging in vm_map_remove().
|
---|
790 | */
|
---|
791 | const RTR0PROCESS R0Process = (RTR0PROCESS)m_Task;
|
---|
792 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
793 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
794 | for (unsigned i = 0; i < RT_ELEMENTS(g_apSessionHashTab); i++)
|
---|
795 | {
|
---|
796 | for (PSUPDRVSESSION pSession = g_apSessionHashTab[i]; pSession; pSession = pSession->pNextHash)
|
---|
797 | {
|
---|
798 | dprintf2(("pSession=%p R0Process=%p (=? %p)\n", pSession, pSession->R0Process, R0Process));
|
---|
799 | if (pSession->R0Process == R0Process)
|
---|
800 | {
|
---|
801 | /*
|
---|
802 | * It is safe to leave the spinlock here; the session shouldn't be able
|
---|
803 | * to go away while we're cleaning it up, changes to pNextHash will not
|
---|
804 | * harm us, and new sessions can't possibly be added for this process.
|
---|
805 | */
|
---|
806 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
807 | supdrvCleanupSession(&g_DevExt, pSession);
|
---|
808 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
809 | }
|
---|
810 | }
|
---|
811 | }
|
---|
812 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
813 |
|
---|
814 | /* IOUserClient::clientDied() calls close... */
|
---|
815 | return IOUserClient::clientDied();
|
---|
816 | }
|
---|
817 |
|
---|
818 |
|
---|
819 | /**
|
---|
820 | * Terminate the service (initiate the destruction).
|
---|
821 | */
|
---|
822 | bool org_virtualbox_SupDrvClient::terminate(IOOptionBits fOptions)
|
---|
823 | {
|
---|
824 | dprintf(("org_virtualbox_SupDrvClient::terminate([%p], %#x)\n", this, fOptions));
|
---|
825 | return IOUserClient::terminate(fOptions);
|
---|
826 | }
|
---|
827 |
|
---|
828 |
|
---|
829 | /**
|
---|
830 | * The final stage of the client service destruction.
|
---|
831 | */
|
---|
832 | bool org_virtualbox_SupDrvClient::finalize(IOOptionBits fOptions)
|
---|
833 | {
|
---|
834 | dprintf(("org_virtualbox_SupDrvClient::finalize([%p], %#x)\n", this, fOptions));
|
---|
835 | return IOUserClient::finalize(fOptions);
|
---|
836 | }
|
---|
837 |
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Stop the client service.
|
---|
841 | */
|
---|
842 | void org_virtualbox_SupDrvClient::stop(IOService *pProvider)
|
---|
843 | {
|
---|
844 | dprintf(("org_virtualbox_SupDrvClient::stop([%p])\n", this));
|
---|
845 | IOUserClient::stop(pProvider);
|
---|
846 | }
|
---|
847 |
|
---|