1 | /* $Id: SUPDrv-freebsd.c 22678 2009-09-01 15:12:40Z 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 | mtx_unlock(&Giant);
|
---|
143 | rc = VBoxDrvFreeBSDUnload();
|
---|
144 | mtx_lock(&Giant);
|
---|
145 | break;
|
---|
146 |
|
---|
147 | case MOD_SHUTDOWN:
|
---|
148 | case MOD_QUIESCE:
|
---|
149 | default:
|
---|
150 | return EOPNOTSUPP;
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (RT_SUCCESS(rc))
|
---|
154 | return 0;
|
---|
155 | return RTErrConvertToErrno(rc);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | static int VBoxDrvFreeBSDLoad(void)
|
---|
160 | {
|
---|
161 | dprintf(("VBoxDrvFreeBSDLoad:\n"));
|
---|
162 |
|
---|
163 | g_cUsers = 0;
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Initialize the runtime.
|
---|
167 | */
|
---|
168 | int rc = RTR0Init(0);
|
---|
169 | if (RT_SUCCESS(rc))
|
---|
170 | {
|
---|
171 | /*
|
---|
172 | * Initialize the device extension.
|
---|
173 | */
|
---|
174 | rc = supdrvInitDevExt(&g_VBoxDrvFreeBSDDevExt);
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | {
|
---|
177 | /*
|
---|
178 | * Configure device cloning.
|
---|
179 | */
|
---|
180 | clone_setup(&g_pVBoxDrvFreeBSDClones);
|
---|
181 | g_VBoxDrvFreeBSDEHTag = EVENTHANDLER_REGISTER(dev_clone, VBoxDrvFreeBSDClone, 0, 1000);
|
---|
182 | if (g_VBoxDrvFreeBSDEHTag)
|
---|
183 | {
|
---|
184 | dprintf(("VBoxDrvFreeBSDLoad: returns successfully\n"));
|
---|
185 | return VINF_SUCCESS;
|
---|
186 | }
|
---|
187 |
|
---|
188 | printf("vboxdrv: EVENTHANDLER_REGISTER(dev_clone,,,) failed\n");
|
---|
189 | clone_cleanup(&g_pVBoxDrvFreeBSDClones);
|
---|
190 | rc = SUPDRV_ERR_ALREADY_LOADED;
|
---|
191 | supdrvDeleteDevExt(&g_VBoxDrvFreeBSDDevExt);
|
---|
192 | }
|
---|
193 | else
|
---|
194 | printf("vboxdrv: supdrvInitDevExt failed, rc=%d\n", rc);
|
---|
195 | RTR0Term();
|
---|
196 | }
|
---|
197 | else
|
---|
198 | printf("vboxdrv: RTR0Init failed, rc=%d\n", rc);
|
---|
199 | return rc;
|
---|
200 | }
|
---|
201 |
|
---|
202 | static int VBoxDrvFreeBSDUnload(void)
|
---|
203 | {
|
---|
204 | dprintf(("VBoxDrvFreeBSDUnload:\n"));
|
---|
205 |
|
---|
206 | if (g_cUsers > 0)
|
---|
207 | return EBUSY;
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * Reserve what we did in VBoxDrvFreeBSDInit.
|
---|
211 | */
|
---|
212 | EVENTHANDLER_DEREGISTER(dev_clone, g_VBoxDrvFreeBSDEHTag);
|
---|
213 | clone_cleanup(&g_pVBoxDrvFreeBSDClones);
|
---|
214 |
|
---|
215 | supdrvDeleteDevExt(&g_VBoxDrvFreeBSDDevExt);
|
---|
216 |
|
---|
217 | RTR0Term();
|
---|
218 |
|
---|
219 | memset(&g_VBoxDrvFreeBSDDevExt, 0, sizeof(g_VBoxDrvFreeBSDDevExt));
|
---|
220 |
|
---|
221 | dprintf(("VBoxDrvFreeBSDUnload: returns\n"));
|
---|
222 | return VINF_SUCCESS;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * DEVFS event handler.
|
---|
228 | */
|
---|
229 | static void VBoxDrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *pszName, int cchName, struct cdev **ppDev)
|
---|
230 | {
|
---|
231 | int iUnit;
|
---|
232 | int rc;
|
---|
233 |
|
---|
234 | dprintf(("VBoxDrvFreeBSDClone: pszName=%s ppDev=%p\n", pszName, ppDev));
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * One device node per user, si_drv1 points to the session.
|
---|
238 | * /dev/vboxdrv<N> where N = {0...255}.
|
---|
239 | */
|
---|
240 | if (!ppDev)
|
---|
241 | return;
|
---|
242 | if (dev_stdclone(pszName, NULL, "vboxdrv", &iUnit) != 1)
|
---|
243 | return;
|
---|
244 | if (iUnit >= 256 || iUnit < 0)
|
---|
245 | {
|
---|
246 | dprintf(("VBoxDrvFreeBSDClone: iUnit=%d >= 256 - rejected\n", iUnit));
|
---|
247 | return;
|
---|
248 | }
|
---|
249 |
|
---|
250 | dprintf(("VBoxDrvFreeBSDClone: pszName=%s iUnit=%d\n", pszName, iUnit));
|
---|
251 |
|
---|
252 | rc = clone_create(&g_pVBoxDrvFreeBSDClones, &g_VBoxDrvFreeBSDChrDevSW, &iUnit, ppDev, 0);
|
---|
253 | dprintf(("VBoxDrvFreeBSDClone: clone_create -> %d; iUnit=%d\n", rc, iUnit));
|
---|
254 | if (rc)
|
---|
255 | {
|
---|
256 | #if __FreeBSD_version > 800061
|
---|
257 | *ppDev = make_dev(&g_VBoxDrvFreeBSDChrDevSW, iUnit, UID_ROOT, GID_WHEEL, VBOXDRV_PERM, "vboxdrv%d", iUnit);
|
---|
258 | #else
|
---|
259 | *ppDev = make_dev(&g_VBoxDrvFreeBSDChrDevSW, unit2minor(iUnit), UID_ROOT, GID_WHEEL, VBOXDRV_PERM, "vboxdrv%d", iUnit);
|
---|
260 | #endif
|
---|
261 | if (*ppDev)
|
---|
262 | {
|
---|
263 | dev_ref(*ppDev);
|
---|
264 | (*ppDev)->si_flags |= SI_CHEAPCLONE;
|
---|
265 | dprintf(("VBoxDrvFreeBSDClone: Created *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
|
---|
266 | *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
|
---|
267 | (*ppDev)->si_drv1 = (*ppDev)->si_drv2 = NULL;
|
---|
268 | }
|
---|
269 | else
|
---|
270 | OSDBGPRINT(("VBoxDrvFreeBSDClone: make_dev iUnit=%d failed\n", iUnit));
|
---|
271 | }
|
---|
272 | else
|
---|
273 | dprintf(("VBoxDrvFreeBSDClone: Existing *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
|
---|
274 | *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 |
|
---|
279 | /**
|
---|
280 | *
|
---|
281 | * @returns 0 on success, errno on failure.
|
---|
282 | * EBUSY if the device is used by someone else.
|
---|
283 | * @param pDev The device node.
|
---|
284 | * @param fOpen The open flags.
|
---|
285 | * @param pTd The thread.
|
---|
286 | * @param pFd The file descriptor. FreeBSD 7.0 and later.
|
---|
287 | * @param iFd The file descriptor index(?). Pre FreeBSD 7.0.
|
---|
288 | */
|
---|
289 | #if __FreeBSD__ >= 7
|
---|
290 | static int VBoxDrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd, struct file *pFd)
|
---|
291 | #else
|
---|
292 | static int VBoxDrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd, int iFd)
|
---|
293 | #endif
|
---|
294 | {
|
---|
295 | PSUPDRVSESSION pSession;
|
---|
296 | int rc;
|
---|
297 |
|
---|
298 | #if __FreeBSD_version < 800062
|
---|
299 | dprintf(("VBoxDrvFreeBSDOpen: fOpen=%#x iUnit=%d\n", fOpen, minor2unit(minor(pDev))));
|
---|
300 | #else
|
---|
301 | dprintf(("VBoxDrvFreeBSDOpen: fOpen=%#x iUnit=%d\n", fOpen, minor(dev2udev(pDev))));
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | /*
|
---|
305 | * Let's be a bit picky about the flags...
|
---|
306 | */
|
---|
307 | if (fOpen != (FREAD|FWRITE /*=O_RDWR*/))
|
---|
308 | {
|
---|
309 | dprintf(("VBoxDrvFreeBSDOpen: fOpen=%#x expected %#x\n", fOpen, O_RDWR));
|
---|
310 | return EINVAL;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /*
|
---|
314 | * Try grab it (we don't grab the giant, remember).
|
---|
315 | */
|
---|
316 | if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, (void *)0x42, NULL))
|
---|
317 | return EBUSY;
|
---|
318 |
|
---|
319 | /*
|
---|
320 | * Create a new session.
|
---|
321 | */
|
---|
322 | rc = supdrvCreateSession(&g_VBoxDrvFreeBSDDevExt, true /* fUser */, &pSession);
|
---|
323 | if (RT_SUCCESS(rc))
|
---|
324 | {
|
---|
325 | /** @todo get (r)uid and (r)gid.
|
---|
326 | pSession->Uid = stuff;
|
---|
327 | pSession->Gid = stuff; */
|
---|
328 | if (ASMAtomicCmpXchgPtr(&pDev->si_drv1, pSession, (void *)0x42))
|
---|
329 | {
|
---|
330 | ASMAtomicIncU32(&g_cUsers);
|
---|
331 | return 0;
|
---|
332 | }
|
---|
333 |
|
---|
334 | OSDBGPRINT(("VBoxDrvFreeBSDOpen: si_drv1=%p, expected 0x42!\n", pDev->si_drv1));
|
---|
335 | supdrvCloseSession(&g_VBoxDrvFreeBSDDevExt, pSession);
|
---|
336 | }
|
---|
337 |
|
---|
338 | return RTErrConvertToErrno(rc);
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Close a file device previously opened by VBoxDrvFreeBSDOpen
|
---|
344 | *
|
---|
345 | * @returns 0 on success.
|
---|
346 | * @param pDev The device.
|
---|
347 | * @param fFile The file descriptor flags.
|
---|
348 | * @param DevType The device type (CHR.
|
---|
349 | * @param pTd The calling thread.
|
---|
350 | */
|
---|
351 | static int VBoxDrvFreeBSDClose(struct cdev *pDev, int fFile, int DevType, struct thread *pTd)
|
---|
352 | {
|
---|
353 | PSUPDRVSESSION pSession = (PSUPDRVSESSION)pDev->si_drv1;
|
---|
354 | #if __FreeBSD_version < 800062
|
---|
355 | dprintf(("VBoxDrvFreeBSDClose: fFile=%#x iUnit=%d pSession=%p\n", fFile, minor2unit(minor(pDev)), pSession));
|
---|
356 | #else
|
---|
357 | dprintf(("VBoxDrvFreeBSDClose: fFile=%#x iUnit=%d pSession=%p\n", fFile, minor(dev2udev(pDev)), pSession));
|
---|
358 | #endif
|
---|
359 |
|
---|
360 | /*
|
---|
361 | * Close the session if it's still hanging on to the device...
|
---|
362 | */
|
---|
363 | if (VALID_PTR(pSession))
|
---|
364 | {
|
---|
365 | supdrvCloseSession(&g_VBoxDrvFreeBSDDevExt, pSession);
|
---|
366 | if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, NULL, pSession))
|
---|
367 | OSDBGPRINT(("VBoxDrvFreeBSDClose: si_drv1=%p expected %p!\n", pDev->si_drv1, pSession));
|
---|
368 | ASMAtomicDecU32(&g_cUsers);
|
---|
369 | /* Don't use destroy_dev here because it may sleep resulting in a hanging user process. */
|
---|
370 | destroy_dev_sched(pDev);
|
---|
371 | }
|
---|
372 | else
|
---|
373 | OSDBGPRINT(("VBoxDrvFreeBSDClose: si_drv1=%p!\n", pSession));
|
---|
374 | return 0;
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * I/O control request.
|
---|
380 | *
|
---|
381 | * @returns depends...
|
---|
382 | * @param pDev The device.
|
---|
383 | * @param ulCmd The command.
|
---|
384 | * @param pvData Pointer to the data.
|
---|
385 | * @param fFile The file descriptor flags.
|
---|
386 | * @param pTd The calling thread.
|
---|
387 | */
|
---|
388 | static int VBoxDrvFreeBSDIOCtl(struct cdev *pDev, u_long ulCmd, caddr_t pvData, int fFile, struct thread *pTd)
|
---|
389 | {
|
---|
390 | /*
|
---|
391 | * Validate the input.
|
---|
392 | */
|
---|
393 | PSUPDRVSESSION pSession = (PSUPDRVSESSION)pDev->si_drv1;
|
---|
394 | if (RT_UNLIKELY(!VALID_PTR(pSession)))
|
---|
395 | return EINVAL;
|
---|
396 |
|
---|
397 | /*
|
---|
398 | * Deal with the fast ioctl path first.
|
---|
399 | */
|
---|
400 | if ( ulCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
401 | || ulCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
|
---|
402 | || ulCmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
403 | return supdrvIOCtlFast(ulCmd, *(uint32_t *)pvData, &g_VBoxDrvFreeBSDDevExt, pSession);
|
---|
404 |
|
---|
405 | return VBoxDrvFreeBSDIOCtlSlow(pSession, ulCmd, pvData, pTd);
|
---|
406 | }
|
---|
407 |
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Deal with the 'slow' I/O control requests.
|
---|
411 | *
|
---|
412 | * @returns 0 on success, appropriate errno on failure.
|
---|
413 | * @param pSession The session.
|
---|
414 | * @param ulCmd The command.
|
---|
415 | * @param pvData The request data.
|
---|
416 | * @param pTd The calling thread.
|
---|
417 | */
|
---|
418 | static int VBoxDrvFreeBSDIOCtlSlow(PSUPDRVSESSION pSession, u_long ulCmd, caddr_t pvData, struct thread *pTd)
|
---|
419 | {
|
---|
420 | PSUPREQHDR pHdr;
|
---|
421 | uint32_t cbReq = IOCPARM_LEN(ulCmd);
|
---|
422 | void *pvUser = NULL;
|
---|
423 |
|
---|
424 | /*
|
---|
425 | * Buffered request?
|
---|
426 | */
|
---|
427 | if ((IOC_DIRMASK & ulCmd) == IOC_INOUT)
|
---|
428 | {
|
---|
429 | pHdr = (PSUPREQHDR)pvData;
|
---|
430 | if (RT_UNLIKELY(cbReq < sizeof(*pHdr)))
|
---|
431 | {
|
---|
432 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: cbReq=%#x < %#x; ulCmd=%#lx\n", cbReq, (int)sizeof(*pHdr), ulCmd));
|
---|
433 | return EINVAL;
|
---|
434 | }
|
---|
435 | if (RT_UNLIKELY((pHdr->fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
|
---|
436 | {
|
---|
437 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: bad magic fFlags=%#x; ulCmd=%#lx\n", pHdr->fFlags, ulCmd));
|
---|
438 | return EINVAL;
|
---|
439 | }
|
---|
440 | if (RT_UNLIKELY( RT_MAX(pHdr->cbIn, pHdr->cbOut) != cbReq
|
---|
441 | || pHdr->cbIn < sizeof(*pHdr)
|
---|
442 | || pHdr->cbOut < sizeof(*pHdr)))
|
---|
443 | {
|
---|
444 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: max(%#x,%#x) != %#x; ulCmd=%#lx\n", pHdr->cbIn, pHdr->cbOut, cbReq, ulCmd));
|
---|
445 | return EINVAL;
|
---|
446 | }
|
---|
447 | }
|
---|
448 | /*
|
---|
449 | * Big unbuffered request?
|
---|
450 | */
|
---|
451 | else if ((IOC_DIRMASK & ulCmd) == IOC_VOID && !cbReq)
|
---|
452 | {
|
---|
453 | /*
|
---|
454 | * Read the header, validate it and figure out how much that needs to be buffered.
|
---|
455 | */
|
---|
456 | SUPREQHDR Hdr;
|
---|
457 | pvUser = *(void **)pvData;
|
---|
458 | int rc = copyin(pvUser, &Hdr, sizeof(Hdr));
|
---|
459 | if (RT_UNLIKELY(rc))
|
---|
460 | {
|
---|
461 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: copyin(%p,Hdr,) -> %#x; ulCmd=%#lx\n", pvUser, rc, ulCmd));
|
---|
462 | return rc;
|
---|
463 | }
|
---|
464 | if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
|
---|
465 | {
|
---|
466 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: bad magic fFlags=%#x; ulCmd=%#lx\n", Hdr.fFlags, ulCmd));
|
---|
467 | return EINVAL;
|
---|
468 | }
|
---|
469 | cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
|
---|
470 | if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
|
---|
471 | || Hdr.cbOut < sizeof(Hdr)
|
---|
472 | || cbReq > _1M*16))
|
---|
473 | {
|
---|
474 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: max(%#x,%#x); ulCmd=%#lx\n", Hdr.cbIn, Hdr.cbOut, ulCmd));
|
---|
475 | return EINVAL;
|
---|
476 | }
|
---|
477 |
|
---|
478 | /*
|
---|
479 | * Allocate buffer and copy in the data.
|
---|
480 | */
|
---|
481 | pHdr = (PSUPREQHDR)RTMemTmpAlloc(cbReq);
|
---|
482 | if (RT_UNLIKELY(!pHdr))
|
---|
483 | {
|
---|
484 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: failed to allocate buffer of %d bytes; ulCmd=%#lx\n", cbReq, ulCmd));
|
---|
485 | return ENOMEM;
|
---|
486 | }
|
---|
487 | rc = copyin(pvUser, pHdr, Hdr.cbIn);
|
---|
488 | if (RT_UNLIKELY(rc))
|
---|
489 | {
|
---|
490 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: copyin(%p,%p,%#x) -> %#x; ulCmd=%#lx\n",
|
---|
491 | pvUser, pHdr, Hdr.cbIn, rc, ulCmd));
|
---|
492 | RTMemTmpFree(pHdr);
|
---|
493 | return rc;
|
---|
494 | }
|
---|
495 | }
|
---|
496 | else
|
---|
497 | {
|
---|
498 | dprintf(("VBoxDrvFreeBSDIOCtlSlow: huh? cbReq=%#x ulCmd=%#lx\n", cbReq, ulCmd));
|
---|
499 | return EINVAL;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /*
|
---|
503 | * Process the IOCtl.
|
---|
504 | */
|
---|
505 | int rc = supdrvIOCtl(ulCmd, &g_VBoxDrvFreeBSDDevExt, pSession, pHdr);
|
---|
506 | if (RT_LIKELY(!rc))
|
---|
507 | {
|
---|
508 | /*
|
---|
509 | * If unbuffered, copy back the result before returning.
|
---|
510 | */
|
---|
511 | if (pvUser)
|
---|
512 | {
|
---|
513 | uint32_t cbOut = pHdr->cbOut;
|
---|
514 | if (cbOut > cbReq)
|
---|
515 | {
|
---|
516 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: too much output! %#x > %#x; uCmd=%#lx!\n", cbOut, cbReq, ulCmd));
|
---|
517 | cbOut = cbReq;
|
---|
518 | }
|
---|
519 | rc = copyout(pHdr, pvUser, cbOut);
|
---|
520 | if (RT_UNLIKELY(rc))
|
---|
521 | OSDBGPRINT(("VBoxDrvFreeBSDIOCtlSlow: copyout(%p,%p,%#x) -> %d; uCmd=%#lx!\n", pHdr, pvUser, cbOut, rc, ulCmd));
|
---|
522 |
|
---|
523 | dprintf(("VBoxDrvFreeBSDIOCtlSlow: returns %d / %d ulCmd=%lx\n", 0, pHdr->rc, ulCmd));
|
---|
524 |
|
---|
525 | /* cleanup */
|
---|
526 | RTMemTmpFree(pHdr);
|
---|
527 | }
|
---|
528 | }
|
---|
529 | else
|
---|
530 | {
|
---|
531 | /*
|
---|
532 | * The request failed, just clean up.
|
---|
533 | */
|
---|
534 | if (pvUser)
|
---|
535 | RTMemTmpFree(pHdr);
|
---|
536 |
|
---|
537 | dprintf(("VBoxDrvFreeBSDIOCtlSlow: ulCmd=%lx pData=%p failed, rc=%d\n", ulCmd, pvData, rc));
|
---|
538 | rc = EINVAL;
|
---|
539 | }
|
---|
540 |
|
---|
541 | return rc;
|
---|
542 | }
|
---|
543 |
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * The SUPDRV IDC entry point.
|
---|
547 | *
|
---|
548 | * @returns VBox status code, see supdrvIDC.
|
---|
549 | * @param iReq The request code.
|
---|
550 | * @param pReq The request.
|
---|
551 | */
|
---|
552 | int VBOXCALL SUPDrvFreeBSDIDC(uint32_t uReq, PSUPDRVIDCREQHDR pReq)
|
---|
553 | {
|
---|
554 | PSUPDRVSESSION pSession;
|
---|
555 |
|
---|
556 | /*
|
---|
557 | * Some quick validations.
|
---|
558 | */
|
---|
559 | if (RT_UNLIKELY(!VALID_PTR(pReq)))
|
---|
560 | return VERR_INVALID_POINTER;
|
---|
561 |
|
---|
562 | pSession = pReq->pSession;
|
---|
563 | if (pSession)
|
---|
564 | {
|
---|
565 | if (RT_UNLIKELY(!VALID_PTR(pReq->pSession)))
|
---|
566 | return VERR_INVALID_PARAMETER;
|
---|
567 | if (RT_UNLIKELY(pSession->pDevExt != &g_VBoxDrvFreeBSDDevExt))
|
---|
568 | return VERR_INVALID_PARAMETER;
|
---|
569 | }
|
---|
570 | else if (RT_UNLIKELY(uReq != SUPDRV_IDC_REQ_CONNECT))
|
---|
571 | return VERR_INVALID_PARAMETER;
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Do the job.
|
---|
575 | */
|
---|
576 | return supdrvIDC(uReq, &g_VBoxDrvFreeBSDDevExt, pSession, pReq);
|
---|
577 | }
|
---|
578 |
|
---|
579 |
|
---|
580 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
581 | {
|
---|
582 | NOREF(pObj);
|
---|
583 | NOREF(pSession);
|
---|
584 | }
|
---|
585 |
|
---|
586 |
|
---|
587 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
588 | {
|
---|
589 | NOREF(pObj);
|
---|
590 | NOREF(pSession);
|
---|
591 | NOREF(pszObjName);
|
---|
592 | NOREF(prc);
|
---|
593 | return false;
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
|
---|
598 | {
|
---|
599 | return false;
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
604 | {
|
---|
605 | va_list va;
|
---|
606 | char szMsg[256];
|
---|
607 | int cch;
|
---|
608 |
|
---|
609 | va_start(va, pszFormat);
|
---|
610 | cch = RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, va);
|
---|
611 | va_end(va);
|
---|
612 |
|
---|
613 | printf("%s", szMsg);
|
---|
614 |
|
---|
615 | return cch;
|
---|
616 | }
|
---|
617 |
|
---|