VirtualBox

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

Last change on this file since 78359 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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

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