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"
38 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
39 #include "palacios-graphics-console.h"
42 #ifdef V3_CONFIG_HOST_DEVICE
43 #include "palacios-host-dev.h"
44 #define HOST_DEV_URL_LEN 256
47 extern struct class * v3_class;
48 #define STREAM_NAME_LEN 128
50 static long v3_vm_ioctl(struct file * filp,
51 unsigned int ioctl, unsigned long arg) {
53 struct v3_guest * guest = filp->private_data;
55 printk("V3 IOCTL %d\n", ioctl);
60 printk("Stopping VM\n");
61 stop_palacios_vm(guest);
65 case V3_VM_CONSOLE_CONNECT: {
66 #ifdef V3_CONFIG_CONSOLE
67 return connect_console(guest);
69 printk("Console support not available\n");
75 case V3_VM_STREAM_CONNECT: {
76 #ifdef V3_CONFIG_STREAM
77 void __user * argp = (void __user *)arg;
78 char path_name[STREAM_NAME_LEN];
80 if (copy_from_user(path_name, argp, STREAM_NAME_LEN)) {
81 printk("%s(%d): copy from user error...\n", __FILE__, __LINE__);
85 return open_stream(path_name);
87 printk("Stream support Not available\n");
93 case V3_VM_HOST_DEV_CONNECT: {
94 #ifdef V3_CONFIG_HOST_DEVICE
95 void __user * argp = (void __user *)arg;
96 char host_dev_url[HOST_DEV_URL_LEN];
98 if (copy_from_user(host_dev_url, argp, HOST_DEV_URL_LEN)) {
99 printk("copy from user error getting url for host device connect...\n");
103 return connect_host_dev(guest,host_dev_url);
105 printk("palacios: Host device support not available\n");
112 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
113 return palacios_graphics_console_user_input(&(guest->graphics_console),
114 (struct v3_fb_input __user *) arg) ;
121 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
122 return palacios_graphics_console_user_query(&(guest->graphics_console),
123 (struct v3_fb_query_response __user *) arg);
131 printk("\tUnhandled\n");
138 static int v3_vm_open(struct inode * inode, struct file * filp) {
139 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
140 filp->private_data = guest;
145 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
151 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
157 static struct file_operations v3_vm_fops = {
158 .owner = THIS_MODULE,
159 .unlocked_ioctl = v3_vm_ioctl,
160 .compat_ioctl = v3_vm_ioctl,
163 .write = v3_vm_write,
167 extern u32 pg_allocs;
172 int start_palacios_vm(void * arg) {
173 struct v3_guest * guest = (struct v3_guest *)arg;
177 daemonize(guest->name);
178 // allow_signal(SIGKILL);
182 printk("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
184 cdev_init(&(guest->cdev), &v3_vm_fops);
186 guest->cdev.owner = THIS_MODULE;
187 guest->cdev.ops = &v3_vm_fops;
190 printk("Adding VM device\n");
191 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
194 printk("Fails to add cdev\n");
195 complete(&(guest->start_done));
199 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
200 printk("Fails to create device\n");
201 cdev_del(&(guest->cdev));
202 complete(&(guest->start_done));
206 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
208 if (guest->v3_ctx == NULL) {
209 printk("palacios: failed to create vm\n");
210 cdev_del(&(guest->cdev));
211 complete(&(guest->start_done));
215 complete(&(guest->start_done));
217 printk("palacios: launching vm\n");
220 #ifdef V3_CONFIG_EXT_INSPECTOR
225 if (v3_start_vm(guest->v3_ctx, 0xffffffff) < 0) {
226 printk("palacios: launch of vm failed\n");
227 device_destroy(v3_class, guest->vm_dev);
228 cdev_del(&(guest->cdev));
229 v3_free_vm(guest->v3_ctx);
233 complete(&(guest->thread_done));
235 printk("palacios: vm completed. returning.\n");
243 int stop_palacios_vm(struct v3_guest * guest) {
245 v3_stop_vm(guest->v3_ctx);
247 wait_for_completion(&(guest->thread_done));
249 v3_free_vm(guest->v3_ctx);
251 device_destroy(v3_class, guest->vm_dev);
253 cdev_del(&(guest->cdev));