VirtualBox

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

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

VBoxGuest-netbsd.c: Explicitly include <iprt/err.h>, don't rely on
other IPRT headers to pull it in (they no longer do).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.7 KB
Line 
1/* $Id: VBoxGuest-netbsd.c 76445 2018-12-24 16:01:51Z 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/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 int rc;
649 struct kauth_cred *pCred = pLwp->l_cred;
650 int fIsWheel;
651 uint32_t fRequestor = VMMDEV_REQUESTOR_USERMODE | VMMDEV_REQUESTOR_TRUST_NOT_GIVEN;
652 if (pCred && kauth_cred_geteuid(pCred) == 0)
653 fRequestor |= VMMDEV_REQUESTOR_USR_ROOT;
654 else
655 fRequestor |= VMMDEV_REQUESTOR_USR_USER;
656
657 if (pCred && kauth_cred_ismember_gid(pCred, 0, &fIsWheel) == 0 && fIsWheel)
658 fRequestor |= VMMDEV_REQUESTOR_GRP_WHEEL;
659 fRequestor |= VMMDEV_REQUESTOR_NO_USER_DEVICE; /** @todo implement /dev/vboxuser
660 if (!fUnrestricted)
661 fRequestor |= VMMDEV_REQUESTOR_USER_DEVICE; */
662 fRequestor |= VMMDEV_REQUESTOR_CON_DONT_KNOW; /** @todo can we find out if pLwp is on the console? */
663 rc = VGDrvCommonCreateUserSession(&g_DevExt, fRequestor, &fdata->session);
664 if (RT_SUCCESS(rc))
665 {
666 ASMAtomicIncU32(&cUsers);
667 return fd_clone(fp, fd, flags, &vboxguest_fileops, fdata);
668 }
669
670 aprint_error_dev(sc->sc_dev, "VBox session creation failed\n");
671 closef(fp); /* ??? */
672 error = RTErrConvertToErrno(rc);
673 }
674 kmem_free(fdata, sizeof(*fdata));
675 }
676 else
677 error = ENOMEM;
678 return error;
679
680}
681
682/**
683 * File close handler
684 *
685 */
686static int VBoxGuestNetBSDClose(struct file *fp)
687{
688 struct vboxguest_fdata *fdata = fp->f_data;
689 vboxguest_softc *sc = fdata->sc;
690
691 LogFlow((DEVICE_NAME ": %s\n", __func__));
692
693 VGDrvCommonCloseSession(&g_DevExt, fdata->session);
694 ASMAtomicDecU32(&cUsers);
695
696 kmem_free(fdata, sizeof(*fdata));
697
698 return 0;
699}
700
701/**
702 * IOCTL handler
703 *
704 */
705static int VBoxGuestNetBSDIOCtl(struct file *fp, u_long command, void *data)
706{
707 struct vboxguest_fdata *fdata = fp->f_data;
708
709 if (VBGL_IOCTL_IS_FAST(command))
710 return VGDrvCommonIoCtlFast(command, &g_DevExt, fdata->session);
711
712 return VBoxGuestNetBSDIOCtlSlow(fdata, command, data);
713}
714
715static int VBoxGuestNetBSDIOCtlSlow(struct vboxguest_fdata *fdata, u_long command, void *data)
716{
717 vboxguest_softc *sc = fdata->sc;
718 size_t cbReq = IOCPARM_LEN(command);
719 PVBGLREQHDR pHdr = NULL;
720 void *pvUser = NULL;
721 int err, rc;
722
723 LogFlow(("%s: command=%#lx data=%p\n", __func__, command, data));
724
725 /*
726 * Buffered request?
727 */
728 if ((command & IOC_DIRMASK) == IOC_INOUT)
729 {
730 /* will be validated by VGDrvCommonIoCtl() */
731 pHdr = (PVBGLREQHDR)data;
732 }
733
734 /*
735 * Big unbuffered request? "data" is the userland pointer.
736 */
737 else if ((command & IOC_DIRMASK) == IOC_VOID && cbReq != 0)
738 {
739 /*
740 * Read the header, validate it and figure out how much that
741 * needs to be buffered.
742 */
743 VBGLREQHDR Hdr;
744
745 if (RT_UNLIKELY(cbReq < sizeof(Hdr)))
746 return ENOTTY;
747
748 pvUser = data;
749 err = copyin(pvUser, &Hdr, sizeof(Hdr));
750 if (RT_UNLIKELY(err != 0))
751 return err;
752
753 if (RT_UNLIKELY(Hdr.uVersion != VBGLREQHDR_VERSION))
754 return ENOTTY;
755
756 if (cbReq > 16 * _1M)
757 return EINVAL;
758
759 if (Hdr.cbOut == 0)
760 Hdr.cbOut = Hdr.cbIn;
761
762 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr) || Hdr.cbIn > cbReq
763 || Hdr.cbOut < sizeof(Hdr) || Hdr.cbOut > cbReq))
764 return EINVAL;
765
766 /*
767 * Allocate buffer and copy in the data.
768 */
769 cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
770
771 pHdr = (PVBGLREQHDR)RTMemTmpAlloc(cbReq);
772 if (RT_UNLIKELY(pHdr == NULL))
773 {
774 LogRel(("%s: command=%#lx data=%p: unable to allocate %zu bytes\n",
775 __func__, command, data, cbReq));
776 return ENOMEM;
777 }
778
779 err = copyin(pvUser, pHdr, Hdr.cbIn);
780 if (err != 0)
781 {
782 RTMemTmpFree(pHdr);
783 return err;
784 }
785
786 if (Hdr.cbIn < cbReq)
787 memset((uint8_t *)pHdr + Hdr.cbIn, '\0', cbReq - Hdr.cbIn);
788 }
789
790 /*
791 * Process the IOCtl.
792 */
793 rc = VGDrvCommonIoCtl(command, &g_DevExt, fdata->session, pHdr, cbReq);
794 if (RT_SUCCESS(rc))
795 {
796 err = 0;
797
798 /*
799 * If unbuffered, copy back the result before returning.
800 */
801 if (pvUser != NULL)
802 {
803 size_t cbOut = pHdr->cbOut;
804 if (cbOut > cbReq)
805 {
806 LogRel(("%s: command=%#lx data=%p: too much output: %zu > %zu\n",
807 __func__, command, data, cbOut, cbReq));
808 cbOut = cbReq;
809 }
810
811 err = copyout(pHdr, pvUser, cbOut);
812 RTMemTmpFree(pHdr);
813 }
814 }
815 else
816 {
817 LogRel(("%s: command=%#lx data=%p: error %Rrc\n",
818 __func__, command, data, rc));
819
820 if (pvUser != NULL)
821 RTMemTmpFree(pHdr);
822
823 err = RTErrConvertToErrno(rc);
824 }
825
826 return err;
827}
828
829static int VBoxGuestNetBSDPoll(struct file *fp, int events)
830{
831 struct vboxguest_fdata *fdata = fp->f_data;
832 vboxguest_softc *sc = fdata->sc;
833
834 int rc = 0;
835 int events_processed;
836
837 uint32_t u32CurSeq;
838
839 LogFlow((DEVICE_NAME ": %s\n", __func__));
840
841 u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
842 if (fdata->session->u32MousePosChangedSeq != u32CurSeq)
843 {
844 events_processed = events & (POLLIN | POLLRDNORM);
845 fdata->session->u32MousePosChangedSeq = u32CurSeq;
846 }
847 else
848 {
849 events_processed = 0;
850
851 selrecord(curlwp, &g_SelInfo);
852 }
853
854 return events_processed;
855}
856
857
858/**
859 * @note This code is duplicated on other platforms with variations, so please
860 * keep them all up to date when making changes!
861 */
862int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
863{
864 /*
865 * Simple request validation (common code does the rest).
866 */
867 int rc;
868 if ( RT_VALID_PTR(pReqHdr)
869 && cbReq >= sizeof(*pReqHdr))
870 {
871 /*
872 * All requests except the connect one requires a valid session.
873 */
874 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
875 if (pSession)
876 {
877 if ( RT_VALID_PTR(pSession)
878 && pSession->pDevExt == &g_DevExt)
879 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
880 else
881 rc = VERR_INVALID_HANDLE;
882 }
883 else if (uReq == VBGL_IOCTL_IDC_CONNECT)
884 {
885 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
886 if (RT_SUCCESS(rc))
887 {
888 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
889 if (RT_FAILURE(rc))
890 VGDrvCommonCloseSession(&g_DevExt, pSession);
891 }
892 }
893 else
894 rc = VERR_INVALID_HANDLE;
895 }
896 else
897 rc = VERR_INVALID_POINTER;
898 return rc;
899}
900
901
902MODULE(MODULE_CLASS_DRIVER, vboxguest, "pci");
903
904/*
905 * XXX: See netbsd/vboxguest.ioconf for the details.
906*/
907#if 0
908#include "ioconf.c"
909#else
910
911static const struct cfiattrdata wsmousedevcf_iattrdata = {
912 "wsmousedev", 1, {
913 { "mux", "0", 0 },
914 }
915};
916
917/* device vboxguest: wsmousedev */
918static const struct cfiattrdata * const vboxguest_attrs[] = { &wsmousedevcf_iattrdata, NULL };
919CFDRIVER_DECL(vboxguest, DV_DULL, vboxguest_attrs);
920
921static struct cfdriver * const cfdriver_ioconf_vboxguest[] = {
922 &vboxguest_cd, NULL
923};
924
925
926static const struct cfparent vboxguest_pspec = {
927 "pci", "pci", DVUNIT_ANY
928};
929static int vboxguest_loc[] = { -1, -1 };
930
931
932static const struct cfparent wsmousedev_pspec = {
933 "wsmousedev", "vboxguest", DVUNIT_ANY
934};
935static int wsmousedev_loc[] = { 0 };
936
937
938static struct cfdata cfdata_ioconf_vboxguest[] = {
939 /* vboxguest0 at pci? dev ? function ? */
940 {
941 .cf_name = "vboxguest",
942 .cf_atname = "vboxguest",
943 .cf_unit = 0, /* Only unit 0 is ever used */
944 .cf_fstate = FSTATE_NOTFOUND,
945 .cf_loc = vboxguest_loc,
946 .cf_flags = 0,
947 .cf_pspec = &vboxguest_pspec,
948 },
949
950 /* wsmouse* at vboxguest? */
951 { "wsmouse", "wsmouse", 0, FSTATE_STAR, wsmousedev_loc, 0, &wsmousedev_pspec },
952
953 { NULL, NULL, 0, 0, NULL, 0, NULL }
954};
955
956static struct cfattach * const vboxguest_cfattachinit[] = {
957 &vboxguest_ca, NULL
958};
959
960static const struct cfattachinit cfattach_ioconf_vboxguest[] = {
961 { "vboxguest", vboxguest_cfattachinit },
962 { NULL, NULL }
963};
964#endif
965
966
967static int
968vboxguest_modcmd(modcmd_t cmd, void *opaque)
969{
970 devmajor_t bmajor, cmajor;
971 register_t retval;
972 int error;
973
974 LogFlow((DEVICE_NAME ": %s\n", __func__));
975
976 switch (cmd)
977 {
978 case MODULE_CMD_INIT:
979 error = config_init_component(cfdriver_ioconf_vboxguest,
980 cfattach_ioconf_vboxguest,
981 cfdata_ioconf_vboxguest);
982 if (error)
983 break;
984
985 bmajor = cmajor = NODEVMAJOR;
986 error = devsw_attach("vboxguest",
987 NULL, &bmajor,
988 &g_VBoxGuestNetBSDChrDevSW, &cmajor);
989 if (error)
990 {
991 if (error == EEXIST)
992 error = 0; /* maybe built-in ... improve eventually */
993 else
994 break;
995 }
996
997 error = do_sys_mknod(curlwp, "/dev/vboxguest",
998 0666|S_IFCHR, makedev(cmajor, 0),
999 &retval, UIO_SYSSPACE);
1000 if (error == EEXIST)
1001 error = 0;
1002 break;
1003
1004 case MODULE_CMD_FINI:
1005 error = config_fini_component(cfdriver_ioconf_vboxguest,
1006 cfattach_ioconf_vboxguest,
1007 cfdata_ioconf_vboxguest);
1008 if (error)
1009 break;
1010
1011 devsw_detach(NULL, &g_VBoxGuestNetBSDChrDevSW);
1012 break;
1013
1014 default:
1015 return ENOTTY;
1016 }
1017 return error;
1018}
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