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;
137 ctrl = rb_entry(node, struct vm_ctrl, tree_node);
139 node = rb_next(node);
141 WARNING("Cleaning up guest ctrl that was not removed explicitly (%d)\n", ctrl->cmd);
152 extern struct class * v3_class;
155 static long v3_vm_ioctl(struct file * filp,
156 unsigned int ioctl, unsigned long arg) {
158 struct v3_guest * guest = filp->private_data;
160 INFO("V3 IOCTL %d\n", ioctl);
164 NOTICE("palacios: launching vm\n");
166 if (v3_start_vm(guest->v3_ctx, (0x1 << num_online_cpus()) - 1) < 0) {
167 WARNING("palacios: launch of vm failed\n");
174 NOTICE("Stopping VM (%s) (%p)\n", guest->name, guest);
176 if (irqs_disabled()) {
177 ERROR("WHAT!!?? IRQs are disabled??\n");
181 v3_stop_vm(guest->v3_ctx);
185 NOTICE("Pausing VM (%s)\n", guest->name);
186 v3_pause_vm(guest->v3_ctx);
189 case V3_VM_CONTINUE: {
190 NOTICE("Continuing VM (%s)\n", guest->name);
191 v3_continue_vm(guest->v3_ctx);
194 case V3_VM_SIMULATE: {
195 NOTICE("Simulating VM (%s) for %lu msecs\n", guest->name, arg);
196 v3_simulate_vm(guest->v3_ctx, arg);
201 #ifdef V3_CONFIG_CHECKPOINT
203 struct v3_chkpt_info chkpt;
204 void __user * argp = (void __user *)arg;
206 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
208 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
209 WARNING("Copy from user error getting checkpoint info\n");
213 NOTICE("Saving Guest to %s:%s\n", chkpt.store, chkpt.url);
215 if (v3_save_vm(guest->v3_ctx, chkpt.store, chkpt.url) == -1) {
216 WARNING("Error checkpointing VM state\n");
223 struct v3_chkpt_info chkpt;
224 void __user * argp = (void __user *)arg;
226 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
228 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
229 WARNING("Copy from user error getting checkpoint info\n");
233 NOTICE("Loading Guest from %s:%s\n", chkpt.store, chkpt.url);
235 if (v3_load_vm(guest->v3_ctx, chkpt.store, chkpt.url) == -1) {
236 WARNING("Error Loading VM state\n");
244 #ifdef V3_CONFIG_LIVE_MIGRATION
246 struct v3_chkpt_info chkpt_info;
247 void __user * argp = (void __user *)arg;
249 memset(&chkpt_info,0, sizeof(struct v3_chkpt_info));
251 if(copy_from_user(&chkpt_info, argp, sizeof(struct v3_chkpt_info))){
252 WARNING("Copy from user error getting checkpoint info\n");
257 NOTICE("Sending (live-migrating) Guest to %s:%s\n",chkpt_info.store, chkpt_info.url);
259 if (v3_send_vm(guest->v3_ctx, chkpt_info.store, chkpt_info.url) == -1) {
260 WARNING("Error sending VM\n");
267 case V3_VM_RECEIVE: {
268 struct v3_chkpt_info chkpt_info;
269 void __user * argp = (void __user *)arg;
271 memset(&chkpt_info,0, sizeof(struct v3_chkpt_info));
273 if(copy_from_user(&chkpt_info, argp, sizeof(struct v3_chkpt_info))){
274 WARNING("Copy from user error getting checkpoint info\n");
279 NOTICE("Receiving (live-migrating) Guest to %s:%s\n",chkpt_info.store, chkpt_info.url);
281 if (v3_receive_vm(guest->v3_ctx, chkpt_info.store, chkpt_info.url) == -1) {
282 WARNING("Error receiving VM\n");
291 struct v3_debug_cmd cmd;
292 struct v3_debug_event evt;
293 void __user * argp = (void __user *)arg;
295 memset(&cmd, 0, sizeof(struct v3_debug_cmd));
297 if (copy_from_user(&cmd, argp, sizeof(struct v3_debug_cmd))) {
298 ERROR("Error: Could not copy debug command from user space\n");
302 evt.core_id = cmd.core;
305 INFO("Debugging VM\n");
307 if (v3_deliver_debug_event(guest->v3_ctx, &evt) == -1) {
308 ERROR("Error could not deliver debug cmd\n");
314 case V3_VM_MOVE_CORE: {
315 struct v3_core_move_cmd cmd;
316 void __user * argp = (void __user *)arg;
318 memset(&cmd, 0, sizeof(struct v3_core_move_cmd));
320 if (copy_from_user(&cmd, argp, sizeof(struct v3_core_move_cmd))) {
321 WARNING("copy from user error getting migrate command...\n");
325 INFO("moving guest %s vcore %d to CPU %d\n", guest->name, cmd.vcore_id, cmd.pcore_id);
327 v3_move_vm_core(guest->v3_ctx, cmd.vcore_id, cmd.pcore_id);
332 struct vm_ctrl * ctrl = get_ctrl(guest, ioctl);
335 return ctrl->handler(guest, ioctl, arg, ctrl->priv_data);
339 WARNING("\tUnhandled ctrl cmd: %d\n", ioctl);
347 static int v3_vm_open(struct inode * inode, struct file * filp) {
348 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
349 filp->private_data = guest;
354 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
360 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
366 static struct file_operations v3_vm_fops = {
367 .owner = THIS_MODULE,
368 .unlocked_ioctl = v3_vm_ioctl,
369 .compat_ioctl = v3_vm_ioctl,
372 .write = v3_vm_write,
376 extern u32 pg_allocs;
381 int create_palacios_vm(struct v3_guest * guest) {
384 if (init_vm_extensions(guest) < 0) {
385 WARNING("palacios: failed to initialize extensions\n");
389 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
391 if (guest->v3_ctx == NULL) {
392 WARNING("palacios: failed to create vm\n");
396 NOTICE("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
398 cdev_init(&(guest->cdev), &v3_vm_fops);
400 guest->cdev.owner = THIS_MODULE;
401 guest->cdev.ops = &v3_vm_fops;
404 INFO("Adding VM device\n");
405 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
408 WARNING("Fails to add cdev\n");
412 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
413 WARNING("Fails to create device\n");
417 NOTICE("palacios: vm created at /dev/v3-vm%d\n", MINOR(guest->vm_dev));
422 cdev_del(&(guest->cdev));
424 v3_free_vm(guest->v3_ctx);
426 deinit_vm_extensions(guest);
434 int free_palacios_vm(struct v3_guest * guest) {
436 if (v3_free_vm(guest->v3_ctx)<0) {
440 device_destroy(v3_class, guest->vm_dev);
442 cdev_del(&(guest->cdev));
444 free_guest_ctrls(guest);
448 palacios_free(guest);