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>
19 #include <linux/rbtree.h>
21 #include <palacios/vmm.h>
24 #include "palacios-vm.h"
25 #include "linux-exts.h"
31 int (*handler)(struct v3_guest * guest,
32 unsigned int cmd, unsigned long arg,
37 struct rb_node tree_node;
41 static inline struct vm_ctrl * __insert_ctrl(struct v3_guest * vm,
42 struct vm_ctrl * ctrl) {
43 struct rb_node ** p = &(vm->vm_ctrls.rb_node);
44 struct rb_node * parent = NULL;
45 struct vm_ctrl * tmp_ctrl = NULL;
49 tmp_ctrl = rb_entry(parent, struct vm_ctrl, tree_node);
51 if (ctrl->cmd < tmp_ctrl->cmd) {
53 } else if (ctrl->cmd > tmp_ctrl->cmd) {
60 rb_link_node(&(ctrl->tree_node), parent, p);
67 int add_guest_ctrl(struct v3_guest * guest, unsigned int cmd,
68 int (*handler)(struct v3_guest * guest,
69 unsigned int cmd, unsigned long arg,
72 struct vm_ctrl * ctrl = kmalloc(sizeof(struct vm_ctrl), GFP_KERNEL);
75 printk("Error: Could not allocate vm ctrl %d\n", cmd);
80 ctrl->handler = handler;
81 ctrl->priv_data = priv_data;
83 if (__insert_ctrl(guest, ctrl) != NULL) {
84 printk("Could not insert guest ctrl %d\n", cmd);
89 rb_insert_color(&(ctrl->tree_node), &(guest->vm_ctrls));
95 static struct vm_ctrl * get_ctrl(struct v3_guest * guest, unsigned int cmd) {
96 struct rb_node * n = guest->vm_ctrls.rb_node;
97 struct vm_ctrl * ctrl = NULL;
100 ctrl = rb_entry(n, struct vm_ctrl, tree_node);
102 if (cmd < ctrl->cmd) {
104 } else if (cmd > ctrl->cmd) {
116 #ifdef V3_CONFIG_EXT_INSPECTOR
117 #include "palacios-inspector.h"
120 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
121 #include "palacios-graphics-console.h"
124 #ifdef V3_CONFIG_HOST_DEVICE
125 #include "palacios-host-dev.h"
126 #define HOST_DEV_URL_LEN 256
129 extern struct class * v3_class;
132 static long v3_vm_ioctl(struct file * filp,
133 unsigned int ioctl, unsigned long arg) {
135 struct v3_guest * guest = filp->private_data;
137 printk("V3 IOCTL %d\n", ioctl);
142 printk("Stopping VM\n");
143 stop_palacios_vm(guest);
149 case V3_VM_HOST_DEV_CONNECT: {
150 #ifdef V3_CONFIG_HOST_DEVICE
151 void __user * argp = (void __user *)arg;
152 char host_dev_url[HOST_DEV_URL_LEN];
154 if (copy_from_user(host_dev_url, argp, HOST_DEV_URL_LEN)) {
155 printk("copy from user error getting url for host device connect...\n");
159 return connect_host_dev(guest,host_dev_url);
161 printk("palacios: Host device support not available\n");
168 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
169 return palacios_graphics_console_user_input(&(guest->graphics_console),
170 (struct v3_fb_input __user *) arg) ;
177 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
178 return palacios_graphics_console_user_query(&(guest->graphics_console),
179 (struct v3_fb_query_response __user *) arg);
187 struct vm_ctrl * ctrl = get_ctrl(guest, ioctl);
190 return ctrl->handler(guest, ioctl, arg, ctrl->priv_data);
194 printk("\tUnhandled ctrl cmd: %d\n", ioctl);
202 static int v3_vm_open(struct inode * inode, struct file * filp) {
203 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
204 filp->private_data = guest;
209 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
215 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
221 static struct file_operations v3_vm_fops = {
222 .owner = THIS_MODULE,
223 .unlocked_ioctl = v3_vm_ioctl,
224 .compat_ioctl = v3_vm_ioctl,
227 .write = v3_vm_write,
231 extern u32 pg_allocs;
236 int start_palacios_vm(void * arg) {
237 struct v3_guest * guest = (struct v3_guest *)arg;
241 daemonize(guest->name);
242 // allow_signal(SIGKILL);
245 init_vm_extensions(guest);
247 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
249 if (guest->v3_ctx == NULL) {
250 printk("palacios: failed to create vm\n");
251 complete(&(guest->start_done));
255 // init linux extensions
258 #ifdef V3_CONFIG_EXT_INSPECTOR
262 printk("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
264 cdev_init(&(guest->cdev), &v3_vm_fops);
266 guest->cdev.owner = THIS_MODULE;
267 guest->cdev.ops = &v3_vm_fops;
270 printk("Adding VM device\n");
271 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
274 printk("Fails to add cdev\n");
275 v3_free_vm(guest->v3_ctx);
276 complete(&(guest->start_done));
280 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
281 printk("Fails to create device\n");
282 cdev_del(&(guest->cdev));
283 v3_free_vm(guest->v3_ctx);
284 complete(&(guest->start_done));
288 complete(&(guest->start_done));
290 printk("palacios: launching vm\n");
292 if (v3_start_vm(guest->v3_ctx, 0xffffffff) < 0) {
293 printk("palacios: launch of vm failed\n");
294 device_destroy(v3_class, guest->vm_dev);
295 cdev_del(&(guest->cdev));
296 v3_free_vm(guest->v3_ctx);
300 complete(&(guest->thread_done));
302 printk("palacios: vm completed. returning.\n");
310 int stop_palacios_vm(struct v3_guest * guest) {
312 v3_stop_vm(guest->v3_ctx);
314 wait_for_completion(&(guest->thread_done));
316 v3_free_vm(guest->v3_ctx);
318 device_destroy(v3_class, guest->vm_dev);
320 cdev_del(&(guest->cdev));