1 | /* $Id: VBoxGuest-freebsd.c 58113 2015-10-08 10:13:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Guest Additions Driver for FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2015 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 |
|
---|
18 | /** @todo r=bird: This must merge with SUPDrv-freebsd.c before long. The two
|
---|
19 | * source files should only differ on prefixes and the extra bits wrt to the
|
---|
20 | * pci device. I.e. it should be diffable so that fixes to one can easily be
|
---|
21 | * applied to the other. */
|
---|
22 |
|
---|
23 |
|
---|
24 | /*********************************************************************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *********************************************************************************************************************************/
|
---|
27 | #include <sys/param.h>
|
---|
28 | #undef PVM
|
---|
29 | #include <sys/types.h>
|
---|
30 | #include <sys/module.h>
|
---|
31 | #include <sys/systm.h>
|
---|
32 | #include <sys/errno.h>
|
---|
33 | #include <sys/kernel.h>
|
---|
34 | #include <sys/fcntl.h>
|
---|
35 | #include <sys/conf.h>
|
---|
36 | #include <sys/uio.h>
|
---|
37 | #include <sys/bus.h>
|
---|
38 | #include <sys/poll.h>
|
---|
39 | #include <sys/selinfo.h>
|
---|
40 | #include <sys/queue.h>
|
---|
41 | #include <sys/lock.h>
|
---|
42 | #include <sys/lockmgr.h>
|
---|
43 | #include <sys/malloc.h>
|
---|
44 | #include <sys/file.h>
|
---|
45 | #include <sys/rman.h>
|
---|
46 | #include <machine/bus.h>
|
---|
47 | #include <machine/resource.h>
|
---|
48 | #include <dev/pci/pcivar.h>
|
---|
49 | #include <dev/pci/pcireg.h>
|
---|
50 |
|
---|
51 | #include "VBoxGuestInternal.h"
|
---|
52 | #include <VBox/version.h>
|
---|
53 | #include <VBox/log.h>
|
---|
54 | #include <iprt/assert.h>
|
---|
55 | #include <iprt/initterm.h>
|
---|
56 | #include <iprt/process.h>
|
---|
57 | #include <iprt/mem.h>
|
---|
58 | #include <iprt/asm.h>
|
---|
59 |
|
---|
60 |
|
---|
61 | /*********************************************************************************************************************************
|
---|
62 | * Defined Constants And Macros *
|
---|
63 | *********************************************************************************************************************************/
|
---|
64 | /** The module name. */
|
---|
65 | #define DEVICE_NAME "vboxguest"
|
---|
66 |
|
---|
67 |
|
---|
68 | /*********************************************************************************************************************************
|
---|
69 | * Structures and Typedefs *
|
---|
70 | *********************************************************************************************************************************/
|
---|
71 | struct VBoxGuestDeviceState
|
---|
72 | {
|
---|
73 | /** Resource ID of the I/O port */
|
---|
74 | int iIOPortResId;
|
---|
75 | /** Pointer to the I/O port resource. */
|
---|
76 | struct resource *pIOPortRes;
|
---|
77 | /** Start address of the IO Port. */
|
---|
78 | uint16_t uIOPortBase;
|
---|
79 | /** Resource ID of the MMIO area */
|
---|
80 | int iVMMDevMemResId;
|
---|
81 | /** Pointer to the MMIO resource. */
|
---|
82 | struct resource *pVMMDevMemRes;
|
---|
83 | /** Handle of the MMIO resource. */
|
---|
84 | bus_space_handle_t VMMDevMemHandle;
|
---|
85 | /** Size of the memory area. */
|
---|
86 | bus_size_t VMMDevMemSize;
|
---|
87 | /** Mapping of the register space */
|
---|
88 | void *pMMIOBase;
|
---|
89 | /** IRQ number */
|
---|
90 | int iIrqResId;
|
---|
91 | /** IRQ resource handle. */
|
---|
92 | struct resource *pIrqRes;
|
---|
93 | /** Pointer to the IRQ handler. */
|
---|
94 | void *pfnIrqHandler;
|
---|
95 | /** VMMDev version */
|
---|
96 | uint32_t u32Version;
|
---|
97 | };
|
---|
98 |
|
---|
99 |
|
---|
100 | /*********************************************************************************************************************************
|
---|
101 | * Internal Functions *
|
---|
102 | *********************************************************************************************************************************/
|
---|
103 | /*
|
---|
104 | * Character device file handlers.
|
---|
105 | */
|
---|
106 | static d_fdopen_t vgdrvFreeBSDOpen;
|
---|
107 | static d_close_t vgdrvFreeBSDClose;
|
---|
108 | static d_ioctl_t vgdrvFreeBSDIOCtl;
|
---|
109 | static d_write_t vgdrvFreeBSDWrite;
|
---|
110 | static d_read_t vgdrvFreeBSDRead;
|
---|
111 | static d_poll_t vgdrvFreeBSDPoll;
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * IRQ related functions.
|
---|
115 | */
|
---|
116 | static void vgdrvFreeBSDRemoveIRQ(device_t pDevice, void *pvState);
|
---|
117 | static int vgdrvFreeBSDAddIRQ(device_t pDevice, void *pvState);
|
---|
118 | static int vgdrvFreeBSDISR(void *pvState);
|
---|
119 |
|
---|
120 |
|
---|
121 | /*********************************************************************************************************************************
|
---|
122 | * Global Variables *
|
---|
123 | *********************************************************************************************************************************/
|
---|
124 | static MALLOC_DEFINE(M_VBOXGUEST, "vboxguest", "VirtualBox Guest Device Driver");
|
---|
125 |
|
---|
126 | #ifndef D_NEEDMINOR
|
---|
127 | # define D_NEEDMINOR 0
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * The /dev/vboxguest character device entry points.
|
---|
132 | */
|
---|
133 | static struct cdevsw g_vgdrvFreeBSDChrDevSW =
|
---|
134 | {
|
---|
135 | .d_version = D_VERSION,
|
---|
136 | .d_flags = D_TRACKCLOSE | D_NEEDMINOR,
|
---|
137 | .d_fdopen = vgdrvFreeBSDOpen,
|
---|
138 | .d_close = vgdrvFreeBSDClose,
|
---|
139 | .d_ioctl = vgdrvFreeBSDIOCtl,
|
---|
140 | .d_read = vgdrvFreeBSDRead,
|
---|
141 | .d_write = vgdrvFreeBSDWrite,
|
---|
142 | .d_poll = vgdrvFreeBSDPoll,
|
---|
143 | .d_name = "vboxguest"
|
---|
144 | };
|
---|
145 |
|
---|
146 | /** Device extention & session data association structure. */
|
---|
147 | static VBOXGUESTDEVEXT g_DevExt;
|
---|
148 |
|
---|
149 | /** List of cloned device. Managed by the kernel. */
|
---|
150 | static struct clonedevs *g_pvgdrvFreeBSDClones;
|
---|
151 | /** The dev_clone event handler tag. */
|
---|
152 | static eventhandler_tag g_vgdrvFreeBSDEHTag;
|
---|
153 | /** Reference counter */
|
---|
154 | static volatile uint32_t cUsers;
|
---|
155 | /** selinfo structure used for polling. */
|
---|
156 | static struct selinfo g_SelInfo;
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * DEVFS event handler.
|
---|
160 | */
|
---|
161 | static void vgdrvFreeBSDClone(void *pvArg, struct ucred *pCred, char *pszName, int cchName, struct cdev **ppDev)
|
---|
162 | {
|
---|
163 | int iUnit;
|
---|
164 | int rc;
|
---|
165 |
|
---|
166 | Log(("vgdrvFreeBSDClone: pszName=%s ppDev=%p\n", pszName, ppDev));
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * One device node per user, si_drv1 points to the session.
|
---|
170 | * /dev/vboxguest<N> where N = {0...255}.
|
---|
171 | */
|
---|
172 | if (!ppDev)
|
---|
173 | return;
|
---|
174 | if (strcmp(pszName, "vboxguest") == 0)
|
---|
175 | iUnit = -1;
|
---|
176 | else if (dev_stdclone(pszName, NULL, "vboxguest", &iUnit) != 1)
|
---|
177 | return;
|
---|
178 | if (iUnit >= 256)
|
---|
179 | {
|
---|
180 | Log(("vgdrvFreeBSDClone: iUnit=%d >= 256 - rejected\n", iUnit));
|
---|
181 | return;
|
---|
182 | }
|
---|
183 |
|
---|
184 | Log(("vgdrvFreeBSDClone: pszName=%s iUnit=%d\n", pszName, iUnit));
|
---|
185 |
|
---|
186 | rc = clone_create(&g_pvgdrvFreeBSDClones, &g_vgdrvFreeBSDChrDevSW, &iUnit, ppDev, 0);
|
---|
187 | Log(("vgdrvFreeBSDClone: clone_create -> %d; iUnit=%d\n", rc, iUnit));
|
---|
188 | if (rc)
|
---|
189 | {
|
---|
190 | *ppDev = make_dev(&g_vgdrvFreeBSDChrDevSW,
|
---|
191 | iUnit,
|
---|
192 | UID_ROOT,
|
---|
193 | GID_WHEEL,
|
---|
194 | 0664,
|
---|
195 | "vboxguest%d", iUnit);
|
---|
196 | if (*ppDev)
|
---|
197 | {
|
---|
198 | dev_ref(*ppDev);
|
---|
199 | (*ppDev)->si_flags |= SI_CHEAPCLONE;
|
---|
200 | Log(("vgdrvFreeBSDClone: Created *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
|
---|
201 | *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
|
---|
202 | (*ppDev)->si_drv1 = (*ppDev)->si_drv2 = NULL;
|
---|
203 | }
|
---|
204 | else
|
---|
205 | Log(("vgdrvFreeBSDClone: make_dev iUnit=%d failed\n", iUnit));
|
---|
206 | }
|
---|
207 | else
|
---|
208 | Log(("vgdrvFreeBSDClone: Existing *ppDev=%p iUnit=%d si_drv1=%p si_drv2=%p\n",
|
---|
209 | *ppDev, iUnit, (*ppDev)->si_drv1, (*ppDev)->si_drv2));
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * File open handler
|
---|
214 | *
|
---|
215 | */
|
---|
216 | #if __FreeBSD_version >= 700000
|
---|
217 | static int vgdrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd, struct file *pFd)
|
---|
218 | #else
|
---|
219 | static int vgdrvFreeBSDOpen(struct cdev *pDev, int fOpen, struct thread *pTd)
|
---|
220 | #endif
|
---|
221 | {
|
---|
222 | int rc;
|
---|
223 | PVBOXGUESTSESSION pSession;
|
---|
224 |
|
---|
225 | LogFlow(("vgdrvFreeBSDOpen:\n"));
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Try grab it (we don't grab the giant, remember).
|
---|
229 | */
|
---|
230 | if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, (void *)0x42, NULL))
|
---|
231 | return EBUSY;
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * Create a new session.
|
---|
235 | */
|
---|
236 | rc = VGDrvCommonCreateUserSession(&g_DevExt, &pSession);
|
---|
237 | if (RT_SUCCESS(rc))
|
---|
238 | {
|
---|
239 | if (ASMAtomicCmpXchgPtr(&pDev->si_drv1, pSession, (void *)0x42))
|
---|
240 | {
|
---|
241 | Log(("vgdrvFreeBSDOpen: success - g_DevExt=%p pSession=%p rc=%d pid=%d\n", &g_DevExt, pSession, rc, (int)RTProcSelf()));
|
---|
242 | ASMAtomicIncU32(&cUsers);
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 | VGDrvCommonCloseSession(&g_DevExt, pSession);
|
---|
247 | }
|
---|
248 |
|
---|
249 | LogRel(("vgdrvFreeBSDOpen: failed. rc=%d\n", rc));
|
---|
250 | return RTErrConvertToErrno(rc);
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * File close handler
|
---|
255 | *
|
---|
256 | */
|
---|
257 | static int vgdrvFreeBSDClose(struct cdev *pDev, int fFile, int DevType, struct thread *pTd)
|
---|
258 | {
|
---|
259 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
|
---|
260 | Log(("vgdrvFreeBSDClose: fFile=%#x pSession=%p\n", fFile, pSession));
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * Close the session if it's still hanging on to the device...
|
---|
264 | */
|
---|
265 | if (VALID_PTR(pSession))
|
---|
266 | {
|
---|
267 | VGDrvCommonCloseSession(&g_DevExt, pSession);
|
---|
268 | if (!ASMAtomicCmpXchgPtr(&pDev->si_drv1, NULL, pSession))
|
---|
269 | Log(("vgdrvFreeBSDClose: si_drv1=%p expected %p!\n", pDev->si_drv1, pSession));
|
---|
270 | ASMAtomicDecU32(&cUsers);
|
---|
271 | /* Don't use destroy_dev here because it may sleep resulting in a hanging user process. */
|
---|
272 | destroy_dev_sched(pDev);
|
---|
273 | }
|
---|
274 | else
|
---|
275 | Log(("vgdrvFreeBSDClose: si_drv1=%p!\n", pSession));
|
---|
276 | return 0;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * IOCTL handler
|
---|
281 | *
|
---|
282 | */
|
---|
283 | static int vgdrvFreeBSDIOCtl(struct cdev *pDev, u_long ulCmd, caddr_t pvData, int fFile, struct thread *pTd)
|
---|
284 | {
|
---|
285 | LogFlow(("vgdrvFreeBSDIOCtl\n"));
|
---|
286 |
|
---|
287 | int rc = 0;
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * Validate the input.
|
---|
291 | */
|
---|
292 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
|
---|
293 | if (RT_UNLIKELY(!VALID_PTR(pSession)))
|
---|
294 | return EINVAL;
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Validate the request wrapper.
|
---|
298 | */
|
---|
299 | if (IOCPARM_LEN(ulCmd) != sizeof(VBGLBIGREQ))
|
---|
300 | {
|
---|
301 | Log(("vgdrvFreeBSDIOCtl: bad request %lu size=%lu expected=%d\n", ulCmd, IOCPARM_LEN(ulCmd), sizeof(VBGLBIGREQ)));
|
---|
302 | return ENOTTY;
|
---|
303 | }
|
---|
304 |
|
---|
305 | PVBGLBIGREQ ReqWrap = (PVBGLBIGREQ)pvData;
|
---|
306 | if (ReqWrap->u32Magic != VBGLBIGREQ_MAGIC)
|
---|
307 | {
|
---|
308 | Log(("vgdrvFreeBSDIOCtl: bad magic %#x; pArg=%p Cmd=%lu.\n", ReqWrap->u32Magic, pvData, ulCmd));
|
---|
309 | return EINVAL;
|
---|
310 | }
|
---|
311 | if (RT_UNLIKELY( ReqWrap->cbData == 0
|
---|
312 | || ReqWrap->cbData > _1M*16))
|
---|
313 | {
|
---|
314 | printf("vgdrvFreeBSDIOCtl: bad size %#x; pArg=%p Cmd=%lu.\n", ReqWrap->cbData, pvData, ulCmd);
|
---|
315 | return EINVAL;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /*
|
---|
319 | * Read the request.
|
---|
320 | */
|
---|
321 | void *pvBuf = RTMemTmpAlloc(ReqWrap->cbData);
|
---|
322 | if (RT_UNLIKELY(!pvBuf))
|
---|
323 | {
|
---|
324 | Log(("vgdrvFreeBSDIOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", ReqWrap->cbData));
|
---|
325 | return ENOMEM;
|
---|
326 | }
|
---|
327 |
|
---|
328 | rc = copyin((void *)(uintptr_t)ReqWrap->pvDataR3, pvBuf, ReqWrap->cbData);
|
---|
329 | if (RT_UNLIKELY(rc))
|
---|
330 | {
|
---|
331 | RTMemTmpFree(pvBuf);
|
---|
332 | Log(("vgdrvFreeBSDIOCtl: copyin failed; pvBuf=%p pArg=%p Cmd=%lu. rc=%d\n", pvBuf, pvData, ulCmd, rc));
|
---|
333 | return EFAULT;
|
---|
334 | }
|
---|
335 | if (RT_UNLIKELY( ReqWrap->cbData != 0
|
---|
336 | && !VALID_PTR(pvBuf)))
|
---|
337 | {
|
---|
338 | RTMemTmpFree(pvBuf);
|
---|
339 | Log(("vgdrvFreeBSDIOCtl: pvBuf invalid pointer %p\n", pvBuf));
|
---|
340 | return EINVAL;
|
---|
341 | }
|
---|
342 | Log(("vgdrvFreeBSDIOCtl: pSession=%p pid=%d.\n", pSession, (int)RTProcSelf()));
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * Process the IOCtl.
|
---|
346 | */
|
---|
347 | size_t cbDataReturned;
|
---|
348 | rc = VGDrvCommonIoCtl(ulCmd, &g_DevExt, pSession, pvBuf, ReqWrap->cbData, &cbDataReturned);
|
---|
349 | if (RT_SUCCESS(rc))
|
---|
350 | {
|
---|
351 | rc = 0;
|
---|
352 | if (RT_UNLIKELY(cbDataReturned > ReqWrap->cbData))
|
---|
353 | {
|
---|
354 | Log(("vgdrvFreeBSDIOCtl: too much output data %d expected %d\n", cbDataReturned, ReqWrap->cbData));
|
---|
355 | cbDataReturned = ReqWrap->cbData;
|
---|
356 | }
|
---|
357 | if (cbDataReturned > 0)
|
---|
358 | {
|
---|
359 | rc = copyout(pvBuf, (void *)(uintptr_t)ReqWrap->pvDataR3, cbDataReturned);
|
---|
360 | if (RT_UNLIKELY(rc))
|
---|
361 | {
|
---|
362 | Log(("vgdrvFreeBSDIOCtl: copyout failed; pvBuf=%p pArg=%p Cmd=%lu. rc=%d\n", pvBuf, pvData, ulCmd, rc));
|
---|
363 | rc = EFAULT;
|
---|
364 | }
|
---|
365 | }
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {
|
---|
369 | Log(("vgdrvFreeBSDIOCtl: VGDrvCommonIoCtl failed. rc=%d\n", rc));
|
---|
370 | rc = EFAULT;
|
---|
371 | }
|
---|
372 | RTMemTmpFree(pvBuf);
|
---|
373 | return rc;
|
---|
374 | }
|
---|
375 |
|
---|
376 | static int vgdrvFreeBSDPoll(struct cdev *pDev, int fEvents, struct thread *td)
|
---|
377 | {
|
---|
378 | int fEventsProcessed;
|
---|
379 |
|
---|
380 | LogFlow(("vgdrvFreeBSDPoll: fEvents=%d\n", fEvents));
|
---|
381 |
|
---|
382 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pDev->si_drv1;
|
---|
383 | if (RT_UNLIKELY(!VALID_PTR(pSession))) {
|
---|
384 | Log(("vgdrvFreeBSDPoll: no state data for %s\n", devtoname(pDev)));
|
---|
385 | return (fEvents & (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
|
---|
386 | }
|
---|
387 |
|
---|
388 | uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
|
---|
389 | if (pSession->u32MousePosChangedSeq != u32CurSeq)
|
---|
390 | {
|
---|
391 | fEventsProcessed = fEvents & (POLLIN | POLLRDNORM);
|
---|
392 | pSession->u32MousePosChangedSeq = u32CurSeq;
|
---|
393 | }
|
---|
394 | else
|
---|
395 | {
|
---|
396 | fEventsProcessed = 0;
|
---|
397 |
|
---|
398 | selrecord(td, &g_SelInfo);
|
---|
399 | }
|
---|
400 |
|
---|
401 | return fEventsProcessed;
|
---|
402 | }
|
---|
403 |
|
---|
404 | static int vgdrvFreeBSDWrite(struct cdev *pDev, struct uio *pUio, int fIo)
|
---|
405 | {
|
---|
406 | return 0;
|
---|
407 | }
|
---|
408 |
|
---|
409 | static int vgdrvFreeBSDRead(struct cdev *pDev, struct uio *pUio, int fIo)
|
---|
410 | {
|
---|
411 | return 0;
|
---|
412 | }
|
---|
413 |
|
---|
414 | static int vgdrvFreeBSDDetach(device_t pDevice)
|
---|
415 | {
|
---|
416 | struct VBoxGuestDeviceState *pState = device_get_softc(pDevice);
|
---|
417 |
|
---|
418 | if (cUsers > 0)
|
---|
419 | return EBUSY;
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * Reverse what we did in vgdrvFreeBSDAttach.
|
---|
423 | */
|
---|
424 | if (g_vgdrvFreeBSDEHTag != NULL)
|
---|
425 | EVENTHANDLER_DEREGISTER(dev_clone, g_vgdrvFreeBSDEHTag);
|
---|
426 |
|
---|
427 | clone_cleanup(&g_pvgdrvFreeBSDClones);
|
---|
428 |
|
---|
429 | vgdrvFreeBSDRemoveIRQ(pDevice, pState);
|
---|
430 |
|
---|
431 | if (pState->pVMMDevMemRes)
|
---|
432 | bus_release_resource(pDevice, SYS_RES_MEMORY, pState->iVMMDevMemResId, pState->pVMMDevMemRes);
|
---|
433 | if (pState->pIOPortRes)
|
---|
434 | bus_release_resource(pDevice, SYS_RES_IOPORT, pState->iIOPortResId, pState->pIOPortRes);
|
---|
435 |
|
---|
436 | VGDrvCommonDeleteDevExt(&g_DevExt);
|
---|
437 |
|
---|
438 | RTR0Term();
|
---|
439 |
|
---|
440 | return 0;
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Interrupt service routine.
|
---|
445 | *
|
---|
446 | * @returns Whether the interrupt was from VMMDev.
|
---|
447 | * @param pvState Opaque pointer to the device state.
|
---|
448 | */
|
---|
449 | static int vgdrvFreeBSDISR(void *pvState)
|
---|
450 | {
|
---|
451 | LogFlow(("vgdrvFreeBSDISR: pvState=%p\n", pvState));
|
---|
452 |
|
---|
453 | bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
|
---|
454 |
|
---|
455 | return fOurIRQ ? 0 : 1;
|
---|
456 | }
|
---|
457 |
|
---|
458 | void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
|
---|
459 | {
|
---|
460 | LogFlow(("VGDrvNativeISRMousePollEvent:\n"));
|
---|
461 |
|
---|
462 | /*
|
---|
463 | * Wake up poll waiters.
|
---|
464 | */
|
---|
465 | selwakeup(&g_SelInfo);
|
---|
466 | }
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Sets IRQ for VMMDev.
|
---|
470 | *
|
---|
471 | * @returns FreeBSD error code.
|
---|
472 | * @param pDevice Pointer to the device info structure.
|
---|
473 | * @param pvState Pointer to the state info structure.
|
---|
474 | */
|
---|
475 | static int vgdrvFreeBSDAddIRQ(device_t pDevice, void *pvState)
|
---|
476 | {
|
---|
477 | int iResId = 0;
|
---|
478 | int rc = 0;
|
---|
479 | struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)pvState;
|
---|
480 |
|
---|
481 | pState->pIrqRes = bus_alloc_resource_any(pDevice, SYS_RES_IRQ, &iResId, RF_SHAREABLE | RF_ACTIVE);
|
---|
482 |
|
---|
483 | #if __FreeBSD_version >= 700000
|
---|
484 | rc = bus_setup_intr(pDevice, pState->pIrqRes, INTR_TYPE_BIO | INTR_MPSAFE, NULL, (driver_intr_t *)vgdrvFreeBSDISR, pState,
|
---|
485 | &pState->pfnIrqHandler);
|
---|
486 | #else
|
---|
487 | rc = bus_setup_intr(pDevice, pState->pIrqRes, INTR_TYPE_BIO, (driver_intr_t *)vgdrvFreeBSDISR, pState, &pState->pfnIrqHandler);
|
---|
488 | #endif
|
---|
489 |
|
---|
490 | if (rc)
|
---|
491 | {
|
---|
492 | pState->pfnIrqHandler = NULL;
|
---|
493 | return VERR_DEV_IO_ERROR;
|
---|
494 | }
|
---|
495 |
|
---|
496 | pState->iIrqResId = iResId;
|
---|
497 |
|
---|
498 | return VINF_SUCCESS;
|
---|
499 | }
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Removes IRQ for VMMDev.
|
---|
503 | *
|
---|
504 | * @param pDevice Pointer to the device info structure.
|
---|
505 | * @param pvState Opaque pointer to the state info structure.
|
---|
506 | */
|
---|
507 | static void vgdrvFreeBSDRemoveIRQ(device_t pDevice, void *pvState)
|
---|
508 | {
|
---|
509 | struct VBoxGuestDeviceState *pState = (struct VBoxGuestDeviceState *)pvState;
|
---|
510 |
|
---|
511 | if (pState->pIrqRes)
|
---|
512 | {
|
---|
513 | bus_teardown_intr(pDevice, pState->pIrqRes, pState->pfnIrqHandler);
|
---|
514 | bus_release_resource(pDevice, SYS_RES_IRQ, 0, pState->pIrqRes);
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | static int vgdrvFreeBSDAttach(device_t pDevice)
|
---|
519 | {
|
---|
520 | int rc;
|
---|
521 | int iResId;
|
---|
522 | struct VBoxGuestDeviceState *pState;
|
---|
523 |
|
---|
524 | cUsers = 0;
|
---|
525 |
|
---|
526 | /*
|
---|
527 | * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
|
---|
528 | */
|
---|
529 | rc = RTR0Init(0);
|
---|
530 | if (RT_FAILURE(rc))
|
---|
531 | {
|
---|
532 | LogFunc(("RTR0Init failed.\n"));
|
---|
533 | return ENXIO;
|
---|
534 | }
|
---|
535 |
|
---|
536 | pState = device_get_softc(pDevice);
|
---|
537 |
|
---|
538 | /*
|
---|
539 | * Allocate I/O port resource.
|
---|
540 | */
|
---|
541 | iResId = PCIR_BAR(0);
|
---|
542 | pState->pIOPortRes = bus_alloc_resource_any(pDevice, SYS_RES_IOPORT, &iResId, RF_ACTIVE);
|
---|
543 | pState->uIOPortBase = rman_get_start(pState->pIOPortRes);
|
---|
544 | pState->iIOPortResId = iResId;
|
---|
545 | if (pState->uIOPortBase)
|
---|
546 | {
|
---|
547 | /*
|
---|
548 | * Map the MMIO region.
|
---|
549 | */
|
---|
550 | iResId = PCIR_BAR(1);
|
---|
551 | pState->pVMMDevMemRes = bus_alloc_resource_any(pDevice, SYS_RES_MEMORY, &iResId, RF_ACTIVE);
|
---|
552 | pState->VMMDevMemHandle = rman_get_bushandle(pState->pVMMDevMemRes);
|
---|
553 | pState->VMMDevMemSize = rman_get_size(pState->pVMMDevMemRes);
|
---|
554 |
|
---|
555 | pState->pMMIOBase = rman_get_virtual(pState->pVMMDevMemRes);
|
---|
556 | pState->iVMMDevMemResId = iResId;
|
---|
557 | if (pState->pMMIOBase)
|
---|
558 | {
|
---|
559 | /*
|
---|
560 | * Call the common device extension initializer.
|
---|
561 | */
|
---|
562 | rc = VGDrvCommonInitDevExt(&g_DevExt, pState->uIOPortBase,
|
---|
563 | pState->pMMIOBase, pState->VMMDevMemSize,
|
---|
564 | #if ARCH_BITS == 64
|
---|
565 | VBOXOSTYPE_FreeBSD_x64,
|
---|
566 | #else
|
---|
567 | VBOXOSTYPE_FreeBSD,
|
---|
568 | #endif
|
---|
569 | VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
|
---|
570 | if (RT_SUCCESS(rc))
|
---|
571 | {
|
---|
572 | /*
|
---|
573 | * Add IRQ of VMMDev.
|
---|
574 | */
|
---|
575 | rc = vgdrvFreeBSDAddIRQ(pDevice, pState);
|
---|
576 | if (RT_SUCCESS(rc))
|
---|
577 | {
|
---|
578 | /*
|
---|
579 | * Configure device cloning.
|
---|
580 | */
|
---|
581 | clone_setup(&g_pvgdrvFreeBSDClones);
|
---|
582 | g_vgdrvFreeBSDEHTag = EVENTHANDLER_REGISTER(dev_clone, vgdrvFreeBSDClone, 0, 1000);
|
---|
583 | if (g_vgdrvFreeBSDEHTag)
|
---|
584 | {
|
---|
585 | printf(DEVICE_NAME ": loaded successfully\n");
|
---|
586 | return 0;
|
---|
587 | }
|
---|
588 |
|
---|
589 | printf(DEVICE_NAME ": EVENTHANDLER_REGISTER(dev_clone,,,) failed\n");
|
---|
590 | clone_cleanup(&g_pvgdrvFreeBSDClones);
|
---|
591 | vgdrvFreeBSDRemoveIRQ(pDevice, pState);
|
---|
592 | }
|
---|
593 | else
|
---|
594 | printf((DEVICE_NAME ": VGDrvCommonInitDevExt failed.\n"));
|
---|
595 | VGDrvCommonDeleteDevExt(&g_DevExt);
|
---|
596 | }
|
---|
597 | else
|
---|
598 | printf((DEVICE_NAME ": vgdrvFreeBSDAddIRQ failed.\n"));
|
---|
599 | }
|
---|
600 | else
|
---|
601 | printf((DEVICE_NAME ": MMIO region setup failed.\n"));
|
---|
602 | }
|
---|
603 | else
|
---|
604 | printf((DEVICE_NAME ": IOport setup failed.\n"));
|
---|
605 |
|
---|
606 | RTR0Term();
|
---|
607 | return ENXIO;
|
---|
608 | }
|
---|
609 |
|
---|
610 | static int vgdrvFreeBSDProbe(device_t pDevice)
|
---|
611 | {
|
---|
612 | if ((pci_get_vendor(pDevice) == VMMDEV_VENDORID) && (pci_get_device(pDevice) == VMMDEV_DEVICEID))
|
---|
613 | return 0;
|
---|
614 |
|
---|
615 | return ENXIO;
|
---|
616 | }
|
---|
617 |
|
---|
618 | static device_method_t vgdrvFreeBSDMethods[] =
|
---|
619 | {
|
---|
620 | /* Device interface. */
|
---|
621 | DEVMETHOD(device_probe, vgdrvFreeBSDProbe),
|
---|
622 | DEVMETHOD(device_attach, vgdrvFreeBSDAttach),
|
---|
623 | DEVMETHOD(device_detach, vgdrvFreeBSDDetach),
|
---|
624 | {0,0}
|
---|
625 | };
|
---|
626 |
|
---|
627 | static driver_t vgdrvFreeBSDDriver =
|
---|
628 | {
|
---|
629 | DEVICE_NAME,
|
---|
630 | vgdrvFreeBSDMethods,
|
---|
631 | sizeof(struct VBoxGuestDeviceState),
|
---|
632 | };
|
---|
633 |
|
---|
634 | static devclass_t vgdrvFreeBSDClass;
|
---|
635 |
|
---|
636 | DRIVER_MODULE(vboxguest, pci, vgdrvFreeBSDDriver, vgdrvFreeBSDClass, 0, 0);
|
---|
637 | MODULE_VERSION(vboxguest, 1);
|
---|
638 |
|
---|
639 | /* Common code that depend on g_DevExt. */
|
---|
640 | #include "VBoxGuestIDC-unix.c.h"
|
---|
641 |
|
---|