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