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));
96 static struct vm_ctrl * get_ctrl(struct v3_guest * guest, unsigned int cmd) {
97 struct rb_node * n = guest->vm_ctrls.rb_node;
98 struct vm_ctrl * ctrl = NULL;
101 ctrl = rb_entry(n, struct vm_ctrl, tree_node);
103 if (cmd < ctrl->cmd) {
105 } else if (cmd > ctrl->cmd) {
121 extern struct class * v3_class;
124 static long v3_vm_ioctl(struct file * filp,
125 unsigned int ioctl, unsigned long arg) {
127 struct v3_guest * guest = filp->private_data;
129 INFO("V3 IOCTL %d\n", ioctl);
133 NOTICE("palacios: launching vm\n");
135 if (v3_start_vm(guest->v3_ctx, (0x1 << num_online_cpus()) - 1) < 0) {
136 WARNING("palacios: launch of vm failed\n");
143 NOTICE("Stopping VM (%s) (%p)\n", guest->name, guest);
145 if (irqs_disabled()) {
146 ERROR("WHAT!!?? IRQs are disabled??\n");
150 v3_stop_vm(guest->v3_ctx);
154 NOTICE("Pausing VM (%s)\n", guest->name);
155 v3_pause_vm(guest->v3_ctx);
158 case V3_VM_CONTINUE: {
159 NOTICE("Continuing VM (%s)\n", guest->name);
160 v3_continue_vm(guest->v3_ctx);
163 case V3_VM_SIMULATE: {
164 NOTICE("Simulating VM (%s) for %lu msecs\n", guest->name, arg);
165 v3_simulate_vm(guest->v3_ctx, arg);
170 #ifdef V3_CONFIG_CHECKPOINT
172 struct v3_chkpt_info chkpt;
173 void __user * argp = (void __user *)arg;
175 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
177 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
178 WARNING("Copy from user error getting checkpoint info\n");
182 NOTICE("Saving Guest to %s:%s\n", chkpt.store, chkpt.url);
184 if (v3_save_vm(guest->v3_ctx, chkpt.store, chkpt.url) == -1) {
185 WARNING("Error checkpointing VM state\n");
192 struct v3_chkpt_info chkpt;
193 void __user * argp = (void __user *)arg;
195 memset(&chkpt, 0, sizeof(struct v3_chkpt_info));
197 if (copy_from_user(&chkpt, argp, sizeof(struct v3_chkpt_info))) {
198 WARNING("Copy from user error getting checkpoint info\n");
202 NOTICE("Loading Guest to %s:%s\n", chkpt.store, chkpt.url);
204 if (v3_load_vm(guest->v3_ctx, chkpt.store, chkpt.url) == -1) {
205 WARNING("Error Loading VM state\n");
213 #ifdef V3_CONFIG_LIVE_MIGRATION
215 struct v3_chkpt_info chkpt_info;
216 void __user * argp = (void __user *)arg;
218 memset(&chkpt_info,0, sizeof(struct v3_chkpt_info));
220 if(copy_from_user(&chkpt_info, argp, sizeof(struct v3_chkpt_info))){
221 WARNING("Copy from user error getting checkpoint info\n");
226 NOTICE("Sending (live-migrating) Guest to %s:%s\n",chkpt_info.store, chkpt_info.url);
228 if (v3_send_vm(guest->v3_ctx, chkpt_info.store, chkpt_info.url) == -1) {
229 WARNING("Error sending VM\n");
236 case V3_VM_RECEIVE: {
237 struct v3_chkpt_info chkpt_info;
238 void __user * argp = (void __user *)arg;
240 memset(&chkpt_info,0, sizeof(struct v3_chkpt_info));
242 if(copy_from_user(&chkpt_info, argp, sizeof(struct v3_chkpt_info))){
243 WARNING("Copy from user error getting checkpoint info\n");
248 NOTICE("Receiving (live-migrating) Guest to %s:%s\n",chkpt_info.store, chkpt_info.url);
250 if (v3_receive_vm(guest->v3_ctx, chkpt_info.store, chkpt_info.url) == -1) {
251 WARNING("Error receiving VM\n");
260 struct v3_debug_cmd cmd;
261 struct v3_debug_event evt;
262 void __user * argp = (void __user *)arg;
264 memset(&cmd, 0, sizeof(struct v3_debug_cmd));
266 if (copy_from_user(&cmd, argp, sizeof(struct v3_debug_cmd))) {
267 ERROR("Error: Could not copy debug command from user space\n");
271 evt.core_id = cmd.core;
274 INFO("Debugging VM\n");
276 if (v3_deliver_debug_event(guest->v3_ctx, &evt) == -1) {
277 ERROR("Error could not deliver debug cmd\n");
283 case V3_VM_MOVE_CORE: {
284 struct v3_core_move_cmd cmd;
285 void __user * argp = (void __user *)arg;
287 memset(&cmd, 0, sizeof(struct v3_core_move_cmd));
289 if (copy_from_user(&cmd, argp, sizeof(struct v3_core_move_cmd))) {
290 WARNING("copy from user error getting migrate command...\n");
294 INFO("moving guest %s vcore %d to CPU %d\n", guest->name, cmd.vcore_id, cmd.pcore_id);
296 v3_move_vm_core(guest->v3_ctx, cmd.vcore_id, cmd.pcore_id);
301 struct vm_ctrl * ctrl = get_ctrl(guest, ioctl);
304 return ctrl->handler(guest, ioctl, arg, ctrl->priv_data);
308 WARNING("\tUnhandled ctrl cmd: %d\n", ioctl);
316 static int v3_vm_open(struct inode * inode, struct file * filp) {
317 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
318 filp->private_data = guest;
323 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
329 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
335 static struct file_operations v3_vm_fops = {
336 .owner = THIS_MODULE,
337 .unlocked_ioctl = v3_vm_ioctl,
338 .compat_ioctl = v3_vm_ioctl,
341 .write = v3_vm_write,
345 extern u32 pg_allocs;
350 int create_palacios_vm(struct v3_guest * guest) {
353 if (init_vm_extensions(guest) < 0) {
354 WARNING("palacios: failed to initialize extensions\n");
358 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
360 if (guest->v3_ctx == NULL) {
361 WARNING("palacios: failed to create vm\n");
365 NOTICE("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
367 cdev_init(&(guest->cdev), &v3_vm_fops);
369 guest->cdev.owner = THIS_MODULE;
370 guest->cdev.ops = &v3_vm_fops;
373 INFO("Adding VM device\n");
374 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
377 WARNING("Fails to add cdev\n");
381 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
382 WARNING("Fails to create device\n");
386 NOTICE("palacios: vm created at /dev/v3-vm%d\n", MINOR(guest->vm_dev));
391 cdev_del(&(guest->cdev));
393 v3_free_vm(guest->v3_ctx);
395 deinit_vm_extensions(guest);
403 int free_palacios_vm(struct v3_guest * guest) {
405 v3_free_vm(guest->v3_ctx);
407 device_destroy(v3_class, guest->vm_dev);
409 cdev_del(&(guest->cdev));
412 palacios_free(guest);