VirtualBox

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

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

Removed the old MM code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.4 KB
Line 
1/** @file
2 * The VirtualBox Support Driver - Linux hosts.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek 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 * Some lines of code to disable the local APIC on x86_64 machines taken
16 * from a Mandriva patch by Gwenole Beauchesne <[email protected]>.
17 */
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "SUPDRV.h"
23#include "version-generated.h"
24
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/sched.h>
40#include <linux/slab.h>
41#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
42# include <linux/jiffies.h>
43#endif
44#include <asm/mman.h>
45#include <asm/io.h>
46#include <asm/uaccess.h>
47#ifdef CONFIG_DEVFS_FS
48# include <linux/devfs_fs_kernel.h>
49#endif
50#ifdef CONFIG_VBOXDRV_AS_MISC
51# include <linux/miscdevice.h>
52#endif
53#ifdef CONFIG_X86_LOCAL_APIC
54# include <asm/apic.h>
55# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
56# include <asm/nmi.h>
57# endif
58#endif
59
60#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
61# ifndef page_to_pfn
62# define page_to_pfn(page) ((page) - mem_map)
63# endif
64# include <asm/pgtable.h>
65# define global_flush_tlb __flush_tlb_global
66#endif
67
68#include <iprt/mem.h>
69
70
71/* devfs defines */
72#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
73# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
74
75# define VBOX_REGISTER_DEVFS() \
76({ \
77 void *rc = NULL; \
78 if (devfs_mk_cdev(MKDEV(DEVICE_MAJOR, 0), \
79 S_IFCHR | S_IRUGO | S_IWUGO, \
80 DEVICE_NAME) == 0) \
81 rc = (void *)' '; /* return not NULL */ \
82 rc; \
83 })
84
85# define VBOX_UNREGISTER_DEVFS(handle) \
86 devfs_remove(DEVICE_NAME);
87
88# else /* < 2.6.0 */
89
90# define VBOX_REGISTER_DEVFS() \
91 devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, \
92 DEVICE_MAJOR, 0, \
93 S_IFCHR | S_IRUGO | S_IWUGO, \
94 &gFileOpsVBoxDrv, NULL)
95
96# define VBOX_UNREGISTER_DEVFS(handle) \
97 if (handle != NULL) \
98 devfs_unregister(handle)
99
100# endif /* < 2.6.0 */
101#endif /* CONFIG_DEV_FS && !CONFIG_VBOXDEV_AS_MISC */
102
103#ifndef CONFIG_VBOXDRV_AS_MISC
104# if defined(CONFIG_DEVFS_FS) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 0)
105# define VBOX_REGISTER_DEVICE(a,b,c) devfs_register_chrdev(a,b,c)
106# define VBOX_UNREGISTER_DEVICE(a,b) devfs_unregister_chrdev(a,b)
107# else
108# define VBOX_REGISTER_DEVICE(a,b,c) register_chrdev(a,b,c)
109# define VBOX_UNREGISTER_DEVICE(a,b) unregister_chrdev(a,b)
110# endif
111#endif /* !CONFIG_VBOXDRV_AS_MISC */
112
113
114#ifdef CONFIG_X86_HIGH_ENTRY
115# error "CONFIG_X86_HIGH_ENTRY is not supported by VBoxDrv at this time."
116#endif
117
118/*
119 * This sucks soooo badly on x86! Why don't they export __PAGE_KERNEL_EXEC so PAGE_KERNEL_EXEC would be usable?
120 */
121#if defined(RT_ARCH_AMD64)
122# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL_EXEC
123#elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
124# define MY_PAGE_KERNEL_EXEC __pgprot(cpu_has_pge ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC)
125#else
126# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL
127#endif
128
129/*
130 * The redhat hack section.
131 * - The current hacks are for 2.4.21-15.EL only.
132 */
133#ifndef NO_REDHAT_HACKS
134/* accounting. */
135# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
136# ifdef VM_ACCOUNT
137# define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c, 0) /* should it be 1 or 0? */
138# endif
139# endif
140
141/* backported remap_page_range. */
142# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
143# include <asm/tlb.h>
144# ifdef tlb_vma /* probably not good enough... */
145# define HAVE_26_STYLE_REMAP_PAGE_RANGE 1
146# endif
147# endif
148
149# ifndef RT_ARCH_AMD64
150/* In 2.6.9-22.ELsmp we have to call change_page_attr() twice when changing
151 * the page attributes from PAGE_KERNEL to something else, because there appears
152 * to be a bug in one of the many patches that redhat applied.
153 * It should be safe to do this on less buggy linux kernels too. ;-)
154 */
155# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
156 do { \
157 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) \
158 change_page_attr(pPages, cPages, prot); \
159 change_page_attr(pPages, cPages, prot); \
160 } while (0)
161# endif
162#endif /* !NO_REDHAT_HACKS */
163
164
165#ifndef MY_DO_MUNMAP
166# define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c)
167#endif
168
169#ifndef MY_CHANGE_PAGE_ATTR
170# ifdef RT_ARCH_AMD64 /** @todo This is a cheap hack, but it'll get around that 'else BUG();' in __change_page_attr(). */
171# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
172 do { \
173 change_page_attr(pPages, cPages, PAGE_KERNEL_NOCACHE); \
174 change_page_attr(pPages, cPages, prot); \
175 } while (0)
176# else
177# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) change_page_attr(pPages, cPages, prot)
178# endif
179#endif
180
181
182/** @def ONE_MSEC_IN_JIFFIES
183 * The number of jiffies that make up 1 millisecond. This is only actually used
184 * when HZ is > 1000. */
185#if HZ <= 1000
186# define ONE_MSEC_IN_JIFFIES 0
187#elif !(HZ % 1000)
188# define ONE_MSEC_IN_JIFFIES (HZ / 1000)
189#else
190# define ONE_MSEC_IN_JIFFIES ((HZ + 999) / 1000)
191# error "HZ is not a multiple of 1000, the GIP stuff won't work right!"
192#endif
193
194#ifdef CONFIG_X86_LOCAL_APIC
195
196/* If an NMI occurs while we are inside the world switcher the machine will
197 * crash. The Linux NMI watchdog generates periodic NMIs increasing a counter
198 * which is compared with another counter increased in the timer interrupt
199 * handler. We disable the NMI watchdog.
200 *
201 * - Linux >= 2.6.21: The watchdog is disabled by default on i386 and x86_64.
202 * - Linux < 2.6.21: The watchdog is normally enabled by default on x86_64
203 * and disabled on i386.
204 */
205# if defined(RT_ARCH_AMD64)
206# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21)
207# define DO_DISABLE_NMI 1
208# endif
209# endif
210
211# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
212extern int nmi_active;
213# define nmi_atomic_read(P) *(P)
214# define nmi_atomic_set(P, V) *(P) = (V)
215# define nmi_atomic_dec(P) nmi_atomic_set(P, 0)
216# else
217# define nmi_atomic_read(P) atomic_read(P)
218# define nmi_atomic_set(P, V) atomic_set(P, V)
219# define nmi_atomic_dec(P) atomic_dec(P)
220# endif
221
222# ifndef X86_FEATURE_ARCH_PERFMON
223# define X86_FEATURE_ARCH_PERFMON (3*32+9) /* Intel Architectural PerfMon */
224# endif
225# ifndef MSR_ARCH_PERFMON_EVENTSEL0
226# define MSR_ARCH_PERFMON_EVENTSEL0 0x186
227# endif
228# ifndef ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT
229# define ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT (1 << 0)
230# endif
231
232#endif /* CONFIG_X86_LOCAL_APIC */
233
234
235/*******************************************************************************
236* Defined Constants And Macros *
237*******************************************************************************/
238/**
239 * Device extention & session data association structure.
240 */
241static SUPDRVDEVEXT g_DevExt;
242
243/** Timer structure for the GIP update. */
244static struct timer_list g_GipTimer;
245/** Pointer to the page structure for the GIP. */
246struct page *g_pGipPage;
247
248/** Registered devfs device handle. */
249#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
250# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
251static void *g_hDevFsVBoxDrv = NULL;
252# else
253static devfs_handle_t g_hDevFsVBoxDrv = NULL;
254# endif
255#endif
256
257#ifndef CONFIG_VBOXDRV_AS_MISC
258/** Module major number */
259#define DEVICE_MAJOR 234
260/** Saved major device number */
261static int g_iModuleMajor;
262#endif /* !CONFIG_VBOXDRV_AS_MISC */
263
264/** The module name. */
265#define DEVICE_NAME "vboxdrv"
266
267#ifdef RT_ARCH_AMD64
268/**
269 * Memory for the executable memory heap (in IPRT).
270 */
271extern uint8_t g_abExecMemory[1572864]; /* 1.5 MB */
272__asm__(".section execmemory, \"awx\", @progbits\n\t"
273 ".align 32\n\t"
274 ".globl g_abExecMemory\n"
275 "g_abExecMemory:\n\t"
276 ".zero 1572864\n\t"
277 ".type g_abExecMemory, @object\n\t"
278 ".size g_abExecMemory, 1572864\n\t"
279 ".text\n\t");
280#endif
281
282
283/*******************************************************************************
284* Internal Functions *
285*******************************************************************************/
286static int VBoxDrvLinuxInit(void);
287static void VBoxDrvLinuxUnload(void);
288static int VBoxDrvLinuxCreate(struct inode *pInode, struct file *pFilp);
289static int VBoxDrvLinuxClose(struct inode *pInode, struct file *pFilp);
290static int VBoxDrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
291static int VBoxDrvLinuxIOCtlSlow(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
292static int VBoxDrvLinuxInitGip(PSUPDRVDEVEXT pDevExt);
293static int VBoxDrvLinuxTermGip(PSUPDRVDEVEXT pDevExt);
294static void VBoxDrvLinuxGipTimer(unsigned long ulUser);
295#ifdef CONFIG_SMP
296static void VBoxDrvLinuxGipTimerPerCpu(unsigned long ulUser);
297static void VBoxDrvLinuxGipResumePerCpu(void *pvUser);
298#endif
299static int VBoxDrvLinuxErr2LinuxErr(int);
300
301
302/** The file_operations structure. */
303static struct file_operations gFileOpsVBoxDrv =
304{
305 owner: THIS_MODULE,
306 open: VBoxDrvLinuxCreate,
307 release: VBoxDrvLinuxClose,
308 ioctl: VBoxDrvLinuxIOCtl,
309};
310
311#ifdef CONFIG_VBOXDRV_AS_MISC
312/** The miscdevice structure. */
313static struct miscdevice gMiscDevice =
314{
315 minor: MISC_DYNAMIC_MINOR,
316 name: DEVICE_NAME,
317 fops: &gFileOpsVBoxDrv,
318# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && \
319 LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17)
320 devfs_name: DEVICE_NAME,
321# endif
322};
323#endif
324
325#ifdef CONFIG_X86_LOCAL_APIC
326# ifdef DO_DISABLE_NMI
327
328/** Stop AMD NMI watchdog (x86_64 only). */
329static int stop_k7_watchdog(void)
330{
331 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
332 return 1;
333}
334
335/** Stop Intel P4 NMI watchdog (x86_64 only). */
336static int stop_p4_watchdog(void)
337{
338 wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
339 wrmsr(MSR_P4_IQ_CCCR1, 0, 0);
340 wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
341 return 1;
342}
343
344/** The new method of detecting the event counter */
345static int stop_intel_arch_watchdog(void)
346{
347 unsigned ebx;
348
349 ebx = cpuid_ebx(10);
350 if (!(ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
351 wrmsr(MSR_ARCH_PERFMON_EVENTSEL0, 0, 0);
352 return 1;
353}
354
355/** Stop NMI watchdog. */
356static void vbox_stop_apic_nmi_watchdog(void *unused)
357{
358 int stopped = 0;
359
360 /* only support LOCAL and IO APICs for now */
361 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
362 (nmi_watchdog != NMI_IO_APIC))
363 return;
364
365 if (nmi_watchdog == NMI_LOCAL_APIC)
366 {
367 switch (boot_cpu_data.x86_vendor)
368 {
369 case X86_VENDOR_AMD:
370 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
371 return;
372 stopped = stop_k7_watchdog();
373 break;
374 case X86_VENDOR_INTEL:
375 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
376 {
377 stopped = stop_intel_arch_watchdog();
378 break;
379 }
380 stopped = stop_p4_watchdog();
381 break;
382 default:
383 return;
384 }
385 }
386
387 if (stopped)
388 nmi_atomic_dec(&nmi_active);
389}
390
391/** Disable LAPIC NMI watchdog. */
392static void disable_lapic_nmi_watchdog(void)
393{
394 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
395
396 if (nmi_atomic_read(&nmi_active) <= 0)
397 return;
398
399 on_each_cpu(vbox_stop_apic_nmi_watchdog, NULL, 1, 1);
400
401 BUG_ON(nmi_atomic_read(&nmi_active) != 0);
402
403 /* tell do_nmi() and others that we're not active any more */
404 nmi_watchdog = NMI_NONE;
405}
406
407/** Shutdown NMI. */
408static void nmi_cpu_shutdown(void * dummy)
409{
410 unsigned int vERR, vPC;
411
412 vPC = apic_read(APIC_LVTPC);
413
414 if ((GET_APIC_DELIVERY_MODE(vPC) == APIC_MODE_NMI) && !(vPC & APIC_LVT_MASKED))
415 {
416 vERR = apic_read(APIC_LVTERR);
417 apic_write(APIC_LVTERR, vERR | APIC_LVT_MASKED);
418 apic_write(APIC_LVTPC, vPC | APIC_LVT_MASKED);
419 apic_write(APIC_LVTERR, vERR);
420 }
421}
422
423static void nmi_shutdown(void)
424{
425 on_each_cpu(nmi_cpu_shutdown, NULL, 0, 1);
426}
427# endif /* DO_DISABLE_NMI */
428#endif /* CONFIG_X86_LOCAL_APIC */
429
430/**
431 * Initialize module.
432 *
433 * @returns appropriate status code.
434 */
435static int __init VBoxDrvLinuxInit(void)
436{
437 int rc;
438
439 dprintf(("VBoxDrv::ModuleInit\n"));
440
441#ifdef CONFIG_X86_LOCAL_APIC
442 /*
443 * If an NMI occurs while we are inside the world switcher the macine will crash.
444 * The Linux NMI watchdog generates periodic NMIs increasing a counter which is
445 * compared with another counter increased in the timer interrupt handler. Therefore
446 * we don't allow to setup an NMI watchdog.
447 */
448# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
449 /*
450 * First test: NMI actiated? Works only works with Linux 2.6 -- 2.4 does not export
451 * the nmi_watchdog variable.
452 */
453# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) || \
454 (defined CONFIG_X86_64 && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
455# ifdef DO_DISABLE_NMI
456 if (nmi_atomic_read(&nmi_active) > 0)
457 {
458 printk(KERN_INFO DEVICE_NAME ": Trying to deactivate the NMI watchdog...\n");
459
460 switch (nmi_watchdog)
461 {
462 case NMI_LOCAL_APIC:
463 disable_lapic_nmi_watchdog();
464 break;
465 case NMI_NONE:
466 nmi_atomic_dec(&nmi_active);
467 break;
468 }
469
470 if (nmi_atomic_read(&nmi_active) == 0)
471 {
472 nmi_shutdown();
473 printk(KERN_INFO DEVICE_NAME ": Successfully done.\n");
474 }
475 else
476 printk(KERN_INFO DEVICE_NAME ": Failed!\n");
477 }
478# endif /* DO_DISABLE_NMI */
479
480 /*
481 * Permanent IO_APIC mode active? No way to handle this!
482 */
483 if (nmi_watchdog == NMI_IO_APIC)
484 {
485 printk(KERN_ERR DEVICE_NAME
486 ": NMI watchdog in IO_APIC mode active -- refused to load the kernel module!\n"
487 DEVICE_NAME
488 ": Please disable the NMI watchdog by specifying 'nmi_watchdog=0' at kernel\n"
489 DEVICE_NAME
490 ": command line.\n");
491 return -EINVAL;
492 }
493
494 /*
495 * See arch/i386/kernel/nmi.c on >= 2.6.19: -1 means it can never enabled again
496 */
497 nmi_atomic_set(&nmi_active, -1);
498 printk(KERN_INFO DEVICE_NAME ": Trying to deactivate the NMI watchdog permanently...\n");
499
500 /*
501 * Now fall through and see if it actually was enabled before. If so, fail
502 * as we cannot deactivate it cleanly from here.
503 */
504# else /* < 2.6.19 */
505 /*
506 * Older 2.6 kernels: nmi_watchdog is not initalized by default
507 */
508 if (nmi_watchdog != NMI_NONE)
509 goto nmi_activated;
510# endif
511# endif /* >= 2.6.0 */
512
513 /*
514 * Second test: Interrupt generated by performance counter not masked and can
515 * generate an NMI. Works also with Linux 2.4.
516 */
517 {
518 unsigned int v, ver, maxlvt;
519
520 v = apic_read(APIC_LVR);
521 ver = GET_APIC_VERSION(v);
522 /* 82489DXs do not report # of LVT entries. */
523 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
524 if (maxlvt >= 4)
525 {
526 /* Read status of performance counter IRQ vector */
527 v = apic_read(APIC_LVTPC);
528
529 /* performance counter generates NMI and is not masked? */
530 if ((GET_APIC_DELIVERY_MODE(v) == APIC_MODE_NMI) && !(v & APIC_LVT_MASKED))
531 {
532# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) || \
533 (defined CONFIG_X86_64 && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
534 printk(KERN_ERR DEVICE_NAME
535 ": NMI watchdog either active or at least initialized. Please disable the NMI\n"
536 DEVICE_NAME
537 ": watchdog by specifying 'nmi_watchdog=0' at kernel command line.\n");
538 return -EINVAL;
539# else /* < 2.6.19 */
540# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
541nmi_activated:
542# endif
543 printk(KERN_ERR DEVICE_NAME
544 ": NMI watchdog active -- refused to load the kernel module! Please disable\n"
545 DEVICE_NAME
546 ": the NMI watchdog by specifying 'nmi_watchdog=0' at kernel command line.\n");
547 return -EINVAL;
548# endif /* >= 2.6.19 */
549 }
550 }
551 }
552# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
553 printk(KERN_INFO DEVICE_NAME ": Successfully done.\n");
554# endif /* >= 2.6.19 */
555#endif /* CONFIG_X86_LOCAL_APIC */
556
557#ifdef CONFIG_VBOXDRV_AS_MISC
558 rc = misc_register(&gMiscDevice);
559 if (rc)
560 {
561 printk(KERN_ERR DEVICE_NAME ": Can't register misc device! rc=%d\n", rc);
562 return rc;
563 }
564#else /* !CONFIG_VBOXDRV_AS_MISC */
565 /*
566 * Register character device.
567 */
568 g_iModuleMajor = DEVICE_MAJOR;
569 rc = VBOX_REGISTER_DEVICE((dev_t)g_iModuleMajor, DEVICE_NAME, &gFileOpsVBoxDrv);
570 if (rc < 0)
571 {
572 dprintf(("VBOX_REGISTER_DEVICE failed with rc=%#x!\n", rc));
573 return rc;
574 }
575
576 /*
577 * Save returned module major number
578 */
579 if (DEVICE_MAJOR != 0)
580 g_iModuleMajor = DEVICE_MAJOR;
581 else
582 g_iModuleMajor = rc;
583 rc = 0;
584
585#ifdef CONFIG_DEVFS_FS
586 /*
587 * Register a device entry
588 */
589 g_hDevFsVBoxDrv = VBOX_REGISTER_DEVFS();
590 if (g_hDevFsVBoxDrv == NULL)
591 {
592 dprintf(("devfs_register failed!\n"));
593 rc = -EINVAL;
594 }
595#endif
596#endif /* !CONFIG_VBOXDRV_AS_MISC */
597 if (!rc)
598 {
599 /*
600 * Initialize the runtime.
601 * On AMD64 we'll have to donate the high rwx memory block to the exec allocator.
602 */
603 rc = RTR0Init(0);
604 if (RT_SUCCESS(rc))
605 {
606#ifdef RT_ARCH_AMD64
607 rc = RTR0MemExecDonate(&g_abExecMemory[0], sizeof(g_abExecMemory));
608#endif
609 /*
610 * Initialize the device extension.
611 */
612 if (RT_SUCCESS(rc))
613 rc = supdrvInitDevExt(&g_DevExt);
614 if (!rc)
615 {
616 /*
617 * Create the GIP page.
618 */
619 rc = VBoxDrvLinuxInitGip(&g_DevExt);
620 if (!rc)
621 {
622 dprintf(("VBoxDrv::ModuleInit returning %#x\n", rc));
623 return rc;
624 }
625
626 supdrvDeleteDevExt(&g_DevExt);
627 }
628 else
629 rc = -EINVAL;
630 RTR0Term();
631 }
632 else
633 rc = -EINVAL;
634
635 /*
636 * Failed, cleanup and return the error code.
637 */
638#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
639 VBOX_UNREGISTER_DEVFS(g_hDevFsVBoxDrv);
640#endif
641 }
642#ifdef CONFIG_VBOXDRV_AS_MISC
643 misc_deregister(&gMiscDevice);
644 dprintf(("VBoxDrv::ModuleInit returning %#x (minor:%d)\n", rc, gMiscDevice.minor));
645#else
646 VBOX_UNREGISTER_DEVICE(g_iModuleMajor, DEVICE_NAME);
647 dprintf(("VBoxDrv::ModuleInit returning %#x (major:%d)\n", rc, g_iModuleMajor));
648#endif
649 return rc;
650}
651
652
653/**
654 * Unload the module.
655 */
656static void __exit VBoxDrvLinuxUnload(void)
657{
658 int rc;
659 dprintf(("VBoxDrvLinuxUnload\n"));
660
661 /*
662 * I Don't think it's possible to unload a driver which processes have
663 * opened, at least we'll blindly assume that here.
664 */
665#ifdef CONFIG_VBOXDRV_AS_MISC
666 rc = misc_deregister(&gMiscDevice);
667 if (rc < 0)
668 {
669 dprintf(("misc_deregister failed with rc=%#x\n", rc));
670 }
671#else /* !CONFIG_VBOXDRV_AS_MISC */
672#ifdef CONFIG_DEVFS_FS
673 /*
674 * Unregister a device entry
675 */
676 VBOX_UNREGISTER_DEVFS(g_hDevFsVBoxDrv);
677#endif // devfs
678 rc = VBOX_UNREGISTER_DEVICE(g_iModuleMajor, DEVICE_NAME);
679 if (rc < 0)
680 {
681 dprintf(("unregister_chrdev failed with rc=%#x (major:%d)\n", rc, g_iModuleMajor));
682 }
683#endif /* !CONFIG_VBOXDRV_AS_MISC */
684
685 /*
686 * Destroy GIP, delete the device extension and terminate IPRT.
687 */
688 VBoxDrvLinuxTermGip(&g_DevExt);
689 supdrvDeleteDevExt(&g_DevExt);
690 RTR0Term();
691}
692
693
694/**
695 * Device open. Called on open /dev/vboxdrv
696 *
697 * @param pInode Pointer to inode info structure.
698 * @param pFilp Associated file pointer.
699 */
700static int VBoxDrvLinuxCreate(struct inode *pInode, struct file *pFilp)
701{
702 int rc;
703 PSUPDRVSESSION pSession;
704 dprintf(("VBoxDrvLinuxCreate: pFilp=%p\n", pFilp));
705
706 /*
707 * Call common code for the rest.
708 */
709 rc = supdrvCreateSession(&g_DevExt, (PSUPDRVSESSION *)&pSession);
710 if (!rc)
711 {
712 pSession->Uid = current->euid;
713 pSession->Gid = current->egid;
714 pSession->Process = RTProcSelf();
715 pSession->R0Process = RTR0ProcHandleSelf();
716 }
717
718 dprintf(("VBoxDrvLinuxCreate: g_DevExt=%p pSession=%p rc=%d\n", &g_DevExt, pSession, rc));
719 pFilp->private_data = pSession;
720
721 return VBoxDrvLinuxErr2LinuxErr(rc);
722}
723
724
725/**
726 * Close device.
727 *
728 * @param pInode Pointer to inode info structure.
729 * @param pFilp Associated file pointer.
730 */
731static int VBoxDrvLinuxClose(struct inode *pInode, struct file *pFilp)
732{
733 dprintf(("VBoxDrvLinuxClose: pFilp=%p private_data=%p\n", pFilp, pFilp->private_data));
734 supdrvCloseSession(&g_DevExt, (PSUPDRVSESSION)pFilp->private_data);
735 pFilp->private_data = NULL;
736 return 0;
737}
738
739
740/**
741 * Device I/O Control entry point.
742 *
743 * @param pInode Pointer to inode info structure.
744 * @param pFilp Associated file pointer.
745 * @param uCmd The function specified to ioctl().
746 * @param ulArg The argument specified to ioctl().
747 */
748static int VBoxDrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
749{
750 /*
751 * Deal with the two high-speed IOCtl that takes it's arguments from
752 * the session and iCmd, and only returns a VBox status code.
753 */
754 if (RT_LIKELY( uCmd == SUP_IOCTL_FAST_DO_RAW_RUN
755 || uCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
756 || uCmd == SUP_IOCTL_FAST_DO_NOP))
757 return supdrvIOCtlFast(uCmd, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data);
758 return VBoxDrvLinuxIOCtlSlow(pInode, pFilp, uCmd, ulArg);
759}
760
761
762/**
763 * Device I/O Control entry point.
764 *
765 * @param pInode Pointer to inode info structure.
766 * @param pFilp Associated file pointer.
767 * @param uCmd The function specified to ioctl().
768 * @param ulArg The argument specified to ioctl().
769 */
770static int VBoxDrvLinuxIOCtlSlow(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
771{
772 int rc;
773 SUPREQHDR Hdr;
774 PSUPREQHDR pHdr;
775 uint32_t cbBuf;
776
777 dprintf2(("VBoxDrvLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p\n", pFilp, uCmd, (void *)ulArg));
778
779 /*
780 * Read the header.
781 */
782 if (RT_UNLIKELY(copy_from_user(&Hdr, (void *)ulArg, sizeof(Hdr))))
783 {
784 dprintf(("VBoxDrvLinuxIOCtl: copy_from_user(,%#lx,) failed; uCmd=%#x.\n", ulArg, uCmd));
785 return -EFAULT;
786 }
787 if (RT_UNLIKELY((Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK) != SUPREQHDR_FLAGS_MAGIC))
788 {
789 dprintf(("VBoxDrvLinuxIOCtl: bad header magic %#x; uCmd=%#x\n", Hdr.fFlags & SUPREQHDR_FLAGS_MAGIC_MASK, uCmd));
790 return -EINVAL;
791 }
792
793 /*
794 * Buffer the request.
795 */
796 cbBuf = RT_MAX(Hdr.cbIn, Hdr.cbOut);
797 if (RT_UNLIKELY(cbBuf > _1M*16))
798 {
799 dprintf(("VBoxDrvLinuxIOCtl: too big cbBuf=%#x; uCmd=%#x\n", cbBuf, uCmd));
800 return -E2BIG;
801 }
802 if (RT_UNLIKELY(cbBuf != _IOC_SIZE(uCmd) && _IOC_SIZE(uCmd)))
803 {
804 dprintf(("VBoxDrvLinuxIOCtl: bad ioctl cbBuf=%#x _IOC_SIZE=%#x; uCmd=%#x.\n", cbBuf, _IOC_SIZE(uCmd), uCmd));
805 return -EINVAL;
806 }
807 pHdr = RTMemAlloc(cbBuf);
808 if (RT_UNLIKELY(!pHdr))
809 {
810 OSDBGPRINT(("VBoxDrvLinuxIOCtl: failed to allocate buffer of %d bytes for uCmd=%#x.\n", cbBuf, uCmd));
811 return -ENOMEM;
812 }
813 if (RT_UNLIKELY(copy_from_user(pHdr, (void *)ulArg, Hdr.cbIn)))
814 {
815 dprintf(("VBoxDrvLinuxIOCtl: copy_from_user(,%#lx, %#x) failed; uCmd=%#x.\n", ulArg, Hdr.cbIn, uCmd));
816 RTMemFree(pHdr);
817 return -EFAULT;
818 }
819
820 /*
821 * Process the IOCtl.
822 */
823 rc = supdrvIOCtl(uCmd, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data, pHdr);
824
825 /*
826 * Copy ioctl data and output buffer back to user space.
827 */
828 if (RT_LIKELY(!rc))
829 {
830 uint32_t cbOut = pHdr->cbOut;
831 if (RT_UNLIKELY(cbOut > cbBuf))
832 {
833 OSDBGPRINT(("VBoxDrvLinuxIOCtl: too much output! %#x > %#x; uCmd=%#x!\n", cbOut, cbBuf, uCmd));
834 cbOut = cbBuf;
835 }
836 if (RT_UNLIKELY(copy_to_user((void *)ulArg, pHdr, cbOut)))
837 {
838 /* this is really bad! */
839 OSDBGPRINT(("VBoxDrvLinuxIOCtl: copy_to_user(%#lx,,%#x); uCmd=%#x!\n", ulArg, cbOut, uCmd));
840 rc = -EFAULT;
841 }
842 }
843 else
844 {
845 dprintf(("VBoxDrvLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p failed, rc=%d\n", pFilp, uCmd, (void *)ulArg, rc));
846 rc = -EINVAL;
847 }
848 RTMemFree(pHdr);
849
850 dprintf2(("VBoxDrvLinuxIOCtl: returns %d\n", rc));
851 return rc;
852}
853
854
855/**
856 * Initializes any OS specific object creator fields.
857 */
858void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
859{
860 NOREF(pObj);
861 NOREF(pSession);
862}
863
864
865/**
866 * Checks if the session can access the object.
867 *
868 * @returns true if a decision has been made.
869 * @returns false if the default access policy should be applied.
870 *
871 * @param pObj The object in question.
872 * @param pSession The session wanting to access the object.
873 * @param pszObjName The object name, can be NULL.
874 * @param prc Where to store the result when returning true.
875 */
876bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
877{
878 NOREF(pObj);
879 NOREF(pSession);
880 NOREF(pszObjName);
881 NOREF(prc);
882 return false;
883}
884
885
886/**
887 * Initializes the GIP.
888 *
889 * @returns negative errno.
890 * @param pDevExt Instance data. GIP stuff may be updated.
891 */
892static int VBoxDrvLinuxInitGip(PSUPDRVDEVEXT pDevExt)
893{
894 struct page *pPage;
895 dma_addr_t HCPhys;
896 PSUPGLOBALINFOPAGE pGip;
897#ifdef CONFIG_SMP
898 unsigned i;
899#endif
900 dprintf(("VBoxDrvLinuxInitGip:\n"));
901
902 /*
903 * Allocate the page.
904 */
905 pPage = alloc_pages(GFP_USER, 0);
906 if (!pPage)
907 {
908 dprintf(("VBoxDrvLinuxInitGip: failed to allocate the GIP page\n"));
909 return -ENOMEM;
910 }
911
912 /*
913 * Lock the page.
914 */
915 SetPageReserved(pPage);
916 g_pGipPage = pPage;
917
918 /*
919 * Call common initialization routine.
920 */
921 HCPhys = page_to_phys(pPage);
922 pGip = (PSUPGLOBALINFOPAGE)page_address(pPage);
923 pDevExt->ulLastJiffies = jiffies;
924#ifdef TICK_NSEC
925 pDevExt->u64LastMonotime = (uint64_t)pDevExt->ulLastJiffies * TICK_NSEC;
926 dprintf(("VBoxDrvInitGIP: TICK_NSEC=%ld HZ=%d jiffies=%ld now=%lld\n",
927 TICK_NSEC, HZ, pDevExt->ulLastJiffies, pDevExt->u64LastMonotime));
928#else
929 pDevExt->u64LastMonotime = (uint64_t)pDevExt->ulLastJiffies * (1000000 / HZ);
930 dprintf(("VBoxDrvInitGIP: TICK_NSEC=%d HZ=%d jiffies=%ld now=%lld\n",
931 (int)(1000000 / HZ), HZ, pDevExt->ulLastJiffies, pDevExt->u64LastMonotime));
932#endif
933 supdrvGipInit(pDevExt, pGip, HCPhys, pDevExt->u64LastMonotime,
934 HZ <= 1000 ? HZ : 1000);
935
936 /*
937 * Initialize the timer.
938 */
939 init_timer(&g_GipTimer);
940 g_GipTimer.data = (unsigned long)pDevExt;
941 g_GipTimer.function = VBoxDrvLinuxGipTimer;
942 g_GipTimer.expires = jiffies;
943#ifdef CONFIG_SMP
944 for (i = 0; i < RT_ELEMENTS(pDevExt->aCPUs); i++)
945 {
946 pDevExt->aCPUs[i].u64LastMonotime = pDevExt->u64LastMonotime;
947 pDevExt->aCPUs[i].ulLastJiffies = pDevExt->ulLastJiffies;
948 pDevExt->aCPUs[i].iSmpProcessorId = -512;
949 init_timer(&pDevExt->aCPUs[i].Timer);
950 pDevExt->aCPUs[i].Timer.data = i;
951 pDevExt->aCPUs[i].Timer.function = VBoxDrvLinuxGipTimerPerCpu;
952 pDevExt->aCPUs[i].Timer.expires = jiffies;
953 }
954#endif
955
956 return 0;
957}
958
959
960/**
961 * Terminates the GIP.
962 *
963 * @returns negative errno.
964 * @param pDevExt Instance data. GIP stuff may be updated.
965 */
966static int VBoxDrvLinuxTermGip(PSUPDRVDEVEXT pDevExt)
967{
968 struct page *pPage;
969 PSUPGLOBALINFOPAGE pGip;
970#ifdef CONFIG_SMP
971 unsigned i;
972#endif
973 dprintf(("VBoxDrvLinuxTermGip:\n"));
974
975 /*
976 * Delete the timer if it's pending.
977 */
978 if (timer_pending(&g_GipTimer))
979 del_timer_sync(&g_GipTimer);
980#ifdef CONFIG_SMP
981 for (i = 0; i < RT_ELEMENTS(pDevExt->aCPUs); i++)
982 if (timer_pending(&pDevExt->aCPUs[i].Timer))
983 del_timer_sync(&pDevExt->aCPUs[i].Timer);
984#endif
985
986 /*
987 * Uninitialize the content.
988 */
989 pGip = pDevExt->pGip;
990 pDevExt->pGip = NULL;
991 if (pGip)
992 supdrvGipTerm(pGip);
993
994 /*
995 * Free the page.
996 */
997 pPage = g_pGipPage;
998 g_pGipPage = NULL;
999 if (pPage)
1000 {
1001 ClearPageReserved(pPage);
1002 __free_pages(pPage, 0);
1003 }
1004
1005 return 0;
1006}
1007
1008/**
1009 * Timer callback function.
1010 *
1011 * In ASYNC TSC mode this is called on the primary CPU, and we're
1012 * assuming that the CPU remains online.
1013 *
1014 * @param ulUser The device extension pointer.
1015 */
1016static void VBoxDrvLinuxGipTimer(unsigned long ulUser)
1017{
1018 PSUPDRVDEVEXT pDevExt;
1019 PSUPGLOBALINFOPAGE pGip;
1020 unsigned long ulNow;
1021 unsigned long ulDiff;
1022 uint64_t u64Monotime;
1023 unsigned long SavedFlags;
1024
1025 local_irq_save(SavedFlags);
1026
1027 ulNow = jiffies;
1028 pDevExt = (PSUPDRVDEVEXT)ulUser;
1029 pGip = pDevExt->pGip;
1030
1031#ifdef CONFIG_SMP
1032 if (pGip && pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
1033 {
1034 uint8_t iCPU = ASMGetApicId();
1035 ulDiff = ulNow - pDevExt->aCPUs[iCPU].ulLastJiffies;
1036 pDevExt->aCPUs[iCPU].ulLastJiffies = ulNow;
1037#ifdef TICK_NSEC
1038 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * TICK_NSEC;
1039#else
1040 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * (1000000 / HZ);
1041#endif
1042 pDevExt->aCPUs[iCPU].u64LastMonotime = u64Monotime;
1043 }
1044 else
1045#endif /* CONFIG_SMP */
1046 {
1047 ulDiff = ulNow - pDevExt->ulLastJiffies;
1048 pDevExt->ulLastJiffies = ulNow;
1049#ifdef TICK_NSEC
1050 u64Monotime = pDevExt->u64LastMonotime + ulDiff * TICK_NSEC;
1051#else
1052 u64Monotime = pDevExt->u64LastMonotime + ulDiff * (1000000 / HZ);
1053#endif
1054 pDevExt->u64LastMonotime = u64Monotime;
1055 }
1056 if (RT_LIKELY(pGip))
1057 supdrvGipUpdate(pDevExt->pGip, u64Monotime);
1058 if (RT_LIKELY(!pDevExt->fGIPSuspended))
1059 mod_timer(&g_GipTimer, ulNow + (HZ <= 1000 ? 1 : ONE_MSEC_IN_JIFFIES));
1060
1061 local_irq_restore(SavedFlags);
1062}
1063
1064
1065#ifdef CONFIG_SMP
1066/**
1067 * Timer callback function for the other CPUs.
1068 *
1069 * @param iTimerCPU The APIC ID of this timer.
1070 */
1071static void VBoxDrvLinuxGipTimerPerCpu(unsigned long iTimerCPU)
1072{
1073 PSUPDRVDEVEXT pDevExt;
1074 PSUPGLOBALINFOPAGE pGip;
1075 uint8_t iCPU;
1076 uint64_t u64Monotime;
1077 unsigned long SavedFlags;
1078 unsigned long ulNow;
1079
1080 local_irq_save(SavedFlags);
1081
1082 ulNow = jiffies;
1083 pDevExt = &g_DevExt;
1084 pGip = pDevExt->pGip;
1085 iCPU = ASMGetApicId();
1086
1087 if (RT_LIKELY(iCPU < RT_ELEMENTS(pGip->aCPUs)))
1088 {
1089 if (RT_LIKELY(iTimerCPU == iCPU))
1090 {
1091 unsigned long ulDiff = ulNow - pDevExt->aCPUs[iCPU].ulLastJiffies;
1092 pDevExt->aCPUs[iCPU].ulLastJiffies = ulNow;
1093#ifdef TICK_NSEC
1094 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * TICK_NSEC;
1095#else
1096 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * (1000000 / HZ);
1097#endif
1098 pDevExt->aCPUs[iCPU].u64LastMonotime = u64Monotime;
1099 if (RT_LIKELY(pGip))
1100 supdrvGipUpdatePerCpu(pGip, u64Monotime, iCPU);
1101 if (RT_LIKELY(!pDevExt->fGIPSuspended))
1102 mod_timer(&pDevExt->aCPUs[iCPU].Timer, ulNow + (HZ <= 1000 ? 1 : ONE_MSEC_IN_JIFFIES));
1103 }
1104 else
1105 printk("vboxdrv: error: GIP CPU update timer executing on the wrong CPU: apicid=%d != timer-apicid=%ld (cpuid=%d !=? timer-cpuid=%d)\n",
1106 iCPU, iTimerCPU, smp_processor_id(), pDevExt->aCPUs[iTimerCPU].iSmpProcessorId);
1107 }
1108 else
1109 printk("vboxdrv: error: APIC ID is bogus (GIP CPU update): apicid=%d max=%lu cpuid=%d\n",
1110 iCPU, (unsigned long)RT_ELEMENTS(pGip->aCPUs), smp_processor_id());
1111
1112 local_irq_restore(SavedFlags);
1113}
1114#endif /* CONFIG_SMP */
1115
1116
1117/**
1118 * Maps the GIP into user space.
1119 *
1120 * @returns negative errno.
1121 * @param pDevExt Instance data.
1122 */
1123int VBOXCALL supdrvOSGipMap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE *ppGip)
1124{
1125 int rc = 0;
1126 unsigned long ulAddr;
1127 unsigned long HCPhys = pDevExt->HCPhysGip;
1128 pgprot_t pgFlags;
1129 pgprot_val(pgFlags) = _PAGE_PRESENT | _PAGE_USER;
1130 dprintf2(("supdrvOSGipMap: ppGip=%p\n", ppGip));
1131
1132 /*
1133 * Allocate user space mapping and put the physical pages into it.
1134 */
1135 down_write(&current->mm->mmap_sem);
1136 ulAddr = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ, MAP_SHARED | MAP_ANONYMOUS, 0);
1137 if (!(ulAddr & ~PAGE_MASK))
1138 {
1139#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && !defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1140 int rc2 = remap_page_range(ulAddr, HCPhys, PAGE_SIZE, pgFlags);
1141#else
1142 int rc2 = 0;
1143 struct vm_area_struct *vma = find_vma(current->mm, ulAddr);
1144 if (vma)
1145#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
1146 rc2 = remap_page_range(vma, ulAddr, HCPhys, PAGE_SIZE, pgFlags);
1147#else
1148 rc2 = remap_pfn_range(vma, ulAddr, HCPhys >> PAGE_SHIFT, PAGE_SIZE, pgFlags);
1149#endif
1150 else
1151 {
1152 rc = SUPDRV_ERR_NO_MEMORY;
1153 dprintf(("supdrvOSGipMap: no vma found for ulAddr=%#lx!\n", ulAddr));
1154 }
1155#endif
1156 if (rc2)
1157 {
1158 rc = SUPDRV_ERR_NO_MEMORY;
1159 dprintf(("supdrvOSGipMap: remap_page_range failed rc2=%d\n", rc2));
1160 }
1161 }
1162 else
1163 {
1164 dprintf(("supdrvOSGipMap: do_mmap failed ulAddr=%#lx\n", ulAddr));
1165 rc = SUPDRV_ERR_NO_MEMORY;
1166 }
1167 up_write(&current->mm->mmap_sem); /* not quite sure when to give this up. */
1168
1169 /*
1170 * Success?
1171 */
1172 if (!rc)
1173 {
1174 *ppGip = (PSUPGLOBALINFOPAGE)ulAddr;
1175 dprintf2(("supdrvOSGipMap: ppGip=%p\n", *ppGip));
1176 return 0;
1177 }
1178
1179 /*
1180 * Failure, cleanup and be gone.
1181 */
1182 if (ulAddr & ~PAGE_MASK)
1183 {
1184 down_write(&current->mm->mmap_sem);
1185 MY_DO_MUNMAP(current->mm, ulAddr, PAGE_SIZE);
1186 up_write(&current->mm->mmap_sem);
1187 }
1188
1189 dprintf2(("supdrvOSGipMap: returns %d\n", rc));
1190 return rc;
1191}
1192
1193
1194/**
1195 * Maps the GIP into user space.
1196 *
1197 * @returns negative errno.
1198 * @param pDevExt Instance data.
1199 */
1200int VBOXCALL supdrvOSGipUnmap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip)
1201{
1202 dprintf2(("supdrvOSGipUnmap: pGip=%p\n", pGip));
1203 if (current->mm)
1204 {
1205 down_write(&current->mm->mmap_sem);
1206 MY_DO_MUNMAP(current->mm, (unsigned long)pGip, PAGE_SIZE);
1207 up_write(&current->mm->mmap_sem);
1208 }
1209 dprintf2(("supdrvOSGipUnmap: returns 0\n"));
1210 return 0;
1211}
1212
1213
1214/**
1215 * Resumes the GIP updating.
1216 *
1217 * @param pDevExt Instance data.
1218 */
1219void VBOXCALL supdrvOSGipResume(PSUPDRVDEVEXT pDevExt)
1220{
1221 dprintf2(("supdrvOSGipResume:\n"));
1222 ASMAtomicXchgU8(&pDevExt->fGIPSuspended, false);
1223#ifdef CONFIG_SMP
1224 if (pDevExt->pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
1225#endif
1226 mod_timer(&g_GipTimer, jiffies);
1227#ifdef CONFIG_SMP
1228 else
1229 {
1230 mod_timer(&g_GipTimer, jiffies);
1231 smp_call_function(VBoxDrvLinuxGipResumePerCpu, pDevExt, 0 /* retry */, 1 /* wait */);
1232 }
1233#endif
1234}
1235
1236
1237#ifdef CONFIG_SMP
1238/**
1239 * Callback for resuming GIP updating on the other CPUs.
1240 *
1241 * This is only used when the GIP is in async tsc mode.
1242 *
1243 * @param pvUser Pointer to the device instance.
1244 */
1245static void VBoxDrvLinuxGipResumePerCpu(void *pvUser)
1246{
1247 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
1248 uint8_t iCPU = ASMGetApicId();
1249
1250 if (RT_UNLIKELY(iCPU >= RT_ELEMENTS(pDevExt->pGip->aCPUs)))
1251 {
1252 printk("vboxdrv: error: apicid=%d max=%lu cpuid=%d\n",
1253 iCPU, (unsigned long)RT_ELEMENTS(pDevExt->pGip->aCPUs), smp_processor_id());
1254 return;
1255 }
1256
1257 pDevExt->aCPUs[iCPU].iSmpProcessorId = smp_processor_id();
1258 mod_timer(&pDevExt->aCPUs[iCPU].Timer, jiffies);
1259}
1260#endif /* CONFIG_SMP */
1261
1262
1263/**
1264 * Suspends the GIP updating.
1265 *
1266 * @param pDevExt Instance data.
1267 */
1268void VBOXCALL supdrvOSGipSuspend(PSUPDRVDEVEXT pDevExt)
1269{
1270#ifdef CONFIG_SMP
1271 unsigned i;
1272#endif
1273 dprintf2(("supdrvOSGipSuspend:\n"));
1274 ASMAtomicXchgU8(&pDevExt->fGIPSuspended, true);
1275
1276 if (timer_pending(&g_GipTimer))
1277 del_timer_sync(&g_GipTimer);
1278#ifdef CONFIG_SMP
1279 for (i = 0; i < RT_ELEMENTS(pDevExt->aCPUs); i++)
1280 if (timer_pending(&pDevExt->aCPUs[i].Timer))
1281 del_timer_sync(&pDevExt->aCPUs[i].Timer);
1282#endif
1283}
1284
1285
1286/**
1287 * Get the current CPU count.
1288 * @returns Number of cpus.
1289 */
1290unsigned VBOXCALL supdrvOSGetCPUCount(void)
1291{
1292#ifdef CONFIG_SMP
1293# if defined(num_present_cpus)
1294 return num_present_cpus();
1295# elif defined(num_online_cpus)
1296 return num_online_cpus();
1297# else
1298 return smp_num_cpus;
1299# endif
1300#else
1301 return 1;
1302#endif
1303}
1304
1305/**
1306 * Force async tsc mode.
1307 * @todo add a module argument for this.
1308 */
1309bool VBOXCALL supdrvOSGetForcedAsyncTscMode(void)
1310{
1311 return false;
1312}
1313
1314
1315/**
1316 * Converts a supdrv error code to an linux error code.
1317 *
1318 * @returns corresponding linux error code.
1319 * @param rc supdrv error code (SUPDRV_ERR_* defines).
1320 */
1321static int VBoxDrvLinuxErr2LinuxErr(int rc)
1322{
1323 switch (rc)
1324 {
1325 case 0: return 0;
1326 case SUPDRV_ERR_GENERAL_FAILURE: return -EACCES;
1327 case SUPDRV_ERR_INVALID_PARAM: return -EINVAL;
1328 case SUPDRV_ERR_INVALID_MAGIC: return -EILSEQ;
1329 case SUPDRV_ERR_INVALID_HANDLE: return -ENXIO;
1330 case SUPDRV_ERR_INVALID_POINTER: return -EFAULT;
1331 case SUPDRV_ERR_LOCK_FAILED: return -ENOLCK;
1332 case SUPDRV_ERR_ALREADY_LOADED: return -EEXIST;
1333 case SUPDRV_ERR_PERMISSION_DENIED: return -EPERM;
1334 case SUPDRV_ERR_VERSION_MISMATCH: return -ENOSYS;
1335 case SUPDRV_ERR_IDT_FAILED: return -1000;
1336 }
1337
1338 return -EPERM;
1339}
1340
1341
1342RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
1343{
1344#if 1
1345 va_list args;
1346 char szMsg[512];
1347
1348 va_start(args, pszFormat);
1349 vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, args);
1350 szMsg[sizeof(szMsg) - 1] = '\0';
1351 printk("%s", szMsg);
1352 va_end(args);
1353#else
1354 /* forward to printf - needs some more GCC hacking to fix ebp... */
1355 __asm__ __volatile__ ("mov %0, %esp\n\t"
1356 "jmp %1\n\t",
1357 :: "r" ((uintptr_t)&pszFormat - 4),
1358 "m" (printk));
1359#endif
1360 return 0;
1361}
1362
1363
1364/** Runtime assert implementation for Linux Ring-0. */
1365RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1366{
1367 printk("!!Assertion Failed!!\n"
1368 "Expression: %s\n"
1369 "Location : %s(%d) %s\n",
1370 pszExpr, pszFile, uLine, pszFunction);
1371}
1372
1373
1374/** Runtime assert implementation for Linux Ring-0. */
1375RTDECL(void) AssertMsg2(const char *pszFormat, ...)
1376{ /* forwarder. */
1377 va_list ap;
1378 char msg[256];
1379
1380 va_start(ap, pszFormat);
1381 vsnprintf(msg, sizeof(msg) - 1, pszFormat, ap);
1382 msg[sizeof(msg) - 1] = '\0';
1383 printk("%s", msg);
1384 va_end(ap);
1385}
1386
1387
1388/* GCC C++ hack. */
1389unsigned __gxx_personality_v0 = 0xcccccccc;
1390
1391
1392module_init(VBoxDrvLinuxInit);
1393module_exit(VBoxDrvLinuxUnload);
1394
1395MODULE_AUTHOR("innotek GmbH");
1396MODULE_DESCRIPTION("VirtualBox Support Driver");
1397MODULE_LICENSE("GPL");
1398#ifdef MODULE_VERSION
1399#define xstr(s) str(s)
1400#define str(s) #s
1401MODULE_VERSION(VBOX_VERSION_STRING " (" xstr(SUPDRVIOC_VERSION) ")");
1402#endif
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