1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/*
* Linux kernel wrapper for KQEMU
* Copyright (c) 2004-2005 Fabrice Bellard
*/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
#include <linux/version.h>
#include <linux/ioctl.h>
#include <linux/smp_lock.h>
#include <linux/miscdevice.h>
#include <asm/atomic.h>
#include <asm/processor.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include "kqemu-kernel.h"
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,19)
#error "Linux 2.4.19 or above needed"
#endif
/* The pfn_to_page() API appeared in 2.5.14 and changed to function during 2.6.x */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && !defined(pfn_to_page)
#define page_to_pfn(page) ((page) - mem_map)
#define pfn_to_page(pfn) (mem_map + (pfn))
#endif
#ifdef PAGE_KERNEL_EXEC
#if defined(__i386__)
/* problem : i386 kernels usually don't export __PAGE_KERNEL_EXEC */
#undef PAGE_KERNEL_EXEC
#define PAGE_KERNEL_EXEC __pgprot(__PAGE_KERNEL & ~_PAGE_NX)
#endif
#else
#define PAGE_KERNEL_EXEC PAGE_KERNEL
#endif
//#define DEBUG
#ifdef DEBUG
int lock_count;
int page_alloc_count;
#endif
/* if 0 is used, then devfs/udev is used to automatically create the
device */
int major = 250;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
module_param(major, int, 0);
#else
MODULE_PARM(major,"i");
#endif
/* Lock the page at virtual address 'user_addr' and return its
physical address (page index). Return a host OS private user page
identifier or NULL if error */
struct kqemu_user_page *CDECL kqemu_lock_user_page(unsigned long *ppage_index,
unsigned long user_addr)
{
int ret;
struct page *page;
ret = get_user_pages(current, current->mm,
user_addr,
1, /* 1 page. */
1, /* 'write': intent to write. */
0, /* 'force': ? */
&page,
NULL);
if (ret != 1)
return NULL;
/* we ensure here that the page cannot be swapped out by the
kernel. */
/* XXX: This test may be incorrect for 2.6 kernels */
if (!page->mapping) {
put_page(page);
return NULL;
}
#ifdef DEBUG
lock_count++;
#endif
*ppage_index = page_to_pfn(page);
return (struct kqemu_user_page *)page;
}
void CDECL kqemu_unlock_user_page(struct kqemu_user_page *page1)
{
struct page *page = (struct page *)page1;
set_page_dirty(page);
put_page(page);
#ifdef DEBUG
lock_count--;
#endif
}
/* Allocate a new page and return its physical address (page
index). Return a host OS private page identifier or NULL if
error */
struct kqemu_page *CDECL kqemu_alloc_zeroed_page(unsigned long *ppage_index)
{
unsigned long vaddr;
struct page *page;
vaddr = get_zeroed_page(GFP_KERNEL);
if (!vaddr)
return NULL;
#ifdef DEBUG
page_alloc_count++;
#endif
page = virt_to_page(vaddr);
*ppage_index = page_to_pfn(page);
return (struct kqemu_page *)page;
}
void CDECL kqemu_free_page(struct kqemu_page *page1)
{
struct page *page = (struct page *)page1;
__free_page(page);
#ifdef DEBUG
page_alloc_count--;
#endif
}
/* Return a host kernel address of the physical page whose private
identifier is 'page1' */
void * CDECL kqemu_page_kaddr(struct kqemu_page *page1)
{
struct page *page = (struct page *)page1;
return page_address(page);
}
/* Allocate 'size' bytes of memory in host kernel address space (size
is a multiple of 4 KB) and return the address or NULL if error. The
allocated memory must be marked as executable by the host kernel
and must be page aligned. On i386 with PAE (but not on x86_64), it
must be allocated in the first 4 GB of physical memory. */
void * CDECL kqemu_vmalloc(unsigned int size)
{
return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
}
void CDECL kqemu_vfree(void *ptr)
{
return vfree(ptr);
}
/* Convert a page aligned address inside a memory area allocated by
kqemu_vmalloc() to a physical address (page index) */
unsigned long CDECL kqemu_vmalloc_to_phys(const void *vaddr)
{
struct page *page;
page = vmalloc_to_page((void *)vaddr);
if (!page)
return -1;
return page_to_pfn(page);
}
/* Map a IO area in the kernel address space and return its
address. Return NULL if error or not implemented. This function is
only used if an APIC is detected on the host CPU. */
void * CDECL kqemu_io_map(unsigned long page_index, unsigned int size)
{
return ioremap(page_index << PAGE_SHIFT, size);
}
/* Unmap the IO area */
void CDECL kqemu_io_unmap(void *ptr, unsigned int size)
{
return iounmap(ptr);
}
/* return TRUE if a signal is pending (i.e. the guest must stop
execution) */
int CDECL kqemu_schedule(void)
{
if (need_resched()) {
schedule();
}
return signal_pending(current);
}
char log_buf[4096];
void CDECL kqemu_log(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(log_buf, sizeof(log_buf), fmt, ap);
printk("kqemu: %s", log_buf);
va_end(ap);
}
/*********************************************************/
static struct kqemu_global_state *kqemu_gs;
struct kqemu_instance {
struct semaphore sem;
struct kqemu_state *state;
};
static int kqemu_open(struct inode *inode, struct file *filp)
{
struct kqemu_instance *ks;
ks = kmalloc(sizeof(struct kqemu_instance), GFP_KERNEL);
if (!ks)
return -ENOMEM;
init_MUTEX(&ks->sem);
ks->state = NULL;
filp->private_data = ks;
return 0;
}
static int kqemu_release(struct inode *inode, struct file *filp)
{
struct kqemu_instance *ks = filp->private_data;
down(&ks->sem);
if (ks->state) {
kqemu_delete(ks->state);
ks->state = NULL;
}
up(&ks->sem);
kfree(ks);
#ifdef DEBUG
printk("lock_count=%d page_alloc_count=%d\n",
lock_count, page_alloc_count);
#endif
return 0;
}
static int kqemu_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
struct kqemu_instance *ks = filp->private_data;
struct kqemu_state *s = ks->state;
long ret;
down(&ks->sem);
switch(cmd) {
case KQEMU_INIT:
{
struct kqemu_init d1, *d = &d1;
if (s) {
ret = -EIO;
break;
}
if (copy_from_user(d, (void *)arg, sizeof(*d))) {
ret = -EFAULT;
break;
}
s = kqemu_init(d, kqemu_gs);
if (!s) {
ret = -ENOMEM;
break;
}
ks->state = s;
ret = 0;
}
break;
case KQEMU_EXEC:
{
struct kqemu_cpu_state *ctx;
if (!s) {
ret = -EIO;
break;
}
ctx = kqemu_get_cpu_state(s);
if (copy_from_user(ctx, (void *)arg, sizeof(*ctx))) {
ret = -EFAULT;
break;
}
unlock_kernel();
ret = kqemu_exec(s);
lock_kernel();
if (copy_to_user((void *)arg, ctx, sizeof(*ctx))) {
ret = -EFAULT;
break;
}
}
break;
case KQEMU_GET_VERSION:
{
if (put_user(KQEMU_VERSION, (int *)arg) < 0) {
ret = -EFAULT;
} else {
ret = 0;
}
}
break;
default:
ret = -ENOIOCTLCMD;
break;
}
up(&ks->sem);
return ret;
}
static struct file_operations kqemu_fops = {
owner: THIS_MODULE,
ioctl: kqemu_ioctl,
open: kqemu_open,
release: kqemu_release,
};
static struct miscdevice kqemu_dev =
{
.minor = MISC_DYNAMIC_MINOR,
.name = "kqemu",
.fops = &kqemu_fops,
};
int init_module(void)
{
int ret, max_locked_pages;
struct sysinfo si;
printk("QEMU Accelerator Module version %d.%d.%d, Copyright (c) 2005-2006 Fabrice Bellard\n"
"This is a proprietary product. Read the LICENSE file for more information\n"
"Redistribution of this module is prohibited without authorization\n",
(KQEMU_VERSION >> 16),
(KQEMU_VERSION >> 8) & 0xff,
(KQEMU_VERSION) & 0xff);
si_meminfo(&si);
max_locked_pages = si.totalram / 2;
kqemu_gs = kqemu_global_init(max_locked_pages);
if (!kqemu_gs)
return -ENOMEM;
if (major > 0) {
ret = register_chrdev(major, "kqemu", &kqemu_fops);
if (ret < 0) {
kqemu_global_delete(kqemu_gs);
printk("kqemu: could not get major %d\n", major);
return ret;
}
} else {
ret = misc_register (&kqemu_dev);
if (ret < 0) {
kqemu_global_delete(kqemu_gs);
printk("kqemu: could not create device\n");
return ret;
}
}
printk("KQEMU installed, max_locked_mem=%dkB.\n",
max_locked_pages * 4);
return 0;
}
void cleanup_module(void)
{
if (major > 0)
unregister_chrdev(major, "kqemu");
else
misc_deregister (&kqemu_dev);
if (kqemu_gs) {
kqemu_global_delete(kqemu_gs);
kqemu_gs = NULL;
}
}
MODULE_LICENSE("Proprietary");
|