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-vm.h"
26 #ifdef V3_CONFIG_STREAM
27 #include "palacios-stream.h"
30 #ifdef V3_CONFIG_CONSOLE
31 #include "palacios-console.h"
34 #ifdef V3_CONFIG_EXT_INSPECTOR
35 #include "palacios-inspector.h"
39 extern struct class * v3_class;
40 #define STREAM_NAME_LEN 128
42 static long v3_vm_ioctl(struct file * filp,
43 unsigned int ioctl, unsigned long arg) {
45 struct v3_guest * guest = filp->private_data;
47 printk("V3 IOCTL %d\n", ioctl);
52 printk("Stopping VM\n");
53 stop_palacios_vm(guest);
57 case V3_VM_CONSOLE_CONNECT: {
58 #ifdef V3_CONFIG_CONSOLE
59 return connect_console(guest);
61 printk("Console support not available\n");
67 case V3_VM_STREAM_CONNECT: {
68 #ifdef V3_CONFIG_STREAM
69 void __user * argp = (void __user *)arg;
70 char path_name[STREAM_NAME_LEN];
72 if (copy_from_user(path_name, argp, STREAM_NAME_LEN)) {
73 printk("%s(%d): copy from user error...\n", __FILE__, __LINE__);
77 return open_stream(path_name);
79 printk("Stream support Not available\n");
86 printk("\tUnhandled\n");
93 static int v3_vm_open(struct inode * inode, struct file * filp) {
94 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
95 filp->private_data = guest;
100 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
106 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
112 static struct file_operations v3_vm_fops = {
113 .owner = THIS_MODULE,
114 .unlocked_ioctl = v3_vm_ioctl,
115 .compat_ioctl = v3_vm_ioctl,
118 .write = v3_vm_write,
122 extern int vm_running;
123 extern u32 pg_allocs;
128 int start_palacios_vm(void * arg) {
129 struct v3_guest * guest = (struct v3_guest *)arg;
133 daemonize(guest->name);
134 // allow_signal(SIGKILL);
138 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
140 if (guest->v3_ctx == NULL) {
141 printk("palacios: failed to create vm\n");
142 complete(&(guest->start_done));
146 printk("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
148 cdev_init(&(guest->cdev), &v3_vm_fops);
150 guest->cdev.owner = THIS_MODULE;
151 guest->cdev.ops = &v3_vm_fops;
154 printk("Adding VM device\n");
155 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
158 printk("Fails to add cdev\n");
159 v3_free_vm(guest->v3_ctx);
160 complete(&(guest->start_done));
164 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
165 printk("Fails to create device\n");
166 cdev_del(&(guest->cdev));
167 v3_free_vm(guest->v3_ctx);
168 complete(&(guest->start_done));
172 complete(&(guest->start_done));
174 printk("palacios: launching vm\n");
177 #if V3_CONFIG_EXT_INSPECTOR
182 if (v3_start_vm(guest->v3_ctx, 0xffffffff) < 0) {
183 printk("palacios: launch of vm failed\n");
184 device_destroy(v3_class, guest->vm_dev);
185 cdev_del(&(guest->cdev));
186 v3_free_vm(guest->v3_ctx);
190 complete(&(guest->thread_done));
192 printk("palacios: vm completed. returning.\n");
200 int stop_palacios_vm(struct v3_guest * guest) {
202 v3_stop_vm(guest->v3_ctx);
204 wait_for_completion(&(guest->thread_done));
206 v3_free_vm(guest->v3_ctx);
208 device_destroy(v3_class, guest->vm_dev);
210 cdev_del(&(guest->cdev));