2 Palacios main control interface
7 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <linux/errno.h>
10 #include <linux/percpu.h>
12 #include <linux/uaccess.h>
13 #include <linux/device.h>
14 #include <linux/cdev.h>
18 #include <linux/file.h>
19 #include <linux/spinlock.h>
20 #include <linux/kthread.h>
22 #include <linux/proc_fs.h>
24 #include <palacios/vmm.h>
30 #include "linux-exts.h"
34 MODULE_LICENSE("GPL");
37 int cpu_list[NR_CPUS] = {};
39 module_param_array(cpu_list, int, &cpu_list_len, 0644);
40 MODULE_PARM_DESC(cpu_list, "Comma-delimited list of CPUs that Palacios will run on");
46 static int v3_major_num = 0;
48 static struct v3_guest * guest_map[MAX_VMS] = {[0 ... MAX_VMS - 1] = 0};
49 static struct proc_dir_entry *dir = 0;
51 struct class * v3_class = NULL;
52 static struct cdev ctrl_dev;
54 static int register_vm(struct v3_guest * guest) {
57 for (i = 0; i < MAX_VMS; i++) {
58 if (guest_map[i] == NULL) {
69 static long v3_dev_ioctl(struct file * filp,
70 unsigned int ioctl, unsigned long arg) {
71 void __user * argp = (void __user *)arg;
72 DEBUG("V3 IOCTL %d\n", ioctl);
76 case V3_CREATE_GUEST:{
78 struct v3_guest_img user_image;
79 struct v3_guest * guest = palacios_alloc(sizeof(struct v3_guest));
82 ERROR("Palacios: Error allocating Kernel guest_image\n");
86 memset(guest, 0, sizeof(struct v3_guest));
88 INFO("Palacios: Creating V3 Guest...\n");
90 vm_minor = register_vm(guest);
93 ERROR("Palacios Error: Too many VMs are currently running\n");
97 guest->vm_dev = MKDEV(v3_major_num, vm_minor);
99 if (copy_from_user(&user_image, argp, sizeof(struct v3_guest_img))) {
100 ERROR("Palacios Error: copy from user error getting guest image...\n");
104 guest->img_size = user_image.size;
106 DEBUG("Palacios: Allocating kernel memory for guest image (%llu bytes)\n", user_image.size);
107 guest->img = vmalloc(guest->img_size);
109 if (IS_ERR(guest->img)) {
110 ERROR("Palacios Error: Could not allocate space for guest image\n");
114 if (copy_from_user(guest->img, user_image.guest_data, guest->img_size)) {
115 ERROR("Palacios: Error loading guest data\n");
119 strncpy(guest->name, user_image.name, 127);
121 INIT_LIST_HEAD(&(guest->exts));
123 if (create_palacios_vm(guest) == -1) {
124 ERROR("Palacios: Error creating guest\n");
134 guest_map[vm_minor] = NULL;
136 palacios_free(guest);
142 case V3_FREE_GUEST: {
143 unsigned long vm_idx = arg;
144 struct v3_guest * guest = guest_map[vm_idx];
147 ERROR("No VM at index %ld\n",vm_idx);
151 INFO("Freeing VM (%s) (%p)\n", guest->name, guest);
153 free_palacios_vm(guest);
154 guest_map[vm_idx] = NULL;
157 case V3_ADD_MEMORY: {
158 struct v3_mem_region mem;
160 memset(&mem, 0, sizeof(struct v3_mem_region));
162 if (copy_from_user(&mem, argp, sizeof(struct v3_mem_region))) {
163 ERROR("copy from user error getting mem_region...\n");
167 DEBUG("Adding %llu pages to Palacios memory\n", mem.num_pages);
169 if (add_palacios_memory(mem.base_addr, mem.num_pages) == -1) {
170 ERROR("Error adding memory to Palacios\n");
177 case V3_RESET_MEMORY: {
178 if (palacios_init_mm() == -1) {
179 ERROR("Error resetting Palacios memory\n");
186 struct global_ctrl * ctrl = get_global_ctrl(ioctl);
189 return ctrl->handler(ioctl, arg);
192 WARNING("\tUnhandled global ctrl cmd: %d\n", ioctl);
203 static struct file_operations v3_ctrl_fops = {
204 .owner = THIS_MODULE,
205 .unlocked_ioctl = v3_dev_ioctl,
206 .compat_ioctl = v3_dev_ioctl,
211 struct proc_dir_entry *palacios_get_procdir(void)
216 static int read_guests(char * buf, char ** start, off_t off, int count,
217 int * eof, void * data)
222 for(i = 0; i < MAX_VMS; i++) {
223 if (guest_map[i] != NULL) {
225 len += snprintf(buf+len, count-len,
226 "%s\t/dev/v3-vm%d\n",
227 guest_map[i]->name, i);
235 static int show_mem(char * buf, char ** start, off_t off, int count,
236 int * eof, void * data)
240 len = snprintf(buf,count, "%p\n", (void *)get_palacios_base_addr());
241 len += snprintf(buf+len,count-len, "%lld\n", get_palacios_num_pages());
247 static int __init v3_init(void) {
248 dev_t dev = MKDEV(0, 0); // We dynamicallly assign the major number
254 // Initialize Palacios
258 // initialize extensions
259 init_lnx_extensions();
262 v3_class = class_create(THIS_MODULE, "vms");
263 if (IS_ERR(v3_class)) {
264 ERROR("Failed to register V3 VM device class\n");
265 return PTR_ERR(v3_class);
268 INFO("intializing V3 Control device\n");
270 ret = alloc_chrdev_region(&dev, 0, MAX_VMS + 1, "v3vee");
273 ERROR("Error registering device region for V3 devices\n");
277 v3_major_num = MAJOR(dev);
279 dev = MKDEV(v3_major_num, MAX_VMS + 1);
282 DEBUG("Creating V3 Control device: Major %d, Minor %d\n", v3_major_num, MINOR(dev));
283 cdev_init(&ctrl_dev, &v3_ctrl_fops);
284 ctrl_dev.owner = THIS_MODULE;
285 ctrl_dev.ops = &v3_ctrl_fops;
286 cdev_add(&ctrl_dev, dev, 1);
288 device_create(v3_class, NULL, dev, NULL, "v3vee");
291 ERROR("Error adding v3 control device\n");
295 dir = proc_mkdir("v3vee", NULL);
297 struct proc_dir_entry *entry;
299 entry = create_proc_read_entry("v3-guests", 0444, dir,
302 INFO("/proc/v3vee/v3-guests successfully created\n");
304 ERROR("Could not create proc entry\n");
308 entry = create_proc_read_entry("v3-mem", 0444, dir,
311 INFO("/proc/v3vee/v3-mem successfully added\n");
313 ERROR("Could not create proc entry\n");
317 ERROR("Could not create proc entry\n");
324 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
326 class_destroy(v3_class);
332 static void __exit v3_exit(void) {
333 extern u32 pg_allocs;
338 struct v3_guest * guest;
342 /* Stop and free any running VMs */
343 for (i = 0; i < MAX_VMS; i++) {
344 if (guest_map[i] != NULL) {
345 guest = (struct v3_guest *)guest_map[i];
347 if (v3_stop_vm(guest->v3_ctx) < 0)
348 ERROR("Couldn't stop VM %d\n", i);
350 free_palacios_vm(guest);
355 dev = MKDEV(v3_major_num, MAX_VMS + 1);
357 INFO("Removing V3 Control device\n");
362 DEBUG("Palacios Mallocs = %d, Frees = %d\n", mallocs, frees);
363 DEBUG("Palacios Page Allocs = %d, Page Frees = %d\n", pg_allocs, pg_frees);
365 unregister_chrdev_region(MKDEV(v3_major_num, 0), MAX_VMS + 1);
369 device_destroy(v3_class, dev);
370 class_destroy(v3_class);
373 deinit_lnx_extensions();
375 palacios_deinit_mm();
377 remove_proc_entry("v3-guests", dir);
378 remove_proc_entry("v3-mem", dir);
379 remove_proc_entry("v3vee", NULL);
381 DEBUG("Palacios Module Mallocs = %d, Frees = %d\n", mod_allocs, mod_frees);
386 module_init(v3_init);
387 module_exit(v3_exit);
391 void * trace_malloc(size_t size, gfp_t flags) {
395 addr = kmalloc(size, flags);
401 void trace_free(const void * objp) {