1 | /* $Id: VBoxGuest-freebsd.c 100267 2023-06-23 14:57:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Guest Additions Driver for FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
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 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
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
|
---|
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.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
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 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Header Files *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | #include <sys/param.h>
|
---|
47 | #undef PVM
|
---|
48 | #include <sys/types.h>
|
---|
49 | #include <sys/module.h>
|
---|
50 | #include <sys/systm.h>
|
---|
51 | #include <sys/errno.h>
|
---|
52 | #include <sys/kernel.h>
|
---|
53 | #include <sys/fcntl.h>
|
---|
54 | #include <sys/conf.h>
|
---|
55 | #include <sys/uio.h>
|
---|
56 | #include <sys/bus.h>
|
---|
57 | #include <sys/poll.h>
|
---|
58 | #include <sys/selinfo.h>
|
---|
59 | #include <sys/queue.h>
|
---|
60 | #include <sys/lock.h>
|
---|
61 | #include <sys/lockmgr.h>
|
---|
62 | #include <sys/malloc.h>
|
---|
63 | #include <sys/file.h>
|
---|
64 | #include <sys/rman.h>
|
---|
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"
|
---|
71 | #include <VBox/version.h>
|
---|
72 | #include <VBox/log.h>
|
---|
73 | #include <iprt/assert.h>
|
---|
74 | #include <iprt/initterm.h>
|
---|
75 | #include <iprt/process.h>
|
---|
76 | #include <iprt/string.h>
|
---|
77 | #include <iprt/mem.h>
|
---|
78 | #include <iprt/asm.h>
|
---|
79 |
|
---|
80 |
|
---|
81 | /*********************************************************************************************************************************
|
---|
82 | * Defined Constants And Macros *
|
---|
83 | *********************************************************************************************************************************/
|
---|
84 | /** The module name. */
|
---|
85 | #define DEVICE_NAME "vboxguest"
|
---|
86 |
|
---|
87 |
|
---|
88 | /*********************************************************************************************************************************
|
---|
89 | * Structures and Typedefs *
|
---|
90 | *********************************************************************************************************************************/
|
---|
91 | struct VBoxGuestDeviceState
|
---|
92 | {
|
---|
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. */
|
---|
98 | uint16_t uIOPortBase;
|
---|
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;
|
---|
107 | /** Mapping of the register space */
|
---|
108 | void *pMMIOBase;
|
---|
109 | /** IRQ number */
|
---|
110 | int iIrqResId;
|
---|
111 | /** IRQ resource handle. */
|
---|
112 | struct resource *pIrqRes;
|
---|
113 | /** Pointer to the IRQ handler. */
|
---|
114 | void *pfnIrqHandler;
|
---|
115 | /** VMMDev version */
|
---|
116 | uint32_t u32Version;
|
---|
117 | };
|
---|
118 |
|
---|
119 |
|
---|
120 | /*********************************************************************************************************************************
|
---|
121 | * Internal Functions *
|
---|
122 | *********************************************************************************************************************************/
|
---|
123 | /*
|
---|
124 | * Character device file handlers.
|
---|
125 | */
|
---|
126 | static d_fdopen_t vgdrvFreeBSDOpen;
|
---|
127 | static d_close_t vgdrvFreeBSDClose;
|
---|
128 | static d_ioctl_t vgdrvFreeBSDIOCtl;
|
---|
129 | static int vgdrvFreeBSDIOCtlSlow(PVBOXGUESTSESSION pSession, u_long ulCmd, caddr_t pvData, struct thread *pTd);
|
---|
130 | static d_write_t vgdrvFreeBSDWrite;
|
---|
131 | static d_read_t vgdrvFreeBSDRead;
|
---|
132 | static d_poll_t vgdrvFreeBSDPoll;
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * IRQ related functions.
|
---|
136 | */
|
---|
137 | static void vgdrvFreeBSDRemoveIRQ(device_t pDevice, void *pvState);
|
---|
138 | static int vgdrvFreeBSDAddIRQ(device_t pDevice, void *pvState);
|
---|
139 | static int vgdrvFreeBSDISR(void *pvState);
|
---|
140 |
|
---|
141 |
|
---|
142 | /*********************************************************************************************************************************
|
---|
143 | * Global Variables *
|
---|
144 | *********************************************************************************************************************************/
|
---|
145 | static MALLOC_DEFINE(M_VBOXGUEST, "vboxguest", "VirtualBox Guest Device Driver");
|
---|
146 |
|
---|
147 | #ifndef D_NEEDMINOR
|
---|
148 | # define D_NEEDMINOR 0
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * The /dev/vboxguest character device entry points.
|
---|
153 | */
|
---|
154 | static struct cdevsw g_vgdrvFreeBSDChrDevSW =
|
---|
155 | {
|
---|
156 | .d_version = D_VERSION,
|
---|
157 | .d_flags = D_TRACKCLOSE | D_NEEDMINOR,
|
---|
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"
|
---|
165 | };
|
---|
166 |
|
---|
167 | /** Device extention & session data association structure. */
|
---|
168 | static VBOXGUESTDEVEXT g_DevExt;
|
---|
169 |
|
---|
170 | /** List of cloned device. Managed by the kernel. */
|
---|
171 | static struct clonedevs *g_pvgdrvFreeBSDClones;
|
---|
172 | /** The dev_clone event handler tag. */
|
---|
173 | static eventhandler_tag g_vgdrvFreeBSDEHTag;
|
---|
174 | /** Reference counter */
|
---|
175 | static volatile uint32_t cUsers;
|
---|
176 | /** selinfo structure used for polling. */
|
---|
177 | static struct selinfo g_SelInfo;
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * DEVFS event handler.
|
---|
181 | */
|
---|
182 | static void vgdrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *pszName, int cchName, struct cdev **ppDev)
|
---|
183 | {
|
---|
184 | int iUnit;
|
---|
185 | int rc;
|
---|
186 |
|
---|
187 | Log(("vgdrvFreeBSDClone: pszName=%s ppDev=%p\n", pszName, ppDev));
|
---|
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;
|
---|
195 | if (strcmp(pszName, "vboxguest") == 0)
|
---|
196 | iUnit = -1;
|
---|
197 | else if (dev_stdclone(pszName, NULL, "vboxguest", &iUnit) != 1)
|
---|
198 | return;
|
---|
199 | if (iUnit >= 256)
|
---|
200 | {
|
---|
201 | Log(("vgdrvFreeBSDClone: iUnit=%d >= 256 - rejected\n", iUnit));
|
---|
202 | return;
|
---|
203 | }
|
---|
204 |
|
---|
205 | Log(("vgdrvFreeBSDClone: pszName=%s iUnit=%d\n", pszName, iUnit));
|
---|
206 |
|
---|
207 | rc = clone_create(&g_pvgdrvFreeBSDClones, &g_vgdrvFreeBSDChrDevSW, &iUnit, ppDev, 0);
|
---|
208 | Log(("vgdrvFreeBSDClone: clone_create -> %d; iUnit=%d\n", rc, iUnit));
|
---|
209 | if (rc)
|
---|
210 | {
|
---|
211 | *ppDev = make_dev(&g_vgdrvFreeBSDChrDevSW,
|
---|
212 | iUnit,
|
---|
213 | UID_ROOT,
|
---|
214 | GID_WHEEL,
|
---|
215 | 0664,
|
---|
216 | "vboxguest%d", iUnit);
|
---|
217 | if (*ppDev)
|
---|
218 | {
|
---|
219 | dev_ref(*ppDev);
|
---|
220 | (*ppDev)->si_flags |= SI_CHEAPCLONE;
|
---|
221 | Log(("vgdrvFreeBSDClone: Created *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
|
---|
222 | *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
|
---|
223 | (*ppDev)->si_drv1 = (*ppDev)->si_drv2 = NULL;
|
---|
224 | }
|
---|
225 | else
|
---|
226 | Log(("vgdrvFreeBSDClone: make_dev iUnit=%d failed\n", iUnit));
|
---|
227 | }
|
---|
228 | else
|
---|
229 | Log(("vgdrvFreeBSDClone: Existing *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
|
---|
230 | *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * File open handler
|
---|
235 | *
|
---|
236 | */
|
---|
237 | #if __FreeBSD_version >= 700000
|
---|
238 | static int vgdrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd, struct file *pFd)
|
---|
239 | #else
|
---|
240 | static int vgdrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd)
|
---|
241 | #endif
|
---|
242 | {
|
---|
243 | int rc;
|
---|
244 | PVBOXGUESTSESSION pSession;
|
---|
245 | uint32_t fRequestor;
|
---|
246 | struct ucred *pCred = curthread->td_ucred;
|
---|
247 | if (!pCred)
|
---|
248 | pCred = curproc->p_ucred;
|
---|
249 |
|
---|
250 | LogFlow(("vgdrvFreeBSDOpen:\n"));
|
---|
251 |
|
---|
252 | /*
|
---|
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 | /*
|
---|
259 | * Create a new session.
|
---|
260 | */
|
---|
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);
|
---|
273 | if (RT_SUCCESS(rc))
|
---|
274 | {
|
---|
275 | if (ASMAtomicCmpXchgPtr(&pDev->si_drv1, pSession, (void *)0x42))
|
---|
276 | {
|
---|
277 | Log(("vgdrvFreeBSDOpen: success - g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, (int)RTProcSelf()));
|
---|
278 | ASMAtomicIncU32(&cUsers);
|
---|
279 | return 0;
|
---|
280 | }
|
---|
281 |
|
---|
282 | VGDrvCommonCloseSession(&g_DevExt, pSession);
|
---|
283 | }
|
---|
284 |
|
---|
285 | LogRel(("vgdrvFreeBSDOpen: failed. rc=%d\n", rc));
|
---|
286 | return RTErrConvertToErrno(rc);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * File close handler
|
---|
291 | *
|
---|
292 | */
|
---|
293 | static int vgdrvFreeBSDClose(struct cdev *pDev, int fFile, int DevType, struct thread *pTd)
|
---|
294 | {
|
---|
295 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
|
---|
296 | Log(("vgdrvFreeBSDClose: fFile=%#x pSession=%p\n", fFile, pSession));
|
---|
297 |
|
---|
298 | /*
|
---|
299 | * Close the session if it's still hanging on to the device...
|
---|
300 | */
|
---|
301 | if (RT_VALID_PTR(pSession))
|
---|
302 | {
|
---|
303 | VGDrvCommonCloseSession(&g_DevExt, pSession);
|
---|
304 | if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, NULL, pSession))
|
---|
305 | Log(("vgdrvFreeBSDClose: si_drv1=%p expected %p!\n", pDev->si_drv1, pSession));
|
---|
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);
|
---|
309 | }
|
---|
310 | else
|
---|
311 | Log(("vgdrvFreeBSDClose: si_drv1=%p!\n", pSession));
|
---|
312 | return 0;
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * I/O control request.
|
---|
318 | *
|
---|
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.
|
---|
325 | */
|
---|
326 | static int vgdrvFreeBSDIOCtl(struct cdev *pDev, u_long ulCmd, caddr_t pvData, int fFile, struct thread *pTd)
|
---|
327 | {
|
---|
328 | PVBOXGUESTSESSION pSession;
|
---|
329 | devfs_get_cdevpriv((void **)&pSession);
|
---|
330 |
|
---|
331 | /*
|
---|
332 | * Deal with the fast ioctl path first.
|
---|
333 | */
|
---|
334 | if (VBGL_IOCTL_IS_FAST(ulCmd))
|
---|
335 | return VGDrvCommonIoCtlFast(ulCmd, &g_DevExt, pSession);
|
---|
336 |
|
---|
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 | */
|
---|
350 | static 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 |
|
---|
356 | /*
|
---|
357 | * Buffered request?
|
---|
358 | */
|
---|
359 | if ((IOC_DIRMASK & ulCmd) == IOC_INOUT)
|
---|
360 | {
|
---|
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 | }
|
---|
379 | }
|
---|
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 | }
|
---|
409 |
|
---|
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);
|
---|
429 | }
|
---|
430 | else
|
---|
431 | {
|
---|
432 | Log(("vgdrvFreeBSDIOCtlSlow: huh? cbReq=%#x ulCmd=%#lx\n", cbReq, ulCmd));
|
---|
433 | return EINVAL;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * Process the IOCtl.
|
---|
438 | */
|
---|
439 | int rc = VGDrvCommonIoCtl(ulCmd, &g_DevExt, pSession, pHdr, cbReq);
|
---|
440 | if (RT_LIKELY(!rc))
|
---|
441 | {
|
---|
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));
|
---|
456 |
|
---|
457 | Log(("vgdrvFreeBSDIOCtlSlow: returns %d / %d ulCmd=%lx\n", 0, pHdr->rc, ulCmd));
|
---|
458 |
|
---|
459 | /* cleanup */
|
---|
460 | RTMemTmpFree(pHdr);
|
---|
461 | }
|
---|
462 | }
|
---|
463 | else
|
---|
464 | {
|
---|
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;
|
---|
473 | }
|
---|
474 |
|
---|
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 | */
|
---|
483 | int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
|
---|
484 | {
|
---|
485 | /*
|
---|
486 | * Simple request validation (common code does the rest).
|
---|
487 | */
|
---|
488 | int rc;
|
---|
489 | if ( RT_VALID_PTR(pReqHdr)
|
---|
490 | && cbReq >= sizeof(*pReqHdr))
|
---|
491 | {
|
---|
492 | /*
|
---|
493 | * All requests except the connect one requires a valid session.
|
---|
494 | */
|
---|
495 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
|
---|
496 | if (pSession)
|
---|
497 | {
|
---|
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;
|
---|
503 | }
|
---|
504 | else if (uReq == VBGL_IOCTL_IDC_CONNECT)
|
---|
505 | {
|
---|
506 | rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
|
---|
507 | if (RT_SUCCESS(rc))
|
---|
508 | {
|
---|
509 | rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
|
---|
510 | if (RT_FAILURE(rc))
|
---|
511 | VGDrvCommonCloseSession(&g_DevExt, pSession);
|
---|
512 | }
|
---|
513 | }
|
---|
514 | else
|
---|
515 | rc = VERR_INVALID_HANDLE;
|
---|
516 | }
|
---|
517 | else
|
---|
518 | rc = VERR_INVALID_POINTER;
|
---|
519 | return rc;
|
---|
520 | }
|
---|
521 |
|
---|
522 |
|
---|
523 | static int vgdrvFreeBSDPoll(struct cdev *pDev, int fEvents, struct thread *td)
|
---|
524 | {
|
---|
525 | int fEventsProcessed;
|
---|
526 |
|
---|
527 | LogFlow(("vgdrvFreeBSDPoll: fEvents=%d\n", fEvents));
|
---|
528 |
|
---|
529 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
|
---|
530 | if (RT_UNLIKELY(!RT_VALID_PTR(pSession))) {
|
---|
531 | Log(("vgdrvFreeBSDPoll: no state data for %s\n", devtoname(pDev)));
|
---|
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 |
|
---|
551 | static int vgdrvFreeBSDWrite(struct cdev *pDev, struct uio *pUio, int fIo)
|
---|
552 | {
|
---|
553 | return 0;
|
---|
554 | }
|
---|
555 |
|
---|
556 | static int vgdrvFreeBSDRead(struct cdev *pDev, struct uio *pUio, int fIo)
|
---|
557 | {
|
---|
558 | return 0;
|
---|
559 | }
|
---|
560 |
|
---|
561 | static int vgdrvFreeBSDDetach(device_t pDevice)
|
---|
562 | {
|
---|
563 | struct VBoxGuestDeviceState *pState = device_get_softc(pDevice);
|
---|
564 |
|
---|
565 | if (cUsers > 0)
|
---|
566 | return EBUSY;
|
---|
567 |
|
---|
568 | /*
|
---|
569 | * Reverse what we did in vgdrvFreeBSDAttach.
|
---|
570 | */
|
---|
571 | if (g_vgdrvFreeBSDEHTag != NULL)
|
---|
572 | EVENTHANDLER_DEREGISTER(dev_clone, g_vgdrvFreeBSDEHTag);
|
---|
573 |
|
---|
574 | clone_cleanup(&g_pvgdrvFreeBSDClones);
|
---|
575 |
|
---|
576 | vgdrvFreeBSDRemoveIRQ(pDevice, pState);
|
---|
577 |
|
---|
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 |
|
---|
583 | VGDrvCommonDeleteDevExt(&g_DevExt);
|
---|
584 |
|
---|
585 | RTR0Term();
|
---|
586 |
|
---|
587 | return 0;
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * Interrupt service routine.
|
---|
593 | *
|
---|
594 | * @returns Whether the interrupt was from VMMDev.
|
---|
595 | * @param pvState Opaque pointer to the device state.
|
---|
596 | */
|
---|
597 | static int vgdrvFreeBSDISR(void *pvState)
|
---|
598 | {
|
---|
599 | LogFlow(("vgdrvFreeBSDISR: pvState=%p\n", pvState));
|
---|
600 |
|
---|
601 | bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
|
---|
602 |
|
---|
603 | return fOurIRQ ? 0 : 1;
|
---|
604 | }
|
---|
605 |
|
---|
606 | void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
|
---|
607 | {
|
---|
608 | LogFlow(("VGDrvNativeISRMousePollEvent:\n"));
|
---|
609 |
|
---|
610 | /*
|
---|
611 | * Wake up poll waiters.
|
---|
612 | */
|
---|
613 | selwakeup(&g_SelInfo);
|
---|
614 | }
|
---|
615 |
|
---|
616 |
|
---|
617 | bool 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 |
|
---|
624 | /**
|
---|
625 | * Sets IRQ for VMMDev.
|
---|
626 | *
|
---|
627 | * @returns FreeBSD error code.
|
---|
628 | * @param pDevice Pointer to the device info structure.
|
---|
629 | * @param pvState Pointer to the state info structure.
|
---|
630 | */
|
---|
631 | static int vgdrvFreeBSDAddIRQ(device_t pDevice, void *pvState)
|
---|
632 | {
|
---|
633 | int iResId = 0;
|
---|
634 | int rc = 0;
|
---|
635 | struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)pvState;
|
---|
636 |
|
---|
637 | pState->pIrqRes = bus_alloc_resource_any(pDevice, SYS_RES_IRQ, &iResId, RF_SHAREABLE | RF_ACTIVE);
|
---|
638 |
|
---|
639 | #if __FreeBSD_version >= 700000
|
---|
640 | rc = bus_setup_intr(pDevice, pState->pIrqRes, INTR_TYPE_BIO | INTR_MPSAFE, NULL, (driver_intr_t *)vgdrvFreeBSDISR, pState,
|
---|
641 | &pState->pfnIrqHandler);
|
---|
642 | #else
|
---|
643 | rc = bus_setup_intr(pDevice, pState->pIrqRes, INTR_TYPE_BIO, (driver_intr_t *)vgdrvFreeBSDISR, pState, &pState->pfnIrqHandler);
|
---|
644 | #endif
|
---|
645 |
|
---|
646 | if (rc)
|
---|
647 | {
|
---|
648 | pState->pfnIrqHandler = NULL;
|
---|
649 | return VERR_DEV_IO_ERROR;
|
---|
650 | }
|
---|
651 |
|
---|
652 | pState->iIrqResId = iResId;
|
---|
653 |
|
---|
654 | return VINF_SUCCESS;
|
---|
655 | }
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * Removes IRQ for VMMDev.
|
---|
659 | *
|
---|
660 | * @param pDevice Pointer to the device info structure.
|
---|
661 | * @param pvState Opaque pointer to the state info structure.
|
---|
662 | */
|
---|
663 | static void vgdrvFreeBSDRemoveIRQ(device_t pDevice, void *pvState)
|
---|
664 | {
|
---|
665 | struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)pvState;
|
---|
666 |
|
---|
667 | if (pState->pIrqRes)
|
---|
668 | {
|
---|
669 | bus_teardown_intr(pDevice, pState->pIrqRes, pState->pfnIrqHandler);
|
---|
670 | bus_release_resource(pDevice, SYS_RES_IRQ, 0, pState->pIrqRes);
|
---|
671 | }
|
---|
672 | }
|
---|
673 |
|
---|
674 | static int vgdrvFreeBSDAttach(device_t pDevice)
|
---|
675 | {
|
---|
676 | int rc;
|
---|
677 | int iResId;
|
---|
678 | struct VBoxGuestDeviceState *pState;
|
---|
679 |
|
---|
680 | cUsers = 0;
|
---|
681 |
|
---|
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 |
|
---|
692 | pState = device_get_softc(pDevice);
|
---|
693 |
|
---|
694 | /*
|
---|
695 | * Allocate I/O port resource.
|
---|
696 | */
|
---|
697 | iResId = PCIR_BAR(0);
|
---|
698 | pState->pIOPortRes = bus_alloc_resource_any(pDevice, SYS_RES_IOPORT, &iResId, RF_ACTIVE);
|
---|
699 | pState->uIOPortBase = rman_get_start(pState->pIOPortRes);
|
---|
700 | pState->iIOPortResId = iResId;
|
---|
701 | if (pState->uIOPortBase)
|
---|
702 | {
|
---|
703 | /*
|
---|
704 | * Map the MMIO region.
|
---|
705 | */
|
---|
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);
|
---|
709 | pState->VMMDevMemSize = rman_get_size(pState->pVMMDevMemRes);
|
---|
710 |
|
---|
711 | pState->pMMIOBase = rman_get_virtual(pState->pVMMDevMemRes);
|
---|
712 | pState->iVMMDevMemResId = iResId;
|
---|
713 | if (pState->pMMIOBase)
|
---|
714 | {
|
---|
715 | /*
|
---|
716 | * Call the common device extension initializer.
|
---|
717 | */
|
---|
718 | rc = VGDrvCommonInitDevExt(&g_DevExt, pState->uIOPortBase,
|
---|
719 | NULL /*pvMmioReq*/,
|
---|
720 | pState->pMMIOBase, pState->VMMDevMemSize,
|
---|
721 | #if ARCH_BITS == 64
|
---|
722 | VBOXOSTYPE_FreeBSD_x64,
|
---|
723 | #else
|
---|
724 | VBOXOSTYPE_FreeBSD,
|
---|
725 | #endif
|
---|
726 | VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
|
---|
727 | if (RT_SUCCESS(rc))
|
---|
728 | {
|
---|
729 | /*
|
---|
730 | * Add IRQ of VMMDev.
|
---|
731 | */
|
---|
732 | rc = vgdrvFreeBSDAddIRQ(pDevice, pState);
|
---|
733 | if (RT_SUCCESS(rc))
|
---|
734 | {
|
---|
735 | /*
|
---|
736 | * Read host configuration.
|
---|
737 | */
|
---|
738 | VGDrvCommonProcessOptionsFromHost(&g_DevExt);
|
---|
739 |
|
---|
740 | /*
|
---|
741 | * Configure device cloning.
|
---|
742 | */
|
---|
743 | clone_setup(&g_pvgdrvFreeBSDClones);
|
---|
744 | g_vgdrvFreeBSDEHTag = EVENTHANDLER_REGISTER(dev_clone, vgdrvFreeBSDClone, 0, 1000);
|
---|
745 | if (g_vgdrvFreeBSDEHTag)
|
---|
746 | {
|
---|
747 | printf(DEVICE_NAME ": loaded successfully\n");
|
---|
748 | return 0;
|
---|
749 | }
|
---|
750 |
|
---|
751 | printf(DEVICE_NAME ": EVENTHANDLER_REGISTER(dev_clone,,,) failed\n");
|
---|
752 | clone_cleanup(&g_pvgdrvFreeBSDClones);
|
---|
753 | vgdrvFreeBSDRemoveIRQ(pDevice, pState);
|
---|
754 | }
|
---|
755 | else
|
---|
756 | printf((DEVICE_NAME ": VGDrvCommonInitDevExt failed.\n"));
|
---|
757 | VGDrvCommonDeleteDevExt(&g_DevExt);
|
---|
758 | }
|
---|
759 | else
|
---|
760 | printf((DEVICE_NAME ": vgdrvFreeBSDAddIRQ failed.\n"));
|
---|
761 | }
|
---|
762 | else
|
---|
763 | printf((DEVICE_NAME ": MMIO region setup failed.\n"));
|
---|
764 | }
|
---|
765 | else
|
---|
766 | printf((DEVICE_NAME ": IOport setup failed.\n"));
|
---|
767 |
|
---|
768 | RTR0Term();
|
---|
769 | return ENXIO;
|
---|
770 | }
|
---|
771 |
|
---|
772 | static int vgdrvFreeBSDProbe(device_t pDevice)
|
---|
773 | {
|
---|
774 | if ((pci_get_vendor(pDevice) == VMMDEV_VENDORID) && (pci_get_device(pDevice) == VMMDEV_DEVICEID))
|
---|
775 | return 0;
|
---|
776 |
|
---|
777 | return ENXIO;
|
---|
778 | }
|
---|
779 |
|
---|
780 | static device_method_t vgdrvFreeBSDMethods[] =
|
---|
781 | {
|
---|
782 | /* Device interface. */
|
---|
783 | DEVMETHOD(device_probe, vgdrvFreeBSDProbe),
|
---|
784 | DEVMETHOD(device_attach, vgdrvFreeBSDAttach),
|
---|
785 | DEVMETHOD(device_detach, vgdrvFreeBSDDetach),
|
---|
786 | {0,0}
|
---|
787 | };
|
---|
788 |
|
---|
789 | static driver_t vgdrvFreeBSDDriver =
|
---|
790 | {
|
---|
791 | DEVICE_NAME,
|
---|
792 | vgdrvFreeBSDMethods,
|
---|
793 | sizeof(struct VBoxGuestDeviceState),
|
---|
794 | };
|
---|
795 |
|
---|
796 | static devclass_t vgdrvFreeBSDClass;
|
---|
797 |
|
---|
798 | DRIVER_MODULE(vboxguest, pci, vgdrvFreeBSDDriver, vgdrvFreeBSDClass, 0, 0);
|
---|
799 | MODULE_VERSION(vboxguest, 1);
|
---|
800 |
|
---|