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