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 u8 v3_minor_map[MAX_VMS / 8] = {[0 ... (MAX_VMS / 8) - 1] = 0};
40 struct class * v3_class = NULL;
41 static struct cdev ctrl_dev;
43 static int register_vm( void ) {
47 for (i = 0; i < sizeof(v3_minor_map); i++) {
48 if (v3_minor_map[i] != 0xff) {
49 for (j = 0; j < 8; j++) {
50 if (!(v3_minor_map[i] & (0x1 << j))) {
52 v3_minor_map[i] |= (0x1 << j);
72 static long v3_dev_ioctl(struct file * filp,
73 unsigned int ioctl, unsigned long arg) {
74 void __user * argp = (void __user *)arg;
75 printk("V3 IOCTL %d\n", ioctl);
81 struct v3_guest_img user_image;
82 struct v3_guest * guest = kmalloc(sizeof(struct v3_guest), GFP_KERNEL);
85 printk("Error allocating Kernel guest_image\n");
89 memset(guest, 0, sizeof(struct v3_guest));
91 printk("Starting V3 Guest...\n");
93 vm_minor = register_vm();
96 printk("Too many VMs are currently running\n");
100 guest->vm_dev = MKDEV(v3_major_num, vm_minor);
102 if (copy_from_user(&user_image, argp, sizeof(struct v3_guest_img))) {
103 printk("copy from user error getting guest image...\n");
107 guest->img_size = user_image.size;
109 printk("Allocating kernel memory for guest image (%llu bytes)\n", user_image.size);
110 guest->img = vmalloc(guest->img_size);
112 if (IS_ERR(guest->img)) {
113 printk("Error: Could not allocate space for guest image\n");
117 if (copy_from_user(guest->img, user_image.guest_data, guest->img_size)) {
118 printk("Error loading guest data\n");
122 strncpy(guest->name, user_image.name, 127);
124 printk("Launching VM\n");
126 INIT_LIST_HEAD(&(guest->exts));
128 init_completion(&(guest->start_done));
129 init_completion(&(guest->thread_done));
132 struct task_struct * launch_thread = NULL;
133 // At some point we're going to want to allow the user to specify a CPU mask
134 // But for now, well just launch from the local core, and rely on the global cpu mask
137 launch_thread = kthread_create(start_palacios_vm, guest, guest->name);
139 if (IS_ERR(launch_thread)) {
141 printk("Palacios error creating launch thread for vm (%s)\n", guest->name);
145 kthread_bind(launch_thread, smp_processor_id());
148 wake_up_process(launch_thread);
151 wait_for_completion(&(guest->start_done));
153 return guest->vm_dev;
156 case V3_ADD_MEMORY: {
157 struct v3_mem_region mem;
159 memset(&mem, 0, sizeof(struct v3_mem_region));
161 if (copy_from_user(&mem, argp, sizeof(struct v3_mem_region))) {
162 printk("copy from user error getting mem_region...\n");
166 printk("Adding %llu pages to Palacios memory\n", mem.num_pages);
168 if (add_palacios_memory(mem.base_addr, mem.num_pages) == -1) {
169 printk("Error adding memory to Palacios\n");
177 printk("\tUnhandled\n");
186 static struct file_operations v3_ctrl_fops = {
187 .owner = THIS_MODULE,
188 .unlocked_ioctl = v3_dev_ioctl,
189 .compat_ioctl = v3_dev_ioctl,
194 static int __init v3_init(void) {
195 dev_t dev = MKDEV(0, 0); // We dynamicallly assign the major number
202 // Initialize Palacios
207 // initialize extensions
208 init_lnx_extensions();
211 v3_class = class_create(THIS_MODULE, "vms");
212 if (IS_ERR(v3_class)) {
213 printk("Failed to register V3 VM device class\n");
214 return PTR_ERR(v3_class);
217 printk("intializing V3 Control device\n");
219 ret = alloc_chrdev_region(&dev, 0, MAX_VMS + 1, "v3vee");
222 printk("Error registering device region for V3 devices\n");
226 v3_major_num = MAJOR(dev);
228 dev = MKDEV(v3_major_num, MAX_VMS + 1);
231 printk("Creating V3 Control device: Major %d, Minor %d\n", v3_major_num, MINOR(dev));
232 cdev_init(&ctrl_dev, &v3_ctrl_fops);
233 ctrl_dev.owner = THIS_MODULE;
234 ctrl_dev.ops = &v3_ctrl_fops;
235 cdev_add(&ctrl_dev, dev, 1);
237 device_create(v3_class, NULL, dev, NULL, "v3vee");
240 printk("Error adding v3 control device\n");
249 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
251 class_destroy(v3_class);
257 static void __exit v3_exit(void) {
258 extern u32 pg_allocs;
264 // should probably try to stop any guests
268 dev_t dev = MKDEV(v3_major_num, MAX_VMS + 1);
270 printk("Removing V3 Control device\n");
275 printk("Palacios Mallocs = %d, Frees = %d\n", mallocs, frees);
276 printk("Palacios Page Allocs = %d, Page Frees = %d\n", pg_allocs, pg_frees);
278 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
282 device_destroy(v3_class, dev);
283 class_destroy(v3_class);
286 deinit_lnx_extensions();
288 palacios_deinit_mm();
290 printk("Palacios Module Mallocs = %d, Frees = %d\n", mod_allocs, mod_frees);
295 module_init(v3_init);
296 module_exit(v3_exit);
300 void * trace_malloc(size_t size, gfp_t flags) {
304 addr = kmalloc(size, flags);
310 void trace_free(const void * objp) {