VirtualBox

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

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

vboxdrv: made it compile with Linux 2.6.24 if CONFIG_VBOXDRV_FIXEDMAJOR is defined

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