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 struct v3_debug_cmd cmd;
214 struct v3_debug_event evt;
215 void __user * argp = (void __user *)arg;
217 memset(&cmd, 0, sizeof(struct v3_debug_cmd));
219 if (copy_from_user(&cmd, argp, sizeof(struct v3_debug_cmd))) {
220 ERROR("Error: Could not copy debug command from user space\n");
224 evt.core_id = cmd.core;
227 INFO("Debugging VM\n");
229 if (v3_deliver_debug_event(guest->v3_ctx, &evt) == -1) {
230 ERROR("Error could not deliver debug cmd\n");
236 case V3_VM_MOVE_CORE: {
237 struct v3_core_move_cmd cmd;
238 void __user * argp = (void __user *)arg;
240 memset(&cmd, 0, sizeof(struct v3_core_move_cmd));
242 if (copy_from_user(&cmd, argp, sizeof(struct v3_core_move_cmd))) {
243 WARNING("copy from user error getting migrate command...\n");
247 INFO("moving guest %s vcore %d to CPU %d\n", guest->name, cmd.vcore_id, cmd.pcore_id);
249 v3_move_vm_core(guest->v3_ctx, cmd.vcore_id, cmd.pcore_id);
254 struct vm_ctrl * ctrl = get_ctrl(guest, ioctl);
257 return ctrl->handler(guest, ioctl, arg, ctrl->priv_data);
261 WARNING("\tUnhandled ctrl cmd: %d\n", ioctl);
269 static int v3_vm_open(struct inode * inode, struct file * filp) {
270 struct v3_guest * guest = container_of(inode->i_cdev, struct v3_guest, cdev);
271 filp->private_data = guest;
276 static ssize_t v3_vm_read(struct file * filp, char __user * buf, size_t size, loff_t * offset) {
282 static ssize_t v3_vm_write(struct file * filp, const char __user * buf, size_t size, loff_t * offset) {
288 static struct file_operations v3_vm_fops = {
289 .owner = THIS_MODULE,
290 .unlocked_ioctl = v3_vm_ioctl,
291 .compat_ioctl = v3_vm_ioctl,
294 .write = v3_vm_write,
298 extern u32 pg_allocs;
303 int create_palacios_vm(struct v3_guest * guest) {
306 init_vm_extensions(guest);
308 guest->v3_ctx = v3_create_vm(guest->img, (void *)guest, guest->name);
310 if (guest->v3_ctx == NULL) {
311 WARNING("palacios: failed to create vm\n");
316 NOTICE("Creating VM device: Major %d, Minor %d\n", MAJOR(guest->vm_dev), MINOR(guest->vm_dev));
318 cdev_init(&(guest->cdev), &v3_vm_fops);
320 guest->cdev.owner = THIS_MODULE;
321 guest->cdev.ops = &v3_vm_fops;
324 INFO("Adding VM device\n");
325 err = cdev_add(&(guest->cdev), guest->vm_dev, 1);
328 WARNING("Fails to add cdev\n");
329 v3_free_vm(guest->v3_ctx);
333 if (device_create(v3_class, NULL, guest->vm_dev, guest, "v3-vm%d", MINOR(guest->vm_dev)) == NULL){
334 WARNING("Fails to create device\n");
335 cdev_del(&(guest->cdev));
336 v3_free_vm(guest->v3_ctx);
340 NOTICE("palacios: vm created at /dev/v3-vm%d\n", MINOR(guest->vm_dev));
349 int free_palacios_vm(struct v3_guest * guest) {
351 v3_free_vm(guest->v3_ctx);
353 device_destroy(v3_class, guest->vm_dev);
355 cdev_del(&(guest->cdev));
358 palacios_free(guest);