VirtualBox

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

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

VMMDev,VBoxGuest: Classify who is calling the host (part 1). bugref:9105

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