VirtualBox

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

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

VBoxGuest: Enabled VGDrvCommonProcessOptionsFromHost for all but linux and freebsd.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.4 KB
Line 
1/* $Id: VBoxGuest-netbsd.c 70066 2017-12-11 16:33:05Z 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_BAR0,
280 PCI_MAPREG_TYPE_IO, 0,
281 &sc->sc_iot, &sc->sc_ioh,
282 &sc->sc_iobase, &sc->sc_iosize) == 0);
283
284 if (ioh_valid)
285 {
286
287 /*
288 * Map the MMIO region.
289 */
290 memh_valid = (pci_mapreg_map(pa, PCI_BAR1,
291 PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR,
292 &sc->sc_memt, &sc->sc_memh,
293 NULL, &sc->sc_memsize) == 0);
294 if (memh_valid)
295 {
296 /*
297 * Call the common device extension initializer.
298 */
299 rc = VGDrvCommonInitDevExt(&g_DevExt, sc->sc_iobase,
300 bus_space_vaddr(sc->sc_memt, sc->sc_memh),
301 sc->sc_memsize,
302#if ARCH_BITS == 64
303 VBOXOSTYPE_NetBSD_x64,
304#else
305 VBOXOSTYPE_NetBSD,
306#endif
307 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
308 if (RT_SUCCESS(rc))
309 {
310 /*
311 * Add IRQ of VMMDev.
312 */
313 rc = VBoxGuestNetBSDAddIRQ(sc, pa);
314 if (RT_SUCCESS(rc))
315 {
316 sc->vboxguest_state |= VBOXGUEST_STATE_INITOK;
317 VBoxGuestNetBSDWsmAttach(sc);
318
319 g_SC = sc;
320 return;
321 }
322 VGDrvCommonDeleteDevExt(&g_DevExt);
323 }
324 else
325 {
326 aprint_error_dev(sc->sc_dev, "init failed\n");
327 }
328 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
329 }
330 else
331 {
332 aprint_error_dev(sc->sc_dev, "MMIO mapping failed\n");
333 }
334 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
335 }
336 else
337 {
338 aprint_error_dev(sc->sc_dev, "IO mapping failed\n");
339 }
340
341 RTR0Term();
342 return;
343}
344
345
346/**
347 * Sets IRQ for VMMDev.
348 *
349 * @returns NetBSD error code.
350 * @param sc Pointer to the state info structure.
351 * @param pa Pointer to the PCI attach arguments.
352 */
353static int VBoxGuestNetBSDAddIRQ(vboxguest_softc *sc, struct pci_attach_args *pa)
354{
355 int iResId = 0;
356 int rc = 0;
357 const char *intrstr;
358#if __NetBSD_Prereq__(6, 99, 39)
359 char intstrbuf[100];
360#endif
361
362 LogFlow((DEVICE_NAME ": %s\n", __func__));
363
364 if (pci_intr_map(pa, &sc->ih))
365 {
366 aprint_error_dev(sc->sc_dev, "couldn't map interrupt.\n");
367 return VERR_DEV_IO_ERROR;
368 }
369
370 intrstr = pci_intr_string(sc->sc_pc, sc->ih
371#if __NetBSD_Prereq__(6, 99, 39)
372 , intstrbuf, sizeof(intstrbuf)
373#endif
374 );
375 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
376
377 sc->pfnIrqHandler = pci_intr_establish(sc->sc_pc, sc->ih, IPL_BIO, VBoxGuestNetBSDISR, sc);
378 if (sc->pfnIrqHandler == NULL)
379 {
380 aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
381 return VERR_DEV_IO_ERROR;
382 }
383
384 return VINF_SUCCESS;
385}
386
387
388/*
389 * Optionally attach wsmouse(4) device as a child.
390 */
391static void VBoxGuestNetBSDWsmAttach(vboxguest_softc *sc)
392{
393 struct wsmousedev_attach_args am = { &vboxguest_wsm_accessops, sc };
394
395 PVBOXGUESTSESSION session = NULL;
396 VMMDevReqMouseStatus *req = NULL;
397 int rc;
398
399 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &session);
400 if (RT_FAILURE(rc))
401 goto fail;
402
403 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&req, sizeof(*req),
404 VMMDevReq_GetMouseStatus);
405 if (RT_FAILURE(rc))
406 goto fail;
407
408 sc->sc_wsmousedev = config_found_ia(sc->sc_dev, "wsmousedev", &am, wsmousedevprint);
409 if (sc->sc_wsmousedev == NULL)
410 goto fail;
411
412 sc->sc_session = session;
413 sc->sc_vmmmousereq = req;
414
415 tpcalib_init(&sc->sc_tpcalib);
416 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
417 &vboxguest_wsm_default_calib, 0, 0);
418 return;
419
420 fail:
421 if (session != NULL)
422 VGDrvCommonCloseSession(&g_DevExt, session);
423 if (req != NULL)
424 VbglR0GRFree((VMMDevRequestHeader *)req);
425}
426
427
428static int VBoxGuestNetBSDDetach(device_t self, int flags)
429{
430 vboxguest_softc *sc;
431 sc = device_private(self);
432
433 LogFlow((DEVICE_NAME ": %s\n", __func__));
434
435 if (cUsers > 0)
436 return EBUSY;
437
438 if ((sc->vboxguest_state & VBOXGUEST_STATE_INITOK) == 0)
439 return 0;
440
441 /*
442 * Reverse what we did in VBoxGuestNetBSDAttach.
443 */
444 if (sc->sc_vmmmousereq != NULL)
445 VbglR0GRFree((VMMDevRequestHeader *)sc->sc_vmmmousereq);
446
447 VBoxGuestNetBSDRemoveIRQ(sc);
448
449 VGDrvCommonDeleteDevExt(&g_DevExt);
450
451 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
452 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
453
454 RTR0Term();
455
456 return config_detach_children(self, flags);
457}
458
459
460/**
461 * Removes IRQ for VMMDev.
462 *
463 * @param vboxguest Opaque pointer to the state info structure.
464 */
465static void VBoxGuestNetBSDRemoveIRQ(vboxguest_softc *sc)
466{
467 LogFlow((DEVICE_NAME ": %s\n", __func__));
468
469 if (sc->pfnIrqHandler)
470 {
471 pci_intr_disestablish(sc->sc_pc, sc->pfnIrqHandler);
472 }
473}
474
475
476/**
477 * Interrupt service routine.
478 *
479 * @returns Whether the interrupt was from VMMDev.
480 * @param pvState Opaque pointer to the device state.
481 */
482static int VBoxGuestNetBSDISR(void *pvState)
483{
484 LogFlow((DEVICE_NAME ": %s: pvState=%p\n", __func__, pvState));
485
486 bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
487
488 return fOurIRQ ? 1 : 0;
489}
490
491
492/*
493 * Called by VGDrvCommonISR() if mouse position changed
494 */
495void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
496{
497 vboxguest_softc *sc = g_SC;
498
499 LogFlow((DEVICE_NAME ": %s\n", __func__));
500
501 /*
502 * Wake up poll waiters.
503 */
504 selnotify(&g_SelInfo, 0, 0);
505
506 if (sc->sc_vmmmousereq != NULL) {
507 int x, y;
508 int rc;
509
510 sc->sc_vmmmousereq->mouseFeatures = 0;
511 sc->sc_vmmmousereq->pointerXPos = 0;
512 sc->sc_vmmmousereq->pointerYPos = 0;
513
514 rc = VbglR0GRPerform(&sc->sc_vmmmousereq->header);
515 if (RT_FAILURE(rc))
516 return;
517
518 tpcalib_trans(&sc->sc_tpcalib,
519 sc->sc_vmmmousereq->pointerXPos,
520 sc->sc_vmmmousereq->pointerYPos,
521 &x, &y);
522
523 wsmouse_input(sc->sc_wsmousedev,
524 0, /* buttons */
525 x, y,
526 0, 0, /* z, w */
527 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
528 }
529}
530
531
532bool VGDrvNativeProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue)
533{
534 RT_NOREF(pDevExt); RT_NOREF(pszName); RT_NOREF(pszValue);
535 return false;
536}
537
538
539static int VBoxGuestNetBSDSetMouseStatus(vboxguest_softc *sc, uint32_t fStatus)
540{
541 VBGLIOCSETMOUSESTATUS Req;
542 int rc;
543
544 VBGLREQHDR_INIT(&Req.Hdr, SET_MOUSE_STATUS);
545 Req.u.In.fStatus = fStatus;
546 rc = VGDrvCommonIoCtl(VBGL_IOCTL_SET_MOUSE_STATUS,
547 &g_DevExt,
548 sc->sc_session,
549 &Req.Hdr, sizeof(Req));
550 if (RT_SUCCESS(rc))
551 rc = Req.Hdr.rc;
552
553 return rc;
554}
555
556
557static int
558VBoxGuestNetBSDWsmEnable(void *cookie)
559{
560 vboxguest_softc *sc = cookie;
561 int rc;
562
563 rc = VBoxGuestNetBSDSetMouseStatus(sc, VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
564 | VMMDEV_MOUSE_NEW_PROTOCOL);
565 if (RT_FAILURE(rc))
566 return RTErrConvertToErrno(rc);
567
568 return 0;
569}
570
571
572static void
573VBoxGuestNetBSDWsmDisable(void *cookie)
574{
575 vboxguest_softc *sc = cookie;
576 VBoxGuestNetBSDSetMouseStatus(sc, 0);
577}
578
579
580static int
581VBoxGuestNetBSDWsmIOCtl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
582{
583 vboxguest_softc *sc = cookie;
584
585 switch (cmd) {
586 case WSMOUSEIO_GTYPE:
587 *(u_int *)data = WSMOUSE_TYPE_TPANEL;
588 break;
589
590 case WSMOUSEIO_SCALIBCOORDS:
591 case WSMOUSEIO_GCALIBCOORDS:
592 return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
593
594 default:
595 return EPASSTHROUGH;
596 }
597 return 0;
598}
599
600
601/**
602 * File open handler
603 *
604 */
605static int VBoxGuestNetBSDOpen(dev_t device, int flags, int fmt, struct lwp *process)
606{
607 int rc;
608 vboxguest_softc *sc;
609 struct vboxguest_fdata *fdata;
610 file_t *fp;
611 int fd, error;
612
613 LogFlow((DEVICE_NAME ": %s\n", __func__));
614
615 if ((sc = device_lookup_private(&vboxguest_cd, minor(device))) == NULL)
616 {
617 printf("device_lookup_private failed\n");
618 return (ENXIO);
619 }
620
621 if ((sc->vboxguest_state & VBOXGUEST_STATE_INITOK) == 0)
622 {
623 aprint_error_dev(sc->sc_dev, "device not configured\n");
624 return (ENXIO);
625 }
626
627 fdata = kmem_alloc(sizeof(*fdata), KM_SLEEP);
628 if (fdata == NULL)
629 {
630 return (ENOMEM);
631 }
632
633 fdata->sc = sc;
634
635 if ((error = fd_allocfile(&fp, &fd)) != 0)
636 {
637 kmem_free(fdata, sizeof(*fdata));
638 return error;
639 }
640
641 /*
642 * Create a new session.
643 */
644 rc = VGDrvCommonCreateUserSession(&g_DevExt, &fdata->session);
645 if (! RT_SUCCESS(rc))
646 {
647 aprint_error_dev(sc->sc_dev, "VBox session creation failed\n");
648 closef(fp); /* ??? */
649 kmem_free(fdata, sizeof(*fdata));
650 return RTErrConvertToErrno(rc);
651 }
652 ASMAtomicIncU32(&cUsers);
653 return fd_clone(fp, fd, flags, &vboxguest_fileops, fdata);
654
655}
656
657/**
658 * File close handler
659 *
660 */
661static int VBoxGuestNetBSDClose(struct file *fp)
662{
663 struct vboxguest_fdata *fdata = fp->f_data;
664 vboxguest_softc *sc = fdata->sc;
665
666 LogFlow((DEVICE_NAME ": %s\n", __func__));
667
668 VGDrvCommonCloseSession(&g_DevExt, fdata->session);
669 ASMAtomicDecU32(&cUsers);
670
671 kmem_free(fdata, sizeof(*fdata));
672
673 return 0;
674}
675
676/**
677 * IOCTL handler
678 *
679 */
680static int VBoxGuestNetBSDIOCtl(struct file *fp, u_long command, void *data)
681{
682 struct vboxguest_fdata *fdata = fp->f_data;
683
684 if (VBGL_IOCTL_IS_FAST(command))
685 return VGDrvCommonIoCtlFast(command, &g_DevExt, fdata->session);
686
687 return VBoxGuestNetBSDIOCtlSlow(fdata, command, data);
688}
689
690static int VBoxGuestNetBSDIOCtlSlow(struct vboxguest_fdata *fdata, u_long command, void *data)
691{
692 vboxguest_softc *sc = fdata->sc;
693 size_t cbReq = IOCPARM_LEN(command);
694 PVBGLREQHDR pHdr = NULL;
695 void *pvUser = NULL;
696 int err, rc;
697
698 LogFlow(("%s: command=%#lx data=%p\n", __func__, command, data));
699
700 /*
701 * Buffered request?
702 */
703 if ((command & IOC_DIRMASK) == IOC_INOUT)
704 {
705 /* will be validated by VGDrvCommonIoCtl() */
706 pHdr = (PVBGLREQHDR)data;
707 }
708
709 /*
710 * Big unbuffered request? "data" is the userland pointer.
711 */
712 else if ((command & IOC_DIRMASK) == IOC_VOID && cbReq != 0)
713 {
714 /*
715 * Read the header, validate it and figure out how much that
716 * needs to be buffered.
717 */
718 VBGLREQHDR Hdr;
719
720 if (RT_UNLIKELY(cbReq < sizeof(Hdr)))
721 return ENOTTY;
722
723 pvUser = data;
724 err = copyin(pvUser, &Hdr, sizeof(Hdr));
725 if (RT_UNLIKELY(err != 0))
726 return err;
727
728 if (RT_UNLIKELY(Hdr.uVersion != VBGLREQHDR_VERSION))
729 return ENOTTY;
730
731 if (cbReq > 16 * _1M)
732 return EINVAL;
733
734 if (Hdr.cbOut == 0)
735 Hdr.cbOut = Hdr.cbIn;
736
737 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr) || Hdr.cbIn > cbReq
738 || Hdr.cbOut < sizeof(Hdr) || Hdr.cbOut > cbReq))
739 return EINVAL;
740
741 /*
742 * Allocate buffer and copy in the data.
743 */
744 cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
745
746 pHdr = (PVBGLREQHDR)RTMemTmpAlloc(cbReq);
747 if (RT_UNLIKELY(pHdr == NULL))
748 {
749 LogRel(("%s: command=%#lx data=%p: unable to allocate %zu bytes\n",
750 __func__, command, data, cbReq));
751 return ENOMEM;
752 }
753
754 err = copyin(pvUser, pHdr, Hdr.cbIn);
755 if (err != 0)
756 {
757 RTMemTmpFree(pHdr);
758 return err;
759 }
760
761 if (Hdr.cbIn < cbReq)
762 memset((uint8_t *)pHdr + Hdr.cbIn, '\0', cbReq - Hdr.cbIn);
763 }
764
765 /*
766 * Process the IOCtl.
767 */
768 rc = VGDrvCommonIoCtl(command, &g_DevExt, fdata->session, pHdr, cbReq);
769 if (RT_SUCCESS(rc))
770 {
771 err = 0;
772
773 /*
774 * If unbuffered, copy back the result before returning.
775 */
776 if (pvUser != NULL)
777 {
778 size_t cbOut = pHdr->cbOut;
779 if (cbOut > cbReq)
780 {
781 LogRel(("%s: command=%#lx data=%p: too much output: %zu > %zu\n",
782 __func__, command, data, cbOut, cbReq));
783 cbOut = cbReq;
784 }
785
786 err = copyout(pHdr, pvUser, cbOut);
787 RTMemTmpFree(pHdr);
788 }
789 }
790 else
791 {
792 LogRel(("%s: command=%#lx data=%p: error %Rrc\n",
793 __func__, command, data, rc));
794
795 if (pvUser != NULL)
796 RTMemTmpFree(pHdr);
797
798 err = RTErrConvertToErrno(rc);
799 }
800
801 return err;
802}
803
804static int VBoxGuestNetBSDPoll(struct file *fp, int events)
805{
806 struct vboxguest_fdata *fdata = fp->f_data;
807 vboxguest_softc *sc = fdata->sc;
808
809 int rc = 0;
810 int events_processed;
811
812 uint32_t u32CurSeq;
813
814 LogFlow((DEVICE_NAME ": %s\n", __func__));
815
816 u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
817 if (fdata->session->u32MousePosChangedSeq != u32CurSeq)
818 {
819 events_processed = events & (POLLIN | POLLRDNORM);
820 fdata->session->u32MousePosChangedSeq = u32CurSeq;
821 }
822 else
823 {
824 events_processed = 0;
825
826 selrecord(curlwp, &g_SelInfo);
827 }
828
829 return events_processed;
830}
831
832
833/**
834 * @note This code is duplicated on other platforms with variations, so please
835 * keep them all up to date when making changes!
836 */
837int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
838{
839 /*
840 * Simple request validation (common code does the rest).
841 */
842 int rc;
843 if ( RT_VALID_PTR(pReqHdr)
844 && cbReq >= sizeof(*pReqHdr))
845 {
846 /*
847 * All requests except the connect one requires a valid session.
848 */
849 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
850 if (pSession)
851 {
852 if ( RT_VALID_PTR(pSession)
853 && pSession->pDevExt == &g_DevExt)
854 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
855 else
856 rc = VERR_INVALID_HANDLE;
857 }
858 else if (uReq == VBGL_IOCTL_IDC_CONNECT)
859 {
860 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
861 if (RT_SUCCESS(rc))
862 {
863 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
864 if (RT_FAILURE(rc))
865 VGDrvCommonCloseSession(&g_DevExt, pSession);
866 }
867 }
868 else
869 rc = VERR_INVALID_HANDLE;
870 }
871 else
872 rc = VERR_INVALID_POINTER;
873 return rc;
874}
875
876
877MODULE(MODULE_CLASS_DRIVER, vboxguest, "pci");
878
879static const struct cfiattrdata wsmousedevcf_iattrdata = {
880 "wsmousedev", 1, {
881 { "mux", "0", 0 },
882 }
883};
884
885/* device vboxguest: wsmousedev */
886static const struct cfiattrdata * const vboxguest_attrs[] = { &wsmousedevcf_iattrdata, NULL };
887CFDRIVER_DECL(vboxguest, DV_DULL, vboxguest_attrs);
888
889static struct cfdriver * const cfdriver_ioconf_vboxguest[] = {
890 &vboxguest_cd, NULL
891};
892
893
894static const struct cfparent vboxguest_pspec = {
895 "pci", "pci", DVUNIT_ANY
896};
897static int vboxguest_loc[] = { -1, -1 };
898
899
900static const struct cfparent wsmousedev_pspec = {
901 "wsmousedev", "vboxguest", DVUNIT_ANY
902};
903static int wsmousedev_loc[] = { 0 };
904
905
906static struct cfdata cfdata_ioconf_vboxguest[] = {
907 /* vboxguest0 at pci? dev ? function ? */
908 {
909 .cf_name = "vboxguest",
910 .cf_atname = "vboxguest",
911 .cf_unit = 0, /* Only unit 0 is ever used */
912 .cf_fstate = FSTATE_NOTFOUND,
913 .cf_loc = vboxguest_loc,
914 .cf_flags = 0,
915 .cf_pspec = &vboxguest_pspec,
916 },
917
918 /* wsmouse* at vboxguest? */
919 { "wsmouse", "wsmouse", 0, FSTATE_STAR, wsmousedev_loc, 0, &wsmousedev_pspec },
920
921 { NULL, NULL, 0, 0, NULL, 0, NULL }
922};
923
924static struct cfattach * const vboxguest_cfattachinit[] = {
925 &vboxguest_ca, NULL
926};
927
928static const struct cfattachinit cfattach_ioconf_vboxguest[] = {
929 { "vboxguest", vboxguest_cfattachinit },
930 { NULL, NULL }
931};
932
933static int
934vboxguest_modcmd(modcmd_t cmd, void *opaque)
935{
936 devmajor_t bmajor, cmajor;
937 register_t retval;
938 int error;
939
940 LogFlow((DEVICE_NAME ": %s\n", __func__));
941
942 switch (cmd)
943 {
944 case MODULE_CMD_INIT:
945 error = config_init_component(cfdriver_ioconf_vboxguest,
946 cfattach_ioconf_vboxguest,
947 cfdata_ioconf_vboxguest);
948 if (error)
949 break;
950
951 bmajor = cmajor = NODEVMAJOR;
952 error = devsw_attach("vboxguest",
953 NULL, &bmajor,
954 &g_VBoxGuestNetBSDChrDevSW, &cmajor);
955 if (error)
956 {
957 if (error == EEXIST)
958 error = 0; /* maybe built-in ... improve eventually */
959 else
960 break;
961 }
962
963 error = do_sys_mknod(curlwp, "/dev/vboxguest",
964 0666|S_IFCHR, makedev(cmajor, 0),
965 &retval, UIO_SYSSPACE);
966 if (error == EEXIST)
967 error = 0;
968 break;
969
970 case MODULE_CMD_FINI:
971 error = config_fini_component(cfdriver_ioconf_vboxguest,
972 cfattach_ioconf_vboxguest,
973 cfdata_ioconf_vboxguest);
974 if (error)
975 break;
976
977 devsw_detach(NULL, &g_VBoxGuestNetBSDChrDevSW);
978 break;
979
980 default:
981 return ENOTTY;
982 }
983 return error;
984}
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