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>
22 #include <palacios/vmm_host_events.h>
26 #include "linux-exts.h"
32 int (*handler)(struct v3_guest * guest,
33 unsigned int cmd, unsigned long arg,
38 struct rb_node tree_node;
42 static inline struct vm_ctrl * __insert_ctrl(struct v3_guest * vm,
43 struct vm_ctrl * ctrl) {
44 struct rb_node ** p = &(vm->vm_ctrls.rb_node);
45 struct rb_node * parent = NULL;
46 struct vm_ctrl * tmp_ctrl = NULL;
50 tmp_ctrl = rb_entry(parent, struct vm_ctrl, tree_node);
52 if (ctrl->cmd < tmp_ctrl->cmd) {
54 } else if (ctrl->cmd > tmp_ctrl->cmd) {
61 rb_link_node(&(ctrl->tree_node), parent, p);
68 int add_guest_ctrl(struct v3_guest * guest, unsigned int cmd,
69 int (*handler)(struct v3_guest * guest,
70 unsigned int cmd, unsigned long arg,
73 struct vm_ctrl * ctrl = palacios_alloc(sizeof(struct vm_ctrl));
76 WARNING("Error: Could not allocate vm ctrl %d\n", cmd);
81 ctrl->handler = handler;
82 ctrl->priv_data = priv_data;
84 if (__insert_ctrl(guest, ctrl) != NULL) {
85 WARNING("Could not insert guest ctrl %d\n", cmd);
90 rb_insert_color(&(ctrl->tree_node), &(guest->vm_ctrls));
98 static struct vm_ctrl * get_ctrl(struct v3_guest * guest, unsigned int cmd) {
99 struct rb_node * n = guest->vm_ctrls.rb_node;
100 struct vm_ctrl * ctrl = NULL;
103 ctrl = rb_entry(n, struct vm_ctrl, tree_node);
105 if (cmd < ctrl->cmd) {
107 } else if (cmd > ctrl->cmd) {
117 int remove_guest_ctrl(struct v3_guest * guest, unsigned int cmd) {
118 struct vm_ctrl * ctrl = get_ctrl(guest, cmd);
121 INFO("Could not find control (%d) to remove\n", cmd);
125 rb_erase(&(ctrl->tree_node), &(guest->vm_ctrls));
132 static void free_guest_ctrls(struct v3_guest * guest) {
133 struct rb_node * node = rb_first(&(guest->vm_ctrls));
134 struct vm_ctrl * ctrl = NULL;
135 struct rb_node * tmp_node = NULL;
138 ctrl = rb_entry(node, struct vm_ctrl, tree_node);
140 node = rb_next(node);
142 WARNING("Cleaning up guest ctrl that was not removed explicitly (%d)\n", ctrl->cmd);
153 extern struct class * v3_class;
156 static long v3_vm_ioctl(struct file * filp,
157 unsigned int ioctl, unsigned long arg) {
159 struct v3_guest * guest = filp->private_data;
161 INFO("V3 IOCTL %d\n", ioctl);
165 NOTICE("palacios: launching vm\n");
167 if (v3_start_vm(guest->v3_ctx, (0x1 << num_online_cpus()) - 1) < 0) {
168 WARNING("palacios: launch of vm failed\n");
175 NOTICE("Stopping VM (%s) (%p)\n", guest->name, guest);
177 if (irqs_disabled()) {
178 ERROR("WHAT!!?? IRQs are disabled??\n");
182 v3_stop_vm(guest->v3_ctx);
186 NOTICE("Pausing VM (%s)\n", guest->name);
187 v3_pause_vm(guest->v3_ctx);
190 case V3_VM_CONTINUE: {
191 NOTICE("Continuing VM (%s)\n", guest->name);
192 v3_continue_vm(guest->v3_ctx);
195 case V3_VM_SIMULATE: {
196 NOTICE("Simulating VM (%s) for %lu msecs\n", guest->name, arg);
197 v3_simulate_vm(guest->v3_ctx, arg);
202 #ifdef V3_CONFIG_CHECKPOINT
204 struct v3_chkpt_info chkpt;
205 void __user * argp = (void __user *)arg;
207 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
209 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
210 WARNING("Copy from user error getting checkpoint info\n");
214 NOTICE("Saving Guest to %s:%s\n", chkpt.store, chkpt.url);
216 if (v3_save_vm(guest->v3_ctx, chkpt.store, chkpt.url) == -1) {
217 WARNING("Error checkpointing VM state\n");
224 struct v3_chkpt_info chkpt;
225 void __user * argp = (void __user *)arg;
227 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
229 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
230 WARNING("Copy from user error getting checkpoint info\n");
234 NOTICE("Loading Guest to %s:%s\n", chkpt.store, chkpt.url);
236 if (v3_load_vm(guest->v3_ctx, chkpt.store, chkpt.url) == -1) {
237 WARNING("Error Loading VM state\n");
245 #ifdef V3_CONFIG_LIVE_MIGRATION
247 struct v3_chkpt_info chkpt_info;
248 void __user * argp = (void __user *)arg;
250 memset(&chkpt_info,0, sizeof(struct v3_chkpt_info));
252 if(copy_from_user(&chkpt_info, argp, sizeof(struct v3_chkpt_info))){
253 WARNING("Copy from user error getting checkpoint info\n");
258 NOTICE("Sending (live-migrating) Guest to %s:%s\n",chkpt_info.store, chkpt_info.url);
260 if (v3_send_vm(guest->v3_ctx, chkpt_info.store, chkpt_info.url) == -1) {
261 WARNING("Error sending VM\n");
268 case V3_VM_RECEIVE: {
269 struct v3_chkpt_info chkpt_info;
270 void __user * argp = (void __user *)arg;
272 memset(&chkpt_info,0, sizeof(struct v3_chkpt_info));
274 if(copy_from_user(&chkpt_info, argp, sizeof(struct v3_chkpt_info))){
275 WARNING("Copy from user error getting checkpoint info\n");
280 NOTICE("Receiving (live-migrating) Guest to %s:%s\n",chkpt_info.store, chkpt_info.url);
282 if (v3_receive_vm(guest->v3_ctx, chkpt_info.store, chkpt_info.url) == -1) {
283 WARNING("Error receiving VM\n");
292 struct v3_debug_cmd cmd;
293 struct v3_debug_event evt;
294 void __user * argp = (void __user *)arg;
296 memset(&cmd, 0, sizeof(struct v3_debug_cmd));
298 if (copy_from_user(&cmd, argp, sizeof(struct v3_debug_cmd))) {
299 ERROR("Error: Could not copy debug command from user space\n");
303 evt.core_id = cmd.core;
306 INFO("Debugging VM\n");
308 if (v3_deliver_debug_event(guest->v3_ctx, &evt) == -1) {
309 ERROR("Error could not deliver debug cmd\n");
315 case V3_VM_MOVE_CORE: {
316 struct v3_core_move_cmd cmd;
317 void __user * argp = (void __user *)arg;
319 memset(&cmd, 0, sizeof(struct v3_core_move_cmd));
321 if (copy_from_user(&cmd, argp, sizeof(struct v3_core_move_cmd))) {
322 WARNING("copy from user error getting migrate command...\n");
326 INFO("moving guest %s vcore %d to CPU %d\n", guest->name, cmd.vcore_id, cmd.pcore_id);
328 v3_move_vm_core(guest->v3_ctx, cmd.vcore_id, cmd.pcore_id);
333 struct vm_ctrl * ctrl = get_ctrl(guest, ioctl);
336 return ctrl->handler(guest, ioctl, arg, ctrl->priv_data);
340 WARNING("\tUnhandled ctrl cmd: %d\n", ioctl);
348 static int v3_vm_open(struct inode * inode, struct file * filp) {
349 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
350 filp->private_data = guest;
355 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
361 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
367 static struct file_operations v3_vm_fops = {
368 .owner = THIS_MODULE,
369 .unlocked_ioctl = v3_vm_ioctl,
370 .compat_ioctl = v3_vm_ioctl,
373 .write = v3_vm_write,
377 extern u32 pg_allocs;
382 int create_palacios_vm(struct v3_guest * guest) {
385 if (init_vm_extensions(guest) < 0) {
386 WARNING("palacios: failed to initialize extensions\n");
390 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
392 if (guest->v3_ctx == NULL) {
393 WARNING("palacios: failed to create vm\n");
397 NOTICE("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
399 cdev_init(&(guest->cdev), &v3_vm_fops);
401 guest->cdev.owner = THIS_MODULE;
402 guest->cdev.ops = &v3_vm_fops;
405 INFO("Adding VM device\n");
406 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
409 WARNING("Fails to add cdev\n");
413 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
414 WARNING("Fails to create device\n");
418 NOTICE("palacios: vm created at /dev/v3-vm%d\n", MINOR(guest->vm_dev));
423 cdev_del(&(guest->cdev));
425 v3_free_vm(guest->v3_ctx);
427 deinit_vm_extensions(guest);
435 int free_palacios_vm(struct v3_guest * guest) {
437 v3_free_vm(guest->v3_ctx);
439 device_destroy(v3_class, guest->vm_dev);
441 cdev_del(&(guest->cdev));
443 free_guest_ctrls(guest);
447 palacios_free(guest);