VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/freebsd/SUPDrv-freebsd.c@ 18499

Last change on this file since 18499 was 16030, checked in by vboxsync, 15 years ago

SUPDrv-freebsd.c: Made it compile again.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use