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"
46 extern struct class * v3_class;
47 #define STREAM_NAME_LEN 128
49 static long v3_vm_ioctl(struct file * filp,
50 unsigned int ioctl, unsigned long arg) {
52 struct v3_guest * guest = filp->private_data;
54 printk("V3 IOCTL %d\n", ioctl);
59 printk("Stopping VM\n");
60 stop_palacios_vm(guest);
64 case V3_VM_CONSOLE_CONNECT: {
65 #ifdef V3_CONFIG_CONSOLE
66 return connect_console(guest);
68 printk("Console support not available\n");
74 case V3_VM_STREAM_CONNECT: {
75 #ifdef V3_CONFIG_STREAM
76 void __user * argp = (void __user *)arg;
77 char path_name[STREAM_NAME_LEN];
79 if (copy_from_user(path_name, argp, STREAM_NAME_LEN)) {
80 printk("%s(%d): copy from user error...\n", __FILE__, __LINE__);
84 return open_stream(path_name);
86 printk("Stream support Not available\n");
92 case V3_VM_HOST_DEV_CONNECT: {
93 #ifdef V3_CONFIG_HOST_DEV
94 if (copy_from_user(host_dev_url, argp, HOST_DEV_URL_LEN)) {
95 printk("copy from user error getting url for host device connect...\n");
99 return connect_host_dev(guest,host_dev_url);
101 printk("palacios: Host device support not available\n");
108 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
109 return palacios_graphics_console_user_input(&(guest->graphics_console),
110 (struct v3_fb_input __user *) arg) ;
117 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
118 return palacios_graphics_console_user_query(&(guest->graphics_console),
119 (struct v3_fb_query_response __user *) arg);
127 printk("\tUnhandled\n");
134 static int v3_vm_open(struct inode * inode, struct file * filp) {
135 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
136 filp->private_data = guest;
141 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
147 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
153 static struct file_operations v3_vm_fops = {
154 .owner = THIS_MODULE,
155 .unlocked_ioctl = v3_vm_ioctl,
156 .compat_ioctl = v3_vm_ioctl,
159 .write = v3_vm_write,
163 extern int vm_running;
164 extern u32 pg_allocs;
169 int start_palacios_vm(void * arg) {
170 struct v3_guest * guest = (struct v3_guest *)arg;
174 daemonize(guest->name);
175 // allow_signal(SIGKILL);
179 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
181 if (guest->v3_ctx == NULL) {
182 printk("palacios: failed to create vm\n");
183 complete(&(guest->start_done));
187 printk("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
189 cdev_init(&(guest->cdev), &v3_vm_fops);
191 guest->cdev.owner = THIS_MODULE;
192 guest->cdev.ops = &v3_vm_fops;
195 printk("Adding VM device\n");
196 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
199 printk("Fails to add cdev\n");
200 v3_free_vm(guest->v3_ctx);
201 complete(&(guest->start_done));
205 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
206 printk("Fails to create device\n");
207 cdev_del(&(guest->cdev));
208 v3_free_vm(guest->v3_ctx);
209 complete(&(guest->start_done));
213 complete(&(guest->start_done));
215 printk("palacios: launching vm\n");
218 #ifdef V3_CONFIG_EXT_INSPECTOR
223 if (v3_start_vm(guest->v3_ctx, 0xffffffff) < 0) {
224 printk("palacios: launch of vm failed\n");
225 device_destroy(v3_class, guest->vm_dev);
226 cdev_del(&(guest->cdev));
227 v3_free_vm(guest->v3_ctx);
231 complete(&(guest->thread_done));
233 printk("palacios: vm completed. returning.\n");
241 int stop_palacios_vm(struct v3_guest * guest) {
243 v3_stop_vm(guest->v3_ctx);
245 wait_for_completion(&(guest->thread_done));
247 v3_free_vm(guest->v3_ctx);
249 device_destroy(v3_class, guest->vm_dev);
251 cdev_del(&(guest->cdev));