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>
15 #include <linux/vmalloc.h>
16 #include <linux/file.h>
17 #include <linux/spinlock.h>
18 #include <linux/rbtree.h>
20 #include <palacios/vmm.h>
24 #include "linux-exts.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) {
119 extern struct class * v3_class;
122 static long v3_vm_ioctl(struct file * filp,
123 unsigned int ioctl, unsigned long arg) {
125 struct v3_guest * guest = filp->private_data;
127 printk("V3 IOCTL %d\n", ioctl);
132 printk("Stopping VM (%s)\n", guest->name);
133 stop_palacios_vm(guest);
137 printk("Pausing VM (%s)\n", guest->name);
138 v3_pause_vm(guest->v3_ctx);
141 case V3_VM_CONTINUE: {
142 printk("Continuing VM (%s)\n", guest->name);
143 v3_continue_vm(guest->v3_ctx);
148 struct vm_ctrl * ctrl = get_ctrl(guest, ioctl);
151 return ctrl->handler(guest, ioctl, arg, ctrl->priv_data);
155 printk("\tUnhandled ctrl cmd: %d\n", ioctl);
163 static int v3_vm_open(struct inode * inode, struct file * filp) {
164 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
165 filp->private_data = guest;
170 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
176 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
182 static struct file_operations v3_vm_fops = {
183 .owner = THIS_MODULE,
184 .unlocked_ioctl = v3_vm_ioctl,
185 .compat_ioctl = v3_vm_ioctl,
188 .write = v3_vm_write,
192 extern u32 pg_allocs;
197 int start_palacios_vm(void * arg) {
198 struct v3_guest * guest = (struct v3_guest *)arg;
202 daemonize(guest->name);
203 // allow_signal(SIGKILL);
206 init_vm_extensions(guest);
208 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
210 if (guest->v3_ctx == NULL) {
211 printk("palacios: failed to create vm\n");
212 complete(&(guest->start_done));
217 printk("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
219 cdev_init(&(guest->cdev), &v3_vm_fops);
221 guest->cdev.owner = THIS_MODULE;
222 guest->cdev.ops = &v3_vm_fops;
225 printk("Adding VM device\n");
226 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
229 printk("Fails to add cdev\n");
230 v3_free_vm(guest->v3_ctx);
231 complete(&(guest->start_done));
235 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
236 printk("Fails to create device\n");
237 cdev_del(&(guest->cdev));
238 v3_free_vm(guest->v3_ctx);
239 complete(&(guest->start_done));
243 complete(&(guest->start_done));
245 printk("palacios: launching vm\n");
247 if (v3_start_vm(guest->v3_ctx, 0xffffffff) < 0) {
248 printk("palacios: launch of vm failed\n");
249 device_destroy(v3_class, guest->vm_dev);
250 cdev_del(&(guest->cdev));
251 v3_free_vm(guest->v3_ctx);
255 complete(&(guest->thread_done));
257 printk("palacios: vm completed. returning.\n");
265 int stop_palacios_vm(struct v3_guest * guest) {
267 v3_stop_vm(guest->v3_ctx);
269 wait_for_completion(&(guest->thread_done));
271 v3_free_vm(guest->v3_ctx);
273 device_destroy(v3_class, guest->vm_dev);
275 cdev_del(&(guest->cdev));