VirtualBox

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

Last change on this file since 5985 was 5985, checked in by vboxsync, 17 years ago

Linux support module: print version of module into kernel log when successfully loaded

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