VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-freebsd.c@ 72352

Last change on this file since 72352 was 70873, checked in by vboxsync, 7 years ago

VMMDev,VBoxGuest: Classify who is calling the host (part 1). bugref:9105

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.3 KB
Line 
1/* $Id: VBoxGuest-freebsd.c 70873 2018-02-05 18:13:55Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions Driver for FreeBSD.
4 */
5
6/*
7 * Copyright (C) 2007-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/** @todo r=bird: This must merge with SUPDrv-freebsd.c before long. The two
28 * source files should only differ on prefixes and the extra bits wrt to the
29 * pci device. I.e. it should be diffable so that fixes to one can easily be
30 * applied to the other. */
31
32
33/*********************************************************************************************************************************
34* Header Files *
35*********************************************************************************************************************************/
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#include <sys/bus.h>
47#include <sys/poll.h>
48#include <sys/selinfo.h>
49#include <sys/queue.h>
50#include <sys/lock.h>
51#include <sys/lockmgr.h>
52#include <sys/malloc.h>
53#include <sys/file.h>
54#include <sys/rman.h>
55#include <machine/bus.h>
56#include <machine/resource.h>
57#include <dev/pci/pcivar.h>
58#include <dev/pci/pcireg.h>
59
60#include "VBoxGuestInternal.h"
61#include <VBox/version.h>
62#include <VBox/log.h>
63#include <iprt/assert.h>
64#include <iprt/initterm.h>
65#include <iprt/process.h>
66#include <iprt/string.h>
67#include <iprt/mem.h>
68#include <iprt/asm.h>
69
70
71/*********************************************************************************************************************************
72* Defined Constants And Macros *
73*********************************************************************************************************************************/
74/** The module name. */
75#define DEVICE_NAME "vboxguest"
76
77
78/*********************************************************************************************************************************
79* Structures and Typedefs *
80*********************************************************************************************************************************/
81struct VBoxGuestDeviceState
82{
83 /** Resource ID of the I/O port */
84 int iIOPortResId;
85 /** Pointer to the I/O port resource. */
86 struct resource *pIOPortRes;
87 /** Start address of the IO Port. */
88 uint16_t uIOPortBase;
89 /** Resource ID of the MMIO area */
90 int iVMMDevMemResId;
91 /** Pointer to the MMIO resource. */
92 struct resource *pVMMDevMemRes;
93 /** Handle of the MMIO resource. */
94 bus_space_handle_t VMMDevMemHandle;
95 /** Size of the memory area. */
96 bus_size_t VMMDevMemSize;
97 /** Mapping of the register space */
98 void *pMMIOBase;
99 /** IRQ number */
100 int iIrqResId;
101 /** IRQ resource handle. */
102 struct resource *pIrqRes;
103 /** Pointer to the IRQ handler. */
104 void *pfnIrqHandler;
105 /** VMMDev version */
106 uint32_t u32Version;
107};
108
109
110/*********************************************************************************************************************************
111* Internal Functions *
112*********************************************************************************************************************************/
113/*
114 * Character device file handlers.
115 */
116static d_fdopen_t vgdrvFreeBSDOpen;
117static d_close_t vgdrvFreeBSDClose;
118static d_ioctl_t vgdrvFreeBSDIOCtl;
119static int vgdrvFreeBSDIOCtlSlow(PVBOXGUESTSESSION pSession, u_long ulCmd, caddr_t pvData, struct thread *pTd);
120static d_write_t vgdrvFreeBSDWrite;
121static d_read_t vgdrvFreeBSDRead;
122static d_poll_t vgdrvFreeBSDPoll;
123
124/*
125 * IRQ related functions.
126 */
127static void vgdrvFreeBSDRemoveIRQ(device_t pDevice, void *pvState);
128static int vgdrvFreeBSDAddIRQ(device_t pDevice, void *pvState);
129static int vgdrvFreeBSDISR(void *pvState);
130
131
132/*********************************************************************************************************************************
133* Global Variables *
134*********************************************************************************************************************************/
135static MALLOC_DEFINE(M_VBOXGUEST, "vboxguest", "VirtualBox Guest Device Driver");
136
137#ifndef D_NEEDMINOR
138# define D_NEEDMINOR 0
139#endif
140
141/*
142 * The /dev/vboxguest character device entry points.
143 */
144static struct cdevsw g_vgdrvFreeBSDChrDevSW =
145{
146 .d_version = D_VERSION,
147 .d_flags = D_TRACKCLOSE | D_NEEDMINOR,
148 .d_fdopen = vgdrvFreeBSDOpen,
149 .d_close = vgdrvFreeBSDClose,
150 .d_ioctl = vgdrvFreeBSDIOCtl,
151 .d_read = vgdrvFreeBSDRead,
152 .d_write = vgdrvFreeBSDWrite,
153 .d_poll = vgdrvFreeBSDPoll,
154 .d_name = "vboxguest"
155};
156
157/** Device extention & session data association structure. */
158static VBOXGUESTDEVEXT g_DevExt;
159
160/** List of cloned device. Managed by the kernel. */
161static struct clonedevs *g_pvgdrvFreeBSDClones;
162/** The dev_clone event handler tag. */
163static eventhandler_tag g_vgdrvFreeBSDEHTag;
164/** Reference counter */
165static volatile uint32_t cUsers;
166/** selinfo structure used for polling. */
167static struct selinfo g_SelInfo;
168
169/**
170 * DEVFS event handler.
171 */
172static void vgdrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *pszName, int cchName, struct cdev **ppDev)
173{
174 int iUnit;
175 int rc;
176
177 Log(("vgdrvFreeBSDClone: pszName=%s ppDev=%p\n", pszName, ppDev));
178
179 /*
180 * One device node per user, si_drv1 points to the session.
181 * /dev/vboxguest<N> where N = {0...255}.
182 */
183 if (!ppDev)
184 return;
185 if (strcmp(pszName, "vboxguest") == 0)
186 iUnit = -1;
187 else if (dev_stdclone(pszName, NULL, "vboxguest", &iUnit) != 1)
188 return;
189 if (iUnit >= 256)
190 {
191 Log(("vgdrvFreeBSDClone: iUnit=%d >= 256 - rejected\n", iUnit));
192 return;
193 }
194
195 Log(("vgdrvFreeBSDClone: pszName=%s iUnit=%d\n", pszName, iUnit));
196
197 rc = clone_create(&g_pvgdrvFreeBSDClones, &g_vgdrvFreeBSDChrDevSW, &iUnit, ppDev, 0);
198 Log(("vgdrvFreeBSDClone: clone_create -> %d; iUnit=%d\n", rc, iUnit));
199 if (rc)
200 {
201 *ppDev = make_dev(&g_vgdrvFreeBSDChrDevSW,
202 iUnit,
203 UID_ROOT,
204 GID_WHEEL,
205 0664,
206 "vboxguest%d", iUnit);
207 if (*ppDev)
208 {
209 dev_ref(*ppDev);
210 (*ppDev)->si_flags |= SI_CHEAPCLONE;
211 Log(("vgdrvFreeBSDClone: Created *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
212 *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
213 (*ppDev)->si_drv1 = (*ppDev)->si_drv2 = NULL;
214 }
215 else
216 Log(("vgdrvFreeBSDClone: make_dev iUnit=%d failed\n", iUnit));
217 }
218 else
219 Log(("vgdrvFreeBSDClone: Existing *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
220 *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
221}
222
223/**
224 * File open handler
225 *
226 */
227#if __FreeBSD_version >= 700000
228static int vgdrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd, struct file *pFd)
229#else
230static int vgdrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd)
231#endif
232{
233 int rc;
234 PVBOXGUESTSESSION pSession;
235
236 LogFlow(("vgdrvFreeBSDOpen:\n"));
237
238 /*
239 * Try grab it (we don't grab the giant, remember).
240 */
241 if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, (void *)0x42, NULL))
242 return EBUSY;
243
244 /*
245 * Create a new session.
246 */
247 rc = VGDrvCommonCreateUserSession(&g_DevExt, VMMDEV_REQUESTOR_USERMODE, &pSession);
248 if (RT_SUCCESS(rc))
249 {
250 if (ASMAtomicCmpXchgPtr(&pDev->si_drv1, pSession, (void *)0x42))
251 {
252 Log(("vgdrvFreeBSDOpen: success - g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, (int)RTProcSelf()));
253 ASMAtomicIncU32(&cUsers);
254 return 0;
255 }
256
257 VGDrvCommonCloseSession(&g_DevExt, pSession);
258 }
259
260 LogRel(("vgdrvFreeBSDOpen: failed. rc=%d\n", rc));
261 return RTErrConvertToErrno(rc);
262}
263
264/**
265 * File close handler
266 *
267 */
268static int vgdrvFreeBSDClose(struct cdev *pDev, int fFile, int DevType, struct thread *pTd)
269{
270 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
271 Log(("vgdrvFreeBSDClose: fFile=%#x pSession=%p\n", fFile, pSession));
272
273 /*
274 * Close the session if it's still hanging on to the device...
275 */
276 if (VALID_PTR(pSession))
277 {
278 VGDrvCommonCloseSession(&g_DevExt, pSession);
279 if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, NULL, pSession))
280 Log(("vgdrvFreeBSDClose: si_drv1=%p expected %p!\n", pDev->si_drv1, pSession));
281 ASMAtomicDecU32(&cUsers);
282 /* Don't use destroy_dev here because it may sleep resulting in a hanging user process. */
283 destroy_dev_sched(pDev);
284 }
285 else
286 Log(("vgdrvFreeBSDClose: si_drv1=%p!\n", pSession));
287 return 0;
288}
289
290
291/**
292 * I/O control request.
293 *
294 * @returns depends...
295 * @param pDev The device.
296 * @param ulCmd The command.
297 * @param pvData Pointer to the data.
298 * @param fFile The file descriptor flags.
299 * @param pTd The calling thread.
300 */
301static int vgdrvFreeBSDIOCtl(struct cdev *pDev, u_long ulCmd, caddr_t pvData, int fFile, struct thread *pTd)
302{
303 PVBOXGUESTSESSION pSession;
304 devfs_get_cdevpriv((void **)&pSession);
305
306 /*
307 * Deal with the fast ioctl path first.
308 */
309 if (VBGL_IOCTL_IS_FAST(ulCmd))
310 return VGDrvCommonIoCtlFast(ulCmd, &g_DevExt, pSession);
311
312 return vgdrvFreeBSDIOCtlSlow(pSession, ulCmd, pvData, pTd);
313}
314
315
316/**
317 * Deal with the 'slow' I/O control requests.
318 *
319 * @returns 0 on success, appropriate errno on failure.
320 * @param pSession The session.
321 * @param ulCmd The command.
322 * @param pvData The request data.
323 * @param pTd The calling thread.
324 */
325static int vgdrvFreeBSDIOCtlSlow(PVBOXGUESTSESSION pSession, u_long ulCmd, caddr_t pvData, struct thread *pTd)
326{
327 PVBGLREQHDR pHdr;
328 uint32_t cbReq = IOCPARM_LEN(ulCmd);
329 void *pvUser = NULL;
330
331 /*
332 * Buffered request?
333 */
334 if ((IOC_DIRMASK & ulCmd) == IOC_INOUT)
335 {
336 pHdr = (PVBGLREQHDR)pvData;
337 if (RT_UNLIKELY(cbReq < sizeof(*pHdr)))
338 {
339 LogRel(("vgdrvFreeBSDIOCtlSlow: cbReq=%#x < %#x; ulCmd=%#lx\n", cbReq, (int)sizeof(*pHdr), ulCmd));
340 return EINVAL;
341 }
342 if (RT_UNLIKELY(pHdr->uVersion != VBGLREQHDR_VERSION))
343 {
344 LogRel(("vgdrvFreeBSDIOCtlSlow: bad uVersion=%#x; ulCmd=%#lx\n", pHdr->uVersion, ulCmd));
345 return EINVAL;
346 }
347 if (RT_UNLIKELY( RT_MAX(pHdr->cbIn, pHdr->cbOut) != cbReq
348 || pHdr->cbIn < sizeof(*pHdr)
349 || (pHdr->cbOut < sizeof(*pHdr) && pHdr->cbOut != 0)))
350 {
351 LogRel(("vgdrvFreeBSDIOCtlSlow: max(%#x,%#x) != %#x; ulCmd=%#lx\n", pHdr->cbIn, pHdr->cbOut, cbReq, ulCmd));
352 return EINVAL;
353 }
354 }
355 /*
356 * Big unbuffered request?
357 */
358 else if ((IOC_DIRMASK & ulCmd) == IOC_VOID && !cbReq)
359 {
360 /*
361 * Read the header, validate it and figure out how much that needs to be buffered.
362 */
363 VBGLREQHDR Hdr;
364 pvUser = *(void **)pvData;
365 int rc = copyin(pvUser, &Hdr, sizeof(Hdr));
366 if (RT_UNLIKELY(rc))
367 {
368 LogRel(("vgdrvFreeBSDIOCtlSlow: copyin(%p,Hdr,) -> %#x; ulCmd=%#lx\n", pvUser, rc, ulCmd));
369 return rc;
370 }
371 if (RT_UNLIKELY(Hdr.uVersion != VBGLREQHDR_VERSION))
372 {
373 LogRel(("vgdrvFreeBSDIOCtlSlow: bad uVersion=%#x; ulCmd=%#lx\n", Hdr.uVersion, ulCmd));
374 return EINVAL;
375 }
376 cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
377 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
378 || (Hdr.cbOut < sizeof(Hdr) && Hdr.cbOut != 0)
379 || cbReq > _1M*16))
380 {
381 LogRel(("vgdrvFreeBSDIOCtlSlow: max(%#x,%#x); ulCmd=%#lx\n", Hdr.cbIn, Hdr.cbOut, ulCmd));
382 return EINVAL;
383 }
384
385 /*
386 * Allocate buffer and copy in the data.
387 */
388 pHdr = (PVBGLREQHDR)RTMemTmpAlloc(cbReq);
389 if (RT_UNLIKELY(!pHdr))
390 {
391 LogRel(("vgdrvFreeBSDIOCtlSlow: failed to allocate buffer of %d bytes; ulCmd=%#lx\n", cbReq, ulCmd));
392 return ENOMEM;
393 }
394 rc = copyin(pvUser, pHdr, Hdr.cbIn);
395 if (RT_UNLIKELY(rc))
396 {
397 LogRel(("vgdrvFreeBSDIOCtlSlow: copyin(%p,%p,%#x) -> %#x; ulCmd=%#lx\n",
398 pvUser, pHdr, Hdr.cbIn, rc, ulCmd));
399 RTMemTmpFree(pHdr);
400 return rc;
401 }
402 if (Hdr.cbIn < cbReq)
403 RT_BZERO((uint8_t *)pHdr + Hdr.cbIn, cbReq - Hdr.cbIn);
404 }
405 else
406 {
407 Log(("vgdrvFreeBSDIOCtlSlow: huh? cbReq=%#x ulCmd=%#lx\n", cbReq, ulCmd));
408 return EINVAL;
409 }
410
411 /*
412 * Process the IOCtl.
413 */
414 int rc = VGDrvCommonIoCtl(ulCmd, &g_DevExt, pSession, pHdr, cbReq);
415 if (RT_LIKELY(!rc))
416 {
417 /*
418 * If unbuffered, copy back the result before returning.
419 */
420 if (pvUser)
421 {
422 uint32_t cbOut = pHdr->cbOut;
423 if (cbOut > cbReq)
424 {
425 LogRel(("vgdrvFreeBSDIOCtlSlow: too much output! %#x > %#x; uCmd=%#lx!\n", cbOut, cbReq, ulCmd));
426 cbOut = cbReq;
427 }
428 rc = copyout(pHdr, pvUser, cbOut);
429 if (RT_UNLIKELY(rc))
430 LogRel(("vgdrvFreeBSDIOCtlSlow: copyout(%p,%p,%#x) -> %d; uCmd=%#lx!\n", pHdr, pvUser, cbOut, rc, ulCmd));
431
432 Log(("vgdrvFreeBSDIOCtlSlow: returns %d / %d ulCmd=%lx\n", 0, pHdr->rc, ulCmd));
433
434 /* cleanup */
435 RTMemTmpFree(pHdr);
436 }
437 }
438 else
439 {
440 /*
441 * The request failed, just clean up.
442 */
443 if (pvUser)
444 RTMemTmpFree(pHdr);
445
446 Log(("vgdrvFreeBSDIOCtlSlow: ulCmd=%lx pData=%p failed, rc=%d\n", ulCmd, pvData, rc));
447 rc = EINVAL;
448 }
449
450 return rc;
451}
452
453
454/**
455 * @note This code is duplicated on other platforms with variations, so please
456 * keep them all up to date when making changes!
457 */
458int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
459{
460 /*
461 * Simple request validation (common code does the rest).
462 */
463 int rc;
464 if ( RT_VALID_PTR(pReqHdr)
465 && cbReq >= sizeof(*pReqHdr))
466 {
467 /*
468 * All requests except the connect one requires a valid session.
469 */
470 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
471 if (pSession)
472 {
473 if ( RT_VALID_PTR(pSession)
474 && pSession->pDevExt == &g_DevExt)
475 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
476 else
477 rc = VERR_INVALID_HANDLE;
478 }
479 else if (uReq == VBGL_IOCTL_IDC_CONNECT)
480 {
481 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
482 if (RT_SUCCESS(rc))
483 {
484 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
485 if (RT_FAILURE(rc))
486 VGDrvCommonCloseSession(&g_DevExt, pSession);
487 }
488 }
489 else
490 rc = VERR_INVALID_HANDLE;
491 }
492 else
493 rc = VERR_INVALID_POINTER;
494 return rc;
495}
496
497
498static int vgdrvFreeBSDPoll(struct cdev *pDev, int fEvents, struct thread *td)
499{
500 int fEventsProcessed;
501
502 LogFlow(("vgdrvFreeBSDPoll: fEvents=%d\n", fEvents));
503
504 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
505 if (RT_UNLIKELY(!VALID_PTR(pSession))) {
506 Log(("vgdrvFreeBSDPoll: no state data for %s\n", devtoname(pDev)));
507 return (fEvents & (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
508 }
509
510 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
511 if (pSession->u32MousePosChangedSeq != u32CurSeq)
512 {
513 fEventsProcessed = fEvents & (POLLIN | POLLRDNORM);
514 pSession->u32MousePosChangedSeq = u32CurSeq;
515 }
516 else
517 {
518 fEventsProcessed = 0;
519
520 selrecord(td, &g_SelInfo);
521 }
522
523 return fEventsProcessed;
524}
525
526static int vgdrvFreeBSDWrite(struct cdev *pDev, struct uio *pUio, int fIo)
527{
528 return 0;
529}
530
531static int vgdrvFreeBSDRead(struct cdev *pDev, struct uio *pUio, int fIo)
532{
533 return 0;
534}
535
536static int vgdrvFreeBSDDetach(device_t pDevice)
537{
538 struct VBoxGuestDeviceState *pState = device_get_softc(pDevice);
539
540 if (cUsers > 0)
541 return EBUSY;
542
543 /*
544 * Reverse what we did in vgdrvFreeBSDAttach.
545 */
546 if (g_vgdrvFreeBSDEHTag != NULL)
547 EVENTHANDLER_DEREGISTER(dev_clone, g_vgdrvFreeBSDEHTag);
548
549 clone_cleanup(&g_pvgdrvFreeBSDClones);
550
551 vgdrvFreeBSDRemoveIRQ(pDevice, pState);
552
553 if (pState->pVMMDevMemRes)
554 bus_release_resource(pDevice, SYS_RES_MEMORY, pState->iVMMDevMemResId, pState->pVMMDevMemRes);
555 if (pState->pIOPortRes)
556 bus_release_resource(pDevice, SYS_RES_IOPORT, pState->iIOPortResId, pState->pIOPortRes);
557
558 VGDrvCommonDeleteDevExt(&g_DevExt);
559
560 RTR0Term();
561
562 return 0;
563}
564
565
566/**
567 * Interrupt service routine.
568 *
569 * @returns Whether the interrupt was from VMMDev.
570 * @param pvState Opaque pointer to the device state.
571 */
572static int vgdrvFreeBSDISR(void *pvState)
573{
574 LogFlow(("vgdrvFreeBSDISR: pvState=%p\n", pvState));
575
576 bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
577
578 return fOurIRQ ? 0 : 1;
579}
580
581void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
582{
583 LogFlow(("VGDrvNativeISRMousePollEvent:\n"));
584
585 /*
586 * Wake up poll waiters.
587 */
588 selwakeup(&g_SelInfo);
589}
590
591
592bool VGDrvNativeProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue)
593{
594 RT_NOREF(pDevExt); RT_NOREF(pszName); RT_NOREF(pszValue);
595 return false;
596}
597
598
599/**
600 * Sets IRQ for VMMDev.
601 *
602 * @returns FreeBSD error code.
603 * @param pDevice Pointer to the device info structure.
604 * @param pvState Pointer to the state info structure.
605 */
606static int vgdrvFreeBSDAddIRQ(device_t pDevice, void *pvState)
607{
608 int iResId = 0;
609 int rc = 0;
610 struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)pvState;
611
612 pState->pIrqRes = bus_alloc_resource_any(pDevice, SYS_RES_IRQ, &iResId, RF_SHAREABLE | RF_ACTIVE);
613
614#if __FreeBSD_version >= 700000
615 rc = bus_setup_intr(pDevice, pState->pIrqRes, INTR_TYPE_BIO | INTR_MPSAFE, NULL, (driver_intr_t *)vgdrvFreeBSDISR, pState,
616 &pState->pfnIrqHandler);
617#else
618 rc = bus_setup_intr(pDevice, pState->pIrqRes, INTR_TYPE_BIO, (driver_intr_t *)vgdrvFreeBSDISR, pState, &pState->pfnIrqHandler);
619#endif
620
621 if (rc)
622 {
623 pState->pfnIrqHandler = NULL;
624 return VERR_DEV_IO_ERROR;
625 }
626
627 pState->iIrqResId = iResId;
628
629 return VINF_SUCCESS;
630}
631
632/**
633 * Removes IRQ for VMMDev.
634 *
635 * @param pDevice Pointer to the device info structure.
636 * @param pvState Opaque pointer to the state info structure.
637 */
638static void vgdrvFreeBSDRemoveIRQ(device_t pDevice, void *pvState)
639{
640 struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)pvState;
641
642 if (pState->pIrqRes)
643 {
644 bus_teardown_intr(pDevice, pState->pIrqRes, pState->pfnIrqHandler);
645 bus_release_resource(pDevice, SYS_RES_IRQ, 0, pState->pIrqRes);
646 }
647}
648
649static int vgdrvFreeBSDAttach(device_t pDevice)
650{
651 int rc;
652 int iResId;
653 struct VBoxGuestDeviceState *pState;
654
655 cUsers = 0;
656
657 /*
658 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
659 */
660 rc = RTR0Init(0);
661 if (RT_FAILURE(rc))
662 {
663 LogFunc(("RTR0Init failed.\n"));
664 return ENXIO;
665 }
666
667 pState = device_get_softc(pDevice);
668
669 /*
670 * Allocate I/O port resource.
671 */
672 iResId = PCIR_BAR(0);
673 pState->pIOPortRes = bus_alloc_resource_any(pDevice, SYS_RES_IOPORT, &iResId, RF_ACTIVE);
674 pState->uIOPortBase = rman_get_start(pState->pIOPortRes);
675 pState->iIOPortResId = iResId;
676 if (pState->uIOPortBase)
677 {
678 /*
679 * Map the MMIO region.
680 */
681 iResId = PCIR_BAR(1);
682 pState->pVMMDevMemRes = bus_alloc_resource_any(pDevice, SYS_RES_MEMORY, &iResId, RF_ACTIVE);
683 pState->VMMDevMemHandle = rman_get_bushandle(pState->pVMMDevMemRes);
684 pState->VMMDevMemSize = rman_get_size(pState->pVMMDevMemRes);
685
686 pState->pMMIOBase = rman_get_virtual(pState->pVMMDevMemRes);
687 pState->iVMMDevMemResId = iResId;
688 if (pState->pMMIOBase)
689 {
690 /*
691 * Call the common device extension initializer.
692 */
693 rc = VGDrvCommonInitDevExt(&g_DevExt, pState->uIOPortBase,
694 pState->pMMIOBase, pState->VMMDevMemSize,
695#if ARCH_BITS == 64
696 VBOXOSTYPE_FreeBSD_x64,
697#else
698 VBOXOSTYPE_FreeBSD,
699#endif
700 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
701 if (RT_SUCCESS(rc))
702 {
703 /*
704 * Add IRQ of VMMDev.
705 */
706 rc = vgdrvFreeBSDAddIRQ(pDevice, pState);
707 if (RT_SUCCESS(rc))
708 {
709 /*
710 * Read host configuration.
711 */
712 VGDrvCommonProcessOptionsFromHost(&g_DevExt);
713
714 /*
715 * Configure device cloning.
716 */
717 clone_setup(&g_pvgdrvFreeBSDClones);
718 g_vgdrvFreeBSDEHTag = EVENTHANDLER_REGISTER(dev_clone, vgdrvFreeBSDClone, 0, 1000);
719 if (g_vgdrvFreeBSDEHTag)
720 {
721 printf(DEVICE_NAME ": loaded successfully\n");
722 return 0;
723 }
724
725 printf(DEVICE_NAME ": EVENTHANDLER_REGISTER(dev_clone,,,) failed\n");
726 clone_cleanup(&g_pvgdrvFreeBSDClones);
727 vgdrvFreeBSDRemoveIRQ(pDevice, pState);
728 }
729 else
730 printf((DEVICE_NAME ": VGDrvCommonInitDevExt failed.\n"));
731 VGDrvCommonDeleteDevExt(&g_DevExt);
732 }
733 else
734 printf((DEVICE_NAME ": vgdrvFreeBSDAddIRQ failed.\n"));
735 }
736 else
737 printf((DEVICE_NAME ": MMIO region setup failed.\n"));
738 }
739 else
740 printf((DEVICE_NAME ": IOport setup failed.\n"));
741
742 RTR0Term();
743 return ENXIO;
744}
745
746static int vgdrvFreeBSDProbe(device_t pDevice)
747{
748 if ((pci_get_vendor(pDevice) == VMMDEV_VENDORID) && (pci_get_device(pDevice) == VMMDEV_DEVICEID))
749 return 0;
750
751 return ENXIO;
752}
753
754static device_method_t vgdrvFreeBSDMethods[] =
755{
756 /* Device interface. */
757 DEVMETHOD(device_probe, vgdrvFreeBSDProbe),
758 DEVMETHOD(device_attach, vgdrvFreeBSDAttach),
759 DEVMETHOD(device_detach, vgdrvFreeBSDDetach),
760 {0,0}
761};
762
763static driver_t vgdrvFreeBSDDriver =
764{
765 DEVICE_NAME,
766 vgdrvFreeBSDMethods,
767 sizeof(struct VBoxGuestDeviceState),
768};
769
770static devclass_t vgdrvFreeBSDClass;
771
772DRIVER_MODULE(vboxguest, pci, vgdrvFreeBSDDriver, vgdrvFreeBSDClass, 0, 0);
773MODULE_VERSION(vboxguest, 1);
774
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette