VirtualBox

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

Last change on this file was 100267, checked in by vboxsync, 11 months ago

Additions: Make the R0 physical heap configurable to allow for allocations >= 4GiB if supported by the VBox device (the MMIO request path is available), add support for the MMIO request path required for ARM, bugref:10457

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

© 2023 Oracle
ContactPrivacy policyTerms of Use