6 #include <linux/device.h>
7 #include <linux/cdev.h>
8 #include <linux/errno.h>
9 #include <linux/percpu.h>
11 #include <linux/uaccess.h>
12 #include <linux/poll.h>
13 #include <linux/anon_inodes.h>
14 #include <linux/sched.h>
16 #include <linux/smp_lock.h>
17 #include <linux/file.h>
18 #include <linux/spinlock.h>
21 #include <palacios/vmm.h>
24 #include "palacios-console.h"
25 #include "palacios-serial.h"
26 #include "palacios-vm.h"
28 extern struct class * v3_class;
29 #define STREAM_NAME_LEN 128
31 static long v3_vm_ioctl(struct file * filp,
32 unsigned int ioctl, unsigned long arg) {
33 void __user * argp = (void __user *)arg;
34 char path_name[STREAM_NAME_LEN];
36 struct v3_guest * guest = filp->private_data;
38 printk("V3 IOCTL %d\n", ioctl);
42 case V3_VM_CONSOLE_CONNECT: {
43 return connect_console(guest);
46 case V3_VM_SERIAL_CONNECT: {
47 if (copy_from_user(path_name, argp, STREAM_NAME_LEN)) {
48 printk("copy from user error getting guest image...\n");
52 return open_serial(path_name);
56 printk("Stopping VM\n");
57 stop_palacios_vm(guest);
61 printk("\tUnhandled\n");
68 static int v3_vm_open(struct inode * inode, struct file * filp) {
69 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
70 filp->private_data = guest;
75 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
81 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
88 static struct file_operations v3_vm_fops = {
90 .unlocked_ioctl = v3_vm_ioctl,
91 .compat_ioctl = v3_vm_ioctl,
99 extern int vm_running;
100 extern u32 pg_allocs;
106 int start_palacios_vm(void * arg) {
107 struct v3_guest * guest = (struct v3_guest *)arg;
111 daemonize(guest->name);
112 // allow_signal(SIGKILL);
118 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
120 if (guest->v3_ctx == NULL) {
121 printk("palacios: failed to create vm\n");
125 printk("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
127 cdev_init(&(guest->cdev), &v3_vm_fops);
129 guest->cdev.owner = THIS_MODULE;
130 guest->cdev.ops = &v3_vm_fops;
133 printk("Adding VM device\n");
134 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
137 printk("Fails to add cdev\n");
141 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
142 printk("Fails to create device\n");
146 complete(&(guest->thread_done));
149 printk("palacios: launching vm\n");
151 if (v3_start_vm(guest->v3_ctx, 0xffffffff) < 0) {
152 printk("palacios: launch of vm failed\n");
156 complete(&(guest->thread_done));
159 printk("palacios: vm completed. returning.\n");
167 int stop_palacios_vm(struct v3_guest * guest) {
169 v3_stop_vm(guest->v3_ctx);
171 wait_for_completion(&(guest->thread_done));
173 v3_free_vm(guest->v3_ctx);