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