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/file.h>
16 #include <linux/spinlock.h>
17 #include <linux/rbtree.h>
18 #include <linux/module.h>
20 #include <palacios/vmm.h>
21 #include <palacios/vmm_host_events.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 = palacios_alloc(sizeof(struct vm_ctrl));
75 WARNING("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 WARNING("Could not insert guest ctrl %d\n", cmd);
89 rb_insert_color(&(ctrl->tree_node), &(guest->vm_ctrls));
97 static struct vm_ctrl * get_ctrl(struct v3_guest * guest, unsigned int cmd) {
98 struct rb_node * n = guest->vm_ctrls.rb_node;
99 struct vm_ctrl * ctrl = NULL;
102 ctrl = rb_entry(n, struct vm_ctrl, tree_node);
104 if (cmd < ctrl->cmd) {
106 } else if (cmd > ctrl->cmd) {
116 int remove_guest_ctrl(struct v3_guest * guest, unsigned int cmd) {
117 struct vm_ctrl * ctrl = get_ctrl(guest, cmd);
120 INFO("Could not find control (%d) to remove\n", cmd);
124 rb_erase(&(ctrl->tree_node), &(guest->vm_ctrls));
131 static void free_guest_ctrls(struct v3_guest * guest) {
132 struct rb_node * node = rb_first(&(guest->vm_ctrls));
133 struct vm_ctrl * ctrl = NULL;
136 ctrl = rb_entry(node, struct vm_ctrl, tree_node);
138 node = rb_next(node);
140 WARNING("Cleaning up guest ctrl that was not removed explicitly (%d)\n", ctrl->cmd);
151 extern struct class * v3_class;
154 static long v3_vm_ioctl(struct file * filp,
155 unsigned int ioctl, unsigned long arg) {
157 struct v3_guest * guest = filp->private_data;
159 INFO("V3 IOCTL %d\n", ioctl);
163 NOTICE("palacios: launching vm\n");
165 if (v3_start_vm(guest->v3_ctx, (0x1 << num_online_cpus()) - 1) < 0) {
166 WARNING("palacios: launch of vm failed\n");
173 NOTICE("Stopping VM (%s) (%p)\n", guest->name, guest);
175 if (irqs_disabled()) {
176 ERROR("WHAT!!?? IRQs are disabled??\n");
180 v3_stop_vm(guest->v3_ctx);
184 NOTICE("Pausing VM (%s)\n", guest->name);
185 v3_pause_vm(guest->v3_ctx);
188 case V3_VM_CONTINUE: {
189 NOTICE("Continuing VM (%s)\n", guest->name);
190 v3_continue_vm(guest->v3_ctx);
193 case V3_VM_SIMULATE: {
194 NOTICE("Simulating VM (%s) for %lu msecs\n", guest->name, arg);
195 v3_simulate_vm(guest->v3_ctx, arg);
200 #ifdef V3_CONFIG_CHECKPOINT
202 struct v3_chkpt_info chkpt;
203 void __user * argp = (void __user *)arg;
205 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
207 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
208 WARNING("Copy from user error getting checkpoint info\n");
212 NOTICE("Saving Guest to %s:%s\n", chkpt.store, chkpt.url);
214 if (v3_save_vm(guest->v3_ctx, chkpt.store, chkpt.url, chkpt.opts) == -1) {
215 WARNING("Error checkpointing VM state\n");
222 struct v3_chkpt_info chkpt;
223 void __user * argp = (void __user *)arg;
225 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
227 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
228 WARNING("Copy from user error getting checkpoint info\n");
232 NOTICE("Loading Guest from %s:%s\n", chkpt.store, chkpt.url);
234 if (v3_load_vm(guest->v3_ctx, chkpt.store, chkpt.url, chkpt.opts) == -1) {
235 WARNING("Error Loading VM state\n");
243 #ifdef V3_CONFIG_LIVE_MIGRATION
245 struct v3_chkpt_info chkpt_info;
246 void __user * argp = (void __user *)arg;
248 memset(&chkpt_info,0, sizeof(struct v3_chkpt_info));
250 if(copy_from_user(&chkpt_info, argp, sizeof(struct v3_chkpt_info))){
251 WARNING("Copy from user error getting checkpoint info\n");
256 NOTICE("Sending (live-migrating) Guest to %s:%s\n",chkpt_info.store, chkpt_info.url);
258 if (v3_send_vm(guest->v3_ctx, chkpt_info.store, chkpt_info.url, chkpt_info.opts) == -1) {
259 WARNING("Error sending VM\n");
266 case V3_VM_RECEIVE: {
267 struct v3_chkpt_info chkpt_info;
268 void __user * argp = (void __user *)arg;
270 memset(&chkpt_info,0, sizeof(struct v3_chkpt_info));
272 if(copy_from_user(&chkpt_info, argp, sizeof(struct v3_chkpt_info))){
273 WARNING("Copy from user error getting checkpoint info\n");
278 NOTICE("Receiving (live-migrating) Guest to %s:%s\n",chkpt_info.store, chkpt_info.url);
280 if (v3_receive_vm(guest->v3_ctx, chkpt_info.store, chkpt_info.url, chkpt_info.opts) == -1) {
281 WARNING("Error receiving VM\n");
290 struct v3_debug_cmd cmd;
291 struct v3_debug_event evt;
292 void __user * argp = (void __user *)arg;
294 memset(&cmd, 0, sizeof(struct v3_debug_cmd));
296 if (copy_from_user(&cmd, argp, sizeof(struct v3_debug_cmd))) {
297 ERROR("Error: Could not copy debug command from user space\n");
301 evt.core_id = cmd.core;
304 INFO("Debugging VM\n");
306 if (v3_deliver_debug_event(guest->v3_ctx, &evt) == -1) {
307 ERROR("Error could not deliver debug cmd\n");
313 case V3_VM_MOVE_CORE: {
314 struct v3_core_move_cmd cmd;
315 void __user * argp = (void __user *)arg;
317 memset(&cmd, 0, sizeof(struct v3_core_move_cmd));
319 if (copy_from_user(&cmd, argp, sizeof(struct v3_core_move_cmd))) {
320 WARNING("copy from user error getting migrate command...\n");
324 INFO("moving guest %s vcore %d to CPU %d\n", guest->name, cmd.vcore_id, cmd.pcore_id);
326 v3_move_vm_core(guest->v3_ctx, cmd.vcore_id, cmd.pcore_id);
331 struct vm_ctrl * ctrl = get_ctrl(guest, ioctl);
334 return ctrl->handler(guest, ioctl, arg, ctrl->priv_data);
338 WARNING("\tUnhandled ctrl cmd: %d\n", ioctl);
346 static int v3_vm_open(struct inode * inode, struct file * filp) {
347 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
348 filp->private_data = guest;
353 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
359 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
365 static struct file_operations v3_vm_fops = {
366 .owner = THIS_MODULE,
367 .unlocked_ioctl = v3_vm_ioctl,
368 .compat_ioctl = v3_vm_ioctl,
371 .write = v3_vm_write,
375 extern u32 pg_allocs;
380 int create_palacios_vm(struct v3_guest * guest) {
383 if (init_vm_extensions(guest) < 0) {
384 WARNING("palacios: failed to initialize extensions\n");
388 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
390 if (guest->v3_ctx == NULL) {
391 WARNING("palacios: failed to create vm\n");
395 NOTICE("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
397 cdev_init(&(guest->cdev), &v3_vm_fops);
399 guest->cdev.owner = THIS_MODULE;
400 guest->cdev.ops = &v3_vm_fops;
403 INFO("Adding VM device\n");
404 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
407 WARNING("Fails to add cdev\n");
411 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
412 WARNING("Fails to create device\n");
416 NOTICE("palacios: vm created at /dev/v3-vm%d\n", MINOR(guest->vm_dev));
421 cdev_del(&(guest->cdev));
423 v3_free_vm(guest->v3_ctx);
425 deinit_vm_extensions(guest);
433 int free_palacios_vm(struct v3_guest * guest) {
435 if (v3_free_vm(guest->v3_ctx)<0) {
439 device_destroy(v3_class, guest->vm_dev);
441 cdev_del(&(guest->cdev));
443 free_guest_ctrls(guest);
446 palacios_vfree(guest->img);
447 palacios_free(guest);