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