VirtualBox

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

Last change on this file since 63454 was 63454, checked in by vboxsync, 9 years ago

VBoxGuest-netbsd.c: match should return 0 on failure.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.8 KB
Line 
1/* $Id: VBoxGuest-netbsd.c 63454 2016-08-15 00:46:36Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions Driver for NetBSD.
4 */
5
6/*
7 * Copyright (C) 2007-2016 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
18/** @todo r=bird: This must merge with SUPDrv-netbsd.c before long. The two
19 * source files should only differ on prefixes and the extra bits wrt to the
20 * pci device. I.e. it should be diffable so that fixes to one can easily be
21 * applied to the other. */
22
23
24/*********************************************************************************************************************************
25* Header Files *
26*********************************************************************************************************************************/
27#include <sys/param.h>
28#include <sys/systm.h>
29#include <sys/select.h>
30#include <sys/conf.h>
31#include <sys/kernel.h>
32#include <sys/kmem.h>
33#include <sys/module.h>
34#include <sys/device.h>
35#include <sys/bus.h>
36#include <sys/poll.h>
37#include <sys/proc.h>
38#include <sys/stat.h>
39#include <sys/selinfo.h>
40#include <sys/queue.h>
41#include <sys/lock.h>
42#include <sys/types.h>
43#include <sys/conf.h>
44#include <sys/malloc.h>
45#include <sys/uio.h>
46#include <sys/file.h>
47#include <sys/filedesc.h>
48#include <sys/vfs_syscalls.h>
49#include <dev/pci/pcivar.h>
50#include <dev/pci/pcireg.h>
51#include <dev/pci/pcidevs.h>
52
53#ifdef PVM
54# undef PVM
55#endif
56#include "VBoxGuestInternal.h"
57#include <VBox/log.h>
58#include <iprt/assert.h>
59#include <iprt/initterm.h>
60#include <iprt/process.h>
61#include <iprt/mem.h>
62#include <iprt/asm.h>
63
64
65/*********************************************************************************************************************************
66* Defined Constants And Macros *
67*********************************************************************************************************************************/
68/** The module name. */
69#define DEVICE_NAME "vboxguest"
70
71
72/*********************************************************************************************************************************
73* Structures and Typedefs *
74*********************************************************************************************************************************/
75typedef struct VBoxGuestDeviceState
76{
77 device_t sc_dev;
78 pci_chipset_tag_t pc;
79 bus_space_tag_t io_tag;
80 bus_space_handle_t io_handle;
81 bus_addr_t uIOPortBase;
82 bus_size_t io_regsize;
83 bus_space_tag_t iVMMDevMemResId;
84 bus_space_handle_t VMMDevMemHandle;
85
86 /** Size of the memory area. */
87 bus_size_t VMMDevMemSize;
88 /** Mapping of the register space */
89 bus_addr_t pMMIOBase;
90
91 /** IRQ resource handle. */
92 pci_intr_handle_t ih;
93 /** Pointer to the IRQ handler. */
94 void *pfnIrqHandler;
95
96 /** Controller features, limits and status. */
97 u_int vboxguest_state;
98} vboxguest_softc;
99
100
101struct vboxguest_session
102{
103 vboxguest_softc *sc;
104 PVBOXGUESTSESSION session;
105};
106
107#define VBOXGUEST_STATE_INITOK 1 << 0
108
109/*********************************************************************************************************************************
110* Internal Functions *
111*********************************************************************************************************************************/
112/*
113 * Character device file handlers.
114 */
115static int VBoxGuestNetBSDOpen(dev_t device, int flags, int fmt, struct lwp *process);
116static int VBoxGuestNetBSDClose(struct file *fp);
117static int VBoxGuestNetBSDIOCtl(struct file *fp, u_long cmd, void *addr);
118static int VBoxGuestNetBSDPoll(struct file *fp, int events);
119static void VBoxGuestNetBSDAttach(device_t, device_t, void*);
120static int VBoxGuestNetBSDDetach(device_t pDevice, int flags);
121
122/*
123 * IRQ related functions.
124 */
125static void VBoxGuestNetBSDRemoveIRQ(vboxguest_softc *sc);
126static int VBoxGuestNetBSDAddIRQ(vboxguest_softc *pvState, struct pci_attach_args *pa);
127static int VBoxGuestNetBSDISR(void *pvState);
128
129
130/*********************************************************************************************************************************
131* Global Variables *
132*********************************************************************************************************************************/
133/*
134 * The /dev/vboxguest character device entry points.
135 */
136static struct cdevsw g_VBoxGuestNetBSDChrDevSW =
137{
138 VBoxGuestNetBSDOpen,
139 noclose,
140 noread,
141 nowrite,
142 noioctl,
143 nostop,
144 notty,
145 nopoll,
146 nommap,
147 nokqfilter,
148};
149
150static const struct fileops vboxguest_fileops = {
151 .fo_read = fbadop_read,
152 .fo_write = fbadop_write,
153 .fo_ioctl = VBoxGuestNetBSDIOCtl,
154 .fo_fcntl = fnullop_fcntl,
155 .fo_poll = VBoxGuestNetBSDPoll,
156 .fo_stat = fbadop_stat,
157 .fo_close = VBoxGuestNetBSDClose,
158 .fo_kqfilter = fnullop_kqfilter,
159 .fo_restart = fnullop_restart
160};
161
162/** Device extention & session data association structure. */
163static VBOXGUESTDEVEXT g_DevExt;
164/** Reference counter */
165static volatile uint32_t cUsers;
166/** selinfo structure used for polling. */
167static struct selinfo g_SelInfo;
168
169CFDRIVER_DECL(vboxguest, DV_DULL, NULL);
170extern struct cfdriver vboxguest_cd;
171
172/**
173 * File open handler
174 *
175 */
176static int VBoxGuestNetBSDOpen(dev_t device, int flags, int fmt, struct lwp *process)
177{
178 int rc;
179 vboxguest_softc *vboxguest;
180 struct vboxguest_session *session;
181 file_t *fp;
182 int fd, error;
183
184 LogFlow((DEVICE_NAME ": %s\n", __func__));
185
186 if ((vboxguest = device_lookup_private(&vboxguest_cd, minor(device))) == NULL) {
187 printf("device_lookup_private failed\n");
188 return (ENXIO);
189 }
190 if ((vboxguest->vboxguest_state & VBOXGUEST_STATE_INITOK) == 0) {
191 aprint_error_dev(vboxguest->sc_dev, "device not configured\n");
192 return (ENXIO);
193 }
194
195 session = kmem_alloc(sizeof(*session), KM_SLEEP);
196 if (session == NULL) {
197 return (ENOMEM);
198 }
199
200 session->sc = vboxguest;
201
202 if ((error = fd_allocfile(&fp, &fd)) != 0) {
203 kmem_free(session, sizeof(*session));
204 return error;
205 }
206
207 /*
208 * Create a new session.
209 */
210 rc = VGDrvCommonCreateUserSession(&g_DevExt, &session->session);
211 if (! RT_SUCCESS(rc))
212 {
213 aprint_error_dev(vboxguest->sc_dev, "VBox session creation failed\n");
214 closef(fp); /* ??? */
215 kmem_free(session, sizeof(*session));
216 return RTErrConvertToErrno(rc);
217 }
218 ASMAtomicIncU32(&cUsers);
219 return fd_clone(fp, fd, flags, &vboxguest_fileops, session);
220
221}
222
223/**
224 * File close handler
225 *
226 */
227static int VBoxGuestNetBSDClose(struct file *fp)
228{
229 struct vboxguest_session *session = fp->f_data;
230 vboxguest_softc *vboxguest = session->sc;
231
232 LogFlow((DEVICE_NAME ": %s\n", __func__));
233
234 VGDrvCommonCloseSession(&g_DevExt, session->session);
235 ASMAtomicDecU32(&cUsers);
236
237 kmem_free(session, sizeof(*session));
238
239 return 0;
240}
241
242/**
243 * IOCTL handler
244 *
245 */
246static int VBoxGuestNetBSDIOCtl(struct file *fp, u_long command, void *data)
247{
248 struct vboxguest_session *session = fp->f_data;
249 vboxguest_softc *vboxguest = session->sc;
250
251 int rc = 0;
252
253 LogFlow((DEVICE_NAME ": %s: command=%#lx data=%p\n",
254 __func__, command, data));
255
256 /*
257 * Validate the request wrapper.
258 */
259 if (IOCPARM_LEN(command) != sizeof(VBGLBIGREQ)) {
260 Log((DEVICE_NAME ": %s: bad request %#lx size=%lu expected=%zu\n",
261 __func__, command, IOCPARM_LEN(command), sizeof(VBGLBIGREQ)));
262 return ENOTTY;
263 }
264
265 PVBGLBIGREQ ReqWrap = (PVBGLBIGREQ)data;
266 if (ReqWrap->u32Magic != VBGLBIGREQ_MAGIC)
267 {
268 Log((DEVICE_NAME ": %s: bad magic %#" PRIx32 "; pArg=%p Cmd=%#lx\n",
269 __func__, ReqWrap->u32Magic, data, command));
270 return EINVAL;
271 }
272 if (RT_UNLIKELY( ReqWrap->cbData == 0
273 || ReqWrap->cbData > _1M*16))
274 {
275 Log((DEVICE_NAME ": %s: bad size %" PRIu32 "; pArg=%p Cmd=%#lx\n",
276 __func__, ReqWrap->cbData, data, command));
277 return EINVAL;
278 }
279
280 /*
281 * Read the request.
282 */
283 void *pvBuf = RTMemTmpAlloc(ReqWrap->cbData);
284 if (RT_UNLIKELY(!pvBuf))
285 {
286 Log((DEVICE_NAME ": %s: RTMemTmpAlloc failed to alloc %" PRIu32 " bytes.\n",
287 __func__, ReqWrap->cbData));
288 return ENOMEM;
289 }
290
291 rc = copyin((void *)(uintptr_t)ReqWrap->pvDataR3, pvBuf, ReqWrap->cbData);
292 if (RT_UNLIKELY(rc))
293 {
294 RTMemTmpFree(pvBuf);
295 Log((DEVICE_NAME ": %s: copyin failed; pvBuf=%p pArg=%p Cmd=%#lx. rc=%d\n",
296 __func__, pvBuf, data, command, rc));
297 aprint_error_dev(vboxguest->sc_dev, "copyin failed\n");
298 return EFAULT;
299 }
300 if (RT_UNLIKELY( ReqWrap->cbData != 0
301 && !VALID_PTR(pvBuf)))
302 {
303 RTMemTmpFree(pvBuf);
304 Log((DEVICE_NAME ": %s: invalid pvBuf=%p\n",
305 __func__, pvBuf));
306 return EINVAL;
307 }
308
309 /*
310 * Process the IOCtl.
311 */
312 size_t cbDataReturned;
313 rc = VGDrvCommonIoCtl(command, &g_DevExt, session->session, pvBuf, ReqWrap->cbData, &cbDataReturned);
314 if (RT_SUCCESS(rc)) {
315 rc = 0;
316 if (RT_UNLIKELY(cbDataReturned > ReqWrap->cbData))
317 {
318 Log((DEVICE_NAME ": %s: too much output data %zu expected %" PRIu32 "\n",
319 __func__, cbDataReturned, ReqWrap->cbData));
320 cbDataReturned = ReqWrap->cbData;
321 }
322 if (cbDataReturned > 0)
323 {
324 rc = copyout(pvBuf, (void *)(uintptr_t)ReqWrap->pvDataR3, cbDataReturned);
325 if (RT_UNLIKELY(rc))
326 {
327 Log((DEVICE_NAME ": %s: copyout failed; pvBuf=%p pArg=%p. rc=%d\n",
328 __func__, pvBuf, data, rc));
329 }
330 }
331 } else {
332 Log((DEVICE_NAME ": %s: VGDrvCommonIoCtl failed. rc=%d\n",
333 __func__, rc));
334 rc = -rc;
335 }
336 RTMemTmpFree(pvBuf);
337 return rc;
338}
339
340static int VBoxGuestNetBSDPoll(struct file *fp, int events)
341{
342 struct vboxguest_session *session = fp->f_data;
343 vboxguest_softc *vboxguest = session->sc;
344
345 int rc = 0;
346 int events_processed;
347
348 uint32_t u32CurSeq;
349
350 LogFlow((DEVICE_NAME ": %s\n", __func__));
351
352 u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
353 if (session->session->u32MousePosChangedSeq != u32CurSeq) {
354 events_processed = events & (POLLIN | POLLRDNORM);
355 session->session->u32MousePosChangedSeq = u32CurSeq;
356 } else {
357 events_processed = 0;
358
359 selrecord(curlwp, &g_SelInfo);
360 }
361
362 return events_processed;
363}
364
365static int VBoxGuestNetBSDDetach(device_t self, int flags)
366{
367 vboxguest_softc *vboxguest;
368 vboxguest = device_private(self);
369
370 LogFlow((DEVICE_NAME ": %s\n", __func__));
371
372 if (cUsers > 0)
373 return EBUSY;
374
375 if ((vboxguest->vboxguest_state & VBOXGUEST_STATE_INITOK) == 0) {
376 return 0;
377 }
378
379 /*
380 * Reverse what we did in VBoxGuestNetBSDAttach.
381 */
382
383 VBoxGuestNetBSDRemoveIRQ(vboxguest);
384
385 VGDrvCommonDeleteDevExt(&g_DevExt);
386
387 bus_space_unmap(vboxguest->iVMMDevMemResId, vboxguest->VMMDevMemHandle, vboxguest->VMMDevMemSize);
388 bus_space_unmap(vboxguest->io_tag, vboxguest->io_handle, vboxguest->io_regsize);
389
390 RTR0Term();
391
392 return 0;
393}
394
395/**
396 * Interrupt service routine.
397 *
398 * @returns Whether the interrupt was from VMMDev.
399 * @param pvState Opaque pointer to the device state.
400 */
401static int VBoxGuestNetBSDISR(void *pvState)
402{
403 LogFlow((DEVICE_NAME ": %s: pvState=%p\n", __func__, pvState));
404
405 bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
406
407 return fOurIRQ ? 0 : 1;
408}
409
410void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
411{
412 LogFlow((DEVICE_NAME ": %s\n", __func__));
413
414 /*
415 * Wake up poll waiters.
416 */
417 selnotify(&g_SelInfo, 0, 0);
418}
419
420/**
421 * Sets IRQ for VMMDev.
422 *
423 * @returns NetBSD error code.
424 * @param vboxguest Pointer to the state info structure.
425 * @param pa Pointer to the PCI attach arguments.
426 */
427static int VBoxGuestNetBSDAddIRQ(vboxguest_softc *vboxguest, struct pci_attach_args *pa)
428{
429 int iResId = 0;
430 int rc = 0;
431 const char *intrstr;
432#if __NetBSD_Prereq__(6, 99, 39)
433 char intstrbuf[100];
434#endif
435
436 LogFlow((DEVICE_NAME ": %s\n", __func__));
437
438 if (pci_intr_map(pa, &vboxguest->ih)) {
439 aprint_error_dev(vboxguest->sc_dev, "couldn't map interrupt.\n");
440 return VERR_DEV_IO_ERROR;
441 }
442 intrstr = pci_intr_string(vboxguest->pc, vboxguest->ih
443#if __NetBSD_Prereq__(6, 99, 39)
444 , intstrbuf, sizeof(intstrbuf)
445#endif
446 );
447 aprint_normal_dev(vboxguest->sc_dev, "interrupting at %s\n", intrstr);
448 vboxguest->pfnIrqHandler = pci_intr_establish(vboxguest->pc, vboxguest->ih, IPL_BIO, VBoxGuestNetBSDISR, vboxguest);
449 if (vboxguest->pfnIrqHandler == NULL) {
450 aprint_error_dev(vboxguest->sc_dev, "couldn't establish interrupt\n");
451 return VERR_DEV_IO_ERROR;
452 }
453
454 return VINF_SUCCESS;
455}
456
457/**
458 * Removes IRQ for VMMDev.
459 *
460 * @param vboxguest Opaque pointer to the state info structure.
461 */
462static void VBoxGuestNetBSDRemoveIRQ(vboxguest_softc *vboxguest)
463{
464 LogFlow((DEVICE_NAME ": %s\n", __func__));
465
466 if (vboxguest->pfnIrqHandler)
467 {
468 pci_intr_disestablish(vboxguest->pc, vboxguest->pfnIrqHandler);
469 }
470}
471
472static void VBoxGuestNetBSDAttach(device_t parent, device_t self, void *aux)
473{
474 int rc = VINF_SUCCESS;
475 int iResId = 0;
476 vboxguest_softc *vboxguest;
477 struct pci_attach_args *pa = aux;
478 bus_space_tag_t iot, memt;
479 bus_space_handle_t ioh, memh;
480 bus_dma_segment_t seg;
481 int ioh_valid, memh_valid;
482
483 cUsers = 0;
484
485 aprint_normal(": VirtualBox Guest\n");
486
487 vboxguest = device_private(self);
488 vboxguest->sc_dev = self;
489
490 /*
491 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
492 */
493 rc = RTR0Init(0);
494 if (RT_FAILURE(rc))
495 {
496 LogFunc(("RTR0Init failed.\n"));
497 aprint_error_dev(vboxguest->sc_dev, "RTR0Init failed\n");
498 return ;
499 }
500
501 vboxguest->pc = pa->pa_pc;
502
503 /*
504 * Allocate I/O port resource.
505 */
506 ioh_valid = (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0, &vboxguest->io_tag, &vboxguest->io_handle, &vboxguest->uIOPortBase, &vboxguest->io_regsize) == 0);
507
508 if (ioh_valid)
509 {
510
511 /*
512 * Map the MMIO region.
513 */
514 memh_valid = (pci_mapreg_map(pa, PCI_MAPREG_START+4, PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR, &vboxguest->iVMMDevMemResId, &vboxguest->VMMDevMemHandle, &vboxguest->pMMIOBase, &vboxguest->VMMDevMemSize) == 0);
515 if (memh_valid)
516 {
517 /*
518 * Call the common device extension initializer.
519 */
520 rc = VGDrvCommonInitDevExt(&g_DevExt, vboxguest->uIOPortBase,
521 bus_space_vaddr(vboxguest->iVMMDevMemResId,
522 vboxguest->VMMDevMemHandle),
523 vboxguest->VMMDevMemSize,
524#if ARCH_BITS == 64
525 VBOXOSTYPE_NetBSD_x64,
526#else
527 VBOXOSTYPE_NetBSD,
528#endif
529 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
530 if (RT_SUCCESS(rc))
531 {
532 /*
533 * Add IRQ of VMMDev.
534 */
535 rc = VBoxGuestNetBSDAddIRQ(vboxguest, pa);
536 if (RT_SUCCESS(rc))
537 {
538 vboxguest->vboxguest_state |= VBOXGUEST_STATE_INITOK;
539 return ;
540 }
541 VGDrvCommonDeleteDevExt(&g_DevExt);
542 } else {
543 aprint_error_dev(vboxguest->sc_dev, "init failed\n");
544 }
545 bus_space_unmap(vboxguest->iVMMDevMemResId, vboxguest->VMMDevMemHandle, vboxguest->VMMDevMemSize);
546 } else {
547 aprint_error_dev(vboxguest->sc_dev, "MMIO mapping failed\n");
548 }
549 bus_space_unmap(vboxguest->io_tag, vboxguest->io_handle, vboxguest->io_regsize);
550 } else {
551 aprint_error_dev(vboxguest->sc_dev, "IO mapping failed\n");
552 }
553
554 RTR0Term();
555 return ;
556}
557
558static int
559VBoxGuestNetBSDMatch(device_t parent, cfdata_t match, void *aux)
560{
561 const struct pci_attach_args *pa = aux;
562
563 if ( PCI_VENDOR(pa->pa_id) == VMMDEV_VENDORID
564 && PCI_PRODUCT(pa->pa_id) == VMMDEV_DEVICEID)
565 {
566 return 1;
567 }
568
569 return 0;
570}
571
572/* Common code that depend on g_DevExt. */
573#include "VBoxGuestIDC-unix.c.h"
574
575CFATTACH_DECL_NEW(vboxguest, sizeof(vboxguest_softc),
576 VBoxGuestNetBSDMatch, VBoxGuestNetBSDAttach, VBoxGuestNetBSDDetach, NULL);
577
578MODULE(MODULE_CLASS_DRIVER, vboxguest, "pci");
579
580static int loc[2] = {-1, -1};
581
582static const struct cfparent pspec = {
583 "pci", "pci", DVUNIT_ANY
584};
585
586static struct cfdata vboxguest_cfdata[] = {
587 {
588 .cf_name = "vboxguest",
589 .cf_atname = "vboxguest",
590 .cf_unit = 0, /* Only unit 0 is ever used */
591 .cf_fstate = FSTATE_STAR,
592 .cf_loc = loc,
593 .cf_flags = 0,
594 .cf_pspec = &pspec,
595 },
596 { NULL, NULL, 0, 0, NULL, 0, NULL }
597};
598
599static int
600vboxguest_modcmd(modcmd_t cmd, void *opaque)
601{
602 devmajor_t bmajor, cmajor;
603 int error;
604 register_t retval;
605
606 LogFlow((DEVICE_NAME ": %s\n", __func__));
607
608 bmajor = cmajor = NODEVMAJOR;
609 switch (cmd) {
610 case MODULE_CMD_INIT:
611 error = config_cfdriver_attach(&vboxguest_cd);
612 if (error) {
613 printf("config_cfdriver_attach failed: %d", error);
614 break;
615 }
616 error = config_cfattach_attach(vboxguest_cd.cd_name, &vboxguest_ca);
617 if (error) {
618 config_cfdriver_detach(&vboxguest_cd);
619 printf("%s: unable to register cfattach\n", vboxguest_cd.cd_name);
620 break;
621 }
622
623 error = config_cfdata_attach(vboxguest_cfdata, 1);
624 if (error) {
625 printf("%s: unable to attach cfdata\n", vboxguest_cd.cd_name);
626 config_cfattach_detach(vboxguest_cd.cd_name, &vboxguest_ca);
627 config_cfdriver_detach(&vboxguest_cd);
628 break;
629 }
630
631 error = devsw_attach("vboxguest", NULL, &bmajor, &g_VBoxGuestNetBSDChrDevSW, &cmajor);
632
633 if (error == EEXIST)
634 error = 0; /* maybe built-in ... improve eventually */
635 if (error)
636 break;
637
638 error = do_sys_mknod(curlwp, "/dev/vboxguest", 0666|S_IFCHR, makedev(cmajor, 0), &retval, UIO_SYSSPACE);
639 if (error == EEXIST)
640 error = 0;
641 break;
642 case MODULE_CMD_FINI:
643 error = config_cfdata_detach(vboxguest_cfdata);
644 if (error)
645 break;
646 error = config_cfattach_detach(vboxguest_cd.cd_name, &vboxguest_ca);
647 if (error)
648 break;
649 config_cfdriver_detach(&vboxguest_cd);
650 error = devsw_detach(NULL, &g_VBoxGuestNetBSDChrDevSW);
651 break;
652 default:
653 return ENOTTY;
654 }
655 return error;
656}
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