VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/linux/SUPDrv-linux.c@ 876

Last change on this file since 876 was 876, checked in by vboxsync, 18 years ago

protect locked pages against fork()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.3 KB
Line 
1/** @file
2 * The VirtualBox Support Driver - Linux hosts.
3 */
4
5/*
6 * Copyright (C) 2006 InnoTek Systemberatung GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21/*******************************************************************************
22* Header Files *
23*******************************************************************************/
24#include "SUPDRV.h"
25#include <iprt/assert.h>
26#include <iprt/spinlock.h>
27#include <iprt/semaphore.h>
28#include <iprt/initterm.h>
29#include <iprt/process.h>
30#include <iprt/err.h>
31#include <iprt/mem.h>
32
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/fs.h>
37#include <linux/mm.h>
38#include <linux/pagemap.h>
39#include <linux/slab.h>
40#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
41# include <linux/jiffies.h>
42#endif
43#include <asm/mman.h>
44#include <asm/io.h>
45#include <asm/uaccess.h>
46#ifdef CONFIG_DEVFS_FS
47# include <linux/devfs_fs_kernel.h>
48#endif
49#ifdef CONFIG_VBOXDRV_AS_MISC
50# include <linux/miscdevice.h>
51#endif
52#ifdef CONFIG_X86_LOCAL_APIC
53# include <asm/apic.h>
54# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
55# include <asm/nmi.h>
56# endif
57#endif
58
59#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
60# ifndef page_to_pfn
61# define page_to_pfn(page) ((page) - mem_map)
62# endif
63# include <asm/pgtable.h>
64# define global_flush_tlb __flush_tlb_global
65#endif
66
67/* devfs defines */
68#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
69# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
70
71# define VBOX_REGISTER_DEVFS() \
72({ \
73 void *rc = NULL; \
74 if (devfs_mk_cdev(MKDEV(DEVICE_MAJOR, 0), \
75 S_IFCHR | S_IRUGO | S_IWUGO, \
76 DEVICE_NAME) == 0) \
77 rc = (void *)' '; /* return not NULL */ \
78 rc; \
79 })
80
81# define VBOX_UNREGISTER_DEVFS(handle) \
82 devfs_remove(DEVICE_NAME);
83
84# else /* < 2.6.0 */
85
86# define VBOX_REGISTER_DEVFS() \
87 devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, \
88 DEVICE_MAJOR, 0, \
89 S_IFCHR | S_IRUGO | S_IWUGO, \
90 &gFileOpsVBoxDrv, NULL)
91
92# define VBOX_UNREGISTER_DEVFS(handle) \
93 if (handle != NULL) \
94 devfs_unregister(handle)
95
96# endif /* < 2.6.0 */
97#endif /* CONFIG_DEV_FS && !CONFIG_VBOXDEV_AS_MISC */
98
99#ifndef CONFIG_VBOXDRV_AS_MISC
100# if defined(CONFIG_DEVFS_FS) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 0)
101# define VBOX_REGISTER_DEVICE(a,b,c) devfs_register_chrdev(a,b,c)
102# define VBOX_UNREGISTER_DEVICE(a,b) devfs_unregister_chrdev(a,b)
103# else
104# define VBOX_REGISTER_DEVICE(a,b,c) register_chrdev(a,b,c)
105# define VBOX_UNREGISTER_DEVICE(a,b) unregister_chrdev(a,b)
106# endif
107#endif /* !CONFIG_VBOXDRV_AS_MISC */
108
109
110#ifdef CONFIG_X86_HIGH_ENTRY
111# error "CONFIG_X86_HIGH_ENTRY is not supported by VBoxDrv at this time."
112#endif
113
114/*
115 * This sucks soooo badly on x86! Why don't they export __PAGE_KERNEL_EXEC so PAGE_KERNEL_EXEC would be usable?
116 */
117#if defined(__AMD64__)
118# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL_EXEC
119#elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
120# define MY_PAGE_KERNEL_EXEC __pgprot(cpu_has_pge ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC)
121#else
122# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL
123#endif
124
125/*
126 * The redhat hack section.
127 * - The current hacks are for 2.4.21-15.EL only.
128 */
129#ifndef NO_REDHAT_HACKS
130/* accounting. */
131# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
132# ifdef VM_ACCOUNT
133# define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c, 0) /* should it be 1 or 0? */
134# endif
135# endif
136
137/* backported remap_page_range. */
138# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
139# include <asm/tlb.h>
140# ifdef tlb_vma /* probably not good enough... */
141# define HAVE_26_STYLE_REMAP_PAGE_RANGE 1
142# endif
143# endif
144
145# ifndef __AMD64__
146/* In 2.6.9-22.ELsmp we have to call change_page_attr() twice when changing
147 * the page attributes from PAGE_KERNEL to something else, because there appears
148 * to be a bug in one of the many patches that redhat applied.
149 * It should be safe to do this on less buggy linux kernels too. ;-)
150 */
151# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
152 do { \
153 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) \
154 change_page_attr(pPages, cPages, prot); \
155 change_page_attr(pPages, cPages, prot); \
156 } while (0)
157# endif
158#endif /* !NO_REDHAT_HACKS */
159
160
161#ifndef MY_DO_MUNMAP
162# define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c)
163#endif
164
165#ifndef MY_CHANGE_PAGE_ATTR
166# ifdef __AMD64__ /** @todo This is a cheap hack, but it'll get around that 'else BUG();' in __change_page_attr(). */
167# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
168 do { \
169 change_page_attr(pPages, cPages, PAGE_KERNEL_NOCACHE); \
170 change_page_attr(pPages, cPages, prot); \
171 } while (0)
172# else
173# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) change_page_attr(pPages, cPages, prot)
174# endif
175#endif
176
177
178/** @def ONE_MSEC_IN_JIFFIES
179 * The number of jiffies that make up 1 millisecond. This is only actually used
180 * when HZ is > 1000. */
181#if HZ <= 1000
182# define ONE_MSEC_IN_JIFFIES 0
183#elif !(HZ % 1000)
184# define ONE_MSEC_IN_JIFFIES (HZ / 1000)
185#else
186# define ONE_MSEC_IN_JIFFIES ((HZ + 999) / 1000)
187# error "HZ is not a multiple of 1000, the GIP stuff won't work right!"
188#endif
189
190
191/*******************************************************************************
192* Defined Constants And Macros *
193*******************************************************************************/
194/**
195 * Device extention & session data association structure.
196 */
197static SUPDRVDEVEXT g_DevExt;
198
199/** Timer structure for the GIP update. */
200static struct timer_list g_GipTimer;
201/** Pointer to the page structure for the GIP. */
202struct page *g_pGipPage;
203
204/** Registered devfs device handle. */
205#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
206# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
207static void *g_hDevFsVBoxDrv = NULL;
208# else
209static devfs_handle_t g_hDevFsVBoxDrv = NULL;
210# endif
211#endif
212
213#ifndef CONFIG_VBOXDRV_AS_MISC
214/** Module major number */
215#define DEVICE_MAJOR 234
216/** Saved major device number */
217static int g_iModuleMajor;
218#endif /* !CONFIG_VBOXDRV_AS_MISC */
219
220/** The module name. */
221#define DEVICE_NAME "vboxdrv"
222
223#ifdef __AMD64__
224/**
225 * Memory for the executable memory heap (in IPRT).
226 */
227extern uint8_t g_abExecMemory[1572864]; /* 1.5 MB */
228__asm__(".section execmemory, \"awx\", @progbits\n\t"
229 ".align 32\n\t"
230 ".globl g_abExecMemory\n"
231 "g_abExecMemory:\n\t"
232 ".zero 1572864\n\t"
233 ".type g_abExecMemory, @object\n\t"
234 ".size g_abExecMemory, 1572864\n\t"
235 ".text\n\t");
236#endif
237
238
239/*******************************************************************************
240* Internal Functions *
241*******************************************************************************/
242static int VBoxSupDrvInit(void);
243static void VBoxSupDrvUnload(void);
244static int VBoxSupDrvCreate(struct inode *pInode, struct file *pFilp);
245static int VBoxSupDrvClose(struct inode *pInode, struct file *pFilp);
246static int VBoxSupDrvDeviceControl(struct inode *pInode, struct file *pFilp,
247 unsigned int IOCmd, unsigned long IOArg);
248static void *VBoxSupDrvMapUser(struct page **papPages, unsigned cPages, unsigned fProt, pgprot_t pgFlags);
249static int VBoxSupDrvInitGip(PSUPDRVDEVEXT pDevExt);
250static int VBoxSupDrvTermGip(PSUPDRVDEVEXT pDevExt);
251static void VBoxSupGipTimer(unsigned long ulUser);
252static int VBoxSupDrvOrder(unsigned long size);
253static int VBoxSupDrvErr2LinuxErr(int);
254
255
256/** The file_operations structure. */
257static struct file_operations gFileOpsVBoxDrv =
258{
259 owner: THIS_MODULE,
260 open: VBoxSupDrvCreate,
261 release: VBoxSupDrvClose,
262 ioctl: VBoxSupDrvDeviceControl,
263};
264
265#ifdef CONFIG_VBOXDRV_AS_MISC
266/** The miscdevice structure. */
267static struct miscdevice gMiscDevice =
268{
269 minor: MISC_DYNAMIC_MINOR,
270 name: DEVICE_NAME,
271 fops: &gFileOpsVBoxDrv,
272# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && \
273 LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17)
274 devfs_name: DEVICE_NAME,
275# endif
276};
277#endif
278
279
280/**
281 * Initialize module.
282 *
283 * @returns appropritate status code.
284 */
285static int __init VBoxSupDrvInit(void)
286{
287 int rc;
288
289 dprintf(("VBoxDrv::ModuleInit\n"));
290
291#ifdef CONFIG_X86_LOCAL_APIC
292 /*
293 * If an NMI occurs while we are inside the world switcher the macine will crash.
294 * The Linux NMI watchdog generates periodic NMIs increasing a counter which is
295 * compared with another counter increased in the timer interrupt handler. Therefore
296 * we don't allow to setup an NMI watchdog.
297 */
298# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
299 /*
300 * First test: NMI actiated? Works only works with Linux 2.6 -- 2.4 does not export
301 * the nmi_watchdog variable.
302 */
303# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
304 /*
305 * Permanent IO_APIC mode active? No way to handle this!
306 */
307 if (nmi_watchdog == NMI_IO_APIC)
308 {
309 printk(KERN_ERR DEVICE_NAME
310 ": NMI watchdog in IO_APIC mode active -- refused to load the kernel module!\n"
311 DEVICE_NAME
312 ": Please disable the NMI watchdog by specifying 'nmi_watchdog=0' at kernel\n"
313 DEVICE_NAME
314 ": command line.\n");
315 return -EINVAL;
316 }
317
318 /*
319 * See arch/i386/kernel/nmi.c on >= 2.6.19: -1 means it can never enabled again
320 */
321 atomic_set(&nmi_active, -1);
322 printk(KERN_INFO DEVICE_NAME ": Trying to deactivate NMI watchdog permanently...\n");
323
324 /*
325 * Now fall through and see if it actually was enabled before. If so, fail
326 * as we cannot deactivate it cleanly from here.
327 */
328# else /* < 2.6.19 */
329 /*
330 * Older 2.6 kernels: nmi_watchdog is not initalized by default
331 */
332 if (nmi_watchdog != NMI_NONE)
333 goto nmi_activated;
334# endif
335# endif /* >= 2.6.0 */
336
337 /*
338 * Second test: Interrupt generated by performance counter not masked and can
339 * generate an NMI. Works also with Linux 2.4.
340 */
341 {
342 unsigned int v, ver, maxlvt;
343
344 v = apic_read(APIC_LVR);
345 ver = GET_APIC_VERSION(v);
346 /* 82489DXs do not report # of LVT entries. */
347 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
348 if (maxlvt >= 4)
349 {
350 /* Read status of performance counter IRQ vector */
351 v = apic_read(APIC_LVTPC);
352
353 /* performance counter generates NMI and is not masked? */
354 if ((GET_APIC_DELIVERY_MODE(v) == APIC_MODE_NMI) && !(v & APIC_LVT_MASKED))
355 {
356# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
357 printk(KERN_ERR DEVICE_NAME
358 ": NMI watchdog either active or at least initialized. Please disable the NMI\n"
359 DEVICE_NAME
360 ": watchdog by specifying 'nmi_watchdog=0' at kernel command line.\n");
361 return -EINVAL;
362# else /* < 2.6.19 */
363# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
364nmi_activated:
365# endif
366 printk(KERN_ERR DEVICE_NAME
367 ": NMI watchdog active -- refused to load the kernel module! Please disable\n"
368 DEVICE_NAME
369 ": the NMI watchdog by specifying 'nmi_watchdog=0' at kernel command line.\n");
370 return -EINVAL;
371# endif /* >= 2.6.19 */
372 }
373 }
374 }
375# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
376 printk(KERN_INFO DEVICE_NAME ": Successfully done.\n");
377# endif /* >= 2.6.19 */
378#endif /* CONFIG_X86_LOCAL_APIC */
379
380#ifdef CONFIG_VBOXDRV_AS_MISC
381 rc = misc_register(&gMiscDevice);
382 if (rc)
383 {
384 printk(KERN_ERR DEVICE_NAME ": Can't register misc device! rc=%d\n", rc);
385 return rc;
386 }
387#else /* !CONFIG_VBOXDRV_AS_MISC */
388 /*
389 * Register character device.
390 */
391 g_iModuleMajor = DEVICE_MAJOR;
392 rc = VBOX_REGISTER_DEVICE((dev_t)g_iModuleMajor, DEVICE_NAME, &gFileOpsVBoxDrv);
393 if (rc < 0)
394 {
395 dprintf(("VBOX_REGISTER_DEVICE failed with rc=%#x!\n", rc));
396 return rc;
397 }
398
399 /*
400 * Save returned module major number
401 */
402 if (DEVICE_MAJOR != 0)
403 g_iModuleMajor = DEVICE_MAJOR;
404 else
405 g_iModuleMajor = rc;
406 rc = 0;
407
408#ifdef CONFIG_DEVFS_FS
409 /*
410 * Register a device entry
411 */
412 g_hDevFsVBoxDrv = VBOX_REGISTER_DEVFS();
413 if (g_hDevFsVBoxDrv == NULL)
414 {
415 dprintf(("devfs_register failed!\n"));
416 rc = -EINVAL;
417 }
418#endif
419#endif /* !CONFIG_VBOXDRV_AS_MISC */
420 if (!rc)
421 {
422 /*
423 * Initialize the runtime.
424 * On AMD64 we'll have to donate the high rwx memory block to the exec allocator.
425 */
426 rc = RTR0Init(0);
427 if (RT_SUCCESS(rc))
428 {
429#ifdef __AMD64__
430 rc = RTR0MemExecDonate(&g_abExecMemory[0], sizeof(g_abExecMemory));
431#endif
432 /*
433 * Initialize the device extension.
434 */
435 if (RT_SUCCESS(rc))
436 rc = supdrvInitDevExt(&g_DevExt);
437 if (!rc)
438 {
439 /*
440 * Create the GIP page.
441 */
442 rc = VBoxSupDrvInitGip(&g_DevExt);
443 if (!rc)
444 {
445 dprintf(("VBoxDrv::ModuleInit returning %#x\n", rc));
446 return rc;
447 }
448
449 supdrvDeleteDevExt(&g_DevExt);
450 }
451 else
452 rc = -EINVAL;
453 RTR0Term();
454 }
455 else
456 rc = -EINVAL;
457
458 /*
459 * Failed, cleanup and return the error code.
460 */
461#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
462 VBOX_UNREGISTER_DEVFS(g_hDevFsVBoxDrv);
463#endif
464 }
465#ifdef CONFIG_VBOXDRV_AS_MISC
466 misc_deregister(&gMiscDevice);
467 dprintf(("VBoxDrv::ModuleInit returning %#x (minor:%d)\n", rc, gMiscDevice.minor));
468#else
469 VBOX_UNREGISTER_DEVICE(g_iModuleMajor, DEVICE_NAME);
470 dprintf(("VBoxDrv::ModuleInit returning %#x (major:%d)\n", rc, g_iModuleMajor));
471#endif
472 return rc;
473}
474
475
476/**
477 * Unload the module.
478 */
479static void __exit VBoxSupDrvUnload(void)
480{
481 int rc;
482 dprintf(("VBoxSupDrvUnload\n"));
483
484 /*
485 * I Don't think it's possible to unload a driver which processes have
486 * opened, at least we'll blindly assume that here.
487 */
488#ifdef CONFIG_VBOXDRV_AS_MISC
489 rc = misc_deregister(&gMiscDevice);
490 if (rc < 0)
491 {
492 dprintf(("misc_deregister failed with rc=%#x\n", rc));
493 }
494#else /* !CONFIG_VBOXDRV_AS_MISC */
495#ifdef CONFIG_DEVFS_FS
496 /*
497 * Unregister a device entry
498 */
499 VBOX_UNREGISTER_DEVFS(g_hDevFsVBoxDrv);
500#endif // devfs
501 rc = VBOX_UNREGISTER_DEVICE(g_iModuleMajor, DEVICE_NAME);
502 if (rc < 0)
503 {
504 dprintf(("unregister_chrdev failed with rc=%#x (major:%d)\n", rc, g_iModuleMajor));
505 }
506#endif /* !CONFIG_VBOXDRV_AS_MISC */
507
508 /*
509 * Destroy GIP, delete the device extension and terminate IPRT.
510 */
511 VBoxSupDrvTermGip(&g_DevExt);
512 supdrvDeleteDevExt(&g_DevExt);
513 RTR0Term();
514}
515
516
517/**
518 * Device open. Called on open /dev/vboxdrv
519 *
520 * @param pInode Pointer to inode info structure.
521 * @param pFilp Associated file pointer.
522 */
523static int VBoxSupDrvCreate(struct inode *pInode, struct file *pFilp)
524{
525 int rc;
526 PSUPDRVSESSION pSession;
527 dprintf(("VBoxSupDrvCreate: pFilp=%p\n", pFilp));
528
529 /*
530 * Call common code for the rest.
531 */
532 rc = supdrvCreateSession(&g_DevExt, (PSUPDRVSESSION *)&pSession);
533 if (!rc)
534 {
535 pSession->Uid = current->euid;
536 pSession->Gid = current->egid;
537 pSession->Process = RTProcSelf();
538 pSession->R0Process = RTR0ProcHandleSelf();
539 }
540
541 dprintf(("VBoxSupDrvCreate: g_DevExt=%p pSession=%p rc=%d\n", &g_DevExt, pSession, rc));
542 pFilp->private_data = pSession;
543
544 return VBoxSupDrvErr2LinuxErr(rc);
545}
546
547
548/**
549 * Close device.
550 *
551 * @param pInode Pointer to inode info structure.
552 * @param pFilp Associated file pointer.
553 */
554static int VBoxSupDrvClose(struct inode *pInode, struct file *pFilp)
555{
556 dprintf(("VBoxSupDrvClose: pFilp=%p private_data=%p\n", pFilp, pFilp->private_data));
557 supdrvCloseSession(&g_DevExt, (PSUPDRVSESSION)pFilp->private_data);
558 pFilp->private_data = NULL;
559 return 0;
560}
561
562
563/**
564 * Device I/O Control entry point.
565 *
566 * @param pInode Pointer to inode info structure.
567 * @param pFilp Associated file pointer.
568 * @param IOCmd The function specified to ioctl().
569 * @param IOArg The argument specified to ioctl().
570 */
571static int VBoxSupDrvDeviceControl(struct inode *pInode, struct file *pFilp,
572 unsigned int IOCmd, unsigned long IOArg)
573{
574 int rc;
575 SUPDRVIOCTLDATA Args;
576 void *pvBuf = NULL;
577 int cbBuf = 0;
578 unsigned cbOut = 0;
579
580 dprintf2(("VBoxSupDrvDeviceControl: pFilp=%p IOCmd=%x IOArg=%p\n", pFilp, IOCmd, (void *)IOArg));
581
582 /*
583 * Copy ioctl data structure from user space.
584 */
585 if (_IOC_SIZE(IOCmd) != sizeof(SUPDRVIOCTLDATA))
586 {
587 dprintf(("VBoxSupDrvDeviceControl: incorrect input length! cbArgs=%d\n", _IOC_SIZE(IOCmd)));
588 return -EINVAL;
589 }
590 if (copy_from_user(&Args, (void *)IOArg, _IOC_SIZE(IOCmd)))
591 {
592 dprintf(("VBoxSupDrvDeviceControl: copy_from_user(&Args) failed.\n"));
593 return -EFAULT;
594 }
595
596 /*
597 * Allocate and copy user space input data buffer to kernel space.
598 */
599 if (Args.cbIn > 0 || Args.cbOut > 0)
600 {
601 cbBuf = max(Args.cbIn, Args.cbOut);
602 pvBuf = vmalloc(cbBuf);
603 if (pvBuf == NULL)
604 {
605 dprintf(("VBoxSupDrvDeviceControl: failed to allocate buffer of %d bytes.\n", cbBuf));
606 return -ENOMEM;
607 }
608
609 if (copy_from_user(pvBuf, (void *)Args.pvIn, Args.cbIn))
610 {
611 dprintf(("VBoxSupDrvDeviceControl: copy_from_user(pvBuf) failed.\n"));
612 vfree(pvBuf);
613 return -EFAULT;
614 }
615 }
616
617 /*
618 * Process the IOCtl.
619 */
620 rc = supdrvIOCtl(IOCmd, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data,
621 pvBuf, Args.cbIn, pvBuf, Args.cbOut, &cbOut);
622
623 /*
624 * Copy ioctl data and output buffer back to user space.
625 */
626 if (rc)
627 {
628 dprintf(("VBoxSupDrvDeviceControl: pFilp=%p IOCmd=%x IOArg=%p failed, rc=%d (linux rc=%d)\n",
629 pFilp, IOCmd, (void *)IOArg, rc, VBoxSupDrvErr2LinuxErr(rc)));
630 rc = VBoxSupDrvErr2LinuxErr(rc);
631 }
632 else if (cbOut > 0)
633 {
634 if (pvBuf != NULL && cbOut <= cbBuf)
635 {
636 if (copy_to_user((void *)Args.pvOut, pvBuf, cbOut))
637 {
638 dprintf(("copy_to_user failed.\n"));
639 rc = -EFAULT;
640 }
641 }
642 else
643 {
644 dprintf(("WHAT!?! supdrvIOCtl messed up! cbOut=%d cbBuf=%d pvBuf=%p\n", cbOut, cbBuf, pvBuf));
645 rc = -EPERM;
646 }
647 }
648
649 if (pvBuf)
650 vfree(pvBuf);
651
652 dprintf2(("VBoxSupDrvDeviceControl: returns %d\n", rc));
653 return rc;
654}
655
656
657/**
658 * Initializes any OS specific object creator fields.
659 */
660void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
661{
662 NOREF(pObj);
663 NOREF(pSession);
664}
665
666
667/**
668 * Checks if the session can access the object.
669 *
670 * @returns true if a decision has been made.
671 * @returns false if the default access policy should be applied.
672 *
673 * @param pObj The object in question.
674 * @param pSession The session wanting to access the object.
675 * @param pszObjName The object name, can be NULL.
676 * @param prc Where to store the result when returning true.
677 */
678bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
679{
680 NOREF(pObj);
681 NOREF(pSession);
682 NOREF(pszObjName);
683 NOREF(prc);
684 return false;
685}
686
687
688/**
689 * Compute order. Some functions allocate 2^order pages.
690 *
691 * @returns order.
692 * @param cPages Number of pages.
693 */
694static int VBoxSupDrvOrder(unsigned long cPages)
695{
696 int iOrder;
697 unsigned long cTmp;
698
699 for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
700 ;
701 if (cPages & ~(1 << iOrder))
702 ++iOrder;
703
704 return iOrder;
705}
706
707
708/**
709 * OS Specific code for locking down memory.
710 *
711 * @returns 0 on success.
712 * @returns SUPDRV_ERR_* on failure.
713 * @param pMem Pointer to memory.
714 * This is not linked in anywhere.
715 * @param paPages Array which should be filled with the address of the physical pages.
716 *
717 * @remark See sgl_map_user_pages() for an example of an similar function.
718 */
719int VBOXCALL supdrvOSLockMemOne(PSUPDRVMEMREF pMem, PSUPPAGE paPages)
720{
721 int rc;
722 struct page **papPages;
723 unsigned iPage;
724 unsigned cPages = pMem->cb >> PAGE_SHIFT;
725 unsigned long pv = (unsigned long)pMem->pvR3;
726 struct vm_area_struct **papVMAs;
727
728 /*
729 * Allocate page pointer array.
730 */
731 papPages = vmalloc(cPages * sizeof(*papPages));
732 if (!papPages)
733 return SUPDRV_ERR_NO_MEMORY;
734
735 /*
736 * Allocate the VMA pointer array.
737 */
738 papVMAs = vmalloc(cPages * sizeof(*papVMAs));
739 if (!papVMAs)
740 return SUPDRV_ERR_NO_MEMORY;
741
742 /*
743 * Get user pages.
744 */
745 down_read(&current->mm->mmap_sem);
746 rc = get_user_pages(current, /* Task for fault acounting. */
747 current->mm, /* Whose pages. */
748 (unsigned long)pv, /* Where from. */
749 cPages, /* How many pages. */
750 1, /* Write to memory. */
751 0, /* force. */
752 papPages, /* Page array. */
753 papVMAs); /* vmas */
754 if (rc != cPages)
755 {
756 up_read(&current->mm->mmap_sem);
757 dprintf(("supdrvOSLockMemOne: get_user_pages failed. rc=%d\n", rc));
758 return SUPDRV_ERR_LOCK_FAILED;
759 }
760
761 for (iPage = 0; iPage < cPages; iPage++)
762 flush_dcache_page(papPages[iPage]);
763 up_read(&current->mm->mmap_sem);
764
765 pMem->u.locked.papPages = papPages;
766 pMem->u.locked.cPages = cPages;
767
768 /*
769 * Get addresses, protect against fork()
770 */
771 for (iPage = 0; iPage < cPages; iPage++)
772 {
773 paPages[iPage].Phys = page_to_phys(papPages[iPage]);
774 paPages[iPage].uReserved = 0;
775 papVMAs[iPage]->vm_flags |= VM_DONTCOPY;
776 }
777
778 vfree(papVMAs);
779
780 dprintf2(("supdrvOSLockMemOne: pvR3=%p cb=%d papPages=%p\n",
781 pMem->pvR3, pMem->cb, pMem->u.locked.papPages));
782 return 0;
783}
784
785
786/**
787 * Unlocks the memory pointed to by pv.
788 *
789 * @param pv Memory to unlock.
790 * @param cb Size of the memory (debug).
791 *
792 * @remark See sgl_unmap_user_pages() for an example of an similar function.
793 */
794void VBOXCALL supdrvOSUnlockMemOne(PSUPDRVMEMREF pMem)
795{
796 unsigned iPage;
797 dprintf2(("supdrvOSUnlockMemOne: pvR3=%p cb=%d papPages=%p\n",
798 pMem->pvR3, pMem->cb, pMem->u.locked.papPages));
799
800 /*
801 * Loop thru the pages and release them.
802 */
803 for (iPage = 0; iPage < pMem->u.locked.cPages; iPage++)
804 {
805 if (!PageReserved(pMem->u.locked.papPages[iPage]))
806 SetPageDirty(pMem->u.locked.papPages[iPage]);
807 page_cache_release(pMem->u.locked.papPages[iPage]);
808 }
809
810 /* free the page array */
811 vfree(pMem->u.locked.papPages);
812 pMem->u.locked.cPages = 0;
813}
814
815
816/**
817 * OS Specific code for allocating page aligned memory with continuous fixed
818 * physical paged backing.
819 *
820 * @returns 0 on success.
821 * @returns SUPDRV_ERR_* on failure.
822 * @param pMem Memory reference record of the memory to be allocated.
823 * (This is not linked in anywhere.)
824 * @param ppvR0 Where to store the virtual address of the ring-0 mapping. (optional)
825 * @param ppvR3 Where to store the virtual address of the ring-3 mapping.
826 * @param pHCPhys Where to store the physical address.
827 */
828int VBOXCALL supdrvOSContAllocOne(PSUPDRVMEMREF pMem, void **ppvR0, void **ppvR3, PRTHCPHYS pHCPhys)
829{
830 struct page *paPages;
831 unsigned iPage;
832 unsigned cbAligned = RT_ALIGN(pMem->cb, PAGE_SIZE);
833 unsigned cPages = cbAligned >> PAGE_SHIFT;
834 unsigned cOrder = VBoxSupDrvOrder(cPages);
835 unsigned long ulAddr;
836 dma_addr_t HCPhys;
837 int rc = 0;
838 pgprot_t pgFlags;
839 pgprot_val(pgFlags) = _PAGE_PRESENT | _PAGE_RW | _PAGE_USER;
840
841 Assert(ppvR3);
842 Assert(pHCPhys);
843
844 /*
845 * Allocate page pointer array.
846 */
847#ifdef __AMD64__ /** @todo check out if there is a correct way of getting memory below 4GB (physically). */
848 paPages = alloc_pages(GFP_DMA, cOrder);
849#else
850 paPages = alloc_pages(GFP_USER, cOrder);
851#endif
852 if (!paPages)
853 return SUPDRV_ERR_NO_MEMORY;
854
855 /*
856 * Lock the pages.
857 */
858 for (iPage = 0; iPage < cPages; iPage++)
859 {
860 SetPageReserved(&paPages[iPage]);
861 if (!PageHighMem(&paPages[iPage]) && pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
862 MY_CHANGE_PAGE_ATTR(&paPages[iPage], 1, MY_PAGE_KERNEL_EXEC);
863#ifdef DEBUG
864 if (iPage + 1 < cPages && (page_to_phys((&paPages[iPage])) + 0x1000) != page_to_phys((&paPages[iPage + 1])))
865 {
866 dprintf(("supdrvOSContAllocOne: Pages are not continuous!!!! iPage=%d phys=%llx physnext=%llx\n",
867 iPage, (long long)page_to_phys((&paPages[iPage])), (long long)page_to_phys((&paPages[iPage + 1]))));
868 BUG();
869 }
870#endif
871 }
872 HCPhys = page_to_phys(paPages);
873
874 /*
875 * Allocate user space mapping and put the physical pages into it.
876 */
877 down_write(&current->mm->mmap_sem);
878 ulAddr = do_mmap(NULL, 0, cbAligned, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_ANONYMOUS, 0);
879 if (!(ulAddr & ~PAGE_MASK))
880 {
881#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && !defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
882 int rc2 = remap_page_range(ulAddr, HCPhys, cbAligned, pgFlags);
883#else
884 int rc2 = 0;
885 struct vm_area_struct *vma = find_vma(current->mm, ulAddr);
886 if (vma)
887#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
888 rc2 = remap_page_range(vma, ulAddr, HCPhys, cbAligned, pgFlags);
889#else
890 rc2 = remap_pfn_range(vma, ulAddr, HCPhys >> PAGE_SHIFT, cbAligned, pgFlags);
891#endif
892 else
893 {
894 rc = SUPDRV_ERR_NO_MEMORY;
895 dprintf(("supdrvOSContAllocOne: no vma found for ulAddr=%#lx!\n", ulAddr));
896 }
897#endif
898 if (rc2)
899 {
900 rc = SUPDRV_ERR_NO_MEMORY;
901 dprintf(("supdrvOSContAllocOne: remap_page_range failed rc2=%d\n", rc2));
902 }
903 }
904 else
905 {
906 dprintf(("supdrvOSContAllocOne: do_mmap failed ulAddr=%#lx\n", ulAddr));
907 rc = SUPDRV_ERR_NO_MEMORY;
908 }
909 up_write(&current->mm->mmap_sem); /* not quite sure when to give this up. */
910
911 /*
912 * Success?
913 */
914 if (!rc)
915 {
916 *pHCPhys = HCPhys;
917 *ppvR3 = (void *)ulAddr;
918 if (ppvR0)
919 *ppvR0 = (void *)ulAddr;
920 pMem->pvR3 = (void *)ulAddr;
921 pMem->pvR0 = NULL;
922 pMem->u.cont.paPages = paPages;
923 pMem->u.cont.cPages = cPages;
924 pMem->cb = cbAligned;
925
926 dprintf2(("supdrvOSContAllocOne: pvR0=%p pvR3=%p cb=%d paPages=%p *pHCPhys=%lx *ppvR0=*ppvR3=%p\n",
927 pMem->pvR0, pMem->pvR3, pMem->cb, paPages, (unsigned long)*pHCPhys, *ppvR3));
928 global_flush_tlb();
929 return 0;
930 }
931
932 /*
933 * Failure, cleanup and be gone.
934 */
935 down_write(&current->mm->mmap_sem);
936 if (ulAddr & ~PAGE_MASK)
937 MY_DO_MUNMAP(current->mm, ulAddr, pMem->cb);
938 for (iPage = 0; iPage < cPages; iPage++)
939 {
940 ClearPageReserved(&paPages[iPage]);
941 if (!PageHighMem(&paPages[iPage]) && pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
942 MY_CHANGE_PAGE_ATTR(&paPages[iPage], 1, PAGE_KERNEL);
943 }
944 up_write(&current->mm->mmap_sem); /* check when we can leave this. */
945 __free_pages(paPages, cOrder);
946
947 global_flush_tlb();
948 return rc;
949}
950
951
952/**
953 * Frees contiguous memory.
954 *
955 * @param pMem Memory reference record of the memory to be freed.
956 */
957void VBOXCALL supdrvOSContFreeOne(PSUPDRVMEMREF pMem)
958{
959 unsigned iPage;
960
961 dprintf2(("supdrvOSContFreeOne: pvR0=%p pvR3=%p cb=%d paPages=%p\n",
962 pMem->pvR0, pMem->pvR3, pMem->cb, pMem->u.cont.paPages));
963
964 /*
965 * do_exit() destroys the mm before closing files.
966 * I really hope it cleans up our stuff properly...
967 */
968 if (current->mm)
969 {
970 down_write(&current->mm->mmap_sem);
971 MY_DO_MUNMAP(current->mm, (unsigned long)pMem->pvR3, pMem->cb);
972 up_write(&current->mm->mmap_sem); /* check when we can leave this. */
973 }
974
975 /*
976 * Change page attributes freeing the pages.
977 */
978 for (iPage = 0; iPage < pMem->u.cont.cPages; iPage++)
979 {
980 ClearPageReserved(&pMem->u.cont.paPages[iPage]);
981 if (!PageHighMem(&pMem->u.cont.paPages[iPage]) && pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
982 MY_CHANGE_PAGE_ATTR(&pMem->u.cont.paPages[iPage], 1, PAGE_KERNEL);
983 }
984 __free_pages(pMem->u.cont.paPages, VBoxSupDrvOrder(pMem->u.cont.cPages));
985
986 pMem->u.cont.cPages = 0;
987}
988
989
990/**
991 * Allocates memory which mapped into both kernel and user space.
992 * The returned memory is page aligned and so is the allocation.
993 *
994 * @returns 0 on success.
995 * @returns SUPDRV_ERR_* on failure.
996 * @param pMem Memory reference record of the memory to be allocated.
997 * (This is not linked in anywhere.)
998 * @param ppvR0 Where to store the address of the Ring-0 mapping.
999 * @param ppvR3 Where to store the address of the Ring-3 mapping.
1000 */
1001int VBOXCALL supdrvOSMemAllocOne(PSUPDRVMEMREF pMem, void **ppvR0, void **ppvR3)
1002{
1003 const unsigned cbAligned = RT_ALIGN(pMem->cb, PAGE_SIZE);
1004 const unsigned cPages = cbAligned >> PAGE_SHIFT;
1005#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 22)
1006 unsigned cOrder = VBoxSupDrvOrder(cPages);
1007 struct page *paPages;
1008#endif
1009 struct page **papPages;
1010 unsigned iPage;
1011 pgprot_t pgFlags;
1012 pgprot_val(pgFlags) = _PAGE_PRESENT | _PAGE_RW | _PAGE_USER;
1013
1014 /*
1015 * Allocate array with page pointers.
1016 */
1017 pMem->u.mem.cPages = 0;
1018 pMem->u.mem.papPages = papPages = kmalloc(sizeof(papPages[0]) * cPages, GFP_KERNEL);
1019 if (!papPages)
1020 return SUPDRV_ERR_NO_MEMORY;
1021
1022 /*
1023 * Allocate the pages.
1024 */
1025#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1026 for (iPage = 0; iPage < cPages; iPage++)
1027 {
1028 papPages[iPage] = alloc_page(GFP_HIGHUSER);
1029 if (!papPages[iPage])
1030 {
1031 pMem->u.mem.cPages = iPage;
1032 supdrvOSMemFreeOne(pMem);
1033 return SUPDRV_ERR_NO_MEMORY;
1034 }
1035 }
1036
1037#else /* < 2.4.22 */
1038 paPages = alloc_pages(GFP_USER, cOrder);
1039 if (!paPages)
1040 {
1041 supdrvOSMemFreeOne(pMem);
1042 return SUPDRV_ERR_NO_MEMORY;
1043 }
1044 for (iPage = 0; iPage < cPages; iPage++)
1045 {
1046 papPages[iPage] = &paPages[iPage];
1047 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
1048 MY_CHANGE_PAGE_ATTR(papPages[iPage], 1, MY_PAGE_KERNEL_EXEC);
1049 if (PageHighMem(papPages[iPage]))
1050 BUG();
1051 }
1052#endif
1053 pMem->u.mem.cPages = cPages;
1054
1055 /*
1056 * Reserve the pages.
1057 */
1058 for (iPage = 0; iPage < cPages; iPage++)
1059 SetPageReserved(papPages[iPage]);
1060
1061 /*
1062 * Create the Ring-0 mapping.
1063 */
1064 if (ppvR0)
1065 {
1066#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1067# ifdef VM_MAP
1068 *ppvR0 = pMem->pvR0 = vmap(papPages, cPages, VM_MAP, pgFlags);
1069# else
1070 *ppvR0 = pMem->pvR0 = vmap(papPages, cPages, VM_ALLOC, pgFlags);
1071# endif
1072#else
1073 *ppvR0 = pMem->pvR0 = phys_to_virt(page_to_phys(papPages[0]));
1074#endif
1075 }
1076 if (pMem->pvR0 || !ppvR0)
1077 {
1078 /*
1079 * Create the ring3 mapping.
1080 */
1081 if (ppvR3)
1082 *ppvR3 = pMem->pvR3 = VBoxSupDrvMapUser(papPages, cPages, PROT_READ | PROT_WRITE | PROT_EXEC, pgFlags);
1083 if (pMem->pvR3 || !ppvR3)
1084 return 0;
1085 dprintf(("supdrvOSMemAllocOne: failed to map into r3! cPages=%u\n", cPages));
1086 }
1087 else
1088 dprintf(("supdrvOSMemAllocOne: failed to map into r0! cPages=%u\n", cPages));
1089
1090 supdrvOSMemFreeOne(pMem);
1091 return SUPDRV_ERR_NO_MEMORY;
1092}
1093
1094
1095/**
1096 * Get the physical addresses of the pages in the allocation.
1097 * This is called while inside bundle the spinlock.
1098 *
1099 * @param pMem Memory reference record of the memory.
1100 * @param paPages Where to store the page addresses.
1101 */
1102void VBOXCALL supdrvOSMemGetPages(PSUPDRVMEMREF pMem, PSUPPAGE paPages)
1103{
1104 unsigned iPage;
1105 for (iPage = 0; iPage < pMem->u.mem.cPages; iPage++)
1106 {
1107 paPages[iPage].Phys = page_to_phys(pMem->u.mem.papPages[iPage]);
1108 paPages[iPage].uReserved = 0;
1109 }
1110}
1111
1112
1113/**
1114 * Frees memory allocated by supdrvOSMemAllocOne().
1115 *
1116 * @param pMem Memory reference record of the memory to be free.
1117 */
1118void VBOXCALL supdrvOSMemFreeOne(PSUPDRVMEMREF pMem)
1119{
1120 dprintf2(("supdrvOSMemFreeOne: pvR0=%p pvR3=%p cb=%d cPages=%d papPages=%p\n",
1121 pMem->pvR0, pMem->pvR3, pMem->cb, pMem->u.mem.cPages, pMem->u.mem.papPages));
1122
1123 /*
1124 * Unmap the user mapping (if any).
1125 * do_exit() destroys the mm before closing files.
1126 */
1127 if (pMem->pvR3 && current->mm)
1128 {
1129 down_write(&current->mm->mmap_sem);
1130 MY_DO_MUNMAP(current->mm, (unsigned long)pMem->pvR3, RT_ALIGN(pMem->cb, PAGE_SIZE));
1131 up_write(&current->mm->mmap_sem); /* check when we can leave this. */
1132 }
1133 pMem->pvR3 = NULL;
1134
1135 /*
1136 * Unmap the kernel mapping (if any).
1137 */
1138 if (pMem->pvR0)
1139 {
1140#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1141 vunmap(pMem->pvR0);
1142#endif
1143 pMem->pvR0 = NULL;
1144 }
1145
1146 /*
1147 * Free the physical pages.
1148 */
1149 if (pMem->u.mem.papPages)
1150 {
1151 struct page **papPages = pMem->u.mem.papPages;
1152 const unsigned cPages = pMem->u.mem.cPages;
1153 unsigned iPage;
1154
1155 /* Restore the page flags. */
1156 for (iPage = 0; iPage < cPages; iPage++)
1157 {
1158 ClearPageReserved(papPages[iPage]);
1159#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 22)
1160 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
1161 MY_CHANGE_PAGE_ATTR(papPages[iPage], 1, PAGE_KERNEL);
1162#endif
1163 }
1164
1165 /* Free the pages. */
1166#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1167 for (iPage = 0; iPage < pMem->u.cont.cPages; iPage++)
1168 __free_page(papPages[iPage]);
1169#else
1170 if (cPages > 0)
1171 __free_pages(papPages[0], VBoxSupDrvOrder(cPages));
1172#endif
1173 /* Free the page pointer array. */
1174 kfree(papPages);
1175 pMem->u.mem.papPages = NULL;
1176 }
1177 pMem->u.mem.cPages = 0;
1178}
1179
1180
1181/**
1182 * Maps a range of pages into user space.
1183 *
1184 * @returns Pointer to the user space mapping on success.
1185 * @returns NULL on failure.
1186 * @param papPages Array of the pages to map.
1187 * @param cPages Number of pages to map.
1188 * @param fProt The mapping protection.
1189 * @param pgFlags The page level protection.
1190 */
1191static void *VBoxSupDrvMapUser(struct page **papPages, unsigned cPages, unsigned fProt, pgprot_t pgFlags)
1192{
1193 int rc = SUPDRV_ERR_NO_MEMORY;
1194 unsigned long ulAddr;
1195
1196 /*
1197 * Allocate user space mapping.
1198 */
1199 down_write(&current->mm->mmap_sem);
1200 ulAddr = do_mmap(NULL, 0, cPages * PAGE_SIZE, fProt, MAP_SHARED | MAP_ANONYMOUS, 0);
1201 if (!(ulAddr & ~PAGE_MASK))
1202 {
1203 /*
1204 * Map page by page into the mmap area.
1205 * This is generic, paranoid and not very efficient.
1206 */
1207 int rc = 0;
1208 unsigned long ulAddrCur = ulAddr;
1209 unsigned iPage;
1210 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
1211 {
1212#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1213 struct vm_area_struct *vma = find_vma(current->mm, ulAddrCur);
1214 if (!vma)
1215 break;
1216#endif
1217
1218#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1219 rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(papPages[iPage]), PAGE_SIZE, pgFlags);
1220#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1221 rc = remap_page_range(vma, ulAddrCur, page_to_phys(papPages[iPage]), PAGE_SIZE, pgFlags);
1222#else /* 2.4 */
1223 rc = remap_page_range(ulAddrCur, page_to_phys(papPages[iPage]), PAGE_SIZE, pgFlags);
1224#endif
1225 if (rc)
1226 break;
1227 }
1228
1229 /*
1230 * Successful?
1231 */
1232 if (iPage >= cPages)
1233 {
1234 up_write(&current->mm->mmap_sem);
1235 return (void *)ulAddr;
1236 }
1237
1238 /* no, cleanup! */
1239 if (rc)
1240 dprintf(("VBoxSupDrvMapUser: remap_[page|pfn]_range failed! rc=%d\n", rc));
1241 else
1242 dprintf(("VBoxSupDrvMapUser: find_vma failed!\n"));
1243
1244 MY_DO_MUNMAP(current->mm, ulAddr, cPages * PAGE_SIZE);
1245 }
1246 else
1247 {
1248 dprintf(("supdrvOSContAllocOne: do_mmap failed ulAddr=%#lx\n", ulAddr));
1249 rc = SUPDRV_ERR_NO_MEMORY;
1250 }
1251 up_write(&current->mm->mmap_sem);
1252
1253 return NULL;
1254}
1255
1256
1257/**
1258 * Initializes the GIP.
1259 *
1260 * @returns negative errno.
1261 * @param pDevExt Instance data. GIP stuff may be updated.
1262 */
1263static int VBoxSupDrvInitGip(PSUPDRVDEVEXT pDevExt)
1264{
1265 struct page *pPage;
1266 dma_addr_t HCPhys;
1267 PSUPGLOBALINFOPAGE pGip;
1268 dprintf(("VBoxSupDrvInitGip:\n"));
1269
1270 /*
1271 * Allocate the page.
1272 */
1273 pPage = alloc_pages(GFP_USER, 0);
1274 if (!pPage)
1275 {
1276 dprintf(("VBoxSupDrvInitGip: failed to allocate the GIP page\n"));
1277 return -ENOMEM;
1278 }
1279
1280 /*
1281 * Lock the page.
1282 */
1283 SetPageReserved(pPage);
1284 g_pGipPage = pPage;
1285
1286 /*
1287 * Call common initialization routine.
1288 */
1289 HCPhys = page_to_phys(pPage);
1290 pGip = (PSUPGLOBALINFOPAGE)page_address(pPage);
1291 pDevExt->ulLastJiffies = jiffies;
1292#ifdef TICK_NSEC
1293 pDevExt->u64LastMonotime = (uint64_t)pDevExt->ulLastJiffies * TICK_NSEC;
1294 dprintf(("VBoxSupDrvInitGIP: TICK_NSEC=%ld HZ=%d jiffies=%ld now=%lld\n",
1295 TICK_NSEC, HZ, pDevExt->ulLastJiffies, pDevExt->u64LastMonotime));
1296#else
1297 pDevExt->u64LastMonotime = (uint64_t)pDevExt->ulLastJiffies * (1000000 / HZ);
1298 dprintf(("VBoxSupDrvInitGIP: TICK_NSEC=%d HZ=%d jiffies=%ld now=%lld\n",
1299 (int)(1000000 / HZ), HZ, pDevExt->ulLastJiffies, pDevExt->u64LastMonotime));
1300#endif
1301 supdrvGipInit(pDevExt, pGip, HCPhys, pDevExt->u64LastMonotime,
1302 HZ <= 1000 ? HZ : 1000);
1303
1304 /*
1305 * Initialize the timer.
1306 */
1307 init_timer(&g_GipTimer);
1308 g_GipTimer.data = (unsigned long)pDevExt;
1309 g_GipTimer.function = VBoxSupGipTimer;
1310 g_GipTimer.expires = jiffies;
1311
1312 return 0;
1313}
1314
1315
1316/**
1317 * Terminates the GIP.
1318 *
1319 * @returns negative errno.
1320 * @param pDevExt Instance data. GIP stuff may be updated.
1321 */
1322static int VBoxSupDrvTermGip(PSUPDRVDEVEXT pDevExt)
1323{
1324 struct page *pPage;
1325 PSUPGLOBALINFOPAGE pGip;
1326 dprintf(("VBoxSupDrvTermGip:\n"));
1327
1328 /*
1329 * Delete the timer if it's pending.
1330 */
1331 if (timer_pending(&g_GipTimer))
1332 del_timer(&g_GipTimer);
1333
1334 /*
1335 * Uninitialize the content.
1336 */
1337 pGip = pDevExt->pGip;
1338 pDevExt->pGip = NULL;
1339 if (pGip)
1340 supdrvGipTerm(pGip);
1341
1342 /*
1343 * Free the page.
1344 */
1345 pPage = g_pGipPage;
1346 g_pGipPage = NULL;
1347 if (pPage)
1348 {
1349 ClearPageReserved(pPage);
1350 __free_pages(pPage, 0);
1351 }
1352
1353 return 0;
1354}
1355
1356/**
1357 * Timer callback function.
1358 * The ulUser parameter is the device extension pointer.
1359 */
1360static void VBoxSupGipTimer(unsigned long ulUser)
1361{
1362 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)ulUser;
1363 unsigned long ulNow = jiffies;
1364 unsigned long ulDiff = ulNow - pDevExt->ulLastJiffies;
1365 pDevExt->ulLastJiffies = ulNow;
1366#ifdef TICK_NSEC
1367 pDevExt->u64LastMonotime += ulDiff * TICK_NSEC;
1368#else
1369 pDevExt->u64LastMonotime += ulDiff * (1000000 / HZ);
1370#endif
1371 supdrvGipUpdate(pDevExt->pGip, pDevExt->u64LastMonotime);
1372 mod_timer(&g_GipTimer, jiffies + (HZ <= 1000 ? 0 : ONE_MSEC_IN_JIFFIES));
1373}
1374
1375
1376/**
1377 * Maps the GIP into user space.
1378 *
1379 * @returns negative errno.
1380 * @param pDevExt Instance data.
1381 */
1382int VBOXCALL supdrvOSGipMap(PSUPDRVDEVEXT pDevExt, PCSUPGLOBALINFOPAGE *ppGip)
1383{
1384 int rc = 0;
1385 unsigned long ulAddr;
1386 unsigned long HCPhys = pDevExt->HCPhysGip;
1387 pgprot_t pgFlags;
1388 pgprot_val(pgFlags) = _PAGE_PRESENT | _PAGE_USER;
1389 dprintf2(("supdrvOSGipMap: ppGip=%p\n", ppGip));
1390
1391 /*
1392 * Allocate user space mapping and put the physical pages into it.
1393 */
1394 down_write(&current->mm->mmap_sem);
1395 ulAddr = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ, MAP_SHARED | MAP_ANONYMOUS, 0);
1396 if (!(ulAddr & ~PAGE_MASK))
1397 {
1398#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && !defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1399 int rc2 = remap_page_range(ulAddr, HCPhys, PAGE_SIZE, pgFlags);
1400#else
1401 int rc2 = 0;
1402 struct vm_area_struct *vma = find_vma(current->mm, ulAddr);
1403 if (vma)
1404#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
1405 rc2 = remap_page_range(vma, ulAddr, HCPhys, PAGE_SIZE, pgFlags);
1406#else
1407 rc2 = remap_pfn_range(vma, ulAddr, HCPhys >> PAGE_SHIFT, PAGE_SIZE, pgFlags);
1408#endif
1409 else
1410 {
1411 rc = SUPDRV_ERR_NO_MEMORY;
1412 dprintf(("supdrvOSGipMap: no vma found for ulAddr=%#lx!\n", ulAddr));
1413 }
1414#endif
1415 if (rc2)
1416 {
1417 rc = SUPDRV_ERR_NO_MEMORY;
1418 dprintf(("supdrvOSGipMap: remap_page_range failed rc2=%d\n", rc2));
1419 }
1420 }
1421 else
1422 {
1423 dprintf(("supdrvOSGipMap: do_mmap failed ulAddr=%#lx\n", ulAddr));
1424 rc = SUPDRV_ERR_NO_MEMORY;
1425 }
1426 up_write(&current->mm->mmap_sem); /* not quite sure when to give this up. */
1427
1428 /*
1429 * Success?
1430 */
1431 if (!rc)
1432 {
1433 *ppGip = (PCSUPGLOBALINFOPAGE)ulAddr;
1434 dprintf2(("supdrvOSGipMap: ppGip=%p\n", *ppGip));
1435 return 0;
1436 }
1437
1438 /*
1439 * Failure, cleanup and be gone.
1440 */
1441 if (ulAddr & ~PAGE_MASK)
1442 {
1443 down_write(&current->mm->mmap_sem);
1444 MY_DO_MUNMAP(current->mm, ulAddr, PAGE_SIZE);
1445 up_write(&current->mm->mmap_sem);
1446 }
1447
1448 dprintf2(("supdrvOSGipMap: returns %d\n", rc));
1449 return rc;
1450}
1451
1452
1453/**
1454 * Maps the GIP into user space.
1455 *
1456 * @returns negative errno.
1457 * @param pDevExt Instance data.
1458 */
1459int VBOXCALL supdrvOSGipUnmap(PSUPDRVDEVEXT pDevExt, PCSUPGLOBALINFOPAGE pGip)
1460{
1461 dprintf2(("supdrvOSGipUnmap: pGip=%p\n", pGip));
1462 if (current->mm)
1463 {
1464 down_write(&current->mm->mmap_sem);
1465 MY_DO_MUNMAP(current->mm, (unsigned long)pGip, PAGE_SIZE);
1466 up_write(&current->mm->mmap_sem);
1467 }
1468 dprintf2(("supdrvOSGipUnmap: returns 0\n"));
1469 return 0;
1470}
1471
1472
1473/**
1474 * Resumes the GIP updating.
1475 *
1476 * @param pDevExt Instance data.
1477 */
1478void VBOXCALL supdrvOSGipResume(PSUPDRVDEVEXT pDevExt)
1479{
1480 dprintf2(("supdrvOSGipResume:\n"));
1481 mod_timer(&g_GipTimer, jiffies);
1482}
1483
1484
1485/**
1486 * Suspends the GIP updating.
1487 *
1488 * @param pDevExt Instance data.
1489 */
1490void VBOXCALL supdrvOSGipSuspend(PSUPDRVDEVEXT pDevExt)
1491{
1492 dprintf2(("supdrvOSGipSuspend:\n"));
1493 if (timer_pending(&g_GipTimer))
1494 del_timer(&g_GipTimer);
1495}
1496
1497
1498/**
1499 * Converts a supdrv error code to an linux error code.
1500 *
1501 * @returns corresponding linux error code.
1502 * @param rc supdrv error code (SUPDRV_ERR_* defines).
1503 */
1504static int VBoxSupDrvErr2LinuxErr(int rc)
1505{
1506 switch (rc)
1507 {
1508 case 0: return 0;
1509 case SUPDRV_ERR_GENERAL_FAILURE: return -EACCES;
1510 case SUPDRV_ERR_INVALID_PARAM: return -EINVAL;
1511 case SUPDRV_ERR_INVALID_MAGIC: return -EILSEQ;
1512 case SUPDRV_ERR_INVALID_HANDLE: return -ENXIO;
1513 case SUPDRV_ERR_INVALID_POINTER: return -EFAULT;
1514 case SUPDRV_ERR_LOCK_FAILED: return -ENOLCK;
1515 case SUPDRV_ERR_ALREADY_LOADED: return -EEXIST;
1516 case SUPDRV_ERR_PERMISSION_DENIED: return -EPERM;
1517 case SUPDRV_ERR_VERSION_MISMATCH: return -ENOSYS;
1518 }
1519
1520 return -EPERM;
1521}
1522
1523
1524RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
1525{
1526#if 1
1527 va_list args;
1528 char szMsg[512];
1529
1530 va_start(args, pszFormat);
1531 vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, args);
1532 szMsg[sizeof(szMsg) - 1] = '\0';
1533 printk("%s", szMsg);
1534 va_end(args);
1535#else
1536 /* forward to printf - needs some more GCC hacking to fix ebp... */
1537 __asm__ __volatile__ ("mov %0, %esp\n\t"
1538 "jmp %1\n\t",
1539 :: "r" ((uintptr_t)&pszFormat - 4),
1540 "m" (printk));
1541#endif
1542 return 0;
1543}
1544
1545
1546/** Runtime assert implementation for Linux Ring-0. */
1547RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1548{
1549 printk("!!Assertion Failed!!\n"
1550 "Expression: %s\n"
1551 "Location : %s(%d) %s\n",
1552 pszExpr, pszFile, uLine, pszFunction);
1553}
1554
1555
1556/** Runtime assert implementation for Linux Ring-0. */
1557RTDECL(void) AssertMsg2(const char *pszFormat, ...)
1558{ /* forwarder. */
1559 va_list ap;
1560 char msg[256];
1561
1562 va_start(ap, pszFormat);
1563 vsnprintf(msg, sizeof(msg) - 1, pszFormat, ap);
1564 msg[sizeof(msg) - 1] = '\0';
1565 printk("%s", msg);
1566 va_end(ap);
1567}
1568
1569
1570/* GCC C++ hack. */
1571unsigned __gxx_personality_v0 = 0xcccccccc;
1572
1573
1574module_init(VBoxSupDrvInit);
1575module_exit(VBoxSupDrvUnload);
1576
1577MODULE_AUTHOR("InnoTek Systemberatung GmbH");
1578MODULE_DESCRIPTION("VirtualBox Support Driver");
1579MODULE_LICENSE("GPL");
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