VirtualBox

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

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

USE_NEW_OS_INTERFACE_FOR_MM

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 56.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 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/* devfs defines */
69#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
70# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
71
72# define VBOX_REGISTER_DEVFS() \
73({ \
74 void *rc = NULL; \
75 if (devfs_mk_cdev(MKDEV(DEVICE_MAJOR, 0), \
76 S_IFCHR | S_IRUGO | S_IWUGO, \
77 DEVICE_NAME) == 0) \
78 rc = (void *)' '; /* return not NULL */ \
79 rc; \
80 })
81
82# define VBOX_UNREGISTER_DEVFS(handle) \
83 devfs_remove(DEVICE_NAME);
84
85# else /* < 2.6.0 */
86
87# define VBOX_REGISTER_DEVFS() \
88 devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, \
89 DEVICE_MAJOR, 0, \
90 S_IFCHR | S_IRUGO | S_IWUGO, \
91 &gFileOpsVBoxDrv, NULL)
92
93# define VBOX_UNREGISTER_DEVFS(handle) \
94 if (handle != NULL) \
95 devfs_unregister(handle)
96
97# endif /* < 2.6.0 */
98#endif /* CONFIG_DEV_FS && !CONFIG_VBOXDEV_AS_MISC */
99
100#ifndef CONFIG_VBOXDRV_AS_MISC
101# if defined(CONFIG_DEVFS_FS) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 0)
102# define VBOX_REGISTER_DEVICE(a,b,c) devfs_register_chrdev(a,b,c)
103# define VBOX_UNREGISTER_DEVICE(a,b) devfs_unregister_chrdev(a,b)
104# else
105# define VBOX_REGISTER_DEVICE(a,b,c) register_chrdev(a,b,c)
106# define VBOX_UNREGISTER_DEVICE(a,b) unregister_chrdev(a,b)
107# endif
108#endif /* !CONFIG_VBOXDRV_AS_MISC */
109
110
111#ifdef CONFIG_X86_HIGH_ENTRY
112# error "CONFIG_X86_HIGH_ENTRY is not supported by VBoxDrv at this time."
113#endif
114
115/*
116 * This sucks soooo badly on x86! Why don't they export __PAGE_KERNEL_EXEC so PAGE_KERNEL_EXEC would be usable?
117 */
118#if defined(RT_ARCH_AMD64)
119# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL_EXEC
120#elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
121# define MY_PAGE_KERNEL_EXEC __pgprot(cpu_has_pge ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC)
122#else
123# define MY_PAGE_KERNEL_EXEC PAGE_KERNEL
124#endif
125
126/*
127 * The redhat hack section.
128 * - The current hacks are for 2.4.21-15.EL only.
129 */
130#ifndef NO_REDHAT_HACKS
131/* accounting. */
132# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
133# ifdef VM_ACCOUNT
134# define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c, 0) /* should it be 1 or 0? */
135# endif
136# endif
137
138/* backported remap_page_range. */
139# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
140# include <asm/tlb.h>
141# ifdef tlb_vma /* probably not good enough... */
142# define HAVE_26_STYLE_REMAP_PAGE_RANGE 1
143# endif
144# endif
145
146# ifndef RT_ARCH_AMD64
147/* In 2.6.9-22.ELsmp we have to call change_page_attr() twice when changing
148 * the page attributes from PAGE_KERNEL to something else, because there appears
149 * to be a bug in one of the many patches that redhat applied.
150 * It should be safe to do this on less buggy linux kernels too. ;-)
151 */
152# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
153 do { \
154 if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) \
155 change_page_attr(pPages, cPages, prot); \
156 change_page_attr(pPages, cPages, prot); \
157 } while (0)
158# endif
159#endif /* !NO_REDHAT_HACKS */
160
161
162#ifndef MY_DO_MUNMAP
163# define MY_DO_MUNMAP(a,b,c) do_munmap(a, b, c)
164#endif
165
166#ifndef MY_CHANGE_PAGE_ATTR
167# ifdef RT_ARCH_AMD64 /** @todo This is a cheap hack, but it'll get around that 'else BUG();' in __change_page_attr(). */
168# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) \
169 do { \
170 change_page_attr(pPages, cPages, PAGE_KERNEL_NOCACHE); \
171 change_page_attr(pPages, cPages, prot); \
172 } while (0)
173# else
174# define MY_CHANGE_PAGE_ATTR(pPages, cPages, prot) change_page_attr(pPages, cPages, prot)
175# endif
176#endif
177
178
179/** @def ONE_MSEC_IN_JIFFIES
180 * The number of jiffies that make up 1 millisecond. This is only actually used
181 * when HZ is > 1000. */
182#if HZ <= 1000
183# define ONE_MSEC_IN_JIFFIES 0
184#elif !(HZ % 1000)
185# define ONE_MSEC_IN_JIFFIES (HZ / 1000)
186#else
187# define ONE_MSEC_IN_JIFFIES ((HZ + 999) / 1000)
188# error "HZ is not a multiple of 1000, the GIP stuff won't work right!"
189#endif
190
191#ifdef CONFIG_X86_LOCAL_APIC
192
193/* If an NMI occurs while we are inside the world switcher the machine will
194 * crash. The Linux NMI watchdog generates periodic NMIs increasing a counter
195 * which is compared with another counter increased in the timer interrupt
196 * handler. We disable the NMI watchdog.
197 *
198 * - Linux >= 2.6.21: The watchdog is disabled by default on i386 and x86_64.
199 * - Linux < 2.6.21: The watchdog is normally enabled by default on x86_64
200 * and disabled on i386.
201 */
202# if defined(RT_ARCH_AMD64)
203# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21)
204# define DO_DISABLE_NMI 1
205# endif
206# endif
207
208# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
209extern int nmi_active;
210# define nmi_atomic_read(P) *(P)
211# define nmi_atomic_set(P, V) *(P) = (V)
212# define nmi_atomic_dec(P) nmi_atomic_set(P, 0)
213# else
214# define nmi_atomic_read(P) atomic_read(P)
215# define nmi_atomic_set(P, V) atomic_set(P, V)
216# define nmi_atomic_dec(P) atomic_dec(P)
217# endif
218
219# ifndef X86_FEATURE_ARCH_PERFMON
220# define X86_FEATURE_ARCH_PERFMON (3*32+9) /* Intel Architectural PerfMon */
221# endif
222# ifndef MSR_ARCH_PERFMON_EVENTSEL0
223# define MSR_ARCH_PERFMON_EVENTSEL0 0x186
224# endif
225# ifndef ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT
226# define ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT (1 << 0)
227# endif
228
229#endif /* CONFIG_X86_LOCAL_APIC */
230
231
232/*******************************************************************************
233* Defined Constants And Macros *
234*******************************************************************************/
235/**
236 * Device extention & session data association structure.
237 */
238static SUPDRVDEVEXT g_DevExt;
239
240/** Timer structure for the GIP update. */
241static struct timer_list g_GipTimer;
242/** Pointer to the page structure for the GIP. */
243struct page *g_pGipPage;
244
245/** Registered devfs device handle. */
246#if defined(CONFIG_DEVFS_FS) && !defined(CONFIG_VBOXDRV_AS_MISC)
247# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
248static void *g_hDevFsVBoxDrv = NULL;
249# else
250static devfs_handle_t g_hDevFsVBoxDrv = NULL;
251# endif
252#endif
253
254#ifndef CONFIG_VBOXDRV_AS_MISC
255/** Module major number */
256#define DEVICE_MAJOR 234
257/** Saved major device number */
258static int g_iModuleMajor;
259#endif /* !CONFIG_VBOXDRV_AS_MISC */
260
261/** The module name. */
262#define DEVICE_NAME "vboxdrv"
263
264#ifdef RT_ARCH_AMD64
265/**
266 * Memory for the executable memory heap (in IPRT).
267 */
268extern uint8_t g_abExecMemory[1572864]; /* 1.5 MB */
269__asm__(".section execmemory, \"awx\", @progbits\n\t"
270 ".align 32\n\t"
271 ".globl g_abExecMemory\n"
272 "g_abExecMemory:\n\t"
273 ".zero 1572864\n\t"
274 ".type g_abExecMemory, @object\n\t"
275 ".size g_abExecMemory, 1572864\n\t"
276 ".text\n\t");
277#endif
278
279
280/*******************************************************************************
281* Internal Functions *
282*******************************************************************************/
283static int VBoxSupDrvInit(void);
284static void VBoxSupDrvUnload(void);
285static int VBoxSupDrvCreate(struct inode *pInode, struct file *pFilp);
286static int VBoxSupDrvClose(struct inode *pInode, struct file *pFilp);
287static int VBoxSupDrvDeviceControl(struct inode *pInode, struct file *pFilp,
288 unsigned int IOCmd, unsigned long IOArg);
289#ifndef USE_NEW_OS_INTERFACE_FOR_MM
290static RTR3PTR VBoxSupDrvMapUser(struct page **papPages, unsigned cPages, unsigned fProt, pgprot_t pgFlags);
291#endif /* !USE_NEW_OS_INTERFACE_FOR_MM */
292static int VBoxSupDrvInitGip(PSUPDRVDEVEXT pDevExt);
293static int VBoxSupDrvTermGip(PSUPDRVDEVEXT pDevExt);
294static void VBoxSupGipTimer(unsigned long ulUser);
295#ifdef CONFIG_SMP
296static void VBoxSupGipTimerPerCpu(unsigned long ulUser);
297static void VBoxSupGipResumePerCpu(void *pvUser);
298#endif
299static int VBoxSupDrvErr2LinuxErr(int);
300
301
302/** The file_operations structure. */
303static struct file_operations gFileOpsVBoxDrv =
304{
305 owner: THIS_MODULE,
306 open: VBoxSupDrvCreate,
307 release: VBoxSupDrvClose,
308 ioctl: VBoxSupDrvDeviceControl,
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 VBoxSupDrvInit(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 = VBoxSupDrvInitGip(&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 VBoxSupDrvUnload(void)
657{
658 int rc;
659 dprintf(("VBoxSupDrvUnload\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 VBoxSupDrvTermGip(&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 VBoxSupDrvCreate(struct inode *pInode, struct file *pFilp)
701{
702 int rc;
703 PSUPDRVSESSION pSession;
704 dprintf(("VBoxSupDrvCreate: 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(("VBoxSupDrvCreate: g_DevExt=%p pSession=%p rc=%d\n", &g_DevExt, pSession, rc));
719 pFilp->private_data = pSession;
720
721 return VBoxSupDrvErr2LinuxErr(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 VBoxSupDrvClose(struct inode *pInode, struct file *pFilp)
732{
733 dprintf(("VBoxSupDrvClose: 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 IOCmd The function specified to ioctl().
746 * @param IOArg The argument specified to ioctl().
747 */
748static int VBoxSupDrvDeviceControl(struct inode *pInode, struct file *pFilp,
749 unsigned int IOCmd, unsigned long IOArg)
750{
751 int rc;
752 SUPDRVIOCTLDATA Args;
753 void *pvBuf = NULL;
754 int cbBuf = 0;
755 unsigned cbOut = 0;
756
757 dprintf2(("VBoxSupDrvDeviceControl: pFilp=%p IOCmd=%x IOArg=%p\n", pFilp, IOCmd, (void *)IOArg));
758
759 /*
760 * Copy ioctl data structure from user space.
761 */
762 if (_IOC_SIZE(IOCmd) != sizeof(SUPDRVIOCTLDATA))
763 {
764 dprintf(("VBoxSupDrvDeviceControl: incorrect input length! cbArgs=%d\n", _IOC_SIZE(IOCmd)));
765 return -EINVAL;
766 }
767 if (copy_from_user(&Args, (void *)IOArg, _IOC_SIZE(IOCmd)))
768 {
769 dprintf(("VBoxSupDrvDeviceControl: copy_from_user(&Args) failed.\n"));
770 return -EFAULT;
771 }
772
773 /*
774 * Allocate and copy user space input data buffer to kernel space.
775 */
776 if (Args.cbIn > 0 || Args.cbOut > 0)
777 {
778 cbBuf = max(Args.cbIn, Args.cbOut);
779 pvBuf = vmalloc(cbBuf);
780 if (pvBuf == NULL)
781 {
782 dprintf(("VBoxSupDrvDeviceControl: failed to allocate buffer of %d bytes.\n", cbBuf));
783 return -ENOMEM;
784 }
785
786 if (copy_from_user(pvBuf, (void *)Args.pvIn, Args.cbIn))
787 {
788 dprintf(("VBoxSupDrvDeviceControl: copy_from_user(pvBuf) failed.\n"));
789 vfree(pvBuf);
790 return -EFAULT;
791 }
792 }
793
794 /*
795 * Process the IOCtl.
796 */
797 rc = supdrvIOCtl(IOCmd, &g_DevExt, (PSUPDRVSESSION)pFilp->private_data,
798 pvBuf, Args.cbIn, pvBuf, Args.cbOut, &cbOut);
799
800 /*
801 * Copy ioctl data and output buffer back to user space.
802 */
803 if (rc)
804 {
805 dprintf(("VBoxSupDrvDeviceControl: pFilp=%p IOCmd=%x IOArg=%p failed, rc=%d (linux rc=%d)\n",
806 pFilp, IOCmd, (void *)IOArg, rc, VBoxSupDrvErr2LinuxErr(rc)));
807 rc = VBoxSupDrvErr2LinuxErr(rc);
808 }
809 else if (cbOut > 0)
810 {
811 if (pvBuf != NULL && cbOut <= cbBuf)
812 {
813 if (copy_to_user((void *)Args.pvOut, pvBuf, cbOut))
814 {
815 dprintf(("copy_to_user failed.\n"));
816 rc = -EFAULT;
817 }
818 }
819 else
820 {
821 dprintf(("WHAT!?! supdrvIOCtl messed up! cbOut=%d cbBuf=%d pvBuf=%p\n", cbOut, cbBuf, pvBuf));
822 rc = -EPERM;
823 }
824 }
825
826 if (pvBuf)
827 vfree(pvBuf);
828
829 dprintf2(("VBoxSupDrvDeviceControl: returns %d\n", rc));
830 return rc;
831}
832
833
834/**
835 * Initializes any OS specific object creator fields.
836 */
837void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
838{
839 NOREF(pObj);
840 NOREF(pSession);
841}
842
843
844/**
845 * Checks if the session can access the object.
846 *
847 * @returns true if a decision has been made.
848 * @returns false if the default access policy should be applied.
849 *
850 * @param pObj The object in question.
851 * @param pSession The session wanting to access the object.
852 * @param pszObjName The object name, can be NULL.
853 * @param prc Where to store the result when returning true.
854 */
855bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
856{
857 NOREF(pObj);
858 NOREF(pSession);
859 NOREF(pszObjName);
860 NOREF(prc);
861 return false;
862}
863
864
865#ifndef USE_NEW_OS_INTERFACE_FOR_MM
866
867/**
868 * Compute order. Some functions allocate 2^order pages.
869 *
870 * @returns order.
871 * @param cPages Number of pages.
872 */
873static int VBoxSupDrvOrder(unsigned long cPages)
874{
875 int iOrder;
876 unsigned long cTmp;
877
878 for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
879 ;
880 if (cPages & ~(1 << iOrder))
881 ++iOrder;
882
883 return iOrder;
884}
885
886
887/**
888 * OS Specific code for locking down memory.
889 *
890 * @returns 0 on success.
891 * @returns SUPDRV_ERR_* on failure.
892 * @param pMem Pointer to memory.
893 * This is not linked in anywhere.
894 * @param paPages Array which should be filled with the address of the physical pages.
895 *
896 * @remark See sgl_map_user_pages() for an example of an similar function.
897 */
898int VBOXCALL supdrvOSLockMemOne(PSUPDRVMEMREF pMem, PSUPPAGE paPages)
899{
900 int rc;
901 struct page **papPages;
902 unsigned iPage;
903 unsigned cPages = pMem->cb >> PAGE_SHIFT;
904 unsigned long pv = (unsigned long)pMem->pvR3;
905 struct vm_area_struct **papVMAs;
906
907 /*
908 * Allocate page pointer array.
909 */
910 papPages = vmalloc(cPages * sizeof(*papPages));
911 if (!papPages)
912 return SUPDRV_ERR_NO_MEMORY;
913
914 /*
915 * Allocate the VMA pointer array.
916 */
917 papVMAs = vmalloc(cPages * sizeof(*papVMAs));
918 if (!papVMAs)
919 return SUPDRV_ERR_NO_MEMORY;
920
921 /*
922 * Get user pages.
923 */
924 down_read(&current->mm->mmap_sem);
925 rc = get_user_pages(current, /* Task for fault acounting. */
926 current->mm, /* Whose pages. */
927 (unsigned long)pv, /* Where from. */
928 cPages, /* How many pages. */
929 1, /* Write to memory. */
930 0, /* force. */
931 papPages, /* Page array. */
932 papVMAs); /* vmas */
933 if (rc != cPages)
934 {
935 up_read(&current->mm->mmap_sem);
936 dprintf(("supdrvOSLockMemOne: get_user_pages failed. rc=%d\n", rc));
937 return SUPDRV_ERR_LOCK_FAILED;
938 }
939
940 for (iPage = 0; iPage < cPages; iPage++)
941 flush_dcache_page(papPages[iPage]);
942 up_read(&current->mm->mmap_sem);
943
944 pMem->u.locked.papPages = papPages;
945 pMem->u.locked.cPages = cPages;
946
947 /*
948 * Get addresses, protect against fork()
949 */
950 for (iPage = 0; iPage < cPages; iPage++)
951 {
952 paPages[iPage].Phys = page_to_phys(papPages[iPage]);
953 paPages[iPage].uReserved = 0;
954 papVMAs[iPage]->vm_flags |= VM_DONTCOPY;
955 }
956
957 vfree(papVMAs);
958
959 dprintf2(("supdrvOSLockMemOne: pvR3=%p cb=%d papPages=%p\n",
960 pMem->pvR3, pMem->cb, pMem->u.locked.papPages));
961 return 0;
962}
963
964
965/**
966 * Unlocks the memory pointed to by pv.
967 *
968 * @param pMem Pointer to memory to unlock.
969 *
970 * @remark See sgl_unmap_user_pages() for an example of an similar function.
971 */
972void VBOXCALL supdrvOSUnlockMemOne(PSUPDRVMEMREF pMem)
973{
974 unsigned iPage;
975 dprintf2(("supdrvOSUnlockMemOne: pvR3=%p cb=%d papPages=%p\n",
976 pMem->pvR3, pMem->cb, pMem->u.locked.papPages));
977
978 /*
979 * Loop thru the pages and release them.
980 */
981 for (iPage = 0; iPage < pMem->u.locked.cPages; iPage++)
982 {
983 if (!PageReserved(pMem->u.locked.papPages[iPage]))
984 SetPageDirty(pMem->u.locked.papPages[iPage]);
985 page_cache_release(pMem->u.locked.papPages[iPage]);
986 }
987
988 /* free the page array */
989 vfree(pMem->u.locked.papPages);
990 pMem->u.locked.cPages = 0;
991}
992
993
994/**
995 * OS Specific code for allocating page aligned memory with continuous fixed
996 * physical paged backing.
997 *
998 * @returns 0 on success.
999 * @returns SUPDRV_ERR_* on failure.
1000 * @param pMem Memory reference record of the memory to be allocated.
1001 * (This is not linked in anywhere.)
1002 * @param ppvR0 Where to store the virtual address of the ring-0 mapping. (optional)
1003 * @param ppvR3 Where to store the virtual address of the ring-3 mapping.
1004 * @param pHCPhys Where to store the physical address.
1005 */
1006int VBOXCALL supdrvOSContAllocOne(PSUPDRVMEMREF pMem, PRTR0PTR ppvR0, PRTR3PTR ppvR3, PRTHCPHYS pHCPhys)
1007{
1008 struct page *paPages;
1009 unsigned iPage;
1010 unsigned cbAligned = RT_ALIGN(pMem->cb, PAGE_SIZE);
1011 unsigned cPages = cbAligned >> PAGE_SHIFT;
1012 unsigned cOrder = VBoxSupDrvOrder(cPages);
1013 unsigned long ulAddr;
1014 dma_addr_t HCPhys;
1015 int rc = 0;
1016 pgprot_t pgFlags;
1017 pgprot_val(pgFlags) = _PAGE_PRESENT | _PAGE_RW | _PAGE_USER;
1018
1019 Assert(ppvR3);
1020 Assert(pHCPhys);
1021
1022 /*
1023 * Allocate page pointer array.
1024 */
1025#ifdef RT_ARCH_AMD64 /** @todo check out if there is a correct way of getting memory below 4GB (physically). */
1026 paPages = alloc_pages(GFP_DMA, cOrder);
1027#else
1028 paPages = alloc_pages(GFP_USER, cOrder);
1029#endif
1030 if (!paPages)
1031 return SUPDRV_ERR_NO_MEMORY;
1032
1033 /*
1034 * Lock the pages.
1035 */
1036 for (iPage = 0; iPage < cPages; iPage++)
1037 {
1038 SetPageReserved(&paPages[iPage]);
1039 if (!PageHighMem(&paPages[iPage]) && pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
1040 MY_CHANGE_PAGE_ATTR(&paPages[iPage], 1, MY_PAGE_KERNEL_EXEC);
1041#ifdef DEBUG
1042 if (iPage + 1 < cPages && (page_to_phys((&paPages[iPage])) + 0x1000) != page_to_phys((&paPages[iPage + 1])))
1043 {
1044 dprintf(("supdrvOSContAllocOne: Pages are not continuous!!!! iPage=%d phys=%llx physnext=%llx\n",
1045 iPage, (long long)page_to_phys((&paPages[iPage])), (long long)page_to_phys((&paPages[iPage + 1]))));
1046 BUG();
1047 }
1048#endif
1049 }
1050 HCPhys = page_to_phys(paPages);
1051
1052 /*
1053 * Allocate user space mapping and put the physical pages into it.
1054 */
1055 down_write(&current->mm->mmap_sem);
1056 ulAddr = do_mmap(NULL, 0, cbAligned, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_ANONYMOUS, 0);
1057 if (!(ulAddr & ~PAGE_MASK))
1058 {
1059#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && !defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1060 int rc2 = remap_page_range(ulAddr, HCPhys, cbAligned, pgFlags);
1061#else
1062 int rc2 = 0;
1063 struct vm_area_struct *vma = find_vma(current->mm, ulAddr);
1064 if (vma)
1065#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
1066 rc2 = remap_page_range(vma, ulAddr, HCPhys, cbAligned, pgFlags);
1067#else
1068 rc2 = remap_pfn_range(vma, ulAddr, HCPhys >> PAGE_SHIFT, cbAligned, pgFlags);
1069#endif
1070 else
1071 {
1072 rc = SUPDRV_ERR_NO_MEMORY;
1073 dprintf(("supdrvOSContAllocOne: no vma found for ulAddr=%#lx!\n", ulAddr));
1074 }
1075#endif
1076 if (rc2)
1077 {
1078 rc = SUPDRV_ERR_NO_MEMORY;
1079 dprintf(("supdrvOSContAllocOne: remap_page_range failed rc2=%d\n", rc2));
1080 }
1081 }
1082 else
1083 {
1084 dprintf(("supdrvOSContAllocOne: do_mmap failed ulAddr=%#lx\n", ulAddr));
1085 rc = SUPDRV_ERR_NO_MEMORY;
1086 }
1087 up_write(&current->mm->mmap_sem); /* not quite sure when to give this up. */
1088
1089 /*
1090 * Success?
1091 */
1092 if (!rc)
1093 {
1094 *pHCPhys = HCPhys;
1095 *ppvR3 = ulAddr;
1096 if (ppvR0)
1097 *ppvR0 = (void *)ulAddr;
1098 pMem->pvR3 = ulAddr;
1099 pMem->pvR0 = NULL;
1100 pMem->u.cont.paPages = paPages;
1101 pMem->u.cont.cPages = cPages;
1102 pMem->cb = cbAligned;
1103
1104 dprintf2(("supdrvOSContAllocOne: pvR0=%p pvR3=%p cb=%d paPages=%p *pHCPhys=%lx *ppvR0=*ppvR3=%p\n",
1105 pMem->pvR0, pMem->pvR3, pMem->cb, paPages, (unsigned long)*pHCPhys, *ppvR3));
1106 global_flush_tlb();
1107 return 0;
1108 }
1109
1110 /*
1111 * Failure, cleanup and be gone.
1112 */
1113 down_write(&current->mm->mmap_sem);
1114 if (ulAddr & ~PAGE_MASK)
1115 MY_DO_MUNMAP(current->mm, ulAddr, pMem->cb);
1116 for (iPage = 0; iPage < cPages; iPage++)
1117 {
1118 ClearPageReserved(&paPages[iPage]);
1119 if (!PageHighMem(&paPages[iPage]) && pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
1120 MY_CHANGE_PAGE_ATTR(&paPages[iPage], 1, PAGE_KERNEL);
1121 }
1122 up_write(&current->mm->mmap_sem); /* check when we can leave this. */
1123 __free_pages(paPages, cOrder);
1124
1125 global_flush_tlb();
1126 return rc;
1127}
1128
1129
1130/**
1131 * Frees contiguous memory.
1132 *
1133 * @param pMem Memory reference record of the memory to be freed.
1134 */
1135void VBOXCALL supdrvOSContFreeOne(PSUPDRVMEMREF pMem)
1136{
1137 unsigned iPage;
1138
1139 dprintf2(("supdrvOSContFreeOne: pvR0=%p pvR3=%p cb=%d paPages=%p\n",
1140 pMem->pvR0, pMem->pvR3, pMem->cb, pMem->u.cont.paPages));
1141
1142 /*
1143 * do_exit() destroys the mm before closing files.
1144 * I really hope it cleans up our stuff properly...
1145 */
1146 if (current->mm)
1147 {
1148 down_write(&current->mm->mmap_sem);
1149 MY_DO_MUNMAP(current->mm, (unsigned long)pMem->pvR3, pMem->cb);
1150 up_write(&current->mm->mmap_sem); /* check when we can leave this. */
1151 }
1152
1153 /*
1154 * Change page attributes freeing the pages.
1155 */
1156 for (iPage = 0; iPage < pMem->u.cont.cPages; iPage++)
1157 {
1158 ClearPageReserved(&pMem->u.cont.paPages[iPage]);
1159 if (!PageHighMem(&pMem->u.cont.paPages[iPage]) && pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
1160 MY_CHANGE_PAGE_ATTR(&pMem->u.cont.paPages[iPage], 1, PAGE_KERNEL);
1161 }
1162 __free_pages(pMem->u.cont.paPages, VBoxSupDrvOrder(pMem->u.cont.cPages));
1163
1164 pMem->u.cont.cPages = 0;
1165}
1166
1167
1168/**
1169 * Allocates memory which mapped into both kernel and user space.
1170 * The returned memory is page aligned and so is the allocation.
1171 *
1172 * @returns 0 on success.
1173 * @returns SUPDRV_ERR_* on failure.
1174 * @param pMem Memory reference record of the memory to be allocated.
1175 * (This is not linked in anywhere.)
1176 * @param ppvR0 Where to store the address of the Ring-0 mapping.
1177 * @param ppvR3 Where to store the address of the Ring-3 mapping.
1178 */
1179int VBOXCALL supdrvOSMemAllocOne(PSUPDRVMEMREF pMem, PRTR0PTR ppvR0, PRTR3PTR ppvR3)
1180{
1181 const unsigned cbAligned = RT_ALIGN(pMem->cb, PAGE_SIZE);
1182 const unsigned cPages = cbAligned >> PAGE_SHIFT;
1183#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 22)
1184 unsigned cOrder = VBoxSupDrvOrder(cPages);
1185 struct page *paPages;
1186#endif
1187 struct page **papPages;
1188 unsigned iPage;
1189 pgprot_t pgFlags;
1190 pgprot_val(pgFlags) = _PAGE_PRESENT | _PAGE_RW | _PAGE_USER;
1191
1192 /*
1193 * Allocate array with page pointers.
1194 */
1195 pMem->u.mem.cPages = 0;
1196 pMem->u.mem.papPages = papPages = kmalloc(sizeof(papPages[0]) * cPages, GFP_KERNEL);
1197 if (!papPages)
1198 return SUPDRV_ERR_NO_MEMORY;
1199
1200 /*
1201 * Allocate the pages.
1202 */
1203#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1204 for (iPage = 0; iPage < cPages; iPage++)
1205 {
1206 papPages[iPage] = alloc_page(GFP_HIGHUSER);
1207 if (!papPages[iPage])
1208 {
1209 pMem->u.mem.cPages = iPage;
1210 supdrvOSMemFreeOne(pMem);
1211 return SUPDRV_ERR_NO_MEMORY;
1212 }
1213 }
1214
1215#else /* < 2.4.22 */
1216 paPages = alloc_pages(GFP_USER, cOrder);
1217 if (!paPages)
1218 {
1219 supdrvOSMemFreeOne(pMem);
1220 return SUPDRV_ERR_NO_MEMORY;
1221 }
1222 for (iPage = 0; iPage < cPages; iPage++)
1223 {
1224 papPages[iPage] = &paPages[iPage];
1225 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
1226 MY_CHANGE_PAGE_ATTR(papPages[iPage], 1, MY_PAGE_KERNEL_EXEC);
1227 if (PageHighMem(papPages[iPage]))
1228 BUG();
1229 }
1230#endif
1231 pMem->u.mem.cPages = cPages;
1232
1233 /*
1234 * Reserve the pages.
1235 */
1236 for (iPage = 0; iPage < cPages; iPage++)
1237 SetPageReserved(papPages[iPage]);
1238
1239 /*
1240 * Create the Ring-0 mapping.
1241 */
1242 if (ppvR0)
1243 {
1244#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1245# ifdef VM_MAP
1246 *ppvR0 = pMem->pvR0 = vmap(papPages, cPages, VM_MAP, pgFlags);
1247# else
1248 *ppvR0 = pMem->pvR0 = vmap(papPages, cPages, VM_ALLOC, pgFlags);
1249# endif
1250#else
1251 *ppvR0 = pMem->pvR0 = phys_to_virt(page_to_phys(papPages[0]));
1252#endif
1253 }
1254 if (pMem->pvR0 || !ppvR0)
1255 {
1256 /*
1257 * Create the ring3 mapping.
1258 */
1259 if (ppvR3)
1260 *ppvR3 = pMem->pvR3 = VBoxSupDrvMapUser(papPages, cPages, PROT_READ | PROT_WRITE | PROT_EXEC, pgFlags);
1261 if (pMem->pvR3 || !ppvR3)
1262 return 0;
1263 dprintf(("supdrvOSMemAllocOne: failed to map into r3! cPages=%u\n", cPages));
1264 }
1265 else
1266 dprintf(("supdrvOSMemAllocOne: failed to map into r0! cPages=%u\n", cPages));
1267
1268 supdrvOSMemFreeOne(pMem);
1269 return SUPDRV_ERR_NO_MEMORY;
1270}
1271
1272
1273/**
1274 * Get the physical addresses of the pages in the allocation.
1275 * This is called while inside bundle the spinlock.
1276 *
1277 * @param pMem Memory reference record of the memory.
1278 * @param paPages Where to store the page addresses.
1279 */
1280void VBOXCALL supdrvOSMemGetPages(PSUPDRVMEMREF pMem, PSUPPAGE paPages)
1281{
1282 unsigned iPage;
1283 for (iPage = 0; iPage < pMem->u.mem.cPages; iPage++)
1284 {
1285 paPages[iPage].Phys = page_to_phys(pMem->u.mem.papPages[iPage]);
1286 paPages[iPage].uReserved = 0;
1287 }
1288}
1289
1290
1291/**
1292 * Frees memory allocated by supdrvOSMemAllocOne().
1293 *
1294 * @param pMem Memory reference record of the memory to be free.
1295 */
1296void VBOXCALL supdrvOSMemFreeOne(PSUPDRVMEMREF pMem)
1297{
1298 dprintf2(("supdrvOSMemFreeOne: pvR0=%p pvR3=%p cb=%d cPages=%d papPages=%p\n",
1299 pMem->pvR0, pMem->pvR3, pMem->cb, pMem->u.mem.cPages, pMem->u.mem.papPages));
1300
1301 /*
1302 * Unmap the user mapping (if any).
1303 * do_exit() destroys the mm before closing files.
1304 */
1305 if (pMem->pvR3 && current->mm)
1306 {
1307 down_write(&current->mm->mmap_sem);
1308 MY_DO_MUNMAP(current->mm, (unsigned long)pMem->pvR3, RT_ALIGN(pMem->cb, PAGE_SIZE));
1309 up_write(&current->mm->mmap_sem); /* check when we can leave this. */
1310 }
1311 pMem->pvR3 = NIL_RTR3PTR;
1312
1313 /*
1314 * Unmap the kernel mapping (if any).
1315 */
1316 if (pMem->pvR0)
1317 {
1318#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1319 vunmap(pMem->pvR0);
1320#endif
1321 pMem->pvR0 = NULL;
1322 }
1323
1324 /*
1325 * Free the physical pages.
1326 */
1327 if (pMem->u.mem.papPages)
1328 {
1329 struct page **papPages = pMem->u.mem.papPages;
1330 const unsigned cPages = pMem->u.mem.cPages;
1331 unsigned iPage;
1332
1333 /* Restore the page flags. */
1334 for (iPage = 0; iPage < cPages; iPage++)
1335 {
1336 ClearPageReserved(papPages[iPage]);
1337#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 22)
1338 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
1339 MY_CHANGE_PAGE_ATTR(papPages[iPage], 1, PAGE_KERNEL);
1340#endif
1341 }
1342
1343 /* Free the pages. */
1344#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1345 for (iPage = 0; iPage < pMem->u.cont.cPages; iPage++)
1346 __free_page(papPages[iPage]);
1347#else
1348 if (cPages > 0)
1349 __free_pages(papPages[0], VBoxSupDrvOrder(cPages));
1350#endif
1351 /* Free the page pointer array. */
1352 kfree(papPages);
1353 pMem->u.mem.papPages = NULL;
1354 }
1355 pMem->u.mem.cPages = 0;
1356}
1357
1358
1359/**
1360 * Maps a range of pages into user space.
1361 *
1362 * @returns Pointer to the user space mapping on success.
1363 * @returns NULL on failure.
1364 * @param papPages Array of the pages to map.
1365 * @param cPages Number of pages to map.
1366 * @param fProt The mapping protection.
1367 * @param pgFlags The page level protection.
1368 */
1369static RTR3PTR VBoxSupDrvMapUser(struct page **papPages, unsigned cPages, unsigned fProt, pgprot_t pgFlags)
1370{
1371 int rc = SUPDRV_ERR_NO_MEMORY;
1372 unsigned long ulAddr;
1373
1374 /*
1375 * Allocate user space mapping.
1376 */
1377 down_write(&current->mm->mmap_sem);
1378 ulAddr = do_mmap(NULL, 0, cPages * PAGE_SIZE, fProt, MAP_SHARED | MAP_ANONYMOUS, 0);
1379 if (!(ulAddr & ~PAGE_MASK))
1380 {
1381 /*
1382 * Map page by page into the mmap area.
1383 * This is generic, paranoid and not very efficient.
1384 */
1385 int rc = 0;
1386 unsigned long ulAddrCur = ulAddr;
1387 unsigned iPage;
1388 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
1389 {
1390#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1391 struct vm_area_struct *vma = find_vma(current->mm, ulAddrCur);
1392 if (!vma)
1393 break;
1394#endif
1395
1396#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1397 rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(papPages[iPage]), PAGE_SIZE, pgFlags);
1398#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1399 rc = remap_page_range(vma, ulAddrCur, page_to_phys(papPages[iPage]), PAGE_SIZE, pgFlags);
1400#else /* 2.4 */
1401 rc = remap_page_range(ulAddrCur, page_to_phys(papPages[iPage]), PAGE_SIZE, pgFlags);
1402#endif
1403 if (rc)
1404 break;
1405 }
1406
1407 /*
1408 * Successful?
1409 */
1410 if (iPage >= cPages)
1411 {
1412 up_write(&current->mm->mmap_sem);
1413 return ulAddr;
1414 }
1415
1416 /* no, cleanup! */
1417 if (rc)
1418 dprintf(("VBoxSupDrvMapUser: remap_[page|pfn]_range failed! rc=%d\n", rc));
1419 else
1420 dprintf(("VBoxSupDrvMapUser: find_vma failed!\n"));
1421
1422 MY_DO_MUNMAP(current->mm, ulAddr, cPages << PAGE_SHIFT);
1423 }
1424 else
1425 {
1426 dprintf(("supdrvOSContAllocOne: do_mmap failed ulAddr=%#lx\n", ulAddr));
1427 rc = SUPDRV_ERR_NO_MEMORY;
1428 }
1429 up_write(&current->mm->mmap_sem);
1430
1431 return NIL_RTR3PTR;
1432}
1433
1434#endif /* !USE_NEW_OS_INTERFACE_FOR_MM */
1435
1436
1437/**
1438 * Initializes the GIP.
1439 *
1440 * @returns negative errno.
1441 * @param pDevExt Instance data. GIP stuff may be updated.
1442 */
1443static int VBoxSupDrvInitGip(PSUPDRVDEVEXT pDevExt)
1444{
1445 struct page *pPage;
1446 dma_addr_t HCPhys;
1447 PSUPGLOBALINFOPAGE pGip;
1448#ifdef CONFIG_SMP
1449 unsigned i;
1450#endif
1451 dprintf(("VBoxSupDrvInitGip:\n"));
1452
1453 /*
1454 * Allocate the page.
1455 */
1456 pPage = alloc_pages(GFP_USER, 0);
1457 if (!pPage)
1458 {
1459 dprintf(("VBoxSupDrvInitGip: failed to allocate the GIP page\n"));
1460 return -ENOMEM;
1461 }
1462
1463 /*
1464 * Lock the page.
1465 */
1466 SetPageReserved(pPage);
1467 g_pGipPage = pPage;
1468
1469 /*
1470 * Call common initialization routine.
1471 */
1472 HCPhys = page_to_phys(pPage);
1473 pGip = (PSUPGLOBALINFOPAGE)page_address(pPage);
1474 pDevExt->ulLastJiffies = jiffies;
1475#ifdef TICK_NSEC
1476 pDevExt->u64LastMonotime = (uint64_t)pDevExt->ulLastJiffies * TICK_NSEC;
1477 dprintf(("VBoxSupDrvInitGIP: TICK_NSEC=%ld HZ=%d jiffies=%ld now=%lld\n",
1478 TICK_NSEC, HZ, pDevExt->ulLastJiffies, pDevExt->u64LastMonotime));
1479#else
1480 pDevExt->u64LastMonotime = (uint64_t)pDevExt->ulLastJiffies * (1000000 / HZ);
1481 dprintf(("VBoxSupDrvInitGIP: TICK_NSEC=%d HZ=%d jiffies=%ld now=%lld\n",
1482 (int)(1000000 / HZ), HZ, pDevExt->ulLastJiffies, pDevExt->u64LastMonotime));
1483#endif
1484 supdrvGipInit(pDevExt, pGip, HCPhys, pDevExt->u64LastMonotime,
1485 HZ <= 1000 ? HZ : 1000);
1486
1487 /*
1488 * Initialize the timer.
1489 */
1490 init_timer(&g_GipTimer);
1491 g_GipTimer.data = (unsigned long)pDevExt;
1492 g_GipTimer.function = VBoxSupGipTimer;
1493 g_GipTimer.expires = jiffies;
1494#ifdef CONFIG_SMP
1495 for (i = 0; i < RT_ELEMENTS(pDevExt->aCPUs); i++)
1496 {
1497 pDevExt->aCPUs[i].u64LastMonotime = pDevExt->u64LastMonotime;
1498 pDevExt->aCPUs[i].ulLastJiffies = pDevExt->ulLastJiffies;
1499 pDevExt->aCPUs[i].iSmpProcessorId = -512;
1500 init_timer(&pDevExt->aCPUs[i].Timer);
1501 pDevExt->aCPUs[i].Timer.data = i;
1502 pDevExt->aCPUs[i].Timer.function = VBoxSupGipTimerPerCpu;
1503 pDevExt->aCPUs[i].Timer.expires = jiffies;
1504 }
1505#endif
1506
1507 return 0;
1508}
1509
1510
1511/**
1512 * Terminates the GIP.
1513 *
1514 * @returns negative errno.
1515 * @param pDevExt Instance data. GIP stuff may be updated.
1516 */
1517static int VBoxSupDrvTermGip(PSUPDRVDEVEXT pDevExt)
1518{
1519 struct page *pPage;
1520 PSUPGLOBALINFOPAGE pGip;
1521#ifdef CONFIG_SMP
1522 unsigned i;
1523#endif
1524 dprintf(("VBoxSupDrvTermGip:\n"));
1525
1526 /*
1527 * Delete the timer if it's pending.
1528 */
1529 if (timer_pending(&g_GipTimer))
1530 del_timer_sync(&g_GipTimer);
1531#ifdef CONFIG_SMP
1532 for (i = 0; i < RT_ELEMENTS(pDevExt->aCPUs); i++)
1533 if (timer_pending(&pDevExt->aCPUs[i].Timer))
1534 del_timer_sync(&pDevExt->aCPUs[i].Timer);
1535#endif
1536
1537 /*
1538 * Uninitialize the content.
1539 */
1540 pGip = pDevExt->pGip;
1541 pDevExt->pGip = NULL;
1542 if (pGip)
1543 supdrvGipTerm(pGip);
1544
1545 /*
1546 * Free the page.
1547 */
1548 pPage = g_pGipPage;
1549 g_pGipPage = NULL;
1550 if (pPage)
1551 {
1552 ClearPageReserved(pPage);
1553 __free_pages(pPage, 0);
1554 }
1555
1556 return 0;
1557}
1558
1559/**
1560 * Timer callback function.
1561 *
1562 * In ASYNC TSC mode this is called on the primary CPU, and we're
1563 * assuming that the CPU remains online.
1564 *
1565 * @param ulUser The device extension pointer.
1566 */
1567static void VBoxSupGipTimer(unsigned long ulUser)
1568{
1569 PSUPDRVDEVEXT pDevExt;
1570 PSUPGLOBALINFOPAGE pGip;
1571 unsigned long ulNow;
1572 unsigned long ulDiff;
1573 uint64_t u64Monotime;
1574 unsigned long SavedFlags;
1575
1576 local_irq_save(SavedFlags);
1577
1578 pDevExt = (PSUPDRVDEVEXT)ulUser;
1579 pGip = pDevExt->pGip;
1580 ulNow = jiffies;
1581
1582#ifdef CONFIG_SMP
1583 if (pGip && pGip->u32Mode == SUPGIPMODE_ASYNC_TSC)
1584 {
1585 uint8_t iCPU = ASMGetApicId();
1586 ulDiff = ulNow - pDevExt->aCPUs[iCPU].ulLastJiffies;
1587 pDevExt->aCPUs[iCPU].ulLastJiffies = ulNow;
1588#ifdef TICK_NSEC
1589 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * TICK_NSEC;
1590#else
1591 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * (1000000 / HZ);
1592#endif
1593 pDevExt->aCPUs[iCPU].u64LastMonotime = u64Monotime;
1594 }
1595 else
1596#endif /* CONFIG_SMP */
1597 {
1598 ulDiff = ulNow - pDevExt->ulLastJiffies;
1599 pDevExt->ulLastJiffies = ulNow;
1600#ifdef TICK_NSEC
1601 u64Monotime = pDevExt->u64LastMonotime + ulDiff * TICK_NSEC;
1602#else
1603 u64Monotime = pDevExt->u64LastMonotime + ulDiff * (1000000 / HZ);
1604#endif
1605 pDevExt->u64LastMonotime = u64Monotime;
1606 }
1607 if (RT_LIKELY(pGip))
1608 supdrvGipUpdate(pDevExt->pGip, u64Monotime);
1609 if (RT_LIKELY(!pDevExt->fGIPSuspended))
1610 mod_timer(&g_GipTimer, ulNow + (HZ <= 1000 ? 0 : ONE_MSEC_IN_JIFFIES));
1611
1612 local_irq_restore(SavedFlags);
1613}
1614
1615
1616#ifdef CONFIG_SMP
1617/**
1618 * Timer callback function for the other CPUs.
1619 *
1620 * @param iTimerCPU The APIC ID of this timer.
1621 */
1622static void VBoxSupGipTimerPerCpu(unsigned long iTimerCPU)
1623{
1624 PSUPDRVDEVEXT pDevExt;
1625 PSUPGLOBALINFOPAGE pGip;
1626 uint8_t iCPU;
1627 uint64_t u64Monotime;
1628 unsigned long SavedFlags;
1629
1630 local_irq_save(SavedFlags);
1631
1632 pDevExt = &g_DevExt;
1633 pGip = pDevExt->pGip;
1634 iCPU = ASMGetApicId();
1635
1636 if (RT_LIKELY(iCPU < RT_ELEMENTS(pGip->aCPUs)))
1637 {
1638 if (RT_LIKELY(iTimerCPU == iCPU))
1639 {
1640 unsigned long ulNow = jiffies;
1641 unsigned long ulDiff = ulNow - pDevExt->aCPUs[iCPU].ulLastJiffies;
1642 pDevExt->aCPUs[iCPU].ulLastJiffies = ulNow;
1643#ifdef TICK_NSEC
1644 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * TICK_NSEC;
1645#else
1646 u64Monotime = pDevExt->aCPUs[iCPU].u64LastMonotime + ulDiff * (1000000 / HZ);
1647#endif
1648 pDevExt->aCPUs[iCPU].u64LastMonotime = u64Monotime;
1649 if (RT_LIKELY(pGip))
1650 supdrvGipUpdatePerCpu(pGip, u64Monotime, iCPU);
1651 if (RT_LIKELY(!pDevExt->fGIPSuspended))
1652 mod_timer(&pDevExt->aCPUs[iCPU].Timer, ulNow + (HZ <= 1000 ? 0 : ONE_MSEC_IN_JIFFIES));
1653 }
1654 else
1655 printk("vboxdrv: error: GIP CPU update timer executing on the wrong CPU: apicid=%d != timer-apicid=%ld (cpuid=%d !=? timer-cpuid=%d)\n",
1656 iCPU, iTimerCPU, smp_processor_id(), pDevExt->aCPUs[iTimerCPU].iSmpProcessorId);
1657 }
1658 else
1659 printk("vboxdrv: error: APIC ID is bogus (GIP CPU update): apicid=%d max=%lu cpuid=%d\n",
1660 iCPU, (unsigned long)RT_ELEMENTS(pGip->aCPUs), smp_processor_id());
1661
1662 local_irq_restore(SavedFlags);
1663}
1664#endif /* CONFIG_SMP */
1665
1666
1667/**
1668 * Maps the GIP into user space.
1669 *
1670 * @returns negative errno.
1671 * @param pDevExt Instance data.
1672 */
1673int VBOXCALL supdrvOSGipMap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE *ppGip)
1674{
1675 int rc = 0;
1676 unsigned long ulAddr;
1677 unsigned long HCPhys = pDevExt->HCPhysGip;
1678 pgprot_t pgFlags;
1679 pgprot_val(pgFlags) = _PAGE_PRESENT | _PAGE_USER;
1680 dprintf2(("supdrvOSGipMap: ppGip=%p\n", ppGip));
1681
1682 /*
1683 * Allocate user space mapping and put the physical pages into it.
1684 */
1685 down_write(&current->mm->mmap_sem);
1686 ulAddr = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ, MAP_SHARED | MAP_ANONYMOUS, 0);
1687 if (!(ulAddr & ~PAGE_MASK))
1688 {
1689#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && !defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1690 int rc2 = remap_page_range(ulAddr, HCPhys, PAGE_SIZE, pgFlags);
1691#else
1692 int rc2 = 0;
1693 struct vm_area_struct *vma = find_vma(current->mm, ulAddr);
1694 if (vma)
1695#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
1696 rc2 = remap_page_range(vma, ulAddr, HCPhys, PAGE_SIZE, pgFlags);
1697#else
1698 rc2 = remap_pfn_range(vma, ulAddr, HCPhys >> PAGE_SHIFT, PAGE_SIZE, pgFlags);
1699#endif
1700 else
1701 {
1702 rc = SUPDRV_ERR_NO_MEMORY;
1703 dprintf(("supdrvOSGipMap: no vma found for ulAddr=%#lx!\n", ulAddr));
1704 }
1705#endif
1706 if (rc2)
1707 {
1708 rc = SUPDRV_ERR_NO_MEMORY;
1709 dprintf(("supdrvOSGipMap: remap_page_range failed rc2=%d\n", rc2));
1710 }
1711 }
1712 else
1713 {
1714 dprintf(("supdrvOSGipMap: do_mmap failed ulAddr=%#lx\n", ulAddr));
1715 rc = SUPDRV_ERR_NO_MEMORY;
1716 }
1717 up_write(&current->mm->mmap_sem); /* not quite sure when to give this up. */
1718
1719 /*
1720 * Success?
1721 */
1722 if (!rc)
1723 {
1724 *ppGip = (PSUPGLOBALINFOPAGE)ulAddr;
1725 dprintf2(("supdrvOSGipMap: ppGip=%p\n", *ppGip));
1726 return 0;
1727 }
1728
1729 /*
1730 * Failure, cleanup and be gone.
1731 */
1732 if (ulAddr & ~PAGE_MASK)
1733 {
1734 down_write(&current->mm->mmap_sem);
1735 MY_DO_MUNMAP(current->mm, ulAddr, PAGE_SIZE);
1736 up_write(&current->mm->mmap_sem);
1737 }
1738
1739 dprintf2(("supdrvOSGipMap: returns %d\n", rc));
1740 return rc;
1741}
1742
1743
1744/**
1745 * Maps the GIP into user space.
1746 *
1747 * @returns negative errno.
1748 * @param pDevExt Instance data.
1749 */
1750int VBOXCALL supdrvOSGipUnmap(PSUPDRVDEVEXT pDevExt, PSUPGLOBALINFOPAGE pGip)
1751{
1752 dprintf2(("supdrvOSGipUnmap: pGip=%p\n", pGip));
1753 if (current->mm)
1754 {
1755 down_write(&current->mm->mmap_sem);
1756 MY_DO_MUNMAP(current->mm, (unsigned long)pGip, PAGE_SIZE);
1757 up_write(&current->mm->mmap_sem);
1758 }
1759 dprintf2(("supdrvOSGipUnmap: returns 0\n"));
1760 return 0;
1761}
1762
1763
1764/**
1765 * Resumes the GIP updating.
1766 *
1767 * @param pDevExt Instance data.
1768 */
1769void VBOXCALL supdrvOSGipResume(PSUPDRVDEVEXT pDevExt)
1770{
1771 dprintf2(("supdrvOSGipResume:\n"));
1772 ASMAtomicXchgU8(&pDevExt->fGIPSuspended, false);
1773#ifdef CONFIG_SMP
1774 if (pDevExt->pGip->u32Mode != SUPGIPMODE_ASYNC_TSC)
1775#endif
1776 mod_timer(&g_GipTimer, jiffies);
1777#ifdef CONFIG_SMP
1778 else
1779 {
1780 mod_timer(&g_GipTimer, jiffies);
1781 smp_call_function(VBoxSupGipResumePerCpu, pDevExt, 0 /* retry */, 1 /* wait */);
1782 }
1783#endif
1784}
1785
1786
1787#ifdef CONFIG_SMP
1788/**
1789 * Callback for resuming GIP updating on the other CPUs.
1790 *
1791 * This is only used when the GIP is in async tsc mode.
1792 *
1793 * @param pvUser Pointer to the device instance.
1794 */
1795static void VBoxSupGipResumePerCpu(void *pvUser)
1796{
1797 PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pvUser;
1798 uint8_t iCPU = ASMGetApicId();
1799
1800 if (RT_UNLIKELY(iCPU >= RT_ELEMENTS(pDevExt->pGip->aCPUs)))
1801 {
1802 printk("vboxdrv: error: apicid=%d max=%lu cpuid=%d\n",
1803 iCPU, (unsigned long)RT_ELEMENTS(pDevExt->pGip->aCPUs), smp_processor_id());
1804 return;
1805 }
1806
1807 pDevExt->aCPUs[iCPU].iSmpProcessorId = smp_processor_id();
1808 mod_timer(&pDevExt->aCPUs[iCPU].Timer, jiffies);
1809}
1810#endif /* CONFIG_SMP */
1811
1812
1813/**
1814 * Suspends the GIP updating.
1815 *
1816 * @param pDevExt Instance data.
1817 */
1818void VBOXCALL supdrvOSGipSuspend(PSUPDRVDEVEXT pDevExt)
1819{
1820#ifdef CONFIG_SMP
1821 unsigned i;
1822#endif
1823 dprintf2(("supdrvOSGipSuspend:\n"));
1824 ASMAtomicXchgU8(&pDevExt->fGIPSuspended, true);
1825
1826 if (timer_pending(&g_GipTimer))
1827 del_timer_sync(&g_GipTimer);
1828#ifdef CONFIG_SMP
1829 for (i = 0; i < RT_ELEMENTS(pDevExt->aCPUs); i++)
1830 if (timer_pending(&pDevExt->aCPUs[i].Timer))
1831 del_timer_sync(&pDevExt->aCPUs[i].Timer);
1832#endif
1833}
1834
1835
1836/**
1837 * Get the current CPU count.
1838 * @returns Number of cpus.
1839 */
1840unsigned VBOXCALL supdrvOSGetCPUCount(void)
1841{
1842#ifdef CONFIG_SMP
1843# ifdef num_present_cpus
1844 return num_present_cpus();
1845# else
1846 return smp_num_cpus;
1847# endif
1848#else
1849 return 1;
1850#endif
1851}
1852
1853/**
1854 * Force async tsc mode.
1855 * @todo add a module argument for this.
1856 */
1857bool VBOXCALL supdrvOSGetForcedAsyncTscMode(void)
1858{
1859 return false;
1860}
1861
1862
1863/**
1864 * Converts a supdrv error code to an linux error code.
1865 *
1866 * @returns corresponding linux error code.
1867 * @param rc supdrv error code (SUPDRV_ERR_* defines).
1868 */
1869static int VBoxSupDrvErr2LinuxErr(int rc)
1870{
1871 switch (rc)
1872 {
1873 case 0: return 0;
1874 case SUPDRV_ERR_GENERAL_FAILURE: return -EACCES;
1875 case SUPDRV_ERR_INVALID_PARAM: return -EINVAL;
1876 case SUPDRV_ERR_INVALID_MAGIC: return -EILSEQ;
1877 case SUPDRV_ERR_INVALID_HANDLE: return -ENXIO;
1878 case SUPDRV_ERR_INVALID_POINTER: return -EFAULT;
1879 case SUPDRV_ERR_LOCK_FAILED: return -ENOLCK;
1880 case SUPDRV_ERR_ALREADY_LOADED: return -EEXIST;
1881 case SUPDRV_ERR_PERMISSION_DENIED: return -EPERM;
1882 case SUPDRV_ERR_VERSION_MISMATCH: return -ENOSYS;
1883 case SUPDRV_ERR_IDT_FAILED: return -1000;
1884 }
1885
1886 return -EPERM;
1887}
1888
1889
1890RTDECL(int) SUPR0Printf(const char *pszFormat, ...)
1891{
1892#if 1
1893 va_list args;
1894 char szMsg[512];
1895
1896 va_start(args, pszFormat);
1897 vsnprintf(szMsg, sizeof(szMsg) - 1, pszFormat, args);
1898 szMsg[sizeof(szMsg) - 1] = '\0';
1899 printk("%s", szMsg);
1900 va_end(args);
1901#else
1902 /* forward to printf - needs some more GCC hacking to fix ebp... */
1903 __asm__ __volatile__ ("mov %0, %esp\n\t"
1904 "jmp %1\n\t",
1905 :: "r" ((uintptr_t)&pszFormat - 4),
1906 "m" (printk));
1907#endif
1908 return 0;
1909}
1910
1911
1912/** Runtime assert implementation for Linux Ring-0. */
1913RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1914{
1915 printk("!!Assertion Failed!!\n"
1916 "Expression: %s\n"
1917 "Location : %s(%d) %s\n",
1918 pszExpr, pszFile, uLine, pszFunction);
1919}
1920
1921
1922/** Runtime assert implementation for Linux Ring-0. */
1923RTDECL(void) AssertMsg2(const char *pszFormat, ...)
1924{ /* forwarder. */
1925 va_list ap;
1926 char msg[256];
1927
1928 va_start(ap, pszFormat);
1929 vsnprintf(msg, sizeof(msg) - 1, pszFormat, ap);
1930 msg[sizeof(msg) - 1] = '\0';
1931 printk("%s", msg);
1932 va_end(ap);
1933}
1934
1935
1936/* GCC C++ hack. */
1937unsigned __gxx_personality_v0 = 0xcccccccc;
1938
1939
1940module_init(VBoxSupDrvInit);
1941module_exit(VBoxSupDrvUnload);
1942
1943MODULE_AUTHOR("innotek GmbH");
1944MODULE_DESCRIPTION("VirtualBox Support Driver");
1945MODULE_LICENSE("GPL");
1946#ifdef MODULE_VERSION
1947#define xstr(s) str(s)
1948#define str(s) #s
1949MODULE_VERSION(VBOX_VERSION_STRING " (" xstr(SUPDRVIOC_VERSION) ")");
1950#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette