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"
30 int (*handler)(struct v3_guest * guest,
31 unsigned int cmd, unsigned long arg,
36 struct rb_node tree_node;
40 static inline struct vm_ctrl * __insert_ctrl(struct v3_guest * vm,
41 struct vm_ctrl * ctrl) {
42 struct rb_node ** p = &(vm->vm_ctrls.rb_node);
43 struct rb_node * parent = NULL;
44 struct vm_ctrl * tmp_ctrl = NULL;
48 tmp_ctrl = rb_entry(parent, struct vm_ctrl, tree_node);
50 if (ctrl->cmd < tmp_ctrl->cmd) {
52 } else if (ctrl->cmd > tmp_ctrl->cmd) {
59 rb_link_node(&(ctrl->tree_node), parent, p);
66 int add_guest_ctrl(struct v3_guest * guest, unsigned int cmd,
67 int (*handler)(struct v3_guest * guest,
68 unsigned int cmd, unsigned long arg,
71 struct vm_ctrl * ctrl = kmalloc(sizeof(struct vm_ctrl), GFP_KERNEL);
74 printk("Error: Could not allocate vm ctrl %d\n", cmd);
79 ctrl->handler = handler;
80 ctrl->priv_data = priv_data;
82 if (__insert_ctrl(guest, ctrl) != NULL) {
83 printk("Could not insert guest ctrl %d\n", cmd);
88 rb_insert_color(&(ctrl->tree_node), &(guest->vm_ctrls));
94 static struct vm_ctrl * get_ctrl(struct v3_guest * guest, unsigned int cmd) {
95 struct rb_node * n = guest->vm_ctrls.rb_node;
96 struct vm_ctrl * ctrl = NULL;
99 ctrl = rb_entry(n, struct vm_ctrl, tree_node);
101 if (cmd < ctrl->cmd) {
103 } else if (cmd > ctrl->cmd) {
114 #ifdef V3_CONFIG_STREAM
115 #include "palacios-stream.h"
119 #ifdef V3_CONFIG_EXT_INSPECTOR
120 #include "palacios-inspector.h"
123 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
124 #include "palacios-graphics-console.h"
127 #ifdef V3_CONFIG_HOST_DEVICE
128 #include "palacios-host-dev.h"
129 #define HOST_DEV_URL_LEN 256
132 extern struct class * v3_class;
133 #define STREAM_NAME_LEN 128
135 static long v3_vm_ioctl(struct file * filp,
136 unsigned int ioctl, unsigned long arg) {
138 struct v3_guest * guest = filp->private_data;
140 printk("V3 IOCTL %d\n", ioctl);
145 printk("Stopping VM\n");
146 stop_palacios_vm(guest);
151 case V3_VM_STREAM_CONNECT: {
152 #ifdef V3_CONFIG_STREAM
153 void __user * argp = (void __user *)arg;
154 char path_name[STREAM_NAME_LEN];
156 if (copy_from_user(path_name, argp, STREAM_NAME_LEN)) {
157 printk("%s(%d): copy from user error...\n", __FILE__, __LINE__);
161 return open_stream(path_name);
163 printk("Stream support Not available\n");
169 case V3_VM_HOST_DEV_CONNECT: {
170 #ifdef V3_CONFIG_HOST_DEVICE
171 void __user * argp = (void __user *)arg;
172 char host_dev_url[HOST_DEV_URL_LEN];
174 if (copy_from_user(host_dev_url, argp, HOST_DEV_URL_LEN)) {
175 printk("copy from user error getting url for host device connect...\n");
179 return connect_host_dev(guest,host_dev_url);
181 printk("palacios: Host device support not available\n");
188 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
189 return palacios_graphics_console_user_input(&(guest->graphics_console),
190 (struct v3_fb_input __user *) arg) ;
197 #ifdef V3_CONFIG_GRAPHICS_CONSOLE
198 return palacios_graphics_console_user_query(&(guest->graphics_console),
199 (struct v3_fb_query_response __user *) arg);
207 struct vm_ctrl * ctrl = get_ctrl(guest, ioctl);
210 return ctrl->handler(guest, ioctl, arg, ctrl->priv_data);
214 printk("\tUnhandled ctrl cmd: %d\n", ioctl);
222 static int v3_vm_open(struct inode * inode, struct file * filp) {
223 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
224 filp->private_data = guest;
229 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
235 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
241 static struct file_operations v3_vm_fops = {
242 .owner = THIS_MODULE,
243 .unlocked_ioctl = v3_vm_ioctl,
244 .compat_ioctl = v3_vm_ioctl,
247 .write = v3_vm_write,
251 extern u32 pg_allocs;
256 int start_palacios_vm(void * arg) {
257 struct v3_guest * guest = (struct v3_guest *)arg;
261 daemonize(guest->name);
262 // allow_signal(SIGKILL);
266 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
268 if (guest->v3_ctx == NULL) {
269 printk("palacios: failed to create vm\n");
270 complete(&(guest->start_done));
274 // init linux extensions
277 #ifdef V3_CONFIG_EXT_INSPECTOR
281 printk("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
283 cdev_init(&(guest->cdev), &v3_vm_fops);
285 guest->cdev.owner = THIS_MODULE;
286 guest->cdev.ops = &v3_vm_fops;
289 printk("Adding VM device\n");
290 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
293 printk("Fails to add cdev\n");
294 v3_free_vm(guest->v3_ctx);
295 complete(&(guest->start_done));
299 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
300 printk("Fails to create device\n");
301 cdev_del(&(guest->cdev));
302 v3_free_vm(guest->v3_ctx);
303 complete(&(guest->start_done));
307 complete(&(guest->start_done));
309 printk("palacios: launching vm\n");
311 if (v3_start_vm(guest->v3_ctx, 0xffffffff) < 0) {
312 printk("palacios: launch of vm failed\n");
313 device_destroy(v3_class, guest->vm_dev);
314 cdev_del(&(guest->cdev));
315 v3_free_vm(guest->v3_ctx);
319 complete(&(guest->thread_done));
321 printk("palacios: vm completed. returning.\n");
329 int stop_palacios_vm(struct v3_guest * guest) {
331 v3_stop_vm(guest->v3_ctx);
333 wait_for_completion(&(guest->thread_done));
335 v3_free_vm(guest->v3_ctx);
337 device_destroy(v3_class, guest->vm_dev);
339 cdev_del(&(guest->cdev));