VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-netbsd.c@ 70048

Last change on this file since 70048 was 70048, checked in by vboxsync, 7 years ago

VBoxGuest-netbsd.c: give mem bus space members their usual names.
Retire otherwise unused pMMIOBase (the actuall address is obtained via
bus_space_vaddr).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.2 KB
Line 
1/* $Id: VBoxGuest-netbsd.c 70048 2017-12-09 01:57:49Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions Driver for NetBSD.
4 */
5
6/*
7 * Copyright (C) 2007-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/select.h>
34#include <sys/conf.h>
35#include <sys/kernel.h>
36#include <sys/kmem.h>
37#include <sys/module.h>
38#include <sys/device.h>
39#include <sys/bus.h>
40#include <sys/poll.h>
41#include <sys/proc.h>
42#include <sys/stat.h>
43#include <sys/selinfo.h>
44#include <sys/queue.h>
45#include <sys/lock.h>
46#include <sys/types.h>
47#include <sys/conf.h>
48#include <sys/malloc.h>
49#include <sys/uio.h>
50#include <sys/file.h>
51#include <sys/filedesc.h>
52#include <sys/vfs_syscalls.h>
53#include <dev/pci/pcivar.h>
54#include <dev/pci/pcireg.h>
55#include <dev/pci/pcidevs.h>
56
57#include <dev/wscons/wsconsio.h>
58#include <dev/wscons/wsmousevar.h>
59#include <dev/wscons/tpcalibvar.h>
60
61#ifdef PVM
62# undef PVM
63#endif
64#include "VBoxGuestInternal.h"
65#include <VBox/log.h>
66#include <iprt/assert.h>
67#include <iprt/initterm.h>
68#include <iprt/process.h>
69#include <iprt/mem.h>
70#include <iprt/asm.h>
71
72
73/*********************************************************************************************************************************
74* Defined Constants And Macros *
75*********************************************************************************************************************************/
76/** The module name. */
77#define DEVICE_NAME "vboxguest"
78
79
80/*********************************************************************************************************************************
81* Structures and Typedefs *
82*********************************************************************************************************************************/
83typedef struct VBoxGuestDeviceState
84{
85 device_t sc_dev;
86 pci_chipset_tag_t sc_pc;
87
88 bus_space_tag_t sc_iot;
89 bus_space_handle_t sc_ioh;
90 bus_addr_t sc_iobase;
91 bus_size_t sc_iosize;
92
93 bus_space_tag_t sc_memt;
94 bus_space_handle_t sc_memh;
95
96 /** Size of the memory area. */
97 bus_size_t sc_memsize;
98
99 /** IRQ resource handle. */
100 pci_intr_handle_t ih;
101 /** Pointer to the IRQ handler. */
102 void *pfnIrqHandler;
103
104 /** Controller features, limits and status. */
105 u_int vboxguest_state;
106
107 device_t sc_wsmousedev;
108 VMMDevReqMouseStatus *sc_vmmmousereq;
109 PVBOXGUESTSESSION sc_session;
110 struct tpcalib_softc sc_tpcalib;
111} vboxguest_softc;
112
113
114struct vboxguest_fdata
115{
116 vboxguest_softc *sc;
117 PVBOXGUESTSESSION session;
118};
119
120#define VBOXGUEST_STATE_INITOK 1 << 0
121
122
123/*********************************************************************************************************************************
124* Internal Functions *
125*********************************************************************************************************************************/
126/*
127 * Driver(9) autoconf machinery.
128 */
129static int VBoxGuestNetBSDMatch(device_t parent, cfdata_t match, void *aux);
130static void VBoxGuestNetBSDAttach(device_t parent, device_t self, void *aux);
131static void VBoxGuestNetBSDWsmAttach(vboxguest_softc *sc);
132static int VBoxGuestNetBSDDetach(device_t self, int flags);
133
134/*
135 * IRQ related functions.
136 */
137static int VBoxGuestNetBSDAddIRQ(vboxguest_softc *sc, struct pci_attach_args *pa);
138static void VBoxGuestNetBSDRemoveIRQ(vboxguest_softc *sc);
139static int VBoxGuestNetBSDISR(void *pvState);
140
141/*
142 * Character device file handlers.
143 */
144static int VBoxGuestNetBSDOpen(dev_t device, int flags, int fmt, struct lwp *process);
145static int VBoxGuestNetBSDClose(struct file *fp);
146static int VBoxGuestNetBSDIOCtl(struct file *fp, u_long cmd, void *addr);
147static int VBoxGuestNetBSDIOCtlSlow(struct vboxguest_fdata *fdata, u_long command, void *data);
148static int VBoxGuestNetBSDPoll(struct file *fp, int events);
149
150/*
151 * wsmouse(4) accessops
152 */
153static int VBoxGuestNetBSDWsmEnable(void *cookie);
154static void VBoxGuestNetBSDWsmDisable(void *cookie);
155static int VBoxGuestNetBSDWsmIOCtl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l);
156
157static int VBoxGuestNetBSDSetMouseStatus(vboxguest_softc *sc, uint32_t fStatus);
158
159
160/*********************************************************************************************************************************
161* Global Variables *
162*********************************************************************************************************************************/
163extern struct cfdriver vboxguest_cd; /* CFDRIVER_DECL */
164extern struct cfattach vboxguest_ca; /* CFATTACH_DECL */
165
166/*
167 * The /dev/vboxguest character device entry points.
168 */
169static struct cdevsw g_VBoxGuestNetBSDChrDevSW =
170{
171 VBoxGuestNetBSDOpen,
172 noclose,
173 noread,
174 nowrite,
175 noioctl,
176 nostop,
177 notty,
178 nopoll,
179 nommap,
180 nokqfilter,
181};
182
183static const struct fileops vboxguest_fileops = {
184 .fo_read = fbadop_read,
185 .fo_write = fbadop_write,
186 .fo_ioctl = VBoxGuestNetBSDIOCtl,
187 .fo_fcntl = fnullop_fcntl,
188 .fo_poll = VBoxGuestNetBSDPoll,
189 .fo_stat = fbadop_stat,
190 .fo_close = VBoxGuestNetBSDClose,
191 .fo_kqfilter = fnullop_kqfilter,
192 .fo_restart = fnullop_restart
193};
194
195
196const struct wsmouse_accessops vboxguest_wsm_accessops = {
197 VBoxGuestNetBSDWsmEnable,
198 VBoxGuestNetBSDWsmIOCtl,
199 VBoxGuestNetBSDWsmDisable,
200};
201
202
203static struct wsmouse_calibcoords vboxguest_wsm_default_calib = {
204 .minx = VMMDEV_MOUSE_RANGE_MIN,
205 .miny = VMMDEV_MOUSE_RANGE_MIN,
206 .maxx = VMMDEV_MOUSE_RANGE_MAX,
207 .maxy = VMMDEV_MOUSE_RANGE_MAX,
208 .samplelen = WSMOUSE_CALIBCOORDS_RESET,
209};
210
211/** Device extention & session data association structure. */
212static VBOXGUESTDEVEXT g_DevExt;
213
214static vboxguest_softc *g_SC;
215
216/** Reference counter */
217static volatile uint32_t cUsers;
218/** selinfo structure used for polling. */
219static struct selinfo g_SelInfo;
220
221
222CFATTACH_DECL_NEW(vboxguest, sizeof(vboxguest_softc),
223 VBoxGuestNetBSDMatch, VBoxGuestNetBSDAttach, VBoxGuestNetBSDDetach, NULL);
224
225
226static int VBoxGuestNetBSDMatch(device_t parent, cfdata_t match, void *aux)
227{
228 const struct pci_attach_args *pa = aux;
229
230 if (RT_UNLIKELY(g_SC != NULL)) /* should not happen */
231 return 0;
232
233 if ( PCI_VENDOR(pa->pa_id) == VMMDEV_VENDORID
234 && PCI_PRODUCT(pa->pa_id) == VMMDEV_DEVICEID)
235 {
236 return 1;
237 }
238
239 return 0;
240}
241
242
243static void VBoxGuestNetBSDAttach(device_t parent, device_t self, void *aux)
244{
245 int rc = VINF_SUCCESS;
246 int iResId = 0;
247 vboxguest_softc *sc;
248 struct pci_attach_args *pa = aux;
249 bus_space_tag_t iot, memt;
250 bus_space_handle_t ioh, memh;
251 bus_dma_segment_t seg;
252 int ioh_valid, memh_valid;
253
254 KASSERT(g_SC == NULL);
255
256 cUsers = 0;
257
258 aprint_normal(": VirtualBox Guest\n");
259
260 sc = device_private(self);
261 sc->sc_dev = self;
262
263 /*
264 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
265 */
266 rc = RTR0Init(0);
267 if (RT_FAILURE(rc))
268 {
269 LogFunc(("RTR0Init failed.\n"));
270 aprint_error_dev(sc->sc_dev, "RTR0Init failed\n");
271 return;
272 }
273
274 sc->sc_pc = pa->pa_pc;
275
276 /*
277 * Allocate I/O port resource.
278 */
279 ioh_valid = (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
280 &sc->sc_iot, &sc->sc_ioh,
281 &sc->sc_iobase, &sc->sc_iosize) == 0);
282
283 if (ioh_valid)
284 {
285
286 /*
287 * Map the MMIO region.
288 */
289 memh_valid = (pci_mapreg_map(pa, PCI_MAPREG_START+4, PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR,
290 &sc->sc_memt, &sc->sc_memh,
291 NULL, &sc->sc_memsize) == 0);
292 if (memh_valid)
293 {
294 /*
295 * Call the common device extension initializer.
296 */
297 rc = VGDrvCommonInitDevExt(&g_DevExt, sc->sc_iobase,
298 bus_space_vaddr(sc->sc_memt, sc->sc_memh),
299 sc->sc_memsize,
300#if ARCH_BITS == 64
301 VBOXOSTYPE_NetBSD_x64,
302#else
303 VBOXOSTYPE_NetBSD,
304#endif
305 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
306 if (RT_SUCCESS(rc))
307 {
308 /*
309 * Add IRQ of VMMDev.
310 */
311 rc = VBoxGuestNetBSDAddIRQ(sc, pa);
312 if (RT_SUCCESS(rc))
313 {
314 sc->vboxguest_state |= VBOXGUEST_STATE_INITOK;
315 VBoxGuestNetBSDWsmAttach(sc);
316
317 g_SC = sc;
318 return;
319 }
320 VGDrvCommonDeleteDevExt(&g_DevExt);
321 }
322 else
323 {
324 aprint_error_dev(sc->sc_dev, "init failed\n");
325 }
326 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
327 }
328 else
329 {
330 aprint_error_dev(sc->sc_dev, "MMIO mapping failed\n");
331 }
332 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
333 }
334 else
335 {
336 aprint_error_dev(sc->sc_dev, "IO mapping failed\n");
337 }
338
339 RTR0Term();
340 return;
341}
342
343
344/**
345 * Sets IRQ for VMMDev.
346 *
347 * @returns NetBSD error code.
348 * @param vboxguest Pointer to the state info structure.
349 * @param pa Pointer to the PCI attach arguments.
350 */
351static int VBoxGuestNetBSDAddIRQ(vboxguest_softc *sc, struct pci_attach_args *pa)
352{
353 int iResId = 0;
354 int rc = 0;
355 const char *intrstr;
356#if __NetBSD_Prereq__(6, 99, 39)
357 char intstrbuf[100];
358#endif
359
360 LogFlow((DEVICE_NAME ": %s\n", __func__));
361
362 if (pci_intr_map(pa, &sc->ih))
363 {
364 aprint_error_dev(sc->sc_dev, "couldn't map interrupt.\n");
365 return VERR_DEV_IO_ERROR;
366 }
367
368 intrstr = pci_intr_string(sc->sc_pc, sc->ih
369#if __NetBSD_Prereq__(6, 99, 39)
370 , intstrbuf, sizeof(intstrbuf)
371#endif
372 );
373 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
374
375 sc->pfnIrqHandler = pci_intr_establish(sc->sc_pc, sc->ih, IPL_BIO, VBoxGuestNetBSDISR, sc);
376 if (sc->pfnIrqHandler == NULL)
377 {
378 aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
379 return VERR_DEV_IO_ERROR;
380 }
381
382 return VINF_SUCCESS;
383}
384
385
386/*
387 * Optionally attach wsmouse(4) device as a child.
388 */
389static void VBoxGuestNetBSDWsmAttach(vboxguest_softc *sc)
390{
391 struct wsmousedev_attach_args am = { &vboxguest_wsm_accessops, sc };
392
393 PVBOXGUESTSESSION session = NULL;
394 VMMDevReqMouseStatus *req = NULL;
395 int rc;
396
397 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &session);
398 if (RT_FAILURE(rc))
399 goto fail;
400
401 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&req, sizeof(*req),
402 VMMDevReq_GetMouseStatus);
403 if (RT_FAILURE(rc))
404 goto fail;
405
406 sc->sc_wsmousedev = config_found_ia(sc->sc_dev, "wsmousedev", &am, wsmousedevprint);
407 if (sc->sc_wsmousedev == NULL)
408 goto fail;
409
410 sc->sc_session = session;
411 sc->sc_vmmmousereq = req;
412
413 tpcalib_init(&sc->sc_tpcalib);
414 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
415 &vboxguest_wsm_default_calib, 0, 0);
416 return;
417
418 fail:
419 if (session != NULL)
420 VGDrvCommonCloseSession(&g_DevExt, session);
421 if (req != NULL)
422 VbglR0GRFree((VMMDevRequestHeader *)req);
423}
424
425
426static int VBoxGuestNetBSDDetach(device_t self, int flags)
427{
428 vboxguest_softc *sc;
429 sc = device_private(self);
430
431 LogFlow((DEVICE_NAME ": %s\n", __func__));
432
433 if (cUsers > 0)
434 return EBUSY;
435
436 if ((sc->vboxguest_state & VBOXGUEST_STATE_INITOK) == 0)
437 return 0;
438
439 /*
440 * Reverse what we did in VBoxGuestNetBSDAttach.
441 */
442 if (sc->sc_vmmmousereq != NULL)
443 VbglR0GRFree((VMMDevRequestHeader *)sc->sc_vmmmousereq);
444
445 VBoxGuestNetBSDRemoveIRQ(sc);
446
447 VGDrvCommonDeleteDevExt(&g_DevExt);
448
449 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
450 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
451
452 RTR0Term();
453
454 return config_detach_children(self, flags);
455}
456
457
458/**
459 * Removes IRQ for VMMDev.
460 *
461 * @param vboxguest Opaque pointer to the state info structure.
462 */
463static void VBoxGuestNetBSDRemoveIRQ(vboxguest_softc *sc)
464{
465 LogFlow((DEVICE_NAME ": %s\n", __func__));
466
467 if (sc->pfnIrqHandler)
468 {
469 pci_intr_disestablish(sc->sc_pc, sc->pfnIrqHandler);
470 }
471}
472
473
474/**
475 * Interrupt service routine.
476 *
477 * @returns Whether the interrupt was from VMMDev.
478 * @param pvState Opaque pointer to the device state.
479 */
480static int VBoxGuestNetBSDISR(void *pvState)
481{
482 LogFlow((DEVICE_NAME ": %s: pvState=%p\n", __func__, pvState));
483
484 bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
485
486 return fOurIRQ ? 0 : 1;
487}
488
489
490/*
491 * Called by VGDrvCommonISR() if mouse position changed
492 */
493void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
494{
495 vboxguest_softc *sc = g_SC;
496
497 LogFlow((DEVICE_NAME ": %s\n", __func__));
498
499 /*
500 * Wake up poll waiters.
501 */
502 selnotify(&g_SelInfo, 0, 0);
503
504 if (sc->sc_vmmmousereq != NULL) {
505 int x, y;
506 int rc;
507
508 sc->sc_vmmmousereq->mouseFeatures = 0;
509 sc->sc_vmmmousereq->pointerXPos = 0;
510 sc->sc_vmmmousereq->pointerYPos = 0;
511
512 rc = VbglR0GRPerform(&sc->sc_vmmmousereq->header);
513 if (RT_FAILURE(rc))
514 return;
515
516 tpcalib_trans(&sc->sc_tpcalib,
517 sc->sc_vmmmousereq->pointerXPos,
518 sc->sc_vmmmousereq->pointerYPos,
519 &x, &y);
520
521 wsmouse_input(sc->sc_wsmousedev,
522 0, /* buttons */
523 x, y,
524 0, 0, /* z, w */
525 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
526 }
527}
528
529
530static int VBoxGuestNetBSDSetMouseStatus(vboxguest_softc *sc, uint32_t fStatus)
531{
532 VBGLIOCSETMOUSESTATUS Req;
533 int rc;
534
535 VBGLREQHDR_INIT(&Req.Hdr, SET_MOUSE_STATUS);
536 Req.u.In.fStatus = fStatus;
537 rc = VGDrvCommonIoCtl(VBGL_IOCTL_SET_MOUSE_STATUS,
538 &g_DevExt,
539 sc->sc_session,
540 &Req.Hdr, sizeof(Req));
541 if (RT_SUCCESS(rc))
542 rc = Req.Hdr.rc;
543
544 return rc;
545}
546
547
548static int
549VBoxGuestNetBSDWsmEnable(void *cookie)
550{
551 vboxguest_softc *sc = cookie;
552 int rc;
553
554 rc = VBoxGuestNetBSDSetMouseStatus(sc, VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
555 | VMMDEV_MOUSE_NEW_PROTOCOL);
556 if (RT_FAILURE(rc))
557 return RTErrConvertToErrno(rc);
558
559 return 0;
560}
561
562
563static void
564VBoxGuestNetBSDWsmDisable(void *cookie)
565{
566 vboxguest_softc *sc = cookie;
567 VBoxGuestNetBSDSetMouseStatus(sc, 0);
568}
569
570
571static int
572VBoxGuestNetBSDWsmIOCtl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
573{
574 vboxguest_softc *sc = cookie;
575
576 switch (cmd) {
577 case WSMOUSEIO_GTYPE:
578 *(u_int *)data = WSMOUSE_TYPE_TPANEL;
579 break;
580
581 case WSMOUSEIO_SCALIBCOORDS:
582 case WSMOUSEIO_GCALIBCOORDS:
583 return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
584
585 default:
586 return EPASSTHROUGH;
587 }
588 return 0;
589}
590
591
592/**
593 * File open handler
594 *
595 */
596static int VBoxGuestNetBSDOpen(dev_t device, int flags, int fmt, struct lwp *process)
597{
598 int rc;
599 vboxguest_softc *sc;
600 struct vboxguest_fdata *fdata;
601 file_t *fp;
602 int fd, error;
603
604 LogFlow((DEVICE_NAME ": %s\n", __func__));
605
606 if ((sc = device_lookup_private(&vboxguest_cd, minor(device))) == NULL)
607 {
608 printf("device_lookup_private failed\n");
609 return (ENXIO);
610 }
611
612 if ((sc->vboxguest_state & VBOXGUEST_STATE_INITOK) == 0)
613 {
614 aprint_error_dev(sc->sc_dev, "device not configured\n");
615 return (ENXIO);
616 }
617
618 fdata = kmem_alloc(sizeof(*fdata), KM_SLEEP);
619 if (fdata == NULL)
620 {
621 return (ENOMEM);
622 }
623
624 fdata->sc = sc;
625
626 if ((error = fd_allocfile(&fp, &fd)) != 0)
627 {
628 kmem_free(fdata, sizeof(*fdata));
629 return error;
630 }
631
632 /*
633 * Create a new session.
634 */
635 rc = VGDrvCommonCreateUserSession(&g_DevExt, &fdata->session);
636 if (! RT_SUCCESS(rc))
637 {
638 aprint_error_dev(sc->sc_dev, "VBox session creation failed\n");
639 closef(fp); /* ??? */
640 kmem_free(fdata, sizeof(*fdata));
641 return RTErrConvertToErrno(rc);
642 }
643 ASMAtomicIncU32(&cUsers);
644 return fd_clone(fp, fd, flags, &vboxguest_fileops, fdata);
645
646}
647
648/**
649 * File close handler
650 *
651 */
652static int VBoxGuestNetBSDClose(struct file *fp)
653{
654 struct vboxguest_fdata *fdata = fp->f_data;
655 vboxguest_softc *sc = fdata->sc;
656
657 LogFlow((DEVICE_NAME ": %s\n", __func__));
658
659 VGDrvCommonCloseSession(&g_DevExt, fdata->session);
660 ASMAtomicDecU32(&cUsers);
661
662 kmem_free(fdata, sizeof(*fdata));
663
664 return 0;
665}
666
667/**
668 * IOCTL handler
669 *
670 */
671static int VBoxGuestNetBSDIOCtl(struct file *fp, u_long command, void *data)
672{
673 struct vboxguest_fdata *fdata = fp->f_data;
674
675 if (VBGL_IOCTL_IS_FAST(command))
676 return VGDrvCommonIoCtlFast(command, &g_DevExt, fdata->session);
677
678 return VBoxGuestNetBSDIOCtlSlow(fdata, command, data);
679}
680
681static int VBoxGuestNetBSDIOCtlSlow(struct vboxguest_fdata *fdata, u_long command, void *data)
682{
683 vboxguest_softc *sc = fdata->sc;
684 size_t cbReq = IOCPARM_LEN(command);
685 PVBGLREQHDR pHdr = NULL;
686 void *pvUser = NULL;
687 int err, rc;
688
689 LogFlow(("%s: command=%#lx data=%p\n", __func__, command, data));
690
691 /*
692 * Buffered request?
693 */
694 if ((command & IOC_DIRMASK) == IOC_INOUT)
695 {
696 /* will be validated by VGDrvCommonIoCtl() */
697 pHdr = (PVBGLREQHDR)data;
698 }
699
700 /*
701 * Big unbuffered request? "data" is the userland pointer.
702 */
703 else if ((command & IOC_DIRMASK) == IOC_VOID && cbReq != 0)
704 {
705 /*
706 * Read the header, validate it and figure out how much that
707 * needs to be buffered.
708 */
709 VBGLREQHDR Hdr;
710
711 if (RT_UNLIKELY(cbReq < sizeof(Hdr)))
712 return ENOTTY;
713
714 pvUser = data;
715 err = copyin(pvUser, &Hdr, sizeof(Hdr));
716 if (RT_UNLIKELY(err != 0))
717 return err;
718
719 if (RT_UNLIKELY(Hdr.uVersion != VBGLREQHDR_VERSION))
720 return ENOTTY;
721
722 if (cbReq > 16 * _1M)
723 return EINVAL;
724
725 if (Hdr.cbOut == 0)
726 Hdr.cbOut = Hdr.cbIn;
727
728 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr) || Hdr.cbIn > cbReq
729 || Hdr.cbOut < sizeof(Hdr) || Hdr.cbOut > cbReq))
730 return EINVAL;
731
732 /*
733 * Allocate buffer and copy in the data.
734 */
735 cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
736
737 pHdr = (PVBGLREQHDR)RTMemTmpAlloc(cbReq);
738 if (RT_UNLIKELY(pHdr == NULL))
739 {
740 LogRel(("%s: command=%#lx data=%p: unable to allocate %zu bytes\n",
741 __func__, command, data, cbReq));
742 return ENOMEM;
743 }
744
745 err = copyin(pvUser, pHdr, Hdr.cbIn);
746 if (err != 0)
747 {
748 RTMemTmpFree(pHdr);
749 return err;
750 }
751
752 if (Hdr.cbIn < cbReq)
753 memset((uint8_t *)pHdr + Hdr.cbIn, '\0', cbReq - Hdr.cbIn);
754 }
755
756 /*
757 * Process the IOCtl.
758 */
759 rc = VGDrvCommonIoCtl(command, &g_DevExt, fdata->session, pHdr, cbReq);
760 if (RT_SUCCESS(rc))
761 {
762 err = 0;
763
764 /*
765 * If unbuffered, copy back the result before returning.
766 */
767 if (pvUser != NULL)
768 {
769 size_t cbOut = pHdr->cbOut;
770 if (cbOut > cbReq)
771 {
772 LogRel(("%s: command=%#lx data=%p: too much output: %zu > %zu\n",
773 __func__, command, data, cbOut, cbReq));
774 cbOut = cbReq;
775 }
776
777 err = copyout(pHdr, pvUser, cbOut);
778 RTMemTmpFree(pHdr);
779 }
780 }
781 else
782 {
783 LogRel(("%s: command=%#lx data=%p: error %Rrc\n",
784 __func__, command, data, rc));
785
786 if (pvUser != NULL)
787 RTMemTmpFree(pHdr);
788
789 err = RTErrConvertToErrno(rc);
790 }
791
792 return err;
793}
794
795static int VBoxGuestNetBSDPoll(struct file *fp, int events)
796{
797 struct vboxguest_fdata *fdata = fp->f_data;
798 vboxguest_softc *sc = fdata->sc;
799
800 int rc = 0;
801 int events_processed;
802
803 uint32_t u32CurSeq;
804
805 LogFlow((DEVICE_NAME ": %s\n", __func__));
806
807 u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
808 if (fdata->session->u32MousePosChangedSeq != u32CurSeq)
809 {
810 events_processed = events & (POLLIN | POLLRDNORM);
811 fdata->session->u32MousePosChangedSeq = u32CurSeq;
812 }
813 else
814 {
815 events_processed = 0;
816
817 selrecord(curlwp, &g_SelInfo);
818 }
819
820 return events_processed;
821}
822
823
824/**
825 * @note This code is duplicated on other platforms with variations, so please
826 * keep them all up to date when making changes!
827 */
828int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
829{
830 /*
831 * Simple request validation (common code does the rest).
832 */
833 int rc;
834 if ( RT_VALID_PTR(pReqHdr)
835 && cbReq >= sizeof(*pReqHdr))
836 {
837 /*
838 * All requests except the connect one requires a valid session.
839 */
840 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
841 if (pSession)
842 {
843 if ( RT_VALID_PTR(pSession)
844 && pSession->pDevExt == &g_DevExt)
845 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
846 else
847 rc = VERR_INVALID_HANDLE;
848 }
849 else if (uReq == VBGL_IOCTL_IDC_CONNECT)
850 {
851 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
852 if (RT_SUCCESS(rc))
853 {
854 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
855 if (RT_FAILURE(rc))
856 VGDrvCommonCloseSession(&g_DevExt, pSession);
857 }
858 }
859 else
860 rc = VERR_INVALID_HANDLE;
861 }
862 else
863 rc = VERR_INVALID_POINTER;
864 return rc;
865}
866
867
868MODULE(MODULE_CLASS_DRIVER, vboxguest, "pci");
869
870static const struct cfiattrdata wsmousedevcf_iattrdata = {
871 "wsmousedev", 1, {
872 { "mux", "0", 0 },
873 }
874};
875
876/* device vboxguest: wsmousedev */
877static const struct cfiattrdata * const vboxguest_attrs[] = { &wsmousedevcf_iattrdata, NULL };
878CFDRIVER_DECL(vboxguest, DV_DULL, vboxguest_attrs);
879
880static struct cfdriver * const cfdriver_ioconf_vboxguest[] = {
881 &vboxguest_cd, NULL
882};
883
884
885static const struct cfparent vboxguest_pspec = {
886 "pci", "pci", DVUNIT_ANY
887};
888static int vboxguest_loc[] = { -1, -1 };
889
890
891static const struct cfparent wsmousedev_pspec = {
892 "wsmousedev", "vboxguest", DVUNIT_ANY
893};
894static int wsmousedev_loc[] = { 0 };
895
896
897static struct cfdata cfdata_ioconf_vboxguest[] = {
898 /* vboxguest0 at pci? dev ? function ? */
899 {
900 .cf_name = "vboxguest",
901 .cf_atname = "vboxguest",
902 .cf_unit = 0, /* Only unit 0 is ever used */
903 .cf_fstate = FSTATE_NOTFOUND,
904 .cf_loc = vboxguest_loc,
905 .cf_flags = 0,
906 .cf_pspec = &vboxguest_pspec,
907 },
908
909 /* wsmouse* at vboxguest? */
910 { "wsmouse", "wsmouse", 0, FSTATE_STAR, wsmousedev_loc, 0, &wsmousedev_pspec },
911
912 { NULL, NULL, 0, 0, NULL, 0, NULL }
913};
914
915static struct cfattach * const vboxguest_cfattachinit[] = {
916 &vboxguest_ca, NULL
917};
918
919static const struct cfattachinit cfattach_ioconf_vboxguest[] = {
920 { "vboxguest", vboxguest_cfattachinit },
921 { NULL, NULL }
922};
923
924static int
925vboxguest_modcmd(modcmd_t cmd, void *opaque)
926{
927 devmajor_t bmajor, cmajor;
928 register_t retval;
929 int error;
930
931 LogFlow((DEVICE_NAME ": %s\n", __func__));
932
933 switch (cmd)
934 {
935 case MODULE_CMD_INIT:
936 error = config_init_component(cfdriver_ioconf_vboxguest,
937 cfattach_ioconf_vboxguest,
938 cfdata_ioconf_vboxguest);
939 if (error)
940 break;
941
942 bmajor = cmajor = NODEVMAJOR;
943 error = devsw_attach("vboxguest",
944 NULL, &bmajor,
945 &g_VBoxGuestNetBSDChrDevSW, &cmajor);
946 if (error)
947 {
948 if (error == EEXIST)
949 error = 0; /* maybe built-in ... improve eventually */
950 else
951 break;
952 }
953
954 error = do_sys_mknod(curlwp, "/dev/vboxguest",
955 0666|S_IFCHR, makedev(cmajor, 0),
956 &retval, UIO_SYSSPACE);
957 if (error == EEXIST)
958 error = 0;
959 break;
960
961 case MODULE_CMD_FINI:
962 error = config_fini_component(cfdriver_ioconf_vboxguest,
963 cfattach_ioconf_vboxguest,
964 cfdata_ioconf_vboxguest);
965 if (error)
966 break;
967
968 devsw_detach(NULL, &g_VBoxGuestNetBSDChrDevSW);
969 break;
970
971 default:
972 return ENOTTY;
973 }
974 return error;
975}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette