2 Palacios main control interface
7 #include <linux/module.h>
8 #include <linux/errno.h>
9 #include <linux/percpu.h>
11 #include <linux/uaccess.h>
12 #include <linux/device.h>
13 #include <linux/cdev.h>
17 #include <linux/file.h>
18 #include <linux/spinlock.h>
19 #include <linux/kthread.h>
25 #include "linux-exts.h"
29 MODULE_LICENSE("GPL");
35 static int v3_major_num = 0;
37 static struct v3_guest * guest_map[MAX_VMS] = {[0 ... MAX_VMS - 1] = 0};
39 struct class * v3_class = NULL;
40 static struct cdev ctrl_dev;
42 static int register_vm(struct v3_guest * guest) {
45 for (i = 0; i < MAX_VMS; i++) {
46 if (guest_map[i] == NULL) {
57 static long v3_dev_ioctl(struct file * filp,
58 unsigned int ioctl, unsigned long arg) {
59 void __user * argp = (void __user *)arg;
60 printk("V3 IOCTL %d\n", ioctl);
66 struct v3_guest_img user_image;
67 struct v3_guest * guest = kmalloc(sizeof(struct v3_guest), GFP_KERNEL);
70 printk("Error allocating Kernel guest_image\n");
74 memset(guest, 0, sizeof(struct v3_guest));
76 printk("Starting V3 Guest... (%p)\n", guest);
78 vm_minor = register_vm(guest);
81 printk("Too many VMs are currently running\n");
85 guest->vm_dev = MKDEV(v3_major_num, vm_minor);
87 if (copy_from_user(&user_image, argp, sizeof(struct v3_guest_img))) {
88 printk("copy from user error getting guest image...\n");
92 guest->img_size = user_image.size;
94 printk("Allocating kernel memory for guest image (%llu bytes)\n", user_image.size);
95 guest->img = vmalloc(guest->img_size);
97 if (IS_ERR(guest->img)) {
98 printk("Error: Could not allocate space for guest image\n");
102 if (copy_from_user(guest->img, user_image.guest_data, guest->img_size)) {
103 printk("Error loading guest data\n");
107 strncpy(guest->name, user_image.name, 127);
109 printk("Launching VM\n");
111 INIT_LIST_HEAD(&(guest->exts));
113 init_completion(&(guest->start_done));
114 init_completion(&(guest->thread_done));
117 struct task_struct * launch_thread = NULL;
118 // At some point we're going to want to allow the user to specify a CPU mask
119 // But for now, well just launch from the local core, and rely on the global cpu mask
122 launch_thread = kthread_create(start_palacios_vm, guest, guest->name);
124 if (IS_ERR(launch_thread)) {
126 printk("Palacios error creating launch thread for vm (%s)\n", guest->name);
130 kthread_bind(launch_thread, smp_processor_id());
133 wake_up_process(launch_thread);
136 wait_for_completion(&(guest->start_done));
138 return guest->vm_dev;
141 case V3_STOP_GUEST: {
142 unsigned long vm_idx = arg;
143 struct v3_guest * guest = guest_map[vm_idx];
145 printk("Stopping VM idx=%d\n", vm_idx);
146 printk("Stopping VM (%s) (%p)\n", guest->name, guest);
149 if (irqs_disabled()) {
150 printk("WHAT!!?? IRQs are disabled??\n");
154 stop_palacios_vm(guest);
155 guest_map[vm_idx] = NULL;
158 case V3_ADD_MEMORY: {
159 struct v3_mem_region mem;
161 memset(&mem, 0, sizeof(struct v3_mem_region));
163 if (copy_from_user(&mem, argp, sizeof(struct v3_mem_region))) {
164 printk("copy from user error getting mem_region...\n");
168 printk("Adding %llu pages to Palacios memory\n", mem.num_pages);
170 if (add_palacios_memory(mem.base_addr, mem.num_pages) == -1) {
171 printk("Error adding memory to Palacios\n");
179 printk("\tUnhandled\n");
188 static struct file_operations v3_ctrl_fops = {
189 .owner = THIS_MODULE,
190 .unlocked_ioctl = v3_dev_ioctl,
191 .compat_ioctl = v3_dev_ioctl,
196 static int __init v3_init(void) {
197 dev_t dev = MKDEV(0, 0); // We dynamicallly assign the major number
204 // Initialize Palacios
209 // initialize extensions
210 init_lnx_extensions();
213 v3_class = class_create(THIS_MODULE, "vms");
214 if (IS_ERR(v3_class)) {
215 printk("Failed to register V3 VM device class\n");
216 return PTR_ERR(v3_class);
219 printk("intializing V3 Control device\n");
221 ret = alloc_chrdev_region(&dev, 0, MAX_VMS + 1, "v3vee");
224 printk("Error registering device region for V3 devices\n");
228 v3_major_num = MAJOR(dev);
230 dev = MKDEV(v3_major_num, MAX_VMS + 1);
233 printk("Creating V3 Control device: Major %d, Minor %d\n", v3_major_num, MINOR(dev));
234 cdev_init(&ctrl_dev, &v3_ctrl_fops);
235 ctrl_dev.owner = THIS_MODULE;
236 ctrl_dev.ops = &v3_ctrl_fops;
237 cdev_add(&ctrl_dev, dev, 1);
239 device_create(v3_class, NULL, dev, NULL, "v3vee");
242 printk("Error adding v3 control device\n");
251 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
253 class_destroy(v3_class);
259 static void __exit v3_exit(void) {
260 extern u32 pg_allocs;
266 // should probably try to stop any guests
270 dev_t dev = MKDEV(v3_major_num, MAX_VMS + 1);
272 printk("Removing V3 Control device\n");
277 printk("Palacios Mallocs = %d, Frees = %d\n", mallocs, frees);
278 printk("Palacios Page Allocs = %d, Page Frees = %d\n", pg_allocs, pg_frees);
280 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
284 device_destroy(v3_class, dev);
285 class_destroy(v3_class);
288 deinit_lnx_extensions();
290 palacios_deinit_mm();
292 printk("Palacios Module Mallocs = %d, Frees = %d\n", mod_allocs, mod_frees);
297 module_init(v3_init);
298 module_exit(v3_exit);
302 void * trace_malloc(size_t size, gfp_t flags) {
306 addr = kmalloc(size, flags);
312 void trace_free(const void * objp) {