1 | /* $Id: SUPDrv-freebsd.c 19745 2009-05-15 15:32:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDrv - The VirtualBox Support Driver - FreeBSD specifics.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #define LOG_GROUP LOG_GROUP_SUP_DRV
|
---|
35 | /* Deal with conflicts first. */
|
---|
36 | #include <sys/param.h>
|
---|
37 | #undef PVM
|
---|
38 | #include <sys/types.h>
|
---|
39 | #include <sys/module.h>
|
---|
40 | #include <sys/systm.h>
|
---|
41 | #include <sys/errno.h>
|
---|
42 | #include <sys/kernel.h>
|
---|
43 | #include <sys/fcntl.h>
|
---|
44 | #include <sys/conf.h>
|
---|
45 | #include <sys/uio.h>
|
---|
46 |
|
---|
47 | #include "../SUPDrvInternal.h"
|
---|
48 | #include <VBox/version.h>
|
---|
49 | #include <iprt/initterm.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include <iprt/spinlock.h>
|
---|
52 | #include <iprt/process.h>
|
---|
53 | #include <iprt/assert.h>
|
---|
54 | #include <iprt/uuid.h>
|
---|
55 | #include <VBox/log.h>
|
---|
56 | #include <iprt/alloc.h>
|
---|
57 | #include <iprt/err.h>
|
---|
58 |
|
---|
59 | #ifdef VBOX_WITH_HARDENING
|
---|
60 | # define VBOXDRV_PERM 0600
|
---|
61 | #else
|
---|
62 | # define VBOXDRV_PERM 0666
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | /*******************************************************************************
|
---|
66 | * Internal Functions *
|
---|
67 | *******************************************************************************/
|
---|
68 | static int VBoxDrvFreeBSDModuleEvent(struct module *pMod, int enmEventType, void *pvArg);
|
---|
69 | static int VBoxDrvFreeBSDLoad(void);
|
---|
70 | static int VBoxDrvFreeBSDUnload(void);
|
---|
71 | static void VBoxDrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *pachName, int cchName, struct cdev **ppDev);
|
---|
72 |
|
---|
73 | static d_fdopen_t VBoxDrvFreeBSDOpen;
|
---|
74 | static d_close_t VBoxDrvFreeBSDClose;
|
---|
75 | static d_ioctl_t VBoxDrvFreeBSDIOCtl;
|
---|
76 | static int VBoxDrvFreeBSDIOCtlSlow(PSUPDRVSESSION pSession, u_long ulCmd, caddr_t pvData, struct thread *pTd);
|
---|
77 |
|
---|
78 |
|
---|
79 | /*******************************************************************************
|
---|
80 | * Global Variables *
|
---|
81 | *******************************************************************************/
|
---|
82 | /**
|
---|
83 | * Module info structure used by the kernel.
|
---|
84 | */
|
---|
85 | static moduledata_t g_VBoxDrvFreeBSDModule =
|
---|
86 | {
|
---|
87 | "vboxdrv",
|
---|
88 | VBoxDrvFreeBSDModuleEvent,
|
---|
89 | NULL
|
---|
90 | };
|
---|
91 |
|
---|
92 | /** Declare the module as a pseudo device. */
|
---|
93 | DECLARE_MODULE(vboxdrv, g_VBoxDrvFreeBSDModule, SI_SUB_PSEUDO, SI_ORDER_ANY);
|
---|
94 | MODULE_VERSION(vboxdrv, 1);
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * The /dev/vboxdrv character device entry points.
|
---|
98 | */
|
---|
99 | static struct cdevsw g_VBoxDrvFreeBSDChrDevSW =
|
---|
100 | {
|
---|
101 | .d_version = D_VERSION,
|
---|
102 | #if __FreeBSD_version > 800061
|
---|
103 | .d_flags = D_PSEUDO | D_TRACKCLOSE | D_NEEDMINOR,
|
---|
104 | #else
|
---|
105 | .d_flags = D_PSEUDO | D_TRACKCLOSE,
|
---|
106 | #endif
|
---|
107 | .d_fdopen = VBoxDrvFreeBSDOpen,
|
---|
108 | .d_close = VBoxDrvFreeBSDClose,
|
---|
109 | .d_ioctl = VBoxDrvFreeBSDIOCtl,
|
---|
110 | .d_name = "vboxdrv"
|
---|
111 | };
|
---|
112 |
|
---|
113 | /** List of cloned device. Managed by the kernel. */
|
---|
114 | static struct clonedevs *g_pVBoxDrvFreeBSDClones;
|
---|
115 | /** The dev_clone event handler tag. */
|
---|
116 | static eventhandler_tag g_VBoxDrvFreeBSDEHTag;
|
---|
117 | /** Reference counter. */
|
---|
118 | static volatile uint32_t g_cUsers;
|
---|
119 |
|
---|
120 | /** The device extention. */
|
---|
121 | static SUPDRVDEVEXT g_VBoxDrvFreeBSDDevExt;
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Module event handler.
|
---|
125 | *
|
---|
126 | * @param pMod The module structure.
|
---|
127 | * @param enmEventType The event type (modeventtype_t).
|
---|
128 | * @param pvArg Module argument. NULL.
|
---|
129 | *
|
---|
130 | * @return 0 on success, errno.h status code on failure.
|
---|
131 | */
|
---|
132 | static int VBoxDrvFreeBSDModuleEvent(struct module *pMod, int enmEventType, void *pvArg)
|
---|
133 | {
|
---|
134 | int rc;
|
---|
135 | switch (enmEventType)
|
---|
136 | {
|
---|
137 | case MOD_LOAD:
|
---|
138 | rc = VBoxDrvFreeBSDLoad();
|
---|
139 | break;
|
---|
140 |
|
---|
141 | case MOD_UNLOAD:
|
---|
142 | rc = VBoxDrvFreeBSDUnload();
|
---|
143 | break;
|
---|
144 |
|
---|
145 | case MOD_SHUTDOWN:
|
---|
146 | case MOD_QUIESCE:
|
---|
147 | default:
|
---|
148 | return EOPNOTSUPP;
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (RT_SUCCESS(rc))
|
---|
152 | return 0;
|
---|
153 | return RTErrConvertToErrno(rc);
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | static int VBoxDrvFreeBSDLoad(void)
|
---|
158 | {
|
---|
159 | dprintf(("VBoxDrvFreeBSDLoad:\n"));
|
---|
160 |
|
---|
161 | g_cUsers = 0;
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Initialize the runtime.
|
---|
165 | */
|
---|
166 | int rc = RTR0Init(0);
|
---|
167 | if (RT_SUCCESS(rc))
|
---|
168 | {
|
---|
169 | /*
|
---|
170 | * Initialize the device extension.
|
---|
171 | */
|
---|
172 | rc = supdrvInitDevExt(&g_VBoxDrvFreeBSDDevExt);
|
---|
173 | if (RT_SUCCESS(rc))
|
---|
174 | {
|
---|
175 | /*
|
---|
176 | * Configure device cloning.
|
---|
177 | */
|
---|
178 | clone_setup(&g_pVBoxDrvFreeBSDClones);
|
---|
179 | g_VBoxDrvFreeBSDEHTag = EVENTHANDLER_REGISTER(dev_clone, VBoxDrvFreeBSDClone, 0, 1000);
|
---|
180 | if (g_VBoxDrvFreeBSDEHTag)
|
---|
181 | {
|
---|
182 | dprintf(("VBoxDrvFreeBSDLoad: returns successfully\n"));
|
---|
183 | return VINF_SUCCESS;
|
---|
184 | }
|
---|
185 |
|
---|
186 | printf("vboxdrv: EVENTHANDLER_REGISTER(dev_clone,,,) failed\n");
|
---|
187 | clone_cleanup(&g_pVBoxDrvFreeBSDClones);
|
---|
188 | rc = SUPDRV_ERR_ALREADY_LOADED;
|
---|
189 | supdrvDeleteDevExt(&g_VBoxDrvFreeBSDDevExt);
|
---|
190 | }
|
---|
191 | else
|
---|
192 | printf("vboxdrv: supdrvInitDevExt failed, rc=%d\n", rc);
|
---|
193 | RTR0Term();
|
---|
194 | }
|
---|
195 | else
|
---|
196 | printf("vboxdrv: RTR0Init failed, rc=%d\n", rc);
|
---|
197 | return rc;
|
---|
198 | }
|
---|
199 |
|
---|
200 | static int VBoxDrvFreeBSDUnload(void)
|
---|
201 | {
|
---|
202 | dprintf(("VBoxDrvFreeBSDUnload:\n"));
|
---|
203 |
|
---|
204 | if (g_cUsers > 0)
|
---|
205 | return EBUSY;
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Reserve what we did in VBoxDrvFreeBSDInit.
|
---|
209 | */
|
---|
210 | clone_cleanup(&g_pVBoxDrvFreeBSDClones);
|
---|
211 |
|
---|
212 | supdrvDeleteDevExt(&g_VBoxDrvFreeBSDDevExt);
|
---|
213 |
|
---|
214 | RTR0Term();
|
---|
215 |
|
---|
216 | memset(&g_VBoxDrvFreeBSDDevExt, 0, sizeof(g_VBoxDrvFreeBSDDevExt));
|
---|
217 |
|
---|
218 | dprintf(("VBoxDrvFreeBSDUnload: returns\n"));
|
---|
219 | return VINF_SUCCESS;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * DEVFS event handler.
|
---|
225 | */
|
---|
226 | static void VBoxDrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *pszName, int cchName, struct cdev **ppDev)
|
---|
227 | {
|
---|
228 | int iUnit;
|
---|
229 | int rc;
|
---|
230 |
|
---|
231 | dprintf(("VBoxDrvFreeBSDClone: pszName=%s ppDev=%p\n", pszName, ppDev));
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * One device node per user, si_drv1 points to the session.
|
---|
235 | * /dev/vboxdrv<N> where N = {0...255}.
|
---|
236 | */
|
---|
237 | if (!ppDev)
|
---|
238 | return;
|
---|
239 | if (dev_stdclone(pszName, NULL, "vboxdrv", &iUnit) != 1)
|
---|
240 | return;
|
---|
241 | if (iUnit >= 256 || iUnit < 0)
|
---|
242 | {
|
---|
243 | dprintf(("VBoxDrvFreeBSDClone: iUnit=%d >= 256 - rejected\n", iUnit));
|
---|
244 | return;
|
---|
245 | }
|
---|
246 |
|
---|
247 | dprintf(("VBoxDrvFreeBSDClone: pszName=%s iUnit=%d\n", pszName, iUnit));
|
---|
248 |
|
---|
249 | rc = clone_create(&g_pVBoxDrvFreeBSDClones, &g_VBoxDrvFreeBSDChrDevSW, &iUnit, ppDev, 0);
|
---|
250 | dprintf(("VBoxDrvFreeBSDClone: clone_create -> %d; iUnit=%d\n", rc, iUnit));
|
---|
251 | if (rc)
|
---|
252 | {
|
---|
253 | #if __FreeBSD_version > 800061
|
---|
254 | *ppDev = make_dev(&g_VBoxDrvFreeBSDChrDevSW, iUnit, UID_ROOT, GID_WHEEL, VBOXDRV_PERM, "vboxdrv%d", iUnit);
|
---|
255 | #else
|
---|
256 | *ppDev = make_dev(&g_VBoxDrvFreeBSDChrDevSW, unit2minor(iUnit), UID_ROOT, GID_WHEEL, VBOXDRV_PERM, "vboxdrv%d", iUnit);
|
---|
257 | #endif
|
---|
258 | if (*ppDev)
|
---|
259 | {
|
---|
260 | dev_ref(*ppDev);
|
---|
261 | (*ppDev)->si_flags |= SI_CHEAPCLONE;
|
---|
262 | dprintf(("VBoxDrvFreeBSDClone: Created *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
|
---|
263 | *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
|
---|
264 | (*ppDev)->si_drv1 = (*ppDev)->si_drv2 = NULL;
|
---|
265 | }
|
---|
266 | else
|
---|
267 | OSDBGPRINT(("VBoxDrvFreeBSDClone: make_dev iUnit=%d failed\n", iUnit));
|
---|
268 | }
|
---|
269 | else
|
---|
270 | dprintf(("VBoxDrvFreeBSDClone: Existing *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
|
---|
271 | *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 |
|
---|
276 | /**
|
---|
277 | *
|
---|
278 | * @returns 0 on success, errno on failure.
|
---|
279 | * EBUSY if the device is used by someone else.
|
---|
280 | * @param pDev The device node.
|
---|
281 | * @param fOpen The open flags.
|
---|
282 | * @param pTd The thread.
|
---|
283 | * @param pFd The file descriptor. FreeBSD 7.0 and later.
|
---|
284 | * @param iFd The file descriptor index(?). Pre FreeBSD 7.0.
|
---|
285 | */
|
---|
286 | #if __FreeBSD__ >= 7
|
---|
287 | static int VBoxDrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd, struct file *pFd)
|
---|
288 | #else
|
---|
289 | static int VBoxDrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd, int iFd)
|
---|
290 | #endif
|
---|
291 | {
|
---|
292 | PSUPDRVSESSION pSession;
|
---|
293 | int rc;
|
---|
294 |
|
---|
295 | #if __FreeBSD_version < 800062
|
---|
296 | dprintf(("VBoxDrvFreeBSDOpen: fOpen=%#x iUnit=%d\n", fOpen, minor2unit(minor(pDev))));
|
---|
297 | #else
|
---|
298 | dprintf(("VBoxDrvFreeBSDOpen: fOpen=%#x iUnit=%d\n", fOpen, minor(pDev)));
|
---|
299 | #endif
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Let's be a bit picky about the flags...
|
---|
303 | */
|
---|
304 | if (fOpen != (FREAD|FWRITE /*=O_RDWR*/))
|
---|
305 | {
|
---|
306 | dprintf(("VBoxDrvFreeBSDOpen: fOpen=%#x expected %#x\n", fOpen, O_RDWR));
|
---|
307 | return EINVAL;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /*
|
---|
311 | * Try grab it (we don't grab the giant, remember).
|
---|
312 | */
|
---|
313 | if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, (void *)0x42, NULL))
|
---|
314 | return EBUSY;
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * Create a new session.
|
---|
318 | */
|
---|
319 | rc = supdrvCreateSession(&g_VBoxDrvFreeBSDDevExt, true /* fUser */, &pSession);
|
---|
320 | if (RT_SUCCESS(rc))
|
---|
321 | {
|
---|
322 | /** @todo get (r)uid and (r)gid.
|
---|
323 | pSession->Uid = stuff;
|
---|
324 | pSession->Gid = stuff; */
|
---|
325 | if (ASMAtomicCmpXchgPtr(&pDev->si_drv1, pSession, (void *)0x42))
|
---|
326 | {
|
---|
327 | ASMAtomicIncU32(&g_cUsers);
|
---|
328 | return 0;
|
---|
329 | }
|
---|
330 |
|
---|
331 | OSDBGPRINT(("VBoxDrvFreeBSDOpen: si_drv1=%p, expected 0x42!\n", pDev->si_drv1));
|
---|
332 | supdrvCloseSession(&g_VBoxDrvFreeBSDDevExt, pSession);
|
---|
333 | }
|
---|
334 |
|
---|
335 | return RTErrConvertToErrno(rc);
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Close a file device previously opened by VBoxDrvFreeBSDOpen
|
---|
341 | *
|
---|
342 | * @returns 0 on success.
|
---|
343 | * @param pDev The device.
|
---|
344 | * @param fFile The file descriptor flags.
|
---|
345 | * @param DevType The device type (CHR.
|
---|
346 | * @param pTd The calling thread.
|
---|
347 | */
|
---|
348 | static int VBoxDrvFreeBSDClose(struct cdev *pDev, int fFile, int DevType, struct thread *pTd)
|
---|
349 | {
|
---|
350 | PSUPDRVSESSION pSession = (PSUPDRVSESSION)pDev->si_drv1;
|
---|
351 | #if __FreeBSD_version < 800062
|
---|
352 | dprintf(("VBoxDrvFreeBSDClose: fFile=%#x iUnit=%d pSession=%p\n", fFile, minor2unit(minor(pDev)), pSession));
|
---|
353 | #else
|
---|
354 | dprintf(("VBoxDrvFreeBSDClose: fFile=%#x iUnit=%d pSession=%p\n", fFile, minor(pDev), pSession));
|
---|
355 | #endif
|
---|
356 |
|
---|
357 | /*
|
---|
358 | * Close the session if it's still hanging on to the device...
|
---|
359 | */
|
---|
360 | if (VALID_PTR(pSession))
|
---|
361 | {
|
---|
362 | supdrvCloseSession(&g_VBoxDrvFreeBSDDevExt, pSession);
|
---|
363 | if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, NULL, pSession))
|
---|
364 | OSDBGPRINT(("VBoxDrvFreeBSDClose: si_drv1=%p expected %p!\n", pDev->si_drv1, pSession));
|
---|
365 | ASMAtomicDecU32(&g_cUsers);
|
---|
366 | /* Don't use destroy_dev here because it may sleep resulting in a hanging user process. */
|
---|
367 | destroy_dev_sched(pDev);
|
---|
368 | }
|
---|
369 | else
|
---|
370 | OSDBGPRINT(("VBoxDrvFreeBSDClose: si_drv1=%p!\n", pSession));
|
---|
371 | return 0;
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * I/O control request.
|
---|
377 | *
|
---|
378 | * @returns depends...
|
---|
379 | * @param pDev The device.
|
---|
380 | * @param ulCmd The command.
|
---|
381 | * @param pvData Pointer to the data.
|
---|
382 | * @param fFile The file descriptor flags.
|
---|
383 | * @param pTd The calling thread.
|
---|
384 | */
|
---|
385 | static int VBoxDrvFreeBSDIOCtl(struct cdev *pDev, u_long ulCmd, caddr_t pvData, int fFile, struct thread *pTd)
|
---|
386 | {
|
---|
387 | /*
|
---|
388 | * Validate the input.
|
---|
389 | */
|
---|
390 | PSUPDRVSESSION pSession = (PSUPDRVSESSION)pDev->si_drv1;
|
---|
391 | if (RT_UNLIKELY(!VALID_PTR(pSession)))
|
---|
392 | return EINVAL;
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * Deal with the fast ioctl path first.
|
---|
396 | */
|
---|
397 | if ( ulCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
398 | || ulCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
|
---|
399 | || ulCmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
400 | return supdrvIOCtlFast(ulCmd, *(uint32_t *)pvData, &g_VBoxDrvFreeBSDDevExt, pSession);
|
---|
401 |
|
---|
402 | return VBoxDrvFreeBSDIOCtlSlow(pSession, ulCmd, pvData, pTd);
|
---|
403 | }
|
---|
404 |
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Deal with the 'slow' I/O control requests.
|
---|
408 | *
|
---|
409 | * @returns 0 on success, appropriate errno on failure.
|
---|
410 | * @param pSession The session.
|
---|
411 | * @param ulCmd The command.
|
---|
412 | * @param pvData The request data.
|
---|
413 | * @param pTd The calling thread.
|
---|
414 | */
|
---|
415 | static int VBoxDrvFreeBSDIOCtlSlow(PSUPDRVSESSION pSession, u_long ulCmd, caddr_t pvData, struct thread *pTd)
|
---|
416 | {
|
---|
417 | PSUPREQHDR pHdr;
|
---|
418 | uint32_t cbReq = IOCPARM_LEN(ulCmd);
|
---|
419 | void *pvUser = NULL;
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * Buffered request?
|
---|
423 | */
|
---|
424 | if ((IOC_DIRMASK & ulCmd) == IOC_INOUT)
|
---|
425 | {
|
---|
426 | pHdr = (PSUPREQHDR)pvData;
|
---|
427 | if (RT_UNLIKELY(cbReq < sizeof(*pHdr)))
|
---|
428 | {
|
---|
429 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: cbReq=%#x < %#x; ulCmd=%#lx\n", cbReq, (int)sizeof(*pHdr), ulCmd));
|
---|
430 | return EINVAL;
|
---|
431 | }
|
---|
432 | if (RT_UNLIKELY((pHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
|
---|
433 | {
|
---|
434 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: bad magic fFlags=%#x; ulCmd=%#lx\n", pHdr->fFlags, ulCmd));
|
---|
435 | return EINVAL;
|
---|
436 | }
|
---|
437 | if (RT_UNLIKELY( RT_MAX(pHdr->cbIn, pHdr->cbOut) != cbReq
|
---|
438 | || pHdr->cbIn < sizeof(*pHdr)
|
---|
439 | || pHdr->cbOut < sizeof(*pHdr)))
|
---|
440 | {
|
---|
441 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: max(%#x,%#x) != %#x; ulCmd=%#lx\n", pHdr->cbIn, pHdr->cbOut, cbReq, ulCmd));
|
---|
442 | return EINVAL;
|
---|
443 | }
|
---|
444 | }
|
---|
445 | /*
|
---|
446 | * Big unbuffered request?
|
---|
447 | */
|
---|
448 | else if ((IOC_DIRMASK & ulCmd) == IOC_VOID && !cbReq)
|
---|
449 | {
|
---|
450 | /*
|
---|
451 | * Read the header, validate it and figure out how much that needs to be buffered.
|
---|
452 | */
|
---|
453 | SUPREQHDR Hdr;
|
---|
454 | pvUser = *(void **)pvData;
|
---|
455 | int rc = copyin(pvUser, &Hdr, sizeof(Hdr));
|
---|
456 | if (RT_UNLIKELY(rc))
|
---|
457 | {
|
---|
458 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: copyin(%p,Hdr,) -> %#x; ulCmd=%#lx\n", pvUser, rc, ulCmd));
|
---|
459 | return rc;
|
---|
460 | }
|
---|
461 | if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
|
---|
462 | {
|
---|
463 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: bad magic fFlags=%#x; ulCmd=%#lx\n", Hdr.fFlags, ulCmd));
|
---|
464 | return EINVAL;
|
---|
465 | }
|
---|
466 | cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
|
---|
467 | if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
|
---|
468 | || Hdr.cbOut < sizeof(Hdr)
|
---|
469 | || cbReq > _1M*16))
|
---|
470 | {
|
---|
471 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: max(%#x,%#x); ulCmd=%#lx\n", Hdr.cbIn, Hdr.cbOut, ulCmd));
|
---|
472 | return EINVAL;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Allocate buffer and copy in the data.
|
---|
477 | */
|
---|
478 | pHdr = (PSUPREQHDR)RTMemTmpAlloc(cbReq);
|
---|
479 | if (RT_UNLIKELY(!pHdr))
|
---|
480 | {
|
---|
481 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: failed to allocate buffer of %d bytes; ulCmd=%#lx\n", cbReq, ulCmd));
|
---|
482 | return ENOMEM;
|
---|
483 | }
|
---|
484 | rc = copyin(pvUser, pHdr, Hdr.cbIn);
|
---|
485 | if (RT_UNLIKELY(rc))
|
---|
486 | {
|
---|
487 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: copyin(%p,%p,%#x) -> %#x; ulCmd=%#lx\n",
|
---|
488 | pvUser, pHdr, Hdr.cbIn, rc, ulCmd));
|
---|
489 | RTMemTmpFree(pHdr);
|
---|
490 | return rc;
|
---|
491 | }
|
---|
492 | }
|
---|
493 | else
|
---|
494 | {
|
---|
495 | dprintf(("VBoxDrvFreeBSDIOCtlSlow: huh? cbReq=%#x ulCmd=%#lx\n", cbReq, ulCmd));
|
---|
496 | return EINVAL;
|
---|
497 | }
|
---|
498 |
|
---|
499 | /*
|
---|
500 | * Process the IOCtl.
|
---|
501 | */
|
---|
502 | int rc = supdrvIOCtl(ulCmd, &g_VBoxDrvFreeBSDDevExt, pSession, pHdr);
|
---|
503 | if (RT_LIKELY(!rc))
|
---|
504 | {
|
---|
505 | /*
|
---|
506 | * If unbuffered, copy back the result before returning.
|
---|
507 | */
|
---|
508 | if (pvUser)
|
---|
509 | {
|
---|
510 | uint32_t cbOut = pHdr->cbOut;
|
---|
511 | if (cbOut > cbReq)
|
---|
512 | {
|
---|
513 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: too much output! %#x > %#x; uCmd=%#lx!\n", cbOut, cbReq, ulCmd));
|
---|
514 | cbOut = cbReq;
|
---|
515 | }
|
---|
516 | rc = copyout(pHdr, pvUser, cbOut);
|
---|
517 | if (RT_UNLIKELY(rc))
|
---|
518 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: copyout(%p,%p,%#x) -> %d; uCmd=%#lx!\n", pHdr, pvUser, cbOut, rc, ulCmd));
|
---|
519 |
|
---|
520 | dprintf(("VBoxDrvFreeBSDIOCtlSlow: returns %d / %d ulCmd=%lx\n", 0, pHdr->rc, ulCmd));
|
---|
521 |
|
---|
522 | /* cleanup */
|
---|
523 | RTMemTmpFree(pHdr);
|
---|
524 | }
|
---|
525 | }
|
---|
526 | else
|
---|
527 | {
|
---|
528 | /*
|
---|
529 | * The request failed, just clean up.
|
---|
530 | */
|
---|
531 | if (pvUser)
|
---|
532 | RTMemTmpFree(pHdr);
|
---|
533 |
|
---|
534 | dprintf(("VBoxDrvFreeBSDIOCtlSlow: ulCmd=%lx pData=%p failed, rc=%d\n", ulCmd, pvData, rc));
|
---|
535 | rc = EINVAL;
|
---|
536 | }
|
---|
537 |
|
---|
538 | return rc;
|
---|
539 | }
|
---|
540 |
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * The SUPDRV IDC entry point.
|
---|
544 | *
|
---|
545 | * @returns VBox status code, see supdrvIDC.
|
---|
546 | * @param iReq The request code.
|
---|
547 | * @param pReq The request.
|
---|
548 | */
|
---|
549 | int VBOXCALL SUPDrvFreeBSDIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
|
---|
550 | {
|
---|
551 | PSUPDRVSESSION pSession;
|
---|
552 |
|
---|
553 | /*
|
---|
554 | * Some quick validations.
|
---|
555 | */
|
---|
556 | if (RT_UNLIKELY(!VALID_PTR(pReq)))
|
---|
557 | return VERR_INVALID_POINTER;
|
---|
558 |
|
---|
559 | pSession = pReq->pSession;
|
---|
560 | if (pSession)
|
---|
561 | {
|
---|
562 | if (RT_UNLIKELY(!VALID_PTR(pReq->pSession)))
|
---|
563 | return VERR_INVALID_PARAMETER;
|
---|
564 | if (RT_UNLIKELY(pSession->pDevExt != &g_VBoxDrvFreeBSDDevExt))
|
---|
565 | return VERR_INVALID_PARAMETER;
|
---|
566 | }
|
---|
567 | else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
|
---|
568 | return VERR_INVALID_PARAMETER;
|
---|
569 |
|
---|
570 | /*
|
---|
571 | * Do the job.
|
---|
572 | */
|
---|
573 | return supdrvIDC(uReq, &g_VBoxDrvFreeBSDDevExt, pSession, pReq);
|
---|
574 | }
|
---|
575 |
|
---|
576 |
|
---|
577 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
578 | {
|
---|
579 | NOREF(pObj);
|
---|
580 | NOREF(pSession);
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
585 | {
|
---|
586 | NOREF(pObj);
|
---|
587 | NOREF(pSession);
|
---|
588 | NOREF(pszObjName);
|
---|
589 | NOREF(prc);
|
---|
590 | return false;
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
|
---|
595 | {
|
---|
596 | return false;
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|
600 | SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
601 | {
|
---|
602 | va_list va;
|
---|
603 | char szMsg[256];
|
---|
604 | int cch;
|
---|
605 |
|
---|
606 | va_start(va, pszFormat);
|
---|
607 | cch = RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, va);
|
---|
608 | va_end(va);
|
---|
609 |
|
---|
610 | printf("%s", szMsg);
|
---|
611 |
|
---|
612 | return cch;
|
---|
613 | }
|
---|
614 |
|
---|