VirtualBox

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

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

Additions/common/VBoxGuest: NetBSD support, based on FreeBSD version.

From Haomai Wang GSoC project with additional changes by Arto Huusko.
Adapted to catch up on changes in the VirtualBox tree.

TODO: Needs more stylistic changes to conform to our style and the
diff to the FreeBSD can be further reduced.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.9 KB
Line 
1/* $Id: VBoxGuest-netbsd.c 63339 2016-08-11 13:02:31Z 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 struct pci_attach_args *pa;
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);
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 ":VBoxGuestNetBSDOpen\n"));
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 VGDrvCommonCloseSession(&g_DevExt, session->session);
233 ASMAtomicDecU32(&cUsers);
234
235 kmem_free(session, sizeof(*session));
236
237 return 0;
238}
239
240/**
241 * IOCTL handler
242 *
243 */
244static int VBoxGuestNetBSDIOCtl(struct file *fp, u_long command, void *data)
245{
246 struct vboxguest_session *session = fp->f_data;
247 vboxguest_softc *vboxguest = session->sc;
248
249 int rc = 0;
250
251 /*
252 * Validate the request wrapper.
253 */
254 if (IOCPARM_LEN(command) != sizeof(VBGLBIGREQ)) {
255 Log((DEVICE_NAME ": VBoxGuestFreeBSDIOCtl: bad request %lu size=%lu expected=%d\n", command, IOCPARM_LEN(command), sizeof(VBGLBIGREQ)));
256 return ENOTTY;
257 }
258
259 PVBGLBIGREQ ReqWrap = (PVBGLBIGREQ)data;
260 if (ReqWrap->u32Magic != VBGLBIGREQ_MAGIC)
261 {
262 Log((DEVICE_NAME ": VBoxGuestNetBSDIOCtl: bad magic %#x; pArg=%p Cmd=%lu.\n", ReqWrap->u32Magic, data, command));
263 return EINVAL;
264 }
265 if (RT_UNLIKELY( ReqWrap->cbData == 0
266 || ReqWrap->cbData > _1M*16))
267 {
268 printf(DEVICE_NAME ": VBoxGuestNetBSDIOCtl: bad size %#x; pArg=%p Cmd=%lu.\n", ReqWrap->cbData, data, command);
269 return EINVAL;
270 }
271
272 /*
273 * Read the request.
274 */
275 void *pvBuf = RTMemTmpAlloc(ReqWrap->cbData);
276 if (RT_UNLIKELY(!pvBuf))
277 {
278 Log((DEVICE_NAME ":VBoxGuestNetBSDIOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", ReqWrap->cbData));
279 return ENOMEM;
280 }
281
282 rc = copyin((void *)(uintptr_t)ReqWrap->pvDataR3, pvBuf, ReqWrap->cbData);
283 if (RT_UNLIKELY(rc))
284 {
285 RTMemTmpFree(pvBuf);
286 Log((DEVICE_NAME ":VBoxGuestNetBSDIOCtl: copyin failed; pvBuf=%p pArg=%p Cmd=%lu. rc=%d\n", pvBuf, data, command, rc));
287aprint_error_dev(vboxguest->sc_dev, "copyin failed\n");
288 return EFAULT;
289 }
290 if (RT_UNLIKELY( ReqWrap->cbData != 0
291 && !VALID_PTR(pvBuf)))
292 {
293 RTMemTmpFree(pvBuf);
294 Log((DEVICE_NAME ":VBoxGuestNetBSDIOCtl: pvBuf invalid pointer %p\n", pvBuf));
295 return EINVAL;
296 }
297
298 /*
299 * Process the IOCtl.
300 */
301 size_t cbDataReturned;
302 rc = VGDrvCommonIoCtl(command, &g_DevExt, session->session, pvBuf, ReqWrap->cbData, &cbDataReturned);
303 if (RT_SUCCESS(rc)) {
304 rc = 0;
305 if (RT_UNLIKELY(cbDataReturned > ReqWrap->cbData))
306 {
307 Log((DEVICE_NAME ":VBoxGuestNetBSDIOCtl: too much output data %d expected %d\n", cbDataReturned, ReqWrap->cbData));
308 cbDataReturned = ReqWrap->cbData;
309 }
310 if (cbDataReturned > 0)
311 {
312 rc = copyout(pvBuf, (void *)(uintptr_t)ReqWrap->pvDataR3, cbDataReturned);
313 if (RT_UNLIKELY(rc))
314 {
315 Log((DEVICE_NAME ":VBoxGuestNetBSDIOCtl: copyout failed; pvBuf=%p pArg=%p. rc=%d\n", pvBuf, data, rc));
316 }
317 }
318 } else {
319 Log((DEVICE_NAME ":VBoxGuestNetBSDIOCtl: VGDrvCommonIoCtl failed. rc=%d\n", rc));
320 rc = -rc;
321 }
322 RTMemTmpFree(pvBuf);
323 return rc;
324}
325
326static int VBoxGuestNetBSDPoll(struct file *fp, int events)
327{
328 struct vboxguest_session *session = fp->f_data;
329 vboxguest_softc *vboxguest = session->sc;
330
331 int rc = 0;
332 int events_processed;
333
334 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
335 if (session->session->u32MousePosChangedSeq != u32CurSeq) {
336 events_processed = events & (POLLIN | POLLRDNORM);
337 session->session->u32MousePosChangedSeq = u32CurSeq;
338 } else {
339 events_processed = 0;
340
341 selrecord(curlwp, &g_SelInfo);
342 }
343
344 return events_processed;
345}
346
347static int VBoxGuestNetBSDDetach(device_t self, int flags)
348{
349 vboxguest_softc *vboxguest;
350 vboxguest = device_private(self);
351
352 if (cUsers > 0)
353 return EBUSY;
354
355 if ((vboxguest->vboxguest_state & VBOXGUEST_STATE_INITOK) == 0) {
356 return 0;
357 }
358
359 /*
360 * Reverse what we did in VBoxGuestNetBSDAttach.
361 */
362
363 VBoxGuestNetBSDRemoveIRQ(vboxguest);
364
365 VGDrvCommonDeleteDevExt(&g_DevExt);
366
367 RTR0Term();
368
369 return 0;
370}
371
372/**
373 * Interrupt service routine.
374 *
375 * @returns Whether the interrupt was from VMMDev.
376 * @param pvState Opaque pointer to the device state.
377 */
378static int VBoxGuestNetBSDISR(void *pvState)
379{
380 LogFlow((DEVICE_NAME ":VBoxGuestNetBSDISR pvState=%p\n", pvState));
381
382 bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
383
384 return fOurIRQ ? 0 : 1;
385}
386
387void VBoxGuestNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
388{
389 LogFlow((DEVICE_NAME "::NativeISRMousePollEvent:\n"));
390
391 /*
392 * Wake up poll waiters.
393 */
394 selnotify(&g_SelInfo, 0, 0);
395}
396
397/**
398 * Sets IRQ for VMMDev.
399 *
400 * @returns NetBSD error code.
401 * @param pDevice Pointer to the device info structure.
402 * @param pvState Pointer to the state info structure.
403 */
404static int VBoxGuestNetBSDAddIRQ(vboxguest_softc *vboxguest)
405{
406 int iResId = 0;
407 int rc = 0;
408 struct pci_attach_args *pa = vboxguest->pa;
409 const char *intrstr;
410 char intstrbuf[100];
411
412 if (pci_intr_map(pa, &vboxguest->ih)) {
413 aprint_error_dev(vboxguest->sc_dev, "couldn't map interrupt.\n");
414 return VERR_DEV_IO_ERROR;
415 }
416 intrstr = pci_intr_string(vboxguest->pa->pa_pc, vboxguest->ih,
417 intstrbuf, sizeof(intstrbuf));
418 aprint_normal_dev(vboxguest->sc_dev, "interrupting at %s\n", intrstr);
419 vboxguest->pfnIrqHandler = pci_intr_establish(vboxguest->pa->pa_pc, vboxguest->ih, IPL_BIO, VBoxGuestNetBSDISR, vboxguest);
420 if (vboxguest->pfnIrqHandler == NULL) {
421 aprint_error_dev(vboxguest->sc_dev, "couldn't establish interrupt\n");
422 return VERR_DEV_IO_ERROR;
423 }
424
425 return VINF_SUCCESS;
426}
427
428/**
429 * Removes IRQ for VMMDev.
430 *
431 * @param pDevice Pointer to the device info structure.
432 * @param pvState Opaque pointer to the state info structure.
433 */
434static void VBoxGuestNetBSDRemoveIRQ(vboxguest_softc *vboxguest)
435{
436 if (vboxguest->pfnIrqHandler)
437 {
438 pci_intr_disestablish(vboxguest->pa->pa_pc, vboxguest->pfnIrqHandler);
439 }
440}
441
442static void VBoxGuestNetBSDAttach(device_t parent, device_t self, void *aux)
443{
444 int rc = VINF_SUCCESS;
445 int iResId = 0;
446 vboxguest_softc *vboxguest;
447 struct pci_attach_args *pa = aux;
448 bus_space_tag_t iot, memt;
449 bus_space_handle_t ioh, memh;
450 bus_dma_segment_t seg;
451 int ioh_valid, memh_valid;
452
453 cUsers = 0;
454
455 aprint_normal(": VirtualBox Guest\n");
456
457 vboxguest = device_private(self);
458 vboxguest->sc_dev = self;
459
460 /*
461 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
462 */
463 rc = RTR0Init(0);
464 if (RT_FAILURE(rc))
465 {
466 LogFunc(("RTR0Init failed.\n"));
467 aprint_error_dev(vboxguest->sc_dev, "RTR0Init failed\n");
468 return ;
469 }
470
471 vboxguest->pa = pa;
472
473 /*
474 * Allocate I/O port resource.
475 */
476 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);
477
478 if (ioh_valid)
479 {
480
481 /*
482 * Map the MMIO region.
483 */
484 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);
485 if (memh_valid)
486 {
487 /*
488 * Call the common device extension initializer.
489 */
490 rc = VGDrvCommonInitDevExt(&g_DevExt, vboxguest->uIOPortBase,
491 bus_space_vaddr(vboxguest->iVMMDevMemResId,
492 vboxguest->VMMDevMemHandle),
493 vboxguest->VMMDevMemSize,
494#if ARCH_BITS == 64
495 VBOXOSTYPE_NetBSD_x64,
496#else
497 VBOXOSTYPE_NetBSD,
498#endif
499 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
500 if (RT_SUCCESS(rc))
501 {
502 /*
503 * Add IRQ of VMMDev.
504 */
505 rc = VBoxGuestNetBSDAddIRQ(vboxguest);
506 if (RT_SUCCESS(rc))
507 {
508 vboxguest->vboxguest_state |= VBOXGUEST_STATE_INITOK;
509 return ;
510 }
511 VGDrvCommonDeleteDevExt(&g_DevExt);
512 } else {
513 aprint_error_dev(vboxguest->sc_dev, "init failed\n");
514 }
515 } else {
516 aprint_error_dev(vboxguest->sc_dev, "MMIO mapping failed\n");
517 }
518 } else {
519 aprint_error_dev(vboxguest->sc_dev, "IO mapping failed\n");
520 }
521
522 RTR0Term();
523 return ;
524}
525
526static int
527VBoxGuestNetBSDMatch(device_t parent, cfdata_t match, void *aux)
528{
529 const struct pci_attach_args *pa = aux;
530 if ((PCI_VENDOR(pa->pa_id) == VMMDEV_VENDORID)
531 && (PCI_PRODUCT(pa->pa_id) == VMMDEV_DEVICEID)) {
532 return 1;
533 } else {
534 return 2;
535 }
536}
537
538/* Common code that depend on g_DevExt. */
539#include "VBoxGuestIDC-unix.c.h"
540
541CFATTACH_DECL_NEW(vboxguest, sizeof(vboxguest_softc),
542 VBoxGuestNetBSDMatch, VBoxGuestNetBSDAttach, VBoxGuestNetBSDDetach, NULL);
543
544MODULE(MODULE_CLASS_DRIVER, vboxguest, "pci");
545
546static int loc[2] = {-1, -1};
547
548static const struct cfparent pspec = {
549 "pci", "pci", DVUNIT_ANY
550};
551
552static struct cfdata vboxguest_cfdata[] = {
553 {
554 .cf_name = "vboxguest",
555 .cf_atname = "vboxguest",
556 .cf_unit = 0, /* Only unit 0 is ever used */
557 .cf_fstate = FSTATE_STAR,
558 .cf_loc = loc,
559 .cf_flags = 0,
560 .cf_pspec = &pspec,
561 },
562 { NULL, NULL, 0, 0, NULL, 0, NULL }
563};
564
565static int
566vboxguest_modcmd(modcmd_t cmd, void *opaque)
567{
568 devmajor_t bmajor, cmajor;
569 int error;
570 register_t retval;
571
572 bmajor = cmajor = NODEVMAJOR;
573 switch (cmd) {
574 case MODULE_CMD_INIT:
575 error = config_cfdriver_attach(&vboxguest_cd);
576 if (error) {
577 printf("config_cfdriver_attach failed: %d", error);
578 break;
579 }
580 error = config_cfattach_attach(vboxguest_cd.cd_name, &vboxguest_ca);
581 if (error) {
582 config_cfdriver_detach(&vboxguest_cd);
583 printf("%s: unable to register cfattach\n", vboxguest_cd.cd_name);
584 break;
585 }
586
587 error = config_cfdata_attach(vboxguest_cfdata, 1);
588 if (error) {
589 printf("%s: unable to attach cfdata\n", vboxguest_cd.cd_name);
590 config_cfattach_detach(vboxguest_cd.cd_name, &vboxguest_ca);
591 config_cfdriver_detach(&vboxguest_cd);
592 break;
593 }
594
595 error = devsw_attach("vboxguest", NULL, &bmajor, &g_VBoxGuestNetBSDChrDevSW, &cmajor);
596
597 if (error == EEXIST)
598 error = 0; /* maybe built-in ... improve eventually */
599 if (error)
600 break;
601
602 error = do_sys_mknod(curlwp, "/dev/vboxguest", 0666|S_IFCHR, makedev(cmajor, 0), &retval, UIO_SYSSPACE);
603 if (error == EEXIST)
604 error = 0;
605 break;
606 case MODULE_CMD_FINI:
607 error = config_cfdata_detach(vboxguest_cfdata);
608 if (error)
609 break;
610 error = config_cfattach_detach(vboxguest_cd.cd_name, &vboxguest_ca);
611 if (error)
612 break;
613 config_cfdriver_detach(&vboxguest_cd);
614 error = devsw_detach(NULL, &g_VBoxGuestNetBSDChrDevSW);
615 break;
616 default:
617 return ENOTTY;
618 }
619 return error;
620}
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