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>
19 #include <linux/module.h>
21 #include <palacios/vmm.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) {
120 extern struct class * v3_class;
123 static long v3_vm_ioctl(struct file * filp,
124 unsigned int ioctl, unsigned long arg) {
126 struct v3_guest * guest = filp->private_data;
128 printk("V3 IOCTL %d\n", ioctl);
132 printk("palacios: launching vm\n");
134 if (v3_start_vm(guest->v3_ctx, (0x1 << num_online_cpus()) - 1) < 0) {
135 printk("palacios: launch of vm failed\n");
142 printk("Stopping VM (%s) (%p)\n", guest->name, guest);
144 if (irqs_disabled()) {
145 printk("WHAT!!?? IRQs are disabled??\n");
149 v3_stop_vm(guest->v3_ctx);
153 printk("Pausing VM (%s)\n", guest->name);
154 v3_pause_vm(guest->v3_ctx);
157 case V3_VM_CONTINUE: {
158 printk("Continuing VM (%s)\n", guest->name);
159 v3_continue_vm(guest->v3_ctx);
162 case V3_VM_SIMULATE: {
163 printk("Simulating VM (%s) for %lu msecs\n", guest->name, arg);
164 v3_simulate_vm(guest->v3_ctx, arg);
169 #ifdef V3_CONFIG_CHECKPOINT
171 struct v3_chkpt_info chkpt;
172 void __user * argp = (void __user *)arg;
174 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
176 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
177 printk("Copy from user error getting checkpoint info\n");
181 printk("Saving Guest to %s:%s\n", chkpt.store, chkpt.url);
183 if (v3_save_vm(guest->v3_ctx, chkpt.store, chkpt.url) == -1) {
184 printk("Error checkpointing VM state\n");
191 struct v3_chkpt_info chkpt;
192 void __user * argp = (void __user *)arg;
194 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
196 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
197 printk("Copy from user error getting checkpoint info\n");
201 printk("Loading Guest to %s:%s\n", chkpt.store, chkpt.url);
203 if (v3_load_vm(guest->v3_ctx, chkpt.store, chkpt.url) == -1) {
204 printk("Error Loading VM state\n");
211 case V3_VM_MOVE_CORE: {
212 struct v3_core_move_cmd cmd;
213 void __user * argp = (void __user *)arg;
215 memset(&cmd, 0, sizeof(struct v3_core_move_cmd));
217 if (copy_from_user(&cmd, argp, sizeof(struct v3_core_move_cmd))) {
218 printk("copy from user error getting migrate command...\n");
222 printk("moving guest %s vcore %d to CPU %d\n", guest->name, cmd.vcore_id, cmd.pcore_id);
224 v3_move_vm_core(guest->v3_ctx, cmd.vcore_id, cmd.pcore_id);
229 struct vm_ctrl * ctrl = get_ctrl(guest, ioctl);
232 return ctrl->handler(guest, ioctl, arg, ctrl->priv_data);
236 printk("\tUnhandled ctrl cmd: %d\n", ioctl);
244 static int v3_vm_open(struct inode * inode, struct file * filp) {
245 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
246 filp->private_data = guest;
251 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
257 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
263 static struct file_operations v3_vm_fops = {
264 .owner = THIS_MODULE,
265 .unlocked_ioctl = v3_vm_ioctl,
266 .compat_ioctl = v3_vm_ioctl,
269 .write = v3_vm_write,
273 extern u32 pg_allocs;
278 int create_palacios_vm(struct v3_guest * guest) {
281 init_vm_extensions(guest);
283 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
285 if (guest->v3_ctx == NULL) {
286 printk("palacios: failed to create vm\n");
291 printk("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
293 cdev_init(&(guest->cdev), &v3_vm_fops);
295 guest->cdev.owner = THIS_MODULE;
296 guest->cdev.ops = &v3_vm_fops;
299 printk("Adding VM device\n");
300 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
303 printk("Fails to add cdev\n");
304 v3_free_vm(guest->v3_ctx);
308 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
309 printk("Fails to create device\n");
310 cdev_del(&(guest->cdev));
311 v3_free_vm(guest->v3_ctx);
315 printk("palacios: vm created at /dev/v3-vm%d\n", MINOR(guest->vm_dev));
324 int free_palacios_vm(struct v3_guest * guest) {
326 v3_free_vm(guest->v3_ctx);
328 device_destroy(v3_class, guest->vm_dev);
330 cdev_del(&(guest->cdev));